1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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.
198 procedure Enter_Overloaded_Entity (S : Entity_Id);
199 -- This procedure makes S, a new overloaded entity, into the first visible
200 -- entity with that name.
202 function Is_Non_Overriding_Operation
204 New_E : Entity_Id) return Boolean;
205 -- Enforce the rule given in 12.3(18): a private operation in an instance
206 -- overrides an inherited operation only if the corresponding operation
207 -- was overriding in the generic. This needs to be checked for primitive
208 -- operations of types derived (in the generic unit) from formal private
209 -- or formal derived types.
211 procedure Make_Inequality_Operator (S : Entity_Id);
212 -- Create the declaration for an inequality operator that is implicitly
213 -- created by a user-defined equality operator that yields a boolean.
215 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
216 -- Formal_Id is an formal parameter entity. This procedure deals with
217 -- setting the proper validity status for this entity, which depends on
218 -- the kind of parameter and the validity checking mode.
220 ---------------------------------------------
221 -- Analyze_Abstract_Subprogram_Declaration --
222 ---------------------------------------------
224 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
225 Designator : constant Entity_Id :=
226 Analyze_Subprogram_Specification (Specification (N));
227 Scop : constant Entity_Id := Current_Scope;
230 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
232 Generate_Definition (Designator);
233 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
234 Set_Is_Abstract_Subprogram (Designator);
235 New_Overloaded_Entity (Designator);
236 Check_Delayed_Subprogram (Designator);
238 Set_Categorization_From_Scope (Designator, Scop);
240 if Ekind (Scope (Designator)) = E_Protected_Type then
242 ("abstract subprogram not allowed in protected type", N);
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation (Designator)
250 and then not Present (Overridden_Operation (Designator))
251 and then (not Is_Operator_Symbol_Name (Chars (Designator))
252 or else Scop /= Scope (Etype (First_Formal (Designator))))
255 ("abstract subprogram is not dispatching or overriding?r?", N);
258 Generate_Reference_To_Formals (Designator);
259 Check_Eliminated (Designator);
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Designator);
264 end Analyze_Abstract_Subprogram_Declaration;
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
270 procedure Analyze_Expression_Function (N : Node_Id) is
271 Loc : constant Source_Ptr := Sloc (N);
272 LocX : constant Source_Ptr := Sloc (Expression (N));
273 Expr : constant Node_Id := Expression (N);
274 Spec : constant Node_Id := Specification (N);
279 -- If the expression is a completion, Prev is the entity whose
280 -- declaration is completed. Def_Id is needed to analyze the spec.
288 -- This is one of the occasions on which we transform the tree during
289 -- semantic analysis. If this is a completion, transform the expression
290 -- function into an equivalent subprogram body, and analyze it.
292 -- Expression functions are inlined unconditionally. The back-end will
293 -- determine whether this is possible.
295 Inline_Processing_Required := True;
297 -- Create a specification for the generated body. Types and defauts in
298 -- the profile are copies of the spec, but new entities must be created
299 -- for the unit name and the formals.
301 New_Spec := New_Copy_Tree (Spec);
302 Set_Defining_Unit_Name (New_Spec,
303 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
304 Chars (Defining_Unit_Name (Spec))));
306 if Present (Parameter_Specifications (New_Spec)) then
308 Formal_Spec : Node_Id;
310 Formal_Spec := First (Parameter_Specifications (New_Spec));
311 while Present (Formal_Spec) loop
312 Set_Defining_Identifier
314 Make_Defining_Identifier (Sloc (Formal_Spec),
315 Chars => Chars (Defining_Identifier (Formal_Spec))));
321 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
323 -- If there are previous overloadable entities with the same name,
324 -- check whether any of them is completed by the expression function.
326 if Present (Prev) and then Is_Overloadable (Prev) then
327 Def_Id := Analyze_Subprogram_Specification (Spec);
328 Prev := Find_Corresponding_Spec (N);
331 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
334 Make_Subprogram_Body (Loc,
335 Specification => New_Spec,
336 Declarations => Empty_List,
337 Handled_Statement_Sequence =>
338 Make_Handled_Sequence_Of_Statements (LocX,
339 Statements => New_List (Ret)));
341 -- If the expression completes a generic subprogram, we must create a
342 -- separate node for the body, because at instantiation the original
343 -- node of the generic copy must be a generic subprogram body, and
344 -- cannot be a expression function. Otherwise we just rewrite the
345 -- expression with the non-generic body.
347 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
348 Insert_After (N, New_Body);
350 -- Propagate any aspects or pragmas that apply to the expression
351 -- function to the proper body when the expression function acts
354 if Has_Aspects (N) then
355 Move_Aspects (N, To => New_Body);
358 Relocate_Pragmas_To_Body (New_Body);
360 Rewrite (N, Make_Null_Statement (Loc));
361 Set_Has_Completion (Prev, False);
364 Set_Is_Inlined (Prev);
366 elsif Present (Prev) and then Comes_From_Source (Prev) then
367 Set_Has_Completion (Prev, False);
369 -- For navigation purposes, indicate that the function is a body
371 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
372 Rewrite (N, New_Body);
374 -- Propagate any pragmas that apply to the expression function to the
375 -- proper body when the expression function acts as a completion.
376 -- Aspects are automatically transfered because of node rewriting.
378 Relocate_Pragmas_To_Body (N);
381 -- Prev is the previous entity with the same name, but it is can
382 -- be an unrelated spec that is not completed by the expression
383 -- function. In that case the relevant entity is the one in the body.
384 -- Not clear that the backend can inline it in this case ???
386 if Has_Completion (Prev) then
387 Set_Is_Inlined (Prev);
389 -- The formals of the expression function are body formals,
390 -- and do not appear in the ali file, which will only contain
391 -- references to the formals of the original subprogram spec.
398 F1 := First_Formal (Def_Id);
399 F2 := First_Formal (Prev);
401 while Present (F1) loop
402 Set_Spec_Entity (F1, F2);
409 Set_Is_Inlined (Defining_Entity (New_Body));
412 -- If this is not a completion, create both a declaration and a body, so
413 -- that the expression can be inlined whenever possible.
416 -- An expression function that is not a completion is not a
417 -- subprogram declaration, and thus cannot appear in a protected
420 if Nkind (Parent (N)) = N_Protected_Definition then
422 ("an expression function is not a legal protected operation", N);
426 Make_Subprogram_Declaration (Loc, Specification => Spec);
428 Rewrite (N, New_Decl);
430 Set_Is_Inlined (Defining_Entity (New_Decl));
432 -- To prevent premature freeze action, insert the new body at the end
433 -- of the current declarations, or at the end of the package spec.
434 -- However, resolve usage names now, to prevent spurious visibility
435 -- on later entities.
438 Decls : List_Id := List_Containing (N);
439 Par : constant Node_Id := Parent (Decls);
440 Id : constant Entity_Id := Defining_Entity (New_Decl);
443 if Nkind (Par) = N_Package_Specification
444 and then Decls = Visible_Declarations (Par)
445 and then Present (Private_Declarations (Par))
446 and then not Is_Empty_List (Private_Declarations (Par))
448 Decls := Private_Declarations (Par);
451 Insert_After (Last (Decls), New_Body);
453 Install_Formals (Id);
455 -- Preanalyze the expression for name capture, except in an
456 -- instance, where this has been done during generic analysis,
457 -- and will be redone when analyzing the body.
460 Expr : constant Node_Id := Expression (Ret);
463 Set_Parent (Expr, Ret);
465 if not In_Instance then
466 Preanalyze_Spec_Expression (Expr, Etype (Id));
474 -- If the return expression is a static constant, we suppress warning
475 -- messages on unused formals, which in most cases will be noise.
477 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
478 Is_OK_Static_Expression (Expr));
479 end Analyze_Expression_Function;
481 ----------------------------------------
482 -- Analyze_Extended_Return_Statement --
483 ----------------------------------------
485 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
487 Analyze_Return_Statement (N);
488 end Analyze_Extended_Return_Statement;
490 ----------------------------
491 -- Analyze_Function_Call --
492 ----------------------------
494 procedure Analyze_Function_Call (N : Node_Id) is
495 Actuals : constant List_Id := Parameter_Associations (N);
496 Func_Nam : constant Node_Id := Name (N);
502 -- A call of the form A.B (X) may be an Ada 2005 call, which is
503 -- rewritten as B (A, X). If the rewriting is successful, the call
504 -- has been analyzed and we just return.
506 if Nkind (Func_Nam) = N_Selected_Component
507 and then Name (N) /= Func_Nam
508 and then Is_Rewrite_Substitution (N)
509 and then Present (Etype (N))
514 -- If error analyzing name, then set Any_Type as result type and return
516 if Etype (Func_Nam) = Any_Type then
517 Set_Etype (N, Any_Type);
521 -- Otherwise analyze the parameters
523 if Present (Actuals) then
524 Actual := First (Actuals);
525 while Present (Actual) loop
527 Check_Parameterless_Call (Actual);
533 end Analyze_Function_Call;
535 -----------------------------
536 -- Analyze_Function_Return --
537 -----------------------------
539 procedure Analyze_Function_Return (N : Node_Id) is
540 Loc : constant Source_Ptr := Sloc (N);
541 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
542 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
544 R_Type : constant Entity_Id := Etype (Scope_Id);
545 -- Function result subtype
547 procedure Check_Limited_Return (Expr : Node_Id);
548 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
549 -- limited types. Used only for simple return statements.
550 -- Expr is the expression returned.
552 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
553 -- Check that the return_subtype_indication properly matches the result
554 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
556 --------------------------
557 -- Check_Limited_Return --
558 --------------------------
560 procedure Check_Limited_Return (Expr : Node_Id) is
562 -- Ada 2005 (AI-318-02): Return-by-reference types have been
563 -- removed and replaced by anonymous access results. This is an
564 -- incompatibility with Ada 95. Not clear whether this should be
565 -- enforced yet or perhaps controllable with special switch. ???
567 -- A limited interface that is not immutably limited is OK.
569 if Is_Limited_Interface (R_Type)
571 not (Is_Task_Interface (R_Type)
572 or else Is_Protected_Interface (R_Type)
573 or else Is_Synchronized_Interface (R_Type))
577 elsif Is_Limited_Type (R_Type)
578 and then not Is_Interface (R_Type)
579 and then Comes_From_Source (N)
580 and then not In_Instance_Body
581 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
585 if Ada_Version >= Ada_2005
586 and then not Debug_Flag_Dot_L
587 and then not GNAT_Mode
590 ("(Ada 2005) cannot copy object of a limited type " &
591 "(RM-2005 6.5(5.5/2))", Expr);
593 if Is_Limited_View (R_Type) then
595 ("\return by reference not permitted in Ada 2005", Expr);
598 -- Warn in Ada 95 mode, to give folks a heads up about this
601 -- In GNAT mode, this is just a warning, to allow it to be
602 -- evilly turned off. Otherwise it is a real error.
604 -- In a generic context, simplify the warning because it makes
605 -- no sense to discuss pass-by-reference or copy.
607 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
608 if Inside_A_Generic then
610 ("return of limited object not permitted in Ada 2005 "
611 & "(RM-2005 6.5(5.5/2))?y?", Expr);
613 elsif Is_Limited_View (R_Type) then
615 ("return by reference not permitted in Ada 2005 "
616 & "(RM-2005 6.5(5.5/2))?y?", Expr);
619 ("cannot copy object of a limited type in Ada 2005 "
620 & "(RM-2005 6.5(5.5/2))?y?", Expr);
623 -- Ada 95 mode, compatibility warnings disabled
626 return; -- skip continuation messages below
629 if not Inside_A_Generic then
631 ("\consider switching to return of access type", Expr);
632 Explain_Limited_Type (R_Type, Expr);
635 end Check_Limited_Return;
637 -------------------------------------
638 -- Check_Return_Subtype_Indication --
639 -------------------------------------
641 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
642 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
644 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
645 -- Subtype given in the extended return statement (must match R_Type)
647 Subtype_Ind : constant Node_Id :=
648 Object_Definition (Original_Node (Obj_Decl));
650 R_Type_Is_Anon_Access :
652 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
654 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
656 Ekind (R_Type) = E_Anonymous_Access_Type;
657 -- True if return type of the function is an anonymous access type
658 -- Can't we make Is_Anonymous_Access_Type in einfo ???
660 R_Stm_Type_Is_Anon_Access :
662 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
664 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
666 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
667 -- True if type of the return object is an anonymous access type
670 -- First, avoid cascaded errors
672 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
676 -- "return access T" case; check that the return statement also has
677 -- "access T", and that the subtypes statically match:
678 -- if this is an access to subprogram the signatures must match.
680 if R_Type_Is_Anon_Access then
681 if R_Stm_Type_Is_Anon_Access then
683 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
685 if Base_Type (Designated_Type (R_Stm_Type)) /=
686 Base_Type (Designated_Type (R_Type))
687 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
690 ("subtype must statically match function result subtype",
691 Subtype_Mark (Subtype_Ind));
695 -- For two anonymous access to subprogram types, the
696 -- types themselves must be type conformant.
698 if not Conforming_Types
699 (R_Stm_Type, R_Type, Fully_Conformant)
702 ("subtype must statically match function result subtype",
708 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
711 -- If the return object is of an anonymous access type, then report
712 -- an error if the function's result type is not also anonymous.
714 elsif R_Stm_Type_Is_Anon_Access
715 and then not R_Type_Is_Anon_Access
717 Error_Msg_N ("anonymous access not allowed for function with " &
718 "named access result", Subtype_Ind);
720 -- Subtype indication case: check that the return object's type is
721 -- covered by the result type, and that the subtypes statically match
722 -- when the result subtype is constrained. Also handle record types
723 -- with unknown discriminants for which we have built the underlying
724 -- record view. Coverage is needed to allow specific-type return
725 -- objects when the result type is class-wide (see AI05-32).
727 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
728 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
732 Underlying_Record_View (Base_Type (R_Stm_Type))))
734 -- A null exclusion may be present on the return type, on the
735 -- function specification, on the object declaration or on the
738 if Is_Access_Type (R_Type)
740 (Can_Never_Be_Null (R_Type)
741 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
742 Can_Never_Be_Null (R_Stm_Type)
745 ("subtype must statically match function result subtype",
749 -- AI05-103: for elementary types, subtypes must statically match
751 if Is_Constrained (R_Type)
752 or else Is_Access_Type (R_Type)
754 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
756 ("subtype must statically match function result subtype",
761 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
762 and then Is_Null_Extension (Base_Type (R_Type))
768 ("wrong type for return_subtype_indication", Subtype_Ind);
770 end Check_Return_Subtype_Indication;
772 ---------------------
773 -- Local Variables --
774 ---------------------
778 -- Start of processing for Analyze_Function_Return
781 Set_Return_Present (Scope_Id);
783 if Nkind (N) = N_Simple_Return_Statement then
784 Expr := Expression (N);
786 -- Guard against a malformed expression. The parser may have tried to
787 -- recover but the node is not analyzable.
789 if Nkind (Expr) = N_Error then
790 Set_Etype (Expr, Any_Type);
791 Expander_Mode_Save_And_Set (False);
795 -- The resolution of a controlled [extension] aggregate associated
796 -- with a return statement creates a temporary which needs to be
797 -- finalized on function exit. Wrap the return statement inside a
798 -- block so that the finalization machinery can detect this case.
799 -- This early expansion is done only when the return statement is
800 -- not part of a handled sequence of statements.
802 if Nkind_In (Expr, N_Aggregate,
803 N_Extension_Aggregate)
804 and then Needs_Finalization (R_Type)
805 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
808 Make_Block_Statement (Loc,
809 Handled_Statement_Sequence =>
810 Make_Handled_Sequence_Of_Statements (Loc,
811 Statements => New_List (Relocate_Node (N)))));
817 Analyze_And_Resolve (Expr, R_Type);
818 Check_Limited_Return (Expr);
821 -- RETURN only allowed in SPARK as the last statement in function
823 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
825 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
826 or else Present (Next (N)))
828 Check_SPARK_Restriction
829 ("RETURN should be the last statement in function", N);
833 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
835 -- Analyze parts specific to extended_return_statement:
838 Obj_Decl : constant Node_Id :=
839 Last (Return_Object_Declarations (N));
840 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
841 HSS : constant Node_Id := Handled_Statement_Sequence (N);
844 Expr := Expression (Obj_Decl);
846 -- Note: The check for OK_For_Limited_Init will happen in
847 -- Analyze_Object_Declaration; we treat it as a normal
848 -- object declaration.
850 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
853 Check_Return_Subtype_Indication (Obj_Decl);
855 if Present (HSS) then
858 if Present (Exception_Handlers (HSS)) then
860 -- ???Has_Nested_Block_With_Handler needs to be set.
861 -- Probably by creating an actual N_Block_Statement.
862 -- Probably in Expand.
868 -- Mark the return object as referenced, since the return is an
869 -- implicit reference of the object.
871 Set_Referenced (Defining_Identifier (Obj_Decl));
873 Check_References (Stm_Entity);
875 -- Check RM 6.5 (5.9/3)
878 if Ada_Version < Ada_2012 then
880 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
881 -- Can it really happen (extended return???)
884 ("aliased only allowed for limited"
885 & " return objects in Ada 2012?", N);
887 elsif not Is_Limited_View (R_Type) then
888 Error_Msg_N ("aliased only allowed for limited"
889 & " return objects", N);
895 -- Case of Expr present
899 -- Defend against previous errors
901 and then Nkind (Expr) /= N_Empty
902 and then Present (Etype (Expr))
904 -- Apply constraint check. Note that this is done before the implicit
905 -- conversion of the expression done for anonymous access types to
906 -- ensure correct generation of the null-excluding check associated
907 -- with null-excluding expressions found in return statements.
909 Apply_Constraint_Check (Expr, R_Type);
911 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
912 -- type, apply an implicit conversion of the expression to that type
913 -- to force appropriate static and run-time accessibility checks.
915 if Ada_Version >= Ada_2005
916 and then Ekind (R_Type) = E_Anonymous_Access_Type
918 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
919 Analyze_And_Resolve (Expr, R_Type);
921 -- If this is a local anonymous access to subprogram, the
922 -- accessibility check can be applied statically. The return is
923 -- illegal if the access type of the return expression is declared
924 -- inside of the subprogram (except if it is the subtype indication
925 -- of an extended return statement).
927 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
928 if not Comes_From_Source (Current_Scope)
929 or else Ekind (Current_Scope) = E_Return_Statement
934 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
936 Error_Msg_N ("cannot return local access to subprogram", N);
940 -- If the result type is class-wide, then check that the return
941 -- expression's type is not declared at a deeper level than the
942 -- function (RM05-6.5(5.6/2)).
944 if Ada_Version >= Ada_2005
945 and then Is_Class_Wide_Type (R_Type)
947 if Type_Access_Level (Etype (Expr)) >
948 Subprogram_Access_Level (Scope_Id)
951 ("level of return expression type is deeper than " &
952 "class-wide function!", Expr);
956 -- Check incorrect use of dynamically tagged expression
958 if Is_Tagged_Type (R_Type) then
959 Check_Dynamically_Tagged_Expression
965 -- ??? A real run-time accessibility check is needed in cases
966 -- involving dereferences of access parameters. For now we just
967 -- check the static cases.
969 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
970 and then Is_Limited_View (Etype (Scope_Id))
971 and then Object_Access_Level (Expr) >
972 Subprogram_Access_Level (Scope_Id)
974 -- Suppress the message in a generic, where the rewriting
977 if Inside_A_Generic then
982 Make_Raise_Program_Error (Loc,
983 Reason => PE_Accessibility_Check_Failed));
986 Error_Msg_Warn := SPARK_Mode /= On;
987 Error_Msg_N ("cannot return a local value by reference<<", N);
988 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
993 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
994 and then Null_Exclusion_Present (Parent (Scope_Id))
996 Apply_Compile_Time_Constraint_Error
998 Msg => "(Ada 2005) null not allowed for "
999 & "null-excluding return??",
1000 Reason => CE_Null_Not_Allowed);
1003 end Analyze_Function_Return;
1005 -------------------------------------
1006 -- Analyze_Generic_Subprogram_Body --
1007 -------------------------------------
1009 procedure Analyze_Generic_Subprogram_Body
1013 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1014 Kind : constant Entity_Kind := Ekind (Gen_Id);
1015 Body_Id : Entity_Id;
1020 -- Copy body and disable expansion while analyzing the generic For a
1021 -- stub, do not copy the stub (which would load the proper body), this
1022 -- will be done when the proper body is analyzed.
1024 if Nkind (N) /= N_Subprogram_Body_Stub then
1025 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1030 Spec := Specification (N);
1032 -- Within the body of the generic, the subprogram is callable, and
1033 -- behaves like the corresponding non-generic unit.
1035 Body_Id := Defining_Entity (Spec);
1037 if Kind = E_Generic_Procedure
1038 and then Nkind (Spec) /= N_Procedure_Specification
1040 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1043 elsif Kind = E_Generic_Function
1044 and then Nkind (Spec) /= N_Function_Specification
1046 Error_Msg_N ("invalid body for generic function ", Body_Id);
1050 Set_Corresponding_Body (Gen_Decl, Body_Id);
1052 if Has_Completion (Gen_Id)
1053 and then Nkind (Parent (N)) /= N_Subunit
1055 Error_Msg_N ("duplicate generic body", N);
1058 Set_Has_Completion (Gen_Id);
1061 if Nkind (N) = N_Subprogram_Body_Stub then
1062 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1064 Set_Corresponding_Spec (N, Gen_Id);
1067 if Nkind (Parent (N)) = N_Compilation_Unit then
1068 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1071 -- Make generic parameters immediately visible in the body. They are
1072 -- needed to process the formals declarations. Then make the formals
1073 -- visible in a separate step.
1075 Push_Scope (Gen_Id);
1079 First_Ent : Entity_Id;
1082 First_Ent := First_Entity (Gen_Id);
1085 while Present (E) and then not Is_Formal (E) loop
1090 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1092 -- Now generic formals are visible, and the specification can be
1093 -- analyzed, for subsequent conformance check.
1095 Body_Id := Analyze_Subprogram_Specification (Spec);
1097 -- Make formal parameters visible
1101 -- E is the first formal parameter, we loop through the formals
1102 -- installing them so that they will be visible.
1104 Set_First_Entity (Gen_Id, E);
1105 while Present (E) loop
1111 -- Visible generic entity is callable within its own body
1113 Set_Ekind (Gen_Id, Ekind (Body_Id));
1114 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1115 Set_Ekind (Body_Id, E_Subprogram_Body);
1116 Set_Convention (Body_Id, Convention (Gen_Id));
1117 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1118 Set_Scope (Body_Id, Scope (Gen_Id));
1119 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1121 if Nkind (N) = N_Subprogram_Body_Stub then
1123 -- No body to analyze, so restore state of generic unit
1125 Set_Ekind (Gen_Id, Kind);
1126 Set_Ekind (Body_Id, Kind);
1128 if Present (First_Ent) then
1129 Set_First_Entity (Gen_Id, First_Ent);
1136 -- If this is a compilation unit, it must be made visible explicitly,
1137 -- because the compilation of the declaration, unlike other library
1138 -- unit declarations, does not. If it is not a unit, the following
1139 -- is redundant but harmless.
1141 Set_Is_Immediately_Visible (Gen_Id);
1142 Reference_Body_Formals (Gen_Id, Body_Id);
1144 if Is_Child_Unit (Gen_Id) then
1145 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1148 Set_Actual_Subtypes (N, Current_Scope);
1150 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1151 -- invariants and predicates associated with the body and its spec.
1152 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1153 -- prepares the contract assertions for generic subprograms or for
1154 -- ASIS. Do not generate contract checks in SPARK mode.
1156 if not GNATprove_Mode then
1157 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1160 -- If the generic unit carries pre- or post-conditions, copy them
1161 -- to the original generic tree, so that they are properly added
1162 -- to any instantiation.
1165 Orig : constant Node_Id := Original_Node (N);
1169 Cond := First (Declarations (N));
1170 while Present (Cond) loop
1171 if Nkind (Cond) = N_Pragma
1172 and then Pragma_Name (Cond) = Name_Check
1174 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1176 elsif Nkind (Cond) = N_Pragma
1177 and then Pragma_Name (Cond) = Name_Postcondition
1179 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1180 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1189 Analyze_Declarations (Declarations (N));
1191 Analyze (Handled_Statement_Sequence (N));
1193 Save_Global_References (Original_Node (N));
1195 -- Prior to exiting the scope, include generic formals again (if any
1196 -- are present) in the set of local entities.
1198 if Present (First_Ent) then
1199 Set_First_Entity (Gen_Id, First_Ent);
1202 Check_References (Gen_Id);
1205 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1207 Check_Subprogram_Order (N);
1209 -- Outside of its body, unit is generic again
1211 Set_Ekind (Gen_Id, Kind);
1212 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1215 Style.Check_Identifier (Body_Id, Gen_Id);
1219 end Analyze_Generic_Subprogram_Body;
1221 ----------------------------
1222 -- Analyze_Null_Procedure --
1223 ----------------------------
1225 procedure Analyze_Null_Procedure
1227 Is_Completion : out Boolean)
1229 Loc : constant Source_Ptr := Sloc (N);
1230 Spec : constant Node_Id := Specification (N);
1231 Designator : Entity_Id;
1233 Null_Body : Node_Id := Empty;
1237 -- Capture the profile of the null procedure before analysis, for
1238 -- expansion at the freeze point and at each point of call. The body is
1239 -- used if the procedure has preconditions, or if it is a completion. In
1240 -- the first case the body is analyzed at the freeze point, in the other
1241 -- it replaces the null procedure declaration.
1244 Make_Subprogram_Body (Loc,
1245 Specification => New_Copy_Tree (Spec),
1246 Declarations => New_List,
1247 Handled_Statement_Sequence =>
1248 Make_Handled_Sequence_Of_Statements (Loc,
1249 Statements => New_List (Make_Null_Statement (Loc))));
1251 -- Create new entities for body and formals
1253 Set_Defining_Unit_Name (Specification (Null_Body),
1254 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1256 Form := First (Parameter_Specifications (Specification (Null_Body)));
1257 while Present (Form) loop
1258 Set_Defining_Identifier (Form,
1259 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1263 -- Determine whether the null procedure may be a completion of a generic
1264 -- suprogram, in which case we use the new null body as the completion
1265 -- and set minimal semantic information on the original declaration,
1266 -- which is rewritten as a null statement.
1268 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1270 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1271 Insert_Before (N, Null_Body);
1272 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1273 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1275 Rewrite (N, Make_Null_Statement (Loc));
1276 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1277 Is_Completion := True;
1281 -- Resolve the types of the formals now, because the freeze point
1282 -- may appear in a different context, e.g. an instantiation.
1284 Form := First (Parameter_Specifications (Specification (Null_Body)));
1285 while Present (Form) loop
1286 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1287 Find_Type (Parameter_Type (Form));
1290 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1292 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1295 -- The case of a null procedure with a formal that is an
1296 -- access_to_subprogram type, and that is used as an actual
1297 -- in an instantiation is left to the enthusiastic reader.
1306 -- If there are previous overloadable entities with the same name,
1307 -- check whether any of them is completed by the null procedure.
1309 if Present (Prev) and then Is_Overloadable (Prev) then
1310 Designator := Analyze_Subprogram_Specification (Spec);
1311 Prev := Find_Corresponding_Spec (N);
1314 if No (Prev) or else not Comes_From_Source (Prev) then
1315 Designator := Analyze_Subprogram_Specification (Spec);
1316 Set_Has_Completion (Designator);
1318 -- Signal to caller that this is a procedure declaration
1320 Is_Completion := False;
1322 -- Null procedures are always inlined, but generic formal subprograms
1323 -- which appear as such in the internal instance of formal packages,
1324 -- need no completion and are not marked Inline.
1327 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1329 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1330 Set_Body_To_Inline (N, Null_Body);
1331 Set_Is_Inlined (Designator);
1335 -- The null procedure is a completion
1337 Is_Completion := True;
1339 if Expander_Active then
1340 Rewrite (N, Null_Body);
1344 Designator := Analyze_Subprogram_Specification (Spec);
1345 Set_Has_Completion (Designator);
1346 Set_Has_Completion (Prev);
1349 end Analyze_Null_Procedure;
1351 -----------------------------
1352 -- Analyze_Operator_Symbol --
1353 -----------------------------
1355 -- An operator symbol such as "+" or "and" may appear in context where the
1356 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1357 -- is just a string, as in (conjunction = "or"). In these cases the parser
1358 -- generates this node, and the semantics does the disambiguation. Other
1359 -- such case are actuals in an instantiation, the generic unit in an
1360 -- instantiation, and pragma arguments.
1362 procedure Analyze_Operator_Symbol (N : Node_Id) is
1363 Par : constant Node_Id := Parent (N);
1366 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1367 or else Nkind (Par) = N_Function_Instantiation
1368 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1369 or else (Nkind (Par) = N_Pragma_Argument_Association
1370 and then not Is_Pragma_String_Literal (Par))
1371 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1372 or else (Nkind (Par) = N_Attribute_Reference
1373 and then Attribute_Name (Par) /= Name_Value)
1375 Find_Direct_Name (N);
1378 Change_Operator_Symbol_To_String_Literal (N);
1381 end Analyze_Operator_Symbol;
1383 -----------------------------------
1384 -- Analyze_Parameter_Association --
1385 -----------------------------------
1387 procedure Analyze_Parameter_Association (N : Node_Id) is
1389 Analyze (Explicit_Actual_Parameter (N));
1390 end Analyze_Parameter_Association;
1392 ----------------------------
1393 -- Analyze_Procedure_Call --
1394 ----------------------------
1396 procedure Analyze_Procedure_Call (N : Node_Id) is
1397 Loc : constant Source_Ptr := Sloc (N);
1398 P : constant Node_Id := Name (N);
1399 Actuals : constant List_Id := Parameter_Associations (N);
1403 procedure Analyze_Call_And_Resolve;
1404 -- Do Analyze and Resolve calls for procedure call
1405 -- At end, check illegal order dependence.
1407 ------------------------------
1408 -- Analyze_Call_And_Resolve --
1409 ------------------------------
1411 procedure Analyze_Call_And_Resolve is
1413 if Nkind (N) = N_Procedure_Call_Statement then
1415 Resolve (N, Standard_Void_Type);
1419 end Analyze_Call_And_Resolve;
1421 -- Start of processing for Analyze_Procedure_Call
1424 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1425 -- a procedure call or an entry call. The prefix may denote an access
1426 -- to subprogram type, in which case an implicit dereference applies.
1427 -- If the prefix is an indexed component (without implicit dereference)
1428 -- then the construct denotes a call to a member of an entire family.
1429 -- If the prefix is a simple name, it may still denote a call to a
1430 -- parameterless member of an entry family. Resolution of these various
1431 -- interpretations is delicate.
1435 -- If this is a call of the form Obj.Op, the call may have been
1436 -- analyzed and possibly rewritten into a block, in which case
1439 if Analyzed (N) then
1443 -- If there is an error analyzing the name (which may have been
1444 -- rewritten if the original call was in prefix notation) then error
1445 -- has been emitted already, mark node and return.
1447 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1448 Set_Etype (N, Any_Type);
1452 -- Otherwise analyze the parameters
1454 if Present (Actuals) then
1455 Actual := First (Actuals);
1457 while Present (Actual) loop
1459 Check_Parameterless_Call (Actual);
1464 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1466 if Nkind (P) = N_Attribute_Reference
1467 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1469 Name_Elab_Subp_Body)
1471 if Present (Actuals) then
1473 ("no parameters allowed for this call", First (Actuals));
1477 Set_Etype (N, Standard_Void_Type);
1480 elsif Is_Entity_Name (P)
1481 and then Is_Record_Type (Etype (Entity (P)))
1482 and then Remote_AST_I_Dereference (P)
1486 elsif Is_Entity_Name (P)
1487 and then Ekind (Entity (P)) /= E_Entry_Family
1489 if Is_Access_Type (Etype (P))
1490 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1491 and then No (Actuals)
1492 and then Comes_From_Source (N)
1494 Error_Msg_N ("missing explicit dereference in call", N);
1497 Analyze_Call_And_Resolve;
1499 -- If the prefix is the simple name of an entry family, this is
1500 -- a parameterless call from within the task body itself.
1502 elsif Is_Entity_Name (P)
1503 and then Nkind (P) = N_Identifier
1504 and then Ekind (Entity (P)) = E_Entry_Family
1505 and then Present (Actuals)
1506 and then No (Next (First (Actuals)))
1508 -- Can be call to parameterless entry family. What appears to be the
1509 -- sole argument is in fact the entry index. Rewrite prefix of node
1510 -- accordingly. Source representation is unchanged by this
1514 Make_Indexed_Component (Loc,
1516 Make_Selected_Component (Loc,
1517 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1518 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1519 Expressions => Actuals);
1520 Set_Name (N, New_N);
1521 Set_Etype (New_N, Standard_Void_Type);
1522 Set_Parameter_Associations (N, No_List);
1523 Analyze_Call_And_Resolve;
1525 elsif Nkind (P) = N_Explicit_Dereference then
1526 if Ekind (Etype (P)) = E_Subprogram_Type then
1527 Analyze_Call_And_Resolve;
1529 Error_Msg_N ("expect access to procedure in call", P);
1532 -- The name can be a selected component or an indexed component that
1533 -- yields an access to subprogram. Such a prefix is legal if the call
1534 -- has parameter associations.
1536 elsif Is_Access_Type (Etype (P))
1537 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1539 if Present (Actuals) then
1540 Analyze_Call_And_Resolve;
1542 Error_Msg_N ("missing explicit dereference in call ", N);
1545 -- If not an access to subprogram, then the prefix must resolve to the
1546 -- name of an entry, entry family, or protected operation.
1548 -- For the case of a simple entry call, P is a selected component where
1549 -- the prefix is the task and the selector name is the entry. A call to
1550 -- a protected procedure will have the same syntax. If the protected
1551 -- object contains overloaded operations, the entity may appear as a
1552 -- function, the context will select the operation whose type is Void.
1554 elsif Nkind (P) = N_Selected_Component
1555 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1559 Analyze_Call_And_Resolve;
1561 elsif Nkind (P) = N_Selected_Component
1562 and then Ekind (Entity (Selector_Name (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,
1573 Prefix => New_Copy (P),
1574 Expressions => Actuals);
1575 Set_Name (N, New_N);
1576 Set_Etype (New_N, Standard_Void_Type);
1577 Set_Parameter_Associations (N, No_List);
1578 Analyze_Call_And_Resolve;
1580 -- For the case of a reference to an element of an entry family, P is
1581 -- an indexed component whose prefix is a selected component (task and
1582 -- entry family), and whose index is the entry family index.
1584 elsif Nkind (P) = N_Indexed_Component
1585 and then Nkind (Prefix (P)) = N_Selected_Component
1586 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1588 Analyze_Call_And_Resolve;
1590 -- If the prefix is the name of an entry family, it is a call from
1591 -- within the task body itself.
1593 elsif Nkind (P) = N_Indexed_Component
1594 and then Nkind (Prefix (P)) = N_Identifier
1595 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1598 Make_Selected_Component (Loc,
1599 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1600 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1601 Rewrite (Prefix (P), New_N);
1603 Analyze_Call_And_Resolve;
1605 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1606 -- procedure name, so the construct can only be a qualified expression.
1608 elsif Nkind (P) = N_Qualified_Expression
1609 and then Ada_Version >= Ada_2012
1611 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1614 -- Anything else is an error
1617 Error_Msg_N ("invalid procedure or entry call", N);
1619 end Analyze_Procedure_Call;
1621 ------------------------------
1622 -- Analyze_Return_Statement --
1623 ------------------------------
1625 procedure Analyze_Return_Statement (N : Node_Id) is
1627 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1628 N_Extended_Return_Statement));
1630 Returns_Object : constant Boolean :=
1631 Nkind (N) = N_Extended_Return_Statement
1633 (Nkind (N) = N_Simple_Return_Statement
1634 and then Present (Expression (N)));
1635 -- True if we're returning something; that is, "return <expression>;"
1636 -- or "return Result : T [:= ...]". False for "return;". Used for error
1637 -- checking: If Returns_Object is True, N should apply to a function
1638 -- body; otherwise N should apply to a procedure body, entry body,
1639 -- accept statement, or extended return statement.
1641 function Find_What_It_Applies_To return Entity_Id;
1642 -- Find the entity representing the innermost enclosing body, accept
1643 -- statement, or extended return statement. If the result is a callable
1644 -- construct or extended return statement, then this will be the value
1645 -- of the Return_Applies_To attribute. Otherwise, the program is
1646 -- illegal. See RM-6.5(4/2).
1648 -----------------------------
1649 -- Find_What_It_Applies_To --
1650 -----------------------------
1652 function Find_What_It_Applies_To return Entity_Id is
1653 Result : Entity_Id := Empty;
1656 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1657 -- and postconditions.
1659 for J in reverse 0 .. Scope_Stack.Last loop
1660 Result := Scope_Stack.Table (J).Entity;
1661 exit when not Ekind_In (Result, E_Block, E_Loop)
1662 and then Chars (Result) /= Name_uPostconditions;
1665 pragma Assert (Present (Result));
1667 end Find_What_It_Applies_To;
1669 -- Local declarations
1671 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1672 Kind : constant Entity_Kind := Ekind (Scope_Id);
1673 Loc : constant Source_Ptr := Sloc (N);
1674 Stm_Entity : constant Entity_Id :=
1676 (E_Return_Statement, Current_Scope, Loc, 'R');
1678 -- Start of processing for Analyze_Return_Statement
1681 Set_Return_Statement_Entity (N, Stm_Entity);
1683 Set_Etype (Stm_Entity, Standard_Void_Type);
1684 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1686 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1687 -- (4/2): an inner return statement will apply to this extended return.
1689 if Nkind (N) = N_Extended_Return_Statement then
1690 Push_Scope (Stm_Entity);
1693 -- Check that pragma No_Return is obeyed. Don't complain about the
1694 -- implicitly-generated return that is placed at the end.
1696 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1697 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1700 -- Warn on any unassigned OUT parameters if in procedure
1702 if Ekind (Scope_Id) = E_Procedure then
1703 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1706 -- Check that functions return objects, and other things do not
1708 if Kind = E_Function or else Kind = E_Generic_Function then
1709 if not Returns_Object then
1710 Error_Msg_N ("missing expression in return from function", N);
1713 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1714 if Returns_Object then
1715 Error_Msg_N ("procedure cannot return value (use function)", N);
1718 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1719 if Returns_Object then
1720 if Is_Protected_Type (Scope (Scope_Id)) then
1721 Error_Msg_N ("entry body cannot return value", N);
1723 Error_Msg_N ("accept statement cannot return value", N);
1727 elsif Kind = E_Return_Statement then
1729 -- We are nested within another return statement, which must be an
1730 -- extended_return_statement.
1732 if Returns_Object then
1733 if Nkind (N) = N_Extended_Return_Statement then
1735 ("extended return statement cannot be nested (use `RETURN;`)",
1738 -- Case of a simple return statement with a value inside extended
1739 -- return statement.
1743 ("return nested in extended return statement cannot return " &
1744 "value (use `RETURN;`)", N);
1749 Error_Msg_N ("illegal context for return statement", N);
1752 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1753 Analyze_Function_Return (N);
1755 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1756 Set_Return_Present (Scope_Id);
1759 if Nkind (N) = N_Extended_Return_Statement then
1763 Kill_Current_Values (Last_Assignment_Only => True);
1764 Check_Unreachable_Code (N);
1766 Analyze_Dimension (N);
1767 end Analyze_Return_Statement;
1769 -------------------------------------
1770 -- Analyze_Simple_Return_Statement --
1771 -------------------------------------
1773 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1775 if Present (Expression (N)) then
1776 Mark_Coextensions (N, Expression (N));
1779 Analyze_Return_Statement (N);
1780 end Analyze_Simple_Return_Statement;
1782 -------------------------
1783 -- Analyze_Return_Type --
1784 -------------------------
1786 procedure Analyze_Return_Type (N : Node_Id) is
1787 Designator : constant Entity_Id := Defining_Entity (N);
1788 Typ : Entity_Id := Empty;
1791 -- Normal case where result definition does not indicate an error
1793 if Result_Definition (N) /= Error then
1794 if Nkind (Result_Definition (N)) = N_Access_Definition then
1795 Check_SPARK_Restriction
1796 ("access result is not allowed", Result_Definition (N));
1798 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1801 AD : constant Node_Id :=
1802 Access_To_Subprogram_Definition (Result_Definition (N));
1804 if Present (AD) and then Protected_Present (AD) then
1805 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1807 Typ := Access_Definition (N, Result_Definition (N));
1811 Set_Parent (Typ, Result_Definition (N));
1812 Set_Is_Local_Anonymous_Access (Typ);
1813 Set_Etype (Designator, Typ);
1815 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1817 Null_Exclusion_Static_Checks (N);
1819 -- Subtype_Mark case
1822 Find_Type (Result_Definition (N));
1823 Typ := Entity (Result_Definition (N));
1824 Set_Etype (Designator, Typ);
1826 -- Unconstrained array as result is not allowed in SPARK
1828 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1829 Check_SPARK_Restriction
1830 ("returning an unconstrained array is not allowed",
1831 Result_Definition (N));
1834 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1836 Null_Exclusion_Static_Checks (N);
1838 -- If a null exclusion is imposed on the result type, then create
1839 -- a null-excluding itype (an access subtype) and use it as the
1840 -- function's Etype. Note that the null exclusion checks are done
1841 -- right before this, because they don't get applied to types that
1842 -- do not come from source.
1844 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1845 Set_Etype (Designator,
1846 Create_Null_Excluding_Itype
1849 Scope_Id => Scope (Current_Scope)));
1851 -- The new subtype must be elaborated before use because
1852 -- it is visible outside of the function. However its base
1853 -- type may not be frozen yet, so the reference that will
1854 -- force elaboration must be attached to the freezing of
1857 -- If the return specification appears on a proper body,
1858 -- the subtype will have been created already on the spec.
1860 if Is_Frozen (Typ) then
1861 if Nkind (Parent (N)) = N_Subprogram_Body
1862 and then Nkind (Parent (Parent (N))) = N_Subunit
1866 Build_Itype_Reference (Etype (Designator), Parent (N));
1870 Ensure_Freeze_Node (Typ);
1873 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1875 Set_Itype (IR, Etype (Designator));
1876 Append_Freeze_Actions (Typ, New_List (IR));
1881 Set_Etype (Designator, Typ);
1884 if Ekind (Typ) = E_Incomplete_Type
1885 and then Is_Value_Type (Typ)
1889 elsif Ekind (Typ) = E_Incomplete_Type
1890 or else (Is_Class_Wide_Type (Typ)
1891 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1893 -- AI05-0151: Tagged incomplete types are allowed in all formal
1894 -- parts. Untagged incomplete types are not allowed in bodies.
1896 if Ada_Version >= Ada_2012 then
1897 if Is_Tagged_Type (Typ) then
1900 elsif Nkind_In (Parent (Parent (N)),
1906 ("invalid use of untagged incomplete type&",
1910 -- The type must be completed in the current package. This
1911 -- is checked at the end of the package declaraton, when
1912 -- Taft-amendment types are identified. If the return type
1913 -- is class-wide, there is no required check, the type can
1914 -- be a bona fide TAT.
1916 if Ekind (Scope (Current_Scope)) = E_Package
1917 and then In_Private_Part (Scope (Current_Scope))
1918 and then not Is_Class_Wide_Type (Typ)
1920 Append_Elmt (Designator, Private_Dependents (Typ));
1925 ("invalid use of incomplete type&", Designator, Typ);
1930 -- Case where result definition does indicate an error
1933 Set_Etype (Designator, Any_Type);
1935 end Analyze_Return_Type;
1937 -----------------------------
1938 -- Analyze_Subprogram_Body --
1939 -----------------------------
1941 procedure Analyze_Subprogram_Body (N : Node_Id) is
1942 Loc : constant Source_Ptr := Sloc (N);
1943 Body_Spec : constant Node_Id := Specification (N);
1944 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1947 if Debug_Flag_C then
1948 Write_Str ("==> subprogram body ");
1949 Write_Name (Chars (Body_Id));
1950 Write_Str (" from ");
1951 Write_Location (Loc);
1956 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1958 -- The real work is split out into the helper, so it can do "return;"
1959 -- without skipping the debug output:
1961 Analyze_Subprogram_Body_Helper (N);
1963 if Debug_Flag_C then
1965 Write_Str ("<== subprogram body ");
1966 Write_Name (Chars (Body_Id));
1967 Write_Str (" from ");
1968 Write_Location (Loc);
1971 end Analyze_Subprogram_Body;
1973 --------------------------------------
1974 -- Analyze_Subprogram_Body_Contract --
1975 --------------------------------------
1977 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
1978 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
1979 Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl);
1981 Ref_Depends : Node_Id := Empty;
1982 Ref_Global : Node_Id := Empty;
1985 -- When a subprogram body declaration is erroneous, its defining entity
1986 -- is left unanalyzed. There is nothing left to do in this case because
1987 -- the body lacks a contract.
1989 if not Analyzed (Body_Id) then
1993 -- Locate and store pragmas Refined_Depends and Refined_Global since
1994 -- their order of analysis matters.
1996 Prag := Classifications (Contract (Body_Id));
1997 while Present (Prag) loop
1998 if Pragma_Name (Prag) = Name_Refined_Depends then
1999 Ref_Depends := Prag;
2000 elsif Pragma_Name (Prag) = Name_Refined_Global then
2004 Prag := Next_Pragma (Prag);
2007 -- Analyze Refined_Global first as Refined_Depends may mention items
2008 -- classified in the global refinement.
2010 if Present (Ref_Global) then
2011 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2013 -- When the corresponding Global aspect/pragma references a state with
2014 -- visible refinement, the body requires Refined_Global.
2016 elsif Present (Spec_Id) then
2017 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2019 if Present (Prag) and then Contains_Refined_State (Prag) then
2021 ("body of subprogram & requires global refinement",
2022 Body_Decl, Spec_Id);
2026 -- Refined_Depends must be analyzed after Refined_Global in order to see
2027 -- the modes of all global refinements.
2029 if Present (Ref_Depends) then
2030 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2032 -- When the corresponding Depends aspect/pragma references a state with
2033 -- visible refinement, the body requires Refined_Depends.
2035 elsif Present (Spec_Id) then
2036 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2038 if Present (Prag) and then Contains_Refined_State (Prag) then
2040 ("body of subprogram & requires dependance refinement",
2041 Body_Decl, Spec_Id);
2044 end Analyze_Subprogram_Body_Contract;
2046 ------------------------------------
2047 -- Analyze_Subprogram_Body_Helper --
2048 ------------------------------------
2050 -- This procedure is called for regular subprogram bodies, generic bodies,
2051 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2052 -- specification matters, and is used to create a proper declaration for
2053 -- the subprogram, or to perform conformance checks.
2055 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2056 Loc : constant Source_Ptr := Sloc (N);
2057 Body_Spec : constant Node_Id := Specification (N);
2058 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2059 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2060 Conformant : Boolean;
2062 Prot_Typ : Entity_Id := Empty;
2063 Spec_Id : Entity_Id;
2064 Spec_Decl : Node_Id := Empty;
2066 Last_Real_Spec_Entity : Entity_Id := Empty;
2067 -- When we analyze a separate spec, the entity chain ends up containing
2068 -- the formals, as well as any itypes generated during analysis of the
2069 -- default expressions for parameters, or the arguments of associated
2070 -- precondition/postcondition pragmas (which are analyzed in the context
2071 -- of the spec since they have visibility on formals).
2073 -- These entities belong with the spec and not the body. However we do
2074 -- the analysis of the body in the context of the spec (again to obtain
2075 -- visibility to the formals), and all the entities generated during
2076 -- this analysis end up also chained to the entity chain of the spec.
2077 -- But they really belong to the body, and there is circuitry to move
2078 -- them from the spec to the body.
2080 -- However, when we do this move, we don't want to move the real spec
2081 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2082 -- variable points to the last real spec entity, so we only move those
2083 -- chained beyond that point. It is initialized to Empty to deal with
2084 -- the case where there is no separate spec.
2086 procedure Check_Anonymous_Return;
2087 -- Ada 2005: if a function returns an access type that denotes a task,
2088 -- or a type that contains tasks, we must create a master entity for
2089 -- the anonymous type, which typically will be used in an allocator
2090 -- in the body of the function.
2092 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2093 -- Look ahead to recognize a pragma that may appear after the body.
2094 -- If there is a previous spec, check that it appears in the same
2095 -- declarative part. If the pragma is Inline_Always, perform inlining
2096 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2097 -- If the body acts as a spec, and inlining is required, we create a
2098 -- subprogram declaration for it, in order to attach the body to inline.
2099 -- If pragma does not appear after the body, check whether there is
2100 -- an inline pragma before any local declarations.
2102 procedure Check_Missing_Return;
2103 -- Checks for a function with a no return statements, and also performs
2104 -- the warning checks implemented by Check_Returns. In formal mode, also
2105 -- verify that a function ends with a RETURN and that a procedure does
2106 -- not contain any RETURN.
2108 function Disambiguate_Spec return Entity_Id;
2109 -- When a primitive is declared between the private view and the full
2110 -- view of a concurrent type which implements an interface, a special
2111 -- mechanism is used to find the corresponding spec of the primitive
2114 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2115 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2116 -- incomplete types coming from a limited context and swap their limited
2117 -- views with the non-limited ones.
2119 function Is_Private_Concurrent_Primitive
2120 (Subp_Id : Entity_Id) return Boolean;
2121 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2122 -- type that implements an interface and has a private view.
2124 procedure Set_Trivial_Subprogram (N : Node_Id);
2125 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2126 -- subprogram whose body is being analyzed. N is the statement node
2127 -- causing the flag to be set, if the following statement is a return
2128 -- of an entity, we mark the entity as set in source to suppress any
2129 -- warning on the stylized use of function stubs with a dummy return.
2131 procedure Verify_Overriding_Indicator;
2132 -- If there was a previous spec, the entity has been entered in the
2133 -- current scope previously. If the body itself carries an overriding
2134 -- indicator, check that it is consistent with the known status of the
2137 ----------------------------
2138 -- Check_Anonymous_Return --
2139 ----------------------------
2141 procedure Check_Anonymous_Return is
2147 if Present (Spec_Id) then
2153 if Ekind (Scop) = E_Function
2154 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2155 and then not Is_Thunk (Scop)
2156 and then (Has_Task (Designated_Type (Etype (Scop)))
2158 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2160 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2161 and then Expander_Active
2163 -- Avoid cases with no tasking support
2165 and then RTE_Available (RE_Current_Master)
2166 and then not Restriction_Active (No_Task_Hierarchy)
2169 Make_Object_Declaration (Loc,
2170 Defining_Identifier =>
2171 Make_Defining_Identifier (Loc, Name_uMaster),
2172 Constant_Present => True,
2173 Object_Definition =>
2174 New_Reference_To (RTE (RE_Master_Id), Loc),
2176 Make_Explicit_Dereference (Loc,
2177 New_Reference_To (RTE (RE_Current_Master), Loc)));
2179 if Present (Declarations (N)) then
2180 Prepend (Decl, Declarations (N));
2182 Set_Declarations (N, New_List (Decl));
2185 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2186 Set_Has_Master_Entity (Scop);
2188 -- Now mark the containing scope as a task master
2191 while Nkind (Par) /= N_Compilation_Unit loop
2192 Par := Parent (Par);
2193 pragma Assert (Present (Par));
2195 -- If we fall off the top, we are at the outer level, and
2196 -- the environment task is our effective master, so nothing
2200 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2202 Set_Is_Task_Master (Par, True);
2207 end Check_Anonymous_Return;
2209 -------------------------
2210 -- Check_Inline_Pragma --
2211 -------------------------
2213 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2217 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2218 -- True when N is a pragma Inline or Inline_Always that applies
2219 -- to this subprogram.
2221 -----------------------
2222 -- Is_Inline_Pragma --
2223 -----------------------
2225 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2228 Nkind (N) = N_Pragma
2230 (Pragma_Name (N) = Name_Inline_Always
2233 and then Pragma_Name (N) = Name_Inline))
2236 (Expression (First (Pragma_Argument_Associations (N)))) =
2238 end Is_Inline_Pragma;
2240 -- Start of processing for Check_Inline_Pragma
2243 if not Expander_Active then
2247 if Is_List_Member (N)
2248 and then Present (Next (N))
2249 and then Is_Inline_Pragma (Next (N))
2253 elsif Nkind (N) /= N_Subprogram_Body_Stub
2254 and then Present (Declarations (N))
2255 and then Is_Inline_Pragma (First (Declarations (N)))
2257 Prag := First (Declarations (N));
2263 if Present (Prag) then
2264 if Present (Spec_Id) then
2265 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2270 -- Create a subprogram declaration, to make treatment uniform
2273 Subp : constant Entity_Id :=
2274 Make_Defining_Identifier (Loc, Chars (Body_Id));
2275 Decl : constant Node_Id :=
2276 Make_Subprogram_Declaration (Loc,
2278 New_Copy_Tree (Specification (N)));
2281 Set_Defining_Unit_Name (Specification (Decl), Subp);
2283 if Present (First_Formal (Body_Id)) then
2284 Plist := Copy_Parameter_List (Body_Id);
2285 Set_Parameter_Specifications
2286 (Specification (Decl), Plist);
2289 Insert_Before (N, Decl);
2292 Set_Has_Pragma_Inline (Subp);
2294 if Pragma_Name (Prag) = Name_Inline_Always then
2295 Set_Is_Inlined (Subp);
2296 Set_Has_Pragma_Inline_Always (Subp);
2303 end Check_Inline_Pragma;
2305 --------------------------
2306 -- Check_Missing_Return --
2307 --------------------------
2309 procedure Check_Missing_Return is
2311 Missing_Ret : Boolean;
2314 if Nkind (Body_Spec) = N_Function_Specification then
2315 if Present (Spec_Id) then
2321 if Return_Present (Id) then
2322 Check_Returns (HSS, 'F', Missing_Ret);
2325 Set_Has_Missing_Return (Id);
2328 elsif Is_Generic_Subprogram (Id)
2329 or else not Is_Machine_Code_Subprogram (Id)
2331 Error_Msg_N ("missing RETURN statement in function body", N);
2334 -- If procedure with No_Return, check returns
2336 elsif Nkind (Body_Spec) = N_Procedure_Specification
2337 and then Present (Spec_Id)
2338 and then No_Return (Spec_Id)
2340 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2343 -- Special checks in SPARK mode
2345 if Nkind (Body_Spec) = N_Function_Specification then
2347 -- In SPARK mode, last statement of a function should be a return
2350 Stat : constant Node_Id := Last_Source_Statement (HSS);
2353 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2354 N_Extended_Return_Statement)
2356 Check_SPARK_Restriction
2357 ("last statement in function should be RETURN", Stat);
2361 -- In SPARK mode, verify that a procedure has no return
2363 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2364 if Present (Spec_Id) then
2370 -- Would be nice to point to return statement here, can we
2371 -- borrow the Check_Returns procedure here ???
2373 if Return_Present (Id) then
2374 Check_SPARK_Restriction
2375 ("procedure should not have RETURN", N);
2378 end Check_Missing_Return;
2380 -----------------------
2381 -- Disambiguate_Spec --
2382 -----------------------
2384 function Disambiguate_Spec return Entity_Id is
2385 Priv_Spec : Entity_Id;
2388 procedure Replace_Types (To_Corresponding : Boolean);
2389 -- Depending on the flag, replace the type of formal parameters of
2390 -- Body_Id if it is a concurrent type implementing interfaces with
2391 -- the corresponding record type or the other way around.
2393 procedure Replace_Types (To_Corresponding : Boolean) is
2395 Formal_Typ : Entity_Id;
2398 Formal := First_Formal (Body_Id);
2399 while Present (Formal) loop
2400 Formal_Typ := Etype (Formal);
2402 if Is_Class_Wide_Type (Formal_Typ) then
2403 Formal_Typ := Root_Type (Formal_Typ);
2406 -- From concurrent type to corresponding record
2408 if To_Corresponding then
2409 if Is_Concurrent_Type (Formal_Typ)
2410 and then Present (Corresponding_Record_Type (Formal_Typ))
2411 and then Present (Interfaces (
2412 Corresponding_Record_Type (Formal_Typ)))
2415 Corresponding_Record_Type (Formal_Typ));
2418 -- From corresponding record to concurrent type
2421 if Is_Concurrent_Record_Type (Formal_Typ)
2422 and then Present (Interfaces (Formal_Typ))
2425 Corresponding_Concurrent_Type (Formal_Typ));
2429 Next_Formal (Formal);
2433 -- Start of processing for Disambiguate_Spec
2436 -- Try to retrieve the specification of the body as is. All error
2437 -- messages are suppressed because the body may not have a spec in
2438 -- its current state.
2440 Spec_N := Find_Corresponding_Spec (N, False);
2442 -- It is possible that this is the body of a primitive declared
2443 -- between a private and a full view of a concurrent type. The
2444 -- controlling parameter of the spec carries the concurrent type,
2445 -- not the corresponding record type as transformed by Analyze_
2446 -- Subprogram_Specification. In such cases, we undo the change
2447 -- made by the analysis of the specification and try to find the
2450 -- Note that wrappers already have their corresponding specs and
2451 -- bodies set during their creation, so if the candidate spec is
2452 -- a wrapper, then we definitely need to swap all types to their
2453 -- original concurrent status.
2456 or else Is_Primitive_Wrapper (Spec_N)
2458 -- Restore all references of corresponding record types to the
2459 -- original concurrent types.
2461 Replace_Types (To_Corresponding => False);
2462 Priv_Spec := Find_Corresponding_Spec (N, False);
2464 -- The current body truly belongs to a primitive declared between
2465 -- a private and a full view. We leave the modified body as is,
2466 -- and return the true spec.
2468 if Present (Priv_Spec)
2469 and then Is_Private_Primitive (Priv_Spec)
2474 -- In case that this is some sort of error, restore the original
2475 -- state of the body.
2477 Replace_Types (To_Corresponding => True);
2481 end Disambiguate_Spec;
2483 ----------------------------
2484 -- Exchange_Limited_Views --
2485 ----------------------------
2487 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2488 procedure Detect_And_Exchange (Id : Entity_Id);
2489 -- Determine whether Id's type denotes an incomplete type associated
2490 -- with a limited with clause and exchange the limited view with the
2493 -------------------------
2494 -- Detect_And_Exchange --
2495 -------------------------
2497 procedure Detect_And_Exchange (Id : Entity_Id) is
2498 Typ : constant Entity_Id := Etype (Id);
2501 if Ekind (Typ) = E_Incomplete_Type
2502 and then From_Limited_With (Typ)
2503 and then Present (Non_Limited_View (Typ))
2505 Set_Etype (Id, Non_Limited_View (Typ));
2507 end Detect_And_Exchange;
2513 -- Start of processing for Exchange_Limited_Views
2516 if No (Subp_Id) then
2519 -- Do not process subprogram bodies as they already use the non-
2520 -- limited view of types.
2522 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2526 -- Examine all formals and swap views when applicable
2528 Formal := First_Formal (Subp_Id);
2529 while Present (Formal) loop
2530 Detect_And_Exchange (Formal);
2532 Next_Formal (Formal);
2535 -- Process the return type of a function
2537 if Ekind (Subp_Id) = E_Function then
2538 Detect_And_Exchange (Subp_Id);
2540 end Exchange_Limited_Views;
2542 -------------------------------------
2543 -- Is_Private_Concurrent_Primitive --
2544 -------------------------------------
2546 function Is_Private_Concurrent_Primitive
2547 (Subp_Id : Entity_Id) return Boolean
2549 Formal_Typ : Entity_Id;
2552 if Present (First_Formal (Subp_Id)) then
2553 Formal_Typ := Etype (First_Formal (Subp_Id));
2555 if Is_Concurrent_Record_Type (Formal_Typ) then
2556 if Is_Class_Wide_Type (Formal_Typ) then
2557 Formal_Typ := Root_Type (Formal_Typ);
2560 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2563 -- The type of the first formal is a concurrent tagged type with
2567 Is_Concurrent_Type (Formal_Typ)
2568 and then Is_Tagged_Type (Formal_Typ)
2569 and then Has_Private_Declaration (Formal_Typ);
2573 end Is_Private_Concurrent_Primitive;
2575 ----------------------------
2576 -- Set_Trivial_Subprogram --
2577 ----------------------------
2579 procedure Set_Trivial_Subprogram (N : Node_Id) is
2580 Nxt : constant Node_Id := Next (N);
2583 Set_Is_Trivial_Subprogram (Body_Id);
2585 if Present (Spec_Id) then
2586 Set_Is_Trivial_Subprogram (Spec_Id);
2590 and then Nkind (Nxt) = N_Simple_Return_Statement
2591 and then No (Next (Nxt))
2592 and then Present (Expression (Nxt))
2593 and then Is_Entity_Name (Expression (Nxt))
2595 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2597 end Set_Trivial_Subprogram;
2599 ---------------------------------
2600 -- Verify_Overriding_Indicator --
2601 ---------------------------------
2603 procedure Verify_Overriding_Indicator is
2605 if Must_Override (Body_Spec) then
2606 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2607 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2611 elsif not Present (Overridden_Operation (Spec_Id)) then
2613 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2616 elsif Must_Not_Override (Body_Spec) then
2617 if Present (Overridden_Operation (Spec_Id)) then
2619 ("subprogram& overrides inherited operation",
2620 Body_Spec, Spec_Id);
2622 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2623 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2626 ("subprogram & overrides predefined operator ",
2627 Body_Spec, Spec_Id);
2629 -- If this is not a primitive operation or protected subprogram,
2630 -- then the overriding indicator is altogether illegal.
2632 elsif not Is_Primitive (Spec_Id)
2633 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2636 ("overriding indicator only allowed " &
2637 "if subprogram is primitive",
2642 and then Present (Overridden_Operation (Spec_Id))
2644 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2645 Style.Missing_Overriding (N, Body_Id);
2648 and then Can_Override_Operator (Spec_Id)
2649 and then not Is_Predefined_File_Name
2650 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2652 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2653 Style.Missing_Overriding (N, Body_Id);
2655 end Verify_Overriding_Indicator;
2657 -- Start of processing for Analyze_Subprogram_Body_Helper
2660 -- Generic subprograms are handled separately. They always have a
2661 -- generic specification. Determine whether current scope has a
2662 -- previous declaration.
2664 -- If the subprogram body is defined within an instance of the same
2665 -- name, the instance appears as a package renaming, and will be hidden
2666 -- within the subprogram.
2668 if Present (Prev_Id)
2669 and then not Is_Overloadable (Prev_Id)
2670 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2671 or else Comes_From_Source (Prev_Id))
2673 if Is_Generic_Subprogram (Prev_Id) then
2675 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2676 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2678 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2680 if Nkind (N) = N_Subprogram_Body then
2681 HSS := Handled_Statement_Sequence (N);
2682 Check_Missing_Return;
2688 -- Previous entity conflicts with subprogram name. Attempting to
2689 -- enter name will post error.
2691 Enter_Name (Body_Id);
2695 -- Non-generic case, find the subprogram declaration, if one was seen,
2696 -- or enter new overloaded entity in the current scope. If the
2697 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2698 -- part of the context of one of its subunits. No need to redo the
2701 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2705 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2707 if Nkind (N) = N_Subprogram_Body_Stub
2708 or else No (Corresponding_Spec (N))
2710 if Is_Private_Concurrent_Primitive (Body_Id) then
2711 Spec_Id := Disambiguate_Spec;
2713 Spec_Id := Find_Corresponding_Spec (N);
2716 -- If this is a duplicate body, no point in analyzing it
2718 if Error_Posted (N) then
2722 -- A subprogram body should cause freezing of its own declaration,
2723 -- but if there was no previous explicit declaration, then the
2724 -- subprogram will get frozen too late (there may be code within
2725 -- the body that depends on the subprogram having been frozen,
2726 -- such as uses of extra formals), so we force it to be frozen
2727 -- here. Same holds if the body and spec are compilation units.
2728 -- Finally, if the return type is an anonymous access to protected
2729 -- subprogram, it must be frozen before the body because its
2730 -- expansion has generated an equivalent type that is used when
2731 -- elaborating the body.
2733 -- An exception in the case of Ada 2012, AI05-177: The bodies
2734 -- created for expression functions do not freeze.
2737 and then Nkind (Original_Node (N)) /= N_Expression_Function
2739 Freeze_Before (N, Body_Id);
2741 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2742 Freeze_Before (N, Spec_Id);
2744 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2745 Freeze_Before (N, Etype (Body_Id));
2749 Spec_Id := Corresponding_Spec (N);
2753 -- Language-defined aspects cannot appear in a subprogram body [stub] if
2754 -- the subprogram has a separate spec. Certainly implementation-defined
2755 -- aspects are allowed to appear (per Aspects_On_Body_Of_Stub_OK).
2757 if Has_Aspects (N) then
2758 if Present (Spec_Id)
2759 and then not Aspects_On_Body_Or_Stub_OK (N)
2761 -- Do not emit an error on a subprogram body stub that act as
2764 and then Nkind (Parent (Parent (Spec_Id))) /= N_Subprogram_Body_Stub
2767 ("aspect specifications must appear in subprogram declaration",
2770 -- Delay the analysis of aspect specifications that apply to a body
2771 -- stub until the proper body is analyzed. If the corresponding body
2772 -- is missing, the aspects are still analyzed in Analyze_Proper_Body.
2774 elsif Nkind (N) in N_Body_Stub then
2778 Analyze_Aspect_Specifications (N, Body_Id);
2782 -- Previously we scanned the body to look for nested subprograms, and
2783 -- rejected an inline directive if nested subprograms were present,
2784 -- because the back-end would generate conflicting symbols for the
2785 -- nested bodies. This is now unnecessary.
2787 -- Look ahead to recognize a pragma Inline that appears after the body
2789 Check_Inline_Pragma (Spec_Id);
2791 -- Deal with special case of a fully private operation in the body of
2792 -- the protected type. We must create a declaration for the subprogram,
2793 -- in order to attach the protected subprogram that will be used in
2794 -- internal calls. We exclude compiler generated bodies from the
2795 -- expander since the issue does not arise for those cases.
2798 and then Comes_From_Source (N)
2799 and then Is_Protected_Type (Current_Scope)
2801 Spec_Id := Build_Private_Protected_Declaration (N);
2804 -- If a separate spec is present, then deal with freezing issues
2806 if Present (Spec_Id) then
2807 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2808 Verify_Overriding_Indicator;
2810 -- In general, the spec will be frozen when we start analyzing the
2811 -- body. However, for internally generated operations, such as
2812 -- wrapper functions for inherited operations with controlling
2813 -- results, the spec may not have been frozen by the time we expand
2814 -- the freeze actions that include the bodies. In particular, extra
2815 -- formals for accessibility or for return-in-place may need to be
2816 -- generated. Freeze nodes, if any, are inserted before the current
2817 -- body. These freeze actions are also needed in ASIS mode to enable
2818 -- the proper back-annotations.
2820 if not Is_Frozen (Spec_Id)
2821 and then (Expander_Active or ASIS_Mode)
2823 -- Force the generation of its freezing node to ensure proper
2824 -- management of access types in the backend.
2826 -- This is definitely needed for some cases, but it is not clear
2827 -- why, to be investigated further???
2829 Set_Has_Delayed_Freeze (Spec_Id);
2830 Freeze_Before (N, Spec_Id);
2834 -- Mark presence of postcondition procedure in current scope and mark
2835 -- the procedure itself as needing debug info. The latter is important
2836 -- when analyzing decision coverage (for example, for MC/DC coverage).
2838 if Chars (Body_Id) = Name_uPostconditions then
2839 Set_Has_Postconditions (Current_Scope);
2840 Set_Debug_Info_Needed (Body_Id);
2843 -- Place subprogram on scope stack, and make formals visible. If there
2844 -- is a spec, the visible entity remains that of the spec.
2846 if Present (Spec_Id) then
2847 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2849 if Is_Child_Unit (Spec_Id) then
2850 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2854 Style.Check_Identifier (Body_Id, Spec_Id);
2857 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2858 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2860 if Is_Abstract_Subprogram (Spec_Id) then
2861 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2865 Set_Convention (Body_Id, Convention (Spec_Id));
2866 Set_Has_Completion (Spec_Id);
2868 if Is_Protected_Type (Scope (Spec_Id)) then
2869 Prot_Typ := Scope (Spec_Id);
2872 -- If this is a body generated for a renaming, do not check for
2873 -- full conformance. The check is redundant, because the spec of
2874 -- the body is a copy of the spec in the renaming declaration,
2875 -- and the test can lead to spurious errors on nested defaults.
2877 if Present (Spec_Decl)
2878 and then not Comes_From_Source (N)
2880 (Nkind (Original_Node (Spec_Decl)) =
2881 N_Subprogram_Renaming_Declaration
2882 or else (Present (Corresponding_Body (Spec_Decl))
2884 Nkind (Unit_Declaration_Node
2885 (Corresponding_Body (Spec_Decl))) =
2886 N_Subprogram_Renaming_Declaration))
2890 -- Conversely, the spec may have been generated for specless body
2891 -- with an inline pragma.
2893 elsif Comes_From_Source (N)
2894 and then not Comes_From_Source (Spec_Id)
2895 and then Has_Pragma_Inline (Spec_Id)
2902 Fully_Conformant, True, Conformant, Body_Id);
2905 -- If the body is not fully conformant, we have to decide if we
2906 -- should analyze it or not. If it has a really messed up profile
2907 -- then we probably should not analyze it, since we will get too
2908 -- many bogus messages.
2910 -- Our decision is to go ahead in the non-fully conformant case
2911 -- only if it is at least mode conformant with the spec. Note
2912 -- that the call to Check_Fully_Conformant has issued the proper
2913 -- error messages to complain about the lack of conformance.
2916 and then not Mode_Conformant (Body_Id, Spec_Id)
2922 if Spec_Id /= Body_Id then
2923 Reference_Body_Formals (Spec_Id, Body_Id);
2926 if Nkind (N) = N_Subprogram_Body_Stub then
2927 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
2932 Set_Corresponding_Spec (N, Spec_Id);
2934 -- Ada 2005 (AI-345): If the operation is a primitive operation
2935 -- of a concurrent type, the type of the first parameter has been
2936 -- replaced with the corresponding record, which is the proper
2937 -- run-time structure to use. However, within the body there may
2938 -- be uses of the formals that depend on primitive operations
2939 -- of the type (in particular calls in prefixed form) for which
2940 -- we need the original concurrent type. The operation may have
2941 -- several controlling formals, so the replacement must be done
2944 if Comes_From_Source (Spec_Id)
2945 and then Present (First_Entity (Spec_Id))
2946 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2947 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2948 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
2949 and then Present (Corresponding_Concurrent_Type
2950 (Etype (First_Entity (Spec_Id))))
2953 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2957 Form := First_Formal (Spec_Id);
2958 while Present (Form) loop
2959 if Etype (Form) = Typ then
2960 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2968 -- Make the formals visible, and place subprogram on scope stack.
2969 -- This is also the point at which we set Last_Real_Spec_Entity
2970 -- to mark the entities which will not be moved to the body.
2972 Install_Formals (Spec_Id);
2973 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2975 -- Within an instance, add local renaming declarations so that
2976 -- gdb can retrieve the values of actuals more easily. This is
2977 -- only relevant if generating code (and indeed we definitely
2978 -- do not want these definitions -gnatc mode, because that would
2981 if Is_Generic_Instance (Spec_Id)
2982 and then Is_Wrapper_Package (Current_Scope)
2983 and then Expander_Active
2985 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2988 Push_Scope (Spec_Id);
2990 -- Set SPARK_Mode from spec if spec had a SPARK_Mode pragma
2992 if Present (SPARK_Mode_Pragmas (Spec_Id)) then
2994 Get_SPARK_Mode_From_Pragma (SPARK_Mode_Pragmas (Spec_Id));
2997 -- Make sure that the subprogram is immediately visible. For
2998 -- child units that have no separate spec this is indispensable.
2999 -- Otherwise it is safe albeit redundant.
3001 Set_Is_Immediately_Visible (Spec_Id);
3004 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3005 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3006 Set_Ekind (Body_Id, E_Subprogram_Body);
3007 Set_Scope (Body_Id, Scope (Spec_Id));
3008 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3010 -- Case of subprogram body with no previous spec
3013 -- Check for style warning required
3017 -- Only apply check for source level subprograms for which checks
3018 -- have not been suppressed.
3020 and then Comes_From_Source (Body_Id)
3021 and then not Suppress_Style_Checks (Body_Id)
3023 -- No warnings within an instance
3025 and then not In_Instance
3027 -- No warnings for expression functions
3029 and then Nkind (Original_Node (N)) /= N_Expression_Function
3031 Style.Body_With_No_Spec (N);
3034 New_Overloaded_Entity (Body_Id);
3036 if Nkind (N) /= N_Subprogram_Body_Stub then
3037 Set_Acts_As_Spec (N);
3038 Generate_Definition (Body_Id);
3039 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3041 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3042 Install_Formals (Body_Id);
3043 Push_Scope (Body_Id);
3046 -- For stubs and bodies with no previous spec, generate references to
3049 Generate_Reference_To_Formals (Body_Id);
3052 -- If the return type is an anonymous access type whose designated type
3053 -- is the limited view of a class-wide type and the non-limited view is
3054 -- available, update the return type accordingly.
3056 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3062 Rtyp := Etype (Current_Scope);
3064 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3065 Etyp := Directly_Designated_Type (Rtyp);
3067 if Is_Class_Wide_Type (Etyp)
3068 and then From_Limited_With (Etyp)
3070 Set_Directly_Designated_Type
3071 (Etype (Current_Scope), Available_View (Etyp));
3077 -- If this is the proper body of a stub, we must verify that the stub
3078 -- conforms to the body, and to the previous spec if one was present.
3079 -- We know already that the body conforms to that spec. This test is
3080 -- only required for subprograms that come from source.
3082 if Nkind (Parent (N)) = N_Subunit
3083 and then Comes_From_Source (N)
3084 and then not Error_Posted (Body_Id)
3085 and then Nkind (Corresponding_Stub (Parent (N))) =
3086 N_Subprogram_Body_Stub
3089 Old_Id : constant Entity_Id :=
3091 (Specification (Corresponding_Stub (Parent (N))));
3093 Conformant : Boolean := False;
3096 if No (Spec_Id) then
3097 Check_Fully_Conformant (Body_Id, Old_Id);
3101 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3103 if not Conformant then
3105 -- The stub was taken to be a new declaration. Indicate that
3108 Set_Has_Completion (Old_Id, False);
3114 Set_Has_Completion (Body_Id);
3115 Check_Eliminated (Body_Id);
3117 if Nkind (N) = N_Subprogram_Body_Stub then
3121 -- Handle frontend inlining. There is no need to prepare us for inlining
3122 -- if we will not generate the code.
3126 if not Debug_Flag_Dot_K then
3127 if Present (Spec_Id)
3128 and then Expander_Active
3130 (Has_Pragma_Inline_Always (Spec_Id)
3131 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3133 Build_Body_To_Inline (N, Spec_Id);
3138 elsif Expander_Active
3139 and then Serious_Errors_Detected = 0
3140 and then Present (Spec_Id)
3141 and then Has_Pragma_Inline (Spec_Id)
3143 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3146 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3147 -- of the specification we have to install the private withed units.
3148 -- This holds for child units as well.
3150 if Is_Compilation_Unit (Body_Id)
3151 or else Nkind (Parent (N)) = N_Compilation_Unit
3153 Install_Private_With_Clauses (Body_Id);
3156 Check_Anonymous_Return;
3158 -- Set the Protected_Formal field of each extra formal of the protected
3159 -- subprogram to reference the corresponding extra formal of the
3160 -- subprogram that implements it. For regular formals this occurs when
3161 -- the protected subprogram's declaration is expanded, but the extra
3162 -- formals don't get created until the subprogram is frozen. We need to
3163 -- do this before analyzing the protected subprogram's body so that any
3164 -- references to the original subprogram's extra formals will be changed
3165 -- refer to the implementing subprogram's formals (see Expand_Formal).
3167 if Present (Spec_Id)
3168 and then Is_Protected_Type (Scope (Spec_Id))
3169 and then Present (Protected_Body_Subprogram (Spec_Id))
3172 Impl_Subp : constant Entity_Id :=
3173 Protected_Body_Subprogram (Spec_Id);
3174 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3175 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3177 while Present (Prot_Ext_Formal) loop
3178 pragma Assert (Present (Impl_Ext_Formal));
3179 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3180 Next_Formal_With_Extras (Prot_Ext_Formal);
3181 Next_Formal_With_Extras (Impl_Ext_Formal);
3186 -- Now we can go on to analyze the body
3188 HSS := Handled_Statement_Sequence (N);
3189 Set_Actual_Subtypes (N, Current_Scope);
3191 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3192 -- invariants and predicates associated with the body and its spec.
3193 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3194 -- prepares the contract assertions for generic subprograms or for ASIS.
3195 -- Do not generate contract checks in SPARK mode.
3197 if not GNATprove_Mode then
3198 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3201 -- Add a declaration for the Protection object, renaming declarations
3202 -- for discriminals and privals and finally a declaration for the entry
3203 -- family index (if applicable). This form of early expansion is done
3204 -- when the Expander is active because Install_Private_Data_Declarations
3205 -- references entities which were created during regular expansion. The
3206 -- body may be the rewritting of an expression function, and we need to
3207 -- verify that the original node is in the source.
3210 and then Comes_From_Source (Original_Node (N))
3211 and then Present (Prot_Typ)
3212 and then Present (Spec_Id)
3213 and then not Is_Eliminated (Spec_Id)
3215 Install_Private_Data_Declarations
3216 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3219 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3220 -- may now appear in parameter and result profiles. Since the analysis
3221 -- of a subprogram body may use the parameter and result profile of the
3222 -- spec, swap any limited views with their non-limited counterpart.
3224 if Ada_Version >= Ada_2012 then
3225 Exchange_Limited_Views (Spec_Id);
3228 -- Analyze the declarations (this call will analyze the precondition
3229 -- Check pragmas we prepended to the list, as well as the declaration
3230 -- of the _Postconditions procedure).
3232 Analyze_Declarations (Declarations (N));
3234 -- Check completion, and analyze the statements
3237 Inspect_Deferred_Constant_Completion (Declarations (N));
3240 -- Deal with end of scope processing for the body
3242 Process_End_Label (HSS, 't', Current_Scope);
3244 Check_Subprogram_Order (N);
3245 Set_Analyzed (Body_Id);
3247 -- If we have a separate spec, then the analysis of the declarations
3248 -- caused the entities in the body to be chained to the spec id, but
3249 -- we want them chained to the body id. Only the formal parameters
3250 -- end up chained to the spec id in this case.
3252 if Present (Spec_Id) then
3254 -- We must conform to the categorization of our spec
3256 Validate_Categorization_Dependency (N, Spec_Id);
3258 -- And if this is a child unit, the parent units must conform
3260 if Is_Child_Unit (Spec_Id) then
3261 Validate_Categorization_Dependency
3262 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3265 -- Here is where we move entities from the spec to the body
3267 -- Case where there are entities that stay with the spec
3269 if Present (Last_Real_Spec_Entity) then
3271 -- No body entities (happens when the only real spec entities come
3272 -- from precondition and postcondition pragmas).
3274 if No (Last_Entity (Body_Id)) then
3276 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3278 -- Body entities present (formals), so chain stuff past them
3282 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3285 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3286 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3287 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3289 -- Case where there are no spec entities, in this case there can be
3290 -- no body entities either, so just move everything.
3293 pragma Assert (No (Last_Entity (Body_Id)));
3294 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3295 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3296 Set_First_Entity (Spec_Id, Empty);
3297 Set_Last_Entity (Spec_Id, Empty);
3301 Check_Missing_Return;
3303 -- Now we are going to check for variables that are never modified in
3304 -- the body of the procedure. But first we deal with a special case
3305 -- where we want to modify this check. If the body of the subprogram
3306 -- starts with a raise statement or its equivalent, or if the body
3307 -- consists entirely of a null statement, then it is pretty obvious
3308 -- that it is OK to not reference the parameters. For example, this
3309 -- might be the following common idiom for a stubbed function:
3310 -- statement of the procedure raises an exception. In particular this
3311 -- deals with the common idiom of a stubbed function, which might
3312 -- appear as something like:
3314 -- function F (A : Integer) return Some_Type;
3317 -- raise Program_Error;
3321 -- Here the purpose of X is simply to satisfy the annoying requirement
3322 -- in Ada that there be at least one return, and we certainly do not
3323 -- want to go posting warnings on X that it is not initialized! On
3324 -- the other hand, if X is entirely unreferenced that should still
3327 -- What we do is to detect these cases, and if we find them, flag the
3328 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3329 -- suppress unwanted warnings. For the case of the function stub above
3330 -- we have a special test to set X as apparently assigned to suppress
3337 -- Skip initial labels (for one thing this occurs when we are in
3338 -- front end ZCX mode, but in any case it is irrelevant), and also
3339 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3341 Stm := First (Statements (HSS));
3342 while Nkind (Stm) = N_Label
3343 or else Nkind (Stm) in N_Push_xxx_Label
3348 -- Do the test on the original statement before expansion
3351 Ostm : constant Node_Id := Original_Node (Stm);
3354 -- If explicit raise statement, turn on flag
3356 if Nkind (Ostm) = N_Raise_Statement then
3357 Set_Trivial_Subprogram (Stm);
3359 -- If null statement, and no following statements, turn on flag
3361 elsif Nkind (Stm) = N_Null_Statement
3362 and then Comes_From_Source (Stm)
3363 and then No (Next (Stm))
3365 Set_Trivial_Subprogram (Stm);
3367 -- Check for explicit call cases which likely raise an exception
3369 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3370 if Is_Entity_Name (Name (Ostm)) then
3372 Ent : constant Entity_Id := Entity (Name (Ostm));
3375 -- If the procedure is marked No_Return, then likely it
3376 -- raises an exception, but in any case it is not coming
3377 -- back here, so turn on the flag.
3380 and then Ekind (Ent) = E_Procedure
3381 and then No_Return (Ent)
3383 Set_Trivial_Subprogram (Stm);
3391 -- Check for variables that are never modified
3397 -- If there is a separate spec, then transfer Never_Set_In_Source
3398 -- flags from out parameters to the corresponding entities in the
3399 -- body. The reason we do that is we want to post error flags on
3400 -- the body entities, not the spec entities.
3402 if Present (Spec_Id) then
3403 E1 := First_Entity (Spec_Id);
3404 while Present (E1) loop
3405 if Ekind (E1) = E_Out_Parameter then
3406 E2 := First_Entity (Body_Id);
3407 while Present (E2) loop
3408 exit when Chars (E1) = Chars (E2);
3412 if Present (E2) then
3413 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3421 -- Check references in body
3423 Check_References (Body_Id);
3425 end Analyze_Subprogram_Body_Helper;
3427 ---------------------------------
3428 -- Analyze_Subprogram_Contract --
3429 ---------------------------------
3431 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3432 Items : constant Node_Id := Contract (Subp);
3433 Case_Prag : Node_Id := Empty;
3434 Depends : Node_Id := Empty;
3435 Global : Node_Id := Empty;
3437 Post_Prag : Node_Id := Empty;
3439 Seen_In_Case : Boolean := False;
3440 Seen_In_Post : Boolean := False;
3443 if Present (Items) then
3445 -- Analyze pre- and postconditions
3447 Prag := Pre_Post_Conditions (Items);
3448 while Present (Prag) loop
3449 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3451 -- Verify whether a postcondition mentions attribute 'Result and
3452 -- its expression introduces a post-state.
3454 if Warn_On_Suspicious_Contract
3455 and then Pragma_Name (Prag) = Name_Postcondition
3458 Check_Result_And_Post_State (Prag, Seen_In_Post);
3461 Prag := Next_Pragma (Prag);
3464 -- Analyze contract-cases and test-cases
3466 Prag := Contract_Test_Cases (Items);
3467 while Present (Prag) loop
3468 Nam := Pragma_Name (Prag);
3470 if Nam = Name_Contract_Cases then
3471 Analyze_Contract_Cases_In_Decl_Part (Prag);
3473 -- Verify whether contract-cases mention attribute 'Result and
3474 -- its expression introduces a post-state. Perform the check
3475 -- only when the pragma is legal.
3477 if Warn_On_Suspicious_Contract
3478 and then not Error_Posted (Prag)
3481 Check_Result_And_Post_State (Prag, Seen_In_Case);
3485 pragma Assert (Nam = Name_Test_Case);
3486 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3489 Prag := Next_Pragma (Prag);
3492 -- Analyze classification pragmas
3494 Prag := Classifications (Contract (Subp));
3495 while Present (Prag) loop
3496 Nam := Pragma_Name (Prag);
3498 if Nam = Name_Depends then
3500 else pragma Assert (Nam = Name_Global);
3504 Prag := Next_Pragma (Prag);
3507 -- Analyze Global first as Depends may mention items classified in
3508 -- the global categorization.
3510 if Present (Global) then
3511 Analyze_Global_In_Decl_Part (Global);
3514 -- Depends must be analyzed after Global in order to see the modes of
3515 -- all global items.
3517 if Present (Depends) then
3518 Analyze_Depends_In_Decl_Part (Depends);
3522 -- Emit an error when neither the postconditions nor the contract-cases
3523 -- mention attribute 'Result in the context of a function.
3525 if Warn_On_Suspicious_Contract
3526 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3528 if Present (Case_Prag)
3529 and then not Seen_In_Case
3530 and then Present (Post_Prag)
3531 and then not Seen_In_Post
3534 ("neither function postcondition nor contract cases mention "
3535 & "result?T?", Post_Prag);
3537 elsif Present (Case_Prag) and then not Seen_In_Case then
3539 ("contract cases do not mention result?T?", Case_Prag);
3541 elsif Present (Post_Prag) and then not Seen_In_Post then
3543 ("function postcondition does not mention result?T?", Post_Prag);
3546 end Analyze_Subprogram_Contract;
3548 ------------------------------------
3549 -- Analyze_Subprogram_Declaration --
3550 ------------------------------------
3552 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3553 Scop : constant Entity_Id := Current_Scope;
3554 Designator : Entity_Id;
3555 Is_Completion : Boolean;
3556 -- Indicates whether a null procedure declaration is a completion
3559 -- Null procedures are not allowed in SPARK
3561 if Nkind (Specification (N)) = N_Procedure_Specification
3562 and then Null_Present (Specification (N))
3564 Check_SPARK_Restriction ("null procedure is not allowed", N);
3566 if Is_Protected_Type (Current_Scope) then
3567 Error_Msg_N ("protected operation cannot be a null procedure", N);
3570 Analyze_Null_Procedure (N, Is_Completion);
3572 if Is_Completion then
3574 -- The null procedure acts as a body, nothing further is needed.
3580 Designator := Analyze_Subprogram_Specification (Specification (N));
3582 -- A reference may already have been generated for the unit name, in
3583 -- which case the following call is redundant. However it is needed for
3584 -- declarations that are the rewriting of an expression function.
3586 Generate_Definition (Designator);
3588 if Debug_Flag_C then
3589 Write_Str ("==> subprogram spec ");
3590 Write_Name (Chars (Designator));
3591 Write_Str (" from ");
3592 Write_Location (Sloc (N));
3597 Validate_RCI_Subprogram_Declaration (N);
3598 New_Overloaded_Entity (Designator);
3599 Check_Delayed_Subprogram (Designator);
3601 -- If the type of the first formal of the current subprogram is a
3602 -- non-generic tagged private type, mark the subprogram as being a
3603 -- private primitive. Ditto if this is a function with controlling
3604 -- result, and the return type is currently private. In both cases,
3605 -- the type of the controlling argument or result must be in the
3606 -- current scope for the operation to be primitive.
3608 if Has_Controlling_Result (Designator)
3609 and then Is_Private_Type (Etype (Designator))
3610 and then Scope (Etype (Designator)) = Current_Scope
3611 and then not Is_Generic_Actual_Type (Etype (Designator))
3613 Set_Is_Private_Primitive (Designator);
3615 elsif Present (First_Formal (Designator)) then
3617 Formal_Typ : constant Entity_Id :=
3618 Etype (First_Formal (Designator));
3620 Set_Is_Private_Primitive (Designator,
3621 Is_Tagged_Type (Formal_Typ)
3622 and then Scope (Formal_Typ) = Current_Scope
3623 and then Is_Private_Type (Formal_Typ)
3624 and then not Is_Generic_Actual_Type (Formal_Typ));
3628 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3631 if Ada_Version >= Ada_2005
3632 and then Comes_From_Source (N)
3633 and then Is_Dispatching_Operation (Designator)
3640 if Has_Controlling_Result (Designator) then
3641 Etyp := Etype (Designator);
3644 E := First_Entity (Designator);
3646 and then Is_Formal (E)
3647 and then not Is_Controlling_Formal (E)
3655 if Is_Access_Type (Etyp) then
3656 Etyp := Directly_Designated_Type (Etyp);
3659 if Is_Interface (Etyp)
3660 and then not Is_Abstract_Subprogram (Designator)
3661 and then not (Ekind (Designator) = E_Procedure
3662 and then Null_Present (Specification (N)))
3664 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3666 -- Specialize error message based on procedures vs. functions,
3667 -- since functions can't be null subprograms.
3669 if Ekind (Designator) = E_Procedure then
3671 ("interface procedure % must be abstract or null", N);
3673 Error_Msg_N ("interface function % must be abstract", N);
3679 -- What is the following code for, it used to be
3681 -- ??? Set_Suppress_Elaboration_Checks
3682 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3684 -- The following seems equivalent, but a bit dubious
3686 if Elaboration_Checks_Suppressed (Designator) then
3687 Set_Kill_Elaboration_Checks (Designator);
3690 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3691 Set_Categorization_From_Scope (Designator, Scop);
3694 -- For a compilation unit, check for library-unit pragmas
3696 Push_Scope (Designator);
3697 Set_Categorization_From_Pragmas (N);
3698 Validate_Categorization_Dependency (N, Designator);
3702 -- For a compilation unit, set body required. This flag will only be
3703 -- reset if a valid Import or Interface pragma is processed later on.
3705 if Nkind (Parent (N)) = N_Compilation_Unit then
3706 Set_Body_Required (Parent (N), True);
3708 if Ada_Version >= Ada_2005
3709 and then Nkind (Specification (N)) = N_Procedure_Specification
3710 and then Null_Present (Specification (N))
3713 ("null procedure cannot be declared at library level", N);
3717 Generate_Reference_To_Formals (Designator);
3718 Check_Eliminated (Designator);
3720 if Debug_Flag_C then
3722 Write_Str ("<== subprogram spec ");
3723 Write_Name (Chars (Designator));
3724 Write_Str (" from ");
3725 Write_Location (Sloc (N));
3729 if Is_Protected_Type (Current_Scope) then
3731 -- Indicate that this is a protected operation, because it may be
3732 -- used in subsequent declarations within the protected type.
3734 Set_Convention (Designator, Convention_Protected);
3737 List_Inherited_Pre_Post_Aspects (Designator);
3739 if Has_Aspects (N) then
3740 Analyze_Aspect_Specifications (N, Designator);
3742 end Analyze_Subprogram_Declaration;
3744 --------------------------------------
3745 -- Analyze_Subprogram_Specification --
3746 --------------------------------------
3748 -- Reminder: N here really is a subprogram specification (not a subprogram
3749 -- declaration). This procedure is called to analyze the specification in
3750 -- both subprogram bodies and subprogram declarations (specs).
3752 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3753 Designator : constant Entity_Id := Defining_Entity (N);
3754 Formals : constant List_Id := Parameter_Specifications (N);
3756 -- Start of processing for Analyze_Subprogram_Specification
3759 -- User-defined operator is not allowed in SPARK, except as a renaming
3761 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3762 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3764 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3767 -- Proceed with analysis. Do not emit a cross-reference entry if the
3768 -- specification comes from an expression function, because it may be
3769 -- the completion of a previous declaration. It is is not, the cross-
3770 -- reference entry will be emitted for the new subprogram declaration.
3772 if Nkind (Parent (N)) /= N_Expression_Function then
3773 Generate_Definition (Designator);
3776 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3778 if Nkind (N) = N_Function_Specification then
3779 Set_Ekind (Designator, E_Function);
3780 Set_Mechanism (Designator, Default_Mechanism);
3782 Set_Ekind (Designator, E_Procedure);
3783 Set_Etype (Designator, Standard_Void_Type);
3786 -- Introduce new scope for analysis of the formals and the return type
3788 Set_Scope (Designator, Current_Scope);
3790 if Present (Formals) then
3791 Push_Scope (Designator);
3792 Process_Formals (Formals, N);
3794 -- Check dimensions in N for formals with default expression
3796 Analyze_Dimension_Formals (N, Formals);
3798 -- Ada 2005 (AI-345): If this is an overriding operation of an
3799 -- inherited interface operation, and the controlling type is
3800 -- a synchronized type, replace the type with its corresponding
3801 -- record, to match the proper signature of an overriding operation.
3802 -- Same processing for an access parameter whose designated type is
3803 -- derived from a synchronized interface.
3805 if Ada_Version >= Ada_2005 then
3808 Formal_Typ : Entity_Id;
3809 Rec_Typ : Entity_Id;
3810 Desig_Typ : Entity_Id;
3813 Formal := First_Formal (Designator);
3814 while Present (Formal) loop
3815 Formal_Typ := Etype (Formal);
3817 if Is_Concurrent_Type (Formal_Typ)
3818 and then Present (Corresponding_Record_Type (Formal_Typ))
3820 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3822 if Present (Interfaces (Rec_Typ)) then
3823 Set_Etype (Formal, Rec_Typ);
3826 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3827 Desig_Typ := Designated_Type (Formal_Typ);
3829 if Is_Concurrent_Type (Desig_Typ)
3830 and then Present (Corresponding_Record_Type (Desig_Typ))
3832 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3834 if Present (Interfaces (Rec_Typ)) then
3835 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3840 Next_Formal (Formal);
3847 -- The subprogram scope is pushed and popped around the processing of
3848 -- the return type for consistency with call above to Process_Formals
3849 -- (which itself can call Analyze_Return_Type), and to ensure that any
3850 -- itype created for the return type will be associated with the proper
3853 elsif Nkind (N) = N_Function_Specification then
3854 Push_Scope (Designator);
3855 Analyze_Return_Type (N);
3861 if Nkind (N) = N_Function_Specification then
3863 -- Deal with operator symbol case
3865 if Nkind (Designator) = N_Defining_Operator_Symbol then
3866 Valid_Operator_Definition (Designator);
3869 May_Need_Actuals (Designator);
3871 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3872 -- the subprogram is abstract also. This does not apply to renaming
3873 -- declarations, where abstractness is inherited, and to subprogram
3874 -- bodies generated for stream operations, which become renamings as
3877 -- In case of primitives associated with abstract interface types
3878 -- the check is applied later (see Analyze_Subprogram_Declaration).
3880 if not Nkind_In (Original_Node (Parent (N)),
3881 N_Subprogram_Renaming_Declaration,
3882 N_Abstract_Subprogram_Declaration,
3883 N_Formal_Abstract_Subprogram_Declaration)
3885 if Is_Abstract_Type (Etype (Designator))
3886 and then not Is_Interface (Etype (Designator))
3889 ("function that returns abstract type must be abstract", N);
3891 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3892 -- access result whose designated type is abstract.
3894 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3896 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3897 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3898 and then Ada_Version >= Ada_2012
3900 Error_Msg_N ("function whose access result designates "
3901 & "abstract type must be abstract", N);
3907 end Analyze_Subprogram_Specification;
3909 --------------------------
3910 -- Build_Body_To_Inline --
3911 --------------------------
3913 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3914 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3915 Original_Body : Node_Id;
3916 Body_To_Analyze : Node_Id;
3917 Max_Size : constant := 10;
3918 Stat_Count : Integer := 0;
3920 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3921 -- Check for declarations that make inlining not worthwhile
3923 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3924 -- Check for statements that make inlining not worthwhile: any tasking
3925 -- statement, nested at any level. Keep track of total number of
3926 -- elementary statements, as a measure of acceptable size.
3928 function Has_Pending_Instantiation return Boolean;
3929 -- If some enclosing body contains instantiations that appear before the
3930 -- corresponding generic body, the enclosing body has a freeze node so
3931 -- that it can be elaborated after the generic itself. This might
3932 -- conflict with subsequent inlinings, so that it is unsafe to try to
3933 -- inline in such a case.
3935 function Has_Single_Return return Boolean;
3936 -- In general we cannot inline functions that return unconstrained type.
3937 -- However, we can handle such functions if all return statements return
3938 -- a local variable that is the only declaration in the body of the
3939 -- function. In that case the call can be replaced by that local
3940 -- variable as is done for other inlined calls.
3942 procedure Remove_Pragmas;
3943 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3944 -- parameter has no meaning when the body is inlined and the formals
3945 -- are rewritten. Remove it from body to inline. The analysis of the
3946 -- non-inlined body will handle the pragma properly.
3948 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3949 -- If the body of the subprogram includes a call that returns an
3950 -- unconstrained type, the secondary stack is involved, and it
3951 -- is not worth inlining.
3953 ------------------------------
3954 -- Has_Excluded_Declaration --
3955 ------------------------------
3957 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3960 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3961 -- Nested subprograms make a given body ineligible for inlining, but
3962 -- we make an exception for instantiations of unchecked conversion.
3963 -- The body has not been analyzed yet, so check the name, and verify
3964 -- that the visible entity with that name is the predefined unit.
3966 -----------------------------
3967 -- Is_Unchecked_Conversion --
3968 -----------------------------
3970 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3971 Id : constant Node_Id := Name (D);
3975 if Nkind (Id) = N_Identifier
3976 and then Chars (Id) = Name_Unchecked_Conversion
3978 Conv := Current_Entity (Id);
3980 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3981 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3983 Conv := Current_Entity (Selector_Name (Id));
3988 return Present (Conv)
3989 and then Is_Predefined_File_Name
3990 (Unit_File_Name (Get_Source_Unit (Conv)))
3991 and then Is_Intrinsic_Subprogram (Conv);
3992 end Is_Unchecked_Conversion;
3994 -- Start of processing for Has_Excluded_Declaration
3998 while Present (D) loop
3999 if (Nkind (D) = N_Function_Instantiation
4000 and then not Is_Unchecked_Conversion (D))
4001 or else Nkind_In (D, N_Protected_Type_Declaration,
4002 N_Package_Declaration,
4003 N_Package_Instantiation,
4005 N_Procedure_Instantiation,
4006 N_Task_Type_Declaration)
4009 ("cannot inline & (non-allowed declaration)?", D, Subp);
4017 end Has_Excluded_Declaration;
4019 ----------------------------
4020 -- Has_Excluded_Statement --
4021 ----------------------------
4023 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4029 while Present (S) loop
4030 Stat_Count := Stat_Count + 1;
4032 if Nkind_In (S, N_Abort_Statement,
4033 N_Asynchronous_Select,
4034 N_Conditional_Entry_Call,
4035 N_Delay_Relative_Statement,
4036 N_Delay_Until_Statement,
4041 ("cannot inline & (non-allowed statement)?", S, Subp);
4044 elsif Nkind (S) = N_Block_Statement then
4045 if Present (Declarations (S))
4046 and then Has_Excluded_Declaration (Declarations (S))
4050 elsif Present (Handled_Statement_Sequence (S))
4053 (Exception_Handlers (Handled_Statement_Sequence (S)))
4055 Has_Excluded_Statement
4056 (Statements (Handled_Statement_Sequence (S))))
4061 elsif Nkind (S) = N_Case_Statement then
4062 E := First (Alternatives (S));
4063 while Present (E) loop
4064 if Has_Excluded_Statement (Statements (E)) then
4071 elsif Nkind (S) = N_If_Statement then
4072 if Has_Excluded_Statement (Then_Statements (S)) then
4076 if Present (Elsif_Parts (S)) then
4077 E := First (Elsif_Parts (S));
4078 while Present (E) loop
4079 if Has_Excluded_Statement (Then_Statements (E)) then
4087 if Present (Else_Statements (S))
4088 and then Has_Excluded_Statement (Else_Statements (S))
4093 elsif Nkind (S) = N_Loop_Statement
4094 and then Has_Excluded_Statement (Statements (S))
4098 elsif Nkind (S) = N_Extended_Return_Statement then
4099 if Has_Excluded_Statement
4100 (Statements (Handled_Statement_Sequence (S)))
4102 (Exception_Handlers (Handled_Statement_Sequence (S)))
4112 end Has_Excluded_Statement;
4114 -------------------------------
4115 -- Has_Pending_Instantiation --
4116 -------------------------------
4118 function Has_Pending_Instantiation return Boolean is
4123 while Present (S) loop
4124 if Is_Compilation_Unit (S)
4125 or else Is_Child_Unit (S)
4129 elsif Ekind (S) = E_Package
4130 and then Has_Forward_Instantiation (S)
4139 end Has_Pending_Instantiation;
4141 ------------------------
4142 -- Has_Single_Return --
4143 ------------------------
4145 function Has_Single_Return return Boolean is
4146 Return_Statement : Node_Id := Empty;
4148 function Check_Return (N : Node_Id) return Traverse_Result;
4154 function Check_Return (N : Node_Id) return Traverse_Result is
4156 if Nkind (N) = N_Simple_Return_Statement then
4157 if Present (Expression (N))
4158 and then Is_Entity_Name (Expression (N))
4160 if No (Return_Statement) then
4161 Return_Statement := N;
4164 elsif Chars (Expression (N)) =
4165 Chars (Expression (Return_Statement))
4173 -- A return statement within an extended return is a noop
4176 elsif No (Expression (N))
4177 and then Nkind (Parent (Parent (N))) =
4178 N_Extended_Return_Statement
4183 -- Expression has wrong form
4188 -- We can only inline a build-in-place function if
4189 -- it has a single extended return.
4191 elsif Nkind (N) = N_Extended_Return_Statement then
4192 if No (Return_Statement) then
4193 Return_Statement := N;
4205 function Check_All_Returns is new Traverse_Func (Check_Return);
4207 -- Start of processing for Has_Single_Return
4210 if Check_All_Returns (N) /= OK then
4213 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4217 return Present (Declarations (N))
4218 and then Present (First (Declarations (N)))
4219 and then Chars (Expression (Return_Statement)) =
4220 Chars (Defining_Identifier (First (Declarations (N))));
4222 end Has_Single_Return;
4224 --------------------
4225 -- Remove_Pragmas --
4226 --------------------
4228 procedure Remove_Pragmas is
4233 Decl := First (Declarations (Body_To_Analyze));
4234 while Present (Decl) loop
4237 if Nkind (Decl) = N_Pragma
4238 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4248 --------------------------
4249 -- Uses_Secondary_Stack --
4250 --------------------------
4252 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4253 function Check_Call (N : Node_Id) return Traverse_Result;
4254 -- Look for function calls that return an unconstrained type
4260 function Check_Call (N : Node_Id) return Traverse_Result is
4262 if Nkind (N) = N_Function_Call
4263 and then Is_Entity_Name (Name (N))
4264 and then Is_Composite_Type (Etype (Entity (Name (N))))
4265 and then not Is_Constrained (Etype (Entity (Name (N))))
4268 ("cannot inline & (call returns unconstrained type)?",
4276 function Check_Calls is new Traverse_Func (Check_Call);
4279 return Check_Calls (Bod) = Abandon;
4280 end Uses_Secondary_Stack;
4282 -- Start of processing for Build_Body_To_Inline
4285 -- Return immediately if done already
4287 if Nkind (Decl) = N_Subprogram_Declaration
4288 and then Present (Body_To_Inline (Decl))
4292 -- Functions that return unconstrained composite types require
4293 -- secondary stack handling, and cannot currently be inlined, unless
4294 -- all return statements return a local variable that is the first
4295 -- local declaration in the body.
4297 elsif Ekind (Subp) = E_Function
4298 and then not Is_Scalar_Type (Etype (Subp))
4299 and then not Is_Access_Type (Etype (Subp))
4300 and then not Is_Constrained (Etype (Subp))
4302 if not Has_Single_Return then
4304 ("cannot inline & (unconstrained return type)?", N, Subp);
4308 -- Ditto for functions that return controlled types, where controlled
4309 -- actions interfere in complex ways with inlining.
4311 elsif Ekind (Subp) = E_Function
4312 and then Needs_Finalization (Etype (Subp))
4315 ("cannot inline & (controlled return type)?", N, Subp);
4319 if Present (Declarations (N))
4320 and then Has_Excluded_Declaration (Declarations (N))
4325 if Present (Handled_Statement_Sequence (N)) then
4326 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4328 ("cannot inline& (exception handler)?",
4329 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4333 Has_Excluded_Statement
4334 (Statements (Handled_Statement_Sequence (N)))
4340 -- We do not inline a subprogram that is too large, unless it is
4341 -- marked Inline_Always. This pragma does not suppress the other
4342 -- checks on inlining (forbidden declarations, handlers, etc).
4344 if Stat_Count > Max_Size
4345 and then not Has_Pragma_Inline_Always (Subp)
4347 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4351 if Has_Pending_Instantiation then
4353 ("cannot inline& (forward instance within enclosing body)?",
4358 -- Within an instance, the body to inline must be treated as a nested
4359 -- generic, so that the proper global references are preserved.
4361 -- Note that we do not do this at the library level, because it is not
4362 -- needed, and furthermore this causes trouble if front end inlining
4363 -- is activated (-gnatN).
4365 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4366 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4367 Original_Body := Copy_Generic_Node (N, Empty, True);
4369 Original_Body := Copy_Separate_Tree (N);
4372 -- We need to capture references to the formals in order to substitute
4373 -- the actuals at the point of inlining, i.e. instantiation. To treat
4374 -- the formals as globals to the body to inline, we nest it within
4375 -- a dummy parameterless subprogram, declared within the real one.
4376 -- To avoid generating an internal name (which is never public, and
4377 -- which affects serial numbers of other generated names), we use
4378 -- an internal symbol that cannot conflict with user declarations.
4380 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4381 Set_Defining_Unit_Name
4382 (Specification (Original_Body),
4383 Make_Defining_Identifier (Sloc (N), Name_uParent));
4384 Set_Corresponding_Spec (Original_Body, Empty);
4386 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4388 -- Set return type of function, which is also global and does not need
4391 if Ekind (Subp) = E_Function then
4392 Set_Result_Definition (Specification (Body_To_Analyze),
4393 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4396 if No (Declarations (N)) then
4397 Set_Declarations (N, New_List (Body_To_Analyze));
4399 Append (Body_To_Analyze, Declarations (N));
4402 Expander_Mode_Save_And_Set (False);
4405 Analyze (Body_To_Analyze);
4406 Push_Scope (Defining_Entity (Body_To_Analyze));
4407 Save_Global_References (Original_Body);
4409 Remove (Body_To_Analyze);
4411 Expander_Mode_Restore;
4413 -- Restore environment if previously saved
4415 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4419 -- If secondary stk used there is no point in inlining. We have
4420 -- already issued the warning in this case, so nothing to do.
4422 if Uses_Secondary_Stack (Body_To_Analyze) then
4426 Set_Body_To_Inline (Decl, Original_Body);
4427 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4428 Set_Is_Inlined (Subp);
4429 end Build_Body_To_Inline;
4435 procedure Cannot_Inline
4439 Is_Serious : Boolean := False)
4442 pragma Assert (Msg (Msg'Last) = '?');
4446 if not Debug_Flag_Dot_K then
4448 -- Do not emit warning if this is a predefined unit which is not
4449 -- the main unit. With validity checks enabled, some predefined
4450 -- subprograms may contain nested subprograms and become ineligible
4453 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4454 and then not In_Extended_Main_Source_Unit (Subp)
4458 elsif Has_Pragma_Inline_Always (Subp) then
4460 -- Remove last character (question mark) to make this into an
4461 -- error, because the Inline_Always pragma cannot be obeyed.
4463 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4465 elsif Ineffective_Inline_Warnings then
4466 Error_Msg_NE (Msg & "p?", N, Subp);
4473 elsif Is_Serious then
4475 -- Remove last character (question mark) to make this into an error.
4477 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4479 elsif Optimization_Level = 0 then
4481 -- Do not emit warning if this is a predefined unit which is not
4482 -- the main unit. This behavior is currently provided for backward
4483 -- compatibility but it will be removed when we enforce the
4484 -- strictness of the new rules.
4486 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4487 and then not In_Extended_Main_Source_Unit (Subp)
4491 elsif Has_Pragma_Inline_Always (Subp) then
4493 -- Emit a warning if this is a call to a runtime subprogram
4494 -- which is located inside a generic. Previously this call
4495 -- was silently skipped!
4497 if Is_Generic_Instance (Subp) then
4499 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4501 if Is_Predefined_File_Name
4502 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4504 Set_Is_Inlined (Subp, False);
4505 Error_Msg_NE (Msg & "p?", N, Subp);
4511 -- Remove last character (question mark) to make this into an
4512 -- error, because the Inline_Always pragma cannot be obeyed.
4514 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4516 else pragma Assert (Front_End_Inlining);
4517 Set_Is_Inlined (Subp, False);
4519 -- When inlining cannot take place we must issue an error.
4520 -- For backward compatibility we still report a warning.
4522 if Ineffective_Inline_Warnings then
4523 Error_Msg_NE (Msg & "p?", N, Subp);
4527 -- Compiling with optimizations enabled it is too early to report
4528 -- problems since the backend may still perform inlining. In order
4529 -- to report unhandled inlinings the program must be compiled with
4530 -- -Winline and the error is reported by the backend.
4537 ------------------------------------
4538 -- Check_And_Build_Body_To_Inline --
4539 ------------------------------------
4541 procedure Check_And_Build_Body_To_Inline
4543 Spec_Id : Entity_Id;
4544 Body_Id : Entity_Id)
4546 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4547 -- Use generic machinery to build an unexpanded body for the subprogram.
4548 -- This body is subsequently used for inline expansions at call sites.
4550 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4551 -- Return true if we generate code for the function body N, the function
4552 -- body N has no local declarations and its unique statement is a single
4553 -- extended return statement with a handled statements sequence.
4555 function Check_Body_To_Inline
4557 Subp : Entity_Id) return Boolean;
4558 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4559 -- inlined by the frontend. These are the rules:
4560 -- * At -O0 use fe inlining when inline_always is specified except if
4561 -- the function returns a controlled type.
4562 -- * At other optimization levels use the fe inlining for both inline
4563 -- and inline_always in the following cases:
4564 -- - function returning a known at compile time constant
4565 -- - function returning a call to an intrinsic function
4566 -- - function returning an unconstrained type (see Can_Split
4567 -- Unconstrained_Function).
4568 -- - function returning a call to a frontend-inlined function
4569 -- Use the back-end mechanism otherwise
4571 -- In addition, in the following cases the function cannot be inlined by
4573 -- - functions that uses the secondary stack
4574 -- - functions that have declarations of:
4575 -- - Concurrent types
4579 -- - functions that have some of the following statements:
4581 -- - asynchronous-select
4582 -- - conditional-entry-call
4585 -- - selective-accept
4586 -- - timed-entry-call
4587 -- - functions that have exception handlers
4588 -- - functions that have some enclosing body containing instantiations
4589 -- that appear before the corresponding generic body.
4591 procedure Generate_Body_To_Inline
4593 Body_To_Inline : out Node_Id);
4594 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4595 -- of pragmas referencing the formals are removed since they have no
4596 -- meaning when the body is inlined and the formals are rewritten (the
4597 -- analysis of the non-inlined body will handle these pragmas properly).
4598 -- A new internal name is associated with Body_To_Inline.
4600 procedure Split_Unconstrained_Function
4602 Spec_Id : Entity_Id);
4603 -- N is an inlined function body that returns an unconstrained type and
4604 -- has a single extended return statement. Split N in two subprograms:
4605 -- a procedure P' and a function F'. The formals of P' duplicate the
4606 -- formals of N plus an extra formal which is used return a value;
4607 -- its body is composed by the declarations and list of statements
4608 -- of the extended return statement of N.
4610 --------------------------
4611 -- Build_Body_To_Inline --
4612 --------------------------
4614 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4615 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4616 Original_Body : Node_Id;
4617 Body_To_Analyze : Node_Id;
4620 pragma Assert (Current_Scope = Spec_Id);
4622 -- Within an instance, the body to inline must be treated as a nested
4623 -- generic, so that the proper global references are preserved. We
4624 -- do not do this at the library level, because it is not needed, and
4625 -- furthermore this causes trouble if front end inlining is activated
4629 and then Scope (Current_Scope) /= Standard_Standard
4631 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4634 -- We need to capture references to the formals in order
4635 -- to substitute the actuals at the point of inlining, i.e.
4636 -- instantiation. To treat the formals as globals to the body to
4637 -- inline, we nest it within a dummy parameterless subprogram,
4638 -- declared within the real one.
4640 Generate_Body_To_Inline (N, Original_Body);
4641 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4643 -- Set return type of function, which is also global and does not
4644 -- need to be resolved.
4646 if Ekind (Spec_Id) = E_Function then
4647 Set_Result_Definition (Specification (Body_To_Analyze),
4648 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4651 if No (Declarations (N)) then
4652 Set_Declarations (N, New_List (Body_To_Analyze));
4654 Append_To (Declarations (N), Body_To_Analyze);
4657 Preanalyze (Body_To_Analyze);
4659 Push_Scope (Defining_Entity (Body_To_Analyze));
4660 Save_Global_References (Original_Body);
4662 Remove (Body_To_Analyze);
4664 -- Restore environment if previously saved
4667 and then Scope (Current_Scope) /= Standard_Standard
4672 pragma Assert (No (Body_To_Inline (Decl)));
4673 Set_Body_To_Inline (Decl, Original_Body);
4674 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4675 end Build_Body_To_Inline;
4677 --------------------------
4678 -- Check_Body_To_Inline --
4679 --------------------------
4681 function Check_Body_To_Inline
4683 Subp : Entity_Id) return Boolean
4685 Max_Size : constant := 10;
4686 Stat_Count : Integer := 0;
4688 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4689 -- Check for declarations that make inlining not worthwhile
4691 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4692 -- Check for statements that make inlining not worthwhile: any
4693 -- tasking statement, nested at any level. Keep track of total
4694 -- number of elementary statements, as a measure of acceptable size.
4696 function Has_Pending_Instantiation return Boolean;
4697 -- Return True if some enclosing body contains instantiations that
4698 -- appear before the corresponding generic body.
4700 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4701 -- Return True if all the return statements of the function body N
4702 -- are simple return statements and return a compile time constant
4704 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4705 -- Return True if all the return statements of the function body N
4706 -- are simple return statements and return an intrinsic function call
4708 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4709 -- If the body of the subprogram includes a call that returns an
4710 -- unconstrained type, the secondary stack is involved, and it
4711 -- is not worth inlining.
4713 ------------------------------
4714 -- Has_Excluded_Declaration --
4715 ------------------------------
4717 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4720 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4721 -- Nested subprograms make a given body ineligible for inlining,
4722 -- but we make an exception for instantiations of unchecked
4723 -- conversion. The body has not been analyzed yet, so check the
4724 -- name, and verify that the visible entity with that name is the
4727 -----------------------------
4728 -- Is_Unchecked_Conversion --
4729 -----------------------------
4731 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4732 Id : constant Node_Id := Name (D);
4736 if Nkind (Id) = N_Identifier
4737 and then Chars (Id) = Name_Unchecked_Conversion
4739 Conv := Current_Entity (Id);
4741 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4743 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4745 Conv := Current_Entity (Selector_Name (Id));
4750 return Present (Conv)
4751 and then Is_Predefined_File_Name
4752 (Unit_File_Name (Get_Source_Unit (Conv)))
4753 and then Is_Intrinsic_Subprogram (Conv);
4754 end Is_Unchecked_Conversion;
4756 -- Start of processing for Has_Excluded_Declaration
4760 while Present (D) loop
4761 if (Nkind (D) = N_Function_Instantiation
4762 and then not Is_Unchecked_Conversion (D))
4763 or else Nkind_In (D, N_Protected_Type_Declaration,
4764 N_Package_Declaration,
4765 N_Package_Instantiation,
4767 N_Procedure_Instantiation,
4768 N_Task_Type_Declaration)
4771 ("cannot inline & (non-allowed declaration)?", D, Subp);
4780 end Has_Excluded_Declaration;
4782 ----------------------------
4783 -- Has_Excluded_Statement --
4784 ----------------------------
4786 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4792 while Present (S) loop
4793 Stat_Count := Stat_Count + 1;
4795 if Nkind_In (S, N_Abort_Statement,
4796 N_Asynchronous_Select,
4797 N_Conditional_Entry_Call,
4798 N_Delay_Relative_Statement,
4799 N_Delay_Until_Statement,
4804 ("cannot inline & (non-allowed statement)?", S, Subp);
4807 elsif Nkind (S) = N_Block_Statement then
4808 if Present (Declarations (S))
4809 and then Has_Excluded_Declaration (Declarations (S))
4813 elsif Present (Handled_Statement_Sequence (S)) then
4815 (Exception_Handlers (Handled_Statement_Sequence (S)))
4818 ("cannot inline& (exception handler)?",
4819 First (Exception_Handlers
4820 (Handled_Statement_Sequence (S))),
4824 elsif Has_Excluded_Statement
4825 (Statements (Handled_Statement_Sequence (S)))
4831 elsif Nkind (S) = N_Case_Statement then
4832 E := First (Alternatives (S));
4833 while Present (E) loop
4834 if Has_Excluded_Statement (Statements (E)) then
4841 elsif Nkind (S) = N_If_Statement then
4842 if Has_Excluded_Statement (Then_Statements (S)) then
4846 if Present (Elsif_Parts (S)) then
4847 E := First (Elsif_Parts (S));
4848 while Present (E) loop
4849 if Has_Excluded_Statement (Then_Statements (E)) then
4856 if Present (Else_Statements (S))
4857 and then Has_Excluded_Statement (Else_Statements (S))
4862 elsif Nkind (S) = N_Loop_Statement
4863 and then Has_Excluded_Statement (Statements (S))
4867 elsif Nkind (S) = N_Extended_Return_Statement then
4868 if Present (Handled_Statement_Sequence (S))
4870 Has_Excluded_Statement
4871 (Statements (Handled_Statement_Sequence (S)))
4875 elsif Present (Handled_Statement_Sequence (S))
4877 Present (Exception_Handlers
4878 (Handled_Statement_Sequence (S)))
4881 ("cannot inline& (exception handler)?",
4882 First (Exception_Handlers
4883 (Handled_Statement_Sequence (S))),
4893 end Has_Excluded_Statement;
4895 -------------------------------
4896 -- Has_Pending_Instantiation --
4897 -------------------------------
4899 function Has_Pending_Instantiation return Boolean is
4904 while Present (S) loop
4905 if Is_Compilation_Unit (S)
4906 or else Is_Child_Unit (S)
4910 elsif Ekind (S) = E_Package
4911 and then Has_Forward_Instantiation (S)
4920 end Has_Pending_Instantiation;
4922 ------------------------------------
4923 -- Returns_Compile_Time_Constant --
4924 ------------------------------------
4926 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4928 function Check_Return (N : Node_Id) return Traverse_Result;
4934 function Check_Return (N : Node_Id) return Traverse_Result is
4936 if Nkind (N) = N_Extended_Return_Statement then
4939 elsif Nkind (N) = N_Simple_Return_Statement then
4940 if Present (Expression (N)) then
4942 Orig_Expr : constant Node_Id :=
4943 Original_Node (Expression (N));
4946 if Nkind_In (Orig_Expr, N_Integer_Literal,
4948 N_Character_Literal)
4952 elsif Is_Entity_Name (Orig_Expr)
4953 and then Ekind (Entity (Orig_Expr)) = E_Constant
4954 and then Is_Static_Expression (Orig_Expr)
4962 -- Expression has wrong form
4968 -- Continue analyzing statements
4975 function Check_All_Returns is new Traverse_Func (Check_Return);
4977 -- Start of processing for Returns_Compile_Time_Constant
4980 return Check_All_Returns (N) = OK;
4981 end Returns_Compile_Time_Constant;
4983 --------------------------------------
4984 -- Returns_Intrinsic_Function_Call --
4985 --------------------------------------
4987 function Returns_Intrinsic_Function_Call
4988 (N : Node_Id) return Boolean
4990 function Check_Return (N : Node_Id) return Traverse_Result;
4996 function Check_Return (N : Node_Id) return Traverse_Result is
4998 if Nkind (N) = N_Extended_Return_Statement then
5001 elsif Nkind (N) = N_Simple_Return_Statement then
5002 if Present (Expression (N)) then
5004 Orig_Expr : constant Node_Id :=
5005 Original_Node (Expression (N));
5008 if Nkind (Orig_Expr) in N_Op
5009 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5013 elsif Nkind (Orig_Expr) in N_Has_Entity
5014 and then Present (Entity (Orig_Expr))
5015 and then Ekind (Entity (Orig_Expr)) = E_Function
5016 and then Is_Inlined (Entity (Orig_Expr))
5020 elsif Nkind (Orig_Expr) in N_Has_Entity
5021 and then Present (Entity (Orig_Expr))
5022 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5031 -- Expression has wrong form
5037 -- Continue analyzing statements
5044 function Check_All_Returns is new Traverse_Func (Check_Return);
5046 -- Start of processing for Returns_Intrinsic_Function_Call
5049 return Check_All_Returns (N) = OK;
5050 end Returns_Intrinsic_Function_Call;
5052 --------------------------
5053 -- Uses_Secondary_Stack --
5054 --------------------------
5056 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5058 function Check_Call (N : Node_Id) return Traverse_Result;
5059 -- Look for function calls that return an unconstrained type
5065 function Check_Call (N : Node_Id) return Traverse_Result is
5067 if Nkind (N) = N_Function_Call
5068 and then Is_Entity_Name (Name (N))
5069 and then Is_Composite_Type (Etype (Entity (Name (N))))
5070 and then not Is_Constrained (Etype (Entity (Name (N))))
5073 ("cannot inline & (call returns unconstrained type)?",
5082 function Check_Calls is new Traverse_Func (Check_Call);
5084 -- Start of processing for Uses_Secondary_Stack
5087 return Check_Calls (N) = Abandon;
5088 end Uses_Secondary_Stack;
5092 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5093 May_Inline : constant Boolean :=
5094 Has_Pragma_Inline_Always (Spec_Id)
5095 or else (Has_Pragma_Inline (Spec_Id)
5096 and then ((Optimization_Level > 0
5097 and then Ekind (Spec_Id)
5099 or else Front_End_Inlining));
5100 Body_To_Analyze : Node_Id;
5102 -- Start of processing for Check_Body_To_Inline
5105 -- No action needed in stubs since the attribute Body_To_Inline
5108 if Nkind (Decl) = N_Subprogram_Body_Stub then
5111 -- Cannot build the body to inline if the attribute is already set.
5112 -- This attribute may have been set if this is a subprogram renaming
5113 -- declarations (see Freeze.Build_Renamed_Body).
5115 elsif Present (Body_To_Inline (Decl)) then
5118 -- No action needed if the subprogram does not fulfill the minimum
5119 -- conditions to be inlined by the frontend
5121 elsif not May_Inline then
5125 -- Check excluded declarations
5127 if Present (Declarations (N))
5128 and then Has_Excluded_Declaration (Declarations (N))
5133 -- Check excluded statements
5135 if Present (Handled_Statement_Sequence (N)) then
5137 (Exception_Handlers (Handled_Statement_Sequence (N)))
5140 ("cannot inline& (exception handler)?",
5142 (Exception_Handlers (Handled_Statement_Sequence (N))),
5147 elsif Has_Excluded_Statement
5148 (Statements (Handled_Statement_Sequence (N)))
5154 -- For backward compatibility, compiling under -gnatN we do not
5155 -- inline a subprogram that is too large, unless it is marked
5156 -- Inline_Always. This pragma does not suppress the other checks
5157 -- on inlining (forbidden declarations, handlers, etc).
5159 if Front_End_Inlining
5160 and then not Has_Pragma_Inline_Always (Subp)
5161 and then Stat_Count > Max_Size
5163 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5167 -- If some enclosing body contains instantiations that appear before
5168 -- the corresponding generic body, the enclosing body has a freeze
5169 -- node so that it can be elaborated after the generic itself. This
5170 -- might conflict with subsequent inlinings, so that it is unsafe to
5171 -- try to inline in such a case.
5173 if Has_Pending_Instantiation then
5175 ("cannot inline& (forward instance within enclosing body)?",
5181 -- Generate and preanalyze the body to inline (needed to perform
5182 -- the rest of the checks)
5184 Generate_Body_To_Inline (N, Body_To_Analyze);
5186 if Ekind (Subp) = E_Function then
5187 Set_Result_Definition (Specification (Body_To_Analyze),
5188 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5191 -- Nest the body to analyze within the real one
5193 if No (Declarations (N)) then
5194 Set_Declarations (N, New_List (Body_To_Analyze));
5196 Append_To (Declarations (N), Body_To_Analyze);
5199 Preanalyze (Body_To_Analyze);
5200 Remove (Body_To_Analyze);
5202 -- Keep separate checks needed when compiling without optimizations
5204 if Optimization_Level = 0
5206 -- AAMP and VM targets have no support for inlining in the backend
5207 -- and hence we use frontend inlining at all optimization levels.
5209 or else AAMP_On_Target
5210 or else VM_Target /= No_VM
5212 -- Cannot inline functions whose body has a call that returns an
5213 -- unconstrained type since the secondary stack is involved, and
5214 -- it is not worth inlining.
5216 if Uses_Secondary_Stack (Body_To_Analyze) then
5219 -- Cannot inline functions that return controlled types since
5220 -- controlled actions interfere in complex ways with inlining.
5222 elsif Ekind (Subp) = E_Function
5223 and then Needs_Finalization (Etype (Subp))
5226 ("cannot inline & (controlled return type)?", N, Subp);
5229 elsif Returns_Unconstrained_Type (Subp) then
5231 ("cannot inline & (unconstrained return type)?", N, Subp);
5235 -- Compiling with optimizations enabled
5238 -- Procedures are never frontend inlined in this case!
5240 if Ekind (Subp) /= E_Function then
5243 -- Functions returning unconstrained types are tested
5244 -- separately (see Can_Split_Unconstrained_Function).
5246 elsif Returns_Unconstrained_Type (Subp) then
5249 -- Check supported cases
5251 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5252 and then Convention (Subp) /= Convention_Intrinsic
5253 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5260 end Check_Body_To_Inline;
5262 --------------------------------------
5263 -- Can_Split_Unconstrained_Function --
5264 --------------------------------------
5266 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5268 Ret_Node : constant Node_Id :=
5269 First (Statements (Handled_Statement_Sequence (N)));
5273 -- No user defined declarations allowed in the function except inside
5274 -- the unique return statement; implicit labels are the only allowed
5277 if not Is_Empty_List (Declarations (N)) then
5278 D := First (Declarations (N));
5279 while Present (D) loop
5280 if Nkind (D) /= N_Implicit_Label_Declaration then
5288 -- We only split the inlined function when we are generating the code
5289 -- of its body; otherwise we leave duplicated split subprograms in
5290 -- the tree which (if referenced) generate wrong references at link
5293 return In_Extended_Main_Code_Unit (N)
5294 and then Present (Ret_Node)
5295 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5296 and then No (Next (Ret_Node))
5297 and then Present (Handled_Statement_Sequence (Ret_Node));
5298 end Can_Split_Unconstrained_Function;
5300 -----------------------------
5301 -- Generate_Body_To_Inline --
5302 -----------------------------
5304 procedure Generate_Body_To_Inline
5306 Body_To_Inline : out Node_Id)
5308 procedure Remove_Pragmas (N : Node_Id);
5309 -- Remove occurrences of pragmas that may reference the formals of
5310 -- N. The analysis of the non-inlined body will handle these pragmas
5313 --------------------
5314 -- Remove_Pragmas --
5315 --------------------
5317 procedure Remove_Pragmas (N : Node_Id) is
5322 Decl := First (Declarations (N));
5323 while Present (Decl) loop
5326 if Nkind (Decl) = N_Pragma
5327 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5337 -- Start of processing for Generate_Body_To_Inline
5340 -- Within an instance, the body to inline must be treated as a nested
5341 -- generic, so that the proper global references are preserved.
5343 -- Note that we do not do this at the library level, because it
5344 -- is not needed, and furthermore this causes trouble if front
5345 -- end inlining is activated (-gnatN).
5348 and then Scope (Current_Scope) /= Standard_Standard
5350 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5352 Body_To_Inline := Copy_Separate_Tree (N);
5355 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5356 -- parameter has no meaning when the body is inlined and the formals
5357 -- are rewritten. Remove it from body to inline. The analysis of the
5358 -- non-inlined body will handle the pragma properly.
5360 Remove_Pragmas (Body_To_Inline);
5362 -- We need to capture references to the formals in order
5363 -- to substitute the actuals at the point of inlining, i.e.
5364 -- instantiation. To treat the formals as globals to the body to
5365 -- inline, we nest it within a dummy parameterless subprogram,
5366 -- declared within the real one.
5368 Set_Parameter_Specifications
5369 (Specification (Body_To_Inline), No_List);
5371 -- A new internal name is associated with Body_To_Inline to avoid
5372 -- conflicts when the non-inlined body N is analyzed.
5374 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5375 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5376 Set_Corresponding_Spec (Body_To_Inline, Empty);
5377 end Generate_Body_To_Inline;
5379 ----------------------------------
5380 -- Split_Unconstrained_Function --
5381 ----------------------------------
5383 procedure Split_Unconstrained_Function
5385 Spec_Id : Entity_Id)
5387 Loc : constant Source_Ptr := Sloc (N);
5388 Ret_Node : constant Node_Id :=
5389 First (Statements (Handled_Statement_Sequence (N)));
5390 Ret_Obj : constant Node_Id :=
5391 First (Return_Object_Declarations (Ret_Node));
5393 procedure Build_Procedure
5394 (Proc_Id : out Entity_Id;
5395 Decl_List : out List_Id);
5396 -- Build a procedure containing the statements found in the extended
5397 -- return statement of the unconstrained function body N.
5399 procedure Build_Procedure
5400 (Proc_Id : out Entity_Id;
5401 Decl_List : out List_Id)
5404 Formal_List : constant List_Id := New_List;
5405 Proc_Spec : Node_Id;
5406 Proc_Body : Node_Id;
5407 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5408 Body_Decl_List : List_Id := No_List;
5409 Param_Type : Node_Id;
5412 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5413 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5416 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5419 Append_To (Formal_List,
5420 Make_Parameter_Specification (Loc,
5421 Defining_Identifier =>
5422 Make_Defining_Identifier (Loc,
5423 Chars => Chars (Defining_Identifier (Ret_Obj))),
5424 In_Present => False,
5425 Out_Present => True,
5426 Null_Exclusion_Present => False,
5427 Parameter_Type => Param_Type));
5429 Formal := First_Formal (Spec_Id);
5430 while Present (Formal) loop
5431 Append_To (Formal_List,
5432 Make_Parameter_Specification (Loc,
5433 Defining_Identifier =>
5434 Make_Defining_Identifier (Sloc (Formal),
5435 Chars => Chars (Formal)),
5436 In_Present => In_Present (Parent (Formal)),
5437 Out_Present => Out_Present (Parent (Formal)),
5438 Null_Exclusion_Present =>
5439 Null_Exclusion_Present (Parent (Formal)),
5441 New_Reference_To (Etype (Formal), Loc),
5443 Copy_Separate_Tree (Expression (Parent (Formal)))));
5445 Next_Formal (Formal);
5449 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5452 Make_Procedure_Specification (Loc,
5453 Defining_Unit_Name => Proc_Id,
5454 Parameter_Specifications => Formal_List);
5456 Decl_List := New_List;
5458 Append_To (Decl_List,
5459 Make_Subprogram_Declaration (Loc, Proc_Spec));
5461 -- Can_Convert_Unconstrained_Function checked that the function
5462 -- has no local declarations except implicit label declarations.
5463 -- Copy these declarations to the built procedure.
5465 if Present (Declarations (N)) then
5466 Body_Decl_List := New_List;
5473 D := First (Declarations (N));
5474 while Present (D) loop
5475 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5478 Make_Implicit_Label_Declaration (Loc,
5479 Make_Defining_Identifier (Loc,
5480 Chars => Chars (Defining_Identifier (D))),
5481 Label_Construct => Empty);
5482 Append_To (Body_Decl_List, New_D);
5489 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5492 Make_Subprogram_Body (Loc,
5493 Specification => Copy_Separate_Tree (Proc_Spec),
5494 Declarations => Body_Decl_List,
5495 Handled_Statement_Sequence =>
5496 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5498 Set_Defining_Unit_Name (Specification (Proc_Body),
5499 Make_Defining_Identifier (Loc, Subp_Name));
5501 Append_To (Decl_List, Proc_Body);
5502 end Build_Procedure;
5506 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5508 Proc_Id : Entity_Id;
5509 Proc_Call : Node_Id;
5511 -- Start of processing for Split_Unconstrained_Function
5514 -- Build the associated procedure, analyze it and insert it before
5515 -- the function body N
5518 Scope : constant Entity_Id := Current_Scope;
5519 Decl_List : List_Id;
5522 Build_Procedure (Proc_Id, Decl_List);
5523 Insert_Actions (N, Decl_List);
5527 -- Build the call to the generated procedure
5530 Actual_List : constant List_Id := New_List;
5534 Append_To (Actual_List,
5535 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5537 Formal := First_Formal (Spec_Id);
5538 while Present (Formal) loop
5539 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5541 -- Avoid spurious warning on unreferenced formals
5543 Set_Referenced (Formal);
5544 Next_Formal (Formal);
5548 Make_Procedure_Call_Statement (Loc,
5549 Name => New_Reference_To (Proc_Id, Loc),
5550 Parameter_Associations => Actual_List);
5558 -- main_1__F1b (New_Obj, ...);
5563 Make_Block_Statement (Loc,
5564 Declarations => New_List (New_Obj),
5565 Handled_Statement_Sequence =>
5566 Make_Handled_Sequence_Of_Statements (Loc,
5567 Statements => New_List (
5571 Make_Simple_Return_Statement (Loc,
5574 (Defining_Identifier (New_Obj), Loc)))));
5576 Rewrite (Ret_Node, Blk_Stmt);
5577 end Split_Unconstrained_Function;
5579 -- Start of processing for Check_And_Build_Body_To_Inline
5582 -- Do not inline any subprogram that contains nested subprograms, since
5583 -- the backend inlining circuit seems to generate uninitialized
5584 -- references in this case. We know this happens in the case of front
5585 -- end ZCX support, but it also appears it can happen in other cases as
5586 -- well. The backend often rejects attempts to inline in the case of
5587 -- nested procedures anyway, so little if anything is lost by this.
5588 -- Note that this is test is for the benefit of the back-end. There is
5589 -- a separate test for front-end inlining that also rejects nested
5592 -- Do not do this test if errors have been detected, because in some
5593 -- error cases, this code blows up, and we don't need it anyway if
5594 -- there have been errors, since we won't get to the linker anyway.
5596 if Comes_From_Source (Body_Id)
5597 and then (Has_Pragma_Inline_Always (Spec_Id)
5598 or else Optimization_Level > 0)
5599 and then Serious_Errors_Detected = 0
5607 P_Ent := Scope (P_Ent);
5608 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5610 if Is_Subprogram (P_Ent) then
5611 Set_Is_Inlined (P_Ent, False);
5613 if Comes_From_Source (P_Ent)
5614 and then Has_Pragma_Inline (P_Ent)
5617 ("cannot inline& (nested subprogram)?", N, P_Ent,
5618 Is_Serious => True);
5625 -- Build the body to inline only if really needed!
5627 if Check_Body_To_Inline (N, Spec_Id)
5628 and then Serious_Errors_Detected = 0
5630 if Returns_Unconstrained_Type (Spec_Id) then
5631 if Can_Split_Unconstrained_Function (N) then
5632 Split_Unconstrained_Function (N, Spec_Id);
5633 Build_Body_To_Inline (N, Spec_Id);
5634 Set_Is_Inlined (Spec_Id);
5637 Build_Body_To_Inline (N, Spec_Id);
5638 Set_Is_Inlined (Spec_Id);
5641 end Check_And_Build_Body_To_Inline;
5643 -----------------------
5644 -- Check_Conformance --
5645 -----------------------
5647 procedure Check_Conformance
5648 (New_Id : Entity_Id;
5650 Ctype : Conformance_Type;
5652 Conforms : out Boolean;
5653 Err_Loc : Node_Id := Empty;
5654 Get_Inst : Boolean := False;
5655 Skip_Controlling_Formals : Boolean := False)
5657 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5658 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5659 -- If Errmsg is True, then processing continues to post an error message
5660 -- for conformance error on given node. Two messages are output. The
5661 -- first message points to the previous declaration with a general "no
5662 -- conformance" message. The second is the detailed reason, supplied as
5663 -- Msg. The parameter N provide information for a possible & insertion
5664 -- in the message, and also provides the location for posting the
5665 -- message in the absence of a specified Err_Loc location.
5667 -----------------------
5668 -- Conformance_Error --
5669 -----------------------
5671 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5678 if No (Err_Loc) then
5684 Error_Msg_Sloc := Sloc (Old_Id);
5687 when Type_Conformant =>
5688 Error_Msg_N -- CODEFIX
5689 ("not type conformant with declaration#!", Enode);
5691 when Mode_Conformant =>
5692 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5694 ("not mode conformant with operation inherited#!",
5698 ("not mode conformant with declaration#!", Enode);
5701 when Subtype_Conformant =>
5702 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5704 ("not subtype conformant with operation inherited#!",
5708 ("not subtype conformant with declaration#!", Enode);
5711 when Fully_Conformant =>
5712 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5713 Error_Msg_N -- CODEFIX
5714 ("not fully conformant with operation inherited#!",
5717 Error_Msg_N -- CODEFIX
5718 ("not fully conformant with declaration#!", Enode);
5722 Error_Msg_NE (Msg, Enode, N);
5724 end Conformance_Error;
5728 Old_Type : constant Entity_Id := Etype (Old_Id);
5729 New_Type : constant Entity_Id := Etype (New_Id);
5730 Old_Formal : Entity_Id;
5731 New_Formal : Entity_Id;
5732 Access_Types_Match : Boolean;
5733 Old_Formal_Base : Entity_Id;
5734 New_Formal_Base : Entity_Id;
5736 -- Start of processing for Check_Conformance
5741 -- We need a special case for operators, since they don't appear
5744 if Ctype = Type_Conformant then
5745 if Ekind (New_Id) = E_Operator
5746 and then Operator_Matches_Spec (New_Id, Old_Id)
5752 -- If both are functions/operators, check return types conform
5754 if Old_Type /= Standard_Void_Type
5755 and then New_Type /= Standard_Void_Type
5758 -- If we are checking interface conformance we omit controlling
5759 -- arguments and result, because we are only checking the conformance
5760 -- of the remaining parameters.
5762 if Has_Controlling_Result (Old_Id)
5763 and then Has_Controlling_Result (New_Id)
5764 and then Skip_Controlling_Formals
5768 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5769 Conformance_Error ("\return type does not match!", New_Id);
5773 -- Ada 2005 (AI-231): In case of anonymous access types check the
5774 -- null-exclusion and access-to-constant attributes match.
5776 if Ada_Version >= Ada_2005
5777 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5779 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5780 or else Is_Access_Constant (Etype (Old_Type)) /=
5781 Is_Access_Constant (Etype (New_Type)))
5783 Conformance_Error ("\return type does not match!", New_Id);
5787 -- If either is a function/operator and the other isn't, error
5789 elsif Old_Type /= Standard_Void_Type
5790 or else New_Type /= Standard_Void_Type
5792 Conformance_Error ("\functions can only match functions!", New_Id);
5796 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5797 -- If this is a renaming as body, refine error message to indicate that
5798 -- the conflict is with the original declaration. If the entity is not
5799 -- frozen, the conventions don't have to match, the one of the renamed
5800 -- entity is inherited.
5802 if Ctype >= Subtype_Conformant then
5803 if Convention (Old_Id) /= Convention (New_Id) then
5804 if not Is_Frozen (New_Id) then
5807 elsif Present (Err_Loc)
5808 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5809 and then Present (Corresponding_Spec (Err_Loc))
5811 Error_Msg_Name_1 := Chars (New_Id);
5813 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5814 Conformance_Error ("\prior declaration for% has convention %!");
5817 Conformance_Error ("\calling conventions do not match!");
5822 elsif Is_Formal_Subprogram (Old_Id)
5823 or else Is_Formal_Subprogram (New_Id)
5825 Conformance_Error ("\formal subprograms not allowed!");
5830 -- Deal with parameters
5832 -- Note: we use the entity information, rather than going directly
5833 -- to the specification in the tree. This is not only simpler, but
5834 -- absolutely necessary for some cases of conformance tests between
5835 -- operators, where the declaration tree simply does not exist!
5837 Old_Formal := First_Formal (Old_Id);
5838 New_Formal := First_Formal (New_Id);
5839 while Present (Old_Formal) and then Present (New_Formal) loop
5840 if Is_Controlling_Formal (Old_Formal)
5841 and then Is_Controlling_Formal (New_Formal)
5842 and then Skip_Controlling_Formals
5844 -- The controlling formals will have different types when
5845 -- comparing an interface operation with its match, but both
5846 -- or neither must be access parameters.
5848 if Is_Access_Type (Etype (Old_Formal))
5850 Is_Access_Type (Etype (New_Formal))
5852 goto Skip_Controlling_Formal;
5855 ("\access parameter does not match!", New_Formal);
5859 -- Ada 2012: Mode conformance also requires that formal parameters
5860 -- be both aliased, or neither.
5862 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5863 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5865 ("\aliased parameter mismatch!", New_Formal);
5869 if Ctype = Fully_Conformant then
5871 -- Names must match. Error message is more accurate if we do
5872 -- this before checking that the types of the formals match.
5874 if Chars (Old_Formal) /= Chars (New_Formal) then
5875 Conformance_Error ("\name & does not match!", New_Formal);
5877 -- Set error posted flag on new formal as well to stop
5878 -- junk cascaded messages in some cases.
5880 Set_Error_Posted (New_Formal);
5884 -- Null exclusion must match
5886 if Null_Exclusion_Present (Parent (Old_Formal))
5888 Null_Exclusion_Present (Parent (New_Formal))
5890 -- Only give error if both come from source. This should be
5891 -- investigated some time, since it should not be needed ???
5893 if Comes_From_Source (Old_Formal)
5895 Comes_From_Source (New_Formal)
5898 ("\null exclusion for & does not match", New_Formal);
5900 -- Mark error posted on the new formal to avoid duplicated
5901 -- complaint about types not matching.
5903 Set_Error_Posted (New_Formal);
5908 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5909 -- case occurs whenever a subprogram is being renamed and one of its
5910 -- parameters imposes a null exclusion. For example:
5912 -- type T is null record;
5913 -- type Acc_T is access T;
5914 -- subtype Acc_T_Sub is Acc_T;
5916 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5917 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5920 Old_Formal_Base := Etype (Old_Formal);
5921 New_Formal_Base := Etype (New_Formal);
5924 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5925 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5928 Access_Types_Match := Ada_Version >= Ada_2005
5930 -- Ensure that this rule is only applied when New_Id is a
5931 -- renaming of Old_Id.
5933 and then Nkind (Parent (Parent (New_Id))) =
5934 N_Subprogram_Renaming_Declaration
5935 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5936 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5937 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5939 -- Now handle the allowed access-type case
5941 and then Is_Access_Type (Old_Formal_Base)
5942 and then Is_Access_Type (New_Formal_Base)
5944 -- The type kinds must match. The only exception occurs with
5945 -- multiple generics of the form:
5948 -- type F is private; type A is private;
5949 -- type F_Ptr is access F; type A_Ptr is access A;
5950 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5951 -- package F_Pack is ... package A_Pack is
5952 -- package F_Inst is
5953 -- new F_Pack (A, A_Ptr, A_P);
5955 -- When checking for conformance between the parameters of A_P
5956 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5957 -- because the compiler has transformed A_Ptr into a subtype of
5958 -- F_Ptr. We catch this case in the code below.
5960 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5962 (Is_Generic_Type (Old_Formal_Base)
5963 and then Is_Generic_Type (New_Formal_Base)
5964 and then Is_Internal (New_Formal_Base)
5965 and then Etype (Etype (New_Formal_Base)) =
5967 and then Directly_Designated_Type (Old_Formal_Base) =
5968 Directly_Designated_Type (New_Formal_Base)
5969 and then ((Is_Itype (Old_Formal_Base)
5970 and then Can_Never_Be_Null (Old_Formal_Base))
5972 (Is_Itype (New_Formal_Base)
5973 and then Can_Never_Be_Null (New_Formal_Base)));
5975 -- Types must always match. In the visible part of an instance,
5976 -- usual overloading rules for dispatching operations apply, and
5977 -- we check base types (not the actual subtypes).
5979 if In_Instance_Visible_Part
5980 and then Is_Dispatching_Operation (New_Id)
5982 if not Conforming_Types
5983 (T1 => Base_Type (Etype (Old_Formal)),
5984 T2 => Base_Type (Etype (New_Formal)),
5986 Get_Inst => Get_Inst)
5987 and then not Access_Types_Match
5989 Conformance_Error ("\type of & does not match!", New_Formal);
5993 elsif not Conforming_Types
5994 (T1 => Old_Formal_Base,
5995 T2 => New_Formal_Base,
5997 Get_Inst => Get_Inst)
5998 and then not Access_Types_Match
6000 -- Don't give error message if old type is Any_Type. This test
6001 -- avoids some cascaded errors, e.g. in case of a bad spec.
6003 if Errmsg and then Old_Formal_Base = Any_Type then
6006 Conformance_Error ("\type of & does not match!", New_Formal);
6012 -- For mode conformance, mode must match
6014 if Ctype >= Mode_Conformant then
6015 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6016 if not Ekind_In (New_Id, E_Function, E_Procedure)
6017 or else not Is_Primitive_Wrapper (New_Id)
6019 Conformance_Error ("\mode of & does not match!", New_Formal);
6023 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6025 if Is_Protected_Type
6026 (Corresponding_Concurrent_Type (T))
6028 Error_Msg_PT (T, New_Id);
6031 ("\mode of & does not match!", New_Formal);
6038 -- Part of mode conformance for access types is having the same
6039 -- constant modifier.
6041 elsif Access_Types_Match
6042 and then Is_Access_Constant (Old_Formal_Base) /=
6043 Is_Access_Constant (New_Formal_Base)
6046 ("\constant modifier does not match!", New_Formal);
6051 if Ctype >= Subtype_Conformant then
6053 -- Ada 2005 (AI-231): In case of anonymous access types check
6054 -- the null-exclusion and access-to-constant attributes must
6055 -- match. For null exclusion, we test the types rather than the
6056 -- formals themselves, since the attribute is only set reliably
6057 -- on the formals in the Ada 95 case, and we exclude the case
6058 -- where Old_Formal is marked as controlling, to avoid errors
6059 -- when matching completing bodies with dispatching declarations
6060 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6062 if Ada_Version >= Ada_2005
6063 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6064 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6066 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6067 Can_Never_Be_Null (Etype (New_Formal))
6069 not Is_Controlling_Formal (Old_Formal))
6071 Is_Access_Constant (Etype (Old_Formal)) /=
6072 Is_Access_Constant (Etype (New_Formal)))
6074 -- Do not complain if error already posted on New_Formal. This
6075 -- avoids some redundant error messages.
6077 and then not Error_Posted (New_Formal)
6079 -- It is allowed to omit the null-exclusion in case of stream
6080 -- attribute subprograms. We recognize stream subprograms
6081 -- through their TSS-generated suffix.
6084 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6087 if TSS_Name /= TSS_Stream_Read
6088 and then TSS_Name /= TSS_Stream_Write
6089 and then TSS_Name /= TSS_Stream_Input
6090 and then TSS_Name /= TSS_Stream_Output
6092 -- Here we have a definite conformance error. It is worth
6093 -- special casing the error message for the case of a
6094 -- controlling formal (which excludes null).
6096 if Is_Controlling_Formal (New_Formal) then
6097 Error_Msg_Node_2 := Scope (New_Formal);
6099 ("\controlling formal& of& excludes null, "
6100 & "declaration must exclude null as well",
6103 -- Normal case (couldn't we give more detail here???)
6107 ("\type of & does not match!", New_Formal);
6116 -- Full conformance checks
6118 if Ctype = Fully_Conformant then
6120 -- We have checked already that names match
6122 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6124 -- Check default expressions for in parameters
6127 NewD : constant Boolean :=
6128 Present (Default_Value (New_Formal));
6129 OldD : constant Boolean :=
6130 Present (Default_Value (Old_Formal));
6132 if NewD or OldD then
6134 -- The old default value has been analyzed because the
6135 -- current full declaration will have frozen everything
6136 -- before. The new default value has not been analyzed,
6137 -- so analyze it now before we check for conformance.
6140 Push_Scope (New_Id);
6141 Preanalyze_Spec_Expression
6142 (Default_Value (New_Formal), Etype (New_Formal));
6146 if not (NewD and OldD)
6147 or else not Fully_Conformant_Expressions
6148 (Default_Value (Old_Formal),
6149 Default_Value (New_Formal))
6152 ("\default expression for & does not match!",
6161 -- A couple of special checks for Ada 83 mode. These checks are
6162 -- skipped if either entity is an operator in package Standard,
6163 -- or if either old or new instance is not from the source program.
6165 if Ada_Version = Ada_83
6166 and then Sloc (Old_Id) > Standard_Location
6167 and then Sloc (New_Id) > Standard_Location
6168 and then Comes_From_Source (Old_Id)
6169 and then Comes_From_Source (New_Id)
6172 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6173 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6176 -- Explicit IN must be present or absent in both cases. This
6177 -- test is required only in the full conformance case.
6179 if In_Present (Old_Param) /= In_Present (New_Param)
6180 and then Ctype = Fully_Conformant
6183 ("\(Ada 83) IN must appear in both declarations",
6188 -- Grouping (use of comma in param lists) must be the same
6189 -- This is where we catch a misconformance like:
6192 -- A : Integer; B : Integer
6194 -- which are represented identically in the tree except
6195 -- for the setting of the flags More_Ids and Prev_Ids.
6197 if More_Ids (Old_Param) /= More_Ids (New_Param)
6198 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6201 ("\grouping of & does not match!", New_Formal);
6207 -- This label is required when skipping controlling formals
6209 <<Skip_Controlling_Formal>>
6211 Next_Formal (Old_Formal);
6212 Next_Formal (New_Formal);
6215 if Present (Old_Formal) then
6216 Conformance_Error ("\too few parameters!");
6219 elsif Present (New_Formal) then
6220 Conformance_Error ("\too many parameters!", New_Formal);
6223 end Check_Conformance;
6225 -----------------------
6226 -- Check_Conventions --
6227 -----------------------
6229 procedure Check_Conventions (Typ : Entity_Id) is
6230 Ifaces_List : Elist_Id;
6232 procedure Check_Convention (Op : Entity_Id);
6233 -- Verify that the convention of inherited dispatching operation Op is
6234 -- consistent among all subprograms it overrides. In order to minimize
6235 -- the search, Search_From is utilized to designate a specific point in
6236 -- the list rather than iterating over the whole list once more.
6238 ----------------------
6239 -- Check_Convention --
6240 ----------------------
6242 procedure Check_Convention (Op : Entity_Id) is
6243 function Convention_Of (Id : Entity_Id) return Convention_Id;
6244 -- Given an entity, return its convention. The function treats Ghost
6245 -- as convention Ada because the two have the same dynamic semantics.
6251 function Convention_Of (Id : Entity_Id) return Convention_Id is
6252 Conv : constant Convention_Id := Convention (Id);
6254 if Conv = Convention_Ghost then
6255 return Convention_Ada;
6263 Op_Conv : constant Convention_Id := Convention_Of (Op);
6264 Iface_Conv : Convention_Id;
6265 Iface_Elmt : Elmt_Id;
6266 Iface_Prim_Elmt : Elmt_Id;
6267 Iface_Prim : Entity_Id;
6269 -- Start of processing for Check_Convention
6272 Iface_Elmt := First_Elmt (Ifaces_List);
6273 while Present (Iface_Elmt) loop
6275 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6276 while Present (Iface_Prim_Elmt) loop
6277 Iface_Prim := Node (Iface_Prim_Elmt);
6278 Iface_Conv := Convention_Of (Iface_Prim);
6280 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6281 and then Iface_Conv /= Op_Conv
6284 ("inconsistent conventions in primitive operations", Typ);
6286 Error_Msg_Name_1 := Chars (Op);
6287 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6288 Error_Msg_Sloc := Sloc (Op);
6290 if Comes_From_Source (Op) or else No (Alias (Op)) then
6291 if not Present (Overridden_Operation (Op)) then
6292 Error_Msg_N ("\\primitive % defined #", Typ);
6295 ("\\overriding operation % with " &
6296 "convention % defined #", Typ);
6299 else pragma Assert (Present (Alias (Op)));
6300 Error_Msg_Sloc := Sloc (Alias (Op));
6302 ("\\inherited operation % with " &
6303 "convention % defined #", Typ);
6306 Error_Msg_Name_1 := Chars (Op);
6307 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6308 Error_Msg_Sloc := Sloc (Iface_Prim);
6310 ("\\overridden operation % with " &
6311 "convention % defined #", Typ);
6313 -- Avoid cascading errors
6318 Next_Elmt (Iface_Prim_Elmt);
6321 Next_Elmt (Iface_Elmt);
6323 end Check_Convention;
6327 Prim_Op : Entity_Id;
6328 Prim_Op_Elmt : Elmt_Id;
6330 -- Start of processing for Check_Conventions
6333 if not Has_Interfaces (Typ) then
6337 Collect_Interfaces (Typ, Ifaces_List);
6339 -- The algorithm checks every overriding dispatching operation against
6340 -- all the corresponding overridden dispatching operations, detecting
6341 -- differences in conventions.
6343 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6344 while Present (Prim_Op_Elmt) loop
6345 Prim_Op := Node (Prim_Op_Elmt);
6347 -- A small optimization: skip the predefined dispatching operations
6348 -- since they always have the same convention.
6350 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6351 Check_Convention (Prim_Op);
6354 Next_Elmt (Prim_Op_Elmt);
6356 end Check_Conventions;
6358 ------------------------------
6359 -- Check_Delayed_Subprogram --
6360 ------------------------------
6362 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6365 procedure Possible_Freeze (T : Entity_Id);
6366 -- T is the type of either a formal parameter or of the return type.
6367 -- If T is not yet frozen and needs a delayed freeze, then the
6368 -- subprogram itself must be delayed. If T is the limited view of an
6369 -- incomplete type the subprogram must be frozen as well, because
6370 -- T may depend on local types that have not been frozen yet.
6372 ---------------------
6373 -- Possible_Freeze --
6374 ---------------------
6376 procedure Possible_Freeze (T : Entity_Id) is
6378 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6379 Set_Has_Delayed_Freeze (Designator);
6381 elsif Is_Access_Type (T)
6382 and then Has_Delayed_Freeze (Designated_Type (T))
6383 and then not Is_Frozen (Designated_Type (T))
6385 Set_Has_Delayed_Freeze (Designator);
6387 elsif Ekind (T) = E_Incomplete_Type
6388 and then From_Limited_With (T)
6390 Set_Has_Delayed_Freeze (Designator);
6392 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6393 -- of a subprogram or entry declaration.
6395 elsif Ekind (T) = E_Incomplete_Type
6396 and then Ada_Version >= Ada_2012
6398 Set_Has_Delayed_Freeze (Designator);
6401 end Possible_Freeze;
6403 -- Start of processing for Check_Delayed_Subprogram
6406 -- All subprograms, including abstract subprograms, may need a freeze
6407 -- node if some formal type or the return type needs one.
6409 Possible_Freeze (Etype (Designator));
6410 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6412 -- Need delayed freeze if any of the formal types themselves need
6413 -- a delayed freeze and are not yet frozen.
6415 F := First_Formal (Designator);
6416 while Present (F) loop
6417 Possible_Freeze (Etype (F));
6418 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6422 -- Mark functions that return by reference. Note that it cannot be
6423 -- done for delayed_freeze subprograms because the underlying
6424 -- returned type may not be known yet (for private types)
6426 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6428 Typ : constant Entity_Id := Etype (Designator);
6429 Utyp : constant Entity_Id := Underlying_Type (Typ);
6431 if Is_Limited_View (Typ) then
6432 Set_Returns_By_Ref (Designator);
6433 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6434 Set_Returns_By_Ref (Designator);
6438 end Check_Delayed_Subprogram;
6440 ------------------------------------
6441 -- Check_Discriminant_Conformance --
6442 ------------------------------------
6444 procedure Check_Discriminant_Conformance
6449 Old_Discr : Entity_Id := First_Discriminant (Prev);
6450 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6451 New_Discr_Id : Entity_Id;
6452 New_Discr_Type : Entity_Id;
6454 procedure Conformance_Error (Msg : String; N : Node_Id);
6455 -- Post error message for conformance error on given node. Two messages
6456 -- are output. The first points to the previous declaration with a
6457 -- general "no conformance" message. The second is the detailed reason,
6458 -- supplied as Msg. The parameter N provide information for a possible
6459 -- & insertion in the message.
6461 -----------------------
6462 -- Conformance_Error --
6463 -----------------------
6465 procedure Conformance_Error (Msg : String; N : Node_Id) is
6467 Error_Msg_Sloc := Sloc (Prev_Loc);
6468 Error_Msg_N -- CODEFIX
6469 ("not fully conformant with declaration#!", N);
6470 Error_Msg_NE (Msg, N, N);
6471 end Conformance_Error;
6473 -- Start of processing for Check_Discriminant_Conformance
6476 while Present (Old_Discr) and then Present (New_Discr) loop
6477 New_Discr_Id := Defining_Identifier (New_Discr);
6479 -- The subtype mark of the discriminant on the full type has not
6480 -- been analyzed so we do it here. For an access discriminant a new
6483 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6485 Access_Definition (N, Discriminant_Type (New_Discr));
6488 Analyze (Discriminant_Type (New_Discr));
6489 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6491 -- Ada 2005: if the discriminant definition carries a null
6492 -- exclusion, create an itype to check properly for consistency
6493 -- with partial declaration.
6495 if Is_Access_Type (New_Discr_Type)
6496 and then Null_Exclusion_Present (New_Discr)
6499 Create_Null_Excluding_Itype
6500 (T => New_Discr_Type,
6501 Related_Nod => New_Discr,
6502 Scope_Id => Current_Scope);
6506 if not Conforming_Types
6507 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6509 Conformance_Error ("type of & does not match!", New_Discr_Id);
6512 -- Treat the new discriminant as an occurrence of the old one,
6513 -- for navigation purposes, and fill in some semantic
6514 -- information, for completeness.
6516 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6517 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6518 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6523 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6524 Conformance_Error ("name & does not match!", New_Discr_Id);
6528 -- Default expressions must match
6531 NewD : constant Boolean :=
6532 Present (Expression (New_Discr));
6533 OldD : constant Boolean :=
6534 Present (Expression (Parent (Old_Discr)));
6537 if NewD or OldD then
6539 -- The old default value has been analyzed and expanded,
6540 -- because the current full declaration will have frozen
6541 -- everything before. The new default values have not been
6542 -- expanded, so expand now to check conformance.
6545 Preanalyze_Spec_Expression
6546 (Expression (New_Discr), New_Discr_Type);
6549 if not (NewD and OldD)
6550 or else not Fully_Conformant_Expressions
6551 (Expression (Parent (Old_Discr)),
6552 Expression (New_Discr))
6556 ("default expression for & does not match!",
6563 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6565 if Ada_Version = Ada_83 then
6567 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6570 -- Grouping (use of comma in param lists) must be the same
6571 -- This is where we catch a misconformance like:
6574 -- A : Integer; B : Integer
6576 -- which are represented identically in the tree except
6577 -- for the setting of the flags More_Ids and Prev_Ids.
6579 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6580 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6583 ("grouping of & does not match!", New_Discr_Id);
6589 Next_Discriminant (Old_Discr);
6593 if Present (Old_Discr) then
6594 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6597 elsif Present (New_Discr) then
6599 ("too many discriminants!", Defining_Identifier (New_Discr));
6602 end Check_Discriminant_Conformance;
6604 ----------------------------
6605 -- Check_Fully_Conformant --
6606 ----------------------------
6608 procedure Check_Fully_Conformant
6609 (New_Id : Entity_Id;
6611 Err_Loc : Node_Id := Empty)
6614 pragma Warnings (Off, Result);
6617 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6618 end Check_Fully_Conformant;
6620 ---------------------------
6621 -- Check_Mode_Conformant --
6622 ---------------------------
6624 procedure Check_Mode_Conformant
6625 (New_Id : Entity_Id;
6627 Err_Loc : Node_Id := Empty;
6628 Get_Inst : Boolean := False)
6631 pragma Warnings (Off, Result);
6634 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6635 end Check_Mode_Conformant;
6637 --------------------------------
6638 -- Check_Overriding_Indicator --
6639 --------------------------------
6641 procedure Check_Overriding_Indicator
6643 Overridden_Subp : Entity_Id;
6644 Is_Primitive : Boolean)
6650 -- No overriding indicator for literals
6652 if Ekind (Subp) = E_Enumeration_Literal then
6655 elsif Ekind (Subp) = E_Entry then
6656 Decl := Parent (Subp);
6658 -- No point in analyzing a malformed operator
6660 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6661 and then Error_Posted (Subp)
6666 Decl := Unit_Declaration_Node (Subp);
6669 if Nkind_In (Decl, N_Subprogram_Body,
6670 N_Subprogram_Body_Stub,
6671 N_Subprogram_Declaration,
6672 N_Abstract_Subprogram_Declaration,
6673 N_Subprogram_Renaming_Declaration)
6675 Spec := Specification (Decl);
6677 elsif Nkind (Decl) = N_Entry_Declaration then
6684 -- The overriding operation is type conformant with the overridden one,
6685 -- but the names of the formals are not required to match. If the names
6686 -- appear permuted in the overriding operation, this is a possible
6687 -- source of confusion that is worth diagnosing. Controlling formals
6688 -- often carry names that reflect the type, and it is not worthwhile
6689 -- requiring that their names match.
6691 if Present (Overridden_Subp)
6692 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6699 Form1 := First_Formal (Subp);
6700 Form2 := First_Formal (Overridden_Subp);
6702 -- If the overriding operation is a synchronized operation, skip
6703 -- the first parameter of the overridden operation, which is
6704 -- implicit in the new one. If the operation is declared in the
6705 -- body it is not primitive and all formals must match.
6707 if Is_Concurrent_Type (Scope (Subp))
6708 and then Is_Tagged_Type (Scope (Subp))
6709 and then not Has_Completion (Scope (Subp))
6711 Form2 := Next_Formal (Form2);
6714 if Present (Form1) then
6715 Form1 := Next_Formal (Form1);
6716 Form2 := Next_Formal (Form2);
6719 while Present (Form1) loop
6720 if not Is_Controlling_Formal (Form1)
6721 and then Present (Next_Formal (Form2))
6722 and then Chars (Form1) = Chars (Next_Formal (Form2))
6724 Error_Msg_Node_2 := Alias (Overridden_Subp);
6725 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6727 ("& does not match corresponding formal of&#",
6732 Next_Formal (Form1);
6733 Next_Formal (Form2);
6738 -- If there is an overridden subprogram, then check that there is no
6739 -- "not overriding" indicator, and mark the subprogram as overriding.
6740 -- This is not done if the overridden subprogram is marked as hidden,
6741 -- which can occur for the case of inherited controlled operations
6742 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6743 -- subprogram is not itself hidden. (Note: This condition could probably
6744 -- be simplified, leaving out the testing for the specific controlled
6745 -- cases, but it seems safer and clearer this way, and echoes similar
6746 -- special-case tests of this kind in other places.)
6748 if Present (Overridden_Subp)
6749 and then (not Is_Hidden (Overridden_Subp)
6751 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6754 and then Present (Alias (Overridden_Subp))
6755 and then not Is_Hidden (Alias (Overridden_Subp))))
6757 if Must_Not_Override (Spec) then
6758 Error_Msg_Sloc := Sloc (Overridden_Subp);
6760 if Ekind (Subp) = E_Entry then
6762 ("entry & overrides inherited operation #", Spec, Subp);
6765 ("subprogram & overrides inherited operation #", Spec, Subp);
6768 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6769 -- as an extension of Root_Controlled, and thus has a useless Adjust
6770 -- operation. This operation should not be inherited by other limited
6771 -- controlled types. An explicit Adjust for them is not overriding.
6773 elsif Must_Override (Spec)
6774 and then Chars (Overridden_Subp) = Name_Adjust
6775 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6776 and then Present (Alias (Overridden_Subp))
6778 Is_Predefined_File_Name
6779 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6781 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6783 elsif Is_Subprogram (Subp) then
6784 if Is_Init_Proc (Subp) then
6787 elsif No (Overridden_Operation (Subp)) then
6789 -- For entities generated by Derive_Subprograms the overridden
6790 -- operation is the inherited primitive (which is available
6791 -- through the attribute alias)
6793 if (Is_Dispatching_Operation (Subp)
6794 or else Is_Dispatching_Operation (Overridden_Subp))
6795 and then not Comes_From_Source (Overridden_Subp)
6796 and then Find_Dispatching_Type (Overridden_Subp) =
6797 Find_Dispatching_Type (Subp)
6798 and then Present (Alias (Overridden_Subp))
6799 and then Comes_From_Source (Alias (Overridden_Subp))
6801 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6804 Set_Overridden_Operation (Subp, Overridden_Subp);
6809 -- If primitive flag is set or this is a protected operation, then
6810 -- the operation is overriding at the point of its declaration, so
6811 -- warn if necessary. Otherwise it may have been declared before the
6812 -- operation it overrides and no check is required.
6815 and then not Must_Override (Spec)
6816 and then (Is_Primitive
6817 or else Ekind (Scope (Subp)) = E_Protected_Type)
6819 Style.Missing_Overriding (Decl, Subp);
6822 -- If Subp is an operator, it may override a predefined operation, if
6823 -- it is defined in the same scope as the type to which it applies.
6824 -- In that case Overridden_Subp is empty because of our implicit
6825 -- representation for predefined operators. We have to check whether the
6826 -- signature of Subp matches that of a predefined operator. Note that
6827 -- first argument provides the name of the operator, and the second
6828 -- argument the signature that may match that of a standard operation.
6829 -- If the indicator is overriding, then the operator must match a
6830 -- predefined signature, because we know already that there is no
6831 -- explicit overridden operation.
6833 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6834 if Must_Not_Override (Spec) then
6836 -- If this is not a primitive or a protected subprogram, then
6837 -- "not overriding" is illegal.
6840 and then Ekind (Scope (Subp)) /= E_Protected_Type
6843 ("overriding indicator only allowed "
6844 & "if subprogram is primitive", Subp);
6846 elsif Can_Override_Operator (Subp) then
6848 ("subprogram& overrides predefined operator ", Spec, Subp);
6851 elsif Must_Override (Spec) then
6852 if No (Overridden_Operation (Subp))
6853 and then not Can_Override_Operator (Subp)
6855 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6858 elsif not Error_Posted (Subp)
6859 and then Style_Check
6860 and then Can_Override_Operator (Subp)
6862 not Is_Predefined_File_Name
6863 (Unit_File_Name (Get_Source_Unit (Subp)))
6865 -- If style checks are enabled, indicate that the indicator is
6866 -- missing. However, at the point of declaration, the type of
6867 -- which this is a primitive operation may be private, in which
6868 -- case the indicator would be premature.
6870 if Has_Private_Declaration (Etype (Subp))
6871 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6875 Style.Missing_Overriding (Decl, Subp);
6879 elsif Must_Override (Spec) then
6880 if Ekind (Subp) = E_Entry then
6881 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6883 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6886 -- If the operation is marked "not overriding" and it's not primitive
6887 -- then an error is issued, unless this is an operation of a task or
6888 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6889 -- has been specified have already been checked above.
6891 elsif Must_Not_Override (Spec)
6892 and then not Is_Primitive
6893 and then Ekind (Subp) /= E_Entry
6894 and then Ekind (Scope (Subp)) /= E_Protected_Type
6897 ("overriding indicator only allowed if subprogram is primitive",
6901 end Check_Overriding_Indicator;
6907 -- Note: this procedure needs to know far too much about how the expander
6908 -- messes with exceptions. The use of the flag Exception_Junk and the
6909 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6910 -- works, but is not very clean. It would be better if the expansion
6911 -- routines would leave Original_Node working nicely, and we could use
6912 -- Original_Node here to ignore all the peculiar expander messing ???
6914 procedure Check_Returns
6918 Proc : Entity_Id := Empty)
6922 procedure Check_Statement_Sequence (L : List_Id);
6923 -- Internal recursive procedure to check a list of statements for proper
6924 -- termination by a return statement (or a transfer of control or a
6925 -- compound statement that is itself internally properly terminated).
6927 ------------------------------
6928 -- Check_Statement_Sequence --
6929 ------------------------------
6931 procedure Check_Statement_Sequence (L : List_Id) is
6936 Raise_Exception_Call : Boolean;
6937 -- Set True if statement sequence terminated by Raise_Exception call
6938 -- or a Reraise_Occurrence call.
6941 Raise_Exception_Call := False;
6943 -- Get last real statement
6945 Last_Stm := Last (L);
6947 -- Deal with digging out exception handler statement sequences that
6948 -- have been transformed by the local raise to goto optimization.
6949 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6950 -- optimization has occurred, we are looking at something like:
6953 -- original stmts in block
6957 -- goto L1; | omitted if No_Exception_Propagation
6962 -- goto L3; -- skip handler when exception not raised
6964 -- <<L1>> -- target label for local exception
6978 -- and what we have to do is to dig out the estmts1 and estmts2
6979 -- sequences (which were the original sequences of statements in
6980 -- the exception handlers) and check them.
6982 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6987 exit when Nkind (Stm) /= N_Block_Statement;
6988 exit when not Exception_Junk (Stm);
6991 exit when Nkind (Stm) /= N_Label;
6992 exit when not Exception_Junk (Stm);
6993 Check_Statement_Sequence
6994 (Statements (Handled_Statement_Sequence (Next (Stm))));
6999 exit when Nkind (Stm) /= N_Goto_Statement;
7000 exit when not Exception_Junk (Stm);
7004 -- Don't count pragmas
7006 while Nkind (Last_Stm) = N_Pragma
7008 -- Don't count call to SS_Release (can happen after Raise_Exception)
7011 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7013 Nkind (Name (Last_Stm)) = N_Identifier
7015 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7017 -- Don't count exception junk
7020 (Nkind_In (Last_Stm, N_Goto_Statement,
7022 N_Object_Declaration)
7023 and then Exception_Junk (Last_Stm))
7024 or else Nkind (Last_Stm) in N_Push_xxx_Label
7025 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7027 -- Inserted code, such as finalization calls, is irrelevant: we only
7028 -- need to check original source.
7030 or else Is_Rewrite_Insertion (Last_Stm)
7035 -- Here we have the "real" last statement
7037 Kind := Nkind (Last_Stm);
7039 -- Transfer of control, OK. Note that in the No_Return procedure
7040 -- case, we already diagnosed any explicit return statements, so
7041 -- we can treat them as OK in this context.
7043 if Is_Transfer (Last_Stm) then
7046 -- Check cases of explicit non-indirect procedure calls
7048 elsif Kind = N_Procedure_Call_Statement
7049 and then Is_Entity_Name (Name (Last_Stm))
7051 -- Check call to Raise_Exception procedure which is treated
7052 -- specially, as is a call to Reraise_Occurrence.
7054 -- We suppress the warning in these cases since it is likely that
7055 -- the programmer really does not expect to deal with the case
7056 -- of Null_Occurrence, and thus would find a warning about a
7057 -- missing return curious, and raising Program_Error does not
7058 -- seem such a bad behavior if this does occur.
7060 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7061 -- behavior will be to raise Constraint_Error (see AI-329).
7063 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7065 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7067 Raise_Exception_Call := True;
7069 -- For Raise_Exception call, test first argument, if it is
7070 -- an attribute reference for a 'Identity call, then we know
7071 -- that the call cannot possibly return.
7074 Arg : constant Node_Id :=
7075 Original_Node (First_Actual (Last_Stm));
7077 if Nkind (Arg) = N_Attribute_Reference
7078 and then Attribute_Name (Arg) = Name_Identity
7085 -- If statement, need to look inside if there is an else and check
7086 -- each constituent statement sequence for proper termination.
7088 elsif Kind = N_If_Statement
7089 and then Present (Else_Statements (Last_Stm))
7091 Check_Statement_Sequence (Then_Statements (Last_Stm));
7092 Check_Statement_Sequence (Else_Statements (Last_Stm));
7094 if Present (Elsif_Parts (Last_Stm)) then
7096 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7099 while Present (Elsif_Part) loop
7100 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7108 -- Case statement, check each case for proper termination
7110 elsif Kind = N_Case_Statement then
7114 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7115 while Present (Case_Alt) loop
7116 Check_Statement_Sequence (Statements (Case_Alt));
7117 Next_Non_Pragma (Case_Alt);
7123 -- Block statement, check its handled sequence of statements
7125 elsif Kind = N_Block_Statement then
7131 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7140 -- Loop statement. If there is an iteration scheme, we can definitely
7141 -- fall out of the loop. Similarly if there is an exit statement, we
7142 -- can fall out. In either case we need a following return.
7144 elsif Kind = N_Loop_Statement then
7145 if Present (Iteration_Scheme (Last_Stm))
7146 or else Has_Exit (Entity (Identifier (Last_Stm)))
7150 -- A loop with no exit statement or iteration scheme is either
7151 -- an infinite loop, or it has some other exit (raise/return).
7152 -- In either case, no warning is required.
7158 -- Timed entry call, check entry call and delay alternatives
7160 -- Note: in expanded code, the timed entry call has been converted
7161 -- to a set of expanded statements on which the check will work
7162 -- correctly in any case.
7164 elsif Kind = N_Timed_Entry_Call then
7166 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7167 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7170 -- If statement sequence of entry call alternative is missing,
7171 -- then we can definitely fall through, and we post the error
7172 -- message on the entry call alternative itself.
7174 if No (Statements (ECA)) then
7177 -- If statement sequence of delay alternative is missing, then
7178 -- we can definitely fall through, and we post the error
7179 -- message on the delay alternative itself.
7181 -- Note: if both ECA and DCA are missing the return, then we
7182 -- post only one message, should be enough to fix the bugs.
7183 -- If not we will get a message next time on the DCA when the
7186 elsif No (Statements (DCA)) then
7189 -- Else check both statement sequences
7192 Check_Statement_Sequence (Statements (ECA));
7193 Check_Statement_Sequence (Statements (DCA));
7198 -- Conditional entry call, check entry call and else part
7200 -- Note: in expanded code, the conditional entry call has been
7201 -- converted to a set of expanded statements on which the check
7202 -- will work correctly in any case.
7204 elsif Kind = N_Conditional_Entry_Call then
7206 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7209 -- If statement sequence of entry call alternative is missing,
7210 -- then we can definitely fall through, and we post the error
7211 -- message on the entry call alternative itself.
7213 if No (Statements (ECA)) then
7216 -- Else check statement sequence and else part
7219 Check_Statement_Sequence (Statements (ECA));
7220 Check_Statement_Sequence (Else_Statements (Last_Stm));
7226 -- If we fall through, issue appropriate message
7229 if not Raise_Exception_Call then
7231 -- In GNATprove mode, it is an error to have a missing return
7233 Error_Msg_Warn := SPARK_Mode /= On;
7235 ("RETURN statement missing following this statement<<!",
7238 ("\Program_Error ]<<!", Last_Stm);
7241 -- Note: we set Err even though we have not issued a warning
7242 -- because we still have a case of a missing return. This is
7243 -- an extremely marginal case, probably will never be noticed
7244 -- but we might as well get it right.
7248 -- Otherwise we have the case of a procedure marked No_Return
7251 if not Raise_Exception_Call then
7252 if GNATprove_Mode then
7254 ("implied return after this statement "
7255 & "would have raised Program_Error", Last_Stm);
7258 ("implied return after this statement "
7259 & "will raise Program_Error??", Last_Stm);
7262 Error_Msg_Warn := SPARK_Mode /= On;
7264 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7268 RE : constant Node_Id :=
7269 Make_Raise_Program_Error (Sloc (Last_Stm),
7270 Reason => PE_Implicit_Return);
7272 Insert_After (Last_Stm, RE);
7276 end Check_Statement_Sequence;
7278 -- Start of processing for Check_Returns
7282 Check_Statement_Sequence (Statements (HSS));
7284 if Present (Exception_Handlers (HSS)) then
7285 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7286 while Present (Handler) loop
7287 Check_Statement_Sequence (Statements (Handler));
7288 Next_Non_Pragma (Handler);
7293 ----------------------------
7294 -- Check_Subprogram_Order --
7295 ----------------------------
7297 procedure Check_Subprogram_Order (N : Node_Id) is
7299 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7300 -- This is used to check if S1 > S2 in the sense required by this test,
7301 -- for example nameab < namec, but name2 < name10.
7303 -----------------------------
7304 -- Subprogram_Name_Greater --
7305 -----------------------------
7307 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7312 -- Deal with special case where names are identical except for a
7313 -- numerical suffix. These are handled specially, taking the numeric
7314 -- ordering from the suffix into account.
7317 while S1 (L1) in '0' .. '9' loop
7322 while S2 (L2) in '0' .. '9' loop
7326 -- If non-numeric parts non-equal, do straight compare
7328 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7331 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7332 -- that a missing suffix is treated as numeric zero in this test.
7336 while L1 < S1'Last loop
7338 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7342 while L2 < S2'Last loop
7344 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7349 end Subprogram_Name_Greater;
7351 -- Start of processing for Check_Subprogram_Order
7354 -- Check body in alpha order if this is option
7357 and then Style_Check_Order_Subprograms
7358 and then Nkind (N) = N_Subprogram_Body
7359 and then Comes_From_Source (N)
7360 and then In_Extended_Main_Source_Unit (N)
7364 renames Scope_Stack.Table
7365 (Scope_Stack.Last).Last_Subprogram_Name;
7367 Body_Id : constant Entity_Id :=
7368 Defining_Entity (Specification (N));
7371 Get_Decoded_Name_String (Chars (Body_Id));
7374 if Subprogram_Name_Greater
7375 (LSN.all, Name_Buffer (1 .. Name_Len))
7377 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7383 LSN := new String'(Name_Buffer (1 .. Name_Len));
7386 end Check_Subprogram_Order;
7388 ------------------------------
7389 -- Check_Subtype_Conformant --
7390 ------------------------------
7392 procedure Check_Subtype_Conformant
7393 (New_Id : Entity_Id;
7395 Err_Loc : Node_Id := Empty;
7396 Skip_Controlling_Formals : Boolean := False;
7397 Get_Inst : Boolean := False)
7400 pragma Warnings (Off, Result);
7403 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7404 Skip_Controlling_Formals => Skip_Controlling_Formals,
7405 Get_Inst => Get_Inst);
7406 end Check_Subtype_Conformant;
7408 ---------------------------
7409 -- Check_Type_Conformant --
7410 ---------------------------
7412 procedure Check_Type_Conformant
7413 (New_Id : Entity_Id;
7415 Err_Loc : Node_Id := Empty)
7418 pragma Warnings (Off, Result);
7421 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7422 end Check_Type_Conformant;
7424 ---------------------------
7425 -- Can_Override_Operator --
7426 ---------------------------
7428 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7432 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7436 Typ := Base_Type (Etype (First_Formal (Subp)));
7438 -- Check explicitly that the operation is a primitive of the type
7440 return Operator_Matches_Spec (Subp, Subp)
7441 and then not Is_Generic_Type (Typ)
7442 and then Scope (Subp) = Scope (Typ)
7443 and then not Is_Class_Wide_Type (Typ);
7445 end Can_Override_Operator;
7447 ----------------------
7448 -- Conforming_Types --
7449 ----------------------
7451 function Conforming_Types
7454 Ctype : Conformance_Type;
7455 Get_Inst : Boolean := False) return Boolean
7457 Type_1 : Entity_Id := T1;
7458 Type_2 : Entity_Id := T2;
7459 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7461 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7462 -- If neither T1 nor T2 are generic actual types, or if they are in
7463 -- different scopes (e.g. parent and child instances), then verify that
7464 -- the base types are equal. Otherwise T1 and T2 must be on the same
7465 -- subtype chain. The whole purpose of this procedure is to prevent
7466 -- spurious ambiguities in an instantiation that may arise if two
7467 -- distinct generic types are instantiated with the same actual.
7469 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7470 -- An access parameter can designate an incomplete type. If the
7471 -- incomplete type is the limited view of a type from a limited_
7472 -- with_clause, check whether the non-limited view is available. If
7473 -- it is a (non-limited) incomplete type, get the full view.
7475 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7476 -- Returns True if and only if either T1 denotes a limited view of T2
7477 -- or T2 denotes a limited view of T1. This can arise when the limited
7478 -- with view of a type is used in a subprogram declaration and the
7479 -- subprogram body is in the scope of a regular with clause for the
7480 -- same unit. In such a case, the two type entities can be considered
7481 -- identical for purposes of conformance checking.
7483 ----------------------
7484 -- Base_Types_Match --
7485 ----------------------
7487 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7488 BT1 : constant Entity_Id := Base_Type (T1);
7489 BT2 : constant Entity_Id := Base_Type (T2);
7495 elsif BT1 = BT2 then
7497 -- The following is too permissive. A more precise test should
7498 -- check that the generic actual is an ancestor subtype of the
7501 -- See code in Find_Corresponding_Spec that applies an additional
7502 -- filter to handle accidental amiguities in instances.
7504 return not Is_Generic_Actual_Type (T1)
7505 or else not Is_Generic_Actual_Type (T2)
7506 or else Scope (T1) /= Scope (T2);
7508 -- If T2 is a generic actual type it is declared as the subtype of
7509 -- the actual. If that actual is itself a subtype we need to use its
7510 -- own base type to check for compatibility.
7512 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7515 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7521 end Base_Types_Match;
7523 --------------------------
7524 -- Find_Designated_Type --
7525 --------------------------
7527 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7531 Desig := Directly_Designated_Type (T);
7533 if Ekind (Desig) = E_Incomplete_Type then
7535 -- If regular incomplete type, get full view if available
7537 if Present (Full_View (Desig)) then
7538 Desig := Full_View (Desig);
7540 -- If limited view of a type, get non-limited view if available,
7541 -- and check again for a regular incomplete type.
7543 elsif Present (Non_Limited_View (Desig)) then
7544 Desig := Get_Full_View (Non_Limited_View (Desig));
7549 end Find_Designated_Type;
7551 -------------------------------
7552 -- Matches_Limited_With_View --
7553 -------------------------------
7555 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7557 -- In some cases a type imported through a limited_with clause, and
7558 -- its nonlimited view are both visible, for example in an anonymous
7559 -- access-to-class-wide type in a formal. Both entities designate the
7562 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7565 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7568 elsif From_Limited_With (T1)
7569 and then From_Limited_With (T2)
7570 and then Available_View (T1) = Available_View (T2)
7577 end Matches_Limited_With_View;
7579 -- Start of processing for Conforming_Types
7582 -- The context is an instance association for a formal access-to-
7583 -- subprogram type; the formal parameter types require mapping because
7584 -- they may denote other formal parameters of the generic unit.
7587 Type_1 := Get_Instance_Of (T1);
7588 Type_2 := Get_Instance_Of (T2);
7591 -- If one of the types is a view of the other introduced by a limited
7592 -- with clause, treat these as conforming for all purposes.
7594 if Matches_Limited_With_View (T1, T2) then
7597 elsif Base_Types_Match (Type_1, Type_2) then
7598 return Ctype <= Mode_Conformant
7599 or else Subtypes_Statically_Match (Type_1, Type_2);
7601 elsif Is_Incomplete_Or_Private_Type (Type_1)
7602 and then Present (Full_View (Type_1))
7603 and then Base_Types_Match (Full_View (Type_1), Type_2)
7605 return Ctype <= Mode_Conformant
7606 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7608 elsif Ekind (Type_2) = E_Incomplete_Type
7609 and then Present (Full_View (Type_2))
7610 and then Base_Types_Match (Type_1, Full_View (Type_2))
7612 return Ctype <= Mode_Conformant
7613 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7615 elsif Is_Private_Type (Type_2)
7616 and then In_Instance
7617 and then Present (Full_View (Type_2))
7618 and then Base_Types_Match (Type_1, Full_View (Type_2))
7620 return Ctype <= Mode_Conformant
7621 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7624 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7625 -- treated recursively because they carry a signature. As far as
7626 -- conformance is concerned, convention plays no role, and either
7627 -- or both could be access to protected subprograms.
7629 Are_Anonymous_Access_To_Subprogram_Types :=
7630 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7631 E_Anonymous_Access_Protected_Subprogram_Type)
7633 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7634 E_Anonymous_Access_Protected_Subprogram_Type);
7636 -- Test anonymous access type case. For this case, static subtype
7637 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7638 -- the base types because we may have built internal subtype entities
7639 -- to handle null-excluding types (see Process_Formals).
7641 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7643 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7645 -- Ada 2005 (AI-254)
7647 or else Are_Anonymous_Access_To_Subprogram_Types
7650 Desig_1 : Entity_Id;
7651 Desig_2 : Entity_Id;
7654 -- In Ada 2005, access constant indicators must match for
7655 -- subtype conformance.
7657 if Ada_Version >= Ada_2005
7658 and then Ctype >= Subtype_Conformant
7660 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7665 Desig_1 := Find_Designated_Type (Type_1);
7666 Desig_2 := Find_Designated_Type (Type_2);
7668 -- If the context is an instance association for a formal
7669 -- access-to-subprogram type; formal access parameter designated
7670 -- types require mapping because they may denote other formal
7671 -- parameters of the generic unit.
7674 Desig_1 := Get_Instance_Of (Desig_1);
7675 Desig_2 := Get_Instance_Of (Desig_2);
7678 -- It is possible for a Class_Wide_Type to be introduced for an
7679 -- incomplete type, in which case there is a separate class_ wide
7680 -- type for the full view. The types conform if their Etypes
7681 -- conform, i.e. one may be the full view of the other. This can
7682 -- only happen in the context of an access parameter, other uses
7683 -- of an incomplete Class_Wide_Type are illegal.
7685 if Is_Class_Wide_Type (Desig_1)
7687 Is_Class_Wide_Type (Desig_2)
7691 (Etype (Base_Type (Desig_1)),
7692 Etype (Base_Type (Desig_2)), Ctype);
7694 elsif Are_Anonymous_Access_To_Subprogram_Types then
7695 if Ada_Version < Ada_2005 then
7696 return Ctype = Type_Conformant
7698 Subtypes_Statically_Match (Desig_1, Desig_2);
7700 -- We must check the conformance of the signatures themselves
7704 Conformant : Boolean;
7707 (Desig_1, Desig_2, Ctype, False, Conformant);
7713 return Base_Type (Desig_1) = Base_Type (Desig_2)
7714 and then (Ctype = Type_Conformant
7716 Subtypes_Statically_Match (Desig_1, Desig_2));
7720 -- Otherwise definitely no match
7723 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7724 and then Is_Access_Type (Type_2))
7725 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7726 and then Is_Access_Type (Type_1)))
7729 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7731 May_Hide_Profile := True;
7736 end Conforming_Types;
7738 --------------------------
7739 -- Create_Extra_Formals --
7740 --------------------------
7742 procedure Create_Extra_Formals (E : Entity_Id) is
7744 First_Extra : Entity_Id := Empty;
7745 Last_Extra : Entity_Id;
7746 Formal_Type : Entity_Id;
7747 P_Formal : Entity_Id := Empty;
7749 function Add_Extra_Formal
7750 (Assoc_Entity : Entity_Id;
7753 Suffix : String) return Entity_Id;
7754 -- Add an extra formal to the current list of formals and extra formals.
7755 -- The extra formal is added to the end of the list of extra formals,
7756 -- and also returned as the result. These formals are always of mode IN.
7757 -- The new formal has the type Typ, is declared in Scope, and its name
7758 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7759 -- The following suffixes are currently used. They should not be changed
7760 -- without coordinating with CodePeer, which makes use of these to
7761 -- provide better messages.
7763 -- O denotes the Constrained bit.
7764 -- L denotes the accessibility level.
7765 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7766 -- the full list in exp_ch6.BIP_Formal_Kind.
7768 ----------------------
7769 -- Add_Extra_Formal --
7770 ----------------------
7772 function Add_Extra_Formal
7773 (Assoc_Entity : Entity_Id;
7776 Suffix : String) return Entity_Id
7778 EF : constant Entity_Id :=
7779 Make_Defining_Identifier (Sloc (Assoc_Entity),
7780 Chars => New_External_Name (Chars (Assoc_Entity),
7784 -- A little optimization. Never generate an extra formal for the
7785 -- _init operand of an initialization procedure, since it could
7788 if Chars (Formal) = Name_uInit then
7792 Set_Ekind (EF, E_In_Parameter);
7793 Set_Actual_Subtype (EF, Typ);
7794 Set_Etype (EF, Typ);
7795 Set_Scope (EF, Scope);
7796 Set_Mechanism (EF, Default_Mechanism);
7797 Set_Formal_Validity (EF);
7799 if No (First_Extra) then
7801 Set_Extra_Formals (Scope, First_Extra);
7804 if Present (Last_Extra) then
7805 Set_Extra_Formal (Last_Extra, EF);
7811 end Add_Extra_Formal;
7813 -- Start of processing for Create_Extra_Formals
7816 -- We never generate extra formals if expansion is not active because we
7817 -- don't need them unless we are generating code.
7819 if not Expander_Active then
7823 -- No need to generate extra formals in interface thunks whose target
7824 -- primitive has no extra formals.
7826 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7830 -- If this is a derived subprogram then the subtypes of the parent
7831 -- subprogram's formal parameters will be used to determine the need
7832 -- for extra formals.
7834 if Is_Overloadable (E) and then Present (Alias (E)) then
7835 P_Formal := First_Formal (Alias (E));
7838 Last_Extra := Empty;
7839 Formal := First_Formal (E);
7840 while Present (Formal) loop
7841 Last_Extra := Formal;
7842 Next_Formal (Formal);
7845 -- If Extra_formals were already created, don't do it again. This
7846 -- situation may arise for subprogram types created as part of
7847 -- dispatching calls (see Expand_Dispatching_Call)
7849 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7853 -- If the subprogram is a predefined dispatching subprogram then don't
7854 -- generate any extra constrained or accessibility level formals. In
7855 -- general we suppress these for internal subprograms (by not calling
7856 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7857 -- generated stream attributes do get passed through because extra
7858 -- build-in-place formals are needed in some cases (limited 'Input).
7860 if Is_Predefined_Internal_Operation (E) then
7861 goto Test_For_Func_Result_Extras;
7864 Formal := First_Formal (E);
7865 while Present (Formal) loop
7867 -- Create extra formal for supporting the attribute 'Constrained.
7868 -- The case of a private type view without discriminants also
7869 -- requires the extra formal if the underlying type has defaulted
7872 if Ekind (Formal) /= E_In_Parameter then
7873 if Present (P_Formal) then
7874 Formal_Type := Etype (P_Formal);
7876 Formal_Type := Etype (Formal);
7879 -- Do not produce extra formals for Unchecked_Union parameters.
7880 -- Jump directly to the end of the loop.
7882 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7883 goto Skip_Extra_Formal_Generation;
7886 if not Has_Discriminants (Formal_Type)
7887 and then Ekind (Formal_Type) in Private_Kind
7888 and then Present (Underlying_Type (Formal_Type))
7890 Formal_Type := Underlying_Type (Formal_Type);
7893 -- Suppress the extra formal if formal's subtype is constrained or
7894 -- indefinite, or we're compiling for Ada 2012 and the underlying
7895 -- type is tagged and limited. In Ada 2012, a limited tagged type
7896 -- can have defaulted discriminants, but 'Constrained is required
7897 -- to return True, so the formal is never needed (see AI05-0214).
7898 -- Note that this ensures consistency of calling sequences for
7899 -- dispatching operations when some types in a class have defaults
7900 -- on discriminants and others do not (and requiring the extra
7901 -- formal would introduce distributed overhead).
7903 -- If the type does not have a completion yet, treat as prior to
7904 -- Ada 2012 for consistency.
7906 if Has_Discriminants (Formal_Type)
7907 and then not Is_Constrained (Formal_Type)
7908 and then not Is_Indefinite_Subtype (Formal_Type)
7909 and then (Ada_Version < Ada_2012
7910 or else No (Underlying_Type (Formal_Type))
7912 (Is_Limited_Type (Formal_Type)
7915 (Underlying_Type (Formal_Type)))))
7917 Set_Extra_Constrained
7918 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7922 -- Create extra formal for supporting accessibility checking. This
7923 -- is done for both anonymous access formals and formals of named
7924 -- access types that are marked as controlling formals. The latter
7925 -- case can occur when Expand_Dispatching_Call creates a subprogram
7926 -- type and substitutes the types of access-to-class-wide actuals
7927 -- for the anonymous access-to-specific-type of controlling formals.
7928 -- Base_Type is applied because in cases where there is a null
7929 -- exclusion the formal may have an access subtype.
7931 -- This is suppressed if we specifically suppress accessibility
7932 -- checks at the package level for either the subprogram, or the
7933 -- package in which it resides. However, we do not suppress it
7934 -- simply if the scope has accessibility checks suppressed, since
7935 -- this could cause trouble when clients are compiled with a
7936 -- different suppression setting. The explicit checks at the
7937 -- package level are safe from this point of view.
7939 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7940 or else (Is_Controlling_Formal (Formal)
7941 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7943 (Explicit_Suppress (E, Accessibility_Check)
7945 Explicit_Suppress (Scope (E), Accessibility_Check))
7948 or else Present (Extra_Accessibility (P_Formal)))
7950 Set_Extra_Accessibility
7951 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7954 -- This label is required when skipping extra formal generation for
7955 -- Unchecked_Union parameters.
7957 <<Skip_Extra_Formal_Generation>>
7959 if Present (P_Formal) then
7960 Next_Formal (P_Formal);
7963 Next_Formal (Formal);
7966 <<Test_For_Func_Result_Extras>>
7968 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7969 -- function call is ... determined by the point of call ...".
7971 if Needs_Result_Accessibility_Level (E) then
7972 Set_Extra_Accessibility_Of_Result
7973 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7976 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7977 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7979 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7981 Result_Subt : constant Entity_Id := Etype (E);
7982 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7983 Formal_Typ : Entity_Id;
7985 Discard : Entity_Id;
7986 pragma Warnings (Off, Discard);
7989 -- In the case of functions with unconstrained result subtypes,
7990 -- add a 4-state formal indicating whether the return object is
7991 -- allocated by the caller (1), or should be allocated by the
7992 -- callee on the secondary stack (2), in the global heap (3), or
7993 -- in a user-defined storage pool (4). For the moment we just use
7994 -- Natural for the type of this formal. Note that this formal
7995 -- isn't usually needed in the case where the result subtype is
7996 -- constrained, but it is needed when the function has a tagged
7997 -- result, because generally such functions can be called in a
7998 -- dispatching context and such calls must be handled like calls
7999 -- to a class-wide function.
8001 if Needs_BIP_Alloc_Form (E) then
8004 (E, Standard_Natural,
8005 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8007 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8008 -- use a user-defined pool. This formal is not added on
8009 -- .NET/JVM/ZFP as those targets do not support pools.
8011 if VM_Target = No_VM
8012 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8016 (E, RTE (RE_Root_Storage_Pool_Ptr),
8017 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8021 -- In the case of functions whose result type needs finalization,
8022 -- add an extra formal which represents the finalization master.
8024 if Needs_BIP_Finalization_Master (E) then
8027 (E, RTE (RE_Finalization_Master_Ptr),
8028 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8031 -- When the result type contains tasks, add two extra formals: the
8032 -- master of the tasks to be created, and the caller's activation
8035 if Has_Task (Full_Subt) then
8038 (E, RTE (RE_Master_Id),
8039 E, BIP_Formal_Suffix (BIP_Task_Master));
8042 (E, RTE (RE_Activation_Chain_Access),
8043 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8046 -- All build-in-place functions get an extra formal that will be
8047 -- passed the address of the return object within the caller.
8050 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8052 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8053 Set_Etype (Formal_Typ, Formal_Typ);
8054 Set_Depends_On_Private
8055 (Formal_Typ, Has_Private_Component (Formal_Typ));
8056 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8057 Set_Is_Access_Constant (Formal_Typ, False);
8059 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8060 -- the designated type comes from the limited view (for back-end
8063 Set_From_Limited_With
8064 (Formal_Typ, From_Limited_With (Result_Subt));
8066 Layout_Type (Formal_Typ);
8070 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8073 end Create_Extra_Formals;
8075 -----------------------------
8076 -- Enter_Overloaded_Entity --
8077 -----------------------------
8079 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8080 E : Entity_Id := Current_Entity_In_Scope (S);
8081 C_E : Entity_Id := Current_Entity (S);
8085 Set_Has_Homonym (E);
8086 Set_Has_Homonym (S);
8089 Set_Is_Immediately_Visible (S);
8090 Set_Scope (S, Current_Scope);
8092 -- Chain new entity if front of homonym in current scope, so that
8093 -- homonyms are contiguous.
8095 if Present (E) and then E /= C_E then
8096 while Homonym (C_E) /= E loop
8097 C_E := Homonym (C_E);
8100 Set_Homonym (C_E, S);
8104 Set_Current_Entity (S);
8109 if Is_Inherited_Operation (S) then
8110 Append_Inherited_Subprogram (S);
8112 Append_Entity (S, Current_Scope);
8115 Set_Public_Status (S);
8117 if Debug_Flag_E then
8118 Write_Str ("New overloaded entity chain: ");
8119 Write_Name (Chars (S));
8122 while Present (E) loop
8123 Write_Str (" "); Write_Int (Int (E));
8130 -- Generate warning for hiding
8133 and then Comes_From_Source (S)
8134 and then In_Extended_Main_Source_Unit (S)
8141 -- Warn unless genuine overloading. Do not emit warning on
8142 -- hiding predefined operators in Standard (these are either an
8143 -- (artifact of our implicit declarations, or simple noise) but
8144 -- keep warning on a operator defined on a local subtype, because
8145 -- of the real danger that different operators may be applied in
8146 -- various parts of the program.
8148 -- Note that if E and S have the same scope, there is never any
8149 -- hiding. Either the two conflict, and the program is illegal,
8150 -- or S is overriding an implicit inherited subprogram.
8152 if Scope (E) /= Scope (S)
8153 and then (not Is_Overloadable (E)
8154 or else Subtype_Conformant (E, S))
8155 and then (Is_Immediately_Visible (E)
8157 Is_Potentially_Use_Visible (S))
8159 if Scope (E) /= Standard_Standard then
8160 Error_Msg_Sloc := Sloc (E);
8161 Error_Msg_N ("declaration of & hides one#?h?", S);
8163 elsif Nkind (S) = N_Defining_Operator_Symbol
8165 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8168 ("declaration of & hides predefined operator?h?", S);
8173 end Enter_Overloaded_Entity;
8175 -----------------------------
8176 -- Check_Untagged_Equality --
8177 -----------------------------
8179 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8180 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8181 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8185 if Nkind (Decl) = N_Subprogram_Declaration
8186 and then Is_Record_Type (Typ)
8187 and then not Is_Tagged_Type (Typ)
8189 -- If the type is not declared in a package, or if we are in the
8190 -- body of the package or in some other scope, the new operation is
8191 -- not primitive, and therefore legal, though suspicious. If the
8192 -- type is a generic actual (sub)type, the operation is not primitive
8193 -- either because the base type is declared elsewhere.
8195 if Is_Frozen (Typ) then
8196 if Ekind (Scope (Typ)) /= E_Package
8197 or else Scope (Typ) /= Current_Scope
8201 elsif Is_Generic_Actual_Type (Typ) then
8204 elsif In_Package_Body (Scope (Typ)) then
8206 ("equality operator must be declared "
8207 & "before type& is frozen", Eq_Op, Typ);
8209 ("\move declaration to package spec", Eq_Op);
8213 ("equality operator must be declared "
8214 & "before type& is frozen", Eq_Op, Typ);
8216 Obj_Decl := Next (Parent (Typ));
8217 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8218 if Nkind (Obj_Decl) = N_Object_Declaration
8219 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8222 ("type& is frozen by declaration??", Obj_Decl, Typ);
8224 ("\an equality operator cannot be declared after this "
8225 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8233 elsif not In_Same_List (Parent (Typ), Decl)
8234 and then not Is_Limited_Type (Typ)
8237 -- This makes it illegal to have a primitive equality declared in
8238 -- the private part if the type is visible.
8240 Error_Msg_N ("equality operator appears too late", Eq_Op);
8243 end Check_Untagged_Equality;
8245 -----------------------------
8246 -- Find_Corresponding_Spec --
8247 -----------------------------
8249 function Find_Corresponding_Spec
8251 Post_Error : Boolean := True) return Entity_Id
8253 Spec : constant Node_Id := Specification (N);
8254 Designator : constant Entity_Id := Defining_Entity (Spec);
8258 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8259 -- Even if fully conformant, a body may depend on a generic actual when
8260 -- the spec does not, or vice versa, in which case they were distinct
8261 -- entities in the generic.
8263 -------------------------------
8264 -- Different_Generic_Profile --
8265 -------------------------------
8267 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8270 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8271 -- Check that the types of corresponding formals have the same
8272 -- generic actual if any. We have to account for subtypes of a
8273 -- generic formal, declared between a spec and a body, which may
8274 -- appear distinct in an instance but matched in the generic.
8276 -------------------------
8277 -- Same_Generic_Actual --
8278 -------------------------
8280 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8282 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8284 (Present (Parent (T1))
8285 and then Comes_From_Source (Parent (T1))
8286 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8287 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8288 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8289 end Same_Generic_Actual;
8291 -- Start of processing for Different_Generic_Profile
8294 if not In_Instance then
8297 elsif Ekind (E) = E_Function
8298 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8303 F1 := First_Formal (Designator);
8304 F2 := First_Formal (E);
8305 while Present (F1) loop
8306 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8315 end Different_Generic_Profile;
8317 -- Start of processing for Find_Corresponding_Spec
8320 E := Current_Entity (Designator);
8321 while Present (E) loop
8323 -- We are looking for a matching spec. It must have the same scope,
8324 -- and the same name, and either be type conformant, or be the case
8325 -- of a library procedure spec and its body (which belong to one
8326 -- another regardless of whether they are type conformant or not).
8328 if Scope (E) = Current_Scope then
8329 if Current_Scope = Standard_Standard
8330 or else (Ekind (E) = Ekind (Designator)
8331 and then Type_Conformant (E, Designator))
8333 -- Within an instantiation, we know that spec and body are
8334 -- subtype conformant, because they were subtype conformant in
8335 -- the generic. We choose the subtype-conformant entity here as
8336 -- well, to resolve spurious ambiguities in the instance that
8337 -- were not present in the generic (i.e. when two different
8338 -- types are given the same actual). If we are looking for a
8339 -- spec to match a body, full conformance is expected.
8342 Set_Convention (Designator, Convention (E));
8344 -- Skip past subprogram bodies and subprogram renamings that
8345 -- may appear to have a matching spec, but that aren't fully
8346 -- conformant with it. That can occur in cases where an
8347 -- actual type causes unrelated homographs in the instance.
8349 if Nkind_In (N, N_Subprogram_Body,
8350 N_Subprogram_Renaming_Declaration)
8351 and then Present (Homonym (E))
8352 and then not Fully_Conformant (Designator, E)
8356 elsif not Subtype_Conformant (Designator, E) then
8359 elsif Different_Generic_Profile (E) then
8364 -- Ada 2012 (AI05-0165): For internally generated bodies of
8365 -- null procedures locate the internally generated spec. We
8366 -- enforce mode conformance since a tagged type may inherit
8367 -- from interfaces several null primitives which differ only
8368 -- in the mode of the formals.
8370 if not (Comes_From_Source (E))
8371 and then Is_Null_Procedure (E)
8372 and then not Mode_Conformant (Designator, E)
8376 -- For null procedures coming from source that are completions,
8377 -- analysis of the generated body will establish the link.
8379 elsif Comes_From_Source (E)
8380 and then Nkind (Spec) = N_Procedure_Specification
8381 and then Null_Present (Spec)
8385 elsif not Has_Completion (E) then
8386 if Nkind (N) /= N_Subprogram_Body_Stub then
8387 Set_Corresponding_Spec (N, E);
8390 Set_Has_Completion (E);
8393 elsif Nkind (Parent (N)) = N_Subunit then
8395 -- If this is the proper body of a subunit, the completion
8396 -- flag is set when analyzing the stub.
8400 -- If E is an internal function with a controlling result that
8401 -- was created for an operation inherited by a null extension,
8402 -- it may be overridden by a body without a previous spec (one
8403 -- more reason why these should be shunned). In that case we
8404 -- remove the generated body if present, because the current
8405 -- one is the explicit overriding.
8407 elsif Ekind (E) = E_Function
8408 and then Ada_Version >= Ada_2005
8409 and then not Comes_From_Source (E)
8410 and then Has_Controlling_Result (E)
8411 and then Is_Null_Extension (Etype (E))
8412 and then Comes_From_Source (Spec)
8414 Set_Has_Completion (E, False);
8417 and then Nkind (Parent (E)) = N_Function_Specification
8420 (Unit_Declaration_Node
8421 (Corresponding_Body (Unit_Declaration_Node (E))));
8425 -- If expansion is disabled, or if the wrapper function has
8426 -- not been generated yet, this a late body overriding an
8427 -- inherited operation, or it is an overriding by some other
8428 -- declaration before the controlling result is frozen. In
8429 -- either case this is a declaration of a new entity.
8435 -- If the body already exists, then this is an error unless
8436 -- the previous declaration is the implicit declaration of a
8437 -- derived subprogram. It is also legal for an instance to
8438 -- contain type conformant overloadable declarations (but the
8439 -- generic declaration may not), per 8.3(26/2).
8441 elsif No (Alias (E))
8442 and then not Is_Intrinsic_Subprogram (E)
8443 and then not In_Instance
8446 Error_Msg_Sloc := Sloc (E);
8448 if Is_Imported (E) then
8450 ("body not allowed for imported subprogram & declared#",
8453 Error_Msg_NE ("duplicate body for & declared#", N, E);
8457 -- Child units cannot be overloaded, so a conformance mismatch
8458 -- between body and a previous spec is an error.
8460 elsif Is_Child_Unit (E)
8462 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8464 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8469 ("body of child unit does not match previous declaration", N);
8477 -- On exit, we know that no previous declaration of subprogram exists
8480 end Find_Corresponding_Spec;
8482 ----------------------
8483 -- Fully_Conformant --
8484 ----------------------
8486 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8489 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8491 end Fully_Conformant;
8493 ----------------------------------
8494 -- Fully_Conformant_Expressions --
8495 ----------------------------------
8497 function Fully_Conformant_Expressions
8498 (Given_E1 : Node_Id;
8499 Given_E2 : Node_Id) return Boolean
8501 E1 : constant Node_Id := Original_Node (Given_E1);
8502 E2 : constant Node_Id := Original_Node (Given_E2);
8503 -- We always test conformance on original nodes, since it is possible
8504 -- for analysis and/or expansion to make things look as though they
8505 -- conform when they do not, e.g. by converting 1+2 into 3.
8507 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8508 renames Fully_Conformant_Expressions;
8510 function FCL (L1, L2 : List_Id) return Boolean;
8511 -- Compare elements of two lists for conformance. Elements have to be
8512 -- conformant, and actuals inserted as default parameters do not match
8513 -- explicit actuals with the same value.
8515 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8516 -- Compare an operator node with a function call
8522 function FCL (L1, L2 : List_Id) return Boolean is
8526 if L1 = No_List then
8532 if L2 = No_List then
8538 -- Compare two lists, skipping rewrite insertions (we want to compare
8539 -- the original trees, not the expanded versions!)
8542 if Is_Rewrite_Insertion (N1) then
8544 elsif Is_Rewrite_Insertion (N2) then
8550 elsif not FCE (N1, N2) then
8563 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8564 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8569 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8574 Act := First (Actuals);
8576 if Nkind (Op_Node) in N_Binary_Op then
8577 if not FCE (Left_Opnd (Op_Node), Act) then
8584 return Present (Act)
8585 and then FCE (Right_Opnd (Op_Node), Act)
8586 and then No (Next (Act));
8590 -- Start of processing for Fully_Conformant_Expressions
8593 -- Non-conformant if paren count does not match. Note: if some idiot
8594 -- complains that we don't do this right for more than 3 levels of
8595 -- parentheses, they will be treated with the respect they deserve!
8597 if Paren_Count (E1) /= Paren_Count (E2) then
8600 -- If same entities are referenced, then they are conformant even if
8601 -- they have different forms (RM 8.3.1(19-20)).
8603 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8604 if Present (Entity (E1)) then
8605 return Entity (E1) = Entity (E2)
8606 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8607 and then Ekind (Entity (E1)) = E_Discriminant
8608 and then Ekind (Entity (E2)) = E_In_Parameter);
8610 elsif Nkind (E1) = N_Expanded_Name
8611 and then Nkind (E2) = N_Expanded_Name
8612 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8613 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8615 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8618 -- Identifiers in component associations don't always have
8619 -- entities, but their names must conform.
8621 return Nkind (E1) = N_Identifier
8622 and then Nkind (E2) = N_Identifier
8623 and then Chars (E1) = Chars (E2);
8626 elsif Nkind (E1) = N_Character_Literal
8627 and then Nkind (E2) = N_Expanded_Name
8629 return Nkind (Selector_Name (E2)) = N_Character_Literal
8630 and then Chars (E1) = Chars (Selector_Name (E2));
8632 elsif Nkind (E2) = N_Character_Literal
8633 and then Nkind (E1) = N_Expanded_Name
8635 return Nkind (Selector_Name (E1)) = N_Character_Literal
8636 and then Chars (E2) = Chars (Selector_Name (E1));
8638 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8639 return FCO (E1, E2);
8641 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8642 return FCO (E2, E1);
8644 -- Otherwise we must have the same syntactic entity
8646 elsif Nkind (E1) /= Nkind (E2) then
8649 -- At this point, we specialize by node type
8656 FCL (Expressions (E1), Expressions (E2))
8658 FCL (Component_Associations (E1),
8659 Component_Associations (E2));
8662 if Nkind (Expression (E1)) = N_Qualified_Expression
8664 Nkind (Expression (E2)) = N_Qualified_Expression
8666 return FCE (Expression (E1), Expression (E2));
8668 -- Check that the subtype marks and any constraints
8673 Indic1 : constant Node_Id := Expression (E1);
8674 Indic2 : constant Node_Id := Expression (E2);
8679 if Nkind (Indic1) /= N_Subtype_Indication then
8681 Nkind (Indic2) /= N_Subtype_Indication
8682 and then Entity (Indic1) = Entity (Indic2);
8684 elsif Nkind (Indic2) /= N_Subtype_Indication then
8686 Nkind (Indic1) /= N_Subtype_Indication
8687 and then Entity (Indic1) = Entity (Indic2);
8690 if Entity (Subtype_Mark (Indic1)) /=
8691 Entity (Subtype_Mark (Indic2))
8696 Elt1 := First (Constraints (Constraint (Indic1)));
8697 Elt2 := First (Constraints (Constraint (Indic2)));
8698 while Present (Elt1) and then Present (Elt2) loop
8699 if not FCE (Elt1, Elt2) then
8712 when N_Attribute_Reference =>
8714 Attribute_Name (E1) = Attribute_Name (E2)
8715 and then FCL (Expressions (E1), Expressions (E2));
8719 Entity (E1) = Entity (E2)
8720 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8721 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8723 when N_Short_Circuit | N_Membership_Test =>
8725 FCE (Left_Opnd (E1), Left_Opnd (E2))
8727 FCE (Right_Opnd (E1), Right_Opnd (E2));
8729 when N_Case_Expression =>
8735 if not FCE (Expression (E1), Expression (E2)) then
8739 Alt1 := First (Alternatives (E1));
8740 Alt2 := First (Alternatives (E2));
8742 if Present (Alt1) /= Present (Alt2) then
8744 elsif No (Alt1) then
8748 if not FCE (Expression (Alt1), Expression (Alt2))
8749 or else not FCL (Discrete_Choices (Alt1),
8750 Discrete_Choices (Alt2))
8761 when N_Character_Literal =>
8763 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8765 when N_Component_Association =>
8767 FCL (Choices (E1), Choices (E2))
8769 FCE (Expression (E1), Expression (E2));
8771 when N_Explicit_Dereference =>
8773 FCE (Prefix (E1), Prefix (E2));
8775 when N_Extension_Aggregate =>
8777 FCL (Expressions (E1), Expressions (E2))
8778 and then Null_Record_Present (E1) =
8779 Null_Record_Present (E2)
8780 and then FCL (Component_Associations (E1),
8781 Component_Associations (E2));
8783 when N_Function_Call =>
8785 FCE (Name (E1), Name (E2))
8787 FCL (Parameter_Associations (E1),
8788 Parameter_Associations (E2));
8790 when N_If_Expression =>
8792 FCL (Expressions (E1), Expressions (E2));
8794 when N_Indexed_Component =>
8796 FCE (Prefix (E1), Prefix (E2))
8798 FCL (Expressions (E1), Expressions (E2));
8800 when N_Integer_Literal =>
8801 return (Intval (E1) = Intval (E2));
8806 when N_Operator_Symbol =>
8808 Chars (E1) = Chars (E2);
8810 when N_Others_Choice =>
8813 when N_Parameter_Association =>
8815 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8816 and then FCE (Explicit_Actual_Parameter (E1),
8817 Explicit_Actual_Parameter (E2));
8819 when N_Qualified_Expression =>
8821 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8823 FCE (Expression (E1), Expression (E2));
8825 when N_Quantified_Expression =>
8826 if not FCE (Condition (E1), Condition (E2)) then
8830 if Present (Loop_Parameter_Specification (E1))
8831 and then Present (Loop_Parameter_Specification (E2))
8834 L1 : constant Node_Id :=
8835 Loop_Parameter_Specification (E1);
8836 L2 : constant Node_Id :=
8837 Loop_Parameter_Specification (E2);
8841 Reverse_Present (L1) = Reverse_Present (L2)
8843 FCE (Defining_Identifier (L1),
8844 Defining_Identifier (L2))
8846 FCE (Discrete_Subtype_Definition (L1),
8847 Discrete_Subtype_Definition (L2));
8850 elsif Present (Iterator_Specification (E1))
8851 and then Present (Iterator_Specification (E2))
8854 I1 : constant Node_Id := Iterator_Specification (E1);
8855 I2 : constant Node_Id := Iterator_Specification (E2);
8859 FCE (Defining_Identifier (I1),
8860 Defining_Identifier (I2))
8862 Of_Present (I1) = Of_Present (I2)
8864 Reverse_Present (I1) = Reverse_Present (I2)
8865 and then FCE (Name (I1), Name (I2))
8866 and then FCE (Subtype_Indication (I1),
8867 Subtype_Indication (I2));
8870 -- The quantified expressions used different specifications to
8871 -- walk their respective ranges.
8879 FCE (Low_Bound (E1), Low_Bound (E2))
8881 FCE (High_Bound (E1), High_Bound (E2));
8883 when N_Real_Literal =>
8884 return (Realval (E1) = Realval (E2));
8886 when N_Selected_Component =>
8888 FCE (Prefix (E1), Prefix (E2))
8890 FCE (Selector_Name (E1), Selector_Name (E2));
8894 FCE (Prefix (E1), Prefix (E2))
8896 FCE (Discrete_Range (E1), Discrete_Range (E2));
8898 when N_String_Literal =>
8900 S1 : constant String_Id := Strval (E1);
8901 S2 : constant String_Id := Strval (E2);
8902 L1 : constant Nat := String_Length (S1);
8903 L2 : constant Nat := String_Length (S2);
8910 for J in 1 .. L1 loop
8911 if Get_String_Char (S1, J) /=
8912 Get_String_Char (S2, J)
8922 when N_Type_Conversion =>
8924 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8926 FCE (Expression (E1), Expression (E2));
8930 Entity (E1) = Entity (E2)
8932 FCE (Right_Opnd (E1), Right_Opnd (E2));
8934 when N_Unchecked_Type_Conversion =>
8936 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8938 FCE (Expression (E1), Expression (E2));
8940 -- All other node types cannot appear in this context. Strictly
8941 -- we should raise a fatal internal error. Instead we just ignore
8942 -- the nodes. This means that if anyone makes a mistake in the
8943 -- expander and mucks an expression tree irretrievably, the result
8944 -- will be a failure to detect a (probably very obscure) case
8945 -- of non-conformance, which is better than bombing on some
8946 -- case where two expressions do in fact conform.
8953 end Fully_Conformant_Expressions;
8955 ----------------------------------------
8956 -- Fully_Conformant_Discrete_Subtypes --
8957 ----------------------------------------
8959 function Fully_Conformant_Discrete_Subtypes
8960 (Given_S1 : Node_Id;
8961 Given_S2 : Node_Id) return Boolean
8963 S1 : constant Node_Id := Original_Node (Given_S1);
8964 S2 : constant Node_Id := Original_Node (Given_S2);
8966 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8967 -- Special-case for a bound given by a discriminant, which in the body
8968 -- is replaced with the discriminal of the enclosing type.
8970 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8971 -- Check both bounds
8973 -----------------------
8974 -- Conforming_Bounds --
8975 -----------------------
8977 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8979 if Is_Entity_Name (B1)
8980 and then Is_Entity_Name (B2)
8981 and then Ekind (Entity (B1)) = E_Discriminant
8983 return Chars (B1) = Chars (B2);
8986 return Fully_Conformant_Expressions (B1, B2);
8988 end Conforming_Bounds;
8990 -----------------------
8991 -- Conforming_Ranges --
8992 -----------------------
8994 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8997 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8999 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9000 end Conforming_Ranges;
9002 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9005 if Nkind (S1) /= Nkind (S2) then
9008 elsif Is_Entity_Name (S1) then
9009 return Entity (S1) = Entity (S2);
9011 elsif Nkind (S1) = N_Range then
9012 return Conforming_Ranges (S1, S2);
9014 elsif Nkind (S1) = N_Subtype_Indication then
9016 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9019 (Range_Expression (Constraint (S1)),
9020 Range_Expression (Constraint (S2)));
9024 end Fully_Conformant_Discrete_Subtypes;
9026 --------------------
9027 -- Install_Entity --
9028 --------------------
9030 procedure Install_Entity (E : Entity_Id) is
9031 Prev : constant Entity_Id := Current_Entity (E);
9033 Set_Is_Immediately_Visible (E);
9034 Set_Current_Entity (E);
9035 Set_Homonym (E, Prev);
9038 ---------------------
9039 -- Install_Formals --
9040 ---------------------
9042 procedure Install_Formals (Id : Entity_Id) is
9045 F := First_Formal (Id);
9046 while Present (F) loop
9050 end Install_Formals;
9052 -----------------------------
9053 -- Is_Interface_Conformant --
9054 -----------------------------
9056 function Is_Interface_Conformant
9057 (Tagged_Type : Entity_Id;
9058 Iface_Prim : Entity_Id;
9059 Prim : Entity_Id) return Boolean
9061 -- The operation may in fact be an inherited (implicit) operation
9062 -- rather than the original interface primitive, so retrieve the
9063 -- ultimate ancestor.
9065 Iface : constant Entity_Id :=
9066 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9067 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9069 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9070 -- Return the controlling formal of Prim
9072 ------------------------
9073 -- Controlling_Formal --
9074 ------------------------
9076 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9080 E := First_Entity (Prim);
9081 while Present (E) loop
9082 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9090 end Controlling_Formal;
9094 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9095 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9097 -- Start of processing for Is_Interface_Conformant
9100 pragma Assert (Is_Subprogram (Iface_Prim)
9101 and then Is_Subprogram (Prim)
9102 and then Is_Dispatching_Operation (Iface_Prim)
9103 and then Is_Dispatching_Operation (Prim));
9105 pragma Assert (Is_Interface (Iface)
9106 or else (Present (Alias (Iface_Prim))
9109 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9111 if Prim = Iface_Prim
9112 or else not Is_Subprogram (Prim)
9113 or else Ekind (Prim) /= Ekind (Iface_Prim)
9114 or else not Is_Dispatching_Operation (Prim)
9115 or else Scope (Prim) /= Scope (Tagged_Type)
9117 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9118 or else not Primitive_Names_Match (Iface_Prim, Prim)
9122 -- The mode of the controlling formals must match
9124 elsif Present (Iface_Ctrl_F)
9125 and then Present (Prim_Ctrl_F)
9126 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9130 -- Case of a procedure, or a function whose result type matches the
9131 -- result type of the interface primitive, or a function that has no
9132 -- controlling result (I or access I).
9134 elsif Ekind (Iface_Prim) = E_Procedure
9135 or else Etype (Prim) = Etype (Iface_Prim)
9136 or else not Has_Controlling_Result (Prim)
9138 return Type_Conformant
9139 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9141 -- Case of a function returning an interface, or an access to one. Check
9142 -- that the return types correspond.
9144 elsif Implements_Interface (Typ, Iface) then
9145 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9147 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9152 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9153 Skip_Controlling_Formals => True);
9159 end Is_Interface_Conformant;
9161 ---------------------------------
9162 -- Is_Non_Overriding_Operation --
9163 ---------------------------------
9165 function Is_Non_Overriding_Operation
9166 (Prev_E : Entity_Id;
9167 New_E : Entity_Id) return Boolean
9171 G_Typ : Entity_Id := Empty;
9173 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9174 -- If F_Type is a derived type associated with a generic actual subtype,
9175 -- then return its Generic_Parent_Type attribute, else return Empty.
9177 function Types_Correspond
9178 (P_Type : Entity_Id;
9179 N_Type : Entity_Id) return Boolean;
9180 -- Returns true if and only if the types (or designated types in the
9181 -- case of anonymous access types) are the same or N_Type is derived
9182 -- directly or indirectly from P_Type.
9184 -----------------------------
9185 -- Get_Generic_Parent_Type --
9186 -----------------------------
9188 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9194 if Is_Derived_Type (F_Typ)
9195 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9197 -- The tree must be traversed to determine the parent subtype in
9198 -- the generic unit, which unfortunately isn't always available
9199 -- via semantic attributes. ??? (Note: The use of Original_Node
9200 -- is needed for cases where a full derived type has been
9203 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9204 if Nkind (Defn) = N_Derived_Type_Definition then
9205 Indic := Subtype_Indication (Defn);
9207 if Nkind (Indic) = N_Subtype_Indication then
9208 G_Typ := Entity (Subtype_Mark (Indic));
9210 G_Typ := Entity (Indic);
9213 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9214 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9216 return Generic_Parent_Type (Parent (G_Typ));
9222 end Get_Generic_Parent_Type;
9224 ----------------------
9225 -- Types_Correspond --
9226 ----------------------
9228 function Types_Correspond
9229 (P_Type : Entity_Id;
9230 N_Type : Entity_Id) return Boolean
9232 Prev_Type : Entity_Id := Base_Type (P_Type);
9233 New_Type : Entity_Id := Base_Type (N_Type);
9236 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9237 Prev_Type := Designated_Type (Prev_Type);
9240 if Ekind (New_Type) = E_Anonymous_Access_Type then
9241 New_Type := Designated_Type (New_Type);
9244 if Prev_Type = New_Type then
9247 elsif not Is_Class_Wide_Type (New_Type) then
9248 while Etype (New_Type) /= New_Type loop
9249 New_Type := Etype (New_Type);
9250 if New_Type = Prev_Type then
9256 end Types_Correspond;
9258 -- Start of processing for Is_Non_Overriding_Operation
9261 -- In the case where both operations are implicit derived subprograms
9262 -- then neither overrides the other. This can only occur in certain
9263 -- obscure cases (e.g., derivation from homographs created in a generic
9266 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9269 elsif Ekind (Current_Scope) = E_Package
9270 and then Is_Generic_Instance (Current_Scope)
9271 and then In_Private_Part (Current_Scope)
9272 and then Comes_From_Source (New_E)
9274 -- We examine the formals and result type of the inherited operation,
9275 -- to determine whether their type is derived from (the instance of)
9276 -- a generic type. The first such formal or result type is the one
9279 Formal := First_Formal (Prev_E);
9280 while Present (Formal) loop
9281 F_Typ := Base_Type (Etype (Formal));
9283 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9284 F_Typ := Designated_Type (F_Typ);
9287 G_Typ := Get_Generic_Parent_Type (F_Typ);
9288 exit when Present (G_Typ);
9290 Next_Formal (Formal);
9293 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9294 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9301 -- If the generic type is a private type, then the original operation
9302 -- was not overriding in the generic, because there was no primitive
9303 -- operation to override.
9305 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9306 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9307 N_Formal_Private_Type_Definition
9311 -- The generic parent type is the ancestor of a formal derived
9312 -- type declaration. We need to check whether it has a primitive
9313 -- operation that should be overridden by New_E in the generic.
9317 P_Formal : Entity_Id;
9318 N_Formal : Entity_Id;
9322 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9325 while Present (Prim_Elt) loop
9326 P_Prim := Node (Prim_Elt);
9328 if Chars (P_Prim) = Chars (New_E)
9329 and then Ekind (P_Prim) = Ekind (New_E)
9331 P_Formal := First_Formal (P_Prim);
9332 N_Formal := First_Formal (New_E);
9333 while Present (P_Formal) and then Present (N_Formal) loop
9334 P_Typ := Etype (P_Formal);
9335 N_Typ := Etype (N_Formal);
9337 if not Types_Correspond (P_Typ, N_Typ) then
9341 Next_Entity (P_Formal);
9342 Next_Entity (N_Formal);
9345 -- Found a matching primitive operation belonging to the
9346 -- formal ancestor type, so the new subprogram is
9350 and then No (N_Formal)
9351 and then (Ekind (New_E) /= E_Function
9354 (Etype (P_Prim), Etype (New_E)))
9360 Next_Elmt (Prim_Elt);
9363 -- If no match found, then the new subprogram does not override
9364 -- in the generic (nor in the instance).
9366 -- If the type in question is not abstract, and the subprogram
9367 -- is, this will be an error if the new operation is in the
9368 -- private part of the instance. Emit a warning now, which will
9369 -- make the subsequent error message easier to understand.
9371 if not Is_Abstract_Type (F_Typ)
9372 and then Is_Abstract_Subprogram (Prev_E)
9373 and then In_Private_Part (Current_Scope)
9375 Error_Msg_Node_2 := F_Typ;
9377 ("private operation& in generic unit does not override " &
9378 "any primitive operation of& (RM 12.3 (18))??",
9388 end Is_Non_Overriding_Operation;
9390 -------------------------------------
9391 -- List_Inherited_Pre_Post_Aspects --
9392 -------------------------------------
9394 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9396 if Opt.List_Inherited_Aspects
9397 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9400 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9404 for J in Inherited'Range loop
9405 P := Pre_Post_Conditions (Contract (Inherited (J)));
9406 while Present (P) loop
9407 Error_Msg_Sloc := Sloc (P);
9409 if Class_Present (P) and then not Split_PPC (P) then
9410 if Pragma_Name (P) = Name_Precondition then
9412 ("info: & inherits `Pre''Class` aspect from #?L?",
9416 ("info: & inherits `Post''Class` aspect from #?L?",
9421 P := Next_Pragma (P);
9426 end List_Inherited_Pre_Post_Aspects;
9428 ------------------------------
9429 -- Make_Inequality_Operator --
9430 ------------------------------
9432 -- S is the defining identifier of an equality operator. We build a
9433 -- subprogram declaration with the right signature. This operation is
9434 -- intrinsic, because it is always expanded as the negation of the
9435 -- call to the equality function.
9437 procedure Make_Inequality_Operator (S : Entity_Id) is
9438 Loc : constant Source_Ptr := Sloc (S);
9441 Op_Name : Entity_Id;
9443 FF : constant Entity_Id := First_Formal (S);
9444 NF : constant Entity_Id := Next_Formal (FF);
9447 -- Check that equality was properly defined, ignore call if not
9454 A : constant Entity_Id :=
9455 Make_Defining_Identifier (Sloc (FF),
9456 Chars => Chars (FF));
9458 B : constant Entity_Id :=
9459 Make_Defining_Identifier (Sloc (NF),
9460 Chars => Chars (NF));
9463 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9465 Formals := New_List (
9466 Make_Parameter_Specification (Loc,
9467 Defining_Identifier => A,
9469 New_Reference_To (Etype (First_Formal (S)),
9470 Sloc (Etype (First_Formal (S))))),
9472 Make_Parameter_Specification (Loc,
9473 Defining_Identifier => B,
9475 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9476 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9479 Make_Subprogram_Declaration (Loc,
9481 Make_Function_Specification (Loc,
9482 Defining_Unit_Name => Op_Name,
9483 Parameter_Specifications => Formals,
9484 Result_Definition =>
9485 New_Reference_To (Standard_Boolean, Loc)));
9487 -- Insert inequality right after equality if it is explicit or after
9488 -- the derived type when implicit. These entities are created only
9489 -- for visibility purposes, and eventually replaced in the course
9490 -- of expansion, so they do not need to be attached to the tree and
9491 -- seen by the back-end. Keeping them internal also avoids spurious
9492 -- freezing problems. The declaration is inserted in the tree for
9493 -- analysis, and removed afterwards. If the equality operator comes
9494 -- from an explicit declaration, attach the inequality immediately
9495 -- after. Else the equality is inherited from a derived type
9496 -- declaration, so insert inequality after that declaration.
9498 if No (Alias (S)) then
9499 Insert_After (Unit_Declaration_Node (S), Decl);
9500 elsif Is_List_Member (Parent (S)) then
9501 Insert_After (Parent (S), Decl);
9503 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9506 Mark_Rewrite_Insertion (Decl);
9507 Set_Is_Intrinsic_Subprogram (Op_Name);
9510 Set_Has_Completion (Op_Name);
9511 Set_Corresponding_Equality (Op_Name, S);
9512 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9514 end Make_Inequality_Operator;
9516 ----------------------
9517 -- May_Need_Actuals --
9518 ----------------------
9520 procedure May_Need_Actuals (Fun : Entity_Id) is
9525 F := First_Formal (Fun);
9527 while Present (F) loop
9528 if No (Default_Value (F)) then
9536 Set_Needs_No_Actuals (Fun, B);
9537 end May_Need_Actuals;
9539 ---------------------
9540 -- Mode_Conformant --
9541 ---------------------
9543 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9546 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9548 end Mode_Conformant;
9550 ---------------------------
9551 -- New_Overloaded_Entity --
9552 ---------------------------
9554 procedure New_Overloaded_Entity
9556 Derived_Type : Entity_Id := Empty)
9558 Overridden_Subp : Entity_Id := Empty;
9559 -- Set if the current scope has an operation that is type-conformant
9560 -- with S, and becomes hidden by S.
9562 Is_Primitive_Subp : Boolean;
9563 -- Set to True if the new subprogram is primitive
9566 -- Entity that S overrides
9568 Prev_Vis : Entity_Id := Empty;
9569 -- Predecessor of E in Homonym chain
9571 procedure Check_For_Primitive_Subprogram
9572 (Is_Primitive : out Boolean;
9573 Is_Overriding : Boolean := False);
9574 -- If the subprogram being analyzed is a primitive operation of the type
9575 -- of a formal or result, set the Has_Primitive_Operations flag on the
9576 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9577 -- corresponding flag on the entity itself for later use.
9579 procedure Check_Synchronized_Overriding
9580 (Def_Id : Entity_Id;
9581 Overridden_Subp : out Entity_Id);
9582 -- First determine if Def_Id is an entry or a subprogram either defined
9583 -- in the scope of a task or protected type, or is a primitive of such
9584 -- a type. Check whether Def_Id overrides a subprogram of an interface
9585 -- implemented by the synchronized type, return the overridden entity
9588 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9589 -- Check that E is declared in the private part of the current package,
9590 -- or in the package body, where it may hide a previous declaration.
9591 -- We can't use In_Private_Part by itself because this flag is also
9592 -- set when freezing entities, so we must examine the place of the
9593 -- declaration in the tree, and recognize wrapper packages as well.
9595 function Is_Overriding_Alias
9597 New_E : Entity_Id) return Boolean;
9598 -- Check whether new subprogram and old subprogram are both inherited
9599 -- from subprograms that have distinct dispatch table entries. This can
9600 -- occur with derivations from instances with accidental homonyms. The
9601 -- function is conservative given that the converse is only true within
9602 -- instances that contain accidental overloadings.
9604 ------------------------------------
9605 -- Check_For_Primitive_Subprogram --
9606 ------------------------------------
9608 procedure Check_For_Primitive_Subprogram
9609 (Is_Primitive : out Boolean;
9610 Is_Overriding : Boolean := False)
9616 function Visible_Part_Type (T : Entity_Id) return Boolean;
9617 -- Returns true if T is declared in the visible part of the current
9618 -- package scope; otherwise returns false. Assumes that T is declared
9621 procedure Check_Private_Overriding (T : Entity_Id);
9622 -- Checks that if a primitive abstract subprogram of a visible
9623 -- abstract type is declared in a private part, then it must override
9624 -- an abstract subprogram declared in the visible part. Also checks
9625 -- that if a primitive function with a controlling result is declared
9626 -- in a private part, then it must override a function declared in
9627 -- the visible part.
9629 ------------------------------
9630 -- Check_Private_Overriding --
9631 ------------------------------
9633 procedure Check_Private_Overriding (T : Entity_Id) is
9635 if Is_Package_Or_Generic_Package (Current_Scope)
9636 and then In_Private_Part (Current_Scope)
9637 and then Visible_Part_Type (T)
9638 and then not In_Instance
9640 if Is_Abstract_Type (T)
9641 and then Is_Abstract_Subprogram (S)
9642 and then (not Is_Overriding
9643 or else not Is_Abstract_Subprogram (E))
9646 ("abstract subprograms must be visible "
9647 & "(RM 3.9.3(10))!", S);
9649 elsif Ekind (S) = E_Function and then not Is_Overriding then
9650 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9652 ("private function with tagged result must"
9653 & " override visible-part function", S);
9655 ("\move subprogram to the visible part"
9656 & " (RM 3.9.3(10))", S);
9658 -- AI05-0073: extend this test to the case of a function
9659 -- with a controlling access result.
9661 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9662 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9664 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9665 and then Ada_Version >= Ada_2012
9668 ("private function with controlling access result "
9669 & "must override visible-part function", S);
9671 ("\move subprogram to the visible part"
9672 & " (RM 3.9.3(10))", S);
9676 end Check_Private_Overriding;
9678 -----------------------
9679 -- Visible_Part_Type --
9680 -----------------------
9682 function Visible_Part_Type (T : Entity_Id) return Boolean is
9683 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9687 -- If the entity is a private type, then it must be declared in a
9690 if Ekind (T) in Private_Kind then
9694 -- Otherwise, we traverse the visible part looking for its
9695 -- corresponding declaration. We cannot use the declaration
9696 -- node directly because in the private part the entity of a
9697 -- private type is the one in the full view, which does not
9698 -- indicate that it is the completion of something visible.
9700 N := First (Visible_Declarations (Specification (P)));
9701 while Present (N) loop
9702 if Nkind (N) = N_Full_Type_Declaration
9703 and then Present (Defining_Identifier (N))
9704 and then T = Defining_Identifier (N)
9708 elsif Nkind_In (N, N_Private_Type_Declaration,
9709 N_Private_Extension_Declaration)
9710 and then Present (Defining_Identifier (N))
9711 and then T = Full_View (Defining_Identifier (N))
9720 end Visible_Part_Type;
9722 -- Start of processing for Check_For_Primitive_Subprogram
9725 Is_Primitive := False;
9727 if not Comes_From_Source (S) then
9730 -- If subprogram is at library level, it is not primitive operation
9732 elsif Current_Scope = Standard_Standard then
9735 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9736 and then not In_Package_Body (Current_Scope))
9737 or else Is_Overriding
9739 -- For function, check return type
9741 if Ekind (S) = E_Function then
9742 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9743 F_Typ := Designated_Type (Etype (S));
9748 B_Typ := Base_Type (F_Typ);
9750 if Scope (B_Typ) = Current_Scope
9751 and then not Is_Class_Wide_Type (B_Typ)
9752 and then not Is_Generic_Type (B_Typ)
9754 Is_Primitive := True;
9755 Set_Has_Primitive_Operations (B_Typ);
9756 Set_Is_Primitive (S);
9757 Check_Private_Overriding (B_Typ);
9761 -- For all subprograms, check formals
9763 Formal := First_Formal (S);
9764 while Present (Formal) loop
9765 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9766 F_Typ := Designated_Type (Etype (Formal));
9768 F_Typ := Etype (Formal);
9771 B_Typ := Base_Type (F_Typ);
9773 if Ekind (B_Typ) = E_Access_Subtype then
9774 B_Typ := Base_Type (B_Typ);
9777 if Scope (B_Typ) = Current_Scope
9778 and then not Is_Class_Wide_Type (B_Typ)
9779 and then not Is_Generic_Type (B_Typ)
9781 Is_Primitive := True;
9782 Set_Is_Primitive (S);
9783 Set_Has_Primitive_Operations (B_Typ);
9784 Check_Private_Overriding (B_Typ);
9787 Next_Formal (Formal);
9790 -- Special case: An equality function can be redefined for a type
9791 -- occurring in a declarative part, and won't otherwise be treated as
9792 -- a primitive because it doesn't occur in a package spec and doesn't
9793 -- override an inherited subprogram. It's important that we mark it
9794 -- primitive so it can be returned by Collect_Primitive_Operations
9795 -- and be used in composing the equality operation of later types
9796 -- that have a component of the type.
9798 elsif Chars (S) = Name_Op_Eq
9799 and then Etype (S) = Standard_Boolean
9801 B_Typ := Base_Type (Etype (First_Formal (S)));
9803 if Scope (B_Typ) = Current_Scope
9805 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9806 and then not Is_Limited_Type (B_Typ)
9808 Is_Primitive := True;
9809 Set_Is_Primitive (S);
9810 Set_Has_Primitive_Operations (B_Typ);
9811 Check_Private_Overriding (B_Typ);
9814 end Check_For_Primitive_Subprogram;
9816 -----------------------------------
9817 -- Check_Synchronized_Overriding --
9818 -----------------------------------
9820 procedure Check_Synchronized_Overriding
9821 (Def_Id : Entity_Id;
9822 Overridden_Subp : out Entity_Id)
9824 Ifaces_List : Elist_Id;
9828 function Matches_Prefixed_View_Profile
9829 (Prim_Params : List_Id;
9830 Iface_Params : List_Id) return Boolean;
9831 -- Determine whether a subprogram's parameter profile Prim_Params
9832 -- matches that of a potentially overridden interface subprogram
9833 -- Iface_Params. Also determine if the type of first parameter of
9834 -- Iface_Params is an implemented interface.
9836 -----------------------------------
9837 -- Matches_Prefixed_View_Profile --
9838 -----------------------------------
9840 function Matches_Prefixed_View_Profile
9841 (Prim_Params : List_Id;
9842 Iface_Params : List_Id) return Boolean
9844 Iface_Id : Entity_Id;
9845 Iface_Param : Node_Id;
9846 Iface_Typ : Entity_Id;
9847 Prim_Id : Entity_Id;
9848 Prim_Param : Node_Id;
9849 Prim_Typ : Entity_Id;
9851 function Is_Implemented
9852 (Ifaces_List : Elist_Id;
9853 Iface : Entity_Id) return Boolean;
9854 -- Determine if Iface is implemented by the current task or
9857 --------------------
9858 -- Is_Implemented --
9859 --------------------
9861 function Is_Implemented
9862 (Ifaces_List : Elist_Id;
9863 Iface : Entity_Id) return Boolean
9865 Iface_Elmt : Elmt_Id;
9868 Iface_Elmt := First_Elmt (Ifaces_List);
9869 while Present (Iface_Elmt) loop
9870 if Node (Iface_Elmt) = Iface then
9874 Next_Elmt (Iface_Elmt);
9880 -- Start of processing for Matches_Prefixed_View_Profile
9883 Iface_Param := First (Iface_Params);
9884 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9886 if Is_Access_Type (Iface_Typ) then
9887 Iface_Typ := Designated_Type (Iface_Typ);
9890 Prim_Param := First (Prim_Params);
9892 -- The first parameter of the potentially overridden subprogram
9893 -- must be an interface implemented by Prim.
9895 if not Is_Interface (Iface_Typ)
9896 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9901 -- The checks on the object parameters are done, move onto the
9902 -- rest of the parameters.
9904 if not In_Scope then
9905 Prim_Param := Next (Prim_Param);
9908 Iface_Param := Next (Iface_Param);
9909 while Present (Iface_Param) and then Present (Prim_Param) loop
9910 Iface_Id := Defining_Identifier (Iface_Param);
9911 Iface_Typ := Find_Parameter_Type (Iface_Param);
9913 Prim_Id := Defining_Identifier (Prim_Param);
9914 Prim_Typ := Find_Parameter_Type (Prim_Param);
9916 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9917 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9918 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9920 Iface_Typ := Designated_Type (Iface_Typ);
9921 Prim_Typ := Designated_Type (Prim_Typ);
9924 -- Case of multiple interface types inside a parameter profile
9926 -- (Obj_Param : in out Iface; ...; Param : Iface)
9928 -- If the interface type is implemented, then the matching type
9929 -- in the primitive should be the implementing record type.
9931 if Ekind (Iface_Typ) = E_Record_Type
9932 and then Is_Interface (Iface_Typ)
9933 and then Is_Implemented (Ifaces_List, Iface_Typ)
9935 if Prim_Typ /= Typ then
9939 -- The two parameters must be both mode and subtype conformant
9941 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9943 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9952 -- One of the two lists contains more parameters than the other
9954 if Present (Iface_Param) or else Present (Prim_Param) then
9959 end Matches_Prefixed_View_Profile;
9961 -- Start of processing for Check_Synchronized_Overriding
9964 Overridden_Subp := Empty;
9966 -- Def_Id must be an entry or a subprogram. We should skip predefined
9967 -- primitives internally generated by the frontend; however at this
9968 -- stage predefined primitives are still not fully decorated. As a
9969 -- minor optimization we skip here internally generated subprograms.
9971 if (Ekind (Def_Id) /= E_Entry
9972 and then Ekind (Def_Id) /= E_Function
9973 and then Ekind (Def_Id) /= E_Procedure)
9974 or else not Comes_From_Source (Def_Id)
9979 -- Search for the concurrent declaration since it contains the list
9980 -- of all implemented interfaces. In this case, the subprogram is
9981 -- declared within the scope of a protected or a task type.
9983 if Present (Scope (Def_Id))
9984 and then Is_Concurrent_Type (Scope (Def_Id))
9985 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9987 Typ := Scope (Def_Id);
9990 -- The enclosing scope is not a synchronized type and the subprogram
9993 elsif No (First_Formal (Def_Id)) then
9996 -- The subprogram has formals and hence it may be a primitive of a
10000 Typ := Etype (First_Formal (Def_Id));
10002 if Is_Access_Type (Typ) then
10003 Typ := Directly_Designated_Type (Typ);
10006 if Is_Concurrent_Type (Typ)
10007 and then not Is_Generic_Actual_Type (Typ)
10011 -- This case occurs when the concurrent type is declared within
10012 -- a generic unit. As a result the corresponding record has been
10013 -- built and used as the type of the first formal, we just have
10014 -- to retrieve the corresponding concurrent type.
10016 elsif Is_Concurrent_Record_Type (Typ)
10017 and then not Is_Class_Wide_Type (Typ)
10018 and then Present (Corresponding_Concurrent_Type (Typ))
10020 Typ := Corresponding_Concurrent_Type (Typ);
10028 -- There is no overriding to check if is an inherited operation in a
10029 -- type derivation on for a generic actual.
10031 Collect_Interfaces (Typ, Ifaces_List);
10033 if Is_Empty_Elmt_List (Ifaces_List) then
10037 -- Determine whether entry or subprogram Def_Id overrides a primitive
10038 -- operation that belongs to one of the interfaces in Ifaces_List.
10041 Candidate : Entity_Id := Empty;
10042 Hom : Entity_Id := Empty;
10043 Iface_Typ : Entity_Id;
10044 Subp : Entity_Id := Empty;
10047 -- Traverse the homonym chain, looking for a potentially
10048 -- overridden subprogram that belongs to an implemented
10051 Hom := Current_Entity_In_Scope (Def_Id);
10052 while Present (Hom) loop
10056 or else not Is_Overloadable (Subp)
10057 or else not Is_Primitive (Subp)
10058 or else not Is_Dispatching_Operation (Subp)
10059 or else not Present (Find_Dispatching_Type (Subp))
10060 or else not Is_Interface (Find_Dispatching_Type (Subp))
10064 -- Entries and procedures can override abstract or null
10065 -- interface procedures.
10067 elsif (Ekind (Def_Id) = E_Procedure
10068 or else Ekind (Def_Id) = E_Entry)
10069 and then Ekind (Subp) = E_Procedure
10070 and then Matches_Prefixed_View_Profile
10071 (Parameter_Specifications (Parent (Def_Id)),
10072 Parameter_Specifications (Parent (Subp)))
10076 -- For an overridden subprogram Subp, check whether the mode
10077 -- of its first parameter is correct depending on the kind
10078 -- of synchronized type.
10081 Formal : constant Node_Id := First_Formal (Candidate);
10084 -- In order for an entry or a protected procedure to
10085 -- override, the first parameter of the overridden
10086 -- routine must be of mode "out", "in out" or
10087 -- access-to-variable.
10089 if Ekind_In (Candidate, E_Entry, E_Procedure)
10090 and then Is_Protected_Type (Typ)
10091 and then Ekind (Formal) /= E_In_Out_Parameter
10092 and then Ekind (Formal) /= E_Out_Parameter
10093 and then Nkind (Parameter_Type (Parent (Formal))) /=
10094 N_Access_Definition
10098 -- All other cases are OK since a task entry or routine
10099 -- does not have a restriction on the mode of the first
10100 -- parameter of the overridden interface routine.
10103 Overridden_Subp := Candidate;
10108 -- Functions can override abstract interface functions
10110 elsif Ekind (Def_Id) = E_Function
10111 and then Ekind (Subp) = E_Function
10112 and then Matches_Prefixed_View_Profile
10113 (Parameter_Specifications (Parent (Def_Id)),
10114 Parameter_Specifications (Parent (Subp)))
10115 and then Etype (Result_Definition (Parent (Def_Id))) =
10116 Etype (Result_Definition (Parent (Subp)))
10118 Overridden_Subp := Subp;
10122 Hom := Homonym (Hom);
10125 -- After examining all candidates for overriding, we are left with
10126 -- the best match which is a mode incompatible interface routine.
10127 -- Do not emit an error if the Expander is active since this error
10128 -- will be detected later on after all concurrent types are
10129 -- expanded and all wrappers are built. This check is meant for
10130 -- spec-only compilations.
10132 if Present (Candidate) and then not Expander_Active then
10134 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10136 -- Def_Id is primitive of a protected type, declared inside the
10137 -- type, and the candidate is primitive of a limited or
10138 -- synchronized interface.
10141 and then Is_Protected_Type (Typ)
10143 (Is_Limited_Interface (Iface_Typ)
10144 or else Is_Protected_Interface (Iface_Typ)
10145 or else Is_Synchronized_Interface (Iface_Typ)
10146 or else Is_Task_Interface (Iface_Typ))
10148 Error_Msg_PT (Parent (Typ), Candidate);
10152 Overridden_Subp := Candidate;
10155 end Check_Synchronized_Overriding;
10157 ----------------------------
10158 -- Is_Private_Declaration --
10159 ----------------------------
10161 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10162 Priv_Decls : List_Id;
10163 Decl : constant Node_Id := Unit_Declaration_Node (E);
10166 if Is_Package_Or_Generic_Package (Current_Scope)
10167 and then In_Private_Part (Current_Scope)
10170 Private_Declarations (Package_Specification (Current_Scope));
10172 return In_Package_Body (Current_Scope)
10174 (Is_List_Member (Decl)
10175 and then List_Containing (Decl) = Priv_Decls)
10176 or else (Nkind (Parent (Decl)) = N_Package_Specification
10178 Is_Compilation_Unit
10179 (Defining_Entity (Parent (Decl)))
10180 and then List_Containing (Parent (Parent (Decl))) =
10185 end Is_Private_Declaration;
10187 --------------------------
10188 -- Is_Overriding_Alias --
10189 --------------------------
10191 function Is_Overriding_Alias
10192 (Old_E : Entity_Id;
10193 New_E : Entity_Id) return Boolean
10195 AO : constant Entity_Id := Alias (Old_E);
10196 AN : constant Entity_Id := Alias (New_E);
10199 return Scope (AO) /= Scope (AN)
10200 or else No (DTC_Entity (AO))
10201 or else No (DTC_Entity (AN))
10202 or else DT_Position (AO) = DT_Position (AN);
10203 end Is_Overriding_Alias;
10205 -- Start of processing for New_Overloaded_Entity
10208 -- We need to look for an entity that S may override. This must be a
10209 -- homonym in the current scope, so we look for the first homonym of
10210 -- S in the current scope as the starting point for the search.
10212 E := Current_Entity_In_Scope (S);
10214 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10215 -- They are directly added to the list of primitive operations of
10216 -- Derived_Type, unless this is a rederivation in the private part
10217 -- of an operation that was already derived in the visible part of
10218 -- the current package.
10220 if Ada_Version >= Ada_2005
10221 and then Present (Derived_Type)
10222 and then Present (Alias (S))
10223 and then Is_Dispatching_Operation (Alias (S))
10224 and then Present (Find_Dispatching_Type (Alias (S)))
10225 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10227 -- For private types, when the full-view is processed we propagate to
10228 -- the full view the non-overridden entities whose attribute "alias"
10229 -- references an interface primitive. These entities were added by
10230 -- Derive_Subprograms to ensure that interface primitives are
10233 -- Inside_Freeze_Actions is non zero when S corresponds with an
10234 -- internal entity that links an interface primitive with its
10235 -- covering primitive through attribute Interface_Alias (see
10236 -- Add_Internal_Interface_Entities).
10238 if Inside_Freezing_Actions = 0
10239 and then Is_Package_Or_Generic_Package (Current_Scope)
10240 and then In_Private_Part (Current_Scope)
10241 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10242 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10243 and then Full_View (Defining_Identifier (Parent (E)))
10244 = Defining_Identifier (Parent (S))
10245 and then Alias (E) = Alias (S)
10247 Check_Operation_From_Private_View (S, E);
10248 Set_Is_Dispatching_Operation (S);
10253 Enter_Overloaded_Entity (S);
10254 Check_Dispatching_Operation (S, Empty);
10255 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10261 -- If there is no homonym then this is definitely not overriding
10264 Enter_Overloaded_Entity (S);
10265 Check_Dispatching_Operation (S, Empty);
10266 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10268 -- If subprogram has an explicit declaration, check whether it has an
10269 -- overriding indicator.
10271 if Comes_From_Source (S) then
10272 Check_Synchronized_Overriding (S, Overridden_Subp);
10274 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10275 -- it may have overridden some hidden inherited primitive. Update
10276 -- Overridden_Subp to avoid spurious errors when checking the
10277 -- overriding indicator.
10279 if Ada_Version >= Ada_2012
10280 and then No (Overridden_Subp)
10281 and then Is_Dispatching_Operation (S)
10282 and then Present (Overridden_Operation (S))
10284 Overridden_Subp := Overridden_Operation (S);
10287 Check_Overriding_Indicator
10288 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10291 -- If there is a homonym that is not overloadable, then we have an
10292 -- error, except for the special cases checked explicitly below.
10294 elsif not Is_Overloadable (E) then
10296 -- Check for spurious conflict produced by a subprogram that has the
10297 -- same name as that of the enclosing generic package. The conflict
10298 -- occurs within an instance, between the subprogram and the renaming
10299 -- declaration for the package. After the subprogram, the package
10300 -- renaming declaration becomes hidden.
10302 if Ekind (E) = E_Package
10303 and then Present (Renamed_Object (E))
10304 and then Renamed_Object (E) = Current_Scope
10305 and then Nkind (Parent (Renamed_Object (E))) =
10306 N_Package_Specification
10307 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10310 Set_Is_Immediately_Visible (E, False);
10311 Enter_Overloaded_Entity (S);
10312 Set_Homonym (S, Homonym (E));
10313 Check_Dispatching_Operation (S, Empty);
10314 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10316 -- If the subprogram is implicit it is hidden by the previous
10317 -- declaration. However if it is dispatching, it must appear in the
10318 -- dispatch table anyway, because it can be dispatched to even if it
10319 -- cannot be called directly.
10321 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10322 Set_Scope (S, Current_Scope);
10324 if Is_Dispatching_Operation (Alias (S)) then
10325 Check_Dispatching_Operation (S, Empty);
10331 Error_Msg_Sloc := Sloc (E);
10333 -- Generate message, with useful additional warning if in generic
10335 if Is_Generic_Unit (E) then
10336 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10337 Error_Msg_N ("\& conflicts with declaration#", S);
10339 Error_Msg_N ("& conflicts with declaration#", S);
10345 -- E exists and is overloadable
10348 Check_Synchronized_Overriding (S, Overridden_Subp);
10350 -- Loop through E and its homonyms to determine if any of them is
10351 -- the candidate for overriding by S.
10353 while Present (E) loop
10355 -- Definitely not interesting if not in the current scope
10357 if Scope (E) /= Current_Scope then
10360 -- A function can overload the name of an abstract state. The
10361 -- state can be viewed as a function with a profile that cannot
10362 -- be matched by anything.
10364 elsif Ekind (S) = E_Function
10365 and then Ekind (E) = E_Abstract_State
10367 Enter_Overloaded_Entity (S);
10370 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10371 -- procedures locate the internally generated spec. We enforce
10372 -- mode conformance since a tagged type may inherit from
10373 -- interfaces several null primitives which differ only in
10374 -- the mode of the formals.
10376 elsif not Comes_From_Source (S)
10377 and then Is_Null_Procedure (S)
10378 and then not Mode_Conformant (E, S)
10382 -- Check if we have type conformance
10384 elsif Type_Conformant (E, S) then
10386 -- If the old and new entities have the same profile and one
10387 -- is not the body of the other, then this is an error, unless
10388 -- one of them is implicitly declared.
10390 -- There are some cases when both can be implicit, for example
10391 -- when both a literal and a function that overrides it are
10392 -- inherited in a derivation, or when an inherited operation
10393 -- of a tagged full type overrides the inherited operation of
10394 -- a private extension. Ada 83 had a special rule for the
10395 -- literal case. In Ada 95, the later implicit operation hides
10396 -- the former, and the literal is always the former. In the
10397 -- odd case where both are derived operations declared at the
10398 -- same point, both operations should be declared, and in that
10399 -- case we bypass the following test and proceed to the next
10400 -- part. This can only occur for certain obscure cases in
10401 -- instances, when an operation on a type derived from a formal
10402 -- private type does not override a homograph inherited from
10403 -- the actual. In subsequent derivations of such a type, the
10404 -- DT positions of these operations remain distinct, if they
10407 if Present (Alias (S))
10408 and then (No (Alias (E))
10409 or else Comes_From_Source (E)
10410 or else Is_Abstract_Subprogram (S)
10412 (Is_Dispatching_Operation (E)
10413 and then Is_Overriding_Alias (E, S)))
10414 and then Ekind (E) /= E_Enumeration_Literal
10416 -- When an derived operation is overloaded it may be due to
10417 -- the fact that the full view of a private extension
10418 -- re-inherits. It has to be dealt with.
10420 if Is_Package_Or_Generic_Package (Current_Scope)
10421 and then In_Private_Part (Current_Scope)
10423 Check_Operation_From_Private_View (S, E);
10426 -- In any case the implicit operation remains hidden by the
10427 -- existing declaration, which is overriding. Indicate that
10428 -- E overrides the operation from which S is inherited.
10430 if Present (Alias (S)) then
10431 Set_Overridden_Operation (E, Alias (S));
10433 Set_Overridden_Operation (E, S);
10436 if Comes_From_Source (E) then
10437 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10442 -- Within an instance, the renaming declarations for actual
10443 -- subprograms may become ambiguous, but they do not hide each
10446 elsif Ekind (E) /= E_Entry
10447 and then not Comes_From_Source (E)
10448 and then not Is_Generic_Instance (E)
10449 and then (Present (Alias (E))
10450 or else Is_Intrinsic_Subprogram (E))
10451 and then (not In_Instance
10452 or else No (Parent (E))
10453 or else Nkind (Unit_Declaration_Node (E)) /=
10454 N_Subprogram_Renaming_Declaration)
10456 -- A subprogram child unit is not allowed to override an
10457 -- inherited subprogram (10.1.1(20)).
10459 if Is_Child_Unit (S) then
10461 ("child unit overrides inherited subprogram in parent",
10466 if Is_Non_Overriding_Operation (E, S) then
10467 Enter_Overloaded_Entity (S);
10469 if No (Derived_Type)
10470 or else Is_Tagged_Type (Derived_Type)
10472 Check_Dispatching_Operation (S, Empty);
10478 -- E is a derived operation or an internal operator which
10479 -- is being overridden. Remove E from further visibility.
10480 -- Furthermore, if E is a dispatching operation, it must be
10481 -- replaced in the list of primitive operations of its type
10482 -- (see Override_Dispatching_Operation).
10484 Overridden_Subp := E;
10490 Prev := First_Entity (Current_Scope);
10491 while Present (Prev) and then Next_Entity (Prev) /= E loop
10492 Next_Entity (Prev);
10495 -- It is possible for E to be in the current scope and
10496 -- yet not in the entity chain. This can only occur in a
10497 -- generic context where E is an implicit concatenation
10498 -- in the formal part, because in a generic body the
10499 -- entity chain starts with the formals.
10502 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10504 -- E must be removed both from the entity_list of the
10505 -- current scope, and from the visibility chain
10507 if Debug_Flag_E then
10508 Write_Str ("Override implicit operation ");
10509 Write_Int (Int (E));
10513 -- If E is a predefined concatenation, it stands for four
10514 -- different operations. As a result, a single explicit
10515 -- declaration does not hide it. In a possible ambiguous
10516 -- situation, Disambiguate chooses the user-defined op,
10517 -- so it is correct to retain the previous internal one.
10519 if Chars (E) /= Name_Op_Concat
10520 or else Ekind (E) /= E_Operator
10522 -- For nondispatching derived operations that are
10523 -- overridden by a subprogram declared in the private
10524 -- part of a package, we retain the derived subprogram
10525 -- but mark it as not immediately visible. If the
10526 -- derived operation was declared in the visible part
10527 -- then this ensures that it will still be visible
10528 -- outside the package with the proper signature
10529 -- (calls from outside must also be directed to this
10530 -- version rather than the overriding one, unlike the
10531 -- dispatching case). Calls from inside the package
10532 -- will still resolve to the overriding subprogram
10533 -- since the derived one is marked as not visible
10534 -- within the package.
10536 -- If the private operation is dispatching, we achieve
10537 -- the overriding by keeping the implicit operation
10538 -- but setting its alias to be the overriding one. In
10539 -- this fashion the proper body is executed in all
10540 -- cases, but the original signature is used outside
10543 -- If the overriding is not in the private part, we
10544 -- remove the implicit operation altogether.
10546 if Is_Private_Declaration (S) then
10547 if not Is_Dispatching_Operation (E) then
10548 Set_Is_Immediately_Visible (E, False);
10550 -- Work done in Override_Dispatching_Operation,
10551 -- so nothing else needs to be done here.
10557 -- Find predecessor of E in Homonym chain
10559 if E = Current_Entity (E) then
10562 Prev_Vis := Current_Entity (E);
10563 while Homonym (Prev_Vis) /= E loop
10564 Prev_Vis := Homonym (Prev_Vis);
10568 if Prev_Vis /= Empty then
10570 -- Skip E in the visibility chain
10572 Set_Homonym (Prev_Vis, Homonym (E));
10575 Set_Name_Entity_Id (Chars (E), Homonym (E));
10578 Set_Next_Entity (Prev, Next_Entity (E));
10580 if No (Next_Entity (Prev)) then
10581 Set_Last_Entity (Current_Scope, Prev);
10586 Enter_Overloaded_Entity (S);
10588 -- For entities generated by Derive_Subprograms the
10589 -- overridden operation is the inherited primitive
10590 -- (which is available through the attribute alias).
10592 if not (Comes_From_Source (E))
10593 and then Is_Dispatching_Operation (E)
10594 and then Find_Dispatching_Type (E) =
10595 Find_Dispatching_Type (S)
10596 and then Present (Alias (E))
10597 and then Comes_From_Source (Alias (E))
10599 Set_Overridden_Operation (S, Alias (E));
10601 -- Normal case of setting entity as overridden
10603 -- Note: Static_Initialization and Overridden_Operation
10604 -- attributes use the same field in subprogram entities.
10605 -- Static_Initialization is only defined for internal
10606 -- initialization procedures, where Overridden_Operation
10607 -- is irrelevant. Therefore the setting of this attribute
10608 -- must check whether the target is an init_proc.
10610 elsif not Is_Init_Proc (S) then
10611 Set_Overridden_Operation (S, E);
10614 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10616 -- If S is a user-defined subprogram or a null procedure
10617 -- expanded to override an inherited null procedure, or a
10618 -- predefined dispatching primitive then indicate that E
10619 -- overrides the operation from which S is inherited.
10621 if Comes_From_Source (S)
10623 (Present (Parent (S))
10625 Nkind (Parent (S)) = N_Procedure_Specification
10627 Null_Present (Parent (S)))
10629 (Present (Alias (E))
10631 Is_Predefined_Dispatching_Operation (Alias (E)))
10633 if Present (Alias (E)) then
10634 Set_Overridden_Operation (S, Alias (E));
10638 if Is_Dispatching_Operation (E) then
10640 -- An overriding dispatching subprogram inherits the
10641 -- convention of the overridden subprogram (AI-117).
10643 Set_Convention (S, Convention (E));
10644 Check_Dispatching_Operation (S, E);
10647 Check_Dispatching_Operation (S, Empty);
10650 Check_For_Primitive_Subprogram
10651 (Is_Primitive_Subp, Is_Overriding => True);
10652 goto Check_Inequality;
10655 -- Apparent redeclarations in instances can occur when two
10656 -- formal types get the same actual type. The subprograms in
10657 -- in the instance are legal, even if not callable from the
10658 -- outside. Calls from within are disambiguated elsewhere.
10659 -- For dispatching operations in the visible part, the usual
10660 -- rules apply, and operations with the same profile are not
10661 -- legal (B830001).
10663 elsif (In_Instance_Visible_Part
10664 and then not Is_Dispatching_Operation (E))
10665 or else In_Instance_Not_Visible
10669 -- Here we have a real error (identical profile)
10672 Error_Msg_Sloc := Sloc (E);
10674 -- Avoid cascaded errors if the entity appears in
10675 -- subsequent calls.
10677 Set_Scope (S, Current_Scope);
10679 -- Generate error, with extra useful warning for the case
10680 -- of a generic instance with no completion.
10682 if Is_Generic_Instance (S)
10683 and then not Has_Completion (E)
10686 ("instantiation cannot provide body for&", S);
10687 Error_Msg_N ("\& conflicts with declaration#", S);
10689 Error_Msg_N ("& conflicts with declaration#", S);
10696 -- If one subprogram has an access parameter and the other
10697 -- a parameter of an access type, calls to either might be
10698 -- ambiguous. Verify that parameters match except for the
10699 -- access parameter.
10701 if May_Hide_Profile then
10707 F1 := First_Formal (S);
10708 F2 := First_Formal (E);
10709 while Present (F1) and then Present (F2) loop
10710 if Is_Access_Type (Etype (F1)) then
10711 if not Is_Access_Type (Etype (F2))
10712 or else not Conforming_Types
10713 (Designated_Type (Etype (F1)),
10714 Designated_Type (Etype (F2)),
10717 May_Hide_Profile := False;
10721 not Conforming_Types
10722 (Etype (F1), Etype (F2), Type_Conformant)
10724 May_Hide_Profile := False;
10731 if May_Hide_Profile
10735 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10744 -- On exit, we know that S is a new entity
10746 Enter_Overloaded_Entity (S);
10747 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10748 Check_Overriding_Indicator
10749 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10751 -- Overloading is not allowed in SPARK, except for operators
10753 if Nkind (S) /= N_Defining_Operator_Symbol then
10754 Error_Msg_Sloc := Sloc (Homonym (S));
10755 Check_SPARK_Restriction
10756 ("overloading not allowed with entity#", S);
10759 -- If S is a derived operation for an untagged type then by
10760 -- definition it's not a dispatching operation (even if the parent
10761 -- operation was dispatching), so Check_Dispatching_Operation is not
10762 -- called in that case.
10764 if No (Derived_Type)
10765 or else Is_Tagged_Type (Derived_Type)
10767 Check_Dispatching_Operation (S, Empty);
10771 -- If this is a user-defined equality operator that is not a derived
10772 -- subprogram, create the corresponding inequality. If the operation is
10773 -- dispatching, the expansion is done elsewhere, and we do not create
10774 -- an explicit inequality operation.
10776 <<Check_Inequality>>
10777 if Chars (S) = Name_Op_Eq
10778 and then Etype (S) = Standard_Boolean
10779 and then Present (Parent (S))
10780 and then not Is_Dispatching_Operation (S)
10782 Make_Inequality_Operator (S);
10784 if Ada_Version >= Ada_2012 then
10785 Check_Untagged_Equality (S);
10788 end New_Overloaded_Entity;
10790 ---------------------
10791 -- Process_Formals --
10792 ---------------------
10794 procedure Process_Formals
10796 Related_Nod : Node_Id)
10798 Param_Spec : Node_Id;
10799 Formal : Entity_Id;
10800 Formal_Type : Entity_Id;
10804 Num_Out_Params : Nat := 0;
10805 First_Out_Param : Entity_Id := Empty;
10806 -- Used for setting Is_Only_Out_Parameter
10808 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10809 -- Determine whether an access type designates a type coming from a
10812 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10813 -- Check whether the default has a class-wide type. After analysis the
10814 -- default has the type of the formal, so we must also check explicitly
10815 -- for an access attribute.
10817 ----------------------------------
10818 -- Designates_From_Limited_With --
10819 ----------------------------------
10821 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10822 Desig : Entity_Id := Typ;
10825 if Is_Access_Type (Desig) then
10826 Desig := Directly_Designated_Type (Desig);
10829 if Is_Class_Wide_Type (Desig) then
10830 Desig := Root_Type (Desig);
10834 Ekind (Desig) = E_Incomplete_Type
10835 and then From_Limited_With (Desig);
10836 end Designates_From_Limited_With;
10838 ---------------------------
10839 -- Is_Class_Wide_Default --
10840 ---------------------------
10842 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10844 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10845 or else (Nkind (D) = N_Attribute_Reference
10846 and then Attribute_Name (D) = Name_Access
10847 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10848 end Is_Class_Wide_Default;
10850 -- Start of processing for Process_Formals
10853 -- In order to prevent premature use of the formals in the same formal
10854 -- part, the Ekind is left undefined until all default expressions are
10855 -- analyzed. The Ekind is established in a separate loop at the end.
10857 Param_Spec := First (T);
10858 while Present (Param_Spec) loop
10859 Formal := Defining_Identifier (Param_Spec);
10860 Set_Never_Set_In_Source (Formal, True);
10861 Enter_Name (Formal);
10863 -- Case of ordinary parameters
10865 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10866 Find_Type (Parameter_Type (Param_Spec));
10867 Ptype := Parameter_Type (Param_Spec);
10869 if Ptype = Error then
10873 Formal_Type := Entity (Ptype);
10875 if Is_Incomplete_Type (Formal_Type)
10877 (Is_Class_Wide_Type (Formal_Type)
10878 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10880 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10881 -- primitive operations, as long as their completion is
10882 -- in the same declarative part. If in the private part
10883 -- this means that the type cannot be a Taft-amendment type.
10884 -- Check is done on package exit. For access to subprograms,
10885 -- the use is legal for Taft-amendment types.
10887 -- Ada 2012: tagged incomplete types are allowed as generic
10888 -- formal types. They do not introduce dependencies and the
10889 -- corresponding generic subprogram does not have a delayed
10890 -- freeze, because it does not need a freeze node.
10892 if Is_Tagged_Type (Formal_Type) then
10893 if Ekind (Scope (Current_Scope)) = E_Package
10894 and then not From_Limited_With (Formal_Type)
10895 and then not Is_Generic_Type (Formal_Type)
10896 and then not Is_Class_Wide_Type (Formal_Type)
10899 (Parent (T), N_Access_Function_Definition,
10900 N_Access_Procedure_Definition)
10904 Private_Dependents (Base_Type (Formal_Type)));
10906 -- Freezing is delayed to ensure that Register_Prim
10907 -- will get called for this operation, which is needed
10908 -- in cases where static dispatch tables aren't built.
10909 -- (Note that the same is done for controlling access
10910 -- parameter cases in function Access_Definition.)
10912 Set_Has_Delayed_Freeze (Current_Scope);
10916 -- Special handling of Value_Type for CIL case
10918 elsif Is_Value_Type (Formal_Type) then
10921 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10922 N_Access_Procedure_Definition)
10924 -- AI05-0151: Tagged incomplete types are allowed in all
10925 -- formal parts. Untagged incomplete types are not allowed
10928 if Ada_Version >= Ada_2012 then
10929 if Is_Tagged_Type (Formal_Type) then
10932 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10937 ("invalid use of untagged incomplete type&",
10938 Ptype, Formal_Type);
10943 ("invalid use of incomplete type&",
10944 Param_Spec, Formal_Type);
10946 -- Further checks on the legality of incomplete types
10947 -- in formal parts are delayed until the freeze point
10948 -- of the enclosing subprogram or access to subprogram.
10952 elsif Ekind (Formal_Type) = E_Void then
10954 ("premature use of&",
10955 Parameter_Type (Param_Spec), Formal_Type);
10958 -- Ada 2012 (AI-142): Handle aliased parameters
10960 if Ada_Version >= Ada_2012
10961 and then Aliased_Present (Param_Spec)
10963 Set_Is_Aliased (Formal);
10966 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10967 -- declaration corresponding to the null-excluding type of the
10968 -- formal in the enclosing scope. Finally, replace the parameter
10969 -- type of the formal with the internal subtype.
10971 if Ada_Version >= Ada_2005
10972 and then Null_Exclusion_Present (Param_Spec)
10974 if not Is_Access_Type (Formal_Type) then
10976 ("`NOT NULL` allowed only for an access type", Param_Spec);
10979 if Can_Never_Be_Null (Formal_Type)
10980 and then Comes_From_Source (Related_Nod)
10983 ("`NOT NULL` not allowed (& already excludes null)",
10984 Param_Spec, Formal_Type);
10988 Create_Null_Excluding_Itype
10990 Related_Nod => Related_Nod,
10991 Scope_Id => Scope (Current_Scope));
10993 -- If the designated type of the itype is an itype that is
10994 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10995 -- on the access subtype, to prevent order-of-elaboration
10996 -- issues in the backend.
10999 -- type T is access procedure;
11000 -- procedure Op (O : not null T);
11002 if Is_Itype (Directly_Designated_Type (Formal_Type))
11004 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11006 Set_Has_Delayed_Freeze (Formal_Type);
11011 -- An access formal type
11015 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11017 -- No need to continue if we already notified errors
11019 if not Present (Formal_Type) then
11023 -- Ada 2005 (AI-254)
11026 AD : constant Node_Id :=
11027 Access_To_Subprogram_Definition
11028 (Parameter_Type (Param_Spec));
11030 if Present (AD) and then Protected_Present (AD) then
11032 Replace_Anonymous_Access_To_Protected_Subprogram
11038 Set_Etype (Formal, Formal_Type);
11040 -- Deal with default expression if present
11042 Default := Expression (Param_Spec);
11044 if Present (Default) then
11045 Check_SPARK_Restriction
11046 ("default expression is not allowed", Default);
11048 if Out_Present (Param_Spec) then
11050 ("default initialization only allowed for IN parameters",
11054 -- Do the special preanalysis of the expression (see section on
11055 -- "Handling of Default Expressions" in the spec of package Sem).
11057 Preanalyze_Spec_Expression (Default, Formal_Type);
11059 -- An access to constant cannot be the default for
11060 -- an access parameter that is an access to variable.
11062 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11063 and then not Is_Access_Constant (Formal_Type)
11064 and then Is_Access_Type (Etype (Default))
11065 and then Is_Access_Constant (Etype (Default))
11068 ("formal that is access to variable cannot be initialized " &
11069 "with an access-to-constant expression", Default);
11072 -- Check that the designated type of an access parameter's default
11073 -- is not a class-wide type unless the parameter's designated type
11074 -- is also class-wide.
11076 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11077 and then not Designates_From_Limited_With (Formal_Type)
11078 and then Is_Class_Wide_Default (Default)
11079 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11082 ("access to class-wide expression not allowed here", Default);
11085 -- Check incorrect use of dynamically tagged expressions
11087 if Is_Tagged_Type (Formal_Type) then
11088 Check_Dynamically_Tagged_Expression
11090 Typ => Formal_Type,
11091 Related_Nod => Default);
11095 -- Ada 2005 (AI-231): Static checks
11097 if Ada_Version >= Ada_2005
11098 and then Is_Access_Type (Etype (Formal))
11099 and then Can_Never_Be_Null (Etype (Formal))
11101 Null_Exclusion_Static_Checks (Param_Spec);
11108 -- If this is the formal part of a function specification, analyze the
11109 -- subtype mark in the context where the formals are visible but not
11110 -- yet usable, and may hide outer homographs.
11112 if Nkind (Related_Nod) = N_Function_Specification then
11113 Analyze_Return_Type (Related_Nod);
11116 -- Now set the kind (mode) of each formal
11118 Param_Spec := First (T);
11119 while Present (Param_Spec) loop
11120 Formal := Defining_Identifier (Param_Spec);
11121 Set_Formal_Mode (Formal);
11123 if Ekind (Formal) = E_In_Parameter then
11124 Set_Default_Value (Formal, Expression (Param_Spec));
11126 if Present (Expression (Param_Spec)) then
11127 Default := Expression (Param_Spec);
11129 if Is_Scalar_Type (Etype (Default)) then
11130 if Nkind (Parameter_Type (Param_Spec)) /=
11131 N_Access_Definition
11133 Formal_Type := Entity (Parameter_Type (Param_Spec));
11137 (Related_Nod, Parameter_Type (Param_Spec));
11140 Apply_Scalar_Range_Check (Default, Formal_Type);
11144 elsif Ekind (Formal) = E_Out_Parameter then
11145 Num_Out_Params := Num_Out_Params + 1;
11147 if Num_Out_Params = 1 then
11148 First_Out_Param := Formal;
11151 elsif Ekind (Formal) = E_In_Out_Parameter then
11152 Num_Out_Params := Num_Out_Params + 1;
11155 -- Skip remaining processing if formal type was in error
11157 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11158 goto Next_Parameter;
11161 -- Force call by reference if aliased
11163 if Is_Aliased (Formal) then
11164 Set_Mechanism (Formal, By_Reference);
11166 -- Warn if user asked this to be passed by copy
11168 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11170 ("cannot pass aliased parameter & by copy?", Formal);
11173 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11175 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11176 Set_Mechanism (Formal, By_Copy);
11178 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11179 Set_Mechanism (Formal, By_Reference);
11186 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11187 Set_Is_Only_Out_Parameter (First_Out_Param);
11189 end Process_Formals;
11191 ----------------------------
11192 -- Reference_Body_Formals --
11193 ----------------------------
11195 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11200 if Error_Posted (Spec) then
11204 -- Iterate over both lists. They may be of different lengths if the two
11205 -- specs are not conformant.
11207 Fs := First_Formal (Spec);
11208 Fb := First_Formal (Bod);
11209 while Present (Fs) and then Present (Fb) loop
11210 Generate_Reference (Fs, Fb, 'b');
11212 if Style_Check then
11213 Style.Check_Identifier (Fb, Fs);
11216 Set_Spec_Entity (Fb, Fs);
11217 Set_Referenced (Fs, False);
11221 end Reference_Body_Formals;
11223 -------------------------
11224 -- Set_Actual_Subtypes --
11225 -------------------------
11227 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11229 Formal : Entity_Id;
11231 First_Stmt : Node_Id := Empty;
11232 AS_Needed : Boolean;
11235 -- If this is an empty initialization procedure, no need to create
11236 -- actual subtypes (small optimization).
11238 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11242 Formal := First_Formal (Subp);
11243 while Present (Formal) loop
11244 T := Etype (Formal);
11246 -- We never need an actual subtype for a constrained formal
11248 if Is_Constrained (T) then
11249 AS_Needed := False;
11251 -- If we have unknown discriminants, then we do not need an actual
11252 -- subtype, or more accurately we cannot figure it out! Note that
11253 -- all class-wide types have unknown discriminants.
11255 elsif Has_Unknown_Discriminants (T) then
11256 AS_Needed := False;
11258 -- At this stage we have an unconstrained type that may need an
11259 -- actual subtype. For sure the actual subtype is needed if we have
11260 -- an unconstrained array type.
11262 elsif Is_Array_Type (T) then
11265 -- The only other case needing an actual subtype is an unconstrained
11266 -- record type which is an IN parameter (we cannot generate actual
11267 -- subtypes for the OUT or IN OUT case, since an assignment can
11268 -- change the discriminant values. However we exclude the case of
11269 -- initialization procedures, since discriminants are handled very
11270 -- specially in this context, see the section entitled "Handling of
11271 -- Discriminants" in Einfo.
11273 -- We also exclude the case of Discrim_SO_Functions (functions used
11274 -- in front end layout mode for size/offset values), since in such
11275 -- functions only discriminants are referenced, and not only are such
11276 -- subtypes not needed, but they cannot always be generated, because
11277 -- of order of elaboration issues.
11279 elsif Is_Record_Type (T)
11280 and then Ekind (Formal) = E_In_Parameter
11281 and then Chars (Formal) /= Name_uInit
11282 and then not Is_Unchecked_Union (T)
11283 and then not Is_Discrim_SO_Function (Subp)
11287 -- All other cases do not need an actual subtype
11290 AS_Needed := False;
11293 -- Generate actual subtypes for unconstrained arrays and
11294 -- unconstrained discriminated records.
11297 if Nkind (N) = N_Accept_Statement then
11299 -- If expansion is active, the formal is replaced by a local
11300 -- variable that renames the corresponding entry of the
11301 -- parameter block, and it is this local variable that may
11302 -- require an actual subtype.
11304 if Expander_Active then
11305 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11307 Decl := Build_Actual_Subtype (T, Formal);
11310 if Present (Handled_Statement_Sequence (N)) then
11312 First (Statements (Handled_Statement_Sequence (N)));
11313 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11314 Mark_Rewrite_Insertion (Decl);
11316 -- If the accept statement has no body, there will be no
11317 -- reference to the actuals, so no need to compute actual
11324 Decl := Build_Actual_Subtype (T, Formal);
11325 Prepend (Decl, Declarations (N));
11326 Mark_Rewrite_Insertion (Decl);
11329 -- The declaration uses the bounds of an existing object, and
11330 -- therefore needs no constraint checks.
11332 Analyze (Decl, Suppress => All_Checks);
11334 -- We need to freeze manually the generated type when it is
11335 -- inserted anywhere else than in a declarative part.
11337 if Present (First_Stmt) then
11338 Insert_List_Before_And_Analyze (First_Stmt,
11339 Freeze_Entity (Defining_Identifier (Decl), N));
11342 if Nkind (N) = N_Accept_Statement
11343 and then Expander_Active
11345 Set_Actual_Subtype (Renamed_Object (Formal),
11346 Defining_Identifier (Decl));
11348 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11352 Next_Formal (Formal);
11354 end Set_Actual_Subtypes;
11356 ---------------------
11357 -- Set_Formal_Mode --
11358 ---------------------
11360 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11361 Spec : constant Node_Id := Parent (Formal_Id);
11364 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11365 -- since we ensure that corresponding actuals are always valid at the
11366 -- point of the call.
11368 if Out_Present (Spec) then
11369 if Ekind (Scope (Formal_Id)) = E_Function
11370 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11372 -- [IN] OUT parameters allowed for functions in Ada 2012
11374 if Ada_Version >= Ada_2012 then
11376 -- Even in Ada 2012 operators can only have IN parameters
11378 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11379 Error_Msg_N ("operators can only have IN parameters", Spec);
11382 if In_Present (Spec) then
11383 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11385 Set_Ekind (Formal_Id, E_Out_Parameter);
11388 -- But not in earlier versions of Ada
11391 Error_Msg_N ("functions can only have IN parameters", Spec);
11392 Set_Ekind (Formal_Id, E_In_Parameter);
11395 elsif In_Present (Spec) then
11396 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11399 Set_Ekind (Formal_Id, E_Out_Parameter);
11400 Set_Never_Set_In_Source (Formal_Id, True);
11401 Set_Is_True_Constant (Formal_Id, False);
11402 Set_Current_Value (Formal_Id, Empty);
11406 Set_Ekind (Formal_Id, E_In_Parameter);
11409 -- Set Is_Known_Non_Null for access parameters since the language
11410 -- guarantees that access parameters are always non-null. We also set
11411 -- Can_Never_Be_Null, since there is no way to change the value.
11413 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11415 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11416 -- null; In Ada 2005, only if then null_exclusion is explicit.
11418 if Ada_Version < Ada_2005
11419 or else Can_Never_Be_Null (Etype (Formal_Id))
11421 Set_Is_Known_Non_Null (Formal_Id);
11422 Set_Can_Never_Be_Null (Formal_Id);
11425 -- Ada 2005 (AI-231): Null-exclusion access subtype
11427 elsif Is_Access_Type (Etype (Formal_Id))
11428 and then Can_Never_Be_Null (Etype (Formal_Id))
11430 Set_Is_Known_Non_Null (Formal_Id);
11432 -- We can also set Can_Never_Be_Null (thus preventing some junk
11433 -- access checks) for the case of an IN parameter, which cannot
11434 -- be changed, or for an IN OUT parameter, which can be changed but
11435 -- not to a null value. But for an OUT parameter, the initial value
11436 -- passed in can be null, so we can't set this flag in that case.
11438 if Ekind (Formal_Id) /= E_Out_Parameter then
11439 Set_Can_Never_Be_Null (Formal_Id);
11443 Set_Mechanism (Formal_Id, Default_Mechanism);
11444 Set_Formal_Validity (Formal_Id);
11445 end Set_Formal_Mode;
11447 -------------------------
11448 -- Set_Formal_Validity --
11449 -------------------------
11451 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11453 -- If no validity checking, then we cannot assume anything about the
11454 -- validity of parameters, since we do not know there is any checking
11455 -- of the validity on the call side.
11457 if not Validity_Checks_On then
11460 -- If validity checking for parameters is enabled, this means we are
11461 -- not supposed to make any assumptions about argument values.
11463 elsif Validity_Check_Parameters then
11466 -- If we are checking in parameters, we will assume that the caller is
11467 -- also checking parameters, so we can assume the parameter is valid.
11469 elsif Ekind (Formal_Id) = E_In_Parameter
11470 and then Validity_Check_In_Params
11472 Set_Is_Known_Valid (Formal_Id, True);
11474 -- Similar treatment for IN OUT parameters
11476 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11477 and then Validity_Check_In_Out_Params
11479 Set_Is_Known_Valid (Formal_Id, True);
11481 end Set_Formal_Validity;
11483 ------------------------
11484 -- Subtype_Conformant --
11485 ------------------------
11487 function Subtype_Conformant
11488 (New_Id : Entity_Id;
11489 Old_Id : Entity_Id;
11490 Skip_Controlling_Formals : Boolean := False) return Boolean
11494 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11495 Skip_Controlling_Formals => Skip_Controlling_Formals);
11497 end Subtype_Conformant;
11499 ---------------------
11500 -- Type_Conformant --
11501 ---------------------
11503 function Type_Conformant
11504 (New_Id : Entity_Id;
11505 Old_Id : Entity_Id;
11506 Skip_Controlling_Formals : Boolean := False) return Boolean
11510 May_Hide_Profile := False;
11513 (New_Id, Old_Id, Type_Conformant, False, Result,
11514 Skip_Controlling_Formals => Skip_Controlling_Formals);
11516 end Type_Conformant;
11518 -------------------------------
11519 -- Valid_Operator_Definition --
11520 -------------------------------
11522 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11525 Id : constant Name_Id := Chars (Designator);
11529 F := First_Formal (Designator);
11530 while Present (F) loop
11533 if Present (Default_Value (F)) then
11535 ("default values not allowed for operator parameters",
11542 -- Verify that user-defined operators have proper number of arguments
11543 -- First case of operators which can only be unary
11545 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11548 -- Case of operators which can be unary or binary
11550 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11551 N_OK := (N in 1 .. 2);
11553 -- All other operators can only be binary
11561 ("incorrect number of arguments for operator", Designator);
11565 and then Base_Type (Etype (Designator)) = Standard_Boolean
11566 and then not Is_Intrinsic_Subprogram (Designator)
11569 ("explicit definition of inequality not allowed", Designator);
11571 end Valid_Operator_Definition;