1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
51 with Output; use Output;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
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;
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
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
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
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
154 Ctype : Conformance_Type;
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
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
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
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;
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
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))))
258 ("abstract subprogram is not dispatching or overriding?r?", N);
261 Generate_Reference_To_Formals (Designator);
262 Check_Eliminated (Designator);
264 if Has_Aspects (N) then
265 Analyze_Aspect_Specifications (N, Designator);
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);
282 -- If the expression is a completion, Prev is the entity whose
283 -- declaration is completed. Def_Id is needed to analyze the spec.
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
311 Formal_Spec : Node_Id;
315 Formal_Spec := First (Parameter_Specifications (New_Spec));
317 -- Create a new formal parameter at the same source position
319 while Present (Formal_Spec) loop
320 Def := Defining_Identifier (Formal_Spec);
321 Set_Defining_Identifier (Formal_Spec,
322 Make_Defining_Identifier (Sloc (Def),
323 Chars => Chars (Def)));
329 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
331 -- If there are previous overloadable entities with the same name,
332 -- check whether any of them is completed by the expression function.
334 if Present (Prev) and then Is_Overloadable (Prev) then
335 Def_Id := Analyze_Subprogram_Specification (Spec);
336 Prev := Find_Corresponding_Spec (N);
339 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
342 Make_Subprogram_Body (Loc,
343 Specification => New_Spec,
344 Declarations => Empty_List,
345 Handled_Statement_Sequence =>
346 Make_Handled_Sequence_Of_Statements (LocX,
347 Statements => New_List (Ret)));
349 -- If the expression completes a generic subprogram, we must create a
350 -- separate node for the body, because at instantiation the original
351 -- node of the generic copy must be a generic subprogram body, and
352 -- cannot be a expression function. Otherwise we just rewrite the
353 -- expression with the non-generic body.
355 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
356 Insert_After (N, New_Body);
358 -- Propagate any aspects or pragmas that apply to the expression
359 -- function to the proper body when the expression function acts
362 if Has_Aspects (N) then
363 Move_Aspects (N, To => New_Body);
366 Relocate_Pragmas_To_Body (New_Body);
368 Rewrite (N, Make_Null_Statement (Loc));
369 Set_Has_Completion (Prev, False);
372 Set_Is_Inlined (Prev);
374 elsif Present (Prev) and then Comes_From_Source (Prev) then
375 Set_Has_Completion (Prev, False);
377 -- An expression function that is a completion freezes the
378 -- expression. This means freezing the return type, and if it is
379 -- an access type, freezing its designated type as well.
381 -- Note that we cannot defer this freezing to the analysis of the
382 -- expression itself, because a freeze node might appear in a nested
383 -- scope, leading to an elaboration order issue in gigi.
385 Freeze_Before (N, Etype (Prev));
387 if Is_Access_Type (Etype (Prev)) then
388 Freeze_Before (N, Designated_Type (Etype (Prev)));
391 -- For navigation purposes, indicate that the function is a body
393 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
394 Rewrite (N, New_Body);
396 -- Correct the parent pointer of the aspect specification list to
397 -- reference the rewritten node.
399 if Has_Aspects (N) then
400 Set_Parent (Aspect_Specifications (N), N);
403 -- Propagate any pragmas that apply to the expression function to the
404 -- proper body when the expression function acts as a completion.
405 -- Aspects are automatically transfered because of node rewriting.
407 Relocate_Pragmas_To_Body (N);
410 -- Prev is the previous entity with the same name, but it is can
411 -- be an unrelated spec that is not completed by the expression
412 -- function. In that case the relevant entity is the one in the body.
413 -- Not clear that the backend can inline it in this case ???
415 if Has_Completion (Prev) then
416 Set_Is_Inlined (Prev);
418 -- The formals of the expression function are body formals,
419 -- and do not appear in the ali file, which will only contain
420 -- references to the formals of the original subprogram spec.
427 F1 := First_Formal (Def_Id);
428 F2 := First_Formal (Prev);
430 while Present (F1) loop
431 Set_Spec_Entity (F1, F2);
438 Set_Is_Inlined (Defining_Entity (New_Body));
441 -- If this is not a completion, create both a declaration and a body, so
442 -- that the expression can be inlined whenever possible.
445 -- An expression function that is not a completion is not a
446 -- subprogram declaration, and thus cannot appear in a protected
449 if Nkind (Parent (N)) = N_Protected_Definition then
451 ("an expression function is not a legal protected operation", N);
455 Make_Subprogram_Declaration (Loc, Specification => Spec);
457 Rewrite (N, New_Decl);
459 -- Correct the parent pointer of the aspect specification list to
460 -- reference the rewritten node.
462 if Has_Aspects (N) then
463 Set_Parent (Aspect_Specifications (N), N);
467 Set_Is_Inlined (Defining_Entity (New_Decl));
469 -- To prevent premature freeze action, insert the new body at the end
470 -- of the current declarations, or at the end of the package spec.
471 -- However, resolve usage names now, to prevent spurious visibility
472 -- on later entities. Note that the function can now be called in
473 -- the current declarative part, which will appear to be prior to
474 -- the presence of the body in the code. There are nevertheless no
475 -- order of elaboration issues because all name resolution has taken
476 -- place at the point of declaration.
479 Decls : List_Id := List_Containing (N);
480 Par : constant Node_Id := Parent (Decls);
481 Id : constant Entity_Id := Defining_Entity (New_Decl);
484 if Nkind (Par) = N_Package_Specification
485 and then Decls = Visible_Declarations (Par)
486 and then Present (Private_Declarations (Par))
487 and then not Is_Empty_List (Private_Declarations (Par))
489 Decls := Private_Declarations (Par);
492 Insert_After (Last (Decls), New_Body);
494 Install_Formals (Id);
496 -- Preanalyze the expression for name capture, except in an
497 -- instance, where this has been done during generic analysis,
498 -- and will be redone when analyzing the body.
501 Expr : constant Node_Id := Expression (Ret);
504 Set_Parent (Expr, Ret);
506 if not In_Instance then
507 Preanalyze_Spec_Expression (Expr, Etype (Id));
515 -- If the return expression is a static constant, we suppress warning
516 -- messages on unused formals, which in most cases will be noise.
518 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
519 Is_OK_Static_Expression (Expr));
520 end Analyze_Expression_Function;
522 ----------------------------------------
523 -- Analyze_Extended_Return_Statement --
524 ----------------------------------------
526 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
528 Check_Compiler_Unit ("extended return statement", N);
529 Analyze_Return_Statement (N);
530 end Analyze_Extended_Return_Statement;
532 ----------------------------
533 -- Analyze_Function_Call --
534 ----------------------------
536 procedure Analyze_Function_Call (N : Node_Id) is
537 Actuals : constant List_Id := Parameter_Associations (N);
538 Func_Nam : constant Node_Id := Name (N);
544 -- A call of the form A.B (X) may be an Ada 2005 call, which is
545 -- rewritten as B (A, X). If the rewriting is successful, the call
546 -- has been analyzed and we just return.
548 if Nkind (Func_Nam) = N_Selected_Component
549 and then Name (N) /= Func_Nam
550 and then Is_Rewrite_Substitution (N)
551 and then Present (Etype (N))
556 -- If error analyzing name, then set Any_Type as result type and return
558 if Etype (Func_Nam) = Any_Type then
559 Set_Etype (N, Any_Type);
563 -- Otherwise analyze the parameters
565 if Present (Actuals) then
566 Actual := First (Actuals);
567 while Present (Actual) loop
569 Check_Parameterless_Call (Actual);
575 end Analyze_Function_Call;
577 -----------------------------
578 -- Analyze_Function_Return --
579 -----------------------------
581 procedure Analyze_Function_Return (N : Node_Id) is
582 Loc : constant Source_Ptr := Sloc (N);
583 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
584 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
586 R_Type : constant Entity_Id := Etype (Scope_Id);
587 -- Function result subtype
589 procedure Check_Limited_Return (Expr : Node_Id);
590 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
591 -- limited types. Used only for simple return statements.
592 -- Expr is the expression returned.
594 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
595 -- Check that the return_subtype_indication properly matches the result
596 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
598 --------------------------
599 -- Check_Limited_Return --
600 --------------------------
602 procedure Check_Limited_Return (Expr : Node_Id) is
604 -- Ada 2005 (AI-318-02): Return-by-reference types have been
605 -- removed and replaced by anonymous access results. This is an
606 -- incompatibility with Ada 95. Not clear whether this should be
607 -- enforced yet or perhaps controllable with special switch. ???
609 -- A limited interface that is not immutably limited is OK.
611 if Is_Limited_Interface (R_Type)
613 not (Is_Task_Interface (R_Type)
614 or else Is_Protected_Interface (R_Type)
615 or else Is_Synchronized_Interface (R_Type))
619 elsif Is_Limited_Type (R_Type)
620 and then not Is_Interface (R_Type)
621 and then Comes_From_Source (N)
622 and then not In_Instance_Body
623 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
627 if Ada_Version >= Ada_2005
628 and then not Debug_Flag_Dot_L
629 and then not GNAT_Mode
632 ("(Ada 2005) cannot copy object of a limited type " &
633 "(RM-2005 6.5(5.5/2))", Expr);
635 if Is_Limited_View (R_Type) then
637 ("\return by reference not permitted in Ada 2005", Expr);
640 -- Warn in Ada 95 mode, to give folks a heads up about this
643 -- In GNAT mode, this is just a warning, to allow it to be
644 -- evilly turned off. Otherwise it is a real error.
646 -- In a generic context, simplify the warning because it makes
647 -- no sense to discuss pass-by-reference or copy.
649 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
650 if Inside_A_Generic then
652 ("return of limited object not permitted in Ada 2005 "
653 & "(RM-2005 6.5(5.5/2))?y?", Expr);
655 elsif Is_Limited_View (R_Type) then
657 ("return by reference not permitted in Ada 2005 "
658 & "(RM-2005 6.5(5.5/2))?y?", Expr);
661 ("cannot copy object of a limited type in Ada 2005 "
662 & "(RM-2005 6.5(5.5/2))?y?", Expr);
665 -- Ada 95 mode, compatibility warnings disabled
668 return; -- skip continuation messages below
671 if not Inside_A_Generic then
673 ("\consider switching to return of access type", Expr);
674 Explain_Limited_Type (R_Type, Expr);
677 end Check_Limited_Return;
679 -------------------------------------
680 -- Check_Return_Subtype_Indication --
681 -------------------------------------
683 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
684 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
686 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
687 -- Subtype given in the extended return statement (must match R_Type)
689 Subtype_Ind : constant Node_Id :=
690 Object_Definition (Original_Node (Obj_Decl));
692 R_Type_Is_Anon_Access : constant Boolean :=
694 E_Anonymous_Access_Subprogram_Type,
695 E_Anonymous_Access_Protected_Subprogram_Type,
696 E_Anonymous_Access_Type);
697 -- True if return type of the function is an anonymous access type
698 -- Can't we make Is_Anonymous_Access_Type in einfo ???
700 R_Stm_Type_Is_Anon_Access : constant Boolean :=
701 Ekind_In (R_Stm_Type,
702 E_Anonymous_Access_Subprogram_Type,
703 E_Anonymous_Access_Protected_Subprogram_Type,
704 E_Anonymous_Access_Type);
705 -- True if type of the return object is an anonymous access type
707 procedure Error_No_Match (N : Node_Id);
708 -- Output error messages for case where types do not statically
709 -- match. N is the location for the messages.
715 procedure Error_No_Match (N : Node_Id) is
718 ("subtype must statically match function result subtype", N);
720 if not Predicates_Match (R_Stm_Type, R_Type) then
721 Error_Msg_Node_2 := R_Type;
723 ("\predicate of & does not match predicate of &",
728 -- Start of processing for Check_Return_Subtype_Indication
731 -- First, avoid cascaded errors
733 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
737 -- "return access T" case; check that the return statement also has
738 -- "access T", and that the subtypes statically match:
739 -- if this is an access to subprogram the signatures must match.
741 if R_Type_Is_Anon_Access then
742 if R_Stm_Type_Is_Anon_Access then
744 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
746 if Base_Type (Designated_Type (R_Stm_Type)) /=
747 Base_Type (Designated_Type (R_Type))
748 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
750 Error_No_Match (Subtype_Mark (Subtype_Ind));
754 -- For two anonymous access to subprogram types, the
755 -- types themselves must be type conformant.
757 if not Conforming_Types
758 (R_Stm_Type, R_Type, Fully_Conformant)
760 Error_No_Match (Subtype_Ind);
765 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
768 -- If the return object is of an anonymous access type, then report
769 -- an error if the function's result type is not also anonymous.
771 elsif R_Stm_Type_Is_Anon_Access
772 and then not R_Type_Is_Anon_Access
774 Error_Msg_N ("anonymous access not allowed for function with " &
775 "named access result", Subtype_Ind);
777 -- Subtype indication case: check that the return object's type is
778 -- covered by the result type, and that the subtypes statically match
779 -- when the result subtype is constrained. Also handle record types
780 -- with unknown discriminants for which we have built the underlying
781 -- record view. Coverage is needed to allow specific-type return
782 -- objects when the result type is class-wide (see AI05-32).
784 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
785 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
789 Underlying_Record_View (Base_Type (R_Stm_Type))))
791 -- A null exclusion may be present on the return type, on the
792 -- function specification, on the object declaration or on the
795 if Is_Access_Type (R_Type)
797 (Can_Never_Be_Null (R_Type)
798 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
799 Can_Never_Be_Null (R_Stm_Type)
801 Error_No_Match (Subtype_Ind);
804 -- AI05-103: for elementary types, subtypes must statically match
806 if Is_Constrained (R_Type)
807 or else Is_Access_Type (R_Type)
809 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
810 Error_No_Match (Subtype_Ind);
814 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
815 and then Is_Null_Extension (Base_Type (R_Type))
821 ("wrong type for return_subtype_indication", Subtype_Ind);
823 end Check_Return_Subtype_Indication;
825 ---------------------
826 -- Local Variables --
827 ---------------------
831 -- Start of processing for Analyze_Function_Return
834 Set_Return_Present (Scope_Id);
836 if Nkind (N) = N_Simple_Return_Statement then
837 Expr := Expression (N);
839 -- Guard against a malformed expression. The parser may have tried to
840 -- recover but the node is not analyzable.
842 if Nkind (Expr) = N_Error then
843 Set_Etype (Expr, Any_Type);
844 Expander_Mode_Save_And_Set (False);
848 -- The resolution of a controlled [extension] aggregate associated
849 -- with a return statement creates a temporary which needs to be
850 -- finalized on function exit. Wrap the return statement inside a
851 -- block so that the finalization machinery can detect this case.
852 -- This early expansion is done only when the return statement is
853 -- not part of a handled sequence of statements.
855 if Nkind_In (Expr, N_Aggregate,
856 N_Extension_Aggregate)
857 and then Needs_Finalization (R_Type)
858 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
861 Make_Block_Statement (Loc,
862 Handled_Statement_Sequence =>
863 Make_Handled_Sequence_Of_Statements (Loc,
864 Statements => New_List (Relocate_Node (N)))));
870 Analyze_And_Resolve (Expr, R_Type);
871 Check_Limited_Return (Expr);
874 -- RETURN only allowed in SPARK as the last statement in function
876 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
878 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
879 or else Present (Next (N)))
881 Check_SPARK_Restriction
882 ("RETURN should be the last statement in function", N);
886 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
888 -- Analyze parts specific to extended_return_statement:
891 Obj_Decl : constant Node_Id :=
892 Last (Return_Object_Declarations (N));
893 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
894 HSS : constant Node_Id := Handled_Statement_Sequence (N);
897 Expr := Expression (Obj_Decl);
899 -- Note: The check for OK_For_Limited_Init will happen in
900 -- Analyze_Object_Declaration; we treat it as a normal
901 -- object declaration.
903 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
906 Check_Return_Subtype_Indication (Obj_Decl);
908 if Present (HSS) then
911 if Present (Exception_Handlers (HSS)) then
913 -- ???Has_Nested_Block_With_Handler needs to be set.
914 -- Probably by creating an actual N_Block_Statement.
915 -- Probably in Expand.
921 -- Mark the return object as referenced, since the return is an
922 -- implicit reference of the object.
924 Set_Referenced (Defining_Identifier (Obj_Decl));
926 Check_References (Stm_Entity);
928 -- Check RM 6.5 (5.9/3)
931 if Ada_Version < Ada_2012 then
933 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
934 -- Can it really happen (extended return???)
937 ("aliased only allowed for limited return objects "
938 & "in Ada 2012??", N);
940 elsif not Is_Limited_View (R_Type) then
941 Error_Msg_N ("aliased only allowed for limited"
942 & " return objects", N);
948 -- Case of Expr present
952 -- Defend against previous errors
954 and then Nkind (Expr) /= N_Empty
955 and then Present (Etype (Expr))
957 -- Apply constraint check. Note that this is done before the implicit
958 -- conversion of the expression done for anonymous access types to
959 -- ensure correct generation of the null-excluding check associated
960 -- with null-excluding expressions found in return statements.
962 Apply_Constraint_Check (Expr, R_Type);
964 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
965 -- type, apply an implicit conversion of the expression to that type
966 -- to force appropriate static and run-time accessibility checks.
968 if Ada_Version >= Ada_2005
969 and then Ekind (R_Type) = E_Anonymous_Access_Type
971 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
972 Analyze_And_Resolve (Expr, R_Type);
974 -- If this is a local anonymous access to subprogram, the
975 -- accessibility check can be applied statically. The return is
976 -- illegal if the access type of the return expression is declared
977 -- inside of the subprogram (except if it is the subtype indication
978 -- of an extended return statement).
980 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
981 if not Comes_From_Source (Current_Scope)
982 or else Ekind (Current_Scope) = E_Return_Statement
987 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
989 Error_Msg_N ("cannot return local access to subprogram", N);
993 -- If the result type is class-wide, then check that the return
994 -- expression's type is not declared at a deeper level than the
995 -- function (RM05-6.5(5.6/2)).
997 if Ada_Version >= Ada_2005
998 and then Is_Class_Wide_Type (R_Type)
1000 if Type_Access_Level (Etype (Expr)) >
1001 Subprogram_Access_Level (Scope_Id)
1004 ("level of return expression type is deeper than " &
1005 "class-wide function!", Expr);
1009 -- Check incorrect use of dynamically tagged expression
1011 if Is_Tagged_Type (R_Type) then
1012 Check_Dynamically_Tagged_Expression
1018 -- ??? A real run-time accessibility check is needed in cases
1019 -- involving dereferences of access parameters. For now we just
1020 -- check the static cases.
1022 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1023 and then Is_Limited_View (Etype (Scope_Id))
1024 and then Object_Access_Level (Expr) >
1025 Subprogram_Access_Level (Scope_Id)
1027 -- Suppress the message in a generic, where the rewriting
1030 if Inside_A_Generic then
1035 Make_Raise_Program_Error (Loc,
1036 Reason => PE_Accessibility_Check_Failed));
1039 Error_Msg_Warn := SPARK_Mode /= On;
1040 Error_Msg_N ("cannot return a local value by reference<<", N);
1041 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1045 if Known_Null (Expr)
1046 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1047 and then Null_Exclusion_Present (Parent (Scope_Id))
1049 Apply_Compile_Time_Constraint_Error
1051 Msg => "(Ada 2005) null not allowed for "
1052 & "null-excluding return??",
1053 Reason => CE_Null_Not_Allowed);
1056 end Analyze_Function_Return;
1058 -------------------------------------
1059 -- Analyze_Generic_Subprogram_Body --
1060 -------------------------------------
1062 procedure Analyze_Generic_Subprogram_Body
1066 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1067 Kind : constant Entity_Kind := Ekind (Gen_Id);
1068 Body_Id : Entity_Id;
1073 -- Copy body and disable expansion while analyzing the generic For a
1074 -- stub, do not copy the stub (which would load the proper body), this
1075 -- will be done when the proper body is analyzed.
1077 if Nkind (N) /= N_Subprogram_Body_Stub then
1078 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1083 Spec := Specification (N);
1085 -- Within the body of the generic, the subprogram is callable, and
1086 -- behaves like the corresponding non-generic unit.
1088 Body_Id := Defining_Entity (Spec);
1090 if Kind = E_Generic_Procedure
1091 and then Nkind (Spec) /= N_Procedure_Specification
1093 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1096 elsif Kind = E_Generic_Function
1097 and then Nkind (Spec) /= N_Function_Specification
1099 Error_Msg_N ("invalid body for generic function ", Body_Id);
1103 Set_Corresponding_Body (Gen_Decl, Body_Id);
1105 if Has_Completion (Gen_Id)
1106 and then Nkind (Parent (N)) /= N_Subunit
1108 Error_Msg_N ("duplicate generic body", N);
1111 Set_Has_Completion (Gen_Id);
1114 if Nkind (N) = N_Subprogram_Body_Stub then
1115 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1117 Set_Corresponding_Spec (N, Gen_Id);
1120 if Nkind (Parent (N)) = N_Compilation_Unit then
1121 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1124 -- Make generic parameters immediately visible in the body. They are
1125 -- needed to process the formals declarations. Then make the formals
1126 -- visible in a separate step.
1128 Push_Scope (Gen_Id);
1132 First_Ent : Entity_Id;
1135 First_Ent := First_Entity (Gen_Id);
1138 while Present (E) and then not Is_Formal (E) loop
1143 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1145 -- Now generic formals are visible, and the specification can be
1146 -- analyzed, for subsequent conformance check.
1148 Body_Id := Analyze_Subprogram_Specification (Spec);
1150 -- Make formal parameters visible
1154 -- E is the first formal parameter, we loop through the formals
1155 -- installing them so that they will be visible.
1157 Set_First_Entity (Gen_Id, E);
1158 while Present (E) loop
1164 -- Visible generic entity is callable within its own body
1166 Set_Ekind (Gen_Id, Ekind (Body_Id));
1167 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1168 Set_Ekind (Body_Id, E_Subprogram_Body);
1169 Set_Convention (Body_Id, Convention (Gen_Id));
1170 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1171 Set_Scope (Body_Id, Scope (Gen_Id));
1172 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1174 if Nkind (N) = N_Subprogram_Body_Stub then
1176 -- No body to analyze, so restore state of generic unit
1178 Set_Ekind (Gen_Id, Kind);
1179 Set_Ekind (Body_Id, Kind);
1181 if Present (First_Ent) then
1182 Set_First_Entity (Gen_Id, First_Ent);
1189 -- If this is a compilation unit, it must be made visible explicitly,
1190 -- because the compilation of the declaration, unlike other library
1191 -- unit declarations, does not. If it is not a unit, the following
1192 -- is redundant but harmless.
1194 Set_Is_Immediately_Visible (Gen_Id);
1195 Reference_Body_Formals (Gen_Id, Body_Id);
1197 if Is_Child_Unit (Gen_Id) then
1198 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1201 Set_Actual_Subtypes (N, Current_Scope);
1203 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1204 -- invariants and predicates associated with the body and its spec.
1205 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1206 -- prepares the contract assertions for generic subprograms or for
1207 -- ASIS. Do not generate contract checks in SPARK mode.
1209 if not GNATprove_Mode then
1210 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1213 -- If the generic unit carries pre- or post-conditions, copy them
1214 -- to the original generic tree, so that they are properly added
1215 -- to any instantiation.
1218 Orig : constant Node_Id := Original_Node (N);
1222 Cond := First (Declarations (N));
1223 while Present (Cond) loop
1224 if Nkind (Cond) = N_Pragma
1225 and then Pragma_Name (Cond) = Name_Check
1227 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1229 elsif Nkind (Cond) = N_Pragma
1230 and then Pragma_Name (Cond) = Name_Postcondition
1232 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1233 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1242 Check_SPARK_Mode_In_Generic (N);
1244 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1245 Set_SPARK_Pragma_Inherited (Body_Id, True);
1247 Analyze_Declarations (Declarations (N));
1249 Analyze (Handled_Statement_Sequence (N));
1251 Save_Global_References (Original_Node (N));
1253 -- Prior to exiting the scope, include generic formals again (if any
1254 -- are present) in the set of local entities.
1256 if Present (First_Ent) then
1257 Set_First_Entity (Gen_Id, First_Ent);
1260 Check_References (Gen_Id);
1263 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1265 Check_Subprogram_Order (N);
1267 -- Outside of its body, unit is generic again
1269 Set_Ekind (Gen_Id, Kind);
1270 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1273 Style.Check_Identifier (Body_Id, Gen_Id);
1277 end Analyze_Generic_Subprogram_Body;
1279 ----------------------------
1280 -- Analyze_Null_Procedure --
1281 ----------------------------
1283 procedure Analyze_Null_Procedure
1285 Is_Completion : out Boolean)
1287 Loc : constant Source_Ptr := Sloc (N);
1288 Spec : constant Node_Id := Specification (N);
1289 Designator : Entity_Id;
1291 Null_Body : Node_Id := Empty;
1295 -- Capture the profile of the null procedure before analysis, for
1296 -- expansion at the freeze point and at each point of call. The body is
1297 -- used if the procedure has preconditions, or if it is a completion. In
1298 -- the first case the body is analyzed at the freeze point, in the other
1299 -- it replaces the null procedure declaration.
1302 Make_Subprogram_Body (Loc,
1303 Specification => New_Copy_Tree (Spec),
1304 Declarations => New_List,
1305 Handled_Statement_Sequence =>
1306 Make_Handled_Sequence_Of_Statements (Loc,
1307 Statements => New_List (Make_Null_Statement (Loc))));
1309 -- Create new entities for body and formals
1311 Set_Defining_Unit_Name (Specification (Null_Body),
1312 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1314 Form := First (Parameter_Specifications (Specification (Null_Body)));
1315 while Present (Form) loop
1316 Set_Defining_Identifier (Form,
1317 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1321 -- Determine whether the null procedure may be a completion of a generic
1322 -- suprogram, in which case we use the new null body as the completion
1323 -- and set minimal semantic information on the original declaration,
1324 -- which is rewritten as a null statement.
1326 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1328 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1329 Insert_Before (N, Null_Body);
1330 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1331 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1333 Rewrite (N, Make_Null_Statement (Loc));
1334 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1335 Is_Completion := True;
1339 -- Resolve the types of the formals now, because the freeze point
1340 -- may appear in a different context, e.g. an instantiation.
1342 Form := First (Parameter_Specifications (Specification (Null_Body)));
1343 while Present (Form) loop
1344 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1345 Find_Type (Parameter_Type (Form));
1348 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1350 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1353 -- The case of a null procedure with a formal that is an
1354 -- access_to_subprogram type, and that is used as an actual
1355 -- in an instantiation is left to the enthusiastic reader.
1364 -- If there are previous overloadable entities with the same name,
1365 -- check whether any of them is completed by the null procedure.
1367 if Present (Prev) and then Is_Overloadable (Prev) then
1368 Designator := Analyze_Subprogram_Specification (Spec);
1369 Prev := Find_Corresponding_Spec (N);
1372 if No (Prev) or else not Comes_From_Source (Prev) then
1373 Designator := Analyze_Subprogram_Specification (Spec);
1374 Set_Has_Completion (Designator);
1376 -- Signal to caller that this is a procedure declaration
1378 Is_Completion := False;
1380 -- Null procedures are always inlined, but generic formal subprograms
1381 -- which appear as such in the internal instance of formal packages,
1382 -- need no completion and are not marked Inline.
1385 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1387 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1388 Set_Body_To_Inline (N, Null_Body);
1389 Set_Is_Inlined (Designator);
1393 -- The null procedure is a completion
1395 Is_Completion := True;
1397 if Expander_Active then
1398 Rewrite (N, Null_Body);
1402 Designator := Analyze_Subprogram_Specification (Spec);
1403 Set_Has_Completion (Designator);
1404 Set_Has_Completion (Prev);
1407 end Analyze_Null_Procedure;
1409 -----------------------------
1410 -- Analyze_Operator_Symbol --
1411 -----------------------------
1413 -- An operator symbol such as "+" or "and" may appear in context where the
1414 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1415 -- is just a string, as in (conjunction = "or"). In these cases the parser
1416 -- generates this node, and the semantics does the disambiguation. Other
1417 -- such case are actuals in an instantiation, the generic unit in an
1418 -- instantiation, and pragma arguments.
1420 procedure Analyze_Operator_Symbol (N : Node_Id) is
1421 Par : constant Node_Id := Parent (N);
1424 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1425 or else Nkind (Par) = N_Function_Instantiation
1426 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1427 or else (Nkind (Par) = N_Pragma_Argument_Association
1428 and then not Is_Pragma_String_Literal (Par))
1429 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1430 or else (Nkind (Par) = N_Attribute_Reference
1431 and then Attribute_Name (Par) /= Name_Value)
1433 Find_Direct_Name (N);
1436 Change_Operator_Symbol_To_String_Literal (N);
1439 end Analyze_Operator_Symbol;
1441 -----------------------------------
1442 -- Analyze_Parameter_Association --
1443 -----------------------------------
1445 procedure Analyze_Parameter_Association (N : Node_Id) is
1447 Analyze (Explicit_Actual_Parameter (N));
1448 end Analyze_Parameter_Association;
1450 ----------------------------
1451 -- Analyze_Procedure_Call --
1452 ----------------------------
1454 procedure Analyze_Procedure_Call (N : Node_Id) is
1455 Loc : constant Source_Ptr := Sloc (N);
1456 P : constant Node_Id := Name (N);
1457 Actuals : constant List_Id := Parameter_Associations (N);
1461 procedure Analyze_Call_And_Resolve;
1462 -- Do Analyze and Resolve calls for procedure call
1463 -- At end, check illegal order dependence.
1465 ------------------------------
1466 -- Analyze_Call_And_Resolve --
1467 ------------------------------
1469 procedure Analyze_Call_And_Resolve is
1471 if Nkind (N) = N_Procedure_Call_Statement then
1473 Resolve (N, Standard_Void_Type);
1477 end Analyze_Call_And_Resolve;
1479 -- Start of processing for Analyze_Procedure_Call
1482 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1483 -- a procedure call or an entry call. The prefix may denote an access
1484 -- to subprogram type, in which case an implicit dereference applies.
1485 -- If the prefix is an indexed component (without implicit dereference)
1486 -- then the construct denotes a call to a member of an entire family.
1487 -- If the prefix is a simple name, it may still denote a call to a
1488 -- parameterless member of an entry family. Resolution of these various
1489 -- interpretations is delicate.
1493 -- If this is a call of the form Obj.Op, the call may have been
1494 -- analyzed and possibly rewritten into a block, in which case
1497 if Analyzed (N) then
1501 -- If there is an error analyzing the name (which may have been
1502 -- rewritten if the original call was in prefix notation) then error
1503 -- has been emitted already, mark node and return.
1505 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1506 Set_Etype (N, Any_Type);
1510 -- Otherwise analyze the parameters
1512 if Present (Actuals) then
1513 Actual := First (Actuals);
1515 while Present (Actual) loop
1517 Check_Parameterless_Call (Actual);
1522 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1524 if Nkind (P) = N_Attribute_Reference
1525 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1527 Name_Elab_Subp_Body)
1529 if Present (Actuals) then
1531 ("no parameters allowed for this call", First (Actuals));
1535 Set_Etype (N, Standard_Void_Type);
1538 elsif Is_Entity_Name (P)
1539 and then Is_Record_Type (Etype (Entity (P)))
1540 and then Remote_AST_I_Dereference (P)
1544 elsif Is_Entity_Name (P)
1545 and then Ekind (Entity (P)) /= E_Entry_Family
1547 if Is_Access_Type (Etype (P))
1548 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1549 and then No (Actuals)
1550 and then Comes_From_Source (N)
1552 Error_Msg_N ("missing explicit dereference in call", N);
1555 Analyze_Call_And_Resolve;
1557 -- If the prefix is the simple name of an entry family, this is
1558 -- a parameterless call from within the task body itself.
1560 elsif Is_Entity_Name (P)
1561 and then Nkind (P) = N_Identifier
1562 and then Ekind (Entity (P)) = E_Entry_Family
1563 and then Present (Actuals)
1564 and then No (Next (First (Actuals)))
1566 -- Can be call to parameterless entry family. What appears to be the
1567 -- sole argument is in fact the entry index. Rewrite prefix of node
1568 -- accordingly. Source representation is unchanged by this
1572 Make_Indexed_Component (Loc,
1574 Make_Selected_Component (Loc,
1575 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1576 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1577 Expressions => Actuals);
1578 Set_Name (N, New_N);
1579 Set_Etype (New_N, Standard_Void_Type);
1580 Set_Parameter_Associations (N, No_List);
1581 Analyze_Call_And_Resolve;
1583 elsif Nkind (P) = N_Explicit_Dereference then
1584 if Ekind (Etype (P)) = E_Subprogram_Type then
1585 Analyze_Call_And_Resolve;
1587 Error_Msg_N ("expect access to procedure in call", P);
1590 -- The name can be a selected component or an indexed component that
1591 -- yields an access to subprogram. Such a prefix is legal if the call
1592 -- has parameter associations.
1594 elsif Is_Access_Type (Etype (P))
1595 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1597 if Present (Actuals) then
1598 Analyze_Call_And_Resolve;
1600 Error_Msg_N ("missing explicit dereference in call ", N);
1603 -- If not an access to subprogram, then the prefix must resolve to the
1604 -- name of an entry, entry family, or protected operation.
1606 -- For the case of a simple entry call, P is a selected component where
1607 -- the prefix is the task and the selector name is the entry. A call to
1608 -- a protected procedure will have the same syntax. If the protected
1609 -- object contains overloaded operations, the entity may appear as a
1610 -- function, the context will select the operation whose type is Void.
1612 elsif Nkind (P) = N_Selected_Component
1613 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1617 Analyze_Call_And_Resolve;
1619 elsif Nkind (P) = N_Selected_Component
1620 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1621 and then Present (Actuals)
1622 and then No (Next (First (Actuals)))
1624 -- Can be call to parameterless entry family. What appears to be the
1625 -- sole argument is in fact the entry index. Rewrite prefix of node
1626 -- accordingly. Source representation is unchanged by this
1630 Make_Indexed_Component (Loc,
1631 Prefix => New_Copy (P),
1632 Expressions => Actuals);
1633 Set_Name (N, New_N);
1634 Set_Etype (New_N, Standard_Void_Type);
1635 Set_Parameter_Associations (N, No_List);
1636 Analyze_Call_And_Resolve;
1638 -- For the case of a reference to an element of an entry family, P is
1639 -- an indexed component whose prefix is a selected component (task and
1640 -- entry family), and whose index is the entry family index.
1642 elsif Nkind (P) = N_Indexed_Component
1643 and then Nkind (Prefix (P)) = N_Selected_Component
1644 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1646 Analyze_Call_And_Resolve;
1648 -- If the prefix is the name of an entry family, it is a call from
1649 -- within the task body itself.
1651 elsif Nkind (P) = N_Indexed_Component
1652 and then Nkind (Prefix (P)) = N_Identifier
1653 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1656 Make_Selected_Component (Loc,
1657 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1658 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1659 Rewrite (Prefix (P), New_N);
1661 Analyze_Call_And_Resolve;
1663 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1664 -- procedure name, so the construct can only be a qualified expression.
1666 elsif Nkind (P) = N_Qualified_Expression
1667 and then Ada_Version >= Ada_2012
1669 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1672 -- Anything else is an error
1675 Error_Msg_N ("invalid procedure or entry call", N);
1677 end Analyze_Procedure_Call;
1679 ------------------------------
1680 -- Analyze_Return_Statement --
1681 ------------------------------
1683 procedure Analyze_Return_Statement (N : Node_Id) is
1685 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1686 N_Extended_Return_Statement));
1688 Returns_Object : constant Boolean :=
1689 Nkind (N) = N_Extended_Return_Statement
1691 (Nkind (N) = N_Simple_Return_Statement
1692 and then Present (Expression (N)));
1693 -- True if we're returning something; that is, "return <expression>;"
1694 -- or "return Result : T [:= ...]". False for "return;". Used for error
1695 -- checking: If Returns_Object is True, N should apply to a function
1696 -- body; otherwise N should apply to a procedure body, entry body,
1697 -- accept statement, or extended return statement.
1699 function Find_What_It_Applies_To return Entity_Id;
1700 -- Find the entity representing the innermost enclosing body, accept
1701 -- statement, or extended return statement. If the result is a callable
1702 -- construct or extended return statement, then this will be the value
1703 -- of the Return_Applies_To attribute. Otherwise, the program is
1704 -- illegal. See RM-6.5(4/2).
1706 -----------------------------
1707 -- Find_What_It_Applies_To --
1708 -----------------------------
1710 function Find_What_It_Applies_To return Entity_Id is
1711 Result : Entity_Id := Empty;
1714 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1715 -- and postconditions.
1717 for J in reverse 0 .. Scope_Stack.Last loop
1718 Result := Scope_Stack.Table (J).Entity;
1719 exit when not Ekind_In (Result, E_Block, E_Loop)
1720 and then Chars (Result) /= Name_uPostconditions;
1723 pragma Assert (Present (Result));
1725 end Find_What_It_Applies_To;
1727 -- Local declarations
1729 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1730 Kind : constant Entity_Kind := Ekind (Scope_Id);
1731 Loc : constant Source_Ptr := Sloc (N);
1732 Stm_Entity : constant Entity_Id :=
1734 (E_Return_Statement, Current_Scope, Loc, 'R');
1736 -- Start of processing for Analyze_Return_Statement
1739 Set_Return_Statement_Entity (N, Stm_Entity);
1741 Set_Etype (Stm_Entity, Standard_Void_Type);
1742 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1744 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1745 -- (4/2): an inner return statement will apply to this extended return.
1747 if Nkind (N) = N_Extended_Return_Statement then
1748 Push_Scope (Stm_Entity);
1751 -- Check that pragma No_Return is obeyed. Don't complain about the
1752 -- implicitly-generated return that is placed at the end.
1754 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1755 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1758 -- Warn on any unassigned OUT parameters if in procedure
1760 if Ekind (Scope_Id) = E_Procedure then
1761 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1764 -- Check that functions return objects, and other things do not
1766 if Kind = E_Function or else Kind = E_Generic_Function then
1767 if not Returns_Object then
1768 Error_Msg_N ("missing expression in return from function", N);
1771 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1772 if Returns_Object then
1773 Error_Msg_N ("procedure cannot return value (use function)", N);
1776 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1777 if Returns_Object then
1778 if Is_Protected_Type (Scope (Scope_Id)) then
1779 Error_Msg_N ("entry body cannot return value", N);
1781 Error_Msg_N ("accept statement cannot return value", N);
1785 elsif Kind = E_Return_Statement then
1787 -- We are nested within another return statement, which must be an
1788 -- extended_return_statement.
1790 if Returns_Object then
1791 if Nkind (N) = N_Extended_Return_Statement then
1793 ("extended return statement cannot be nested (use `RETURN;`)",
1796 -- Case of a simple return statement with a value inside extended
1797 -- return statement.
1801 ("return nested in extended return statement cannot return " &
1802 "value (use `RETURN;`)", N);
1807 Error_Msg_N ("illegal context for return statement", N);
1810 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1811 Analyze_Function_Return (N);
1813 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1814 Set_Return_Present (Scope_Id);
1817 if Nkind (N) = N_Extended_Return_Statement then
1821 Kill_Current_Values (Last_Assignment_Only => True);
1822 Check_Unreachable_Code (N);
1824 Analyze_Dimension (N);
1825 end Analyze_Return_Statement;
1827 -------------------------------------
1828 -- Analyze_Simple_Return_Statement --
1829 -------------------------------------
1831 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1833 if Present (Expression (N)) then
1834 Mark_Coextensions (N, Expression (N));
1837 Analyze_Return_Statement (N);
1838 end Analyze_Simple_Return_Statement;
1840 -------------------------
1841 -- Analyze_Return_Type --
1842 -------------------------
1844 procedure Analyze_Return_Type (N : Node_Id) is
1845 Designator : constant Entity_Id := Defining_Entity (N);
1846 Typ : Entity_Id := Empty;
1849 -- Normal case where result definition does not indicate an error
1851 if Result_Definition (N) /= Error then
1852 if Nkind (Result_Definition (N)) = N_Access_Definition then
1853 Check_SPARK_Restriction
1854 ("access result is not allowed", Result_Definition (N));
1856 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1859 AD : constant Node_Id :=
1860 Access_To_Subprogram_Definition (Result_Definition (N));
1862 if Present (AD) and then Protected_Present (AD) then
1863 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1865 Typ := Access_Definition (N, Result_Definition (N));
1869 Set_Parent (Typ, Result_Definition (N));
1870 Set_Is_Local_Anonymous_Access (Typ);
1871 Set_Etype (Designator, Typ);
1873 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1875 Null_Exclusion_Static_Checks (N);
1877 -- Subtype_Mark case
1880 Find_Type (Result_Definition (N));
1881 Typ := Entity (Result_Definition (N));
1882 Set_Etype (Designator, Typ);
1884 -- Unconstrained array as result is not allowed in SPARK
1886 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1887 Check_SPARK_Restriction
1888 ("returning an unconstrained array is not allowed",
1889 Result_Definition (N));
1892 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1894 Null_Exclusion_Static_Checks (N);
1896 -- If a null exclusion is imposed on the result type, then create
1897 -- a null-excluding itype (an access subtype) and use it as the
1898 -- function's Etype. Note that the null exclusion checks are done
1899 -- right before this, because they don't get applied to types that
1900 -- do not come from source.
1902 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1903 Set_Etype (Designator,
1904 Create_Null_Excluding_Itype
1907 Scope_Id => Scope (Current_Scope)));
1909 -- The new subtype must be elaborated before use because
1910 -- it is visible outside of the function. However its base
1911 -- type may not be frozen yet, so the reference that will
1912 -- force elaboration must be attached to the freezing of
1915 -- If the return specification appears on a proper body,
1916 -- the subtype will have been created already on the spec.
1918 if Is_Frozen (Typ) then
1919 if Nkind (Parent (N)) = N_Subprogram_Body
1920 and then Nkind (Parent (Parent (N))) = N_Subunit
1924 Build_Itype_Reference (Etype (Designator), Parent (N));
1928 Ensure_Freeze_Node (Typ);
1931 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1933 Set_Itype (IR, Etype (Designator));
1934 Append_Freeze_Actions (Typ, New_List (IR));
1939 Set_Etype (Designator, Typ);
1942 if Ekind (Typ) = E_Incomplete_Type
1943 and then Is_Value_Type (Typ)
1947 elsif Ekind (Typ) = E_Incomplete_Type
1948 or else (Is_Class_Wide_Type (Typ)
1949 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1951 -- AI05-0151: Tagged incomplete types are allowed in all formal
1952 -- parts. Untagged incomplete types are not allowed in bodies.
1954 if Ada_Version >= Ada_2012 then
1955 if Is_Tagged_Type (Typ) then
1958 elsif Nkind (Parent (N)) = N_Subprogram_Body
1959 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
1963 ("invalid use of untagged incomplete type&",
1967 -- The type must be completed in the current package. This
1968 -- is checked at the end of the package declaration when
1969 -- Taft-amendment types are identified. If the return type
1970 -- is class-wide, there is no required check, the type can
1971 -- be a bona fide TAT.
1973 if Ekind (Scope (Current_Scope)) = E_Package
1974 and then In_Private_Part (Scope (Current_Scope))
1975 and then not Is_Class_Wide_Type (Typ)
1977 Append_Elmt (Designator, Private_Dependents (Typ));
1982 ("invalid use of incomplete type&", Designator, Typ);
1987 -- Case where result definition does indicate an error
1990 Set_Etype (Designator, Any_Type);
1992 end Analyze_Return_Type;
1994 -----------------------------
1995 -- Analyze_Subprogram_Body --
1996 -----------------------------
1998 procedure Analyze_Subprogram_Body (N : Node_Id) is
1999 Loc : constant Source_Ptr := Sloc (N);
2000 Body_Spec : constant Node_Id := Specification (N);
2001 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2004 if Debug_Flag_C then
2005 Write_Str ("==> subprogram body ");
2006 Write_Name (Chars (Body_Id));
2007 Write_Str (" from ");
2008 Write_Location (Loc);
2013 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2015 -- The real work is split out into the helper, so it can do "return;"
2016 -- without skipping the debug output:
2018 Analyze_Subprogram_Body_Helper (N);
2020 if Debug_Flag_C then
2022 Write_Str ("<== subprogram body ");
2023 Write_Name (Chars (Body_Id));
2024 Write_Str (" from ");
2025 Write_Location (Loc);
2028 end Analyze_Subprogram_Body;
2030 --------------------------------------
2031 -- Analyze_Subprogram_Body_Contract --
2032 --------------------------------------
2034 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2035 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2036 Mode : SPARK_Mode_Type;
2038 Ref_Depends : Node_Id := Empty;
2039 Ref_Global : Node_Id := Empty;
2040 Spec_Id : Entity_Id;
2043 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2045 -- When a subprogram body declaration is illegal, its defining entity is
2046 -- left unanalyzed. There is nothing left to do in this case because the
2047 -- body lacks a contract, or even a proper Ekind.
2049 if Ekind (Body_Id) = E_Void then
2053 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2054 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2056 Spec_Id := Corresponding_Spec (Body_Decl);
2059 -- Locate and store pragmas Refined_Depends and Refined_Global since
2060 -- their order of analysis matters.
2062 Prag := Classifications (Contract (Body_Id));
2063 while Present (Prag) loop
2064 if Pragma_Name (Prag) = Name_Refined_Depends then
2065 Ref_Depends := Prag;
2066 elsif Pragma_Name (Prag) = Name_Refined_Global then
2070 Prag := Next_Pragma (Prag);
2073 -- Analyze Refined_Global first as Refined_Depends may mention items
2074 -- classified in the global refinement.
2076 if Present (Ref_Global) then
2077 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2079 -- When the corresponding Global aspect/pragma references a state with
2080 -- visible refinement, the body requires Refined_Global. Refinement is
2081 -- not required when SPARK checks are suppressed.
2083 elsif Present (Spec_Id) then
2084 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2086 if SPARK_Mode /= Off
2087 and then Present (Prag)
2088 and then Contains_Refined_State (Prag)
2091 ("body of subprogram & requires global refinement",
2092 Body_Decl, Spec_Id);
2096 -- Refined_Depends must be analyzed after Refined_Global in order to see
2097 -- the modes of all global refinements.
2099 if Present (Ref_Depends) then
2100 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2102 -- When the corresponding Depends aspect/pragma references a state with
2103 -- visible refinement, the body requires Refined_Depends. Refinement is
2104 -- not required when SPARK checks are suppressed.
2106 elsif Present (Spec_Id) then
2107 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2109 if SPARK_Mode /= Off
2110 and then Present (Prag)
2111 and then Contains_Refined_State (Prag)
2114 ("body of subprogram & requires dependance refinement",
2115 Body_Decl, Spec_Id);
2119 Restore_SPARK_Mode (Mode);
2120 end Analyze_Subprogram_Body_Contract;
2122 ------------------------------------
2123 -- Analyze_Subprogram_Body_Helper --
2124 ------------------------------------
2126 -- This procedure is called for regular subprogram bodies, generic bodies,
2127 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2128 -- specification matters, and is used to create a proper declaration for
2129 -- the subprogram, or to perform conformance checks.
2131 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2132 Loc : constant Source_Ptr := Sloc (N);
2133 Body_Spec : constant Node_Id := Specification (N);
2134 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2135 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2136 Conformant : Boolean;
2138 Prot_Typ : Entity_Id := Empty;
2139 Spec_Id : Entity_Id;
2140 Spec_Decl : Node_Id := Empty;
2142 Last_Real_Spec_Entity : Entity_Id := Empty;
2143 -- When we analyze a separate spec, the entity chain ends up containing
2144 -- the formals, as well as any itypes generated during analysis of the
2145 -- default expressions for parameters, or the arguments of associated
2146 -- precondition/postcondition pragmas (which are analyzed in the context
2147 -- of the spec since they have visibility on formals).
2149 -- These entities belong with the spec and not the body. However we do
2150 -- the analysis of the body in the context of the spec (again to obtain
2151 -- visibility to the formals), and all the entities generated during
2152 -- this analysis end up also chained to the entity chain of the spec.
2153 -- But they really belong to the body, and there is circuitry to move
2154 -- them from the spec to the body.
2156 -- However, when we do this move, we don't want to move the real spec
2157 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2158 -- variable points to the last real spec entity, so we only move those
2159 -- chained beyond that point. It is initialized to Empty to deal with
2160 -- the case where there is no separate spec.
2162 procedure Analyze_Aspects_On_Body_Or_Stub;
2163 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2164 -- assumed that N has aspects.
2166 procedure Check_Anonymous_Return;
2167 -- Ada 2005: if a function returns an access type that denotes a task,
2168 -- or a type that contains tasks, we must create a master entity for
2169 -- the anonymous type, which typically will be used in an allocator
2170 -- in the body of the function.
2172 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2173 -- Look ahead to recognize a pragma that may appear after the body.
2174 -- If there is a previous spec, check that it appears in the same
2175 -- declarative part. If the pragma is Inline_Always, perform inlining
2176 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2177 -- If the body acts as a spec, and inlining is required, we create a
2178 -- subprogram declaration for it, in order to attach the body to inline.
2179 -- If pragma does not appear after the body, check whether there is
2180 -- an inline pragma before any local declarations.
2182 procedure Check_Missing_Return;
2183 -- Checks for a function with a no return statements, and also performs
2184 -- the warning checks implemented by Check_Returns. In formal mode, also
2185 -- verify that a function ends with a RETURN and that a procedure does
2186 -- not contain any RETURN.
2188 function Disambiguate_Spec return Entity_Id;
2189 -- When a primitive is declared between the private view and the full
2190 -- view of a concurrent type which implements an interface, a special
2191 -- mechanism is used to find the corresponding spec of the primitive
2194 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2195 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2196 -- incomplete types coming from a limited context and swap their limited
2197 -- views with the non-limited ones.
2199 function Is_Private_Concurrent_Primitive
2200 (Subp_Id : Entity_Id) return Boolean;
2201 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2202 -- type that implements an interface and has a private view.
2204 procedure Set_Trivial_Subprogram (N : Node_Id);
2205 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2206 -- subprogram whose body is being analyzed. N is the statement node
2207 -- causing the flag to be set, if the following statement is a return
2208 -- of an entity, we mark the entity as set in source to suppress any
2209 -- warning on the stylized use of function stubs with a dummy return.
2211 procedure Verify_Overriding_Indicator;
2212 -- If there was a previous spec, the entity has been entered in the
2213 -- current scope previously. If the body itself carries an overriding
2214 -- indicator, check that it is consistent with the known status of the
2217 -------------------------------------
2218 -- Analyze_Aspects_On_Body_Or_Stub --
2219 -------------------------------------
2221 procedure Analyze_Aspects_On_Body_Or_Stub is
2222 procedure Diagnose_Misplaced_Aspects;
2223 -- Subprogram body [stub] N has aspects, but they are not properly
2224 -- placed. Provide precise diagnostics depending on the aspects
2227 --------------------------------
2228 -- Diagnose_Misplaced_Aspects --
2229 --------------------------------
2231 procedure Diagnose_Misplaced_Aspects is
2235 -- The current aspect along with its name and id
2237 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2238 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2239 -- the name of the refined version of the aspect.
2241 ------------------------
2242 -- SPARK_Aspect_Error --
2243 ------------------------
2245 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2247 -- The corresponding spec already contains the aspect in
2248 -- question and the one appearing on the body must be the
2251 -- procedure P with Global ...;
2252 -- procedure P with Global ... is ... end P;
2256 if Has_Aspect (Spec_Id, Asp_Id) then
2257 Error_Msg_Name_1 := Asp_Nam;
2259 -- Subunits cannot carry aspects that apply to a subprogram
2262 if Nkind (Parent (N)) = N_Subunit then
2263 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2266 Error_Msg_Name_2 := Ref_Nam;
2267 Error_Msg_N ("aspect % should be %", Asp);
2270 -- Otherwise the aspect must appear in the spec, not in the
2274 -- procedure P with Global ... is ... end P;
2278 ("aspect specification must appear in subprogram "
2279 & "declaration", Asp);
2281 end SPARK_Aspect_Error;
2283 -- Start of processing for Diagnose_Misplaced_Aspects
2286 -- Iterate over the aspect specifications and emit specific errors
2287 -- where applicable.
2289 Asp := First (Aspect_Specifications (N));
2290 while Present (Asp) loop
2291 Asp_Nam := Chars (Identifier (Asp));
2292 Asp_Id := Get_Aspect_Id (Asp_Nam);
2294 -- Do not emit errors on aspects that can appear on a
2295 -- subprogram body. This scenario occurs when the aspect
2296 -- specification list contains both misplaced and properly
2299 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2302 -- Special diagnostics for SPARK aspects
2304 elsif Asp_Nam = Name_Depends then
2305 SPARK_Aspect_Error (Name_Refined_Depends);
2307 elsif Asp_Nam = Name_Global then
2308 SPARK_Aspect_Error (Name_Refined_Global);
2310 elsif Asp_Nam = Name_Post then
2311 SPARK_Aspect_Error (Name_Refined_Post);
2315 ("aspect specification must appear in subprogram "
2316 & "declaration", Asp);
2321 end Diagnose_Misplaced_Aspects;
2323 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2326 -- Language-defined aspects cannot be associated with a subprogram
2327 -- body [stub] if the subprogram has a spec. Certain implementation
2328 -- defined aspects are allowed to break this rule (for list, see
2329 -- table Aspect_On_Body_Or_Stub_OK).
2331 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2332 Diagnose_Misplaced_Aspects;
2334 Analyze_Aspect_Specifications (N, Body_Id);
2336 end Analyze_Aspects_On_Body_Or_Stub;
2338 ----------------------------
2339 -- Check_Anonymous_Return --
2340 ----------------------------
2342 procedure Check_Anonymous_Return is
2348 if Present (Spec_Id) then
2354 if Ekind (Scop) = E_Function
2355 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2356 and then not Is_Thunk (Scop)
2357 and then (Has_Task (Designated_Type (Etype (Scop)))
2359 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2361 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2362 and then Expander_Active
2364 -- Avoid cases with no tasking support
2366 and then RTE_Available (RE_Current_Master)
2367 and then not Restriction_Active (No_Task_Hierarchy)
2370 Make_Object_Declaration (Loc,
2371 Defining_Identifier =>
2372 Make_Defining_Identifier (Loc, Name_uMaster),
2373 Constant_Present => True,
2374 Object_Definition =>
2375 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2377 Make_Explicit_Dereference (Loc,
2378 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2380 if Present (Declarations (N)) then
2381 Prepend (Decl, Declarations (N));
2383 Set_Declarations (N, New_List (Decl));
2386 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2387 Set_Has_Master_Entity (Scop);
2389 -- Now mark the containing scope as a task master
2392 while Nkind (Par) /= N_Compilation_Unit loop
2393 Par := Parent (Par);
2394 pragma Assert (Present (Par));
2396 -- If we fall off the top, we are at the outer level, and
2397 -- the environment task is our effective master, so nothing
2401 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2403 Set_Is_Task_Master (Par, True);
2408 end Check_Anonymous_Return;
2410 -------------------------
2411 -- Check_Inline_Pragma --
2412 -------------------------
2414 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2418 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2419 -- True when N is a pragma Inline or Inline_Always that applies
2420 -- to this subprogram.
2422 -----------------------
2423 -- Is_Inline_Pragma --
2424 -----------------------
2426 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2429 Nkind (N) = N_Pragma
2431 (Pragma_Name (N) = Name_Inline_Always
2434 and then Pragma_Name (N) = Name_Inline))
2437 (Expression (First (Pragma_Argument_Associations (N)))) =
2439 end Is_Inline_Pragma;
2441 -- Start of processing for Check_Inline_Pragma
2444 if not Expander_Active then
2448 if Is_List_Member (N)
2449 and then Present (Next (N))
2450 and then Is_Inline_Pragma (Next (N))
2454 elsif Nkind (N) /= N_Subprogram_Body_Stub
2455 and then Present (Declarations (N))
2456 and then Is_Inline_Pragma (First (Declarations (N)))
2458 Prag := First (Declarations (N));
2464 if Present (Prag) then
2465 if Present (Spec_Id) then
2466 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2471 -- Create a subprogram declaration, to make treatment uniform
2474 Subp : constant Entity_Id :=
2475 Make_Defining_Identifier (Loc, Chars (Body_Id));
2476 Decl : constant Node_Id :=
2477 Make_Subprogram_Declaration (Loc,
2479 New_Copy_Tree (Specification (N)));
2482 Set_Defining_Unit_Name (Specification (Decl), Subp);
2484 if Present (First_Formal (Body_Id)) then
2485 Plist := Copy_Parameter_List (Body_Id);
2486 Set_Parameter_Specifications
2487 (Specification (Decl), Plist);
2490 Insert_Before (N, Decl);
2493 Set_Has_Pragma_Inline (Subp);
2495 if Pragma_Name (Prag) = Name_Inline_Always then
2496 Set_Is_Inlined (Subp);
2497 Set_Has_Pragma_Inline_Always (Subp);
2500 -- Prior to copying the subprogram body to create a template
2501 -- for it for subsequent inlining, remove the pragma from
2502 -- the current body so that the copy that will produce the
2503 -- new body will start from a completely unanalyzed tree.
2505 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2506 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2513 end Check_Inline_Pragma;
2515 --------------------------
2516 -- Check_Missing_Return --
2517 --------------------------
2519 procedure Check_Missing_Return is
2521 Missing_Ret : Boolean;
2524 if Nkind (Body_Spec) = N_Function_Specification then
2525 if Present (Spec_Id) then
2531 if Return_Present (Id) then
2532 Check_Returns (HSS, 'F', Missing_Ret);
2535 Set_Has_Missing_Return (Id);
2538 elsif Is_Generic_Subprogram (Id)
2539 or else not Is_Machine_Code_Subprogram (Id)
2541 Error_Msg_N ("missing RETURN statement in function body", N);
2544 -- If procedure with No_Return, check returns
2546 elsif Nkind (Body_Spec) = N_Procedure_Specification
2547 and then Present (Spec_Id)
2548 and then No_Return (Spec_Id)
2550 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2553 -- Special checks in SPARK mode
2555 if Nkind (Body_Spec) = N_Function_Specification then
2557 -- In SPARK mode, last statement of a function should be a return
2560 Stat : constant Node_Id := Last_Source_Statement (HSS);
2563 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2564 N_Extended_Return_Statement)
2566 Check_SPARK_Restriction
2567 ("last statement in function should be RETURN", Stat);
2571 -- In SPARK mode, verify that a procedure has no return
2573 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2574 if Present (Spec_Id) then
2580 -- Would be nice to point to return statement here, can we
2581 -- borrow the Check_Returns procedure here ???
2583 if Return_Present (Id) then
2584 Check_SPARK_Restriction
2585 ("procedure should not have RETURN", N);
2588 end Check_Missing_Return;
2590 -----------------------
2591 -- Disambiguate_Spec --
2592 -----------------------
2594 function Disambiguate_Spec return Entity_Id is
2595 Priv_Spec : Entity_Id;
2598 procedure Replace_Types (To_Corresponding : Boolean);
2599 -- Depending on the flag, replace the type of formal parameters of
2600 -- Body_Id if it is a concurrent type implementing interfaces with
2601 -- the corresponding record type or the other way around.
2603 procedure Replace_Types (To_Corresponding : Boolean) is
2605 Formal_Typ : Entity_Id;
2608 Formal := First_Formal (Body_Id);
2609 while Present (Formal) loop
2610 Formal_Typ := Etype (Formal);
2612 if Is_Class_Wide_Type (Formal_Typ) then
2613 Formal_Typ := Root_Type (Formal_Typ);
2616 -- From concurrent type to corresponding record
2618 if To_Corresponding then
2619 if Is_Concurrent_Type (Formal_Typ)
2620 and then Present (Corresponding_Record_Type (Formal_Typ))
2621 and then Present (Interfaces (
2622 Corresponding_Record_Type (Formal_Typ)))
2625 Corresponding_Record_Type (Formal_Typ));
2628 -- From corresponding record to concurrent type
2631 if Is_Concurrent_Record_Type (Formal_Typ)
2632 and then Present (Interfaces (Formal_Typ))
2635 Corresponding_Concurrent_Type (Formal_Typ));
2639 Next_Formal (Formal);
2643 -- Start of processing for Disambiguate_Spec
2646 -- Try to retrieve the specification of the body as is. All error
2647 -- messages are suppressed because the body may not have a spec in
2648 -- its current state.
2650 Spec_N := Find_Corresponding_Spec (N, False);
2652 -- It is possible that this is the body of a primitive declared
2653 -- between a private and a full view of a concurrent type. The
2654 -- controlling parameter of the spec carries the concurrent type,
2655 -- not the corresponding record type as transformed by Analyze_
2656 -- Subprogram_Specification. In such cases, we undo the change
2657 -- made by the analysis of the specification and try to find the
2660 -- Note that wrappers already have their corresponding specs and
2661 -- bodies set during their creation, so if the candidate spec is
2662 -- a wrapper, then we definitely need to swap all types to their
2663 -- original concurrent status.
2666 or else Is_Primitive_Wrapper (Spec_N)
2668 -- Restore all references of corresponding record types to the
2669 -- original concurrent types.
2671 Replace_Types (To_Corresponding => False);
2672 Priv_Spec := Find_Corresponding_Spec (N, False);
2674 -- The current body truly belongs to a primitive declared between
2675 -- a private and a full view. We leave the modified body as is,
2676 -- and return the true spec.
2678 if Present (Priv_Spec)
2679 and then Is_Private_Primitive (Priv_Spec)
2684 -- In case that this is some sort of error, restore the original
2685 -- state of the body.
2687 Replace_Types (To_Corresponding => True);
2691 end Disambiguate_Spec;
2693 ----------------------------
2694 -- Exchange_Limited_Views --
2695 ----------------------------
2697 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2698 procedure Detect_And_Exchange (Id : Entity_Id);
2699 -- Determine whether Id's type denotes an incomplete type associated
2700 -- with a limited with clause and exchange the limited view with the
2703 -------------------------
2704 -- Detect_And_Exchange --
2705 -------------------------
2707 procedure Detect_And_Exchange (Id : Entity_Id) is
2708 Typ : constant Entity_Id := Etype (Id);
2711 if Ekind (Typ) = E_Incomplete_Type
2712 and then From_Limited_With (Typ)
2713 and then Present (Non_Limited_View (Typ))
2715 Set_Etype (Id, Non_Limited_View (Typ));
2717 end Detect_And_Exchange;
2723 -- Start of processing for Exchange_Limited_Views
2726 if No (Subp_Id) then
2729 -- Do not process subprogram bodies as they already use the non-
2730 -- limited view of types.
2732 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2736 -- Examine all formals and swap views when applicable
2738 Formal := First_Formal (Subp_Id);
2739 while Present (Formal) loop
2740 Detect_And_Exchange (Formal);
2742 Next_Formal (Formal);
2745 -- Process the return type of a function
2747 if Ekind (Subp_Id) = E_Function then
2748 Detect_And_Exchange (Subp_Id);
2750 end Exchange_Limited_Views;
2752 -------------------------------------
2753 -- Is_Private_Concurrent_Primitive --
2754 -------------------------------------
2756 function Is_Private_Concurrent_Primitive
2757 (Subp_Id : Entity_Id) return Boolean
2759 Formal_Typ : Entity_Id;
2762 if Present (First_Formal (Subp_Id)) then
2763 Formal_Typ := Etype (First_Formal (Subp_Id));
2765 if Is_Concurrent_Record_Type (Formal_Typ) then
2766 if Is_Class_Wide_Type (Formal_Typ) then
2767 Formal_Typ := Root_Type (Formal_Typ);
2770 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2773 -- The type of the first formal is a concurrent tagged type with
2777 Is_Concurrent_Type (Formal_Typ)
2778 and then Is_Tagged_Type (Formal_Typ)
2779 and then Has_Private_Declaration (Formal_Typ);
2783 end Is_Private_Concurrent_Primitive;
2785 ----------------------------
2786 -- Set_Trivial_Subprogram --
2787 ----------------------------
2789 procedure Set_Trivial_Subprogram (N : Node_Id) is
2790 Nxt : constant Node_Id := Next (N);
2793 Set_Is_Trivial_Subprogram (Body_Id);
2795 if Present (Spec_Id) then
2796 Set_Is_Trivial_Subprogram (Spec_Id);
2800 and then Nkind (Nxt) = N_Simple_Return_Statement
2801 and then No (Next (Nxt))
2802 and then Present (Expression (Nxt))
2803 and then Is_Entity_Name (Expression (Nxt))
2805 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2807 end Set_Trivial_Subprogram;
2809 ---------------------------------
2810 -- Verify_Overriding_Indicator --
2811 ---------------------------------
2813 procedure Verify_Overriding_Indicator is
2815 if Must_Override (Body_Spec) then
2816 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2817 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2821 elsif not Present (Overridden_Operation (Spec_Id)) then
2823 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2825 -- Overriding indicators aren't allowed for protected subprogram
2826 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2827 -- this to a warning if -gnatd.E is enabled.
2829 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2830 Error_Msg_Warn := Error_To_Warning;
2832 ("<<overriding indicator not allowed for protected "
2833 & "subprogram body", Body_Spec);
2836 elsif Must_Not_Override (Body_Spec) then
2837 if Present (Overridden_Operation (Spec_Id)) then
2839 ("subprogram& overrides inherited operation",
2840 Body_Spec, Spec_Id);
2842 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2843 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2846 ("subprogram & overrides predefined operator ",
2847 Body_Spec, Spec_Id);
2849 -- Overriding indicators aren't allowed for protected subprogram
2850 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2851 -- this to a warning if -gnatd.E is enabled.
2853 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2854 Error_Msg_Warn := Error_To_Warning;
2857 ("<<overriding indicator not allowed " &
2858 "for protected subprogram body",
2861 -- If this is not a primitive operation, then the overriding
2862 -- indicator is altogether illegal.
2864 elsif not Is_Primitive (Spec_Id) then
2866 ("overriding indicator only allowed " &
2867 "if subprogram is primitive",
2871 -- If checking the style rule and the operation overrides, then
2872 -- issue a warning about a missing overriding_indicator. Protected
2873 -- subprogram bodies are excluded from this style checking, since
2874 -- they aren't primitives (even though their declarations can
2875 -- override) and aren't allowed to have an overriding_indicator.
2878 and then Present (Overridden_Operation (Spec_Id))
2879 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2881 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2882 Style.Missing_Overriding (N, Body_Id);
2885 and then Can_Override_Operator (Spec_Id)
2886 and then not Is_Predefined_File_Name
2887 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2889 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2890 Style.Missing_Overriding (N, Body_Id);
2892 end Verify_Overriding_Indicator;
2894 -- Start of processing for Analyze_Subprogram_Body_Helper
2897 -- Generic subprograms are handled separately. They always have a
2898 -- generic specification. Determine whether current scope has a
2899 -- previous declaration.
2901 -- If the subprogram body is defined within an instance of the same
2902 -- name, the instance appears as a package renaming, and will be hidden
2903 -- within the subprogram.
2905 if Present (Prev_Id)
2906 and then not Is_Overloadable (Prev_Id)
2907 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2908 or else Comes_From_Source (Prev_Id))
2910 if Is_Generic_Subprogram (Prev_Id) then
2912 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2913 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2915 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2917 if Nkind (N) = N_Subprogram_Body then
2918 HSS := Handled_Statement_Sequence (N);
2919 Check_Missing_Return;
2925 -- Previous entity conflicts with subprogram name. Attempting to
2926 -- enter name will post error.
2928 Enter_Name (Body_Id);
2932 -- Non-generic case, find the subprogram declaration, if one was seen,
2933 -- or enter new overloaded entity in the current scope. If the
2934 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2935 -- part of the context of one of its subunits. No need to redo the
2938 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2942 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2944 if Nkind (N) = N_Subprogram_Body_Stub
2945 or else No (Corresponding_Spec (N))
2947 if Is_Private_Concurrent_Primitive (Body_Id) then
2948 Spec_Id := Disambiguate_Spec;
2950 Spec_Id := Find_Corresponding_Spec (N);
2953 -- If this is a duplicate body, no point in analyzing it
2955 if Error_Posted (N) then
2959 -- A subprogram body should cause freezing of its own declaration,
2960 -- but if there was no previous explicit declaration, then the
2961 -- subprogram will get frozen too late (there may be code within
2962 -- the body that depends on the subprogram having been frozen,
2963 -- such as uses of extra formals), so we force it to be frozen
2964 -- here. Same holds if the body and spec are compilation units.
2965 -- Finally, if the return type is an anonymous access to protected
2966 -- subprogram, it must be frozen before the body because its
2967 -- expansion has generated an equivalent type that is used when
2968 -- elaborating the body.
2970 -- An exception in the case of Ada 2012, AI05-177: The bodies
2971 -- created for expression functions do not freeze.
2974 and then Nkind (Original_Node (N)) /= N_Expression_Function
2976 Freeze_Before (N, Body_Id);
2978 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2979 Freeze_Before (N, Spec_Id);
2981 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2982 Freeze_Before (N, Etype (Body_Id));
2986 Spec_Id := Corresponding_Spec (N);
2990 -- Previously we scanned the body to look for nested subprograms, and
2991 -- rejected an inline directive if nested subprograms were present,
2992 -- because the back-end would generate conflicting symbols for the
2993 -- nested bodies. This is now unnecessary.
2995 -- Look ahead to recognize a pragma Inline that appears after the body
2997 Check_Inline_Pragma (Spec_Id);
2999 -- Deal with special case of a fully private operation in the body of
3000 -- the protected type. We must create a declaration for the subprogram,
3001 -- in order to attach the protected subprogram that will be used in
3002 -- internal calls. We exclude compiler generated bodies from the
3003 -- expander since the issue does not arise for those cases.
3006 and then Comes_From_Source (N)
3007 and then Is_Protected_Type (Current_Scope)
3009 Spec_Id := Build_Private_Protected_Declaration (N);
3012 -- If a separate spec is present, then deal with freezing issues
3014 if Present (Spec_Id) then
3015 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3016 Verify_Overriding_Indicator;
3018 -- In general, the spec will be frozen when we start analyzing the
3019 -- body. However, for internally generated operations, such as
3020 -- wrapper functions for inherited operations with controlling
3021 -- results, the spec may not have been frozen by the time we expand
3022 -- the freeze actions that include the bodies. In particular, extra
3023 -- formals for accessibility or for return-in-place may need to be
3024 -- generated. Freeze nodes, if any, are inserted before the current
3025 -- body. These freeze actions are also needed in ASIS mode to enable
3026 -- the proper back-annotations.
3028 if not Is_Frozen (Spec_Id)
3029 and then (Expander_Active or ASIS_Mode)
3031 -- Force the generation of its freezing node to ensure proper
3032 -- management of access types in the backend.
3034 -- This is definitely needed for some cases, but it is not clear
3035 -- why, to be investigated further???
3037 Set_Has_Delayed_Freeze (Spec_Id);
3038 Freeze_Before (N, Spec_Id);
3042 -- Mark presence of postcondition procedure in current scope and mark
3043 -- the procedure itself as needing debug info. The latter is important
3044 -- when analyzing decision coverage (for example, for MC/DC coverage).
3046 if Chars (Body_Id) = Name_uPostconditions then
3047 Set_Has_Postconditions (Current_Scope);
3048 Set_Debug_Info_Needed (Body_Id);
3051 -- Place subprogram on scope stack, and make formals visible. If there
3052 -- is a spec, the visible entity remains that of the spec.
3054 if Present (Spec_Id) then
3055 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3057 if Is_Child_Unit (Spec_Id) then
3058 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3062 Style.Check_Identifier (Body_Id, Spec_Id);
3065 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3066 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3068 if Is_Abstract_Subprogram (Spec_Id) then
3069 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3073 Set_Convention (Body_Id, Convention (Spec_Id));
3074 Set_Has_Completion (Spec_Id);
3076 if Is_Protected_Type (Scope (Spec_Id)) then
3077 Prot_Typ := Scope (Spec_Id);
3080 -- If this is a body generated for a renaming, do not check for
3081 -- full conformance. The check is redundant, because the spec of
3082 -- the body is a copy of the spec in the renaming declaration,
3083 -- and the test can lead to spurious errors on nested defaults.
3085 if Present (Spec_Decl)
3086 and then not Comes_From_Source (N)
3088 (Nkind (Original_Node (Spec_Decl)) =
3089 N_Subprogram_Renaming_Declaration
3090 or else (Present (Corresponding_Body (Spec_Decl))
3092 Nkind (Unit_Declaration_Node
3093 (Corresponding_Body (Spec_Decl))) =
3094 N_Subprogram_Renaming_Declaration))
3098 -- Conversely, the spec may have been generated for specless body
3099 -- with an inline pragma.
3101 elsif Comes_From_Source (N)
3102 and then not Comes_From_Source (Spec_Id)
3103 and then Has_Pragma_Inline (Spec_Id)
3110 Fully_Conformant, True, Conformant, Body_Id);
3113 -- If the body is not fully conformant, we have to decide if we
3114 -- should analyze it or not. If it has a really messed up profile
3115 -- then we probably should not analyze it, since we will get too
3116 -- many bogus messages.
3118 -- Our decision is to go ahead in the non-fully conformant case
3119 -- only if it is at least mode conformant with the spec. Note
3120 -- that the call to Check_Fully_Conformant has issued the proper
3121 -- error messages to complain about the lack of conformance.
3124 and then not Mode_Conformant (Body_Id, Spec_Id)
3130 if Spec_Id /= Body_Id then
3131 Reference_Body_Formals (Spec_Id, Body_Id);
3134 Set_Ekind (Body_Id, E_Subprogram_Body);
3136 if Nkind (N) = N_Subprogram_Body_Stub then
3137 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3142 Set_Corresponding_Spec (N, Spec_Id);
3144 -- Ada 2005 (AI-345): If the operation is a primitive operation
3145 -- of a concurrent type, the type of the first parameter has been
3146 -- replaced with the corresponding record, which is the proper
3147 -- run-time structure to use. However, within the body there may
3148 -- be uses of the formals that depend on primitive operations
3149 -- of the type (in particular calls in prefixed form) for which
3150 -- we need the original concurrent type. The operation may have
3151 -- several controlling formals, so the replacement must be done
3154 if Comes_From_Source (Spec_Id)
3155 and then Present (First_Entity (Spec_Id))
3156 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3157 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3158 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3159 and then Present (Corresponding_Concurrent_Type
3160 (Etype (First_Entity (Spec_Id))))
3163 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3167 Form := First_Formal (Spec_Id);
3168 while Present (Form) loop
3169 if Etype (Form) = Typ then
3170 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3178 -- Make the formals visible, and place subprogram on scope stack.
3179 -- This is also the point at which we set Last_Real_Spec_Entity
3180 -- to mark the entities which will not be moved to the body.
3182 Install_Formals (Spec_Id);
3183 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3185 -- Within an instance, add local renaming declarations so that
3186 -- gdb can retrieve the values of actuals more easily. This is
3187 -- only relevant if generating code (and indeed we definitely
3188 -- do not want these definitions -gnatc mode, because that would
3191 if Is_Generic_Instance (Spec_Id)
3192 and then Is_Wrapper_Package (Current_Scope)
3193 and then Expander_Active
3195 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3198 Push_Scope (Spec_Id);
3200 -- Make sure that the subprogram is immediately visible. For
3201 -- child units that have no separate spec this is indispensable.
3202 -- Otherwise it is safe albeit redundant.
3204 Set_Is_Immediately_Visible (Spec_Id);
3207 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3208 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3209 Set_Scope (Body_Id, Scope (Spec_Id));
3210 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3212 -- Case of subprogram body with no previous spec
3215 -- Check for style warning required
3219 -- Only apply check for source level subprograms for which checks
3220 -- have not been suppressed.
3222 and then Comes_From_Source (Body_Id)
3223 and then not Suppress_Style_Checks (Body_Id)
3225 -- No warnings within an instance
3227 and then not In_Instance
3229 -- No warnings for expression functions
3231 and then Nkind (Original_Node (N)) /= N_Expression_Function
3233 Style.Body_With_No_Spec (N);
3236 New_Overloaded_Entity (Body_Id);
3238 if Nkind (N) /= N_Subprogram_Body_Stub then
3239 Set_Acts_As_Spec (N);
3240 Generate_Definition (Body_Id);
3241 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3243 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3244 Install_Formals (Body_Id);
3246 Push_Scope (Body_Id);
3249 -- For stubs and bodies with no previous spec, generate references to
3252 Generate_Reference_To_Formals (Body_Id);
3255 -- Set SPARK_Mode from context
3257 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3258 Set_SPARK_Pragma_Inherited (Body_Id, True);
3260 -- If the return type is an anonymous access type whose designated type
3261 -- is the limited view of a class-wide type and the non-limited view is
3262 -- available, update the return type accordingly.
3264 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3270 Rtyp := Etype (Current_Scope);
3272 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3273 Etyp := Directly_Designated_Type (Rtyp);
3275 if Is_Class_Wide_Type (Etyp)
3276 and then From_Limited_With (Etyp)
3278 Set_Directly_Designated_Type
3279 (Etype (Current_Scope), Available_View (Etyp));
3285 -- If this is the proper body of a stub, we must verify that the stub
3286 -- conforms to the body, and to the previous spec if one was present.
3287 -- We know already that the body conforms to that spec. This test is
3288 -- only required for subprograms that come from source.
3290 if Nkind (Parent (N)) = N_Subunit
3291 and then Comes_From_Source (N)
3292 and then not Error_Posted (Body_Id)
3293 and then Nkind (Corresponding_Stub (Parent (N))) =
3294 N_Subprogram_Body_Stub
3297 Old_Id : constant Entity_Id :=
3299 (Specification (Corresponding_Stub (Parent (N))));
3301 Conformant : Boolean := False;
3304 if No (Spec_Id) then
3305 Check_Fully_Conformant (Body_Id, Old_Id);
3309 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3311 if not Conformant then
3313 -- The stub was taken to be a new declaration. Indicate that
3316 Set_Has_Completion (Old_Id, False);
3322 Set_Has_Completion (Body_Id);
3323 Check_Eliminated (Body_Id);
3325 if Nkind (N) = N_Subprogram_Body_Stub then
3327 -- Analyze any aspect specifications that appear on the subprogram
3330 if Has_Aspects (N) then
3331 Analyze_Aspects_On_Body_Or_Stub;
3334 -- Stop the analysis now as the stub cannot be inlined, plus it does
3335 -- not have declarative or statement lists.
3340 -- Handle frontend inlining. There is no need to prepare us for inlining
3341 -- if we will not generate the code.
3345 if not Debug_Flag_Dot_K then
3346 if Present (Spec_Id)
3347 and then Expander_Active
3349 (Has_Pragma_Inline_Always (Spec_Id)
3350 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3352 Build_Body_To_Inline (N, Spec_Id);
3357 elsif Expander_Active
3358 and then Serious_Errors_Detected = 0
3359 and then Present (Spec_Id)
3360 and then Has_Pragma_Inline (Spec_Id)
3362 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3365 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3366 -- of the specification we have to install the private withed units.
3367 -- This holds for child units as well.
3369 if Is_Compilation_Unit (Body_Id)
3370 or else Nkind (Parent (N)) = N_Compilation_Unit
3372 Install_Private_With_Clauses (Body_Id);
3375 Check_Anonymous_Return;
3377 -- Set the Protected_Formal field of each extra formal of the protected
3378 -- subprogram to reference the corresponding extra formal of the
3379 -- subprogram that implements it. For regular formals this occurs when
3380 -- the protected subprogram's declaration is expanded, but the extra
3381 -- formals don't get created until the subprogram is frozen. We need to
3382 -- do this before analyzing the protected subprogram's body so that any
3383 -- references to the original subprogram's extra formals will be changed
3384 -- refer to the implementing subprogram's formals (see Expand_Formal).
3386 if Present (Spec_Id)
3387 and then Is_Protected_Type (Scope (Spec_Id))
3388 and then Present (Protected_Body_Subprogram (Spec_Id))
3391 Impl_Subp : constant Entity_Id :=
3392 Protected_Body_Subprogram (Spec_Id);
3393 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3394 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3396 while Present (Prot_Ext_Formal) loop
3397 pragma Assert (Present (Impl_Ext_Formal));
3398 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3399 Next_Formal_With_Extras (Prot_Ext_Formal);
3400 Next_Formal_With_Extras (Impl_Ext_Formal);
3405 -- Now we can go on to analyze the body
3407 HSS := Handled_Statement_Sequence (N);
3408 Set_Actual_Subtypes (N, Current_Scope);
3410 -- Add a declaration for the Protection object, renaming declarations
3411 -- for discriminals and privals and finally a declaration for the entry
3412 -- family index (if applicable). This form of early expansion is done
3413 -- when the Expander is active because Install_Private_Data_Declarations
3414 -- references entities which were created during regular expansion. The
3415 -- subprogram entity must come from source, and not be an internally
3416 -- generated subprogram.
3419 and then Present (Prot_Typ)
3420 and then Present (Spec_Id)
3421 and then Comes_From_Source (Spec_Id)
3422 and then not Is_Eliminated (Spec_Id)
3424 Install_Private_Data_Declarations
3425 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3428 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3429 -- may now appear in parameter and result profiles. Since the analysis
3430 -- of a subprogram body may use the parameter and result profile of the
3431 -- spec, swap any limited views with their non-limited counterpart.
3433 if Ada_Version >= Ada_2012 then
3434 Exchange_Limited_Views (Spec_Id);
3437 -- Analyze any aspect specifications that appear on the subprogram body
3439 if Has_Aspects (N) then
3440 Analyze_Aspects_On_Body_Or_Stub;
3443 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3444 -- invariants and predicates associated with the body and its spec.
3445 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3446 -- prepares the contract assertions for generic subprograms or for ASIS.
3447 -- Do not generate contract checks in SPARK mode.
3449 if not GNATprove_Mode then
3450 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3453 -- Analyze the declarations (this call will analyze the precondition
3454 -- Check pragmas we prepended to the list, as well as the declaration
3455 -- of the _Postconditions procedure).
3457 Analyze_Declarations (Declarations (N));
3459 -- After declarations have been analyzed, the body has been set
3460 -- its final value of SPARK_Mode. Check that SPARK_Mode for body
3461 -- is consistent with SPARK_Mode for spec.
3463 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3464 if Present (SPARK_Pragma (Spec_Id)) then
3465 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3467 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3469 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3470 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3471 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3473 ("\value Off was set for SPARK_Mode on&#", N, Spec_Id);
3476 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3480 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3481 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3482 Error_Msg_Sloc := Sloc (Spec_Id);
3483 Error_Msg_NE ("\no value was set for SPARK_Mode on&#", N, Spec_Id);
3487 -- Check completion, and analyze the statements
3490 Inspect_Deferred_Constant_Completion (Declarations (N));
3493 -- Deal with end of scope processing for the body
3495 Process_End_Label (HSS, 't', Current_Scope);
3497 Check_Subprogram_Order (N);
3498 Set_Analyzed (Body_Id);
3500 -- If we have a separate spec, then the analysis of the declarations
3501 -- caused the entities in the body to be chained to the spec id, but
3502 -- we want them chained to the body id. Only the formal parameters
3503 -- end up chained to the spec id in this case.
3505 if Present (Spec_Id) then
3507 -- We must conform to the categorization of our spec
3509 Validate_Categorization_Dependency (N, Spec_Id);
3511 -- And if this is a child unit, the parent units must conform
3513 if Is_Child_Unit (Spec_Id) then
3514 Validate_Categorization_Dependency
3515 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3518 -- Here is where we move entities from the spec to the body
3520 -- Case where there are entities that stay with the spec
3522 if Present (Last_Real_Spec_Entity) then
3524 -- No body entities (happens when the only real spec entities come
3525 -- from precondition and postcondition pragmas).
3527 if No (Last_Entity (Body_Id)) then
3529 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3531 -- Body entities present (formals), so chain stuff past them
3535 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3538 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3539 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3540 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3542 -- Case where there are no spec entities, in this case there can be
3543 -- no body entities either, so just move everything.
3546 pragma Assert (No (Last_Entity (Body_Id)));
3547 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3548 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3549 Set_First_Entity (Spec_Id, Empty);
3550 Set_Last_Entity (Spec_Id, Empty);
3554 Check_Missing_Return;
3556 -- Now we are going to check for variables that are never modified in
3557 -- the body of the procedure. But first we deal with a special case
3558 -- where we want to modify this check. If the body of the subprogram
3559 -- starts with a raise statement or its equivalent, or if the body
3560 -- consists entirely of a null statement, then it is pretty obvious that
3561 -- it is OK to not reference the parameters. For example, this might be
3562 -- the following common idiom for a stubbed function: statement of the
3563 -- procedure raises an exception. In particular this deals with the
3564 -- common idiom of a stubbed function, which appears something like:
3566 -- function F (A : Integer) return Some_Type;
3569 -- raise Program_Error;
3573 -- Here the purpose of X is simply to satisfy the annoying requirement
3574 -- in Ada that there be at least one return, and we certainly do not
3575 -- want to go posting warnings on X that it is not initialized. On
3576 -- the other hand, if X is entirely unreferenced that should still
3579 -- What we do is to detect these cases, and if we find them, flag the
3580 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3581 -- suppress unwanted warnings. For the case of the function stub above
3582 -- we have a special test to set X as apparently assigned to suppress
3589 -- Skip initial labels (for one thing this occurs when we are in
3590 -- front end ZCX mode, but in any case it is irrelevant), and also
3591 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3593 Stm := First (Statements (HSS));
3594 while Nkind (Stm) = N_Label
3595 or else Nkind (Stm) in N_Push_xxx_Label
3600 -- Do the test on the original statement before expansion
3603 Ostm : constant Node_Id := Original_Node (Stm);
3606 -- If explicit raise statement, turn on flag
3608 if Nkind (Ostm) = N_Raise_Statement then
3609 Set_Trivial_Subprogram (Stm);
3611 -- If null statement, and no following statements, turn on flag
3613 elsif Nkind (Stm) = N_Null_Statement
3614 and then Comes_From_Source (Stm)
3615 and then No (Next (Stm))
3617 Set_Trivial_Subprogram (Stm);
3619 -- Check for explicit call cases which likely raise an exception
3621 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3622 if Is_Entity_Name (Name (Ostm)) then
3624 Ent : constant Entity_Id := Entity (Name (Ostm));
3627 -- If the procedure is marked No_Return, then likely it
3628 -- raises an exception, but in any case it is not coming
3629 -- back here, so turn on the flag.
3632 and then Ekind (Ent) = E_Procedure
3633 and then No_Return (Ent)
3635 Set_Trivial_Subprogram (Stm);
3643 -- Check for variables that are never modified
3649 -- If there is a separate spec, then transfer Never_Set_In_Source
3650 -- flags from out parameters to the corresponding entities in the
3651 -- body. The reason we do that is we want to post error flags on
3652 -- the body entities, not the spec entities.
3654 if Present (Spec_Id) then
3655 E1 := First_Entity (Spec_Id);
3656 while Present (E1) loop
3657 if Ekind (E1) = E_Out_Parameter then
3658 E2 := First_Entity (Body_Id);
3659 while Present (E2) loop
3660 exit when Chars (E1) = Chars (E2);
3664 if Present (E2) then
3665 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3673 -- Check references in body
3675 Check_References (Body_Id);
3677 end Analyze_Subprogram_Body_Helper;
3679 ---------------------------------
3680 -- Analyze_Subprogram_Contract --
3681 ---------------------------------
3683 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3684 Items : constant Node_Id := Contract (Subp);
3685 Case_Prag : Node_Id := Empty;
3686 Depends : Node_Id := Empty;
3687 Global : Node_Id := Empty;
3688 Mode : SPARK_Mode_Type;
3690 Post_Prag : Node_Id := Empty;
3692 Seen_In_Case : Boolean := False;
3693 Seen_In_Post : Boolean := False;
3696 Save_SPARK_Mode_And_Set (Subp, Mode);
3698 if Present (Items) then
3700 -- Analyze pre- and postconditions
3702 Prag := Pre_Post_Conditions (Items);
3703 while Present (Prag) loop
3704 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3706 -- Verify whether a postcondition mentions attribute 'Result and
3707 -- its expression introduces a post-state.
3709 if Warn_On_Suspicious_Contract
3710 and then Pragma_Name (Prag) = Name_Postcondition
3713 Check_Result_And_Post_State (Prag, Seen_In_Post);
3716 Prag := Next_Pragma (Prag);
3719 -- Analyze contract-cases and test-cases
3721 Prag := Contract_Test_Cases (Items);
3722 while Present (Prag) loop
3723 Nam := Pragma_Name (Prag);
3725 if Nam = Name_Contract_Cases then
3726 Analyze_Contract_Cases_In_Decl_Part (Prag);
3728 -- Verify whether contract-cases mention attribute 'Result and
3729 -- its expression introduces a post-state. Perform the check
3730 -- only when the pragma is legal.
3732 if Warn_On_Suspicious_Contract
3733 and then not Error_Posted (Prag)
3736 Check_Result_And_Post_State (Prag, Seen_In_Case);
3740 pragma Assert (Nam = Name_Test_Case);
3741 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3744 Prag := Next_Pragma (Prag);
3747 -- Analyze classification pragmas
3749 Prag := Classifications (Items);
3750 while Present (Prag) loop
3751 Nam := Pragma_Name (Prag);
3753 if Nam = Name_Depends then
3755 else pragma Assert (Nam = Name_Global);
3759 Prag := Next_Pragma (Prag);
3762 -- Analyze Global first as Depends may mention items classified in
3763 -- the global categorization.
3765 if Present (Global) then
3766 Analyze_Global_In_Decl_Part (Global);
3769 -- Depends must be analyzed after Global in order to see the modes of
3770 -- all global items.
3772 if Present (Depends) then
3773 Analyze_Depends_In_Decl_Part (Depends);
3777 -- Emit an error when neither the postconditions nor the contract-cases
3778 -- mention attribute 'Result in the context of a function.
3780 if Warn_On_Suspicious_Contract
3781 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3783 if Present (Case_Prag)
3784 and then not Seen_In_Case
3785 and then Present (Post_Prag)
3786 and then not Seen_In_Post
3789 ("neither function postcondition nor contract cases mention "
3790 & "result?T?", Post_Prag);
3792 elsif Present (Case_Prag) and then not Seen_In_Case then
3794 ("contract cases do not mention result?T?", Case_Prag);
3796 -- OK if we have at least one IN OUT parameter
3798 elsif Present (Post_Prag) and then not Seen_In_Post then
3802 F := First_Formal (Subp);
3803 while Present (F) loop
3804 if Ekind (F) = E_In_Out_Parameter then
3812 -- If no in-out parameters and no mention of Result, the contract
3813 -- is certainly suspicious.
3816 ("function postcondition does not mention result?T?", Post_Prag);
3820 Restore_SPARK_Mode (Mode);
3821 end Analyze_Subprogram_Contract;
3823 ------------------------------------
3824 -- Analyze_Subprogram_Declaration --
3825 ------------------------------------
3827 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3828 Scop : constant Entity_Id := Current_Scope;
3829 Designator : Entity_Id;
3831 Is_Completion : Boolean;
3832 -- Indicates whether a null procedure declaration is a completion
3835 -- Null procedures are not allowed in SPARK
3837 if Nkind (Specification (N)) = N_Procedure_Specification
3838 and then Null_Present (Specification (N))
3840 Check_SPARK_Restriction ("null procedure is not allowed", N);
3842 if Is_Protected_Type (Current_Scope) then
3843 Error_Msg_N ("protected operation cannot be a null procedure", N);
3846 Analyze_Null_Procedure (N, Is_Completion);
3848 if Is_Completion then
3850 -- The null procedure acts as a body, nothing further is needed.
3856 Designator := Analyze_Subprogram_Specification (Specification (N));
3858 -- A reference may already have been generated for the unit name, in
3859 -- which case the following call is redundant. However it is needed for
3860 -- declarations that are the rewriting of an expression function.
3862 Generate_Definition (Designator);
3864 -- Set SPARK mode from current context (may be overwritten later with
3865 -- explicit pragma).
3867 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
3868 Set_SPARK_Pragma_Inherited (Designator, True);
3870 if Debug_Flag_C then
3871 Write_Str ("==> subprogram spec ");
3872 Write_Name (Chars (Designator));
3873 Write_Str (" from ");
3874 Write_Location (Sloc (N));
3879 Validate_RCI_Subprogram_Declaration (N);
3880 New_Overloaded_Entity (Designator);
3881 Check_Delayed_Subprogram (Designator);
3883 -- If the type of the first formal of the current subprogram is a non-
3884 -- generic tagged private type, mark the subprogram as being a private
3885 -- primitive. Ditto if this is a function with controlling result, and
3886 -- the return type is currently private. In both cases, the type of the
3887 -- controlling argument or result must be in the current scope for the
3888 -- operation to be primitive.
3890 if Has_Controlling_Result (Designator)
3891 and then Is_Private_Type (Etype (Designator))
3892 and then Scope (Etype (Designator)) = Current_Scope
3893 and then not Is_Generic_Actual_Type (Etype (Designator))
3895 Set_Is_Private_Primitive (Designator);
3897 elsif Present (First_Formal (Designator)) then
3899 Formal_Typ : constant Entity_Id :=
3900 Etype (First_Formal (Designator));
3902 Set_Is_Private_Primitive (Designator,
3903 Is_Tagged_Type (Formal_Typ)
3904 and then Scope (Formal_Typ) = Current_Scope
3905 and then Is_Private_Type (Formal_Typ)
3906 and then not Is_Generic_Actual_Type (Formal_Typ));
3910 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3913 if Ada_Version >= Ada_2005
3914 and then Comes_From_Source (N)
3915 and then Is_Dispatching_Operation (Designator)
3922 if Has_Controlling_Result (Designator) then
3923 Etyp := Etype (Designator);
3926 E := First_Entity (Designator);
3928 and then Is_Formal (E)
3929 and then not Is_Controlling_Formal (E)
3937 if Is_Access_Type (Etyp) then
3938 Etyp := Directly_Designated_Type (Etyp);
3941 if Is_Interface (Etyp)
3942 and then not Is_Abstract_Subprogram (Designator)
3943 and then not (Ekind (Designator) = E_Procedure
3944 and then Null_Present (Specification (N)))
3946 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3948 -- Specialize error message based on procedures vs. functions,
3949 -- since functions can't be null subprograms.
3951 if Ekind (Designator) = E_Procedure then
3953 ("interface procedure % must be abstract or null", N);
3955 Error_Msg_N ("interface function % must be abstract", N);
3961 -- What is the following code for, it used to be
3963 -- ??? Set_Suppress_Elaboration_Checks
3964 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3966 -- The following seems equivalent, but a bit dubious
3968 if Elaboration_Checks_Suppressed (Designator) then
3969 Set_Kill_Elaboration_Checks (Designator);
3972 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3973 Set_Categorization_From_Scope (Designator, Scop);
3976 -- For a compilation unit, check for library-unit pragmas
3978 Push_Scope (Designator);
3979 Set_Categorization_From_Pragmas (N);
3980 Validate_Categorization_Dependency (N, Designator);
3984 -- For a compilation unit, set body required. This flag will only be
3985 -- reset if a valid Import or Interface pragma is processed later on.
3987 if Nkind (Parent (N)) = N_Compilation_Unit then
3988 Set_Body_Required (Parent (N), True);
3990 if Ada_Version >= Ada_2005
3991 and then Nkind (Specification (N)) = N_Procedure_Specification
3992 and then Null_Present (Specification (N))
3995 ("null procedure cannot be declared at library level", N);
3999 Generate_Reference_To_Formals (Designator);
4000 Check_Eliminated (Designator);
4002 if Debug_Flag_C then
4004 Write_Str ("<== subprogram spec ");
4005 Write_Name (Chars (Designator));
4006 Write_Str (" from ");
4007 Write_Location (Sloc (N));
4011 if Is_Protected_Type (Current_Scope) then
4013 -- Indicate that this is a protected operation, because it may be
4014 -- used in subsequent declarations within the protected type.
4016 Set_Convention (Designator, Convention_Protected);
4019 List_Inherited_Pre_Post_Aspects (Designator);
4021 if Has_Aspects (N) then
4022 Analyze_Aspect_Specifications (N, Designator);
4024 end Analyze_Subprogram_Declaration;
4026 --------------------------------------
4027 -- Analyze_Subprogram_Specification --
4028 --------------------------------------
4030 -- Reminder: N here really is a subprogram specification (not a subprogram
4031 -- declaration). This procedure is called to analyze the specification in
4032 -- both subprogram bodies and subprogram declarations (specs).
4034 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4035 Designator : constant Entity_Id := Defining_Entity (N);
4036 Formals : constant List_Id := Parameter_Specifications (N);
4038 -- Start of processing for Analyze_Subprogram_Specification
4041 -- User-defined operator is not allowed in SPARK, except as a renaming
4043 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4044 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4046 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
4049 -- Proceed with analysis. Do not emit a cross-reference entry if the
4050 -- specification comes from an expression function, because it may be
4051 -- the completion of a previous declaration. It is is not, the cross-
4052 -- reference entry will be emitted for the new subprogram declaration.
4054 if Nkind (Parent (N)) /= N_Expression_Function then
4055 Generate_Definition (Designator);
4058 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4060 if Nkind (N) = N_Function_Specification then
4061 Set_Ekind (Designator, E_Function);
4062 Set_Mechanism (Designator, Default_Mechanism);
4064 Set_Ekind (Designator, E_Procedure);
4065 Set_Etype (Designator, Standard_Void_Type);
4068 -- Introduce new scope for analysis of the formals and the return type
4070 Set_Scope (Designator, Current_Scope);
4072 if Present (Formals) then
4073 Push_Scope (Designator);
4074 Process_Formals (Formals, N);
4076 -- Check dimensions in N for formals with default expression
4078 Analyze_Dimension_Formals (N, Formals);
4080 -- Ada 2005 (AI-345): If this is an overriding operation of an
4081 -- inherited interface operation, and the controlling type is
4082 -- a synchronized type, replace the type with its corresponding
4083 -- record, to match the proper signature of an overriding operation.
4084 -- Same processing for an access parameter whose designated type is
4085 -- derived from a synchronized interface.
4087 if Ada_Version >= Ada_2005 then
4090 Formal_Typ : Entity_Id;
4091 Rec_Typ : Entity_Id;
4092 Desig_Typ : Entity_Id;
4095 Formal := First_Formal (Designator);
4096 while Present (Formal) loop
4097 Formal_Typ := Etype (Formal);
4099 if Is_Concurrent_Type (Formal_Typ)
4100 and then Present (Corresponding_Record_Type (Formal_Typ))
4102 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4104 if Present (Interfaces (Rec_Typ)) then
4105 Set_Etype (Formal, Rec_Typ);
4108 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4109 Desig_Typ := Designated_Type (Formal_Typ);
4111 if Is_Concurrent_Type (Desig_Typ)
4112 and then Present (Corresponding_Record_Type (Desig_Typ))
4114 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4116 if Present (Interfaces (Rec_Typ)) then
4117 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4122 Next_Formal (Formal);
4129 -- The subprogram scope is pushed and popped around the processing of
4130 -- the return type for consistency with call above to Process_Formals
4131 -- (which itself can call Analyze_Return_Type), and to ensure that any
4132 -- itype created for the return type will be associated with the proper
4135 elsif Nkind (N) = N_Function_Specification then
4136 Push_Scope (Designator);
4137 Analyze_Return_Type (N);
4143 if Nkind (N) = N_Function_Specification then
4145 -- Deal with operator symbol case
4147 if Nkind (Designator) = N_Defining_Operator_Symbol then
4148 Valid_Operator_Definition (Designator);
4151 May_Need_Actuals (Designator);
4153 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4154 -- the subprogram is abstract also. This does not apply to renaming
4155 -- declarations, where abstractness is inherited, and to subprogram
4156 -- bodies generated for stream operations, which become renamings as
4159 -- In case of primitives associated with abstract interface types
4160 -- the check is applied later (see Analyze_Subprogram_Declaration).
4162 if not Nkind_In (Original_Node (Parent (N)),
4163 N_Subprogram_Renaming_Declaration,
4164 N_Abstract_Subprogram_Declaration,
4165 N_Formal_Abstract_Subprogram_Declaration)
4167 if Is_Abstract_Type (Etype (Designator))
4168 and then not Is_Interface (Etype (Designator))
4171 ("function that returns abstract type must be abstract", N);
4173 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4174 -- access result whose designated type is abstract.
4176 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4178 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4179 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4180 and then Ada_Version >= Ada_2012
4182 Error_Msg_N ("function whose access result designates "
4183 & "abstract type must be abstract", N);
4189 end Analyze_Subprogram_Specification;
4191 --------------------------
4192 -- Build_Body_To_Inline --
4193 --------------------------
4195 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
4196 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
4197 Original_Body : Node_Id;
4198 Body_To_Analyze : Node_Id;
4199 Max_Size : constant := 10;
4200 Stat_Count : Integer := 0;
4202 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4203 -- Check for declarations that make inlining not worthwhile
4205 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4206 -- Check for statements that make inlining not worthwhile: any tasking
4207 -- statement, nested at any level. Keep track of total number of
4208 -- elementary statements, as a measure of acceptable size.
4210 function Has_Pending_Instantiation return Boolean;
4211 -- If some enclosing body contains instantiations that appear before the
4212 -- corresponding generic body, the enclosing body has a freeze node so
4213 -- that it can be elaborated after the generic itself. This might
4214 -- conflict with subsequent inlinings, so that it is unsafe to try to
4215 -- inline in such a case.
4217 function Has_Single_Return return Boolean;
4218 -- In general we cannot inline functions that return unconstrained type.
4219 -- However, we can handle such functions if all return statements return
4220 -- a local variable that is the only declaration in the body of the
4221 -- function. In that case the call can be replaced by that local
4222 -- variable as is done for other inlined calls.
4224 procedure Remove_Pragmas;
4225 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
4226 -- parameter has no meaning when the body is inlined and the formals
4227 -- are rewritten. Remove it from body to inline. The analysis of the
4228 -- non-inlined body will handle the pragma properly.
4230 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
4231 -- If the body of the subprogram includes a call that returns an
4232 -- unconstrained type, the secondary stack is involved, and it
4233 -- is not worth inlining.
4235 ------------------------------
4236 -- Has_Excluded_Declaration --
4237 ------------------------------
4239 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4242 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4243 -- Nested subprograms make a given body ineligible for inlining, but
4244 -- we make an exception for instantiations of unchecked conversion.
4245 -- The body has not been analyzed yet, so check the name, and verify
4246 -- that the visible entity with that name is the predefined unit.
4248 -----------------------------
4249 -- Is_Unchecked_Conversion --
4250 -----------------------------
4252 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4253 Id : constant Node_Id := Name (D);
4257 if Nkind (Id) = N_Identifier
4258 and then Chars (Id) = Name_Unchecked_Conversion
4260 Conv := Current_Entity (Id);
4262 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4263 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4265 Conv := Current_Entity (Selector_Name (Id));
4270 return Present (Conv)
4271 and then Is_Predefined_File_Name
4272 (Unit_File_Name (Get_Source_Unit (Conv)))
4273 and then Is_Intrinsic_Subprogram (Conv);
4274 end Is_Unchecked_Conversion;
4276 -- Start of processing for Has_Excluded_Declaration
4280 while Present (D) loop
4281 if (Nkind (D) = N_Function_Instantiation
4282 and then not Is_Unchecked_Conversion (D))
4283 or else Nkind_In (D, N_Protected_Type_Declaration,
4284 N_Package_Declaration,
4285 N_Package_Instantiation,
4287 N_Procedure_Instantiation,
4288 N_Task_Type_Declaration)
4291 ("cannot inline & (non-allowed declaration)?", D, Subp);
4299 end Has_Excluded_Declaration;
4301 ----------------------------
4302 -- Has_Excluded_Statement --
4303 ----------------------------
4305 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4311 while Present (S) loop
4312 Stat_Count := Stat_Count + 1;
4314 if Nkind_In (S, N_Abort_Statement,
4315 N_Asynchronous_Select,
4316 N_Conditional_Entry_Call,
4317 N_Delay_Relative_Statement,
4318 N_Delay_Until_Statement,
4323 ("cannot inline & (non-allowed statement)?", S, Subp);
4326 elsif Nkind (S) = N_Block_Statement then
4327 if Present (Declarations (S))
4328 and then Has_Excluded_Declaration (Declarations (S))
4332 elsif Present (Handled_Statement_Sequence (S))
4335 (Exception_Handlers (Handled_Statement_Sequence (S)))
4337 Has_Excluded_Statement
4338 (Statements (Handled_Statement_Sequence (S))))
4343 elsif Nkind (S) = N_Case_Statement then
4344 E := First (Alternatives (S));
4345 while Present (E) loop
4346 if Has_Excluded_Statement (Statements (E)) then
4353 elsif Nkind (S) = N_If_Statement then
4354 if Has_Excluded_Statement (Then_Statements (S)) then
4358 if Present (Elsif_Parts (S)) then
4359 E := First (Elsif_Parts (S));
4360 while Present (E) loop
4361 if Has_Excluded_Statement (Then_Statements (E)) then
4369 if Present (Else_Statements (S))
4370 and then Has_Excluded_Statement (Else_Statements (S))
4375 elsif Nkind (S) = N_Loop_Statement
4376 and then Has_Excluded_Statement (Statements (S))
4380 elsif Nkind (S) = N_Extended_Return_Statement then
4381 if Has_Excluded_Statement
4382 (Statements (Handled_Statement_Sequence (S)))
4384 (Exception_Handlers (Handled_Statement_Sequence (S)))
4394 end Has_Excluded_Statement;
4396 -------------------------------
4397 -- Has_Pending_Instantiation --
4398 -------------------------------
4400 function Has_Pending_Instantiation return Boolean is
4405 while Present (S) loop
4406 if Is_Compilation_Unit (S)
4407 or else Is_Child_Unit (S)
4411 elsif Ekind (S) = E_Package
4412 and then Has_Forward_Instantiation (S)
4421 end Has_Pending_Instantiation;
4423 ------------------------
4424 -- Has_Single_Return --
4425 ------------------------
4427 function Has_Single_Return return Boolean is
4428 Return_Statement : Node_Id := Empty;
4430 function Check_Return (N : Node_Id) return Traverse_Result;
4436 function Check_Return (N : Node_Id) return Traverse_Result is
4438 if Nkind (N) = N_Simple_Return_Statement then
4439 if Present (Expression (N))
4440 and then Is_Entity_Name (Expression (N))
4442 if No (Return_Statement) then
4443 Return_Statement := N;
4446 elsif Chars (Expression (N)) =
4447 Chars (Expression (Return_Statement))
4455 -- A return statement within an extended return is a noop
4458 elsif No (Expression (N))
4459 and then Nkind (Parent (Parent (N))) =
4460 N_Extended_Return_Statement
4465 -- Expression has wrong form
4470 -- We can only inline a build-in-place function if
4471 -- it has a single extended return.
4473 elsif Nkind (N) = N_Extended_Return_Statement then
4474 if No (Return_Statement) then
4475 Return_Statement := N;
4487 function Check_All_Returns is new Traverse_Func (Check_Return);
4489 -- Start of processing for Has_Single_Return
4492 if Check_All_Returns (N) /= OK then
4495 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4499 return Present (Declarations (N))
4500 and then Present (First (Declarations (N)))
4501 and then Chars (Expression (Return_Statement)) =
4502 Chars (Defining_Identifier (First (Declarations (N))));
4504 end Has_Single_Return;
4506 --------------------
4507 -- Remove_Pragmas --
4508 --------------------
4510 procedure Remove_Pragmas is
4515 Decl := First (Declarations (Body_To_Analyze));
4516 while Present (Decl) loop
4519 if Nkind (Decl) = N_Pragma
4520 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4530 --------------------------
4531 -- Uses_Secondary_Stack --
4532 --------------------------
4534 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4535 function Check_Call (N : Node_Id) return Traverse_Result;
4536 -- Look for function calls that return an unconstrained type
4542 function Check_Call (N : Node_Id) return Traverse_Result is
4544 if Nkind (N) = N_Function_Call
4545 and then Is_Entity_Name (Name (N))
4546 and then Is_Composite_Type (Etype (Entity (Name (N))))
4547 and then not Is_Constrained (Etype (Entity (Name (N))))
4550 ("cannot inline & (call returns unconstrained type)?",
4558 function Check_Calls is new Traverse_Func (Check_Call);
4561 return Check_Calls (Bod) = Abandon;
4562 end Uses_Secondary_Stack;
4564 -- Start of processing for Build_Body_To_Inline
4567 -- Return immediately if done already
4569 if Nkind (Decl) = N_Subprogram_Declaration
4570 and then Present (Body_To_Inline (Decl))
4574 -- Functions that return unconstrained composite types require
4575 -- secondary stack handling, and cannot currently be inlined, unless
4576 -- all return statements return a local variable that is the first
4577 -- local declaration in the body.
4579 elsif Ekind (Subp) = E_Function
4580 and then not Is_Scalar_Type (Etype (Subp))
4581 and then not Is_Access_Type (Etype (Subp))
4582 and then not Is_Constrained (Etype (Subp))
4584 if not Has_Single_Return then
4586 ("cannot inline & (unconstrained return type)?", N, Subp);
4590 -- Ditto for functions that return controlled types, where controlled
4591 -- actions interfere in complex ways with inlining.
4593 elsif Ekind (Subp) = E_Function
4594 and then Needs_Finalization (Etype (Subp))
4597 ("cannot inline & (controlled return type)?", N, Subp);
4601 if Present (Declarations (N))
4602 and then Has_Excluded_Declaration (Declarations (N))
4607 if Present (Handled_Statement_Sequence (N)) then
4608 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4610 ("cannot inline& (exception handler)?",
4611 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4615 Has_Excluded_Statement
4616 (Statements (Handled_Statement_Sequence (N)))
4622 -- We do not inline a subprogram that is too large, unless it is
4623 -- marked Inline_Always. This pragma does not suppress the other
4624 -- checks on inlining (forbidden declarations, handlers, etc).
4626 if Stat_Count > Max_Size
4627 and then not Has_Pragma_Inline_Always (Subp)
4629 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4633 if Has_Pending_Instantiation then
4635 ("cannot inline& (forward instance within enclosing body)?",
4640 -- Within an instance, the body to inline must be treated as a nested
4641 -- generic, so that the proper global references are preserved.
4643 -- Note that we do not do this at the library level, because it is not
4644 -- needed, and furthermore this causes trouble if front end inlining
4645 -- is activated (-gnatN).
4647 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4648 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4649 Original_Body := Copy_Generic_Node (N, Empty, True);
4651 Original_Body := Copy_Separate_Tree (N);
4654 -- We need to capture references to the formals in order to substitute
4655 -- the actuals at the point of inlining, i.e. instantiation. To treat
4656 -- the formals as globals to the body to inline, we nest it within
4657 -- a dummy parameterless subprogram, declared within the real one.
4658 -- To avoid generating an internal name (which is never public, and
4659 -- which affects serial numbers of other generated names), we use
4660 -- an internal symbol that cannot conflict with user declarations.
4662 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4663 Set_Defining_Unit_Name
4664 (Specification (Original_Body),
4665 Make_Defining_Identifier (Sloc (N), Name_uParent));
4666 Set_Corresponding_Spec (Original_Body, Empty);
4668 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4670 -- Set return type of function, which is also global and does not need
4673 if Ekind (Subp) = E_Function then
4674 Set_Result_Definition (Specification (Body_To_Analyze),
4675 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4678 if No (Declarations (N)) then
4679 Set_Declarations (N, New_List (Body_To_Analyze));
4681 Append (Body_To_Analyze, Declarations (N));
4684 Expander_Mode_Save_And_Set (False);
4687 Analyze (Body_To_Analyze);
4688 Push_Scope (Defining_Entity (Body_To_Analyze));
4689 Save_Global_References (Original_Body);
4691 Remove (Body_To_Analyze);
4693 Expander_Mode_Restore;
4695 -- Restore environment if previously saved
4697 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4701 -- If secondary stk used there is no point in inlining. We have
4702 -- already issued the warning in this case, so nothing to do.
4704 if Uses_Secondary_Stack (Body_To_Analyze) then
4708 Set_Body_To_Inline (Decl, Original_Body);
4709 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4710 Set_Is_Inlined (Subp);
4711 end Build_Body_To_Inline;
4717 procedure Cannot_Inline
4721 Is_Serious : Boolean := False)
4724 pragma Assert (Msg (Msg'Last) = '?');
4728 if not Debug_Flag_Dot_K then
4730 -- Do not emit warning if this is a predefined unit which is not
4731 -- the main unit. With validity checks enabled, some predefined
4732 -- subprograms may contain nested subprograms and become ineligible
4735 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4736 and then not In_Extended_Main_Source_Unit (Subp)
4740 elsif Has_Pragma_Inline_Always (Subp) then
4742 -- Remove last character (question mark) to make this into an
4743 -- error, because the Inline_Always pragma cannot be obeyed.
4745 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4747 elsif Ineffective_Inline_Warnings then
4748 Error_Msg_NE (Msg & "p?", N, Subp);
4755 elsif Is_Serious then
4757 -- Remove last character (question mark) to make this into an error.
4759 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4761 elsif Optimization_Level = 0 then
4763 -- Do not emit warning if this is a predefined unit which is not
4764 -- the main unit. This behavior is currently provided for backward
4765 -- compatibility but it will be removed when we enforce the
4766 -- strictness of the new rules.
4768 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4769 and then not In_Extended_Main_Source_Unit (Subp)
4773 elsif Has_Pragma_Inline_Always (Subp) then
4775 -- Emit a warning if this is a call to a runtime subprogram
4776 -- which is located inside a generic. Previously this call
4777 -- was silently skipped.
4779 if Is_Generic_Instance (Subp) then
4781 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4783 if Is_Predefined_File_Name
4784 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4786 Set_Is_Inlined (Subp, False);
4787 Error_Msg_NE (Msg & "p?", N, Subp);
4793 -- Remove last character (question mark) to make this into an
4794 -- error, because the Inline_Always pragma cannot be obeyed.
4796 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4798 else pragma Assert (Front_End_Inlining);
4799 Set_Is_Inlined (Subp, False);
4801 -- When inlining cannot take place we must issue an error.
4802 -- For backward compatibility we still report a warning.
4804 if Ineffective_Inline_Warnings then
4805 Error_Msg_NE (Msg & "p?", N, Subp);
4809 -- Compiling with optimizations enabled it is too early to report
4810 -- problems since the backend may still perform inlining. In order
4811 -- to report unhandled inlinings the program must be compiled with
4812 -- -Winline and the error is reported by the backend.
4819 ------------------------------------
4820 -- Check_And_Build_Body_To_Inline --
4821 ------------------------------------
4823 procedure Check_And_Build_Body_To_Inline
4825 Spec_Id : Entity_Id;
4826 Body_Id : Entity_Id)
4828 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4829 -- Use generic machinery to build an unexpanded body for the subprogram.
4830 -- This body is subsequently used for inline expansions at call sites.
4832 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4833 -- Return true if we generate code for the function body N, the function
4834 -- body N has no local declarations and its unique statement is a single
4835 -- extended return statement with a handled statements sequence.
4837 function Check_Body_To_Inline
4839 Subp : Entity_Id) return Boolean;
4840 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4841 -- inlined by the frontend. These are the rules:
4842 -- * At -O0 use fe inlining when inline_always is specified except if
4843 -- the function returns a controlled type.
4844 -- * At other optimization levels use the fe inlining for both inline
4845 -- and inline_always in the following cases:
4846 -- - function returning a known at compile time constant
4847 -- - function returning a call to an intrinsic function
4848 -- - function returning an unconstrained type (see Can_Split
4849 -- Unconstrained_Function).
4850 -- - function returning a call to a frontend-inlined function
4851 -- Use the back-end mechanism otherwise
4853 -- In addition, in the following cases the function cannot be inlined by
4855 -- - functions that uses the secondary stack
4856 -- - functions that have declarations of:
4857 -- - Concurrent types
4861 -- - functions that have some of the following statements:
4863 -- - asynchronous-select
4864 -- - conditional-entry-call
4867 -- - selective-accept
4868 -- - timed-entry-call
4869 -- - functions that have exception handlers
4870 -- - functions that have some enclosing body containing instantiations
4871 -- that appear before the corresponding generic body.
4873 procedure Generate_Body_To_Inline
4875 Body_To_Inline : out Node_Id);
4876 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4877 -- of pragmas referencing the formals are removed since they have no
4878 -- meaning when the body is inlined and the formals are rewritten (the
4879 -- analysis of the non-inlined body will handle these pragmas properly).
4880 -- A new internal name is associated with Body_To_Inline.
4882 procedure Split_Unconstrained_Function
4884 Spec_Id : Entity_Id);
4885 -- N is an inlined function body that returns an unconstrained type and
4886 -- has a single extended return statement. Split N in two subprograms:
4887 -- a procedure P' and a function F'. The formals of P' duplicate the
4888 -- formals of N plus an extra formal which is used return a value;
4889 -- its body is composed by the declarations and list of statements
4890 -- of the extended return statement of N.
4892 --------------------------
4893 -- Build_Body_To_Inline --
4894 --------------------------
4896 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4897 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4898 Original_Body : Node_Id;
4899 Body_To_Analyze : Node_Id;
4902 pragma Assert (Current_Scope = Spec_Id);
4904 -- Within an instance, the body to inline must be treated as a nested
4905 -- generic, so that the proper global references are preserved. We
4906 -- do not do this at the library level, because it is not needed, and
4907 -- furthermore this causes trouble if front end inlining is activated
4911 and then Scope (Current_Scope) /= Standard_Standard
4913 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4916 -- We need to capture references to the formals in order
4917 -- to substitute the actuals at the point of inlining, i.e.
4918 -- instantiation. To treat the formals as globals to the body to
4919 -- inline, we nest it within a dummy parameterless subprogram,
4920 -- declared within the real one.
4922 Generate_Body_To_Inline (N, Original_Body);
4923 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4925 -- Set return type of function, which is also global and does not
4926 -- need to be resolved.
4928 if Ekind (Spec_Id) = E_Function then
4929 Set_Result_Definition (Specification (Body_To_Analyze),
4930 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4933 if No (Declarations (N)) then
4934 Set_Declarations (N, New_List (Body_To_Analyze));
4936 Append_To (Declarations (N), Body_To_Analyze);
4939 Preanalyze (Body_To_Analyze);
4941 Push_Scope (Defining_Entity (Body_To_Analyze));
4942 Save_Global_References (Original_Body);
4944 Remove (Body_To_Analyze);
4946 -- Restore environment if previously saved
4949 and then Scope (Current_Scope) /= Standard_Standard
4954 pragma Assert (No (Body_To_Inline (Decl)));
4955 Set_Body_To_Inline (Decl, Original_Body);
4956 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4957 end Build_Body_To_Inline;
4959 --------------------------
4960 -- Check_Body_To_Inline --
4961 --------------------------
4963 function Check_Body_To_Inline
4965 Subp : Entity_Id) return Boolean
4967 Max_Size : constant := 10;
4968 Stat_Count : Integer := 0;
4970 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4971 -- Check for declarations that make inlining not worthwhile
4973 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4974 -- Check for statements that make inlining not worthwhile: any
4975 -- tasking statement, nested at any level. Keep track of total
4976 -- number of elementary statements, as a measure of acceptable size.
4978 function Has_Pending_Instantiation return Boolean;
4979 -- Return True if some enclosing body contains instantiations that
4980 -- appear before the corresponding generic body.
4982 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4983 -- Return True if all the return statements of the function body N
4984 -- are simple return statements and return a compile time constant
4986 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4987 -- Return True if all the return statements of the function body N
4988 -- are simple return statements and return an intrinsic function call
4990 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4991 -- If the body of the subprogram includes a call that returns an
4992 -- unconstrained type, the secondary stack is involved, and it
4993 -- is not worth inlining.
4995 ------------------------------
4996 -- Has_Excluded_Declaration --
4997 ------------------------------
4999 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
5002 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
5003 -- Nested subprograms make a given body ineligible for inlining,
5004 -- but we make an exception for instantiations of unchecked
5005 -- conversion. The body has not been analyzed yet, so check the
5006 -- name, and verify that the visible entity with that name is the
5009 -----------------------------
5010 -- Is_Unchecked_Conversion --
5011 -----------------------------
5013 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
5014 Id : constant Node_Id := Name (D);
5018 if Nkind (Id) = N_Identifier
5019 and then Chars (Id) = Name_Unchecked_Conversion
5021 Conv := Current_Entity (Id);
5023 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
5025 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
5027 Conv := Current_Entity (Selector_Name (Id));
5032 return Present (Conv)
5033 and then Is_Predefined_File_Name
5034 (Unit_File_Name (Get_Source_Unit (Conv)))
5035 and then Is_Intrinsic_Subprogram (Conv);
5036 end Is_Unchecked_Conversion;
5038 -- Start of processing for Has_Excluded_Declaration
5042 while Present (D) loop
5043 if (Nkind (D) = N_Function_Instantiation
5044 and then not Is_Unchecked_Conversion (D))
5045 or else Nkind_In (D, N_Protected_Type_Declaration,
5046 N_Package_Declaration,
5047 N_Package_Instantiation,
5049 N_Procedure_Instantiation,
5050 N_Task_Type_Declaration)
5053 ("cannot inline & (non-allowed declaration)?", D, Subp);
5062 end Has_Excluded_Declaration;
5064 ----------------------------
5065 -- Has_Excluded_Statement --
5066 ----------------------------
5068 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
5074 while Present (S) loop
5075 Stat_Count := Stat_Count + 1;
5077 if Nkind_In (S, N_Abort_Statement,
5078 N_Asynchronous_Select,
5079 N_Conditional_Entry_Call,
5080 N_Delay_Relative_Statement,
5081 N_Delay_Until_Statement,
5086 ("cannot inline & (non-allowed statement)?", S, Subp);
5089 elsif Nkind (S) = N_Block_Statement then
5090 if Present (Declarations (S))
5091 and then Has_Excluded_Declaration (Declarations (S))
5095 elsif Present (Handled_Statement_Sequence (S)) then
5097 (Exception_Handlers (Handled_Statement_Sequence (S)))
5100 ("cannot inline& (exception handler)?",
5101 First (Exception_Handlers
5102 (Handled_Statement_Sequence (S))),
5106 elsif Has_Excluded_Statement
5107 (Statements (Handled_Statement_Sequence (S)))
5113 elsif Nkind (S) = N_Case_Statement then
5114 E := First (Alternatives (S));
5115 while Present (E) loop
5116 if Has_Excluded_Statement (Statements (E)) then
5123 elsif Nkind (S) = N_If_Statement then
5124 if Has_Excluded_Statement (Then_Statements (S)) then
5128 if Present (Elsif_Parts (S)) then
5129 E := First (Elsif_Parts (S));
5130 while Present (E) loop
5131 if Has_Excluded_Statement (Then_Statements (E)) then
5138 if Present (Else_Statements (S))
5139 and then Has_Excluded_Statement (Else_Statements (S))
5144 elsif Nkind (S) = N_Loop_Statement
5145 and then Has_Excluded_Statement (Statements (S))
5149 elsif Nkind (S) = N_Extended_Return_Statement then
5150 if Present (Handled_Statement_Sequence (S))
5152 Has_Excluded_Statement
5153 (Statements (Handled_Statement_Sequence (S)))
5157 elsif Present (Handled_Statement_Sequence (S))
5159 Present (Exception_Handlers
5160 (Handled_Statement_Sequence (S)))
5163 ("cannot inline& (exception handler)?",
5164 First (Exception_Handlers
5165 (Handled_Statement_Sequence (S))),
5175 end Has_Excluded_Statement;
5177 -------------------------------
5178 -- Has_Pending_Instantiation --
5179 -------------------------------
5181 function Has_Pending_Instantiation return Boolean is
5186 while Present (S) loop
5187 if Is_Compilation_Unit (S)
5188 or else Is_Child_Unit (S)
5192 elsif Ekind (S) = E_Package
5193 and then Has_Forward_Instantiation (S)
5202 end Has_Pending_Instantiation;
5204 ------------------------------------
5205 -- Returns_Compile_Time_Constant --
5206 ------------------------------------
5208 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
5210 function Check_Return (N : Node_Id) return Traverse_Result;
5216 function Check_Return (N : Node_Id) return Traverse_Result is
5218 if Nkind (N) = N_Extended_Return_Statement then
5221 elsif Nkind (N) = N_Simple_Return_Statement then
5222 if Present (Expression (N)) then
5224 Orig_Expr : constant Node_Id :=
5225 Original_Node (Expression (N));
5228 if Nkind_In (Orig_Expr, N_Integer_Literal,
5230 N_Character_Literal)
5234 elsif Is_Entity_Name (Orig_Expr)
5235 and then Ekind (Entity (Orig_Expr)) = E_Constant
5236 and then Is_Static_Expression (Orig_Expr)
5244 -- Expression has wrong form
5250 -- Continue analyzing statements
5257 function Check_All_Returns is new Traverse_Func (Check_Return);
5259 -- Start of processing for Returns_Compile_Time_Constant
5262 return Check_All_Returns (N) = OK;
5263 end Returns_Compile_Time_Constant;
5265 --------------------------------------
5266 -- Returns_Intrinsic_Function_Call --
5267 --------------------------------------
5269 function Returns_Intrinsic_Function_Call
5270 (N : Node_Id) return Boolean
5272 function Check_Return (N : Node_Id) return Traverse_Result;
5278 function Check_Return (N : Node_Id) return Traverse_Result is
5280 if Nkind (N) = N_Extended_Return_Statement then
5283 elsif Nkind (N) = N_Simple_Return_Statement then
5284 if Present (Expression (N)) then
5286 Orig_Expr : constant Node_Id :=
5287 Original_Node (Expression (N));
5290 if Nkind (Orig_Expr) in N_Op
5291 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5295 elsif Nkind (Orig_Expr) in N_Has_Entity
5296 and then Present (Entity (Orig_Expr))
5297 and then Ekind (Entity (Orig_Expr)) = E_Function
5298 and then Is_Inlined (Entity (Orig_Expr))
5302 elsif Nkind (Orig_Expr) in N_Has_Entity
5303 and then Present (Entity (Orig_Expr))
5304 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5313 -- Expression has wrong form
5319 -- Continue analyzing statements
5326 function Check_All_Returns is new Traverse_Func (Check_Return);
5328 -- Start of processing for Returns_Intrinsic_Function_Call
5331 return Check_All_Returns (N) = OK;
5332 end Returns_Intrinsic_Function_Call;
5334 --------------------------
5335 -- Uses_Secondary_Stack --
5336 --------------------------
5338 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5340 function Check_Call (N : Node_Id) return Traverse_Result;
5341 -- Look for function calls that return an unconstrained type
5347 function Check_Call (N : Node_Id) return Traverse_Result is
5349 if Nkind (N) = N_Function_Call
5350 and then Is_Entity_Name (Name (N))
5351 and then Is_Composite_Type (Etype (Entity (Name (N))))
5352 and then not Is_Constrained (Etype (Entity (Name (N))))
5355 ("cannot inline & (call returns unconstrained type)?",
5364 function Check_Calls is new Traverse_Func (Check_Call);
5366 -- Start of processing for Uses_Secondary_Stack
5369 return Check_Calls (N) = Abandon;
5370 end Uses_Secondary_Stack;
5374 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5375 May_Inline : constant Boolean :=
5376 Has_Pragma_Inline_Always (Spec_Id)
5377 or else (Has_Pragma_Inline (Spec_Id)
5378 and then ((Optimization_Level > 0
5379 and then Ekind (Spec_Id)
5381 or else Front_End_Inlining));
5382 Body_To_Analyze : Node_Id;
5384 -- Start of processing for Check_Body_To_Inline
5387 -- No action needed in stubs since the attribute Body_To_Inline
5390 if Nkind (Decl) = N_Subprogram_Body_Stub then
5393 -- Cannot build the body to inline if the attribute is already set.
5394 -- This attribute may have been set if this is a subprogram renaming
5395 -- declarations (see Freeze.Build_Renamed_Body).
5397 elsif Present (Body_To_Inline (Decl)) then
5400 -- No action needed if the subprogram does not fulfill the minimum
5401 -- conditions to be inlined by the frontend
5403 elsif not May_Inline then
5407 -- Check excluded declarations
5409 if Present (Declarations (N))
5410 and then Has_Excluded_Declaration (Declarations (N))
5415 -- Check excluded statements
5417 if Present (Handled_Statement_Sequence (N)) then
5419 (Exception_Handlers (Handled_Statement_Sequence (N)))
5422 ("cannot inline& (exception handler)?",
5424 (Exception_Handlers (Handled_Statement_Sequence (N))),
5429 elsif Has_Excluded_Statement
5430 (Statements (Handled_Statement_Sequence (N)))
5436 -- For backward compatibility, compiling under -gnatN we do not
5437 -- inline a subprogram that is too large, unless it is marked
5438 -- Inline_Always. This pragma does not suppress the other checks
5439 -- on inlining (forbidden declarations, handlers, etc).
5441 if Front_End_Inlining
5442 and then not Has_Pragma_Inline_Always (Subp)
5443 and then Stat_Count > Max_Size
5445 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5449 -- If some enclosing body contains instantiations that appear before
5450 -- the corresponding generic body, the enclosing body has a freeze
5451 -- node so that it can be elaborated after the generic itself. This
5452 -- might conflict with subsequent inlinings, so that it is unsafe to
5453 -- try to inline in such a case.
5455 if Has_Pending_Instantiation then
5457 ("cannot inline& (forward instance within enclosing body)?",
5463 -- Generate and preanalyze the body to inline (needed to perform
5464 -- the rest of the checks)
5466 Generate_Body_To_Inline (N, Body_To_Analyze);
5468 if Ekind (Subp) = E_Function then
5469 Set_Result_Definition (Specification (Body_To_Analyze),
5470 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5473 -- Nest the body to analyze within the real one
5475 if No (Declarations (N)) then
5476 Set_Declarations (N, New_List (Body_To_Analyze));
5478 Append_To (Declarations (N), Body_To_Analyze);
5481 Preanalyze (Body_To_Analyze);
5482 Remove (Body_To_Analyze);
5484 -- Keep separate checks needed when compiling without optimizations
5486 if Optimization_Level = 0
5488 -- AAMP and VM targets have no support for inlining in the backend
5489 -- and hence we use frontend inlining at all optimization levels.
5491 or else AAMP_On_Target
5492 or else VM_Target /= No_VM
5494 -- Cannot inline functions whose body has a call that returns an
5495 -- unconstrained type since the secondary stack is involved, and
5496 -- it is not worth inlining.
5498 if Uses_Secondary_Stack (Body_To_Analyze) then
5501 -- Cannot inline functions that return controlled types since
5502 -- controlled actions interfere in complex ways with inlining.
5504 elsif Ekind (Subp) = E_Function
5505 and then Needs_Finalization (Etype (Subp))
5508 ("cannot inline & (controlled return type)?", N, Subp);
5511 elsif Returns_Unconstrained_Type (Subp) then
5513 ("cannot inline & (unconstrained return type)?", N, Subp);
5517 -- Compiling with optimizations enabled
5520 -- Procedures are never frontend inlined in this case
5522 if Ekind (Subp) /= E_Function then
5525 -- Functions returning unconstrained types are tested
5526 -- separately (see Can_Split_Unconstrained_Function).
5528 elsif Returns_Unconstrained_Type (Subp) then
5531 -- Check supported cases
5533 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5534 and then Convention (Subp) /= Convention_Intrinsic
5535 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5542 end Check_Body_To_Inline;
5544 --------------------------------------
5545 -- Can_Split_Unconstrained_Function --
5546 --------------------------------------
5548 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5550 Ret_Node : constant Node_Id :=
5551 First (Statements (Handled_Statement_Sequence (N)));
5555 -- No user defined declarations allowed in the function except inside
5556 -- the unique return statement; implicit labels are the only allowed
5559 if not Is_Empty_List (Declarations (N)) then
5560 D := First (Declarations (N));
5561 while Present (D) loop
5562 if Nkind (D) /= N_Implicit_Label_Declaration then
5570 -- We only split the inlined function when we are generating the code
5571 -- of its body; otherwise we leave duplicated split subprograms in
5572 -- the tree which (if referenced) generate wrong references at link
5575 return In_Extended_Main_Code_Unit (N)
5576 and then Present (Ret_Node)
5577 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5578 and then No (Next (Ret_Node))
5579 and then Present (Handled_Statement_Sequence (Ret_Node));
5580 end Can_Split_Unconstrained_Function;
5582 -----------------------------
5583 -- Generate_Body_To_Inline --
5584 -----------------------------
5586 procedure Generate_Body_To_Inline
5588 Body_To_Inline : out Node_Id)
5590 procedure Remove_Pragmas (N : Node_Id);
5591 -- Remove occurrences of pragmas that may reference the formals of
5592 -- N. The analysis of the non-inlined body will handle these pragmas
5595 --------------------
5596 -- Remove_Pragmas --
5597 --------------------
5599 procedure Remove_Pragmas (N : Node_Id) is
5604 Decl := First (Declarations (N));
5605 while Present (Decl) loop
5608 if Nkind (Decl) = N_Pragma
5609 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5619 -- Start of processing for Generate_Body_To_Inline
5622 -- Within an instance, the body to inline must be treated as a nested
5623 -- generic, so that the proper global references are preserved.
5625 -- Note that we do not do this at the library level, because it
5626 -- is not needed, and furthermore this causes trouble if front
5627 -- end inlining is activated (-gnatN).
5630 and then Scope (Current_Scope) /= Standard_Standard
5632 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5634 Body_To_Inline := Copy_Separate_Tree (N);
5637 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5638 -- parameter has no meaning when the body is inlined and the formals
5639 -- are rewritten. Remove it from body to inline. The analysis of the
5640 -- non-inlined body will handle the pragma properly.
5642 Remove_Pragmas (Body_To_Inline);
5644 -- We need to capture references to the formals in order
5645 -- to substitute the actuals at the point of inlining, i.e.
5646 -- instantiation. To treat the formals as globals to the body to
5647 -- inline, we nest it within a dummy parameterless subprogram,
5648 -- declared within the real one.
5650 Set_Parameter_Specifications
5651 (Specification (Body_To_Inline), No_List);
5653 -- A new internal name is associated with Body_To_Inline to avoid
5654 -- conflicts when the non-inlined body N is analyzed.
5656 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5657 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5658 Set_Corresponding_Spec (Body_To_Inline, Empty);
5659 end Generate_Body_To_Inline;
5661 ----------------------------------
5662 -- Split_Unconstrained_Function --
5663 ----------------------------------
5665 procedure Split_Unconstrained_Function
5667 Spec_Id : Entity_Id)
5669 Loc : constant Source_Ptr := Sloc (N);
5670 Ret_Node : constant Node_Id :=
5671 First (Statements (Handled_Statement_Sequence (N)));
5672 Ret_Obj : constant Node_Id :=
5673 First (Return_Object_Declarations (Ret_Node));
5675 procedure Build_Procedure
5676 (Proc_Id : out Entity_Id;
5677 Decl_List : out List_Id);
5678 -- Build a procedure containing the statements found in the extended
5679 -- return statement of the unconstrained function body N.
5681 procedure Build_Procedure
5682 (Proc_Id : out Entity_Id;
5683 Decl_List : out List_Id)
5686 Formal_List : constant List_Id := New_List;
5687 Proc_Spec : Node_Id;
5688 Proc_Body : Node_Id;
5689 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5690 Body_Decl_List : List_Id := No_List;
5691 Param_Type : Node_Id;
5694 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5695 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5698 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5701 Append_To (Formal_List,
5702 Make_Parameter_Specification (Loc,
5703 Defining_Identifier =>
5704 Make_Defining_Identifier (Loc,
5705 Chars => Chars (Defining_Identifier (Ret_Obj))),
5706 In_Present => False,
5707 Out_Present => True,
5708 Null_Exclusion_Present => False,
5709 Parameter_Type => Param_Type));
5711 Formal := First_Formal (Spec_Id);
5712 while Present (Formal) loop
5713 Append_To (Formal_List,
5714 Make_Parameter_Specification (Loc,
5715 Defining_Identifier =>
5716 Make_Defining_Identifier (Sloc (Formal),
5717 Chars => Chars (Formal)),
5718 In_Present => In_Present (Parent (Formal)),
5719 Out_Present => Out_Present (Parent (Formal)),
5720 Null_Exclusion_Present =>
5721 Null_Exclusion_Present (Parent (Formal)),
5723 New_Occurrence_Of (Etype (Formal), Loc),
5725 Copy_Separate_Tree (Expression (Parent (Formal)))));
5727 Next_Formal (Formal);
5731 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5734 Make_Procedure_Specification (Loc,
5735 Defining_Unit_Name => Proc_Id,
5736 Parameter_Specifications => Formal_List);
5738 Decl_List := New_List;
5740 Append_To (Decl_List,
5741 Make_Subprogram_Declaration (Loc, Proc_Spec));
5743 -- Can_Convert_Unconstrained_Function checked that the function
5744 -- has no local declarations except implicit label declarations.
5745 -- Copy these declarations to the built procedure.
5747 if Present (Declarations (N)) then
5748 Body_Decl_List := New_List;
5755 D := First (Declarations (N));
5756 while Present (D) loop
5757 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5760 Make_Implicit_Label_Declaration (Loc,
5761 Make_Defining_Identifier (Loc,
5762 Chars => Chars (Defining_Identifier (D))),
5763 Label_Construct => Empty);
5764 Append_To (Body_Decl_List, New_D);
5771 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5774 Make_Subprogram_Body (Loc,
5775 Specification => Copy_Separate_Tree (Proc_Spec),
5776 Declarations => Body_Decl_List,
5777 Handled_Statement_Sequence =>
5778 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5780 Set_Defining_Unit_Name (Specification (Proc_Body),
5781 Make_Defining_Identifier (Loc, Subp_Name));
5783 Append_To (Decl_List, Proc_Body);
5784 end Build_Procedure;
5788 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5790 Proc_Id : Entity_Id;
5791 Proc_Call : Node_Id;
5793 -- Start of processing for Split_Unconstrained_Function
5796 -- Build the associated procedure, analyze it and insert it before
5797 -- the function body N
5800 Scope : constant Entity_Id := Current_Scope;
5801 Decl_List : List_Id;
5804 Build_Procedure (Proc_Id, Decl_List);
5805 Insert_Actions (N, Decl_List);
5809 -- Build the call to the generated procedure
5812 Actual_List : constant List_Id := New_List;
5816 Append_To (Actual_List,
5817 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
5819 Formal := First_Formal (Spec_Id);
5820 while Present (Formal) loop
5821 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
5823 -- Avoid spurious warning on unreferenced formals
5825 Set_Referenced (Formal);
5826 Next_Formal (Formal);
5830 Make_Procedure_Call_Statement (Loc,
5831 Name => New_Occurrence_Of (Proc_Id, Loc),
5832 Parameter_Associations => Actual_List);
5840 -- main_1__F1b (New_Obj, ...);
5845 Make_Block_Statement (Loc,
5846 Declarations => New_List (New_Obj),
5847 Handled_Statement_Sequence =>
5848 Make_Handled_Sequence_Of_Statements (Loc,
5849 Statements => New_List (
5853 Make_Simple_Return_Statement (Loc,
5856 (Defining_Identifier (New_Obj), Loc)))));
5858 Rewrite (Ret_Node, Blk_Stmt);
5859 end Split_Unconstrained_Function;
5861 -- Start of processing for Check_And_Build_Body_To_Inline
5864 -- Do not inline any subprogram that contains nested subprograms, since
5865 -- the backend inlining circuit seems to generate uninitialized
5866 -- references in this case. We know this happens in the case of front
5867 -- end ZCX support, but it also appears it can happen in other cases as
5868 -- well. The backend often rejects attempts to inline in the case of
5869 -- nested procedures anyway, so little if anything is lost by this.
5870 -- Note that this is test is for the benefit of the back-end. There is
5871 -- a separate test for front-end inlining that also rejects nested
5874 -- Do not do this test if errors have been detected, because in some
5875 -- error cases, this code blows up, and we don't need it anyway if
5876 -- there have been errors, since we won't get to the linker anyway.
5878 if Comes_From_Source (Body_Id)
5879 and then (Has_Pragma_Inline_Always (Spec_Id)
5880 or else Optimization_Level > 0)
5881 and then Serious_Errors_Detected = 0
5889 P_Ent := Scope (P_Ent);
5890 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5892 if Is_Subprogram (P_Ent) then
5893 Set_Is_Inlined (P_Ent, False);
5895 if Comes_From_Source (P_Ent)
5896 and then Has_Pragma_Inline (P_Ent)
5899 ("cannot inline& (nested subprogram)?", N, P_Ent,
5900 Is_Serious => True);
5907 -- Build the body to inline only if really needed
5909 if Check_Body_To_Inline (N, Spec_Id)
5910 and then Serious_Errors_Detected = 0
5912 if Returns_Unconstrained_Type (Spec_Id) then
5913 if Can_Split_Unconstrained_Function (N) then
5914 Split_Unconstrained_Function (N, Spec_Id);
5915 Build_Body_To_Inline (N, Spec_Id);
5916 Set_Is_Inlined (Spec_Id);
5919 Build_Body_To_Inline (N, Spec_Id);
5920 Set_Is_Inlined (Spec_Id);
5923 end Check_And_Build_Body_To_Inline;
5925 -----------------------
5926 -- Check_Conformance --
5927 -----------------------
5929 procedure Check_Conformance
5930 (New_Id : Entity_Id;
5932 Ctype : Conformance_Type;
5934 Conforms : out Boolean;
5935 Err_Loc : Node_Id := Empty;
5936 Get_Inst : Boolean := False;
5937 Skip_Controlling_Formals : Boolean := False)
5939 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5940 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5941 -- If Errmsg is True, then processing continues to post an error message
5942 -- for conformance error on given node. Two messages are output. The
5943 -- first message points to the previous declaration with a general "no
5944 -- conformance" message. The second is the detailed reason, supplied as
5945 -- Msg. The parameter N provide information for a possible & insertion
5946 -- in the message, and also provides the location for posting the
5947 -- message in the absence of a specified Err_Loc location.
5949 -----------------------
5950 -- Conformance_Error --
5951 -----------------------
5953 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5960 if No (Err_Loc) then
5966 Error_Msg_Sloc := Sloc (Old_Id);
5969 when Type_Conformant =>
5970 Error_Msg_N -- CODEFIX
5971 ("not type conformant with declaration#!", Enode);
5973 when Mode_Conformant =>
5974 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5976 ("not mode conformant with operation inherited#!",
5980 ("not mode conformant with declaration#!", Enode);
5983 when Subtype_Conformant =>
5984 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5986 ("not subtype conformant with operation inherited#!",
5990 ("not subtype conformant with declaration#!", Enode);
5993 when Fully_Conformant =>
5994 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5995 Error_Msg_N -- CODEFIX
5996 ("not fully conformant with operation inherited#!",
5999 Error_Msg_N -- CODEFIX
6000 ("not fully conformant with declaration#!", Enode);
6004 Error_Msg_NE (Msg, Enode, N);
6006 end Conformance_Error;
6010 Old_Type : constant Entity_Id := Etype (Old_Id);
6011 New_Type : constant Entity_Id := Etype (New_Id);
6012 Old_Formal : Entity_Id;
6013 New_Formal : Entity_Id;
6014 Access_Types_Match : Boolean;
6015 Old_Formal_Base : Entity_Id;
6016 New_Formal_Base : Entity_Id;
6018 -- Start of processing for Check_Conformance
6023 -- We need a special case for operators, since they don't appear
6026 if Ctype = Type_Conformant then
6027 if Ekind (New_Id) = E_Operator
6028 and then Operator_Matches_Spec (New_Id, Old_Id)
6034 -- If both are functions/operators, check return types conform
6036 if Old_Type /= Standard_Void_Type
6037 and then New_Type /= Standard_Void_Type
6040 -- If we are checking interface conformance we omit controlling
6041 -- arguments and result, because we are only checking the conformance
6042 -- of the remaining parameters.
6044 if Has_Controlling_Result (Old_Id)
6045 and then Has_Controlling_Result (New_Id)
6046 and then Skip_Controlling_Formals
6050 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
6051 if Ctype >= Subtype_Conformant
6052 and then not Predicates_Match (Old_Type, New_Type)
6055 ("\predicate of return type does not match!", New_Id);
6058 ("\return type does not match!", New_Id);
6064 -- Ada 2005 (AI-231): In case of anonymous access types check the
6065 -- null-exclusion and access-to-constant attributes match.
6067 if Ada_Version >= Ada_2005
6068 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
6070 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
6071 or else Is_Access_Constant (Etype (Old_Type)) /=
6072 Is_Access_Constant (Etype (New_Type)))
6074 Conformance_Error ("\return type does not match!", New_Id);
6078 -- If either is a function/operator and the other isn't, error
6080 elsif Old_Type /= Standard_Void_Type
6081 or else New_Type /= Standard_Void_Type
6083 Conformance_Error ("\functions can only match functions!", New_Id);
6087 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
6088 -- If this is a renaming as body, refine error message to indicate that
6089 -- the conflict is with the original declaration. If the entity is not
6090 -- frozen, the conventions don't have to match, the one of the renamed
6091 -- entity is inherited.
6093 if Ctype >= Subtype_Conformant then
6094 if Convention (Old_Id) /= Convention (New_Id) then
6095 if not Is_Frozen (New_Id) then
6098 elsif Present (Err_Loc)
6099 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
6100 and then Present (Corresponding_Spec (Err_Loc))
6102 Error_Msg_Name_1 := Chars (New_Id);
6104 Name_Ada + Convention_Id'Pos (Convention (New_Id));
6105 Conformance_Error ("\prior declaration for% has convention %!");
6108 Conformance_Error ("\calling conventions do not match!");
6113 elsif Is_Formal_Subprogram (Old_Id)
6114 or else Is_Formal_Subprogram (New_Id)
6116 Conformance_Error ("\formal subprograms not allowed!");
6121 -- Deal with parameters
6123 -- Note: we use the entity information, rather than going directly
6124 -- to the specification in the tree. This is not only simpler, but
6125 -- absolutely necessary for some cases of conformance tests between
6126 -- operators, where the declaration tree simply does not exist.
6128 Old_Formal := First_Formal (Old_Id);
6129 New_Formal := First_Formal (New_Id);
6130 while Present (Old_Formal) and then Present (New_Formal) loop
6131 if Is_Controlling_Formal (Old_Formal)
6132 and then Is_Controlling_Formal (New_Formal)
6133 and then Skip_Controlling_Formals
6135 -- The controlling formals will have different types when
6136 -- comparing an interface operation with its match, but both
6137 -- or neither must be access parameters.
6139 if Is_Access_Type (Etype (Old_Formal))
6141 Is_Access_Type (Etype (New_Formal))
6143 goto Skip_Controlling_Formal;
6146 ("\access parameter does not match!", New_Formal);
6150 -- Ada 2012: Mode conformance also requires that formal parameters
6151 -- be both aliased, or neither.
6153 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
6154 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
6156 ("\aliased parameter mismatch!", New_Formal);
6160 if Ctype = Fully_Conformant then
6162 -- Names must match. Error message is more accurate if we do
6163 -- this before checking that the types of the formals match.
6165 if Chars (Old_Formal) /= Chars (New_Formal) then
6166 Conformance_Error ("\name & does not match!", New_Formal);
6168 -- Set error posted flag on new formal as well to stop
6169 -- junk cascaded messages in some cases.
6171 Set_Error_Posted (New_Formal);
6175 -- Null exclusion must match
6177 if Null_Exclusion_Present (Parent (Old_Formal))
6179 Null_Exclusion_Present (Parent (New_Formal))
6181 -- Only give error if both come from source. This should be
6182 -- investigated some time, since it should not be needed ???
6184 if Comes_From_Source (Old_Formal)
6186 Comes_From_Source (New_Formal)
6189 ("\null exclusion for & does not match", New_Formal);
6191 -- Mark error posted on the new formal to avoid duplicated
6192 -- complaint about types not matching.
6194 Set_Error_Posted (New_Formal);
6199 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6200 -- case occurs whenever a subprogram is being renamed and one of its
6201 -- parameters imposes a null exclusion. For example:
6203 -- type T is null record;
6204 -- type Acc_T is access T;
6205 -- subtype Acc_T_Sub is Acc_T;
6207 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6208 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6211 Old_Formal_Base := Etype (Old_Formal);
6212 New_Formal_Base := Etype (New_Formal);
6215 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6216 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6219 Access_Types_Match := Ada_Version >= Ada_2005
6221 -- Ensure that this rule is only applied when New_Id is a
6222 -- renaming of Old_Id.
6224 and then Nkind (Parent (Parent (New_Id))) =
6225 N_Subprogram_Renaming_Declaration
6226 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
6227 and then Present (Entity (Name (Parent (Parent (New_Id)))))
6228 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
6230 -- Now handle the allowed access-type case
6232 and then Is_Access_Type (Old_Formal_Base)
6233 and then Is_Access_Type (New_Formal_Base)
6235 -- The type kinds must match. The only exception occurs with
6236 -- multiple generics of the form:
6239 -- type F is private; type A is private;
6240 -- type F_Ptr is access F; type A_Ptr is access A;
6241 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6242 -- package F_Pack is ... package A_Pack is
6243 -- package F_Inst is
6244 -- new F_Pack (A, A_Ptr, A_P);
6246 -- When checking for conformance between the parameters of A_P
6247 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6248 -- because the compiler has transformed A_Ptr into a subtype of
6249 -- F_Ptr. We catch this case in the code below.
6251 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
6253 (Is_Generic_Type (Old_Formal_Base)
6254 and then Is_Generic_Type (New_Formal_Base)
6255 and then Is_Internal (New_Formal_Base)
6256 and then Etype (Etype (New_Formal_Base)) =
6258 and then Directly_Designated_Type (Old_Formal_Base) =
6259 Directly_Designated_Type (New_Formal_Base)
6260 and then ((Is_Itype (Old_Formal_Base)
6261 and then Can_Never_Be_Null (Old_Formal_Base))
6263 (Is_Itype (New_Formal_Base)
6264 and then Can_Never_Be_Null (New_Formal_Base)));
6266 -- Types must always match. In the visible part of an instance,
6267 -- usual overloading rules for dispatching operations apply, and
6268 -- we check base types (not the actual subtypes).
6270 if In_Instance_Visible_Part
6271 and then Is_Dispatching_Operation (New_Id)
6273 if not Conforming_Types
6274 (T1 => Base_Type (Etype (Old_Formal)),
6275 T2 => Base_Type (Etype (New_Formal)),
6277 Get_Inst => Get_Inst)
6278 and then not Access_Types_Match
6280 Conformance_Error ("\type of & does not match!", New_Formal);
6284 elsif not Conforming_Types
6285 (T1 => Old_Formal_Base,
6286 T2 => New_Formal_Base,
6288 Get_Inst => Get_Inst)
6289 and then not Access_Types_Match
6291 -- Don't give error message if old type is Any_Type. This test
6292 -- avoids some cascaded errors, e.g. in case of a bad spec.
6294 if Errmsg and then Old_Formal_Base = Any_Type then
6297 if Ctype >= Subtype_Conformant
6299 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6302 ("\predicate of & does not match!", New_Formal);
6305 ("\type of & does not match!", New_Formal);
6312 -- For mode conformance, mode must match
6314 if Ctype >= Mode_Conformant then
6315 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6316 if not Ekind_In (New_Id, E_Function, E_Procedure)
6317 or else not Is_Primitive_Wrapper (New_Id)
6319 Conformance_Error ("\mode of & does not match!", New_Formal);
6323 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6325 if Is_Protected_Type
6326 (Corresponding_Concurrent_Type (T))
6328 Error_Msg_PT (T, New_Id);
6331 ("\mode of & does not match!", New_Formal);
6338 -- Part of mode conformance for access types is having the same
6339 -- constant modifier.
6341 elsif Access_Types_Match
6342 and then Is_Access_Constant (Old_Formal_Base) /=
6343 Is_Access_Constant (New_Formal_Base)
6346 ("\constant modifier does not match!", New_Formal);
6351 if Ctype >= Subtype_Conformant then
6353 -- Ada 2005 (AI-231): In case of anonymous access types check
6354 -- the null-exclusion and access-to-constant attributes must
6355 -- match. For null exclusion, we test the types rather than the
6356 -- formals themselves, since the attribute is only set reliably
6357 -- on the formals in the Ada 95 case, and we exclude the case
6358 -- where Old_Formal is marked as controlling, to avoid errors
6359 -- when matching completing bodies with dispatching declarations
6360 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6362 if Ada_Version >= Ada_2005
6363 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6364 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6366 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6367 Can_Never_Be_Null (Etype (New_Formal))
6369 not Is_Controlling_Formal (Old_Formal))
6371 Is_Access_Constant (Etype (Old_Formal)) /=
6372 Is_Access_Constant (Etype (New_Formal)))
6374 -- Do not complain if error already posted on New_Formal. This
6375 -- avoids some redundant error messages.
6377 and then not Error_Posted (New_Formal)
6379 -- It is allowed to omit the null-exclusion in case of stream
6380 -- attribute subprograms. We recognize stream subprograms
6381 -- through their TSS-generated suffix.
6384 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6387 if TSS_Name /= TSS_Stream_Read
6388 and then TSS_Name /= TSS_Stream_Write
6389 and then TSS_Name /= TSS_Stream_Input
6390 and then TSS_Name /= TSS_Stream_Output
6392 -- Here we have a definite conformance error. It is worth
6393 -- special casing the error message for the case of a
6394 -- controlling formal (which excludes null).
6396 if Is_Controlling_Formal (New_Formal) then
6397 Error_Msg_Node_2 := Scope (New_Formal);
6399 ("\controlling formal& of& excludes null, "
6400 & "declaration must exclude null as well",
6403 -- Normal case (couldn't we give more detail here???)
6407 ("\type of & does not match!", New_Formal);
6416 -- Full conformance checks
6418 if Ctype = Fully_Conformant then
6420 -- We have checked already that names match
6422 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6424 -- Check default expressions for in parameters
6427 NewD : constant Boolean :=
6428 Present (Default_Value (New_Formal));
6429 OldD : constant Boolean :=
6430 Present (Default_Value (Old_Formal));
6432 if NewD or OldD then
6434 -- The old default value has been analyzed because the
6435 -- current full declaration will have frozen everything
6436 -- before. The new default value has not been analyzed,
6437 -- so analyze it now before we check for conformance.
6440 Push_Scope (New_Id);
6441 Preanalyze_Spec_Expression
6442 (Default_Value (New_Formal), Etype (New_Formal));
6446 if not (NewD and OldD)
6447 or else not Fully_Conformant_Expressions
6448 (Default_Value (Old_Formal),
6449 Default_Value (New_Formal))
6452 ("\default expression for & does not match!",
6461 -- A couple of special checks for Ada 83 mode. These checks are
6462 -- skipped if either entity is an operator in package Standard,
6463 -- or if either old or new instance is not from the source program.
6465 if Ada_Version = Ada_83
6466 and then Sloc (Old_Id) > Standard_Location
6467 and then Sloc (New_Id) > Standard_Location
6468 and then Comes_From_Source (Old_Id)
6469 and then Comes_From_Source (New_Id)
6472 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6473 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6476 -- Explicit IN must be present or absent in both cases. This
6477 -- test is required only in the full conformance case.
6479 if In_Present (Old_Param) /= In_Present (New_Param)
6480 and then Ctype = Fully_Conformant
6483 ("\(Ada 83) IN must appear in both declarations",
6488 -- Grouping (use of comma in param lists) must be the same
6489 -- This is where we catch a misconformance like:
6492 -- A : Integer; B : Integer
6494 -- which are represented identically in the tree except
6495 -- for the setting of the flags More_Ids and Prev_Ids.
6497 if More_Ids (Old_Param) /= More_Ids (New_Param)
6498 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6501 ("\grouping of & does not match!", New_Formal);
6507 -- This label is required when skipping controlling formals
6509 <<Skip_Controlling_Formal>>
6511 Next_Formal (Old_Formal);
6512 Next_Formal (New_Formal);
6515 if Present (Old_Formal) then
6516 Conformance_Error ("\too few parameters!");
6519 elsif Present (New_Formal) then
6520 Conformance_Error ("\too many parameters!", New_Formal);
6523 end Check_Conformance;
6525 -----------------------
6526 -- Check_Conventions --
6527 -----------------------
6529 procedure Check_Conventions (Typ : Entity_Id) is
6530 Ifaces_List : Elist_Id;
6532 procedure Check_Convention (Op : Entity_Id);
6533 -- Verify that the convention of inherited dispatching operation Op is
6534 -- consistent among all subprograms it overrides. In order to minimize
6535 -- the search, Search_From is utilized to designate a specific point in
6536 -- the list rather than iterating over the whole list once more.
6538 ----------------------
6539 -- Check_Convention --
6540 ----------------------
6542 procedure Check_Convention (Op : Entity_Id) is
6543 function Convention_Of (Id : Entity_Id) return Convention_Id;
6544 -- Given an entity, return its convention. The function treats Ghost
6545 -- as convention Ada because the two have the same dynamic semantics.
6551 function Convention_Of (Id : Entity_Id) return Convention_Id is
6552 Conv : constant Convention_Id := Convention (Id);
6554 if Conv = Convention_Ghost then
6555 return Convention_Ada;
6563 Op_Conv : constant Convention_Id := Convention_Of (Op);
6564 Iface_Conv : Convention_Id;
6565 Iface_Elmt : Elmt_Id;
6566 Iface_Prim_Elmt : Elmt_Id;
6567 Iface_Prim : Entity_Id;
6569 -- Start of processing for Check_Convention
6572 Iface_Elmt := First_Elmt (Ifaces_List);
6573 while Present (Iface_Elmt) loop
6575 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6576 while Present (Iface_Prim_Elmt) loop
6577 Iface_Prim := Node (Iface_Prim_Elmt);
6578 Iface_Conv := Convention_Of (Iface_Prim);
6580 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6581 and then Iface_Conv /= Op_Conv
6584 ("inconsistent conventions in primitive operations", Typ);
6586 Error_Msg_Name_1 := Chars (Op);
6587 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6588 Error_Msg_Sloc := Sloc (Op);
6590 if Comes_From_Source (Op) or else No (Alias (Op)) then
6591 if not Present (Overridden_Operation (Op)) then
6592 Error_Msg_N ("\\primitive % defined #", Typ);
6595 ("\\overriding operation % with " &
6596 "convention % defined #", Typ);
6599 else pragma Assert (Present (Alias (Op)));
6600 Error_Msg_Sloc := Sloc (Alias (Op));
6602 ("\\inherited operation % with " &
6603 "convention % defined #", Typ);
6606 Error_Msg_Name_1 := Chars (Op);
6607 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6608 Error_Msg_Sloc := Sloc (Iface_Prim);
6610 ("\\overridden operation % with " &
6611 "convention % defined #", Typ);
6613 -- Avoid cascading errors
6618 Next_Elmt (Iface_Prim_Elmt);
6621 Next_Elmt (Iface_Elmt);
6623 end Check_Convention;
6627 Prim_Op : Entity_Id;
6628 Prim_Op_Elmt : Elmt_Id;
6630 -- Start of processing for Check_Conventions
6633 if not Has_Interfaces (Typ) then
6637 Collect_Interfaces (Typ, Ifaces_List);
6639 -- The algorithm checks every overriding dispatching operation against
6640 -- all the corresponding overridden dispatching operations, detecting
6641 -- differences in conventions.
6643 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6644 while Present (Prim_Op_Elmt) loop
6645 Prim_Op := Node (Prim_Op_Elmt);
6647 -- A small optimization: skip the predefined dispatching operations
6648 -- since they always have the same convention.
6650 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6651 Check_Convention (Prim_Op);
6654 Next_Elmt (Prim_Op_Elmt);
6656 end Check_Conventions;
6658 ------------------------------
6659 -- Check_Delayed_Subprogram --
6660 ------------------------------
6662 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6665 procedure Possible_Freeze (T : Entity_Id);
6666 -- T is the type of either a formal parameter or of the return type.
6667 -- If T is not yet frozen and needs a delayed freeze, then the
6668 -- subprogram itself must be delayed. If T is the limited view of an
6669 -- incomplete type the subprogram must be frozen as well, because
6670 -- T may depend on local types that have not been frozen yet.
6672 ---------------------
6673 -- Possible_Freeze --
6674 ---------------------
6676 procedure Possible_Freeze (T : Entity_Id) is
6678 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6679 Set_Has_Delayed_Freeze (Designator);
6681 elsif Is_Access_Type (T)
6682 and then Has_Delayed_Freeze (Designated_Type (T))
6683 and then not Is_Frozen (Designated_Type (T))
6685 Set_Has_Delayed_Freeze (Designator);
6687 elsif Ekind (T) = E_Incomplete_Type
6688 and then From_Limited_With (T)
6690 Set_Has_Delayed_Freeze (Designator);
6692 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6693 -- of a subprogram or entry declaration.
6695 elsif Ekind (T) = E_Incomplete_Type
6696 and then Ada_Version >= Ada_2012
6698 Set_Has_Delayed_Freeze (Designator);
6701 end Possible_Freeze;
6703 -- Start of processing for Check_Delayed_Subprogram
6706 -- All subprograms, including abstract subprograms, may need a freeze
6707 -- node if some formal type or the return type needs one.
6709 Possible_Freeze (Etype (Designator));
6710 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6712 -- Need delayed freeze if any of the formal types themselves need
6713 -- a delayed freeze and are not yet frozen.
6715 F := First_Formal (Designator);
6716 while Present (F) loop
6717 Possible_Freeze (Etype (F));
6718 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6722 -- Mark functions that return by reference. Note that it cannot be
6723 -- done for delayed_freeze subprograms because the underlying
6724 -- returned type may not be known yet (for private types)
6726 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6728 Typ : constant Entity_Id := Etype (Designator);
6729 Utyp : constant Entity_Id := Underlying_Type (Typ);
6731 if Is_Limited_View (Typ) then
6732 Set_Returns_By_Ref (Designator);
6733 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6734 Set_Returns_By_Ref (Designator);
6738 end Check_Delayed_Subprogram;
6740 ------------------------------------
6741 -- Check_Discriminant_Conformance --
6742 ------------------------------------
6744 procedure Check_Discriminant_Conformance
6749 Old_Discr : Entity_Id := First_Discriminant (Prev);
6750 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6751 New_Discr_Id : Entity_Id;
6752 New_Discr_Type : Entity_Id;
6754 procedure Conformance_Error (Msg : String; N : Node_Id);
6755 -- Post error message for conformance error on given node. Two messages
6756 -- are output. The first points to the previous declaration with a
6757 -- general "no conformance" message. The second is the detailed reason,
6758 -- supplied as Msg. The parameter N provide information for a possible
6759 -- & insertion in the message.
6761 -----------------------
6762 -- Conformance_Error --
6763 -----------------------
6765 procedure Conformance_Error (Msg : String; N : Node_Id) is
6767 Error_Msg_Sloc := Sloc (Prev_Loc);
6768 Error_Msg_N -- CODEFIX
6769 ("not fully conformant with declaration#!", N);
6770 Error_Msg_NE (Msg, N, N);
6771 end Conformance_Error;
6773 -- Start of processing for Check_Discriminant_Conformance
6776 while Present (Old_Discr) and then Present (New_Discr) loop
6777 New_Discr_Id := Defining_Identifier (New_Discr);
6779 -- The subtype mark of the discriminant on the full type has not
6780 -- been analyzed so we do it here. For an access discriminant a new
6783 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6785 Access_Definition (N, Discriminant_Type (New_Discr));
6788 Analyze (Discriminant_Type (New_Discr));
6789 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6791 -- Ada 2005: if the discriminant definition carries a null
6792 -- exclusion, create an itype to check properly for consistency
6793 -- with partial declaration.
6795 if Is_Access_Type (New_Discr_Type)
6796 and then Null_Exclusion_Present (New_Discr)
6799 Create_Null_Excluding_Itype
6800 (T => New_Discr_Type,
6801 Related_Nod => New_Discr,
6802 Scope_Id => Current_Scope);
6806 if not Conforming_Types
6807 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6809 Conformance_Error ("type of & does not match!", New_Discr_Id);
6812 -- Treat the new discriminant as an occurrence of the old one,
6813 -- for navigation purposes, and fill in some semantic
6814 -- information, for completeness.
6816 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6817 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6818 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6823 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6824 Conformance_Error ("name & does not match!", New_Discr_Id);
6828 -- Default expressions must match
6831 NewD : constant Boolean :=
6832 Present (Expression (New_Discr));
6833 OldD : constant Boolean :=
6834 Present (Expression (Parent (Old_Discr)));
6837 if NewD or OldD then
6839 -- The old default value has been analyzed and expanded,
6840 -- because the current full declaration will have frozen
6841 -- everything before. The new default values have not been
6842 -- expanded, so expand now to check conformance.
6845 Preanalyze_Spec_Expression
6846 (Expression (New_Discr), New_Discr_Type);
6849 if not (NewD and OldD)
6850 or else not Fully_Conformant_Expressions
6851 (Expression (Parent (Old_Discr)),
6852 Expression (New_Discr))
6856 ("default expression for & does not match!",
6863 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6865 if Ada_Version = Ada_83 then
6867 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6870 -- Grouping (use of comma in param lists) must be the same
6871 -- This is where we catch a misconformance like:
6874 -- A : Integer; B : Integer
6876 -- which are represented identically in the tree except
6877 -- for the setting of the flags More_Ids and Prev_Ids.
6879 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6880 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6883 ("grouping of & does not match!", New_Discr_Id);
6889 Next_Discriminant (Old_Discr);
6893 if Present (Old_Discr) then
6894 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6897 elsif Present (New_Discr) then
6899 ("too many discriminants!", Defining_Identifier (New_Discr));
6902 end Check_Discriminant_Conformance;
6904 ----------------------------
6905 -- Check_Fully_Conformant --
6906 ----------------------------
6908 procedure Check_Fully_Conformant
6909 (New_Id : Entity_Id;
6911 Err_Loc : Node_Id := Empty)
6914 pragma Warnings (Off, Result);
6917 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6918 end Check_Fully_Conformant;
6920 ---------------------------
6921 -- Check_Mode_Conformant --
6922 ---------------------------
6924 procedure Check_Mode_Conformant
6925 (New_Id : Entity_Id;
6927 Err_Loc : Node_Id := Empty;
6928 Get_Inst : Boolean := False)
6931 pragma Warnings (Off, Result);
6934 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6935 end Check_Mode_Conformant;
6937 --------------------------------
6938 -- Check_Overriding_Indicator --
6939 --------------------------------
6941 procedure Check_Overriding_Indicator
6943 Overridden_Subp : Entity_Id;
6944 Is_Primitive : Boolean)
6950 -- No overriding indicator for literals
6952 if Ekind (Subp) = E_Enumeration_Literal then
6955 elsif Ekind (Subp) = E_Entry then
6956 Decl := Parent (Subp);
6958 -- No point in analyzing a malformed operator
6960 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6961 and then Error_Posted (Subp)
6966 Decl := Unit_Declaration_Node (Subp);
6969 if Nkind_In (Decl, N_Subprogram_Body,
6970 N_Subprogram_Body_Stub,
6971 N_Subprogram_Declaration,
6972 N_Abstract_Subprogram_Declaration,
6973 N_Subprogram_Renaming_Declaration)
6975 Spec := Specification (Decl);
6977 elsif Nkind (Decl) = N_Entry_Declaration then
6984 -- The overriding operation is type conformant with the overridden one,
6985 -- but the names of the formals are not required to match. If the names
6986 -- appear permuted in the overriding operation, this is a possible
6987 -- source of confusion that is worth diagnosing. Controlling formals
6988 -- often carry names that reflect the type, and it is not worthwhile
6989 -- requiring that their names match.
6991 if Present (Overridden_Subp)
6992 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6999 Form1 := First_Formal (Subp);
7000 Form2 := First_Formal (Overridden_Subp);
7002 -- If the overriding operation is a synchronized operation, skip
7003 -- the first parameter of the overridden operation, which is
7004 -- implicit in the new one. If the operation is declared in the
7005 -- body it is not primitive and all formals must match.
7007 if Is_Concurrent_Type (Scope (Subp))
7008 and then Is_Tagged_Type (Scope (Subp))
7009 and then not Has_Completion (Scope (Subp))
7011 Form2 := Next_Formal (Form2);
7014 if Present (Form1) then
7015 Form1 := Next_Formal (Form1);
7016 Form2 := Next_Formal (Form2);
7019 while Present (Form1) loop
7020 if not Is_Controlling_Formal (Form1)
7021 and then Present (Next_Formal (Form2))
7022 and then Chars (Form1) = Chars (Next_Formal (Form2))
7024 Error_Msg_Node_2 := Alias (Overridden_Subp);
7025 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7027 ("& does not match corresponding formal of&#",
7032 Next_Formal (Form1);
7033 Next_Formal (Form2);
7038 -- If there is an overridden subprogram, then check that there is no
7039 -- "not overriding" indicator, and mark the subprogram as overriding.
7040 -- This is not done if the overridden subprogram is marked as hidden,
7041 -- which can occur for the case of inherited controlled operations
7042 -- (see Derive_Subprogram), unless the inherited subprogram's parent
7043 -- subprogram is not itself hidden. (Note: This condition could probably
7044 -- be simplified, leaving out the testing for the specific controlled
7045 -- cases, but it seems safer and clearer this way, and echoes similar
7046 -- special-case tests of this kind in other places.)
7048 if Present (Overridden_Subp)
7049 and then (not Is_Hidden (Overridden_Subp)
7051 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
7054 and then Present (Alias (Overridden_Subp))
7055 and then not Is_Hidden (Alias (Overridden_Subp))))
7057 if Must_Not_Override (Spec) then
7058 Error_Msg_Sloc := Sloc (Overridden_Subp);
7060 if Ekind (Subp) = E_Entry then
7062 ("entry & overrides inherited operation #", Spec, Subp);
7065 ("subprogram & overrides inherited operation #", Spec, Subp);
7068 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
7069 -- as an extension of Root_Controlled, and thus has a useless Adjust
7070 -- operation. This operation should not be inherited by other limited
7071 -- controlled types. An explicit Adjust for them is not overriding.
7073 elsif Must_Override (Spec)
7074 and then Chars (Overridden_Subp) = Name_Adjust
7075 and then Is_Limited_Type (Etype (First_Formal (Subp)))
7076 and then Present (Alias (Overridden_Subp))
7078 Is_Predefined_File_Name
7079 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
7081 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7083 elsif Is_Subprogram (Subp) then
7084 if Is_Init_Proc (Subp) then
7087 elsif No (Overridden_Operation (Subp)) then
7089 -- For entities generated by Derive_Subprograms the overridden
7090 -- operation is the inherited primitive (which is available
7091 -- through the attribute alias)
7093 if (Is_Dispatching_Operation (Subp)
7094 or else Is_Dispatching_Operation (Overridden_Subp))
7095 and then not Comes_From_Source (Overridden_Subp)
7096 and then Find_Dispatching_Type (Overridden_Subp) =
7097 Find_Dispatching_Type (Subp)
7098 and then Present (Alias (Overridden_Subp))
7099 and then Comes_From_Source (Alias (Overridden_Subp))
7101 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7104 Set_Overridden_Operation (Subp, Overridden_Subp);
7109 -- If primitive flag is set or this is a protected operation, then
7110 -- the operation is overriding at the point of its declaration, so
7111 -- warn if necessary. Otherwise it may have been declared before the
7112 -- operation it overrides and no check is required.
7115 and then not Must_Override (Spec)
7116 and then (Is_Primitive
7117 or else Ekind (Scope (Subp)) = E_Protected_Type)
7119 Style.Missing_Overriding (Decl, Subp);
7122 -- If Subp is an operator, it may override a predefined operation, if
7123 -- it is defined in the same scope as the type to which it applies.
7124 -- In that case Overridden_Subp is empty because of our implicit
7125 -- representation for predefined operators. We have to check whether the
7126 -- signature of Subp matches that of a predefined operator. Note that
7127 -- first argument provides the name of the operator, and the second
7128 -- argument the signature that may match that of a standard operation.
7129 -- If the indicator is overriding, then the operator must match a
7130 -- predefined signature, because we know already that there is no
7131 -- explicit overridden operation.
7133 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7134 if Must_Not_Override (Spec) then
7136 -- If this is not a primitive or a protected subprogram, then
7137 -- "not overriding" is illegal.
7140 and then Ekind (Scope (Subp)) /= E_Protected_Type
7143 ("overriding indicator only allowed "
7144 & "if subprogram is primitive", Subp);
7146 elsif Can_Override_Operator (Subp) then
7148 ("subprogram& overrides predefined operator ", Spec, Subp);
7151 elsif Must_Override (Spec) then
7152 if No (Overridden_Operation (Subp))
7153 and then not Can_Override_Operator (Subp)
7155 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7158 elsif not Error_Posted (Subp)
7159 and then Style_Check
7160 and then Can_Override_Operator (Subp)
7162 not Is_Predefined_File_Name
7163 (Unit_File_Name (Get_Source_Unit (Subp)))
7165 -- If style checks are enabled, indicate that the indicator is
7166 -- missing. However, at the point of declaration, the type of
7167 -- which this is a primitive operation may be private, in which
7168 -- case the indicator would be premature.
7170 if Has_Private_Declaration (Etype (Subp))
7171 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7175 Style.Missing_Overriding (Decl, Subp);
7179 elsif Must_Override (Spec) then
7180 if Ekind (Subp) = E_Entry then
7181 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7183 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7186 -- If the operation is marked "not overriding" and it's not primitive
7187 -- then an error is issued, unless this is an operation of a task or
7188 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7189 -- has been specified have already been checked above.
7191 elsif Must_Not_Override (Spec)
7192 and then not Is_Primitive
7193 and then Ekind (Subp) /= E_Entry
7194 and then Ekind (Scope (Subp)) /= E_Protected_Type
7197 ("overriding indicator only allowed if subprogram is primitive",
7201 end Check_Overriding_Indicator;
7207 -- Note: this procedure needs to know far too much about how the expander
7208 -- messes with exceptions. The use of the flag Exception_Junk and the
7209 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7210 -- works, but is not very clean. It would be better if the expansion
7211 -- routines would leave Original_Node working nicely, and we could use
7212 -- Original_Node here to ignore all the peculiar expander messing ???
7214 procedure Check_Returns
7218 Proc : Entity_Id := Empty)
7222 procedure Check_Statement_Sequence (L : List_Id);
7223 -- Internal recursive procedure to check a list of statements for proper
7224 -- termination by a return statement (or a transfer of control or a
7225 -- compound statement that is itself internally properly terminated).
7227 ------------------------------
7228 -- Check_Statement_Sequence --
7229 ------------------------------
7231 procedure Check_Statement_Sequence (L : List_Id) is
7236 function Assert_False return Boolean;
7237 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7238 -- rewritten as a null statement when assertions are off. The assert
7239 -- is not active, but it is still enough to kill the warning.
7245 function Assert_False return Boolean is
7246 Orig : constant Node_Id := Original_Node (Last_Stm);
7249 if Nkind (Orig) = N_Pragma
7250 and then Pragma_Name (Orig) = Name_Assert
7251 and then not Error_Posted (Orig)
7254 Arg : constant Node_Id :=
7255 First (Pragma_Argument_Associations (Orig));
7256 Exp : constant Node_Id := Expression (Arg);
7258 return Nkind (Exp) = N_Identifier
7259 and then Chars (Exp) = Name_False;
7269 Raise_Exception_Call : Boolean;
7270 -- Set True if statement sequence terminated by Raise_Exception call
7271 -- or a Reraise_Occurrence call.
7273 -- Start of processing for Check_Statement_Sequence
7276 Raise_Exception_Call := False;
7278 -- Get last real statement
7280 Last_Stm := Last (L);
7282 -- Deal with digging out exception handler statement sequences that
7283 -- have been transformed by the local raise to goto optimization.
7284 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7285 -- optimization has occurred, we are looking at something like:
7288 -- original stmts in block
7292 -- goto L1; | omitted if No_Exception_Propagation
7297 -- goto L3; -- skip handler when exception not raised
7299 -- <<L1>> -- target label for local exception
7313 -- and what we have to do is to dig out the estmts1 and estmts2
7314 -- sequences (which were the original sequences of statements in
7315 -- the exception handlers) and check them.
7317 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7322 exit when Nkind (Stm) /= N_Block_Statement;
7323 exit when not Exception_Junk (Stm);
7326 exit when Nkind (Stm) /= N_Label;
7327 exit when not Exception_Junk (Stm);
7328 Check_Statement_Sequence
7329 (Statements (Handled_Statement_Sequence (Next (Stm))));
7334 exit when Nkind (Stm) /= N_Goto_Statement;
7335 exit when not Exception_Junk (Stm);
7339 -- Don't count pragmas
7341 while Nkind (Last_Stm) = N_Pragma
7343 -- Don't count call to SS_Release (can happen after Raise_Exception)
7346 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7348 Nkind (Name (Last_Stm)) = N_Identifier
7350 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7352 -- Don't count exception junk
7355 (Nkind_In (Last_Stm, N_Goto_Statement,
7357 N_Object_Declaration)
7358 and then Exception_Junk (Last_Stm))
7359 or else Nkind (Last_Stm) in N_Push_xxx_Label
7360 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7362 -- Inserted code, such as finalization calls, is irrelevant: we only
7363 -- need to check original source.
7365 or else Is_Rewrite_Insertion (Last_Stm)
7370 -- Here we have the "real" last statement
7372 Kind := Nkind (Last_Stm);
7374 -- Transfer of control, OK. Note that in the No_Return procedure
7375 -- case, we already diagnosed any explicit return statements, so
7376 -- we can treat them as OK in this context.
7378 if Is_Transfer (Last_Stm) then
7381 -- Check cases of explicit non-indirect procedure calls
7383 elsif Kind = N_Procedure_Call_Statement
7384 and then Is_Entity_Name (Name (Last_Stm))
7386 -- Check call to Raise_Exception procedure which is treated
7387 -- specially, as is a call to Reraise_Occurrence.
7389 -- We suppress the warning in these cases since it is likely that
7390 -- the programmer really does not expect to deal with the case
7391 -- of Null_Occurrence, and thus would find a warning about a
7392 -- missing return curious, and raising Program_Error does not
7393 -- seem such a bad behavior if this does occur.
7395 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7396 -- behavior will be to raise Constraint_Error (see AI-329).
7398 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7400 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7402 Raise_Exception_Call := True;
7404 -- For Raise_Exception call, test first argument, if it is
7405 -- an attribute reference for a 'Identity call, then we know
7406 -- that the call cannot possibly return.
7409 Arg : constant Node_Id :=
7410 Original_Node (First_Actual (Last_Stm));
7412 if Nkind (Arg) = N_Attribute_Reference
7413 and then Attribute_Name (Arg) = Name_Identity
7420 -- If statement, need to look inside if there is an else and check
7421 -- each constituent statement sequence for proper termination.
7423 elsif Kind = N_If_Statement
7424 and then Present (Else_Statements (Last_Stm))
7426 Check_Statement_Sequence (Then_Statements (Last_Stm));
7427 Check_Statement_Sequence (Else_Statements (Last_Stm));
7429 if Present (Elsif_Parts (Last_Stm)) then
7431 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7434 while Present (Elsif_Part) loop
7435 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7443 -- Case statement, check each case for proper termination
7445 elsif Kind = N_Case_Statement then
7449 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7450 while Present (Case_Alt) loop
7451 Check_Statement_Sequence (Statements (Case_Alt));
7452 Next_Non_Pragma (Case_Alt);
7458 -- Block statement, check its handled sequence of statements
7460 elsif Kind = N_Block_Statement then
7466 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7475 -- Loop statement. If there is an iteration scheme, we can definitely
7476 -- fall out of the loop. Similarly if there is an exit statement, we
7477 -- can fall out. In either case we need a following return.
7479 elsif Kind = N_Loop_Statement then
7480 if Present (Iteration_Scheme (Last_Stm))
7481 or else Has_Exit (Entity (Identifier (Last_Stm)))
7485 -- A loop with no exit statement or iteration scheme is either
7486 -- an infinite loop, or it has some other exit (raise/return).
7487 -- In either case, no warning is required.
7493 -- Timed entry call, check entry call and delay alternatives
7495 -- Note: in expanded code, the timed entry call has been converted
7496 -- to a set of expanded statements on which the check will work
7497 -- correctly in any case.
7499 elsif Kind = N_Timed_Entry_Call then
7501 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7502 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7505 -- If statement sequence of entry call alternative is missing,
7506 -- then we can definitely fall through, and we post the error
7507 -- message on the entry call alternative itself.
7509 if No (Statements (ECA)) then
7512 -- If statement sequence of delay alternative is missing, then
7513 -- we can definitely fall through, and we post the error
7514 -- message on the delay alternative itself.
7516 -- Note: if both ECA and DCA are missing the return, then we
7517 -- post only one message, should be enough to fix the bugs.
7518 -- If not we will get a message next time on the DCA when the
7521 elsif No (Statements (DCA)) then
7524 -- Else check both statement sequences
7527 Check_Statement_Sequence (Statements (ECA));
7528 Check_Statement_Sequence (Statements (DCA));
7533 -- Conditional entry call, check entry call and else part
7535 -- Note: in expanded code, the conditional entry call has been
7536 -- converted to a set of expanded statements on which the check
7537 -- will work correctly in any case.
7539 elsif Kind = N_Conditional_Entry_Call then
7541 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7544 -- If statement sequence of entry call alternative is missing,
7545 -- then we can definitely fall through, and we post the error
7546 -- message on the entry call alternative itself.
7548 if No (Statements (ECA)) then
7551 -- Else check statement sequence and else part
7554 Check_Statement_Sequence (Statements (ECA));
7555 Check_Statement_Sequence (Else_Statements (Last_Stm));
7561 -- If we fall through, issue appropriate message
7565 -- Kill warning if last statement is a raise exception call,
7566 -- or a pragma Assert (False). Note that with assertions enabled,
7567 -- such a pragma has been converted into a raise exception call
7568 -- already, so the Assert_False is for the assertions off case.
7570 if not Raise_Exception_Call and then not Assert_False then
7572 -- In GNATprove mode, it is an error to have a missing return
7574 Error_Msg_Warn := SPARK_Mode /= On;
7576 -- Issue error message or warning
7579 ("RETURN statement missing following this statement<<!",
7582 ("\Program_Error ]<<!", Last_Stm);
7585 -- Note: we set Err even though we have not issued a warning
7586 -- because we still have a case of a missing return. This is
7587 -- an extremely marginal case, probably will never be noticed
7588 -- but we might as well get it right.
7592 -- Otherwise we have the case of a procedure marked No_Return
7595 if not Raise_Exception_Call then
7596 if GNATprove_Mode then
7598 ("implied return after this statement "
7599 & "would have raised Program_Error", Last_Stm);
7602 ("implied return after this statement "
7603 & "will raise Program_Error??", Last_Stm);
7606 Error_Msg_Warn := SPARK_Mode /= On;
7608 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7612 RE : constant Node_Id :=
7613 Make_Raise_Program_Error (Sloc (Last_Stm),
7614 Reason => PE_Implicit_Return);
7616 Insert_After (Last_Stm, RE);
7620 end Check_Statement_Sequence;
7622 -- Start of processing for Check_Returns
7626 Check_Statement_Sequence (Statements (HSS));
7628 if Present (Exception_Handlers (HSS)) then
7629 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7630 while Present (Handler) loop
7631 Check_Statement_Sequence (Statements (Handler));
7632 Next_Non_Pragma (Handler);
7637 ----------------------------
7638 -- Check_Subprogram_Order --
7639 ----------------------------
7641 procedure Check_Subprogram_Order (N : Node_Id) is
7643 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7644 -- This is used to check if S1 > S2 in the sense required by this test,
7645 -- for example nameab < namec, but name2 < name10.
7647 -----------------------------
7648 -- Subprogram_Name_Greater --
7649 -----------------------------
7651 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7656 -- Deal with special case where names are identical except for a
7657 -- numerical suffix. These are handled specially, taking the numeric
7658 -- ordering from the suffix into account.
7661 while S1 (L1) in '0' .. '9' loop
7666 while S2 (L2) in '0' .. '9' loop
7670 -- If non-numeric parts non-equal, do straight compare
7672 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7675 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7676 -- that a missing suffix is treated as numeric zero in this test.
7680 while L1 < S1'Last loop
7682 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7686 while L2 < S2'Last loop
7688 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7693 end Subprogram_Name_Greater;
7695 -- Start of processing for Check_Subprogram_Order
7698 -- Check body in alpha order if this is option
7701 and then Style_Check_Order_Subprograms
7702 and then Nkind (N) = N_Subprogram_Body
7703 and then Comes_From_Source (N)
7704 and then In_Extended_Main_Source_Unit (N)
7708 renames Scope_Stack.Table
7709 (Scope_Stack.Last).Last_Subprogram_Name;
7711 Body_Id : constant Entity_Id :=
7712 Defining_Entity (Specification (N));
7715 Get_Decoded_Name_String (Chars (Body_Id));
7718 if Subprogram_Name_Greater
7719 (LSN.all, Name_Buffer (1 .. Name_Len))
7721 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7727 LSN := new String'(Name_Buffer (1 .. Name_Len));
7730 end Check_Subprogram_Order;
7732 ------------------------------
7733 -- Check_Subtype_Conformant --
7734 ------------------------------
7736 procedure Check_Subtype_Conformant
7737 (New_Id : Entity_Id;
7739 Err_Loc : Node_Id := Empty;
7740 Skip_Controlling_Formals : Boolean := False;
7741 Get_Inst : Boolean := False)
7744 pragma Warnings (Off, Result);
7747 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7748 Skip_Controlling_Formals => Skip_Controlling_Formals,
7749 Get_Inst => Get_Inst);
7750 end Check_Subtype_Conformant;
7752 ---------------------------
7753 -- Check_Type_Conformant --
7754 ---------------------------
7756 procedure Check_Type_Conformant
7757 (New_Id : Entity_Id;
7759 Err_Loc : Node_Id := Empty)
7762 pragma Warnings (Off, Result);
7765 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7766 end Check_Type_Conformant;
7768 ---------------------------
7769 -- Can_Override_Operator --
7770 ---------------------------
7772 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7776 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7780 Typ := Base_Type (Etype (First_Formal (Subp)));
7782 -- Check explicitly that the operation is a primitive of the type
7784 return Operator_Matches_Spec (Subp, Subp)
7785 and then not Is_Generic_Type (Typ)
7786 and then Scope (Subp) = Scope (Typ)
7787 and then not Is_Class_Wide_Type (Typ);
7789 end Can_Override_Operator;
7791 ----------------------
7792 -- Conforming_Types --
7793 ----------------------
7795 function Conforming_Types
7798 Ctype : Conformance_Type;
7799 Get_Inst : Boolean := False) return Boolean
7801 Type_1 : Entity_Id := T1;
7802 Type_2 : Entity_Id := T2;
7803 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7805 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7806 -- If neither T1 nor T2 are generic actual types, or if they are in
7807 -- different scopes (e.g. parent and child instances), then verify that
7808 -- the base types are equal. Otherwise T1 and T2 must be on the same
7809 -- subtype chain. The whole purpose of this procedure is to prevent
7810 -- spurious ambiguities in an instantiation that may arise if two
7811 -- distinct generic types are instantiated with the same actual.
7813 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7814 -- An access parameter can designate an incomplete type. If the
7815 -- incomplete type is the limited view of a type from a limited_
7816 -- with_clause, check whether the non-limited view is available. If
7817 -- it is a (non-limited) incomplete type, get the full view.
7819 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7820 -- Returns True if and only if either T1 denotes a limited view of T2
7821 -- or T2 denotes a limited view of T1. This can arise when the limited
7822 -- with view of a type is used in a subprogram declaration and the
7823 -- subprogram body is in the scope of a regular with clause for the
7824 -- same unit. In such a case, the two type entities can be considered
7825 -- identical for purposes of conformance checking.
7827 ----------------------
7828 -- Base_Types_Match --
7829 ----------------------
7831 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7832 BT1 : constant Entity_Id := Base_Type (T1);
7833 BT2 : constant Entity_Id := Base_Type (T2);
7839 elsif BT1 = BT2 then
7841 -- The following is too permissive. A more precise test should
7842 -- check that the generic actual is an ancestor subtype of the
7845 -- See code in Find_Corresponding_Spec that applies an additional
7846 -- filter to handle accidental amiguities in instances.
7848 return not Is_Generic_Actual_Type (T1)
7849 or else not Is_Generic_Actual_Type (T2)
7850 or else Scope (T1) /= Scope (T2);
7852 -- If T2 is a generic actual type it is declared as the subtype of
7853 -- the actual. If that actual is itself a subtype we need to use its
7854 -- own base type to check for compatibility.
7856 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7859 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7865 end Base_Types_Match;
7867 --------------------------
7868 -- Find_Designated_Type --
7869 --------------------------
7871 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7875 Desig := Directly_Designated_Type (T);
7877 if Ekind (Desig) = E_Incomplete_Type then
7879 -- If regular incomplete type, get full view if available
7881 if Present (Full_View (Desig)) then
7882 Desig := Full_View (Desig);
7884 -- If limited view of a type, get non-limited view if available,
7885 -- and check again for a regular incomplete type.
7887 elsif Present (Non_Limited_View (Desig)) then
7888 Desig := Get_Full_View (Non_Limited_View (Desig));
7893 end Find_Designated_Type;
7895 -------------------------------
7896 -- Matches_Limited_With_View --
7897 -------------------------------
7899 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7901 -- In some cases a type imported through a limited_with clause, and
7902 -- its nonlimited view are both visible, for example in an anonymous
7903 -- access-to-class-wide type in a formal. Both entities designate the
7906 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7909 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7912 elsif From_Limited_With (T1)
7913 and then From_Limited_With (T2)
7914 and then Available_View (T1) = Available_View (T2)
7921 end Matches_Limited_With_View;
7923 -- Start of processing for Conforming_Types
7926 -- The context is an instance association for a formal access-to-
7927 -- subprogram type; the formal parameter types require mapping because
7928 -- they may denote other formal parameters of the generic unit.
7931 Type_1 := Get_Instance_Of (T1);
7932 Type_2 := Get_Instance_Of (T2);
7935 -- If one of the types is a view of the other introduced by a limited
7936 -- with clause, treat these as conforming for all purposes.
7938 if Matches_Limited_With_View (T1, T2) then
7941 elsif Base_Types_Match (Type_1, Type_2) then
7942 return Ctype <= Mode_Conformant
7943 or else Subtypes_Statically_Match (Type_1, Type_2);
7945 elsif Is_Incomplete_Or_Private_Type (Type_1)
7946 and then Present (Full_View (Type_1))
7947 and then Base_Types_Match (Full_View (Type_1), Type_2)
7949 return Ctype <= Mode_Conformant
7950 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7952 elsif Ekind (Type_2) = E_Incomplete_Type
7953 and then Present (Full_View (Type_2))
7954 and then Base_Types_Match (Type_1, Full_View (Type_2))
7956 return Ctype <= Mode_Conformant
7957 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7959 elsif Is_Private_Type (Type_2)
7960 and then In_Instance
7961 and then Present (Full_View (Type_2))
7962 and then Base_Types_Match (Type_1, Full_View (Type_2))
7964 return Ctype <= Mode_Conformant
7965 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7968 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7969 -- treated recursively because they carry a signature. As far as
7970 -- conformance is concerned, convention plays no role, and either
7971 -- or both could be access to protected subprograms.
7973 Are_Anonymous_Access_To_Subprogram_Types :=
7974 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7975 E_Anonymous_Access_Protected_Subprogram_Type)
7977 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7978 E_Anonymous_Access_Protected_Subprogram_Type);
7980 -- Test anonymous access type case. For this case, static subtype
7981 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7982 -- the base types because we may have built internal subtype entities
7983 -- to handle null-excluding types (see Process_Formals).
7985 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7987 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7989 -- Ada 2005 (AI-254)
7991 or else Are_Anonymous_Access_To_Subprogram_Types
7994 Desig_1 : Entity_Id;
7995 Desig_2 : Entity_Id;
7998 -- In Ada 2005, access constant indicators must match for
7999 -- subtype conformance.
8001 if Ada_Version >= Ada_2005
8002 and then Ctype >= Subtype_Conformant
8004 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8009 Desig_1 := Find_Designated_Type (Type_1);
8010 Desig_2 := Find_Designated_Type (Type_2);
8012 -- If the context is an instance association for a formal
8013 -- access-to-subprogram type; formal access parameter designated
8014 -- types require mapping because they may denote other formal
8015 -- parameters of the generic unit.
8018 Desig_1 := Get_Instance_Of (Desig_1);
8019 Desig_2 := Get_Instance_Of (Desig_2);
8022 -- It is possible for a Class_Wide_Type to be introduced for an
8023 -- incomplete type, in which case there is a separate class_ wide
8024 -- type for the full view. The types conform if their Etypes
8025 -- conform, i.e. one may be the full view of the other. This can
8026 -- only happen in the context of an access parameter, other uses
8027 -- of an incomplete Class_Wide_Type are illegal.
8029 if Is_Class_Wide_Type (Desig_1)
8031 Is_Class_Wide_Type (Desig_2)
8035 (Etype (Base_Type (Desig_1)),
8036 Etype (Base_Type (Desig_2)), Ctype);
8038 elsif Are_Anonymous_Access_To_Subprogram_Types then
8039 if Ada_Version < Ada_2005 then
8040 return Ctype = Type_Conformant
8042 Subtypes_Statically_Match (Desig_1, Desig_2);
8044 -- We must check the conformance of the signatures themselves
8048 Conformant : Boolean;
8051 (Desig_1, Desig_2, Ctype, False, Conformant);
8057 return Base_Type (Desig_1) = Base_Type (Desig_2)
8058 and then (Ctype = Type_Conformant
8060 Subtypes_Statically_Match (Desig_1, Desig_2));
8064 -- Otherwise definitely no match
8067 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8068 and then Is_Access_Type (Type_2))
8069 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8070 and then Is_Access_Type (Type_1)))
8073 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8075 May_Hide_Profile := True;
8080 end Conforming_Types;
8082 --------------------------
8083 -- Create_Extra_Formals --
8084 --------------------------
8086 procedure Create_Extra_Formals (E : Entity_Id) is
8088 First_Extra : Entity_Id := Empty;
8089 Last_Extra : Entity_Id;
8090 Formal_Type : Entity_Id;
8091 P_Formal : Entity_Id := Empty;
8093 function Add_Extra_Formal
8094 (Assoc_Entity : Entity_Id;
8097 Suffix : String) return Entity_Id;
8098 -- Add an extra formal to the current list of formals and extra formals.
8099 -- The extra formal is added to the end of the list of extra formals,
8100 -- and also returned as the result. These formals are always of mode IN.
8101 -- The new formal has the type Typ, is declared in Scope, and its name
8102 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8103 -- The following suffixes are currently used. They should not be changed
8104 -- without coordinating with CodePeer, which makes use of these to
8105 -- provide better messages.
8107 -- O denotes the Constrained bit.
8108 -- L denotes the accessibility level.
8109 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8110 -- the full list in exp_ch6.BIP_Formal_Kind.
8112 ----------------------
8113 -- Add_Extra_Formal --
8114 ----------------------
8116 function Add_Extra_Formal
8117 (Assoc_Entity : Entity_Id;
8120 Suffix : String) return Entity_Id
8122 EF : constant Entity_Id :=
8123 Make_Defining_Identifier (Sloc (Assoc_Entity),
8124 Chars => New_External_Name (Chars (Assoc_Entity),
8128 -- A little optimization. Never generate an extra formal for the
8129 -- _init operand of an initialization procedure, since it could
8132 if Chars (Formal) = Name_uInit then
8136 Set_Ekind (EF, E_In_Parameter);
8137 Set_Actual_Subtype (EF, Typ);
8138 Set_Etype (EF, Typ);
8139 Set_Scope (EF, Scope);
8140 Set_Mechanism (EF, Default_Mechanism);
8141 Set_Formal_Validity (EF);
8143 if No (First_Extra) then
8145 Set_Extra_Formals (Scope, First_Extra);
8148 if Present (Last_Extra) then
8149 Set_Extra_Formal (Last_Extra, EF);
8155 end Add_Extra_Formal;
8157 -- Start of processing for Create_Extra_Formals
8160 -- We never generate extra formals if expansion is not active because we
8161 -- don't need them unless we are generating code.
8163 if not Expander_Active then
8167 -- No need to generate extra formals in interface thunks whose target
8168 -- primitive has no extra formals.
8170 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8174 -- If this is a derived subprogram then the subtypes of the parent
8175 -- subprogram's formal parameters will be used to determine the need
8176 -- for extra formals.
8178 if Is_Overloadable (E) and then Present (Alias (E)) then
8179 P_Formal := First_Formal (Alias (E));
8182 Last_Extra := Empty;
8183 Formal := First_Formal (E);
8184 while Present (Formal) loop
8185 Last_Extra := Formal;
8186 Next_Formal (Formal);
8189 -- If Extra_formals were already created, don't do it again. This
8190 -- situation may arise for subprogram types created as part of
8191 -- dispatching calls (see Expand_Dispatching_Call)
8193 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8197 -- If the subprogram is a predefined dispatching subprogram then don't
8198 -- generate any extra constrained or accessibility level formals. In
8199 -- general we suppress these for internal subprograms (by not calling
8200 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8201 -- generated stream attributes do get passed through because extra
8202 -- build-in-place formals are needed in some cases (limited 'Input).
8204 if Is_Predefined_Internal_Operation (E) then
8205 goto Test_For_Func_Result_Extras;
8208 Formal := First_Formal (E);
8209 while Present (Formal) loop
8211 -- Create extra formal for supporting the attribute 'Constrained.
8212 -- The case of a private type view without discriminants also
8213 -- requires the extra formal if the underlying type has defaulted
8216 if Ekind (Formal) /= E_In_Parameter then
8217 if Present (P_Formal) then
8218 Formal_Type := Etype (P_Formal);
8220 Formal_Type := Etype (Formal);
8223 -- Do not produce extra formals for Unchecked_Union parameters.
8224 -- Jump directly to the end of the loop.
8226 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8227 goto Skip_Extra_Formal_Generation;
8230 if not Has_Discriminants (Formal_Type)
8231 and then Ekind (Formal_Type) in Private_Kind
8232 and then Present (Underlying_Type (Formal_Type))
8234 Formal_Type := Underlying_Type (Formal_Type);
8237 -- Suppress the extra formal if formal's subtype is constrained or
8238 -- indefinite, or we're compiling for Ada 2012 and the underlying
8239 -- type is tagged and limited. In Ada 2012, a limited tagged type
8240 -- can have defaulted discriminants, but 'Constrained is required
8241 -- to return True, so the formal is never needed (see AI05-0214).
8242 -- Note that this ensures consistency of calling sequences for
8243 -- dispatching operations when some types in a class have defaults
8244 -- on discriminants and others do not (and requiring the extra
8245 -- formal would introduce distributed overhead).
8247 -- If the type does not have a completion yet, treat as prior to
8248 -- Ada 2012 for consistency.
8250 if Has_Discriminants (Formal_Type)
8251 and then not Is_Constrained (Formal_Type)
8252 and then not Is_Indefinite_Subtype (Formal_Type)
8253 and then (Ada_Version < Ada_2012
8254 or else No (Underlying_Type (Formal_Type))
8256 (Is_Limited_Type (Formal_Type)
8259 (Underlying_Type (Formal_Type)))))
8261 Set_Extra_Constrained
8262 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8266 -- Create extra formal for supporting accessibility checking. This
8267 -- is done for both anonymous access formals and formals of named
8268 -- access types that are marked as controlling formals. The latter
8269 -- case can occur when Expand_Dispatching_Call creates a subprogram
8270 -- type and substitutes the types of access-to-class-wide actuals
8271 -- for the anonymous access-to-specific-type of controlling formals.
8272 -- Base_Type is applied because in cases where there is a null
8273 -- exclusion the formal may have an access subtype.
8275 -- This is suppressed if we specifically suppress accessibility
8276 -- checks at the package level for either the subprogram, or the
8277 -- package in which it resides. However, we do not suppress it
8278 -- simply if the scope has accessibility checks suppressed, since
8279 -- this could cause trouble when clients are compiled with a
8280 -- different suppression setting. The explicit checks at the
8281 -- package level are safe from this point of view.
8283 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8284 or else (Is_Controlling_Formal (Formal)
8285 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8287 (Explicit_Suppress (E, Accessibility_Check)
8289 Explicit_Suppress (Scope (E), Accessibility_Check))
8292 or else Present (Extra_Accessibility (P_Formal)))
8294 Set_Extra_Accessibility
8295 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8298 -- This label is required when skipping extra formal generation for
8299 -- Unchecked_Union parameters.
8301 <<Skip_Extra_Formal_Generation>>
8303 if Present (P_Formal) then
8304 Next_Formal (P_Formal);
8307 Next_Formal (Formal);
8310 <<Test_For_Func_Result_Extras>>
8312 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8313 -- function call is ... determined by the point of call ...".
8315 if Needs_Result_Accessibility_Level (E) then
8316 Set_Extra_Accessibility_Of_Result
8317 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8320 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8321 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8323 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8325 Result_Subt : constant Entity_Id := Etype (E);
8326 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8327 Formal_Typ : Entity_Id;
8329 Discard : Entity_Id;
8330 pragma Warnings (Off, Discard);
8333 -- In the case of functions with unconstrained result subtypes,
8334 -- add a 4-state formal indicating whether the return object is
8335 -- allocated by the caller (1), or should be allocated by the
8336 -- callee on the secondary stack (2), in the global heap (3), or
8337 -- in a user-defined storage pool (4). For the moment we just use
8338 -- Natural for the type of this formal. Note that this formal
8339 -- isn't usually needed in the case where the result subtype is
8340 -- constrained, but it is needed when the function has a tagged
8341 -- result, because generally such functions can be called in a
8342 -- dispatching context and such calls must be handled like calls
8343 -- to a class-wide function.
8345 if Needs_BIP_Alloc_Form (E) then
8348 (E, Standard_Natural,
8349 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8351 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8352 -- use a user-defined pool. This formal is not added on
8353 -- .NET/JVM/ZFP as those targets do not support pools.
8355 if VM_Target = No_VM
8356 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8360 (E, RTE (RE_Root_Storage_Pool_Ptr),
8361 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8365 -- In the case of functions whose result type needs finalization,
8366 -- add an extra formal which represents the finalization master.
8368 if Needs_BIP_Finalization_Master (E) then
8371 (E, RTE (RE_Finalization_Master_Ptr),
8372 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8375 -- When the result type contains tasks, add two extra formals: the
8376 -- master of the tasks to be created, and the caller's activation
8379 if Has_Task (Full_Subt) then
8382 (E, RTE (RE_Master_Id),
8383 E, BIP_Formal_Suffix (BIP_Task_Master));
8386 (E, RTE (RE_Activation_Chain_Access),
8387 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8390 -- All build-in-place functions get an extra formal that will be
8391 -- passed the address of the return object within the caller.
8394 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8396 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8397 Set_Etype (Formal_Typ, Formal_Typ);
8398 Set_Depends_On_Private
8399 (Formal_Typ, Has_Private_Component (Formal_Typ));
8400 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8401 Set_Is_Access_Constant (Formal_Typ, False);
8403 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8404 -- the designated type comes from the limited view (for back-end
8407 Set_From_Limited_With
8408 (Formal_Typ, From_Limited_With (Result_Subt));
8410 Layout_Type (Formal_Typ);
8414 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8417 end Create_Extra_Formals;
8419 -----------------------------
8420 -- Enter_Overloaded_Entity --
8421 -----------------------------
8423 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8424 E : Entity_Id := Current_Entity_In_Scope (S);
8425 C_E : Entity_Id := Current_Entity (S);
8429 Set_Has_Homonym (E);
8430 Set_Has_Homonym (S);
8433 Set_Is_Immediately_Visible (S);
8434 Set_Scope (S, Current_Scope);
8436 -- Chain new entity if front of homonym in current scope, so that
8437 -- homonyms are contiguous.
8439 if Present (E) and then E /= C_E then
8440 while Homonym (C_E) /= E loop
8441 C_E := Homonym (C_E);
8444 Set_Homonym (C_E, S);
8448 Set_Current_Entity (S);
8453 if Is_Inherited_Operation (S) then
8454 Append_Inherited_Subprogram (S);
8456 Append_Entity (S, Current_Scope);
8459 Set_Public_Status (S);
8461 if Debug_Flag_E then
8462 Write_Str ("New overloaded entity chain: ");
8463 Write_Name (Chars (S));
8466 while Present (E) loop
8467 Write_Str (" "); Write_Int (Int (E));
8474 -- Generate warning for hiding
8477 and then Comes_From_Source (S)
8478 and then In_Extended_Main_Source_Unit (S)
8485 -- Warn unless genuine overloading. Do not emit warning on
8486 -- hiding predefined operators in Standard (these are either an
8487 -- (artifact of our implicit declarations, or simple noise) but
8488 -- keep warning on a operator defined on a local subtype, because
8489 -- of the real danger that different operators may be applied in
8490 -- various parts of the program.
8492 -- Note that if E and S have the same scope, there is never any
8493 -- hiding. Either the two conflict, and the program is illegal,
8494 -- or S is overriding an implicit inherited subprogram.
8496 if Scope (E) /= Scope (S)
8497 and then (not Is_Overloadable (E)
8498 or else Subtype_Conformant (E, S))
8499 and then (Is_Immediately_Visible (E)
8501 Is_Potentially_Use_Visible (S))
8503 if Scope (E) /= Standard_Standard then
8504 Error_Msg_Sloc := Sloc (E);
8505 Error_Msg_N ("declaration of & hides one#?h?", S);
8507 elsif Nkind (S) = N_Defining_Operator_Symbol
8509 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8512 ("declaration of & hides predefined operator?h?", S);
8517 end Enter_Overloaded_Entity;
8519 -----------------------------
8520 -- Check_Untagged_Equality --
8521 -----------------------------
8523 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8524 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8525 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8529 -- This check applies only if we have a subprogram declaration with a
8530 -- non-tagged record type.
8532 if Nkind (Decl) /= N_Subprogram_Declaration
8533 or else not Is_Record_Type (Typ)
8534 or else Is_Tagged_Type (Typ)
8539 -- In Ada 2012 case, we will output errors or warnings depending on
8540 -- the setting of debug flag -gnatd.E.
8542 if Ada_Version >= Ada_2012 then
8543 Error_Msg_Warn := Debug_Flag_Dot_EE;
8545 -- In earlier versions of Ada, nothing to do unless we are warning on
8546 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8549 if not Warn_On_Ada_2012_Compatibility then
8554 -- Cases where the type has already been frozen
8556 if Is_Frozen (Typ) then
8558 -- If the type is not declared in a package, or if we are in the body
8559 -- of the package or in some other scope, the new operation is not
8560 -- primitive, and therefore legal, though suspicious. Should we
8561 -- generate a warning in this case ???
8563 if Ekind (Scope (Typ)) /= E_Package
8564 or else Scope (Typ) /= Current_Scope
8568 -- If the type is a generic actual (sub)type, the operation is not
8569 -- primitive either because the base type is declared elsewhere.
8571 elsif Is_Generic_Actual_Type (Typ) then
8574 -- Here we have a definite error of declaration after freezing
8577 if Ada_Version >= Ada_2012 then
8579 ("equality operator must be declared before type& is "
8580 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8582 -- In Ada 2012 mode with error turned to warning, output one
8583 -- more warning to warn that the equality operation may not
8584 -- compose. This is the consequence of ignoring the error.
8586 if Error_Msg_Warn then
8587 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8592 ("equality operator must be declared before type& is "
8593 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8596 -- If we are in the package body, we could just move the
8597 -- declaration to the package spec, so add a message saying that.
8599 if In_Package_Body (Scope (Typ)) then
8600 if Ada_Version >= Ada_2012 then
8602 ("\move declaration to package spec<<", Eq_Op);
8605 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8608 -- Otherwise try to find the freezing point
8611 Obj_Decl := Next (Parent (Typ));
8612 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8613 if Nkind (Obj_Decl) = N_Object_Declaration
8614 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8616 -- Freezing point, output warnings
8618 if Ada_Version >= Ada_2012 then
8620 ("type& is frozen by declaration??", Obj_Decl, Typ);
8622 ("\an equality operator cannot be declared after "
8627 ("type& is frozen by declaration (Ada 2012)?y?",
8630 ("\an equality operator cannot be declared after "
8631 & "this point (Ada 2012)?y?",
8643 -- Here if type is not frozen yet. It is illegal to have a primitive
8644 -- equality declared in the private part if the type is visible.
8646 elsif not In_Same_List (Parent (Typ), Decl)
8647 and then not Is_Limited_Type (Typ)
8649 -- Shouldn't we give an RM reference here???
8651 if Ada_Version >= Ada_2012 then
8653 ("equality operator appears too late<<", Eq_Op);
8656 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8659 -- No error detected
8664 end Check_Untagged_Equality;
8666 -----------------------------
8667 -- Find_Corresponding_Spec --
8668 -----------------------------
8670 function Find_Corresponding_Spec
8672 Post_Error : Boolean := True) return Entity_Id
8674 Spec : constant Node_Id := Specification (N);
8675 Designator : constant Entity_Id := Defining_Entity (Spec);
8679 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8680 -- Even if fully conformant, a body may depend on a generic actual when
8681 -- the spec does not, or vice versa, in which case they were distinct
8682 -- entities in the generic.
8684 -------------------------------
8685 -- Different_Generic_Profile --
8686 -------------------------------
8688 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8691 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8692 -- Check that the types of corresponding formals have the same
8693 -- generic actual if any. We have to account for subtypes of a
8694 -- generic formal, declared between a spec and a body, which may
8695 -- appear distinct in an instance but matched in the generic.
8697 -------------------------
8698 -- Same_Generic_Actual --
8699 -------------------------
8701 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8703 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8705 (Present (Parent (T1))
8706 and then Comes_From_Source (Parent (T1))
8707 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8708 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8709 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8710 end Same_Generic_Actual;
8712 -- Start of processing for Different_Generic_Profile
8715 if not In_Instance then
8718 elsif Ekind (E) = E_Function
8719 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8724 F1 := First_Formal (Designator);
8725 F2 := First_Formal (E);
8726 while Present (F1) loop
8727 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8736 end Different_Generic_Profile;
8738 -- Start of processing for Find_Corresponding_Spec
8741 E := Current_Entity (Designator);
8742 while Present (E) loop
8744 -- We are looking for a matching spec. It must have the same scope,
8745 -- and the same name, and either be type conformant, or be the case
8746 -- of a library procedure spec and its body (which belong to one
8747 -- another regardless of whether they are type conformant or not).
8749 if Scope (E) = Current_Scope then
8750 if Current_Scope = Standard_Standard
8751 or else (Ekind (E) = Ekind (Designator)
8752 and then Type_Conformant (E, Designator))
8754 -- Within an instantiation, we know that spec and body are
8755 -- subtype conformant, because they were subtype conformant in
8756 -- the generic. We choose the subtype-conformant entity here as
8757 -- well, to resolve spurious ambiguities in the instance that
8758 -- were not present in the generic (i.e. when two different
8759 -- types are given the same actual). If we are looking for a
8760 -- spec to match a body, full conformance is expected.
8763 Set_Convention (Designator, Convention (E));
8765 -- Skip past subprogram bodies and subprogram renamings that
8766 -- may appear to have a matching spec, but that aren't fully
8767 -- conformant with it. That can occur in cases where an
8768 -- actual type causes unrelated homographs in the instance.
8770 if Nkind_In (N, N_Subprogram_Body,
8771 N_Subprogram_Renaming_Declaration)
8772 and then Present (Homonym (E))
8773 and then not Fully_Conformant (Designator, E)
8777 elsif not Subtype_Conformant (Designator, E) then
8780 elsif Different_Generic_Profile (E) then
8785 -- Ada 2012 (AI05-0165): For internally generated bodies of
8786 -- null procedures locate the internally generated spec. We
8787 -- enforce mode conformance since a tagged type may inherit
8788 -- from interfaces several null primitives which differ only
8789 -- in the mode of the formals.
8791 if not (Comes_From_Source (E))
8792 and then Is_Null_Procedure (E)
8793 and then not Mode_Conformant (Designator, E)
8797 -- For null procedures coming from source that are completions,
8798 -- analysis of the generated body will establish the link.
8800 elsif Comes_From_Source (E)
8801 and then Nkind (Spec) = N_Procedure_Specification
8802 and then Null_Present (Spec)
8806 elsif not Has_Completion (E) then
8807 if Nkind (N) /= N_Subprogram_Body_Stub then
8808 Set_Corresponding_Spec (N, E);
8811 Set_Has_Completion (E);
8814 elsif Nkind (Parent (N)) = N_Subunit then
8816 -- If this is the proper body of a subunit, the completion
8817 -- flag is set when analyzing the stub.
8821 -- If E is an internal function with a controlling result that
8822 -- was created for an operation inherited by a null extension,
8823 -- it may be overridden by a body without a previous spec (one
8824 -- more reason why these should be shunned). In that case we
8825 -- remove the generated body if present, because the current
8826 -- one is the explicit overriding.
8828 elsif Ekind (E) = E_Function
8829 and then Ada_Version >= Ada_2005
8830 and then not Comes_From_Source (E)
8831 and then Has_Controlling_Result (E)
8832 and then Is_Null_Extension (Etype (E))
8833 and then Comes_From_Source (Spec)
8835 Set_Has_Completion (E, False);
8838 and then Nkind (Parent (E)) = N_Function_Specification
8841 (Unit_Declaration_Node
8842 (Corresponding_Body (Unit_Declaration_Node (E))));
8846 -- If expansion is disabled, or if the wrapper function has
8847 -- not been generated yet, this a late body overriding an
8848 -- inherited operation, or it is an overriding by some other
8849 -- declaration before the controlling result is frozen. In
8850 -- either case this is a declaration of a new entity.
8856 -- If the body already exists, then this is an error unless
8857 -- the previous declaration is the implicit declaration of a
8858 -- derived subprogram. It is also legal for an instance to
8859 -- contain type conformant overloadable declarations (but the
8860 -- generic declaration may not), per 8.3(26/2).
8862 elsif No (Alias (E))
8863 and then not Is_Intrinsic_Subprogram (E)
8864 and then not In_Instance
8867 Error_Msg_Sloc := Sloc (E);
8869 if Is_Imported (E) then
8871 ("body not allowed for imported subprogram & declared#",
8874 Error_Msg_NE ("duplicate body for & declared#", N, E);
8878 -- Child units cannot be overloaded, so a conformance mismatch
8879 -- between body and a previous spec is an error.
8881 elsif Is_Child_Unit (E)
8883 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8885 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8890 ("body of child unit does not match previous declaration", N);
8898 -- On exit, we know that no previous declaration of subprogram exists
8901 end Find_Corresponding_Spec;
8903 ----------------------
8904 -- Fully_Conformant --
8905 ----------------------
8907 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8910 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8912 end Fully_Conformant;
8914 ----------------------------------
8915 -- Fully_Conformant_Expressions --
8916 ----------------------------------
8918 function Fully_Conformant_Expressions
8919 (Given_E1 : Node_Id;
8920 Given_E2 : Node_Id) return Boolean
8922 E1 : constant Node_Id := Original_Node (Given_E1);
8923 E2 : constant Node_Id := Original_Node (Given_E2);
8924 -- We always test conformance on original nodes, since it is possible
8925 -- for analysis and/or expansion to make things look as though they
8926 -- conform when they do not, e.g. by converting 1+2 into 3.
8928 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8929 renames Fully_Conformant_Expressions;
8931 function FCL (L1, L2 : List_Id) return Boolean;
8932 -- Compare elements of two lists for conformance. Elements have to be
8933 -- conformant, and actuals inserted as default parameters do not match
8934 -- explicit actuals with the same value.
8936 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8937 -- Compare an operator node with a function call
8943 function FCL (L1, L2 : List_Id) return Boolean is
8947 if L1 = No_List then
8953 if L2 = No_List then
8959 -- Compare two lists, skipping rewrite insertions (we want to compare
8960 -- the original trees, not the expanded versions).
8963 if Is_Rewrite_Insertion (N1) then
8965 elsif Is_Rewrite_Insertion (N2) then
8971 elsif not FCE (N1, N2) then
8984 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8985 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8990 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8995 Act := First (Actuals);
8997 if Nkind (Op_Node) in N_Binary_Op then
8998 if not FCE (Left_Opnd (Op_Node), Act) then
9005 return Present (Act)
9006 and then FCE (Right_Opnd (Op_Node), Act)
9007 and then No (Next (Act));
9011 -- Start of processing for Fully_Conformant_Expressions
9014 -- Non-conformant if paren count does not match. Note: if some idiot
9015 -- complains that we don't do this right for more than 3 levels of
9016 -- parentheses, they will be treated with the respect they deserve.
9018 if Paren_Count (E1) /= Paren_Count (E2) then
9021 -- If same entities are referenced, then they are conformant even if
9022 -- they have different forms (RM 8.3.1(19-20)).
9024 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9025 if Present (Entity (E1)) then
9026 return Entity (E1) = Entity (E2)
9027 or else (Chars (Entity (E1)) = Chars (Entity (E2))
9028 and then Ekind (Entity (E1)) = E_Discriminant
9029 and then Ekind (Entity (E2)) = E_In_Parameter);
9031 elsif Nkind (E1) = N_Expanded_Name
9032 and then Nkind (E2) = N_Expanded_Name
9033 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9034 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9036 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9039 -- Identifiers in component associations don't always have
9040 -- entities, but their names must conform.
9042 return Nkind (E1) = N_Identifier
9043 and then Nkind (E2) = N_Identifier
9044 and then Chars (E1) = Chars (E2);
9047 elsif Nkind (E1) = N_Character_Literal
9048 and then Nkind (E2) = N_Expanded_Name
9050 return Nkind (Selector_Name (E2)) = N_Character_Literal
9051 and then Chars (E1) = Chars (Selector_Name (E2));
9053 elsif Nkind (E2) = N_Character_Literal
9054 and then Nkind (E1) = N_Expanded_Name
9056 return Nkind (Selector_Name (E1)) = N_Character_Literal
9057 and then Chars (E2) = Chars (Selector_Name (E1));
9059 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9060 return FCO (E1, E2);
9062 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9063 return FCO (E2, E1);
9065 -- Otherwise we must have the same syntactic entity
9067 elsif Nkind (E1) /= Nkind (E2) then
9070 -- At this point, we specialize by node type
9077 FCL (Expressions (E1), Expressions (E2))
9079 FCL (Component_Associations (E1),
9080 Component_Associations (E2));
9083 if Nkind (Expression (E1)) = N_Qualified_Expression
9085 Nkind (Expression (E2)) = N_Qualified_Expression
9087 return FCE (Expression (E1), Expression (E2));
9089 -- Check that the subtype marks and any constraints
9094 Indic1 : constant Node_Id := Expression (E1);
9095 Indic2 : constant Node_Id := Expression (E2);
9100 if Nkind (Indic1) /= N_Subtype_Indication then
9102 Nkind (Indic2) /= N_Subtype_Indication
9103 and then Entity (Indic1) = Entity (Indic2);
9105 elsif Nkind (Indic2) /= N_Subtype_Indication then
9107 Nkind (Indic1) /= N_Subtype_Indication
9108 and then Entity (Indic1) = Entity (Indic2);
9111 if Entity (Subtype_Mark (Indic1)) /=
9112 Entity (Subtype_Mark (Indic2))
9117 Elt1 := First (Constraints (Constraint (Indic1)));
9118 Elt2 := First (Constraints (Constraint (Indic2)));
9119 while Present (Elt1) and then Present (Elt2) loop
9120 if not FCE (Elt1, Elt2) then
9133 when N_Attribute_Reference =>
9135 Attribute_Name (E1) = Attribute_Name (E2)
9136 and then FCL (Expressions (E1), Expressions (E2));
9140 Entity (E1) = Entity (E2)
9141 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9142 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9144 when N_Short_Circuit | N_Membership_Test =>
9146 FCE (Left_Opnd (E1), Left_Opnd (E2))
9148 FCE (Right_Opnd (E1), Right_Opnd (E2));
9150 when N_Case_Expression =>
9156 if not FCE (Expression (E1), Expression (E2)) then
9160 Alt1 := First (Alternatives (E1));
9161 Alt2 := First (Alternatives (E2));
9163 if Present (Alt1) /= Present (Alt2) then
9165 elsif No (Alt1) then
9169 if not FCE (Expression (Alt1), Expression (Alt2))
9170 or else not FCL (Discrete_Choices (Alt1),
9171 Discrete_Choices (Alt2))
9182 when N_Character_Literal =>
9184 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9186 when N_Component_Association =>
9188 FCL (Choices (E1), Choices (E2))
9190 FCE (Expression (E1), Expression (E2));
9192 when N_Explicit_Dereference =>
9194 FCE (Prefix (E1), Prefix (E2));
9196 when N_Extension_Aggregate =>
9198 FCL (Expressions (E1), Expressions (E2))
9199 and then Null_Record_Present (E1) =
9200 Null_Record_Present (E2)
9201 and then FCL (Component_Associations (E1),
9202 Component_Associations (E2));
9204 when N_Function_Call =>
9206 FCE (Name (E1), Name (E2))
9208 FCL (Parameter_Associations (E1),
9209 Parameter_Associations (E2));
9211 when N_If_Expression =>
9213 FCL (Expressions (E1), Expressions (E2));
9215 when N_Indexed_Component =>
9217 FCE (Prefix (E1), Prefix (E2))
9219 FCL (Expressions (E1), Expressions (E2));
9221 when N_Integer_Literal =>
9222 return (Intval (E1) = Intval (E2));
9227 when N_Operator_Symbol =>
9229 Chars (E1) = Chars (E2);
9231 when N_Others_Choice =>
9234 when N_Parameter_Association =>
9236 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9237 and then FCE (Explicit_Actual_Parameter (E1),
9238 Explicit_Actual_Parameter (E2));
9240 when N_Qualified_Expression =>
9242 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9244 FCE (Expression (E1), Expression (E2));
9246 when N_Quantified_Expression =>
9247 if not FCE (Condition (E1), Condition (E2)) then
9251 if Present (Loop_Parameter_Specification (E1))
9252 and then Present (Loop_Parameter_Specification (E2))
9255 L1 : constant Node_Id :=
9256 Loop_Parameter_Specification (E1);
9257 L2 : constant Node_Id :=
9258 Loop_Parameter_Specification (E2);
9262 Reverse_Present (L1) = Reverse_Present (L2)
9264 FCE (Defining_Identifier (L1),
9265 Defining_Identifier (L2))
9267 FCE (Discrete_Subtype_Definition (L1),
9268 Discrete_Subtype_Definition (L2));
9271 elsif Present (Iterator_Specification (E1))
9272 and then Present (Iterator_Specification (E2))
9275 I1 : constant Node_Id := Iterator_Specification (E1);
9276 I2 : constant Node_Id := Iterator_Specification (E2);
9280 FCE (Defining_Identifier (I1),
9281 Defining_Identifier (I2))
9283 Of_Present (I1) = Of_Present (I2)
9285 Reverse_Present (I1) = Reverse_Present (I2)
9286 and then FCE (Name (I1), Name (I2))
9287 and then FCE (Subtype_Indication (I1),
9288 Subtype_Indication (I2));
9291 -- The quantified expressions used different specifications to
9292 -- walk their respective ranges.
9300 FCE (Low_Bound (E1), Low_Bound (E2))
9302 FCE (High_Bound (E1), High_Bound (E2));
9304 when N_Real_Literal =>
9305 return (Realval (E1) = Realval (E2));
9307 when N_Selected_Component =>
9309 FCE (Prefix (E1), Prefix (E2))
9311 FCE (Selector_Name (E1), Selector_Name (E2));
9315 FCE (Prefix (E1), Prefix (E2))
9317 FCE (Discrete_Range (E1), Discrete_Range (E2));
9319 when N_String_Literal =>
9321 S1 : constant String_Id := Strval (E1);
9322 S2 : constant String_Id := Strval (E2);
9323 L1 : constant Nat := String_Length (S1);
9324 L2 : constant Nat := String_Length (S2);
9331 for J in 1 .. L1 loop
9332 if Get_String_Char (S1, J) /=
9333 Get_String_Char (S2, J)
9343 when N_Type_Conversion =>
9345 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9347 FCE (Expression (E1), Expression (E2));
9351 Entity (E1) = Entity (E2)
9353 FCE (Right_Opnd (E1), Right_Opnd (E2));
9355 when N_Unchecked_Type_Conversion =>
9357 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9359 FCE (Expression (E1), Expression (E2));
9361 -- All other node types cannot appear in this context. Strictly
9362 -- we should raise a fatal internal error. Instead we just ignore
9363 -- the nodes. This means that if anyone makes a mistake in the
9364 -- expander and mucks an expression tree irretrievably, the result
9365 -- will be a failure to detect a (probably very obscure) case
9366 -- of non-conformance, which is better than bombing on some
9367 -- case where two expressions do in fact conform.
9374 end Fully_Conformant_Expressions;
9376 ----------------------------------------
9377 -- Fully_Conformant_Discrete_Subtypes --
9378 ----------------------------------------
9380 function Fully_Conformant_Discrete_Subtypes
9381 (Given_S1 : Node_Id;
9382 Given_S2 : Node_Id) return Boolean
9384 S1 : constant Node_Id := Original_Node (Given_S1);
9385 S2 : constant Node_Id := Original_Node (Given_S2);
9387 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9388 -- Special-case for a bound given by a discriminant, which in the body
9389 -- is replaced with the discriminal of the enclosing type.
9391 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9392 -- Check both bounds
9394 -----------------------
9395 -- Conforming_Bounds --
9396 -----------------------
9398 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9400 if Is_Entity_Name (B1)
9401 and then Is_Entity_Name (B2)
9402 and then Ekind (Entity (B1)) = E_Discriminant
9404 return Chars (B1) = Chars (B2);
9407 return Fully_Conformant_Expressions (B1, B2);
9409 end Conforming_Bounds;
9411 -----------------------
9412 -- Conforming_Ranges --
9413 -----------------------
9415 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9418 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9420 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9421 end Conforming_Ranges;
9423 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9426 if Nkind (S1) /= Nkind (S2) then
9429 elsif Is_Entity_Name (S1) then
9430 return Entity (S1) = Entity (S2);
9432 elsif Nkind (S1) = N_Range then
9433 return Conforming_Ranges (S1, S2);
9435 elsif Nkind (S1) = N_Subtype_Indication then
9437 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9440 (Range_Expression (Constraint (S1)),
9441 Range_Expression (Constraint (S2)));
9445 end Fully_Conformant_Discrete_Subtypes;
9447 --------------------
9448 -- Install_Entity --
9449 --------------------
9451 procedure Install_Entity (E : Entity_Id) is
9452 Prev : constant Entity_Id := Current_Entity (E);
9454 Set_Is_Immediately_Visible (E);
9455 Set_Current_Entity (E);
9456 Set_Homonym (E, Prev);
9459 ---------------------
9460 -- Install_Formals --
9461 ---------------------
9463 procedure Install_Formals (Id : Entity_Id) is
9466 F := First_Formal (Id);
9467 while Present (F) loop
9471 end Install_Formals;
9473 -----------------------------
9474 -- Is_Interface_Conformant --
9475 -----------------------------
9477 function Is_Interface_Conformant
9478 (Tagged_Type : Entity_Id;
9479 Iface_Prim : Entity_Id;
9480 Prim : Entity_Id) return Boolean
9482 -- The operation may in fact be an inherited (implicit) operation
9483 -- rather than the original interface primitive, so retrieve the
9484 -- ultimate ancestor.
9486 Iface : constant Entity_Id :=
9487 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9488 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9490 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9491 -- Return the controlling formal of Prim
9493 ------------------------
9494 -- Controlling_Formal --
9495 ------------------------
9497 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9501 E := First_Entity (Prim);
9502 while Present (E) loop
9503 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9511 end Controlling_Formal;
9515 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9516 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9518 -- Start of processing for Is_Interface_Conformant
9521 pragma Assert (Is_Subprogram (Iface_Prim)
9522 and then Is_Subprogram (Prim)
9523 and then Is_Dispatching_Operation (Iface_Prim)
9524 and then Is_Dispatching_Operation (Prim));
9526 pragma Assert (Is_Interface (Iface)
9527 or else (Present (Alias (Iface_Prim))
9530 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9532 if Prim = Iface_Prim
9533 or else not Is_Subprogram (Prim)
9534 or else Ekind (Prim) /= Ekind (Iface_Prim)
9535 or else not Is_Dispatching_Operation (Prim)
9536 or else Scope (Prim) /= Scope (Tagged_Type)
9538 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9539 or else not Primitive_Names_Match (Iface_Prim, Prim)
9543 -- The mode of the controlling formals must match
9545 elsif Present (Iface_Ctrl_F)
9546 and then Present (Prim_Ctrl_F)
9547 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9551 -- Case of a procedure, or a function whose result type matches the
9552 -- result type of the interface primitive, or a function that has no
9553 -- controlling result (I or access I).
9555 elsif Ekind (Iface_Prim) = E_Procedure
9556 or else Etype (Prim) = Etype (Iface_Prim)
9557 or else not Has_Controlling_Result (Prim)
9559 return Type_Conformant
9560 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9562 -- Case of a function returning an interface, or an access to one. Check
9563 -- that the return types correspond.
9565 elsif Implements_Interface (Typ, Iface) then
9566 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9568 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9573 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9574 Skip_Controlling_Formals => True);
9580 end Is_Interface_Conformant;
9582 ---------------------------------
9583 -- Is_Non_Overriding_Operation --
9584 ---------------------------------
9586 function Is_Non_Overriding_Operation
9587 (Prev_E : Entity_Id;
9588 New_E : Entity_Id) return Boolean
9592 G_Typ : Entity_Id := Empty;
9594 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9595 -- If F_Type is a derived type associated with a generic actual subtype,
9596 -- then return its Generic_Parent_Type attribute, else return Empty.
9598 function Types_Correspond
9599 (P_Type : Entity_Id;
9600 N_Type : Entity_Id) return Boolean;
9601 -- Returns true if and only if the types (or designated types in the
9602 -- case of anonymous access types) are the same or N_Type is derived
9603 -- directly or indirectly from P_Type.
9605 -----------------------------
9606 -- Get_Generic_Parent_Type --
9607 -----------------------------
9609 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9615 if Is_Derived_Type (F_Typ)
9616 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9618 -- The tree must be traversed to determine the parent subtype in
9619 -- the generic unit, which unfortunately isn't always available
9620 -- via semantic attributes. ??? (Note: The use of Original_Node
9621 -- is needed for cases where a full derived type has been
9624 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9625 if Nkind (Defn) = N_Derived_Type_Definition then
9626 Indic := Subtype_Indication (Defn);
9628 if Nkind (Indic) = N_Subtype_Indication then
9629 G_Typ := Entity (Subtype_Mark (Indic));
9631 G_Typ := Entity (Indic);
9634 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9635 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9637 return Generic_Parent_Type (Parent (G_Typ));
9643 end Get_Generic_Parent_Type;
9645 ----------------------
9646 -- Types_Correspond --
9647 ----------------------
9649 function Types_Correspond
9650 (P_Type : Entity_Id;
9651 N_Type : Entity_Id) return Boolean
9653 Prev_Type : Entity_Id := Base_Type (P_Type);
9654 New_Type : Entity_Id := Base_Type (N_Type);
9657 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9658 Prev_Type := Designated_Type (Prev_Type);
9661 if Ekind (New_Type) = E_Anonymous_Access_Type then
9662 New_Type := Designated_Type (New_Type);
9665 if Prev_Type = New_Type then
9668 elsif not Is_Class_Wide_Type (New_Type) then
9669 while Etype (New_Type) /= New_Type loop
9670 New_Type := Etype (New_Type);
9671 if New_Type = Prev_Type then
9677 end Types_Correspond;
9679 -- Start of processing for Is_Non_Overriding_Operation
9682 -- In the case where both operations are implicit derived subprograms
9683 -- then neither overrides the other. This can only occur in certain
9684 -- obscure cases (e.g., derivation from homographs created in a generic
9687 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9690 elsif Ekind (Current_Scope) = E_Package
9691 and then Is_Generic_Instance (Current_Scope)
9692 and then In_Private_Part (Current_Scope)
9693 and then Comes_From_Source (New_E)
9695 -- We examine the formals and result type of the inherited operation,
9696 -- to determine whether their type is derived from (the instance of)
9697 -- a generic type. The first such formal or result type is the one
9700 Formal := First_Formal (Prev_E);
9701 while Present (Formal) loop
9702 F_Typ := Base_Type (Etype (Formal));
9704 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9705 F_Typ := Designated_Type (F_Typ);
9708 G_Typ := Get_Generic_Parent_Type (F_Typ);
9709 exit when Present (G_Typ);
9711 Next_Formal (Formal);
9714 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9715 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9722 -- If the generic type is a private type, then the original operation
9723 -- was not overriding in the generic, because there was no primitive
9724 -- operation to override.
9726 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9727 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9728 N_Formal_Private_Type_Definition
9732 -- The generic parent type is the ancestor of a formal derived
9733 -- type declaration. We need to check whether it has a primitive
9734 -- operation that should be overridden by New_E in the generic.
9738 P_Formal : Entity_Id;
9739 N_Formal : Entity_Id;
9743 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9746 while Present (Prim_Elt) loop
9747 P_Prim := Node (Prim_Elt);
9749 if Chars (P_Prim) = Chars (New_E)
9750 and then Ekind (P_Prim) = Ekind (New_E)
9752 P_Formal := First_Formal (P_Prim);
9753 N_Formal := First_Formal (New_E);
9754 while Present (P_Formal) and then Present (N_Formal) loop
9755 P_Typ := Etype (P_Formal);
9756 N_Typ := Etype (N_Formal);
9758 if not Types_Correspond (P_Typ, N_Typ) then
9762 Next_Entity (P_Formal);
9763 Next_Entity (N_Formal);
9766 -- Found a matching primitive operation belonging to the
9767 -- formal ancestor type, so the new subprogram is
9771 and then No (N_Formal)
9772 and then (Ekind (New_E) /= E_Function
9775 (Etype (P_Prim), Etype (New_E)))
9781 Next_Elmt (Prim_Elt);
9784 -- If no match found, then the new subprogram does not override
9785 -- in the generic (nor in the instance).
9787 -- If the type in question is not abstract, and the subprogram
9788 -- is, this will be an error if the new operation is in the
9789 -- private part of the instance. Emit a warning now, which will
9790 -- make the subsequent error message easier to understand.
9792 if not Is_Abstract_Type (F_Typ)
9793 and then Is_Abstract_Subprogram (Prev_E)
9794 and then In_Private_Part (Current_Scope)
9796 Error_Msg_Node_2 := F_Typ;
9798 ("private operation& in generic unit does not override " &
9799 "any primitive operation of& (RM 12.3 (18))??",
9809 end Is_Non_Overriding_Operation;
9811 -------------------------------------
9812 -- List_Inherited_Pre_Post_Aspects --
9813 -------------------------------------
9815 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9817 if Opt.List_Inherited_Aspects
9818 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9821 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9825 for J in Inherited'Range loop
9826 P := Pre_Post_Conditions (Contract (Inherited (J)));
9827 while Present (P) loop
9828 Error_Msg_Sloc := Sloc (P);
9830 if Class_Present (P) and then not Split_PPC (P) then
9831 if Pragma_Name (P) = Name_Precondition then
9833 ("info: & inherits `Pre''Class` aspect from #?L?",
9837 ("info: & inherits `Post''Class` aspect from #?L?",
9842 P := Next_Pragma (P);
9847 end List_Inherited_Pre_Post_Aspects;
9849 ------------------------------
9850 -- Make_Inequality_Operator --
9851 ------------------------------
9853 -- S is the defining identifier of an equality operator. We build a
9854 -- subprogram declaration with the right signature. This operation is
9855 -- intrinsic, because it is always expanded as the negation of the
9856 -- call to the equality function.
9858 procedure Make_Inequality_Operator (S : Entity_Id) is
9859 Loc : constant Source_Ptr := Sloc (S);
9862 Op_Name : Entity_Id;
9864 FF : constant Entity_Id := First_Formal (S);
9865 NF : constant Entity_Id := Next_Formal (FF);
9868 -- Check that equality was properly defined, ignore call if not
9875 A : constant Entity_Id :=
9876 Make_Defining_Identifier (Sloc (FF),
9877 Chars => Chars (FF));
9879 B : constant Entity_Id :=
9880 Make_Defining_Identifier (Sloc (NF),
9881 Chars => Chars (NF));
9884 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9886 Formals := New_List (
9887 Make_Parameter_Specification (Loc,
9888 Defining_Identifier => A,
9890 New_Occurrence_Of (Etype (First_Formal (S)),
9891 Sloc (Etype (First_Formal (S))))),
9893 Make_Parameter_Specification (Loc,
9894 Defining_Identifier => B,
9896 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9897 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9900 Make_Subprogram_Declaration (Loc,
9902 Make_Function_Specification (Loc,
9903 Defining_Unit_Name => Op_Name,
9904 Parameter_Specifications => Formals,
9905 Result_Definition =>
9906 New_Occurrence_Of (Standard_Boolean, Loc)));
9908 -- Insert inequality right after equality if it is explicit or after
9909 -- the derived type when implicit. These entities are created only
9910 -- for visibility purposes, and eventually replaced in the course
9911 -- of expansion, so they do not need to be attached to the tree and
9912 -- seen by the back-end. Keeping them internal also avoids spurious
9913 -- freezing problems. The declaration is inserted in the tree for
9914 -- analysis, and removed afterwards. If the equality operator comes
9915 -- from an explicit declaration, attach the inequality immediately
9916 -- after. Else the equality is inherited from a derived type
9917 -- declaration, so insert inequality after that declaration.
9919 if No (Alias (S)) then
9920 Insert_After (Unit_Declaration_Node (S), Decl);
9921 elsif Is_List_Member (Parent (S)) then
9922 Insert_After (Parent (S), Decl);
9924 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9927 Mark_Rewrite_Insertion (Decl);
9928 Set_Is_Intrinsic_Subprogram (Op_Name);
9931 Set_Has_Completion (Op_Name);
9932 Set_Corresponding_Equality (Op_Name, S);
9933 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9935 end Make_Inequality_Operator;
9937 ----------------------
9938 -- May_Need_Actuals --
9939 ----------------------
9941 procedure May_Need_Actuals (Fun : Entity_Id) is
9946 F := First_Formal (Fun);
9948 while Present (F) loop
9949 if No (Default_Value (F)) then
9957 Set_Needs_No_Actuals (Fun, B);
9958 end May_Need_Actuals;
9960 ---------------------
9961 -- Mode_Conformant --
9962 ---------------------
9964 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9967 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9969 end Mode_Conformant;
9971 ---------------------------
9972 -- New_Overloaded_Entity --
9973 ---------------------------
9975 procedure New_Overloaded_Entity
9977 Derived_Type : Entity_Id := Empty)
9979 Overridden_Subp : Entity_Id := Empty;
9980 -- Set if the current scope has an operation that is type-conformant
9981 -- with S, and becomes hidden by S.
9983 Is_Primitive_Subp : Boolean;
9984 -- Set to True if the new subprogram is primitive
9987 -- Entity that S overrides
9989 Prev_Vis : Entity_Id := Empty;
9990 -- Predecessor of E in Homonym chain
9992 procedure Check_For_Primitive_Subprogram
9993 (Is_Primitive : out Boolean;
9994 Is_Overriding : Boolean := False);
9995 -- If the subprogram being analyzed is a primitive operation of the type
9996 -- of a formal or result, set the Has_Primitive_Operations flag on the
9997 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9998 -- corresponding flag on the entity itself for later use.
10000 procedure Check_Synchronized_Overriding
10001 (Def_Id : Entity_Id;
10002 Overridden_Subp : out Entity_Id);
10003 -- First determine if Def_Id is an entry or a subprogram either defined
10004 -- in the scope of a task or protected type, or is a primitive of such
10005 -- a type. Check whether Def_Id overrides a subprogram of an interface
10006 -- implemented by the synchronized type, return the overridden entity
10009 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10010 -- Check that E is declared in the private part of the current package,
10011 -- or in the package body, where it may hide a previous declaration.
10012 -- We can't use In_Private_Part by itself because this flag is also
10013 -- set when freezing entities, so we must examine the place of the
10014 -- declaration in the tree, and recognize wrapper packages as well.
10016 function Is_Overriding_Alias
10017 (Old_E : Entity_Id;
10018 New_E : Entity_Id) return Boolean;
10019 -- Check whether new subprogram and old subprogram are both inherited
10020 -- from subprograms that have distinct dispatch table entries. This can
10021 -- occur with derivations from instances with accidental homonyms. The
10022 -- function is conservative given that the converse is only true within
10023 -- instances that contain accidental overloadings.
10025 ------------------------------------
10026 -- Check_For_Primitive_Subprogram --
10027 ------------------------------------
10029 procedure Check_For_Primitive_Subprogram
10030 (Is_Primitive : out Boolean;
10031 Is_Overriding : Boolean := False)
10033 Formal : Entity_Id;
10037 function Visible_Part_Type (T : Entity_Id) return Boolean;
10038 -- Returns true if T is declared in the visible part of the current
10039 -- package scope; otherwise returns false. Assumes that T is declared
10042 procedure Check_Private_Overriding (T : Entity_Id);
10043 -- Checks that if a primitive abstract subprogram of a visible
10044 -- abstract type is declared in a private part, then it must override
10045 -- an abstract subprogram declared in the visible part. Also checks
10046 -- that if a primitive function with a controlling result is declared
10047 -- in a private part, then it must override a function declared in
10048 -- the visible part.
10050 ------------------------------
10051 -- Check_Private_Overriding --
10052 ------------------------------
10054 procedure Check_Private_Overriding (T : Entity_Id) is
10056 if Is_Package_Or_Generic_Package (Current_Scope)
10057 and then In_Private_Part (Current_Scope)
10058 and then Visible_Part_Type (T)
10059 and then not In_Instance
10061 if Is_Abstract_Type (T)
10062 and then Is_Abstract_Subprogram (S)
10063 and then (not Is_Overriding
10064 or else not Is_Abstract_Subprogram (E))
10067 ("abstract subprograms must be visible "
10068 & "(RM 3.9.3(10))!", S);
10070 elsif Ekind (S) = E_Function and then not Is_Overriding then
10071 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
10073 ("private function with tagged result must"
10074 & " override visible-part function", S);
10076 ("\move subprogram to the visible part"
10077 & " (RM 3.9.3(10))", S);
10079 -- AI05-0073: extend this test to the case of a function
10080 -- with a controlling access result.
10082 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10083 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10085 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
10086 and then Ada_Version >= Ada_2012
10089 ("private function with controlling access result "
10090 & "must override visible-part function", S);
10092 ("\move subprogram to the visible part"
10093 & " (RM 3.9.3(10))", S);
10097 end Check_Private_Overriding;
10099 -----------------------
10100 -- Visible_Part_Type --
10101 -----------------------
10103 function Visible_Part_Type (T : Entity_Id) return Boolean is
10104 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10108 -- If the entity is a private type, then it must be declared in a
10111 if Ekind (T) in Private_Kind then
10115 -- Otherwise, we traverse the visible part looking for its
10116 -- corresponding declaration. We cannot use the declaration
10117 -- node directly because in the private part the entity of a
10118 -- private type is the one in the full view, which does not
10119 -- indicate that it is the completion of something visible.
10121 N := First (Visible_Declarations (Specification (P)));
10122 while Present (N) loop
10123 if Nkind (N) = N_Full_Type_Declaration
10124 and then Present (Defining_Identifier (N))
10125 and then T = Defining_Identifier (N)
10129 elsif Nkind_In (N, N_Private_Type_Declaration,
10130 N_Private_Extension_Declaration)
10131 and then Present (Defining_Identifier (N))
10132 and then T = Full_View (Defining_Identifier (N))
10141 end Visible_Part_Type;
10143 -- Start of processing for Check_For_Primitive_Subprogram
10146 Is_Primitive := False;
10148 if not Comes_From_Source (S) then
10151 -- If subprogram is at library level, it is not primitive operation
10153 elsif Current_Scope = Standard_Standard then
10156 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10157 and then not In_Package_Body (Current_Scope))
10158 or else Is_Overriding
10160 -- For function, check return type
10162 if Ekind (S) = E_Function then
10163 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10164 F_Typ := Designated_Type (Etype (S));
10166 F_Typ := Etype (S);
10169 B_Typ := Base_Type (F_Typ);
10171 if Scope (B_Typ) = Current_Scope
10172 and then not Is_Class_Wide_Type (B_Typ)
10173 and then not Is_Generic_Type (B_Typ)
10175 Is_Primitive := True;
10176 Set_Has_Primitive_Operations (B_Typ);
10177 Set_Is_Primitive (S);
10178 Check_Private_Overriding (B_Typ);
10182 -- For all subprograms, check formals
10184 Formal := First_Formal (S);
10185 while Present (Formal) loop
10186 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10187 F_Typ := Designated_Type (Etype (Formal));
10189 F_Typ := Etype (Formal);
10192 B_Typ := Base_Type (F_Typ);
10194 if Ekind (B_Typ) = E_Access_Subtype then
10195 B_Typ := Base_Type (B_Typ);
10198 if Scope (B_Typ) = Current_Scope
10199 and then not Is_Class_Wide_Type (B_Typ)
10200 and then not Is_Generic_Type (B_Typ)
10202 Is_Primitive := True;
10203 Set_Is_Primitive (S);
10204 Set_Has_Primitive_Operations (B_Typ);
10205 Check_Private_Overriding (B_Typ);
10208 Next_Formal (Formal);
10211 -- Special case: An equality function can be redefined for a type
10212 -- occurring in a declarative part, and won't otherwise be treated as
10213 -- a primitive because it doesn't occur in a package spec and doesn't
10214 -- override an inherited subprogram. It's important that we mark it
10215 -- primitive so it can be returned by Collect_Primitive_Operations
10216 -- and be used in composing the equality operation of later types
10217 -- that have a component of the type.
10219 elsif Chars (S) = Name_Op_Eq
10220 and then Etype (S) = Standard_Boolean
10222 B_Typ := Base_Type (Etype (First_Formal (S)));
10224 if Scope (B_Typ) = Current_Scope
10226 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10227 and then not Is_Limited_Type (B_Typ)
10229 Is_Primitive := True;
10230 Set_Is_Primitive (S);
10231 Set_Has_Primitive_Operations (B_Typ);
10232 Check_Private_Overriding (B_Typ);
10235 end Check_For_Primitive_Subprogram;
10237 -----------------------------------
10238 -- Check_Synchronized_Overriding --
10239 -----------------------------------
10241 procedure Check_Synchronized_Overriding
10242 (Def_Id : Entity_Id;
10243 Overridden_Subp : out Entity_Id)
10245 Ifaces_List : Elist_Id;
10246 In_Scope : Boolean;
10249 function Matches_Prefixed_View_Profile
10250 (Prim_Params : List_Id;
10251 Iface_Params : List_Id) return Boolean;
10252 -- Determine whether a subprogram's parameter profile Prim_Params
10253 -- matches that of a potentially overridden interface subprogram
10254 -- Iface_Params. Also determine if the type of first parameter of
10255 -- Iface_Params is an implemented interface.
10257 -----------------------------------
10258 -- Matches_Prefixed_View_Profile --
10259 -----------------------------------
10261 function Matches_Prefixed_View_Profile
10262 (Prim_Params : List_Id;
10263 Iface_Params : List_Id) return Boolean
10265 Iface_Id : Entity_Id;
10266 Iface_Param : Node_Id;
10267 Iface_Typ : Entity_Id;
10268 Prim_Id : Entity_Id;
10269 Prim_Param : Node_Id;
10270 Prim_Typ : Entity_Id;
10272 function Is_Implemented
10273 (Ifaces_List : Elist_Id;
10274 Iface : Entity_Id) return Boolean;
10275 -- Determine if Iface is implemented by the current task or
10278 --------------------
10279 -- Is_Implemented --
10280 --------------------
10282 function Is_Implemented
10283 (Ifaces_List : Elist_Id;
10284 Iface : Entity_Id) return Boolean
10286 Iface_Elmt : Elmt_Id;
10289 Iface_Elmt := First_Elmt (Ifaces_List);
10290 while Present (Iface_Elmt) loop
10291 if Node (Iface_Elmt) = Iface then
10295 Next_Elmt (Iface_Elmt);
10299 end Is_Implemented;
10301 -- Start of processing for Matches_Prefixed_View_Profile
10304 Iface_Param := First (Iface_Params);
10305 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
10307 if Is_Access_Type (Iface_Typ) then
10308 Iface_Typ := Designated_Type (Iface_Typ);
10311 Prim_Param := First (Prim_Params);
10313 -- The first parameter of the potentially overridden subprogram
10314 -- must be an interface implemented by Prim.
10316 if not Is_Interface (Iface_Typ)
10317 or else not Is_Implemented (Ifaces_List, Iface_Typ)
10322 -- The checks on the object parameters are done, move onto the
10323 -- rest of the parameters.
10325 if not In_Scope then
10326 Prim_Param := Next (Prim_Param);
10329 Iface_Param := Next (Iface_Param);
10330 while Present (Iface_Param) and then Present (Prim_Param) loop
10331 Iface_Id := Defining_Identifier (Iface_Param);
10332 Iface_Typ := Find_Parameter_Type (Iface_Param);
10334 Prim_Id := Defining_Identifier (Prim_Param);
10335 Prim_Typ := Find_Parameter_Type (Prim_Param);
10337 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
10338 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
10339 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
10341 Iface_Typ := Designated_Type (Iface_Typ);
10342 Prim_Typ := Designated_Type (Prim_Typ);
10345 -- Case of multiple interface types inside a parameter profile
10347 -- (Obj_Param : in out Iface; ...; Param : Iface)
10349 -- If the interface type is implemented, then the matching type
10350 -- in the primitive should be the implementing record type.
10352 if Ekind (Iface_Typ) = E_Record_Type
10353 and then Is_Interface (Iface_Typ)
10354 and then Is_Implemented (Ifaces_List, Iface_Typ)
10356 if Prim_Typ /= Typ then
10360 -- The two parameters must be both mode and subtype conformant
10362 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
10364 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
10369 Next (Iface_Param);
10373 -- One of the two lists contains more parameters than the other
10375 if Present (Iface_Param) or else Present (Prim_Param) then
10380 end Matches_Prefixed_View_Profile;
10382 -- Start of processing for Check_Synchronized_Overriding
10385 Overridden_Subp := Empty;
10387 -- Def_Id must be an entry or a subprogram. We should skip predefined
10388 -- primitives internally generated by the frontend; however at this
10389 -- stage predefined primitives are still not fully decorated. As a
10390 -- minor optimization we skip here internally generated subprograms.
10392 if (Ekind (Def_Id) /= E_Entry
10393 and then Ekind (Def_Id) /= E_Function
10394 and then Ekind (Def_Id) /= E_Procedure)
10395 or else not Comes_From_Source (Def_Id)
10400 -- Search for the concurrent declaration since it contains the list
10401 -- of all implemented interfaces. In this case, the subprogram is
10402 -- declared within the scope of a protected or a task type.
10404 if Present (Scope (Def_Id))
10405 and then Is_Concurrent_Type (Scope (Def_Id))
10406 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10408 Typ := Scope (Def_Id);
10411 -- The enclosing scope is not a synchronized type and the subprogram
10414 elsif No (First_Formal (Def_Id)) then
10417 -- The subprogram has formals and hence it may be a primitive of a
10418 -- concurrent type.
10421 Typ := Etype (First_Formal (Def_Id));
10423 if Is_Access_Type (Typ) then
10424 Typ := Directly_Designated_Type (Typ);
10427 if Is_Concurrent_Type (Typ)
10428 and then not Is_Generic_Actual_Type (Typ)
10432 -- This case occurs when the concurrent type is declared within
10433 -- a generic unit. As a result the corresponding record has been
10434 -- built and used as the type of the first formal, we just have
10435 -- to retrieve the corresponding concurrent type.
10437 elsif Is_Concurrent_Record_Type (Typ)
10438 and then not Is_Class_Wide_Type (Typ)
10439 and then Present (Corresponding_Concurrent_Type (Typ))
10441 Typ := Corresponding_Concurrent_Type (Typ);
10449 -- There is no overriding to check if is an inherited operation in a
10450 -- type derivation on for a generic actual.
10452 Collect_Interfaces (Typ, Ifaces_List);
10454 if Is_Empty_Elmt_List (Ifaces_List) then
10458 -- Determine whether entry or subprogram Def_Id overrides a primitive
10459 -- operation that belongs to one of the interfaces in Ifaces_List.
10462 Candidate : Entity_Id := Empty;
10463 Hom : Entity_Id := Empty;
10464 Iface_Typ : Entity_Id;
10465 Subp : Entity_Id := Empty;
10468 -- Traverse the homonym chain, looking for a potentially
10469 -- overridden subprogram that belongs to an implemented
10472 Hom := Current_Entity_In_Scope (Def_Id);
10473 while Present (Hom) loop
10477 or else not Is_Overloadable (Subp)
10478 or else not Is_Primitive (Subp)
10479 or else not Is_Dispatching_Operation (Subp)
10480 or else not Present (Find_Dispatching_Type (Subp))
10481 or else not Is_Interface (Find_Dispatching_Type (Subp))
10485 -- Entries and procedures can override abstract or null
10486 -- interface procedures.
10488 elsif (Ekind (Def_Id) = E_Procedure
10489 or else Ekind (Def_Id) = E_Entry)
10490 and then Ekind (Subp) = E_Procedure
10491 and then Matches_Prefixed_View_Profile
10492 (Parameter_Specifications (Parent (Def_Id)),
10493 Parameter_Specifications (Parent (Subp)))
10497 -- For an overridden subprogram Subp, check whether the mode
10498 -- of its first parameter is correct depending on the kind
10499 -- of synchronized type.
10502 Formal : constant Node_Id := First_Formal (Candidate);
10505 -- In order for an entry or a protected procedure to
10506 -- override, the first parameter of the overridden
10507 -- routine must be of mode "out", "in out" or
10508 -- access-to-variable.
10510 if Ekind_In (Candidate, E_Entry, E_Procedure)
10511 and then Is_Protected_Type (Typ)
10512 and then Ekind (Formal) /= E_In_Out_Parameter
10513 and then Ekind (Formal) /= E_Out_Parameter
10514 and then Nkind (Parameter_Type (Parent (Formal))) /=
10515 N_Access_Definition
10519 -- All other cases are OK since a task entry or routine
10520 -- does not have a restriction on the mode of the first
10521 -- parameter of the overridden interface routine.
10524 Overridden_Subp := Candidate;
10529 -- Functions can override abstract interface functions
10531 elsif Ekind (Def_Id) = E_Function
10532 and then Ekind (Subp) = E_Function
10533 and then Matches_Prefixed_View_Profile
10534 (Parameter_Specifications (Parent (Def_Id)),
10535 Parameter_Specifications (Parent (Subp)))
10536 and then Etype (Result_Definition (Parent (Def_Id))) =
10537 Etype (Result_Definition (Parent (Subp)))
10539 Overridden_Subp := Subp;
10543 Hom := Homonym (Hom);
10546 -- After examining all candidates for overriding, we are left with
10547 -- the best match which is a mode incompatible interface routine.
10548 -- Do not emit an error if the Expander is active since this error
10549 -- will be detected later on after all concurrent types are
10550 -- expanded and all wrappers are built. This check is meant for
10551 -- spec-only compilations.
10553 if Present (Candidate) and then not Expander_Active then
10555 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10557 -- Def_Id is primitive of a protected type, declared inside the
10558 -- type, and the candidate is primitive of a limited or
10559 -- synchronized interface.
10562 and then Is_Protected_Type (Typ)
10564 (Is_Limited_Interface (Iface_Typ)
10565 or else Is_Protected_Interface (Iface_Typ)
10566 or else Is_Synchronized_Interface (Iface_Typ)
10567 or else Is_Task_Interface (Iface_Typ))
10569 Error_Msg_PT (Parent (Typ), Candidate);
10573 Overridden_Subp := Candidate;
10576 end Check_Synchronized_Overriding;
10578 ----------------------------
10579 -- Is_Private_Declaration --
10580 ----------------------------
10582 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10583 Priv_Decls : List_Id;
10584 Decl : constant Node_Id := Unit_Declaration_Node (E);
10587 if Is_Package_Or_Generic_Package (Current_Scope)
10588 and then In_Private_Part (Current_Scope)
10591 Private_Declarations (Package_Specification (Current_Scope));
10593 return In_Package_Body (Current_Scope)
10595 (Is_List_Member (Decl)
10596 and then List_Containing (Decl) = Priv_Decls)
10597 or else (Nkind (Parent (Decl)) = N_Package_Specification
10599 Is_Compilation_Unit
10600 (Defining_Entity (Parent (Decl)))
10601 and then List_Containing (Parent (Parent (Decl))) =
10606 end Is_Private_Declaration;
10608 --------------------------
10609 -- Is_Overriding_Alias --
10610 --------------------------
10612 function Is_Overriding_Alias
10613 (Old_E : Entity_Id;
10614 New_E : Entity_Id) return Boolean
10616 AO : constant Entity_Id := Alias (Old_E);
10617 AN : constant Entity_Id := Alias (New_E);
10620 return Scope (AO) /= Scope (AN)
10621 or else No (DTC_Entity (AO))
10622 or else No (DTC_Entity (AN))
10623 or else DT_Position (AO) = DT_Position (AN);
10624 end Is_Overriding_Alias;
10626 -- Start of processing for New_Overloaded_Entity
10629 -- We need to look for an entity that S may override. This must be a
10630 -- homonym in the current scope, so we look for the first homonym of
10631 -- S in the current scope as the starting point for the search.
10633 E := Current_Entity_In_Scope (S);
10635 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10636 -- They are directly added to the list of primitive operations of
10637 -- Derived_Type, unless this is a rederivation in the private part
10638 -- of an operation that was already derived in the visible part of
10639 -- the current package.
10641 if Ada_Version >= Ada_2005
10642 and then Present (Derived_Type)
10643 and then Present (Alias (S))
10644 and then Is_Dispatching_Operation (Alias (S))
10645 and then Present (Find_Dispatching_Type (Alias (S)))
10646 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10648 -- For private types, when the full-view is processed we propagate to
10649 -- the full view the non-overridden entities whose attribute "alias"
10650 -- references an interface primitive. These entities were added by
10651 -- Derive_Subprograms to ensure that interface primitives are
10654 -- Inside_Freeze_Actions is non zero when S corresponds with an
10655 -- internal entity that links an interface primitive with its
10656 -- covering primitive through attribute Interface_Alias (see
10657 -- Add_Internal_Interface_Entities).
10659 if Inside_Freezing_Actions = 0
10660 and then Is_Package_Or_Generic_Package (Current_Scope)
10661 and then In_Private_Part (Current_Scope)
10662 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10663 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10664 and then Full_View (Defining_Identifier (Parent (E)))
10665 = Defining_Identifier (Parent (S))
10666 and then Alias (E) = Alias (S)
10668 Check_Operation_From_Private_View (S, E);
10669 Set_Is_Dispatching_Operation (S);
10674 Enter_Overloaded_Entity (S);
10675 Check_Dispatching_Operation (S, Empty);
10676 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10682 -- If there is no homonym then this is definitely not overriding
10685 Enter_Overloaded_Entity (S);
10686 Check_Dispatching_Operation (S, Empty);
10687 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10689 -- If subprogram has an explicit declaration, check whether it has an
10690 -- overriding indicator.
10692 if Comes_From_Source (S) then
10693 Check_Synchronized_Overriding (S, Overridden_Subp);
10695 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10696 -- it may have overridden some hidden inherited primitive. Update
10697 -- Overridden_Subp to avoid spurious errors when checking the
10698 -- overriding indicator.
10700 if Ada_Version >= Ada_2012
10701 and then No (Overridden_Subp)
10702 and then Is_Dispatching_Operation (S)
10703 and then Present (Overridden_Operation (S))
10705 Overridden_Subp := Overridden_Operation (S);
10708 Check_Overriding_Indicator
10709 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10712 -- If there is a homonym that is not overloadable, then we have an
10713 -- error, except for the special cases checked explicitly below.
10715 elsif not Is_Overloadable (E) then
10717 -- Check for spurious conflict produced by a subprogram that has the
10718 -- same name as that of the enclosing generic package. The conflict
10719 -- occurs within an instance, between the subprogram and the renaming
10720 -- declaration for the package. After the subprogram, the package
10721 -- renaming declaration becomes hidden.
10723 if Ekind (E) = E_Package
10724 and then Present (Renamed_Object (E))
10725 and then Renamed_Object (E) = Current_Scope
10726 and then Nkind (Parent (Renamed_Object (E))) =
10727 N_Package_Specification
10728 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10731 Set_Is_Immediately_Visible (E, False);
10732 Enter_Overloaded_Entity (S);
10733 Set_Homonym (S, Homonym (E));
10734 Check_Dispatching_Operation (S, Empty);
10735 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10737 -- If the subprogram is implicit it is hidden by the previous
10738 -- declaration. However if it is dispatching, it must appear in the
10739 -- dispatch table anyway, because it can be dispatched to even if it
10740 -- cannot be called directly.
10742 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10743 Set_Scope (S, Current_Scope);
10745 if Is_Dispatching_Operation (Alias (S)) then
10746 Check_Dispatching_Operation (S, Empty);
10752 Error_Msg_Sloc := Sloc (E);
10754 -- Generate message, with useful additional warning if in generic
10756 if Is_Generic_Unit (E) then
10757 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10758 Error_Msg_N ("\& conflicts with declaration#", S);
10760 Error_Msg_N ("& conflicts with declaration#", S);
10766 -- E exists and is overloadable
10769 Check_Synchronized_Overriding (S, Overridden_Subp);
10771 -- Loop through E and its homonyms to determine if any of them is
10772 -- the candidate for overriding by S.
10774 while Present (E) loop
10776 -- Definitely not interesting if not in the current scope
10778 if Scope (E) /= Current_Scope then
10781 -- A function can overload the name of an abstract state. The
10782 -- state can be viewed as a function with a profile that cannot
10783 -- be matched by anything.
10785 elsif Ekind (S) = E_Function
10786 and then Ekind (E) = E_Abstract_State
10788 Enter_Overloaded_Entity (S);
10791 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10792 -- procedures locate the internally generated spec. We enforce
10793 -- mode conformance since a tagged type may inherit from
10794 -- interfaces several null primitives which differ only in
10795 -- the mode of the formals.
10797 elsif not Comes_From_Source (S)
10798 and then Is_Null_Procedure (S)
10799 and then not Mode_Conformant (E, S)
10803 -- Check if we have type conformance
10805 elsif Type_Conformant (E, S) then
10807 -- If the old and new entities have the same profile and one
10808 -- is not the body of the other, then this is an error, unless
10809 -- one of them is implicitly declared.
10811 -- There are some cases when both can be implicit, for example
10812 -- when both a literal and a function that overrides it are
10813 -- inherited in a derivation, or when an inherited operation
10814 -- of a tagged full type overrides the inherited operation of
10815 -- a private extension. Ada 83 had a special rule for the
10816 -- literal case. In Ada 95, the later implicit operation hides
10817 -- the former, and the literal is always the former. In the
10818 -- odd case where both are derived operations declared at the
10819 -- same point, both operations should be declared, and in that
10820 -- case we bypass the following test and proceed to the next
10821 -- part. This can only occur for certain obscure cases in
10822 -- instances, when an operation on a type derived from a formal
10823 -- private type does not override a homograph inherited from
10824 -- the actual. In subsequent derivations of such a type, the
10825 -- DT positions of these operations remain distinct, if they
10828 if Present (Alias (S))
10829 and then (No (Alias (E))
10830 or else Comes_From_Source (E)
10831 or else Is_Abstract_Subprogram (S)
10833 (Is_Dispatching_Operation (E)
10834 and then Is_Overriding_Alias (E, S)))
10835 and then Ekind (E) /= E_Enumeration_Literal
10837 -- When an derived operation is overloaded it may be due to
10838 -- the fact that the full view of a private extension
10839 -- re-inherits. It has to be dealt with.
10841 if Is_Package_Or_Generic_Package (Current_Scope)
10842 and then In_Private_Part (Current_Scope)
10844 Check_Operation_From_Private_View (S, E);
10847 -- In any case the implicit operation remains hidden by the
10848 -- existing declaration, which is overriding. Indicate that
10849 -- E overrides the operation from which S is inherited.
10851 if Present (Alias (S)) then
10852 Set_Overridden_Operation (E, Alias (S));
10854 Set_Overridden_Operation (E, S);
10857 if Comes_From_Source (E) then
10858 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10863 -- Within an instance, the renaming declarations for actual
10864 -- subprograms may become ambiguous, but they do not hide each
10867 elsif Ekind (E) /= E_Entry
10868 and then not Comes_From_Source (E)
10869 and then not Is_Generic_Instance (E)
10870 and then (Present (Alias (E))
10871 or else Is_Intrinsic_Subprogram (E))
10872 and then (not In_Instance
10873 or else No (Parent (E))
10874 or else Nkind (Unit_Declaration_Node (E)) /=
10875 N_Subprogram_Renaming_Declaration)
10877 -- A subprogram child unit is not allowed to override an
10878 -- inherited subprogram (10.1.1(20)).
10880 if Is_Child_Unit (S) then
10882 ("child unit overrides inherited subprogram in parent",
10887 if Is_Non_Overriding_Operation (E, S) then
10888 Enter_Overloaded_Entity (S);
10890 if No (Derived_Type)
10891 or else Is_Tagged_Type (Derived_Type)
10893 Check_Dispatching_Operation (S, Empty);
10899 -- E is a derived operation or an internal operator which
10900 -- is being overridden. Remove E from further visibility.
10901 -- Furthermore, if E is a dispatching operation, it must be
10902 -- replaced in the list of primitive operations of its type
10903 -- (see Override_Dispatching_Operation).
10905 Overridden_Subp := E;
10911 Prev := First_Entity (Current_Scope);
10912 while Present (Prev) and then Next_Entity (Prev) /= E loop
10913 Next_Entity (Prev);
10916 -- It is possible for E to be in the current scope and
10917 -- yet not in the entity chain. This can only occur in a
10918 -- generic context where E is an implicit concatenation
10919 -- in the formal part, because in a generic body the
10920 -- entity chain starts with the formals.
10923 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10925 -- E must be removed both from the entity_list of the
10926 -- current scope, and from the visibility chain
10928 if Debug_Flag_E then
10929 Write_Str ("Override implicit operation ");
10930 Write_Int (Int (E));
10934 -- If E is a predefined concatenation, it stands for four
10935 -- different operations. As a result, a single explicit
10936 -- declaration does not hide it. In a possible ambiguous
10937 -- situation, Disambiguate chooses the user-defined op,
10938 -- so it is correct to retain the previous internal one.
10940 if Chars (E) /= Name_Op_Concat
10941 or else Ekind (E) /= E_Operator
10943 -- For nondispatching derived operations that are
10944 -- overridden by a subprogram declared in the private
10945 -- part of a package, we retain the derived subprogram
10946 -- but mark it as not immediately visible. If the
10947 -- derived operation was declared in the visible part
10948 -- then this ensures that it will still be visible
10949 -- outside the package with the proper signature
10950 -- (calls from outside must also be directed to this
10951 -- version rather than the overriding one, unlike the
10952 -- dispatching case). Calls from inside the package
10953 -- will still resolve to the overriding subprogram
10954 -- since the derived one is marked as not visible
10955 -- within the package.
10957 -- If the private operation is dispatching, we achieve
10958 -- the overriding by keeping the implicit operation
10959 -- but setting its alias to be the overriding one. In
10960 -- this fashion the proper body is executed in all
10961 -- cases, but the original signature is used outside
10964 -- If the overriding is not in the private part, we
10965 -- remove the implicit operation altogether.
10967 if Is_Private_Declaration (S) then
10968 if not Is_Dispatching_Operation (E) then
10969 Set_Is_Immediately_Visible (E, False);
10971 -- Work done in Override_Dispatching_Operation,
10972 -- so nothing else needs to be done here.
10978 -- Find predecessor of E in Homonym chain
10980 if E = Current_Entity (E) then
10983 Prev_Vis := Current_Entity (E);
10984 while Homonym (Prev_Vis) /= E loop
10985 Prev_Vis := Homonym (Prev_Vis);
10989 if Prev_Vis /= Empty then
10991 -- Skip E in the visibility chain
10993 Set_Homonym (Prev_Vis, Homonym (E));
10996 Set_Name_Entity_Id (Chars (E), Homonym (E));
10999 Set_Next_Entity (Prev, Next_Entity (E));
11001 if No (Next_Entity (Prev)) then
11002 Set_Last_Entity (Current_Scope, Prev);
11007 Enter_Overloaded_Entity (S);
11009 -- For entities generated by Derive_Subprograms the
11010 -- overridden operation is the inherited primitive
11011 -- (which is available through the attribute alias).
11013 if not (Comes_From_Source (E))
11014 and then Is_Dispatching_Operation (E)
11015 and then Find_Dispatching_Type (E) =
11016 Find_Dispatching_Type (S)
11017 and then Present (Alias (E))
11018 and then Comes_From_Source (Alias (E))
11020 Set_Overridden_Operation (S, Alias (E));
11022 -- Normal case of setting entity as overridden
11024 -- Note: Static_Initialization and Overridden_Operation
11025 -- attributes use the same field in subprogram entities.
11026 -- Static_Initialization is only defined for internal
11027 -- initialization procedures, where Overridden_Operation
11028 -- is irrelevant. Therefore the setting of this attribute
11029 -- must check whether the target is an init_proc.
11031 elsif not Is_Init_Proc (S) then
11032 Set_Overridden_Operation (S, E);
11035 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11037 -- If S is a user-defined subprogram or a null procedure
11038 -- expanded to override an inherited null procedure, or a
11039 -- predefined dispatching primitive then indicate that E
11040 -- overrides the operation from which S is inherited.
11042 if Comes_From_Source (S)
11044 (Present (Parent (S))
11046 Nkind (Parent (S)) = N_Procedure_Specification
11048 Null_Present (Parent (S)))
11050 (Present (Alias (E))
11052 Is_Predefined_Dispatching_Operation (Alias (E)))
11054 if Present (Alias (E)) then
11055 Set_Overridden_Operation (S, Alias (E));
11059 if Is_Dispatching_Operation (E) then
11061 -- An overriding dispatching subprogram inherits the
11062 -- convention of the overridden subprogram (AI-117).
11064 Set_Convention (S, Convention (E));
11065 Check_Dispatching_Operation (S, E);
11068 Check_Dispatching_Operation (S, Empty);
11071 Check_For_Primitive_Subprogram
11072 (Is_Primitive_Subp, Is_Overriding => True);
11073 goto Check_Inequality;
11076 -- Apparent redeclarations in instances can occur when two
11077 -- formal types get the same actual type. The subprograms in
11078 -- in the instance are legal, even if not callable from the
11079 -- outside. Calls from within are disambiguated elsewhere.
11080 -- For dispatching operations in the visible part, the usual
11081 -- rules apply, and operations with the same profile are not
11082 -- legal (B830001).
11084 elsif (In_Instance_Visible_Part
11085 and then not Is_Dispatching_Operation (E))
11086 or else In_Instance_Not_Visible
11090 -- Here we have a real error (identical profile)
11093 Error_Msg_Sloc := Sloc (E);
11095 -- Avoid cascaded errors if the entity appears in
11096 -- subsequent calls.
11098 Set_Scope (S, Current_Scope);
11100 -- Generate error, with extra useful warning for the case
11101 -- of a generic instance with no completion.
11103 if Is_Generic_Instance (S)
11104 and then not Has_Completion (E)
11107 ("instantiation cannot provide body for&", S);
11108 Error_Msg_N ("\& conflicts with declaration#", S);
11110 Error_Msg_N ("& conflicts with declaration#", S);
11117 -- If one subprogram has an access parameter and the other
11118 -- a parameter of an access type, calls to either might be
11119 -- ambiguous. Verify that parameters match except for the
11120 -- access parameter.
11122 if May_Hide_Profile then
11128 F1 := First_Formal (S);
11129 F2 := First_Formal (E);
11130 while Present (F1) and then Present (F2) loop
11131 if Is_Access_Type (Etype (F1)) then
11132 if not Is_Access_Type (Etype (F2))
11133 or else not Conforming_Types
11134 (Designated_Type (Etype (F1)),
11135 Designated_Type (Etype (F2)),
11138 May_Hide_Profile := False;
11142 not Conforming_Types
11143 (Etype (F1), Etype (F2), Type_Conformant)
11145 May_Hide_Profile := False;
11152 if May_Hide_Profile
11156 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11165 -- On exit, we know that S is a new entity
11167 Enter_Overloaded_Entity (S);
11168 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11169 Check_Overriding_Indicator
11170 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11172 -- Overloading is not allowed in SPARK, except for operators
11174 if Nkind (S) /= N_Defining_Operator_Symbol then
11175 Error_Msg_Sloc := Sloc (Homonym (S));
11176 Check_SPARK_Restriction
11177 ("overloading not allowed with entity#", S);
11180 -- If S is a derived operation for an untagged type then by
11181 -- definition it's not a dispatching operation (even if the parent
11182 -- operation was dispatching), so Check_Dispatching_Operation is not
11183 -- called in that case.
11185 if No (Derived_Type)
11186 or else Is_Tagged_Type (Derived_Type)
11188 Check_Dispatching_Operation (S, Empty);
11192 -- If this is a user-defined equality operator that is not a derived
11193 -- subprogram, create the corresponding inequality. If the operation is
11194 -- dispatching, the expansion is done elsewhere, and we do not create
11195 -- an explicit inequality operation.
11197 <<Check_Inequality>>
11198 if Chars (S) = Name_Op_Eq
11199 and then Etype (S) = Standard_Boolean
11200 and then Present (Parent (S))
11201 and then not Is_Dispatching_Operation (S)
11203 Make_Inequality_Operator (S);
11204 Check_Untagged_Equality (S);
11206 end New_Overloaded_Entity;
11208 ---------------------
11209 -- Process_Formals --
11210 ---------------------
11212 procedure Process_Formals
11214 Related_Nod : Node_Id)
11216 Param_Spec : Node_Id;
11217 Formal : Entity_Id;
11218 Formal_Type : Entity_Id;
11222 Num_Out_Params : Nat := 0;
11223 First_Out_Param : Entity_Id := Empty;
11224 -- Used for setting Is_Only_Out_Parameter
11226 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11227 -- Determine whether an access type designates a type coming from a
11230 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11231 -- Check whether the default has a class-wide type. After analysis the
11232 -- default has the type of the formal, so we must also check explicitly
11233 -- for an access attribute.
11235 ----------------------------------
11236 -- Designates_From_Limited_With --
11237 ----------------------------------
11239 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11240 Desig : Entity_Id := Typ;
11243 if Is_Access_Type (Desig) then
11244 Desig := Directly_Designated_Type (Desig);
11247 if Is_Class_Wide_Type (Desig) then
11248 Desig := Root_Type (Desig);
11252 Ekind (Desig) = E_Incomplete_Type
11253 and then From_Limited_With (Desig);
11254 end Designates_From_Limited_With;
11256 ---------------------------
11257 -- Is_Class_Wide_Default --
11258 ---------------------------
11260 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11262 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11263 or else (Nkind (D) = N_Attribute_Reference
11264 and then Attribute_Name (D) = Name_Access
11265 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11266 end Is_Class_Wide_Default;
11268 -- Start of processing for Process_Formals
11271 -- In order to prevent premature use of the formals in the same formal
11272 -- part, the Ekind is left undefined until all default expressions are
11273 -- analyzed. The Ekind is established in a separate loop at the end.
11275 Param_Spec := First (T);
11276 while Present (Param_Spec) loop
11277 Formal := Defining_Identifier (Param_Spec);
11278 Set_Never_Set_In_Source (Formal, True);
11279 Enter_Name (Formal);
11281 -- Case of ordinary parameters
11283 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11284 Find_Type (Parameter_Type (Param_Spec));
11285 Ptype := Parameter_Type (Param_Spec);
11287 if Ptype = Error then
11291 Formal_Type := Entity (Ptype);
11293 if Is_Incomplete_Type (Formal_Type)
11295 (Is_Class_Wide_Type (Formal_Type)
11296 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11298 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11299 -- primitive operations, as long as their completion is
11300 -- in the same declarative part. If in the private part
11301 -- this means that the type cannot be a Taft-amendment type.
11302 -- Check is done on package exit. For access to subprograms,
11303 -- the use is legal for Taft-amendment types.
11305 -- Ada 2012: tagged incomplete types are allowed as generic
11306 -- formal types. They do not introduce dependencies and the
11307 -- corresponding generic subprogram does not have a delayed
11308 -- freeze, because it does not need a freeze node. However,
11309 -- it is still the case that untagged incomplete types cannot
11310 -- be Taft-amendment types and must be completed in private
11311 -- part, so the subprogram must appear in the list of private
11312 -- dependents of the type.
11314 if Is_Tagged_Type (Formal_Type)
11315 or else Ada_Version >= Ada_2012
11317 if Ekind (Scope (Current_Scope)) = E_Package
11318 and then not From_Limited_With (Formal_Type)
11319 and then not Is_Generic_Type (Formal_Type)
11320 and then not Is_Class_Wide_Type (Formal_Type)
11323 (Parent (T), N_Access_Function_Definition,
11324 N_Access_Procedure_Definition)
11328 Private_Dependents (Base_Type (Formal_Type)));
11330 -- Freezing is delayed to ensure that Register_Prim
11331 -- will get called for this operation, which is needed
11332 -- in cases where static dispatch tables aren't built.
11333 -- (Note that the same is done for controlling access
11334 -- parameter cases in function Access_Definition.)
11336 Set_Has_Delayed_Freeze (Current_Scope);
11340 -- Special handling of Value_Type for CIL case
11342 elsif Is_Value_Type (Formal_Type) then
11345 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11346 N_Access_Procedure_Definition)
11348 -- AI05-0151: Tagged incomplete types are allowed in all
11349 -- formal parts. Untagged incomplete types are not allowed
11352 if Ada_Version >= Ada_2012 then
11353 if Is_Tagged_Type (Formal_Type) then
11356 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
11361 ("invalid use of untagged incomplete type&",
11362 Ptype, Formal_Type);
11367 ("invalid use of incomplete type&",
11368 Param_Spec, Formal_Type);
11370 -- Further checks on the legality of incomplete types
11371 -- in formal parts are delayed until the freeze point
11372 -- of the enclosing subprogram or access to subprogram.
11376 elsif Ekind (Formal_Type) = E_Void then
11378 ("premature use of&",
11379 Parameter_Type (Param_Spec), Formal_Type);
11382 -- Ada 2012 (AI-142): Handle aliased parameters
11384 if Ada_Version >= Ada_2012
11385 and then Aliased_Present (Param_Spec)
11387 Set_Is_Aliased (Formal);
11390 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11391 -- declaration corresponding to the null-excluding type of the
11392 -- formal in the enclosing scope. Finally, replace the parameter
11393 -- type of the formal with the internal subtype.
11395 if Ada_Version >= Ada_2005
11396 and then Null_Exclusion_Present (Param_Spec)
11398 if not Is_Access_Type (Formal_Type) then
11400 ("`NOT NULL` allowed only for an access type", Param_Spec);
11403 if Can_Never_Be_Null (Formal_Type)
11404 and then Comes_From_Source (Related_Nod)
11407 ("`NOT NULL` not allowed (& already excludes null)",
11408 Param_Spec, Formal_Type);
11412 Create_Null_Excluding_Itype
11414 Related_Nod => Related_Nod,
11415 Scope_Id => Scope (Current_Scope));
11417 -- If the designated type of the itype is an itype that is
11418 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11419 -- on the access subtype, to prevent order-of-elaboration
11420 -- issues in the backend.
11423 -- type T is access procedure;
11424 -- procedure Op (O : not null T);
11426 if Is_Itype (Directly_Designated_Type (Formal_Type))
11428 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11430 Set_Has_Delayed_Freeze (Formal_Type);
11435 -- An access formal type
11439 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11441 -- No need to continue if we already notified errors
11443 if not Present (Formal_Type) then
11447 -- Ada 2005 (AI-254)
11450 AD : constant Node_Id :=
11451 Access_To_Subprogram_Definition
11452 (Parameter_Type (Param_Spec));
11454 if Present (AD) and then Protected_Present (AD) then
11456 Replace_Anonymous_Access_To_Protected_Subprogram
11462 Set_Etype (Formal, Formal_Type);
11464 -- Deal with default expression if present
11466 Default := Expression (Param_Spec);
11468 if Present (Default) then
11469 Check_SPARK_Restriction
11470 ("default expression is not allowed", Default);
11472 if Out_Present (Param_Spec) then
11474 ("default initialization only allowed for IN parameters",
11478 -- Do the special preanalysis of the expression (see section on
11479 -- "Handling of Default Expressions" in the spec of package Sem).
11481 Preanalyze_Spec_Expression (Default, Formal_Type);
11483 -- An access to constant cannot be the default for
11484 -- an access parameter that is an access to variable.
11486 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11487 and then not Is_Access_Constant (Formal_Type)
11488 and then Is_Access_Type (Etype (Default))
11489 and then Is_Access_Constant (Etype (Default))
11492 ("formal that is access to variable cannot be initialized " &
11493 "with an access-to-constant expression", Default);
11496 -- Check that the designated type of an access parameter's default
11497 -- is not a class-wide type unless the parameter's designated type
11498 -- is also class-wide.
11500 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11501 and then not Designates_From_Limited_With (Formal_Type)
11502 and then Is_Class_Wide_Default (Default)
11503 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11506 ("access to class-wide expression not allowed here", Default);
11509 -- Check incorrect use of dynamically tagged expressions
11511 if Is_Tagged_Type (Formal_Type) then
11512 Check_Dynamically_Tagged_Expression
11514 Typ => Formal_Type,
11515 Related_Nod => Default);
11519 -- Ada 2005 (AI-231): Static checks
11521 if Ada_Version >= Ada_2005
11522 and then Is_Access_Type (Etype (Formal))
11523 and then Can_Never_Be_Null (Etype (Formal))
11525 Null_Exclusion_Static_Checks (Param_Spec);
11528 -- The following checks are relevant when SPARK_Mode is on as these
11529 -- are not standard Ada legality rules.
11531 if SPARK_Mode = On then
11532 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11534 -- A function cannot have a parameter of mode IN OUT or OUT
11537 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11539 ("function cannot have parameter of mode `OUT` or "
11540 & "`IN OUT`", Formal);
11542 -- A function cannot have a volatile formal parameter
11543 -- (SPARK RM 7.1.3(10)).
11545 elsif Is_SPARK_Volatile (Formal) then
11547 ("function cannot have a volatile formal parameter",
11551 -- A procedure cannot have a formal parameter of mode IN because
11552 -- it behaves as a constant (SPARK RM 7.1.3(6)).
11554 elsif Ekind (Scope (Formal)) = E_Procedure
11555 and then Ekind (Formal) = E_In_Parameter
11556 and then Is_SPARK_Volatile (Formal)
11559 ("formal parameter of mode `IN` cannot be volatile", Formal);
11567 -- If this is the formal part of a function specification, analyze the
11568 -- subtype mark in the context where the formals are visible but not
11569 -- yet usable, and may hide outer homographs.
11571 if Nkind (Related_Nod) = N_Function_Specification then
11572 Analyze_Return_Type (Related_Nod);
11575 -- Now set the kind (mode) of each formal
11577 Param_Spec := First (T);
11578 while Present (Param_Spec) loop
11579 Formal := Defining_Identifier (Param_Spec);
11580 Set_Formal_Mode (Formal);
11582 if Ekind (Formal) = E_In_Parameter then
11583 Set_Default_Value (Formal, Expression (Param_Spec));
11585 if Present (Expression (Param_Spec)) then
11586 Default := Expression (Param_Spec);
11588 if Is_Scalar_Type (Etype (Default)) then
11589 if Nkind (Parameter_Type (Param_Spec)) /=
11590 N_Access_Definition
11592 Formal_Type := Entity (Parameter_Type (Param_Spec));
11596 (Related_Nod, Parameter_Type (Param_Spec));
11599 Apply_Scalar_Range_Check (Default, Formal_Type);
11603 elsif Ekind (Formal) = E_Out_Parameter then
11604 Num_Out_Params := Num_Out_Params + 1;
11606 if Num_Out_Params = 1 then
11607 First_Out_Param := Formal;
11610 elsif Ekind (Formal) = E_In_Out_Parameter then
11611 Num_Out_Params := Num_Out_Params + 1;
11614 -- Skip remaining processing if formal type was in error
11616 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11617 goto Next_Parameter;
11620 -- Force call by reference if aliased
11622 if Is_Aliased (Formal) then
11623 Set_Mechanism (Formal, By_Reference);
11625 -- Warn if user asked this to be passed by copy
11627 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11629 ("cannot pass aliased parameter & by copy??", Formal);
11632 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11634 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11635 Set_Mechanism (Formal, By_Copy);
11637 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11638 Set_Mechanism (Formal, By_Reference);
11645 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11646 Set_Is_Only_Out_Parameter (First_Out_Param);
11648 end Process_Formals;
11650 ----------------------------
11651 -- Reference_Body_Formals --
11652 ----------------------------
11654 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11659 if Error_Posted (Spec) then
11663 -- Iterate over both lists. They may be of different lengths if the two
11664 -- specs are not conformant.
11666 Fs := First_Formal (Spec);
11667 Fb := First_Formal (Bod);
11668 while Present (Fs) and then Present (Fb) loop
11669 Generate_Reference (Fs, Fb, 'b');
11671 if Style_Check then
11672 Style.Check_Identifier (Fb, Fs);
11675 Set_Spec_Entity (Fb, Fs);
11676 Set_Referenced (Fs, False);
11680 end Reference_Body_Formals;
11682 -------------------------
11683 -- Set_Actual_Subtypes --
11684 -------------------------
11686 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11688 Formal : Entity_Id;
11690 First_Stmt : Node_Id := Empty;
11691 AS_Needed : Boolean;
11694 -- If this is an empty initialization procedure, no need to create
11695 -- actual subtypes (small optimization).
11697 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11701 Formal := First_Formal (Subp);
11702 while Present (Formal) loop
11703 T := Etype (Formal);
11705 -- We never need an actual subtype for a constrained formal
11707 if Is_Constrained (T) then
11708 AS_Needed := False;
11710 -- If we have unknown discriminants, then we do not need an actual
11711 -- subtype, or more accurately we cannot figure it out. Note that
11712 -- all class-wide types have unknown discriminants.
11714 elsif Has_Unknown_Discriminants (T) then
11715 AS_Needed := False;
11717 -- At this stage we have an unconstrained type that may need an
11718 -- actual subtype. For sure the actual subtype is needed if we have
11719 -- an unconstrained array type.
11721 elsif Is_Array_Type (T) then
11724 -- The only other case needing an actual subtype is an unconstrained
11725 -- record type which is an IN parameter (we cannot generate actual
11726 -- subtypes for the OUT or IN OUT case, since an assignment can
11727 -- change the discriminant values. However we exclude the case of
11728 -- initialization procedures, since discriminants are handled very
11729 -- specially in this context, see the section entitled "Handling of
11730 -- Discriminants" in Einfo.
11732 -- We also exclude the case of Discrim_SO_Functions (functions used
11733 -- in front end layout mode for size/offset values), since in such
11734 -- functions only discriminants are referenced, and not only are such
11735 -- subtypes not needed, but they cannot always be generated, because
11736 -- of order of elaboration issues.
11738 elsif Is_Record_Type (T)
11739 and then Ekind (Formal) = E_In_Parameter
11740 and then Chars (Formal) /= Name_uInit
11741 and then not Is_Unchecked_Union (T)
11742 and then not Is_Discrim_SO_Function (Subp)
11746 -- All other cases do not need an actual subtype
11749 AS_Needed := False;
11752 -- Generate actual subtypes for unconstrained arrays and
11753 -- unconstrained discriminated records.
11756 if Nkind (N) = N_Accept_Statement then
11758 -- If expansion is active, the formal is replaced by a local
11759 -- variable that renames the corresponding entry of the
11760 -- parameter block, and it is this local variable that may
11761 -- require an actual subtype.
11763 if Expander_Active then
11764 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11766 Decl := Build_Actual_Subtype (T, Formal);
11769 if Present (Handled_Statement_Sequence (N)) then
11771 First (Statements (Handled_Statement_Sequence (N)));
11772 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11773 Mark_Rewrite_Insertion (Decl);
11775 -- If the accept statement has no body, there will be no
11776 -- reference to the actuals, so no need to compute actual
11783 Decl := Build_Actual_Subtype (T, Formal);
11784 Prepend (Decl, Declarations (N));
11785 Mark_Rewrite_Insertion (Decl);
11788 -- The declaration uses the bounds of an existing object, and
11789 -- therefore needs no constraint checks.
11791 Analyze (Decl, Suppress => All_Checks);
11793 -- We need to freeze manually the generated type when it is
11794 -- inserted anywhere else than in a declarative part.
11796 if Present (First_Stmt) then
11797 Insert_List_Before_And_Analyze (First_Stmt,
11798 Freeze_Entity (Defining_Identifier (Decl), N));
11800 -- Ditto if the type has a dynamic predicate, because the
11801 -- generated function will mention the actual subtype.
11803 elsif Has_Dynamic_Predicate_Aspect (T) then
11804 Insert_List_Before_And_Analyze (Decl,
11805 Freeze_Entity (Defining_Identifier (Decl), N));
11808 if Nkind (N) = N_Accept_Statement
11809 and then Expander_Active
11811 Set_Actual_Subtype (Renamed_Object (Formal),
11812 Defining_Identifier (Decl));
11814 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11818 Next_Formal (Formal);
11820 end Set_Actual_Subtypes;
11822 ---------------------
11823 -- Set_Formal_Mode --
11824 ---------------------
11826 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11827 Spec : constant Node_Id := Parent (Formal_Id);
11830 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11831 -- since we ensure that corresponding actuals are always valid at the
11832 -- point of the call.
11834 if Out_Present (Spec) then
11835 if Ekind (Scope (Formal_Id)) = E_Function
11836 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11838 -- [IN] OUT parameters allowed for functions in Ada 2012
11840 if Ada_Version >= Ada_2012 then
11842 -- Even in Ada 2012 operators can only have IN parameters
11844 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11845 Error_Msg_N ("operators can only have IN parameters", Spec);
11848 if In_Present (Spec) then
11849 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11851 Set_Ekind (Formal_Id, E_Out_Parameter);
11854 -- But not in earlier versions of Ada
11857 Error_Msg_N ("functions can only have IN parameters", Spec);
11858 Set_Ekind (Formal_Id, E_In_Parameter);
11861 elsif In_Present (Spec) then
11862 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11865 Set_Ekind (Formal_Id, E_Out_Parameter);
11866 Set_Never_Set_In_Source (Formal_Id, True);
11867 Set_Is_True_Constant (Formal_Id, False);
11868 Set_Current_Value (Formal_Id, Empty);
11872 Set_Ekind (Formal_Id, E_In_Parameter);
11875 -- Set Is_Known_Non_Null for access parameters since the language
11876 -- guarantees that access parameters are always non-null. We also set
11877 -- Can_Never_Be_Null, since there is no way to change the value.
11879 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11881 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11882 -- null; In Ada 2005, only if then null_exclusion is explicit.
11884 if Ada_Version < Ada_2005
11885 or else Can_Never_Be_Null (Etype (Formal_Id))
11887 Set_Is_Known_Non_Null (Formal_Id);
11888 Set_Can_Never_Be_Null (Formal_Id);
11891 -- Ada 2005 (AI-231): Null-exclusion access subtype
11893 elsif Is_Access_Type (Etype (Formal_Id))
11894 and then Can_Never_Be_Null (Etype (Formal_Id))
11896 Set_Is_Known_Non_Null (Formal_Id);
11898 -- We can also set Can_Never_Be_Null (thus preventing some junk
11899 -- access checks) for the case of an IN parameter, which cannot
11900 -- be changed, or for an IN OUT parameter, which can be changed but
11901 -- not to a null value. But for an OUT parameter, the initial value
11902 -- passed in can be null, so we can't set this flag in that case.
11904 if Ekind (Formal_Id) /= E_Out_Parameter then
11905 Set_Can_Never_Be_Null (Formal_Id);
11909 Set_Mechanism (Formal_Id, Default_Mechanism);
11910 Set_Formal_Validity (Formal_Id);
11911 end Set_Formal_Mode;
11913 -------------------------
11914 -- Set_Formal_Validity --
11915 -------------------------
11917 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11919 -- If no validity checking, then we cannot assume anything about the
11920 -- validity of parameters, since we do not know there is any checking
11921 -- of the validity on the call side.
11923 if not Validity_Checks_On then
11926 -- If validity checking for parameters is enabled, this means we are
11927 -- not supposed to make any assumptions about argument values.
11929 elsif Validity_Check_Parameters then
11932 -- If we are checking in parameters, we will assume that the caller is
11933 -- also checking parameters, so we can assume the parameter is valid.
11935 elsif Ekind (Formal_Id) = E_In_Parameter
11936 and then Validity_Check_In_Params
11938 Set_Is_Known_Valid (Formal_Id, True);
11940 -- Similar treatment for IN OUT parameters
11942 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11943 and then Validity_Check_In_Out_Params
11945 Set_Is_Known_Valid (Formal_Id, True);
11947 end Set_Formal_Validity;
11949 ------------------------
11950 -- Subtype_Conformant --
11951 ------------------------
11953 function Subtype_Conformant
11954 (New_Id : Entity_Id;
11955 Old_Id : Entity_Id;
11956 Skip_Controlling_Formals : Boolean := False) return Boolean
11960 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11961 Skip_Controlling_Formals => Skip_Controlling_Formals);
11963 end Subtype_Conformant;
11965 ---------------------
11966 -- Type_Conformant --
11967 ---------------------
11969 function Type_Conformant
11970 (New_Id : Entity_Id;
11971 Old_Id : Entity_Id;
11972 Skip_Controlling_Formals : Boolean := False) return Boolean
11976 May_Hide_Profile := False;
11979 (New_Id, Old_Id, Type_Conformant, False, Result,
11980 Skip_Controlling_Formals => Skip_Controlling_Formals);
11982 end Type_Conformant;
11984 -------------------------------
11985 -- Valid_Operator_Definition --
11986 -------------------------------
11988 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11991 Id : constant Name_Id := Chars (Designator);
11995 F := First_Formal (Designator);
11996 while Present (F) loop
11999 if Present (Default_Value (F)) then
12001 ("default values not allowed for operator parameters",
12008 -- Verify that user-defined operators have proper number of arguments
12009 -- First case of operators which can only be unary
12011 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12014 -- Case of operators which can be unary or binary
12016 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12017 N_OK := (N in 1 .. 2);
12019 -- All other operators can only be binary
12027 ("incorrect number of arguments for operator", Designator);
12031 and then Base_Type (Etype (Designator)) = Standard_Boolean
12032 and then not Is_Intrinsic_Subprogram (Designator)
12035 ("explicit definition of inequality not allowed", Designator);
12037 end Valid_Operator_Definition;