[multiple changes]
[gcc.git] / gcc / ada / sem_ch6.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
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 --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Itypes; use Itypes;
44 with Lib.Xref; use Lib.Xref;
45 with Layout; use Layout;
46 with Namet; use Namet;
47 with Lib; use Lib;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Output; use Output;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch5; use Sem_Ch5;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch10; use Sem_Ch10;
63 with Sem_Ch12; use Sem_Ch12;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Prag; use Sem_Prag;
72 with Sem_Res; use Sem_Res;
73 with Sem_Util; use Sem_Util;
74 with Sem_Type; use Sem_Type;
75 with Sem_Warn; use Sem_Warn;
76 with Sinput; use Sinput;
77 with Stand; use Stand;
78 with Sinfo; use Sinfo;
79 with Sinfo.CN; use Sinfo.CN;
80 with Snames; use Snames;
81 with Stringt; use Stringt;
82 with Style;
83 with Stylesw; use Stylesw;
84 with Targparm; use Targparm;
85 with Tbuild; use Tbuild;
86 with Uintp; use Uintp;
87 with Urealp; use Urealp;
88 with Validsw; use Validsw;
89
90 package body Sem_Ch6 is
91
92 May_Hide_Profile : Boolean := False;
93 -- This flag is used to indicate that two formals in two subprograms being
94 -- checked for conformance differ only in that one is an access parameter
95 -- while the other is of a general access type with the same designated
96 -- type. In this case, if the rest of the signatures match, a call to
97 -- either subprogram may be ambiguous, which is worth a warning. The flag
98 -- is set in Compatible_Types, and the warning emitted in
99 -- New_Overloaded_Entity.
100
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
104
105 procedure Analyze_Null_Procedure
106 (N : Node_Id;
107 Is_Completion : out Boolean);
108 -- A null procedure can be a declaration or (Ada 2012) a completion.
109
110 procedure Analyze_Return_Statement (N : Node_Id);
111 -- Common processing for simple and extended return statements
112
113 procedure Analyze_Function_Return (N : Node_Id);
114 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
115 -- applies to a [generic] function.
116
117 procedure Analyze_Return_Type (N : Node_Id);
118 -- Subsidiary to Process_Formals: analyze subtype mark in function
119 -- specification in a context where the formals are visible and hide
120 -- outer homographs.
121
122 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
123 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
124 -- that we can use RETURN but not skip the debug output at the end.
125
126 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
127 -- Analyze a generic subprogram body. N is the body to be analyzed, and
128 -- Gen_Id is the defining entity Id for the corresponding spec.
129
130 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
131 -- If a subprogram has pragma Inline and inlining is active, use generic
132 -- machinery to build an unexpanded body for the subprogram. This body is
133 -- subsequently used for inline expansions at call sites. If subprogram can
134 -- be inlined (depending on size and nature of local declarations) this
135 -- function returns true. Otherwise subprogram body is treated normally.
136 -- If proper warnings are enabled and the subprogram contains a construct
137 -- that cannot be inlined, the offending construct is flagged accordingly.
138
139 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
140 -- Returns true if Subp can override a predefined operator.
141
142 procedure Check_And_Build_Body_To_Inline
143 (N : Node_Id;
144 Spec_Id : Entity_Id;
145 Body_Id : Entity_Id);
146 -- Spec_Id and Body_Id are the entities of the specification and body of
147 -- the subprogram body N. If N can be inlined by the frontend (supported
148 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
149 -- associated with N and attach it to the declaration node of Spec_Id.
150
151 procedure Check_Conformance
152 (New_Id : Entity_Id;
153 Old_Id : Entity_Id;
154 Ctype : Conformance_Type;
155 Errmsg : Boolean;
156 Conforms : out Boolean;
157 Err_Loc : Node_Id := Empty;
158 Get_Inst : Boolean := False;
159 Skip_Controlling_Formals : Boolean := False);
160 -- Given two entities, this procedure checks that the profiles associated
161 -- with these entities meet the conformance criterion given by the third
162 -- parameter. If they conform, Conforms is set True and control returns
163 -- to the caller. If they do not conform, Conforms is set to False, and
164 -- in addition, if Errmsg is True on the call, proper messages are output
165 -- to complain about the conformance failure. If Err_Loc is non_Empty
166 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
167 -- error messages are placed on the appropriate part of the construct
168 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
169 -- against a formal access-to-subprogram type so Get_Instance_Of must
170 -- be called.
171
172 procedure Check_Subprogram_Order (N : Node_Id);
173 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
174 -- the alpha ordering rule for N if this ordering requirement applicable.
175
176 procedure Check_Returns
177 (HSS : Node_Id;
178 Mode : Character;
179 Err : out Boolean;
180 Proc : Entity_Id := Empty);
181 -- Called to check for missing return statements in a function body, or for
182 -- returns present in a procedure body which has No_Return set. HSS is the
183 -- handled statement sequence for the subprogram body. This procedure
184 -- checks all flow paths to make sure they either have return (Mode = 'F',
185 -- used for functions) or do not have a return (Mode = 'P', used for
186 -- No_Return procedures). The flag Err is set if there are any control
187 -- paths not explicitly terminated by a return in the function case, and is
188 -- True otherwise. Proc is the entity for the procedure case and is used
189 -- in posting the warning message.
190
191 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
192 -- In Ada 2012, a primitive equality operator on an untagged record type
193 -- must appear before the type is frozen, and have the same visibility as
194 -- that of the type. This procedure checks that this rule is met, and
195 -- otherwise emits an error on the subprogram declaration and a warning
196 -- on the earlier freeze point if it is easy to locate.
197
198 procedure Enter_Overloaded_Entity (S : Entity_Id);
199 -- This procedure makes S, a new overloaded entity, into the first visible
200 -- entity with that name.
201
202 function Is_Non_Overriding_Operation
203 (Prev_E : Entity_Id;
204 New_E : Entity_Id) return Boolean;
205 -- Enforce the rule given in 12.3(18): a private operation in an instance
206 -- overrides an inherited operation only if the corresponding operation
207 -- was overriding in the generic. This needs to be checked for primitive
208 -- operations of types derived (in the generic unit) from formal private
209 -- or formal derived types.
210
211 procedure Make_Inequality_Operator (S : Entity_Id);
212 -- Create the declaration for an inequality operator that is implicitly
213 -- created by a user-defined equality operator that yields a boolean.
214
215 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
216 -- Formal_Id is an formal parameter entity. This procedure deals with
217 -- setting the proper validity status for this entity, which depends on
218 -- the kind of parameter and the validity checking mode.
219
220 ---------------------------------------------
221 -- Analyze_Abstract_Subprogram_Declaration --
222 ---------------------------------------------
223
224 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
225 Designator : constant Entity_Id :=
226 Analyze_Subprogram_Specification (Specification (N));
227 Scop : constant Entity_Id := Current_Scope;
228
229 begin
230 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
231
232 Generate_Definition (Designator);
233 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
234 Set_Is_Abstract_Subprogram (Designator);
235 New_Overloaded_Entity (Designator);
236 Check_Delayed_Subprogram (Designator);
237
238 Set_Categorization_From_Scope (Designator, Scop);
239
240 if Ekind (Scope (Designator)) = E_Protected_Type then
241 Error_Msg_N
242 ("abstract subprogram not allowed in protected type", N);
243
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
247
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation (Designator)
250 and then not Present (Overridden_Operation (Designator))
251 and then (not Is_Operator_Symbol_Name (Chars (Designator))
252 or else Scop /= Scope (Etype (First_Formal (Designator))))
253 then
254 Error_Msg_N
255 ("abstract subprogram is not dispatching or overriding?r?", N);
256 end if;
257
258 Generate_Reference_To_Formals (Designator);
259 Check_Eliminated (Designator);
260
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Designator);
263 end if;
264 end Analyze_Abstract_Subprogram_Declaration;
265
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
269
270 procedure Analyze_Expression_Function (N : Node_Id) is
271 Loc : constant Source_Ptr := Sloc (N);
272 LocX : constant Source_Ptr := Sloc (Expression (N));
273 Expr : constant Node_Id := Expression (N);
274 Spec : constant Node_Id := Specification (N);
275
276 Def_Id : Entity_Id;
277
278 Prev : Entity_Id;
279 -- If the expression is a completion, Prev is the entity whose
280 -- declaration is completed. Def_Id is needed to analyze the spec.
281
282 New_Body : Node_Id;
283 New_Decl : Node_Id;
284 New_Spec : Node_Id;
285 Ret : Node_Id;
286
287 begin
288 -- This is one of the occasions on which we transform the tree during
289 -- semantic analysis. If this is a completion, transform the expression
290 -- function into an equivalent subprogram body, and analyze it.
291
292 -- Expression functions are inlined unconditionally. The back-end will
293 -- determine whether this is possible.
294
295 Inline_Processing_Required := True;
296
297 -- Create a specification for the generated body. Types and defauts in
298 -- the profile are copies of the spec, but new entities must be created
299 -- for the unit name and the formals.
300
301 New_Spec := New_Copy_Tree (Spec);
302 Set_Defining_Unit_Name (New_Spec,
303 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
304 Chars (Defining_Unit_Name (Spec))));
305
306 if Present (Parameter_Specifications (New_Spec)) then
307 declare
308 Formal_Spec : Node_Id;
309 begin
310 Formal_Spec := First (Parameter_Specifications (New_Spec));
311 while Present (Formal_Spec) loop
312 Set_Defining_Identifier
313 (Formal_Spec,
314 Make_Defining_Identifier (Sloc (Formal_Spec),
315 Chars => Chars (Defining_Identifier (Formal_Spec))));
316 Next (Formal_Spec);
317 end loop;
318 end;
319 end if;
320
321 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
322
323 -- If there are previous overloadable entities with the same name,
324 -- check whether any of them is completed by the expression function.
325
326 if Present (Prev) and then Is_Overloadable (Prev) then
327 Def_Id := Analyze_Subprogram_Specification (Spec);
328 Prev := Find_Corresponding_Spec (N);
329 end if;
330
331 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
332
333 New_Body :=
334 Make_Subprogram_Body (Loc,
335 Specification => New_Spec,
336 Declarations => Empty_List,
337 Handled_Statement_Sequence =>
338 Make_Handled_Sequence_Of_Statements (LocX,
339 Statements => New_List (Ret)));
340
341 -- If the expression completes a generic subprogram, we must create a
342 -- separate node for the body, because at instantiation the original
343 -- node of the generic copy must be a generic subprogram body, and
344 -- cannot be a expression function. Otherwise we just rewrite the
345 -- expression with the non-generic body.
346
347 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
348 Insert_After (N, New_Body);
349
350 -- Propagate any aspects or pragmas that apply to the expression
351 -- function to the proper body when the expression function acts
352 -- as a completion.
353
354 if Has_Aspects (N) then
355 Move_Aspects (N, To => New_Body);
356 end if;
357
358 Relocate_Pragmas_To_Body (New_Body);
359
360 Rewrite (N, Make_Null_Statement (Loc));
361 Set_Has_Completion (Prev, False);
362 Analyze (N);
363 Analyze (New_Body);
364 Set_Is_Inlined (Prev);
365
366 elsif Present (Prev) and then Comes_From_Source (Prev) then
367 Set_Has_Completion (Prev, False);
368
369 -- For navigation purposes, indicate that the function is a body
370
371 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
372 Rewrite (N, New_Body);
373
374 -- Propagate any pragmas that apply to the expression function to the
375 -- proper body when the expression function acts as a completion.
376 -- Aspects are automatically transfered because of node rewriting.
377
378 Relocate_Pragmas_To_Body (N);
379 Analyze (N);
380
381 -- Prev is the previous entity with the same name, but it is can
382 -- be an unrelated spec that is not completed by the expression
383 -- function. In that case the relevant entity is the one in the body.
384 -- Not clear that the backend can inline it in this case ???
385
386 if Has_Completion (Prev) then
387 Set_Is_Inlined (Prev);
388
389 -- The formals of the expression function are body formals,
390 -- and do not appear in the ali file, which will only contain
391 -- references to the formals of the original subprogram spec.
392
393 declare
394 F1 : Entity_Id;
395 F2 : Entity_Id;
396
397 begin
398 F1 := First_Formal (Def_Id);
399 F2 := First_Formal (Prev);
400
401 while Present (F1) loop
402 Set_Spec_Entity (F1, F2);
403 Next_Formal (F1);
404 Next_Formal (F2);
405 end loop;
406 end;
407
408 else
409 Set_Is_Inlined (Defining_Entity (New_Body));
410 end if;
411
412 -- If this is not a completion, create both a declaration and a body, so
413 -- that the expression can be inlined whenever possible.
414
415 else
416 -- An expression function that is not a completion is not a
417 -- subprogram declaration, and thus cannot appear in a protected
418 -- definition.
419
420 if Nkind (Parent (N)) = N_Protected_Definition then
421 Error_Msg_N
422 ("an expression function is not a legal protected operation", N);
423 end if;
424
425 New_Decl :=
426 Make_Subprogram_Declaration (Loc, Specification => Spec);
427
428 Rewrite (N, New_Decl);
429 Analyze (N);
430 Set_Is_Inlined (Defining_Entity (New_Decl));
431
432 -- To prevent premature freeze action, insert the new body at the end
433 -- of the current declarations, or at the end of the package spec.
434 -- However, resolve usage names now, to prevent spurious visibility
435 -- on later entities.
436
437 declare
438 Decls : List_Id := List_Containing (N);
439 Par : constant Node_Id := Parent (Decls);
440 Id : constant Entity_Id := Defining_Entity (New_Decl);
441
442 begin
443 if Nkind (Par) = N_Package_Specification
444 and then Decls = Visible_Declarations (Par)
445 and then Present (Private_Declarations (Par))
446 and then not Is_Empty_List (Private_Declarations (Par))
447 then
448 Decls := Private_Declarations (Par);
449 end if;
450
451 Insert_After (Last (Decls), New_Body);
452 Push_Scope (Id);
453 Install_Formals (Id);
454
455 -- Preanalyze the expression for name capture, except in an
456 -- instance, where this has been done during generic analysis,
457 -- and will be redone when analyzing the body.
458
459 declare
460 Expr : constant Node_Id := Expression (Ret);
461
462 begin
463 Set_Parent (Expr, Ret);
464
465 if not In_Instance then
466 Preanalyze_Spec_Expression (Expr, Etype (Id));
467 end if;
468 end;
469
470 End_Scope;
471 end;
472 end if;
473
474 -- If the return expression is a static constant, we suppress warning
475 -- messages on unused formals, which in most cases will be noise.
476
477 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
478 Is_OK_Static_Expression (Expr));
479 end Analyze_Expression_Function;
480
481 ----------------------------------------
482 -- Analyze_Extended_Return_Statement --
483 ----------------------------------------
484
485 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
486 begin
487 Analyze_Return_Statement (N);
488 end Analyze_Extended_Return_Statement;
489
490 ----------------------------
491 -- Analyze_Function_Call --
492 ----------------------------
493
494 procedure Analyze_Function_Call (N : Node_Id) is
495 Actuals : constant List_Id := Parameter_Associations (N);
496 Func_Nam : constant Node_Id := Name (N);
497 Actual : Node_Id;
498
499 begin
500 Analyze (Func_Nam);
501
502 -- A call of the form A.B (X) may be an Ada 2005 call, which is
503 -- rewritten as B (A, X). If the rewriting is successful, the call
504 -- has been analyzed and we just return.
505
506 if Nkind (Func_Nam) = N_Selected_Component
507 and then Name (N) /= Func_Nam
508 and then Is_Rewrite_Substitution (N)
509 and then Present (Etype (N))
510 then
511 return;
512 end if;
513
514 -- If error analyzing name, then set Any_Type as result type and return
515
516 if Etype (Func_Nam) = Any_Type then
517 Set_Etype (N, Any_Type);
518 return;
519 end if;
520
521 -- Otherwise analyze the parameters
522
523 if Present (Actuals) then
524 Actual := First (Actuals);
525 while Present (Actual) loop
526 Analyze (Actual);
527 Check_Parameterless_Call (Actual);
528 Next (Actual);
529 end loop;
530 end if;
531
532 Analyze_Call (N);
533 end Analyze_Function_Call;
534
535 -----------------------------
536 -- Analyze_Function_Return --
537 -----------------------------
538
539 procedure Analyze_Function_Return (N : Node_Id) is
540 Loc : constant Source_Ptr := Sloc (N);
541 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
542 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
543
544 R_Type : constant Entity_Id := Etype (Scope_Id);
545 -- Function result subtype
546
547 procedure Check_Limited_Return (Expr : Node_Id);
548 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
549 -- limited types. Used only for simple return statements.
550 -- Expr is the expression returned.
551
552 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
553 -- Check that the return_subtype_indication properly matches the result
554 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
555
556 --------------------------
557 -- Check_Limited_Return --
558 --------------------------
559
560 procedure Check_Limited_Return (Expr : Node_Id) is
561 begin
562 -- Ada 2005 (AI-318-02): Return-by-reference types have been
563 -- removed and replaced by anonymous access results. This is an
564 -- incompatibility with Ada 95. Not clear whether this should be
565 -- enforced yet or perhaps controllable with special switch. ???
566
567 -- A limited interface that is not immutably limited is OK.
568
569 if Is_Limited_Interface (R_Type)
570 and then
571 not (Is_Task_Interface (R_Type)
572 or else Is_Protected_Interface (R_Type)
573 or else Is_Synchronized_Interface (R_Type))
574 then
575 null;
576
577 elsif Is_Limited_Type (R_Type)
578 and then not Is_Interface (R_Type)
579 and then Comes_From_Source (N)
580 and then not In_Instance_Body
581 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
582 then
583 -- Error in Ada 2005
584
585 if Ada_Version >= Ada_2005
586 and then not Debug_Flag_Dot_L
587 and then not GNAT_Mode
588 then
589 Error_Msg_N
590 ("(Ada 2005) cannot copy object of a limited type " &
591 "(RM-2005 6.5(5.5/2))", Expr);
592
593 if Is_Limited_View (R_Type) then
594 Error_Msg_N
595 ("\return by reference not permitted in Ada 2005", Expr);
596 end if;
597
598 -- Warn in Ada 95 mode, to give folks a heads up about this
599 -- incompatibility.
600
601 -- In GNAT mode, this is just a warning, to allow it to be
602 -- evilly turned off. Otherwise it is a real error.
603
604 -- In a generic context, simplify the warning because it makes
605 -- no sense to discuss pass-by-reference or copy.
606
607 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
608 if Inside_A_Generic then
609 Error_Msg_N
610 ("return of limited object not permitted in Ada 2005 "
611 & "(RM-2005 6.5(5.5/2))?y?", Expr);
612
613 elsif Is_Limited_View (R_Type) then
614 Error_Msg_N
615 ("return by reference not permitted in Ada 2005 "
616 & "(RM-2005 6.5(5.5/2))?y?", Expr);
617 else
618 Error_Msg_N
619 ("cannot copy object of a limited type in Ada 2005 "
620 & "(RM-2005 6.5(5.5/2))?y?", Expr);
621 end if;
622
623 -- Ada 95 mode, compatibility warnings disabled
624
625 else
626 return; -- skip continuation messages below
627 end if;
628
629 if not Inside_A_Generic then
630 Error_Msg_N
631 ("\consider switching to return of access type", Expr);
632 Explain_Limited_Type (R_Type, Expr);
633 end if;
634 end if;
635 end Check_Limited_Return;
636
637 -------------------------------------
638 -- Check_Return_Subtype_Indication --
639 -------------------------------------
640
641 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
642 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
643
644 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
645 -- Subtype given in the extended return statement (must match R_Type)
646
647 Subtype_Ind : constant Node_Id :=
648 Object_Definition (Original_Node (Obj_Decl));
649
650 R_Type_Is_Anon_Access :
651 constant Boolean :=
652 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
653 or else
654 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
655 or else
656 Ekind (R_Type) = E_Anonymous_Access_Type;
657 -- True if return type of the function is an anonymous access type
658 -- Can't we make Is_Anonymous_Access_Type in einfo ???
659
660 R_Stm_Type_Is_Anon_Access :
661 constant Boolean :=
662 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
663 or else
664 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
665 or else
666 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
667 -- True if type of the return object is an anonymous access type
668
669 begin
670 -- First, avoid cascaded errors
671
672 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
673 return;
674 end if;
675
676 -- "return access T" case; check that the return statement also has
677 -- "access T", and that the subtypes statically match:
678 -- if this is an access to subprogram the signatures must match.
679
680 if R_Type_Is_Anon_Access then
681 if R_Stm_Type_Is_Anon_Access then
682 if
683 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
684 then
685 if Base_Type (Designated_Type (R_Stm_Type)) /=
686 Base_Type (Designated_Type (R_Type))
687 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
688 then
689 Error_Msg_N
690 ("subtype must statically match function result subtype",
691 Subtype_Mark (Subtype_Ind));
692 end if;
693
694 else
695 -- For two anonymous access to subprogram types, the
696 -- types themselves must be type conformant.
697
698 if not Conforming_Types
699 (R_Stm_Type, R_Type, Fully_Conformant)
700 then
701 Error_Msg_N
702 ("subtype must statically match function result subtype",
703 Subtype_Ind);
704 end if;
705 end if;
706
707 else
708 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
709 end if;
710
711 -- If the return object is of an anonymous access type, then report
712 -- an error if the function's result type is not also anonymous.
713
714 elsif R_Stm_Type_Is_Anon_Access
715 and then not R_Type_Is_Anon_Access
716 then
717 Error_Msg_N ("anonymous access not allowed for function with " &
718 "named access result", Subtype_Ind);
719
720 -- Subtype indication case: check that the return object's type is
721 -- covered by the result type, and that the subtypes statically match
722 -- when the result subtype is constrained. Also handle record types
723 -- with unknown discriminants for which we have built the underlying
724 -- record view. Coverage is needed to allow specific-type return
725 -- objects when the result type is class-wide (see AI05-32).
726
727 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
728 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
729 and then
730 Covers
731 (Base_Type (R_Type),
732 Underlying_Record_View (Base_Type (R_Stm_Type))))
733 then
734 -- A null exclusion may be present on the return type, on the
735 -- function specification, on the object declaration or on the
736 -- subtype itself.
737
738 if Is_Access_Type (R_Type)
739 and then
740 (Can_Never_Be_Null (R_Type)
741 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
742 Can_Never_Be_Null (R_Stm_Type)
743 then
744 Error_Msg_N
745 ("subtype must statically match function result subtype",
746 Subtype_Ind);
747 end if;
748
749 -- AI05-103: for elementary types, subtypes must statically match
750
751 if Is_Constrained (R_Type)
752 or else Is_Access_Type (R_Type)
753 then
754 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
755 Error_Msg_N
756 ("subtype must statically match function result subtype",
757 Subtype_Ind);
758 end if;
759 end if;
760
761 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
762 and then Is_Null_Extension (Base_Type (R_Type))
763 then
764 null;
765
766 else
767 Error_Msg_N
768 ("wrong type for return_subtype_indication", Subtype_Ind);
769 end if;
770 end Check_Return_Subtype_Indication;
771
772 ---------------------
773 -- Local Variables --
774 ---------------------
775
776 Expr : Node_Id;
777
778 -- Start of processing for Analyze_Function_Return
779
780 begin
781 Set_Return_Present (Scope_Id);
782
783 if Nkind (N) = N_Simple_Return_Statement then
784 Expr := Expression (N);
785
786 -- Guard against a malformed expression. The parser may have tried to
787 -- recover but the node is not analyzable.
788
789 if Nkind (Expr) = N_Error then
790 Set_Etype (Expr, Any_Type);
791 Expander_Mode_Save_And_Set (False);
792 return;
793
794 else
795 -- The resolution of a controlled [extension] aggregate associated
796 -- with a return statement creates a temporary which needs to be
797 -- finalized on function exit. Wrap the return statement inside a
798 -- block so that the finalization machinery can detect this case.
799 -- This early expansion is done only when the return statement is
800 -- not part of a handled sequence of statements.
801
802 if Nkind_In (Expr, N_Aggregate,
803 N_Extension_Aggregate)
804 and then Needs_Finalization (R_Type)
805 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
806 then
807 Rewrite (N,
808 Make_Block_Statement (Loc,
809 Handled_Statement_Sequence =>
810 Make_Handled_Sequence_Of_Statements (Loc,
811 Statements => New_List (Relocate_Node (N)))));
812
813 Analyze (N);
814 return;
815 end if;
816
817 Analyze_And_Resolve (Expr, R_Type);
818 Check_Limited_Return (Expr);
819 end if;
820
821 -- RETURN only allowed in SPARK as the last statement in function
822
823 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
824 and then
825 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
826 or else Present (Next (N)))
827 then
828 Check_SPARK_Restriction
829 ("RETURN should be the last statement in function", N);
830 end if;
831
832 else
833 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
834
835 -- Analyze parts specific to extended_return_statement:
836
837 declare
838 Obj_Decl : constant Node_Id :=
839 Last (Return_Object_Declarations (N));
840 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
841 HSS : constant Node_Id := Handled_Statement_Sequence (N);
842
843 begin
844 Expr := Expression (Obj_Decl);
845
846 -- Note: The check for OK_For_Limited_Init will happen in
847 -- Analyze_Object_Declaration; we treat it as a normal
848 -- object declaration.
849
850 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
851 Analyze (Obj_Decl);
852
853 Check_Return_Subtype_Indication (Obj_Decl);
854
855 if Present (HSS) then
856 Analyze (HSS);
857
858 if Present (Exception_Handlers (HSS)) then
859
860 -- ???Has_Nested_Block_With_Handler needs to be set.
861 -- Probably by creating an actual N_Block_Statement.
862 -- Probably in Expand.
863
864 null;
865 end if;
866 end if;
867
868 -- Mark the return object as referenced, since the return is an
869 -- implicit reference of the object.
870
871 Set_Referenced (Defining_Identifier (Obj_Decl));
872
873 Check_References (Stm_Entity);
874
875 -- Check RM 6.5 (5.9/3)
876
877 if Has_Aliased then
878 if Ada_Version < Ada_2012 then
879
880 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
881 -- Can it really happen (extended return???)
882
883 Error_Msg_N
884 ("aliased only allowed for limited"
885 & " return objects in Ada 2012?", N);
886
887 elsif not Is_Limited_View (R_Type) then
888 Error_Msg_N ("aliased only allowed for limited"
889 & " return objects", N);
890 end if;
891 end if;
892 end;
893 end if;
894
895 -- Case of Expr present
896
897 if Present (Expr)
898
899 -- Defend against previous errors
900
901 and then Nkind (Expr) /= N_Empty
902 and then Present (Etype (Expr))
903 then
904 -- Apply constraint check. Note that this is done before the implicit
905 -- conversion of the expression done for anonymous access types to
906 -- ensure correct generation of the null-excluding check associated
907 -- with null-excluding expressions found in return statements.
908
909 Apply_Constraint_Check (Expr, R_Type);
910
911 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
912 -- type, apply an implicit conversion of the expression to that type
913 -- to force appropriate static and run-time accessibility checks.
914
915 if Ada_Version >= Ada_2005
916 and then Ekind (R_Type) = E_Anonymous_Access_Type
917 then
918 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
919 Analyze_And_Resolve (Expr, R_Type);
920
921 -- If this is a local anonymous access to subprogram, the
922 -- accessibility check can be applied statically. The return is
923 -- illegal if the access type of the return expression is declared
924 -- inside of the subprogram (except if it is the subtype indication
925 -- of an extended return statement).
926
927 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
928 if not Comes_From_Source (Current_Scope)
929 or else Ekind (Current_Scope) = E_Return_Statement
930 then
931 null;
932
933 elsif
934 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
935 then
936 Error_Msg_N ("cannot return local access to subprogram", N);
937 end if;
938 end if;
939
940 -- If the result type is class-wide, then check that the return
941 -- expression's type is not declared at a deeper level than the
942 -- function (RM05-6.5(5.6/2)).
943
944 if Ada_Version >= Ada_2005
945 and then Is_Class_Wide_Type (R_Type)
946 then
947 if Type_Access_Level (Etype (Expr)) >
948 Subprogram_Access_Level (Scope_Id)
949 then
950 Error_Msg_N
951 ("level of return expression type is deeper than " &
952 "class-wide function!", Expr);
953 end if;
954 end if;
955
956 -- Check incorrect use of dynamically tagged expression
957
958 if Is_Tagged_Type (R_Type) then
959 Check_Dynamically_Tagged_Expression
960 (Expr => Expr,
961 Typ => R_Type,
962 Related_Nod => N);
963 end if;
964
965 -- ??? A real run-time accessibility check is needed in cases
966 -- involving dereferences of access parameters. For now we just
967 -- check the static cases.
968
969 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
970 and then Is_Limited_View (Etype (Scope_Id))
971 and then Object_Access_Level (Expr) >
972 Subprogram_Access_Level (Scope_Id)
973 then
974 -- Suppress the message in a generic, where the rewriting
975 -- is irrelevant.
976
977 if Inside_A_Generic then
978 null;
979
980 else
981 Rewrite (N,
982 Make_Raise_Program_Error (Loc,
983 Reason => PE_Accessibility_Check_Failed));
984 Analyze (N);
985
986 Error_Msg_Warn := SPARK_Mode /= On;
987 Error_Msg_N ("cannot return a local value by reference<<", N);
988 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
989 end if;
990 end if;
991
992 if Known_Null (Expr)
993 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
994 and then Null_Exclusion_Present (Parent (Scope_Id))
995 then
996 Apply_Compile_Time_Constraint_Error
997 (N => Expr,
998 Msg => "(Ada 2005) null not allowed for "
999 & "null-excluding return??",
1000 Reason => CE_Null_Not_Allowed);
1001 end if;
1002 end if;
1003 end Analyze_Function_Return;
1004
1005 -------------------------------------
1006 -- Analyze_Generic_Subprogram_Body --
1007 -------------------------------------
1008
1009 procedure Analyze_Generic_Subprogram_Body
1010 (N : Node_Id;
1011 Gen_Id : Entity_Id)
1012 is
1013 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1014 Kind : constant Entity_Kind := Ekind (Gen_Id);
1015 Body_Id : Entity_Id;
1016 New_N : Node_Id;
1017 Spec : Node_Id;
1018
1019 begin
1020 -- Copy body and disable expansion while analyzing the generic For a
1021 -- stub, do not copy the stub (which would load the proper body), this
1022 -- will be done when the proper body is analyzed.
1023
1024 if Nkind (N) /= N_Subprogram_Body_Stub then
1025 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1026 Rewrite (N, New_N);
1027 Start_Generic;
1028 end if;
1029
1030 Spec := Specification (N);
1031
1032 -- Within the body of the generic, the subprogram is callable, and
1033 -- behaves like the corresponding non-generic unit.
1034
1035 Body_Id := Defining_Entity (Spec);
1036
1037 if Kind = E_Generic_Procedure
1038 and then Nkind (Spec) /= N_Procedure_Specification
1039 then
1040 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1041 return;
1042
1043 elsif Kind = E_Generic_Function
1044 and then Nkind (Spec) /= N_Function_Specification
1045 then
1046 Error_Msg_N ("invalid body for generic function ", Body_Id);
1047 return;
1048 end if;
1049
1050 Set_Corresponding_Body (Gen_Decl, Body_Id);
1051
1052 if Has_Completion (Gen_Id)
1053 and then Nkind (Parent (N)) /= N_Subunit
1054 then
1055 Error_Msg_N ("duplicate generic body", N);
1056 return;
1057 else
1058 Set_Has_Completion (Gen_Id);
1059 end if;
1060
1061 if Nkind (N) = N_Subprogram_Body_Stub then
1062 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1063 else
1064 Set_Corresponding_Spec (N, Gen_Id);
1065 end if;
1066
1067 if Nkind (Parent (N)) = N_Compilation_Unit then
1068 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1069 end if;
1070
1071 -- Make generic parameters immediately visible in the body. They are
1072 -- needed to process the formals declarations. Then make the formals
1073 -- visible in a separate step.
1074
1075 Push_Scope (Gen_Id);
1076
1077 declare
1078 E : Entity_Id;
1079 First_Ent : Entity_Id;
1080
1081 begin
1082 First_Ent := First_Entity (Gen_Id);
1083
1084 E := First_Ent;
1085 while Present (E) and then not Is_Formal (E) loop
1086 Install_Entity (E);
1087 Next_Entity (E);
1088 end loop;
1089
1090 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1091
1092 -- Now generic formals are visible, and the specification can be
1093 -- analyzed, for subsequent conformance check.
1094
1095 Body_Id := Analyze_Subprogram_Specification (Spec);
1096
1097 -- Make formal parameters visible
1098
1099 if Present (E) then
1100
1101 -- E is the first formal parameter, we loop through the formals
1102 -- installing them so that they will be visible.
1103
1104 Set_First_Entity (Gen_Id, E);
1105 while Present (E) loop
1106 Install_Entity (E);
1107 Next_Formal (E);
1108 end loop;
1109 end if;
1110
1111 -- Visible generic entity is callable within its own body
1112
1113 Set_Ekind (Gen_Id, Ekind (Body_Id));
1114 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1115 Set_Ekind (Body_Id, E_Subprogram_Body);
1116 Set_Convention (Body_Id, Convention (Gen_Id));
1117 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1118 Set_Scope (Body_Id, Scope (Gen_Id));
1119 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1120
1121 if Nkind (N) = N_Subprogram_Body_Stub then
1122
1123 -- No body to analyze, so restore state of generic unit
1124
1125 Set_Ekind (Gen_Id, Kind);
1126 Set_Ekind (Body_Id, Kind);
1127
1128 if Present (First_Ent) then
1129 Set_First_Entity (Gen_Id, First_Ent);
1130 end if;
1131
1132 End_Scope;
1133 return;
1134 end if;
1135
1136 -- If this is a compilation unit, it must be made visible explicitly,
1137 -- because the compilation of the declaration, unlike other library
1138 -- unit declarations, does not. If it is not a unit, the following
1139 -- is redundant but harmless.
1140
1141 Set_Is_Immediately_Visible (Gen_Id);
1142 Reference_Body_Formals (Gen_Id, Body_Id);
1143
1144 if Is_Child_Unit (Gen_Id) then
1145 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1146 end if;
1147
1148 Set_Actual_Subtypes (N, Current_Scope);
1149
1150 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1151 -- invariants and predicates associated with the body and its spec.
1152 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1153 -- prepares the contract assertions for generic subprograms or for
1154 -- ASIS. Do not generate contract checks in SPARK mode.
1155
1156 if not GNATprove_Mode then
1157 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1158 end if;
1159
1160 -- If the generic unit carries pre- or post-conditions, copy them
1161 -- to the original generic tree, so that they are properly added
1162 -- to any instantiation.
1163
1164 declare
1165 Orig : constant Node_Id := Original_Node (N);
1166 Cond : Node_Id;
1167
1168 begin
1169 Cond := First (Declarations (N));
1170 while Present (Cond) loop
1171 if Nkind (Cond) = N_Pragma
1172 and then Pragma_Name (Cond) = Name_Check
1173 then
1174 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1175
1176 elsif Nkind (Cond) = N_Pragma
1177 and then Pragma_Name (Cond) = Name_Postcondition
1178 then
1179 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1180 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1181 else
1182 exit;
1183 end if;
1184
1185 Next (Cond);
1186 end loop;
1187 end;
1188
1189 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1190 Set_SPARK_Pragma_Inherited (Body_Id, True);
1191
1192 Analyze_Declarations (Declarations (N));
1193 Check_Completion;
1194 Analyze (Handled_Statement_Sequence (N));
1195
1196 Save_Global_References (Original_Node (N));
1197
1198 -- Prior to exiting the scope, include generic formals again (if any
1199 -- are present) in the set of local entities.
1200
1201 if Present (First_Ent) then
1202 Set_First_Entity (Gen_Id, First_Ent);
1203 end if;
1204
1205 Check_References (Gen_Id);
1206 end;
1207
1208 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1209 End_Scope;
1210 Check_Subprogram_Order (N);
1211
1212 -- Outside of its body, unit is generic again
1213
1214 Set_Ekind (Gen_Id, Kind);
1215 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1216
1217 if Style_Check then
1218 Style.Check_Identifier (Body_Id, Gen_Id);
1219 end if;
1220
1221 End_Generic;
1222 end Analyze_Generic_Subprogram_Body;
1223
1224 ----------------------------
1225 -- Analyze_Null_Procedure --
1226 ----------------------------
1227
1228 procedure Analyze_Null_Procedure
1229 (N : Node_Id;
1230 Is_Completion : out Boolean)
1231 is
1232 Loc : constant Source_Ptr := Sloc (N);
1233 Spec : constant Node_Id := Specification (N);
1234 Designator : Entity_Id;
1235 Form : Node_Id;
1236 Null_Body : Node_Id := Empty;
1237 Prev : Entity_Id;
1238
1239 begin
1240 -- Capture the profile of the null procedure before analysis, for
1241 -- expansion at the freeze point and at each point of call. The body is
1242 -- used if the procedure has preconditions, or if it is a completion. In
1243 -- the first case the body is analyzed at the freeze point, in the other
1244 -- it replaces the null procedure declaration.
1245
1246 Null_Body :=
1247 Make_Subprogram_Body (Loc,
1248 Specification => New_Copy_Tree (Spec),
1249 Declarations => New_List,
1250 Handled_Statement_Sequence =>
1251 Make_Handled_Sequence_Of_Statements (Loc,
1252 Statements => New_List (Make_Null_Statement (Loc))));
1253
1254 -- Create new entities for body and formals
1255
1256 Set_Defining_Unit_Name (Specification (Null_Body),
1257 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1258
1259 Form := First (Parameter_Specifications (Specification (Null_Body)));
1260 while Present (Form) loop
1261 Set_Defining_Identifier (Form,
1262 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1263 Next (Form);
1264 end loop;
1265
1266 -- Determine whether the null procedure may be a completion of a generic
1267 -- suprogram, in which case we use the new null body as the completion
1268 -- and set minimal semantic information on the original declaration,
1269 -- which is rewritten as a null statement.
1270
1271 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1272
1273 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1274 Insert_Before (N, Null_Body);
1275 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1276 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1277
1278 Rewrite (N, Make_Null_Statement (Loc));
1279 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1280 Is_Completion := True;
1281 return;
1282
1283 else
1284 -- Resolve the types of the formals now, because the freeze point
1285 -- may appear in a different context, e.g. an instantiation.
1286
1287 Form := First (Parameter_Specifications (Specification (Null_Body)));
1288 while Present (Form) loop
1289 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1290 Find_Type (Parameter_Type (Form));
1291
1292 elsif
1293 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1294 then
1295 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1296
1297 else
1298 -- The case of a null procedure with a formal that is an
1299 -- access_to_subprogram type, and that is used as an actual
1300 -- in an instantiation is left to the enthusiastic reader.
1301
1302 null;
1303 end if;
1304
1305 Next (Form);
1306 end loop;
1307 end if;
1308
1309 -- If there are previous overloadable entities with the same name,
1310 -- check whether any of them is completed by the null procedure.
1311
1312 if Present (Prev) and then Is_Overloadable (Prev) then
1313 Designator := Analyze_Subprogram_Specification (Spec);
1314 Prev := Find_Corresponding_Spec (N);
1315 end if;
1316
1317 if No (Prev) or else not Comes_From_Source (Prev) then
1318 Designator := Analyze_Subprogram_Specification (Spec);
1319 Set_Has_Completion (Designator);
1320
1321 -- Signal to caller that this is a procedure declaration
1322
1323 Is_Completion := False;
1324
1325 -- Null procedures are always inlined, but generic formal subprograms
1326 -- which appear as such in the internal instance of formal packages,
1327 -- need no completion and are not marked Inline.
1328
1329 if Expander_Active
1330 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1331 then
1332 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1333 Set_Body_To_Inline (N, Null_Body);
1334 Set_Is_Inlined (Designator);
1335 end if;
1336
1337 else
1338 -- The null procedure is a completion
1339
1340 Is_Completion := True;
1341
1342 if Expander_Active then
1343 Rewrite (N, Null_Body);
1344 Analyze (N);
1345
1346 else
1347 Designator := Analyze_Subprogram_Specification (Spec);
1348 Set_Has_Completion (Designator);
1349 Set_Has_Completion (Prev);
1350 end if;
1351 end if;
1352 end Analyze_Null_Procedure;
1353
1354 -----------------------------
1355 -- Analyze_Operator_Symbol --
1356 -----------------------------
1357
1358 -- An operator symbol such as "+" or "and" may appear in context where the
1359 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1360 -- is just a string, as in (conjunction = "or"). In these cases the parser
1361 -- generates this node, and the semantics does the disambiguation. Other
1362 -- such case are actuals in an instantiation, the generic unit in an
1363 -- instantiation, and pragma arguments.
1364
1365 procedure Analyze_Operator_Symbol (N : Node_Id) is
1366 Par : constant Node_Id := Parent (N);
1367
1368 begin
1369 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1370 or else Nkind (Par) = N_Function_Instantiation
1371 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1372 or else (Nkind (Par) = N_Pragma_Argument_Association
1373 and then not Is_Pragma_String_Literal (Par))
1374 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1375 or else (Nkind (Par) = N_Attribute_Reference
1376 and then Attribute_Name (Par) /= Name_Value)
1377 then
1378 Find_Direct_Name (N);
1379
1380 else
1381 Change_Operator_Symbol_To_String_Literal (N);
1382 Analyze (N);
1383 end if;
1384 end Analyze_Operator_Symbol;
1385
1386 -----------------------------------
1387 -- Analyze_Parameter_Association --
1388 -----------------------------------
1389
1390 procedure Analyze_Parameter_Association (N : Node_Id) is
1391 begin
1392 Analyze (Explicit_Actual_Parameter (N));
1393 end Analyze_Parameter_Association;
1394
1395 ----------------------------
1396 -- Analyze_Procedure_Call --
1397 ----------------------------
1398
1399 procedure Analyze_Procedure_Call (N : Node_Id) is
1400 Loc : constant Source_Ptr := Sloc (N);
1401 P : constant Node_Id := Name (N);
1402 Actuals : constant List_Id := Parameter_Associations (N);
1403 Actual : Node_Id;
1404 New_N : Node_Id;
1405
1406 procedure Analyze_Call_And_Resolve;
1407 -- Do Analyze and Resolve calls for procedure call
1408 -- At end, check illegal order dependence.
1409
1410 ------------------------------
1411 -- Analyze_Call_And_Resolve --
1412 ------------------------------
1413
1414 procedure Analyze_Call_And_Resolve is
1415 begin
1416 if Nkind (N) = N_Procedure_Call_Statement then
1417 Analyze_Call (N);
1418 Resolve (N, Standard_Void_Type);
1419 else
1420 Analyze (N);
1421 end if;
1422 end Analyze_Call_And_Resolve;
1423
1424 -- Start of processing for Analyze_Procedure_Call
1425
1426 begin
1427 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1428 -- a procedure call or an entry call. The prefix may denote an access
1429 -- to subprogram type, in which case an implicit dereference applies.
1430 -- If the prefix is an indexed component (without implicit dereference)
1431 -- then the construct denotes a call to a member of an entire family.
1432 -- If the prefix is a simple name, it may still denote a call to a
1433 -- parameterless member of an entry family. Resolution of these various
1434 -- interpretations is delicate.
1435
1436 Analyze (P);
1437
1438 -- If this is a call of the form Obj.Op, the call may have been
1439 -- analyzed and possibly rewritten into a block, in which case
1440 -- we are done.
1441
1442 if Analyzed (N) then
1443 return;
1444 end if;
1445
1446 -- If there is an error analyzing the name (which may have been
1447 -- rewritten if the original call was in prefix notation) then error
1448 -- has been emitted already, mark node and return.
1449
1450 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1451 Set_Etype (N, Any_Type);
1452 return;
1453 end if;
1454
1455 -- Otherwise analyze the parameters
1456
1457 if Present (Actuals) then
1458 Actual := First (Actuals);
1459
1460 while Present (Actual) loop
1461 Analyze (Actual);
1462 Check_Parameterless_Call (Actual);
1463 Next (Actual);
1464 end loop;
1465 end if;
1466
1467 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1468
1469 if Nkind (P) = N_Attribute_Reference
1470 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1471 Name_Elab_Body,
1472 Name_Elab_Subp_Body)
1473 then
1474 if Present (Actuals) then
1475 Error_Msg_N
1476 ("no parameters allowed for this call", First (Actuals));
1477 return;
1478 end if;
1479
1480 Set_Etype (N, Standard_Void_Type);
1481 Set_Analyzed (N);
1482
1483 elsif Is_Entity_Name (P)
1484 and then Is_Record_Type (Etype (Entity (P)))
1485 and then Remote_AST_I_Dereference (P)
1486 then
1487 return;
1488
1489 elsif Is_Entity_Name (P)
1490 and then Ekind (Entity (P)) /= E_Entry_Family
1491 then
1492 if Is_Access_Type (Etype (P))
1493 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1494 and then No (Actuals)
1495 and then Comes_From_Source (N)
1496 then
1497 Error_Msg_N ("missing explicit dereference in call", N);
1498 end if;
1499
1500 Analyze_Call_And_Resolve;
1501
1502 -- If the prefix is the simple name of an entry family, this is
1503 -- a parameterless call from within the task body itself.
1504
1505 elsif Is_Entity_Name (P)
1506 and then Nkind (P) = N_Identifier
1507 and then Ekind (Entity (P)) = E_Entry_Family
1508 and then Present (Actuals)
1509 and then No (Next (First (Actuals)))
1510 then
1511 -- Can be call to parameterless entry family. What appears to be the
1512 -- sole argument is in fact the entry index. Rewrite prefix of node
1513 -- accordingly. Source representation is unchanged by this
1514 -- transformation.
1515
1516 New_N :=
1517 Make_Indexed_Component (Loc,
1518 Prefix =>
1519 Make_Selected_Component (Loc,
1520 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1521 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1522 Expressions => Actuals);
1523 Set_Name (N, New_N);
1524 Set_Etype (New_N, Standard_Void_Type);
1525 Set_Parameter_Associations (N, No_List);
1526 Analyze_Call_And_Resolve;
1527
1528 elsif Nkind (P) = N_Explicit_Dereference then
1529 if Ekind (Etype (P)) = E_Subprogram_Type then
1530 Analyze_Call_And_Resolve;
1531 else
1532 Error_Msg_N ("expect access to procedure in call", P);
1533 end if;
1534
1535 -- The name can be a selected component or an indexed component that
1536 -- yields an access to subprogram. Such a prefix is legal if the call
1537 -- has parameter associations.
1538
1539 elsif Is_Access_Type (Etype (P))
1540 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1541 then
1542 if Present (Actuals) then
1543 Analyze_Call_And_Resolve;
1544 else
1545 Error_Msg_N ("missing explicit dereference in call ", N);
1546 end if;
1547
1548 -- If not an access to subprogram, then the prefix must resolve to the
1549 -- name of an entry, entry family, or protected operation.
1550
1551 -- For the case of a simple entry call, P is a selected component where
1552 -- the prefix is the task and the selector name is the entry. A call to
1553 -- a protected procedure will have the same syntax. If the protected
1554 -- object contains overloaded operations, the entity may appear as a
1555 -- function, the context will select the operation whose type is Void.
1556
1557 elsif Nkind (P) = N_Selected_Component
1558 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1559 E_Procedure,
1560 E_Function)
1561 then
1562 Analyze_Call_And_Resolve;
1563
1564 elsif Nkind (P) = N_Selected_Component
1565 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1566 and then Present (Actuals)
1567 and then No (Next (First (Actuals)))
1568 then
1569 -- Can be call to parameterless entry family. What appears to be the
1570 -- sole argument is in fact the entry index. Rewrite prefix of node
1571 -- accordingly. Source representation is unchanged by this
1572 -- transformation.
1573
1574 New_N :=
1575 Make_Indexed_Component (Loc,
1576 Prefix => New_Copy (P),
1577 Expressions => Actuals);
1578 Set_Name (N, New_N);
1579 Set_Etype (New_N, Standard_Void_Type);
1580 Set_Parameter_Associations (N, No_List);
1581 Analyze_Call_And_Resolve;
1582
1583 -- For the case of a reference to an element of an entry family, P is
1584 -- an indexed component whose prefix is a selected component (task and
1585 -- entry family), and whose index is the entry family index.
1586
1587 elsif Nkind (P) = N_Indexed_Component
1588 and then Nkind (Prefix (P)) = N_Selected_Component
1589 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1590 then
1591 Analyze_Call_And_Resolve;
1592
1593 -- If the prefix is the name of an entry family, it is a call from
1594 -- within the task body itself.
1595
1596 elsif Nkind (P) = N_Indexed_Component
1597 and then Nkind (Prefix (P)) = N_Identifier
1598 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1599 then
1600 New_N :=
1601 Make_Selected_Component (Loc,
1602 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1603 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1604 Rewrite (Prefix (P), New_N);
1605 Analyze (P);
1606 Analyze_Call_And_Resolve;
1607
1608 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1609 -- procedure name, so the construct can only be a qualified expression.
1610
1611 elsif Nkind (P) = N_Qualified_Expression
1612 and then Ada_Version >= Ada_2012
1613 then
1614 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1615 Analyze (N);
1616
1617 -- Anything else is an error
1618
1619 else
1620 Error_Msg_N ("invalid procedure or entry call", N);
1621 end if;
1622 end Analyze_Procedure_Call;
1623
1624 ------------------------------
1625 -- Analyze_Return_Statement --
1626 ------------------------------
1627
1628 procedure Analyze_Return_Statement (N : Node_Id) is
1629
1630 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1631 N_Extended_Return_Statement));
1632
1633 Returns_Object : constant Boolean :=
1634 Nkind (N) = N_Extended_Return_Statement
1635 or else
1636 (Nkind (N) = N_Simple_Return_Statement
1637 and then Present (Expression (N)));
1638 -- True if we're returning something; that is, "return <expression>;"
1639 -- or "return Result : T [:= ...]". False for "return;". Used for error
1640 -- checking: If Returns_Object is True, N should apply to a function
1641 -- body; otherwise N should apply to a procedure body, entry body,
1642 -- accept statement, or extended return statement.
1643
1644 function Find_What_It_Applies_To return Entity_Id;
1645 -- Find the entity representing the innermost enclosing body, accept
1646 -- statement, or extended return statement. If the result is a callable
1647 -- construct or extended return statement, then this will be the value
1648 -- of the Return_Applies_To attribute. Otherwise, the program is
1649 -- illegal. See RM-6.5(4/2).
1650
1651 -----------------------------
1652 -- Find_What_It_Applies_To --
1653 -----------------------------
1654
1655 function Find_What_It_Applies_To return Entity_Id is
1656 Result : Entity_Id := Empty;
1657
1658 begin
1659 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1660 -- and postconditions.
1661
1662 for J in reverse 0 .. Scope_Stack.Last loop
1663 Result := Scope_Stack.Table (J).Entity;
1664 exit when not Ekind_In (Result, E_Block, E_Loop)
1665 and then Chars (Result) /= Name_uPostconditions;
1666 end loop;
1667
1668 pragma Assert (Present (Result));
1669 return Result;
1670 end Find_What_It_Applies_To;
1671
1672 -- Local declarations
1673
1674 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1675 Kind : constant Entity_Kind := Ekind (Scope_Id);
1676 Loc : constant Source_Ptr := Sloc (N);
1677 Stm_Entity : constant Entity_Id :=
1678 New_Internal_Entity
1679 (E_Return_Statement, Current_Scope, Loc, 'R');
1680
1681 -- Start of processing for Analyze_Return_Statement
1682
1683 begin
1684 Set_Return_Statement_Entity (N, Stm_Entity);
1685
1686 Set_Etype (Stm_Entity, Standard_Void_Type);
1687 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1688
1689 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1690 -- (4/2): an inner return statement will apply to this extended return.
1691
1692 if Nkind (N) = N_Extended_Return_Statement then
1693 Push_Scope (Stm_Entity);
1694 end if;
1695
1696 -- Check that pragma No_Return is obeyed. Don't complain about the
1697 -- implicitly-generated return that is placed at the end.
1698
1699 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1700 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1701 end if;
1702
1703 -- Warn on any unassigned OUT parameters if in procedure
1704
1705 if Ekind (Scope_Id) = E_Procedure then
1706 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1707 end if;
1708
1709 -- Check that functions return objects, and other things do not
1710
1711 if Kind = E_Function or else Kind = E_Generic_Function then
1712 if not Returns_Object then
1713 Error_Msg_N ("missing expression in return from function", N);
1714 end if;
1715
1716 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1717 if Returns_Object then
1718 Error_Msg_N ("procedure cannot return value (use function)", N);
1719 end if;
1720
1721 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1722 if Returns_Object then
1723 if Is_Protected_Type (Scope (Scope_Id)) then
1724 Error_Msg_N ("entry body cannot return value", N);
1725 else
1726 Error_Msg_N ("accept statement cannot return value", N);
1727 end if;
1728 end if;
1729
1730 elsif Kind = E_Return_Statement then
1731
1732 -- We are nested within another return statement, which must be an
1733 -- extended_return_statement.
1734
1735 if Returns_Object then
1736 if Nkind (N) = N_Extended_Return_Statement then
1737 Error_Msg_N
1738 ("extended return statement cannot be nested (use `RETURN;`)",
1739 N);
1740
1741 -- Case of a simple return statement with a value inside extended
1742 -- return statement.
1743
1744 else
1745 Error_Msg_N
1746 ("return nested in extended return statement cannot return " &
1747 "value (use `RETURN;`)", N);
1748 end if;
1749 end if;
1750
1751 else
1752 Error_Msg_N ("illegal context for return statement", N);
1753 end if;
1754
1755 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1756 Analyze_Function_Return (N);
1757
1758 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1759 Set_Return_Present (Scope_Id);
1760 end if;
1761
1762 if Nkind (N) = N_Extended_Return_Statement then
1763 End_Scope;
1764 end if;
1765
1766 Kill_Current_Values (Last_Assignment_Only => True);
1767 Check_Unreachable_Code (N);
1768
1769 Analyze_Dimension (N);
1770 end Analyze_Return_Statement;
1771
1772 -------------------------------------
1773 -- Analyze_Simple_Return_Statement --
1774 -------------------------------------
1775
1776 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1777 begin
1778 if Present (Expression (N)) then
1779 Mark_Coextensions (N, Expression (N));
1780 end if;
1781
1782 Analyze_Return_Statement (N);
1783 end Analyze_Simple_Return_Statement;
1784
1785 -------------------------
1786 -- Analyze_Return_Type --
1787 -------------------------
1788
1789 procedure Analyze_Return_Type (N : Node_Id) is
1790 Designator : constant Entity_Id := Defining_Entity (N);
1791 Typ : Entity_Id := Empty;
1792
1793 begin
1794 -- Normal case where result definition does not indicate an error
1795
1796 if Result_Definition (N) /= Error then
1797 if Nkind (Result_Definition (N)) = N_Access_Definition then
1798 Check_SPARK_Restriction
1799 ("access result is not allowed", Result_Definition (N));
1800
1801 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1802
1803 declare
1804 AD : constant Node_Id :=
1805 Access_To_Subprogram_Definition (Result_Definition (N));
1806 begin
1807 if Present (AD) and then Protected_Present (AD) then
1808 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1809 else
1810 Typ := Access_Definition (N, Result_Definition (N));
1811 end if;
1812 end;
1813
1814 Set_Parent (Typ, Result_Definition (N));
1815 Set_Is_Local_Anonymous_Access (Typ);
1816 Set_Etype (Designator, Typ);
1817
1818 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1819
1820 Null_Exclusion_Static_Checks (N);
1821
1822 -- Subtype_Mark case
1823
1824 else
1825 Find_Type (Result_Definition (N));
1826 Typ := Entity (Result_Definition (N));
1827 Set_Etype (Designator, Typ);
1828
1829 -- Unconstrained array as result is not allowed in SPARK
1830
1831 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1832 Check_SPARK_Restriction
1833 ("returning an unconstrained array is not allowed",
1834 Result_Definition (N));
1835 end if;
1836
1837 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1838
1839 Null_Exclusion_Static_Checks (N);
1840
1841 -- If a null exclusion is imposed on the result type, then create
1842 -- a null-excluding itype (an access subtype) and use it as the
1843 -- function's Etype. Note that the null exclusion checks are done
1844 -- right before this, because they don't get applied to types that
1845 -- do not come from source.
1846
1847 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1848 Set_Etype (Designator,
1849 Create_Null_Excluding_Itype
1850 (T => Typ,
1851 Related_Nod => N,
1852 Scope_Id => Scope (Current_Scope)));
1853
1854 -- The new subtype must be elaborated before use because
1855 -- it is visible outside of the function. However its base
1856 -- type may not be frozen yet, so the reference that will
1857 -- force elaboration must be attached to the freezing of
1858 -- the base type.
1859
1860 -- If the return specification appears on a proper body,
1861 -- the subtype will have been created already on the spec.
1862
1863 if Is_Frozen (Typ) then
1864 if Nkind (Parent (N)) = N_Subprogram_Body
1865 and then Nkind (Parent (Parent (N))) = N_Subunit
1866 then
1867 null;
1868 else
1869 Build_Itype_Reference (Etype (Designator), Parent (N));
1870 end if;
1871
1872 else
1873 Ensure_Freeze_Node (Typ);
1874
1875 declare
1876 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1877 begin
1878 Set_Itype (IR, Etype (Designator));
1879 Append_Freeze_Actions (Typ, New_List (IR));
1880 end;
1881 end if;
1882
1883 else
1884 Set_Etype (Designator, Typ);
1885 end if;
1886
1887 if Ekind (Typ) = E_Incomplete_Type
1888 and then Is_Value_Type (Typ)
1889 then
1890 null;
1891
1892 elsif Ekind (Typ) = E_Incomplete_Type
1893 or else (Is_Class_Wide_Type (Typ)
1894 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1895 then
1896 -- AI05-0151: Tagged incomplete types are allowed in all formal
1897 -- parts. Untagged incomplete types are not allowed in bodies.
1898
1899 if Ada_Version >= Ada_2012 then
1900 if Is_Tagged_Type (Typ) then
1901 null;
1902
1903 elsif Nkind_In (Parent (Parent (N)),
1904 N_Accept_Statement,
1905 N_Entry_Body,
1906 N_Subprogram_Body)
1907 then
1908 Error_Msg_NE
1909 ("invalid use of untagged incomplete type&",
1910 Designator, Typ);
1911 end if;
1912
1913 -- The type must be completed in the current package. This
1914 -- is checked at the end of the package declaraton, when
1915 -- Taft-amendment types are identified. If the return type
1916 -- is class-wide, there is no required check, the type can
1917 -- be a bona fide TAT.
1918
1919 if Ekind (Scope (Current_Scope)) = E_Package
1920 and then In_Private_Part (Scope (Current_Scope))
1921 and then not Is_Class_Wide_Type (Typ)
1922 then
1923 Append_Elmt (Designator, Private_Dependents (Typ));
1924 end if;
1925
1926 else
1927 Error_Msg_NE
1928 ("invalid use of incomplete type&", Designator, Typ);
1929 end if;
1930 end if;
1931 end if;
1932
1933 -- Case where result definition does indicate an error
1934
1935 else
1936 Set_Etype (Designator, Any_Type);
1937 end if;
1938 end Analyze_Return_Type;
1939
1940 -----------------------------
1941 -- Analyze_Subprogram_Body --
1942 -----------------------------
1943
1944 procedure Analyze_Subprogram_Body (N : Node_Id) is
1945 Loc : constant Source_Ptr := Sloc (N);
1946 Body_Spec : constant Node_Id := Specification (N);
1947 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1948
1949 begin
1950 if Debug_Flag_C then
1951 Write_Str ("==> subprogram body ");
1952 Write_Name (Chars (Body_Id));
1953 Write_Str (" from ");
1954 Write_Location (Loc);
1955 Write_Eol;
1956 Indent;
1957 end if;
1958
1959 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1960
1961 -- The real work is split out into the helper, so it can do "return;"
1962 -- without skipping the debug output:
1963
1964 Analyze_Subprogram_Body_Helper (N);
1965
1966 if Debug_Flag_C then
1967 Outdent;
1968 Write_Str ("<== subprogram body ");
1969 Write_Name (Chars (Body_Id));
1970 Write_Str (" from ");
1971 Write_Location (Loc);
1972 Write_Eol;
1973 end if;
1974 end Analyze_Subprogram_Body;
1975
1976 --------------------------------------
1977 -- Analyze_Subprogram_Body_Contract --
1978 --------------------------------------
1979
1980 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
1981 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
1982 Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl);
1983 Prag : Node_Id;
1984 Ref_Depends : Node_Id := Empty;
1985 Ref_Global : Node_Id := Empty;
1986
1987 begin
1988 -- When a subprogram body declaration is erroneous, its defining entity
1989 -- is left unanalyzed. There is nothing left to do in this case because
1990 -- the body lacks a contract.
1991
1992 if not Analyzed (Body_Id) then
1993 return;
1994 end if;
1995
1996 -- Locate and store pragmas Refined_Depends and Refined_Global since
1997 -- their order of analysis matters.
1998
1999 Prag := Classifications (Contract (Body_Id));
2000 while Present (Prag) loop
2001 if Pragma_Name (Prag) = Name_Refined_Depends then
2002 Ref_Depends := Prag;
2003 elsif Pragma_Name (Prag) = Name_Refined_Global then
2004 Ref_Global := Prag;
2005 end if;
2006
2007 Prag := Next_Pragma (Prag);
2008 end loop;
2009
2010 -- Analyze Refined_Global first as Refined_Depends may mention items
2011 -- classified in the global refinement.
2012
2013 if Present (Ref_Global) then
2014 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2015
2016 -- When the corresponding Global aspect/pragma references a state with
2017 -- visible refinement, the body requires Refined_Global.
2018
2019 elsif Present (Spec_Id) then
2020 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2021
2022 if Present (Prag) and then Contains_Refined_State (Prag) then
2023 Error_Msg_NE
2024 ("body of subprogram & requires global refinement",
2025 Body_Decl, Spec_Id);
2026 end if;
2027 end if;
2028
2029 -- Refined_Depends must be analyzed after Refined_Global in order to see
2030 -- the modes of all global refinements.
2031
2032 if Present (Ref_Depends) then
2033 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2034
2035 -- When the corresponding Depends aspect/pragma references a state with
2036 -- visible refinement, the body requires Refined_Depends.
2037
2038 elsif Present (Spec_Id) then
2039 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2040
2041 if Present (Prag) and then Contains_Refined_State (Prag) then
2042 Error_Msg_NE
2043 ("body of subprogram & requires dependance refinement",
2044 Body_Decl, Spec_Id);
2045 end if;
2046 end if;
2047 end Analyze_Subprogram_Body_Contract;
2048
2049 ------------------------------------
2050 -- Analyze_Subprogram_Body_Helper --
2051 ------------------------------------
2052
2053 -- This procedure is called for regular subprogram bodies, generic bodies,
2054 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2055 -- specification matters, and is used to create a proper declaration for
2056 -- the subprogram, or to perform conformance checks.
2057
2058 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2059 Loc : constant Source_Ptr := Sloc (N);
2060 Body_Spec : constant Node_Id := Specification (N);
2061 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2062 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2063 Conformant : Boolean;
2064 HSS : Node_Id;
2065 Prot_Typ : Entity_Id := Empty;
2066 Spec_Id : Entity_Id;
2067 Spec_Decl : Node_Id := Empty;
2068
2069 Last_Real_Spec_Entity : Entity_Id := Empty;
2070 -- When we analyze a separate spec, the entity chain ends up containing
2071 -- the formals, as well as any itypes generated during analysis of the
2072 -- default expressions for parameters, or the arguments of associated
2073 -- precondition/postcondition pragmas (which are analyzed in the context
2074 -- of the spec since they have visibility on formals).
2075 --
2076 -- These entities belong with the spec and not the body. However we do
2077 -- the analysis of the body in the context of the spec (again to obtain
2078 -- visibility to the formals), and all the entities generated during
2079 -- this analysis end up also chained to the entity chain of the spec.
2080 -- But they really belong to the body, and there is circuitry to move
2081 -- them from the spec to the body.
2082 --
2083 -- However, when we do this move, we don't want to move the real spec
2084 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2085 -- variable points to the last real spec entity, so we only move those
2086 -- chained beyond that point. It is initialized to Empty to deal with
2087 -- the case where there is no separate spec.
2088
2089 procedure Check_Anonymous_Return;
2090 -- Ada 2005: if a function returns an access type that denotes a task,
2091 -- or a type that contains tasks, we must create a master entity for
2092 -- the anonymous type, which typically will be used in an allocator
2093 -- in the body of the function.
2094
2095 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2096 -- Look ahead to recognize a pragma that may appear after the body.
2097 -- If there is a previous spec, check that it appears in the same
2098 -- declarative part. If the pragma is Inline_Always, perform inlining
2099 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2100 -- If the body acts as a spec, and inlining is required, we create a
2101 -- subprogram declaration for it, in order to attach the body to inline.
2102 -- If pragma does not appear after the body, check whether there is
2103 -- an inline pragma before any local declarations.
2104
2105 procedure Check_Missing_Return;
2106 -- Checks for a function with a no return statements, and also performs
2107 -- the warning checks implemented by Check_Returns. In formal mode, also
2108 -- verify that a function ends with a RETURN and that a procedure does
2109 -- not contain any RETURN.
2110
2111 function Disambiguate_Spec return Entity_Id;
2112 -- When a primitive is declared between the private view and the full
2113 -- view of a concurrent type which implements an interface, a special
2114 -- mechanism is used to find the corresponding spec of the primitive
2115 -- body.
2116
2117 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2118 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2119 -- incomplete types coming from a limited context and swap their limited
2120 -- views with the non-limited ones.
2121
2122 function Is_Private_Concurrent_Primitive
2123 (Subp_Id : Entity_Id) return Boolean;
2124 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2125 -- type that implements an interface and has a private view.
2126
2127 procedure Set_Trivial_Subprogram (N : Node_Id);
2128 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2129 -- subprogram whose body is being analyzed. N is the statement node
2130 -- causing the flag to be set, if the following statement is a return
2131 -- of an entity, we mark the entity as set in source to suppress any
2132 -- warning on the stylized use of function stubs with a dummy return.
2133
2134 procedure Verify_Overriding_Indicator;
2135 -- If there was a previous spec, the entity has been entered in the
2136 -- current scope previously. If the body itself carries an overriding
2137 -- indicator, check that it is consistent with the known status of the
2138 -- entity.
2139
2140 ----------------------------
2141 -- Check_Anonymous_Return --
2142 ----------------------------
2143
2144 procedure Check_Anonymous_Return is
2145 Decl : Node_Id;
2146 Par : Node_Id;
2147 Scop : Entity_Id;
2148
2149 begin
2150 if Present (Spec_Id) then
2151 Scop := Spec_Id;
2152 else
2153 Scop := Body_Id;
2154 end if;
2155
2156 if Ekind (Scop) = E_Function
2157 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2158 and then not Is_Thunk (Scop)
2159 and then (Has_Task (Designated_Type (Etype (Scop)))
2160 or else
2161 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2162 and then
2163 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2164 and then Expander_Active
2165
2166 -- Avoid cases with no tasking support
2167
2168 and then RTE_Available (RE_Current_Master)
2169 and then not Restriction_Active (No_Task_Hierarchy)
2170 then
2171 Decl :=
2172 Make_Object_Declaration (Loc,
2173 Defining_Identifier =>
2174 Make_Defining_Identifier (Loc, Name_uMaster),
2175 Constant_Present => True,
2176 Object_Definition =>
2177 New_Reference_To (RTE (RE_Master_Id), Loc),
2178 Expression =>
2179 Make_Explicit_Dereference (Loc,
2180 New_Reference_To (RTE (RE_Current_Master), Loc)));
2181
2182 if Present (Declarations (N)) then
2183 Prepend (Decl, Declarations (N));
2184 else
2185 Set_Declarations (N, New_List (Decl));
2186 end if;
2187
2188 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2189 Set_Has_Master_Entity (Scop);
2190
2191 -- Now mark the containing scope as a task master
2192
2193 Par := N;
2194 while Nkind (Par) /= N_Compilation_Unit loop
2195 Par := Parent (Par);
2196 pragma Assert (Present (Par));
2197
2198 -- If we fall off the top, we are at the outer level, and
2199 -- the environment task is our effective master, so nothing
2200 -- to mark.
2201
2202 if Nkind_In
2203 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2204 then
2205 Set_Is_Task_Master (Par, True);
2206 exit;
2207 end if;
2208 end loop;
2209 end if;
2210 end Check_Anonymous_Return;
2211
2212 -------------------------
2213 -- Check_Inline_Pragma --
2214 -------------------------
2215
2216 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2217 Prag : Node_Id;
2218 Plist : List_Id;
2219
2220 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2221 -- True when N is a pragma Inline or Inline_Always that applies
2222 -- to this subprogram.
2223
2224 -----------------------
2225 -- Is_Inline_Pragma --
2226 -----------------------
2227
2228 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2229 begin
2230 return
2231 Nkind (N) = N_Pragma
2232 and then
2233 (Pragma_Name (N) = Name_Inline_Always
2234 or else
2235 (Front_End_Inlining
2236 and then Pragma_Name (N) = Name_Inline))
2237 and then
2238 Chars
2239 (Expression (First (Pragma_Argument_Associations (N)))) =
2240 Chars (Body_Id);
2241 end Is_Inline_Pragma;
2242
2243 -- Start of processing for Check_Inline_Pragma
2244
2245 begin
2246 if not Expander_Active then
2247 return;
2248 end if;
2249
2250 if Is_List_Member (N)
2251 and then Present (Next (N))
2252 and then Is_Inline_Pragma (Next (N))
2253 then
2254 Prag := Next (N);
2255
2256 elsif Nkind (N) /= N_Subprogram_Body_Stub
2257 and then Present (Declarations (N))
2258 and then Is_Inline_Pragma (First (Declarations (N)))
2259 then
2260 Prag := First (Declarations (N));
2261
2262 else
2263 Prag := Empty;
2264 end if;
2265
2266 if Present (Prag) then
2267 if Present (Spec_Id) then
2268 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2269 Analyze (Prag);
2270 end if;
2271
2272 else
2273 -- Create a subprogram declaration, to make treatment uniform
2274
2275 declare
2276 Subp : constant Entity_Id :=
2277 Make_Defining_Identifier (Loc, Chars (Body_Id));
2278 Decl : constant Node_Id :=
2279 Make_Subprogram_Declaration (Loc,
2280 Specification =>
2281 New_Copy_Tree (Specification (N)));
2282
2283 begin
2284 Set_Defining_Unit_Name (Specification (Decl), Subp);
2285
2286 if Present (First_Formal (Body_Id)) then
2287 Plist := Copy_Parameter_List (Body_Id);
2288 Set_Parameter_Specifications
2289 (Specification (Decl), Plist);
2290 end if;
2291
2292 Insert_Before (N, Decl);
2293 Analyze (Decl);
2294 Analyze (Prag);
2295 Set_Has_Pragma_Inline (Subp);
2296
2297 if Pragma_Name (Prag) = Name_Inline_Always then
2298 Set_Is_Inlined (Subp);
2299 Set_Has_Pragma_Inline_Always (Subp);
2300 end if;
2301
2302 Spec := Subp;
2303 end;
2304 end if;
2305 end if;
2306 end Check_Inline_Pragma;
2307
2308 --------------------------
2309 -- Check_Missing_Return --
2310 --------------------------
2311
2312 procedure Check_Missing_Return is
2313 Id : Entity_Id;
2314 Missing_Ret : Boolean;
2315
2316 begin
2317 if Nkind (Body_Spec) = N_Function_Specification then
2318 if Present (Spec_Id) then
2319 Id := Spec_Id;
2320 else
2321 Id := Body_Id;
2322 end if;
2323
2324 if Return_Present (Id) then
2325 Check_Returns (HSS, 'F', Missing_Ret);
2326
2327 if Missing_Ret then
2328 Set_Has_Missing_Return (Id);
2329 end if;
2330
2331 elsif Is_Generic_Subprogram (Id)
2332 or else not Is_Machine_Code_Subprogram (Id)
2333 then
2334 Error_Msg_N ("missing RETURN statement in function body", N);
2335 end if;
2336
2337 -- If procedure with No_Return, check returns
2338
2339 elsif Nkind (Body_Spec) = N_Procedure_Specification
2340 and then Present (Spec_Id)
2341 and then No_Return (Spec_Id)
2342 then
2343 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2344 end if;
2345
2346 -- Special checks in SPARK mode
2347
2348 if Nkind (Body_Spec) = N_Function_Specification then
2349
2350 -- In SPARK mode, last statement of a function should be a return
2351
2352 declare
2353 Stat : constant Node_Id := Last_Source_Statement (HSS);
2354 begin
2355 if Present (Stat)
2356 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2357 N_Extended_Return_Statement)
2358 then
2359 Check_SPARK_Restriction
2360 ("last statement in function should be RETURN", Stat);
2361 end if;
2362 end;
2363
2364 -- In SPARK mode, verify that a procedure has no return
2365
2366 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2367 if Present (Spec_Id) then
2368 Id := Spec_Id;
2369 else
2370 Id := Body_Id;
2371 end if;
2372
2373 -- Would be nice to point to return statement here, can we
2374 -- borrow the Check_Returns procedure here ???
2375
2376 if Return_Present (Id) then
2377 Check_SPARK_Restriction
2378 ("procedure should not have RETURN", N);
2379 end if;
2380 end if;
2381 end Check_Missing_Return;
2382
2383 -----------------------
2384 -- Disambiguate_Spec --
2385 -----------------------
2386
2387 function Disambiguate_Spec return Entity_Id is
2388 Priv_Spec : Entity_Id;
2389 Spec_N : Entity_Id;
2390
2391 procedure Replace_Types (To_Corresponding : Boolean);
2392 -- Depending on the flag, replace the type of formal parameters of
2393 -- Body_Id if it is a concurrent type implementing interfaces with
2394 -- the corresponding record type or the other way around.
2395
2396 procedure Replace_Types (To_Corresponding : Boolean) is
2397 Formal : Entity_Id;
2398 Formal_Typ : Entity_Id;
2399
2400 begin
2401 Formal := First_Formal (Body_Id);
2402 while Present (Formal) loop
2403 Formal_Typ := Etype (Formal);
2404
2405 if Is_Class_Wide_Type (Formal_Typ) then
2406 Formal_Typ := Root_Type (Formal_Typ);
2407 end if;
2408
2409 -- From concurrent type to corresponding record
2410
2411 if To_Corresponding then
2412 if Is_Concurrent_Type (Formal_Typ)
2413 and then Present (Corresponding_Record_Type (Formal_Typ))
2414 and then Present (Interfaces (
2415 Corresponding_Record_Type (Formal_Typ)))
2416 then
2417 Set_Etype (Formal,
2418 Corresponding_Record_Type (Formal_Typ));
2419 end if;
2420
2421 -- From corresponding record to concurrent type
2422
2423 else
2424 if Is_Concurrent_Record_Type (Formal_Typ)
2425 and then Present (Interfaces (Formal_Typ))
2426 then
2427 Set_Etype (Formal,
2428 Corresponding_Concurrent_Type (Formal_Typ));
2429 end if;
2430 end if;
2431
2432 Next_Formal (Formal);
2433 end loop;
2434 end Replace_Types;
2435
2436 -- Start of processing for Disambiguate_Spec
2437
2438 begin
2439 -- Try to retrieve the specification of the body as is. All error
2440 -- messages are suppressed because the body may not have a spec in
2441 -- its current state.
2442
2443 Spec_N := Find_Corresponding_Spec (N, False);
2444
2445 -- It is possible that this is the body of a primitive declared
2446 -- between a private and a full view of a concurrent type. The
2447 -- controlling parameter of the spec carries the concurrent type,
2448 -- not the corresponding record type as transformed by Analyze_
2449 -- Subprogram_Specification. In such cases, we undo the change
2450 -- made by the analysis of the specification and try to find the
2451 -- spec again.
2452
2453 -- Note that wrappers already have their corresponding specs and
2454 -- bodies set during their creation, so if the candidate spec is
2455 -- a wrapper, then we definitely need to swap all types to their
2456 -- original concurrent status.
2457
2458 if No (Spec_N)
2459 or else Is_Primitive_Wrapper (Spec_N)
2460 then
2461 -- Restore all references of corresponding record types to the
2462 -- original concurrent types.
2463
2464 Replace_Types (To_Corresponding => False);
2465 Priv_Spec := Find_Corresponding_Spec (N, False);
2466
2467 -- The current body truly belongs to a primitive declared between
2468 -- a private and a full view. We leave the modified body as is,
2469 -- and return the true spec.
2470
2471 if Present (Priv_Spec)
2472 and then Is_Private_Primitive (Priv_Spec)
2473 then
2474 return Priv_Spec;
2475 end if;
2476
2477 -- In case that this is some sort of error, restore the original
2478 -- state of the body.
2479
2480 Replace_Types (To_Corresponding => True);
2481 end if;
2482
2483 return Spec_N;
2484 end Disambiguate_Spec;
2485
2486 ----------------------------
2487 -- Exchange_Limited_Views --
2488 ----------------------------
2489
2490 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2491 procedure Detect_And_Exchange (Id : Entity_Id);
2492 -- Determine whether Id's type denotes an incomplete type associated
2493 -- with a limited with clause and exchange the limited view with the
2494 -- non-limited one.
2495
2496 -------------------------
2497 -- Detect_And_Exchange --
2498 -------------------------
2499
2500 procedure Detect_And_Exchange (Id : Entity_Id) is
2501 Typ : constant Entity_Id := Etype (Id);
2502
2503 begin
2504 if Ekind (Typ) = E_Incomplete_Type
2505 and then From_Limited_With (Typ)
2506 and then Present (Non_Limited_View (Typ))
2507 then
2508 Set_Etype (Id, Non_Limited_View (Typ));
2509 end if;
2510 end Detect_And_Exchange;
2511
2512 -- Local variables
2513
2514 Formal : Entity_Id;
2515
2516 -- Start of processing for Exchange_Limited_Views
2517
2518 begin
2519 if No (Subp_Id) then
2520 return;
2521
2522 -- Do not process subprogram bodies as they already use the non-
2523 -- limited view of types.
2524
2525 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2526 return;
2527 end if;
2528
2529 -- Examine all formals and swap views when applicable
2530
2531 Formal := First_Formal (Subp_Id);
2532 while Present (Formal) loop
2533 Detect_And_Exchange (Formal);
2534
2535 Next_Formal (Formal);
2536 end loop;
2537
2538 -- Process the return type of a function
2539
2540 if Ekind (Subp_Id) = E_Function then
2541 Detect_And_Exchange (Subp_Id);
2542 end if;
2543 end Exchange_Limited_Views;
2544
2545 -------------------------------------
2546 -- Is_Private_Concurrent_Primitive --
2547 -------------------------------------
2548
2549 function Is_Private_Concurrent_Primitive
2550 (Subp_Id : Entity_Id) return Boolean
2551 is
2552 Formal_Typ : Entity_Id;
2553
2554 begin
2555 if Present (First_Formal (Subp_Id)) then
2556 Formal_Typ := Etype (First_Formal (Subp_Id));
2557
2558 if Is_Concurrent_Record_Type (Formal_Typ) then
2559 if Is_Class_Wide_Type (Formal_Typ) then
2560 Formal_Typ := Root_Type (Formal_Typ);
2561 end if;
2562
2563 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2564 end if;
2565
2566 -- The type of the first formal is a concurrent tagged type with
2567 -- a private view.
2568
2569 return
2570 Is_Concurrent_Type (Formal_Typ)
2571 and then Is_Tagged_Type (Formal_Typ)
2572 and then Has_Private_Declaration (Formal_Typ);
2573 end if;
2574
2575 return False;
2576 end Is_Private_Concurrent_Primitive;
2577
2578 ----------------------------
2579 -- Set_Trivial_Subprogram --
2580 ----------------------------
2581
2582 procedure Set_Trivial_Subprogram (N : Node_Id) is
2583 Nxt : constant Node_Id := Next (N);
2584
2585 begin
2586 Set_Is_Trivial_Subprogram (Body_Id);
2587
2588 if Present (Spec_Id) then
2589 Set_Is_Trivial_Subprogram (Spec_Id);
2590 end if;
2591
2592 if Present (Nxt)
2593 and then Nkind (Nxt) = N_Simple_Return_Statement
2594 and then No (Next (Nxt))
2595 and then Present (Expression (Nxt))
2596 and then Is_Entity_Name (Expression (Nxt))
2597 then
2598 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2599 end if;
2600 end Set_Trivial_Subprogram;
2601
2602 ---------------------------------
2603 -- Verify_Overriding_Indicator --
2604 ---------------------------------
2605
2606 procedure Verify_Overriding_Indicator is
2607 begin
2608 if Must_Override (Body_Spec) then
2609 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2610 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2611 then
2612 null;
2613
2614 elsif not Present (Overridden_Operation (Spec_Id)) then
2615 Error_Msg_NE
2616 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2617 end if;
2618
2619 elsif Must_Not_Override (Body_Spec) then
2620 if Present (Overridden_Operation (Spec_Id)) then
2621 Error_Msg_NE
2622 ("subprogram& overrides inherited operation",
2623 Body_Spec, Spec_Id);
2624
2625 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2626 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2627 then
2628 Error_Msg_NE
2629 ("subprogram & overrides predefined operator ",
2630 Body_Spec, Spec_Id);
2631
2632 -- If this is not a primitive operation or protected subprogram,
2633 -- then the overriding indicator is altogether illegal.
2634
2635 elsif not Is_Primitive (Spec_Id)
2636 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2637 then
2638 Error_Msg_N
2639 ("overriding indicator only allowed " &
2640 "if subprogram is primitive",
2641 Body_Spec);
2642 end if;
2643
2644 elsif Style_Check
2645 and then Present (Overridden_Operation (Spec_Id))
2646 then
2647 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2648 Style.Missing_Overriding (N, Body_Id);
2649
2650 elsif Style_Check
2651 and then Can_Override_Operator (Spec_Id)
2652 and then not Is_Predefined_File_Name
2653 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2654 then
2655 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2656 Style.Missing_Overriding (N, Body_Id);
2657 end if;
2658 end Verify_Overriding_Indicator;
2659
2660 -- Start of processing for Analyze_Subprogram_Body_Helper
2661
2662 begin
2663 -- Generic subprograms are handled separately. They always have a
2664 -- generic specification. Determine whether current scope has a
2665 -- previous declaration.
2666
2667 -- If the subprogram body is defined within an instance of the same
2668 -- name, the instance appears as a package renaming, and will be hidden
2669 -- within the subprogram.
2670
2671 if Present (Prev_Id)
2672 and then not Is_Overloadable (Prev_Id)
2673 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2674 or else Comes_From_Source (Prev_Id))
2675 then
2676 if Is_Generic_Subprogram (Prev_Id) then
2677 Spec_Id := Prev_Id;
2678 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2679 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2680
2681 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2682
2683 if Nkind (N) = N_Subprogram_Body then
2684 HSS := Handled_Statement_Sequence (N);
2685 Check_Missing_Return;
2686 end if;
2687
2688 return;
2689
2690 else
2691 -- Previous entity conflicts with subprogram name. Attempting to
2692 -- enter name will post error.
2693
2694 Enter_Name (Body_Id);
2695 return;
2696 end if;
2697
2698 -- Non-generic case, find the subprogram declaration, if one was seen,
2699 -- or enter new overloaded entity in the current scope. If the
2700 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2701 -- part of the context of one of its subunits. No need to redo the
2702 -- analysis.
2703
2704 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2705 return;
2706
2707 else
2708 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2709
2710 if Nkind (N) = N_Subprogram_Body_Stub
2711 or else No (Corresponding_Spec (N))
2712 then
2713 if Is_Private_Concurrent_Primitive (Body_Id) then
2714 Spec_Id := Disambiguate_Spec;
2715 else
2716 Spec_Id := Find_Corresponding_Spec (N);
2717 end if;
2718
2719 -- If this is a duplicate body, no point in analyzing it
2720
2721 if Error_Posted (N) then
2722 return;
2723 end if;
2724
2725 -- A subprogram body should cause freezing of its own declaration,
2726 -- but if there was no previous explicit declaration, then the
2727 -- subprogram will get frozen too late (there may be code within
2728 -- the body that depends on the subprogram having been frozen,
2729 -- such as uses of extra formals), so we force it to be frozen
2730 -- here. Same holds if the body and spec are compilation units.
2731 -- Finally, if the return type is an anonymous access to protected
2732 -- subprogram, it must be frozen before the body because its
2733 -- expansion has generated an equivalent type that is used when
2734 -- elaborating the body.
2735
2736 -- An exception in the case of Ada 2012, AI05-177: The bodies
2737 -- created for expression functions do not freeze.
2738
2739 if No (Spec_Id)
2740 and then Nkind (Original_Node (N)) /= N_Expression_Function
2741 then
2742 Freeze_Before (N, Body_Id);
2743
2744 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2745 Freeze_Before (N, Spec_Id);
2746
2747 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2748 Freeze_Before (N, Etype (Body_Id));
2749 end if;
2750
2751 else
2752 Spec_Id := Corresponding_Spec (N);
2753 end if;
2754 end if;
2755
2756 -- Language-defined aspects cannot appear in a subprogram body [stub] if
2757 -- the subprogram has a separate spec. Certainly implementation-defined
2758 -- aspects are allowed to appear (per Aspects_On_Body_Of_Stub_OK).
2759
2760 if Has_Aspects (N) then
2761 if Present (Spec_Id)
2762 and then not Aspects_On_Body_Or_Stub_OK (N)
2763
2764 -- Do not emit an error on a subprogram body stub that act as
2765 -- its own spec.
2766
2767 and then Nkind (Parent (Parent (Spec_Id))) /= N_Subprogram_Body_Stub
2768 then
2769 Error_Msg_N
2770 ("aspect specifications must appear in subprogram declaration",
2771 N);
2772
2773 -- Delay the analysis of aspect specifications that apply to a body
2774 -- stub until the proper body is analyzed. If the corresponding body
2775 -- is missing, the aspects are still analyzed in Analyze_Proper_Body.
2776
2777 elsif Nkind (N) in N_Body_Stub then
2778 null;
2779
2780 else
2781 Analyze_Aspect_Specifications (N, Body_Id);
2782 end if;
2783 end if;
2784
2785 -- Previously we scanned the body to look for nested subprograms, and
2786 -- rejected an inline directive if nested subprograms were present,
2787 -- because the back-end would generate conflicting symbols for the
2788 -- nested bodies. This is now unnecessary.
2789
2790 -- Look ahead to recognize a pragma Inline that appears after the body
2791
2792 Check_Inline_Pragma (Spec_Id);
2793
2794 -- Deal with special case of a fully private operation in the body of
2795 -- the protected type. We must create a declaration for the subprogram,
2796 -- in order to attach the protected subprogram that will be used in
2797 -- internal calls. We exclude compiler generated bodies from the
2798 -- expander since the issue does not arise for those cases.
2799
2800 if No (Spec_Id)
2801 and then Comes_From_Source (N)
2802 and then Is_Protected_Type (Current_Scope)
2803 then
2804 Spec_Id := Build_Private_Protected_Declaration (N);
2805 end if;
2806
2807 -- If a separate spec is present, then deal with freezing issues
2808
2809 if Present (Spec_Id) then
2810 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2811 Verify_Overriding_Indicator;
2812
2813 -- In general, the spec will be frozen when we start analyzing the
2814 -- body. However, for internally generated operations, such as
2815 -- wrapper functions for inherited operations with controlling
2816 -- results, the spec may not have been frozen by the time we expand
2817 -- the freeze actions that include the bodies. In particular, extra
2818 -- formals for accessibility or for return-in-place may need to be
2819 -- generated. Freeze nodes, if any, are inserted before the current
2820 -- body. These freeze actions are also needed in ASIS mode to enable
2821 -- the proper back-annotations.
2822
2823 if not Is_Frozen (Spec_Id)
2824 and then (Expander_Active or ASIS_Mode)
2825 then
2826 -- Force the generation of its freezing node to ensure proper
2827 -- management of access types in the backend.
2828
2829 -- This is definitely needed for some cases, but it is not clear
2830 -- why, to be investigated further???
2831
2832 Set_Has_Delayed_Freeze (Spec_Id);
2833 Freeze_Before (N, Spec_Id);
2834 end if;
2835 end if;
2836
2837 -- Mark presence of postcondition procedure in current scope and mark
2838 -- the procedure itself as needing debug info. The latter is important
2839 -- when analyzing decision coverage (for example, for MC/DC coverage).
2840
2841 if Chars (Body_Id) = Name_uPostconditions then
2842 Set_Has_Postconditions (Current_Scope);
2843 Set_Debug_Info_Needed (Body_Id);
2844 end if;
2845
2846 -- Place subprogram on scope stack, and make formals visible. If there
2847 -- is a spec, the visible entity remains that of the spec.
2848
2849 if Present (Spec_Id) then
2850 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2851
2852 if Is_Child_Unit (Spec_Id) then
2853 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2854 end if;
2855
2856 if Style_Check then
2857 Style.Check_Identifier (Body_Id, Spec_Id);
2858 end if;
2859
2860 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2861 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2862
2863 if Is_Abstract_Subprogram (Spec_Id) then
2864 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2865 return;
2866
2867 else
2868 Set_Convention (Body_Id, Convention (Spec_Id));
2869 Set_Has_Completion (Spec_Id);
2870
2871 if Is_Protected_Type (Scope (Spec_Id)) then
2872 Prot_Typ := Scope (Spec_Id);
2873 end if;
2874
2875 -- If this is a body generated for a renaming, do not check for
2876 -- full conformance. The check is redundant, because the spec of
2877 -- the body is a copy of the spec in the renaming declaration,
2878 -- and the test can lead to spurious errors on nested defaults.
2879
2880 if Present (Spec_Decl)
2881 and then not Comes_From_Source (N)
2882 and then
2883 (Nkind (Original_Node (Spec_Decl)) =
2884 N_Subprogram_Renaming_Declaration
2885 or else (Present (Corresponding_Body (Spec_Decl))
2886 and then
2887 Nkind (Unit_Declaration_Node
2888 (Corresponding_Body (Spec_Decl))) =
2889 N_Subprogram_Renaming_Declaration))
2890 then
2891 Conformant := True;
2892
2893 -- Conversely, the spec may have been generated for specless body
2894 -- with an inline pragma.
2895
2896 elsif Comes_From_Source (N)
2897 and then not Comes_From_Source (Spec_Id)
2898 and then Has_Pragma_Inline (Spec_Id)
2899 then
2900 Conformant := True;
2901
2902 else
2903 Check_Conformance
2904 (Body_Id, Spec_Id,
2905 Fully_Conformant, True, Conformant, Body_Id);
2906 end if;
2907
2908 -- If the body is not fully conformant, we have to decide if we
2909 -- should analyze it or not. If it has a really messed up profile
2910 -- then we probably should not analyze it, since we will get too
2911 -- many bogus messages.
2912
2913 -- Our decision is to go ahead in the non-fully conformant case
2914 -- only if it is at least mode conformant with the spec. Note
2915 -- that the call to Check_Fully_Conformant has issued the proper
2916 -- error messages to complain about the lack of conformance.
2917
2918 if not Conformant
2919 and then not Mode_Conformant (Body_Id, Spec_Id)
2920 then
2921 return;
2922 end if;
2923 end if;
2924
2925 if Spec_Id /= Body_Id then
2926 Reference_Body_Formals (Spec_Id, Body_Id);
2927 end if;
2928
2929 Set_Ekind (Body_Id, E_Subprogram_Body);
2930
2931 if Nkind (N) = N_Subprogram_Body_Stub then
2932 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
2933
2934 -- Regular body
2935
2936 else
2937 Set_Corresponding_Spec (N, Spec_Id);
2938
2939 -- Ada 2005 (AI-345): If the operation is a primitive operation
2940 -- of a concurrent type, the type of the first parameter has been
2941 -- replaced with the corresponding record, which is the proper
2942 -- run-time structure to use. However, within the body there may
2943 -- be uses of the formals that depend on primitive operations
2944 -- of the type (in particular calls in prefixed form) for which
2945 -- we need the original concurrent type. The operation may have
2946 -- several controlling formals, so the replacement must be done
2947 -- for all of them.
2948
2949 if Comes_From_Source (Spec_Id)
2950 and then Present (First_Entity (Spec_Id))
2951 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2952 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2953 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
2954 and then Present (Corresponding_Concurrent_Type
2955 (Etype (First_Entity (Spec_Id))))
2956 then
2957 declare
2958 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2959 Form : Entity_Id;
2960
2961 begin
2962 Form := First_Formal (Spec_Id);
2963 while Present (Form) loop
2964 if Etype (Form) = Typ then
2965 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2966 end if;
2967
2968 Next_Formal (Form);
2969 end loop;
2970 end;
2971 end if;
2972
2973 -- Make the formals visible, and place subprogram on scope stack.
2974 -- This is also the point at which we set Last_Real_Spec_Entity
2975 -- to mark the entities which will not be moved to the body.
2976
2977 Install_Formals (Spec_Id);
2978 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2979
2980 -- Within an instance, add local renaming declarations so that
2981 -- gdb can retrieve the values of actuals more easily. This is
2982 -- only relevant if generating code (and indeed we definitely
2983 -- do not want these definitions -gnatc mode, because that would
2984 -- confuse ASIS).
2985
2986 if Is_Generic_Instance (Spec_Id)
2987 and then Is_Wrapper_Package (Current_Scope)
2988 and then Expander_Active
2989 then
2990 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2991 end if;
2992
2993 Push_Scope (Spec_Id);
2994
2995 -- Set SPARK_Mode from spec if spec had a SPARK_Mode pragma
2996
2997 if Present (SPARK_Pragma (Spec_Id)) then
2998 SPARK_Mode_Pragma := SPARK_Pragma (Spec_Id);
2999 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Mode_Pragma);
3000 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
3001 Set_SPARK_Pragma_Inherited (Body_Id, True);
3002
3003 -- Otherwise set from context
3004
3005 else
3006 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3007 Set_SPARK_Pragma_Inherited (Body_Id, True);
3008 end if;
3009
3010 -- Make sure that the subprogram is immediately visible. For
3011 -- child units that have no separate spec this is indispensable.
3012 -- Otherwise it is safe albeit redundant.
3013
3014 Set_Is_Immediately_Visible (Spec_Id);
3015 end if;
3016
3017 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3018 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3019 Set_Scope (Body_Id, Scope (Spec_Id));
3020 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3021
3022 -- Case of subprogram body with no previous spec
3023
3024 else
3025 -- Check for style warning required
3026
3027 if Style_Check
3028
3029 -- Only apply check for source level subprograms for which checks
3030 -- have not been suppressed.
3031
3032 and then Comes_From_Source (Body_Id)
3033 and then not Suppress_Style_Checks (Body_Id)
3034
3035 -- No warnings within an instance
3036
3037 and then not In_Instance
3038
3039 -- No warnings for expression functions
3040
3041 and then Nkind (Original_Node (N)) /= N_Expression_Function
3042 then
3043 Style.Body_With_No_Spec (N);
3044 end if;
3045
3046 New_Overloaded_Entity (Body_Id);
3047
3048 if Nkind (N) /= N_Subprogram_Body_Stub then
3049 Set_Acts_As_Spec (N);
3050 Generate_Definition (Body_Id);
3051 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3052 Generate_Reference
3053 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3054 Install_Formals (Body_Id);
3055 Push_Scope (Body_Id);
3056 end if;
3057
3058 -- For stubs and bodies with no previous spec, generate references to
3059 -- formals.
3060
3061 Generate_Reference_To_Formals (Body_Id);
3062 end if;
3063
3064 -- If the return type is an anonymous access type whose designated type
3065 -- is the limited view of a class-wide type and the non-limited view is
3066 -- available, update the return type accordingly.
3067
3068 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3069 declare
3070 Etyp : Entity_Id;
3071 Rtyp : Entity_Id;
3072
3073 begin
3074 Rtyp := Etype (Current_Scope);
3075
3076 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3077 Etyp := Directly_Designated_Type (Rtyp);
3078
3079 if Is_Class_Wide_Type (Etyp)
3080 and then From_Limited_With (Etyp)
3081 then
3082 Set_Directly_Designated_Type
3083 (Etype (Current_Scope), Available_View (Etyp));
3084 end if;
3085 end if;
3086 end;
3087 end if;
3088
3089 -- If this is the proper body of a stub, we must verify that the stub
3090 -- conforms to the body, and to the previous spec if one was present.
3091 -- We know already that the body conforms to that spec. This test is
3092 -- only required for subprograms that come from source.
3093
3094 if Nkind (Parent (N)) = N_Subunit
3095 and then Comes_From_Source (N)
3096 and then not Error_Posted (Body_Id)
3097 and then Nkind (Corresponding_Stub (Parent (N))) =
3098 N_Subprogram_Body_Stub
3099 then
3100 declare
3101 Old_Id : constant Entity_Id :=
3102 Defining_Entity
3103 (Specification (Corresponding_Stub (Parent (N))));
3104
3105 Conformant : Boolean := False;
3106
3107 begin
3108 if No (Spec_Id) then
3109 Check_Fully_Conformant (Body_Id, Old_Id);
3110
3111 else
3112 Check_Conformance
3113 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3114
3115 if not Conformant then
3116
3117 -- The stub was taken to be a new declaration. Indicate that
3118 -- it lacks a body.
3119
3120 Set_Has_Completion (Old_Id, False);
3121 end if;
3122 end if;
3123 end;
3124 end if;
3125
3126 Set_Has_Completion (Body_Id);
3127 Check_Eliminated (Body_Id);
3128
3129 if Nkind (N) = N_Subprogram_Body_Stub then
3130 return;
3131 end if;
3132
3133 -- Handle frontend inlining. There is no need to prepare us for inlining
3134 -- if we will not generate the code.
3135
3136 -- Old semantics
3137
3138 if not Debug_Flag_Dot_K then
3139 if Present (Spec_Id)
3140 and then Expander_Active
3141 and then
3142 (Has_Pragma_Inline_Always (Spec_Id)
3143 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3144 then
3145 Build_Body_To_Inline (N, Spec_Id);
3146 end if;
3147
3148 -- New semantics
3149
3150 elsif Expander_Active
3151 and then Serious_Errors_Detected = 0
3152 and then Present (Spec_Id)
3153 and then Has_Pragma_Inline (Spec_Id)
3154 then
3155 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3156 end if;
3157
3158 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3159 -- of the specification we have to install the private withed units.
3160 -- This holds for child units as well.
3161
3162 if Is_Compilation_Unit (Body_Id)
3163 or else Nkind (Parent (N)) = N_Compilation_Unit
3164 then
3165 Install_Private_With_Clauses (Body_Id);
3166 end if;
3167
3168 Check_Anonymous_Return;
3169
3170 -- Set the Protected_Formal field of each extra formal of the protected
3171 -- subprogram to reference the corresponding extra formal of the
3172 -- subprogram that implements it. For regular formals this occurs when
3173 -- the protected subprogram's declaration is expanded, but the extra
3174 -- formals don't get created until the subprogram is frozen. We need to
3175 -- do this before analyzing the protected subprogram's body so that any
3176 -- references to the original subprogram's extra formals will be changed
3177 -- refer to the implementing subprogram's formals (see Expand_Formal).
3178
3179 if Present (Spec_Id)
3180 and then Is_Protected_Type (Scope (Spec_Id))
3181 and then Present (Protected_Body_Subprogram (Spec_Id))
3182 then
3183 declare
3184 Impl_Subp : constant Entity_Id :=
3185 Protected_Body_Subprogram (Spec_Id);
3186 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3187 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3188 begin
3189 while Present (Prot_Ext_Formal) loop
3190 pragma Assert (Present (Impl_Ext_Formal));
3191 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3192 Next_Formal_With_Extras (Prot_Ext_Formal);
3193 Next_Formal_With_Extras (Impl_Ext_Formal);
3194 end loop;
3195 end;
3196 end if;
3197
3198 -- Now we can go on to analyze the body
3199
3200 HSS := Handled_Statement_Sequence (N);
3201 Set_Actual_Subtypes (N, Current_Scope);
3202
3203 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3204 -- invariants and predicates associated with the body and its spec.
3205 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3206 -- prepares the contract assertions for generic subprograms or for ASIS.
3207 -- Do not generate contract checks in SPARK mode.
3208
3209 if not GNATprove_Mode then
3210 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3211 end if;
3212
3213 -- Add a declaration for the Protection object, renaming declarations
3214 -- for discriminals and privals and finally a declaration for the entry
3215 -- family index (if applicable). This form of early expansion is done
3216 -- when the Expander is active because Install_Private_Data_Declarations
3217 -- references entities which were created during regular expansion. The
3218 -- body may be the rewritting of an expression function, and we need to
3219 -- verify that the original node is in the source.
3220
3221 if Expander_Active
3222 and then Comes_From_Source (Original_Node (N))
3223 and then Present (Prot_Typ)
3224 and then Present (Spec_Id)
3225 and then not Is_Eliminated (Spec_Id)
3226 then
3227 Install_Private_Data_Declarations
3228 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3229 end if;
3230
3231 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3232 -- may now appear in parameter and result profiles. Since the analysis
3233 -- of a subprogram body may use the parameter and result profile of the
3234 -- spec, swap any limited views with their non-limited counterpart.
3235
3236 if Ada_Version >= Ada_2012 then
3237 Exchange_Limited_Views (Spec_Id);
3238 end if;
3239
3240 -- Analyze the declarations (this call will analyze the precondition
3241 -- Check pragmas we prepended to the list, as well as the declaration
3242 -- of the _Postconditions procedure).
3243
3244 Analyze_Declarations (Declarations (N));
3245
3246 -- Check completion, and analyze the statements
3247
3248 Check_Completion;
3249 Inspect_Deferred_Constant_Completion (Declarations (N));
3250 Analyze (HSS);
3251
3252 -- Deal with end of scope processing for the body
3253
3254 Process_End_Label (HSS, 't', Current_Scope);
3255 End_Scope;
3256 Check_Subprogram_Order (N);
3257 Set_Analyzed (Body_Id);
3258
3259 -- If we have a separate spec, then the analysis of the declarations
3260 -- caused the entities in the body to be chained to the spec id, but
3261 -- we want them chained to the body id. Only the formal parameters
3262 -- end up chained to the spec id in this case.
3263
3264 if Present (Spec_Id) then
3265
3266 -- We must conform to the categorization of our spec
3267
3268 Validate_Categorization_Dependency (N, Spec_Id);
3269
3270 -- And if this is a child unit, the parent units must conform
3271
3272 if Is_Child_Unit (Spec_Id) then
3273 Validate_Categorization_Dependency
3274 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3275 end if;
3276
3277 -- Here is where we move entities from the spec to the body
3278
3279 -- Case where there are entities that stay with the spec
3280
3281 if Present (Last_Real_Spec_Entity) then
3282
3283 -- No body entities (happens when the only real spec entities come
3284 -- from precondition and postcondition pragmas).
3285
3286 if No (Last_Entity (Body_Id)) then
3287 Set_First_Entity
3288 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3289
3290 -- Body entities present (formals), so chain stuff past them
3291
3292 else
3293 Set_Next_Entity
3294 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3295 end if;
3296
3297 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3298 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3299 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3300
3301 -- Case where there are no spec entities, in this case there can be
3302 -- no body entities either, so just move everything.
3303
3304 else
3305 pragma Assert (No (Last_Entity (Body_Id)));
3306 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3307 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3308 Set_First_Entity (Spec_Id, Empty);
3309 Set_Last_Entity (Spec_Id, Empty);
3310 end if;
3311 end if;
3312
3313 Check_Missing_Return;
3314
3315 -- Now we are going to check for variables that are never modified in
3316 -- the body of the procedure. But first we deal with a special case
3317 -- where we want to modify this check. If the body of the subprogram
3318 -- starts with a raise statement or its equivalent, or if the body
3319 -- consists entirely of a null statement, then it is pretty obvious
3320 -- that it is OK to not reference the parameters. For example, this
3321 -- might be the following common idiom for a stubbed function:
3322 -- statement of the procedure raises an exception. In particular this
3323 -- deals with the common idiom of a stubbed function, which might
3324 -- appear as something like:
3325
3326 -- function F (A : Integer) return Some_Type;
3327 -- X : Some_Type;
3328 -- begin
3329 -- raise Program_Error;
3330 -- return X;
3331 -- end F;
3332
3333 -- Here the purpose of X is simply to satisfy the annoying requirement
3334 -- in Ada that there be at least one return, and we certainly do not
3335 -- want to go posting warnings on X that it is not initialized! On
3336 -- the other hand, if X is entirely unreferenced that should still
3337 -- get a warning.
3338
3339 -- What we do is to detect these cases, and if we find them, flag the
3340 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3341 -- suppress unwanted warnings. For the case of the function stub above
3342 -- we have a special test to set X as apparently assigned to suppress
3343 -- the warning.
3344
3345 declare
3346 Stm : Node_Id;
3347
3348 begin
3349 -- Skip initial labels (for one thing this occurs when we are in
3350 -- front end ZCX mode, but in any case it is irrelevant), and also
3351 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3352
3353 Stm := First (Statements (HSS));
3354 while Nkind (Stm) = N_Label
3355 or else Nkind (Stm) in N_Push_xxx_Label
3356 loop
3357 Next (Stm);
3358 end loop;
3359
3360 -- Do the test on the original statement before expansion
3361
3362 declare
3363 Ostm : constant Node_Id := Original_Node (Stm);
3364
3365 begin
3366 -- If explicit raise statement, turn on flag
3367
3368 if Nkind (Ostm) = N_Raise_Statement then
3369 Set_Trivial_Subprogram (Stm);
3370
3371 -- If null statement, and no following statements, turn on flag
3372
3373 elsif Nkind (Stm) = N_Null_Statement
3374 and then Comes_From_Source (Stm)
3375 and then No (Next (Stm))
3376 then
3377 Set_Trivial_Subprogram (Stm);
3378
3379 -- Check for explicit call cases which likely raise an exception
3380
3381 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3382 if Is_Entity_Name (Name (Ostm)) then
3383 declare
3384 Ent : constant Entity_Id := Entity (Name (Ostm));
3385
3386 begin
3387 -- If the procedure is marked No_Return, then likely it
3388 -- raises an exception, but in any case it is not coming
3389 -- back here, so turn on the flag.
3390
3391 if Present (Ent)
3392 and then Ekind (Ent) = E_Procedure
3393 and then No_Return (Ent)
3394 then
3395 Set_Trivial_Subprogram (Stm);
3396 end if;
3397 end;
3398 end if;
3399 end if;
3400 end;
3401 end;
3402
3403 -- Check for variables that are never modified
3404
3405 declare
3406 E1, E2 : Entity_Id;
3407
3408 begin
3409 -- If there is a separate spec, then transfer Never_Set_In_Source
3410 -- flags from out parameters to the corresponding entities in the
3411 -- body. The reason we do that is we want to post error flags on
3412 -- the body entities, not the spec entities.
3413
3414 if Present (Spec_Id) then
3415 E1 := First_Entity (Spec_Id);
3416 while Present (E1) loop
3417 if Ekind (E1) = E_Out_Parameter then
3418 E2 := First_Entity (Body_Id);
3419 while Present (E2) loop
3420 exit when Chars (E1) = Chars (E2);
3421 Next_Entity (E2);
3422 end loop;
3423
3424 if Present (E2) then
3425 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3426 end if;
3427 end if;
3428
3429 Next_Entity (E1);
3430 end loop;
3431 end if;
3432
3433 -- Check references in body
3434
3435 Check_References (Body_Id);
3436 end;
3437 end Analyze_Subprogram_Body_Helper;
3438
3439 ---------------------------------
3440 -- Analyze_Subprogram_Contract --
3441 ---------------------------------
3442
3443 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3444 Items : constant Node_Id := Contract (Subp);
3445 Case_Prag : Node_Id := Empty;
3446 Depends : Node_Id := Empty;
3447 Global : Node_Id := Empty;
3448 Nam : Name_Id;
3449 Post_Prag : Node_Id := Empty;
3450 Prag : Node_Id;
3451 Seen_In_Case : Boolean := False;
3452 Seen_In_Post : Boolean := False;
3453
3454 begin
3455 if Present (Items) then
3456
3457 -- Analyze pre- and postconditions
3458
3459 Prag := Pre_Post_Conditions (Items);
3460 while Present (Prag) loop
3461 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3462
3463 -- Verify whether a postcondition mentions attribute 'Result and
3464 -- its expression introduces a post-state.
3465
3466 if Warn_On_Suspicious_Contract
3467 and then Pragma_Name (Prag) = Name_Postcondition
3468 then
3469 Post_Prag := Prag;
3470 Check_Result_And_Post_State (Prag, Seen_In_Post);
3471 end if;
3472
3473 Prag := Next_Pragma (Prag);
3474 end loop;
3475
3476 -- Analyze contract-cases and test-cases
3477
3478 Prag := Contract_Test_Cases (Items);
3479 while Present (Prag) loop
3480 Nam := Pragma_Name (Prag);
3481
3482 if Nam = Name_Contract_Cases then
3483 Analyze_Contract_Cases_In_Decl_Part (Prag);
3484
3485 -- Verify whether contract-cases mention attribute 'Result and
3486 -- its expression introduces a post-state. Perform the check
3487 -- only when the pragma is legal.
3488
3489 if Warn_On_Suspicious_Contract
3490 and then not Error_Posted (Prag)
3491 then
3492 Case_Prag := Prag;
3493 Check_Result_And_Post_State (Prag, Seen_In_Case);
3494 end if;
3495
3496 else
3497 pragma Assert (Nam = Name_Test_Case);
3498 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3499 end if;
3500
3501 Prag := Next_Pragma (Prag);
3502 end loop;
3503
3504 -- Analyze classification pragmas
3505
3506 Prag := Classifications (Items);
3507 while Present (Prag) loop
3508 Nam := Pragma_Name (Prag);
3509
3510 if Nam = Name_Depends then
3511 Depends := Prag;
3512 else pragma Assert (Nam = Name_Global);
3513 Global := Prag;
3514 end if;
3515
3516 Prag := Next_Pragma (Prag);
3517 end loop;
3518
3519 -- Analyze Global first as Depends may mention items classified in
3520 -- the global categorization.
3521
3522 if Present (Global) then
3523 Analyze_Global_In_Decl_Part (Global);
3524 end if;
3525
3526 -- Depends must be analyzed after Global in order to see the modes of
3527 -- all global items.
3528
3529 if Present (Depends) then
3530 Analyze_Depends_In_Decl_Part (Depends);
3531 end if;
3532 end if;
3533
3534 -- Emit an error when neither the postconditions nor the contract-cases
3535 -- mention attribute 'Result in the context of a function.
3536
3537 if Warn_On_Suspicious_Contract
3538 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3539 then
3540 if Present (Case_Prag)
3541 and then not Seen_In_Case
3542 and then Present (Post_Prag)
3543 and then not Seen_In_Post
3544 then
3545 Error_Msg_N
3546 ("neither function postcondition nor contract cases mention "
3547 & "result?T?", Post_Prag);
3548
3549 elsif Present (Case_Prag) and then not Seen_In_Case then
3550 Error_Msg_N
3551 ("contract cases do not mention result?T?", Case_Prag);
3552
3553 elsif Present (Post_Prag) and then not Seen_In_Post then
3554 Error_Msg_N
3555 ("function postcondition does not mention result?T?", Post_Prag);
3556 end if;
3557 end if;
3558 end Analyze_Subprogram_Contract;
3559
3560 ------------------------------------
3561 -- Analyze_Subprogram_Declaration --
3562 ------------------------------------
3563
3564 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3565 Scop : constant Entity_Id := Current_Scope;
3566 Designator : Entity_Id;
3567
3568 Is_Completion : Boolean;
3569 -- Indicates whether a null procedure declaration is a completion
3570
3571 begin
3572 -- Null procedures are not allowed in SPARK
3573
3574 if Nkind (Specification (N)) = N_Procedure_Specification
3575 and then Null_Present (Specification (N))
3576 then
3577 Check_SPARK_Restriction ("null procedure is not allowed", N);
3578
3579 if Is_Protected_Type (Current_Scope) then
3580 Error_Msg_N ("protected operation cannot be a null procedure", N);
3581 end if;
3582
3583 Analyze_Null_Procedure (N, Is_Completion);
3584
3585 if Is_Completion then
3586
3587 -- The null procedure acts as a body, nothing further is needed.
3588
3589 return;
3590 end if;
3591 end if;
3592
3593 Designator := Analyze_Subprogram_Specification (Specification (N));
3594
3595 -- A reference may already have been generated for the unit name, in
3596 -- which case the following call is redundant. However it is needed for
3597 -- declarations that are the rewriting of an expression function.
3598
3599 Generate_Definition (Designator);
3600
3601 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
3602 Set_SPARK_Pragma_Inherited (Designator, True);
3603
3604 if Debug_Flag_C then
3605 Write_Str ("==> subprogram spec ");
3606 Write_Name (Chars (Designator));
3607 Write_Str (" from ");
3608 Write_Location (Sloc (N));
3609 Write_Eol;
3610 Indent;
3611 end if;
3612
3613 Validate_RCI_Subprogram_Declaration (N);
3614 New_Overloaded_Entity (Designator);
3615 Check_Delayed_Subprogram (Designator);
3616
3617 -- If the type of the first formal of the current subprogram is a
3618 -- non-generic tagged private type, mark the subprogram as being a
3619 -- private primitive. Ditto if this is a function with controlling
3620 -- result, and the return type is currently private. In both cases,
3621 -- the type of the controlling argument or result must be in the
3622 -- current scope for the operation to be primitive.
3623
3624 if Has_Controlling_Result (Designator)
3625 and then Is_Private_Type (Etype (Designator))
3626 and then Scope (Etype (Designator)) = Current_Scope
3627 and then not Is_Generic_Actual_Type (Etype (Designator))
3628 then
3629 Set_Is_Private_Primitive (Designator);
3630
3631 elsif Present (First_Formal (Designator)) then
3632 declare
3633 Formal_Typ : constant Entity_Id :=
3634 Etype (First_Formal (Designator));
3635 begin
3636 Set_Is_Private_Primitive (Designator,
3637 Is_Tagged_Type (Formal_Typ)
3638 and then Scope (Formal_Typ) = Current_Scope
3639 and then Is_Private_Type (Formal_Typ)
3640 and then not Is_Generic_Actual_Type (Formal_Typ));
3641 end;
3642 end if;
3643
3644 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3645 -- or null.
3646
3647 if Ada_Version >= Ada_2005
3648 and then Comes_From_Source (N)
3649 and then Is_Dispatching_Operation (Designator)
3650 then
3651 declare
3652 E : Entity_Id;
3653 Etyp : Entity_Id;
3654
3655 begin
3656 if Has_Controlling_Result (Designator) then
3657 Etyp := Etype (Designator);
3658
3659 else
3660 E := First_Entity (Designator);
3661 while Present (E)
3662 and then Is_Formal (E)
3663 and then not Is_Controlling_Formal (E)
3664 loop
3665 Next_Entity (E);
3666 end loop;
3667
3668 Etyp := Etype (E);
3669 end if;
3670
3671 if Is_Access_Type (Etyp) then
3672 Etyp := Directly_Designated_Type (Etyp);
3673 end if;
3674
3675 if Is_Interface (Etyp)
3676 and then not Is_Abstract_Subprogram (Designator)
3677 and then not (Ekind (Designator) = E_Procedure
3678 and then Null_Present (Specification (N)))
3679 then
3680 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3681
3682 -- Specialize error message based on procedures vs. functions,
3683 -- since functions can't be null subprograms.
3684
3685 if Ekind (Designator) = E_Procedure then
3686 Error_Msg_N
3687 ("interface procedure % must be abstract or null", N);
3688 else
3689 Error_Msg_N ("interface function % must be abstract", N);
3690 end if;
3691 end if;
3692 end;
3693 end if;
3694
3695 -- What is the following code for, it used to be
3696
3697 -- ??? Set_Suppress_Elaboration_Checks
3698 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3699
3700 -- The following seems equivalent, but a bit dubious
3701
3702 if Elaboration_Checks_Suppressed (Designator) then
3703 Set_Kill_Elaboration_Checks (Designator);
3704 end if;
3705
3706 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3707 Set_Categorization_From_Scope (Designator, Scop);
3708
3709 else
3710 -- For a compilation unit, check for library-unit pragmas
3711
3712 Push_Scope (Designator);
3713 Set_Categorization_From_Pragmas (N);
3714 Validate_Categorization_Dependency (N, Designator);
3715 Pop_Scope;
3716 end if;
3717
3718 -- For a compilation unit, set body required. This flag will only be
3719 -- reset if a valid Import or Interface pragma is processed later on.
3720
3721 if Nkind (Parent (N)) = N_Compilation_Unit then
3722 Set_Body_Required (Parent (N), True);
3723
3724 if Ada_Version >= Ada_2005
3725 and then Nkind (Specification (N)) = N_Procedure_Specification
3726 and then Null_Present (Specification (N))
3727 then
3728 Error_Msg_N
3729 ("null procedure cannot be declared at library level", N);
3730 end if;
3731 end if;
3732
3733 Generate_Reference_To_Formals (Designator);
3734 Check_Eliminated (Designator);
3735
3736 if Debug_Flag_C then
3737 Outdent;
3738 Write_Str ("<== subprogram spec ");
3739 Write_Name (Chars (Designator));
3740 Write_Str (" from ");
3741 Write_Location (Sloc (N));
3742 Write_Eol;
3743 end if;
3744
3745 if Is_Protected_Type (Current_Scope) then
3746
3747 -- Indicate that this is a protected operation, because it may be
3748 -- used in subsequent declarations within the protected type.
3749
3750 Set_Convention (Designator, Convention_Protected);
3751 end if;
3752
3753 List_Inherited_Pre_Post_Aspects (Designator);
3754
3755 if Has_Aspects (N) then
3756 Analyze_Aspect_Specifications (N, Designator);
3757 end if;
3758 end Analyze_Subprogram_Declaration;
3759
3760 --------------------------------------
3761 -- Analyze_Subprogram_Specification --
3762 --------------------------------------
3763
3764 -- Reminder: N here really is a subprogram specification (not a subprogram
3765 -- declaration). This procedure is called to analyze the specification in
3766 -- both subprogram bodies and subprogram declarations (specs).
3767
3768 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3769 Designator : constant Entity_Id := Defining_Entity (N);
3770 Formals : constant List_Id := Parameter_Specifications (N);
3771
3772 -- Start of processing for Analyze_Subprogram_Specification
3773
3774 begin
3775 -- User-defined operator is not allowed in SPARK, except as a renaming
3776
3777 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3778 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3779 then
3780 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3781 end if;
3782
3783 -- Proceed with analysis. Do not emit a cross-reference entry if the
3784 -- specification comes from an expression function, because it may be
3785 -- the completion of a previous declaration. It is is not, the cross-
3786 -- reference entry will be emitted for the new subprogram declaration.
3787
3788 if Nkind (Parent (N)) /= N_Expression_Function then
3789 Generate_Definition (Designator);
3790 end if;
3791
3792 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3793
3794 if Nkind (N) = N_Function_Specification then
3795 Set_Ekind (Designator, E_Function);
3796 Set_Mechanism (Designator, Default_Mechanism);
3797 else
3798 Set_Ekind (Designator, E_Procedure);
3799 Set_Etype (Designator, Standard_Void_Type);
3800 end if;
3801
3802 -- Introduce new scope for analysis of the formals and the return type
3803
3804 Set_Scope (Designator, Current_Scope);
3805
3806 if Present (Formals) then
3807 Push_Scope (Designator);
3808 Process_Formals (Formals, N);
3809
3810 -- Check dimensions in N for formals with default expression
3811
3812 Analyze_Dimension_Formals (N, Formals);
3813
3814 -- Ada 2005 (AI-345): If this is an overriding operation of an
3815 -- inherited interface operation, and the controlling type is
3816 -- a synchronized type, replace the type with its corresponding
3817 -- record, to match the proper signature of an overriding operation.
3818 -- Same processing for an access parameter whose designated type is
3819 -- derived from a synchronized interface.
3820
3821 if Ada_Version >= Ada_2005 then
3822 declare
3823 Formal : Entity_Id;
3824 Formal_Typ : Entity_Id;
3825 Rec_Typ : Entity_Id;
3826 Desig_Typ : Entity_Id;
3827
3828 begin
3829 Formal := First_Formal (Designator);
3830 while Present (Formal) loop
3831 Formal_Typ := Etype (Formal);
3832
3833 if Is_Concurrent_Type (Formal_Typ)
3834 and then Present (Corresponding_Record_Type (Formal_Typ))
3835 then
3836 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3837
3838 if Present (Interfaces (Rec_Typ)) then
3839 Set_Etype (Formal, Rec_Typ);
3840 end if;
3841
3842 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3843 Desig_Typ := Designated_Type (Formal_Typ);
3844
3845 if Is_Concurrent_Type (Desig_Typ)
3846 and then Present (Corresponding_Record_Type (Desig_Typ))
3847 then
3848 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3849
3850 if Present (Interfaces (Rec_Typ)) then
3851 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3852 end if;
3853 end if;
3854 end if;
3855
3856 Next_Formal (Formal);
3857 end loop;
3858 end;
3859 end if;
3860
3861 End_Scope;
3862
3863 -- The subprogram scope is pushed and popped around the processing of
3864 -- the return type for consistency with call above to Process_Formals
3865 -- (which itself can call Analyze_Return_Type), and to ensure that any
3866 -- itype created for the return type will be associated with the proper
3867 -- scope.
3868
3869 elsif Nkind (N) = N_Function_Specification then
3870 Push_Scope (Designator);
3871 Analyze_Return_Type (N);
3872 End_Scope;
3873 end if;
3874
3875 -- Function case
3876
3877 if Nkind (N) = N_Function_Specification then
3878
3879 -- Deal with operator symbol case
3880
3881 if Nkind (Designator) = N_Defining_Operator_Symbol then
3882 Valid_Operator_Definition (Designator);
3883 end if;
3884
3885 May_Need_Actuals (Designator);
3886
3887 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3888 -- the subprogram is abstract also. This does not apply to renaming
3889 -- declarations, where abstractness is inherited, and to subprogram
3890 -- bodies generated for stream operations, which become renamings as
3891 -- bodies.
3892
3893 -- In case of primitives associated with abstract interface types
3894 -- the check is applied later (see Analyze_Subprogram_Declaration).
3895
3896 if not Nkind_In (Original_Node (Parent (N)),
3897 N_Subprogram_Renaming_Declaration,
3898 N_Abstract_Subprogram_Declaration,
3899 N_Formal_Abstract_Subprogram_Declaration)
3900 then
3901 if Is_Abstract_Type (Etype (Designator))
3902 and then not Is_Interface (Etype (Designator))
3903 then
3904 Error_Msg_N
3905 ("function that returns abstract type must be abstract", N);
3906
3907 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3908 -- access result whose designated type is abstract.
3909
3910 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3911 and then
3912 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3913 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3914 and then Ada_Version >= Ada_2012
3915 then
3916 Error_Msg_N ("function whose access result designates "
3917 & "abstract type must be abstract", N);
3918 end if;
3919 end if;
3920 end if;
3921
3922 return Designator;
3923 end Analyze_Subprogram_Specification;
3924
3925 --------------------------
3926 -- Build_Body_To_Inline --
3927 --------------------------
3928
3929 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3930 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3931 Original_Body : Node_Id;
3932 Body_To_Analyze : Node_Id;
3933 Max_Size : constant := 10;
3934 Stat_Count : Integer := 0;
3935
3936 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3937 -- Check for declarations that make inlining not worthwhile
3938
3939 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3940 -- Check for statements that make inlining not worthwhile: any tasking
3941 -- statement, nested at any level. Keep track of total number of
3942 -- elementary statements, as a measure of acceptable size.
3943
3944 function Has_Pending_Instantiation return Boolean;
3945 -- If some enclosing body contains instantiations that appear before the
3946 -- corresponding generic body, the enclosing body has a freeze node so
3947 -- that it can be elaborated after the generic itself. This might
3948 -- conflict with subsequent inlinings, so that it is unsafe to try to
3949 -- inline in such a case.
3950
3951 function Has_Single_Return return Boolean;
3952 -- In general we cannot inline functions that return unconstrained type.
3953 -- However, we can handle such functions if all return statements return
3954 -- a local variable that is the only declaration in the body of the
3955 -- function. In that case the call can be replaced by that local
3956 -- variable as is done for other inlined calls.
3957
3958 procedure Remove_Pragmas;
3959 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3960 -- parameter has no meaning when the body is inlined and the formals
3961 -- are rewritten. Remove it from body to inline. The analysis of the
3962 -- non-inlined body will handle the pragma properly.
3963
3964 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3965 -- If the body of the subprogram includes a call that returns an
3966 -- unconstrained type, the secondary stack is involved, and it
3967 -- is not worth inlining.
3968
3969 ------------------------------
3970 -- Has_Excluded_Declaration --
3971 ------------------------------
3972
3973 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3974 D : Node_Id;
3975
3976 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3977 -- Nested subprograms make a given body ineligible for inlining, but
3978 -- we make an exception for instantiations of unchecked conversion.
3979 -- The body has not been analyzed yet, so check the name, and verify
3980 -- that the visible entity with that name is the predefined unit.
3981
3982 -----------------------------
3983 -- Is_Unchecked_Conversion --
3984 -----------------------------
3985
3986 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3987 Id : constant Node_Id := Name (D);
3988 Conv : Entity_Id;
3989
3990 begin
3991 if Nkind (Id) = N_Identifier
3992 and then Chars (Id) = Name_Unchecked_Conversion
3993 then
3994 Conv := Current_Entity (Id);
3995
3996 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3997 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3998 then
3999 Conv := Current_Entity (Selector_Name (Id));
4000 else
4001 return False;
4002 end if;
4003
4004 return Present (Conv)
4005 and then Is_Predefined_File_Name
4006 (Unit_File_Name (Get_Source_Unit (Conv)))
4007 and then Is_Intrinsic_Subprogram (Conv);
4008 end Is_Unchecked_Conversion;
4009
4010 -- Start of processing for Has_Excluded_Declaration
4011
4012 begin
4013 D := First (Decls);
4014 while Present (D) loop
4015 if (Nkind (D) = N_Function_Instantiation
4016 and then not Is_Unchecked_Conversion (D))
4017 or else Nkind_In (D, N_Protected_Type_Declaration,
4018 N_Package_Declaration,
4019 N_Package_Instantiation,
4020 N_Subprogram_Body,
4021 N_Procedure_Instantiation,
4022 N_Task_Type_Declaration)
4023 then
4024 Cannot_Inline
4025 ("cannot inline & (non-allowed declaration)?", D, Subp);
4026 return True;
4027 end if;
4028
4029 Next (D);
4030 end loop;
4031
4032 return False;
4033 end Has_Excluded_Declaration;
4034
4035 ----------------------------
4036 -- Has_Excluded_Statement --
4037 ----------------------------
4038
4039 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4040 S : Node_Id;
4041 E : Node_Id;
4042
4043 begin
4044 S := First (Stats);
4045 while Present (S) loop
4046 Stat_Count := Stat_Count + 1;
4047
4048 if Nkind_In (S, N_Abort_Statement,
4049 N_Asynchronous_Select,
4050 N_Conditional_Entry_Call,
4051 N_Delay_Relative_Statement,
4052 N_Delay_Until_Statement,
4053 N_Selective_Accept,
4054 N_Timed_Entry_Call)
4055 then
4056 Cannot_Inline
4057 ("cannot inline & (non-allowed statement)?", S, Subp);
4058 return True;
4059
4060 elsif Nkind (S) = N_Block_Statement then
4061 if Present (Declarations (S))
4062 and then Has_Excluded_Declaration (Declarations (S))
4063 then
4064 return True;
4065
4066 elsif Present (Handled_Statement_Sequence (S))
4067 and then
4068 (Present
4069 (Exception_Handlers (Handled_Statement_Sequence (S)))
4070 or else
4071 Has_Excluded_Statement
4072 (Statements (Handled_Statement_Sequence (S))))
4073 then
4074 return True;
4075 end if;
4076
4077 elsif Nkind (S) = N_Case_Statement then
4078 E := First (Alternatives (S));
4079 while Present (E) loop
4080 if Has_Excluded_Statement (Statements (E)) then
4081 return True;
4082 end if;
4083
4084 Next (E);
4085 end loop;
4086
4087 elsif Nkind (S) = N_If_Statement then
4088 if Has_Excluded_Statement (Then_Statements (S)) then
4089 return True;
4090 end if;
4091
4092 if Present (Elsif_Parts (S)) then
4093 E := First (Elsif_Parts (S));
4094 while Present (E) loop
4095 if Has_Excluded_Statement (Then_Statements (E)) then
4096 return True;
4097 end if;
4098
4099 Next (E);
4100 end loop;
4101 end if;
4102
4103 if Present (Else_Statements (S))
4104 and then Has_Excluded_Statement (Else_Statements (S))
4105 then
4106 return True;
4107 end if;
4108
4109 elsif Nkind (S) = N_Loop_Statement
4110 and then Has_Excluded_Statement (Statements (S))
4111 then
4112 return True;
4113
4114 elsif Nkind (S) = N_Extended_Return_Statement then
4115 if Has_Excluded_Statement
4116 (Statements (Handled_Statement_Sequence (S)))
4117 or else Present
4118 (Exception_Handlers (Handled_Statement_Sequence (S)))
4119 then
4120 return True;
4121 end if;
4122 end if;
4123
4124 Next (S);
4125 end loop;
4126
4127 return False;
4128 end Has_Excluded_Statement;
4129
4130 -------------------------------
4131 -- Has_Pending_Instantiation --
4132 -------------------------------
4133
4134 function Has_Pending_Instantiation return Boolean is
4135 S : Entity_Id;
4136
4137 begin
4138 S := Current_Scope;
4139 while Present (S) loop
4140 if Is_Compilation_Unit (S)
4141 or else Is_Child_Unit (S)
4142 then
4143 return False;
4144
4145 elsif Ekind (S) = E_Package
4146 and then Has_Forward_Instantiation (S)
4147 then
4148 return True;
4149 end if;
4150
4151 S := Scope (S);
4152 end loop;
4153
4154 return False;
4155 end Has_Pending_Instantiation;
4156
4157 ------------------------
4158 -- Has_Single_Return --
4159 ------------------------
4160
4161 function Has_Single_Return return Boolean is
4162 Return_Statement : Node_Id := Empty;
4163
4164 function Check_Return (N : Node_Id) return Traverse_Result;
4165
4166 ------------------
4167 -- Check_Return --
4168 ------------------
4169
4170 function Check_Return (N : Node_Id) return Traverse_Result is
4171 begin
4172 if Nkind (N) = N_Simple_Return_Statement then
4173 if Present (Expression (N))
4174 and then Is_Entity_Name (Expression (N))
4175 then
4176 if No (Return_Statement) then
4177 Return_Statement := N;
4178 return OK;
4179
4180 elsif Chars (Expression (N)) =
4181 Chars (Expression (Return_Statement))
4182 then
4183 return OK;
4184
4185 else
4186 return Abandon;
4187 end if;
4188
4189 -- A return statement within an extended return is a noop
4190 -- after inlining.
4191
4192 elsif No (Expression (N))
4193 and then Nkind (Parent (Parent (N))) =
4194 N_Extended_Return_Statement
4195 then
4196 return OK;
4197
4198 else
4199 -- Expression has wrong form
4200
4201 return Abandon;
4202 end if;
4203
4204 -- We can only inline a build-in-place function if
4205 -- it has a single extended return.
4206
4207 elsif Nkind (N) = N_Extended_Return_Statement then
4208 if No (Return_Statement) then
4209 Return_Statement := N;
4210 return OK;
4211
4212 else
4213 return Abandon;
4214 end if;
4215
4216 else
4217 return OK;
4218 end if;
4219 end Check_Return;
4220
4221 function Check_All_Returns is new Traverse_Func (Check_Return);
4222
4223 -- Start of processing for Has_Single_Return
4224
4225 begin
4226 if Check_All_Returns (N) /= OK then
4227 return False;
4228
4229 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4230 return True;
4231
4232 else
4233 return Present (Declarations (N))
4234 and then Present (First (Declarations (N)))
4235 and then Chars (Expression (Return_Statement)) =
4236 Chars (Defining_Identifier (First (Declarations (N))));
4237 end if;
4238 end Has_Single_Return;
4239
4240 --------------------
4241 -- Remove_Pragmas --
4242 --------------------
4243
4244 procedure Remove_Pragmas is
4245 Decl : Node_Id;
4246 Nxt : Node_Id;
4247
4248 begin
4249 Decl := First (Declarations (Body_To_Analyze));
4250 while Present (Decl) loop
4251 Nxt := Next (Decl);
4252
4253 if Nkind (Decl) = N_Pragma
4254 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4255 Name_Unmodified)
4256 then
4257 Remove (Decl);
4258 end if;
4259
4260 Decl := Nxt;
4261 end loop;
4262 end Remove_Pragmas;
4263
4264 --------------------------
4265 -- Uses_Secondary_Stack --
4266 --------------------------
4267
4268 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4269 function Check_Call (N : Node_Id) return Traverse_Result;
4270 -- Look for function calls that return an unconstrained type
4271
4272 ----------------
4273 -- Check_Call --
4274 ----------------
4275
4276 function Check_Call (N : Node_Id) return Traverse_Result is
4277 begin
4278 if Nkind (N) = N_Function_Call
4279 and then Is_Entity_Name (Name (N))
4280 and then Is_Composite_Type (Etype (Entity (Name (N))))
4281 and then not Is_Constrained (Etype (Entity (Name (N))))
4282 then
4283 Cannot_Inline
4284 ("cannot inline & (call returns unconstrained type)?",
4285 N, Subp);
4286 return Abandon;
4287 else
4288 return OK;
4289 end if;
4290 end Check_Call;
4291
4292 function Check_Calls is new Traverse_Func (Check_Call);
4293
4294 begin
4295 return Check_Calls (Bod) = Abandon;
4296 end Uses_Secondary_Stack;
4297
4298 -- Start of processing for Build_Body_To_Inline
4299
4300 begin
4301 -- Return immediately if done already
4302
4303 if Nkind (Decl) = N_Subprogram_Declaration
4304 and then Present (Body_To_Inline (Decl))
4305 then
4306 return;
4307
4308 -- Functions that return unconstrained composite types require
4309 -- secondary stack handling, and cannot currently be inlined, unless
4310 -- all return statements return a local variable that is the first
4311 -- local declaration in the body.
4312
4313 elsif Ekind (Subp) = E_Function
4314 and then not Is_Scalar_Type (Etype (Subp))
4315 and then not Is_Access_Type (Etype (Subp))
4316 and then not Is_Constrained (Etype (Subp))
4317 then
4318 if not Has_Single_Return then
4319 Cannot_Inline
4320 ("cannot inline & (unconstrained return type)?", N, Subp);
4321 return;
4322 end if;
4323
4324 -- Ditto for functions that return controlled types, where controlled
4325 -- actions interfere in complex ways with inlining.
4326
4327 elsif Ekind (Subp) = E_Function
4328 and then Needs_Finalization (Etype (Subp))
4329 then
4330 Cannot_Inline
4331 ("cannot inline & (controlled return type)?", N, Subp);
4332 return;
4333 end if;
4334
4335 if Present (Declarations (N))
4336 and then Has_Excluded_Declaration (Declarations (N))
4337 then
4338 return;
4339 end if;
4340
4341 if Present (Handled_Statement_Sequence (N)) then
4342 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4343 Cannot_Inline
4344 ("cannot inline& (exception handler)?",
4345 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4346 Subp);
4347 return;
4348 elsif
4349 Has_Excluded_Statement
4350 (Statements (Handled_Statement_Sequence (N)))
4351 then
4352 return;
4353 end if;
4354 end if;
4355
4356 -- We do not inline a subprogram that is too large, unless it is
4357 -- marked Inline_Always. This pragma does not suppress the other
4358 -- checks on inlining (forbidden declarations, handlers, etc).
4359
4360 if Stat_Count > Max_Size
4361 and then not Has_Pragma_Inline_Always (Subp)
4362 then
4363 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4364 return;
4365 end if;
4366
4367 if Has_Pending_Instantiation then
4368 Cannot_Inline
4369 ("cannot inline& (forward instance within enclosing body)?",
4370 N, Subp);
4371 return;
4372 end if;
4373
4374 -- Within an instance, the body to inline must be treated as a nested
4375 -- generic, so that the proper global references are preserved.
4376
4377 -- Note that we do not do this at the library level, because it is not
4378 -- needed, and furthermore this causes trouble if front end inlining
4379 -- is activated (-gnatN).
4380
4381 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4382 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4383 Original_Body := Copy_Generic_Node (N, Empty, True);
4384 else
4385 Original_Body := Copy_Separate_Tree (N);
4386 end if;
4387
4388 -- We need to capture references to the formals in order to substitute
4389 -- the actuals at the point of inlining, i.e. instantiation. To treat
4390 -- the formals as globals to the body to inline, we nest it within
4391 -- a dummy parameterless subprogram, declared within the real one.
4392 -- To avoid generating an internal name (which is never public, and
4393 -- which affects serial numbers of other generated names), we use
4394 -- an internal symbol that cannot conflict with user declarations.
4395
4396 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4397 Set_Defining_Unit_Name
4398 (Specification (Original_Body),
4399 Make_Defining_Identifier (Sloc (N), Name_uParent));
4400 Set_Corresponding_Spec (Original_Body, Empty);
4401
4402 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4403
4404 -- Set return type of function, which is also global and does not need
4405 -- to be resolved.
4406
4407 if Ekind (Subp) = E_Function then
4408 Set_Result_Definition (Specification (Body_To_Analyze),
4409 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4410 end if;
4411
4412 if No (Declarations (N)) then
4413 Set_Declarations (N, New_List (Body_To_Analyze));
4414 else
4415 Append (Body_To_Analyze, Declarations (N));
4416 end if;
4417
4418 Expander_Mode_Save_And_Set (False);
4419 Remove_Pragmas;
4420
4421 Analyze (Body_To_Analyze);
4422 Push_Scope (Defining_Entity (Body_To_Analyze));
4423 Save_Global_References (Original_Body);
4424 End_Scope;
4425 Remove (Body_To_Analyze);
4426
4427 Expander_Mode_Restore;
4428
4429 -- Restore environment if previously saved
4430
4431 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4432 Restore_Env;
4433 end if;
4434
4435 -- If secondary stk used there is no point in inlining. We have
4436 -- already issued the warning in this case, so nothing to do.
4437
4438 if Uses_Secondary_Stack (Body_To_Analyze) then
4439 return;
4440 end if;
4441
4442 Set_Body_To_Inline (Decl, Original_Body);
4443 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4444 Set_Is_Inlined (Subp);
4445 end Build_Body_To_Inline;
4446
4447 -------------------
4448 -- Cannot_Inline --
4449 -------------------
4450
4451 procedure Cannot_Inline
4452 (Msg : String;
4453 N : Node_Id;
4454 Subp : Entity_Id;
4455 Is_Serious : Boolean := False)
4456 is
4457 begin
4458 pragma Assert (Msg (Msg'Last) = '?');
4459
4460 -- Old semantics
4461
4462 if not Debug_Flag_Dot_K then
4463
4464 -- Do not emit warning if this is a predefined unit which is not
4465 -- the main unit. With validity checks enabled, some predefined
4466 -- subprograms may contain nested subprograms and become ineligible
4467 -- for inlining.
4468
4469 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4470 and then not In_Extended_Main_Source_Unit (Subp)
4471 then
4472 null;
4473
4474 elsif Has_Pragma_Inline_Always (Subp) then
4475
4476 -- Remove last character (question mark) to make this into an
4477 -- error, because the Inline_Always pragma cannot be obeyed.
4478
4479 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4480
4481 elsif Ineffective_Inline_Warnings then
4482 Error_Msg_NE (Msg & "p?", N, Subp);
4483 end if;
4484
4485 return;
4486
4487 -- New semantics
4488
4489 elsif Is_Serious then
4490
4491 -- Remove last character (question mark) to make this into an error.
4492
4493 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4494
4495 elsif Optimization_Level = 0 then
4496
4497 -- Do not emit warning if this is a predefined unit which is not
4498 -- the main unit. This behavior is currently provided for backward
4499 -- compatibility but it will be removed when we enforce the
4500 -- strictness of the new rules.
4501
4502 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4503 and then not In_Extended_Main_Source_Unit (Subp)
4504 then
4505 null;
4506
4507 elsif Has_Pragma_Inline_Always (Subp) then
4508
4509 -- Emit a warning if this is a call to a runtime subprogram
4510 -- which is located inside a generic. Previously this call
4511 -- was silently skipped!
4512
4513 if Is_Generic_Instance (Subp) then
4514 declare
4515 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4516 begin
4517 if Is_Predefined_File_Name
4518 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4519 then
4520 Set_Is_Inlined (Subp, False);
4521 Error_Msg_NE (Msg & "p?", N, Subp);
4522 return;
4523 end if;
4524 end;
4525 end if;
4526
4527 -- Remove last character (question mark) to make this into an
4528 -- error, because the Inline_Always pragma cannot be obeyed.
4529
4530 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4531
4532 else pragma Assert (Front_End_Inlining);
4533 Set_Is_Inlined (Subp, False);
4534
4535 -- When inlining cannot take place we must issue an error.
4536 -- For backward compatibility we still report a warning.
4537
4538 if Ineffective_Inline_Warnings then
4539 Error_Msg_NE (Msg & "p?", N, Subp);
4540 end if;
4541 end if;
4542
4543 -- Compiling with optimizations enabled it is too early to report
4544 -- problems since the backend may still perform inlining. In order
4545 -- to report unhandled inlinings the program must be compiled with
4546 -- -Winline and the error is reported by the backend.
4547
4548 else
4549 null;
4550 end if;
4551 end Cannot_Inline;
4552
4553 ------------------------------------
4554 -- Check_And_Build_Body_To_Inline --
4555 ------------------------------------
4556
4557 procedure Check_And_Build_Body_To_Inline
4558 (N : Node_Id;
4559 Spec_Id : Entity_Id;
4560 Body_Id : Entity_Id)
4561 is
4562 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4563 -- Use generic machinery to build an unexpanded body for the subprogram.
4564 -- This body is subsequently used for inline expansions at call sites.
4565
4566 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4567 -- Return true if we generate code for the function body N, the function
4568 -- body N has no local declarations and its unique statement is a single
4569 -- extended return statement with a handled statements sequence.
4570
4571 function Check_Body_To_Inline
4572 (N : Node_Id;
4573 Subp : Entity_Id) return Boolean;
4574 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4575 -- inlined by the frontend. These are the rules:
4576 -- * At -O0 use fe inlining when inline_always is specified except if
4577 -- the function returns a controlled type.
4578 -- * At other optimization levels use the fe inlining for both inline
4579 -- and inline_always in the following cases:
4580 -- - function returning a known at compile time constant
4581 -- - function returning a call to an intrinsic function
4582 -- - function returning an unconstrained type (see Can_Split
4583 -- Unconstrained_Function).
4584 -- - function returning a call to a frontend-inlined function
4585 -- Use the back-end mechanism otherwise
4586 --
4587 -- In addition, in the following cases the function cannot be inlined by
4588 -- the frontend:
4589 -- - functions that uses the secondary stack
4590 -- - functions that have declarations of:
4591 -- - Concurrent types
4592 -- - Packages
4593 -- - Instantiations
4594 -- - Subprograms
4595 -- - functions that have some of the following statements:
4596 -- - abort
4597 -- - asynchronous-select
4598 -- - conditional-entry-call
4599 -- - delay-relative
4600 -- - delay-until
4601 -- - selective-accept
4602 -- - timed-entry-call
4603 -- - functions that have exception handlers
4604 -- - functions that have some enclosing body containing instantiations
4605 -- that appear before the corresponding generic body.
4606
4607 procedure Generate_Body_To_Inline
4608 (N : Node_Id;
4609 Body_To_Inline : out Node_Id);
4610 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4611 -- of pragmas referencing the formals are removed since they have no
4612 -- meaning when the body is inlined and the formals are rewritten (the
4613 -- analysis of the non-inlined body will handle these pragmas properly).
4614 -- A new internal name is associated with Body_To_Inline.
4615
4616 procedure Split_Unconstrained_Function
4617 (N : Node_Id;
4618 Spec_Id : Entity_Id);
4619 -- N is an inlined function body that returns an unconstrained type and
4620 -- has a single extended return statement. Split N in two subprograms:
4621 -- a procedure P' and a function F'. The formals of P' duplicate the
4622 -- formals of N plus an extra formal which is used return a value;
4623 -- its body is composed by the declarations and list of statements
4624 -- of the extended return statement of N.
4625
4626 --------------------------
4627 -- Build_Body_To_Inline --
4628 --------------------------
4629
4630 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4631 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4632 Original_Body : Node_Id;
4633 Body_To_Analyze : Node_Id;
4634
4635 begin
4636 pragma Assert (Current_Scope = Spec_Id);
4637
4638 -- Within an instance, the body to inline must be treated as a nested
4639 -- generic, so that the proper global references are preserved. We
4640 -- do not do this at the library level, because it is not needed, and
4641 -- furthermore this causes trouble if front end inlining is activated
4642 -- (-gnatN).
4643
4644 if In_Instance
4645 and then Scope (Current_Scope) /= Standard_Standard
4646 then
4647 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4648 end if;
4649
4650 -- We need to capture references to the formals in order
4651 -- to substitute the actuals at the point of inlining, i.e.
4652 -- instantiation. To treat the formals as globals to the body to
4653 -- inline, we nest it within a dummy parameterless subprogram,
4654 -- declared within the real one.
4655
4656 Generate_Body_To_Inline (N, Original_Body);
4657 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4658
4659 -- Set return type of function, which is also global and does not
4660 -- need to be resolved.
4661
4662 if Ekind (Spec_Id) = E_Function then
4663 Set_Result_Definition (Specification (Body_To_Analyze),
4664 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4665 end if;
4666
4667 if No (Declarations (N)) then
4668 Set_Declarations (N, New_List (Body_To_Analyze));
4669 else
4670 Append_To (Declarations (N), Body_To_Analyze);
4671 end if;
4672
4673 Preanalyze (Body_To_Analyze);
4674
4675 Push_Scope (Defining_Entity (Body_To_Analyze));
4676 Save_Global_References (Original_Body);
4677 End_Scope;
4678 Remove (Body_To_Analyze);
4679
4680 -- Restore environment if previously saved
4681
4682 if In_Instance
4683 and then Scope (Current_Scope) /= Standard_Standard
4684 then
4685 Restore_Env;
4686 end if;
4687
4688 pragma Assert (No (Body_To_Inline (Decl)));
4689 Set_Body_To_Inline (Decl, Original_Body);
4690 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4691 end Build_Body_To_Inline;
4692
4693 --------------------------
4694 -- Check_Body_To_Inline --
4695 --------------------------
4696
4697 function Check_Body_To_Inline
4698 (N : Node_Id;
4699 Subp : Entity_Id) return Boolean
4700 is
4701 Max_Size : constant := 10;
4702 Stat_Count : Integer := 0;
4703
4704 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4705 -- Check for declarations that make inlining not worthwhile
4706
4707 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4708 -- Check for statements that make inlining not worthwhile: any
4709 -- tasking statement, nested at any level. Keep track of total
4710 -- number of elementary statements, as a measure of acceptable size.
4711
4712 function Has_Pending_Instantiation return Boolean;
4713 -- Return True if some enclosing body contains instantiations that
4714 -- appear before the corresponding generic body.
4715
4716 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4717 -- Return True if all the return statements of the function body N
4718 -- are simple return statements and return a compile time constant
4719
4720 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4721 -- Return True if all the return statements of the function body N
4722 -- are simple return statements and return an intrinsic function call
4723
4724 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4725 -- If the body of the subprogram includes a call that returns an
4726 -- unconstrained type, the secondary stack is involved, and it
4727 -- is not worth inlining.
4728
4729 ------------------------------
4730 -- Has_Excluded_Declaration --
4731 ------------------------------
4732
4733 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4734 D : Node_Id;
4735
4736 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4737 -- Nested subprograms make a given body ineligible for inlining,
4738 -- but we make an exception for instantiations of unchecked
4739 -- conversion. The body has not been analyzed yet, so check the
4740 -- name, and verify that the visible entity with that name is the
4741 -- predefined unit.
4742
4743 -----------------------------
4744 -- Is_Unchecked_Conversion --
4745 -----------------------------
4746
4747 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4748 Id : constant Node_Id := Name (D);
4749 Conv : Entity_Id;
4750
4751 begin
4752 if Nkind (Id) = N_Identifier
4753 and then Chars (Id) = Name_Unchecked_Conversion
4754 then
4755 Conv := Current_Entity (Id);
4756
4757 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4758 and then
4759 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4760 then
4761 Conv := Current_Entity (Selector_Name (Id));
4762 else
4763 return False;
4764 end if;
4765
4766 return Present (Conv)
4767 and then Is_Predefined_File_Name
4768 (Unit_File_Name (Get_Source_Unit (Conv)))
4769 and then Is_Intrinsic_Subprogram (Conv);
4770 end Is_Unchecked_Conversion;
4771
4772 -- Start of processing for Has_Excluded_Declaration
4773
4774 begin
4775 D := First (Decls);
4776 while Present (D) loop
4777 if (Nkind (D) = N_Function_Instantiation
4778 and then not Is_Unchecked_Conversion (D))
4779 or else Nkind_In (D, N_Protected_Type_Declaration,
4780 N_Package_Declaration,
4781 N_Package_Instantiation,
4782 N_Subprogram_Body,
4783 N_Procedure_Instantiation,
4784 N_Task_Type_Declaration)
4785 then
4786 Cannot_Inline
4787 ("cannot inline & (non-allowed declaration)?", D, Subp);
4788
4789 return True;
4790 end if;
4791
4792 Next (D);
4793 end loop;
4794
4795 return False;
4796 end Has_Excluded_Declaration;
4797
4798 ----------------------------
4799 -- Has_Excluded_Statement --
4800 ----------------------------
4801
4802 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4803 S : Node_Id;
4804 E : Node_Id;
4805
4806 begin
4807 S := First (Stats);
4808 while Present (S) loop
4809 Stat_Count := Stat_Count + 1;
4810
4811 if Nkind_In (S, N_Abort_Statement,
4812 N_Asynchronous_Select,
4813 N_Conditional_Entry_Call,
4814 N_Delay_Relative_Statement,
4815 N_Delay_Until_Statement,
4816 N_Selective_Accept,
4817 N_Timed_Entry_Call)
4818 then
4819 Cannot_Inline
4820 ("cannot inline & (non-allowed statement)?", S, Subp);
4821 return True;
4822
4823 elsif Nkind (S) = N_Block_Statement then
4824 if Present (Declarations (S))
4825 and then Has_Excluded_Declaration (Declarations (S))
4826 then
4827 return True;
4828
4829 elsif Present (Handled_Statement_Sequence (S)) then
4830 if Present
4831 (Exception_Handlers (Handled_Statement_Sequence (S)))
4832 then
4833 Cannot_Inline
4834 ("cannot inline& (exception handler)?",
4835 First (Exception_Handlers
4836 (Handled_Statement_Sequence (S))),
4837 Subp);
4838 return True;
4839
4840 elsif Has_Excluded_Statement
4841 (Statements (Handled_Statement_Sequence (S)))
4842 then
4843 return True;
4844 end if;
4845 end if;
4846
4847 elsif Nkind (S) = N_Case_Statement then
4848 E := First (Alternatives (S));
4849 while Present (E) loop
4850 if Has_Excluded_Statement (Statements (E)) then
4851 return True;
4852 end if;
4853
4854 Next (E);
4855 end loop;
4856
4857 elsif Nkind (S) = N_If_Statement then
4858 if Has_Excluded_Statement (Then_Statements (S)) then
4859 return True;
4860 end if;
4861
4862 if Present (Elsif_Parts (S)) then
4863 E := First (Elsif_Parts (S));
4864 while Present (E) loop
4865 if Has_Excluded_Statement (Then_Statements (E)) then
4866 return True;
4867 end if;
4868 Next (E);
4869 end loop;
4870 end if;
4871
4872 if Present (Else_Statements (S))
4873 and then Has_Excluded_Statement (Else_Statements (S))
4874 then
4875 return True;
4876 end if;
4877
4878 elsif Nkind (S) = N_Loop_Statement
4879 and then Has_Excluded_Statement (Statements (S))
4880 then
4881 return True;
4882
4883 elsif Nkind (S) = N_Extended_Return_Statement then
4884 if Present (Handled_Statement_Sequence (S))
4885 and then
4886 Has_Excluded_Statement
4887 (Statements (Handled_Statement_Sequence (S)))
4888 then
4889 return True;
4890
4891 elsif Present (Handled_Statement_Sequence (S))
4892 and then
4893 Present (Exception_Handlers
4894 (Handled_Statement_Sequence (S)))
4895 then
4896 Cannot_Inline
4897 ("cannot inline& (exception handler)?",
4898 First (Exception_Handlers
4899 (Handled_Statement_Sequence (S))),
4900 Subp);
4901 return True;
4902 end if;
4903 end if;
4904
4905 Next (S);
4906 end loop;
4907
4908 return False;
4909 end Has_Excluded_Statement;
4910
4911 -------------------------------
4912 -- Has_Pending_Instantiation --
4913 -------------------------------
4914
4915 function Has_Pending_Instantiation return Boolean is
4916 S : Entity_Id;
4917
4918 begin
4919 S := Current_Scope;
4920 while Present (S) loop
4921 if Is_Compilation_Unit (S)
4922 or else Is_Child_Unit (S)
4923 then
4924 return False;
4925
4926 elsif Ekind (S) = E_Package
4927 and then Has_Forward_Instantiation (S)
4928 then
4929 return True;
4930 end if;
4931
4932 S := Scope (S);
4933 end loop;
4934
4935 return False;
4936 end Has_Pending_Instantiation;
4937
4938 ------------------------------------
4939 -- Returns_Compile_Time_Constant --
4940 ------------------------------------
4941
4942 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4943
4944 function Check_Return (N : Node_Id) return Traverse_Result;
4945
4946 ------------------
4947 -- Check_Return --
4948 ------------------
4949
4950 function Check_Return (N : Node_Id) return Traverse_Result is
4951 begin
4952 if Nkind (N) = N_Extended_Return_Statement then
4953 return Abandon;
4954
4955 elsif Nkind (N) = N_Simple_Return_Statement then
4956 if Present (Expression (N)) then
4957 declare
4958 Orig_Expr : constant Node_Id :=
4959 Original_Node (Expression (N));
4960
4961 begin
4962 if Nkind_In (Orig_Expr, N_Integer_Literal,
4963 N_Real_Literal,
4964 N_Character_Literal)
4965 then
4966 return OK;
4967
4968 elsif Is_Entity_Name (Orig_Expr)
4969 and then Ekind (Entity (Orig_Expr)) = E_Constant
4970 and then Is_Static_Expression (Orig_Expr)
4971 then
4972 return OK;
4973 else
4974 return Abandon;
4975 end if;
4976 end;
4977
4978 -- Expression has wrong form
4979
4980 else
4981 return Abandon;
4982 end if;
4983
4984 -- Continue analyzing statements
4985
4986 else
4987 return OK;
4988 end if;
4989 end Check_Return;
4990
4991 function Check_All_Returns is new Traverse_Func (Check_Return);
4992
4993 -- Start of processing for Returns_Compile_Time_Constant
4994
4995 begin
4996 return Check_All_Returns (N) = OK;
4997 end Returns_Compile_Time_Constant;
4998
4999 --------------------------------------
5000 -- Returns_Intrinsic_Function_Call --
5001 --------------------------------------
5002
5003 function Returns_Intrinsic_Function_Call
5004 (N : Node_Id) return Boolean
5005 is
5006 function Check_Return (N : Node_Id) return Traverse_Result;
5007
5008 ------------------
5009 -- Check_Return --
5010 ------------------
5011
5012 function Check_Return (N : Node_Id) return Traverse_Result is
5013 begin
5014 if Nkind (N) = N_Extended_Return_Statement then
5015 return Abandon;
5016
5017 elsif Nkind (N) = N_Simple_Return_Statement then
5018 if Present (Expression (N)) then
5019 declare
5020 Orig_Expr : constant Node_Id :=
5021 Original_Node (Expression (N));
5022
5023 begin
5024 if Nkind (Orig_Expr) in N_Op
5025 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5026 then
5027 return OK;
5028
5029 elsif Nkind (Orig_Expr) in N_Has_Entity
5030 and then Present (Entity (Orig_Expr))
5031 and then Ekind (Entity (Orig_Expr)) = E_Function
5032 and then Is_Inlined (Entity (Orig_Expr))
5033 then
5034 return OK;
5035
5036 elsif Nkind (Orig_Expr) in N_Has_Entity
5037 and then Present (Entity (Orig_Expr))
5038 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5039 then
5040 return OK;
5041
5042 else
5043 return Abandon;
5044 end if;
5045 end;
5046
5047 -- Expression has wrong form
5048
5049 else
5050 return Abandon;
5051 end if;
5052
5053 -- Continue analyzing statements
5054
5055 else
5056 return OK;
5057 end if;
5058 end Check_Return;
5059
5060 function Check_All_Returns is new Traverse_Func (Check_Return);
5061
5062 -- Start of processing for Returns_Intrinsic_Function_Call
5063
5064 begin
5065 return Check_All_Returns (N) = OK;
5066 end Returns_Intrinsic_Function_Call;
5067
5068 --------------------------
5069 -- Uses_Secondary_Stack --
5070 --------------------------
5071
5072 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5073
5074 function Check_Call (N : Node_Id) return Traverse_Result;
5075 -- Look for function calls that return an unconstrained type
5076
5077 ----------------
5078 -- Check_Call --
5079 ----------------
5080
5081 function Check_Call (N : Node_Id) return Traverse_Result is
5082 begin
5083 if Nkind (N) = N_Function_Call
5084 and then Is_Entity_Name (Name (N))
5085 and then Is_Composite_Type (Etype (Entity (Name (N))))
5086 and then not Is_Constrained (Etype (Entity (Name (N))))
5087 then
5088 Cannot_Inline
5089 ("cannot inline & (call returns unconstrained type)?",
5090 N, Subp);
5091
5092 return Abandon;
5093 else
5094 return OK;
5095 end if;
5096 end Check_Call;
5097
5098 function Check_Calls is new Traverse_Func (Check_Call);
5099
5100 -- Start of processing for Uses_Secondary_Stack
5101
5102 begin
5103 return Check_Calls (N) = Abandon;
5104 end Uses_Secondary_Stack;
5105
5106 -- Local variables
5107
5108 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5109 May_Inline : constant Boolean :=
5110 Has_Pragma_Inline_Always (Spec_Id)
5111 or else (Has_Pragma_Inline (Spec_Id)
5112 and then ((Optimization_Level > 0
5113 and then Ekind (Spec_Id)
5114 = E_Function)
5115 or else Front_End_Inlining));
5116 Body_To_Analyze : Node_Id;
5117
5118 -- Start of processing for Check_Body_To_Inline
5119
5120 begin
5121 -- No action needed in stubs since the attribute Body_To_Inline
5122 -- is not available
5123
5124 if Nkind (Decl) = N_Subprogram_Body_Stub then
5125 return False;
5126
5127 -- Cannot build the body to inline if the attribute is already set.
5128 -- This attribute may have been set if this is a subprogram renaming
5129 -- declarations (see Freeze.Build_Renamed_Body).
5130
5131 elsif Present (Body_To_Inline (Decl)) then
5132 return False;
5133
5134 -- No action needed if the subprogram does not fulfill the minimum
5135 -- conditions to be inlined by the frontend
5136
5137 elsif not May_Inline then
5138 return False;
5139 end if;
5140
5141 -- Check excluded declarations
5142
5143 if Present (Declarations (N))
5144 and then Has_Excluded_Declaration (Declarations (N))
5145 then
5146 return False;
5147 end if;
5148
5149 -- Check excluded statements
5150
5151 if Present (Handled_Statement_Sequence (N)) then
5152 if Present
5153 (Exception_Handlers (Handled_Statement_Sequence (N)))
5154 then
5155 Cannot_Inline
5156 ("cannot inline& (exception handler)?",
5157 First
5158 (Exception_Handlers (Handled_Statement_Sequence (N))),
5159 Subp);
5160
5161 return False;
5162
5163 elsif Has_Excluded_Statement
5164 (Statements (Handled_Statement_Sequence (N)))
5165 then
5166 return False;
5167 end if;
5168 end if;
5169
5170 -- For backward compatibility, compiling under -gnatN we do not
5171 -- inline a subprogram that is too large, unless it is marked
5172 -- Inline_Always. This pragma does not suppress the other checks
5173 -- on inlining (forbidden declarations, handlers, etc).
5174
5175 if Front_End_Inlining
5176 and then not Has_Pragma_Inline_Always (Subp)
5177 and then Stat_Count > Max_Size
5178 then
5179 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5180 return False;
5181 end if;
5182
5183 -- If some enclosing body contains instantiations that appear before
5184 -- the corresponding generic body, the enclosing body has a freeze
5185 -- node so that it can be elaborated after the generic itself. This
5186 -- might conflict with subsequent inlinings, so that it is unsafe to
5187 -- try to inline in such a case.
5188
5189 if Has_Pending_Instantiation then
5190 Cannot_Inline
5191 ("cannot inline& (forward instance within enclosing body)?",
5192 N, Subp);
5193
5194 return False;
5195 end if;
5196
5197 -- Generate and preanalyze the body to inline (needed to perform
5198 -- the rest of the checks)
5199
5200 Generate_Body_To_Inline (N, Body_To_Analyze);
5201
5202 if Ekind (Subp) = E_Function then
5203 Set_Result_Definition (Specification (Body_To_Analyze),
5204 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5205 end if;
5206
5207 -- Nest the body to analyze within the real one
5208
5209 if No (Declarations (N)) then
5210 Set_Declarations (N, New_List (Body_To_Analyze));
5211 else
5212 Append_To (Declarations (N), Body_To_Analyze);
5213 end if;
5214
5215 Preanalyze (Body_To_Analyze);
5216 Remove (Body_To_Analyze);
5217
5218 -- Keep separate checks needed when compiling without optimizations
5219
5220 if Optimization_Level = 0
5221
5222 -- AAMP and VM targets have no support for inlining in the backend
5223 -- and hence we use frontend inlining at all optimization levels.
5224
5225 or else AAMP_On_Target
5226 or else VM_Target /= No_VM
5227 then
5228 -- Cannot inline functions whose body has a call that returns an
5229 -- unconstrained type since the secondary stack is involved, and
5230 -- it is not worth inlining.
5231
5232 if Uses_Secondary_Stack (Body_To_Analyze) then
5233 return False;
5234
5235 -- Cannot inline functions that return controlled types since
5236 -- controlled actions interfere in complex ways with inlining.
5237
5238 elsif Ekind (Subp) = E_Function
5239 and then Needs_Finalization (Etype (Subp))
5240 then
5241 Cannot_Inline
5242 ("cannot inline & (controlled return type)?", N, Subp);
5243 return False;
5244
5245 elsif Returns_Unconstrained_Type (Subp) then
5246 Cannot_Inline
5247 ("cannot inline & (unconstrained return type)?", N, Subp);
5248 return False;
5249 end if;
5250
5251 -- Compiling with optimizations enabled
5252
5253 else
5254 -- Procedures are never frontend inlined in this case!
5255
5256 if Ekind (Subp) /= E_Function then
5257 return False;
5258
5259 -- Functions returning unconstrained types are tested
5260 -- separately (see Can_Split_Unconstrained_Function).
5261
5262 elsif Returns_Unconstrained_Type (Subp) then
5263 null;
5264
5265 -- Check supported cases
5266
5267 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5268 and then Convention (Subp) /= Convention_Intrinsic
5269 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5270 then
5271 return False;
5272 end if;
5273 end if;
5274
5275 return True;
5276 end Check_Body_To_Inline;
5277
5278 --------------------------------------
5279 -- Can_Split_Unconstrained_Function --
5280 --------------------------------------
5281
5282 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5283 is
5284 Ret_Node : constant Node_Id :=
5285 First (Statements (Handled_Statement_Sequence (N)));
5286 D : Node_Id;
5287
5288 begin
5289 -- No user defined declarations allowed in the function except inside
5290 -- the unique return statement; implicit labels are the only allowed
5291 -- declarations.
5292
5293 if not Is_Empty_List (Declarations (N)) then
5294 D := First (Declarations (N));
5295 while Present (D) loop
5296 if Nkind (D) /= N_Implicit_Label_Declaration then
5297 return False;
5298 end if;
5299
5300 Next (D);
5301 end loop;
5302 end if;
5303
5304 -- We only split the inlined function when we are generating the code
5305 -- of its body; otherwise we leave duplicated split subprograms in
5306 -- the tree which (if referenced) generate wrong references at link
5307 -- time.
5308
5309 return In_Extended_Main_Code_Unit (N)
5310 and then Present (Ret_Node)
5311 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5312 and then No (Next (Ret_Node))
5313 and then Present (Handled_Statement_Sequence (Ret_Node));
5314 end Can_Split_Unconstrained_Function;
5315
5316 -----------------------------
5317 -- Generate_Body_To_Inline --
5318 -----------------------------
5319
5320 procedure Generate_Body_To_Inline
5321 (N : Node_Id;
5322 Body_To_Inline : out Node_Id)
5323 is
5324 procedure Remove_Pragmas (N : Node_Id);
5325 -- Remove occurrences of pragmas that may reference the formals of
5326 -- N. The analysis of the non-inlined body will handle these pragmas
5327 -- properly.
5328
5329 --------------------
5330 -- Remove_Pragmas --
5331 --------------------
5332
5333 procedure Remove_Pragmas (N : Node_Id) is
5334 Decl : Node_Id;
5335 Nxt : Node_Id;
5336
5337 begin
5338 Decl := First (Declarations (N));
5339 while Present (Decl) loop
5340 Nxt := Next (Decl);
5341
5342 if Nkind (Decl) = N_Pragma
5343 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5344 Name_Unmodified)
5345 then
5346 Remove (Decl);
5347 end if;
5348
5349 Decl := Nxt;
5350 end loop;
5351 end Remove_Pragmas;
5352
5353 -- Start of processing for Generate_Body_To_Inline
5354
5355 begin
5356 -- Within an instance, the body to inline must be treated as a nested
5357 -- generic, so that the proper global references are preserved.
5358
5359 -- Note that we do not do this at the library level, because it
5360 -- is not needed, and furthermore this causes trouble if front
5361 -- end inlining is activated (-gnatN).
5362
5363 if In_Instance
5364 and then Scope (Current_Scope) /= Standard_Standard
5365 then
5366 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5367 else
5368 Body_To_Inline := Copy_Separate_Tree (N);
5369 end if;
5370
5371 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5372 -- parameter has no meaning when the body is inlined and the formals
5373 -- are rewritten. Remove it from body to inline. The analysis of the
5374 -- non-inlined body will handle the pragma properly.
5375
5376 Remove_Pragmas (Body_To_Inline);
5377
5378 -- We need to capture references to the formals in order
5379 -- to substitute the actuals at the point of inlining, i.e.
5380 -- instantiation. To treat the formals as globals to the body to
5381 -- inline, we nest it within a dummy parameterless subprogram,
5382 -- declared within the real one.
5383
5384 Set_Parameter_Specifications
5385 (Specification (Body_To_Inline), No_List);
5386
5387 -- A new internal name is associated with Body_To_Inline to avoid
5388 -- conflicts when the non-inlined body N is analyzed.
5389
5390 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5391 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5392 Set_Corresponding_Spec (Body_To_Inline, Empty);
5393 end Generate_Body_To_Inline;
5394
5395 ----------------------------------
5396 -- Split_Unconstrained_Function --
5397 ----------------------------------
5398
5399 procedure Split_Unconstrained_Function
5400 (N : Node_Id;
5401 Spec_Id : Entity_Id)
5402 is
5403 Loc : constant Source_Ptr := Sloc (N);
5404 Ret_Node : constant Node_Id :=
5405 First (Statements (Handled_Statement_Sequence (N)));
5406 Ret_Obj : constant Node_Id :=
5407 First (Return_Object_Declarations (Ret_Node));
5408
5409 procedure Build_Procedure
5410 (Proc_Id : out Entity_Id;
5411 Decl_List : out List_Id);
5412 -- Build a procedure containing the statements found in the extended
5413 -- return statement of the unconstrained function body N.
5414
5415 procedure Build_Procedure
5416 (Proc_Id : out Entity_Id;
5417 Decl_List : out List_Id)
5418 is
5419 Formal : Entity_Id;
5420 Formal_List : constant List_Id := New_List;
5421 Proc_Spec : Node_Id;
5422 Proc_Body : Node_Id;
5423 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5424 Body_Decl_List : List_Id := No_List;
5425 Param_Type : Node_Id;
5426
5427 begin
5428 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5429 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5430 else
5431 Param_Type :=
5432 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5433 end if;
5434
5435 Append_To (Formal_List,
5436 Make_Parameter_Specification (Loc,
5437 Defining_Identifier =>
5438 Make_Defining_Identifier (Loc,
5439 Chars => Chars (Defining_Identifier (Ret_Obj))),
5440 In_Present => False,
5441 Out_Present => True,
5442 Null_Exclusion_Present => False,
5443 Parameter_Type => Param_Type));
5444
5445 Formal := First_Formal (Spec_Id);
5446 while Present (Formal) loop
5447 Append_To (Formal_List,
5448 Make_Parameter_Specification (Loc,
5449 Defining_Identifier =>
5450 Make_Defining_Identifier (Sloc (Formal),
5451 Chars => Chars (Formal)),
5452 In_Present => In_Present (Parent (Formal)),
5453 Out_Present => Out_Present (Parent (Formal)),
5454 Null_Exclusion_Present =>
5455 Null_Exclusion_Present (Parent (Formal)),
5456 Parameter_Type =>
5457 New_Reference_To (Etype (Formal), Loc),
5458 Expression =>
5459 Copy_Separate_Tree (Expression (Parent (Formal)))));
5460
5461 Next_Formal (Formal);
5462 end loop;
5463
5464 Proc_Id :=
5465 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5466
5467 Proc_Spec :=
5468 Make_Procedure_Specification (Loc,
5469 Defining_Unit_Name => Proc_Id,
5470 Parameter_Specifications => Formal_List);
5471
5472 Decl_List := New_List;
5473
5474 Append_To (Decl_List,
5475 Make_Subprogram_Declaration (Loc, Proc_Spec));
5476
5477 -- Can_Convert_Unconstrained_Function checked that the function
5478 -- has no local declarations except implicit label declarations.
5479 -- Copy these declarations to the built procedure.
5480
5481 if Present (Declarations (N)) then
5482 Body_Decl_List := New_List;
5483
5484 declare
5485 D : Node_Id;
5486 New_D : Node_Id;
5487
5488 begin
5489 D := First (Declarations (N));
5490 while Present (D) loop
5491 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5492
5493 New_D :=
5494 Make_Implicit_Label_Declaration (Loc,
5495 Make_Defining_Identifier (Loc,
5496 Chars => Chars (Defining_Identifier (D))),
5497 Label_Construct => Empty);
5498 Append_To (Body_Decl_List, New_D);
5499
5500 Next (D);
5501 end loop;
5502 end;
5503 end if;
5504
5505 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5506
5507 Proc_Body :=
5508 Make_Subprogram_Body (Loc,
5509 Specification => Copy_Separate_Tree (Proc_Spec),
5510 Declarations => Body_Decl_List,
5511 Handled_Statement_Sequence =>
5512 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5513
5514 Set_Defining_Unit_Name (Specification (Proc_Body),
5515 Make_Defining_Identifier (Loc, Subp_Name));
5516
5517 Append_To (Decl_List, Proc_Body);
5518 end Build_Procedure;
5519
5520 -- Local variables
5521
5522 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5523 Blk_Stmt : Node_Id;
5524 Proc_Id : Entity_Id;
5525 Proc_Call : Node_Id;
5526
5527 -- Start of processing for Split_Unconstrained_Function
5528
5529 begin
5530 -- Build the associated procedure, analyze it and insert it before
5531 -- the function body N
5532
5533 declare
5534 Scope : constant Entity_Id := Current_Scope;
5535 Decl_List : List_Id;
5536 begin
5537 Pop_Scope;
5538 Build_Procedure (Proc_Id, Decl_List);
5539 Insert_Actions (N, Decl_List);
5540 Push_Scope (Scope);
5541 end;
5542
5543 -- Build the call to the generated procedure
5544
5545 declare
5546 Actual_List : constant List_Id := New_List;
5547 Formal : Entity_Id;
5548
5549 begin
5550 Append_To (Actual_List,
5551 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5552
5553 Formal := First_Formal (Spec_Id);
5554 while Present (Formal) loop
5555 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5556
5557 -- Avoid spurious warning on unreferenced formals
5558
5559 Set_Referenced (Formal);
5560 Next_Formal (Formal);
5561 end loop;
5562
5563 Proc_Call :=
5564 Make_Procedure_Call_Statement (Loc,
5565 Name => New_Reference_To (Proc_Id, Loc),
5566 Parameter_Associations => Actual_List);
5567 end;
5568
5569 -- Generate
5570
5571 -- declare
5572 -- New_Obj : ...
5573 -- begin
5574 -- main_1__F1b (New_Obj, ...);
5575 -- return Obj;
5576 -- end B10b;
5577
5578 Blk_Stmt :=
5579 Make_Block_Statement (Loc,
5580 Declarations => New_List (New_Obj),
5581 Handled_Statement_Sequence =>
5582 Make_Handled_Sequence_Of_Statements (Loc,
5583 Statements => New_List (
5584
5585 Proc_Call,
5586
5587 Make_Simple_Return_Statement (Loc,
5588 Expression =>
5589 New_Reference_To
5590 (Defining_Identifier (New_Obj), Loc)))));
5591
5592 Rewrite (Ret_Node, Blk_Stmt);
5593 end Split_Unconstrained_Function;
5594
5595 -- Start of processing for Check_And_Build_Body_To_Inline
5596
5597 begin
5598 -- Do not inline any subprogram that contains nested subprograms, since
5599 -- the backend inlining circuit seems to generate uninitialized
5600 -- references in this case. We know this happens in the case of front
5601 -- end ZCX support, but it also appears it can happen in other cases as
5602 -- well. The backend often rejects attempts to inline in the case of
5603 -- nested procedures anyway, so little if anything is lost by this.
5604 -- Note that this is test is for the benefit of the back-end. There is
5605 -- a separate test for front-end inlining that also rejects nested
5606 -- subprograms.
5607
5608 -- Do not do this test if errors have been detected, because in some
5609 -- error cases, this code blows up, and we don't need it anyway if
5610 -- there have been errors, since we won't get to the linker anyway.
5611
5612 if Comes_From_Source (Body_Id)
5613 and then (Has_Pragma_Inline_Always (Spec_Id)
5614 or else Optimization_Level > 0)
5615 and then Serious_Errors_Detected = 0
5616 then
5617 declare
5618 P_Ent : Node_Id;
5619
5620 begin
5621 P_Ent := Body_Id;
5622 loop
5623 P_Ent := Scope (P_Ent);
5624 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5625
5626 if Is_Subprogram (P_Ent) then
5627 Set_Is_Inlined (P_Ent, False);
5628
5629 if Comes_From_Source (P_Ent)
5630 and then Has_Pragma_Inline (P_Ent)
5631 then
5632 Cannot_Inline
5633 ("cannot inline& (nested subprogram)?", N, P_Ent,
5634 Is_Serious => True);
5635 end if;
5636 end if;
5637 end loop;
5638 end;
5639 end if;
5640
5641 -- Build the body to inline only if really needed!
5642
5643 if Check_Body_To_Inline (N, Spec_Id)
5644 and then Serious_Errors_Detected = 0
5645 then
5646 if Returns_Unconstrained_Type (Spec_Id) then
5647 if Can_Split_Unconstrained_Function (N) then
5648 Split_Unconstrained_Function (N, Spec_Id);
5649 Build_Body_To_Inline (N, Spec_Id);
5650 Set_Is_Inlined (Spec_Id);
5651 end if;
5652 else
5653 Build_Body_To_Inline (N, Spec_Id);
5654 Set_Is_Inlined (Spec_Id);
5655 end if;
5656 end if;
5657 end Check_And_Build_Body_To_Inline;
5658
5659 -----------------------
5660 -- Check_Conformance --
5661 -----------------------
5662
5663 procedure Check_Conformance
5664 (New_Id : Entity_Id;
5665 Old_Id : Entity_Id;
5666 Ctype : Conformance_Type;
5667 Errmsg : Boolean;
5668 Conforms : out Boolean;
5669 Err_Loc : Node_Id := Empty;
5670 Get_Inst : Boolean := False;
5671 Skip_Controlling_Formals : Boolean := False)
5672 is
5673 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5674 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5675 -- If Errmsg is True, then processing continues to post an error message
5676 -- for conformance error on given node. Two messages are output. The
5677 -- first message points to the previous declaration with a general "no
5678 -- conformance" message. The second is the detailed reason, supplied as
5679 -- Msg. The parameter N provide information for a possible & insertion
5680 -- in the message, and also provides the location for posting the
5681 -- message in the absence of a specified Err_Loc location.
5682
5683 -----------------------
5684 -- Conformance_Error --
5685 -----------------------
5686
5687 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5688 Enode : Node_Id;
5689
5690 begin
5691 Conforms := False;
5692
5693 if Errmsg then
5694 if No (Err_Loc) then
5695 Enode := N;
5696 else
5697 Enode := Err_Loc;
5698 end if;
5699
5700 Error_Msg_Sloc := Sloc (Old_Id);
5701
5702 case Ctype is
5703 when Type_Conformant =>
5704 Error_Msg_N -- CODEFIX
5705 ("not type conformant with declaration#!", Enode);
5706
5707 when Mode_Conformant =>
5708 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5709 Error_Msg_N
5710 ("not mode conformant with operation inherited#!",
5711 Enode);
5712 else
5713 Error_Msg_N
5714 ("not mode conformant with declaration#!", Enode);
5715 end if;
5716
5717 when Subtype_Conformant =>
5718 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5719 Error_Msg_N
5720 ("not subtype conformant with operation inherited#!",
5721 Enode);
5722 else
5723 Error_Msg_N
5724 ("not subtype conformant with declaration#!", Enode);
5725 end if;
5726
5727 when Fully_Conformant =>
5728 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5729 Error_Msg_N -- CODEFIX
5730 ("not fully conformant with operation inherited#!",
5731 Enode);
5732 else
5733 Error_Msg_N -- CODEFIX
5734 ("not fully conformant with declaration#!", Enode);
5735 end if;
5736 end case;
5737
5738 Error_Msg_NE (Msg, Enode, N);
5739 end if;
5740 end Conformance_Error;
5741
5742 -- Local Variables
5743
5744 Old_Type : constant Entity_Id := Etype (Old_Id);
5745 New_Type : constant Entity_Id := Etype (New_Id);
5746 Old_Formal : Entity_Id;
5747 New_Formal : Entity_Id;
5748 Access_Types_Match : Boolean;
5749 Old_Formal_Base : Entity_Id;
5750 New_Formal_Base : Entity_Id;
5751
5752 -- Start of processing for Check_Conformance
5753
5754 begin
5755 Conforms := True;
5756
5757 -- We need a special case for operators, since they don't appear
5758 -- explicitly.
5759
5760 if Ctype = Type_Conformant then
5761 if Ekind (New_Id) = E_Operator
5762 and then Operator_Matches_Spec (New_Id, Old_Id)
5763 then
5764 return;
5765 end if;
5766 end if;
5767
5768 -- If both are functions/operators, check return types conform
5769
5770 if Old_Type /= Standard_Void_Type
5771 and then New_Type /= Standard_Void_Type
5772 then
5773
5774 -- If we are checking interface conformance we omit controlling
5775 -- arguments and result, because we are only checking the conformance
5776 -- of the remaining parameters.
5777
5778 if Has_Controlling_Result (Old_Id)
5779 and then Has_Controlling_Result (New_Id)
5780 and then Skip_Controlling_Formals
5781 then
5782 null;
5783
5784 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5785 Conformance_Error ("\return type does not match!", New_Id);
5786 return;
5787 end if;
5788
5789 -- Ada 2005 (AI-231): In case of anonymous access types check the
5790 -- null-exclusion and access-to-constant attributes match.
5791
5792 if Ada_Version >= Ada_2005
5793 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5794 and then
5795 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5796 or else Is_Access_Constant (Etype (Old_Type)) /=
5797 Is_Access_Constant (Etype (New_Type)))
5798 then
5799 Conformance_Error ("\return type does not match!", New_Id);
5800 return;
5801 end if;
5802
5803 -- If either is a function/operator and the other isn't, error
5804
5805 elsif Old_Type /= Standard_Void_Type
5806 or else New_Type /= Standard_Void_Type
5807 then
5808 Conformance_Error ("\functions can only match functions!", New_Id);
5809 return;
5810 end if;
5811
5812 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5813 -- If this is a renaming as body, refine error message to indicate that
5814 -- the conflict is with the original declaration. If the entity is not
5815 -- frozen, the conventions don't have to match, the one of the renamed
5816 -- entity is inherited.
5817
5818 if Ctype >= Subtype_Conformant then
5819 if Convention (Old_Id) /= Convention (New_Id) then
5820 if not Is_Frozen (New_Id) then
5821 null;
5822
5823 elsif Present (Err_Loc)
5824 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5825 and then Present (Corresponding_Spec (Err_Loc))
5826 then
5827 Error_Msg_Name_1 := Chars (New_Id);
5828 Error_Msg_Name_2 :=
5829 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5830 Conformance_Error ("\prior declaration for% has convention %!");
5831
5832 else
5833 Conformance_Error ("\calling conventions do not match!");
5834 end if;
5835
5836 return;
5837
5838 elsif Is_Formal_Subprogram (Old_Id)
5839 or else Is_Formal_Subprogram (New_Id)
5840 then
5841 Conformance_Error ("\formal subprograms not allowed!");
5842 return;
5843 end if;
5844 end if;
5845
5846 -- Deal with parameters
5847
5848 -- Note: we use the entity information, rather than going directly
5849 -- to the specification in the tree. This is not only simpler, but
5850 -- absolutely necessary for some cases of conformance tests between
5851 -- operators, where the declaration tree simply does not exist!
5852
5853 Old_Formal := First_Formal (Old_Id);
5854 New_Formal := First_Formal (New_Id);
5855 while Present (Old_Formal) and then Present (New_Formal) loop
5856 if Is_Controlling_Formal (Old_Formal)
5857 and then Is_Controlling_Formal (New_Formal)
5858 and then Skip_Controlling_Formals
5859 then
5860 -- The controlling formals will have different types when
5861 -- comparing an interface operation with its match, but both
5862 -- or neither must be access parameters.
5863
5864 if Is_Access_Type (Etype (Old_Formal))
5865 =
5866 Is_Access_Type (Etype (New_Formal))
5867 then
5868 goto Skip_Controlling_Formal;
5869 else
5870 Conformance_Error
5871 ("\access parameter does not match!", New_Formal);
5872 end if;
5873 end if;
5874
5875 -- Ada 2012: Mode conformance also requires that formal parameters
5876 -- be both aliased, or neither.
5877
5878 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5879 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5880 Conformance_Error
5881 ("\aliased parameter mismatch!", New_Formal);
5882 end if;
5883 end if;
5884
5885 if Ctype = Fully_Conformant then
5886
5887 -- Names must match. Error message is more accurate if we do
5888 -- this before checking that the types of the formals match.
5889
5890 if Chars (Old_Formal) /= Chars (New_Formal) then
5891 Conformance_Error ("\name & does not match!", New_Formal);
5892
5893 -- Set error posted flag on new formal as well to stop
5894 -- junk cascaded messages in some cases.
5895
5896 Set_Error_Posted (New_Formal);
5897 return;
5898 end if;
5899
5900 -- Null exclusion must match
5901
5902 if Null_Exclusion_Present (Parent (Old_Formal))
5903 /=
5904 Null_Exclusion_Present (Parent (New_Formal))
5905 then
5906 -- Only give error if both come from source. This should be
5907 -- investigated some time, since it should not be needed ???
5908
5909 if Comes_From_Source (Old_Formal)
5910 and then
5911 Comes_From_Source (New_Formal)
5912 then
5913 Conformance_Error
5914 ("\null exclusion for & does not match", New_Formal);
5915
5916 -- Mark error posted on the new formal to avoid duplicated
5917 -- complaint about types not matching.
5918
5919 Set_Error_Posted (New_Formal);
5920 end if;
5921 end if;
5922 end if;
5923
5924 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5925 -- case occurs whenever a subprogram is being renamed and one of its
5926 -- parameters imposes a null exclusion. For example:
5927
5928 -- type T is null record;
5929 -- type Acc_T is access T;
5930 -- subtype Acc_T_Sub is Acc_T;
5931
5932 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5933 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5934 -- renames P;
5935
5936 Old_Formal_Base := Etype (Old_Formal);
5937 New_Formal_Base := Etype (New_Formal);
5938
5939 if Get_Inst then
5940 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5941 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5942 end if;
5943
5944 Access_Types_Match := Ada_Version >= Ada_2005
5945
5946 -- Ensure that this rule is only applied when New_Id is a
5947 -- renaming of Old_Id.
5948
5949 and then Nkind (Parent (Parent (New_Id))) =
5950 N_Subprogram_Renaming_Declaration
5951 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5952 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5953 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5954
5955 -- Now handle the allowed access-type case
5956
5957 and then Is_Access_Type (Old_Formal_Base)
5958 and then Is_Access_Type (New_Formal_Base)
5959
5960 -- The type kinds must match. The only exception occurs with
5961 -- multiple generics of the form:
5962
5963 -- generic generic
5964 -- type F is private; type A is private;
5965 -- type F_Ptr is access F; type A_Ptr is access A;
5966 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5967 -- package F_Pack is ... package A_Pack is
5968 -- package F_Inst is
5969 -- new F_Pack (A, A_Ptr, A_P);
5970
5971 -- When checking for conformance between the parameters of A_P
5972 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5973 -- because the compiler has transformed A_Ptr into a subtype of
5974 -- F_Ptr. We catch this case in the code below.
5975
5976 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5977 or else
5978 (Is_Generic_Type (Old_Formal_Base)
5979 and then Is_Generic_Type (New_Formal_Base)
5980 and then Is_Internal (New_Formal_Base)
5981 and then Etype (Etype (New_Formal_Base)) =
5982 Old_Formal_Base))
5983 and then Directly_Designated_Type (Old_Formal_Base) =
5984 Directly_Designated_Type (New_Formal_Base)
5985 and then ((Is_Itype (Old_Formal_Base)
5986 and then Can_Never_Be_Null (Old_Formal_Base))
5987 or else
5988 (Is_Itype (New_Formal_Base)
5989 and then Can_Never_Be_Null (New_Formal_Base)));
5990
5991 -- Types must always match. In the visible part of an instance,
5992 -- usual overloading rules for dispatching operations apply, and
5993 -- we check base types (not the actual subtypes).
5994
5995 if In_Instance_Visible_Part
5996 and then Is_Dispatching_Operation (New_Id)
5997 then
5998 if not Conforming_Types
5999 (T1 => Base_Type (Etype (Old_Formal)),
6000 T2 => Base_Type (Etype (New_Formal)),
6001 Ctype => Ctype,
6002 Get_Inst => Get_Inst)
6003 and then not Access_Types_Match
6004 then
6005 Conformance_Error ("\type of & does not match!", New_Formal);
6006 return;
6007 end if;
6008
6009 elsif not Conforming_Types
6010 (T1 => Old_Formal_Base,
6011 T2 => New_Formal_Base,
6012 Ctype => Ctype,
6013 Get_Inst => Get_Inst)
6014 and then not Access_Types_Match
6015 then
6016 -- Don't give error message if old type is Any_Type. This test
6017 -- avoids some cascaded errors, e.g. in case of a bad spec.
6018
6019 if Errmsg and then Old_Formal_Base = Any_Type then
6020 Conforms := False;
6021 else
6022 Conformance_Error ("\type of & does not match!", New_Formal);
6023 end if;
6024
6025 return;
6026 end if;
6027
6028 -- For mode conformance, mode must match
6029
6030 if Ctype >= Mode_Conformant then
6031 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6032 if not Ekind_In (New_Id, E_Function, E_Procedure)
6033 or else not Is_Primitive_Wrapper (New_Id)
6034 then
6035 Conformance_Error ("\mode of & does not match!", New_Formal);
6036
6037 else
6038 declare
6039 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6040 begin
6041 if Is_Protected_Type
6042 (Corresponding_Concurrent_Type (T))
6043 then
6044 Error_Msg_PT (T, New_Id);
6045 else
6046 Conformance_Error
6047 ("\mode of & does not match!", New_Formal);
6048 end if;
6049 end;
6050 end if;
6051
6052 return;
6053
6054 -- Part of mode conformance for access types is having the same
6055 -- constant modifier.
6056
6057 elsif Access_Types_Match
6058 and then Is_Access_Constant (Old_Formal_Base) /=
6059 Is_Access_Constant (New_Formal_Base)
6060 then
6061 Conformance_Error
6062 ("\constant modifier does not match!", New_Formal);
6063 return;
6064 end if;
6065 end if;
6066
6067 if Ctype >= Subtype_Conformant then
6068
6069 -- Ada 2005 (AI-231): In case of anonymous access types check
6070 -- the null-exclusion and access-to-constant attributes must
6071 -- match. For null exclusion, we test the types rather than the
6072 -- formals themselves, since the attribute is only set reliably
6073 -- on the formals in the Ada 95 case, and we exclude the case
6074 -- where Old_Formal is marked as controlling, to avoid errors
6075 -- when matching completing bodies with dispatching declarations
6076 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6077
6078 if Ada_Version >= Ada_2005
6079 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6080 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6081 and then
6082 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6083 Can_Never_Be_Null (Etype (New_Formal))
6084 and then
6085 not Is_Controlling_Formal (Old_Formal))
6086 or else
6087 Is_Access_Constant (Etype (Old_Formal)) /=
6088 Is_Access_Constant (Etype (New_Formal)))
6089
6090 -- Do not complain if error already posted on New_Formal. This
6091 -- avoids some redundant error messages.
6092
6093 and then not Error_Posted (New_Formal)
6094 then
6095 -- It is allowed to omit the null-exclusion in case of stream
6096 -- attribute subprograms. We recognize stream subprograms
6097 -- through their TSS-generated suffix.
6098
6099 declare
6100 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6101
6102 begin
6103 if TSS_Name /= TSS_Stream_Read
6104 and then TSS_Name /= TSS_Stream_Write
6105 and then TSS_Name /= TSS_Stream_Input
6106 and then TSS_Name /= TSS_Stream_Output
6107 then
6108 -- Here we have a definite conformance error. It is worth
6109 -- special casing the error message for the case of a
6110 -- controlling formal (which excludes null).
6111
6112 if Is_Controlling_Formal (New_Formal) then
6113 Error_Msg_Node_2 := Scope (New_Formal);
6114 Conformance_Error
6115 ("\controlling formal& of& excludes null, "
6116 & "declaration must exclude null as well",
6117 New_Formal);
6118
6119 -- Normal case (couldn't we give more detail here???)
6120
6121 else
6122 Conformance_Error
6123 ("\type of & does not match!", New_Formal);
6124 end if;
6125
6126 return;
6127 end if;
6128 end;
6129 end if;
6130 end if;
6131
6132 -- Full conformance checks
6133
6134 if Ctype = Fully_Conformant then
6135
6136 -- We have checked already that names match
6137
6138 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6139
6140 -- Check default expressions for in parameters
6141
6142 declare
6143 NewD : constant Boolean :=
6144 Present (Default_Value (New_Formal));
6145 OldD : constant Boolean :=
6146 Present (Default_Value (Old_Formal));
6147 begin
6148 if NewD or OldD then
6149
6150 -- The old default value has been analyzed because the
6151 -- current full declaration will have frozen everything
6152 -- before. The new default value has not been analyzed,
6153 -- so analyze it now before we check for conformance.
6154
6155 if NewD then
6156 Push_Scope (New_Id);
6157 Preanalyze_Spec_Expression
6158 (Default_Value (New_Formal), Etype (New_Formal));
6159 End_Scope;
6160 end if;
6161
6162 if not (NewD and OldD)
6163 or else not Fully_Conformant_Expressions
6164 (Default_Value (Old_Formal),
6165 Default_Value (New_Formal))
6166 then
6167 Conformance_Error
6168 ("\default expression for & does not match!",
6169 New_Formal);
6170 return;
6171 end if;
6172 end if;
6173 end;
6174 end if;
6175 end if;
6176
6177 -- A couple of special checks for Ada 83 mode. These checks are
6178 -- skipped if either entity is an operator in package Standard,
6179 -- or if either old or new instance is not from the source program.
6180
6181 if Ada_Version = Ada_83
6182 and then Sloc (Old_Id) > Standard_Location
6183 and then Sloc (New_Id) > Standard_Location
6184 and then Comes_From_Source (Old_Id)
6185 and then Comes_From_Source (New_Id)
6186 then
6187 declare
6188 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6189 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6190
6191 begin
6192 -- Explicit IN must be present or absent in both cases. This
6193 -- test is required only in the full conformance case.
6194
6195 if In_Present (Old_Param) /= In_Present (New_Param)
6196 and then Ctype = Fully_Conformant
6197 then
6198 Conformance_Error
6199 ("\(Ada 83) IN must appear in both declarations",
6200 New_Formal);
6201 return;
6202 end if;
6203
6204 -- Grouping (use of comma in param lists) must be the same
6205 -- This is where we catch a misconformance like:
6206
6207 -- A, B : Integer
6208 -- A : Integer; B : Integer
6209
6210 -- which are represented identically in the tree except
6211 -- for the setting of the flags More_Ids and Prev_Ids.
6212
6213 if More_Ids (Old_Param) /= More_Ids (New_Param)
6214 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6215 then
6216 Conformance_Error
6217 ("\grouping of & does not match!", New_Formal);
6218 return;
6219 end if;
6220 end;
6221 end if;
6222
6223 -- This label is required when skipping controlling formals
6224
6225 <<Skip_Controlling_Formal>>
6226
6227 Next_Formal (Old_Formal);
6228 Next_Formal (New_Formal);
6229 end loop;
6230
6231 if Present (Old_Formal) then
6232 Conformance_Error ("\too few parameters!");
6233 return;
6234
6235 elsif Present (New_Formal) then
6236 Conformance_Error ("\too many parameters!", New_Formal);
6237 return;
6238 end if;
6239 end Check_Conformance;
6240
6241 -----------------------
6242 -- Check_Conventions --
6243 -----------------------
6244
6245 procedure Check_Conventions (Typ : Entity_Id) is
6246 Ifaces_List : Elist_Id;
6247
6248 procedure Check_Convention (Op : Entity_Id);
6249 -- Verify that the convention of inherited dispatching operation Op is
6250 -- consistent among all subprograms it overrides. In order to minimize
6251 -- the search, Search_From is utilized to designate a specific point in
6252 -- the list rather than iterating over the whole list once more.
6253
6254 ----------------------
6255 -- Check_Convention --
6256 ----------------------
6257
6258 procedure Check_Convention (Op : Entity_Id) is
6259 function Convention_Of (Id : Entity_Id) return Convention_Id;
6260 -- Given an entity, return its convention. The function treats Ghost
6261 -- as convention Ada because the two have the same dynamic semantics.
6262
6263 -------------------
6264 -- Convention_Of --
6265 -------------------
6266
6267 function Convention_Of (Id : Entity_Id) return Convention_Id is
6268 Conv : constant Convention_Id := Convention (Id);
6269 begin
6270 if Conv = Convention_Ghost then
6271 return Convention_Ada;
6272 else
6273 return Conv;
6274 end if;
6275 end Convention_Of;
6276
6277 -- Local variables
6278
6279 Op_Conv : constant Convention_Id := Convention_Of (Op);
6280 Iface_Conv : Convention_Id;
6281 Iface_Elmt : Elmt_Id;
6282 Iface_Prim_Elmt : Elmt_Id;
6283 Iface_Prim : Entity_Id;
6284
6285 -- Start of processing for Check_Convention
6286
6287 begin
6288 Iface_Elmt := First_Elmt (Ifaces_List);
6289 while Present (Iface_Elmt) loop
6290 Iface_Prim_Elmt :=
6291 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6292 while Present (Iface_Prim_Elmt) loop
6293 Iface_Prim := Node (Iface_Prim_Elmt);
6294 Iface_Conv := Convention_Of (Iface_Prim);
6295
6296 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6297 and then Iface_Conv /= Op_Conv
6298 then
6299 Error_Msg_N
6300 ("inconsistent conventions in primitive operations", Typ);
6301
6302 Error_Msg_Name_1 := Chars (Op);
6303 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6304 Error_Msg_Sloc := Sloc (Op);
6305
6306 if Comes_From_Source (Op) or else No (Alias (Op)) then
6307 if not Present (Overridden_Operation (Op)) then
6308 Error_Msg_N ("\\primitive % defined #", Typ);
6309 else
6310 Error_Msg_N
6311 ("\\overriding operation % with " &
6312 "convention % defined #", Typ);
6313 end if;
6314
6315 else pragma Assert (Present (Alias (Op)));
6316 Error_Msg_Sloc := Sloc (Alias (Op));
6317 Error_Msg_N
6318 ("\\inherited operation % with " &
6319 "convention % defined #", Typ);
6320 end if;
6321
6322 Error_Msg_Name_1 := Chars (Op);
6323 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6324 Error_Msg_Sloc := Sloc (Iface_Prim);
6325 Error_Msg_N
6326 ("\\overridden operation % with " &
6327 "convention % defined #", Typ);
6328
6329 -- Avoid cascading errors
6330
6331 return;
6332 end if;
6333
6334 Next_Elmt (Iface_Prim_Elmt);
6335 end loop;
6336
6337 Next_Elmt (Iface_Elmt);
6338 end loop;
6339 end Check_Convention;
6340
6341 -- Local variables
6342
6343 Prim_Op : Entity_Id;
6344 Prim_Op_Elmt : Elmt_Id;
6345
6346 -- Start of processing for Check_Conventions
6347
6348 begin
6349 if not Has_Interfaces (Typ) then
6350 return;
6351 end if;
6352
6353 Collect_Interfaces (Typ, Ifaces_List);
6354
6355 -- The algorithm checks every overriding dispatching operation against
6356 -- all the corresponding overridden dispatching operations, detecting
6357 -- differences in conventions.
6358
6359 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6360 while Present (Prim_Op_Elmt) loop
6361 Prim_Op := Node (Prim_Op_Elmt);
6362
6363 -- A small optimization: skip the predefined dispatching operations
6364 -- since they always have the same convention.
6365
6366 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6367 Check_Convention (Prim_Op);
6368 end if;
6369
6370 Next_Elmt (Prim_Op_Elmt);
6371 end loop;
6372 end Check_Conventions;
6373
6374 ------------------------------
6375 -- Check_Delayed_Subprogram --
6376 ------------------------------
6377
6378 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6379 F : Entity_Id;
6380
6381 procedure Possible_Freeze (T : Entity_Id);
6382 -- T is the type of either a formal parameter or of the return type.
6383 -- If T is not yet frozen and needs a delayed freeze, then the
6384 -- subprogram itself must be delayed. If T is the limited view of an
6385 -- incomplete type the subprogram must be frozen as well, because
6386 -- T may depend on local types that have not been frozen yet.
6387
6388 ---------------------
6389 -- Possible_Freeze --
6390 ---------------------
6391
6392 procedure Possible_Freeze (T : Entity_Id) is
6393 begin
6394 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6395 Set_Has_Delayed_Freeze (Designator);
6396
6397 elsif Is_Access_Type (T)
6398 and then Has_Delayed_Freeze (Designated_Type (T))
6399 and then not Is_Frozen (Designated_Type (T))
6400 then
6401 Set_Has_Delayed_Freeze (Designator);
6402
6403 elsif Ekind (T) = E_Incomplete_Type
6404 and then From_Limited_With (T)
6405 then
6406 Set_Has_Delayed_Freeze (Designator);
6407
6408 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6409 -- of a subprogram or entry declaration.
6410
6411 elsif Ekind (T) = E_Incomplete_Type
6412 and then Ada_Version >= Ada_2012
6413 then
6414 Set_Has_Delayed_Freeze (Designator);
6415 end if;
6416
6417 end Possible_Freeze;
6418
6419 -- Start of processing for Check_Delayed_Subprogram
6420
6421 begin
6422 -- All subprograms, including abstract subprograms, may need a freeze
6423 -- node if some formal type or the return type needs one.
6424
6425 Possible_Freeze (Etype (Designator));
6426 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6427
6428 -- Need delayed freeze if any of the formal types themselves need
6429 -- a delayed freeze and are not yet frozen.
6430
6431 F := First_Formal (Designator);
6432 while Present (F) loop
6433 Possible_Freeze (Etype (F));
6434 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6435 Next_Formal (F);
6436 end loop;
6437
6438 -- Mark functions that return by reference. Note that it cannot be
6439 -- done for delayed_freeze subprograms because the underlying
6440 -- returned type may not be known yet (for private types)
6441
6442 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6443 declare
6444 Typ : constant Entity_Id := Etype (Designator);
6445 Utyp : constant Entity_Id := Underlying_Type (Typ);
6446 begin
6447 if Is_Limited_View (Typ) then
6448 Set_Returns_By_Ref (Designator);
6449 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6450 Set_Returns_By_Ref (Designator);
6451 end if;
6452 end;
6453 end if;
6454 end Check_Delayed_Subprogram;
6455
6456 ------------------------------------
6457 -- Check_Discriminant_Conformance --
6458 ------------------------------------
6459
6460 procedure Check_Discriminant_Conformance
6461 (N : Node_Id;
6462 Prev : Entity_Id;
6463 Prev_Loc : Node_Id)
6464 is
6465 Old_Discr : Entity_Id := First_Discriminant (Prev);
6466 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6467 New_Discr_Id : Entity_Id;
6468 New_Discr_Type : Entity_Id;
6469
6470 procedure Conformance_Error (Msg : String; N : Node_Id);
6471 -- Post error message for conformance error on given node. Two messages
6472 -- are output. The first points to the previous declaration with a
6473 -- general "no conformance" message. The second is the detailed reason,
6474 -- supplied as Msg. The parameter N provide information for a possible
6475 -- & insertion in the message.
6476
6477 -----------------------
6478 -- Conformance_Error --
6479 -----------------------
6480
6481 procedure Conformance_Error (Msg : String; N : Node_Id) is
6482 begin
6483 Error_Msg_Sloc := Sloc (Prev_Loc);
6484 Error_Msg_N -- CODEFIX
6485 ("not fully conformant with declaration#!", N);
6486 Error_Msg_NE (Msg, N, N);
6487 end Conformance_Error;
6488
6489 -- Start of processing for Check_Discriminant_Conformance
6490
6491 begin
6492 while Present (Old_Discr) and then Present (New_Discr) loop
6493 New_Discr_Id := Defining_Identifier (New_Discr);
6494
6495 -- The subtype mark of the discriminant on the full type has not
6496 -- been analyzed so we do it here. For an access discriminant a new
6497 -- type is created.
6498
6499 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6500 New_Discr_Type :=
6501 Access_Definition (N, Discriminant_Type (New_Discr));
6502
6503 else
6504 Analyze (Discriminant_Type (New_Discr));
6505 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6506
6507 -- Ada 2005: if the discriminant definition carries a null
6508 -- exclusion, create an itype to check properly for consistency
6509 -- with partial declaration.
6510
6511 if Is_Access_Type (New_Discr_Type)
6512 and then Null_Exclusion_Present (New_Discr)
6513 then
6514 New_Discr_Type :=
6515 Create_Null_Excluding_Itype
6516 (T => New_Discr_Type,
6517 Related_Nod => New_Discr,
6518 Scope_Id => Current_Scope);
6519 end if;
6520 end if;
6521
6522 if not Conforming_Types
6523 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6524 then
6525 Conformance_Error ("type of & does not match!", New_Discr_Id);
6526 return;
6527 else
6528 -- Treat the new discriminant as an occurrence of the old one,
6529 -- for navigation purposes, and fill in some semantic
6530 -- information, for completeness.
6531
6532 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6533 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6534 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6535 end if;
6536
6537 -- Names must match
6538
6539 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6540 Conformance_Error ("name & does not match!", New_Discr_Id);
6541 return;
6542 end if;
6543
6544 -- Default expressions must match
6545
6546 declare
6547 NewD : constant Boolean :=
6548 Present (Expression (New_Discr));
6549 OldD : constant Boolean :=
6550 Present (Expression (Parent (Old_Discr)));
6551
6552 begin
6553 if NewD or OldD then
6554
6555 -- The old default value has been analyzed and expanded,
6556 -- because the current full declaration will have frozen
6557 -- everything before. The new default values have not been
6558 -- expanded, so expand now to check conformance.
6559
6560 if NewD then
6561 Preanalyze_Spec_Expression
6562 (Expression (New_Discr), New_Discr_Type);
6563 end if;
6564
6565 if not (NewD and OldD)
6566 or else not Fully_Conformant_Expressions
6567 (Expression (Parent (Old_Discr)),
6568 Expression (New_Discr))
6569
6570 then
6571 Conformance_Error
6572 ("default expression for & does not match!",
6573 New_Discr_Id);
6574 return;
6575 end if;
6576 end if;
6577 end;
6578
6579 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6580
6581 if Ada_Version = Ada_83 then
6582 declare
6583 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6584
6585 begin
6586 -- Grouping (use of comma in param lists) must be the same
6587 -- This is where we catch a misconformance like:
6588
6589 -- A, B : Integer
6590 -- A : Integer; B : Integer
6591
6592 -- which are represented identically in the tree except
6593 -- for the setting of the flags More_Ids and Prev_Ids.
6594
6595 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6596 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6597 then
6598 Conformance_Error
6599 ("grouping of & does not match!", New_Discr_Id);
6600 return;
6601 end if;
6602 end;
6603 end if;
6604
6605 Next_Discriminant (Old_Discr);
6606 Next (New_Discr);
6607 end loop;
6608
6609 if Present (Old_Discr) then
6610 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6611 return;
6612
6613 elsif Present (New_Discr) then
6614 Conformance_Error
6615 ("too many discriminants!", Defining_Identifier (New_Discr));
6616 return;
6617 end if;
6618 end Check_Discriminant_Conformance;
6619
6620 ----------------------------
6621 -- Check_Fully_Conformant --
6622 ----------------------------
6623
6624 procedure Check_Fully_Conformant
6625 (New_Id : Entity_Id;
6626 Old_Id : Entity_Id;
6627 Err_Loc : Node_Id := Empty)
6628 is
6629 Result : Boolean;
6630 pragma Warnings (Off, Result);
6631 begin
6632 Check_Conformance
6633 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6634 end Check_Fully_Conformant;
6635
6636 ---------------------------
6637 -- Check_Mode_Conformant --
6638 ---------------------------
6639
6640 procedure Check_Mode_Conformant
6641 (New_Id : Entity_Id;
6642 Old_Id : Entity_Id;
6643 Err_Loc : Node_Id := Empty;
6644 Get_Inst : Boolean := False)
6645 is
6646 Result : Boolean;
6647 pragma Warnings (Off, Result);
6648 begin
6649 Check_Conformance
6650 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6651 end Check_Mode_Conformant;
6652
6653 --------------------------------
6654 -- Check_Overriding_Indicator --
6655 --------------------------------
6656
6657 procedure Check_Overriding_Indicator
6658 (Subp : Entity_Id;
6659 Overridden_Subp : Entity_Id;
6660 Is_Primitive : Boolean)
6661 is
6662 Decl : Node_Id;
6663 Spec : Node_Id;
6664
6665 begin
6666 -- No overriding indicator for literals
6667
6668 if Ekind (Subp) = E_Enumeration_Literal then
6669 return;
6670
6671 elsif Ekind (Subp) = E_Entry then
6672 Decl := Parent (Subp);
6673
6674 -- No point in analyzing a malformed operator
6675
6676 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6677 and then Error_Posted (Subp)
6678 then
6679 return;
6680
6681 else
6682 Decl := Unit_Declaration_Node (Subp);
6683 end if;
6684
6685 if Nkind_In (Decl, N_Subprogram_Body,
6686 N_Subprogram_Body_Stub,
6687 N_Subprogram_Declaration,
6688 N_Abstract_Subprogram_Declaration,
6689 N_Subprogram_Renaming_Declaration)
6690 then
6691 Spec := Specification (Decl);
6692
6693 elsif Nkind (Decl) = N_Entry_Declaration then
6694 Spec := Decl;
6695
6696 else
6697 return;
6698 end if;
6699
6700 -- The overriding operation is type conformant with the overridden one,
6701 -- but the names of the formals are not required to match. If the names
6702 -- appear permuted in the overriding operation, this is a possible
6703 -- source of confusion that is worth diagnosing. Controlling formals
6704 -- often carry names that reflect the type, and it is not worthwhile
6705 -- requiring that their names match.
6706
6707 if Present (Overridden_Subp)
6708 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6709 then
6710 declare
6711 Form1 : Entity_Id;
6712 Form2 : Entity_Id;
6713
6714 begin
6715 Form1 := First_Formal (Subp);
6716 Form2 := First_Formal (Overridden_Subp);
6717
6718 -- If the overriding operation is a synchronized operation, skip
6719 -- the first parameter of the overridden operation, which is
6720 -- implicit in the new one. If the operation is declared in the
6721 -- body it is not primitive and all formals must match.
6722
6723 if Is_Concurrent_Type (Scope (Subp))
6724 and then Is_Tagged_Type (Scope (Subp))
6725 and then not Has_Completion (Scope (Subp))
6726 then
6727 Form2 := Next_Formal (Form2);
6728 end if;
6729
6730 if Present (Form1) then
6731 Form1 := Next_Formal (Form1);
6732 Form2 := Next_Formal (Form2);
6733 end if;
6734
6735 while Present (Form1) loop
6736 if not Is_Controlling_Formal (Form1)
6737 and then Present (Next_Formal (Form2))
6738 and then Chars (Form1) = Chars (Next_Formal (Form2))
6739 then
6740 Error_Msg_Node_2 := Alias (Overridden_Subp);
6741 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6742 Error_Msg_NE
6743 ("& does not match corresponding formal of&#",
6744 Form1, Form1);
6745 exit;
6746 end if;
6747
6748 Next_Formal (Form1);
6749 Next_Formal (Form2);
6750 end loop;
6751 end;
6752 end if;
6753
6754 -- If there is an overridden subprogram, then check that there is no
6755 -- "not overriding" indicator, and mark the subprogram as overriding.
6756 -- This is not done if the overridden subprogram is marked as hidden,
6757 -- which can occur for the case of inherited controlled operations
6758 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6759 -- subprogram is not itself hidden. (Note: This condition could probably
6760 -- be simplified, leaving out the testing for the specific controlled
6761 -- cases, but it seems safer and clearer this way, and echoes similar
6762 -- special-case tests of this kind in other places.)
6763
6764 if Present (Overridden_Subp)
6765 and then (not Is_Hidden (Overridden_Subp)
6766 or else
6767 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6768 Name_Adjust,
6769 Name_Finalize)
6770 and then Present (Alias (Overridden_Subp))
6771 and then not Is_Hidden (Alias (Overridden_Subp))))
6772 then
6773 if Must_Not_Override (Spec) then
6774 Error_Msg_Sloc := Sloc (Overridden_Subp);
6775
6776 if Ekind (Subp) = E_Entry then
6777 Error_Msg_NE
6778 ("entry & overrides inherited operation #", Spec, Subp);
6779 else
6780 Error_Msg_NE
6781 ("subprogram & overrides inherited operation #", Spec, Subp);
6782 end if;
6783
6784 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6785 -- as an extension of Root_Controlled, and thus has a useless Adjust
6786 -- operation. This operation should not be inherited by other limited
6787 -- controlled types. An explicit Adjust for them is not overriding.
6788
6789 elsif Must_Override (Spec)
6790 and then Chars (Overridden_Subp) = Name_Adjust
6791 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6792 and then Present (Alias (Overridden_Subp))
6793 and then
6794 Is_Predefined_File_Name
6795 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6796 then
6797 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6798
6799 elsif Is_Subprogram (Subp) then
6800 if Is_Init_Proc (Subp) then
6801 null;
6802
6803 elsif No (Overridden_Operation (Subp)) then
6804
6805 -- For entities generated by Derive_Subprograms the overridden
6806 -- operation is the inherited primitive (which is available
6807 -- through the attribute alias)
6808
6809 if (Is_Dispatching_Operation (Subp)
6810 or else Is_Dispatching_Operation (Overridden_Subp))
6811 and then not Comes_From_Source (Overridden_Subp)
6812 and then Find_Dispatching_Type (Overridden_Subp) =
6813 Find_Dispatching_Type (Subp)
6814 and then Present (Alias (Overridden_Subp))
6815 and then Comes_From_Source (Alias (Overridden_Subp))
6816 then
6817 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6818
6819 else
6820 Set_Overridden_Operation (Subp, Overridden_Subp);
6821 end if;
6822 end if;
6823 end if;
6824
6825 -- If primitive flag is set or this is a protected operation, then
6826 -- the operation is overriding at the point of its declaration, so
6827 -- warn if necessary. Otherwise it may have been declared before the
6828 -- operation it overrides and no check is required.
6829
6830 if Style_Check
6831 and then not Must_Override (Spec)
6832 and then (Is_Primitive
6833 or else Ekind (Scope (Subp)) = E_Protected_Type)
6834 then
6835 Style.Missing_Overriding (Decl, Subp);
6836 end if;
6837
6838 -- If Subp is an operator, it may override a predefined operation, if
6839 -- it is defined in the same scope as the type to which it applies.
6840 -- In that case Overridden_Subp is empty because of our implicit
6841 -- representation for predefined operators. We have to check whether the
6842 -- signature of Subp matches that of a predefined operator. Note that
6843 -- first argument provides the name of the operator, and the second
6844 -- argument the signature that may match that of a standard operation.
6845 -- If the indicator is overriding, then the operator must match a
6846 -- predefined signature, because we know already that there is no
6847 -- explicit overridden operation.
6848
6849 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6850 if Must_Not_Override (Spec) then
6851
6852 -- If this is not a primitive or a protected subprogram, then
6853 -- "not overriding" is illegal.
6854
6855 if not Is_Primitive
6856 and then Ekind (Scope (Subp)) /= E_Protected_Type
6857 then
6858 Error_Msg_N
6859 ("overriding indicator only allowed "
6860 & "if subprogram is primitive", Subp);
6861
6862 elsif Can_Override_Operator (Subp) then
6863 Error_Msg_NE
6864 ("subprogram& overrides predefined operator ", Spec, Subp);
6865 end if;
6866
6867 elsif Must_Override (Spec) then
6868 if No (Overridden_Operation (Subp))
6869 and then not Can_Override_Operator (Subp)
6870 then
6871 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6872 end if;
6873
6874 elsif not Error_Posted (Subp)
6875 and then Style_Check
6876 and then Can_Override_Operator (Subp)
6877 and then
6878 not Is_Predefined_File_Name
6879 (Unit_File_Name (Get_Source_Unit (Subp)))
6880 then
6881 -- If style checks are enabled, indicate that the indicator is
6882 -- missing. However, at the point of declaration, the type of
6883 -- which this is a primitive operation may be private, in which
6884 -- case the indicator would be premature.
6885
6886 if Has_Private_Declaration (Etype (Subp))
6887 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6888 then
6889 null;
6890 else
6891 Style.Missing_Overriding (Decl, Subp);
6892 end if;
6893 end if;
6894
6895 elsif Must_Override (Spec) then
6896 if Ekind (Subp) = E_Entry then
6897 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6898 else
6899 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6900 end if;
6901
6902 -- If the operation is marked "not overriding" and it's not primitive
6903 -- then an error is issued, unless this is an operation of a task or
6904 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6905 -- has been specified have already been checked above.
6906
6907 elsif Must_Not_Override (Spec)
6908 and then not Is_Primitive
6909 and then Ekind (Subp) /= E_Entry
6910 and then Ekind (Scope (Subp)) /= E_Protected_Type
6911 then
6912 Error_Msg_N
6913 ("overriding indicator only allowed if subprogram is primitive",
6914 Subp);
6915 return;
6916 end if;
6917 end Check_Overriding_Indicator;
6918
6919 -------------------
6920 -- Check_Returns --
6921 -------------------
6922
6923 -- Note: this procedure needs to know far too much about how the expander
6924 -- messes with exceptions. The use of the flag Exception_Junk and the
6925 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6926 -- works, but is not very clean. It would be better if the expansion
6927 -- routines would leave Original_Node working nicely, and we could use
6928 -- Original_Node here to ignore all the peculiar expander messing ???
6929
6930 procedure Check_Returns
6931 (HSS : Node_Id;
6932 Mode : Character;
6933 Err : out Boolean;
6934 Proc : Entity_Id := Empty)
6935 is
6936 Handler : Node_Id;
6937
6938 procedure Check_Statement_Sequence (L : List_Id);
6939 -- Internal recursive procedure to check a list of statements for proper
6940 -- termination by a return statement (or a transfer of control or a
6941 -- compound statement that is itself internally properly terminated).
6942
6943 ------------------------------
6944 -- Check_Statement_Sequence --
6945 ------------------------------
6946
6947 procedure Check_Statement_Sequence (L : List_Id) is
6948 Last_Stm : Node_Id;
6949 Stm : Node_Id;
6950 Kind : Node_Kind;
6951
6952 Raise_Exception_Call : Boolean;
6953 -- Set True if statement sequence terminated by Raise_Exception call
6954 -- or a Reraise_Occurrence call.
6955
6956 begin
6957 Raise_Exception_Call := False;
6958
6959 -- Get last real statement
6960
6961 Last_Stm := Last (L);
6962
6963 -- Deal with digging out exception handler statement sequences that
6964 -- have been transformed by the local raise to goto optimization.
6965 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6966 -- optimization has occurred, we are looking at something like:
6967
6968 -- begin
6969 -- original stmts in block
6970
6971 -- exception \
6972 -- when excep1 => |
6973 -- goto L1; | omitted if No_Exception_Propagation
6974 -- when excep2 => |
6975 -- goto L2; /
6976 -- end;
6977
6978 -- goto L3; -- skip handler when exception not raised
6979
6980 -- <<L1>> -- target label for local exception
6981 -- begin
6982 -- estmts1
6983 -- end;
6984
6985 -- goto L3;
6986
6987 -- <<L2>>
6988 -- begin
6989 -- estmts2
6990 -- end;
6991
6992 -- <<L3>>
6993
6994 -- and what we have to do is to dig out the estmts1 and estmts2
6995 -- sequences (which were the original sequences of statements in
6996 -- the exception handlers) and check them.
6997
6998 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6999 Stm := Last_Stm;
7000 loop
7001 Prev (Stm);
7002 exit when No (Stm);
7003 exit when Nkind (Stm) /= N_Block_Statement;
7004 exit when not Exception_Junk (Stm);
7005 Prev (Stm);
7006 exit when No (Stm);
7007 exit when Nkind (Stm) /= N_Label;
7008 exit when not Exception_Junk (Stm);
7009 Check_Statement_Sequence
7010 (Statements (Handled_Statement_Sequence (Next (Stm))));
7011
7012 Prev (Stm);
7013 Last_Stm := Stm;
7014 exit when No (Stm);
7015 exit when Nkind (Stm) /= N_Goto_Statement;
7016 exit when not Exception_Junk (Stm);
7017 end loop;
7018 end if;
7019
7020 -- Don't count pragmas
7021
7022 while Nkind (Last_Stm) = N_Pragma
7023
7024 -- Don't count call to SS_Release (can happen after Raise_Exception)
7025
7026 or else
7027 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7028 and then
7029 Nkind (Name (Last_Stm)) = N_Identifier
7030 and then
7031 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7032
7033 -- Don't count exception junk
7034
7035 or else
7036 (Nkind_In (Last_Stm, N_Goto_Statement,
7037 N_Label,
7038 N_Object_Declaration)
7039 and then Exception_Junk (Last_Stm))
7040 or else Nkind (Last_Stm) in N_Push_xxx_Label
7041 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7042
7043 -- Inserted code, such as finalization calls, is irrelevant: we only
7044 -- need to check original source.
7045
7046 or else Is_Rewrite_Insertion (Last_Stm)
7047 loop
7048 Prev (Last_Stm);
7049 end loop;
7050
7051 -- Here we have the "real" last statement
7052
7053 Kind := Nkind (Last_Stm);
7054
7055 -- Transfer of control, OK. Note that in the No_Return procedure
7056 -- case, we already diagnosed any explicit return statements, so
7057 -- we can treat them as OK in this context.
7058
7059 if Is_Transfer (Last_Stm) then
7060 return;
7061
7062 -- Check cases of explicit non-indirect procedure calls
7063
7064 elsif Kind = N_Procedure_Call_Statement
7065 and then Is_Entity_Name (Name (Last_Stm))
7066 then
7067 -- Check call to Raise_Exception procedure which is treated
7068 -- specially, as is a call to Reraise_Occurrence.
7069
7070 -- We suppress the warning in these cases since it is likely that
7071 -- the programmer really does not expect to deal with the case
7072 -- of Null_Occurrence, and thus would find a warning about a
7073 -- missing return curious, and raising Program_Error does not
7074 -- seem such a bad behavior if this does occur.
7075
7076 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7077 -- behavior will be to raise Constraint_Error (see AI-329).
7078
7079 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7080 or else
7081 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7082 then
7083 Raise_Exception_Call := True;
7084
7085 -- For Raise_Exception call, test first argument, if it is
7086 -- an attribute reference for a 'Identity call, then we know
7087 -- that the call cannot possibly return.
7088
7089 declare
7090 Arg : constant Node_Id :=
7091 Original_Node (First_Actual (Last_Stm));
7092 begin
7093 if Nkind (Arg) = N_Attribute_Reference
7094 and then Attribute_Name (Arg) = Name_Identity
7095 then
7096 return;
7097 end if;
7098 end;
7099 end if;
7100
7101 -- If statement, need to look inside if there is an else and check
7102 -- each constituent statement sequence for proper termination.
7103
7104 elsif Kind = N_If_Statement
7105 and then Present (Else_Statements (Last_Stm))
7106 then
7107 Check_Statement_Sequence (Then_Statements (Last_Stm));
7108 Check_Statement_Sequence (Else_Statements (Last_Stm));
7109
7110 if Present (Elsif_Parts (Last_Stm)) then
7111 declare
7112 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7113
7114 begin
7115 while Present (Elsif_Part) loop
7116 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7117 Next (Elsif_Part);
7118 end loop;
7119 end;
7120 end if;
7121
7122 return;
7123
7124 -- Case statement, check each case for proper termination
7125
7126 elsif Kind = N_Case_Statement then
7127 declare
7128 Case_Alt : Node_Id;
7129 begin
7130 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7131 while Present (Case_Alt) loop
7132 Check_Statement_Sequence (Statements (Case_Alt));
7133 Next_Non_Pragma (Case_Alt);
7134 end loop;
7135 end;
7136
7137 return;
7138
7139 -- Block statement, check its handled sequence of statements
7140
7141 elsif Kind = N_Block_Statement then
7142 declare
7143 Err1 : Boolean;
7144
7145 begin
7146 Check_Returns
7147 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7148
7149 if Err1 then
7150 Err := True;
7151 end if;
7152
7153 return;
7154 end;
7155
7156 -- Loop statement. If there is an iteration scheme, we can definitely
7157 -- fall out of the loop. Similarly if there is an exit statement, we
7158 -- can fall out. In either case we need a following return.
7159
7160 elsif Kind = N_Loop_Statement then
7161 if Present (Iteration_Scheme (Last_Stm))
7162 or else Has_Exit (Entity (Identifier (Last_Stm)))
7163 then
7164 null;
7165
7166 -- A loop with no exit statement or iteration scheme is either
7167 -- an infinite loop, or it has some other exit (raise/return).
7168 -- In either case, no warning is required.
7169
7170 else
7171 return;
7172 end if;
7173
7174 -- Timed entry call, check entry call and delay alternatives
7175
7176 -- Note: in expanded code, the timed entry call has been converted
7177 -- to a set of expanded statements on which the check will work
7178 -- correctly in any case.
7179
7180 elsif Kind = N_Timed_Entry_Call then
7181 declare
7182 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7183 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7184
7185 begin
7186 -- If statement sequence of entry call alternative is missing,
7187 -- then we can definitely fall through, and we post the error
7188 -- message on the entry call alternative itself.
7189
7190 if No (Statements (ECA)) then
7191 Last_Stm := ECA;
7192
7193 -- If statement sequence of delay alternative is missing, then
7194 -- we can definitely fall through, and we post the error
7195 -- message on the delay alternative itself.
7196
7197 -- Note: if both ECA and DCA are missing the return, then we
7198 -- post only one message, should be enough to fix the bugs.
7199 -- If not we will get a message next time on the DCA when the
7200 -- ECA is fixed!
7201
7202 elsif No (Statements (DCA)) then
7203 Last_Stm := DCA;
7204
7205 -- Else check both statement sequences
7206
7207 else
7208 Check_Statement_Sequence (Statements (ECA));
7209 Check_Statement_Sequence (Statements (DCA));
7210 return;
7211 end if;
7212 end;
7213
7214 -- Conditional entry call, check entry call and else part
7215
7216 -- Note: in expanded code, the conditional entry call has been
7217 -- converted to a set of expanded statements on which the check
7218 -- will work correctly in any case.
7219
7220 elsif Kind = N_Conditional_Entry_Call then
7221 declare
7222 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7223
7224 begin
7225 -- If statement sequence of entry call alternative is missing,
7226 -- then we can definitely fall through, and we post the error
7227 -- message on the entry call alternative itself.
7228
7229 if No (Statements (ECA)) then
7230 Last_Stm := ECA;
7231
7232 -- Else check statement sequence and else part
7233
7234 else
7235 Check_Statement_Sequence (Statements (ECA));
7236 Check_Statement_Sequence (Else_Statements (Last_Stm));
7237 return;
7238 end if;
7239 end;
7240 end if;
7241
7242 -- If we fall through, issue appropriate message
7243
7244 if Mode = 'F' then
7245 if not Raise_Exception_Call then
7246
7247 -- In GNATprove mode, it is an error to have a missing return
7248
7249 Error_Msg_Warn := SPARK_Mode /= On;
7250 Error_Msg_N
7251 ("RETURN statement missing following this statement<<!",
7252 Last_Stm);
7253 Error_Msg_N
7254 ("\Program_Error ]<<!", Last_Stm);
7255 end if;
7256
7257 -- Note: we set Err even though we have not issued a warning
7258 -- because we still have a case of a missing return. This is
7259 -- an extremely marginal case, probably will never be noticed
7260 -- but we might as well get it right.
7261
7262 Err := True;
7263
7264 -- Otherwise we have the case of a procedure marked No_Return
7265
7266 else
7267 if not Raise_Exception_Call then
7268 if GNATprove_Mode then
7269 Error_Msg_N
7270 ("implied return after this statement "
7271 & "would have raised Program_Error", Last_Stm);
7272 else
7273 Error_Msg_N
7274 ("implied return after this statement "
7275 & "will raise Program_Error??", Last_Stm);
7276 end if;
7277
7278 Error_Msg_Warn := SPARK_Mode /= On;
7279 Error_Msg_NE
7280 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7281 end if;
7282
7283 declare
7284 RE : constant Node_Id :=
7285 Make_Raise_Program_Error (Sloc (Last_Stm),
7286 Reason => PE_Implicit_Return);
7287 begin
7288 Insert_After (Last_Stm, RE);
7289 Analyze (RE);
7290 end;
7291 end if;
7292 end Check_Statement_Sequence;
7293
7294 -- Start of processing for Check_Returns
7295
7296 begin
7297 Err := False;
7298 Check_Statement_Sequence (Statements (HSS));
7299
7300 if Present (Exception_Handlers (HSS)) then
7301 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7302 while Present (Handler) loop
7303 Check_Statement_Sequence (Statements (Handler));
7304 Next_Non_Pragma (Handler);
7305 end loop;
7306 end if;
7307 end Check_Returns;
7308
7309 ----------------------------
7310 -- Check_Subprogram_Order --
7311 ----------------------------
7312
7313 procedure Check_Subprogram_Order (N : Node_Id) is
7314
7315 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7316 -- This is used to check if S1 > S2 in the sense required by this test,
7317 -- for example nameab < namec, but name2 < name10.
7318
7319 -----------------------------
7320 -- Subprogram_Name_Greater --
7321 -----------------------------
7322
7323 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7324 L1, L2 : Positive;
7325 N1, N2 : Natural;
7326
7327 begin
7328 -- Deal with special case where names are identical except for a
7329 -- numerical suffix. These are handled specially, taking the numeric
7330 -- ordering from the suffix into account.
7331
7332 L1 := S1'Last;
7333 while S1 (L1) in '0' .. '9' loop
7334 L1 := L1 - 1;
7335 end loop;
7336
7337 L2 := S2'Last;
7338 while S2 (L2) in '0' .. '9' loop
7339 L2 := L2 - 1;
7340 end loop;
7341
7342 -- If non-numeric parts non-equal, do straight compare
7343
7344 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7345 return S1 > S2;
7346
7347 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7348 -- that a missing suffix is treated as numeric zero in this test.
7349
7350 else
7351 N1 := 0;
7352 while L1 < S1'Last loop
7353 L1 := L1 + 1;
7354 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7355 end loop;
7356
7357 N2 := 0;
7358 while L2 < S2'Last loop
7359 L2 := L2 + 1;
7360 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7361 end loop;
7362
7363 return N1 > N2;
7364 end if;
7365 end Subprogram_Name_Greater;
7366
7367 -- Start of processing for Check_Subprogram_Order
7368
7369 begin
7370 -- Check body in alpha order if this is option
7371
7372 if Style_Check
7373 and then Style_Check_Order_Subprograms
7374 and then Nkind (N) = N_Subprogram_Body
7375 and then Comes_From_Source (N)
7376 and then In_Extended_Main_Source_Unit (N)
7377 then
7378 declare
7379 LSN : String_Ptr
7380 renames Scope_Stack.Table
7381 (Scope_Stack.Last).Last_Subprogram_Name;
7382
7383 Body_Id : constant Entity_Id :=
7384 Defining_Entity (Specification (N));
7385
7386 begin
7387 Get_Decoded_Name_String (Chars (Body_Id));
7388
7389 if LSN /= null then
7390 if Subprogram_Name_Greater
7391 (LSN.all, Name_Buffer (1 .. Name_Len))
7392 then
7393 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7394 end if;
7395
7396 Free (LSN);
7397 end if;
7398
7399 LSN := new String'(Name_Buffer (1 .. Name_Len));
7400 end;
7401 end if;
7402 end Check_Subprogram_Order;
7403
7404 ------------------------------
7405 -- Check_Subtype_Conformant --
7406 ------------------------------
7407
7408 procedure Check_Subtype_Conformant
7409 (New_Id : Entity_Id;
7410 Old_Id : Entity_Id;
7411 Err_Loc : Node_Id := Empty;
7412 Skip_Controlling_Formals : Boolean := False;
7413 Get_Inst : Boolean := False)
7414 is
7415 Result : Boolean;
7416 pragma Warnings (Off, Result);
7417 begin
7418 Check_Conformance
7419 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7420 Skip_Controlling_Formals => Skip_Controlling_Formals,
7421 Get_Inst => Get_Inst);
7422 end Check_Subtype_Conformant;
7423
7424 ---------------------------
7425 -- Check_Type_Conformant --
7426 ---------------------------
7427
7428 procedure Check_Type_Conformant
7429 (New_Id : Entity_Id;
7430 Old_Id : Entity_Id;
7431 Err_Loc : Node_Id := Empty)
7432 is
7433 Result : Boolean;
7434 pragma Warnings (Off, Result);
7435 begin
7436 Check_Conformance
7437 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7438 end Check_Type_Conformant;
7439
7440 ---------------------------
7441 -- Can_Override_Operator --
7442 ---------------------------
7443
7444 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7445 Typ : Entity_Id;
7446
7447 begin
7448 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7449 return False;
7450
7451 else
7452 Typ := Base_Type (Etype (First_Formal (Subp)));
7453
7454 -- Check explicitly that the operation is a primitive of the type
7455
7456 return Operator_Matches_Spec (Subp, Subp)
7457 and then not Is_Generic_Type (Typ)
7458 and then Scope (Subp) = Scope (Typ)
7459 and then not Is_Class_Wide_Type (Typ);
7460 end if;
7461 end Can_Override_Operator;
7462
7463 ----------------------
7464 -- Conforming_Types --
7465 ----------------------
7466
7467 function Conforming_Types
7468 (T1 : Entity_Id;
7469 T2 : Entity_Id;
7470 Ctype : Conformance_Type;
7471 Get_Inst : Boolean := False) return Boolean
7472 is
7473 Type_1 : Entity_Id := T1;
7474 Type_2 : Entity_Id := T2;
7475 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7476
7477 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7478 -- If neither T1 nor T2 are generic actual types, or if they are in
7479 -- different scopes (e.g. parent and child instances), then verify that
7480 -- the base types are equal. Otherwise T1 and T2 must be on the same
7481 -- subtype chain. The whole purpose of this procedure is to prevent
7482 -- spurious ambiguities in an instantiation that may arise if two
7483 -- distinct generic types are instantiated with the same actual.
7484
7485 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7486 -- An access parameter can designate an incomplete type. If the
7487 -- incomplete type is the limited view of a type from a limited_
7488 -- with_clause, check whether the non-limited view is available. If
7489 -- it is a (non-limited) incomplete type, get the full view.
7490
7491 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7492 -- Returns True if and only if either T1 denotes a limited view of T2
7493 -- or T2 denotes a limited view of T1. This can arise when the limited
7494 -- with view of a type is used in a subprogram declaration and the
7495 -- subprogram body is in the scope of a regular with clause for the
7496 -- same unit. In such a case, the two type entities can be considered
7497 -- identical for purposes of conformance checking.
7498
7499 ----------------------
7500 -- Base_Types_Match --
7501 ----------------------
7502
7503 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7504 BT1 : constant Entity_Id := Base_Type (T1);
7505 BT2 : constant Entity_Id := Base_Type (T2);
7506
7507 begin
7508 if T1 = T2 then
7509 return True;
7510
7511 elsif BT1 = BT2 then
7512
7513 -- The following is too permissive. A more precise test should
7514 -- check that the generic actual is an ancestor subtype of the
7515 -- other ???.
7516
7517 -- See code in Find_Corresponding_Spec that applies an additional
7518 -- filter to handle accidental amiguities in instances.
7519
7520 return not Is_Generic_Actual_Type (T1)
7521 or else not Is_Generic_Actual_Type (T2)
7522 or else Scope (T1) /= Scope (T2);
7523
7524 -- If T2 is a generic actual type it is declared as the subtype of
7525 -- the actual. If that actual is itself a subtype we need to use its
7526 -- own base type to check for compatibility.
7527
7528 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7529 return True;
7530
7531 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7532 return True;
7533
7534 else
7535 return False;
7536 end if;
7537 end Base_Types_Match;
7538
7539 --------------------------
7540 -- Find_Designated_Type --
7541 --------------------------
7542
7543 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7544 Desig : Entity_Id;
7545
7546 begin
7547 Desig := Directly_Designated_Type (T);
7548
7549 if Ekind (Desig) = E_Incomplete_Type then
7550
7551 -- If regular incomplete type, get full view if available
7552
7553 if Present (Full_View (Desig)) then
7554 Desig := Full_View (Desig);
7555
7556 -- If limited view of a type, get non-limited view if available,
7557 -- and check again for a regular incomplete type.
7558
7559 elsif Present (Non_Limited_View (Desig)) then
7560 Desig := Get_Full_View (Non_Limited_View (Desig));
7561 end if;
7562 end if;
7563
7564 return Desig;
7565 end Find_Designated_Type;
7566
7567 -------------------------------
7568 -- Matches_Limited_With_View --
7569 -------------------------------
7570
7571 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7572 begin
7573 -- In some cases a type imported through a limited_with clause, and
7574 -- its nonlimited view are both visible, for example in an anonymous
7575 -- access-to-class-wide type in a formal. Both entities designate the
7576 -- same type.
7577
7578 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7579 return True;
7580
7581 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7582 return True;
7583
7584 elsif From_Limited_With (T1)
7585 and then From_Limited_With (T2)
7586 and then Available_View (T1) = Available_View (T2)
7587 then
7588 return True;
7589
7590 else
7591 return False;
7592 end if;
7593 end Matches_Limited_With_View;
7594
7595 -- Start of processing for Conforming_Types
7596
7597 begin
7598 -- The context is an instance association for a formal access-to-
7599 -- subprogram type; the formal parameter types require mapping because
7600 -- they may denote other formal parameters of the generic unit.
7601
7602 if Get_Inst then
7603 Type_1 := Get_Instance_Of (T1);
7604 Type_2 := Get_Instance_Of (T2);
7605 end if;
7606
7607 -- If one of the types is a view of the other introduced by a limited
7608 -- with clause, treat these as conforming for all purposes.
7609
7610 if Matches_Limited_With_View (T1, T2) then
7611 return True;
7612
7613 elsif Base_Types_Match (Type_1, Type_2) then
7614 return Ctype <= Mode_Conformant
7615 or else Subtypes_Statically_Match (Type_1, Type_2);
7616
7617 elsif Is_Incomplete_Or_Private_Type (Type_1)
7618 and then Present (Full_View (Type_1))
7619 and then Base_Types_Match (Full_View (Type_1), Type_2)
7620 then
7621 return Ctype <= Mode_Conformant
7622 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7623
7624 elsif Ekind (Type_2) = E_Incomplete_Type
7625 and then Present (Full_View (Type_2))
7626 and then Base_Types_Match (Type_1, Full_View (Type_2))
7627 then
7628 return Ctype <= Mode_Conformant
7629 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7630
7631 elsif Is_Private_Type (Type_2)
7632 and then In_Instance
7633 and then Present (Full_View (Type_2))
7634 and then Base_Types_Match (Type_1, Full_View (Type_2))
7635 then
7636 return Ctype <= Mode_Conformant
7637 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7638 end if;
7639
7640 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7641 -- treated recursively because they carry a signature. As far as
7642 -- conformance is concerned, convention plays no role, and either
7643 -- or both could be access to protected subprograms.
7644
7645 Are_Anonymous_Access_To_Subprogram_Types :=
7646 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7647 E_Anonymous_Access_Protected_Subprogram_Type)
7648 and then
7649 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7650 E_Anonymous_Access_Protected_Subprogram_Type);
7651
7652 -- Test anonymous access type case. For this case, static subtype
7653 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7654 -- the base types because we may have built internal subtype entities
7655 -- to handle null-excluding types (see Process_Formals).
7656
7657 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7658 and then
7659 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7660
7661 -- Ada 2005 (AI-254)
7662
7663 or else Are_Anonymous_Access_To_Subprogram_Types
7664 then
7665 declare
7666 Desig_1 : Entity_Id;
7667 Desig_2 : Entity_Id;
7668
7669 begin
7670 -- In Ada 2005, access constant indicators must match for
7671 -- subtype conformance.
7672
7673 if Ada_Version >= Ada_2005
7674 and then Ctype >= Subtype_Conformant
7675 and then
7676 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7677 then
7678 return False;
7679 end if;
7680
7681 Desig_1 := Find_Designated_Type (Type_1);
7682 Desig_2 := Find_Designated_Type (Type_2);
7683
7684 -- If the context is an instance association for a formal
7685 -- access-to-subprogram type; formal access parameter designated
7686 -- types require mapping because they may denote other formal
7687 -- parameters of the generic unit.
7688
7689 if Get_Inst then
7690 Desig_1 := Get_Instance_Of (Desig_1);
7691 Desig_2 := Get_Instance_Of (Desig_2);
7692 end if;
7693
7694 -- It is possible for a Class_Wide_Type to be introduced for an
7695 -- incomplete type, in which case there is a separate class_ wide
7696 -- type for the full view. The types conform if their Etypes
7697 -- conform, i.e. one may be the full view of the other. This can
7698 -- only happen in the context of an access parameter, other uses
7699 -- of an incomplete Class_Wide_Type are illegal.
7700
7701 if Is_Class_Wide_Type (Desig_1)
7702 and then
7703 Is_Class_Wide_Type (Desig_2)
7704 then
7705 return
7706 Conforming_Types
7707 (Etype (Base_Type (Desig_1)),
7708 Etype (Base_Type (Desig_2)), Ctype);
7709
7710 elsif Are_Anonymous_Access_To_Subprogram_Types then
7711 if Ada_Version < Ada_2005 then
7712 return Ctype = Type_Conformant
7713 or else
7714 Subtypes_Statically_Match (Desig_1, Desig_2);
7715
7716 -- We must check the conformance of the signatures themselves
7717
7718 else
7719 declare
7720 Conformant : Boolean;
7721 begin
7722 Check_Conformance
7723 (Desig_1, Desig_2, Ctype, False, Conformant);
7724 return Conformant;
7725 end;
7726 end if;
7727
7728 else
7729 return Base_Type (Desig_1) = Base_Type (Desig_2)
7730 and then (Ctype = Type_Conformant
7731 or else
7732 Subtypes_Statically_Match (Desig_1, Desig_2));
7733 end if;
7734 end;
7735
7736 -- Otherwise definitely no match
7737
7738 else
7739 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7740 and then Is_Access_Type (Type_2))
7741 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7742 and then Is_Access_Type (Type_1)))
7743 and then
7744 Conforming_Types
7745 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7746 then
7747 May_Hide_Profile := True;
7748 end if;
7749
7750 return False;
7751 end if;
7752 end Conforming_Types;
7753
7754 --------------------------
7755 -- Create_Extra_Formals --
7756 --------------------------
7757
7758 procedure Create_Extra_Formals (E : Entity_Id) is
7759 Formal : Entity_Id;
7760 First_Extra : Entity_Id := Empty;
7761 Last_Extra : Entity_Id;
7762 Formal_Type : Entity_Id;
7763 P_Formal : Entity_Id := Empty;
7764
7765 function Add_Extra_Formal
7766 (Assoc_Entity : Entity_Id;
7767 Typ : Entity_Id;
7768 Scope : Entity_Id;
7769 Suffix : String) return Entity_Id;
7770 -- Add an extra formal to the current list of formals and extra formals.
7771 -- The extra formal is added to the end of the list of extra formals,
7772 -- and also returned as the result. These formals are always of mode IN.
7773 -- The new formal has the type Typ, is declared in Scope, and its name
7774 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7775 -- The following suffixes are currently used. They should not be changed
7776 -- without coordinating with CodePeer, which makes use of these to
7777 -- provide better messages.
7778
7779 -- O denotes the Constrained bit.
7780 -- L denotes the accessibility level.
7781 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7782 -- the full list in exp_ch6.BIP_Formal_Kind.
7783
7784 ----------------------
7785 -- Add_Extra_Formal --
7786 ----------------------
7787
7788 function Add_Extra_Formal
7789 (Assoc_Entity : Entity_Id;
7790 Typ : Entity_Id;
7791 Scope : Entity_Id;
7792 Suffix : String) return Entity_Id
7793 is
7794 EF : constant Entity_Id :=
7795 Make_Defining_Identifier (Sloc (Assoc_Entity),
7796 Chars => New_External_Name (Chars (Assoc_Entity),
7797 Suffix => Suffix));
7798
7799 begin
7800 -- A little optimization. Never generate an extra formal for the
7801 -- _init operand of an initialization procedure, since it could
7802 -- never be used.
7803
7804 if Chars (Formal) = Name_uInit then
7805 return Empty;
7806 end if;
7807
7808 Set_Ekind (EF, E_In_Parameter);
7809 Set_Actual_Subtype (EF, Typ);
7810 Set_Etype (EF, Typ);
7811 Set_Scope (EF, Scope);
7812 Set_Mechanism (EF, Default_Mechanism);
7813 Set_Formal_Validity (EF);
7814
7815 if No (First_Extra) then
7816 First_Extra := EF;
7817 Set_Extra_Formals (Scope, First_Extra);
7818 end if;
7819
7820 if Present (Last_Extra) then
7821 Set_Extra_Formal (Last_Extra, EF);
7822 end if;
7823
7824 Last_Extra := EF;
7825
7826 return EF;
7827 end Add_Extra_Formal;
7828
7829 -- Start of processing for Create_Extra_Formals
7830
7831 begin
7832 -- We never generate extra formals if expansion is not active because we
7833 -- don't need them unless we are generating code.
7834
7835 if not Expander_Active then
7836 return;
7837 end if;
7838
7839 -- No need to generate extra formals in interface thunks whose target
7840 -- primitive has no extra formals.
7841
7842 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7843 return;
7844 end if;
7845
7846 -- If this is a derived subprogram then the subtypes of the parent
7847 -- subprogram's formal parameters will be used to determine the need
7848 -- for extra formals.
7849
7850 if Is_Overloadable (E) and then Present (Alias (E)) then
7851 P_Formal := First_Formal (Alias (E));
7852 end if;
7853
7854 Last_Extra := Empty;
7855 Formal := First_Formal (E);
7856 while Present (Formal) loop
7857 Last_Extra := Formal;
7858 Next_Formal (Formal);
7859 end loop;
7860
7861 -- If Extra_formals were already created, don't do it again. This
7862 -- situation may arise for subprogram types created as part of
7863 -- dispatching calls (see Expand_Dispatching_Call)
7864
7865 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7866 return;
7867 end if;
7868
7869 -- If the subprogram is a predefined dispatching subprogram then don't
7870 -- generate any extra constrained or accessibility level formals. In
7871 -- general we suppress these for internal subprograms (by not calling
7872 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7873 -- generated stream attributes do get passed through because extra
7874 -- build-in-place formals are needed in some cases (limited 'Input).
7875
7876 if Is_Predefined_Internal_Operation (E) then
7877 goto Test_For_Func_Result_Extras;
7878 end if;
7879
7880 Formal := First_Formal (E);
7881 while Present (Formal) loop
7882
7883 -- Create extra formal for supporting the attribute 'Constrained.
7884 -- The case of a private type view without discriminants also
7885 -- requires the extra formal if the underlying type has defaulted
7886 -- discriminants.
7887
7888 if Ekind (Formal) /= E_In_Parameter then
7889 if Present (P_Formal) then
7890 Formal_Type := Etype (P_Formal);
7891 else
7892 Formal_Type := Etype (Formal);
7893 end if;
7894
7895 -- Do not produce extra formals for Unchecked_Union parameters.
7896 -- Jump directly to the end of the loop.
7897
7898 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7899 goto Skip_Extra_Formal_Generation;
7900 end if;
7901
7902 if not Has_Discriminants (Formal_Type)
7903 and then Ekind (Formal_Type) in Private_Kind
7904 and then Present (Underlying_Type (Formal_Type))
7905 then
7906 Formal_Type := Underlying_Type (Formal_Type);
7907 end if;
7908
7909 -- Suppress the extra formal if formal's subtype is constrained or
7910 -- indefinite, or we're compiling for Ada 2012 and the underlying
7911 -- type is tagged and limited. In Ada 2012, a limited tagged type
7912 -- can have defaulted discriminants, but 'Constrained is required
7913 -- to return True, so the formal is never needed (see AI05-0214).
7914 -- Note that this ensures consistency of calling sequences for
7915 -- dispatching operations when some types in a class have defaults
7916 -- on discriminants and others do not (and requiring the extra
7917 -- formal would introduce distributed overhead).
7918
7919 -- If the type does not have a completion yet, treat as prior to
7920 -- Ada 2012 for consistency.
7921
7922 if Has_Discriminants (Formal_Type)
7923 and then not Is_Constrained (Formal_Type)
7924 and then not Is_Indefinite_Subtype (Formal_Type)
7925 and then (Ada_Version < Ada_2012
7926 or else No (Underlying_Type (Formal_Type))
7927 or else not
7928 (Is_Limited_Type (Formal_Type)
7929 and then
7930 (Is_Tagged_Type
7931 (Underlying_Type (Formal_Type)))))
7932 then
7933 Set_Extra_Constrained
7934 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7935 end if;
7936 end if;
7937
7938 -- Create extra formal for supporting accessibility checking. This
7939 -- is done for both anonymous access formals and formals of named
7940 -- access types that are marked as controlling formals. The latter
7941 -- case can occur when Expand_Dispatching_Call creates a subprogram
7942 -- type and substitutes the types of access-to-class-wide actuals
7943 -- for the anonymous access-to-specific-type of controlling formals.
7944 -- Base_Type is applied because in cases where there is a null
7945 -- exclusion the formal may have an access subtype.
7946
7947 -- This is suppressed if we specifically suppress accessibility
7948 -- checks at the package level for either the subprogram, or the
7949 -- package in which it resides. However, we do not suppress it
7950 -- simply if the scope has accessibility checks suppressed, since
7951 -- this could cause trouble when clients are compiled with a
7952 -- different suppression setting. The explicit checks at the
7953 -- package level are safe from this point of view.
7954
7955 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7956 or else (Is_Controlling_Formal (Formal)
7957 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7958 and then not
7959 (Explicit_Suppress (E, Accessibility_Check)
7960 or else
7961 Explicit_Suppress (Scope (E), Accessibility_Check))
7962 and then
7963 (No (P_Formal)
7964 or else Present (Extra_Accessibility (P_Formal)))
7965 then
7966 Set_Extra_Accessibility
7967 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7968 end if;
7969
7970 -- This label is required when skipping extra formal generation for
7971 -- Unchecked_Union parameters.
7972
7973 <<Skip_Extra_Formal_Generation>>
7974
7975 if Present (P_Formal) then
7976 Next_Formal (P_Formal);
7977 end if;
7978
7979 Next_Formal (Formal);
7980 end loop;
7981
7982 <<Test_For_Func_Result_Extras>>
7983
7984 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7985 -- function call is ... determined by the point of call ...".
7986
7987 if Needs_Result_Accessibility_Level (E) then
7988 Set_Extra_Accessibility_Of_Result
7989 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7990 end if;
7991
7992 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7993 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7994
7995 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7996 declare
7997 Result_Subt : constant Entity_Id := Etype (E);
7998 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7999 Formal_Typ : Entity_Id;
8000
8001 Discard : Entity_Id;
8002 pragma Warnings (Off, Discard);
8003
8004 begin
8005 -- In the case of functions with unconstrained result subtypes,
8006 -- add a 4-state formal indicating whether the return object is
8007 -- allocated by the caller (1), or should be allocated by the
8008 -- callee on the secondary stack (2), in the global heap (3), or
8009 -- in a user-defined storage pool (4). For the moment we just use
8010 -- Natural for the type of this formal. Note that this formal
8011 -- isn't usually needed in the case where the result subtype is
8012 -- constrained, but it is needed when the function has a tagged
8013 -- result, because generally such functions can be called in a
8014 -- dispatching context and such calls must be handled like calls
8015 -- to a class-wide function.
8016
8017 if Needs_BIP_Alloc_Form (E) then
8018 Discard :=
8019 Add_Extra_Formal
8020 (E, Standard_Natural,
8021 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8022
8023 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8024 -- use a user-defined pool. This formal is not added on
8025 -- .NET/JVM/ZFP as those targets do not support pools.
8026
8027 if VM_Target = No_VM
8028 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8029 then
8030 Discard :=
8031 Add_Extra_Formal
8032 (E, RTE (RE_Root_Storage_Pool_Ptr),
8033 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8034 end if;
8035 end if;
8036
8037 -- In the case of functions whose result type needs finalization,
8038 -- add an extra formal which represents the finalization master.
8039
8040 if Needs_BIP_Finalization_Master (E) then
8041 Discard :=
8042 Add_Extra_Formal
8043 (E, RTE (RE_Finalization_Master_Ptr),
8044 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8045 end if;
8046
8047 -- When the result type contains tasks, add two extra formals: the
8048 -- master of the tasks to be created, and the caller's activation
8049 -- chain.
8050
8051 if Has_Task (Full_Subt) then
8052 Discard :=
8053 Add_Extra_Formal
8054 (E, RTE (RE_Master_Id),
8055 E, BIP_Formal_Suffix (BIP_Task_Master));
8056 Discard :=
8057 Add_Extra_Formal
8058 (E, RTE (RE_Activation_Chain_Access),
8059 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8060 end if;
8061
8062 -- All build-in-place functions get an extra formal that will be
8063 -- passed the address of the return object within the caller.
8064
8065 Formal_Typ :=
8066 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8067
8068 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8069 Set_Etype (Formal_Typ, Formal_Typ);
8070 Set_Depends_On_Private
8071 (Formal_Typ, Has_Private_Component (Formal_Typ));
8072 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8073 Set_Is_Access_Constant (Formal_Typ, False);
8074
8075 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8076 -- the designated type comes from the limited view (for back-end
8077 -- purposes).
8078
8079 Set_From_Limited_With
8080 (Formal_Typ, From_Limited_With (Result_Subt));
8081
8082 Layout_Type (Formal_Typ);
8083
8084 Discard :=
8085 Add_Extra_Formal
8086 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8087 end;
8088 end if;
8089 end Create_Extra_Formals;
8090
8091 -----------------------------
8092 -- Enter_Overloaded_Entity --
8093 -----------------------------
8094
8095 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8096 E : Entity_Id := Current_Entity_In_Scope (S);
8097 C_E : Entity_Id := Current_Entity (S);
8098
8099 begin
8100 if Present (E) then
8101 Set_Has_Homonym (E);
8102 Set_Has_Homonym (S);
8103 end if;
8104
8105 Set_Is_Immediately_Visible (S);
8106 Set_Scope (S, Current_Scope);
8107
8108 -- Chain new entity if front of homonym in current scope, so that
8109 -- homonyms are contiguous.
8110
8111 if Present (E) and then E /= C_E then
8112 while Homonym (C_E) /= E loop
8113 C_E := Homonym (C_E);
8114 end loop;
8115
8116 Set_Homonym (C_E, S);
8117
8118 else
8119 E := C_E;
8120 Set_Current_Entity (S);
8121 end if;
8122
8123 Set_Homonym (S, E);
8124
8125 if Is_Inherited_Operation (S) then
8126 Append_Inherited_Subprogram (S);
8127 else
8128 Append_Entity (S, Current_Scope);
8129 end if;
8130
8131 Set_Public_Status (S);
8132
8133 if Debug_Flag_E then
8134 Write_Str ("New overloaded entity chain: ");
8135 Write_Name (Chars (S));
8136
8137 E := S;
8138 while Present (E) loop
8139 Write_Str (" "); Write_Int (Int (E));
8140 E := Homonym (E);
8141 end loop;
8142
8143 Write_Eol;
8144 end if;
8145
8146 -- Generate warning for hiding
8147
8148 if Warn_On_Hiding
8149 and then Comes_From_Source (S)
8150 and then In_Extended_Main_Source_Unit (S)
8151 then
8152 E := S;
8153 loop
8154 E := Homonym (E);
8155 exit when No (E);
8156
8157 -- Warn unless genuine overloading. Do not emit warning on
8158 -- hiding predefined operators in Standard (these are either an
8159 -- (artifact of our implicit declarations, or simple noise) but
8160 -- keep warning on a operator defined on a local subtype, because
8161 -- of the real danger that different operators may be applied in
8162 -- various parts of the program.
8163
8164 -- Note that if E and S have the same scope, there is never any
8165 -- hiding. Either the two conflict, and the program is illegal,
8166 -- or S is overriding an implicit inherited subprogram.
8167
8168 if Scope (E) /= Scope (S)
8169 and then (not Is_Overloadable (E)
8170 or else Subtype_Conformant (E, S))
8171 and then (Is_Immediately_Visible (E)
8172 or else
8173 Is_Potentially_Use_Visible (S))
8174 then
8175 if Scope (E) /= Standard_Standard then
8176 Error_Msg_Sloc := Sloc (E);
8177 Error_Msg_N ("declaration of & hides one#?h?", S);
8178
8179 elsif Nkind (S) = N_Defining_Operator_Symbol
8180 and then
8181 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8182 then
8183 Error_Msg_N
8184 ("declaration of & hides predefined operator?h?", S);
8185 end if;
8186 end if;
8187 end loop;
8188 end if;
8189 end Enter_Overloaded_Entity;
8190
8191 -----------------------------
8192 -- Check_Untagged_Equality --
8193 -----------------------------
8194
8195 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8196 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8197 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8198 Obj_Decl : Node_Id;
8199
8200 begin
8201 if Nkind (Decl) = N_Subprogram_Declaration
8202 and then Is_Record_Type (Typ)
8203 and then not Is_Tagged_Type (Typ)
8204 then
8205 -- If the type is not declared in a package, or if we are in the
8206 -- body of the package or in some other scope, the new operation is
8207 -- not primitive, and therefore legal, though suspicious. If the
8208 -- type is a generic actual (sub)type, the operation is not primitive
8209 -- either because the base type is declared elsewhere.
8210
8211 if Is_Frozen (Typ) then
8212 if Ekind (Scope (Typ)) /= E_Package
8213 or else Scope (Typ) /= Current_Scope
8214 then
8215 null;
8216
8217 elsif Is_Generic_Actual_Type (Typ) then
8218 null;
8219
8220 elsif In_Package_Body (Scope (Typ)) then
8221 Error_Msg_NE
8222 ("equality operator must be declared "
8223 & "before type& is frozen", Eq_Op, Typ);
8224 Error_Msg_N
8225 ("\move declaration to package spec", Eq_Op);
8226
8227 else
8228 Error_Msg_NE
8229 ("equality operator must be declared "
8230 & "before type& is frozen", Eq_Op, Typ);
8231
8232 Obj_Decl := Next (Parent (Typ));
8233 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8234 if Nkind (Obj_Decl) = N_Object_Declaration
8235 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8236 then
8237 Error_Msg_NE
8238 ("type& is frozen by declaration??", Obj_Decl, Typ);
8239 Error_Msg_N
8240 ("\an equality operator cannot be declared after this "
8241 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8242 exit;
8243 end if;
8244
8245 Next (Obj_Decl);
8246 end loop;
8247 end if;
8248
8249 elsif not In_Same_List (Parent (Typ), Decl)
8250 and then not Is_Limited_Type (Typ)
8251 then
8252
8253 -- This makes it illegal to have a primitive equality declared in
8254 -- the private part if the type is visible.
8255
8256 Error_Msg_N ("equality operator appears too late", Eq_Op);
8257 end if;
8258 end if;
8259 end Check_Untagged_Equality;
8260
8261 -----------------------------
8262 -- Find_Corresponding_Spec --
8263 -----------------------------
8264
8265 function Find_Corresponding_Spec
8266 (N : Node_Id;
8267 Post_Error : Boolean := True) return Entity_Id
8268 is
8269 Spec : constant Node_Id := Specification (N);
8270 Designator : constant Entity_Id := Defining_Entity (Spec);
8271
8272 E : Entity_Id;
8273
8274 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8275 -- Even if fully conformant, a body may depend on a generic actual when
8276 -- the spec does not, or vice versa, in which case they were distinct
8277 -- entities in the generic.
8278
8279 -------------------------------
8280 -- Different_Generic_Profile --
8281 -------------------------------
8282
8283 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8284 F1, F2 : Entity_Id;
8285
8286 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8287 -- Check that the types of corresponding formals have the same
8288 -- generic actual if any. We have to account for subtypes of a
8289 -- generic formal, declared between a spec and a body, which may
8290 -- appear distinct in an instance but matched in the generic.
8291
8292 -------------------------
8293 -- Same_Generic_Actual --
8294 -------------------------
8295
8296 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8297 begin
8298 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8299 or else
8300 (Present (Parent (T1))
8301 and then Comes_From_Source (Parent (T1))
8302 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8303 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8304 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8305 end Same_Generic_Actual;
8306
8307 -- Start of processing for Different_Generic_Profile
8308
8309 begin
8310 if not In_Instance then
8311 return False;
8312
8313 elsif Ekind (E) = E_Function
8314 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8315 then
8316 return True;
8317 end if;
8318
8319 F1 := First_Formal (Designator);
8320 F2 := First_Formal (E);
8321 while Present (F1) loop
8322 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8323 return True;
8324 end if;
8325
8326 Next_Formal (F1);
8327 Next_Formal (F2);
8328 end loop;
8329
8330 return False;
8331 end Different_Generic_Profile;
8332
8333 -- Start of processing for Find_Corresponding_Spec
8334
8335 begin
8336 E := Current_Entity (Designator);
8337 while Present (E) loop
8338
8339 -- We are looking for a matching spec. It must have the same scope,
8340 -- and the same name, and either be type conformant, or be the case
8341 -- of a library procedure spec and its body (which belong to one
8342 -- another regardless of whether they are type conformant or not).
8343
8344 if Scope (E) = Current_Scope then
8345 if Current_Scope = Standard_Standard
8346 or else (Ekind (E) = Ekind (Designator)
8347 and then Type_Conformant (E, Designator))
8348 then
8349 -- Within an instantiation, we know that spec and body are
8350 -- subtype conformant, because they were subtype conformant in
8351 -- the generic. We choose the subtype-conformant entity here as
8352 -- well, to resolve spurious ambiguities in the instance that
8353 -- were not present in the generic (i.e. when two different
8354 -- types are given the same actual). If we are looking for a
8355 -- spec to match a body, full conformance is expected.
8356
8357 if In_Instance then
8358 Set_Convention (Designator, Convention (E));
8359
8360 -- Skip past subprogram bodies and subprogram renamings that
8361 -- may appear to have a matching spec, but that aren't fully
8362 -- conformant with it. That can occur in cases where an
8363 -- actual type causes unrelated homographs in the instance.
8364
8365 if Nkind_In (N, N_Subprogram_Body,
8366 N_Subprogram_Renaming_Declaration)
8367 and then Present (Homonym (E))
8368 and then not Fully_Conformant (Designator, E)
8369 then
8370 goto Next_Entity;
8371
8372 elsif not Subtype_Conformant (Designator, E) then
8373 goto Next_Entity;
8374
8375 elsif Different_Generic_Profile (E) then
8376 goto Next_Entity;
8377 end if;
8378 end if;
8379
8380 -- Ada 2012 (AI05-0165): For internally generated bodies of
8381 -- null procedures locate the internally generated spec. We
8382 -- enforce mode conformance since a tagged type may inherit
8383 -- from interfaces several null primitives which differ only
8384 -- in the mode of the formals.
8385
8386 if not (Comes_From_Source (E))
8387 and then Is_Null_Procedure (E)
8388 and then not Mode_Conformant (Designator, E)
8389 then
8390 null;
8391
8392 -- For null procedures coming from source that are completions,
8393 -- analysis of the generated body will establish the link.
8394
8395 elsif Comes_From_Source (E)
8396 and then Nkind (Spec) = N_Procedure_Specification
8397 and then Null_Present (Spec)
8398 then
8399 return E;
8400
8401 elsif not Has_Completion (E) then
8402 if Nkind (N) /= N_Subprogram_Body_Stub then
8403 Set_Corresponding_Spec (N, E);
8404 end if;
8405
8406 Set_Has_Completion (E);
8407 return E;
8408
8409 elsif Nkind (Parent (N)) = N_Subunit then
8410
8411 -- If this is the proper body of a subunit, the completion
8412 -- flag is set when analyzing the stub.
8413
8414 return E;
8415
8416 -- If E is an internal function with a controlling result that
8417 -- was created for an operation inherited by a null extension,
8418 -- it may be overridden by a body without a previous spec (one
8419 -- more reason why these should be shunned). In that case we
8420 -- remove the generated body if present, because the current
8421 -- one is the explicit overriding.
8422
8423 elsif Ekind (E) = E_Function
8424 and then Ada_Version >= Ada_2005
8425 and then not Comes_From_Source (E)
8426 and then Has_Controlling_Result (E)
8427 and then Is_Null_Extension (Etype (E))
8428 and then Comes_From_Source (Spec)
8429 then
8430 Set_Has_Completion (E, False);
8431
8432 if Expander_Active
8433 and then Nkind (Parent (E)) = N_Function_Specification
8434 then
8435 Remove
8436 (Unit_Declaration_Node
8437 (Corresponding_Body (Unit_Declaration_Node (E))));
8438
8439 return E;
8440
8441 -- If expansion is disabled, or if the wrapper function has
8442 -- not been generated yet, this a late body overriding an
8443 -- inherited operation, or it is an overriding by some other
8444 -- declaration before the controlling result is frozen. In
8445 -- either case this is a declaration of a new entity.
8446
8447 else
8448 return Empty;
8449 end if;
8450
8451 -- If the body already exists, then this is an error unless
8452 -- the previous declaration is the implicit declaration of a
8453 -- derived subprogram. It is also legal for an instance to
8454 -- contain type conformant overloadable declarations (but the
8455 -- generic declaration may not), per 8.3(26/2).
8456
8457 elsif No (Alias (E))
8458 and then not Is_Intrinsic_Subprogram (E)
8459 and then not In_Instance
8460 and then Post_Error
8461 then
8462 Error_Msg_Sloc := Sloc (E);
8463
8464 if Is_Imported (E) then
8465 Error_Msg_NE
8466 ("body not allowed for imported subprogram & declared#",
8467 N, E);
8468 else
8469 Error_Msg_NE ("duplicate body for & declared#", N, E);
8470 end if;
8471 end if;
8472
8473 -- Child units cannot be overloaded, so a conformance mismatch
8474 -- between body and a previous spec is an error.
8475
8476 elsif Is_Child_Unit (E)
8477 and then
8478 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8479 and then
8480 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8481 N_Compilation_Unit
8482 and then Post_Error
8483 then
8484 Error_Msg_N
8485 ("body of child unit does not match previous declaration", N);
8486 end if;
8487 end if;
8488
8489 <<Next_Entity>>
8490 E := Homonym (E);
8491 end loop;
8492
8493 -- On exit, we know that no previous declaration of subprogram exists
8494
8495 return Empty;
8496 end Find_Corresponding_Spec;
8497
8498 ----------------------
8499 -- Fully_Conformant --
8500 ----------------------
8501
8502 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8503 Result : Boolean;
8504 begin
8505 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8506 return Result;
8507 end Fully_Conformant;
8508
8509 ----------------------------------
8510 -- Fully_Conformant_Expressions --
8511 ----------------------------------
8512
8513 function Fully_Conformant_Expressions
8514 (Given_E1 : Node_Id;
8515 Given_E2 : Node_Id) return Boolean
8516 is
8517 E1 : constant Node_Id := Original_Node (Given_E1);
8518 E2 : constant Node_Id := Original_Node (Given_E2);
8519 -- We always test conformance on original nodes, since it is possible
8520 -- for analysis and/or expansion to make things look as though they
8521 -- conform when they do not, e.g. by converting 1+2 into 3.
8522
8523 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8524 renames Fully_Conformant_Expressions;
8525
8526 function FCL (L1, L2 : List_Id) return Boolean;
8527 -- Compare elements of two lists for conformance. Elements have to be
8528 -- conformant, and actuals inserted as default parameters do not match
8529 -- explicit actuals with the same value.
8530
8531 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8532 -- Compare an operator node with a function call
8533
8534 ---------
8535 -- FCL --
8536 ---------
8537
8538 function FCL (L1, L2 : List_Id) return Boolean is
8539 N1, N2 : Node_Id;
8540
8541 begin
8542 if L1 = No_List then
8543 N1 := Empty;
8544 else
8545 N1 := First (L1);
8546 end if;
8547
8548 if L2 = No_List then
8549 N2 := Empty;
8550 else
8551 N2 := First (L2);
8552 end if;
8553
8554 -- Compare two lists, skipping rewrite insertions (we want to compare
8555 -- the original trees, not the expanded versions!)
8556
8557 loop
8558 if Is_Rewrite_Insertion (N1) then
8559 Next (N1);
8560 elsif Is_Rewrite_Insertion (N2) then
8561 Next (N2);
8562 elsif No (N1) then
8563 return No (N2);
8564 elsif No (N2) then
8565 return False;
8566 elsif not FCE (N1, N2) then
8567 return False;
8568 else
8569 Next (N1);
8570 Next (N2);
8571 end if;
8572 end loop;
8573 end FCL;
8574
8575 ---------
8576 -- FCO --
8577 ---------
8578
8579 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8580 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8581 Act : Node_Id;
8582
8583 begin
8584 if No (Actuals)
8585 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8586 then
8587 return False;
8588
8589 else
8590 Act := First (Actuals);
8591
8592 if Nkind (Op_Node) in N_Binary_Op then
8593 if not FCE (Left_Opnd (Op_Node), Act) then
8594 return False;
8595 end if;
8596
8597 Next (Act);
8598 end if;
8599
8600 return Present (Act)
8601 and then FCE (Right_Opnd (Op_Node), Act)
8602 and then No (Next (Act));
8603 end if;
8604 end FCO;
8605
8606 -- Start of processing for Fully_Conformant_Expressions
8607
8608 begin
8609 -- Non-conformant if paren count does not match. Note: if some idiot
8610 -- complains that we don't do this right for more than 3 levels of
8611 -- parentheses, they will be treated with the respect they deserve!
8612
8613 if Paren_Count (E1) /= Paren_Count (E2) then
8614 return False;
8615
8616 -- If same entities are referenced, then they are conformant even if
8617 -- they have different forms (RM 8.3.1(19-20)).
8618
8619 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8620 if Present (Entity (E1)) then
8621 return Entity (E1) = Entity (E2)
8622 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8623 and then Ekind (Entity (E1)) = E_Discriminant
8624 and then Ekind (Entity (E2)) = E_In_Parameter);
8625
8626 elsif Nkind (E1) = N_Expanded_Name
8627 and then Nkind (E2) = N_Expanded_Name
8628 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8629 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8630 then
8631 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8632
8633 else
8634 -- Identifiers in component associations don't always have
8635 -- entities, but their names must conform.
8636
8637 return Nkind (E1) = N_Identifier
8638 and then Nkind (E2) = N_Identifier
8639 and then Chars (E1) = Chars (E2);
8640 end if;
8641
8642 elsif Nkind (E1) = N_Character_Literal
8643 and then Nkind (E2) = N_Expanded_Name
8644 then
8645 return Nkind (Selector_Name (E2)) = N_Character_Literal
8646 and then Chars (E1) = Chars (Selector_Name (E2));
8647
8648 elsif Nkind (E2) = N_Character_Literal
8649 and then Nkind (E1) = N_Expanded_Name
8650 then
8651 return Nkind (Selector_Name (E1)) = N_Character_Literal
8652 and then Chars (E2) = Chars (Selector_Name (E1));
8653
8654 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8655 return FCO (E1, E2);
8656
8657 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8658 return FCO (E2, E1);
8659
8660 -- Otherwise we must have the same syntactic entity
8661
8662 elsif Nkind (E1) /= Nkind (E2) then
8663 return False;
8664
8665 -- At this point, we specialize by node type
8666
8667 else
8668 case Nkind (E1) is
8669
8670 when N_Aggregate =>
8671 return
8672 FCL (Expressions (E1), Expressions (E2))
8673 and then
8674 FCL (Component_Associations (E1),
8675 Component_Associations (E2));
8676
8677 when N_Allocator =>
8678 if Nkind (Expression (E1)) = N_Qualified_Expression
8679 or else
8680 Nkind (Expression (E2)) = N_Qualified_Expression
8681 then
8682 return FCE (Expression (E1), Expression (E2));
8683
8684 -- Check that the subtype marks and any constraints
8685 -- are conformant
8686
8687 else
8688 declare
8689 Indic1 : constant Node_Id := Expression (E1);
8690 Indic2 : constant Node_Id := Expression (E2);
8691 Elt1 : Node_Id;
8692 Elt2 : Node_Id;
8693
8694 begin
8695 if Nkind (Indic1) /= N_Subtype_Indication then
8696 return
8697 Nkind (Indic2) /= N_Subtype_Indication
8698 and then Entity (Indic1) = Entity (Indic2);
8699
8700 elsif Nkind (Indic2) /= N_Subtype_Indication then
8701 return
8702 Nkind (Indic1) /= N_Subtype_Indication
8703 and then Entity (Indic1) = Entity (Indic2);
8704
8705 else
8706 if Entity (Subtype_Mark (Indic1)) /=
8707 Entity (Subtype_Mark (Indic2))
8708 then
8709 return False;
8710 end if;
8711
8712 Elt1 := First (Constraints (Constraint (Indic1)));
8713 Elt2 := First (Constraints (Constraint (Indic2)));
8714 while Present (Elt1) and then Present (Elt2) loop
8715 if not FCE (Elt1, Elt2) then
8716 return False;
8717 end if;
8718
8719 Next (Elt1);
8720 Next (Elt2);
8721 end loop;
8722
8723 return True;
8724 end if;
8725 end;
8726 end if;
8727
8728 when N_Attribute_Reference =>
8729 return
8730 Attribute_Name (E1) = Attribute_Name (E2)
8731 and then FCL (Expressions (E1), Expressions (E2));
8732
8733 when N_Binary_Op =>
8734 return
8735 Entity (E1) = Entity (E2)
8736 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8737 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8738
8739 when N_Short_Circuit | N_Membership_Test =>
8740 return
8741 FCE (Left_Opnd (E1), Left_Opnd (E2))
8742 and then
8743 FCE (Right_Opnd (E1), Right_Opnd (E2));
8744
8745 when N_Case_Expression =>
8746 declare
8747 Alt1 : Node_Id;
8748 Alt2 : Node_Id;
8749
8750 begin
8751 if not FCE (Expression (E1), Expression (E2)) then
8752 return False;
8753
8754 else
8755 Alt1 := First (Alternatives (E1));
8756 Alt2 := First (Alternatives (E2));
8757 loop
8758 if Present (Alt1) /= Present (Alt2) then
8759 return False;
8760 elsif No (Alt1) then
8761 return True;
8762 end if;
8763
8764 if not FCE (Expression (Alt1), Expression (Alt2))
8765 or else not FCL (Discrete_Choices (Alt1),
8766 Discrete_Choices (Alt2))
8767 then
8768 return False;
8769 end if;
8770
8771 Next (Alt1);
8772 Next (Alt2);
8773 end loop;
8774 end if;
8775 end;
8776
8777 when N_Character_Literal =>
8778 return
8779 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8780
8781 when N_Component_Association =>
8782 return
8783 FCL (Choices (E1), Choices (E2))
8784 and then
8785 FCE (Expression (E1), Expression (E2));
8786
8787 when N_Explicit_Dereference =>
8788 return
8789 FCE (Prefix (E1), Prefix (E2));
8790
8791 when N_Extension_Aggregate =>
8792 return
8793 FCL (Expressions (E1), Expressions (E2))
8794 and then Null_Record_Present (E1) =
8795 Null_Record_Present (E2)
8796 and then FCL (Component_Associations (E1),
8797 Component_Associations (E2));
8798
8799 when N_Function_Call =>
8800 return
8801 FCE (Name (E1), Name (E2))
8802 and then
8803 FCL (Parameter_Associations (E1),
8804 Parameter_Associations (E2));
8805
8806 when N_If_Expression =>
8807 return
8808 FCL (Expressions (E1), Expressions (E2));
8809
8810 when N_Indexed_Component =>
8811 return
8812 FCE (Prefix (E1), Prefix (E2))
8813 and then
8814 FCL (Expressions (E1), Expressions (E2));
8815
8816 when N_Integer_Literal =>
8817 return (Intval (E1) = Intval (E2));
8818
8819 when N_Null =>
8820 return True;
8821
8822 when N_Operator_Symbol =>
8823 return
8824 Chars (E1) = Chars (E2);
8825
8826 when N_Others_Choice =>
8827 return True;
8828
8829 when N_Parameter_Association =>
8830 return
8831 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8832 and then FCE (Explicit_Actual_Parameter (E1),
8833 Explicit_Actual_Parameter (E2));
8834
8835 when N_Qualified_Expression =>
8836 return
8837 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8838 and then
8839 FCE (Expression (E1), Expression (E2));
8840
8841 when N_Quantified_Expression =>
8842 if not FCE (Condition (E1), Condition (E2)) then
8843 return False;
8844 end if;
8845
8846 if Present (Loop_Parameter_Specification (E1))
8847 and then Present (Loop_Parameter_Specification (E2))
8848 then
8849 declare
8850 L1 : constant Node_Id :=
8851 Loop_Parameter_Specification (E1);
8852 L2 : constant Node_Id :=
8853 Loop_Parameter_Specification (E2);
8854
8855 begin
8856 return
8857 Reverse_Present (L1) = Reverse_Present (L2)
8858 and then
8859 FCE (Defining_Identifier (L1),
8860 Defining_Identifier (L2))
8861 and then
8862 FCE (Discrete_Subtype_Definition (L1),
8863 Discrete_Subtype_Definition (L2));
8864 end;
8865
8866 elsif Present (Iterator_Specification (E1))
8867 and then Present (Iterator_Specification (E2))
8868 then
8869 declare
8870 I1 : constant Node_Id := Iterator_Specification (E1);
8871 I2 : constant Node_Id := Iterator_Specification (E2);
8872
8873 begin
8874 return
8875 FCE (Defining_Identifier (I1),
8876 Defining_Identifier (I2))
8877 and then
8878 Of_Present (I1) = Of_Present (I2)
8879 and then
8880 Reverse_Present (I1) = Reverse_Present (I2)
8881 and then FCE (Name (I1), Name (I2))
8882 and then FCE (Subtype_Indication (I1),
8883 Subtype_Indication (I2));
8884 end;
8885
8886 -- The quantified expressions used different specifications to
8887 -- walk their respective ranges.
8888
8889 else
8890 return False;
8891 end if;
8892
8893 when N_Range =>
8894 return
8895 FCE (Low_Bound (E1), Low_Bound (E2))
8896 and then
8897 FCE (High_Bound (E1), High_Bound (E2));
8898
8899 when N_Real_Literal =>
8900 return (Realval (E1) = Realval (E2));
8901
8902 when N_Selected_Component =>
8903 return
8904 FCE (Prefix (E1), Prefix (E2))
8905 and then
8906 FCE (Selector_Name (E1), Selector_Name (E2));
8907
8908 when N_Slice =>
8909 return
8910 FCE (Prefix (E1), Prefix (E2))
8911 and then
8912 FCE (Discrete_Range (E1), Discrete_Range (E2));
8913
8914 when N_String_Literal =>
8915 declare
8916 S1 : constant String_Id := Strval (E1);
8917 S2 : constant String_Id := Strval (E2);
8918 L1 : constant Nat := String_Length (S1);
8919 L2 : constant Nat := String_Length (S2);
8920
8921 begin
8922 if L1 /= L2 then
8923 return False;
8924
8925 else
8926 for J in 1 .. L1 loop
8927 if Get_String_Char (S1, J) /=
8928 Get_String_Char (S2, J)
8929 then
8930 return False;
8931 end if;
8932 end loop;
8933
8934 return True;
8935 end if;
8936 end;
8937
8938 when N_Type_Conversion =>
8939 return
8940 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8941 and then
8942 FCE (Expression (E1), Expression (E2));
8943
8944 when N_Unary_Op =>
8945 return
8946 Entity (E1) = Entity (E2)
8947 and then
8948 FCE (Right_Opnd (E1), Right_Opnd (E2));
8949
8950 when N_Unchecked_Type_Conversion =>
8951 return
8952 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8953 and then
8954 FCE (Expression (E1), Expression (E2));
8955
8956 -- All other node types cannot appear in this context. Strictly
8957 -- we should raise a fatal internal error. Instead we just ignore
8958 -- the nodes. This means that if anyone makes a mistake in the
8959 -- expander and mucks an expression tree irretrievably, the result
8960 -- will be a failure to detect a (probably very obscure) case
8961 -- of non-conformance, which is better than bombing on some
8962 -- case where two expressions do in fact conform.
8963
8964 when others =>
8965 return True;
8966
8967 end case;
8968 end if;
8969 end Fully_Conformant_Expressions;
8970
8971 ----------------------------------------
8972 -- Fully_Conformant_Discrete_Subtypes --
8973 ----------------------------------------
8974
8975 function Fully_Conformant_Discrete_Subtypes
8976 (Given_S1 : Node_Id;
8977 Given_S2 : Node_Id) return Boolean
8978 is
8979 S1 : constant Node_Id := Original_Node (Given_S1);
8980 S2 : constant Node_Id := Original_Node (Given_S2);
8981
8982 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8983 -- Special-case for a bound given by a discriminant, which in the body
8984 -- is replaced with the discriminal of the enclosing type.
8985
8986 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8987 -- Check both bounds
8988
8989 -----------------------
8990 -- Conforming_Bounds --
8991 -----------------------
8992
8993 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8994 begin
8995 if Is_Entity_Name (B1)
8996 and then Is_Entity_Name (B2)
8997 and then Ekind (Entity (B1)) = E_Discriminant
8998 then
8999 return Chars (B1) = Chars (B2);
9000
9001 else
9002 return Fully_Conformant_Expressions (B1, B2);
9003 end if;
9004 end Conforming_Bounds;
9005
9006 -----------------------
9007 -- Conforming_Ranges --
9008 -----------------------
9009
9010 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9011 begin
9012 return
9013 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9014 and then
9015 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9016 end Conforming_Ranges;
9017
9018 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9019
9020 begin
9021 if Nkind (S1) /= Nkind (S2) then
9022 return False;
9023
9024 elsif Is_Entity_Name (S1) then
9025 return Entity (S1) = Entity (S2);
9026
9027 elsif Nkind (S1) = N_Range then
9028 return Conforming_Ranges (S1, S2);
9029
9030 elsif Nkind (S1) = N_Subtype_Indication then
9031 return
9032 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9033 and then
9034 Conforming_Ranges
9035 (Range_Expression (Constraint (S1)),
9036 Range_Expression (Constraint (S2)));
9037 else
9038 return True;
9039 end if;
9040 end Fully_Conformant_Discrete_Subtypes;
9041
9042 --------------------
9043 -- Install_Entity --
9044 --------------------
9045
9046 procedure Install_Entity (E : Entity_Id) is
9047 Prev : constant Entity_Id := Current_Entity (E);
9048 begin
9049 Set_Is_Immediately_Visible (E);
9050 Set_Current_Entity (E);
9051 Set_Homonym (E, Prev);
9052 end Install_Entity;
9053
9054 ---------------------
9055 -- Install_Formals --
9056 ---------------------
9057
9058 procedure Install_Formals (Id : Entity_Id) is
9059 F : Entity_Id;
9060 begin
9061 F := First_Formal (Id);
9062 while Present (F) loop
9063 Install_Entity (F);
9064 Next_Formal (F);
9065 end loop;
9066 end Install_Formals;
9067
9068 -----------------------------
9069 -- Is_Interface_Conformant --
9070 -----------------------------
9071
9072 function Is_Interface_Conformant
9073 (Tagged_Type : Entity_Id;
9074 Iface_Prim : Entity_Id;
9075 Prim : Entity_Id) return Boolean
9076 is
9077 -- The operation may in fact be an inherited (implicit) operation
9078 -- rather than the original interface primitive, so retrieve the
9079 -- ultimate ancestor.
9080
9081 Iface : constant Entity_Id :=
9082 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9083 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9084
9085 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9086 -- Return the controlling formal of Prim
9087
9088 ------------------------
9089 -- Controlling_Formal --
9090 ------------------------
9091
9092 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9093 E : Entity_Id;
9094
9095 begin
9096 E := First_Entity (Prim);
9097 while Present (E) loop
9098 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9099 return E;
9100 end if;
9101
9102 Next_Entity (E);
9103 end loop;
9104
9105 return Empty;
9106 end Controlling_Formal;
9107
9108 -- Local variables
9109
9110 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9111 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9112
9113 -- Start of processing for Is_Interface_Conformant
9114
9115 begin
9116 pragma Assert (Is_Subprogram (Iface_Prim)
9117 and then Is_Subprogram (Prim)
9118 and then Is_Dispatching_Operation (Iface_Prim)
9119 and then Is_Dispatching_Operation (Prim));
9120
9121 pragma Assert (Is_Interface (Iface)
9122 or else (Present (Alias (Iface_Prim))
9123 and then
9124 Is_Interface
9125 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9126
9127 if Prim = Iface_Prim
9128 or else not Is_Subprogram (Prim)
9129 or else Ekind (Prim) /= Ekind (Iface_Prim)
9130 or else not Is_Dispatching_Operation (Prim)
9131 or else Scope (Prim) /= Scope (Tagged_Type)
9132 or else No (Typ)
9133 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9134 or else not Primitive_Names_Match (Iface_Prim, Prim)
9135 then
9136 return False;
9137
9138 -- The mode of the controlling formals must match
9139
9140 elsif Present (Iface_Ctrl_F)
9141 and then Present (Prim_Ctrl_F)
9142 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9143 then
9144 return False;
9145
9146 -- Case of a procedure, or a function whose result type matches the
9147 -- result type of the interface primitive, or a function that has no
9148 -- controlling result (I or access I).
9149
9150 elsif Ekind (Iface_Prim) = E_Procedure
9151 or else Etype (Prim) = Etype (Iface_Prim)
9152 or else not Has_Controlling_Result (Prim)
9153 then
9154 return Type_Conformant
9155 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9156
9157 -- Case of a function returning an interface, or an access to one. Check
9158 -- that the return types correspond.
9159
9160 elsif Implements_Interface (Typ, Iface) then
9161 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9162 /=
9163 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9164 then
9165 return False;
9166 else
9167 return
9168 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9169 Skip_Controlling_Formals => True);
9170 end if;
9171
9172 else
9173 return False;
9174 end if;
9175 end Is_Interface_Conformant;
9176
9177 ---------------------------------
9178 -- Is_Non_Overriding_Operation --
9179 ---------------------------------
9180
9181 function Is_Non_Overriding_Operation
9182 (Prev_E : Entity_Id;
9183 New_E : Entity_Id) return Boolean
9184 is
9185 Formal : Entity_Id;
9186 F_Typ : Entity_Id;
9187 G_Typ : Entity_Id := Empty;
9188
9189 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9190 -- If F_Type is a derived type associated with a generic actual subtype,
9191 -- then return its Generic_Parent_Type attribute, else return Empty.
9192
9193 function Types_Correspond
9194 (P_Type : Entity_Id;
9195 N_Type : Entity_Id) return Boolean;
9196 -- Returns true if and only if the types (or designated types in the
9197 -- case of anonymous access types) are the same or N_Type is derived
9198 -- directly or indirectly from P_Type.
9199
9200 -----------------------------
9201 -- Get_Generic_Parent_Type --
9202 -----------------------------
9203
9204 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9205 G_Typ : Entity_Id;
9206 Defn : Node_Id;
9207 Indic : Node_Id;
9208
9209 begin
9210 if Is_Derived_Type (F_Typ)
9211 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9212 then
9213 -- The tree must be traversed to determine the parent subtype in
9214 -- the generic unit, which unfortunately isn't always available
9215 -- via semantic attributes. ??? (Note: The use of Original_Node
9216 -- is needed for cases where a full derived type has been
9217 -- rewritten.)
9218
9219 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9220 if Nkind (Defn) = N_Derived_Type_Definition then
9221 Indic := Subtype_Indication (Defn);
9222
9223 if Nkind (Indic) = N_Subtype_Indication then
9224 G_Typ := Entity (Subtype_Mark (Indic));
9225 else
9226 G_Typ := Entity (Indic);
9227 end if;
9228
9229 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9230 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9231 then
9232 return Generic_Parent_Type (Parent (G_Typ));
9233 end if;
9234 end if;
9235 end if;
9236
9237 return Empty;
9238 end Get_Generic_Parent_Type;
9239
9240 ----------------------
9241 -- Types_Correspond --
9242 ----------------------
9243
9244 function Types_Correspond
9245 (P_Type : Entity_Id;
9246 N_Type : Entity_Id) return Boolean
9247 is
9248 Prev_Type : Entity_Id := Base_Type (P_Type);
9249 New_Type : Entity_Id := Base_Type (N_Type);
9250
9251 begin
9252 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9253 Prev_Type := Designated_Type (Prev_Type);
9254 end if;
9255
9256 if Ekind (New_Type) = E_Anonymous_Access_Type then
9257 New_Type := Designated_Type (New_Type);
9258 end if;
9259
9260 if Prev_Type = New_Type then
9261 return True;
9262
9263 elsif not Is_Class_Wide_Type (New_Type) then
9264 while Etype (New_Type) /= New_Type loop
9265 New_Type := Etype (New_Type);
9266 if New_Type = Prev_Type then
9267 return True;
9268 end if;
9269 end loop;
9270 end if;
9271 return False;
9272 end Types_Correspond;
9273
9274 -- Start of processing for Is_Non_Overriding_Operation
9275
9276 begin
9277 -- In the case where both operations are implicit derived subprograms
9278 -- then neither overrides the other. This can only occur in certain
9279 -- obscure cases (e.g., derivation from homographs created in a generic
9280 -- instantiation).
9281
9282 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9283 return True;
9284
9285 elsif Ekind (Current_Scope) = E_Package
9286 and then Is_Generic_Instance (Current_Scope)
9287 and then In_Private_Part (Current_Scope)
9288 and then Comes_From_Source (New_E)
9289 then
9290 -- We examine the formals and result type of the inherited operation,
9291 -- to determine whether their type is derived from (the instance of)
9292 -- a generic type. The first such formal or result type is the one
9293 -- tested.
9294
9295 Formal := First_Formal (Prev_E);
9296 while Present (Formal) loop
9297 F_Typ := Base_Type (Etype (Formal));
9298
9299 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9300 F_Typ := Designated_Type (F_Typ);
9301 end if;
9302
9303 G_Typ := Get_Generic_Parent_Type (F_Typ);
9304 exit when Present (G_Typ);
9305
9306 Next_Formal (Formal);
9307 end loop;
9308
9309 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9310 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9311 end if;
9312
9313 if No (G_Typ) then
9314 return False;
9315 end if;
9316
9317 -- If the generic type is a private type, then the original operation
9318 -- was not overriding in the generic, because there was no primitive
9319 -- operation to override.
9320
9321 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9322 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9323 N_Formal_Private_Type_Definition
9324 then
9325 return True;
9326
9327 -- The generic parent type is the ancestor of a formal derived
9328 -- type declaration. We need to check whether it has a primitive
9329 -- operation that should be overridden by New_E in the generic.
9330
9331 else
9332 declare
9333 P_Formal : Entity_Id;
9334 N_Formal : Entity_Id;
9335 P_Typ : Entity_Id;
9336 N_Typ : Entity_Id;
9337 P_Prim : Entity_Id;
9338 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9339
9340 begin
9341 while Present (Prim_Elt) loop
9342 P_Prim := Node (Prim_Elt);
9343
9344 if Chars (P_Prim) = Chars (New_E)
9345 and then Ekind (P_Prim) = Ekind (New_E)
9346 then
9347 P_Formal := First_Formal (P_Prim);
9348 N_Formal := First_Formal (New_E);
9349 while Present (P_Formal) and then Present (N_Formal) loop
9350 P_Typ := Etype (P_Formal);
9351 N_Typ := Etype (N_Formal);
9352
9353 if not Types_Correspond (P_Typ, N_Typ) then
9354 exit;
9355 end if;
9356
9357 Next_Entity (P_Formal);
9358 Next_Entity (N_Formal);
9359 end loop;
9360
9361 -- Found a matching primitive operation belonging to the
9362 -- formal ancestor type, so the new subprogram is
9363 -- overriding.
9364
9365 if No (P_Formal)
9366 and then No (N_Formal)
9367 and then (Ekind (New_E) /= E_Function
9368 or else
9369 Types_Correspond
9370 (Etype (P_Prim), Etype (New_E)))
9371 then
9372 return False;
9373 end if;
9374 end if;
9375
9376 Next_Elmt (Prim_Elt);
9377 end loop;
9378
9379 -- If no match found, then the new subprogram does not override
9380 -- in the generic (nor in the instance).
9381
9382 -- If the type in question is not abstract, and the subprogram
9383 -- is, this will be an error if the new operation is in the
9384 -- private part of the instance. Emit a warning now, which will
9385 -- make the subsequent error message easier to understand.
9386
9387 if not Is_Abstract_Type (F_Typ)
9388 and then Is_Abstract_Subprogram (Prev_E)
9389 and then In_Private_Part (Current_Scope)
9390 then
9391 Error_Msg_Node_2 := F_Typ;
9392 Error_Msg_NE
9393 ("private operation& in generic unit does not override " &
9394 "any primitive operation of& (RM 12.3 (18))??",
9395 New_E, New_E);
9396 end if;
9397
9398 return True;
9399 end;
9400 end if;
9401 else
9402 return False;
9403 end if;
9404 end Is_Non_Overriding_Operation;
9405
9406 -------------------------------------
9407 -- List_Inherited_Pre_Post_Aspects --
9408 -------------------------------------
9409
9410 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9411 begin
9412 if Opt.List_Inherited_Aspects
9413 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9414 then
9415 declare
9416 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9417 P : Node_Id;
9418
9419 begin
9420 for J in Inherited'Range loop
9421 P := Pre_Post_Conditions (Contract (Inherited (J)));
9422 while Present (P) loop
9423 Error_Msg_Sloc := Sloc (P);
9424
9425 if Class_Present (P) and then not Split_PPC (P) then
9426 if Pragma_Name (P) = Name_Precondition then
9427 Error_Msg_N
9428 ("info: & inherits `Pre''Class` aspect from #?L?",
9429 E);
9430 else
9431 Error_Msg_N
9432 ("info: & inherits `Post''Class` aspect from #?L?",
9433 E);
9434 end if;
9435 end if;
9436
9437 P := Next_Pragma (P);
9438 end loop;
9439 end loop;
9440 end;
9441 end if;
9442 end List_Inherited_Pre_Post_Aspects;
9443
9444 ------------------------------
9445 -- Make_Inequality_Operator --
9446 ------------------------------
9447
9448 -- S is the defining identifier of an equality operator. We build a
9449 -- subprogram declaration with the right signature. This operation is
9450 -- intrinsic, because it is always expanded as the negation of the
9451 -- call to the equality function.
9452
9453 procedure Make_Inequality_Operator (S : Entity_Id) is
9454 Loc : constant Source_Ptr := Sloc (S);
9455 Decl : Node_Id;
9456 Formals : List_Id;
9457 Op_Name : Entity_Id;
9458
9459 FF : constant Entity_Id := First_Formal (S);
9460 NF : constant Entity_Id := Next_Formal (FF);
9461
9462 begin
9463 -- Check that equality was properly defined, ignore call if not
9464
9465 if No (NF) then
9466 return;
9467 end if;
9468
9469 declare
9470 A : constant Entity_Id :=
9471 Make_Defining_Identifier (Sloc (FF),
9472 Chars => Chars (FF));
9473
9474 B : constant Entity_Id :=
9475 Make_Defining_Identifier (Sloc (NF),
9476 Chars => Chars (NF));
9477
9478 begin
9479 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9480
9481 Formals := New_List (
9482 Make_Parameter_Specification (Loc,
9483 Defining_Identifier => A,
9484 Parameter_Type =>
9485 New_Reference_To (Etype (First_Formal (S)),
9486 Sloc (Etype (First_Formal (S))))),
9487
9488 Make_Parameter_Specification (Loc,
9489 Defining_Identifier => B,
9490 Parameter_Type =>
9491 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9492 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9493
9494 Decl :=
9495 Make_Subprogram_Declaration (Loc,
9496 Specification =>
9497 Make_Function_Specification (Loc,
9498 Defining_Unit_Name => Op_Name,
9499 Parameter_Specifications => Formals,
9500 Result_Definition =>
9501 New_Reference_To (Standard_Boolean, Loc)));
9502
9503 -- Insert inequality right after equality if it is explicit or after
9504 -- the derived type when implicit. These entities are created only
9505 -- for visibility purposes, and eventually replaced in the course
9506 -- of expansion, so they do not need to be attached to the tree and
9507 -- seen by the back-end. Keeping them internal also avoids spurious
9508 -- freezing problems. The declaration is inserted in the tree for
9509 -- analysis, and removed afterwards. If the equality operator comes
9510 -- from an explicit declaration, attach the inequality immediately
9511 -- after. Else the equality is inherited from a derived type
9512 -- declaration, so insert inequality after that declaration.
9513
9514 if No (Alias (S)) then
9515 Insert_After (Unit_Declaration_Node (S), Decl);
9516 elsif Is_List_Member (Parent (S)) then
9517 Insert_After (Parent (S), Decl);
9518 else
9519 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9520 end if;
9521
9522 Mark_Rewrite_Insertion (Decl);
9523 Set_Is_Intrinsic_Subprogram (Op_Name);
9524 Analyze (Decl);
9525 Remove (Decl);
9526 Set_Has_Completion (Op_Name);
9527 Set_Corresponding_Equality (Op_Name, S);
9528 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9529 end;
9530 end Make_Inequality_Operator;
9531
9532 ----------------------
9533 -- May_Need_Actuals --
9534 ----------------------
9535
9536 procedure May_Need_Actuals (Fun : Entity_Id) is
9537 F : Entity_Id;
9538 B : Boolean;
9539
9540 begin
9541 F := First_Formal (Fun);
9542 B := True;
9543 while Present (F) loop
9544 if No (Default_Value (F)) then
9545 B := False;
9546 exit;
9547 end if;
9548
9549 Next_Formal (F);
9550 end loop;
9551
9552 Set_Needs_No_Actuals (Fun, B);
9553 end May_Need_Actuals;
9554
9555 ---------------------
9556 -- Mode_Conformant --
9557 ---------------------
9558
9559 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9560 Result : Boolean;
9561 begin
9562 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9563 return Result;
9564 end Mode_Conformant;
9565
9566 ---------------------------
9567 -- New_Overloaded_Entity --
9568 ---------------------------
9569
9570 procedure New_Overloaded_Entity
9571 (S : Entity_Id;
9572 Derived_Type : Entity_Id := Empty)
9573 is
9574 Overridden_Subp : Entity_Id := Empty;
9575 -- Set if the current scope has an operation that is type-conformant
9576 -- with S, and becomes hidden by S.
9577
9578 Is_Primitive_Subp : Boolean;
9579 -- Set to True if the new subprogram is primitive
9580
9581 E : Entity_Id;
9582 -- Entity that S overrides
9583
9584 Prev_Vis : Entity_Id := Empty;
9585 -- Predecessor of E in Homonym chain
9586
9587 procedure Check_For_Primitive_Subprogram
9588 (Is_Primitive : out Boolean;
9589 Is_Overriding : Boolean := False);
9590 -- If the subprogram being analyzed is a primitive operation of the type
9591 -- of a formal or result, set the Has_Primitive_Operations flag on the
9592 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9593 -- corresponding flag on the entity itself for later use.
9594
9595 procedure Check_Synchronized_Overriding
9596 (Def_Id : Entity_Id;
9597 Overridden_Subp : out Entity_Id);
9598 -- First determine if Def_Id is an entry or a subprogram either defined
9599 -- in the scope of a task or protected type, or is a primitive of such
9600 -- a type. Check whether Def_Id overrides a subprogram of an interface
9601 -- implemented by the synchronized type, return the overridden entity
9602 -- or Empty.
9603
9604 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9605 -- Check that E is declared in the private part of the current package,
9606 -- or in the package body, where it may hide a previous declaration.
9607 -- We can't use In_Private_Part by itself because this flag is also
9608 -- set when freezing entities, so we must examine the place of the
9609 -- declaration in the tree, and recognize wrapper packages as well.
9610
9611 function Is_Overriding_Alias
9612 (Old_E : Entity_Id;
9613 New_E : Entity_Id) return Boolean;
9614 -- Check whether new subprogram and old subprogram are both inherited
9615 -- from subprograms that have distinct dispatch table entries. This can
9616 -- occur with derivations from instances with accidental homonyms. The
9617 -- function is conservative given that the converse is only true within
9618 -- instances that contain accidental overloadings.
9619
9620 ------------------------------------
9621 -- Check_For_Primitive_Subprogram --
9622 ------------------------------------
9623
9624 procedure Check_For_Primitive_Subprogram
9625 (Is_Primitive : out Boolean;
9626 Is_Overriding : Boolean := False)
9627 is
9628 Formal : Entity_Id;
9629 F_Typ : Entity_Id;
9630 B_Typ : Entity_Id;
9631
9632 function Visible_Part_Type (T : Entity_Id) return Boolean;
9633 -- Returns true if T is declared in the visible part of the current
9634 -- package scope; otherwise returns false. Assumes that T is declared
9635 -- in a package.
9636
9637 procedure Check_Private_Overriding (T : Entity_Id);
9638 -- Checks that if a primitive abstract subprogram of a visible
9639 -- abstract type is declared in a private part, then it must override
9640 -- an abstract subprogram declared in the visible part. Also checks
9641 -- that if a primitive function with a controlling result is declared
9642 -- in a private part, then it must override a function declared in
9643 -- the visible part.
9644
9645 ------------------------------
9646 -- Check_Private_Overriding --
9647 ------------------------------
9648
9649 procedure Check_Private_Overriding (T : Entity_Id) is
9650 begin
9651 if Is_Package_Or_Generic_Package (Current_Scope)
9652 and then In_Private_Part (Current_Scope)
9653 and then Visible_Part_Type (T)
9654 and then not In_Instance
9655 then
9656 if Is_Abstract_Type (T)
9657 and then Is_Abstract_Subprogram (S)
9658 and then (not Is_Overriding
9659 or else not Is_Abstract_Subprogram (E))
9660 then
9661 Error_Msg_N
9662 ("abstract subprograms must be visible "
9663 & "(RM 3.9.3(10))!", S);
9664
9665 elsif Ekind (S) = E_Function and then not Is_Overriding then
9666 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9667 Error_Msg_N
9668 ("private function with tagged result must"
9669 & " override visible-part function", S);
9670 Error_Msg_N
9671 ("\move subprogram to the visible part"
9672 & " (RM 3.9.3(10))", S);
9673
9674 -- AI05-0073: extend this test to the case of a function
9675 -- with a controlling access result.
9676
9677 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9678 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9679 and then
9680 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9681 and then Ada_Version >= Ada_2012
9682 then
9683 Error_Msg_N
9684 ("private function with controlling access result "
9685 & "must override visible-part function", S);
9686 Error_Msg_N
9687 ("\move subprogram to the visible part"
9688 & " (RM 3.9.3(10))", S);
9689 end if;
9690 end if;
9691 end if;
9692 end Check_Private_Overriding;
9693
9694 -----------------------
9695 -- Visible_Part_Type --
9696 -----------------------
9697
9698 function Visible_Part_Type (T : Entity_Id) return Boolean is
9699 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9700 N : Node_Id;
9701
9702 begin
9703 -- If the entity is a private type, then it must be declared in a
9704 -- visible part.
9705
9706 if Ekind (T) in Private_Kind then
9707 return True;
9708 end if;
9709
9710 -- Otherwise, we traverse the visible part looking for its
9711 -- corresponding declaration. We cannot use the declaration
9712 -- node directly because in the private part the entity of a
9713 -- private type is the one in the full view, which does not
9714 -- indicate that it is the completion of something visible.
9715
9716 N := First (Visible_Declarations (Specification (P)));
9717 while Present (N) loop
9718 if Nkind (N) = N_Full_Type_Declaration
9719 and then Present (Defining_Identifier (N))
9720 and then T = Defining_Identifier (N)
9721 then
9722 return True;
9723
9724 elsif Nkind_In (N, N_Private_Type_Declaration,
9725 N_Private_Extension_Declaration)
9726 and then Present (Defining_Identifier (N))
9727 and then T = Full_View (Defining_Identifier (N))
9728 then
9729 return True;
9730 end if;
9731
9732 Next (N);
9733 end loop;
9734
9735 return False;
9736 end Visible_Part_Type;
9737
9738 -- Start of processing for Check_For_Primitive_Subprogram
9739
9740 begin
9741 Is_Primitive := False;
9742
9743 if not Comes_From_Source (S) then
9744 null;
9745
9746 -- If subprogram is at library level, it is not primitive operation
9747
9748 elsif Current_Scope = Standard_Standard then
9749 null;
9750
9751 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9752 and then not In_Package_Body (Current_Scope))
9753 or else Is_Overriding
9754 then
9755 -- For function, check return type
9756
9757 if Ekind (S) = E_Function then
9758 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9759 F_Typ := Designated_Type (Etype (S));
9760 else
9761 F_Typ := Etype (S);
9762 end if;
9763
9764 B_Typ := Base_Type (F_Typ);
9765
9766 if Scope (B_Typ) = Current_Scope
9767 and then not Is_Class_Wide_Type (B_Typ)
9768 and then not Is_Generic_Type (B_Typ)
9769 then
9770 Is_Primitive := True;
9771 Set_Has_Primitive_Operations (B_Typ);
9772 Set_Is_Primitive (S);
9773 Check_Private_Overriding (B_Typ);
9774 end if;
9775 end if;
9776
9777 -- For all subprograms, check formals
9778
9779 Formal := First_Formal (S);
9780 while Present (Formal) loop
9781 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9782 F_Typ := Designated_Type (Etype (Formal));
9783 else
9784 F_Typ := Etype (Formal);
9785 end if;
9786
9787 B_Typ := Base_Type (F_Typ);
9788
9789 if Ekind (B_Typ) = E_Access_Subtype then
9790 B_Typ := Base_Type (B_Typ);
9791 end if;
9792
9793 if Scope (B_Typ) = Current_Scope
9794 and then not Is_Class_Wide_Type (B_Typ)
9795 and then not Is_Generic_Type (B_Typ)
9796 then
9797 Is_Primitive := True;
9798 Set_Is_Primitive (S);
9799 Set_Has_Primitive_Operations (B_Typ);
9800 Check_Private_Overriding (B_Typ);
9801 end if;
9802
9803 Next_Formal (Formal);
9804 end loop;
9805
9806 -- Special case: An equality function can be redefined for a type
9807 -- occurring in a declarative part, and won't otherwise be treated as
9808 -- a primitive because it doesn't occur in a package spec and doesn't
9809 -- override an inherited subprogram. It's important that we mark it
9810 -- primitive so it can be returned by Collect_Primitive_Operations
9811 -- and be used in composing the equality operation of later types
9812 -- that have a component of the type.
9813
9814 elsif Chars (S) = Name_Op_Eq
9815 and then Etype (S) = Standard_Boolean
9816 then
9817 B_Typ := Base_Type (Etype (First_Formal (S)));
9818
9819 if Scope (B_Typ) = Current_Scope
9820 and then
9821 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9822 and then not Is_Limited_Type (B_Typ)
9823 then
9824 Is_Primitive := True;
9825 Set_Is_Primitive (S);
9826 Set_Has_Primitive_Operations (B_Typ);
9827 Check_Private_Overriding (B_Typ);
9828 end if;
9829 end if;
9830 end Check_For_Primitive_Subprogram;
9831
9832 -----------------------------------
9833 -- Check_Synchronized_Overriding --
9834 -----------------------------------
9835
9836 procedure Check_Synchronized_Overriding
9837 (Def_Id : Entity_Id;
9838 Overridden_Subp : out Entity_Id)
9839 is
9840 Ifaces_List : Elist_Id;
9841 In_Scope : Boolean;
9842 Typ : Entity_Id;
9843
9844 function Matches_Prefixed_View_Profile
9845 (Prim_Params : List_Id;
9846 Iface_Params : List_Id) return Boolean;
9847 -- Determine whether a subprogram's parameter profile Prim_Params
9848 -- matches that of a potentially overridden interface subprogram
9849 -- Iface_Params. Also determine if the type of first parameter of
9850 -- Iface_Params is an implemented interface.
9851
9852 -----------------------------------
9853 -- Matches_Prefixed_View_Profile --
9854 -----------------------------------
9855
9856 function Matches_Prefixed_View_Profile
9857 (Prim_Params : List_Id;
9858 Iface_Params : List_Id) return Boolean
9859 is
9860 Iface_Id : Entity_Id;
9861 Iface_Param : Node_Id;
9862 Iface_Typ : Entity_Id;
9863 Prim_Id : Entity_Id;
9864 Prim_Param : Node_Id;
9865 Prim_Typ : Entity_Id;
9866
9867 function Is_Implemented
9868 (Ifaces_List : Elist_Id;
9869 Iface : Entity_Id) return Boolean;
9870 -- Determine if Iface is implemented by the current task or
9871 -- protected type.
9872
9873 --------------------
9874 -- Is_Implemented --
9875 --------------------
9876
9877 function Is_Implemented
9878 (Ifaces_List : Elist_Id;
9879 Iface : Entity_Id) return Boolean
9880 is
9881 Iface_Elmt : Elmt_Id;
9882
9883 begin
9884 Iface_Elmt := First_Elmt (Ifaces_List);
9885 while Present (Iface_Elmt) loop
9886 if Node (Iface_Elmt) = Iface then
9887 return True;
9888 end if;
9889
9890 Next_Elmt (Iface_Elmt);
9891 end loop;
9892
9893 return False;
9894 end Is_Implemented;
9895
9896 -- Start of processing for Matches_Prefixed_View_Profile
9897
9898 begin
9899 Iface_Param := First (Iface_Params);
9900 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9901
9902 if Is_Access_Type (Iface_Typ) then
9903 Iface_Typ := Designated_Type (Iface_Typ);
9904 end if;
9905
9906 Prim_Param := First (Prim_Params);
9907
9908 -- The first parameter of the potentially overridden subprogram
9909 -- must be an interface implemented by Prim.
9910
9911 if not Is_Interface (Iface_Typ)
9912 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9913 then
9914 return False;
9915 end if;
9916
9917 -- The checks on the object parameters are done, move onto the
9918 -- rest of the parameters.
9919
9920 if not In_Scope then
9921 Prim_Param := Next (Prim_Param);
9922 end if;
9923
9924 Iface_Param := Next (Iface_Param);
9925 while Present (Iface_Param) and then Present (Prim_Param) loop
9926 Iface_Id := Defining_Identifier (Iface_Param);
9927 Iface_Typ := Find_Parameter_Type (Iface_Param);
9928
9929 Prim_Id := Defining_Identifier (Prim_Param);
9930 Prim_Typ := Find_Parameter_Type (Prim_Param);
9931
9932 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9933 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9934 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9935 then
9936 Iface_Typ := Designated_Type (Iface_Typ);
9937 Prim_Typ := Designated_Type (Prim_Typ);
9938 end if;
9939
9940 -- Case of multiple interface types inside a parameter profile
9941
9942 -- (Obj_Param : in out Iface; ...; Param : Iface)
9943
9944 -- If the interface type is implemented, then the matching type
9945 -- in the primitive should be the implementing record type.
9946
9947 if Ekind (Iface_Typ) = E_Record_Type
9948 and then Is_Interface (Iface_Typ)
9949 and then Is_Implemented (Ifaces_List, Iface_Typ)
9950 then
9951 if Prim_Typ /= Typ then
9952 return False;
9953 end if;
9954
9955 -- The two parameters must be both mode and subtype conformant
9956
9957 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9958 or else not
9959 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9960 then
9961 return False;
9962 end if;
9963
9964 Next (Iface_Param);
9965 Next (Prim_Param);
9966 end loop;
9967
9968 -- One of the two lists contains more parameters than the other
9969
9970 if Present (Iface_Param) or else Present (Prim_Param) then
9971 return False;
9972 end if;
9973
9974 return True;
9975 end Matches_Prefixed_View_Profile;
9976
9977 -- Start of processing for Check_Synchronized_Overriding
9978
9979 begin
9980 Overridden_Subp := Empty;
9981
9982 -- Def_Id must be an entry or a subprogram. We should skip predefined
9983 -- primitives internally generated by the frontend; however at this
9984 -- stage predefined primitives are still not fully decorated. As a
9985 -- minor optimization we skip here internally generated subprograms.
9986
9987 if (Ekind (Def_Id) /= E_Entry
9988 and then Ekind (Def_Id) /= E_Function
9989 and then Ekind (Def_Id) /= E_Procedure)
9990 or else not Comes_From_Source (Def_Id)
9991 then
9992 return;
9993 end if;
9994
9995 -- Search for the concurrent declaration since it contains the list
9996 -- of all implemented interfaces. In this case, the subprogram is
9997 -- declared within the scope of a protected or a task type.
9998
9999 if Present (Scope (Def_Id))
10000 and then Is_Concurrent_Type (Scope (Def_Id))
10001 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10002 then
10003 Typ := Scope (Def_Id);
10004 In_Scope := True;
10005
10006 -- The enclosing scope is not a synchronized type and the subprogram
10007 -- has no formals.
10008
10009 elsif No (First_Formal (Def_Id)) then
10010 return;
10011
10012 -- The subprogram has formals and hence it may be a primitive of a
10013 -- concurrent type.
10014
10015 else
10016 Typ := Etype (First_Formal (Def_Id));
10017
10018 if Is_Access_Type (Typ) then
10019 Typ := Directly_Designated_Type (Typ);
10020 end if;
10021
10022 if Is_Concurrent_Type (Typ)
10023 and then not Is_Generic_Actual_Type (Typ)
10024 then
10025 In_Scope := False;
10026
10027 -- This case occurs when the concurrent type is declared within
10028 -- a generic unit. As a result the corresponding record has been
10029 -- built and used as the type of the first formal, we just have
10030 -- to retrieve the corresponding concurrent type.
10031
10032 elsif Is_Concurrent_Record_Type (Typ)
10033 and then not Is_Class_Wide_Type (Typ)
10034 and then Present (Corresponding_Concurrent_Type (Typ))
10035 then
10036 Typ := Corresponding_Concurrent_Type (Typ);
10037 In_Scope := False;
10038
10039 else
10040 return;
10041 end if;
10042 end if;
10043
10044 -- There is no overriding to check if is an inherited operation in a
10045 -- type derivation on for a generic actual.
10046
10047 Collect_Interfaces (Typ, Ifaces_List);
10048
10049 if Is_Empty_Elmt_List (Ifaces_List) then
10050 return;
10051 end if;
10052
10053 -- Determine whether entry or subprogram Def_Id overrides a primitive
10054 -- operation that belongs to one of the interfaces in Ifaces_List.
10055
10056 declare
10057 Candidate : Entity_Id := Empty;
10058 Hom : Entity_Id := Empty;
10059 Iface_Typ : Entity_Id;
10060 Subp : Entity_Id := Empty;
10061
10062 begin
10063 -- Traverse the homonym chain, looking for a potentially
10064 -- overridden subprogram that belongs to an implemented
10065 -- interface.
10066
10067 Hom := Current_Entity_In_Scope (Def_Id);
10068 while Present (Hom) loop
10069 Subp := Hom;
10070
10071 if Subp = Def_Id
10072 or else not Is_Overloadable (Subp)
10073 or else not Is_Primitive (Subp)
10074 or else not Is_Dispatching_Operation (Subp)
10075 or else not Present (Find_Dispatching_Type (Subp))
10076 or else not Is_Interface (Find_Dispatching_Type (Subp))
10077 then
10078 null;
10079
10080 -- Entries and procedures can override abstract or null
10081 -- interface procedures.
10082
10083 elsif (Ekind (Def_Id) = E_Procedure
10084 or else Ekind (Def_Id) = E_Entry)
10085 and then Ekind (Subp) = E_Procedure
10086 and then Matches_Prefixed_View_Profile
10087 (Parameter_Specifications (Parent (Def_Id)),
10088 Parameter_Specifications (Parent (Subp)))
10089 then
10090 Candidate := Subp;
10091
10092 -- For an overridden subprogram Subp, check whether the mode
10093 -- of its first parameter is correct depending on the kind
10094 -- of synchronized type.
10095
10096 declare
10097 Formal : constant Node_Id := First_Formal (Candidate);
10098
10099 begin
10100 -- In order for an entry or a protected procedure to
10101 -- override, the first parameter of the overridden
10102 -- routine must be of mode "out", "in out" or
10103 -- access-to-variable.
10104
10105 if Ekind_In (Candidate, E_Entry, E_Procedure)
10106 and then Is_Protected_Type (Typ)
10107 and then Ekind (Formal) /= E_In_Out_Parameter
10108 and then Ekind (Formal) /= E_Out_Parameter
10109 and then Nkind (Parameter_Type (Parent (Formal))) /=
10110 N_Access_Definition
10111 then
10112 null;
10113
10114 -- All other cases are OK since a task entry or routine
10115 -- does not have a restriction on the mode of the first
10116 -- parameter of the overridden interface routine.
10117
10118 else
10119 Overridden_Subp := Candidate;
10120 return;
10121 end if;
10122 end;
10123
10124 -- Functions can override abstract interface functions
10125
10126 elsif Ekind (Def_Id) = E_Function
10127 and then Ekind (Subp) = E_Function
10128 and then Matches_Prefixed_View_Profile
10129 (Parameter_Specifications (Parent (Def_Id)),
10130 Parameter_Specifications (Parent (Subp)))
10131 and then Etype (Result_Definition (Parent (Def_Id))) =
10132 Etype (Result_Definition (Parent (Subp)))
10133 then
10134 Overridden_Subp := Subp;
10135 return;
10136 end if;
10137
10138 Hom := Homonym (Hom);
10139 end loop;
10140
10141 -- After examining all candidates for overriding, we are left with
10142 -- the best match which is a mode incompatible interface routine.
10143 -- Do not emit an error if the Expander is active since this error
10144 -- will be detected later on after all concurrent types are
10145 -- expanded and all wrappers are built. This check is meant for
10146 -- spec-only compilations.
10147
10148 if Present (Candidate) and then not Expander_Active then
10149 Iface_Typ :=
10150 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10151
10152 -- Def_Id is primitive of a protected type, declared inside the
10153 -- type, and the candidate is primitive of a limited or
10154 -- synchronized interface.
10155
10156 if In_Scope
10157 and then Is_Protected_Type (Typ)
10158 and then
10159 (Is_Limited_Interface (Iface_Typ)
10160 or else Is_Protected_Interface (Iface_Typ)
10161 or else Is_Synchronized_Interface (Iface_Typ)
10162 or else Is_Task_Interface (Iface_Typ))
10163 then
10164 Error_Msg_PT (Parent (Typ), Candidate);
10165 end if;
10166 end if;
10167
10168 Overridden_Subp := Candidate;
10169 return;
10170 end;
10171 end Check_Synchronized_Overriding;
10172
10173 ----------------------------
10174 -- Is_Private_Declaration --
10175 ----------------------------
10176
10177 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10178 Priv_Decls : List_Id;
10179 Decl : constant Node_Id := Unit_Declaration_Node (E);
10180
10181 begin
10182 if Is_Package_Or_Generic_Package (Current_Scope)
10183 and then In_Private_Part (Current_Scope)
10184 then
10185 Priv_Decls :=
10186 Private_Declarations (Package_Specification (Current_Scope));
10187
10188 return In_Package_Body (Current_Scope)
10189 or else
10190 (Is_List_Member (Decl)
10191 and then List_Containing (Decl) = Priv_Decls)
10192 or else (Nkind (Parent (Decl)) = N_Package_Specification
10193 and then not
10194 Is_Compilation_Unit
10195 (Defining_Entity (Parent (Decl)))
10196 and then List_Containing (Parent (Parent (Decl))) =
10197 Priv_Decls);
10198 else
10199 return False;
10200 end if;
10201 end Is_Private_Declaration;
10202
10203 --------------------------
10204 -- Is_Overriding_Alias --
10205 --------------------------
10206
10207 function Is_Overriding_Alias
10208 (Old_E : Entity_Id;
10209 New_E : Entity_Id) return Boolean
10210 is
10211 AO : constant Entity_Id := Alias (Old_E);
10212 AN : constant Entity_Id := Alias (New_E);
10213
10214 begin
10215 return Scope (AO) /= Scope (AN)
10216 or else No (DTC_Entity (AO))
10217 or else No (DTC_Entity (AN))
10218 or else DT_Position (AO) = DT_Position (AN);
10219 end Is_Overriding_Alias;
10220
10221 -- Start of processing for New_Overloaded_Entity
10222
10223 begin
10224 -- We need to look for an entity that S may override. This must be a
10225 -- homonym in the current scope, so we look for the first homonym of
10226 -- S in the current scope as the starting point for the search.
10227
10228 E := Current_Entity_In_Scope (S);
10229
10230 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10231 -- They are directly added to the list of primitive operations of
10232 -- Derived_Type, unless this is a rederivation in the private part
10233 -- of an operation that was already derived in the visible part of
10234 -- the current package.
10235
10236 if Ada_Version >= Ada_2005
10237 and then Present (Derived_Type)
10238 and then Present (Alias (S))
10239 and then Is_Dispatching_Operation (Alias (S))
10240 and then Present (Find_Dispatching_Type (Alias (S)))
10241 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10242 then
10243 -- For private types, when the full-view is processed we propagate to
10244 -- the full view the non-overridden entities whose attribute "alias"
10245 -- references an interface primitive. These entities were added by
10246 -- Derive_Subprograms to ensure that interface primitives are
10247 -- covered.
10248
10249 -- Inside_Freeze_Actions is non zero when S corresponds with an
10250 -- internal entity that links an interface primitive with its
10251 -- covering primitive through attribute Interface_Alias (see
10252 -- Add_Internal_Interface_Entities).
10253
10254 if Inside_Freezing_Actions = 0
10255 and then Is_Package_Or_Generic_Package (Current_Scope)
10256 and then In_Private_Part (Current_Scope)
10257 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10258 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10259 and then Full_View (Defining_Identifier (Parent (E)))
10260 = Defining_Identifier (Parent (S))
10261 and then Alias (E) = Alias (S)
10262 then
10263 Check_Operation_From_Private_View (S, E);
10264 Set_Is_Dispatching_Operation (S);
10265
10266 -- Common case
10267
10268 else
10269 Enter_Overloaded_Entity (S);
10270 Check_Dispatching_Operation (S, Empty);
10271 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10272 end if;
10273
10274 return;
10275 end if;
10276
10277 -- If there is no homonym then this is definitely not overriding
10278
10279 if No (E) then
10280 Enter_Overloaded_Entity (S);
10281 Check_Dispatching_Operation (S, Empty);
10282 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10283
10284 -- If subprogram has an explicit declaration, check whether it has an
10285 -- overriding indicator.
10286
10287 if Comes_From_Source (S) then
10288 Check_Synchronized_Overriding (S, Overridden_Subp);
10289
10290 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10291 -- it may have overridden some hidden inherited primitive. Update
10292 -- Overridden_Subp to avoid spurious errors when checking the
10293 -- overriding indicator.
10294
10295 if Ada_Version >= Ada_2012
10296 and then No (Overridden_Subp)
10297 and then Is_Dispatching_Operation (S)
10298 and then Present (Overridden_Operation (S))
10299 then
10300 Overridden_Subp := Overridden_Operation (S);
10301 end if;
10302
10303 Check_Overriding_Indicator
10304 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10305 end if;
10306
10307 -- If there is a homonym that is not overloadable, then we have an
10308 -- error, except for the special cases checked explicitly below.
10309
10310 elsif not Is_Overloadable (E) then
10311
10312 -- Check for spurious conflict produced by a subprogram that has the
10313 -- same name as that of the enclosing generic package. The conflict
10314 -- occurs within an instance, between the subprogram and the renaming
10315 -- declaration for the package. After the subprogram, the package
10316 -- renaming declaration becomes hidden.
10317
10318 if Ekind (E) = E_Package
10319 and then Present (Renamed_Object (E))
10320 and then Renamed_Object (E) = Current_Scope
10321 and then Nkind (Parent (Renamed_Object (E))) =
10322 N_Package_Specification
10323 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10324 then
10325 Set_Is_Hidden (E);
10326 Set_Is_Immediately_Visible (E, False);
10327 Enter_Overloaded_Entity (S);
10328 Set_Homonym (S, Homonym (E));
10329 Check_Dispatching_Operation (S, Empty);
10330 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10331
10332 -- If the subprogram is implicit it is hidden by the previous
10333 -- declaration. However if it is dispatching, it must appear in the
10334 -- dispatch table anyway, because it can be dispatched to even if it
10335 -- cannot be called directly.
10336
10337 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10338 Set_Scope (S, Current_Scope);
10339
10340 if Is_Dispatching_Operation (Alias (S)) then
10341 Check_Dispatching_Operation (S, Empty);
10342 end if;
10343
10344 return;
10345
10346 else
10347 Error_Msg_Sloc := Sloc (E);
10348
10349 -- Generate message, with useful additional warning if in generic
10350
10351 if Is_Generic_Unit (E) then
10352 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10353 Error_Msg_N ("\& conflicts with declaration#", S);
10354 else
10355 Error_Msg_N ("& conflicts with declaration#", S);
10356 end if;
10357
10358 return;
10359 end if;
10360
10361 -- E exists and is overloadable
10362
10363 else
10364 Check_Synchronized_Overriding (S, Overridden_Subp);
10365
10366 -- Loop through E and its homonyms to determine if any of them is
10367 -- the candidate for overriding by S.
10368
10369 while Present (E) loop
10370
10371 -- Definitely not interesting if not in the current scope
10372
10373 if Scope (E) /= Current_Scope then
10374 null;
10375
10376 -- A function can overload the name of an abstract state. The
10377 -- state can be viewed as a function with a profile that cannot
10378 -- be matched by anything.
10379
10380 elsif Ekind (S) = E_Function
10381 and then Ekind (E) = E_Abstract_State
10382 then
10383 Enter_Overloaded_Entity (S);
10384 return;
10385
10386 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10387 -- procedures locate the internally generated spec. We enforce
10388 -- mode conformance since a tagged type may inherit from
10389 -- interfaces several null primitives which differ only in
10390 -- the mode of the formals.
10391
10392 elsif not Comes_From_Source (S)
10393 and then Is_Null_Procedure (S)
10394 and then not Mode_Conformant (E, S)
10395 then
10396 null;
10397
10398 -- Check if we have type conformance
10399
10400 elsif Type_Conformant (E, S) then
10401
10402 -- If the old and new entities have the same profile and one
10403 -- is not the body of the other, then this is an error, unless
10404 -- one of them is implicitly declared.
10405
10406 -- There are some cases when both can be implicit, for example
10407 -- when both a literal and a function that overrides it are
10408 -- inherited in a derivation, or when an inherited operation
10409 -- of a tagged full type overrides the inherited operation of
10410 -- a private extension. Ada 83 had a special rule for the
10411 -- literal case. In Ada 95, the later implicit operation hides
10412 -- the former, and the literal is always the former. In the
10413 -- odd case where both are derived operations declared at the
10414 -- same point, both operations should be declared, and in that
10415 -- case we bypass the following test and proceed to the next
10416 -- part. This can only occur for certain obscure cases in
10417 -- instances, when an operation on a type derived from a formal
10418 -- private type does not override a homograph inherited from
10419 -- the actual. In subsequent derivations of such a type, the
10420 -- DT positions of these operations remain distinct, if they
10421 -- have been set.
10422
10423 if Present (Alias (S))
10424 and then (No (Alias (E))
10425 or else Comes_From_Source (E)
10426 or else Is_Abstract_Subprogram (S)
10427 or else
10428 (Is_Dispatching_Operation (E)
10429 and then Is_Overriding_Alias (E, S)))
10430 and then Ekind (E) /= E_Enumeration_Literal
10431 then
10432 -- When an derived operation is overloaded it may be due to
10433 -- the fact that the full view of a private extension
10434 -- re-inherits. It has to be dealt with.
10435
10436 if Is_Package_Or_Generic_Package (Current_Scope)
10437 and then In_Private_Part (Current_Scope)
10438 then
10439 Check_Operation_From_Private_View (S, E);
10440 end if;
10441
10442 -- In any case the implicit operation remains hidden by the
10443 -- existing declaration, which is overriding. Indicate that
10444 -- E overrides the operation from which S is inherited.
10445
10446 if Present (Alias (S)) then
10447 Set_Overridden_Operation (E, Alias (S));
10448 else
10449 Set_Overridden_Operation (E, S);
10450 end if;
10451
10452 if Comes_From_Source (E) then
10453 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10454 end if;
10455
10456 return;
10457
10458 -- Within an instance, the renaming declarations for actual
10459 -- subprograms may become ambiguous, but they do not hide each
10460 -- other.
10461
10462 elsif Ekind (E) /= E_Entry
10463 and then not Comes_From_Source (E)
10464 and then not Is_Generic_Instance (E)
10465 and then (Present (Alias (E))
10466 or else Is_Intrinsic_Subprogram (E))
10467 and then (not In_Instance
10468 or else No (Parent (E))
10469 or else Nkind (Unit_Declaration_Node (E)) /=
10470 N_Subprogram_Renaming_Declaration)
10471 then
10472 -- A subprogram child unit is not allowed to override an
10473 -- inherited subprogram (10.1.1(20)).
10474
10475 if Is_Child_Unit (S) then
10476 Error_Msg_N
10477 ("child unit overrides inherited subprogram in parent",
10478 S);
10479 return;
10480 end if;
10481
10482 if Is_Non_Overriding_Operation (E, S) then
10483 Enter_Overloaded_Entity (S);
10484
10485 if No (Derived_Type)
10486 or else Is_Tagged_Type (Derived_Type)
10487 then
10488 Check_Dispatching_Operation (S, Empty);
10489 end if;
10490
10491 return;
10492 end if;
10493
10494 -- E is a derived operation or an internal operator which
10495 -- is being overridden. Remove E from further visibility.
10496 -- Furthermore, if E is a dispatching operation, it must be
10497 -- replaced in the list of primitive operations of its type
10498 -- (see Override_Dispatching_Operation).
10499
10500 Overridden_Subp := E;
10501
10502 declare
10503 Prev : Entity_Id;
10504
10505 begin
10506 Prev := First_Entity (Current_Scope);
10507 while Present (Prev) and then Next_Entity (Prev) /= E loop
10508 Next_Entity (Prev);
10509 end loop;
10510
10511 -- It is possible for E to be in the current scope and
10512 -- yet not in the entity chain. This can only occur in a
10513 -- generic context where E is an implicit concatenation
10514 -- in the formal part, because in a generic body the
10515 -- entity chain starts with the formals.
10516
10517 pragma Assert
10518 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10519
10520 -- E must be removed both from the entity_list of the
10521 -- current scope, and from the visibility chain
10522
10523 if Debug_Flag_E then
10524 Write_Str ("Override implicit operation ");
10525 Write_Int (Int (E));
10526 Write_Eol;
10527 end if;
10528
10529 -- If E is a predefined concatenation, it stands for four
10530 -- different operations. As a result, a single explicit
10531 -- declaration does not hide it. In a possible ambiguous
10532 -- situation, Disambiguate chooses the user-defined op,
10533 -- so it is correct to retain the previous internal one.
10534
10535 if Chars (E) /= Name_Op_Concat
10536 or else Ekind (E) /= E_Operator
10537 then
10538 -- For nondispatching derived operations that are
10539 -- overridden by a subprogram declared in the private
10540 -- part of a package, we retain the derived subprogram
10541 -- but mark it as not immediately visible. If the
10542 -- derived operation was declared in the visible part
10543 -- then this ensures that it will still be visible
10544 -- outside the package with the proper signature
10545 -- (calls from outside must also be directed to this
10546 -- version rather than the overriding one, unlike the
10547 -- dispatching case). Calls from inside the package
10548 -- will still resolve to the overriding subprogram
10549 -- since the derived one is marked as not visible
10550 -- within the package.
10551
10552 -- If the private operation is dispatching, we achieve
10553 -- the overriding by keeping the implicit operation
10554 -- but setting its alias to be the overriding one. In
10555 -- this fashion the proper body is executed in all
10556 -- cases, but the original signature is used outside
10557 -- of the package.
10558
10559 -- If the overriding is not in the private part, we
10560 -- remove the implicit operation altogether.
10561
10562 if Is_Private_Declaration (S) then
10563 if not Is_Dispatching_Operation (E) then
10564 Set_Is_Immediately_Visible (E, False);
10565 else
10566 -- Work done in Override_Dispatching_Operation,
10567 -- so nothing else needs to be done here.
10568
10569 null;
10570 end if;
10571
10572 else
10573 -- Find predecessor of E in Homonym chain
10574
10575 if E = Current_Entity (E) then
10576 Prev_Vis := Empty;
10577 else
10578 Prev_Vis := Current_Entity (E);
10579 while Homonym (Prev_Vis) /= E loop
10580 Prev_Vis := Homonym (Prev_Vis);
10581 end loop;
10582 end if;
10583
10584 if Prev_Vis /= Empty then
10585
10586 -- Skip E in the visibility chain
10587
10588 Set_Homonym (Prev_Vis, Homonym (E));
10589
10590 else
10591 Set_Name_Entity_Id (Chars (E), Homonym (E));
10592 end if;
10593
10594 Set_Next_Entity (Prev, Next_Entity (E));
10595
10596 if No (Next_Entity (Prev)) then
10597 Set_Last_Entity (Current_Scope, Prev);
10598 end if;
10599 end if;
10600 end if;
10601
10602 Enter_Overloaded_Entity (S);
10603
10604 -- For entities generated by Derive_Subprograms the
10605 -- overridden operation is the inherited primitive
10606 -- (which is available through the attribute alias).
10607
10608 if not (Comes_From_Source (E))
10609 and then Is_Dispatching_Operation (E)
10610 and then Find_Dispatching_Type (E) =
10611 Find_Dispatching_Type (S)
10612 and then Present (Alias (E))
10613 and then Comes_From_Source (Alias (E))
10614 then
10615 Set_Overridden_Operation (S, Alias (E));
10616
10617 -- Normal case of setting entity as overridden
10618
10619 -- Note: Static_Initialization and Overridden_Operation
10620 -- attributes use the same field in subprogram entities.
10621 -- Static_Initialization is only defined for internal
10622 -- initialization procedures, where Overridden_Operation
10623 -- is irrelevant. Therefore the setting of this attribute
10624 -- must check whether the target is an init_proc.
10625
10626 elsif not Is_Init_Proc (S) then
10627 Set_Overridden_Operation (S, E);
10628 end if;
10629
10630 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10631
10632 -- If S is a user-defined subprogram or a null procedure
10633 -- expanded to override an inherited null procedure, or a
10634 -- predefined dispatching primitive then indicate that E
10635 -- overrides the operation from which S is inherited.
10636
10637 if Comes_From_Source (S)
10638 or else
10639 (Present (Parent (S))
10640 and then
10641 Nkind (Parent (S)) = N_Procedure_Specification
10642 and then
10643 Null_Present (Parent (S)))
10644 or else
10645 (Present (Alias (E))
10646 and then
10647 Is_Predefined_Dispatching_Operation (Alias (E)))
10648 then
10649 if Present (Alias (E)) then
10650 Set_Overridden_Operation (S, Alias (E));
10651 end if;
10652 end if;
10653
10654 if Is_Dispatching_Operation (E) then
10655
10656 -- An overriding dispatching subprogram inherits the
10657 -- convention of the overridden subprogram (AI-117).
10658
10659 Set_Convention (S, Convention (E));
10660 Check_Dispatching_Operation (S, E);
10661
10662 else
10663 Check_Dispatching_Operation (S, Empty);
10664 end if;
10665
10666 Check_For_Primitive_Subprogram
10667 (Is_Primitive_Subp, Is_Overriding => True);
10668 goto Check_Inequality;
10669 end;
10670
10671 -- Apparent redeclarations in instances can occur when two
10672 -- formal types get the same actual type. The subprograms in
10673 -- in the instance are legal, even if not callable from the
10674 -- outside. Calls from within are disambiguated elsewhere.
10675 -- For dispatching operations in the visible part, the usual
10676 -- rules apply, and operations with the same profile are not
10677 -- legal (B830001).
10678
10679 elsif (In_Instance_Visible_Part
10680 and then not Is_Dispatching_Operation (E))
10681 or else In_Instance_Not_Visible
10682 then
10683 null;
10684
10685 -- Here we have a real error (identical profile)
10686
10687 else
10688 Error_Msg_Sloc := Sloc (E);
10689
10690 -- Avoid cascaded errors if the entity appears in
10691 -- subsequent calls.
10692
10693 Set_Scope (S, Current_Scope);
10694
10695 -- Generate error, with extra useful warning for the case
10696 -- of a generic instance with no completion.
10697
10698 if Is_Generic_Instance (S)
10699 and then not Has_Completion (E)
10700 then
10701 Error_Msg_N
10702 ("instantiation cannot provide body for&", S);
10703 Error_Msg_N ("\& conflicts with declaration#", S);
10704 else
10705 Error_Msg_N ("& conflicts with declaration#", S);
10706 end if;
10707
10708 return;
10709 end if;
10710
10711 else
10712 -- If one subprogram has an access parameter and the other
10713 -- a parameter of an access type, calls to either might be
10714 -- ambiguous. Verify that parameters match except for the
10715 -- access parameter.
10716
10717 if May_Hide_Profile then
10718 declare
10719 F1 : Entity_Id;
10720 F2 : Entity_Id;
10721
10722 begin
10723 F1 := First_Formal (S);
10724 F2 := First_Formal (E);
10725 while Present (F1) and then Present (F2) loop
10726 if Is_Access_Type (Etype (F1)) then
10727 if not Is_Access_Type (Etype (F2))
10728 or else not Conforming_Types
10729 (Designated_Type (Etype (F1)),
10730 Designated_Type (Etype (F2)),
10731 Type_Conformant)
10732 then
10733 May_Hide_Profile := False;
10734 end if;
10735
10736 elsif
10737 not Conforming_Types
10738 (Etype (F1), Etype (F2), Type_Conformant)
10739 then
10740 May_Hide_Profile := False;
10741 end if;
10742
10743 Next_Formal (F1);
10744 Next_Formal (F2);
10745 end loop;
10746
10747 if May_Hide_Profile
10748 and then No (F1)
10749 and then No (F2)
10750 then
10751 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10752 end if;
10753 end;
10754 end if;
10755 end if;
10756
10757 E := Homonym (E);
10758 end loop;
10759
10760 -- On exit, we know that S is a new entity
10761
10762 Enter_Overloaded_Entity (S);
10763 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10764 Check_Overriding_Indicator
10765 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10766
10767 -- Overloading is not allowed in SPARK, except for operators
10768
10769 if Nkind (S) /= N_Defining_Operator_Symbol then
10770 Error_Msg_Sloc := Sloc (Homonym (S));
10771 Check_SPARK_Restriction
10772 ("overloading not allowed with entity#", S);
10773 end if;
10774
10775 -- If S is a derived operation for an untagged type then by
10776 -- definition it's not a dispatching operation (even if the parent
10777 -- operation was dispatching), so Check_Dispatching_Operation is not
10778 -- called in that case.
10779
10780 if No (Derived_Type)
10781 or else Is_Tagged_Type (Derived_Type)
10782 then
10783 Check_Dispatching_Operation (S, Empty);
10784 end if;
10785 end if;
10786
10787 -- If this is a user-defined equality operator that is not a derived
10788 -- subprogram, create the corresponding inequality. If the operation is
10789 -- dispatching, the expansion is done elsewhere, and we do not create
10790 -- an explicit inequality operation.
10791
10792 <<Check_Inequality>>
10793 if Chars (S) = Name_Op_Eq
10794 and then Etype (S) = Standard_Boolean
10795 and then Present (Parent (S))
10796 and then not Is_Dispatching_Operation (S)
10797 then
10798 Make_Inequality_Operator (S);
10799
10800 if Ada_Version >= Ada_2012 then
10801 Check_Untagged_Equality (S);
10802 end if;
10803 end if;
10804 end New_Overloaded_Entity;
10805
10806 ---------------------
10807 -- Process_Formals --
10808 ---------------------
10809
10810 procedure Process_Formals
10811 (T : List_Id;
10812 Related_Nod : Node_Id)
10813 is
10814 Param_Spec : Node_Id;
10815 Formal : Entity_Id;
10816 Formal_Type : Entity_Id;
10817 Default : Node_Id;
10818 Ptype : Entity_Id;
10819
10820 Num_Out_Params : Nat := 0;
10821 First_Out_Param : Entity_Id := Empty;
10822 -- Used for setting Is_Only_Out_Parameter
10823
10824 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10825 -- Determine whether an access type designates a type coming from a
10826 -- limited view.
10827
10828 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10829 -- Check whether the default has a class-wide type. After analysis the
10830 -- default has the type of the formal, so we must also check explicitly
10831 -- for an access attribute.
10832
10833 ----------------------------------
10834 -- Designates_From_Limited_With --
10835 ----------------------------------
10836
10837 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10838 Desig : Entity_Id := Typ;
10839
10840 begin
10841 if Is_Access_Type (Desig) then
10842 Desig := Directly_Designated_Type (Desig);
10843 end if;
10844
10845 if Is_Class_Wide_Type (Desig) then
10846 Desig := Root_Type (Desig);
10847 end if;
10848
10849 return
10850 Ekind (Desig) = E_Incomplete_Type
10851 and then From_Limited_With (Desig);
10852 end Designates_From_Limited_With;
10853
10854 ---------------------------
10855 -- Is_Class_Wide_Default --
10856 ---------------------------
10857
10858 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10859 begin
10860 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10861 or else (Nkind (D) = N_Attribute_Reference
10862 and then Attribute_Name (D) = Name_Access
10863 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10864 end Is_Class_Wide_Default;
10865
10866 -- Start of processing for Process_Formals
10867
10868 begin
10869 -- In order to prevent premature use of the formals in the same formal
10870 -- part, the Ekind is left undefined until all default expressions are
10871 -- analyzed. The Ekind is established in a separate loop at the end.
10872
10873 Param_Spec := First (T);
10874 while Present (Param_Spec) loop
10875 Formal := Defining_Identifier (Param_Spec);
10876 Set_Never_Set_In_Source (Formal, True);
10877 Enter_Name (Formal);
10878
10879 -- Case of ordinary parameters
10880
10881 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10882 Find_Type (Parameter_Type (Param_Spec));
10883 Ptype := Parameter_Type (Param_Spec);
10884
10885 if Ptype = Error then
10886 goto Continue;
10887 end if;
10888
10889 Formal_Type := Entity (Ptype);
10890
10891 if Is_Incomplete_Type (Formal_Type)
10892 or else
10893 (Is_Class_Wide_Type (Formal_Type)
10894 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10895 then
10896 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10897 -- primitive operations, as long as their completion is
10898 -- in the same declarative part. If in the private part
10899 -- this means that the type cannot be a Taft-amendment type.
10900 -- Check is done on package exit. For access to subprograms,
10901 -- the use is legal for Taft-amendment types.
10902
10903 -- Ada 2012: tagged incomplete types are allowed as generic
10904 -- formal types. They do not introduce dependencies and the
10905 -- corresponding generic subprogram does not have a delayed
10906 -- freeze, because it does not need a freeze node.
10907
10908 if Is_Tagged_Type (Formal_Type) then
10909 if Ekind (Scope (Current_Scope)) = E_Package
10910 and then not From_Limited_With (Formal_Type)
10911 and then not Is_Generic_Type (Formal_Type)
10912 and then not Is_Class_Wide_Type (Formal_Type)
10913 then
10914 if not Nkind_In
10915 (Parent (T), N_Access_Function_Definition,
10916 N_Access_Procedure_Definition)
10917 then
10918 Append_Elmt
10919 (Current_Scope,
10920 Private_Dependents (Base_Type (Formal_Type)));
10921
10922 -- Freezing is delayed to ensure that Register_Prim
10923 -- will get called for this operation, which is needed
10924 -- in cases where static dispatch tables aren't built.
10925 -- (Note that the same is done for controlling access
10926 -- parameter cases in function Access_Definition.)
10927
10928 Set_Has_Delayed_Freeze (Current_Scope);
10929 end if;
10930 end if;
10931
10932 -- Special handling of Value_Type for CIL case
10933
10934 elsif Is_Value_Type (Formal_Type) then
10935 null;
10936
10937 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10938 N_Access_Procedure_Definition)
10939 then
10940 -- AI05-0151: Tagged incomplete types are allowed in all
10941 -- formal parts. Untagged incomplete types are not allowed
10942 -- in bodies.
10943
10944 if Ada_Version >= Ada_2012 then
10945 if Is_Tagged_Type (Formal_Type) then
10946 null;
10947
10948 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10949 N_Entry_Body,
10950 N_Subprogram_Body)
10951 then
10952 Error_Msg_NE
10953 ("invalid use of untagged incomplete type&",
10954 Ptype, Formal_Type);
10955 end if;
10956
10957 else
10958 Error_Msg_NE
10959 ("invalid use of incomplete type&",
10960 Param_Spec, Formal_Type);
10961
10962 -- Further checks on the legality of incomplete types
10963 -- in formal parts are delayed until the freeze point
10964 -- of the enclosing subprogram or access to subprogram.
10965 end if;
10966 end if;
10967
10968 elsif Ekind (Formal_Type) = E_Void then
10969 Error_Msg_NE
10970 ("premature use of&",
10971 Parameter_Type (Param_Spec), Formal_Type);
10972 end if;
10973
10974 -- Ada 2012 (AI-142): Handle aliased parameters
10975
10976 if Ada_Version >= Ada_2012
10977 and then Aliased_Present (Param_Spec)
10978 then
10979 Set_Is_Aliased (Formal);
10980 end if;
10981
10982 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10983 -- declaration corresponding to the null-excluding type of the
10984 -- formal in the enclosing scope. Finally, replace the parameter
10985 -- type of the formal with the internal subtype.
10986
10987 if Ada_Version >= Ada_2005
10988 and then Null_Exclusion_Present (Param_Spec)
10989 then
10990 if not Is_Access_Type (Formal_Type) then
10991 Error_Msg_N
10992 ("`NOT NULL` allowed only for an access type", Param_Spec);
10993
10994 else
10995 if Can_Never_Be_Null (Formal_Type)
10996 and then Comes_From_Source (Related_Nod)
10997 then
10998 Error_Msg_NE
10999 ("`NOT NULL` not allowed (& already excludes null)",
11000 Param_Spec, Formal_Type);
11001 end if;
11002
11003 Formal_Type :=
11004 Create_Null_Excluding_Itype
11005 (T => Formal_Type,
11006 Related_Nod => Related_Nod,
11007 Scope_Id => Scope (Current_Scope));
11008
11009 -- If the designated type of the itype is an itype that is
11010 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11011 -- on the access subtype, to prevent order-of-elaboration
11012 -- issues in the backend.
11013
11014 -- Example:
11015 -- type T is access procedure;
11016 -- procedure Op (O : not null T);
11017
11018 if Is_Itype (Directly_Designated_Type (Formal_Type))
11019 and then
11020 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11021 then
11022 Set_Has_Delayed_Freeze (Formal_Type);
11023 end if;
11024 end if;
11025 end if;
11026
11027 -- An access formal type
11028
11029 else
11030 Formal_Type :=
11031 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11032
11033 -- No need to continue if we already notified errors
11034
11035 if not Present (Formal_Type) then
11036 return;
11037 end if;
11038
11039 -- Ada 2005 (AI-254)
11040
11041 declare
11042 AD : constant Node_Id :=
11043 Access_To_Subprogram_Definition
11044 (Parameter_Type (Param_Spec));
11045 begin
11046 if Present (AD) and then Protected_Present (AD) then
11047 Formal_Type :=
11048 Replace_Anonymous_Access_To_Protected_Subprogram
11049 (Param_Spec);
11050 end if;
11051 end;
11052 end if;
11053
11054 Set_Etype (Formal, Formal_Type);
11055
11056 -- Deal with default expression if present
11057
11058 Default := Expression (Param_Spec);
11059
11060 if Present (Default) then
11061 Check_SPARK_Restriction
11062 ("default expression is not allowed", Default);
11063
11064 if Out_Present (Param_Spec) then
11065 Error_Msg_N
11066 ("default initialization only allowed for IN parameters",
11067 Param_Spec);
11068 end if;
11069
11070 -- Do the special preanalysis of the expression (see section on
11071 -- "Handling of Default Expressions" in the spec of package Sem).
11072
11073 Preanalyze_Spec_Expression (Default, Formal_Type);
11074
11075 -- An access to constant cannot be the default for
11076 -- an access parameter that is an access to variable.
11077
11078 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11079 and then not Is_Access_Constant (Formal_Type)
11080 and then Is_Access_Type (Etype (Default))
11081 and then Is_Access_Constant (Etype (Default))
11082 then
11083 Error_Msg_N
11084 ("formal that is access to variable cannot be initialized " &
11085 "with an access-to-constant expression", Default);
11086 end if;
11087
11088 -- Check that the designated type of an access parameter's default
11089 -- is not a class-wide type unless the parameter's designated type
11090 -- is also class-wide.
11091
11092 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11093 and then not Designates_From_Limited_With (Formal_Type)
11094 and then Is_Class_Wide_Default (Default)
11095 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11096 then
11097 Error_Msg_N
11098 ("access to class-wide expression not allowed here", Default);
11099 end if;
11100
11101 -- Check incorrect use of dynamically tagged expressions
11102
11103 if Is_Tagged_Type (Formal_Type) then
11104 Check_Dynamically_Tagged_Expression
11105 (Expr => Default,
11106 Typ => Formal_Type,
11107 Related_Nod => Default);
11108 end if;
11109 end if;
11110
11111 -- Ada 2005 (AI-231): Static checks
11112
11113 if Ada_Version >= Ada_2005
11114 and then Is_Access_Type (Etype (Formal))
11115 and then Can_Never_Be_Null (Etype (Formal))
11116 then
11117 Null_Exclusion_Static_Checks (Param_Spec);
11118 end if;
11119
11120 -- A function cannot have a volatile formal parameter. The following
11121 -- check is relevant when SPARK_Mode is on as it is not a standard
11122 -- Ada legality rule.
11123
11124 if SPARK_Mode = On
11125 and then Is_Volatile_Object (Formal)
11126 and then Ekind_In (Scope (Formal), E_Function, E_Generic_Function)
11127 then
11128 Error_Msg_N
11129 ("function cannot have a volatile formal parameter (SPARK RM "
11130 & "7.1.3(6))", Formal);
11131 end if;
11132
11133 <<Continue>>
11134 Next (Param_Spec);
11135 end loop;
11136
11137 -- If this is the formal part of a function specification, analyze the
11138 -- subtype mark in the context where the formals are visible but not
11139 -- yet usable, and may hide outer homographs.
11140
11141 if Nkind (Related_Nod) = N_Function_Specification then
11142 Analyze_Return_Type (Related_Nod);
11143 end if;
11144
11145 -- Now set the kind (mode) of each formal
11146
11147 Param_Spec := First (T);
11148 while Present (Param_Spec) loop
11149 Formal := Defining_Identifier (Param_Spec);
11150 Set_Formal_Mode (Formal);
11151
11152 if Ekind (Formal) = E_In_Parameter then
11153 Set_Default_Value (Formal, Expression (Param_Spec));
11154
11155 if Present (Expression (Param_Spec)) then
11156 Default := Expression (Param_Spec);
11157
11158 if Is_Scalar_Type (Etype (Default)) then
11159 if Nkind (Parameter_Type (Param_Spec)) /=
11160 N_Access_Definition
11161 then
11162 Formal_Type := Entity (Parameter_Type (Param_Spec));
11163 else
11164 Formal_Type :=
11165 Access_Definition
11166 (Related_Nod, Parameter_Type (Param_Spec));
11167 end if;
11168
11169 Apply_Scalar_Range_Check (Default, Formal_Type);
11170 end if;
11171 end if;
11172
11173 elsif Ekind (Formal) = E_Out_Parameter then
11174 Num_Out_Params := Num_Out_Params + 1;
11175
11176 if Num_Out_Params = 1 then
11177 First_Out_Param := Formal;
11178 end if;
11179
11180 elsif Ekind (Formal) = E_In_Out_Parameter then
11181 Num_Out_Params := Num_Out_Params + 1;
11182 end if;
11183
11184 -- Skip remaining processing if formal type was in error
11185
11186 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11187 goto Next_Parameter;
11188 end if;
11189
11190 -- Force call by reference if aliased
11191
11192 if Is_Aliased (Formal) then
11193 Set_Mechanism (Formal, By_Reference);
11194
11195 -- Warn if user asked this to be passed by copy
11196
11197 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11198 Error_Msg_N
11199 ("cannot pass aliased parameter & by copy?", Formal);
11200 end if;
11201
11202 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11203
11204 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11205 Set_Mechanism (Formal, By_Copy);
11206
11207 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11208 Set_Mechanism (Formal, By_Reference);
11209 end if;
11210
11211 <<Next_Parameter>>
11212 Next (Param_Spec);
11213 end loop;
11214
11215 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11216 Set_Is_Only_Out_Parameter (First_Out_Param);
11217 end if;
11218 end Process_Formals;
11219
11220 ----------------------------
11221 -- Reference_Body_Formals --
11222 ----------------------------
11223
11224 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11225 Fs : Entity_Id;
11226 Fb : Entity_Id;
11227
11228 begin
11229 if Error_Posted (Spec) then
11230 return;
11231 end if;
11232
11233 -- Iterate over both lists. They may be of different lengths if the two
11234 -- specs are not conformant.
11235
11236 Fs := First_Formal (Spec);
11237 Fb := First_Formal (Bod);
11238 while Present (Fs) and then Present (Fb) loop
11239 Generate_Reference (Fs, Fb, 'b');
11240
11241 if Style_Check then
11242 Style.Check_Identifier (Fb, Fs);
11243 end if;
11244
11245 Set_Spec_Entity (Fb, Fs);
11246 Set_Referenced (Fs, False);
11247 Next_Formal (Fs);
11248 Next_Formal (Fb);
11249 end loop;
11250 end Reference_Body_Formals;
11251
11252 -------------------------
11253 -- Set_Actual_Subtypes --
11254 -------------------------
11255
11256 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11257 Decl : Node_Id;
11258 Formal : Entity_Id;
11259 T : Entity_Id;
11260 First_Stmt : Node_Id := Empty;
11261 AS_Needed : Boolean;
11262
11263 begin
11264 -- If this is an empty initialization procedure, no need to create
11265 -- actual subtypes (small optimization).
11266
11267 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11268 return;
11269 end if;
11270
11271 Formal := First_Formal (Subp);
11272 while Present (Formal) loop
11273 T := Etype (Formal);
11274
11275 -- We never need an actual subtype for a constrained formal
11276
11277 if Is_Constrained (T) then
11278 AS_Needed := False;
11279
11280 -- If we have unknown discriminants, then we do not need an actual
11281 -- subtype, or more accurately we cannot figure it out! Note that
11282 -- all class-wide types have unknown discriminants.
11283
11284 elsif Has_Unknown_Discriminants (T) then
11285 AS_Needed := False;
11286
11287 -- At this stage we have an unconstrained type that may need an
11288 -- actual subtype. For sure the actual subtype is needed if we have
11289 -- an unconstrained array type.
11290
11291 elsif Is_Array_Type (T) then
11292 AS_Needed := True;
11293
11294 -- The only other case needing an actual subtype is an unconstrained
11295 -- record type which is an IN parameter (we cannot generate actual
11296 -- subtypes for the OUT or IN OUT case, since an assignment can
11297 -- change the discriminant values. However we exclude the case of
11298 -- initialization procedures, since discriminants are handled very
11299 -- specially in this context, see the section entitled "Handling of
11300 -- Discriminants" in Einfo.
11301
11302 -- We also exclude the case of Discrim_SO_Functions (functions used
11303 -- in front end layout mode for size/offset values), since in such
11304 -- functions only discriminants are referenced, and not only are such
11305 -- subtypes not needed, but they cannot always be generated, because
11306 -- of order of elaboration issues.
11307
11308 elsif Is_Record_Type (T)
11309 and then Ekind (Formal) = E_In_Parameter
11310 and then Chars (Formal) /= Name_uInit
11311 and then not Is_Unchecked_Union (T)
11312 and then not Is_Discrim_SO_Function (Subp)
11313 then
11314 AS_Needed := True;
11315
11316 -- All other cases do not need an actual subtype
11317
11318 else
11319 AS_Needed := False;
11320 end if;
11321
11322 -- Generate actual subtypes for unconstrained arrays and
11323 -- unconstrained discriminated records.
11324
11325 if AS_Needed then
11326 if Nkind (N) = N_Accept_Statement then
11327
11328 -- If expansion is active, the formal is replaced by a local
11329 -- variable that renames the corresponding entry of the
11330 -- parameter block, and it is this local variable that may
11331 -- require an actual subtype.
11332
11333 if Expander_Active then
11334 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11335 else
11336 Decl := Build_Actual_Subtype (T, Formal);
11337 end if;
11338
11339 if Present (Handled_Statement_Sequence (N)) then
11340 First_Stmt :=
11341 First (Statements (Handled_Statement_Sequence (N)));
11342 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11343 Mark_Rewrite_Insertion (Decl);
11344 else
11345 -- If the accept statement has no body, there will be no
11346 -- reference to the actuals, so no need to compute actual
11347 -- subtypes.
11348
11349 return;
11350 end if;
11351
11352 else
11353 Decl := Build_Actual_Subtype (T, Formal);
11354 Prepend (Decl, Declarations (N));
11355 Mark_Rewrite_Insertion (Decl);
11356 end if;
11357
11358 -- The declaration uses the bounds of an existing object, and
11359 -- therefore needs no constraint checks.
11360
11361 Analyze (Decl, Suppress => All_Checks);
11362
11363 -- We need to freeze manually the generated type when it is
11364 -- inserted anywhere else than in a declarative part.
11365
11366 if Present (First_Stmt) then
11367 Insert_List_Before_And_Analyze (First_Stmt,
11368 Freeze_Entity (Defining_Identifier (Decl), N));
11369 end if;
11370
11371 if Nkind (N) = N_Accept_Statement
11372 and then Expander_Active
11373 then
11374 Set_Actual_Subtype (Renamed_Object (Formal),
11375 Defining_Identifier (Decl));
11376 else
11377 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11378 end if;
11379 end if;
11380
11381 Next_Formal (Formal);
11382 end loop;
11383 end Set_Actual_Subtypes;
11384
11385 ---------------------
11386 -- Set_Formal_Mode --
11387 ---------------------
11388
11389 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11390 Spec : constant Node_Id := Parent (Formal_Id);
11391
11392 begin
11393 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11394 -- since we ensure that corresponding actuals are always valid at the
11395 -- point of the call.
11396
11397 if Out_Present (Spec) then
11398 if Ekind (Scope (Formal_Id)) = E_Function
11399 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11400 then
11401 -- [IN] OUT parameters allowed for functions in Ada 2012
11402
11403 if Ada_Version >= Ada_2012 then
11404
11405 -- Even in Ada 2012 operators can only have IN parameters
11406
11407 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11408 Error_Msg_N ("operators can only have IN parameters", Spec);
11409 end if;
11410
11411 if In_Present (Spec) then
11412 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11413 else
11414 Set_Ekind (Formal_Id, E_Out_Parameter);
11415 end if;
11416
11417 -- But not in earlier versions of Ada
11418
11419 else
11420 Error_Msg_N ("functions can only have IN parameters", Spec);
11421 Set_Ekind (Formal_Id, E_In_Parameter);
11422 end if;
11423
11424 elsif In_Present (Spec) then
11425 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11426
11427 else
11428 Set_Ekind (Formal_Id, E_Out_Parameter);
11429 Set_Never_Set_In_Source (Formal_Id, True);
11430 Set_Is_True_Constant (Formal_Id, False);
11431 Set_Current_Value (Formal_Id, Empty);
11432 end if;
11433
11434 else
11435 Set_Ekind (Formal_Id, E_In_Parameter);
11436 end if;
11437
11438 -- Set Is_Known_Non_Null for access parameters since the language
11439 -- guarantees that access parameters are always non-null. We also set
11440 -- Can_Never_Be_Null, since there is no way to change the value.
11441
11442 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11443
11444 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11445 -- null; In Ada 2005, only if then null_exclusion is explicit.
11446
11447 if Ada_Version < Ada_2005
11448 or else Can_Never_Be_Null (Etype (Formal_Id))
11449 then
11450 Set_Is_Known_Non_Null (Formal_Id);
11451 Set_Can_Never_Be_Null (Formal_Id);
11452 end if;
11453
11454 -- Ada 2005 (AI-231): Null-exclusion access subtype
11455
11456 elsif Is_Access_Type (Etype (Formal_Id))
11457 and then Can_Never_Be_Null (Etype (Formal_Id))
11458 then
11459 Set_Is_Known_Non_Null (Formal_Id);
11460
11461 -- We can also set Can_Never_Be_Null (thus preventing some junk
11462 -- access checks) for the case of an IN parameter, which cannot
11463 -- be changed, or for an IN OUT parameter, which can be changed but
11464 -- not to a null value. But for an OUT parameter, the initial value
11465 -- passed in can be null, so we can't set this flag in that case.
11466
11467 if Ekind (Formal_Id) /= E_Out_Parameter then
11468 Set_Can_Never_Be_Null (Formal_Id);
11469 end if;
11470 end if;
11471
11472 Set_Mechanism (Formal_Id, Default_Mechanism);
11473 Set_Formal_Validity (Formal_Id);
11474 end Set_Formal_Mode;
11475
11476 -------------------------
11477 -- Set_Formal_Validity --
11478 -------------------------
11479
11480 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11481 begin
11482 -- If no validity checking, then we cannot assume anything about the
11483 -- validity of parameters, since we do not know there is any checking
11484 -- of the validity on the call side.
11485
11486 if not Validity_Checks_On then
11487 return;
11488
11489 -- If validity checking for parameters is enabled, this means we are
11490 -- not supposed to make any assumptions about argument values.
11491
11492 elsif Validity_Check_Parameters then
11493 return;
11494
11495 -- If we are checking in parameters, we will assume that the caller is
11496 -- also checking parameters, so we can assume the parameter is valid.
11497
11498 elsif Ekind (Formal_Id) = E_In_Parameter
11499 and then Validity_Check_In_Params
11500 then
11501 Set_Is_Known_Valid (Formal_Id, True);
11502
11503 -- Similar treatment for IN OUT parameters
11504
11505 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11506 and then Validity_Check_In_Out_Params
11507 then
11508 Set_Is_Known_Valid (Formal_Id, True);
11509 end if;
11510 end Set_Formal_Validity;
11511
11512 ------------------------
11513 -- Subtype_Conformant --
11514 ------------------------
11515
11516 function Subtype_Conformant
11517 (New_Id : Entity_Id;
11518 Old_Id : Entity_Id;
11519 Skip_Controlling_Formals : Boolean := False) return Boolean
11520 is
11521 Result : Boolean;
11522 begin
11523 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11524 Skip_Controlling_Formals => Skip_Controlling_Formals);
11525 return Result;
11526 end Subtype_Conformant;
11527
11528 ---------------------
11529 -- Type_Conformant --
11530 ---------------------
11531
11532 function Type_Conformant
11533 (New_Id : Entity_Id;
11534 Old_Id : Entity_Id;
11535 Skip_Controlling_Formals : Boolean := False) return Boolean
11536 is
11537 Result : Boolean;
11538 begin
11539 May_Hide_Profile := False;
11540
11541 Check_Conformance
11542 (New_Id, Old_Id, Type_Conformant, False, Result,
11543 Skip_Controlling_Formals => Skip_Controlling_Formals);
11544 return Result;
11545 end Type_Conformant;
11546
11547 -------------------------------
11548 -- Valid_Operator_Definition --
11549 -------------------------------
11550
11551 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11552 N : Integer := 0;
11553 F : Entity_Id;
11554 Id : constant Name_Id := Chars (Designator);
11555 N_OK : Boolean;
11556
11557 begin
11558 F := First_Formal (Designator);
11559 while Present (F) loop
11560 N := N + 1;
11561
11562 if Present (Default_Value (F)) then
11563 Error_Msg_N
11564 ("default values not allowed for operator parameters",
11565 Parent (F));
11566 end if;
11567
11568 Next_Formal (F);
11569 end loop;
11570
11571 -- Verify that user-defined operators have proper number of arguments
11572 -- First case of operators which can only be unary
11573
11574 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11575 N_OK := (N = 1);
11576
11577 -- Case of operators which can be unary or binary
11578
11579 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11580 N_OK := (N in 1 .. 2);
11581
11582 -- All other operators can only be binary
11583
11584 else
11585 N_OK := (N = 2);
11586 end if;
11587
11588 if not N_OK then
11589 Error_Msg_N
11590 ("incorrect number of arguments for operator", Designator);
11591 end if;
11592
11593 if Id = Name_Op_Ne
11594 and then Base_Type (Etype (Designator)) = Standard_Boolean
11595 and then not Is_Intrinsic_Subprogram (Designator)
11596 then
11597 Error_Msg_N
11598 ("explicit definition of inequality not allowed", Designator);
11599 end if;
11600 end Valid_Operator_Definition;
11601
11602 end Sem_Ch6;