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 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1190 Set_SPARK_Pragma_Inherited (Body_Id, True);
1192 Analyze_Declarations (Declarations (N));
1194 Analyze (Handled_Statement_Sequence (N));
1196 Save_Global_References (Original_Node (N));
1198 -- Prior to exiting the scope, include generic formals again (if any
1199 -- are present) in the set of local entities.
1201 if Present (First_Ent) then
1202 Set_First_Entity (Gen_Id, First_Ent);
1205 Check_References (Gen_Id);
1208 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1210 Check_Subprogram_Order (N);
1212 -- Outside of its body, unit is generic again
1214 Set_Ekind (Gen_Id, Kind);
1215 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1218 Style.Check_Identifier (Body_Id, Gen_Id);
1222 end Analyze_Generic_Subprogram_Body;
1224 ----------------------------
1225 -- Analyze_Null_Procedure --
1226 ----------------------------
1228 procedure Analyze_Null_Procedure
1230 Is_Completion : out Boolean)
1232 Loc : constant Source_Ptr := Sloc (N);
1233 Spec : constant Node_Id := Specification (N);
1234 Designator : Entity_Id;
1236 Null_Body : Node_Id := Empty;
1240 -- Capture the profile of the null procedure before analysis, for
1241 -- expansion at the freeze point and at each point of call. The body is
1242 -- used if the procedure has preconditions, or if it is a completion. In
1243 -- the first case the body is analyzed at the freeze point, in the other
1244 -- it replaces the null procedure declaration.
1247 Make_Subprogram_Body (Loc,
1248 Specification => New_Copy_Tree (Spec),
1249 Declarations => New_List,
1250 Handled_Statement_Sequence =>
1251 Make_Handled_Sequence_Of_Statements (Loc,
1252 Statements => New_List (Make_Null_Statement (Loc))));
1254 -- Create new entities for body and formals
1256 Set_Defining_Unit_Name (Specification (Null_Body),
1257 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1259 Form := First (Parameter_Specifications (Specification (Null_Body)));
1260 while Present (Form) loop
1261 Set_Defining_Identifier (Form,
1262 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1266 -- Determine whether the null procedure may be a completion of a generic
1267 -- suprogram, in which case we use the new null body as the completion
1268 -- and set minimal semantic information on the original declaration,
1269 -- which is rewritten as a null statement.
1271 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1273 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1274 Insert_Before (N, Null_Body);
1275 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1276 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1278 Rewrite (N, Make_Null_Statement (Loc));
1279 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1280 Is_Completion := True;
1284 -- Resolve the types of the formals now, because the freeze point
1285 -- may appear in a different context, e.g. an instantiation.
1287 Form := First (Parameter_Specifications (Specification (Null_Body)));
1288 while Present (Form) loop
1289 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1290 Find_Type (Parameter_Type (Form));
1293 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1295 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1298 -- The case of a null procedure with a formal that is an
1299 -- access_to_subprogram type, and that is used as an actual
1300 -- in an instantiation is left to the enthusiastic reader.
1309 -- If there are previous overloadable entities with the same name,
1310 -- check whether any of them is completed by the null procedure.
1312 if Present (Prev) and then Is_Overloadable (Prev) then
1313 Designator := Analyze_Subprogram_Specification (Spec);
1314 Prev := Find_Corresponding_Spec (N);
1317 if No (Prev) or else not Comes_From_Source (Prev) then
1318 Designator := Analyze_Subprogram_Specification (Spec);
1319 Set_Has_Completion (Designator);
1321 -- Signal to caller that this is a procedure declaration
1323 Is_Completion := False;
1325 -- Null procedures are always inlined, but generic formal subprograms
1326 -- which appear as such in the internal instance of formal packages,
1327 -- need no completion and are not marked Inline.
1330 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1332 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1333 Set_Body_To_Inline (N, Null_Body);
1334 Set_Is_Inlined (Designator);
1338 -- The null procedure is a completion
1340 Is_Completion := True;
1342 if Expander_Active then
1343 Rewrite (N, Null_Body);
1347 Designator := Analyze_Subprogram_Specification (Spec);
1348 Set_Has_Completion (Designator);
1349 Set_Has_Completion (Prev);
1352 end Analyze_Null_Procedure;
1354 -----------------------------
1355 -- Analyze_Operator_Symbol --
1356 -----------------------------
1358 -- An operator symbol such as "+" or "and" may appear in context where the
1359 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1360 -- is just a string, as in (conjunction = "or"). In these cases the parser
1361 -- generates this node, and the semantics does the disambiguation. Other
1362 -- such case are actuals in an instantiation, the generic unit in an
1363 -- instantiation, and pragma arguments.
1365 procedure Analyze_Operator_Symbol (N : Node_Id) is
1366 Par : constant Node_Id := Parent (N);
1369 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1370 or else Nkind (Par) = N_Function_Instantiation
1371 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1372 or else (Nkind (Par) = N_Pragma_Argument_Association
1373 and then not Is_Pragma_String_Literal (Par))
1374 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1375 or else (Nkind (Par) = N_Attribute_Reference
1376 and then Attribute_Name (Par) /= Name_Value)
1378 Find_Direct_Name (N);
1381 Change_Operator_Symbol_To_String_Literal (N);
1384 end Analyze_Operator_Symbol;
1386 -----------------------------------
1387 -- Analyze_Parameter_Association --
1388 -----------------------------------
1390 procedure Analyze_Parameter_Association (N : Node_Id) is
1392 Analyze (Explicit_Actual_Parameter (N));
1393 end Analyze_Parameter_Association;
1395 ----------------------------
1396 -- Analyze_Procedure_Call --
1397 ----------------------------
1399 procedure Analyze_Procedure_Call (N : Node_Id) is
1400 Loc : constant Source_Ptr := Sloc (N);
1401 P : constant Node_Id := Name (N);
1402 Actuals : constant List_Id := Parameter_Associations (N);
1406 procedure Analyze_Call_And_Resolve;
1407 -- Do Analyze and Resolve calls for procedure call
1408 -- At end, check illegal order dependence.
1410 ------------------------------
1411 -- Analyze_Call_And_Resolve --
1412 ------------------------------
1414 procedure Analyze_Call_And_Resolve is
1416 if Nkind (N) = N_Procedure_Call_Statement then
1418 Resolve (N, Standard_Void_Type);
1422 end Analyze_Call_And_Resolve;
1424 -- Start of processing for Analyze_Procedure_Call
1427 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1428 -- a procedure call or an entry call. The prefix may denote an access
1429 -- to subprogram type, in which case an implicit dereference applies.
1430 -- If the prefix is an indexed component (without implicit dereference)
1431 -- then the construct denotes a call to a member of an entire family.
1432 -- If the prefix is a simple name, it may still denote a call to a
1433 -- parameterless member of an entry family. Resolution of these various
1434 -- interpretations is delicate.
1438 -- If this is a call of the form Obj.Op, the call may have been
1439 -- analyzed and possibly rewritten into a block, in which case
1442 if Analyzed (N) then
1446 -- If there is an error analyzing the name (which may have been
1447 -- rewritten if the original call was in prefix notation) then error
1448 -- has been emitted already, mark node and return.
1450 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1451 Set_Etype (N, Any_Type);
1455 -- Otherwise analyze the parameters
1457 if Present (Actuals) then
1458 Actual := First (Actuals);
1460 while Present (Actual) loop
1462 Check_Parameterless_Call (Actual);
1467 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1469 if Nkind (P) = N_Attribute_Reference
1470 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1472 Name_Elab_Subp_Body)
1474 if Present (Actuals) then
1476 ("no parameters allowed for this call", First (Actuals));
1480 Set_Etype (N, Standard_Void_Type);
1483 elsif Is_Entity_Name (P)
1484 and then Is_Record_Type (Etype (Entity (P)))
1485 and then Remote_AST_I_Dereference (P)
1489 elsif Is_Entity_Name (P)
1490 and then Ekind (Entity (P)) /= E_Entry_Family
1492 if Is_Access_Type (Etype (P))
1493 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1494 and then No (Actuals)
1495 and then Comes_From_Source (N)
1497 Error_Msg_N ("missing explicit dereference in call", N);
1500 Analyze_Call_And_Resolve;
1502 -- If the prefix is the simple name of an entry family, this is
1503 -- a parameterless call from within the task body itself.
1505 elsif Is_Entity_Name (P)
1506 and then Nkind (P) = N_Identifier
1507 and then Ekind (Entity (P)) = E_Entry_Family
1508 and then Present (Actuals)
1509 and then No (Next (First (Actuals)))
1511 -- Can be call to parameterless entry family. What appears to be the
1512 -- sole argument is in fact the entry index. Rewrite prefix of node
1513 -- accordingly. Source representation is unchanged by this
1517 Make_Indexed_Component (Loc,
1519 Make_Selected_Component (Loc,
1520 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1521 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1522 Expressions => Actuals);
1523 Set_Name (N, New_N);
1524 Set_Etype (New_N, Standard_Void_Type);
1525 Set_Parameter_Associations (N, No_List);
1526 Analyze_Call_And_Resolve;
1528 elsif Nkind (P) = N_Explicit_Dereference then
1529 if Ekind (Etype (P)) = E_Subprogram_Type then
1530 Analyze_Call_And_Resolve;
1532 Error_Msg_N ("expect access to procedure in call", P);
1535 -- The name can be a selected component or an indexed component that
1536 -- yields an access to subprogram. Such a prefix is legal if the call
1537 -- has parameter associations.
1539 elsif Is_Access_Type (Etype (P))
1540 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1542 if Present (Actuals) then
1543 Analyze_Call_And_Resolve;
1545 Error_Msg_N ("missing explicit dereference in call ", N);
1548 -- If not an access to subprogram, then the prefix must resolve to the
1549 -- name of an entry, entry family, or protected operation.
1551 -- For the case of a simple entry call, P is a selected component where
1552 -- the prefix is the task and the selector name is the entry. A call to
1553 -- a protected procedure will have the same syntax. If the protected
1554 -- object contains overloaded operations, the entity may appear as a
1555 -- function, the context will select the operation whose type is Void.
1557 elsif Nkind (P) = N_Selected_Component
1558 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1562 Analyze_Call_And_Resolve;
1564 elsif Nkind (P) = N_Selected_Component
1565 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1566 and then Present (Actuals)
1567 and then No (Next (First (Actuals)))
1569 -- Can be call to parameterless entry family. What appears to be the
1570 -- sole argument is in fact the entry index. Rewrite prefix of node
1571 -- accordingly. Source representation is unchanged by this
1575 Make_Indexed_Component (Loc,
1576 Prefix => New_Copy (P),
1577 Expressions => Actuals);
1578 Set_Name (N, New_N);
1579 Set_Etype (New_N, Standard_Void_Type);
1580 Set_Parameter_Associations (N, No_List);
1581 Analyze_Call_And_Resolve;
1583 -- For the case of a reference to an element of an entry family, P is
1584 -- an indexed component whose prefix is a selected component (task and
1585 -- entry family), and whose index is the entry family index.
1587 elsif Nkind (P) = N_Indexed_Component
1588 and then Nkind (Prefix (P)) = N_Selected_Component
1589 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1591 Analyze_Call_And_Resolve;
1593 -- If the prefix is the name of an entry family, it is a call from
1594 -- within the task body itself.
1596 elsif Nkind (P) = N_Indexed_Component
1597 and then Nkind (Prefix (P)) = N_Identifier
1598 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1601 Make_Selected_Component (Loc,
1602 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1603 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1604 Rewrite (Prefix (P), New_N);
1606 Analyze_Call_And_Resolve;
1608 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1609 -- procedure name, so the construct can only be a qualified expression.
1611 elsif Nkind (P) = N_Qualified_Expression
1612 and then Ada_Version >= Ada_2012
1614 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1617 -- Anything else is an error
1620 Error_Msg_N ("invalid procedure or entry call", N);
1622 end Analyze_Procedure_Call;
1624 ------------------------------
1625 -- Analyze_Return_Statement --
1626 ------------------------------
1628 procedure Analyze_Return_Statement (N : Node_Id) is
1630 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1631 N_Extended_Return_Statement));
1633 Returns_Object : constant Boolean :=
1634 Nkind (N) = N_Extended_Return_Statement
1636 (Nkind (N) = N_Simple_Return_Statement
1637 and then Present (Expression (N)));
1638 -- True if we're returning something; that is, "return <expression>;"
1639 -- or "return Result : T [:= ...]". False for "return;". Used for error
1640 -- checking: If Returns_Object is True, N should apply to a function
1641 -- body; otherwise N should apply to a procedure body, entry body,
1642 -- accept statement, or extended return statement.
1644 function Find_What_It_Applies_To return Entity_Id;
1645 -- Find the entity representing the innermost enclosing body, accept
1646 -- statement, or extended return statement. If the result is a callable
1647 -- construct or extended return statement, then this will be the value
1648 -- of the Return_Applies_To attribute. Otherwise, the program is
1649 -- illegal. See RM-6.5(4/2).
1651 -----------------------------
1652 -- Find_What_It_Applies_To --
1653 -----------------------------
1655 function Find_What_It_Applies_To return Entity_Id is
1656 Result : Entity_Id := Empty;
1659 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1660 -- and postconditions.
1662 for J in reverse 0 .. Scope_Stack.Last loop
1663 Result := Scope_Stack.Table (J).Entity;
1664 exit when not Ekind_In (Result, E_Block, E_Loop)
1665 and then Chars (Result) /= Name_uPostconditions;
1668 pragma Assert (Present (Result));
1670 end Find_What_It_Applies_To;
1672 -- Local declarations
1674 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1675 Kind : constant Entity_Kind := Ekind (Scope_Id);
1676 Loc : constant Source_Ptr := Sloc (N);
1677 Stm_Entity : constant Entity_Id :=
1679 (E_Return_Statement, Current_Scope, Loc, 'R');
1681 -- Start of processing for Analyze_Return_Statement
1684 Set_Return_Statement_Entity (N, Stm_Entity);
1686 Set_Etype (Stm_Entity, Standard_Void_Type);
1687 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1689 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1690 -- (4/2): an inner return statement will apply to this extended return.
1692 if Nkind (N) = N_Extended_Return_Statement then
1693 Push_Scope (Stm_Entity);
1696 -- Check that pragma No_Return is obeyed. Don't complain about the
1697 -- implicitly-generated return that is placed at the end.
1699 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1700 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1703 -- Warn on any unassigned OUT parameters if in procedure
1705 if Ekind (Scope_Id) = E_Procedure then
1706 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1709 -- Check that functions return objects, and other things do not
1711 if Kind = E_Function or else Kind = E_Generic_Function then
1712 if not Returns_Object then
1713 Error_Msg_N ("missing expression in return from function", N);
1716 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1717 if Returns_Object then
1718 Error_Msg_N ("procedure cannot return value (use function)", N);
1721 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1722 if Returns_Object then
1723 if Is_Protected_Type (Scope (Scope_Id)) then
1724 Error_Msg_N ("entry body cannot return value", N);
1726 Error_Msg_N ("accept statement cannot return value", N);
1730 elsif Kind = E_Return_Statement then
1732 -- We are nested within another return statement, which must be an
1733 -- extended_return_statement.
1735 if Returns_Object then
1736 if Nkind (N) = N_Extended_Return_Statement then
1738 ("extended return statement cannot be nested (use `RETURN;`)",
1741 -- Case of a simple return statement with a value inside extended
1742 -- return statement.
1746 ("return nested in extended return statement cannot return " &
1747 "value (use `RETURN;`)", N);
1752 Error_Msg_N ("illegal context for return statement", N);
1755 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1756 Analyze_Function_Return (N);
1758 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1759 Set_Return_Present (Scope_Id);
1762 if Nkind (N) = N_Extended_Return_Statement then
1766 Kill_Current_Values (Last_Assignment_Only => True);
1767 Check_Unreachable_Code (N);
1769 Analyze_Dimension (N);
1770 end Analyze_Return_Statement;
1772 -------------------------------------
1773 -- Analyze_Simple_Return_Statement --
1774 -------------------------------------
1776 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1778 if Present (Expression (N)) then
1779 Mark_Coextensions (N, Expression (N));
1782 Analyze_Return_Statement (N);
1783 end Analyze_Simple_Return_Statement;
1785 -------------------------
1786 -- Analyze_Return_Type --
1787 -------------------------
1789 procedure Analyze_Return_Type (N : Node_Id) is
1790 Designator : constant Entity_Id := Defining_Entity (N);
1791 Typ : Entity_Id := Empty;
1794 -- Normal case where result definition does not indicate an error
1796 if Result_Definition (N) /= Error then
1797 if Nkind (Result_Definition (N)) = N_Access_Definition then
1798 Check_SPARK_Restriction
1799 ("access result is not allowed", Result_Definition (N));
1801 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1804 AD : constant Node_Id :=
1805 Access_To_Subprogram_Definition (Result_Definition (N));
1807 if Present (AD) and then Protected_Present (AD) then
1808 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1810 Typ := Access_Definition (N, Result_Definition (N));
1814 Set_Parent (Typ, Result_Definition (N));
1815 Set_Is_Local_Anonymous_Access (Typ);
1816 Set_Etype (Designator, Typ);
1818 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1820 Null_Exclusion_Static_Checks (N);
1822 -- Subtype_Mark case
1825 Find_Type (Result_Definition (N));
1826 Typ := Entity (Result_Definition (N));
1827 Set_Etype (Designator, Typ);
1829 -- Unconstrained array as result is not allowed in SPARK
1831 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1832 Check_SPARK_Restriction
1833 ("returning an unconstrained array is not allowed",
1834 Result_Definition (N));
1837 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1839 Null_Exclusion_Static_Checks (N);
1841 -- If a null exclusion is imposed on the result type, then create
1842 -- a null-excluding itype (an access subtype) and use it as the
1843 -- function's Etype. Note that the null exclusion checks are done
1844 -- right before this, because they don't get applied to types that
1845 -- do not come from source.
1847 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1848 Set_Etype (Designator,
1849 Create_Null_Excluding_Itype
1852 Scope_Id => Scope (Current_Scope)));
1854 -- The new subtype must be elaborated before use because
1855 -- it is visible outside of the function. However its base
1856 -- type may not be frozen yet, so the reference that will
1857 -- force elaboration must be attached to the freezing of
1860 -- If the return specification appears on a proper body,
1861 -- the subtype will have been created already on the spec.
1863 if Is_Frozen (Typ) then
1864 if Nkind (Parent (N)) = N_Subprogram_Body
1865 and then Nkind (Parent (Parent (N))) = N_Subunit
1869 Build_Itype_Reference (Etype (Designator), Parent (N));
1873 Ensure_Freeze_Node (Typ);
1876 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1878 Set_Itype (IR, Etype (Designator));
1879 Append_Freeze_Actions (Typ, New_List (IR));
1884 Set_Etype (Designator, Typ);
1887 if Ekind (Typ) = E_Incomplete_Type
1888 and then Is_Value_Type (Typ)
1892 elsif Ekind (Typ) = E_Incomplete_Type
1893 or else (Is_Class_Wide_Type (Typ)
1894 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1896 -- AI05-0151: Tagged incomplete types are allowed in all formal
1897 -- parts. Untagged incomplete types are not allowed in bodies.
1899 if Ada_Version >= Ada_2012 then
1900 if Is_Tagged_Type (Typ) then
1903 elsif Nkind_In (Parent (Parent (N)),
1909 ("invalid use of untagged incomplete type&",
1913 -- The type must be completed in the current package. This
1914 -- is checked at the end of the package declaraton, when
1915 -- Taft-amendment types are identified. If the return type
1916 -- is class-wide, there is no required check, the type can
1917 -- be a bona fide TAT.
1919 if Ekind (Scope (Current_Scope)) = E_Package
1920 and then In_Private_Part (Scope (Current_Scope))
1921 and then not Is_Class_Wide_Type (Typ)
1923 Append_Elmt (Designator, Private_Dependents (Typ));
1928 ("invalid use of incomplete type&", Designator, Typ);
1933 -- Case where result definition does indicate an error
1936 Set_Etype (Designator, Any_Type);
1938 end Analyze_Return_Type;
1940 -----------------------------
1941 -- Analyze_Subprogram_Body --
1942 -----------------------------
1944 procedure Analyze_Subprogram_Body (N : Node_Id) is
1945 Loc : constant Source_Ptr := Sloc (N);
1946 Body_Spec : constant Node_Id := Specification (N);
1947 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1950 if Debug_Flag_C then
1951 Write_Str ("==> subprogram body ");
1952 Write_Name (Chars (Body_Id));
1953 Write_Str (" from ");
1954 Write_Location (Loc);
1959 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1961 -- The real work is split out into the helper, so it can do "return;"
1962 -- without skipping the debug output:
1964 Analyze_Subprogram_Body_Helper (N);
1966 if Debug_Flag_C then
1968 Write_Str ("<== subprogram body ");
1969 Write_Name (Chars (Body_Id));
1970 Write_Str (" from ");
1971 Write_Location (Loc);
1974 end Analyze_Subprogram_Body;
1976 --------------------------------------
1977 -- Analyze_Subprogram_Body_Contract --
1978 --------------------------------------
1980 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
1981 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
1982 Spec_Id : constant Entity_Id := Corresponding_Spec (Body_Decl);
1984 Ref_Depends : Node_Id := Empty;
1985 Ref_Global : Node_Id := Empty;
1988 -- When a subprogram body declaration is erroneous, its defining entity
1989 -- is left unanalyzed. There is nothing left to do in this case because
1990 -- the body lacks a contract.
1992 if not Analyzed (Body_Id) then
1996 -- Locate and store pragmas Refined_Depends and Refined_Global since
1997 -- their order of analysis matters.
1999 Prag := Classifications (Contract (Body_Id));
2000 while Present (Prag) loop
2001 if Pragma_Name (Prag) = Name_Refined_Depends then
2002 Ref_Depends := Prag;
2003 elsif Pragma_Name (Prag) = Name_Refined_Global then
2007 Prag := Next_Pragma (Prag);
2010 -- Analyze Refined_Global first as Refined_Depends may mention items
2011 -- classified in the global refinement.
2013 if Present (Ref_Global) then
2014 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2016 -- When the corresponding Global aspect/pragma references a state with
2017 -- visible refinement, the body requires Refined_Global.
2019 elsif Present (Spec_Id) then
2020 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2022 if Present (Prag) and then Contains_Refined_State (Prag) then
2024 ("body of subprogram & requires global refinement",
2025 Body_Decl, Spec_Id);
2029 -- Refined_Depends must be analyzed after Refined_Global in order to see
2030 -- the modes of all global refinements.
2032 if Present (Ref_Depends) then
2033 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2035 -- When the corresponding Depends aspect/pragma references a state with
2036 -- visible refinement, the body requires Refined_Depends.
2038 elsif Present (Spec_Id) then
2039 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2041 if Present (Prag) and then Contains_Refined_State (Prag) then
2043 ("body of subprogram & requires dependance refinement",
2044 Body_Decl, Spec_Id);
2047 end Analyze_Subprogram_Body_Contract;
2049 ------------------------------------
2050 -- Analyze_Subprogram_Body_Helper --
2051 ------------------------------------
2053 -- This procedure is called for regular subprogram bodies, generic bodies,
2054 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2055 -- specification matters, and is used to create a proper declaration for
2056 -- the subprogram, or to perform conformance checks.
2058 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2059 Loc : constant Source_Ptr := Sloc (N);
2060 Body_Spec : constant Node_Id := Specification (N);
2061 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2062 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2063 Conformant : Boolean;
2065 Prot_Typ : Entity_Id := Empty;
2066 Spec_Id : Entity_Id;
2067 Spec_Decl : Node_Id := Empty;
2069 Last_Real_Spec_Entity : Entity_Id := Empty;
2070 -- When we analyze a separate spec, the entity chain ends up containing
2071 -- the formals, as well as any itypes generated during analysis of the
2072 -- default expressions for parameters, or the arguments of associated
2073 -- precondition/postcondition pragmas (which are analyzed in the context
2074 -- of the spec since they have visibility on formals).
2076 -- These entities belong with the spec and not the body. However we do
2077 -- the analysis of the body in the context of the spec (again to obtain
2078 -- visibility to the formals), and all the entities generated during
2079 -- this analysis end up also chained to the entity chain of the spec.
2080 -- But they really belong to the body, and there is circuitry to move
2081 -- them from the spec to the body.
2083 -- However, when we do this move, we don't want to move the real spec
2084 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2085 -- variable points to the last real spec entity, so we only move those
2086 -- chained beyond that point. It is initialized to Empty to deal with
2087 -- the case where there is no separate spec.
2089 procedure Check_Anonymous_Return;
2090 -- Ada 2005: if a function returns an access type that denotes a task,
2091 -- or a type that contains tasks, we must create a master entity for
2092 -- the anonymous type, which typically will be used in an allocator
2093 -- in the body of the function.
2095 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2096 -- Look ahead to recognize a pragma that may appear after the body.
2097 -- If there is a previous spec, check that it appears in the same
2098 -- declarative part. If the pragma is Inline_Always, perform inlining
2099 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2100 -- If the body acts as a spec, and inlining is required, we create a
2101 -- subprogram declaration for it, in order to attach the body to inline.
2102 -- If pragma does not appear after the body, check whether there is
2103 -- an inline pragma before any local declarations.
2105 procedure Check_Missing_Return;
2106 -- Checks for a function with a no return statements, and also performs
2107 -- the warning checks implemented by Check_Returns. In formal mode, also
2108 -- verify that a function ends with a RETURN and that a procedure does
2109 -- not contain any RETURN.
2111 function Disambiguate_Spec return Entity_Id;
2112 -- When a primitive is declared between the private view and the full
2113 -- view of a concurrent type which implements an interface, a special
2114 -- mechanism is used to find the corresponding spec of the primitive
2117 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2118 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2119 -- incomplete types coming from a limited context and swap their limited
2120 -- views with the non-limited ones.
2122 function Is_Private_Concurrent_Primitive
2123 (Subp_Id : Entity_Id) return Boolean;
2124 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2125 -- type that implements an interface and has a private view.
2127 procedure Set_Trivial_Subprogram (N : Node_Id);
2128 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2129 -- subprogram whose body is being analyzed. N is the statement node
2130 -- causing the flag to be set, if the following statement is a return
2131 -- of an entity, we mark the entity as set in source to suppress any
2132 -- warning on the stylized use of function stubs with a dummy return.
2134 procedure Verify_Overriding_Indicator;
2135 -- If there was a previous spec, the entity has been entered in the
2136 -- current scope previously. If the body itself carries an overriding
2137 -- indicator, check that it is consistent with the known status of the
2140 ----------------------------
2141 -- Check_Anonymous_Return --
2142 ----------------------------
2144 procedure Check_Anonymous_Return is
2150 if Present (Spec_Id) then
2156 if Ekind (Scop) = E_Function
2157 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2158 and then not Is_Thunk (Scop)
2159 and then (Has_Task (Designated_Type (Etype (Scop)))
2161 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2163 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2164 and then Expander_Active
2166 -- Avoid cases with no tasking support
2168 and then RTE_Available (RE_Current_Master)
2169 and then not Restriction_Active (No_Task_Hierarchy)
2172 Make_Object_Declaration (Loc,
2173 Defining_Identifier =>
2174 Make_Defining_Identifier (Loc, Name_uMaster),
2175 Constant_Present => True,
2176 Object_Definition =>
2177 New_Reference_To (RTE (RE_Master_Id), Loc),
2179 Make_Explicit_Dereference (Loc,
2180 New_Reference_To (RTE (RE_Current_Master), Loc)));
2182 if Present (Declarations (N)) then
2183 Prepend (Decl, Declarations (N));
2185 Set_Declarations (N, New_List (Decl));
2188 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2189 Set_Has_Master_Entity (Scop);
2191 -- Now mark the containing scope as a task master
2194 while Nkind (Par) /= N_Compilation_Unit loop
2195 Par := Parent (Par);
2196 pragma Assert (Present (Par));
2198 -- If we fall off the top, we are at the outer level, and
2199 -- the environment task is our effective master, so nothing
2203 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2205 Set_Is_Task_Master (Par, True);
2210 end Check_Anonymous_Return;
2212 -------------------------
2213 -- Check_Inline_Pragma --
2214 -------------------------
2216 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2220 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2221 -- True when N is a pragma Inline or Inline_Always that applies
2222 -- to this subprogram.
2224 -----------------------
2225 -- Is_Inline_Pragma --
2226 -----------------------
2228 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2231 Nkind (N) = N_Pragma
2233 (Pragma_Name (N) = Name_Inline_Always
2236 and then Pragma_Name (N) = Name_Inline))
2239 (Expression (First (Pragma_Argument_Associations (N)))) =
2241 end Is_Inline_Pragma;
2243 -- Start of processing for Check_Inline_Pragma
2246 if not Expander_Active then
2250 if Is_List_Member (N)
2251 and then Present (Next (N))
2252 and then Is_Inline_Pragma (Next (N))
2256 elsif Nkind (N) /= N_Subprogram_Body_Stub
2257 and then Present (Declarations (N))
2258 and then Is_Inline_Pragma (First (Declarations (N)))
2260 Prag := First (Declarations (N));
2266 if Present (Prag) then
2267 if Present (Spec_Id) then
2268 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2273 -- Create a subprogram declaration, to make treatment uniform
2276 Subp : constant Entity_Id :=
2277 Make_Defining_Identifier (Loc, Chars (Body_Id));
2278 Decl : constant Node_Id :=
2279 Make_Subprogram_Declaration (Loc,
2281 New_Copy_Tree (Specification (N)));
2284 Set_Defining_Unit_Name (Specification (Decl), Subp);
2286 if Present (First_Formal (Body_Id)) then
2287 Plist := Copy_Parameter_List (Body_Id);
2288 Set_Parameter_Specifications
2289 (Specification (Decl), Plist);
2292 Insert_Before (N, Decl);
2295 Set_Has_Pragma_Inline (Subp);
2297 if Pragma_Name (Prag) = Name_Inline_Always then
2298 Set_Is_Inlined (Subp);
2299 Set_Has_Pragma_Inline_Always (Subp);
2306 end Check_Inline_Pragma;
2308 --------------------------
2309 -- Check_Missing_Return --
2310 --------------------------
2312 procedure Check_Missing_Return is
2314 Missing_Ret : Boolean;
2317 if Nkind (Body_Spec) = N_Function_Specification then
2318 if Present (Spec_Id) then
2324 if Return_Present (Id) then
2325 Check_Returns (HSS, 'F', Missing_Ret);
2328 Set_Has_Missing_Return (Id);
2331 elsif Is_Generic_Subprogram (Id)
2332 or else not Is_Machine_Code_Subprogram (Id)
2334 Error_Msg_N ("missing RETURN statement in function body", N);
2337 -- If procedure with No_Return, check returns
2339 elsif Nkind (Body_Spec) = N_Procedure_Specification
2340 and then Present (Spec_Id)
2341 and then No_Return (Spec_Id)
2343 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2346 -- Special checks in SPARK mode
2348 if Nkind (Body_Spec) = N_Function_Specification then
2350 -- In SPARK mode, last statement of a function should be a return
2353 Stat : constant Node_Id := Last_Source_Statement (HSS);
2356 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2357 N_Extended_Return_Statement)
2359 Check_SPARK_Restriction
2360 ("last statement in function should be RETURN", Stat);
2364 -- In SPARK mode, verify that a procedure has no return
2366 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2367 if Present (Spec_Id) then
2373 -- Would be nice to point to return statement here, can we
2374 -- borrow the Check_Returns procedure here ???
2376 if Return_Present (Id) then
2377 Check_SPARK_Restriction
2378 ("procedure should not have RETURN", N);
2381 end Check_Missing_Return;
2383 -----------------------
2384 -- Disambiguate_Spec --
2385 -----------------------
2387 function Disambiguate_Spec return Entity_Id is
2388 Priv_Spec : Entity_Id;
2391 procedure Replace_Types (To_Corresponding : Boolean);
2392 -- Depending on the flag, replace the type of formal parameters of
2393 -- Body_Id if it is a concurrent type implementing interfaces with
2394 -- the corresponding record type or the other way around.
2396 procedure Replace_Types (To_Corresponding : Boolean) is
2398 Formal_Typ : Entity_Id;
2401 Formal := First_Formal (Body_Id);
2402 while Present (Formal) loop
2403 Formal_Typ := Etype (Formal);
2405 if Is_Class_Wide_Type (Formal_Typ) then
2406 Formal_Typ := Root_Type (Formal_Typ);
2409 -- From concurrent type to corresponding record
2411 if To_Corresponding then
2412 if Is_Concurrent_Type (Formal_Typ)
2413 and then Present (Corresponding_Record_Type (Formal_Typ))
2414 and then Present (Interfaces (
2415 Corresponding_Record_Type (Formal_Typ)))
2418 Corresponding_Record_Type (Formal_Typ));
2421 -- From corresponding record to concurrent type
2424 if Is_Concurrent_Record_Type (Formal_Typ)
2425 and then Present (Interfaces (Formal_Typ))
2428 Corresponding_Concurrent_Type (Formal_Typ));
2432 Next_Formal (Formal);
2436 -- Start of processing for Disambiguate_Spec
2439 -- Try to retrieve the specification of the body as is. All error
2440 -- messages are suppressed because the body may not have a spec in
2441 -- its current state.
2443 Spec_N := Find_Corresponding_Spec (N, False);
2445 -- It is possible that this is the body of a primitive declared
2446 -- between a private and a full view of a concurrent type. The
2447 -- controlling parameter of the spec carries the concurrent type,
2448 -- not the corresponding record type as transformed by Analyze_
2449 -- Subprogram_Specification. In such cases, we undo the change
2450 -- made by the analysis of the specification and try to find the
2453 -- Note that wrappers already have their corresponding specs and
2454 -- bodies set during their creation, so if the candidate spec is
2455 -- a wrapper, then we definitely need to swap all types to their
2456 -- original concurrent status.
2459 or else Is_Primitive_Wrapper (Spec_N)
2461 -- Restore all references of corresponding record types to the
2462 -- original concurrent types.
2464 Replace_Types (To_Corresponding => False);
2465 Priv_Spec := Find_Corresponding_Spec (N, False);
2467 -- The current body truly belongs to a primitive declared between
2468 -- a private and a full view. We leave the modified body as is,
2469 -- and return the true spec.
2471 if Present (Priv_Spec)
2472 and then Is_Private_Primitive (Priv_Spec)
2477 -- In case that this is some sort of error, restore the original
2478 -- state of the body.
2480 Replace_Types (To_Corresponding => True);
2484 end Disambiguate_Spec;
2486 ----------------------------
2487 -- Exchange_Limited_Views --
2488 ----------------------------
2490 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2491 procedure Detect_And_Exchange (Id : Entity_Id);
2492 -- Determine whether Id's type denotes an incomplete type associated
2493 -- with a limited with clause and exchange the limited view with the
2496 -------------------------
2497 -- Detect_And_Exchange --
2498 -------------------------
2500 procedure Detect_And_Exchange (Id : Entity_Id) is
2501 Typ : constant Entity_Id := Etype (Id);
2504 if Ekind (Typ) = E_Incomplete_Type
2505 and then From_Limited_With (Typ)
2506 and then Present (Non_Limited_View (Typ))
2508 Set_Etype (Id, Non_Limited_View (Typ));
2510 end Detect_And_Exchange;
2516 -- Start of processing for Exchange_Limited_Views
2519 if No (Subp_Id) then
2522 -- Do not process subprogram bodies as they already use the non-
2523 -- limited view of types.
2525 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2529 -- Examine all formals and swap views when applicable
2531 Formal := First_Formal (Subp_Id);
2532 while Present (Formal) loop
2533 Detect_And_Exchange (Formal);
2535 Next_Formal (Formal);
2538 -- Process the return type of a function
2540 if Ekind (Subp_Id) = E_Function then
2541 Detect_And_Exchange (Subp_Id);
2543 end Exchange_Limited_Views;
2545 -------------------------------------
2546 -- Is_Private_Concurrent_Primitive --
2547 -------------------------------------
2549 function Is_Private_Concurrent_Primitive
2550 (Subp_Id : Entity_Id) return Boolean
2552 Formal_Typ : Entity_Id;
2555 if Present (First_Formal (Subp_Id)) then
2556 Formal_Typ := Etype (First_Formal (Subp_Id));
2558 if Is_Concurrent_Record_Type (Formal_Typ) then
2559 if Is_Class_Wide_Type (Formal_Typ) then
2560 Formal_Typ := Root_Type (Formal_Typ);
2563 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2566 -- The type of the first formal is a concurrent tagged type with
2570 Is_Concurrent_Type (Formal_Typ)
2571 and then Is_Tagged_Type (Formal_Typ)
2572 and then Has_Private_Declaration (Formal_Typ);
2576 end Is_Private_Concurrent_Primitive;
2578 ----------------------------
2579 -- Set_Trivial_Subprogram --
2580 ----------------------------
2582 procedure Set_Trivial_Subprogram (N : Node_Id) is
2583 Nxt : constant Node_Id := Next (N);
2586 Set_Is_Trivial_Subprogram (Body_Id);
2588 if Present (Spec_Id) then
2589 Set_Is_Trivial_Subprogram (Spec_Id);
2593 and then Nkind (Nxt) = N_Simple_Return_Statement
2594 and then No (Next (Nxt))
2595 and then Present (Expression (Nxt))
2596 and then Is_Entity_Name (Expression (Nxt))
2598 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2600 end Set_Trivial_Subprogram;
2602 ---------------------------------
2603 -- Verify_Overriding_Indicator --
2604 ---------------------------------
2606 procedure Verify_Overriding_Indicator is
2608 if Must_Override (Body_Spec) then
2609 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2610 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2614 elsif not Present (Overridden_Operation (Spec_Id)) then
2616 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2619 elsif Must_Not_Override (Body_Spec) then
2620 if Present (Overridden_Operation (Spec_Id)) then
2622 ("subprogram& overrides inherited operation",
2623 Body_Spec, Spec_Id);
2625 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2626 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2629 ("subprogram & overrides predefined operator ",
2630 Body_Spec, Spec_Id);
2632 -- If this is not a primitive operation or protected subprogram,
2633 -- then the overriding indicator is altogether illegal.
2635 elsif not Is_Primitive (Spec_Id)
2636 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2639 ("overriding indicator only allowed " &
2640 "if subprogram is primitive",
2645 and then Present (Overridden_Operation (Spec_Id))
2647 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2648 Style.Missing_Overriding (N, Body_Id);
2651 and then Can_Override_Operator (Spec_Id)
2652 and then not Is_Predefined_File_Name
2653 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2655 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2656 Style.Missing_Overriding (N, Body_Id);
2658 end Verify_Overriding_Indicator;
2660 -- Start of processing for Analyze_Subprogram_Body_Helper
2663 -- Generic subprograms are handled separately. They always have a
2664 -- generic specification. Determine whether current scope has a
2665 -- previous declaration.
2667 -- If the subprogram body is defined within an instance of the same
2668 -- name, the instance appears as a package renaming, and will be hidden
2669 -- within the subprogram.
2671 if Present (Prev_Id)
2672 and then not Is_Overloadable (Prev_Id)
2673 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2674 or else Comes_From_Source (Prev_Id))
2676 if Is_Generic_Subprogram (Prev_Id) then
2678 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2679 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2681 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2683 if Nkind (N) = N_Subprogram_Body then
2684 HSS := Handled_Statement_Sequence (N);
2685 Check_Missing_Return;
2691 -- Previous entity conflicts with subprogram name. Attempting to
2692 -- enter name will post error.
2694 Enter_Name (Body_Id);
2698 -- Non-generic case, find the subprogram declaration, if one was seen,
2699 -- or enter new overloaded entity in the current scope. If the
2700 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2701 -- part of the context of one of its subunits. No need to redo the
2704 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2708 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2710 if Nkind (N) = N_Subprogram_Body_Stub
2711 or else No (Corresponding_Spec (N))
2713 if Is_Private_Concurrent_Primitive (Body_Id) then
2714 Spec_Id := Disambiguate_Spec;
2716 Spec_Id := Find_Corresponding_Spec (N);
2719 -- If this is a duplicate body, no point in analyzing it
2721 if Error_Posted (N) then
2725 -- A subprogram body should cause freezing of its own declaration,
2726 -- but if there was no previous explicit declaration, then the
2727 -- subprogram will get frozen too late (there may be code within
2728 -- the body that depends on the subprogram having been frozen,
2729 -- such as uses of extra formals), so we force it to be frozen
2730 -- here. Same holds if the body and spec are compilation units.
2731 -- Finally, if the return type is an anonymous access to protected
2732 -- subprogram, it must be frozen before the body because its
2733 -- expansion has generated an equivalent type that is used when
2734 -- elaborating the body.
2736 -- An exception in the case of Ada 2012, AI05-177: The bodies
2737 -- created for expression functions do not freeze.
2740 and then Nkind (Original_Node (N)) /= N_Expression_Function
2742 Freeze_Before (N, Body_Id);
2744 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2745 Freeze_Before (N, Spec_Id);
2747 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2748 Freeze_Before (N, Etype (Body_Id));
2752 Spec_Id := Corresponding_Spec (N);
2756 -- Language-defined aspects cannot appear in a subprogram body [stub] if
2757 -- the subprogram has a separate spec. Certainly implementation-defined
2758 -- aspects are allowed to appear (per Aspects_On_Body_Of_Stub_OK).
2760 if Has_Aspects (N) then
2761 if Present (Spec_Id)
2762 and then not Aspects_On_Body_Or_Stub_OK (N)
2764 -- Do not emit an error on a subprogram body stub that act as
2767 and then Nkind (Parent (Parent (Spec_Id))) /= N_Subprogram_Body_Stub
2770 ("aspect specifications must appear in subprogram declaration",
2773 -- Delay the analysis of aspect specifications that apply to a body
2774 -- stub until the proper body is analyzed. If the corresponding body
2775 -- is missing, the aspects are still analyzed in Analyze_Proper_Body.
2777 elsif Nkind (N) in N_Body_Stub then
2781 Analyze_Aspect_Specifications (N, Body_Id);
2785 -- Previously we scanned the body to look for nested subprograms, and
2786 -- rejected an inline directive if nested subprograms were present,
2787 -- because the back-end would generate conflicting symbols for the
2788 -- nested bodies. This is now unnecessary.
2790 -- Look ahead to recognize a pragma Inline that appears after the body
2792 Check_Inline_Pragma (Spec_Id);
2794 -- Deal with special case of a fully private operation in the body of
2795 -- the protected type. We must create a declaration for the subprogram,
2796 -- in order to attach the protected subprogram that will be used in
2797 -- internal calls. We exclude compiler generated bodies from the
2798 -- expander since the issue does not arise for those cases.
2801 and then Comes_From_Source (N)
2802 and then Is_Protected_Type (Current_Scope)
2804 Spec_Id := Build_Private_Protected_Declaration (N);
2807 -- If a separate spec is present, then deal with freezing issues
2809 if Present (Spec_Id) then
2810 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2811 Verify_Overriding_Indicator;
2813 -- In general, the spec will be frozen when we start analyzing the
2814 -- body. However, for internally generated operations, such as
2815 -- wrapper functions for inherited operations with controlling
2816 -- results, the spec may not have been frozen by the time we expand
2817 -- the freeze actions that include the bodies. In particular, extra
2818 -- formals for accessibility or for return-in-place may need to be
2819 -- generated. Freeze nodes, if any, are inserted before the current
2820 -- body. These freeze actions are also needed in ASIS mode to enable
2821 -- the proper back-annotations.
2823 if not Is_Frozen (Spec_Id)
2824 and then (Expander_Active or ASIS_Mode)
2826 -- Force the generation of its freezing node to ensure proper
2827 -- management of access types in the backend.
2829 -- This is definitely needed for some cases, but it is not clear
2830 -- why, to be investigated further???
2832 Set_Has_Delayed_Freeze (Spec_Id);
2833 Freeze_Before (N, Spec_Id);
2837 -- Mark presence of postcondition procedure in current scope and mark
2838 -- the procedure itself as needing debug info. The latter is important
2839 -- when analyzing decision coverage (for example, for MC/DC coverage).
2841 if Chars (Body_Id) = Name_uPostconditions then
2842 Set_Has_Postconditions (Current_Scope);
2843 Set_Debug_Info_Needed (Body_Id);
2846 -- Place subprogram on scope stack, and make formals visible. If there
2847 -- is a spec, the visible entity remains that of the spec.
2849 if Present (Spec_Id) then
2850 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2852 if Is_Child_Unit (Spec_Id) then
2853 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2857 Style.Check_Identifier (Body_Id, Spec_Id);
2860 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2861 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2863 if Is_Abstract_Subprogram (Spec_Id) then
2864 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2868 Set_Convention (Body_Id, Convention (Spec_Id));
2869 Set_Has_Completion (Spec_Id);
2871 if Is_Protected_Type (Scope (Spec_Id)) then
2872 Prot_Typ := Scope (Spec_Id);
2875 -- If this is a body generated for a renaming, do not check for
2876 -- full conformance. The check is redundant, because the spec of
2877 -- the body is a copy of the spec in the renaming declaration,
2878 -- and the test can lead to spurious errors on nested defaults.
2880 if Present (Spec_Decl)
2881 and then not Comes_From_Source (N)
2883 (Nkind (Original_Node (Spec_Decl)) =
2884 N_Subprogram_Renaming_Declaration
2885 or else (Present (Corresponding_Body (Spec_Decl))
2887 Nkind (Unit_Declaration_Node
2888 (Corresponding_Body (Spec_Decl))) =
2889 N_Subprogram_Renaming_Declaration))
2893 -- Conversely, the spec may have been generated for specless body
2894 -- with an inline pragma.
2896 elsif Comes_From_Source (N)
2897 and then not Comes_From_Source (Spec_Id)
2898 and then Has_Pragma_Inline (Spec_Id)
2905 Fully_Conformant, True, Conformant, Body_Id);
2908 -- If the body is not fully conformant, we have to decide if we
2909 -- should analyze it or not. If it has a really messed up profile
2910 -- then we probably should not analyze it, since we will get too
2911 -- many bogus messages.
2913 -- Our decision is to go ahead in the non-fully conformant case
2914 -- only if it is at least mode conformant with the spec. Note
2915 -- that the call to Check_Fully_Conformant has issued the proper
2916 -- error messages to complain about the lack of conformance.
2919 and then not Mode_Conformant (Body_Id, Spec_Id)
2925 if Spec_Id /= Body_Id then
2926 Reference_Body_Formals (Spec_Id, Body_Id);
2929 Set_Ekind (Body_Id, E_Subprogram_Body);
2931 if Nkind (N) = N_Subprogram_Body_Stub then
2932 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
2937 Set_Corresponding_Spec (N, Spec_Id);
2939 -- Ada 2005 (AI-345): If the operation is a primitive operation
2940 -- of a concurrent type, the type of the first parameter has been
2941 -- replaced with the corresponding record, which is the proper
2942 -- run-time structure to use. However, within the body there may
2943 -- be uses of the formals that depend on primitive operations
2944 -- of the type (in particular calls in prefixed form) for which
2945 -- we need the original concurrent type. The operation may have
2946 -- several controlling formals, so the replacement must be done
2949 if Comes_From_Source (Spec_Id)
2950 and then Present (First_Entity (Spec_Id))
2951 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2952 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2953 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
2954 and then Present (Corresponding_Concurrent_Type
2955 (Etype (First_Entity (Spec_Id))))
2958 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2962 Form := First_Formal (Spec_Id);
2963 while Present (Form) loop
2964 if Etype (Form) = Typ then
2965 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2973 -- Make the formals visible, and place subprogram on scope stack.
2974 -- This is also the point at which we set Last_Real_Spec_Entity
2975 -- to mark the entities which will not be moved to the body.
2977 Install_Formals (Spec_Id);
2978 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2980 -- Within an instance, add local renaming declarations so that
2981 -- gdb can retrieve the values of actuals more easily. This is
2982 -- only relevant if generating code (and indeed we definitely
2983 -- do not want these definitions -gnatc mode, because that would
2986 if Is_Generic_Instance (Spec_Id)
2987 and then Is_Wrapper_Package (Current_Scope)
2988 and then Expander_Active
2990 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2993 Push_Scope (Spec_Id);
2995 -- Set SPARK_Mode from spec if spec had a SPARK_Mode pragma
2997 if Present (SPARK_Pragma (Spec_Id)) then
2998 SPARK_Mode_Pragma := SPARK_Pragma (Spec_Id);
2999 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Mode_Pragma);
3000 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
3001 Set_SPARK_Pragma_Inherited (Body_Id, True);
3003 -- Otherwise set from context
3006 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3007 Set_SPARK_Pragma_Inherited (Body_Id, True);
3010 -- Make sure that the subprogram is immediately visible. For
3011 -- child units that have no separate spec this is indispensable.
3012 -- Otherwise it is safe albeit redundant.
3014 Set_Is_Immediately_Visible (Spec_Id);
3017 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3018 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3019 Set_Scope (Body_Id, Scope (Spec_Id));
3020 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3022 -- Case of subprogram body with no previous spec
3025 -- Check for style warning required
3029 -- Only apply check for source level subprograms for which checks
3030 -- have not been suppressed.
3032 and then Comes_From_Source (Body_Id)
3033 and then not Suppress_Style_Checks (Body_Id)
3035 -- No warnings within an instance
3037 and then not In_Instance
3039 -- No warnings for expression functions
3041 and then Nkind (Original_Node (N)) /= N_Expression_Function
3043 Style.Body_With_No_Spec (N);
3046 New_Overloaded_Entity (Body_Id);
3048 if Nkind (N) /= N_Subprogram_Body_Stub then
3049 Set_Acts_As_Spec (N);
3050 Generate_Definition (Body_Id);
3051 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3053 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3054 Install_Formals (Body_Id);
3055 Push_Scope (Body_Id);
3058 -- For stubs and bodies with no previous spec, generate references to
3061 Generate_Reference_To_Formals (Body_Id);
3064 -- If the return type is an anonymous access type whose designated type
3065 -- is the limited view of a class-wide type and the non-limited view is
3066 -- available, update the return type accordingly.
3068 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3074 Rtyp := Etype (Current_Scope);
3076 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3077 Etyp := Directly_Designated_Type (Rtyp);
3079 if Is_Class_Wide_Type (Etyp)
3080 and then From_Limited_With (Etyp)
3082 Set_Directly_Designated_Type
3083 (Etype (Current_Scope), Available_View (Etyp));
3089 -- If this is the proper body of a stub, we must verify that the stub
3090 -- conforms to the body, and to the previous spec if one was present.
3091 -- We know already that the body conforms to that spec. This test is
3092 -- only required for subprograms that come from source.
3094 if Nkind (Parent (N)) = N_Subunit
3095 and then Comes_From_Source (N)
3096 and then not Error_Posted (Body_Id)
3097 and then Nkind (Corresponding_Stub (Parent (N))) =
3098 N_Subprogram_Body_Stub
3101 Old_Id : constant Entity_Id :=
3103 (Specification (Corresponding_Stub (Parent (N))));
3105 Conformant : Boolean := False;
3108 if No (Spec_Id) then
3109 Check_Fully_Conformant (Body_Id, Old_Id);
3113 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3115 if not Conformant then
3117 -- The stub was taken to be a new declaration. Indicate that
3120 Set_Has_Completion (Old_Id, False);
3126 Set_Has_Completion (Body_Id);
3127 Check_Eliminated (Body_Id);
3129 if Nkind (N) = N_Subprogram_Body_Stub then
3133 -- Handle frontend inlining. There is no need to prepare us for inlining
3134 -- if we will not generate the code.
3138 if not Debug_Flag_Dot_K then
3139 if Present (Spec_Id)
3140 and then Expander_Active
3142 (Has_Pragma_Inline_Always (Spec_Id)
3143 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3145 Build_Body_To_Inline (N, Spec_Id);
3150 elsif Expander_Active
3151 and then Serious_Errors_Detected = 0
3152 and then Present (Spec_Id)
3153 and then Has_Pragma_Inline (Spec_Id)
3155 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3158 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3159 -- of the specification we have to install the private withed units.
3160 -- This holds for child units as well.
3162 if Is_Compilation_Unit (Body_Id)
3163 or else Nkind (Parent (N)) = N_Compilation_Unit
3165 Install_Private_With_Clauses (Body_Id);
3168 Check_Anonymous_Return;
3170 -- Set the Protected_Formal field of each extra formal of the protected
3171 -- subprogram to reference the corresponding extra formal of the
3172 -- subprogram that implements it. For regular formals this occurs when
3173 -- the protected subprogram's declaration is expanded, but the extra
3174 -- formals don't get created until the subprogram is frozen. We need to
3175 -- do this before analyzing the protected subprogram's body so that any
3176 -- references to the original subprogram's extra formals will be changed
3177 -- refer to the implementing subprogram's formals (see Expand_Formal).
3179 if Present (Spec_Id)
3180 and then Is_Protected_Type (Scope (Spec_Id))
3181 and then Present (Protected_Body_Subprogram (Spec_Id))
3184 Impl_Subp : constant Entity_Id :=
3185 Protected_Body_Subprogram (Spec_Id);
3186 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3187 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3189 while Present (Prot_Ext_Formal) loop
3190 pragma Assert (Present (Impl_Ext_Formal));
3191 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3192 Next_Formal_With_Extras (Prot_Ext_Formal);
3193 Next_Formal_With_Extras (Impl_Ext_Formal);
3198 -- Now we can go on to analyze the body
3200 HSS := Handled_Statement_Sequence (N);
3201 Set_Actual_Subtypes (N, Current_Scope);
3203 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3204 -- invariants and predicates associated with the body and its spec.
3205 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3206 -- prepares the contract assertions for generic subprograms or for ASIS.
3207 -- Do not generate contract checks in SPARK mode.
3209 if not GNATprove_Mode then
3210 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3213 -- Add a declaration for the Protection object, renaming declarations
3214 -- for discriminals and privals and finally a declaration for the entry
3215 -- family index (if applicable). This form of early expansion is done
3216 -- when the Expander is active because Install_Private_Data_Declarations
3217 -- references entities which were created during regular expansion. The
3218 -- body may be the rewritting of an expression function, and we need to
3219 -- verify that the original node is in the source.
3222 and then Comes_From_Source (Original_Node (N))
3223 and then Present (Prot_Typ)
3224 and then Present (Spec_Id)
3225 and then not Is_Eliminated (Spec_Id)
3227 Install_Private_Data_Declarations
3228 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3231 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3232 -- may now appear in parameter and result profiles. Since the analysis
3233 -- of a subprogram body may use the parameter and result profile of the
3234 -- spec, swap any limited views with their non-limited counterpart.
3236 if Ada_Version >= Ada_2012 then
3237 Exchange_Limited_Views (Spec_Id);
3240 -- Analyze the declarations (this call will analyze the precondition
3241 -- Check pragmas we prepended to the list, as well as the declaration
3242 -- of the _Postconditions procedure).
3244 Analyze_Declarations (Declarations (N));
3246 -- Check completion, and analyze the statements
3249 Inspect_Deferred_Constant_Completion (Declarations (N));
3252 -- Deal with end of scope processing for the body
3254 Process_End_Label (HSS, 't', Current_Scope);
3256 Check_Subprogram_Order (N);
3257 Set_Analyzed (Body_Id);
3259 -- If we have a separate spec, then the analysis of the declarations
3260 -- caused the entities in the body to be chained to the spec id, but
3261 -- we want them chained to the body id. Only the formal parameters
3262 -- end up chained to the spec id in this case.
3264 if Present (Spec_Id) then
3266 -- We must conform to the categorization of our spec
3268 Validate_Categorization_Dependency (N, Spec_Id);
3270 -- And if this is a child unit, the parent units must conform
3272 if Is_Child_Unit (Spec_Id) then
3273 Validate_Categorization_Dependency
3274 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3277 -- Here is where we move entities from the spec to the body
3279 -- Case where there are entities that stay with the spec
3281 if Present (Last_Real_Spec_Entity) then
3283 -- No body entities (happens when the only real spec entities come
3284 -- from precondition and postcondition pragmas).
3286 if No (Last_Entity (Body_Id)) then
3288 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3290 -- Body entities present (formals), so chain stuff past them
3294 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3297 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3298 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3299 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3301 -- Case where there are no spec entities, in this case there can be
3302 -- no body entities either, so just move everything.
3305 pragma Assert (No (Last_Entity (Body_Id)));
3306 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3307 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3308 Set_First_Entity (Spec_Id, Empty);
3309 Set_Last_Entity (Spec_Id, Empty);
3313 Check_Missing_Return;
3315 -- Now we are going to check for variables that are never modified in
3316 -- the body of the procedure. But first we deal with a special case
3317 -- where we want to modify this check. If the body of the subprogram
3318 -- starts with a raise statement or its equivalent, or if the body
3319 -- consists entirely of a null statement, then it is pretty obvious
3320 -- that it is OK to not reference the parameters. For example, this
3321 -- might be the following common idiom for a stubbed function:
3322 -- statement of the procedure raises an exception. In particular this
3323 -- deals with the common idiom of a stubbed function, which might
3324 -- appear as something like:
3326 -- function F (A : Integer) return Some_Type;
3329 -- raise Program_Error;
3333 -- Here the purpose of X is simply to satisfy the annoying requirement
3334 -- in Ada that there be at least one return, and we certainly do not
3335 -- want to go posting warnings on X that it is not initialized! On
3336 -- the other hand, if X is entirely unreferenced that should still
3339 -- What we do is to detect these cases, and if we find them, flag the
3340 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3341 -- suppress unwanted warnings. For the case of the function stub above
3342 -- we have a special test to set X as apparently assigned to suppress
3349 -- Skip initial labels (for one thing this occurs when we are in
3350 -- front end ZCX mode, but in any case it is irrelevant), and also
3351 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3353 Stm := First (Statements (HSS));
3354 while Nkind (Stm) = N_Label
3355 or else Nkind (Stm) in N_Push_xxx_Label
3360 -- Do the test on the original statement before expansion
3363 Ostm : constant Node_Id := Original_Node (Stm);
3366 -- If explicit raise statement, turn on flag
3368 if Nkind (Ostm) = N_Raise_Statement then
3369 Set_Trivial_Subprogram (Stm);
3371 -- If null statement, and no following statements, turn on flag
3373 elsif Nkind (Stm) = N_Null_Statement
3374 and then Comes_From_Source (Stm)
3375 and then No (Next (Stm))
3377 Set_Trivial_Subprogram (Stm);
3379 -- Check for explicit call cases which likely raise an exception
3381 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3382 if Is_Entity_Name (Name (Ostm)) then
3384 Ent : constant Entity_Id := Entity (Name (Ostm));
3387 -- If the procedure is marked No_Return, then likely it
3388 -- raises an exception, but in any case it is not coming
3389 -- back here, so turn on the flag.
3392 and then Ekind (Ent) = E_Procedure
3393 and then No_Return (Ent)
3395 Set_Trivial_Subprogram (Stm);
3403 -- Check for variables that are never modified
3409 -- If there is a separate spec, then transfer Never_Set_In_Source
3410 -- flags from out parameters to the corresponding entities in the
3411 -- body. The reason we do that is we want to post error flags on
3412 -- the body entities, not the spec entities.
3414 if Present (Spec_Id) then
3415 E1 := First_Entity (Spec_Id);
3416 while Present (E1) loop
3417 if Ekind (E1) = E_Out_Parameter then
3418 E2 := First_Entity (Body_Id);
3419 while Present (E2) loop
3420 exit when Chars (E1) = Chars (E2);
3424 if Present (E2) then
3425 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3433 -- Check references in body
3435 Check_References (Body_Id);
3437 end Analyze_Subprogram_Body_Helper;
3439 ---------------------------------
3440 -- Analyze_Subprogram_Contract --
3441 ---------------------------------
3443 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3444 Items : constant Node_Id := Contract (Subp);
3445 Case_Prag : Node_Id := Empty;
3446 Depends : Node_Id := Empty;
3447 Global : Node_Id := Empty;
3449 Post_Prag : Node_Id := Empty;
3451 Seen_In_Case : Boolean := False;
3452 Seen_In_Post : Boolean := False;
3455 if Present (Items) then
3457 -- Analyze pre- and postconditions
3459 Prag := Pre_Post_Conditions (Items);
3460 while Present (Prag) loop
3461 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3463 -- Verify whether a postcondition mentions attribute 'Result and
3464 -- its expression introduces a post-state.
3466 if Warn_On_Suspicious_Contract
3467 and then Pragma_Name (Prag) = Name_Postcondition
3470 Check_Result_And_Post_State (Prag, Seen_In_Post);
3473 Prag := Next_Pragma (Prag);
3476 -- Analyze contract-cases and test-cases
3478 Prag := Contract_Test_Cases (Items);
3479 while Present (Prag) loop
3480 Nam := Pragma_Name (Prag);
3482 if Nam = Name_Contract_Cases then
3483 Analyze_Contract_Cases_In_Decl_Part (Prag);
3485 -- Verify whether contract-cases mention attribute 'Result and
3486 -- its expression introduces a post-state. Perform the check
3487 -- only when the pragma is legal.
3489 if Warn_On_Suspicious_Contract
3490 and then not Error_Posted (Prag)
3493 Check_Result_And_Post_State (Prag, Seen_In_Case);
3497 pragma Assert (Nam = Name_Test_Case);
3498 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3501 Prag := Next_Pragma (Prag);
3504 -- Analyze classification pragmas
3506 Prag := Classifications (Items);
3507 while Present (Prag) loop
3508 Nam := Pragma_Name (Prag);
3510 if Nam = Name_Depends then
3512 else pragma Assert (Nam = Name_Global);
3516 Prag := Next_Pragma (Prag);
3519 -- Analyze Global first as Depends may mention items classified in
3520 -- the global categorization.
3522 if Present (Global) then
3523 Analyze_Global_In_Decl_Part (Global);
3526 -- Depends must be analyzed after Global in order to see the modes of
3527 -- all global items.
3529 if Present (Depends) then
3530 Analyze_Depends_In_Decl_Part (Depends);
3534 -- Emit an error when neither the postconditions nor the contract-cases
3535 -- mention attribute 'Result in the context of a function.
3537 if Warn_On_Suspicious_Contract
3538 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3540 if Present (Case_Prag)
3541 and then not Seen_In_Case
3542 and then Present (Post_Prag)
3543 and then not Seen_In_Post
3546 ("neither function postcondition nor contract cases mention "
3547 & "result?T?", Post_Prag);
3549 elsif Present (Case_Prag) and then not Seen_In_Case then
3551 ("contract cases do not mention result?T?", Case_Prag);
3553 elsif Present (Post_Prag) and then not Seen_In_Post then
3555 ("function postcondition does not mention result?T?", Post_Prag);
3558 end Analyze_Subprogram_Contract;
3560 ------------------------------------
3561 -- Analyze_Subprogram_Declaration --
3562 ------------------------------------
3564 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3565 Scop : constant Entity_Id := Current_Scope;
3566 Designator : Entity_Id;
3568 Is_Completion : Boolean;
3569 -- Indicates whether a null procedure declaration is a completion
3572 -- Null procedures are not allowed in SPARK
3574 if Nkind (Specification (N)) = N_Procedure_Specification
3575 and then Null_Present (Specification (N))
3577 Check_SPARK_Restriction ("null procedure is not allowed", N);
3579 if Is_Protected_Type (Current_Scope) then
3580 Error_Msg_N ("protected operation cannot be a null procedure", N);
3583 Analyze_Null_Procedure (N, Is_Completion);
3585 if Is_Completion then
3587 -- The null procedure acts as a body, nothing further is needed.
3593 Designator := Analyze_Subprogram_Specification (Specification (N));
3595 -- A reference may already have been generated for the unit name, in
3596 -- which case the following call is redundant. However it is needed for
3597 -- declarations that are the rewriting of an expression function.
3599 Generate_Definition (Designator);
3601 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
3602 Set_SPARK_Pragma_Inherited (Designator, True);
3604 if Debug_Flag_C then
3605 Write_Str ("==> subprogram spec ");
3606 Write_Name (Chars (Designator));
3607 Write_Str (" from ");
3608 Write_Location (Sloc (N));
3613 Validate_RCI_Subprogram_Declaration (N);
3614 New_Overloaded_Entity (Designator);
3615 Check_Delayed_Subprogram (Designator);
3617 -- If the type of the first formal of the current subprogram is a
3618 -- non-generic tagged private type, mark the subprogram as being a
3619 -- private primitive. Ditto if this is a function with controlling
3620 -- result, and the return type is currently private. In both cases,
3621 -- the type of the controlling argument or result must be in the
3622 -- current scope for the operation to be primitive.
3624 if Has_Controlling_Result (Designator)
3625 and then Is_Private_Type (Etype (Designator))
3626 and then Scope (Etype (Designator)) = Current_Scope
3627 and then not Is_Generic_Actual_Type (Etype (Designator))
3629 Set_Is_Private_Primitive (Designator);
3631 elsif Present (First_Formal (Designator)) then
3633 Formal_Typ : constant Entity_Id :=
3634 Etype (First_Formal (Designator));
3636 Set_Is_Private_Primitive (Designator,
3637 Is_Tagged_Type (Formal_Typ)
3638 and then Scope (Formal_Typ) = Current_Scope
3639 and then Is_Private_Type (Formal_Typ)
3640 and then not Is_Generic_Actual_Type (Formal_Typ));
3644 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3647 if Ada_Version >= Ada_2005
3648 and then Comes_From_Source (N)
3649 and then Is_Dispatching_Operation (Designator)
3656 if Has_Controlling_Result (Designator) then
3657 Etyp := Etype (Designator);
3660 E := First_Entity (Designator);
3662 and then Is_Formal (E)
3663 and then not Is_Controlling_Formal (E)
3671 if Is_Access_Type (Etyp) then
3672 Etyp := Directly_Designated_Type (Etyp);
3675 if Is_Interface (Etyp)
3676 and then not Is_Abstract_Subprogram (Designator)
3677 and then not (Ekind (Designator) = E_Procedure
3678 and then Null_Present (Specification (N)))
3680 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3682 -- Specialize error message based on procedures vs. functions,
3683 -- since functions can't be null subprograms.
3685 if Ekind (Designator) = E_Procedure then
3687 ("interface procedure % must be abstract or null", N);
3689 Error_Msg_N ("interface function % must be abstract", N);
3695 -- What is the following code for, it used to be
3697 -- ??? Set_Suppress_Elaboration_Checks
3698 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3700 -- The following seems equivalent, but a bit dubious
3702 if Elaboration_Checks_Suppressed (Designator) then
3703 Set_Kill_Elaboration_Checks (Designator);
3706 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3707 Set_Categorization_From_Scope (Designator, Scop);
3710 -- For a compilation unit, check for library-unit pragmas
3712 Push_Scope (Designator);
3713 Set_Categorization_From_Pragmas (N);
3714 Validate_Categorization_Dependency (N, Designator);
3718 -- For a compilation unit, set body required. This flag will only be
3719 -- reset if a valid Import or Interface pragma is processed later on.
3721 if Nkind (Parent (N)) = N_Compilation_Unit then
3722 Set_Body_Required (Parent (N), True);
3724 if Ada_Version >= Ada_2005
3725 and then Nkind (Specification (N)) = N_Procedure_Specification
3726 and then Null_Present (Specification (N))
3729 ("null procedure cannot be declared at library level", N);
3733 Generate_Reference_To_Formals (Designator);
3734 Check_Eliminated (Designator);
3736 if Debug_Flag_C then
3738 Write_Str ("<== subprogram spec ");
3739 Write_Name (Chars (Designator));
3740 Write_Str (" from ");
3741 Write_Location (Sloc (N));
3745 if Is_Protected_Type (Current_Scope) then
3747 -- Indicate that this is a protected operation, because it may be
3748 -- used in subsequent declarations within the protected type.
3750 Set_Convention (Designator, Convention_Protected);
3753 List_Inherited_Pre_Post_Aspects (Designator);
3755 if Has_Aspects (N) then
3756 Analyze_Aspect_Specifications (N, Designator);
3758 end Analyze_Subprogram_Declaration;
3760 --------------------------------------
3761 -- Analyze_Subprogram_Specification --
3762 --------------------------------------
3764 -- Reminder: N here really is a subprogram specification (not a subprogram
3765 -- declaration). This procedure is called to analyze the specification in
3766 -- both subprogram bodies and subprogram declarations (specs).
3768 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3769 Designator : constant Entity_Id := Defining_Entity (N);
3770 Formals : constant List_Id := Parameter_Specifications (N);
3772 -- Start of processing for Analyze_Subprogram_Specification
3775 -- User-defined operator is not allowed in SPARK, except as a renaming
3777 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3778 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3780 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3783 -- Proceed with analysis. Do not emit a cross-reference entry if the
3784 -- specification comes from an expression function, because it may be
3785 -- the completion of a previous declaration. It is is not, the cross-
3786 -- reference entry will be emitted for the new subprogram declaration.
3788 if Nkind (Parent (N)) /= N_Expression_Function then
3789 Generate_Definition (Designator);
3792 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3794 if Nkind (N) = N_Function_Specification then
3795 Set_Ekind (Designator, E_Function);
3796 Set_Mechanism (Designator, Default_Mechanism);
3798 Set_Ekind (Designator, E_Procedure);
3799 Set_Etype (Designator, Standard_Void_Type);
3802 -- Introduce new scope for analysis of the formals and the return type
3804 Set_Scope (Designator, Current_Scope);
3806 if Present (Formals) then
3807 Push_Scope (Designator);
3808 Process_Formals (Formals, N);
3810 -- Check dimensions in N for formals with default expression
3812 Analyze_Dimension_Formals (N, Formals);
3814 -- Ada 2005 (AI-345): If this is an overriding operation of an
3815 -- inherited interface operation, and the controlling type is
3816 -- a synchronized type, replace the type with its corresponding
3817 -- record, to match the proper signature of an overriding operation.
3818 -- Same processing for an access parameter whose designated type is
3819 -- derived from a synchronized interface.
3821 if Ada_Version >= Ada_2005 then
3824 Formal_Typ : Entity_Id;
3825 Rec_Typ : Entity_Id;
3826 Desig_Typ : Entity_Id;
3829 Formal := First_Formal (Designator);
3830 while Present (Formal) loop
3831 Formal_Typ := Etype (Formal);
3833 if Is_Concurrent_Type (Formal_Typ)
3834 and then Present (Corresponding_Record_Type (Formal_Typ))
3836 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3838 if Present (Interfaces (Rec_Typ)) then
3839 Set_Etype (Formal, Rec_Typ);
3842 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3843 Desig_Typ := Designated_Type (Formal_Typ);
3845 if Is_Concurrent_Type (Desig_Typ)
3846 and then Present (Corresponding_Record_Type (Desig_Typ))
3848 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3850 if Present (Interfaces (Rec_Typ)) then
3851 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3856 Next_Formal (Formal);
3863 -- The subprogram scope is pushed and popped around the processing of
3864 -- the return type for consistency with call above to Process_Formals
3865 -- (which itself can call Analyze_Return_Type), and to ensure that any
3866 -- itype created for the return type will be associated with the proper
3869 elsif Nkind (N) = N_Function_Specification then
3870 Push_Scope (Designator);
3871 Analyze_Return_Type (N);
3877 if Nkind (N) = N_Function_Specification then
3879 -- Deal with operator symbol case
3881 if Nkind (Designator) = N_Defining_Operator_Symbol then
3882 Valid_Operator_Definition (Designator);
3885 May_Need_Actuals (Designator);
3887 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3888 -- the subprogram is abstract also. This does not apply to renaming
3889 -- declarations, where abstractness is inherited, and to subprogram
3890 -- bodies generated for stream operations, which become renamings as
3893 -- In case of primitives associated with abstract interface types
3894 -- the check is applied later (see Analyze_Subprogram_Declaration).
3896 if not Nkind_In (Original_Node (Parent (N)),
3897 N_Subprogram_Renaming_Declaration,
3898 N_Abstract_Subprogram_Declaration,
3899 N_Formal_Abstract_Subprogram_Declaration)
3901 if Is_Abstract_Type (Etype (Designator))
3902 and then not Is_Interface (Etype (Designator))
3905 ("function that returns abstract type must be abstract", N);
3907 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3908 -- access result whose designated type is abstract.
3910 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3912 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3913 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3914 and then Ada_Version >= Ada_2012
3916 Error_Msg_N ("function whose access result designates "
3917 & "abstract type must be abstract", N);
3923 end Analyze_Subprogram_Specification;
3925 --------------------------
3926 -- Build_Body_To_Inline --
3927 --------------------------
3929 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3930 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3931 Original_Body : Node_Id;
3932 Body_To_Analyze : Node_Id;
3933 Max_Size : constant := 10;
3934 Stat_Count : Integer := 0;
3936 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3937 -- Check for declarations that make inlining not worthwhile
3939 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3940 -- Check for statements that make inlining not worthwhile: any tasking
3941 -- statement, nested at any level. Keep track of total number of
3942 -- elementary statements, as a measure of acceptable size.
3944 function Has_Pending_Instantiation return Boolean;
3945 -- If some enclosing body contains instantiations that appear before the
3946 -- corresponding generic body, the enclosing body has a freeze node so
3947 -- that it can be elaborated after the generic itself. This might
3948 -- conflict with subsequent inlinings, so that it is unsafe to try to
3949 -- inline in such a case.
3951 function Has_Single_Return return Boolean;
3952 -- In general we cannot inline functions that return unconstrained type.
3953 -- However, we can handle such functions if all return statements return
3954 -- a local variable that is the only declaration in the body of the
3955 -- function. In that case the call can be replaced by that local
3956 -- variable as is done for other inlined calls.
3958 procedure Remove_Pragmas;
3959 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3960 -- parameter has no meaning when the body is inlined and the formals
3961 -- are rewritten. Remove it from body to inline. The analysis of the
3962 -- non-inlined body will handle the pragma properly.
3964 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3965 -- If the body of the subprogram includes a call that returns an
3966 -- unconstrained type, the secondary stack is involved, and it
3967 -- is not worth inlining.
3969 ------------------------------
3970 -- Has_Excluded_Declaration --
3971 ------------------------------
3973 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3976 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3977 -- Nested subprograms make a given body ineligible for inlining, but
3978 -- we make an exception for instantiations of unchecked conversion.
3979 -- The body has not been analyzed yet, so check the name, and verify
3980 -- that the visible entity with that name is the predefined unit.
3982 -----------------------------
3983 -- Is_Unchecked_Conversion --
3984 -----------------------------
3986 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3987 Id : constant Node_Id := Name (D);
3991 if Nkind (Id) = N_Identifier
3992 and then Chars (Id) = Name_Unchecked_Conversion
3994 Conv := Current_Entity (Id);
3996 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3997 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3999 Conv := Current_Entity (Selector_Name (Id));
4004 return Present (Conv)
4005 and then Is_Predefined_File_Name
4006 (Unit_File_Name (Get_Source_Unit (Conv)))
4007 and then Is_Intrinsic_Subprogram (Conv);
4008 end Is_Unchecked_Conversion;
4010 -- Start of processing for Has_Excluded_Declaration
4014 while Present (D) loop
4015 if (Nkind (D) = N_Function_Instantiation
4016 and then not Is_Unchecked_Conversion (D))
4017 or else Nkind_In (D, N_Protected_Type_Declaration,
4018 N_Package_Declaration,
4019 N_Package_Instantiation,
4021 N_Procedure_Instantiation,
4022 N_Task_Type_Declaration)
4025 ("cannot inline & (non-allowed declaration)?", D, Subp);
4033 end Has_Excluded_Declaration;
4035 ----------------------------
4036 -- Has_Excluded_Statement --
4037 ----------------------------
4039 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4045 while Present (S) loop
4046 Stat_Count := Stat_Count + 1;
4048 if Nkind_In (S, N_Abort_Statement,
4049 N_Asynchronous_Select,
4050 N_Conditional_Entry_Call,
4051 N_Delay_Relative_Statement,
4052 N_Delay_Until_Statement,
4057 ("cannot inline & (non-allowed statement)?", S, Subp);
4060 elsif Nkind (S) = N_Block_Statement then
4061 if Present (Declarations (S))
4062 and then Has_Excluded_Declaration (Declarations (S))
4066 elsif Present (Handled_Statement_Sequence (S))
4069 (Exception_Handlers (Handled_Statement_Sequence (S)))
4071 Has_Excluded_Statement
4072 (Statements (Handled_Statement_Sequence (S))))
4077 elsif Nkind (S) = N_Case_Statement then
4078 E := First (Alternatives (S));
4079 while Present (E) loop
4080 if Has_Excluded_Statement (Statements (E)) then
4087 elsif Nkind (S) = N_If_Statement then
4088 if Has_Excluded_Statement (Then_Statements (S)) then
4092 if Present (Elsif_Parts (S)) then
4093 E := First (Elsif_Parts (S));
4094 while Present (E) loop
4095 if Has_Excluded_Statement (Then_Statements (E)) then
4103 if Present (Else_Statements (S))
4104 and then Has_Excluded_Statement (Else_Statements (S))
4109 elsif Nkind (S) = N_Loop_Statement
4110 and then Has_Excluded_Statement (Statements (S))
4114 elsif Nkind (S) = N_Extended_Return_Statement then
4115 if Has_Excluded_Statement
4116 (Statements (Handled_Statement_Sequence (S)))
4118 (Exception_Handlers (Handled_Statement_Sequence (S)))
4128 end Has_Excluded_Statement;
4130 -------------------------------
4131 -- Has_Pending_Instantiation --
4132 -------------------------------
4134 function Has_Pending_Instantiation return Boolean is
4139 while Present (S) loop
4140 if Is_Compilation_Unit (S)
4141 or else Is_Child_Unit (S)
4145 elsif Ekind (S) = E_Package
4146 and then Has_Forward_Instantiation (S)
4155 end Has_Pending_Instantiation;
4157 ------------------------
4158 -- Has_Single_Return --
4159 ------------------------
4161 function Has_Single_Return return Boolean is
4162 Return_Statement : Node_Id := Empty;
4164 function Check_Return (N : Node_Id) return Traverse_Result;
4170 function Check_Return (N : Node_Id) return Traverse_Result is
4172 if Nkind (N) = N_Simple_Return_Statement then
4173 if Present (Expression (N))
4174 and then Is_Entity_Name (Expression (N))
4176 if No (Return_Statement) then
4177 Return_Statement := N;
4180 elsif Chars (Expression (N)) =
4181 Chars (Expression (Return_Statement))
4189 -- A return statement within an extended return is a noop
4192 elsif No (Expression (N))
4193 and then Nkind (Parent (Parent (N))) =
4194 N_Extended_Return_Statement
4199 -- Expression has wrong form
4204 -- We can only inline a build-in-place function if
4205 -- it has a single extended return.
4207 elsif Nkind (N) = N_Extended_Return_Statement then
4208 if No (Return_Statement) then
4209 Return_Statement := N;
4221 function Check_All_Returns is new Traverse_Func (Check_Return);
4223 -- Start of processing for Has_Single_Return
4226 if Check_All_Returns (N) /= OK then
4229 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4233 return Present (Declarations (N))
4234 and then Present (First (Declarations (N)))
4235 and then Chars (Expression (Return_Statement)) =
4236 Chars (Defining_Identifier (First (Declarations (N))));
4238 end Has_Single_Return;
4240 --------------------
4241 -- Remove_Pragmas --
4242 --------------------
4244 procedure Remove_Pragmas is
4249 Decl := First (Declarations (Body_To_Analyze));
4250 while Present (Decl) loop
4253 if Nkind (Decl) = N_Pragma
4254 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4264 --------------------------
4265 -- Uses_Secondary_Stack --
4266 --------------------------
4268 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4269 function Check_Call (N : Node_Id) return Traverse_Result;
4270 -- Look for function calls that return an unconstrained type
4276 function Check_Call (N : Node_Id) return Traverse_Result is
4278 if Nkind (N) = N_Function_Call
4279 and then Is_Entity_Name (Name (N))
4280 and then Is_Composite_Type (Etype (Entity (Name (N))))
4281 and then not Is_Constrained (Etype (Entity (Name (N))))
4284 ("cannot inline & (call returns unconstrained type)?",
4292 function Check_Calls is new Traverse_Func (Check_Call);
4295 return Check_Calls (Bod) = Abandon;
4296 end Uses_Secondary_Stack;
4298 -- Start of processing for Build_Body_To_Inline
4301 -- Return immediately if done already
4303 if Nkind (Decl) = N_Subprogram_Declaration
4304 and then Present (Body_To_Inline (Decl))
4308 -- Functions that return unconstrained composite types require
4309 -- secondary stack handling, and cannot currently be inlined, unless
4310 -- all return statements return a local variable that is the first
4311 -- local declaration in the body.
4313 elsif Ekind (Subp) = E_Function
4314 and then not Is_Scalar_Type (Etype (Subp))
4315 and then not Is_Access_Type (Etype (Subp))
4316 and then not Is_Constrained (Etype (Subp))
4318 if not Has_Single_Return then
4320 ("cannot inline & (unconstrained return type)?", N, Subp);
4324 -- Ditto for functions that return controlled types, where controlled
4325 -- actions interfere in complex ways with inlining.
4327 elsif Ekind (Subp) = E_Function
4328 and then Needs_Finalization (Etype (Subp))
4331 ("cannot inline & (controlled return type)?", N, Subp);
4335 if Present (Declarations (N))
4336 and then Has_Excluded_Declaration (Declarations (N))
4341 if Present (Handled_Statement_Sequence (N)) then
4342 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4344 ("cannot inline& (exception handler)?",
4345 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4349 Has_Excluded_Statement
4350 (Statements (Handled_Statement_Sequence (N)))
4356 -- We do not inline a subprogram that is too large, unless it is
4357 -- marked Inline_Always. This pragma does not suppress the other
4358 -- checks on inlining (forbidden declarations, handlers, etc).
4360 if Stat_Count > Max_Size
4361 and then not Has_Pragma_Inline_Always (Subp)
4363 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4367 if Has_Pending_Instantiation then
4369 ("cannot inline& (forward instance within enclosing body)?",
4374 -- Within an instance, the body to inline must be treated as a nested
4375 -- generic, so that the proper global references are preserved.
4377 -- Note that we do not do this at the library level, because it is not
4378 -- needed, and furthermore this causes trouble if front end inlining
4379 -- is activated (-gnatN).
4381 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4382 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4383 Original_Body := Copy_Generic_Node (N, Empty, True);
4385 Original_Body := Copy_Separate_Tree (N);
4388 -- We need to capture references to the formals in order to substitute
4389 -- the actuals at the point of inlining, i.e. instantiation. To treat
4390 -- the formals as globals to the body to inline, we nest it within
4391 -- a dummy parameterless subprogram, declared within the real one.
4392 -- To avoid generating an internal name (which is never public, and
4393 -- which affects serial numbers of other generated names), we use
4394 -- an internal symbol that cannot conflict with user declarations.
4396 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4397 Set_Defining_Unit_Name
4398 (Specification (Original_Body),
4399 Make_Defining_Identifier (Sloc (N), Name_uParent));
4400 Set_Corresponding_Spec (Original_Body, Empty);
4402 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4404 -- Set return type of function, which is also global and does not need
4407 if Ekind (Subp) = E_Function then
4408 Set_Result_Definition (Specification (Body_To_Analyze),
4409 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4412 if No (Declarations (N)) then
4413 Set_Declarations (N, New_List (Body_To_Analyze));
4415 Append (Body_To_Analyze, Declarations (N));
4418 Expander_Mode_Save_And_Set (False);
4421 Analyze (Body_To_Analyze);
4422 Push_Scope (Defining_Entity (Body_To_Analyze));
4423 Save_Global_References (Original_Body);
4425 Remove (Body_To_Analyze);
4427 Expander_Mode_Restore;
4429 -- Restore environment if previously saved
4431 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4435 -- If secondary stk used there is no point in inlining. We have
4436 -- already issued the warning in this case, so nothing to do.
4438 if Uses_Secondary_Stack (Body_To_Analyze) then
4442 Set_Body_To_Inline (Decl, Original_Body);
4443 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4444 Set_Is_Inlined (Subp);
4445 end Build_Body_To_Inline;
4451 procedure Cannot_Inline
4455 Is_Serious : Boolean := False)
4458 pragma Assert (Msg (Msg'Last) = '?');
4462 if not Debug_Flag_Dot_K then
4464 -- Do not emit warning if this is a predefined unit which is not
4465 -- the main unit. With validity checks enabled, some predefined
4466 -- subprograms may contain nested subprograms and become ineligible
4469 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4470 and then not In_Extended_Main_Source_Unit (Subp)
4474 elsif Has_Pragma_Inline_Always (Subp) then
4476 -- Remove last character (question mark) to make this into an
4477 -- error, because the Inline_Always pragma cannot be obeyed.
4479 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4481 elsif Ineffective_Inline_Warnings then
4482 Error_Msg_NE (Msg & "p?", N, Subp);
4489 elsif Is_Serious then
4491 -- Remove last character (question mark) to make this into an error.
4493 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4495 elsif Optimization_Level = 0 then
4497 -- Do not emit warning if this is a predefined unit which is not
4498 -- the main unit. This behavior is currently provided for backward
4499 -- compatibility but it will be removed when we enforce the
4500 -- strictness of the new rules.
4502 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4503 and then not In_Extended_Main_Source_Unit (Subp)
4507 elsif Has_Pragma_Inline_Always (Subp) then
4509 -- Emit a warning if this is a call to a runtime subprogram
4510 -- which is located inside a generic. Previously this call
4511 -- was silently skipped!
4513 if Is_Generic_Instance (Subp) then
4515 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4517 if Is_Predefined_File_Name
4518 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4520 Set_Is_Inlined (Subp, False);
4521 Error_Msg_NE (Msg & "p?", N, Subp);
4527 -- Remove last character (question mark) to make this into an
4528 -- error, because the Inline_Always pragma cannot be obeyed.
4530 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4532 else pragma Assert (Front_End_Inlining);
4533 Set_Is_Inlined (Subp, False);
4535 -- When inlining cannot take place we must issue an error.
4536 -- For backward compatibility we still report a warning.
4538 if Ineffective_Inline_Warnings then
4539 Error_Msg_NE (Msg & "p?", N, Subp);
4543 -- Compiling with optimizations enabled it is too early to report
4544 -- problems since the backend may still perform inlining. In order
4545 -- to report unhandled inlinings the program must be compiled with
4546 -- -Winline and the error is reported by the backend.
4553 ------------------------------------
4554 -- Check_And_Build_Body_To_Inline --
4555 ------------------------------------
4557 procedure Check_And_Build_Body_To_Inline
4559 Spec_Id : Entity_Id;
4560 Body_Id : Entity_Id)
4562 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4563 -- Use generic machinery to build an unexpanded body for the subprogram.
4564 -- This body is subsequently used for inline expansions at call sites.
4566 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4567 -- Return true if we generate code for the function body N, the function
4568 -- body N has no local declarations and its unique statement is a single
4569 -- extended return statement with a handled statements sequence.
4571 function Check_Body_To_Inline
4573 Subp : Entity_Id) return Boolean;
4574 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4575 -- inlined by the frontend. These are the rules:
4576 -- * At -O0 use fe inlining when inline_always is specified except if
4577 -- the function returns a controlled type.
4578 -- * At other optimization levels use the fe inlining for both inline
4579 -- and inline_always in the following cases:
4580 -- - function returning a known at compile time constant
4581 -- - function returning a call to an intrinsic function
4582 -- - function returning an unconstrained type (see Can_Split
4583 -- Unconstrained_Function).
4584 -- - function returning a call to a frontend-inlined function
4585 -- Use the back-end mechanism otherwise
4587 -- In addition, in the following cases the function cannot be inlined by
4589 -- - functions that uses the secondary stack
4590 -- - functions that have declarations of:
4591 -- - Concurrent types
4595 -- - functions that have some of the following statements:
4597 -- - asynchronous-select
4598 -- - conditional-entry-call
4601 -- - selective-accept
4602 -- - timed-entry-call
4603 -- - functions that have exception handlers
4604 -- - functions that have some enclosing body containing instantiations
4605 -- that appear before the corresponding generic body.
4607 procedure Generate_Body_To_Inline
4609 Body_To_Inline : out Node_Id);
4610 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4611 -- of pragmas referencing the formals are removed since they have no
4612 -- meaning when the body is inlined and the formals are rewritten (the
4613 -- analysis of the non-inlined body will handle these pragmas properly).
4614 -- A new internal name is associated with Body_To_Inline.
4616 procedure Split_Unconstrained_Function
4618 Spec_Id : Entity_Id);
4619 -- N is an inlined function body that returns an unconstrained type and
4620 -- has a single extended return statement. Split N in two subprograms:
4621 -- a procedure P' and a function F'. The formals of P' duplicate the
4622 -- formals of N plus an extra formal which is used return a value;
4623 -- its body is composed by the declarations and list of statements
4624 -- of the extended return statement of N.
4626 --------------------------
4627 -- Build_Body_To_Inline --
4628 --------------------------
4630 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4631 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4632 Original_Body : Node_Id;
4633 Body_To_Analyze : Node_Id;
4636 pragma Assert (Current_Scope = Spec_Id);
4638 -- Within an instance, the body to inline must be treated as a nested
4639 -- generic, so that the proper global references are preserved. We
4640 -- do not do this at the library level, because it is not needed, and
4641 -- furthermore this causes trouble if front end inlining is activated
4645 and then Scope (Current_Scope) /= Standard_Standard
4647 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4650 -- We need to capture references to the formals in order
4651 -- to substitute the actuals at the point of inlining, i.e.
4652 -- instantiation. To treat the formals as globals to the body to
4653 -- inline, we nest it within a dummy parameterless subprogram,
4654 -- declared within the real one.
4656 Generate_Body_To_Inline (N, Original_Body);
4657 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4659 -- Set return type of function, which is also global and does not
4660 -- need to be resolved.
4662 if Ekind (Spec_Id) = E_Function then
4663 Set_Result_Definition (Specification (Body_To_Analyze),
4664 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4667 if No (Declarations (N)) then
4668 Set_Declarations (N, New_List (Body_To_Analyze));
4670 Append_To (Declarations (N), Body_To_Analyze);
4673 Preanalyze (Body_To_Analyze);
4675 Push_Scope (Defining_Entity (Body_To_Analyze));
4676 Save_Global_References (Original_Body);
4678 Remove (Body_To_Analyze);
4680 -- Restore environment if previously saved
4683 and then Scope (Current_Scope) /= Standard_Standard
4688 pragma Assert (No (Body_To_Inline (Decl)));
4689 Set_Body_To_Inline (Decl, Original_Body);
4690 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4691 end Build_Body_To_Inline;
4693 --------------------------
4694 -- Check_Body_To_Inline --
4695 --------------------------
4697 function Check_Body_To_Inline
4699 Subp : Entity_Id) return Boolean
4701 Max_Size : constant := 10;
4702 Stat_Count : Integer := 0;
4704 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4705 -- Check for declarations that make inlining not worthwhile
4707 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4708 -- Check for statements that make inlining not worthwhile: any
4709 -- tasking statement, nested at any level. Keep track of total
4710 -- number of elementary statements, as a measure of acceptable size.
4712 function Has_Pending_Instantiation return Boolean;
4713 -- Return True if some enclosing body contains instantiations that
4714 -- appear before the corresponding generic body.
4716 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4717 -- Return True if all the return statements of the function body N
4718 -- are simple return statements and return a compile time constant
4720 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4721 -- Return True if all the return statements of the function body N
4722 -- are simple return statements and return an intrinsic function call
4724 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4725 -- If the body of the subprogram includes a call that returns an
4726 -- unconstrained type, the secondary stack is involved, and it
4727 -- is not worth inlining.
4729 ------------------------------
4730 -- Has_Excluded_Declaration --
4731 ------------------------------
4733 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4736 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4737 -- Nested subprograms make a given body ineligible for inlining,
4738 -- but we make an exception for instantiations of unchecked
4739 -- conversion. The body has not been analyzed yet, so check the
4740 -- name, and verify that the visible entity with that name is the
4743 -----------------------------
4744 -- Is_Unchecked_Conversion --
4745 -----------------------------
4747 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4748 Id : constant Node_Id := Name (D);
4752 if Nkind (Id) = N_Identifier
4753 and then Chars (Id) = Name_Unchecked_Conversion
4755 Conv := Current_Entity (Id);
4757 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4759 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4761 Conv := Current_Entity (Selector_Name (Id));
4766 return Present (Conv)
4767 and then Is_Predefined_File_Name
4768 (Unit_File_Name (Get_Source_Unit (Conv)))
4769 and then Is_Intrinsic_Subprogram (Conv);
4770 end Is_Unchecked_Conversion;
4772 -- Start of processing for Has_Excluded_Declaration
4776 while Present (D) loop
4777 if (Nkind (D) = N_Function_Instantiation
4778 and then not Is_Unchecked_Conversion (D))
4779 or else Nkind_In (D, N_Protected_Type_Declaration,
4780 N_Package_Declaration,
4781 N_Package_Instantiation,
4783 N_Procedure_Instantiation,
4784 N_Task_Type_Declaration)
4787 ("cannot inline & (non-allowed declaration)?", D, Subp);
4796 end Has_Excluded_Declaration;
4798 ----------------------------
4799 -- Has_Excluded_Statement --
4800 ----------------------------
4802 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4808 while Present (S) loop
4809 Stat_Count := Stat_Count + 1;
4811 if Nkind_In (S, N_Abort_Statement,
4812 N_Asynchronous_Select,
4813 N_Conditional_Entry_Call,
4814 N_Delay_Relative_Statement,
4815 N_Delay_Until_Statement,
4820 ("cannot inline & (non-allowed statement)?", S, Subp);
4823 elsif Nkind (S) = N_Block_Statement then
4824 if Present (Declarations (S))
4825 and then Has_Excluded_Declaration (Declarations (S))
4829 elsif Present (Handled_Statement_Sequence (S)) then
4831 (Exception_Handlers (Handled_Statement_Sequence (S)))
4834 ("cannot inline& (exception handler)?",
4835 First (Exception_Handlers
4836 (Handled_Statement_Sequence (S))),
4840 elsif Has_Excluded_Statement
4841 (Statements (Handled_Statement_Sequence (S)))
4847 elsif Nkind (S) = N_Case_Statement then
4848 E := First (Alternatives (S));
4849 while Present (E) loop
4850 if Has_Excluded_Statement (Statements (E)) then
4857 elsif Nkind (S) = N_If_Statement then
4858 if Has_Excluded_Statement (Then_Statements (S)) then
4862 if Present (Elsif_Parts (S)) then
4863 E := First (Elsif_Parts (S));
4864 while Present (E) loop
4865 if Has_Excluded_Statement (Then_Statements (E)) then
4872 if Present (Else_Statements (S))
4873 and then Has_Excluded_Statement (Else_Statements (S))
4878 elsif Nkind (S) = N_Loop_Statement
4879 and then Has_Excluded_Statement (Statements (S))
4883 elsif Nkind (S) = N_Extended_Return_Statement then
4884 if Present (Handled_Statement_Sequence (S))
4886 Has_Excluded_Statement
4887 (Statements (Handled_Statement_Sequence (S)))
4891 elsif Present (Handled_Statement_Sequence (S))
4893 Present (Exception_Handlers
4894 (Handled_Statement_Sequence (S)))
4897 ("cannot inline& (exception handler)?",
4898 First (Exception_Handlers
4899 (Handled_Statement_Sequence (S))),
4909 end Has_Excluded_Statement;
4911 -------------------------------
4912 -- Has_Pending_Instantiation --
4913 -------------------------------
4915 function Has_Pending_Instantiation return Boolean is
4920 while Present (S) loop
4921 if Is_Compilation_Unit (S)
4922 or else Is_Child_Unit (S)
4926 elsif Ekind (S) = E_Package
4927 and then Has_Forward_Instantiation (S)
4936 end Has_Pending_Instantiation;
4938 ------------------------------------
4939 -- Returns_Compile_Time_Constant --
4940 ------------------------------------
4942 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4944 function Check_Return (N : Node_Id) return Traverse_Result;
4950 function Check_Return (N : Node_Id) return Traverse_Result is
4952 if Nkind (N) = N_Extended_Return_Statement then
4955 elsif Nkind (N) = N_Simple_Return_Statement then
4956 if Present (Expression (N)) then
4958 Orig_Expr : constant Node_Id :=
4959 Original_Node (Expression (N));
4962 if Nkind_In (Orig_Expr, N_Integer_Literal,
4964 N_Character_Literal)
4968 elsif Is_Entity_Name (Orig_Expr)
4969 and then Ekind (Entity (Orig_Expr)) = E_Constant
4970 and then Is_Static_Expression (Orig_Expr)
4978 -- Expression has wrong form
4984 -- Continue analyzing statements
4991 function Check_All_Returns is new Traverse_Func (Check_Return);
4993 -- Start of processing for Returns_Compile_Time_Constant
4996 return Check_All_Returns (N) = OK;
4997 end Returns_Compile_Time_Constant;
4999 --------------------------------------
5000 -- Returns_Intrinsic_Function_Call --
5001 --------------------------------------
5003 function Returns_Intrinsic_Function_Call
5004 (N : Node_Id) return Boolean
5006 function Check_Return (N : Node_Id) return Traverse_Result;
5012 function Check_Return (N : Node_Id) return Traverse_Result is
5014 if Nkind (N) = N_Extended_Return_Statement then
5017 elsif Nkind (N) = N_Simple_Return_Statement then
5018 if Present (Expression (N)) then
5020 Orig_Expr : constant Node_Id :=
5021 Original_Node (Expression (N));
5024 if Nkind (Orig_Expr) in N_Op
5025 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5029 elsif Nkind (Orig_Expr) in N_Has_Entity
5030 and then Present (Entity (Orig_Expr))
5031 and then Ekind (Entity (Orig_Expr)) = E_Function
5032 and then Is_Inlined (Entity (Orig_Expr))
5036 elsif Nkind (Orig_Expr) in N_Has_Entity
5037 and then Present (Entity (Orig_Expr))
5038 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5047 -- Expression has wrong form
5053 -- Continue analyzing statements
5060 function Check_All_Returns is new Traverse_Func (Check_Return);
5062 -- Start of processing for Returns_Intrinsic_Function_Call
5065 return Check_All_Returns (N) = OK;
5066 end Returns_Intrinsic_Function_Call;
5068 --------------------------
5069 -- Uses_Secondary_Stack --
5070 --------------------------
5072 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5074 function Check_Call (N : Node_Id) return Traverse_Result;
5075 -- Look for function calls that return an unconstrained type
5081 function Check_Call (N : Node_Id) return Traverse_Result is
5083 if Nkind (N) = N_Function_Call
5084 and then Is_Entity_Name (Name (N))
5085 and then Is_Composite_Type (Etype (Entity (Name (N))))
5086 and then not Is_Constrained (Etype (Entity (Name (N))))
5089 ("cannot inline & (call returns unconstrained type)?",
5098 function Check_Calls is new Traverse_Func (Check_Call);
5100 -- Start of processing for Uses_Secondary_Stack
5103 return Check_Calls (N) = Abandon;
5104 end Uses_Secondary_Stack;
5108 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5109 May_Inline : constant Boolean :=
5110 Has_Pragma_Inline_Always (Spec_Id)
5111 or else (Has_Pragma_Inline (Spec_Id)
5112 and then ((Optimization_Level > 0
5113 and then Ekind (Spec_Id)
5115 or else Front_End_Inlining));
5116 Body_To_Analyze : Node_Id;
5118 -- Start of processing for Check_Body_To_Inline
5121 -- No action needed in stubs since the attribute Body_To_Inline
5124 if Nkind (Decl) = N_Subprogram_Body_Stub then
5127 -- Cannot build the body to inline if the attribute is already set.
5128 -- This attribute may have been set if this is a subprogram renaming
5129 -- declarations (see Freeze.Build_Renamed_Body).
5131 elsif Present (Body_To_Inline (Decl)) then
5134 -- No action needed if the subprogram does not fulfill the minimum
5135 -- conditions to be inlined by the frontend
5137 elsif not May_Inline then
5141 -- Check excluded declarations
5143 if Present (Declarations (N))
5144 and then Has_Excluded_Declaration (Declarations (N))
5149 -- Check excluded statements
5151 if Present (Handled_Statement_Sequence (N)) then
5153 (Exception_Handlers (Handled_Statement_Sequence (N)))
5156 ("cannot inline& (exception handler)?",
5158 (Exception_Handlers (Handled_Statement_Sequence (N))),
5163 elsif Has_Excluded_Statement
5164 (Statements (Handled_Statement_Sequence (N)))
5170 -- For backward compatibility, compiling under -gnatN we do not
5171 -- inline a subprogram that is too large, unless it is marked
5172 -- Inline_Always. This pragma does not suppress the other checks
5173 -- on inlining (forbidden declarations, handlers, etc).
5175 if Front_End_Inlining
5176 and then not Has_Pragma_Inline_Always (Subp)
5177 and then Stat_Count > Max_Size
5179 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5183 -- If some enclosing body contains instantiations that appear before
5184 -- the corresponding generic body, the enclosing body has a freeze
5185 -- node so that it can be elaborated after the generic itself. This
5186 -- might conflict with subsequent inlinings, so that it is unsafe to
5187 -- try to inline in such a case.
5189 if Has_Pending_Instantiation then
5191 ("cannot inline& (forward instance within enclosing body)?",
5197 -- Generate and preanalyze the body to inline (needed to perform
5198 -- the rest of the checks)
5200 Generate_Body_To_Inline (N, Body_To_Analyze);
5202 if Ekind (Subp) = E_Function then
5203 Set_Result_Definition (Specification (Body_To_Analyze),
5204 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5207 -- Nest the body to analyze within the real one
5209 if No (Declarations (N)) then
5210 Set_Declarations (N, New_List (Body_To_Analyze));
5212 Append_To (Declarations (N), Body_To_Analyze);
5215 Preanalyze (Body_To_Analyze);
5216 Remove (Body_To_Analyze);
5218 -- Keep separate checks needed when compiling without optimizations
5220 if Optimization_Level = 0
5222 -- AAMP and VM targets have no support for inlining in the backend
5223 -- and hence we use frontend inlining at all optimization levels.
5225 or else AAMP_On_Target
5226 or else VM_Target /= No_VM
5228 -- Cannot inline functions whose body has a call that returns an
5229 -- unconstrained type since the secondary stack is involved, and
5230 -- it is not worth inlining.
5232 if Uses_Secondary_Stack (Body_To_Analyze) then
5235 -- Cannot inline functions that return controlled types since
5236 -- controlled actions interfere in complex ways with inlining.
5238 elsif Ekind (Subp) = E_Function
5239 and then Needs_Finalization (Etype (Subp))
5242 ("cannot inline & (controlled return type)?", N, Subp);
5245 elsif Returns_Unconstrained_Type (Subp) then
5247 ("cannot inline & (unconstrained return type)?", N, Subp);
5251 -- Compiling with optimizations enabled
5254 -- Procedures are never frontend inlined in this case!
5256 if Ekind (Subp) /= E_Function then
5259 -- Functions returning unconstrained types are tested
5260 -- separately (see Can_Split_Unconstrained_Function).
5262 elsif Returns_Unconstrained_Type (Subp) then
5265 -- Check supported cases
5267 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5268 and then Convention (Subp) /= Convention_Intrinsic
5269 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5276 end Check_Body_To_Inline;
5278 --------------------------------------
5279 -- Can_Split_Unconstrained_Function --
5280 --------------------------------------
5282 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5284 Ret_Node : constant Node_Id :=
5285 First (Statements (Handled_Statement_Sequence (N)));
5289 -- No user defined declarations allowed in the function except inside
5290 -- the unique return statement; implicit labels are the only allowed
5293 if not Is_Empty_List (Declarations (N)) then
5294 D := First (Declarations (N));
5295 while Present (D) loop
5296 if Nkind (D) /= N_Implicit_Label_Declaration then
5304 -- We only split the inlined function when we are generating the code
5305 -- of its body; otherwise we leave duplicated split subprograms in
5306 -- the tree which (if referenced) generate wrong references at link
5309 return In_Extended_Main_Code_Unit (N)
5310 and then Present (Ret_Node)
5311 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5312 and then No (Next (Ret_Node))
5313 and then Present (Handled_Statement_Sequence (Ret_Node));
5314 end Can_Split_Unconstrained_Function;
5316 -----------------------------
5317 -- Generate_Body_To_Inline --
5318 -----------------------------
5320 procedure Generate_Body_To_Inline
5322 Body_To_Inline : out Node_Id)
5324 procedure Remove_Pragmas (N : Node_Id);
5325 -- Remove occurrences of pragmas that may reference the formals of
5326 -- N. The analysis of the non-inlined body will handle these pragmas
5329 --------------------
5330 -- Remove_Pragmas --
5331 --------------------
5333 procedure Remove_Pragmas (N : Node_Id) is
5338 Decl := First (Declarations (N));
5339 while Present (Decl) loop
5342 if Nkind (Decl) = N_Pragma
5343 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5353 -- Start of processing for Generate_Body_To_Inline
5356 -- Within an instance, the body to inline must be treated as a nested
5357 -- generic, so that the proper global references are preserved.
5359 -- Note that we do not do this at the library level, because it
5360 -- is not needed, and furthermore this causes trouble if front
5361 -- end inlining is activated (-gnatN).
5364 and then Scope (Current_Scope) /= Standard_Standard
5366 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5368 Body_To_Inline := Copy_Separate_Tree (N);
5371 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5372 -- parameter has no meaning when the body is inlined and the formals
5373 -- are rewritten. Remove it from body to inline. The analysis of the
5374 -- non-inlined body will handle the pragma properly.
5376 Remove_Pragmas (Body_To_Inline);
5378 -- We need to capture references to the formals in order
5379 -- to substitute the actuals at the point of inlining, i.e.
5380 -- instantiation. To treat the formals as globals to the body to
5381 -- inline, we nest it within a dummy parameterless subprogram,
5382 -- declared within the real one.
5384 Set_Parameter_Specifications
5385 (Specification (Body_To_Inline), No_List);
5387 -- A new internal name is associated with Body_To_Inline to avoid
5388 -- conflicts when the non-inlined body N is analyzed.
5390 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5391 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5392 Set_Corresponding_Spec (Body_To_Inline, Empty);
5393 end Generate_Body_To_Inline;
5395 ----------------------------------
5396 -- Split_Unconstrained_Function --
5397 ----------------------------------
5399 procedure Split_Unconstrained_Function
5401 Spec_Id : Entity_Id)
5403 Loc : constant Source_Ptr := Sloc (N);
5404 Ret_Node : constant Node_Id :=
5405 First (Statements (Handled_Statement_Sequence (N)));
5406 Ret_Obj : constant Node_Id :=
5407 First (Return_Object_Declarations (Ret_Node));
5409 procedure Build_Procedure
5410 (Proc_Id : out Entity_Id;
5411 Decl_List : out List_Id);
5412 -- Build a procedure containing the statements found in the extended
5413 -- return statement of the unconstrained function body N.
5415 procedure Build_Procedure
5416 (Proc_Id : out Entity_Id;
5417 Decl_List : out List_Id)
5420 Formal_List : constant List_Id := New_List;
5421 Proc_Spec : Node_Id;
5422 Proc_Body : Node_Id;
5423 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5424 Body_Decl_List : List_Id := No_List;
5425 Param_Type : Node_Id;
5428 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5429 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5432 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5435 Append_To (Formal_List,
5436 Make_Parameter_Specification (Loc,
5437 Defining_Identifier =>
5438 Make_Defining_Identifier (Loc,
5439 Chars => Chars (Defining_Identifier (Ret_Obj))),
5440 In_Present => False,
5441 Out_Present => True,
5442 Null_Exclusion_Present => False,
5443 Parameter_Type => Param_Type));
5445 Formal := First_Formal (Spec_Id);
5446 while Present (Formal) loop
5447 Append_To (Formal_List,
5448 Make_Parameter_Specification (Loc,
5449 Defining_Identifier =>
5450 Make_Defining_Identifier (Sloc (Formal),
5451 Chars => Chars (Formal)),
5452 In_Present => In_Present (Parent (Formal)),
5453 Out_Present => Out_Present (Parent (Formal)),
5454 Null_Exclusion_Present =>
5455 Null_Exclusion_Present (Parent (Formal)),
5457 New_Reference_To (Etype (Formal), Loc),
5459 Copy_Separate_Tree (Expression (Parent (Formal)))));
5461 Next_Formal (Formal);
5465 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5468 Make_Procedure_Specification (Loc,
5469 Defining_Unit_Name => Proc_Id,
5470 Parameter_Specifications => Formal_List);
5472 Decl_List := New_List;
5474 Append_To (Decl_List,
5475 Make_Subprogram_Declaration (Loc, Proc_Spec));
5477 -- Can_Convert_Unconstrained_Function checked that the function
5478 -- has no local declarations except implicit label declarations.
5479 -- Copy these declarations to the built procedure.
5481 if Present (Declarations (N)) then
5482 Body_Decl_List := New_List;
5489 D := First (Declarations (N));
5490 while Present (D) loop
5491 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5494 Make_Implicit_Label_Declaration (Loc,
5495 Make_Defining_Identifier (Loc,
5496 Chars => Chars (Defining_Identifier (D))),
5497 Label_Construct => Empty);
5498 Append_To (Body_Decl_List, New_D);
5505 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5508 Make_Subprogram_Body (Loc,
5509 Specification => Copy_Separate_Tree (Proc_Spec),
5510 Declarations => Body_Decl_List,
5511 Handled_Statement_Sequence =>
5512 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5514 Set_Defining_Unit_Name (Specification (Proc_Body),
5515 Make_Defining_Identifier (Loc, Subp_Name));
5517 Append_To (Decl_List, Proc_Body);
5518 end Build_Procedure;
5522 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5524 Proc_Id : Entity_Id;
5525 Proc_Call : Node_Id;
5527 -- Start of processing for Split_Unconstrained_Function
5530 -- Build the associated procedure, analyze it and insert it before
5531 -- the function body N
5534 Scope : constant Entity_Id := Current_Scope;
5535 Decl_List : List_Id;
5538 Build_Procedure (Proc_Id, Decl_List);
5539 Insert_Actions (N, Decl_List);
5543 -- Build the call to the generated procedure
5546 Actual_List : constant List_Id := New_List;
5550 Append_To (Actual_List,
5551 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5553 Formal := First_Formal (Spec_Id);
5554 while Present (Formal) loop
5555 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5557 -- Avoid spurious warning on unreferenced formals
5559 Set_Referenced (Formal);
5560 Next_Formal (Formal);
5564 Make_Procedure_Call_Statement (Loc,
5565 Name => New_Reference_To (Proc_Id, Loc),
5566 Parameter_Associations => Actual_List);
5574 -- main_1__F1b (New_Obj, ...);
5579 Make_Block_Statement (Loc,
5580 Declarations => New_List (New_Obj),
5581 Handled_Statement_Sequence =>
5582 Make_Handled_Sequence_Of_Statements (Loc,
5583 Statements => New_List (
5587 Make_Simple_Return_Statement (Loc,
5590 (Defining_Identifier (New_Obj), Loc)))));
5592 Rewrite (Ret_Node, Blk_Stmt);
5593 end Split_Unconstrained_Function;
5595 -- Start of processing for Check_And_Build_Body_To_Inline
5598 -- Do not inline any subprogram that contains nested subprograms, since
5599 -- the backend inlining circuit seems to generate uninitialized
5600 -- references in this case. We know this happens in the case of front
5601 -- end ZCX support, but it also appears it can happen in other cases as
5602 -- well. The backend often rejects attempts to inline in the case of
5603 -- nested procedures anyway, so little if anything is lost by this.
5604 -- Note that this is test is for the benefit of the back-end. There is
5605 -- a separate test for front-end inlining that also rejects nested
5608 -- Do not do this test if errors have been detected, because in some
5609 -- error cases, this code blows up, and we don't need it anyway if
5610 -- there have been errors, since we won't get to the linker anyway.
5612 if Comes_From_Source (Body_Id)
5613 and then (Has_Pragma_Inline_Always (Spec_Id)
5614 or else Optimization_Level > 0)
5615 and then Serious_Errors_Detected = 0
5623 P_Ent := Scope (P_Ent);
5624 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5626 if Is_Subprogram (P_Ent) then
5627 Set_Is_Inlined (P_Ent, False);
5629 if Comes_From_Source (P_Ent)
5630 and then Has_Pragma_Inline (P_Ent)
5633 ("cannot inline& (nested subprogram)?", N, P_Ent,
5634 Is_Serious => True);
5641 -- Build the body to inline only if really needed!
5643 if Check_Body_To_Inline (N, Spec_Id)
5644 and then Serious_Errors_Detected = 0
5646 if Returns_Unconstrained_Type (Spec_Id) then
5647 if Can_Split_Unconstrained_Function (N) then
5648 Split_Unconstrained_Function (N, Spec_Id);
5649 Build_Body_To_Inline (N, Spec_Id);
5650 Set_Is_Inlined (Spec_Id);
5653 Build_Body_To_Inline (N, Spec_Id);
5654 Set_Is_Inlined (Spec_Id);
5657 end Check_And_Build_Body_To_Inline;
5659 -----------------------
5660 -- Check_Conformance --
5661 -----------------------
5663 procedure Check_Conformance
5664 (New_Id : Entity_Id;
5666 Ctype : Conformance_Type;
5668 Conforms : out Boolean;
5669 Err_Loc : Node_Id := Empty;
5670 Get_Inst : Boolean := False;
5671 Skip_Controlling_Formals : Boolean := False)
5673 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5674 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5675 -- If Errmsg is True, then processing continues to post an error message
5676 -- for conformance error on given node. Two messages are output. The
5677 -- first message points to the previous declaration with a general "no
5678 -- conformance" message. The second is the detailed reason, supplied as
5679 -- Msg. The parameter N provide information for a possible & insertion
5680 -- in the message, and also provides the location for posting the
5681 -- message in the absence of a specified Err_Loc location.
5683 -----------------------
5684 -- Conformance_Error --
5685 -----------------------
5687 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5694 if No (Err_Loc) then
5700 Error_Msg_Sloc := Sloc (Old_Id);
5703 when Type_Conformant =>
5704 Error_Msg_N -- CODEFIX
5705 ("not type conformant with declaration#!", Enode);
5707 when Mode_Conformant =>
5708 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5710 ("not mode conformant with operation inherited#!",
5714 ("not mode conformant with declaration#!", Enode);
5717 when Subtype_Conformant =>
5718 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5720 ("not subtype conformant with operation inherited#!",
5724 ("not subtype conformant with declaration#!", Enode);
5727 when Fully_Conformant =>
5728 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5729 Error_Msg_N -- CODEFIX
5730 ("not fully conformant with operation inherited#!",
5733 Error_Msg_N -- CODEFIX
5734 ("not fully conformant with declaration#!", Enode);
5738 Error_Msg_NE (Msg, Enode, N);
5740 end Conformance_Error;
5744 Old_Type : constant Entity_Id := Etype (Old_Id);
5745 New_Type : constant Entity_Id := Etype (New_Id);
5746 Old_Formal : Entity_Id;
5747 New_Formal : Entity_Id;
5748 Access_Types_Match : Boolean;
5749 Old_Formal_Base : Entity_Id;
5750 New_Formal_Base : Entity_Id;
5752 -- Start of processing for Check_Conformance
5757 -- We need a special case for operators, since they don't appear
5760 if Ctype = Type_Conformant then
5761 if Ekind (New_Id) = E_Operator
5762 and then Operator_Matches_Spec (New_Id, Old_Id)
5768 -- If both are functions/operators, check return types conform
5770 if Old_Type /= Standard_Void_Type
5771 and then New_Type /= Standard_Void_Type
5774 -- If we are checking interface conformance we omit controlling
5775 -- arguments and result, because we are only checking the conformance
5776 -- of the remaining parameters.
5778 if Has_Controlling_Result (Old_Id)
5779 and then Has_Controlling_Result (New_Id)
5780 and then Skip_Controlling_Formals
5784 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5785 Conformance_Error ("\return type does not match!", New_Id);
5789 -- Ada 2005 (AI-231): In case of anonymous access types check the
5790 -- null-exclusion and access-to-constant attributes match.
5792 if Ada_Version >= Ada_2005
5793 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5795 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5796 or else Is_Access_Constant (Etype (Old_Type)) /=
5797 Is_Access_Constant (Etype (New_Type)))
5799 Conformance_Error ("\return type does not match!", New_Id);
5803 -- If either is a function/operator and the other isn't, error
5805 elsif Old_Type /= Standard_Void_Type
5806 or else New_Type /= Standard_Void_Type
5808 Conformance_Error ("\functions can only match functions!", New_Id);
5812 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5813 -- If this is a renaming as body, refine error message to indicate that
5814 -- the conflict is with the original declaration. If the entity is not
5815 -- frozen, the conventions don't have to match, the one of the renamed
5816 -- entity is inherited.
5818 if Ctype >= Subtype_Conformant then
5819 if Convention (Old_Id) /= Convention (New_Id) then
5820 if not Is_Frozen (New_Id) then
5823 elsif Present (Err_Loc)
5824 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5825 and then Present (Corresponding_Spec (Err_Loc))
5827 Error_Msg_Name_1 := Chars (New_Id);
5829 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5830 Conformance_Error ("\prior declaration for% has convention %!");
5833 Conformance_Error ("\calling conventions do not match!");
5838 elsif Is_Formal_Subprogram (Old_Id)
5839 or else Is_Formal_Subprogram (New_Id)
5841 Conformance_Error ("\formal subprograms not allowed!");
5846 -- Deal with parameters
5848 -- Note: we use the entity information, rather than going directly
5849 -- to the specification in the tree. This is not only simpler, but
5850 -- absolutely necessary for some cases of conformance tests between
5851 -- operators, where the declaration tree simply does not exist!
5853 Old_Formal := First_Formal (Old_Id);
5854 New_Formal := First_Formal (New_Id);
5855 while Present (Old_Formal) and then Present (New_Formal) loop
5856 if Is_Controlling_Formal (Old_Formal)
5857 and then Is_Controlling_Formal (New_Formal)
5858 and then Skip_Controlling_Formals
5860 -- The controlling formals will have different types when
5861 -- comparing an interface operation with its match, but both
5862 -- or neither must be access parameters.
5864 if Is_Access_Type (Etype (Old_Formal))
5866 Is_Access_Type (Etype (New_Formal))
5868 goto Skip_Controlling_Formal;
5871 ("\access parameter does not match!", New_Formal);
5875 -- Ada 2012: Mode conformance also requires that formal parameters
5876 -- be both aliased, or neither.
5878 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5879 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5881 ("\aliased parameter mismatch!", New_Formal);
5885 if Ctype = Fully_Conformant then
5887 -- Names must match. Error message is more accurate if we do
5888 -- this before checking that the types of the formals match.
5890 if Chars (Old_Formal) /= Chars (New_Formal) then
5891 Conformance_Error ("\name & does not match!", New_Formal);
5893 -- Set error posted flag on new formal as well to stop
5894 -- junk cascaded messages in some cases.
5896 Set_Error_Posted (New_Formal);
5900 -- Null exclusion must match
5902 if Null_Exclusion_Present (Parent (Old_Formal))
5904 Null_Exclusion_Present (Parent (New_Formal))
5906 -- Only give error if both come from source. This should be
5907 -- investigated some time, since it should not be needed ???
5909 if Comes_From_Source (Old_Formal)
5911 Comes_From_Source (New_Formal)
5914 ("\null exclusion for & does not match", New_Formal);
5916 -- Mark error posted on the new formal to avoid duplicated
5917 -- complaint about types not matching.
5919 Set_Error_Posted (New_Formal);
5924 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5925 -- case occurs whenever a subprogram is being renamed and one of its
5926 -- parameters imposes a null exclusion. For example:
5928 -- type T is null record;
5929 -- type Acc_T is access T;
5930 -- subtype Acc_T_Sub is Acc_T;
5932 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5933 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5936 Old_Formal_Base := Etype (Old_Formal);
5937 New_Formal_Base := Etype (New_Formal);
5940 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5941 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5944 Access_Types_Match := Ada_Version >= Ada_2005
5946 -- Ensure that this rule is only applied when New_Id is a
5947 -- renaming of Old_Id.
5949 and then Nkind (Parent (Parent (New_Id))) =
5950 N_Subprogram_Renaming_Declaration
5951 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5952 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5953 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5955 -- Now handle the allowed access-type case
5957 and then Is_Access_Type (Old_Formal_Base)
5958 and then Is_Access_Type (New_Formal_Base)
5960 -- The type kinds must match. The only exception occurs with
5961 -- multiple generics of the form:
5964 -- type F is private; type A is private;
5965 -- type F_Ptr is access F; type A_Ptr is access A;
5966 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5967 -- package F_Pack is ... package A_Pack is
5968 -- package F_Inst is
5969 -- new F_Pack (A, A_Ptr, A_P);
5971 -- When checking for conformance between the parameters of A_P
5972 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5973 -- because the compiler has transformed A_Ptr into a subtype of
5974 -- F_Ptr. We catch this case in the code below.
5976 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5978 (Is_Generic_Type (Old_Formal_Base)
5979 and then Is_Generic_Type (New_Formal_Base)
5980 and then Is_Internal (New_Formal_Base)
5981 and then Etype (Etype (New_Formal_Base)) =
5983 and then Directly_Designated_Type (Old_Formal_Base) =
5984 Directly_Designated_Type (New_Formal_Base)
5985 and then ((Is_Itype (Old_Formal_Base)
5986 and then Can_Never_Be_Null (Old_Formal_Base))
5988 (Is_Itype (New_Formal_Base)
5989 and then Can_Never_Be_Null (New_Formal_Base)));
5991 -- Types must always match. In the visible part of an instance,
5992 -- usual overloading rules for dispatching operations apply, and
5993 -- we check base types (not the actual subtypes).
5995 if In_Instance_Visible_Part
5996 and then Is_Dispatching_Operation (New_Id)
5998 if not Conforming_Types
5999 (T1 => Base_Type (Etype (Old_Formal)),
6000 T2 => Base_Type (Etype (New_Formal)),
6002 Get_Inst => Get_Inst)
6003 and then not Access_Types_Match
6005 Conformance_Error ("\type of & does not match!", New_Formal);
6009 elsif not Conforming_Types
6010 (T1 => Old_Formal_Base,
6011 T2 => New_Formal_Base,
6013 Get_Inst => Get_Inst)
6014 and then not Access_Types_Match
6016 -- Don't give error message if old type is Any_Type. This test
6017 -- avoids some cascaded errors, e.g. in case of a bad spec.
6019 if Errmsg and then Old_Formal_Base = Any_Type then
6022 Conformance_Error ("\type of & does not match!", New_Formal);
6028 -- For mode conformance, mode must match
6030 if Ctype >= Mode_Conformant then
6031 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6032 if not Ekind_In (New_Id, E_Function, E_Procedure)
6033 or else not Is_Primitive_Wrapper (New_Id)
6035 Conformance_Error ("\mode of & does not match!", New_Formal);
6039 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6041 if Is_Protected_Type
6042 (Corresponding_Concurrent_Type (T))
6044 Error_Msg_PT (T, New_Id);
6047 ("\mode of & does not match!", New_Formal);
6054 -- Part of mode conformance for access types is having the same
6055 -- constant modifier.
6057 elsif Access_Types_Match
6058 and then Is_Access_Constant (Old_Formal_Base) /=
6059 Is_Access_Constant (New_Formal_Base)
6062 ("\constant modifier does not match!", New_Formal);
6067 if Ctype >= Subtype_Conformant then
6069 -- Ada 2005 (AI-231): In case of anonymous access types check
6070 -- the null-exclusion and access-to-constant attributes must
6071 -- match. For null exclusion, we test the types rather than the
6072 -- formals themselves, since the attribute is only set reliably
6073 -- on the formals in the Ada 95 case, and we exclude the case
6074 -- where Old_Formal is marked as controlling, to avoid errors
6075 -- when matching completing bodies with dispatching declarations
6076 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6078 if Ada_Version >= Ada_2005
6079 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6080 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6082 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6083 Can_Never_Be_Null (Etype (New_Formal))
6085 not Is_Controlling_Formal (Old_Formal))
6087 Is_Access_Constant (Etype (Old_Formal)) /=
6088 Is_Access_Constant (Etype (New_Formal)))
6090 -- Do not complain if error already posted on New_Formal. This
6091 -- avoids some redundant error messages.
6093 and then not Error_Posted (New_Formal)
6095 -- It is allowed to omit the null-exclusion in case of stream
6096 -- attribute subprograms. We recognize stream subprograms
6097 -- through their TSS-generated suffix.
6100 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6103 if TSS_Name /= TSS_Stream_Read
6104 and then TSS_Name /= TSS_Stream_Write
6105 and then TSS_Name /= TSS_Stream_Input
6106 and then TSS_Name /= TSS_Stream_Output
6108 -- Here we have a definite conformance error. It is worth
6109 -- special casing the error message for the case of a
6110 -- controlling formal (which excludes null).
6112 if Is_Controlling_Formal (New_Formal) then
6113 Error_Msg_Node_2 := Scope (New_Formal);
6115 ("\controlling formal& of& excludes null, "
6116 & "declaration must exclude null as well",
6119 -- Normal case (couldn't we give more detail here???)
6123 ("\type of & does not match!", New_Formal);
6132 -- Full conformance checks
6134 if Ctype = Fully_Conformant then
6136 -- We have checked already that names match
6138 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6140 -- Check default expressions for in parameters
6143 NewD : constant Boolean :=
6144 Present (Default_Value (New_Formal));
6145 OldD : constant Boolean :=
6146 Present (Default_Value (Old_Formal));
6148 if NewD or OldD then
6150 -- The old default value has been analyzed because the
6151 -- current full declaration will have frozen everything
6152 -- before. The new default value has not been analyzed,
6153 -- so analyze it now before we check for conformance.
6156 Push_Scope (New_Id);
6157 Preanalyze_Spec_Expression
6158 (Default_Value (New_Formal), Etype (New_Formal));
6162 if not (NewD and OldD)
6163 or else not Fully_Conformant_Expressions
6164 (Default_Value (Old_Formal),
6165 Default_Value (New_Formal))
6168 ("\default expression for & does not match!",
6177 -- A couple of special checks for Ada 83 mode. These checks are
6178 -- skipped if either entity is an operator in package Standard,
6179 -- or if either old or new instance is not from the source program.
6181 if Ada_Version = Ada_83
6182 and then Sloc (Old_Id) > Standard_Location
6183 and then Sloc (New_Id) > Standard_Location
6184 and then Comes_From_Source (Old_Id)
6185 and then Comes_From_Source (New_Id)
6188 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6189 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6192 -- Explicit IN must be present or absent in both cases. This
6193 -- test is required only in the full conformance case.
6195 if In_Present (Old_Param) /= In_Present (New_Param)
6196 and then Ctype = Fully_Conformant
6199 ("\(Ada 83) IN must appear in both declarations",
6204 -- Grouping (use of comma in param lists) must be the same
6205 -- This is where we catch a misconformance like:
6208 -- A : Integer; B : Integer
6210 -- which are represented identically in the tree except
6211 -- for the setting of the flags More_Ids and Prev_Ids.
6213 if More_Ids (Old_Param) /= More_Ids (New_Param)
6214 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6217 ("\grouping of & does not match!", New_Formal);
6223 -- This label is required when skipping controlling formals
6225 <<Skip_Controlling_Formal>>
6227 Next_Formal (Old_Formal);
6228 Next_Formal (New_Formal);
6231 if Present (Old_Formal) then
6232 Conformance_Error ("\too few parameters!");
6235 elsif Present (New_Formal) then
6236 Conformance_Error ("\too many parameters!", New_Formal);
6239 end Check_Conformance;
6241 -----------------------
6242 -- Check_Conventions --
6243 -----------------------
6245 procedure Check_Conventions (Typ : Entity_Id) is
6246 Ifaces_List : Elist_Id;
6248 procedure Check_Convention (Op : Entity_Id);
6249 -- Verify that the convention of inherited dispatching operation Op is
6250 -- consistent among all subprograms it overrides. In order to minimize
6251 -- the search, Search_From is utilized to designate a specific point in
6252 -- the list rather than iterating over the whole list once more.
6254 ----------------------
6255 -- Check_Convention --
6256 ----------------------
6258 procedure Check_Convention (Op : Entity_Id) is
6259 function Convention_Of (Id : Entity_Id) return Convention_Id;
6260 -- Given an entity, return its convention. The function treats Ghost
6261 -- as convention Ada because the two have the same dynamic semantics.
6267 function Convention_Of (Id : Entity_Id) return Convention_Id is
6268 Conv : constant Convention_Id := Convention (Id);
6270 if Conv = Convention_Ghost then
6271 return Convention_Ada;
6279 Op_Conv : constant Convention_Id := Convention_Of (Op);
6280 Iface_Conv : Convention_Id;
6281 Iface_Elmt : Elmt_Id;
6282 Iface_Prim_Elmt : Elmt_Id;
6283 Iface_Prim : Entity_Id;
6285 -- Start of processing for Check_Convention
6288 Iface_Elmt := First_Elmt (Ifaces_List);
6289 while Present (Iface_Elmt) loop
6291 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6292 while Present (Iface_Prim_Elmt) loop
6293 Iface_Prim := Node (Iface_Prim_Elmt);
6294 Iface_Conv := Convention_Of (Iface_Prim);
6296 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6297 and then Iface_Conv /= Op_Conv
6300 ("inconsistent conventions in primitive operations", Typ);
6302 Error_Msg_Name_1 := Chars (Op);
6303 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6304 Error_Msg_Sloc := Sloc (Op);
6306 if Comes_From_Source (Op) or else No (Alias (Op)) then
6307 if not Present (Overridden_Operation (Op)) then
6308 Error_Msg_N ("\\primitive % defined #", Typ);
6311 ("\\overriding operation % with " &
6312 "convention % defined #", Typ);
6315 else pragma Assert (Present (Alias (Op)));
6316 Error_Msg_Sloc := Sloc (Alias (Op));
6318 ("\\inherited operation % with " &
6319 "convention % defined #", Typ);
6322 Error_Msg_Name_1 := Chars (Op);
6323 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6324 Error_Msg_Sloc := Sloc (Iface_Prim);
6326 ("\\overridden operation % with " &
6327 "convention % defined #", Typ);
6329 -- Avoid cascading errors
6334 Next_Elmt (Iface_Prim_Elmt);
6337 Next_Elmt (Iface_Elmt);
6339 end Check_Convention;
6343 Prim_Op : Entity_Id;
6344 Prim_Op_Elmt : Elmt_Id;
6346 -- Start of processing for Check_Conventions
6349 if not Has_Interfaces (Typ) then
6353 Collect_Interfaces (Typ, Ifaces_List);
6355 -- The algorithm checks every overriding dispatching operation against
6356 -- all the corresponding overridden dispatching operations, detecting
6357 -- differences in conventions.
6359 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6360 while Present (Prim_Op_Elmt) loop
6361 Prim_Op := Node (Prim_Op_Elmt);
6363 -- A small optimization: skip the predefined dispatching operations
6364 -- since they always have the same convention.
6366 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6367 Check_Convention (Prim_Op);
6370 Next_Elmt (Prim_Op_Elmt);
6372 end Check_Conventions;
6374 ------------------------------
6375 -- Check_Delayed_Subprogram --
6376 ------------------------------
6378 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6381 procedure Possible_Freeze (T : Entity_Id);
6382 -- T is the type of either a formal parameter or of the return type.
6383 -- If T is not yet frozen and needs a delayed freeze, then the
6384 -- subprogram itself must be delayed. If T is the limited view of an
6385 -- incomplete type the subprogram must be frozen as well, because
6386 -- T may depend on local types that have not been frozen yet.
6388 ---------------------
6389 -- Possible_Freeze --
6390 ---------------------
6392 procedure Possible_Freeze (T : Entity_Id) is
6394 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6395 Set_Has_Delayed_Freeze (Designator);
6397 elsif Is_Access_Type (T)
6398 and then Has_Delayed_Freeze (Designated_Type (T))
6399 and then not Is_Frozen (Designated_Type (T))
6401 Set_Has_Delayed_Freeze (Designator);
6403 elsif Ekind (T) = E_Incomplete_Type
6404 and then From_Limited_With (T)
6406 Set_Has_Delayed_Freeze (Designator);
6408 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6409 -- of a subprogram or entry declaration.
6411 elsif Ekind (T) = E_Incomplete_Type
6412 and then Ada_Version >= Ada_2012
6414 Set_Has_Delayed_Freeze (Designator);
6417 end Possible_Freeze;
6419 -- Start of processing for Check_Delayed_Subprogram
6422 -- All subprograms, including abstract subprograms, may need a freeze
6423 -- node if some formal type or the return type needs one.
6425 Possible_Freeze (Etype (Designator));
6426 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6428 -- Need delayed freeze if any of the formal types themselves need
6429 -- a delayed freeze and are not yet frozen.
6431 F := First_Formal (Designator);
6432 while Present (F) loop
6433 Possible_Freeze (Etype (F));
6434 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6438 -- Mark functions that return by reference. Note that it cannot be
6439 -- done for delayed_freeze subprograms because the underlying
6440 -- returned type may not be known yet (for private types)
6442 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6444 Typ : constant Entity_Id := Etype (Designator);
6445 Utyp : constant Entity_Id := Underlying_Type (Typ);
6447 if Is_Limited_View (Typ) then
6448 Set_Returns_By_Ref (Designator);
6449 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6450 Set_Returns_By_Ref (Designator);
6454 end Check_Delayed_Subprogram;
6456 ------------------------------------
6457 -- Check_Discriminant_Conformance --
6458 ------------------------------------
6460 procedure Check_Discriminant_Conformance
6465 Old_Discr : Entity_Id := First_Discriminant (Prev);
6466 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6467 New_Discr_Id : Entity_Id;
6468 New_Discr_Type : Entity_Id;
6470 procedure Conformance_Error (Msg : String; N : Node_Id);
6471 -- Post error message for conformance error on given node. Two messages
6472 -- are output. The first points to the previous declaration with a
6473 -- general "no conformance" message. The second is the detailed reason,
6474 -- supplied as Msg. The parameter N provide information for a possible
6475 -- & insertion in the message.
6477 -----------------------
6478 -- Conformance_Error --
6479 -----------------------
6481 procedure Conformance_Error (Msg : String; N : Node_Id) is
6483 Error_Msg_Sloc := Sloc (Prev_Loc);
6484 Error_Msg_N -- CODEFIX
6485 ("not fully conformant with declaration#!", N);
6486 Error_Msg_NE (Msg, N, N);
6487 end Conformance_Error;
6489 -- Start of processing for Check_Discriminant_Conformance
6492 while Present (Old_Discr) and then Present (New_Discr) loop
6493 New_Discr_Id := Defining_Identifier (New_Discr);
6495 -- The subtype mark of the discriminant on the full type has not
6496 -- been analyzed so we do it here. For an access discriminant a new
6499 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6501 Access_Definition (N, Discriminant_Type (New_Discr));
6504 Analyze (Discriminant_Type (New_Discr));
6505 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6507 -- Ada 2005: if the discriminant definition carries a null
6508 -- exclusion, create an itype to check properly for consistency
6509 -- with partial declaration.
6511 if Is_Access_Type (New_Discr_Type)
6512 and then Null_Exclusion_Present (New_Discr)
6515 Create_Null_Excluding_Itype
6516 (T => New_Discr_Type,
6517 Related_Nod => New_Discr,
6518 Scope_Id => Current_Scope);
6522 if not Conforming_Types
6523 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6525 Conformance_Error ("type of & does not match!", New_Discr_Id);
6528 -- Treat the new discriminant as an occurrence of the old one,
6529 -- for navigation purposes, and fill in some semantic
6530 -- information, for completeness.
6532 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6533 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6534 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6539 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6540 Conformance_Error ("name & does not match!", New_Discr_Id);
6544 -- Default expressions must match
6547 NewD : constant Boolean :=
6548 Present (Expression (New_Discr));
6549 OldD : constant Boolean :=
6550 Present (Expression (Parent (Old_Discr)));
6553 if NewD or OldD then
6555 -- The old default value has been analyzed and expanded,
6556 -- because the current full declaration will have frozen
6557 -- everything before. The new default values have not been
6558 -- expanded, so expand now to check conformance.
6561 Preanalyze_Spec_Expression
6562 (Expression (New_Discr), New_Discr_Type);
6565 if not (NewD and OldD)
6566 or else not Fully_Conformant_Expressions
6567 (Expression (Parent (Old_Discr)),
6568 Expression (New_Discr))
6572 ("default expression for & does not match!",
6579 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6581 if Ada_Version = Ada_83 then
6583 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6586 -- Grouping (use of comma in param lists) must be the same
6587 -- This is where we catch a misconformance like:
6590 -- A : Integer; B : Integer
6592 -- which are represented identically in the tree except
6593 -- for the setting of the flags More_Ids and Prev_Ids.
6595 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6596 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6599 ("grouping of & does not match!", New_Discr_Id);
6605 Next_Discriminant (Old_Discr);
6609 if Present (Old_Discr) then
6610 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6613 elsif Present (New_Discr) then
6615 ("too many discriminants!", Defining_Identifier (New_Discr));
6618 end Check_Discriminant_Conformance;
6620 ----------------------------
6621 -- Check_Fully_Conformant --
6622 ----------------------------
6624 procedure Check_Fully_Conformant
6625 (New_Id : Entity_Id;
6627 Err_Loc : Node_Id := Empty)
6630 pragma Warnings (Off, Result);
6633 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6634 end Check_Fully_Conformant;
6636 ---------------------------
6637 -- Check_Mode_Conformant --
6638 ---------------------------
6640 procedure Check_Mode_Conformant
6641 (New_Id : Entity_Id;
6643 Err_Loc : Node_Id := Empty;
6644 Get_Inst : Boolean := False)
6647 pragma Warnings (Off, Result);
6650 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6651 end Check_Mode_Conformant;
6653 --------------------------------
6654 -- Check_Overriding_Indicator --
6655 --------------------------------
6657 procedure Check_Overriding_Indicator
6659 Overridden_Subp : Entity_Id;
6660 Is_Primitive : Boolean)
6666 -- No overriding indicator for literals
6668 if Ekind (Subp) = E_Enumeration_Literal then
6671 elsif Ekind (Subp) = E_Entry then
6672 Decl := Parent (Subp);
6674 -- No point in analyzing a malformed operator
6676 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6677 and then Error_Posted (Subp)
6682 Decl := Unit_Declaration_Node (Subp);
6685 if Nkind_In (Decl, N_Subprogram_Body,
6686 N_Subprogram_Body_Stub,
6687 N_Subprogram_Declaration,
6688 N_Abstract_Subprogram_Declaration,
6689 N_Subprogram_Renaming_Declaration)
6691 Spec := Specification (Decl);
6693 elsif Nkind (Decl) = N_Entry_Declaration then
6700 -- The overriding operation is type conformant with the overridden one,
6701 -- but the names of the formals are not required to match. If the names
6702 -- appear permuted in the overriding operation, this is a possible
6703 -- source of confusion that is worth diagnosing. Controlling formals
6704 -- often carry names that reflect the type, and it is not worthwhile
6705 -- requiring that their names match.
6707 if Present (Overridden_Subp)
6708 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6715 Form1 := First_Formal (Subp);
6716 Form2 := First_Formal (Overridden_Subp);
6718 -- If the overriding operation is a synchronized operation, skip
6719 -- the first parameter of the overridden operation, which is
6720 -- implicit in the new one. If the operation is declared in the
6721 -- body it is not primitive and all formals must match.
6723 if Is_Concurrent_Type (Scope (Subp))
6724 and then Is_Tagged_Type (Scope (Subp))
6725 and then not Has_Completion (Scope (Subp))
6727 Form2 := Next_Formal (Form2);
6730 if Present (Form1) then
6731 Form1 := Next_Formal (Form1);
6732 Form2 := Next_Formal (Form2);
6735 while Present (Form1) loop
6736 if not Is_Controlling_Formal (Form1)
6737 and then Present (Next_Formal (Form2))
6738 and then Chars (Form1) = Chars (Next_Formal (Form2))
6740 Error_Msg_Node_2 := Alias (Overridden_Subp);
6741 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6743 ("& does not match corresponding formal of&#",
6748 Next_Formal (Form1);
6749 Next_Formal (Form2);
6754 -- If there is an overridden subprogram, then check that there is no
6755 -- "not overriding" indicator, and mark the subprogram as overriding.
6756 -- This is not done if the overridden subprogram is marked as hidden,
6757 -- which can occur for the case of inherited controlled operations
6758 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6759 -- subprogram is not itself hidden. (Note: This condition could probably
6760 -- be simplified, leaving out the testing for the specific controlled
6761 -- cases, but it seems safer and clearer this way, and echoes similar
6762 -- special-case tests of this kind in other places.)
6764 if Present (Overridden_Subp)
6765 and then (not Is_Hidden (Overridden_Subp)
6767 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6770 and then Present (Alias (Overridden_Subp))
6771 and then not Is_Hidden (Alias (Overridden_Subp))))
6773 if Must_Not_Override (Spec) then
6774 Error_Msg_Sloc := Sloc (Overridden_Subp);
6776 if Ekind (Subp) = E_Entry then
6778 ("entry & overrides inherited operation #", Spec, Subp);
6781 ("subprogram & overrides inherited operation #", Spec, Subp);
6784 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6785 -- as an extension of Root_Controlled, and thus has a useless Adjust
6786 -- operation. This operation should not be inherited by other limited
6787 -- controlled types. An explicit Adjust for them is not overriding.
6789 elsif Must_Override (Spec)
6790 and then Chars (Overridden_Subp) = Name_Adjust
6791 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6792 and then Present (Alias (Overridden_Subp))
6794 Is_Predefined_File_Name
6795 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6797 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6799 elsif Is_Subprogram (Subp) then
6800 if Is_Init_Proc (Subp) then
6803 elsif No (Overridden_Operation (Subp)) then
6805 -- For entities generated by Derive_Subprograms the overridden
6806 -- operation is the inherited primitive (which is available
6807 -- through the attribute alias)
6809 if (Is_Dispatching_Operation (Subp)
6810 or else Is_Dispatching_Operation (Overridden_Subp))
6811 and then not Comes_From_Source (Overridden_Subp)
6812 and then Find_Dispatching_Type (Overridden_Subp) =
6813 Find_Dispatching_Type (Subp)
6814 and then Present (Alias (Overridden_Subp))
6815 and then Comes_From_Source (Alias (Overridden_Subp))
6817 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6820 Set_Overridden_Operation (Subp, Overridden_Subp);
6825 -- If primitive flag is set or this is a protected operation, then
6826 -- the operation is overriding at the point of its declaration, so
6827 -- warn if necessary. Otherwise it may have been declared before the
6828 -- operation it overrides and no check is required.
6831 and then not Must_Override (Spec)
6832 and then (Is_Primitive
6833 or else Ekind (Scope (Subp)) = E_Protected_Type)
6835 Style.Missing_Overriding (Decl, Subp);
6838 -- If Subp is an operator, it may override a predefined operation, if
6839 -- it is defined in the same scope as the type to which it applies.
6840 -- In that case Overridden_Subp is empty because of our implicit
6841 -- representation for predefined operators. We have to check whether the
6842 -- signature of Subp matches that of a predefined operator. Note that
6843 -- first argument provides the name of the operator, and the second
6844 -- argument the signature that may match that of a standard operation.
6845 -- If the indicator is overriding, then the operator must match a
6846 -- predefined signature, because we know already that there is no
6847 -- explicit overridden operation.
6849 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6850 if Must_Not_Override (Spec) then
6852 -- If this is not a primitive or a protected subprogram, then
6853 -- "not overriding" is illegal.
6856 and then Ekind (Scope (Subp)) /= E_Protected_Type
6859 ("overriding indicator only allowed "
6860 & "if subprogram is primitive", Subp);
6862 elsif Can_Override_Operator (Subp) then
6864 ("subprogram& overrides predefined operator ", Spec, Subp);
6867 elsif Must_Override (Spec) then
6868 if No (Overridden_Operation (Subp))
6869 and then not Can_Override_Operator (Subp)
6871 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6874 elsif not Error_Posted (Subp)
6875 and then Style_Check
6876 and then Can_Override_Operator (Subp)
6878 not Is_Predefined_File_Name
6879 (Unit_File_Name (Get_Source_Unit (Subp)))
6881 -- If style checks are enabled, indicate that the indicator is
6882 -- missing. However, at the point of declaration, the type of
6883 -- which this is a primitive operation may be private, in which
6884 -- case the indicator would be premature.
6886 if Has_Private_Declaration (Etype (Subp))
6887 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6891 Style.Missing_Overriding (Decl, Subp);
6895 elsif Must_Override (Spec) then
6896 if Ekind (Subp) = E_Entry then
6897 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6899 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6902 -- If the operation is marked "not overriding" and it's not primitive
6903 -- then an error is issued, unless this is an operation of a task or
6904 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6905 -- has been specified have already been checked above.
6907 elsif Must_Not_Override (Spec)
6908 and then not Is_Primitive
6909 and then Ekind (Subp) /= E_Entry
6910 and then Ekind (Scope (Subp)) /= E_Protected_Type
6913 ("overriding indicator only allowed if subprogram is primitive",
6917 end Check_Overriding_Indicator;
6923 -- Note: this procedure needs to know far too much about how the expander
6924 -- messes with exceptions. The use of the flag Exception_Junk and the
6925 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6926 -- works, but is not very clean. It would be better if the expansion
6927 -- routines would leave Original_Node working nicely, and we could use
6928 -- Original_Node here to ignore all the peculiar expander messing ???
6930 procedure Check_Returns
6934 Proc : Entity_Id := Empty)
6938 procedure Check_Statement_Sequence (L : List_Id);
6939 -- Internal recursive procedure to check a list of statements for proper
6940 -- termination by a return statement (or a transfer of control or a
6941 -- compound statement that is itself internally properly terminated).
6943 ------------------------------
6944 -- Check_Statement_Sequence --
6945 ------------------------------
6947 procedure Check_Statement_Sequence (L : List_Id) is
6952 Raise_Exception_Call : Boolean;
6953 -- Set True if statement sequence terminated by Raise_Exception call
6954 -- or a Reraise_Occurrence call.
6957 Raise_Exception_Call := False;
6959 -- Get last real statement
6961 Last_Stm := Last (L);
6963 -- Deal with digging out exception handler statement sequences that
6964 -- have been transformed by the local raise to goto optimization.
6965 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6966 -- optimization has occurred, we are looking at something like:
6969 -- original stmts in block
6973 -- goto L1; | omitted if No_Exception_Propagation
6978 -- goto L3; -- skip handler when exception not raised
6980 -- <<L1>> -- target label for local exception
6994 -- and what we have to do is to dig out the estmts1 and estmts2
6995 -- sequences (which were the original sequences of statements in
6996 -- the exception handlers) and check them.
6998 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7003 exit when Nkind (Stm) /= N_Block_Statement;
7004 exit when not Exception_Junk (Stm);
7007 exit when Nkind (Stm) /= N_Label;
7008 exit when not Exception_Junk (Stm);
7009 Check_Statement_Sequence
7010 (Statements (Handled_Statement_Sequence (Next (Stm))));
7015 exit when Nkind (Stm) /= N_Goto_Statement;
7016 exit when not Exception_Junk (Stm);
7020 -- Don't count pragmas
7022 while Nkind (Last_Stm) = N_Pragma
7024 -- Don't count call to SS_Release (can happen after Raise_Exception)
7027 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7029 Nkind (Name (Last_Stm)) = N_Identifier
7031 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7033 -- Don't count exception junk
7036 (Nkind_In (Last_Stm, N_Goto_Statement,
7038 N_Object_Declaration)
7039 and then Exception_Junk (Last_Stm))
7040 or else Nkind (Last_Stm) in N_Push_xxx_Label
7041 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7043 -- Inserted code, such as finalization calls, is irrelevant: we only
7044 -- need to check original source.
7046 or else Is_Rewrite_Insertion (Last_Stm)
7051 -- Here we have the "real" last statement
7053 Kind := Nkind (Last_Stm);
7055 -- Transfer of control, OK. Note that in the No_Return procedure
7056 -- case, we already diagnosed any explicit return statements, so
7057 -- we can treat them as OK in this context.
7059 if Is_Transfer (Last_Stm) then
7062 -- Check cases of explicit non-indirect procedure calls
7064 elsif Kind = N_Procedure_Call_Statement
7065 and then Is_Entity_Name (Name (Last_Stm))
7067 -- Check call to Raise_Exception procedure which is treated
7068 -- specially, as is a call to Reraise_Occurrence.
7070 -- We suppress the warning in these cases since it is likely that
7071 -- the programmer really does not expect to deal with the case
7072 -- of Null_Occurrence, and thus would find a warning about a
7073 -- missing return curious, and raising Program_Error does not
7074 -- seem such a bad behavior if this does occur.
7076 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7077 -- behavior will be to raise Constraint_Error (see AI-329).
7079 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7081 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7083 Raise_Exception_Call := True;
7085 -- For Raise_Exception call, test first argument, if it is
7086 -- an attribute reference for a 'Identity call, then we know
7087 -- that the call cannot possibly return.
7090 Arg : constant Node_Id :=
7091 Original_Node (First_Actual (Last_Stm));
7093 if Nkind (Arg) = N_Attribute_Reference
7094 and then Attribute_Name (Arg) = Name_Identity
7101 -- If statement, need to look inside if there is an else and check
7102 -- each constituent statement sequence for proper termination.
7104 elsif Kind = N_If_Statement
7105 and then Present (Else_Statements (Last_Stm))
7107 Check_Statement_Sequence (Then_Statements (Last_Stm));
7108 Check_Statement_Sequence (Else_Statements (Last_Stm));
7110 if Present (Elsif_Parts (Last_Stm)) then
7112 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7115 while Present (Elsif_Part) loop
7116 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7124 -- Case statement, check each case for proper termination
7126 elsif Kind = N_Case_Statement then
7130 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7131 while Present (Case_Alt) loop
7132 Check_Statement_Sequence (Statements (Case_Alt));
7133 Next_Non_Pragma (Case_Alt);
7139 -- Block statement, check its handled sequence of statements
7141 elsif Kind = N_Block_Statement then
7147 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7156 -- Loop statement. If there is an iteration scheme, we can definitely
7157 -- fall out of the loop. Similarly if there is an exit statement, we
7158 -- can fall out. In either case we need a following return.
7160 elsif Kind = N_Loop_Statement then
7161 if Present (Iteration_Scheme (Last_Stm))
7162 or else Has_Exit (Entity (Identifier (Last_Stm)))
7166 -- A loop with no exit statement or iteration scheme is either
7167 -- an infinite loop, or it has some other exit (raise/return).
7168 -- In either case, no warning is required.
7174 -- Timed entry call, check entry call and delay alternatives
7176 -- Note: in expanded code, the timed entry call has been converted
7177 -- to a set of expanded statements on which the check will work
7178 -- correctly in any case.
7180 elsif Kind = N_Timed_Entry_Call then
7182 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7183 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7186 -- If statement sequence of entry call alternative is missing,
7187 -- then we can definitely fall through, and we post the error
7188 -- message on the entry call alternative itself.
7190 if No (Statements (ECA)) then
7193 -- If statement sequence of delay alternative is missing, then
7194 -- we can definitely fall through, and we post the error
7195 -- message on the delay alternative itself.
7197 -- Note: if both ECA and DCA are missing the return, then we
7198 -- post only one message, should be enough to fix the bugs.
7199 -- If not we will get a message next time on the DCA when the
7202 elsif No (Statements (DCA)) then
7205 -- Else check both statement sequences
7208 Check_Statement_Sequence (Statements (ECA));
7209 Check_Statement_Sequence (Statements (DCA));
7214 -- Conditional entry call, check entry call and else part
7216 -- Note: in expanded code, the conditional entry call has been
7217 -- converted to a set of expanded statements on which the check
7218 -- will work correctly in any case.
7220 elsif Kind = N_Conditional_Entry_Call then
7222 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7225 -- If statement sequence of entry call alternative is missing,
7226 -- then we can definitely fall through, and we post the error
7227 -- message on the entry call alternative itself.
7229 if No (Statements (ECA)) then
7232 -- Else check statement sequence and else part
7235 Check_Statement_Sequence (Statements (ECA));
7236 Check_Statement_Sequence (Else_Statements (Last_Stm));
7242 -- If we fall through, issue appropriate message
7245 if not Raise_Exception_Call then
7247 -- In GNATprove mode, it is an error to have a missing return
7249 Error_Msg_Warn := SPARK_Mode /= On;
7251 ("RETURN statement missing following this statement<<!",
7254 ("\Program_Error ]<<!", Last_Stm);
7257 -- Note: we set Err even though we have not issued a warning
7258 -- because we still have a case of a missing return. This is
7259 -- an extremely marginal case, probably will never be noticed
7260 -- but we might as well get it right.
7264 -- Otherwise we have the case of a procedure marked No_Return
7267 if not Raise_Exception_Call then
7268 if GNATprove_Mode then
7270 ("implied return after this statement "
7271 & "would have raised Program_Error", Last_Stm);
7274 ("implied return after this statement "
7275 & "will raise Program_Error??", Last_Stm);
7278 Error_Msg_Warn := SPARK_Mode /= On;
7280 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7284 RE : constant Node_Id :=
7285 Make_Raise_Program_Error (Sloc (Last_Stm),
7286 Reason => PE_Implicit_Return);
7288 Insert_After (Last_Stm, RE);
7292 end Check_Statement_Sequence;
7294 -- Start of processing for Check_Returns
7298 Check_Statement_Sequence (Statements (HSS));
7300 if Present (Exception_Handlers (HSS)) then
7301 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7302 while Present (Handler) loop
7303 Check_Statement_Sequence (Statements (Handler));
7304 Next_Non_Pragma (Handler);
7309 ----------------------------
7310 -- Check_Subprogram_Order --
7311 ----------------------------
7313 procedure Check_Subprogram_Order (N : Node_Id) is
7315 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7316 -- This is used to check if S1 > S2 in the sense required by this test,
7317 -- for example nameab < namec, but name2 < name10.
7319 -----------------------------
7320 -- Subprogram_Name_Greater --
7321 -----------------------------
7323 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7328 -- Deal with special case where names are identical except for a
7329 -- numerical suffix. These are handled specially, taking the numeric
7330 -- ordering from the suffix into account.
7333 while S1 (L1) in '0' .. '9' loop
7338 while S2 (L2) in '0' .. '9' loop
7342 -- If non-numeric parts non-equal, do straight compare
7344 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7347 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7348 -- that a missing suffix is treated as numeric zero in this test.
7352 while L1 < S1'Last loop
7354 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7358 while L2 < S2'Last loop
7360 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7365 end Subprogram_Name_Greater;
7367 -- Start of processing for Check_Subprogram_Order
7370 -- Check body in alpha order if this is option
7373 and then Style_Check_Order_Subprograms
7374 and then Nkind (N) = N_Subprogram_Body
7375 and then Comes_From_Source (N)
7376 and then In_Extended_Main_Source_Unit (N)
7380 renames Scope_Stack.Table
7381 (Scope_Stack.Last).Last_Subprogram_Name;
7383 Body_Id : constant Entity_Id :=
7384 Defining_Entity (Specification (N));
7387 Get_Decoded_Name_String (Chars (Body_Id));
7390 if Subprogram_Name_Greater
7391 (LSN.all, Name_Buffer (1 .. Name_Len))
7393 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7399 LSN := new String'(Name_Buffer (1 .. Name_Len));
7402 end Check_Subprogram_Order;
7404 ------------------------------
7405 -- Check_Subtype_Conformant --
7406 ------------------------------
7408 procedure Check_Subtype_Conformant
7409 (New_Id : Entity_Id;
7411 Err_Loc : Node_Id := Empty;
7412 Skip_Controlling_Formals : Boolean := False;
7413 Get_Inst : Boolean := False)
7416 pragma Warnings (Off, Result);
7419 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7420 Skip_Controlling_Formals => Skip_Controlling_Formals,
7421 Get_Inst => Get_Inst);
7422 end Check_Subtype_Conformant;
7424 ---------------------------
7425 -- Check_Type_Conformant --
7426 ---------------------------
7428 procedure Check_Type_Conformant
7429 (New_Id : Entity_Id;
7431 Err_Loc : Node_Id := Empty)
7434 pragma Warnings (Off, Result);
7437 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7438 end Check_Type_Conformant;
7440 ---------------------------
7441 -- Can_Override_Operator --
7442 ---------------------------
7444 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7448 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7452 Typ := Base_Type (Etype (First_Formal (Subp)));
7454 -- Check explicitly that the operation is a primitive of the type
7456 return Operator_Matches_Spec (Subp, Subp)
7457 and then not Is_Generic_Type (Typ)
7458 and then Scope (Subp) = Scope (Typ)
7459 and then not Is_Class_Wide_Type (Typ);
7461 end Can_Override_Operator;
7463 ----------------------
7464 -- Conforming_Types --
7465 ----------------------
7467 function Conforming_Types
7470 Ctype : Conformance_Type;
7471 Get_Inst : Boolean := False) return Boolean
7473 Type_1 : Entity_Id := T1;
7474 Type_2 : Entity_Id := T2;
7475 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7477 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7478 -- If neither T1 nor T2 are generic actual types, or if they are in
7479 -- different scopes (e.g. parent and child instances), then verify that
7480 -- the base types are equal. Otherwise T1 and T2 must be on the same
7481 -- subtype chain. The whole purpose of this procedure is to prevent
7482 -- spurious ambiguities in an instantiation that may arise if two
7483 -- distinct generic types are instantiated with the same actual.
7485 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7486 -- An access parameter can designate an incomplete type. If the
7487 -- incomplete type is the limited view of a type from a limited_
7488 -- with_clause, check whether the non-limited view is available. If
7489 -- it is a (non-limited) incomplete type, get the full view.
7491 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7492 -- Returns True if and only if either T1 denotes a limited view of T2
7493 -- or T2 denotes a limited view of T1. This can arise when the limited
7494 -- with view of a type is used in a subprogram declaration and the
7495 -- subprogram body is in the scope of a regular with clause for the
7496 -- same unit. In such a case, the two type entities can be considered
7497 -- identical for purposes of conformance checking.
7499 ----------------------
7500 -- Base_Types_Match --
7501 ----------------------
7503 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7504 BT1 : constant Entity_Id := Base_Type (T1);
7505 BT2 : constant Entity_Id := Base_Type (T2);
7511 elsif BT1 = BT2 then
7513 -- The following is too permissive. A more precise test should
7514 -- check that the generic actual is an ancestor subtype of the
7517 -- See code in Find_Corresponding_Spec that applies an additional
7518 -- filter to handle accidental amiguities in instances.
7520 return not Is_Generic_Actual_Type (T1)
7521 or else not Is_Generic_Actual_Type (T2)
7522 or else Scope (T1) /= Scope (T2);
7524 -- If T2 is a generic actual type it is declared as the subtype of
7525 -- the actual. If that actual is itself a subtype we need to use its
7526 -- own base type to check for compatibility.
7528 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7531 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7537 end Base_Types_Match;
7539 --------------------------
7540 -- Find_Designated_Type --
7541 --------------------------
7543 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7547 Desig := Directly_Designated_Type (T);
7549 if Ekind (Desig) = E_Incomplete_Type then
7551 -- If regular incomplete type, get full view if available
7553 if Present (Full_View (Desig)) then
7554 Desig := Full_View (Desig);
7556 -- If limited view of a type, get non-limited view if available,
7557 -- and check again for a regular incomplete type.
7559 elsif Present (Non_Limited_View (Desig)) then
7560 Desig := Get_Full_View (Non_Limited_View (Desig));
7565 end Find_Designated_Type;
7567 -------------------------------
7568 -- Matches_Limited_With_View --
7569 -------------------------------
7571 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7573 -- In some cases a type imported through a limited_with clause, and
7574 -- its nonlimited view are both visible, for example in an anonymous
7575 -- access-to-class-wide type in a formal. Both entities designate the
7578 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7581 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7584 elsif From_Limited_With (T1)
7585 and then From_Limited_With (T2)
7586 and then Available_View (T1) = Available_View (T2)
7593 end Matches_Limited_With_View;
7595 -- Start of processing for Conforming_Types
7598 -- The context is an instance association for a formal access-to-
7599 -- subprogram type; the formal parameter types require mapping because
7600 -- they may denote other formal parameters of the generic unit.
7603 Type_1 := Get_Instance_Of (T1);
7604 Type_2 := Get_Instance_Of (T2);
7607 -- If one of the types is a view of the other introduced by a limited
7608 -- with clause, treat these as conforming for all purposes.
7610 if Matches_Limited_With_View (T1, T2) then
7613 elsif Base_Types_Match (Type_1, Type_2) then
7614 return Ctype <= Mode_Conformant
7615 or else Subtypes_Statically_Match (Type_1, Type_2);
7617 elsif Is_Incomplete_Or_Private_Type (Type_1)
7618 and then Present (Full_View (Type_1))
7619 and then Base_Types_Match (Full_View (Type_1), Type_2)
7621 return Ctype <= Mode_Conformant
7622 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7624 elsif Ekind (Type_2) = E_Incomplete_Type
7625 and then Present (Full_View (Type_2))
7626 and then Base_Types_Match (Type_1, Full_View (Type_2))
7628 return Ctype <= Mode_Conformant
7629 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7631 elsif Is_Private_Type (Type_2)
7632 and then In_Instance
7633 and then Present (Full_View (Type_2))
7634 and then Base_Types_Match (Type_1, Full_View (Type_2))
7636 return Ctype <= Mode_Conformant
7637 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7640 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7641 -- treated recursively because they carry a signature. As far as
7642 -- conformance is concerned, convention plays no role, and either
7643 -- or both could be access to protected subprograms.
7645 Are_Anonymous_Access_To_Subprogram_Types :=
7646 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7647 E_Anonymous_Access_Protected_Subprogram_Type)
7649 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7650 E_Anonymous_Access_Protected_Subprogram_Type);
7652 -- Test anonymous access type case. For this case, static subtype
7653 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7654 -- the base types because we may have built internal subtype entities
7655 -- to handle null-excluding types (see Process_Formals).
7657 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7659 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7661 -- Ada 2005 (AI-254)
7663 or else Are_Anonymous_Access_To_Subprogram_Types
7666 Desig_1 : Entity_Id;
7667 Desig_2 : Entity_Id;
7670 -- In Ada 2005, access constant indicators must match for
7671 -- subtype conformance.
7673 if Ada_Version >= Ada_2005
7674 and then Ctype >= Subtype_Conformant
7676 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7681 Desig_1 := Find_Designated_Type (Type_1);
7682 Desig_2 := Find_Designated_Type (Type_2);
7684 -- If the context is an instance association for a formal
7685 -- access-to-subprogram type; formal access parameter designated
7686 -- types require mapping because they may denote other formal
7687 -- parameters of the generic unit.
7690 Desig_1 := Get_Instance_Of (Desig_1);
7691 Desig_2 := Get_Instance_Of (Desig_2);
7694 -- It is possible for a Class_Wide_Type to be introduced for an
7695 -- incomplete type, in which case there is a separate class_ wide
7696 -- type for the full view. The types conform if their Etypes
7697 -- conform, i.e. one may be the full view of the other. This can
7698 -- only happen in the context of an access parameter, other uses
7699 -- of an incomplete Class_Wide_Type are illegal.
7701 if Is_Class_Wide_Type (Desig_1)
7703 Is_Class_Wide_Type (Desig_2)
7707 (Etype (Base_Type (Desig_1)),
7708 Etype (Base_Type (Desig_2)), Ctype);
7710 elsif Are_Anonymous_Access_To_Subprogram_Types then
7711 if Ada_Version < Ada_2005 then
7712 return Ctype = Type_Conformant
7714 Subtypes_Statically_Match (Desig_1, Desig_2);
7716 -- We must check the conformance of the signatures themselves
7720 Conformant : Boolean;
7723 (Desig_1, Desig_2, Ctype, False, Conformant);
7729 return Base_Type (Desig_1) = Base_Type (Desig_2)
7730 and then (Ctype = Type_Conformant
7732 Subtypes_Statically_Match (Desig_1, Desig_2));
7736 -- Otherwise definitely no match
7739 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7740 and then Is_Access_Type (Type_2))
7741 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7742 and then Is_Access_Type (Type_1)))
7745 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7747 May_Hide_Profile := True;
7752 end Conforming_Types;
7754 --------------------------
7755 -- Create_Extra_Formals --
7756 --------------------------
7758 procedure Create_Extra_Formals (E : Entity_Id) is
7760 First_Extra : Entity_Id := Empty;
7761 Last_Extra : Entity_Id;
7762 Formal_Type : Entity_Id;
7763 P_Formal : Entity_Id := Empty;
7765 function Add_Extra_Formal
7766 (Assoc_Entity : Entity_Id;
7769 Suffix : String) return Entity_Id;
7770 -- Add an extra formal to the current list of formals and extra formals.
7771 -- The extra formal is added to the end of the list of extra formals,
7772 -- and also returned as the result. These formals are always of mode IN.
7773 -- The new formal has the type Typ, is declared in Scope, and its name
7774 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7775 -- The following suffixes are currently used. They should not be changed
7776 -- without coordinating with CodePeer, which makes use of these to
7777 -- provide better messages.
7779 -- O denotes the Constrained bit.
7780 -- L denotes the accessibility level.
7781 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7782 -- the full list in exp_ch6.BIP_Formal_Kind.
7784 ----------------------
7785 -- Add_Extra_Formal --
7786 ----------------------
7788 function Add_Extra_Formal
7789 (Assoc_Entity : Entity_Id;
7792 Suffix : String) return Entity_Id
7794 EF : constant Entity_Id :=
7795 Make_Defining_Identifier (Sloc (Assoc_Entity),
7796 Chars => New_External_Name (Chars (Assoc_Entity),
7800 -- A little optimization. Never generate an extra formal for the
7801 -- _init operand of an initialization procedure, since it could
7804 if Chars (Formal) = Name_uInit then
7808 Set_Ekind (EF, E_In_Parameter);
7809 Set_Actual_Subtype (EF, Typ);
7810 Set_Etype (EF, Typ);
7811 Set_Scope (EF, Scope);
7812 Set_Mechanism (EF, Default_Mechanism);
7813 Set_Formal_Validity (EF);
7815 if No (First_Extra) then
7817 Set_Extra_Formals (Scope, First_Extra);
7820 if Present (Last_Extra) then
7821 Set_Extra_Formal (Last_Extra, EF);
7827 end Add_Extra_Formal;
7829 -- Start of processing for Create_Extra_Formals
7832 -- We never generate extra formals if expansion is not active because we
7833 -- don't need them unless we are generating code.
7835 if not Expander_Active then
7839 -- No need to generate extra formals in interface thunks whose target
7840 -- primitive has no extra formals.
7842 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7846 -- If this is a derived subprogram then the subtypes of the parent
7847 -- subprogram's formal parameters will be used to determine the need
7848 -- for extra formals.
7850 if Is_Overloadable (E) and then Present (Alias (E)) then
7851 P_Formal := First_Formal (Alias (E));
7854 Last_Extra := Empty;
7855 Formal := First_Formal (E);
7856 while Present (Formal) loop
7857 Last_Extra := Formal;
7858 Next_Formal (Formal);
7861 -- If Extra_formals were already created, don't do it again. This
7862 -- situation may arise for subprogram types created as part of
7863 -- dispatching calls (see Expand_Dispatching_Call)
7865 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7869 -- If the subprogram is a predefined dispatching subprogram then don't
7870 -- generate any extra constrained or accessibility level formals. In
7871 -- general we suppress these for internal subprograms (by not calling
7872 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7873 -- generated stream attributes do get passed through because extra
7874 -- build-in-place formals are needed in some cases (limited 'Input).
7876 if Is_Predefined_Internal_Operation (E) then
7877 goto Test_For_Func_Result_Extras;
7880 Formal := First_Formal (E);
7881 while Present (Formal) loop
7883 -- Create extra formal for supporting the attribute 'Constrained.
7884 -- The case of a private type view without discriminants also
7885 -- requires the extra formal if the underlying type has defaulted
7888 if Ekind (Formal) /= E_In_Parameter then
7889 if Present (P_Formal) then
7890 Formal_Type := Etype (P_Formal);
7892 Formal_Type := Etype (Formal);
7895 -- Do not produce extra formals for Unchecked_Union parameters.
7896 -- Jump directly to the end of the loop.
7898 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7899 goto Skip_Extra_Formal_Generation;
7902 if not Has_Discriminants (Formal_Type)
7903 and then Ekind (Formal_Type) in Private_Kind
7904 and then Present (Underlying_Type (Formal_Type))
7906 Formal_Type := Underlying_Type (Formal_Type);
7909 -- Suppress the extra formal if formal's subtype is constrained or
7910 -- indefinite, or we're compiling for Ada 2012 and the underlying
7911 -- type is tagged and limited. In Ada 2012, a limited tagged type
7912 -- can have defaulted discriminants, but 'Constrained is required
7913 -- to return True, so the formal is never needed (see AI05-0214).
7914 -- Note that this ensures consistency of calling sequences for
7915 -- dispatching operations when some types in a class have defaults
7916 -- on discriminants and others do not (and requiring the extra
7917 -- formal would introduce distributed overhead).
7919 -- If the type does not have a completion yet, treat as prior to
7920 -- Ada 2012 for consistency.
7922 if Has_Discriminants (Formal_Type)
7923 and then not Is_Constrained (Formal_Type)
7924 and then not Is_Indefinite_Subtype (Formal_Type)
7925 and then (Ada_Version < Ada_2012
7926 or else No (Underlying_Type (Formal_Type))
7928 (Is_Limited_Type (Formal_Type)
7931 (Underlying_Type (Formal_Type)))))
7933 Set_Extra_Constrained
7934 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7938 -- Create extra formal for supporting accessibility checking. This
7939 -- is done for both anonymous access formals and formals of named
7940 -- access types that are marked as controlling formals. The latter
7941 -- case can occur when Expand_Dispatching_Call creates a subprogram
7942 -- type and substitutes the types of access-to-class-wide actuals
7943 -- for the anonymous access-to-specific-type of controlling formals.
7944 -- Base_Type is applied because in cases where there is a null
7945 -- exclusion the formal may have an access subtype.
7947 -- This is suppressed if we specifically suppress accessibility
7948 -- checks at the package level for either the subprogram, or the
7949 -- package in which it resides. However, we do not suppress it
7950 -- simply if the scope has accessibility checks suppressed, since
7951 -- this could cause trouble when clients are compiled with a
7952 -- different suppression setting. The explicit checks at the
7953 -- package level are safe from this point of view.
7955 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7956 or else (Is_Controlling_Formal (Formal)
7957 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7959 (Explicit_Suppress (E, Accessibility_Check)
7961 Explicit_Suppress (Scope (E), Accessibility_Check))
7964 or else Present (Extra_Accessibility (P_Formal)))
7966 Set_Extra_Accessibility
7967 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7970 -- This label is required when skipping extra formal generation for
7971 -- Unchecked_Union parameters.
7973 <<Skip_Extra_Formal_Generation>>
7975 if Present (P_Formal) then
7976 Next_Formal (P_Formal);
7979 Next_Formal (Formal);
7982 <<Test_For_Func_Result_Extras>>
7984 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7985 -- function call is ... determined by the point of call ...".
7987 if Needs_Result_Accessibility_Level (E) then
7988 Set_Extra_Accessibility_Of_Result
7989 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7992 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7993 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7995 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7997 Result_Subt : constant Entity_Id := Etype (E);
7998 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7999 Formal_Typ : Entity_Id;
8001 Discard : Entity_Id;
8002 pragma Warnings (Off, Discard);
8005 -- In the case of functions with unconstrained result subtypes,
8006 -- add a 4-state formal indicating whether the return object is
8007 -- allocated by the caller (1), or should be allocated by the
8008 -- callee on the secondary stack (2), in the global heap (3), or
8009 -- in a user-defined storage pool (4). For the moment we just use
8010 -- Natural for the type of this formal. Note that this formal
8011 -- isn't usually needed in the case where the result subtype is
8012 -- constrained, but it is needed when the function has a tagged
8013 -- result, because generally such functions can be called in a
8014 -- dispatching context and such calls must be handled like calls
8015 -- to a class-wide function.
8017 if Needs_BIP_Alloc_Form (E) then
8020 (E, Standard_Natural,
8021 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8023 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8024 -- use a user-defined pool. This formal is not added on
8025 -- .NET/JVM/ZFP as those targets do not support pools.
8027 if VM_Target = No_VM
8028 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8032 (E, RTE (RE_Root_Storage_Pool_Ptr),
8033 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8037 -- In the case of functions whose result type needs finalization,
8038 -- add an extra formal which represents the finalization master.
8040 if Needs_BIP_Finalization_Master (E) then
8043 (E, RTE (RE_Finalization_Master_Ptr),
8044 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8047 -- When the result type contains tasks, add two extra formals: the
8048 -- master of the tasks to be created, and the caller's activation
8051 if Has_Task (Full_Subt) then
8054 (E, RTE (RE_Master_Id),
8055 E, BIP_Formal_Suffix (BIP_Task_Master));
8058 (E, RTE (RE_Activation_Chain_Access),
8059 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8062 -- All build-in-place functions get an extra formal that will be
8063 -- passed the address of the return object within the caller.
8066 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8068 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8069 Set_Etype (Formal_Typ, Formal_Typ);
8070 Set_Depends_On_Private
8071 (Formal_Typ, Has_Private_Component (Formal_Typ));
8072 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8073 Set_Is_Access_Constant (Formal_Typ, False);
8075 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8076 -- the designated type comes from the limited view (for back-end
8079 Set_From_Limited_With
8080 (Formal_Typ, From_Limited_With (Result_Subt));
8082 Layout_Type (Formal_Typ);
8086 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8089 end Create_Extra_Formals;
8091 -----------------------------
8092 -- Enter_Overloaded_Entity --
8093 -----------------------------
8095 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8096 E : Entity_Id := Current_Entity_In_Scope (S);
8097 C_E : Entity_Id := Current_Entity (S);
8101 Set_Has_Homonym (E);
8102 Set_Has_Homonym (S);
8105 Set_Is_Immediately_Visible (S);
8106 Set_Scope (S, Current_Scope);
8108 -- Chain new entity if front of homonym in current scope, so that
8109 -- homonyms are contiguous.
8111 if Present (E) and then E /= C_E then
8112 while Homonym (C_E) /= E loop
8113 C_E := Homonym (C_E);
8116 Set_Homonym (C_E, S);
8120 Set_Current_Entity (S);
8125 if Is_Inherited_Operation (S) then
8126 Append_Inherited_Subprogram (S);
8128 Append_Entity (S, Current_Scope);
8131 Set_Public_Status (S);
8133 if Debug_Flag_E then
8134 Write_Str ("New overloaded entity chain: ");
8135 Write_Name (Chars (S));
8138 while Present (E) loop
8139 Write_Str (" "); Write_Int (Int (E));
8146 -- Generate warning for hiding
8149 and then Comes_From_Source (S)
8150 and then In_Extended_Main_Source_Unit (S)
8157 -- Warn unless genuine overloading. Do not emit warning on
8158 -- hiding predefined operators in Standard (these are either an
8159 -- (artifact of our implicit declarations, or simple noise) but
8160 -- keep warning on a operator defined on a local subtype, because
8161 -- of the real danger that different operators may be applied in
8162 -- various parts of the program.
8164 -- Note that if E and S have the same scope, there is never any
8165 -- hiding. Either the two conflict, and the program is illegal,
8166 -- or S is overriding an implicit inherited subprogram.
8168 if Scope (E) /= Scope (S)
8169 and then (not Is_Overloadable (E)
8170 or else Subtype_Conformant (E, S))
8171 and then (Is_Immediately_Visible (E)
8173 Is_Potentially_Use_Visible (S))
8175 if Scope (E) /= Standard_Standard then
8176 Error_Msg_Sloc := Sloc (E);
8177 Error_Msg_N ("declaration of & hides one#?h?", S);
8179 elsif Nkind (S) = N_Defining_Operator_Symbol
8181 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8184 ("declaration of & hides predefined operator?h?", S);
8189 end Enter_Overloaded_Entity;
8191 -----------------------------
8192 -- Check_Untagged_Equality --
8193 -----------------------------
8195 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8196 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8197 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8201 if Nkind (Decl) = N_Subprogram_Declaration
8202 and then Is_Record_Type (Typ)
8203 and then not Is_Tagged_Type (Typ)
8205 -- If the type is not declared in a package, or if we are in the
8206 -- body of the package or in some other scope, the new operation is
8207 -- not primitive, and therefore legal, though suspicious. If the
8208 -- type is a generic actual (sub)type, the operation is not primitive
8209 -- either because the base type is declared elsewhere.
8211 if Is_Frozen (Typ) then
8212 if Ekind (Scope (Typ)) /= E_Package
8213 or else Scope (Typ) /= Current_Scope
8217 elsif Is_Generic_Actual_Type (Typ) then
8220 elsif In_Package_Body (Scope (Typ)) then
8222 ("equality operator must be declared "
8223 & "before type& is frozen", Eq_Op, Typ);
8225 ("\move declaration to package spec", Eq_Op);
8229 ("equality operator must be declared "
8230 & "before type& is frozen", Eq_Op, Typ);
8232 Obj_Decl := Next (Parent (Typ));
8233 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8234 if Nkind (Obj_Decl) = N_Object_Declaration
8235 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8238 ("type& is frozen by declaration??", Obj_Decl, Typ);
8240 ("\an equality operator cannot be declared after this "
8241 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8249 elsif not In_Same_List (Parent (Typ), Decl)
8250 and then not Is_Limited_Type (Typ)
8253 -- This makes it illegal to have a primitive equality declared in
8254 -- the private part if the type is visible.
8256 Error_Msg_N ("equality operator appears too late", Eq_Op);
8259 end Check_Untagged_Equality;
8261 -----------------------------
8262 -- Find_Corresponding_Spec --
8263 -----------------------------
8265 function Find_Corresponding_Spec
8267 Post_Error : Boolean := True) return Entity_Id
8269 Spec : constant Node_Id := Specification (N);
8270 Designator : constant Entity_Id := Defining_Entity (Spec);
8274 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8275 -- Even if fully conformant, a body may depend on a generic actual when
8276 -- the spec does not, or vice versa, in which case they were distinct
8277 -- entities in the generic.
8279 -------------------------------
8280 -- Different_Generic_Profile --
8281 -------------------------------
8283 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8286 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8287 -- Check that the types of corresponding formals have the same
8288 -- generic actual if any. We have to account for subtypes of a
8289 -- generic formal, declared between a spec and a body, which may
8290 -- appear distinct in an instance but matched in the generic.
8292 -------------------------
8293 -- Same_Generic_Actual --
8294 -------------------------
8296 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8298 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8300 (Present (Parent (T1))
8301 and then Comes_From_Source (Parent (T1))
8302 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8303 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8304 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8305 end Same_Generic_Actual;
8307 -- Start of processing for Different_Generic_Profile
8310 if not In_Instance then
8313 elsif Ekind (E) = E_Function
8314 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8319 F1 := First_Formal (Designator);
8320 F2 := First_Formal (E);
8321 while Present (F1) loop
8322 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8331 end Different_Generic_Profile;
8333 -- Start of processing for Find_Corresponding_Spec
8336 E := Current_Entity (Designator);
8337 while Present (E) loop
8339 -- We are looking for a matching spec. It must have the same scope,
8340 -- and the same name, and either be type conformant, or be the case
8341 -- of a library procedure spec and its body (which belong to one
8342 -- another regardless of whether they are type conformant or not).
8344 if Scope (E) = Current_Scope then
8345 if Current_Scope = Standard_Standard
8346 or else (Ekind (E) = Ekind (Designator)
8347 and then Type_Conformant (E, Designator))
8349 -- Within an instantiation, we know that spec and body are
8350 -- subtype conformant, because they were subtype conformant in
8351 -- the generic. We choose the subtype-conformant entity here as
8352 -- well, to resolve spurious ambiguities in the instance that
8353 -- were not present in the generic (i.e. when two different
8354 -- types are given the same actual). If we are looking for a
8355 -- spec to match a body, full conformance is expected.
8358 Set_Convention (Designator, Convention (E));
8360 -- Skip past subprogram bodies and subprogram renamings that
8361 -- may appear to have a matching spec, but that aren't fully
8362 -- conformant with it. That can occur in cases where an
8363 -- actual type causes unrelated homographs in the instance.
8365 if Nkind_In (N, N_Subprogram_Body,
8366 N_Subprogram_Renaming_Declaration)
8367 and then Present (Homonym (E))
8368 and then not Fully_Conformant (Designator, E)
8372 elsif not Subtype_Conformant (Designator, E) then
8375 elsif Different_Generic_Profile (E) then
8380 -- Ada 2012 (AI05-0165): For internally generated bodies of
8381 -- null procedures locate the internally generated spec. We
8382 -- enforce mode conformance since a tagged type may inherit
8383 -- from interfaces several null primitives which differ only
8384 -- in the mode of the formals.
8386 if not (Comes_From_Source (E))
8387 and then Is_Null_Procedure (E)
8388 and then not Mode_Conformant (Designator, E)
8392 -- For null procedures coming from source that are completions,
8393 -- analysis of the generated body will establish the link.
8395 elsif Comes_From_Source (E)
8396 and then Nkind (Spec) = N_Procedure_Specification
8397 and then Null_Present (Spec)
8401 elsif not Has_Completion (E) then
8402 if Nkind (N) /= N_Subprogram_Body_Stub then
8403 Set_Corresponding_Spec (N, E);
8406 Set_Has_Completion (E);
8409 elsif Nkind (Parent (N)) = N_Subunit then
8411 -- If this is the proper body of a subunit, the completion
8412 -- flag is set when analyzing the stub.
8416 -- If E is an internal function with a controlling result that
8417 -- was created for an operation inherited by a null extension,
8418 -- it may be overridden by a body without a previous spec (one
8419 -- more reason why these should be shunned). In that case we
8420 -- remove the generated body if present, because the current
8421 -- one is the explicit overriding.
8423 elsif Ekind (E) = E_Function
8424 and then Ada_Version >= Ada_2005
8425 and then not Comes_From_Source (E)
8426 and then Has_Controlling_Result (E)
8427 and then Is_Null_Extension (Etype (E))
8428 and then Comes_From_Source (Spec)
8430 Set_Has_Completion (E, False);
8433 and then Nkind (Parent (E)) = N_Function_Specification
8436 (Unit_Declaration_Node
8437 (Corresponding_Body (Unit_Declaration_Node (E))));
8441 -- If expansion is disabled, or if the wrapper function has
8442 -- not been generated yet, this a late body overriding an
8443 -- inherited operation, or it is an overriding by some other
8444 -- declaration before the controlling result is frozen. In
8445 -- either case this is a declaration of a new entity.
8451 -- If the body already exists, then this is an error unless
8452 -- the previous declaration is the implicit declaration of a
8453 -- derived subprogram. It is also legal for an instance to
8454 -- contain type conformant overloadable declarations (but the
8455 -- generic declaration may not), per 8.3(26/2).
8457 elsif No (Alias (E))
8458 and then not Is_Intrinsic_Subprogram (E)
8459 and then not In_Instance
8462 Error_Msg_Sloc := Sloc (E);
8464 if Is_Imported (E) then
8466 ("body not allowed for imported subprogram & declared#",
8469 Error_Msg_NE ("duplicate body for & declared#", N, E);
8473 -- Child units cannot be overloaded, so a conformance mismatch
8474 -- between body and a previous spec is an error.
8476 elsif Is_Child_Unit (E)
8478 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8480 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8485 ("body of child unit does not match previous declaration", N);
8493 -- On exit, we know that no previous declaration of subprogram exists
8496 end Find_Corresponding_Spec;
8498 ----------------------
8499 -- Fully_Conformant --
8500 ----------------------
8502 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8505 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8507 end Fully_Conformant;
8509 ----------------------------------
8510 -- Fully_Conformant_Expressions --
8511 ----------------------------------
8513 function Fully_Conformant_Expressions
8514 (Given_E1 : Node_Id;
8515 Given_E2 : Node_Id) return Boolean
8517 E1 : constant Node_Id := Original_Node (Given_E1);
8518 E2 : constant Node_Id := Original_Node (Given_E2);
8519 -- We always test conformance on original nodes, since it is possible
8520 -- for analysis and/or expansion to make things look as though they
8521 -- conform when they do not, e.g. by converting 1+2 into 3.
8523 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8524 renames Fully_Conformant_Expressions;
8526 function FCL (L1, L2 : List_Id) return Boolean;
8527 -- Compare elements of two lists for conformance. Elements have to be
8528 -- conformant, and actuals inserted as default parameters do not match
8529 -- explicit actuals with the same value.
8531 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8532 -- Compare an operator node with a function call
8538 function FCL (L1, L2 : List_Id) return Boolean is
8542 if L1 = No_List then
8548 if L2 = No_List then
8554 -- Compare two lists, skipping rewrite insertions (we want to compare
8555 -- the original trees, not the expanded versions!)
8558 if Is_Rewrite_Insertion (N1) then
8560 elsif Is_Rewrite_Insertion (N2) then
8566 elsif not FCE (N1, N2) then
8579 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8580 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8585 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8590 Act := First (Actuals);
8592 if Nkind (Op_Node) in N_Binary_Op then
8593 if not FCE (Left_Opnd (Op_Node), Act) then
8600 return Present (Act)
8601 and then FCE (Right_Opnd (Op_Node), Act)
8602 and then No (Next (Act));
8606 -- Start of processing for Fully_Conformant_Expressions
8609 -- Non-conformant if paren count does not match. Note: if some idiot
8610 -- complains that we don't do this right for more than 3 levels of
8611 -- parentheses, they will be treated with the respect they deserve!
8613 if Paren_Count (E1) /= Paren_Count (E2) then
8616 -- If same entities are referenced, then they are conformant even if
8617 -- they have different forms (RM 8.3.1(19-20)).
8619 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8620 if Present (Entity (E1)) then
8621 return Entity (E1) = Entity (E2)
8622 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8623 and then Ekind (Entity (E1)) = E_Discriminant
8624 and then Ekind (Entity (E2)) = E_In_Parameter);
8626 elsif Nkind (E1) = N_Expanded_Name
8627 and then Nkind (E2) = N_Expanded_Name
8628 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8629 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8631 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8634 -- Identifiers in component associations don't always have
8635 -- entities, but their names must conform.
8637 return Nkind (E1) = N_Identifier
8638 and then Nkind (E2) = N_Identifier
8639 and then Chars (E1) = Chars (E2);
8642 elsif Nkind (E1) = N_Character_Literal
8643 and then Nkind (E2) = N_Expanded_Name
8645 return Nkind (Selector_Name (E2)) = N_Character_Literal
8646 and then Chars (E1) = Chars (Selector_Name (E2));
8648 elsif Nkind (E2) = N_Character_Literal
8649 and then Nkind (E1) = N_Expanded_Name
8651 return Nkind (Selector_Name (E1)) = N_Character_Literal
8652 and then Chars (E2) = Chars (Selector_Name (E1));
8654 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8655 return FCO (E1, E2);
8657 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8658 return FCO (E2, E1);
8660 -- Otherwise we must have the same syntactic entity
8662 elsif Nkind (E1) /= Nkind (E2) then
8665 -- At this point, we specialize by node type
8672 FCL (Expressions (E1), Expressions (E2))
8674 FCL (Component_Associations (E1),
8675 Component_Associations (E2));
8678 if Nkind (Expression (E1)) = N_Qualified_Expression
8680 Nkind (Expression (E2)) = N_Qualified_Expression
8682 return FCE (Expression (E1), Expression (E2));
8684 -- Check that the subtype marks and any constraints
8689 Indic1 : constant Node_Id := Expression (E1);
8690 Indic2 : constant Node_Id := Expression (E2);
8695 if Nkind (Indic1) /= N_Subtype_Indication then
8697 Nkind (Indic2) /= N_Subtype_Indication
8698 and then Entity (Indic1) = Entity (Indic2);
8700 elsif Nkind (Indic2) /= N_Subtype_Indication then
8702 Nkind (Indic1) /= N_Subtype_Indication
8703 and then Entity (Indic1) = Entity (Indic2);
8706 if Entity (Subtype_Mark (Indic1)) /=
8707 Entity (Subtype_Mark (Indic2))
8712 Elt1 := First (Constraints (Constraint (Indic1)));
8713 Elt2 := First (Constraints (Constraint (Indic2)));
8714 while Present (Elt1) and then Present (Elt2) loop
8715 if not FCE (Elt1, Elt2) then
8728 when N_Attribute_Reference =>
8730 Attribute_Name (E1) = Attribute_Name (E2)
8731 and then FCL (Expressions (E1), Expressions (E2));
8735 Entity (E1) = Entity (E2)
8736 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8737 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8739 when N_Short_Circuit | N_Membership_Test =>
8741 FCE (Left_Opnd (E1), Left_Opnd (E2))
8743 FCE (Right_Opnd (E1), Right_Opnd (E2));
8745 when N_Case_Expression =>
8751 if not FCE (Expression (E1), Expression (E2)) then
8755 Alt1 := First (Alternatives (E1));
8756 Alt2 := First (Alternatives (E2));
8758 if Present (Alt1) /= Present (Alt2) then
8760 elsif No (Alt1) then
8764 if not FCE (Expression (Alt1), Expression (Alt2))
8765 or else not FCL (Discrete_Choices (Alt1),
8766 Discrete_Choices (Alt2))
8777 when N_Character_Literal =>
8779 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8781 when N_Component_Association =>
8783 FCL (Choices (E1), Choices (E2))
8785 FCE (Expression (E1), Expression (E2));
8787 when N_Explicit_Dereference =>
8789 FCE (Prefix (E1), Prefix (E2));
8791 when N_Extension_Aggregate =>
8793 FCL (Expressions (E1), Expressions (E2))
8794 and then Null_Record_Present (E1) =
8795 Null_Record_Present (E2)
8796 and then FCL (Component_Associations (E1),
8797 Component_Associations (E2));
8799 when N_Function_Call =>
8801 FCE (Name (E1), Name (E2))
8803 FCL (Parameter_Associations (E1),
8804 Parameter_Associations (E2));
8806 when N_If_Expression =>
8808 FCL (Expressions (E1), Expressions (E2));
8810 when N_Indexed_Component =>
8812 FCE (Prefix (E1), Prefix (E2))
8814 FCL (Expressions (E1), Expressions (E2));
8816 when N_Integer_Literal =>
8817 return (Intval (E1) = Intval (E2));
8822 when N_Operator_Symbol =>
8824 Chars (E1) = Chars (E2);
8826 when N_Others_Choice =>
8829 when N_Parameter_Association =>
8831 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8832 and then FCE (Explicit_Actual_Parameter (E1),
8833 Explicit_Actual_Parameter (E2));
8835 when N_Qualified_Expression =>
8837 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8839 FCE (Expression (E1), Expression (E2));
8841 when N_Quantified_Expression =>
8842 if not FCE (Condition (E1), Condition (E2)) then
8846 if Present (Loop_Parameter_Specification (E1))
8847 and then Present (Loop_Parameter_Specification (E2))
8850 L1 : constant Node_Id :=
8851 Loop_Parameter_Specification (E1);
8852 L2 : constant Node_Id :=
8853 Loop_Parameter_Specification (E2);
8857 Reverse_Present (L1) = Reverse_Present (L2)
8859 FCE (Defining_Identifier (L1),
8860 Defining_Identifier (L2))
8862 FCE (Discrete_Subtype_Definition (L1),
8863 Discrete_Subtype_Definition (L2));
8866 elsif Present (Iterator_Specification (E1))
8867 and then Present (Iterator_Specification (E2))
8870 I1 : constant Node_Id := Iterator_Specification (E1);
8871 I2 : constant Node_Id := Iterator_Specification (E2);
8875 FCE (Defining_Identifier (I1),
8876 Defining_Identifier (I2))
8878 Of_Present (I1) = Of_Present (I2)
8880 Reverse_Present (I1) = Reverse_Present (I2)
8881 and then FCE (Name (I1), Name (I2))
8882 and then FCE (Subtype_Indication (I1),
8883 Subtype_Indication (I2));
8886 -- The quantified expressions used different specifications to
8887 -- walk their respective ranges.
8895 FCE (Low_Bound (E1), Low_Bound (E2))
8897 FCE (High_Bound (E1), High_Bound (E2));
8899 when N_Real_Literal =>
8900 return (Realval (E1) = Realval (E2));
8902 when N_Selected_Component =>
8904 FCE (Prefix (E1), Prefix (E2))
8906 FCE (Selector_Name (E1), Selector_Name (E2));
8910 FCE (Prefix (E1), Prefix (E2))
8912 FCE (Discrete_Range (E1), Discrete_Range (E2));
8914 when N_String_Literal =>
8916 S1 : constant String_Id := Strval (E1);
8917 S2 : constant String_Id := Strval (E2);
8918 L1 : constant Nat := String_Length (S1);
8919 L2 : constant Nat := String_Length (S2);
8926 for J in 1 .. L1 loop
8927 if Get_String_Char (S1, J) /=
8928 Get_String_Char (S2, J)
8938 when N_Type_Conversion =>
8940 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8942 FCE (Expression (E1), Expression (E2));
8946 Entity (E1) = Entity (E2)
8948 FCE (Right_Opnd (E1), Right_Opnd (E2));
8950 when N_Unchecked_Type_Conversion =>
8952 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8954 FCE (Expression (E1), Expression (E2));
8956 -- All other node types cannot appear in this context. Strictly
8957 -- we should raise a fatal internal error. Instead we just ignore
8958 -- the nodes. This means that if anyone makes a mistake in the
8959 -- expander and mucks an expression tree irretrievably, the result
8960 -- will be a failure to detect a (probably very obscure) case
8961 -- of non-conformance, which is better than bombing on some
8962 -- case where two expressions do in fact conform.
8969 end Fully_Conformant_Expressions;
8971 ----------------------------------------
8972 -- Fully_Conformant_Discrete_Subtypes --
8973 ----------------------------------------
8975 function Fully_Conformant_Discrete_Subtypes
8976 (Given_S1 : Node_Id;
8977 Given_S2 : Node_Id) return Boolean
8979 S1 : constant Node_Id := Original_Node (Given_S1);
8980 S2 : constant Node_Id := Original_Node (Given_S2);
8982 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8983 -- Special-case for a bound given by a discriminant, which in the body
8984 -- is replaced with the discriminal of the enclosing type.
8986 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8987 -- Check both bounds
8989 -----------------------
8990 -- Conforming_Bounds --
8991 -----------------------
8993 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8995 if Is_Entity_Name (B1)
8996 and then Is_Entity_Name (B2)
8997 and then Ekind (Entity (B1)) = E_Discriminant
8999 return Chars (B1) = Chars (B2);
9002 return Fully_Conformant_Expressions (B1, B2);
9004 end Conforming_Bounds;
9006 -----------------------
9007 -- Conforming_Ranges --
9008 -----------------------
9010 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9013 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9015 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9016 end Conforming_Ranges;
9018 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9021 if Nkind (S1) /= Nkind (S2) then
9024 elsif Is_Entity_Name (S1) then
9025 return Entity (S1) = Entity (S2);
9027 elsif Nkind (S1) = N_Range then
9028 return Conforming_Ranges (S1, S2);
9030 elsif Nkind (S1) = N_Subtype_Indication then
9032 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9035 (Range_Expression (Constraint (S1)),
9036 Range_Expression (Constraint (S2)));
9040 end Fully_Conformant_Discrete_Subtypes;
9042 --------------------
9043 -- Install_Entity --
9044 --------------------
9046 procedure Install_Entity (E : Entity_Id) is
9047 Prev : constant Entity_Id := Current_Entity (E);
9049 Set_Is_Immediately_Visible (E);
9050 Set_Current_Entity (E);
9051 Set_Homonym (E, Prev);
9054 ---------------------
9055 -- Install_Formals --
9056 ---------------------
9058 procedure Install_Formals (Id : Entity_Id) is
9061 F := First_Formal (Id);
9062 while Present (F) loop
9066 end Install_Formals;
9068 -----------------------------
9069 -- Is_Interface_Conformant --
9070 -----------------------------
9072 function Is_Interface_Conformant
9073 (Tagged_Type : Entity_Id;
9074 Iface_Prim : Entity_Id;
9075 Prim : Entity_Id) return Boolean
9077 -- The operation may in fact be an inherited (implicit) operation
9078 -- rather than the original interface primitive, so retrieve the
9079 -- ultimate ancestor.
9081 Iface : constant Entity_Id :=
9082 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9083 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9085 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9086 -- Return the controlling formal of Prim
9088 ------------------------
9089 -- Controlling_Formal --
9090 ------------------------
9092 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9096 E := First_Entity (Prim);
9097 while Present (E) loop
9098 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9106 end Controlling_Formal;
9110 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9111 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9113 -- Start of processing for Is_Interface_Conformant
9116 pragma Assert (Is_Subprogram (Iface_Prim)
9117 and then Is_Subprogram (Prim)
9118 and then Is_Dispatching_Operation (Iface_Prim)
9119 and then Is_Dispatching_Operation (Prim));
9121 pragma Assert (Is_Interface (Iface)
9122 or else (Present (Alias (Iface_Prim))
9125 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9127 if Prim = Iface_Prim
9128 or else not Is_Subprogram (Prim)
9129 or else Ekind (Prim) /= Ekind (Iface_Prim)
9130 or else not Is_Dispatching_Operation (Prim)
9131 or else Scope (Prim) /= Scope (Tagged_Type)
9133 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9134 or else not Primitive_Names_Match (Iface_Prim, Prim)
9138 -- The mode of the controlling formals must match
9140 elsif Present (Iface_Ctrl_F)
9141 and then Present (Prim_Ctrl_F)
9142 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9146 -- Case of a procedure, or a function whose result type matches the
9147 -- result type of the interface primitive, or a function that has no
9148 -- controlling result (I or access I).
9150 elsif Ekind (Iface_Prim) = E_Procedure
9151 or else Etype (Prim) = Etype (Iface_Prim)
9152 or else not Has_Controlling_Result (Prim)
9154 return Type_Conformant
9155 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9157 -- Case of a function returning an interface, or an access to one. Check
9158 -- that the return types correspond.
9160 elsif Implements_Interface (Typ, Iface) then
9161 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9163 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9168 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9169 Skip_Controlling_Formals => True);
9175 end Is_Interface_Conformant;
9177 ---------------------------------
9178 -- Is_Non_Overriding_Operation --
9179 ---------------------------------
9181 function Is_Non_Overriding_Operation
9182 (Prev_E : Entity_Id;
9183 New_E : Entity_Id) return Boolean
9187 G_Typ : Entity_Id := Empty;
9189 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9190 -- If F_Type is a derived type associated with a generic actual subtype,
9191 -- then return its Generic_Parent_Type attribute, else return Empty.
9193 function Types_Correspond
9194 (P_Type : Entity_Id;
9195 N_Type : Entity_Id) return Boolean;
9196 -- Returns true if and only if the types (or designated types in the
9197 -- case of anonymous access types) are the same or N_Type is derived
9198 -- directly or indirectly from P_Type.
9200 -----------------------------
9201 -- Get_Generic_Parent_Type --
9202 -----------------------------
9204 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9210 if Is_Derived_Type (F_Typ)
9211 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9213 -- The tree must be traversed to determine the parent subtype in
9214 -- the generic unit, which unfortunately isn't always available
9215 -- via semantic attributes. ??? (Note: The use of Original_Node
9216 -- is needed for cases where a full derived type has been
9219 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9220 if Nkind (Defn) = N_Derived_Type_Definition then
9221 Indic := Subtype_Indication (Defn);
9223 if Nkind (Indic) = N_Subtype_Indication then
9224 G_Typ := Entity (Subtype_Mark (Indic));
9226 G_Typ := Entity (Indic);
9229 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9230 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9232 return Generic_Parent_Type (Parent (G_Typ));
9238 end Get_Generic_Parent_Type;
9240 ----------------------
9241 -- Types_Correspond --
9242 ----------------------
9244 function Types_Correspond
9245 (P_Type : Entity_Id;
9246 N_Type : Entity_Id) return Boolean
9248 Prev_Type : Entity_Id := Base_Type (P_Type);
9249 New_Type : Entity_Id := Base_Type (N_Type);
9252 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9253 Prev_Type := Designated_Type (Prev_Type);
9256 if Ekind (New_Type) = E_Anonymous_Access_Type then
9257 New_Type := Designated_Type (New_Type);
9260 if Prev_Type = New_Type then
9263 elsif not Is_Class_Wide_Type (New_Type) then
9264 while Etype (New_Type) /= New_Type loop
9265 New_Type := Etype (New_Type);
9266 if New_Type = Prev_Type then
9272 end Types_Correspond;
9274 -- Start of processing for Is_Non_Overriding_Operation
9277 -- In the case where both operations are implicit derived subprograms
9278 -- then neither overrides the other. This can only occur in certain
9279 -- obscure cases (e.g., derivation from homographs created in a generic
9282 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9285 elsif Ekind (Current_Scope) = E_Package
9286 and then Is_Generic_Instance (Current_Scope)
9287 and then In_Private_Part (Current_Scope)
9288 and then Comes_From_Source (New_E)
9290 -- We examine the formals and result type of the inherited operation,
9291 -- to determine whether their type is derived from (the instance of)
9292 -- a generic type. The first such formal or result type is the one
9295 Formal := First_Formal (Prev_E);
9296 while Present (Formal) loop
9297 F_Typ := Base_Type (Etype (Formal));
9299 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9300 F_Typ := Designated_Type (F_Typ);
9303 G_Typ := Get_Generic_Parent_Type (F_Typ);
9304 exit when Present (G_Typ);
9306 Next_Formal (Formal);
9309 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9310 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9317 -- If the generic type is a private type, then the original operation
9318 -- was not overriding in the generic, because there was no primitive
9319 -- operation to override.
9321 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9322 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9323 N_Formal_Private_Type_Definition
9327 -- The generic parent type is the ancestor of a formal derived
9328 -- type declaration. We need to check whether it has a primitive
9329 -- operation that should be overridden by New_E in the generic.
9333 P_Formal : Entity_Id;
9334 N_Formal : Entity_Id;
9338 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9341 while Present (Prim_Elt) loop
9342 P_Prim := Node (Prim_Elt);
9344 if Chars (P_Prim) = Chars (New_E)
9345 and then Ekind (P_Prim) = Ekind (New_E)
9347 P_Formal := First_Formal (P_Prim);
9348 N_Formal := First_Formal (New_E);
9349 while Present (P_Formal) and then Present (N_Formal) loop
9350 P_Typ := Etype (P_Formal);
9351 N_Typ := Etype (N_Formal);
9353 if not Types_Correspond (P_Typ, N_Typ) then
9357 Next_Entity (P_Formal);
9358 Next_Entity (N_Formal);
9361 -- Found a matching primitive operation belonging to the
9362 -- formal ancestor type, so the new subprogram is
9366 and then No (N_Formal)
9367 and then (Ekind (New_E) /= E_Function
9370 (Etype (P_Prim), Etype (New_E)))
9376 Next_Elmt (Prim_Elt);
9379 -- If no match found, then the new subprogram does not override
9380 -- in the generic (nor in the instance).
9382 -- If the type in question is not abstract, and the subprogram
9383 -- is, this will be an error if the new operation is in the
9384 -- private part of the instance. Emit a warning now, which will
9385 -- make the subsequent error message easier to understand.
9387 if not Is_Abstract_Type (F_Typ)
9388 and then Is_Abstract_Subprogram (Prev_E)
9389 and then In_Private_Part (Current_Scope)
9391 Error_Msg_Node_2 := F_Typ;
9393 ("private operation& in generic unit does not override " &
9394 "any primitive operation of& (RM 12.3 (18))??",
9404 end Is_Non_Overriding_Operation;
9406 -------------------------------------
9407 -- List_Inherited_Pre_Post_Aspects --
9408 -------------------------------------
9410 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9412 if Opt.List_Inherited_Aspects
9413 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9416 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9420 for J in Inherited'Range loop
9421 P := Pre_Post_Conditions (Contract (Inherited (J)));
9422 while Present (P) loop
9423 Error_Msg_Sloc := Sloc (P);
9425 if Class_Present (P) and then not Split_PPC (P) then
9426 if Pragma_Name (P) = Name_Precondition then
9428 ("info: & inherits `Pre''Class` aspect from #?L?",
9432 ("info: & inherits `Post''Class` aspect from #?L?",
9437 P := Next_Pragma (P);
9442 end List_Inherited_Pre_Post_Aspects;
9444 ------------------------------
9445 -- Make_Inequality_Operator --
9446 ------------------------------
9448 -- S is the defining identifier of an equality operator. We build a
9449 -- subprogram declaration with the right signature. This operation is
9450 -- intrinsic, because it is always expanded as the negation of the
9451 -- call to the equality function.
9453 procedure Make_Inequality_Operator (S : Entity_Id) is
9454 Loc : constant Source_Ptr := Sloc (S);
9457 Op_Name : Entity_Id;
9459 FF : constant Entity_Id := First_Formal (S);
9460 NF : constant Entity_Id := Next_Formal (FF);
9463 -- Check that equality was properly defined, ignore call if not
9470 A : constant Entity_Id :=
9471 Make_Defining_Identifier (Sloc (FF),
9472 Chars => Chars (FF));
9474 B : constant Entity_Id :=
9475 Make_Defining_Identifier (Sloc (NF),
9476 Chars => Chars (NF));
9479 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9481 Formals := New_List (
9482 Make_Parameter_Specification (Loc,
9483 Defining_Identifier => A,
9485 New_Reference_To (Etype (First_Formal (S)),
9486 Sloc (Etype (First_Formal (S))))),
9488 Make_Parameter_Specification (Loc,
9489 Defining_Identifier => B,
9491 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9492 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9495 Make_Subprogram_Declaration (Loc,
9497 Make_Function_Specification (Loc,
9498 Defining_Unit_Name => Op_Name,
9499 Parameter_Specifications => Formals,
9500 Result_Definition =>
9501 New_Reference_To (Standard_Boolean, Loc)));
9503 -- Insert inequality right after equality if it is explicit or after
9504 -- the derived type when implicit. These entities are created only
9505 -- for visibility purposes, and eventually replaced in the course
9506 -- of expansion, so they do not need to be attached to the tree and
9507 -- seen by the back-end. Keeping them internal also avoids spurious
9508 -- freezing problems. The declaration is inserted in the tree for
9509 -- analysis, and removed afterwards. If the equality operator comes
9510 -- from an explicit declaration, attach the inequality immediately
9511 -- after. Else the equality is inherited from a derived type
9512 -- declaration, so insert inequality after that declaration.
9514 if No (Alias (S)) then
9515 Insert_After (Unit_Declaration_Node (S), Decl);
9516 elsif Is_List_Member (Parent (S)) then
9517 Insert_After (Parent (S), Decl);
9519 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9522 Mark_Rewrite_Insertion (Decl);
9523 Set_Is_Intrinsic_Subprogram (Op_Name);
9526 Set_Has_Completion (Op_Name);
9527 Set_Corresponding_Equality (Op_Name, S);
9528 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9530 end Make_Inequality_Operator;
9532 ----------------------
9533 -- May_Need_Actuals --
9534 ----------------------
9536 procedure May_Need_Actuals (Fun : Entity_Id) is
9541 F := First_Formal (Fun);
9543 while Present (F) loop
9544 if No (Default_Value (F)) then
9552 Set_Needs_No_Actuals (Fun, B);
9553 end May_Need_Actuals;
9555 ---------------------
9556 -- Mode_Conformant --
9557 ---------------------
9559 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9562 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9564 end Mode_Conformant;
9566 ---------------------------
9567 -- New_Overloaded_Entity --
9568 ---------------------------
9570 procedure New_Overloaded_Entity
9572 Derived_Type : Entity_Id := Empty)
9574 Overridden_Subp : Entity_Id := Empty;
9575 -- Set if the current scope has an operation that is type-conformant
9576 -- with S, and becomes hidden by S.
9578 Is_Primitive_Subp : Boolean;
9579 -- Set to True if the new subprogram is primitive
9582 -- Entity that S overrides
9584 Prev_Vis : Entity_Id := Empty;
9585 -- Predecessor of E in Homonym chain
9587 procedure Check_For_Primitive_Subprogram
9588 (Is_Primitive : out Boolean;
9589 Is_Overriding : Boolean := False);
9590 -- If the subprogram being analyzed is a primitive operation of the type
9591 -- of a formal or result, set the Has_Primitive_Operations flag on the
9592 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9593 -- corresponding flag on the entity itself for later use.
9595 procedure Check_Synchronized_Overriding
9596 (Def_Id : Entity_Id;
9597 Overridden_Subp : out Entity_Id);
9598 -- First determine if Def_Id is an entry or a subprogram either defined
9599 -- in the scope of a task or protected type, or is a primitive of such
9600 -- a type. Check whether Def_Id overrides a subprogram of an interface
9601 -- implemented by the synchronized type, return the overridden entity
9604 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9605 -- Check that E is declared in the private part of the current package,
9606 -- or in the package body, where it may hide a previous declaration.
9607 -- We can't use In_Private_Part by itself because this flag is also
9608 -- set when freezing entities, so we must examine the place of the
9609 -- declaration in the tree, and recognize wrapper packages as well.
9611 function Is_Overriding_Alias
9613 New_E : Entity_Id) return Boolean;
9614 -- Check whether new subprogram and old subprogram are both inherited
9615 -- from subprograms that have distinct dispatch table entries. This can
9616 -- occur with derivations from instances with accidental homonyms. The
9617 -- function is conservative given that the converse is only true within
9618 -- instances that contain accidental overloadings.
9620 ------------------------------------
9621 -- Check_For_Primitive_Subprogram --
9622 ------------------------------------
9624 procedure Check_For_Primitive_Subprogram
9625 (Is_Primitive : out Boolean;
9626 Is_Overriding : Boolean := False)
9632 function Visible_Part_Type (T : Entity_Id) return Boolean;
9633 -- Returns true if T is declared in the visible part of the current
9634 -- package scope; otherwise returns false. Assumes that T is declared
9637 procedure Check_Private_Overriding (T : Entity_Id);
9638 -- Checks that if a primitive abstract subprogram of a visible
9639 -- abstract type is declared in a private part, then it must override
9640 -- an abstract subprogram declared in the visible part. Also checks
9641 -- that if a primitive function with a controlling result is declared
9642 -- in a private part, then it must override a function declared in
9643 -- the visible part.
9645 ------------------------------
9646 -- Check_Private_Overriding --
9647 ------------------------------
9649 procedure Check_Private_Overriding (T : Entity_Id) is
9651 if Is_Package_Or_Generic_Package (Current_Scope)
9652 and then In_Private_Part (Current_Scope)
9653 and then Visible_Part_Type (T)
9654 and then not In_Instance
9656 if Is_Abstract_Type (T)
9657 and then Is_Abstract_Subprogram (S)
9658 and then (not Is_Overriding
9659 or else not Is_Abstract_Subprogram (E))
9662 ("abstract subprograms must be visible "
9663 & "(RM 3.9.3(10))!", S);
9665 elsif Ekind (S) = E_Function and then not Is_Overriding then
9666 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9668 ("private function with tagged result must"
9669 & " override visible-part function", S);
9671 ("\move subprogram to the visible part"
9672 & " (RM 3.9.3(10))", S);
9674 -- AI05-0073: extend this test to the case of a function
9675 -- with a controlling access result.
9677 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9678 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9680 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9681 and then Ada_Version >= Ada_2012
9684 ("private function with controlling access result "
9685 & "must override visible-part function", S);
9687 ("\move subprogram to the visible part"
9688 & " (RM 3.9.3(10))", S);
9692 end Check_Private_Overriding;
9694 -----------------------
9695 -- Visible_Part_Type --
9696 -----------------------
9698 function Visible_Part_Type (T : Entity_Id) return Boolean is
9699 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9703 -- If the entity is a private type, then it must be declared in a
9706 if Ekind (T) in Private_Kind then
9710 -- Otherwise, we traverse the visible part looking for its
9711 -- corresponding declaration. We cannot use the declaration
9712 -- node directly because in the private part the entity of a
9713 -- private type is the one in the full view, which does not
9714 -- indicate that it is the completion of something visible.
9716 N := First (Visible_Declarations (Specification (P)));
9717 while Present (N) loop
9718 if Nkind (N) = N_Full_Type_Declaration
9719 and then Present (Defining_Identifier (N))
9720 and then T = Defining_Identifier (N)
9724 elsif Nkind_In (N, N_Private_Type_Declaration,
9725 N_Private_Extension_Declaration)
9726 and then Present (Defining_Identifier (N))
9727 and then T = Full_View (Defining_Identifier (N))
9736 end Visible_Part_Type;
9738 -- Start of processing for Check_For_Primitive_Subprogram
9741 Is_Primitive := False;
9743 if not Comes_From_Source (S) then
9746 -- If subprogram is at library level, it is not primitive operation
9748 elsif Current_Scope = Standard_Standard then
9751 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9752 and then not In_Package_Body (Current_Scope))
9753 or else Is_Overriding
9755 -- For function, check return type
9757 if Ekind (S) = E_Function then
9758 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9759 F_Typ := Designated_Type (Etype (S));
9764 B_Typ := Base_Type (F_Typ);
9766 if Scope (B_Typ) = Current_Scope
9767 and then not Is_Class_Wide_Type (B_Typ)
9768 and then not Is_Generic_Type (B_Typ)
9770 Is_Primitive := True;
9771 Set_Has_Primitive_Operations (B_Typ);
9772 Set_Is_Primitive (S);
9773 Check_Private_Overriding (B_Typ);
9777 -- For all subprograms, check formals
9779 Formal := First_Formal (S);
9780 while Present (Formal) loop
9781 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9782 F_Typ := Designated_Type (Etype (Formal));
9784 F_Typ := Etype (Formal);
9787 B_Typ := Base_Type (F_Typ);
9789 if Ekind (B_Typ) = E_Access_Subtype then
9790 B_Typ := Base_Type (B_Typ);
9793 if Scope (B_Typ) = Current_Scope
9794 and then not Is_Class_Wide_Type (B_Typ)
9795 and then not Is_Generic_Type (B_Typ)
9797 Is_Primitive := True;
9798 Set_Is_Primitive (S);
9799 Set_Has_Primitive_Operations (B_Typ);
9800 Check_Private_Overriding (B_Typ);
9803 Next_Formal (Formal);
9806 -- Special case: An equality function can be redefined for a type
9807 -- occurring in a declarative part, and won't otherwise be treated as
9808 -- a primitive because it doesn't occur in a package spec and doesn't
9809 -- override an inherited subprogram. It's important that we mark it
9810 -- primitive so it can be returned by Collect_Primitive_Operations
9811 -- and be used in composing the equality operation of later types
9812 -- that have a component of the type.
9814 elsif Chars (S) = Name_Op_Eq
9815 and then Etype (S) = Standard_Boolean
9817 B_Typ := Base_Type (Etype (First_Formal (S)));
9819 if Scope (B_Typ) = Current_Scope
9821 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9822 and then not Is_Limited_Type (B_Typ)
9824 Is_Primitive := True;
9825 Set_Is_Primitive (S);
9826 Set_Has_Primitive_Operations (B_Typ);
9827 Check_Private_Overriding (B_Typ);
9830 end Check_For_Primitive_Subprogram;
9832 -----------------------------------
9833 -- Check_Synchronized_Overriding --
9834 -----------------------------------
9836 procedure Check_Synchronized_Overriding
9837 (Def_Id : Entity_Id;
9838 Overridden_Subp : out Entity_Id)
9840 Ifaces_List : Elist_Id;
9844 function Matches_Prefixed_View_Profile
9845 (Prim_Params : List_Id;
9846 Iface_Params : List_Id) return Boolean;
9847 -- Determine whether a subprogram's parameter profile Prim_Params
9848 -- matches that of a potentially overridden interface subprogram
9849 -- Iface_Params. Also determine if the type of first parameter of
9850 -- Iface_Params is an implemented interface.
9852 -----------------------------------
9853 -- Matches_Prefixed_View_Profile --
9854 -----------------------------------
9856 function Matches_Prefixed_View_Profile
9857 (Prim_Params : List_Id;
9858 Iface_Params : List_Id) return Boolean
9860 Iface_Id : Entity_Id;
9861 Iface_Param : Node_Id;
9862 Iface_Typ : Entity_Id;
9863 Prim_Id : Entity_Id;
9864 Prim_Param : Node_Id;
9865 Prim_Typ : Entity_Id;
9867 function Is_Implemented
9868 (Ifaces_List : Elist_Id;
9869 Iface : Entity_Id) return Boolean;
9870 -- Determine if Iface is implemented by the current task or
9873 --------------------
9874 -- Is_Implemented --
9875 --------------------
9877 function Is_Implemented
9878 (Ifaces_List : Elist_Id;
9879 Iface : Entity_Id) return Boolean
9881 Iface_Elmt : Elmt_Id;
9884 Iface_Elmt := First_Elmt (Ifaces_List);
9885 while Present (Iface_Elmt) loop
9886 if Node (Iface_Elmt) = Iface then
9890 Next_Elmt (Iface_Elmt);
9896 -- Start of processing for Matches_Prefixed_View_Profile
9899 Iface_Param := First (Iface_Params);
9900 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9902 if Is_Access_Type (Iface_Typ) then
9903 Iface_Typ := Designated_Type (Iface_Typ);
9906 Prim_Param := First (Prim_Params);
9908 -- The first parameter of the potentially overridden subprogram
9909 -- must be an interface implemented by Prim.
9911 if not Is_Interface (Iface_Typ)
9912 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9917 -- The checks on the object parameters are done, move onto the
9918 -- rest of the parameters.
9920 if not In_Scope then
9921 Prim_Param := Next (Prim_Param);
9924 Iface_Param := Next (Iface_Param);
9925 while Present (Iface_Param) and then Present (Prim_Param) loop
9926 Iface_Id := Defining_Identifier (Iface_Param);
9927 Iface_Typ := Find_Parameter_Type (Iface_Param);
9929 Prim_Id := Defining_Identifier (Prim_Param);
9930 Prim_Typ := Find_Parameter_Type (Prim_Param);
9932 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9933 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9934 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9936 Iface_Typ := Designated_Type (Iface_Typ);
9937 Prim_Typ := Designated_Type (Prim_Typ);
9940 -- Case of multiple interface types inside a parameter profile
9942 -- (Obj_Param : in out Iface; ...; Param : Iface)
9944 -- If the interface type is implemented, then the matching type
9945 -- in the primitive should be the implementing record type.
9947 if Ekind (Iface_Typ) = E_Record_Type
9948 and then Is_Interface (Iface_Typ)
9949 and then Is_Implemented (Ifaces_List, Iface_Typ)
9951 if Prim_Typ /= Typ then
9955 -- The two parameters must be both mode and subtype conformant
9957 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9959 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9968 -- One of the two lists contains more parameters than the other
9970 if Present (Iface_Param) or else Present (Prim_Param) then
9975 end Matches_Prefixed_View_Profile;
9977 -- Start of processing for Check_Synchronized_Overriding
9980 Overridden_Subp := Empty;
9982 -- Def_Id must be an entry or a subprogram. We should skip predefined
9983 -- primitives internally generated by the frontend; however at this
9984 -- stage predefined primitives are still not fully decorated. As a
9985 -- minor optimization we skip here internally generated subprograms.
9987 if (Ekind (Def_Id) /= E_Entry
9988 and then Ekind (Def_Id) /= E_Function
9989 and then Ekind (Def_Id) /= E_Procedure)
9990 or else not Comes_From_Source (Def_Id)
9995 -- Search for the concurrent declaration since it contains the list
9996 -- of all implemented interfaces. In this case, the subprogram is
9997 -- declared within the scope of a protected or a task type.
9999 if Present (Scope (Def_Id))
10000 and then Is_Concurrent_Type (Scope (Def_Id))
10001 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10003 Typ := Scope (Def_Id);
10006 -- The enclosing scope is not a synchronized type and the subprogram
10009 elsif No (First_Formal (Def_Id)) then
10012 -- The subprogram has formals and hence it may be a primitive of a
10013 -- concurrent type.
10016 Typ := Etype (First_Formal (Def_Id));
10018 if Is_Access_Type (Typ) then
10019 Typ := Directly_Designated_Type (Typ);
10022 if Is_Concurrent_Type (Typ)
10023 and then not Is_Generic_Actual_Type (Typ)
10027 -- This case occurs when the concurrent type is declared within
10028 -- a generic unit. As a result the corresponding record has been
10029 -- built and used as the type of the first formal, we just have
10030 -- to retrieve the corresponding concurrent type.
10032 elsif Is_Concurrent_Record_Type (Typ)
10033 and then not Is_Class_Wide_Type (Typ)
10034 and then Present (Corresponding_Concurrent_Type (Typ))
10036 Typ := Corresponding_Concurrent_Type (Typ);
10044 -- There is no overriding to check if is an inherited operation in a
10045 -- type derivation on for a generic actual.
10047 Collect_Interfaces (Typ, Ifaces_List);
10049 if Is_Empty_Elmt_List (Ifaces_List) then
10053 -- Determine whether entry or subprogram Def_Id overrides a primitive
10054 -- operation that belongs to one of the interfaces in Ifaces_List.
10057 Candidate : Entity_Id := Empty;
10058 Hom : Entity_Id := Empty;
10059 Iface_Typ : Entity_Id;
10060 Subp : Entity_Id := Empty;
10063 -- Traverse the homonym chain, looking for a potentially
10064 -- overridden subprogram that belongs to an implemented
10067 Hom := Current_Entity_In_Scope (Def_Id);
10068 while Present (Hom) loop
10072 or else not Is_Overloadable (Subp)
10073 or else not Is_Primitive (Subp)
10074 or else not Is_Dispatching_Operation (Subp)
10075 or else not Present (Find_Dispatching_Type (Subp))
10076 or else not Is_Interface (Find_Dispatching_Type (Subp))
10080 -- Entries and procedures can override abstract or null
10081 -- interface procedures.
10083 elsif (Ekind (Def_Id) = E_Procedure
10084 or else Ekind (Def_Id) = E_Entry)
10085 and then Ekind (Subp) = E_Procedure
10086 and then Matches_Prefixed_View_Profile
10087 (Parameter_Specifications (Parent (Def_Id)),
10088 Parameter_Specifications (Parent (Subp)))
10092 -- For an overridden subprogram Subp, check whether the mode
10093 -- of its first parameter is correct depending on the kind
10094 -- of synchronized type.
10097 Formal : constant Node_Id := First_Formal (Candidate);
10100 -- In order for an entry or a protected procedure to
10101 -- override, the first parameter of the overridden
10102 -- routine must be of mode "out", "in out" or
10103 -- access-to-variable.
10105 if Ekind_In (Candidate, E_Entry, E_Procedure)
10106 and then Is_Protected_Type (Typ)
10107 and then Ekind (Formal) /= E_In_Out_Parameter
10108 and then Ekind (Formal) /= E_Out_Parameter
10109 and then Nkind (Parameter_Type (Parent (Formal))) /=
10110 N_Access_Definition
10114 -- All other cases are OK since a task entry or routine
10115 -- does not have a restriction on the mode of the first
10116 -- parameter of the overridden interface routine.
10119 Overridden_Subp := Candidate;
10124 -- Functions can override abstract interface functions
10126 elsif Ekind (Def_Id) = E_Function
10127 and then Ekind (Subp) = E_Function
10128 and then Matches_Prefixed_View_Profile
10129 (Parameter_Specifications (Parent (Def_Id)),
10130 Parameter_Specifications (Parent (Subp)))
10131 and then Etype (Result_Definition (Parent (Def_Id))) =
10132 Etype (Result_Definition (Parent (Subp)))
10134 Overridden_Subp := Subp;
10138 Hom := Homonym (Hom);
10141 -- After examining all candidates for overriding, we are left with
10142 -- the best match which is a mode incompatible interface routine.
10143 -- Do not emit an error if the Expander is active since this error
10144 -- will be detected later on after all concurrent types are
10145 -- expanded and all wrappers are built. This check is meant for
10146 -- spec-only compilations.
10148 if Present (Candidate) and then not Expander_Active then
10150 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10152 -- Def_Id is primitive of a protected type, declared inside the
10153 -- type, and the candidate is primitive of a limited or
10154 -- synchronized interface.
10157 and then Is_Protected_Type (Typ)
10159 (Is_Limited_Interface (Iface_Typ)
10160 or else Is_Protected_Interface (Iface_Typ)
10161 or else Is_Synchronized_Interface (Iface_Typ)
10162 or else Is_Task_Interface (Iface_Typ))
10164 Error_Msg_PT (Parent (Typ), Candidate);
10168 Overridden_Subp := Candidate;
10171 end Check_Synchronized_Overriding;
10173 ----------------------------
10174 -- Is_Private_Declaration --
10175 ----------------------------
10177 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10178 Priv_Decls : List_Id;
10179 Decl : constant Node_Id := Unit_Declaration_Node (E);
10182 if Is_Package_Or_Generic_Package (Current_Scope)
10183 and then In_Private_Part (Current_Scope)
10186 Private_Declarations (Package_Specification (Current_Scope));
10188 return In_Package_Body (Current_Scope)
10190 (Is_List_Member (Decl)
10191 and then List_Containing (Decl) = Priv_Decls)
10192 or else (Nkind (Parent (Decl)) = N_Package_Specification
10194 Is_Compilation_Unit
10195 (Defining_Entity (Parent (Decl)))
10196 and then List_Containing (Parent (Parent (Decl))) =
10201 end Is_Private_Declaration;
10203 --------------------------
10204 -- Is_Overriding_Alias --
10205 --------------------------
10207 function Is_Overriding_Alias
10208 (Old_E : Entity_Id;
10209 New_E : Entity_Id) return Boolean
10211 AO : constant Entity_Id := Alias (Old_E);
10212 AN : constant Entity_Id := Alias (New_E);
10215 return Scope (AO) /= Scope (AN)
10216 or else No (DTC_Entity (AO))
10217 or else No (DTC_Entity (AN))
10218 or else DT_Position (AO) = DT_Position (AN);
10219 end Is_Overriding_Alias;
10221 -- Start of processing for New_Overloaded_Entity
10224 -- We need to look for an entity that S may override. This must be a
10225 -- homonym in the current scope, so we look for the first homonym of
10226 -- S in the current scope as the starting point for the search.
10228 E := Current_Entity_In_Scope (S);
10230 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10231 -- They are directly added to the list of primitive operations of
10232 -- Derived_Type, unless this is a rederivation in the private part
10233 -- of an operation that was already derived in the visible part of
10234 -- the current package.
10236 if Ada_Version >= Ada_2005
10237 and then Present (Derived_Type)
10238 and then Present (Alias (S))
10239 and then Is_Dispatching_Operation (Alias (S))
10240 and then Present (Find_Dispatching_Type (Alias (S)))
10241 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10243 -- For private types, when the full-view is processed we propagate to
10244 -- the full view the non-overridden entities whose attribute "alias"
10245 -- references an interface primitive. These entities were added by
10246 -- Derive_Subprograms to ensure that interface primitives are
10249 -- Inside_Freeze_Actions is non zero when S corresponds with an
10250 -- internal entity that links an interface primitive with its
10251 -- covering primitive through attribute Interface_Alias (see
10252 -- Add_Internal_Interface_Entities).
10254 if Inside_Freezing_Actions = 0
10255 and then Is_Package_Or_Generic_Package (Current_Scope)
10256 and then In_Private_Part (Current_Scope)
10257 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10258 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10259 and then Full_View (Defining_Identifier (Parent (E)))
10260 = Defining_Identifier (Parent (S))
10261 and then Alias (E) = Alias (S)
10263 Check_Operation_From_Private_View (S, E);
10264 Set_Is_Dispatching_Operation (S);
10269 Enter_Overloaded_Entity (S);
10270 Check_Dispatching_Operation (S, Empty);
10271 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10277 -- If there is no homonym then this is definitely not overriding
10280 Enter_Overloaded_Entity (S);
10281 Check_Dispatching_Operation (S, Empty);
10282 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10284 -- If subprogram has an explicit declaration, check whether it has an
10285 -- overriding indicator.
10287 if Comes_From_Source (S) then
10288 Check_Synchronized_Overriding (S, Overridden_Subp);
10290 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10291 -- it may have overridden some hidden inherited primitive. Update
10292 -- Overridden_Subp to avoid spurious errors when checking the
10293 -- overriding indicator.
10295 if Ada_Version >= Ada_2012
10296 and then No (Overridden_Subp)
10297 and then Is_Dispatching_Operation (S)
10298 and then Present (Overridden_Operation (S))
10300 Overridden_Subp := Overridden_Operation (S);
10303 Check_Overriding_Indicator
10304 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10307 -- If there is a homonym that is not overloadable, then we have an
10308 -- error, except for the special cases checked explicitly below.
10310 elsif not Is_Overloadable (E) then
10312 -- Check for spurious conflict produced by a subprogram that has the
10313 -- same name as that of the enclosing generic package. The conflict
10314 -- occurs within an instance, between the subprogram and the renaming
10315 -- declaration for the package. After the subprogram, the package
10316 -- renaming declaration becomes hidden.
10318 if Ekind (E) = E_Package
10319 and then Present (Renamed_Object (E))
10320 and then Renamed_Object (E) = Current_Scope
10321 and then Nkind (Parent (Renamed_Object (E))) =
10322 N_Package_Specification
10323 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10326 Set_Is_Immediately_Visible (E, False);
10327 Enter_Overloaded_Entity (S);
10328 Set_Homonym (S, Homonym (E));
10329 Check_Dispatching_Operation (S, Empty);
10330 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10332 -- If the subprogram is implicit it is hidden by the previous
10333 -- declaration. However if it is dispatching, it must appear in the
10334 -- dispatch table anyway, because it can be dispatched to even if it
10335 -- cannot be called directly.
10337 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10338 Set_Scope (S, Current_Scope);
10340 if Is_Dispatching_Operation (Alias (S)) then
10341 Check_Dispatching_Operation (S, Empty);
10347 Error_Msg_Sloc := Sloc (E);
10349 -- Generate message, with useful additional warning if in generic
10351 if Is_Generic_Unit (E) then
10352 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10353 Error_Msg_N ("\& conflicts with declaration#", S);
10355 Error_Msg_N ("& conflicts with declaration#", S);
10361 -- E exists and is overloadable
10364 Check_Synchronized_Overriding (S, Overridden_Subp);
10366 -- Loop through E and its homonyms to determine if any of them is
10367 -- the candidate for overriding by S.
10369 while Present (E) loop
10371 -- Definitely not interesting if not in the current scope
10373 if Scope (E) /= Current_Scope then
10376 -- A function can overload the name of an abstract state. The
10377 -- state can be viewed as a function with a profile that cannot
10378 -- be matched by anything.
10380 elsif Ekind (S) = E_Function
10381 and then Ekind (E) = E_Abstract_State
10383 Enter_Overloaded_Entity (S);
10386 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10387 -- procedures locate the internally generated spec. We enforce
10388 -- mode conformance since a tagged type may inherit from
10389 -- interfaces several null primitives which differ only in
10390 -- the mode of the formals.
10392 elsif not Comes_From_Source (S)
10393 and then Is_Null_Procedure (S)
10394 and then not Mode_Conformant (E, S)
10398 -- Check if we have type conformance
10400 elsif Type_Conformant (E, S) then
10402 -- If the old and new entities have the same profile and one
10403 -- is not the body of the other, then this is an error, unless
10404 -- one of them is implicitly declared.
10406 -- There are some cases when both can be implicit, for example
10407 -- when both a literal and a function that overrides it are
10408 -- inherited in a derivation, or when an inherited operation
10409 -- of a tagged full type overrides the inherited operation of
10410 -- a private extension. Ada 83 had a special rule for the
10411 -- literal case. In Ada 95, the later implicit operation hides
10412 -- the former, and the literal is always the former. In the
10413 -- odd case where both are derived operations declared at the
10414 -- same point, both operations should be declared, and in that
10415 -- case we bypass the following test and proceed to the next
10416 -- part. This can only occur for certain obscure cases in
10417 -- instances, when an operation on a type derived from a formal
10418 -- private type does not override a homograph inherited from
10419 -- the actual. In subsequent derivations of such a type, the
10420 -- DT positions of these operations remain distinct, if they
10423 if Present (Alias (S))
10424 and then (No (Alias (E))
10425 or else Comes_From_Source (E)
10426 or else Is_Abstract_Subprogram (S)
10428 (Is_Dispatching_Operation (E)
10429 and then Is_Overriding_Alias (E, S)))
10430 and then Ekind (E) /= E_Enumeration_Literal
10432 -- When an derived operation is overloaded it may be due to
10433 -- the fact that the full view of a private extension
10434 -- re-inherits. It has to be dealt with.
10436 if Is_Package_Or_Generic_Package (Current_Scope)
10437 and then In_Private_Part (Current_Scope)
10439 Check_Operation_From_Private_View (S, E);
10442 -- In any case the implicit operation remains hidden by the
10443 -- existing declaration, which is overriding. Indicate that
10444 -- E overrides the operation from which S is inherited.
10446 if Present (Alias (S)) then
10447 Set_Overridden_Operation (E, Alias (S));
10449 Set_Overridden_Operation (E, S);
10452 if Comes_From_Source (E) then
10453 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10458 -- Within an instance, the renaming declarations for actual
10459 -- subprograms may become ambiguous, but they do not hide each
10462 elsif Ekind (E) /= E_Entry
10463 and then not Comes_From_Source (E)
10464 and then not Is_Generic_Instance (E)
10465 and then (Present (Alias (E))
10466 or else Is_Intrinsic_Subprogram (E))
10467 and then (not In_Instance
10468 or else No (Parent (E))
10469 or else Nkind (Unit_Declaration_Node (E)) /=
10470 N_Subprogram_Renaming_Declaration)
10472 -- A subprogram child unit is not allowed to override an
10473 -- inherited subprogram (10.1.1(20)).
10475 if Is_Child_Unit (S) then
10477 ("child unit overrides inherited subprogram in parent",
10482 if Is_Non_Overriding_Operation (E, S) then
10483 Enter_Overloaded_Entity (S);
10485 if No (Derived_Type)
10486 or else Is_Tagged_Type (Derived_Type)
10488 Check_Dispatching_Operation (S, Empty);
10494 -- E is a derived operation or an internal operator which
10495 -- is being overridden. Remove E from further visibility.
10496 -- Furthermore, if E is a dispatching operation, it must be
10497 -- replaced in the list of primitive operations of its type
10498 -- (see Override_Dispatching_Operation).
10500 Overridden_Subp := E;
10506 Prev := First_Entity (Current_Scope);
10507 while Present (Prev) and then Next_Entity (Prev) /= E loop
10508 Next_Entity (Prev);
10511 -- It is possible for E to be in the current scope and
10512 -- yet not in the entity chain. This can only occur in a
10513 -- generic context where E is an implicit concatenation
10514 -- in the formal part, because in a generic body the
10515 -- entity chain starts with the formals.
10518 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10520 -- E must be removed both from the entity_list of the
10521 -- current scope, and from the visibility chain
10523 if Debug_Flag_E then
10524 Write_Str ("Override implicit operation ");
10525 Write_Int (Int (E));
10529 -- If E is a predefined concatenation, it stands for four
10530 -- different operations. As a result, a single explicit
10531 -- declaration does not hide it. In a possible ambiguous
10532 -- situation, Disambiguate chooses the user-defined op,
10533 -- so it is correct to retain the previous internal one.
10535 if Chars (E) /= Name_Op_Concat
10536 or else Ekind (E) /= E_Operator
10538 -- For nondispatching derived operations that are
10539 -- overridden by a subprogram declared in the private
10540 -- part of a package, we retain the derived subprogram
10541 -- but mark it as not immediately visible. If the
10542 -- derived operation was declared in the visible part
10543 -- then this ensures that it will still be visible
10544 -- outside the package with the proper signature
10545 -- (calls from outside must also be directed to this
10546 -- version rather than the overriding one, unlike the
10547 -- dispatching case). Calls from inside the package
10548 -- will still resolve to the overriding subprogram
10549 -- since the derived one is marked as not visible
10550 -- within the package.
10552 -- If the private operation is dispatching, we achieve
10553 -- the overriding by keeping the implicit operation
10554 -- but setting its alias to be the overriding one. In
10555 -- this fashion the proper body is executed in all
10556 -- cases, but the original signature is used outside
10559 -- If the overriding is not in the private part, we
10560 -- remove the implicit operation altogether.
10562 if Is_Private_Declaration (S) then
10563 if not Is_Dispatching_Operation (E) then
10564 Set_Is_Immediately_Visible (E, False);
10566 -- Work done in Override_Dispatching_Operation,
10567 -- so nothing else needs to be done here.
10573 -- Find predecessor of E in Homonym chain
10575 if E = Current_Entity (E) then
10578 Prev_Vis := Current_Entity (E);
10579 while Homonym (Prev_Vis) /= E loop
10580 Prev_Vis := Homonym (Prev_Vis);
10584 if Prev_Vis /= Empty then
10586 -- Skip E in the visibility chain
10588 Set_Homonym (Prev_Vis, Homonym (E));
10591 Set_Name_Entity_Id (Chars (E), Homonym (E));
10594 Set_Next_Entity (Prev, Next_Entity (E));
10596 if No (Next_Entity (Prev)) then
10597 Set_Last_Entity (Current_Scope, Prev);
10602 Enter_Overloaded_Entity (S);
10604 -- For entities generated by Derive_Subprograms the
10605 -- overridden operation is the inherited primitive
10606 -- (which is available through the attribute alias).
10608 if not (Comes_From_Source (E))
10609 and then Is_Dispatching_Operation (E)
10610 and then Find_Dispatching_Type (E) =
10611 Find_Dispatching_Type (S)
10612 and then Present (Alias (E))
10613 and then Comes_From_Source (Alias (E))
10615 Set_Overridden_Operation (S, Alias (E));
10617 -- Normal case of setting entity as overridden
10619 -- Note: Static_Initialization and Overridden_Operation
10620 -- attributes use the same field in subprogram entities.
10621 -- Static_Initialization is only defined for internal
10622 -- initialization procedures, where Overridden_Operation
10623 -- is irrelevant. Therefore the setting of this attribute
10624 -- must check whether the target is an init_proc.
10626 elsif not Is_Init_Proc (S) then
10627 Set_Overridden_Operation (S, E);
10630 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10632 -- If S is a user-defined subprogram or a null procedure
10633 -- expanded to override an inherited null procedure, or a
10634 -- predefined dispatching primitive then indicate that E
10635 -- overrides the operation from which S is inherited.
10637 if Comes_From_Source (S)
10639 (Present (Parent (S))
10641 Nkind (Parent (S)) = N_Procedure_Specification
10643 Null_Present (Parent (S)))
10645 (Present (Alias (E))
10647 Is_Predefined_Dispatching_Operation (Alias (E)))
10649 if Present (Alias (E)) then
10650 Set_Overridden_Operation (S, Alias (E));
10654 if Is_Dispatching_Operation (E) then
10656 -- An overriding dispatching subprogram inherits the
10657 -- convention of the overridden subprogram (AI-117).
10659 Set_Convention (S, Convention (E));
10660 Check_Dispatching_Operation (S, E);
10663 Check_Dispatching_Operation (S, Empty);
10666 Check_For_Primitive_Subprogram
10667 (Is_Primitive_Subp, Is_Overriding => True);
10668 goto Check_Inequality;
10671 -- Apparent redeclarations in instances can occur when two
10672 -- formal types get the same actual type. The subprograms in
10673 -- in the instance are legal, even if not callable from the
10674 -- outside. Calls from within are disambiguated elsewhere.
10675 -- For dispatching operations in the visible part, the usual
10676 -- rules apply, and operations with the same profile are not
10677 -- legal (B830001).
10679 elsif (In_Instance_Visible_Part
10680 and then not Is_Dispatching_Operation (E))
10681 or else In_Instance_Not_Visible
10685 -- Here we have a real error (identical profile)
10688 Error_Msg_Sloc := Sloc (E);
10690 -- Avoid cascaded errors if the entity appears in
10691 -- subsequent calls.
10693 Set_Scope (S, Current_Scope);
10695 -- Generate error, with extra useful warning for the case
10696 -- of a generic instance with no completion.
10698 if Is_Generic_Instance (S)
10699 and then not Has_Completion (E)
10702 ("instantiation cannot provide body for&", S);
10703 Error_Msg_N ("\& conflicts with declaration#", S);
10705 Error_Msg_N ("& conflicts with declaration#", S);
10712 -- If one subprogram has an access parameter and the other
10713 -- a parameter of an access type, calls to either might be
10714 -- ambiguous. Verify that parameters match except for the
10715 -- access parameter.
10717 if May_Hide_Profile then
10723 F1 := First_Formal (S);
10724 F2 := First_Formal (E);
10725 while Present (F1) and then Present (F2) loop
10726 if Is_Access_Type (Etype (F1)) then
10727 if not Is_Access_Type (Etype (F2))
10728 or else not Conforming_Types
10729 (Designated_Type (Etype (F1)),
10730 Designated_Type (Etype (F2)),
10733 May_Hide_Profile := False;
10737 not Conforming_Types
10738 (Etype (F1), Etype (F2), Type_Conformant)
10740 May_Hide_Profile := False;
10747 if May_Hide_Profile
10751 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10760 -- On exit, we know that S is a new entity
10762 Enter_Overloaded_Entity (S);
10763 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10764 Check_Overriding_Indicator
10765 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10767 -- Overloading is not allowed in SPARK, except for operators
10769 if Nkind (S) /= N_Defining_Operator_Symbol then
10770 Error_Msg_Sloc := Sloc (Homonym (S));
10771 Check_SPARK_Restriction
10772 ("overloading not allowed with entity#", S);
10775 -- If S is a derived operation for an untagged type then by
10776 -- definition it's not a dispatching operation (even if the parent
10777 -- operation was dispatching), so Check_Dispatching_Operation is not
10778 -- called in that case.
10780 if No (Derived_Type)
10781 or else Is_Tagged_Type (Derived_Type)
10783 Check_Dispatching_Operation (S, Empty);
10787 -- If this is a user-defined equality operator that is not a derived
10788 -- subprogram, create the corresponding inequality. If the operation is
10789 -- dispatching, the expansion is done elsewhere, and we do not create
10790 -- an explicit inequality operation.
10792 <<Check_Inequality>>
10793 if Chars (S) = Name_Op_Eq
10794 and then Etype (S) = Standard_Boolean
10795 and then Present (Parent (S))
10796 and then not Is_Dispatching_Operation (S)
10798 Make_Inequality_Operator (S);
10800 if Ada_Version >= Ada_2012 then
10801 Check_Untagged_Equality (S);
10804 end New_Overloaded_Entity;
10806 ---------------------
10807 -- Process_Formals --
10808 ---------------------
10810 procedure Process_Formals
10812 Related_Nod : Node_Id)
10814 Param_Spec : Node_Id;
10815 Formal : Entity_Id;
10816 Formal_Type : Entity_Id;
10820 Num_Out_Params : Nat := 0;
10821 First_Out_Param : Entity_Id := Empty;
10822 -- Used for setting Is_Only_Out_Parameter
10824 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10825 -- Determine whether an access type designates a type coming from a
10828 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10829 -- Check whether the default has a class-wide type. After analysis the
10830 -- default has the type of the formal, so we must also check explicitly
10831 -- for an access attribute.
10833 ----------------------------------
10834 -- Designates_From_Limited_With --
10835 ----------------------------------
10837 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10838 Desig : Entity_Id := Typ;
10841 if Is_Access_Type (Desig) then
10842 Desig := Directly_Designated_Type (Desig);
10845 if Is_Class_Wide_Type (Desig) then
10846 Desig := Root_Type (Desig);
10850 Ekind (Desig) = E_Incomplete_Type
10851 and then From_Limited_With (Desig);
10852 end Designates_From_Limited_With;
10854 ---------------------------
10855 -- Is_Class_Wide_Default --
10856 ---------------------------
10858 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10860 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10861 or else (Nkind (D) = N_Attribute_Reference
10862 and then Attribute_Name (D) = Name_Access
10863 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10864 end Is_Class_Wide_Default;
10866 -- Start of processing for Process_Formals
10869 -- In order to prevent premature use of the formals in the same formal
10870 -- part, the Ekind is left undefined until all default expressions are
10871 -- analyzed. The Ekind is established in a separate loop at the end.
10873 Param_Spec := First (T);
10874 while Present (Param_Spec) loop
10875 Formal := Defining_Identifier (Param_Spec);
10876 Set_Never_Set_In_Source (Formal, True);
10877 Enter_Name (Formal);
10879 -- Case of ordinary parameters
10881 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10882 Find_Type (Parameter_Type (Param_Spec));
10883 Ptype := Parameter_Type (Param_Spec);
10885 if Ptype = Error then
10889 Formal_Type := Entity (Ptype);
10891 if Is_Incomplete_Type (Formal_Type)
10893 (Is_Class_Wide_Type (Formal_Type)
10894 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10896 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10897 -- primitive operations, as long as their completion is
10898 -- in the same declarative part. If in the private part
10899 -- this means that the type cannot be a Taft-amendment type.
10900 -- Check is done on package exit. For access to subprograms,
10901 -- the use is legal for Taft-amendment types.
10903 -- Ada 2012: tagged incomplete types are allowed as generic
10904 -- formal types. They do not introduce dependencies and the
10905 -- corresponding generic subprogram does not have a delayed
10906 -- freeze, because it does not need a freeze node.
10908 if Is_Tagged_Type (Formal_Type) then
10909 if Ekind (Scope (Current_Scope)) = E_Package
10910 and then not From_Limited_With (Formal_Type)
10911 and then not Is_Generic_Type (Formal_Type)
10912 and then not Is_Class_Wide_Type (Formal_Type)
10915 (Parent (T), N_Access_Function_Definition,
10916 N_Access_Procedure_Definition)
10920 Private_Dependents (Base_Type (Formal_Type)));
10922 -- Freezing is delayed to ensure that Register_Prim
10923 -- will get called for this operation, which is needed
10924 -- in cases where static dispatch tables aren't built.
10925 -- (Note that the same is done for controlling access
10926 -- parameter cases in function Access_Definition.)
10928 Set_Has_Delayed_Freeze (Current_Scope);
10932 -- Special handling of Value_Type for CIL case
10934 elsif Is_Value_Type (Formal_Type) then
10937 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10938 N_Access_Procedure_Definition)
10940 -- AI05-0151: Tagged incomplete types are allowed in all
10941 -- formal parts. Untagged incomplete types are not allowed
10944 if Ada_Version >= Ada_2012 then
10945 if Is_Tagged_Type (Formal_Type) then
10948 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10953 ("invalid use of untagged incomplete type&",
10954 Ptype, Formal_Type);
10959 ("invalid use of incomplete type&",
10960 Param_Spec, Formal_Type);
10962 -- Further checks on the legality of incomplete types
10963 -- in formal parts are delayed until the freeze point
10964 -- of the enclosing subprogram or access to subprogram.
10968 elsif Ekind (Formal_Type) = E_Void then
10970 ("premature use of&",
10971 Parameter_Type (Param_Spec), Formal_Type);
10974 -- Ada 2012 (AI-142): Handle aliased parameters
10976 if Ada_Version >= Ada_2012
10977 and then Aliased_Present (Param_Spec)
10979 Set_Is_Aliased (Formal);
10982 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10983 -- declaration corresponding to the null-excluding type of the
10984 -- formal in the enclosing scope. Finally, replace the parameter
10985 -- type of the formal with the internal subtype.
10987 if Ada_Version >= Ada_2005
10988 and then Null_Exclusion_Present (Param_Spec)
10990 if not Is_Access_Type (Formal_Type) then
10992 ("`NOT NULL` allowed only for an access type", Param_Spec);
10995 if Can_Never_Be_Null (Formal_Type)
10996 and then Comes_From_Source (Related_Nod)
10999 ("`NOT NULL` not allowed (& already excludes null)",
11000 Param_Spec, Formal_Type);
11004 Create_Null_Excluding_Itype
11006 Related_Nod => Related_Nod,
11007 Scope_Id => Scope (Current_Scope));
11009 -- If the designated type of the itype is an itype that is
11010 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11011 -- on the access subtype, to prevent order-of-elaboration
11012 -- issues in the backend.
11015 -- type T is access procedure;
11016 -- procedure Op (O : not null T);
11018 if Is_Itype (Directly_Designated_Type (Formal_Type))
11020 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11022 Set_Has_Delayed_Freeze (Formal_Type);
11027 -- An access formal type
11031 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11033 -- No need to continue if we already notified errors
11035 if not Present (Formal_Type) then
11039 -- Ada 2005 (AI-254)
11042 AD : constant Node_Id :=
11043 Access_To_Subprogram_Definition
11044 (Parameter_Type (Param_Spec));
11046 if Present (AD) and then Protected_Present (AD) then
11048 Replace_Anonymous_Access_To_Protected_Subprogram
11054 Set_Etype (Formal, Formal_Type);
11056 -- Deal with default expression if present
11058 Default := Expression (Param_Spec);
11060 if Present (Default) then
11061 Check_SPARK_Restriction
11062 ("default expression is not allowed", Default);
11064 if Out_Present (Param_Spec) then
11066 ("default initialization only allowed for IN parameters",
11070 -- Do the special preanalysis of the expression (see section on
11071 -- "Handling of Default Expressions" in the spec of package Sem).
11073 Preanalyze_Spec_Expression (Default, Formal_Type);
11075 -- An access to constant cannot be the default for
11076 -- an access parameter that is an access to variable.
11078 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11079 and then not Is_Access_Constant (Formal_Type)
11080 and then Is_Access_Type (Etype (Default))
11081 and then Is_Access_Constant (Etype (Default))
11084 ("formal that is access to variable cannot be initialized " &
11085 "with an access-to-constant expression", Default);
11088 -- Check that the designated type of an access parameter's default
11089 -- is not a class-wide type unless the parameter's designated type
11090 -- is also class-wide.
11092 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11093 and then not Designates_From_Limited_With (Formal_Type)
11094 and then Is_Class_Wide_Default (Default)
11095 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11098 ("access to class-wide expression not allowed here", Default);
11101 -- Check incorrect use of dynamically tagged expressions
11103 if Is_Tagged_Type (Formal_Type) then
11104 Check_Dynamically_Tagged_Expression
11106 Typ => Formal_Type,
11107 Related_Nod => Default);
11111 -- Ada 2005 (AI-231): Static checks
11113 if Ada_Version >= Ada_2005
11114 and then Is_Access_Type (Etype (Formal))
11115 and then Can_Never_Be_Null (Etype (Formal))
11117 Null_Exclusion_Static_Checks (Param_Spec);
11120 -- A function cannot have a volatile formal parameter. The following
11121 -- check is relevant when SPARK_Mode is on as it is not a standard
11122 -- Ada legality rule.
11125 and then Is_Volatile_Object (Formal)
11126 and then Ekind_In (Scope (Formal), E_Function, E_Generic_Function)
11129 ("function cannot have a volatile formal parameter (SPARK RM "
11130 & "7.1.3(6))", Formal);
11137 -- If this is the formal part of a function specification, analyze the
11138 -- subtype mark in the context where the formals are visible but not
11139 -- yet usable, and may hide outer homographs.
11141 if Nkind (Related_Nod) = N_Function_Specification then
11142 Analyze_Return_Type (Related_Nod);
11145 -- Now set the kind (mode) of each formal
11147 Param_Spec := First (T);
11148 while Present (Param_Spec) loop
11149 Formal := Defining_Identifier (Param_Spec);
11150 Set_Formal_Mode (Formal);
11152 if Ekind (Formal) = E_In_Parameter then
11153 Set_Default_Value (Formal, Expression (Param_Spec));
11155 if Present (Expression (Param_Spec)) then
11156 Default := Expression (Param_Spec);
11158 if Is_Scalar_Type (Etype (Default)) then
11159 if Nkind (Parameter_Type (Param_Spec)) /=
11160 N_Access_Definition
11162 Formal_Type := Entity (Parameter_Type (Param_Spec));
11166 (Related_Nod, Parameter_Type (Param_Spec));
11169 Apply_Scalar_Range_Check (Default, Formal_Type);
11173 elsif Ekind (Formal) = E_Out_Parameter then
11174 Num_Out_Params := Num_Out_Params + 1;
11176 if Num_Out_Params = 1 then
11177 First_Out_Param := Formal;
11180 elsif Ekind (Formal) = E_In_Out_Parameter then
11181 Num_Out_Params := Num_Out_Params + 1;
11184 -- Skip remaining processing if formal type was in error
11186 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11187 goto Next_Parameter;
11190 -- Force call by reference if aliased
11192 if Is_Aliased (Formal) then
11193 Set_Mechanism (Formal, By_Reference);
11195 -- Warn if user asked this to be passed by copy
11197 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11199 ("cannot pass aliased parameter & by copy?", Formal);
11202 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11204 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11205 Set_Mechanism (Formal, By_Copy);
11207 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11208 Set_Mechanism (Formal, By_Reference);
11215 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11216 Set_Is_Only_Out_Parameter (First_Out_Param);
11218 end Process_Formals;
11220 ----------------------------
11221 -- Reference_Body_Formals --
11222 ----------------------------
11224 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11229 if Error_Posted (Spec) then
11233 -- Iterate over both lists. They may be of different lengths if the two
11234 -- specs are not conformant.
11236 Fs := First_Formal (Spec);
11237 Fb := First_Formal (Bod);
11238 while Present (Fs) and then Present (Fb) loop
11239 Generate_Reference (Fs, Fb, 'b');
11241 if Style_Check then
11242 Style.Check_Identifier (Fb, Fs);
11245 Set_Spec_Entity (Fb, Fs);
11246 Set_Referenced (Fs, False);
11250 end Reference_Body_Formals;
11252 -------------------------
11253 -- Set_Actual_Subtypes --
11254 -------------------------
11256 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11258 Formal : Entity_Id;
11260 First_Stmt : Node_Id := Empty;
11261 AS_Needed : Boolean;
11264 -- If this is an empty initialization procedure, no need to create
11265 -- actual subtypes (small optimization).
11267 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11271 Formal := First_Formal (Subp);
11272 while Present (Formal) loop
11273 T := Etype (Formal);
11275 -- We never need an actual subtype for a constrained formal
11277 if Is_Constrained (T) then
11278 AS_Needed := False;
11280 -- If we have unknown discriminants, then we do not need an actual
11281 -- subtype, or more accurately we cannot figure it out! Note that
11282 -- all class-wide types have unknown discriminants.
11284 elsif Has_Unknown_Discriminants (T) then
11285 AS_Needed := False;
11287 -- At this stage we have an unconstrained type that may need an
11288 -- actual subtype. For sure the actual subtype is needed if we have
11289 -- an unconstrained array type.
11291 elsif Is_Array_Type (T) then
11294 -- The only other case needing an actual subtype is an unconstrained
11295 -- record type which is an IN parameter (we cannot generate actual
11296 -- subtypes for the OUT or IN OUT case, since an assignment can
11297 -- change the discriminant values. However we exclude the case of
11298 -- initialization procedures, since discriminants are handled very
11299 -- specially in this context, see the section entitled "Handling of
11300 -- Discriminants" in Einfo.
11302 -- We also exclude the case of Discrim_SO_Functions (functions used
11303 -- in front end layout mode for size/offset values), since in such
11304 -- functions only discriminants are referenced, and not only are such
11305 -- subtypes not needed, but they cannot always be generated, because
11306 -- of order of elaboration issues.
11308 elsif Is_Record_Type (T)
11309 and then Ekind (Formal) = E_In_Parameter
11310 and then Chars (Formal) /= Name_uInit
11311 and then not Is_Unchecked_Union (T)
11312 and then not Is_Discrim_SO_Function (Subp)
11316 -- All other cases do not need an actual subtype
11319 AS_Needed := False;
11322 -- Generate actual subtypes for unconstrained arrays and
11323 -- unconstrained discriminated records.
11326 if Nkind (N) = N_Accept_Statement then
11328 -- If expansion is active, the formal is replaced by a local
11329 -- variable that renames the corresponding entry of the
11330 -- parameter block, and it is this local variable that may
11331 -- require an actual subtype.
11333 if Expander_Active then
11334 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11336 Decl := Build_Actual_Subtype (T, Formal);
11339 if Present (Handled_Statement_Sequence (N)) then
11341 First (Statements (Handled_Statement_Sequence (N)));
11342 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11343 Mark_Rewrite_Insertion (Decl);
11345 -- If the accept statement has no body, there will be no
11346 -- reference to the actuals, so no need to compute actual
11353 Decl := Build_Actual_Subtype (T, Formal);
11354 Prepend (Decl, Declarations (N));
11355 Mark_Rewrite_Insertion (Decl);
11358 -- The declaration uses the bounds of an existing object, and
11359 -- therefore needs no constraint checks.
11361 Analyze (Decl, Suppress => All_Checks);
11363 -- We need to freeze manually the generated type when it is
11364 -- inserted anywhere else than in a declarative part.
11366 if Present (First_Stmt) then
11367 Insert_List_Before_And_Analyze (First_Stmt,
11368 Freeze_Entity (Defining_Identifier (Decl), N));
11371 if Nkind (N) = N_Accept_Statement
11372 and then Expander_Active
11374 Set_Actual_Subtype (Renamed_Object (Formal),
11375 Defining_Identifier (Decl));
11377 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11381 Next_Formal (Formal);
11383 end Set_Actual_Subtypes;
11385 ---------------------
11386 -- Set_Formal_Mode --
11387 ---------------------
11389 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11390 Spec : constant Node_Id := Parent (Formal_Id);
11393 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11394 -- since we ensure that corresponding actuals are always valid at the
11395 -- point of the call.
11397 if Out_Present (Spec) then
11398 if Ekind (Scope (Formal_Id)) = E_Function
11399 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11401 -- [IN] OUT parameters allowed for functions in Ada 2012
11403 if Ada_Version >= Ada_2012 then
11405 -- Even in Ada 2012 operators can only have IN parameters
11407 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11408 Error_Msg_N ("operators can only have IN parameters", Spec);
11411 if In_Present (Spec) then
11412 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11414 Set_Ekind (Formal_Id, E_Out_Parameter);
11417 -- But not in earlier versions of Ada
11420 Error_Msg_N ("functions can only have IN parameters", Spec);
11421 Set_Ekind (Formal_Id, E_In_Parameter);
11424 elsif In_Present (Spec) then
11425 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11428 Set_Ekind (Formal_Id, E_Out_Parameter);
11429 Set_Never_Set_In_Source (Formal_Id, True);
11430 Set_Is_True_Constant (Formal_Id, False);
11431 Set_Current_Value (Formal_Id, Empty);
11435 Set_Ekind (Formal_Id, E_In_Parameter);
11438 -- Set Is_Known_Non_Null for access parameters since the language
11439 -- guarantees that access parameters are always non-null. We also set
11440 -- Can_Never_Be_Null, since there is no way to change the value.
11442 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11444 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11445 -- null; In Ada 2005, only if then null_exclusion is explicit.
11447 if Ada_Version < Ada_2005
11448 or else Can_Never_Be_Null (Etype (Formal_Id))
11450 Set_Is_Known_Non_Null (Formal_Id);
11451 Set_Can_Never_Be_Null (Formal_Id);
11454 -- Ada 2005 (AI-231): Null-exclusion access subtype
11456 elsif Is_Access_Type (Etype (Formal_Id))
11457 and then Can_Never_Be_Null (Etype (Formal_Id))
11459 Set_Is_Known_Non_Null (Formal_Id);
11461 -- We can also set Can_Never_Be_Null (thus preventing some junk
11462 -- access checks) for the case of an IN parameter, which cannot
11463 -- be changed, or for an IN OUT parameter, which can be changed but
11464 -- not to a null value. But for an OUT parameter, the initial value
11465 -- passed in can be null, so we can't set this flag in that case.
11467 if Ekind (Formal_Id) /= E_Out_Parameter then
11468 Set_Can_Never_Be_Null (Formal_Id);
11472 Set_Mechanism (Formal_Id, Default_Mechanism);
11473 Set_Formal_Validity (Formal_Id);
11474 end Set_Formal_Mode;
11476 -------------------------
11477 -- Set_Formal_Validity --
11478 -------------------------
11480 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11482 -- If no validity checking, then we cannot assume anything about the
11483 -- validity of parameters, since we do not know there is any checking
11484 -- of the validity on the call side.
11486 if not Validity_Checks_On then
11489 -- If validity checking for parameters is enabled, this means we are
11490 -- not supposed to make any assumptions about argument values.
11492 elsif Validity_Check_Parameters then
11495 -- If we are checking in parameters, we will assume that the caller is
11496 -- also checking parameters, so we can assume the parameter is valid.
11498 elsif Ekind (Formal_Id) = E_In_Parameter
11499 and then Validity_Check_In_Params
11501 Set_Is_Known_Valid (Formal_Id, True);
11503 -- Similar treatment for IN OUT parameters
11505 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11506 and then Validity_Check_In_Out_Params
11508 Set_Is_Known_Valid (Formal_Id, True);
11510 end Set_Formal_Validity;
11512 ------------------------
11513 -- Subtype_Conformant --
11514 ------------------------
11516 function Subtype_Conformant
11517 (New_Id : Entity_Id;
11518 Old_Id : Entity_Id;
11519 Skip_Controlling_Formals : Boolean := False) return Boolean
11523 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11524 Skip_Controlling_Formals => Skip_Controlling_Formals);
11526 end Subtype_Conformant;
11528 ---------------------
11529 -- Type_Conformant --
11530 ---------------------
11532 function Type_Conformant
11533 (New_Id : Entity_Id;
11534 Old_Id : Entity_Id;
11535 Skip_Controlling_Formals : Boolean := False) return Boolean
11539 May_Hide_Profile := False;
11542 (New_Id, Old_Id, Type_Conformant, False, Result,
11543 Skip_Controlling_Formals => Skip_Controlling_Formals);
11545 end Type_Conformant;
11547 -------------------------------
11548 -- Valid_Operator_Definition --
11549 -------------------------------
11551 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11554 Id : constant Name_Id := Chars (Designator);
11558 F := First_Formal (Designator);
11559 while Present (F) loop
11562 if Present (Default_Value (F)) then
11564 ("default values not allowed for operator parameters",
11571 -- Verify that user-defined operators have proper number of arguments
11572 -- First case of operators which can only be unary
11574 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11577 -- Case of operators which can be unary or binary
11579 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11580 N_OK := (N in 1 .. 2);
11582 -- All other operators can only be binary
11590 ("incorrect number of arguments for operator", Designator);
11594 and then Base_Type (Etype (Designator)) = Standard_Boolean
11595 and then not Is_Intrinsic_Subprogram (Designator)
11598 ("explicit definition of inequality not allowed", Designator);
11600 end Valid_Operator_Definition;