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