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 Process_PPCs
218 Body_Id : Entity_Id);
219 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
220 -- conditions for the body and assembling and inserting the _postconditions
221 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
222 -- the entities for the body and separate spec (if there is no separate
223 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
224 -- provide postconditions, and are also handled in this procedure.
226 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
227 -- Formal_Id is an formal parameter entity. This procedure deals with
228 -- setting the proper validity status for this entity, which depends on
229 -- the kind of parameter and the validity checking mode.
231 ---------------------------------------------
232 -- Analyze_Abstract_Subprogram_Declaration --
233 ---------------------------------------------
235 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
236 Designator : constant Entity_Id :=
237 Analyze_Subprogram_Specification (Specification (N));
238 Scop : constant Entity_Id := Current_Scope;
241 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
243 Generate_Definition (Designator);
244 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
245 Set_Is_Abstract_Subprogram (Designator);
246 New_Overloaded_Entity (Designator);
247 Check_Delayed_Subprogram (Designator);
249 Set_Categorization_From_Scope (Designator, Scop);
251 if Ekind (Scope (Designator)) = E_Protected_Type then
253 ("abstract subprogram not allowed in protected type", N);
255 -- Issue a warning if the abstract subprogram is neither a dispatching
256 -- operation nor an operation that overrides an inherited subprogram or
257 -- predefined operator, since this most likely indicates a mistake.
259 elsif Warn_On_Redundant_Constructs
260 and then not Is_Dispatching_Operation (Designator)
261 and then not Present (Overridden_Operation (Designator))
262 and then (not Is_Operator_Symbol_Name (Chars (Designator))
263 or else Scop /= Scope (Etype (First_Formal (Designator))))
266 ("abstract subprogram is not dispatching or overriding?r?", N);
269 Generate_Reference_To_Formals (Designator);
270 Check_Eliminated (Designator);
272 if Has_Aspects (N) then
273 Analyze_Aspect_Specifications (N, Designator);
275 end Analyze_Abstract_Subprogram_Declaration;
277 ---------------------------------
278 -- Analyze_Expression_Function --
279 ---------------------------------
281 procedure Analyze_Expression_Function (N : Node_Id) is
282 Loc : constant Source_Ptr := Sloc (N);
283 LocX : constant Source_Ptr := Sloc (Expression (N));
284 Expr : constant Node_Id := Expression (N);
285 Spec : constant Node_Id := Specification (N);
290 -- If the expression is a completion, Prev is the entity whose
291 -- declaration is completed. Def_Id is needed to analyze the spec.
299 -- This is one of the occasions on which we transform the tree during
300 -- semantic analysis. If this is a completion, transform the expression
301 -- function into an equivalent subprogram body, and analyze it.
303 -- Expression functions are inlined unconditionally. The back-end will
304 -- determine whether this is possible.
306 Inline_Processing_Required := True;
308 -- Create a specification for the generated body. Types and defauts in
309 -- the profile are copies of the spec, but new entities must be created
310 -- for the unit name and the formals.
312 New_Spec := New_Copy_Tree (Spec);
313 Set_Defining_Unit_Name (New_Spec,
314 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
315 Chars (Defining_Unit_Name (Spec))));
317 if Present (Parameter_Specifications (New_Spec)) then
319 Formal_Spec : Node_Id;
321 Formal_Spec := First (Parameter_Specifications (New_Spec));
322 while Present (Formal_Spec) loop
323 Set_Defining_Identifier
325 Make_Defining_Identifier (Sloc (Formal_Spec),
326 Chars => Chars (Defining_Identifier (Formal_Spec))));
332 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
334 -- If there are previous overloadable entities with the same name,
335 -- check whether any of them is completed by the expression function.
337 if Present (Prev) and then Is_Overloadable (Prev) then
338 Def_Id := Analyze_Subprogram_Specification (Spec);
339 Prev := Find_Corresponding_Spec (N);
342 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
345 Make_Subprogram_Body (Loc,
346 Specification => New_Spec,
347 Declarations => Empty_List,
348 Handled_Statement_Sequence =>
349 Make_Handled_Sequence_Of_Statements (LocX,
350 Statements => New_List (Ret)));
352 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
354 -- If the expression completes a generic subprogram, we must create a
355 -- separate node for the body, because at instantiation the original
356 -- node of the generic copy must be a generic subprogram body, and
357 -- cannot be a expression function. Otherwise we just rewrite the
358 -- expression with the non-generic body.
360 Insert_After (N, New_Body);
361 Rewrite (N, Make_Null_Statement (Loc));
362 Set_Has_Completion (Prev, False);
365 Set_Is_Inlined (Prev);
367 elsif Present (Prev) and then Comes_From_Source (Prev) then
368 Set_Has_Completion (Prev, False);
370 -- For navigation purposes, indicate that the function is a body
372 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
373 Rewrite (N, New_Body);
376 -- Prev is the previous entity with the same name, but it is can
377 -- be an unrelated spec that is not completed by the expression
378 -- function. In that case the relevant entity is the one in the body.
379 -- Not clear that the backend can inline it in this case ???
381 if Has_Completion (Prev) then
382 Set_Is_Inlined (Prev);
384 -- The formals of the expression function are body formals,
385 -- and do not appear in the ali file, which will only contain
386 -- references to the formals of the original subprogram spec.
393 F1 := First_Formal (Def_Id);
394 F2 := First_Formal (Prev);
396 while Present (F1) loop
397 Set_Spec_Entity (F1, F2);
404 Set_Is_Inlined (Defining_Entity (New_Body));
407 -- If this is not a completion, create both a declaration and a body, so
408 -- that the expression can be inlined whenever possible.
411 -- An expression function that is not a completion is not a
412 -- subprogram declaration, and thus cannot appear in a protected
415 if Nkind (Parent (N)) = N_Protected_Definition then
417 ("an expression function is not a legal protected operation", N);
421 Make_Subprogram_Declaration (Loc, Specification => Spec);
423 Rewrite (N, New_Decl);
425 Set_Is_Inlined (Defining_Entity (New_Decl));
427 -- To prevent premature freeze action, insert the new body at the end
428 -- of the current declarations, or at the end of the package spec.
429 -- However, resolve usage names now, to prevent spurious visibility
430 -- on later entities.
433 Decls : List_Id := List_Containing (N);
434 Par : constant Node_Id := Parent (Decls);
435 Id : constant Entity_Id := Defining_Entity (New_Decl);
438 if Nkind (Par) = N_Package_Specification
439 and then Decls = Visible_Declarations (Par)
440 and then Present (Private_Declarations (Par))
441 and then not Is_Empty_List (Private_Declarations (Par))
443 Decls := Private_Declarations (Par);
446 Insert_After (Last (Decls), New_Body);
448 Install_Formals (Id);
450 -- Do a preanalysis of the expression on a separate copy, to
451 -- prevent visibility issues later with operators in instances.
452 -- Attach copy to tree so that parent links are available.
455 Expr : constant Node_Id := New_Copy_Tree (Expression (Ret));
457 Set_Parent (Expr, Ret);
458 Preanalyze_Spec_Expression (Expr, Etype (Id));
465 -- If the return expression is a static constant, we suppress warning
466 -- messages on unused formals, which in most cases will be noise.
468 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
469 Is_OK_Static_Expression (Expr));
470 end Analyze_Expression_Function;
472 ----------------------------------------
473 -- Analyze_Extended_Return_Statement --
474 ----------------------------------------
476 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
478 Analyze_Return_Statement (N);
479 end Analyze_Extended_Return_Statement;
481 ----------------------------
482 -- Analyze_Function_Call --
483 ----------------------------
485 procedure Analyze_Function_Call (N : Node_Id) is
486 Actuals : constant List_Id := Parameter_Associations (N);
487 Func_Nam : constant Node_Id := Name (N);
493 -- A call of the form A.B (X) may be an Ada 2005 call, which is
494 -- rewritten as B (A, X). If the rewriting is successful, the call
495 -- has been analyzed and we just return.
497 if Nkind (Func_Nam) = N_Selected_Component
498 and then Name (N) /= Func_Nam
499 and then Is_Rewrite_Substitution (N)
500 and then Present (Etype (N))
505 -- If error analyzing name, then set Any_Type as result type and return
507 if Etype (Func_Nam) = Any_Type then
508 Set_Etype (N, Any_Type);
512 -- Otherwise analyze the parameters
514 if Present (Actuals) then
515 Actual := First (Actuals);
516 while Present (Actual) loop
518 Check_Parameterless_Call (Actual);
524 end Analyze_Function_Call;
526 -----------------------------
527 -- Analyze_Function_Return --
528 -----------------------------
530 procedure Analyze_Function_Return (N : Node_Id) is
531 Loc : constant Source_Ptr := Sloc (N);
532 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
533 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
535 R_Type : constant Entity_Id := Etype (Scope_Id);
536 -- Function result subtype
538 procedure Check_Limited_Return (Expr : Node_Id);
539 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
540 -- limited types. Used only for simple return statements.
541 -- Expr is the expression returned.
543 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
544 -- Check that the return_subtype_indication properly matches the result
545 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
547 --------------------------
548 -- Check_Limited_Return --
549 --------------------------
551 procedure Check_Limited_Return (Expr : Node_Id) is
553 -- Ada 2005 (AI-318-02): Return-by-reference types have been
554 -- removed and replaced by anonymous access results. This is an
555 -- incompatibility with Ada 95. Not clear whether this should be
556 -- enforced yet or perhaps controllable with special switch. ???
558 -- A limited interface that is not immutably limited is OK.
560 if Is_Limited_Interface (R_Type)
562 not (Is_Task_Interface (R_Type)
563 or else Is_Protected_Interface (R_Type)
564 or else Is_Synchronized_Interface (R_Type))
568 elsif Is_Limited_Type (R_Type)
569 and then not Is_Interface (R_Type)
570 and then Comes_From_Source (N)
571 and then not In_Instance_Body
572 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
576 if Ada_Version >= Ada_2005
577 and then not Debug_Flag_Dot_L
578 and then not GNAT_Mode
581 ("(Ada 2005) cannot copy object of a limited type " &
582 "(RM-2005 6.5(5.5/2))", Expr);
584 if Is_Immutably_Limited_Type (R_Type) then
586 ("\return by reference not permitted in Ada 2005", Expr);
589 -- Warn in Ada 95 mode, to give folks a heads up about this
592 -- In GNAT mode, this is just a warning, to allow it to be
593 -- evilly turned off. Otherwise it is a real error.
595 -- In a generic context, simplify the warning because it makes
596 -- no sense to discuss pass-by-reference or copy.
598 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
599 if Inside_A_Generic then
601 ("return of limited object not permitted in Ada 2005 "
602 & "(RM-2005 6.5(5.5/2))?y?", Expr);
604 elsif Is_Immutably_Limited_Type (R_Type) then
606 ("return by reference not permitted in Ada 2005 "
607 & "(RM-2005 6.5(5.5/2))?y?", Expr);
610 ("cannot copy object of a limited type in Ada 2005 "
611 & "(RM-2005 6.5(5.5/2))?y?", Expr);
614 -- Ada 95 mode, compatibility warnings disabled
617 return; -- skip continuation messages below
620 if not Inside_A_Generic then
622 ("\consider switching to return of access type", Expr);
623 Explain_Limited_Type (R_Type, Expr);
626 end Check_Limited_Return;
628 -------------------------------------
629 -- Check_Return_Subtype_Indication --
630 -------------------------------------
632 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
633 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
635 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
636 -- Subtype given in the extended return statement (must match R_Type)
638 Subtype_Ind : constant Node_Id :=
639 Object_Definition (Original_Node (Obj_Decl));
641 R_Type_Is_Anon_Access :
643 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
645 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
647 Ekind (R_Type) = E_Anonymous_Access_Type;
648 -- True if return type of the function is an anonymous access type
649 -- Can't we make Is_Anonymous_Access_Type in einfo ???
651 R_Stm_Type_Is_Anon_Access :
653 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
655 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
657 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
658 -- True if type of the return object is an anonymous access type
661 -- First, avoid cascaded errors
663 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
667 -- "return access T" case; check that the return statement also has
668 -- "access T", and that the subtypes statically match:
669 -- if this is an access to subprogram the signatures must match.
671 if R_Type_Is_Anon_Access then
672 if R_Stm_Type_Is_Anon_Access then
674 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
676 if Base_Type (Designated_Type (R_Stm_Type)) /=
677 Base_Type (Designated_Type (R_Type))
678 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
681 ("subtype must statically match function result subtype",
682 Subtype_Mark (Subtype_Ind));
686 -- For two anonymous access to subprogram types, the
687 -- types themselves must be type conformant.
689 if not Conforming_Types
690 (R_Stm_Type, R_Type, Fully_Conformant)
693 ("subtype must statically match function result subtype",
699 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
702 -- If the return object is of an anonymous access type, then report
703 -- an error if the function's result type is not also anonymous.
705 elsif R_Stm_Type_Is_Anon_Access
706 and then not R_Type_Is_Anon_Access
708 Error_Msg_N ("anonymous access not allowed for function with " &
709 "named access result", Subtype_Ind);
711 -- Subtype indication case: check that the return object's type is
712 -- covered by the result type, and that the subtypes statically match
713 -- when the result subtype is constrained. Also handle record types
714 -- with unknown discriminants for which we have built the underlying
715 -- record view. Coverage is needed to allow specific-type return
716 -- objects when the result type is class-wide (see AI05-32).
718 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
719 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
723 Underlying_Record_View (Base_Type (R_Stm_Type))))
725 -- A null exclusion may be present on the return type, on the
726 -- function specification, on the object declaration or on the
729 if Is_Access_Type (R_Type)
731 (Can_Never_Be_Null (R_Type)
732 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
733 Can_Never_Be_Null (R_Stm_Type)
736 ("subtype must statically match function result subtype",
740 -- AI05-103: for elementary types, subtypes must statically match
742 if Is_Constrained (R_Type)
743 or else Is_Access_Type (R_Type)
745 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
747 ("subtype must statically match function result subtype",
752 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
753 and then Is_Null_Extension (Base_Type (R_Type))
759 ("wrong type for return_subtype_indication", Subtype_Ind);
761 end Check_Return_Subtype_Indication;
763 ---------------------
764 -- Local Variables --
765 ---------------------
769 -- Start of processing for Analyze_Function_Return
772 Set_Return_Present (Scope_Id);
774 if Nkind (N) = N_Simple_Return_Statement then
775 Expr := Expression (N);
777 -- Guard against a malformed expression. The parser may have tried to
778 -- recover but the node is not analyzable.
780 if Nkind (Expr) = N_Error then
781 Set_Etype (Expr, Any_Type);
782 Expander_Mode_Save_And_Set (False);
786 -- The resolution of a controlled [extension] aggregate associated
787 -- with a return statement creates a temporary which needs to be
788 -- finalized on function exit. Wrap the return statement inside a
789 -- block so that the finalization machinery can detect this case.
790 -- This early expansion is done only when the return statement is
791 -- not part of a handled sequence of statements.
793 if Nkind_In (Expr, N_Aggregate,
794 N_Extension_Aggregate)
795 and then Needs_Finalization (R_Type)
796 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
799 Make_Block_Statement (Loc,
800 Handled_Statement_Sequence =>
801 Make_Handled_Sequence_Of_Statements (Loc,
802 Statements => New_List (Relocate_Node (N)))));
808 Analyze_And_Resolve (Expr, R_Type);
809 Check_Limited_Return (Expr);
812 -- RETURN only allowed in SPARK as the last statement in function
814 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
816 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
817 or else Present (Next (N)))
819 Check_SPARK_Restriction
820 ("RETURN should be the last statement in function", N);
824 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
826 -- Analyze parts specific to extended_return_statement:
829 Obj_Decl : constant Node_Id :=
830 Last (Return_Object_Declarations (N));
831 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
832 HSS : constant Node_Id := Handled_Statement_Sequence (N);
835 Expr := Expression (Obj_Decl);
837 -- Note: The check for OK_For_Limited_Init will happen in
838 -- Analyze_Object_Declaration; we treat it as a normal
839 -- object declaration.
841 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
844 Check_Return_Subtype_Indication (Obj_Decl);
846 if Present (HSS) then
849 if Present (Exception_Handlers (HSS)) then
851 -- ???Has_Nested_Block_With_Handler needs to be set.
852 -- Probably by creating an actual N_Block_Statement.
853 -- Probably in Expand.
859 -- Mark the return object as referenced, since the return is an
860 -- implicit reference of the object.
862 Set_Referenced (Defining_Identifier (Obj_Decl));
864 Check_References (Stm_Entity);
866 -- Check RM 6.5 (5.9/3)
869 if Ada_Version < Ada_2012 then
871 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
872 -- Can it really happen (extended return???)
875 ("aliased only allowed for limited"
876 & " return objects in Ada 2012?", N);
878 elsif not Is_Immutably_Limited_Type (R_Type) then
879 Error_Msg_N ("aliased only allowed for limited"
880 & " return objects", N);
886 -- Case of Expr present
890 -- Defend against previous errors
892 and then Nkind (Expr) /= N_Empty
893 and then Present (Etype (Expr))
895 -- Apply constraint check. Note that this is done before the implicit
896 -- conversion of the expression done for anonymous access types to
897 -- ensure correct generation of the null-excluding check associated
898 -- with null-excluding expressions found in return statements.
900 Apply_Constraint_Check (Expr, R_Type);
902 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
903 -- type, apply an implicit conversion of the expression to that type
904 -- to force appropriate static and run-time accessibility checks.
906 if Ada_Version >= Ada_2005
907 and then Ekind (R_Type) = E_Anonymous_Access_Type
909 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
910 Analyze_And_Resolve (Expr, R_Type);
912 -- If this is a local anonymous access to subprogram, the
913 -- accessibility check can be applied statically. The return is
914 -- illegal if the access type of the return expression is declared
915 -- inside of the subprogram (except if it is the subtype indication
916 -- of an extended return statement).
918 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
919 if not Comes_From_Source (Current_Scope)
920 or else Ekind (Current_Scope) = E_Return_Statement
925 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
927 Error_Msg_N ("cannot return local access to subprogram", N);
931 -- If the result type is class-wide, then check that the return
932 -- expression's type is not declared at a deeper level than the
933 -- function (RM05-6.5(5.6/2)).
935 if Ada_Version >= Ada_2005
936 and then Is_Class_Wide_Type (R_Type)
938 if Type_Access_Level (Etype (Expr)) >
939 Subprogram_Access_Level (Scope_Id)
942 ("level of return expression type is deeper than " &
943 "class-wide function!", Expr);
947 -- Check incorrect use of dynamically tagged expression
949 if Is_Tagged_Type (R_Type) then
950 Check_Dynamically_Tagged_Expression
956 -- ??? A real run-time accessibility check is needed in cases
957 -- involving dereferences of access parameters. For now we just
958 -- check the static cases.
960 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
961 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
962 and then Object_Access_Level (Expr) >
963 Subprogram_Access_Level (Scope_Id)
965 -- Suppress the message in a generic, where the rewriting
968 if Inside_A_Generic then
973 Make_Raise_Program_Error (Loc,
974 Reason => PE_Accessibility_Check_Failed));
978 ("cannot return a local value by reference??", N);
980 ("\& will be raised at run time??",
981 N, Standard_Program_Error);
986 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
987 and then Null_Exclusion_Present (Parent (Scope_Id))
989 Apply_Compile_Time_Constraint_Error
991 Msg => "(Ada 2005) null not allowed for "
992 & "null-excluding return??",
993 Reason => CE_Null_Not_Allowed);
996 end Analyze_Function_Return;
998 -------------------------------------
999 -- Analyze_Generic_Subprogram_Body --
1000 -------------------------------------
1002 procedure Analyze_Generic_Subprogram_Body
1006 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1007 Kind : constant Entity_Kind := Ekind (Gen_Id);
1008 Body_Id : Entity_Id;
1013 -- Copy body and disable expansion while analyzing the generic For a
1014 -- stub, do not copy the stub (which would load the proper body), this
1015 -- will be done when the proper body is analyzed.
1017 if Nkind (N) /= N_Subprogram_Body_Stub then
1018 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1023 Spec := Specification (N);
1025 -- Within the body of the generic, the subprogram is callable, and
1026 -- behaves like the corresponding non-generic unit.
1028 Body_Id := Defining_Entity (Spec);
1030 if Kind = E_Generic_Procedure
1031 and then Nkind (Spec) /= N_Procedure_Specification
1033 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1036 elsif Kind = E_Generic_Function
1037 and then Nkind (Spec) /= N_Function_Specification
1039 Error_Msg_N ("invalid body for generic function ", Body_Id);
1043 Set_Corresponding_Body (Gen_Decl, Body_Id);
1045 if Has_Completion (Gen_Id)
1046 and then Nkind (Parent (N)) /= N_Subunit
1048 Error_Msg_N ("duplicate generic body", N);
1051 Set_Has_Completion (Gen_Id);
1054 if Nkind (N) = N_Subprogram_Body_Stub then
1055 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1057 Set_Corresponding_Spec (N, Gen_Id);
1060 if Nkind (Parent (N)) = N_Compilation_Unit then
1061 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1064 -- Make generic parameters immediately visible in the body. They are
1065 -- needed to process the formals declarations. Then make the formals
1066 -- visible in a separate step.
1068 Push_Scope (Gen_Id);
1072 First_Ent : Entity_Id;
1075 First_Ent := First_Entity (Gen_Id);
1078 while Present (E) and then not Is_Formal (E) loop
1083 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1085 -- Now generic formals are visible, and the specification can be
1086 -- analyzed, for subsequent conformance check.
1088 Body_Id := Analyze_Subprogram_Specification (Spec);
1090 -- Make formal parameters visible
1094 -- E is the first formal parameter, we loop through the formals
1095 -- installing them so that they will be visible.
1097 Set_First_Entity (Gen_Id, E);
1098 while Present (E) loop
1104 -- Visible generic entity is callable within its own body
1106 Set_Ekind (Gen_Id, Ekind (Body_Id));
1107 Set_Contract (Body_Id, Empty);
1108 Set_Ekind (Body_Id, E_Subprogram_Body);
1109 Set_Convention (Body_Id, Convention (Gen_Id));
1110 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1111 Set_Scope (Body_Id, Scope (Gen_Id));
1112 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1114 if Nkind (N) = N_Subprogram_Body_Stub then
1116 -- No body to analyze, so restore state of generic unit
1118 Set_Ekind (Gen_Id, Kind);
1119 Set_Ekind (Body_Id, Kind);
1121 if Present (First_Ent) then
1122 Set_First_Entity (Gen_Id, First_Ent);
1129 -- If this is a compilation unit, it must be made visible explicitly,
1130 -- because the compilation of the declaration, unlike other library
1131 -- unit declarations, does not. If it is not a unit, the following
1132 -- is redundant but harmless.
1134 Set_Is_Immediately_Visible (Gen_Id);
1135 Reference_Body_Formals (Gen_Id, Body_Id);
1137 if Is_Child_Unit (Gen_Id) then
1138 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1141 Set_Actual_Subtypes (N, Current_Scope);
1143 -- Deal with preconditions and postconditions. In formal verification
1144 -- mode, we keep pre- and postconditions attached to entities rather
1145 -- than inserted in the code, in order to facilitate a distinct
1146 -- treatment for them.
1148 if not SPARK_Mode then
1149 Process_PPCs (N, Gen_Id, Body_Id);
1152 -- If the generic unit carries pre- or post-conditions, copy them
1153 -- to the original generic tree, so that they are properly added
1154 -- to any instantiation.
1157 Orig : constant Node_Id := Original_Node (N);
1161 Cond := First (Declarations (N));
1162 while Present (Cond) loop
1163 if Nkind (Cond) = N_Pragma
1164 and then Pragma_Name (Cond) = Name_Check
1166 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1168 elsif Nkind (Cond) = N_Pragma
1169 and then Pragma_Name (Cond) = Name_Postcondition
1171 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1172 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1181 Analyze_Declarations (Declarations (N));
1183 Analyze (Handled_Statement_Sequence (N));
1185 Save_Global_References (Original_Node (N));
1187 -- Prior to exiting the scope, include generic formals again (if any
1188 -- are present) in the set of local entities.
1190 if Present (First_Ent) then
1191 Set_First_Entity (Gen_Id, First_Ent);
1194 Check_References (Gen_Id);
1197 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1199 Check_Subprogram_Order (N);
1201 -- Outside of its body, unit is generic again
1203 Set_Ekind (Gen_Id, Kind);
1204 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1207 Style.Check_Identifier (Body_Id, Gen_Id);
1211 end Analyze_Generic_Subprogram_Body;
1213 ----------------------------
1214 -- Analyze_Null_Procedure --
1215 ----------------------------
1217 procedure Analyze_Null_Procedure
1219 Is_Completion : out Boolean)
1221 Loc : constant Source_Ptr := Sloc (N);
1222 Spec : constant Node_Id := Specification (N);
1223 Designator : Entity_Id;
1225 Null_Body : Node_Id := Empty;
1229 -- Capture the profile of the null procedure before analysis, for
1230 -- expansion at the freeze point and at each point of call. The body is
1231 -- used if the procedure has preconditions, or if it is a completion. In
1232 -- the first case the body is analyzed at the freeze point, in the other
1233 -- it replaces the null procedure declaration.
1236 Make_Subprogram_Body (Loc,
1237 Specification => New_Copy_Tree (Spec),
1238 Declarations => New_List,
1239 Handled_Statement_Sequence =>
1240 Make_Handled_Sequence_Of_Statements (Loc,
1241 Statements => New_List (Make_Null_Statement (Loc))));
1243 -- Create new entities for body and formals
1245 Set_Defining_Unit_Name (Specification (Null_Body),
1246 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1248 Form := First (Parameter_Specifications (Specification (Null_Body)));
1249 while Present (Form) loop
1250 Set_Defining_Identifier (Form,
1251 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1255 -- Determine whether the null procedure may be a completion of a generic
1256 -- suprogram, in which case we use the new null body as the completion
1257 -- and set minimal semantic information on the original declaration,
1258 -- which is rewritten as a null statement.
1260 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1262 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1263 Insert_Before (N, Null_Body);
1264 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1265 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1267 Rewrite (N, Make_Null_Statement (Loc));
1268 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1269 Is_Completion := True;
1274 -- Resolve the types of the formals now, because the freeze point
1275 -- may appear in a different context, e.g. an instantiation.
1277 Form := First (Parameter_Specifications (Specification (Null_Body)));
1278 while Present (Form) loop
1279 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1280 Find_Type (Parameter_Type (Form));
1283 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1285 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1288 -- The case of a null procedure with a formal that is an
1289 -- access_to_subprogram type, and that is used as an actual
1290 -- in an instantiation is left to the enthusiastic reader.
1299 -- If there are previous overloadable entities with the same name,
1300 -- check whether any of them is completed by the null procedure.
1302 if Present (Prev) and then Is_Overloadable (Prev) then
1303 Designator := Analyze_Subprogram_Specification (Spec);
1304 Prev := Find_Corresponding_Spec (N);
1307 if No (Prev) or else not Comes_From_Source (Prev) then
1308 Designator := Analyze_Subprogram_Specification (Spec);
1309 Set_Has_Completion (Designator);
1311 -- Signal to caller that this is a procedure declaration
1313 Is_Completion := False;
1315 -- Null procedures are always inlined, but generic formal subprograms
1316 -- which appear as such in the internal instance of formal packages,
1317 -- need no completion and are not marked Inline.
1320 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1322 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1323 Set_Body_To_Inline (N, Null_Body);
1324 Set_Is_Inlined (Designator);
1328 -- The null procedure is a completion
1330 Is_Completion := True;
1332 if Expander_Active then
1333 Rewrite (N, Null_Body);
1337 Designator := Analyze_Subprogram_Specification (Spec);
1338 Set_Has_Completion (Designator);
1339 Set_Has_Completion (Prev);
1342 end Analyze_Null_Procedure;
1344 -----------------------------
1345 -- Analyze_Operator_Symbol --
1346 -----------------------------
1348 -- An operator symbol such as "+" or "and" may appear in context where the
1349 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1350 -- is just a string, as in (conjunction = "or"). In these cases the parser
1351 -- generates this node, and the semantics does the disambiguation. Other
1352 -- such case are actuals in an instantiation, the generic unit in an
1353 -- instantiation, and pragma arguments.
1355 procedure Analyze_Operator_Symbol (N : Node_Id) is
1356 Par : constant Node_Id := Parent (N);
1359 if (Nkind (Par) = N_Function_Call
1360 and then N = Name (Par))
1361 or else Nkind (Par) = N_Function_Instantiation
1362 or else (Nkind (Par) = N_Indexed_Component
1363 and then N = Prefix (Par))
1364 or else (Nkind (Par) = N_Pragma_Argument_Association
1365 and then not Is_Pragma_String_Literal (Par))
1366 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1367 or else (Nkind (Par) = N_Attribute_Reference
1368 and then Attribute_Name (Par) /= Name_Value)
1370 Find_Direct_Name (N);
1373 Change_Operator_Symbol_To_String_Literal (N);
1376 end Analyze_Operator_Symbol;
1378 -----------------------------------
1379 -- Analyze_Parameter_Association --
1380 -----------------------------------
1382 procedure Analyze_Parameter_Association (N : Node_Id) is
1384 Analyze (Explicit_Actual_Parameter (N));
1385 end Analyze_Parameter_Association;
1387 ----------------------------
1388 -- Analyze_Procedure_Call --
1389 ----------------------------
1391 procedure Analyze_Procedure_Call (N : Node_Id) is
1392 Loc : constant Source_Ptr := Sloc (N);
1393 P : constant Node_Id := Name (N);
1394 Actuals : constant List_Id := Parameter_Associations (N);
1398 procedure Analyze_Call_And_Resolve;
1399 -- Do Analyze and Resolve calls for procedure call
1400 -- At end, check illegal order dependence.
1402 ------------------------------
1403 -- Analyze_Call_And_Resolve --
1404 ------------------------------
1406 procedure Analyze_Call_And_Resolve is
1408 if Nkind (N) = N_Procedure_Call_Statement then
1410 Resolve (N, Standard_Void_Type);
1414 end Analyze_Call_And_Resolve;
1416 -- Start of processing for Analyze_Procedure_Call
1419 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1420 -- a procedure call or an entry call. The prefix may denote an access
1421 -- to subprogram type, in which case an implicit dereference applies.
1422 -- If the prefix is an indexed component (without implicit dereference)
1423 -- then the construct denotes a call to a member of an entire family.
1424 -- If the prefix is a simple name, it may still denote a call to a
1425 -- parameterless member of an entry family. Resolution of these various
1426 -- interpretations is delicate.
1430 -- If this is a call of the form Obj.Op, the call may have been
1431 -- analyzed and possibly rewritten into a block, in which case
1434 if Analyzed (N) then
1438 -- If there is an error analyzing the name (which may have been
1439 -- rewritten if the original call was in prefix notation) then error
1440 -- has been emitted already, mark node and return.
1442 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1443 Set_Etype (N, Any_Type);
1447 -- Otherwise analyze the parameters
1449 if Present (Actuals) then
1450 Actual := First (Actuals);
1452 while Present (Actual) loop
1454 Check_Parameterless_Call (Actual);
1459 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1461 if Nkind (P) = N_Attribute_Reference
1462 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1464 Name_Elab_Subp_Body)
1466 if Present (Actuals) then
1468 ("no parameters allowed for this call", First (Actuals));
1472 Set_Etype (N, Standard_Void_Type);
1475 elsif Is_Entity_Name (P)
1476 and then Is_Record_Type (Etype (Entity (P)))
1477 and then Remote_AST_I_Dereference (P)
1481 elsif Is_Entity_Name (P)
1482 and then Ekind (Entity (P)) /= E_Entry_Family
1484 if Is_Access_Type (Etype (P))
1485 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1486 and then No (Actuals)
1487 and then Comes_From_Source (N)
1489 Error_Msg_N ("missing explicit dereference in call", N);
1492 Analyze_Call_And_Resolve;
1494 -- If the prefix is the simple name of an entry family, this is
1495 -- a parameterless call from within the task body itself.
1497 elsif Is_Entity_Name (P)
1498 and then Nkind (P) = N_Identifier
1499 and then Ekind (Entity (P)) = E_Entry_Family
1500 and then Present (Actuals)
1501 and then No (Next (First (Actuals)))
1503 -- Can be call to parameterless entry family. What appears to be the
1504 -- sole argument is in fact the entry index. Rewrite prefix of node
1505 -- accordingly. Source representation is unchanged by this
1509 Make_Indexed_Component (Loc,
1511 Make_Selected_Component (Loc,
1512 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1513 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1514 Expressions => Actuals);
1515 Set_Name (N, New_N);
1516 Set_Etype (New_N, Standard_Void_Type);
1517 Set_Parameter_Associations (N, No_List);
1518 Analyze_Call_And_Resolve;
1520 elsif Nkind (P) = N_Explicit_Dereference then
1521 if Ekind (Etype (P)) = E_Subprogram_Type then
1522 Analyze_Call_And_Resolve;
1524 Error_Msg_N ("expect access to procedure in call", P);
1527 -- The name can be a selected component or an indexed component that
1528 -- yields an access to subprogram. Such a prefix is legal if the call
1529 -- has parameter associations.
1531 elsif Is_Access_Type (Etype (P))
1532 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1534 if Present (Actuals) then
1535 Analyze_Call_And_Resolve;
1537 Error_Msg_N ("missing explicit dereference in call ", N);
1540 -- If not an access to subprogram, then the prefix must resolve to the
1541 -- name of an entry, entry family, or protected operation.
1543 -- For the case of a simple entry call, P is a selected component where
1544 -- the prefix is the task and the selector name is the entry. A call to
1545 -- a protected procedure will have the same syntax. If the protected
1546 -- object contains overloaded operations, the entity may appear as a
1547 -- function, the context will select the operation whose type is Void.
1549 elsif Nkind (P) = N_Selected_Component
1550 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1554 Analyze_Call_And_Resolve;
1556 elsif Nkind (P) = N_Selected_Component
1557 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1558 and then Present (Actuals)
1559 and then No (Next (First (Actuals)))
1561 -- Can be call to parameterless entry family. What appears to be the
1562 -- sole argument is in fact the entry index. Rewrite prefix of node
1563 -- accordingly. Source representation is unchanged by this
1567 Make_Indexed_Component (Loc,
1568 Prefix => New_Copy (P),
1569 Expressions => Actuals);
1570 Set_Name (N, New_N);
1571 Set_Etype (New_N, Standard_Void_Type);
1572 Set_Parameter_Associations (N, No_List);
1573 Analyze_Call_And_Resolve;
1575 -- For the case of a reference to an element of an entry family, P is
1576 -- an indexed component whose prefix is a selected component (task and
1577 -- entry family), and whose index is the entry family index.
1579 elsif Nkind (P) = N_Indexed_Component
1580 and then Nkind (Prefix (P)) = N_Selected_Component
1581 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1583 Analyze_Call_And_Resolve;
1585 -- If the prefix is the name of an entry family, it is a call from
1586 -- within the task body itself.
1588 elsif Nkind (P) = N_Indexed_Component
1589 and then Nkind (Prefix (P)) = N_Identifier
1590 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1593 Make_Selected_Component (Loc,
1594 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1595 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1596 Rewrite (Prefix (P), New_N);
1598 Analyze_Call_And_Resolve;
1600 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1601 -- procedure name, so the construct can only be a qualified expression.
1603 elsif Nkind (P) = N_Qualified_Expression
1604 and then Ada_Version >= Ada_2012
1606 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1609 -- Anything else is an error
1612 Error_Msg_N ("invalid procedure or entry call", N);
1614 end Analyze_Procedure_Call;
1616 ------------------------------
1617 -- Analyze_Return_Statement --
1618 ------------------------------
1620 procedure Analyze_Return_Statement (N : Node_Id) is
1622 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1623 N_Extended_Return_Statement));
1625 Returns_Object : constant Boolean :=
1626 Nkind (N) = N_Extended_Return_Statement
1628 (Nkind (N) = N_Simple_Return_Statement
1629 and then Present (Expression (N)));
1630 -- True if we're returning something; that is, "return <expression>;"
1631 -- or "return Result : T [:= ...]". False for "return;". Used for error
1632 -- checking: If Returns_Object is True, N should apply to a function
1633 -- body; otherwise N should apply to a procedure body, entry body,
1634 -- accept statement, or extended return statement.
1636 function Find_What_It_Applies_To return Entity_Id;
1637 -- Find the entity representing the innermost enclosing body, accept
1638 -- statement, or extended return statement. If the result is a callable
1639 -- construct or extended return statement, then this will be the value
1640 -- of the Return_Applies_To attribute. Otherwise, the program is
1641 -- illegal. See RM-6.5(4/2).
1643 -----------------------------
1644 -- Find_What_It_Applies_To --
1645 -----------------------------
1647 function Find_What_It_Applies_To return Entity_Id is
1648 Result : Entity_Id := Empty;
1651 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1652 -- and postconditions.
1654 for J in reverse 0 .. Scope_Stack.Last loop
1655 Result := Scope_Stack.Table (J).Entity;
1656 exit when not Ekind_In (Result, E_Block, E_Loop)
1657 and then Chars (Result) /= Name_uPostconditions;
1660 pragma Assert (Present (Result));
1662 end Find_What_It_Applies_To;
1664 -- Local declarations
1666 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1667 Kind : constant Entity_Kind := Ekind (Scope_Id);
1668 Loc : constant Source_Ptr := Sloc (N);
1669 Stm_Entity : constant Entity_Id :=
1671 (E_Return_Statement, Current_Scope, Loc, 'R');
1673 -- Start of processing for Analyze_Return_Statement
1676 Set_Return_Statement_Entity (N, Stm_Entity);
1678 Set_Etype (Stm_Entity, Standard_Void_Type);
1679 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1681 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1682 -- (4/2): an inner return statement will apply to this extended return.
1684 if Nkind (N) = N_Extended_Return_Statement then
1685 Push_Scope (Stm_Entity);
1688 -- Check that pragma No_Return is obeyed. Don't complain about the
1689 -- implicitly-generated return that is placed at the end.
1691 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1692 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1695 -- Warn on any unassigned OUT parameters if in procedure
1697 if Ekind (Scope_Id) = E_Procedure then
1698 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1701 -- Check that functions return objects, and other things do not
1703 if Kind = E_Function or else Kind = E_Generic_Function then
1704 if not Returns_Object then
1705 Error_Msg_N ("missing expression in return from function", N);
1708 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1709 if Returns_Object then
1710 Error_Msg_N ("procedure cannot return value (use function)", N);
1713 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1714 if Returns_Object then
1715 if Is_Protected_Type (Scope (Scope_Id)) then
1716 Error_Msg_N ("entry body cannot return value", N);
1718 Error_Msg_N ("accept statement cannot return value", N);
1722 elsif Kind = E_Return_Statement then
1724 -- We are nested within another return statement, which must be an
1725 -- extended_return_statement.
1727 if Returns_Object then
1728 if Nkind (N) = N_Extended_Return_Statement then
1730 ("extended return statement cannot be nested (use `RETURN;`)",
1733 -- Case of a simple return statement with a value inside extended
1734 -- return statement.
1738 ("return nested in extended return statement cannot return " &
1739 "value (use `RETURN;`)", N);
1744 Error_Msg_N ("illegal context for return statement", N);
1747 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1748 Analyze_Function_Return (N);
1750 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1751 Set_Return_Present (Scope_Id);
1754 if Nkind (N) = N_Extended_Return_Statement then
1758 Kill_Current_Values (Last_Assignment_Only => True);
1759 Check_Unreachable_Code (N);
1761 Analyze_Dimension (N);
1762 end Analyze_Return_Statement;
1764 -------------------------------------
1765 -- Analyze_Simple_Return_Statement --
1766 -------------------------------------
1768 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1770 if Present (Expression (N)) then
1771 Mark_Coextensions (N, Expression (N));
1774 Analyze_Return_Statement (N);
1775 end Analyze_Simple_Return_Statement;
1777 -------------------------
1778 -- Analyze_Return_Type --
1779 -------------------------
1781 procedure Analyze_Return_Type (N : Node_Id) is
1782 Designator : constant Entity_Id := Defining_Entity (N);
1783 Typ : Entity_Id := Empty;
1786 -- Normal case where result definition does not indicate an error
1788 if Result_Definition (N) /= Error then
1789 if Nkind (Result_Definition (N)) = N_Access_Definition then
1790 Check_SPARK_Restriction
1791 ("access result is not allowed", Result_Definition (N));
1793 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1796 AD : constant Node_Id :=
1797 Access_To_Subprogram_Definition (Result_Definition (N));
1799 if Present (AD) and then Protected_Present (AD) then
1800 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1802 Typ := Access_Definition (N, Result_Definition (N));
1806 Set_Parent (Typ, Result_Definition (N));
1807 Set_Is_Local_Anonymous_Access (Typ);
1808 Set_Etype (Designator, Typ);
1810 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1812 Null_Exclusion_Static_Checks (N);
1814 -- Subtype_Mark case
1817 Find_Type (Result_Definition (N));
1818 Typ := Entity (Result_Definition (N));
1819 Set_Etype (Designator, Typ);
1821 -- Unconstrained array as result is not allowed in SPARK
1823 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1824 Check_SPARK_Restriction
1825 ("returning an unconstrained array is not allowed",
1826 Result_Definition (N));
1829 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1831 Null_Exclusion_Static_Checks (N);
1833 -- If a null exclusion is imposed on the result type, then create
1834 -- a null-excluding itype (an access subtype) and use it as the
1835 -- function's Etype. Note that the null exclusion checks are done
1836 -- right before this, because they don't get applied to types that
1837 -- do not come from source.
1839 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1840 Set_Etype (Designator,
1841 Create_Null_Excluding_Itype
1844 Scope_Id => Scope (Current_Scope)));
1846 -- The new subtype must be elaborated before use because
1847 -- it is visible outside of the function. However its base
1848 -- type may not be frozen yet, so the reference that will
1849 -- force elaboration must be attached to the freezing of
1852 -- If the return specification appears on a proper body,
1853 -- the subtype will have been created already on the spec.
1855 if Is_Frozen (Typ) then
1856 if Nkind (Parent (N)) = N_Subprogram_Body
1857 and then Nkind (Parent (Parent (N))) = N_Subunit
1861 Build_Itype_Reference (Etype (Designator), Parent (N));
1865 Ensure_Freeze_Node (Typ);
1868 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1870 Set_Itype (IR, Etype (Designator));
1871 Append_Freeze_Actions (Typ, New_List (IR));
1876 Set_Etype (Designator, Typ);
1879 if Ekind (Typ) = E_Incomplete_Type
1880 and then Is_Value_Type (Typ)
1884 elsif Ekind (Typ) = E_Incomplete_Type
1885 or else (Is_Class_Wide_Type (Typ)
1886 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1888 -- AI05-0151: Tagged incomplete types are allowed in all formal
1889 -- parts. Untagged incomplete types are not allowed in bodies.
1891 if Ada_Version >= Ada_2012 then
1892 if Is_Tagged_Type (Typ) then
1895 elsif Nkind_In (Parent (Parent (N)),
1901 ("invalid use of untagged incomplete type&",
1905 -- The type must be completed in the current package. This
1906 -- is checked at the end of the package declaraton, when
1907 -- Taft-amendment types are identified. If the return type
1908 -- is class-wide, there is no required check, the type can
1909 -- be a bona fide TAT.
1911 if Ekind (Scope (Current_Scope)) = E_Package
1912 and then In_Private_Part (Scope (Current_Scope))
1913 and then not Is_Class_Wide_Type (Typ)
1915 Append_Elmt (Designator, Private_Dependents (Typ));
1920 ("invalid use of incomplete type&", Designator, Typ);
1925 -- Case where result definition does indicate an error
1928 Set_Etype (Designator, Any_Type);
1930 end Analyze_Return_Type;
1932 -----------------------------
1933 -- Analyze_Subprogram_Body --
1934 -----------------------------
1936 procedure Analyze_Subprogram_Body (N : Node_Id) is
1937 Loc : constant Source_Ptr := Sloc (N);
1938 Body_Spec : constant Node_Id := Specification (N);
1939 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1942 if Debug_Flag_C then
1943 Write_Str ("==> subprogram body ");
1944 Write_Name (Chars (Body_Id));
1945 Write_Str (" from ");
1946 Write_Location (Loc);
1951 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1953 -- The real work is split out into the helper, so it can do "return;"
1954 -- without skipping the debug output:
1956 Analyze_Subprogram_Body_Helper (N);
1958 if Debug_Flag_C then
1960 Write_Str ("<== subprogram body ");
1961 Write_Name (Chars (Body_Id));
1962 Write_Str (" from ");
1963 Write_Location (Loc);
1966 end Analyze_Subprogram_Body;
1968 ------------------------------------
1969 -- Analyze_Subprogram_Body_Helper --
1970 ------------------------------------
1972 -- This procedure is called for regular subprogram bodies, generic bodies,
1973 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1974 -- specification matters, and is used to create a proper declaration for
1975 -- the subprogram, or to perform conformance checks.
1977 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1978 Loc : constant Source_Ptr := Sloc (N);
1979 Body_Spec : constant Node_Id := Specification (N);
1980 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1981 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1982 Conformant : Boolean;
1984 Prot_Typ : Entity_Id := Empty;
1985 Spec_Id : Entity_Id;
1986 Spec_Decl : Node_Id := Empty;
1988 Last_Real_Spec_Entity : Entity_Id := Empty;
1989 -- When we analyze a separate spec, the entity chain ends up containing
1990 -- the formals, as well as any itypes generated during analysis of the
1991 -- default expressions for parameters, or the arguments of associated
1992 -- precondition/postcondition pragmas (which are analyzed in the context
1993 -- of the spec since they have visibility on formals).
1995 -- These entities belong with the spec and not the body. However we do
1996 -- the analysis of the body in the context of the spec (again to obtain
1997 -- visibility to the formals), and all the entities generated during
1998 -- this analysis end up also chained to the entity chain of the spec.
1999 -- But they really belong to the body, and there is circuitry to move
2000 -- them from the spec to the body.
2002 -- However, when we do this move, we don't want to move the real spec
2003 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2004 -- variable points to the last real spec entity, so we only move those
2005 -- chained beyond that point. It is initialized to Empty to deal with
2006 -- the case where there is no separate spec.
2008 procedure Check_Anonymous_Return;
2009 -- Ada 2005: if a function returns an access type that denotes a task,
2010 -- or a type that contains tasks, we must create a master entity for
2011 -- the anonymous type, which typically will be used in an allocator
2012 -- in the body of the function.
2014 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2015 -- Look ahead to recognize a pragma that may appear after the body.
2016 -- If there is a previous spec, check that it appears in the same
2017 -- declarative part. If the pragma is Inline_Always, perform inlining
2018 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2019 -- If the body acts as a spec, and inlining is required, we create a
2020 -- subprogram declaration for it, in order to attach the body to inline.
2021 -- If pragma does not appear after the body, check whether there is
2022 -- an inline pragma before any local declarations.
2024 procedure Check_Missing_Return;
2025 -- Checks for a function with a no return statements, and also performs
2026 -- the warning checks implemented by Check_Returns. In formal mode, also
2027 -- verify that a function ends with a RETURN and that a procedure does
2028 -- not contain any RETURN.
2030 function Disambiguate_Spec return Entity_Id;
2031 -- When a primitive is declared between the private view and the full
2032 -- view of a concurrent type which implements an interface, a special
2033 -- mechanism is used to find the corresponding spec of the primitive
2036 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2037 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2038 -- incomplete types coming from a limited context and swap their limited
2039 -- views with the non-limited ones.
2041 function Is_Private_Concurrent_Primitive
2042 (Subp_Id : Entity_Id) return Boolean;
2043 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2044 -- type that implements an interface and has a private view.
2046 procedure Set_Trivial_Subprogram (N : Node_Id);
2047 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2048 -- subprogram whose body is being analyzed. N is the statement node
2049 -- causing the flag to be set, if the following statement is a return
2050 -- of an entity, we mark the entity as set in source to suppress any
2051 -- warning on the stylized use of function stubs with a dummy return.
2053 procedure Verify_Overriding_Indicator;
2054 -- If there was a previous spec, the entity has been entered in the
2055 -- current scope previously. If the body itself carries an overriding
2056 -- indicator, check that it is consistent with the known status of the
2059 ----------------------------
2060 -- Check_Anonymous_Return --
2061 ----------------------------
2063 procedure Check_Anonymous_Return is
2069 if Present (Spec_Id) then
2075 if Ekind (Scop) = E_Function
2076 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2077 and then not Is_Thunk (Scop)
2078 and then (Has_Task (Designated_Type (Etype (Scop)))
2080 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2082 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2083 and then Expander_Active
2085 -- Avoid cases with no tasking support
2087 and then RTE_Available (RE_Current_Master)
2088 and then not Restriction_Active (No_Task_Hierarchy)
2091 Make_Object_Declaration (Loc,
2092 Defining_Identifier =>
2093 Make_Defining_Identifier (Loc, Name_uMaster),
2094 Constant_Present => True,
2095 Object_Definition =>
2096 New_Reference_To (RTE (RE_Master_Id), Loc),
2098 Make_Explicit_Dereference (Loc,
2099 New_Reference_To (RTE (RE_Current_Master), Loc)));
2101 if Present (Declarations (N)) then
2102 Prepend (Decl, Declarations (N));
2104 Set_Declarations (N, New_List (Decl));
2107 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2108 Set_Has_Master_Entity (Scop);
2110 -- Now mark the containing scope as a task master
2113 while Nkind (Par) /= N_Compilation_Unit loop
2114 Par := Parent (Par);
2115 pragma Assert (Present (Par));
2117 -- If we fall off the top, we are at the outer level, and
2118 -- the environment task is our effective master, so nothing
2122 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2124 Set_Is_Task_Master (Par, True);
2129 end Check_Anonymous_Return;
2131 -------------------------
2132 -- Check_Inline_Pragma --
2133 -------------------------
2135 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2139 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2140 -- True when N is a pragma Inline or Inline_Always that applies
2141 -- to this subprogram.
2143 -----------------------
2144 -- Is_Inline_Pragma --
2145 -----------------------
2147 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2150 Nkind (N) = N_Pragma
2152 (Pragma_Name (N) = Name_Inline_Always
2155 and then Pragma_Name (N) = Name_Inline))
2158 (Expression (First (Pragma_Argument_Associations (N)))) =
2160 end Is_Inline_Pragma;
2162 -- Start of processing for Check_Inline_Pragma
2165 if not Expander_Active then
2169 if Is_List_Member (N)
2170 and then Present (Next (N))
2171 and then Is_Inline_Pragma (Next (N))
2175 elsif Nkind (N) /= N_Subprogram_Body_Stub
2176 and then Present (Declarations (N))
2177 and then Is_Inline_Pragma (First (Declarations (N)))
2179 Prag := First (Declarations (N));
2185 if Present (Prag) then
2186 if Present (Spec_Id) then
2187 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2192 -- Create a subprogram declaration, to make treatment uniform
2195 Subp : constant Entity_Id :=
2196 Make_Defining_Identifier (Loc, Chars (Body_Id));
2197 Decl : constant Node_Id :=
2198 Make_Subprogram_Declaration (Loc,
2200 New_Copy_Tree (Specification (N)));
2203 Set_Defining_Unit_Name (Specification (Decl), Subp);
2205 if Present (First_Formal (Body_Id)) then
2206 Plist := Copy_Parameter_List (Body_Id);
2207 Set_Parameter_Specifications
2208 (Specification (Decl), Plist);
2211 Insert_Before (N, Decl);
2214 Set_Has_Pragma_Inline (Subp);
2216 if Pragma_Name (Prag) = Name_Inline_Always then
2217 Set_Is_Inlined (Subp);
2218 Set_Has_Pragma_Inline_Always (Subp);
2225 end Check_Inline_Pragma;
2227 --------------------------
2228 -- Check_Missing_Return --
2229 --------------------------
2231 procedure Check_Missing_Return is
2233 Missing_Ret : Boolean;
2236 if Nkind (Body_Spec) = N_Function_Specification then
2237 if Present (Spec_Id) then
2243 if Return_Present (Id) then
2244 Check_Returns (HSS, 'F', Missing_Ret);
2247 Set_Has_Missing_Return (Id);
2250 elsif Is_Generic_Subprogram (Id)
2251 or else not Is_Machine_Code_Subprogram (Id)
2253 Error_Msg_N ("missing RETURN statement in function body", N);
2256 -- If procedure with No_Return, check returns
2258 elsif Nkind (Body_Spec) = N_Procedure_Specification
2259 and then Present (Spec_Id)
2260 and then No_Return (Spec_Id)
2262 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2265 -- Special checks in SPARK mode
2267 if Nkind (Body_Spec) = N_Function_Specification then
2269 -- In SPARK mode, last statement of a function should be a return
2272 Stat : constant Node_Id := Last_Source_Statement (HSS);
2275 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2276 N_Extended_Return_Statement)
2278 Check_SPARK_Restriction
2279 ("last statement in function should be RETURN", Stat);
2283 -- In SPARK mode, verify that a procedure has no return
2285 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2286 if Present (Spec_Id) then
2292 -- Would be nice to point to return statement here, can we
2293 -- borrow the Check_Returns procedure here ???
2295 if Return_Present (Id) then
2296 Check_SPARK_Restriction
2297 ("procedure should not have RETURN", N);
2300 end Check_Missing_Return;
2302 -----------------------
2303 -- Disambiguate_Spec --
2304 -----------------------
2306 function Disambiguate_Spec return Entity_Id is
2307 Priv_Spec : Entity_Id;
2310 procedure Replace_Types (To_Corresponding : Boolean);
2311 -- Depending on the flag, replace the type of formal parameters of
2312 -- Body_Id if it is a concurrent type implementing interfaces with
2313 -- the corresponding record type or the other way around.
2315 procedure Replace_Types (To_Corresponding : Boolean) is
2317 Formal_Typ : Entity_Id;
2320 Formal := First_Formal (Body_Id);
2321 while Present (Formal) loop
2322 Formal_Typ := Etype (Formal);
2324 if Is_Class_Wide_Type (Formal_Typ) then
2325 Formal_Typ := Root_Type (Formal_Typ);
2328 -- From concurrent type to corresponding record
2330 if To_Corresponding then
2331 if Is_Concurrent_Type (Formal_Typ)
2332 and then Present (Corresponding_Record_Type (Formal_Typ))
2333 and then Present (Interfaces (
2334 Corresponding_Record_Type (Formal_Typ)))
2337 Corresponding_Record_Type (Formal_Typ));
2340 -- From corresponding record to concurrent type
2343 if Is_Concurrent_Record_Type (Formal_Typ)
2344 and then Present (Interfaces (Formal_Typ))
2347 Corresponding_Concurrent_Type (Formal_Typ));
2351 Next_Formal (Formal);
2355 -- Start of processing for Disambiguate_Spec
2358 -- Try to retrieve the specification of the body as is. All error
2359 -- messages are suppressed because the body may not have a spec in
2360 -- its current state.
2362 Spec_N := Find_Corresponding_Spec (N, False);
2364 -- It is possible that this is the body of a primitive declared
2365 -- between a private and a full view of a concurrent type. The
2366 -- controlling parameter of the spec carries the concurrent type,
2367 -- not the corresponding record type as transformed by Analyze_
2368 -- Subprogram_Specification. In such cases, we undo the change
2369 -- made by the analysis of the specification and try to find the
2372 -- Note that wrappers already have their corresponding specs and
2373 -- bodies set during their creation, so if the candidate spec is
2374 -- a wrapper, then we definitely need to swap all types to their
2375 -- original concurrent status.
2378 or else Is_Primitive_Wrapper (Spec_N)
2380 -- Restore all references of corresponding record types to the
2381 -- original concurrent types.
2383 Replace_Types (To_Corresponding => False);
2384 Priv_Spec := Find_Corresponding_Spec (N, False);
2386 -- The current body truly belongs to a primitive declared between
2387 -- a private and a full view. We leave the modified body as is,
2388 -- and return the true spec.
2390 if Present (Priv_Spec)
2391 and then Is_Private_Primitive (Priv_Spec)
2396 -- In case that this is some sort of error, restore the original
2397 -- state of the body.
2399 Replace_Types (To_Corresponding => True);
2403 end Disambiguate_Spec;
2405 ----------------------------
2406 -- Exchange_Limited_Views --
2407 ----------------------------
2409 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2410 procedure Detect_And_Exchange (Id : Entity_Id);
2411 -- Determine whether Id's type denotes an incomplete type associated
2412 -- with a limited with clause and exchange the limited view with the
2415 -------------------------
2416 -- Detect_And_Exchange --
2417 -------------------------
2419 procedure Detect_And_Exchange (Id : Entity_Id) is
2420 Typ : constant Entity_Id := Etype (Id);
2423 if Ekind (Typ) = E_Incomplete_Type
2424 and then From_With_Type (Typ)
2425 and then Present (Non_Limited_View (Typ))
2427 Set_Etype (Id, Non_Limited_View (Typ));
2429 end Detect_And_Exchange;
2435 -- Start of processing for Exchange_Limited_Views
2438 if No (Subp_Id) then
2441 -- Do not process subprogram bodies as they already use the non-
2442 -- limited view of types.
2444 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2448 -- Examine all formals and swap views when applicable
2450 Formal := First_Formal (Subp_Id);
2451 while Present (Formal) loop
2452 Detect_And_Exchange (Formal);
2454 Next_Formal (Formal);
2457 -- Process the return type of a function
2459 if Ekind (Subp_Id) = E_Function then
2460 Detect_And_Exchange (Subp_Id);
2462 end Exchange_Limited_Views;
2464 -------------------------------------
2465 -- Is_Private_Concurrent_Primitive --
2466 -------------------------------------
2468 function Is_Private_Concurrent_Primitive
2469 (Subp_Id : Entity_Id) return Boolean
2471 Formal_Typ : Entity_Id;
2474 if Present (First_Formal (Subp_Id)) then
2475 Formal_Typ := Etype (First_Formal (Subp_Id));
2477 if Is_Concurrent_Record_Type (Formal_Typ) then
2478 if Is_Class_Wide_Type (Formal_Typ) then
2479 Formal_Typ := Root_Type (Formal_Typ);
2482 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2485 -- The type of the first formal is a concurrent tagged type with
2489 Is_Concurrent_Type (Formal_Typ)
2490 and then Is_Tagged_Type (Formal_Typ)
2491 and then Has_Private_Declaration (Formal_Typ);
2495 end Is_Private_Concurrent_Primitive;
2497 ----------------------------
2498 -- Set_Trivial_Subprogram --
2499 ----------------------------
2501 procedure Set_Trivial_Subprogram (N : Node_Id) is
2502 Nxt : constant Node_Id := Next (N);
2505 Set_Is_Trivial_Subprogram (Body_Id);
2507 if Present (Spec_Id) then
2508 Set_Is_Trivial_Subprogram (Spec_Id);
2512 and then Nkind (Nxt) = N_Simple_Return_Statement
2513 and then No (Next (Nxt))
2514 and then Present (Expression (Nxt))
2515 and then Is_Entity_Name (Expression (Nxt))
2517 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2519 end Set_Trivial_Subprogram;
2521 ---------------------------------
2522 -- Verify_Overriding_Indicator --
2523 ---------------------------------
2525 procedure Verify_Overriding_Indicator is
2527 if Must_Override (Body_Spec) then
2528 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2529 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2533 elsif not Present (Overridden_Operation (Spec_Id)) then
2535 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2538 elsif Must_Not_Override (Body_Spec) then
2539 if Present (Overridden_Operation (Spec_Id)) then
2541 ("subprogram& overrides inherited operation",
2542 Body_Spec, Spec_Id);
2544 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2545 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2548 ("subprogram & overrides predefined operator ",
2549 Body_Spec, Spec_Id);
2551 -- If this is not a primitive operation or protected subprogram,
2552 -- then the overriding indicator is altogether illegal.
2554 elsif not Is_Primitive (Spec_Id)
2555 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2558 ("overriding indicator only allowed " &
2559 "if subprogram is primitive",
2564 and then Present (Overridden_Operation (Spec_Id))
2566 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2567 Style.Missing_Overriding (N, Body_Id);
2570 and then Can_Override_Operator (Spec_Id)
2571 and then not Is_Predefined_File_Name
2572 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2574 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2575 Style.Missing_Overriding (N, Body_Id);
2577 end Verify_Overriding_Indicator;
2579 -- Start of processing for Analyze_Subprogram_Body_Helper
2582 -- Generic subprograms are handled separately. They always have a
2583 -- generic specification. Determine whether current scope has a
2584 -- previous declaration.
2586 -- If the subprogram body is defined within an instance of the same
2587 -- name, the instance appears as a package renaming, and will be hidden
2588 -- within the subprogram.
2590 if Present (Prev_Id)
2591 and then not Is_Overloadable (Prev_Id)
2592 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2593 or else Comes_From_Source (Prev_Id))
2595 if Is_Generic_Subprogram (Prev_Id) then
2597 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2598 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2600 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2602 if Nkind (N) = N_Subprogram_Body then
2603 HSS := Handled_Statement_Sequence (N);
2604 Check_Missing_Return;
2610 -- Previous entity conflicts with subprogram name. Attempting to
2611 -- enter name will post error.
2613 Enter_Name (Body_Id);
2617 -- Non-generic case, find the subprogram declaration, if one was seen,
2618 -- or enter new overloaded entity in the current scope. If the
2619 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2620 -- part of the context of one of its subunits. No need to redo the
2623 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2627 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2629 if Nkind (N) = N_Subprogram_Body_Stub
2630 or else No (Corresponding_Spec (N))
2632 if Is_Private_Concurrent_Primitive (Body_Id) then
2633 Spec_Id := Disambiguate_Spec;
2635 Spec_Id := Find_Corresponding_Spec (N);
2638 -- If this is a duplicate body, no point in analyzing it
2640 if Error_Posted (N) then
2644 -- A subprogram body should cause freezing of its own declaration,
2645 -- but if there was no previous explicit declaration, then the
2646 -- subprogram will get frozen too late (there may be code within
2647 -- the body that depends on the subprogram having been frozen,
2648 -- such as uses of extra formals), so we force it to be frozen
2649 -- here. Same holds if the body and spec are compilation units.
2650 -- Finally, if the return type is an anonymous access to protected
2651 -- subprogram, it must be frozen before the body because its
2652 -- expansion has generated an equivalent type that is used when
2653 -- elaborating the body.
2655 -- An exception in the case of Ada 2012, AI05-177: The bodies
2656 -- created for expression functions do not freeze.
2659 and then Nkind (Original_Node (N)) /= N_Expression_Function
2661 Freeze_Before (N, Body_Id);
2663 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2664 Freeze_Before (N, Spec_Id);
2666 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2667 Freeze_Before (N, Etype (Body_Id));
2671 Spec_Id := Corresponding_Spec (N);
2675 -- Language-defined aspects cannot appear in a subprogram body [stub] if
2676 -- the corresponding spec already has aspects. An exception to this rule
2677 -- are certain user-defined aspects.
2679 if Has_Aspects (N) then
2680 if Present (Spec_Id)
2681 and then not Aspects_On_Body_Or_Stub_OK (N)
2683 -- Do not emit an error on a subprogram body stub that act as
2686 and then Nkind (Parent (Parent (Spec_Id))) /= N_Subprogram_Body_Stub
2689 ("aspect specifications must appear in subprogram declaration",
2692 -- Delay the analysis of aspect specifications that apply to a body
2693 -- stub until the proper body is analyzed. If the corresponding body
2694 -- is missing, the aspects are still analyzed in Analyze_Proper_Body.
2696 elsif Nkind (N) in N_Body_Stub then
2700 Analyze_Aspect_Specifications (N, Body_Id);
2704 -- Previously we scanned the body to look for nested subprograms, and
2705 -- rejected an inline directive if nested subprograms were present,
2706 -- because the back-end would generate conflicting symbols for the
2707 -- nested bodies. This is now unnecessary.
2709 -- Look ahead to recognize a pragma Inline that appears after the body
2711 Check_Inline_Pragma (Spec_Id);
2713 -- Deal with special case of a fully private operation in the body of
2714 -- the protected type. We must create a declaration for the subprogram,
2715 -- in order to attach the protected subprogram that will be used in
2716 -- internal calls. We exclude compiler generated bodies from the
2717 -- expander since the issue does not arise for those cases.
2720 and then Comes_From_Source (N)
2721 and then Is_Protected_Type (Current_Scope)
2723 Spec_Id := Build_Private_Protected_Declaration (N);
2726 -- If a separate spec is present, then deal with freezing issues
2728 if Present (Spec_Id) then
2729 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2730 Verify_Overriding_Indicator;
2732 -- In general, the spec will be frozen when we start analyzing the
2733 -- body. However, for internally generated operations, such as
2734 -- wrapper functions for inherited operations with controlling
2735 -- results, the spec may not have been frozen by the time we expand
2736 -- the freeze actions that include the bodies. In particular, extra
2737 -- formals for accessibility or for return-in-place may need to be
2738 -- generated. Freeze nodes, if any, are inserted before the current
2739 -- body. These freeze actions are also needed in ASIS mode to enable
2740 -- the proper back-annotations.
2742 if not Is_Frozen (Spec_Id)
2743 and then (Expander_Active or ASIS_Mode)
2745 -- Force the generation of its freezing node to ensure proper
2746 -- management of access types in the backend.
2748 -- This is definitely needed for some cases, but it is not clear
2749 -- why, to be investigated further???
2751 Set_Has_Delayed_Freeze (Spec_Id);
2752 Freeze_Before (N, Spec_Id);
2756 -- Mark presence of postcondition procedure in current scope and mark
2757 -- the procedure itself as needing debug info. The latter is important
2758 -- when analyzing decision coverage (for example, for MC/DC coverage).
2760 if Chars (Body_Id) = Name_uPostconditions then
2761 Set_Has_Postconditions (Current_Scope);
2762 Set_Debug_Info_Needed (Body_Id);
2765 -- Place subprogram on scope stack, and make formals visible. If there
2766 -- is a spec, the visible entity remains that of the spec.
2768 if Present (Spec_Id) then
2769 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2771 if Is_Child_Unit (Spec_Id) then
2772 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2776 Style.Check_Identifier (Body_Id, Spec_Id);
2779 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2780 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2782 if Is_Abstract_Subprogram (Spec_Id) then
2783 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2787 Set_Convention (Body_Id, Convention (Spec_Id));
2788 Set_Has_Completion (Spec_Id);
2790 if Is_Protected_Type (Scope (Spec_Id)) then
2791 Prot_Typ := Scope (Spec_Id);
2794 -- If this is a body generated for a renaming, do not check for
2795 -- full conformance. The check is redundant, because the spec of
2796 -- the body is a copy of the spec in the renaming declaration,
2797 -- and the test can lead to spurious errors on nested defaults.
2799 if Present (Spec_Decl)
2800 and then not Comes_From_Source (N)
2802 (Nkind (Original_Node (Spec_Decl)) =
2803 N_Subprogram_Renaming_Declaration
2804 or else (Present (Corresponding_Body (Spec_Decl))
2806 Nkind (Unit_Declaration_Node
2807 (Corresponding_Body (Spec_Decl))) =
2808 N_Subprogram_Renaming_Declaration))
2812 -- Conversely, the spec may have been generated for specless body
2813 -- with an inline pragma.
2815 elsif Comes_From_Source (N)
2816 and then not Comes_From_Source (Spec_Id)
2817 and then Has_Pragma_Inline (Spec_Id)
2824 Fully_Conformant, True, Conformant, Body_Id);
2827 -- If the body is not fully conformant, we have to decide if we
2828 -- should analyze it or not. If it has a really messed up profile
2829 -- then we probably should not analyze it, since we will get too
2830 -- many bogus messages.
2832 -- Our decision is to go ahead in the non-fully conformant case
2833 -- only if it is at least mode conformant with the spec. Note
2834 -- that the call to Check_Fully_Conformant has issued the proper
2835 -- error messages to complain about the lack of conformance.
2838 and then not Mode_Conformant (Body_Id, Spec_Id)
2844 if Spec_Id /= Body_Id then
2845 Reference_Body_Formals (Spec_Id, Body_Id);
2848 if Nkind (N) = N_Subprogram_Body_Stub then
2849 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
2854 Set_Corresponding_Spec (N, Spec_Id);
2856 -- Ada 2005 (AI-345): If the operation is a primitive operation
2857 -- of a concurrent type, the type of the first parameter has been
2858 -- replaced with the corresponding record, which is the proper
2859 -- run-time structure to use. However, within the body there may
2860 -- be uses of the formals that depend on primitive operations
2861 -- of the type (in particular calls in prefixed form) for which
2862 -- we need the original concurrent type. The operation may have
2863 -- several controlling formals, so the replacement must be done
2866 if Comes_From_Source (Spec_Id)
2867 and then Present (First_Entity (Spec_Id))
2868 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2869 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2870 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
2871 and then Present (Corresponding_Concurrent_Type
2872 (Etype (First_Entity (Spec_Id))))
2875 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2879 Form := First_Formal (Spec_Id);
2880 while Present (Form) loop
2881 if Etype (Form) = Typ then
2882 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2890 -- Make the formals visible, and place subprogram on scope stack.
2891 -- This is also the point at which we set Last_Real_Spec_Entity
2892 -- to mark the entities which will not be moved to the body.
2894 Install_Formals (Spec_Id);
2895 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2897 -- Within an instance, add local renaming declarations so that
2898 -- gdb can retrieve the values of actuals more easily. This is
2899 -- only relevant if generating code (and indeed we definitely
2900 -- do not want these definitions -gnatc mode, because that would
2903 if Is_Generic_Instance (Spec_Id)
2904 and then Is_Wrapper_Package (Current_Scope)
2905 and then Expander_Active
2907 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2910 Push_Scope (Spec_Id);
2912 -- Make sure that the subprogram is immediately visible. For
2913 -- child units that have no separate spec this is indispensable.
2914 -- Otherwise it is safe albeit redundant.
2916 Set_Is_Immediately_Visible (Spec_Id);
2919 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2920 Set_Contract (Body_Id, Empty);
2921 Set_Ekind (Body_Id, E_Subprogram_Body);
2922 Set_Scope (Body_Id, Scope (Spec_Id));
2923 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2925 -- Case of subprogram body with no previous spec
2928 -- Check for style warning required
2932 -- Only apply check for source level subprograms for which checks
2933 -- have not been suppressed.
2935 and then Comes_From_Source (Body_Id)
2936 and then not Suppress_Style_Checks (Body_Id)
2938 -- No warnings within an instance
2940 and then not In_Instance
2942 -- No warnings for expression functions
2944 and then Nkind (Original_Node (N)) /= N_Expression_Function
2946 Style.Body_With_No_Spec (N);
2949 New_Overloaded_Entity (Body_Id);
2951 if Nkind (N) /= N_Subprogram_Body_Stub then
2952 Set_Acts_As_Spec (N);
2953 Generate_Definition (Body_Id);
2954 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
2956 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2957 Install_Formals (Body_Id);
2958 Push_Scope (Body_Id);
2961 -- For stubs and bodies with no previous spec, generate references to
2964 Generate_Reference_To_Formals (Body_Id);
2967 -- If the return type is an anonymous access type whose designated type
2968 -- is the limited view of a class-wide type and the non-limited view is
2969 -- available, update the return type accordingly.
2971 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
2977 Rtyp := Etype (Current_Scope);
2979 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2980 Etyp := Directly_Designated_Type (Rtyp);
2982 if Is_Class_Wide_Type (Etyp) and then From_With_Type (Etyp) then
2983 Set_Directly_Designated_Type
2984 (Etype (Current_Scope), Available_View (Etyp));
2990 -- If this is the proper body of a stub, we must verify that the stub
2991 -- conforms to the body, and to the previous spec if one was present.
2992 -- We know already that the body conforms to that spec. This test is
2993 -- only required for subprograms that come from source.
2995 if Nkind (Parent (N)) = N_Subunit
2996 and then Comes_From_Source (N)
2997 and then not Error_Posted (Body_Id)
2998 and then Nkind (Corresponding_Stub (Parent (N))) =
2999 N_Subprogram_Body_Stub
3002 Old_Id : constant Entity_Id :=
3004 (Specification (Corresponding_Stub (Parent (N))));
3006 Conformant : Boolean := False;
3009 if No (Spec_Id) then
3010 Check_Fully_Conformant (Body_Id, Old_Id);
3014 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3016 if not Conformant then
3018 -- The stub was taken to be a new declaration. Indicate that
3021 Set_Has_Completion (Old_Id, False);
3027 Set_Has_Completion (Body_Id);
3028 Check_Eliminated (Body_Id);
3030 if Nkind (N) = N_Subprogram_Body_Stub then
3034 -- Handle frontend inlining. There is no need to prepare us for inlining
3035 -- if we will not generate the code.
3039 if not Debug_Flag_Dot_K then
3040 if Present (Spec_Id)
3041 and then Expander_Active
3043 (Has_Pragma_Inline_Always (Spec_Id)
3044 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3046 Build_Body_To_Inline (N, Spec_Id);
3051 elsif Expander_Active
3052 and then Serious_Errors_Detected = 0
3053 and then Present (Spec_Id)
3054 and then Has_Pragma_Inline (Spec_Id)
3056 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3059 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3060 -- of the specification we have to install the private withed units.
3061 -- This holds for child units as well.
3063 if Is_Compilation_Unit (Body_Id)
3064 or else Nkind (Parent (N)) = N_Compilation_Unit
3066 Install_Private_With_Clauses (Body_Id);
3069 Check_Anonymous_Return;
3071 -- Set the Protected_Formal field of each extra formal of the protected
3072 -- subprogram to reference the corresponding extra formal of the
3073 -- subprogram that implements it. For regular formals this occurs when
3074 -- the protected subprogram's declaration is expanded, but the extra
3075 -- formals don't get created until the subprogram is frozen. We need to
3076 -- do this before analyzing the protected subprogram's body so that any
3077 -- references to the original subprogram's extra formals will be changed
3078 -- refer to the implementing subprogram's formals (see Expand_Formal).
3080 if Present (Spec_Id)
3081 and then Is_Protected_Type (Scope (Spec_Id))
3082 and then Present (Protected_Body_Subprogram (Spec_Id))
3085 Impl_Subp : constant Entity_Id :=
3086 Protected_Body_Subprogram (Spec_Id);
3087 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3088 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3090 while Present (Prot_Ext_Formal) loop
3091 pragma Assert (Present (Impl_Ext_Formal));
3092 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3093 Next_Formal_With_Extras (Prot_Ext_Formal);
3094 Next_Formal_With_Extras (Impl_Ext_Formal);
3099 -- Now we can go on to analyze the body
3101 HSS := Handled_Statement_Sequence (N);
3102 Set_Actual_Subtypes (N, Current_Scope);
3104 -- Deal with preconditions and postconditions. In formal verification
3105 -- mode, we keep pre- and postconditions attached to entities rather
3106 -- than inserted in the code, in order to facilitate a distinct
3107 -- treatment for them.
3109 if not SPARK_Mode then
3110 Process_PPCs (N, Spec_Id, Body_Id);
3113 -- Add a declaration for the Protection object, renaming declarations
3114 -- for discriminals and privals and finally a declaration for the entry
3115 -- family index (if applicable). This form of early expansion is done
3116 -- when the Expander is active because Install_Private_Data_Declarations
3117 -- references entities which were created during regular expansion. The
3118 -- body may be the rewritting of an expression function, and we need to
3119 -- verify that the original node is in the source.
3121 if Full_Expander_Active
3122 and then Comes_From_Source (Original_Node (N))
3123 and then Present (Prot_Typ)
3124 and then Present (Spec_Id)
3125 and then not Is_Eliminated (Spec_Id)
3127 Install_Private_Data_Declarations
3128 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3131 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3132 -- may now appear in parameter and result profiles. Since the analysis
3133 -- of a subprogram body may use the parameter and result profile of the
3134 -- spec, swap any limited views with their non-limited counterpart.
3136 if Ada_Version >= Ada_2012 then
3137 Exchange_Limited_Views (Spec_Id);
3140 -- Analyze the declarations (this call will analyze the precondition
3141 -- Check pragmas we prepended to the list, as well as the declaration
3142 -- of the _Postconditions procedure).
3144 Analyze_Declarations (Declarations (N));
3146 -- Check completion, and analyze the statements
3149 Inspect_Deferred_Constant_Completion (Declarations (N));
3152 -- Deal with end of scope processing for the body
3154 Process_End_Label (HSS, 't', Current_Scope);
3156 Check_Subprogram_Order (N);
3157 Set_Analyzed (Body_Id);
3159 -- If we have a separate spec, then the analysis of the declarations
3160 -- caused the entities in the body to be chained to the spec id, but
3161 -- we want them chained to the body id. Only the formal parameters
3162 -- end up chained to the spec id in this case.
3164 if Present (Spec_Id) then
3166 -- We must conform to the categorization of our spec
3168 Validate_Categorization_Dependency (N, Spec_Id);
3170 -- And if this is a child unit, the parent units must conform
3172 if Is_Child_Unit (Spec_Id) then
3173 Validate_Categorization_Dependency
3174 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3177 -- Here is where we move entities from the spec to the body
3179 -- Case where there are entities that stay with the spec
3181 if Present (Last_Real_Spec_Entity) then
3183 -- No body entities (happens when the only real spec entities come
3184 -- from precondition and postcondition pragmas).
3186 if No (Last_Entity (Body_Id)) then
3188 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3190 -- Body entities present (formals), so chain stuff past them
3194 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3197 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3198 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3199 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3201 -- Case where there are no spec entities, in this case there can be
3202 -- no body entities either, so just move everything.
3205 pragma Assert (No (Last_Entity (Body_Id)));
3206 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3207 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3208 Set_First_Entity (Spec_Id, Empty);
3209 Set_Last_Entity (Spec_Id, Empty);
3213 Check_Missing_Return;
3215 -- Now we are going to check for variables that are never modified in
3216 -- the body of the procedure. But first we deal with a special case
3217 -- where we want to modify this check. If the body of the subprogram
3218 -- starts with a raise statement or its equivalent, or if the body
3219 -- consists entirely of a null statement, then it is pretty obvious
3220 -- that it is OK to not reference the parameters. For example, this
3221 -- might be the following common idiom for a stubbed function:
3222 -- statement of the procedure raises an exception. In particular this
3223 -- deals with the common idiom of a stubbed function, which might
3224 -- appear as something like:
3226 -- function F (A : Integer) return Some_Type;
3229 -- raise Program_Error;
3233 -- Here the purpose of X is simply to satisfy the annoying requirement
3234 -- in Ada that there be at least one return, and we certainly do not
3235 -- want to go posting warnings on X that it is not initialized! On
3236 -- the other hand, if X is entirely unreferenced that should still
3239 -- What we do is to detect these cases, and if we find them, flag the
3240 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3241 -- suppress unwanted warnings. For the case of the function stub above
3242 -- we have a special test to set X as apparently assigned to suppress
3249 -- Skip initial labels (for one thing this occurs when we are in
3250 -- front end ZCX mode, but in any case it is irrelevant), and also
3251 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3253 Stm := First (Statements (HSS));
3254 while Nkind (Stm) = N_Label
3255 or else Nkind (Stm) in N_Push_xxx_Label
3260 -- Do the test on the original statement before expansion
3263 Ostm : constant Node_Id := Original_Node (Stm);
3266 -- If explicit raise statement, turn on flag
3268 if Nkind (Ostm) = N_Raise_Statement then
3269 Set_Trivial_Subprogram (Stm);
3271 -- If null statement, and no following statements, turn on flag
3273 elsif Nkind (Stm) = N_Null_Statement
3274 and then Comes_From_Source (Stm)
3275 and then No (Next (Stm))
3277 Set_Trivial_Subprogram (Stm);
3279 -- Check for explicit call cases which likely raise an exception
3281 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3282 if Is_Entity_Name (Name (Ostm)) then
3284 Ent : constant Entity_Id := Entity (Name (Ostm));
3287 -- If the procedure is marked No_Return, then likely it
3288 -- raises an exception, but in any case it is not coming
3289 -- back here, so turn on the flag.
3292 and then Ekind (Ent) = E_Procedure
3293 and then No_Return (Ent)
3295 Set_Trivial_Subprogram (Stm);
3303 -- Check for variables that are never modified
3309 -- If there is a separate spec, then transfer Never_Set_In_Source
3310 -- flags from out parameters to the corresponding entities in the
3311 -- body. The reason we do that is we want to post error flags on
3312 -- the body entities, not the spec entities.
3314 if Present (Spec_Id) then
3315 E1 := First_Entity (Spec_Id);
3316 while Present (E1) loop
3317 if Ekind (E1) = E_Out_Parameter then
3318 E2 := First_Entity (Body_Id);
3319 while Present (E2) loop
3320 exit when Chars (E1) = Chars (E2);
3324 if Present (E2) then
3325 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3333 -- Check references in body
3335 Check_References (Body_Id);
3337 end Analyze_Subprogram_Body_Helper;
3339 ---------------------------------
3340 -- Analyze_Subprogram_Contract --
3341 ---------------------------------
3343 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3344 Result_Seen : Boolean := False;
3345 -- A flag which keeps track of whether at least one postcondition or
3346 -- contract-case mentions attribute 'Result (set True if so).
3348 procedure Check_Result_And_Post_State
3350 Error_Nod : in out Node_Id);
3351 -- Determine whether pragma Prag mentions attribute 'Result and whether
3352 -- the pragma contains an expression that evaluates differently in pre-
3353 -- and post-state. Prag is a postcondition or a contract-cases pragma.
3354 -- Error_Nod denotes the proper error node.
3356 ---------------------------------
3357 -- Check_Result_And_Post_State --
3358 ---------------------------------
3360 procedure Check_Result_And_Post_State
3362 Error_Nod : in out Node_Id)
3364 procedure Check_Expression (Expr : Node_Id);
3365 -- Perform the 'Result and post-state checks on a given expression
3367 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3368 -- Attempt to find attribute 'Result in a subtree denoted by N
3370 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3371 -- Determine whether source node N denotes "True" or "False"
3373 function Mentions_Post_State (N : Node_Id) return Boolean;
3374 -- Determine whether a subtree denoted by N mentions any construct
3375 -- that denotes a post-state.
3377 procedure Check_Function_Result is
3378 new Traverse_Proc (Is_Function_Result);
3380 ----------------------
3381 -- Check_Expression --
3382 ----------------------
3384 procedure Check_Expression (Expr : Node_Id) is
3386 if not Is_Trivial_Boolean (Expr) then
3387 Check_Function_Result (Expr);
3389 if not Mentions_Post_State (Expr) then
3390 if Pragma_Name (Prag) = Name_Contract_Cases then
3392 ("contract case refers only to pre-state?T?", Expr);
3395 ("postcondition refers only to pre-state?T?", Prag);
3399 end Check_Expression;
3401 ------------------------
3402 -- Is_Function_Result --
3403 ------------------------
3405 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3407 if Nkind (N) = N_Attribute_Reference
3408 and then Attribute_Name (N) = Name_Result
3410 Result_Seen := True;
3413 -- Continue the traversal
3418 end Is_Function_Result;
3420 ------------------------
3421 -- Is_Trivial_Boolean --
3422 ------------------------
3424 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3427 Comes_From_Source (N)
3428 and then Is_Entity_Name (N)
3429 and then (Entity (N) = Standard_True
3430 or else Entity (N) = Standard_False);
3431 end Is_Trivial_Boolean;
3433 -------------------------
3434 -- Mentions_Post_State --
3435 -------------------------
3437 function Mentions_Post_State (N : Node_Id) return Boolean is
3438 Post_State_Seen : Boolean := False;
3440 function Is_Post_State (N : Node_Id) return Traverse_Result;
3441 -- Attempt to find a construct that denotes a post-state. If this
3442 -- is the case, set flag Post_State_Seen.
3448 function Is_Post_State (N : Node_Id) return Traverse_Result is
3452 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3453 Post_State_Seen := True;
3456 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3459 if No (Ent) or else Ekind (Ent) in Assignable_Kind then
3460 Post_State_Seen := True;
3464 elsif Nkind (N) = N_Attribute_Reference then
3465 if Attribute_Name (N) = Name_Old then
3467 elsif Attribute_Name (N) = Name_Result then
3468 Post_State_Seen := True;
3476 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3478 -- Start of processing for Mentions_Post_State
3481 Find_Post_State (N);
3482 return Post_State_Seen;
3483 end Mentions_Post_State;
3487 Expr : constant Node_Id :=
3488 Expression (First (Pragma_Argument_Associations (Prag)));
3489 Nam : constant Name_Id := Pragma_Name (Prag);
3492 -- Start of processing for Check_Result_And_Post_State
3495 if No (Error_Nod) then
3499 -- Examine all consequences
3501 if Nam = Name_Contract_Cases then
3502 CCase := First (Component_Associations (Expr));
3503 while Present (CCase) loop
3504 Check_Expression (Expression (CCase));
3509 -- Examine the expression of a postcondition
3512 pragma Assert (Nam = Name_Postcondition);
3513 Check_Expression (Expr);
3515 end Check_Result_And_Post_State;
3519 Items : constant Node_Id := Contract (Subp);
3520 Error_CCase : Node_Id;
3521 Error_Post : Node_Id;
3524 -- Start of processing for Analyze_Subprogram_Contract
3527 Error_CCase := Empty;
3528 Error_Post := Empty;
3530 if Present (Items) then
3532 -- Analyze pre- and postconditions
3534 Prag := Pre_Post_Conditions (Items);
3535 while Present (Prag) loop
3536 Analyze_PPC_In_Decl_Part (Prag, Subp);
3538 -- Verify whether a postcondition mentions attribute 'Result and
3539 -- its expression introduces a post-state.
3541 if Warn_On_Suspicious_Contract
3542 and then Pragma_Name (Prag) = Name_Postcondition
3544 Check_Result_And_Post_State (Prag, Error_Post);
3547 Prag := Next_Pragma (Prag);
3550 -- Analyze contract-cases and test-cases
3552 Prag := Contract_Test_Cases (Items);
3553 while Present (Prag) loop
3554 if Pragma_Name (Prag) = Name_Contract_Cases then
3555 Analyze_Contract_Cases_In_Decl_Part (Prag);
3557 -- Verify whether contract-cases mention attribute 'Result and
3558 -- its expression introduces a post-state. Perform the check
3559 -- only when the pragma is legal.
3561 if Warn_On_Suspicious_Contract
3562 and then not Error_Posted (Prag)
3564 Check_Result_And_Post_State (Prag, Error_CCase);
3568 pragma Assert (Pragma_Name (Prag) = Name_Test_Case);
3569 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3572 Prag := Next_Pragma (Prag);
3575 -- Analyze classification pragmas
3577 Prag := Classifications (Contract (Subp));
3578 while Present (Prag) loop
3579 if Pragma_Name (Prag) = Name_Depends then
3580 Analyze_Depends_In_Decl_Part (Prag);
3582 pragma Assert (Pragma_Name (Prag) = Name_Global);
3583 Analyze_Global_In_Decl_Part (Prag);
3586 Prag := Next_Pragma (Prag);
3590 -- Emit an error when none of the postconditions or contract-cases
3591 -- mention attribute 'Result in the context of a function.
3593 if Warn_On_Suspicious_Contract
3594 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3595 and then not Result_Seen
3597 if Present (Error_Post) and then Present (Error_CCase) then
3599 ("neither function postcondition nor contract cases mention "
3600 & "result?T?", Error_Post);
3602 elsif Present (Error_Post) then
3604 ("function postcondition does not mention result?T?",
3607 elsif Present (Error_CCase) then
3609 ("contract cases do not mention result?T?", Error_CCase);
3612 end Analyze_Subprogram_Contract;
3614 ------------------------------------
3615 -- Analyze_Subprogram_Declaration --
3616 ------------------------------------
3618 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3619 Scop : constant Entity_Id := Current_Scope;
3620 Designator : Entity_Id;
3621 Is_Completion : Boolean;
3622 -- Indicates whether a null procedure declaration is a completion
3625 -- Null procedures are not allowed in SPARK
3627 if Nkind (Specification (N)) = N_Procedure_Specification
3628 and then Null_Present (Specification (N))
3630 Check_SPARK_Restriction ("null procedure is not allowed", N);
3632 if Is_Protected_Type (Current_Scope) then
3633 Error_Msg_N ("protected operation cannot be a null procedure", N);
3636 Analyze_Null_Procedure (N, Is_Completion);
3638 if Is_Completion then
3640 -- The null procedure acts as a body, nothing further is needed.
3646 Designator := Analyze_Subprogram_Specification (Specification (N));
3648 -- A reference may already have been generated for the unit name, in
3649 -- which case the following call is redundant. However it is needed for
3650 -- declarations that are the rewriting of an expression function.
3652 Generate_Definition (Designator);
3654 if Debug_Flag_C then
3655 Write_Str ("==> subprogram spec ");
3656 Write_Name (Chars (Designator));
3657 Write_Str (" from ");
3658 Write_Location (Sloc (N));
3663 Validate_RCI_Subprogram_Declaration (N);
3664 New_Overloaded_Entity (Designator);
3665 Check_Delayed_Subprogram (Designator);
3667 -- If the type of the first formal of the current subprogram is a
3668 -- non-generic tagged private type, mark the subprogram as being a
3669 -- private primitive. Ditto if this is a function with controlling
3670 -- result, and the return type is currently private. In both cases,
3671 -- the type of the controlling argument or result must be in the
3672 -- current scope for the operation to be primitive.
3674 if Has_Controlling_Result (Designator)
3675 and then Is_Private_Type (Etype (Designator))
3676 and then Scope (Etype (Designator)) = Current_Scope
3677 and then not Is_Generic_Actual_Type (Etype (Designator))
3679 Set_Is_Private_Primitive (Designator);
3681 elsif Present (First_Formal (Designator)) then
3683 Formal_Typ : constant Entity_Id :=
3684 Etype (First_Formal (Designator));
3686 Set_Is_Private_Primitive (Designator,
3687 Is_Tagged_Type (Formal_Typ)
3688 and then Scope (Formal_Typ) = Current_Scope
3689 and then Is_Private_Type (Formal_Typ)
3690 and then not Is_Generic_Actual_Type (Formal_Typ));
3694 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3697 if Ada_Version >= Ada_2005
3698 and then Comes_From_Source (N)
3699 and then Is_Dispatching_Operation (Designator)
3706 if Has_Controlling_Result (Designator) then
3707 Etyp := Etype (Designator);
3710 E := First_Entity (Designator);
3712 and then Is_Formal (E)
3713 and then not Is_Controlling_Formal (E)
3721 if Is_Access_Type (Etyp) then
3722 Etyp := Directly_Designated_Type (Etyp);
3725 if Is_Interface (Etyp)
3726 and then not Is_Abstract_Subprogram (Designator)
3727 and then not (Ekind (Designator) = E_Procedure
3728 and then Null_Present (Specification (N)))
3730 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3732 -- Specialize error message based on procedures vs. functions,
3733 -- since functions can't be null subprograms.
3735 if Ekind (Designator) = E_Procedure then
3737 ("interface procedure % must be abstract or null", N);
3739 Error_Msg_N ("interface function % must be abstract", N);
3745 -- What is the following code for, it used to be
3747 -- ??? Set_Suppress_Elaboration_Checks
3748 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3750 -- The following seems equivalent, but a bit dubious
3752 if Elaboration_Checks_Suppressed (Designator) then
3753 Set_Kill_Elaboration_Checks (Designator);
3756 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3757 Set_Categorization_From_Scope (Designator, Scop);
3760 -- For a compilation unit, check for library-unit pragmas
3762 Push_Scope (Designator);
3763 Set_Categorization_From_Pragmas (N);
3764 Validate_Categorization_Dependency (N, Designator);
3768 -- For a compilation unit, set body required. This flag will only be
3769 -- reset if a valid Import or Interface pragma is processed later on.
3771 if Nkind (Parent (N)) = N_Compilation_Unit then
3772 Set_Body_Required (Parent (N), True);
3774 if Ada_Version >= Ada_2005
3775 and then Nkind (Specification (N)) = N_Procedure_Specification
3776 and then Null_Present (Specification (N))
3779 ("null procedure cannot be declared at library level", N);
3783 Generate_Reference_To_Formals (Designator);
3784 Check_Eliminated (Designator);
3786 if Debug_Flag_C then
3788 Write_Str ("<== subprogram spec ");
3789 Write_Name (Chars (Designator));
3790 Write_Str (" from ");
3791 Write_Location (Sloc (N));
3795 if Is_Protected_Type (Current_Scope) then
3797 -- Indicate that this is a protected operation, because it may be
3798 -- used in subsequent declarations within the protected type.
3800 Set_Convention (Designator, Convention_Protected);
3803 List_Inherited_Pre_Post_Aspects (Designator);
3805 if Has_Aspects (N) then
3806 Analyze_Aspect_Specifications (N, Designator);
3808 end Analyze_Subprogram_Declaration;
3810 --------------------------------------
3811 -- Analyze_Subprogram_Specification --
3812 --------------------------------------
3814 -- Reminder: N here really is a subprogram specification (not a subprogram
3815 -- declaration). This procedure is called to analyze the specification in
3816 -- both subprogram bodies and subprogram declarations (specs).
3818 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3819 Designator : constant Entity_Id := Defining_Entity (N);
3820 Formals : constant List_Id := Parameter_Specifications (N);
3822 -- Start of processing for Analyze_Subprogram_Specification
3825 -- User-defined operator is not allowed in SPARK, except as a renaming
3827 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3828 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3830 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3833 -- Proceed with analysis. Do not emit a cross-reference entry if the
3834 -- specification comes from an expression function, because it may be
3835 -- the completion of a previous declaration. It is is not, the cross-
3836 -- reference entry will be emitted for the new subprogram declaration.
3838 if Nkind (Parent (N)) /= N_Expression_Function then
3839 Generate_Definition (Designator);
3842 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3844 if Nkind (N) = N_Function_Specification then
3845 Set_Ekind (Designator, E_Function);
3846 Set_Mechanism (Designator, Default_Mechanism);
3848 Set_Ekind (Designator, E_Procedure);
3849 Set_Etype (Designator, Standard_Void_Type);
3852 -- Introduce new scope for analysis of the formals and the return type
3854 Set_Scope (Designator, Current_Scope);
3856 if Present (Formals) then
3857 Push_Scope (Designator);
3858 Process_Formals (Formals, N);
3860 -- Check dimensions in N for formals with default expression
3862 Analyze_Dimension_Formals (N, Formals);
3864 -- Ada 2005 (AI-345): If this is an overriding operation of an
3865 -- inherited interface operation, and the controlling type is
3866 -- a synchronized type, replace the type with its corresponding
3867 -- record, to match the proper signature of an overriding operation.
3868 -- Same processing for an access parameter whose designated type is
3869 -- derived from a synchronized interface.
3871 if Ada_Version >= Ada_2005 then
3874 Formal_Typ : Entity_Id;
3875 Rec_Typ : Entity_Id;
3876 Desig_Typ : Entity_Id;
3879 Formal := First_Formal (Designator);
3880 while Present (Formal) loop
3881 Formal_Typ := Etype (Formal);
3883 if Is_Concurrent_Type (Formal_Typ)
3884 and then Present (Corresponding_Record_Type (Formal_Typ))
3886 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3888 if Present (Interfaces (Rec_Typ)) then
3889 Set_Etype (Formal, Rec_Typ);
3892 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3893 Desig_Typ := Designated_Type (Formal_Typ);
3895 if Is_Concurrent_Type (Desig_Typ)
3896 and then Present (Corresponding_Record_Type (Desig_Typ))
3898 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3900 if Present (Interfaces (Rec_Typ)) then
3901 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3906 Next_Formal (Formal);
3913 -- The subprogram scope is pushed and popped around the processing of
3914 -- the return type for consistency with call above to Process_Formals
3915 -- (which itself can call Analyze_Return_Type), and to ensure that any
3916 -- itype created for the return type will be associated with the proper
3919 elsif Nkind (N) = N_Function_Specification then
3920 Push_Scope (Designator);
3921 Analyze_Return_Type (N);
3927 if Nkind (N) = N_Function_Specification then
3929 -- Deal with operator symbol case
3931 if Nkind (Designator) = N_Defining_Operator_Symbol then
3932 Valid_Operator_Definition (Designator);
3935 May_Need_Actuals (Designator);
3937 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3938 -- the subprogram is abstract also. This does not apply to renaming
3939 -- declarations, where abstractness is inherited, and to subprogram
3940 -- bodies generated for stream operations, which become renamings as
3943 -- In case of primitives associated with abstract interface types
3944 -- the check is applied later (see Analyze_Subprogram_Declaration).
3946 if not Nkind_In (Original_Node (Parent (N)),
3947 N_Subprogram_Renaming_Declaration,
3948 N_Abstract_Subprogram_Declaration,
3949 N_Formal_Abstract_Subprogram_Declaration)
3951 if Is_Abstract_Type (Etype (Designator))
3952 and then not Is_Interface (Etype (Designator))
3955 ("function that returns abstract type must be abstract", N);
3957 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3958 -- access result whose designated type is abstract.
3960 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3962 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3963 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3964 and then Ada_Version >= Ada_2012
3966 Error_Msg_N ("function whose access result designates "
3967 & "abstract type must be abstract", N);
3973 end Analyze_Subprogram_Specification;
3975 --------------------------
3976 -- Build_Body_To_Inline --
3977 --------------------------
3979 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3980 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3981 Original_Body : Node_Id;
3982 Body_To_Analyze : Node_Id;
3983 Max_Size : constant := 10;
3984 Stat_Count : Integer := 0;
3986 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3987 -- Check for declarations that make inlining not worthwhile
3989 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3990 -- Check for statements that make inlining not worthwhile: any tasking
3991 -- statement, nested at any level. Keep track of total number of
3992 -- elementary statements, as a measure of acceptable size.
3994 function Has_Pending_Instantiation return Boolean;
3995 -- If some enclosing body contains instantiations that appear before the
3996 -- corresponding generic body, the enclosing body has a freeze node so
3997 -- that it can be elaborated after the generic itself. This might
3998 -- conflict with subsequent inlinings, so that it is unsafe to try to
3999 -- inline in such a case.
4001 function Has_Single_Return return Boolean;
4002 -- In general we cannot inline functions that return unconstrained type.
4003 -- However, we can handle such functions if all return statements return
4004 -- a local variable that is the only declaration in the body of the
4005 -- function. In that case the call can be replaced by that local
4006 -- variable as is done for other inlined calls.
4008 procedure Remove_Pragmas;
4009 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
4010 -- parameter has no meaning when the body is inlined and the formals
4011 -- are rewritten. Remove it from body to inline. The analysis of the
4012 -- non-inlined body will handle the pragma properly.
4014 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
4015 -- If the body of the subprogram includes a call that returns an
4016 -- unconstrained type, the secondary stack is involved, and it
4017 -- is not worth inlining.
4019 ------------------------------
4020 -- Has_Excluded_Declaration --
4021 ------------------------------
4023 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4026 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4027 -- Nested subprograms make a given body ineligible for inlining, but
4028 -- we make an exception for instantiations of unchecked conversion.
4029 -- The body has not been analyzed yet, so check the name, and verify
4030 -- that the visible entity with that name is the predefined unit.
4032 -----------------------------
4033 -- Is_Unchecked_Conversion --
4034 -----------------------------
4036 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4037 Id : constant Node_Id := Name (D);
4041 if Nkind (Id) = N_Identifier
4042 and then Chars (Id) = Name_Unchecked_Conversion
4044 Conv := Current_Entity (Id);
4046 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4047 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4049 Conv := Current_Entity (Selector_Name (Id));
4054 return Present (Conv)
4055 and then Is_Predefined_File_Name
4056 (Unit_File_Name (Get_Source_Unit (Conv)))
4057 and then Is_Intrinsic_Subprogram (Conv);
4058 end Is_Unchecked_Conversion;
4060 -- Start of processing for Has_Excluded_Declaration
4064 while Present (D) loop
4065 if (Nkind (D) = N_Function_Instantiation
4066 and then not Is_Unchecked_Conversion (D))
4067 or else Nkind_In (D, N_Protected_Type_Declaration,
4068 N_Package_Declaration,
4069 N_Package_Instantiation,
4071 N_Procedure_Instantiation,
4072 N_Task_Type_Declaration)
4075 ("cannot inline & (non-allowed declaration)?", D, Subp);
4083 end Has_Excluded_Declaration;
4085 ----------------------------
4086 -- Has_Excluded_Statement --
4087 ----------------------------
4089 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4095 while Present (S) loop
4096 Stat_Count := Stat_Count + 1;
4098 if Nkind_In (S, N_Abort_Statement,
4099 N_Asynchronous_Select,
4100 N_Conditional_Entry_Call,
4101 N_Delay_Relative_Statement,
4102 N_Delay_Until_Statement,
4107 ("cannot inline & (non-allowed statement)?", S, Subp);
4110 elsif Nkind (S) = N_Block_Statement then
4111 if Present (Declarations (S))
4112 and then Has_Excluded_Declaration (Declarations (S))
4116 elsif Present (Handled_Statement_Sequence (S))
4119 (Exception_Handlers (Handled_Statement_Sequence (S)))
4121 Has_Excluded_Statement
4122 (Statements (Handled_Statement_Sequence (S))))
4127 elsif Nkind (S) = N_Case_Statement then
4128 E := First (Alternatives (S));
4129 while Present (E) loop
4130 if Has_Excluded_Statement (Statements (E)) then
4137 elsif Nkind (S) = N_If_Statement then
4138 if Has_Excluded_Statement (Then_Statements (S)) then
4142 if Present (Elsif_Parts (S)) then
4143 E := First (Elsif_Parts (S));
4144 while Present (E) loop
4145 if Has_Excluded_Statement (Then_Statements (E)) then
4153 if Present (Else_Statements (S))
4154 and then Has_Excluded_Statement (Else_Statements (S))
4159 elsif Nkind (S) = N_Loop_Statement
4160 and then Has_Excluded_Statement (Statements (S))
4164 elsif Nkind (S) = N_Extended_Return_Statement then
4165 if Has_Excluded_Statement
4166 (Statements (Handled_Statement_Sequence (S)))
4168 (Exception_Handlers (Handled_Statement_Sequence (S)))
4178 end Has_Excluded_Statement;
4180 -------------------------------
4181 -- Has_Pending_Instantiation --
4182 -------------------------------
4184 function Has_Pending_Instantiation return Boolean is
4189 while Present (S) loop
4190 if Is_Compilation_Unit (S)
4191 or else Is_Child_Unit (S)
4195 elsif Ekind (S) = E_Package
4196 and then Has_Forward_Instantiation (S)
4205 end Has_Pending_Instantiation;
4207 ------------------------
4208 -- Has_Single_Return --
4209 ------------------------
4211 function Has_Single_Return return Boolean is
4212 Return_Statement : Node_Id := Empty;
4214 function Check_Return (N : Node_Id) return Traverse_Result;
4220 function Check_Return (N : Node_Id) return Traverse_Result is
4222 if Nkind (N) = N_Simple_Return_Statement then
4223 if Present (Expression (N))
4224 and then Is_Entity_Name (Expression (N))
4226 if No (Return_Statement) then
4227 Return_Statement := N;
4230 elsif Chars (Expression (N)) =
4231 Chars (Expression (Return_Statement))
4239 -- A return statement within an extended return is a noop
4242 elsif No (Expression (N))
4243 and then Nkind (Parent (Parent (N))) =
4244 N_Extended_Return_Statement
4249 -- Expression has wrong form
4254 -- We can only inline a build-in-place function if
4255 -- it has a single extended return.
4257 elsif Nkind (N) = N_Extended_Return_Statement then
4258 if No (Return_Statement) then
4259 Return_Statement := N;
4271 function Check_All_Returns is new Traverse_Func (Check_Return);
4273 -- Start of processing for Has_Single_Return
4276 if Check_All_Returns (N) /= OK then
4279 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4283 return Present (Declarations (N))
4284 and then Present (First (Declarations (N)))
4285 and then Chars (Expression (Return_Statement)) =
4286 Chars (Defining_Identifier (First (Declarations (N))));
4288 end Has_Single_Return;
4290 --------------------
4291 -- Remove_Pragmas --
4292 --------------------
4294 procedure Remove_Pragmas is
4299 Decl := First (Declarations (Body_To_Analyze));
4300 while Present (Decl) loop
4303 if Nkind (Decl) = N_Pragma
4304 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4314 --------------------------
4315 -- Uses_Secondary_Stack --
4316 --------------------------
4318 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4319 function Check_Call (N : Node_Id) return Traverse_Result;
4320 -- Look for function calls that return an unconstrained type
4326 function Check_Call (N : Node_Id) return Traverse_Result is
4328 if Nkind (N) = N_Function_Call
4329 and then Is_Entity_Name (Name (N))
4330 and then Is_Composite_Type (Etype (Entity (Name (N))))
4331 and then not Is_Constrained (Etype (Entity (Name (N))))
4334 ("cannot inline & (call returns unconstrained type)?",
4342 function Check_Calls is new Traverse_Func (Check_Call);
4345 return Check_Calls (Bod) = Abandon;
4346 end Uses_Secondary_Stack;
4348 -- Start of processing for Build_Body_To_Inline
4351 -- Return immediately if done already
4353 if Nkind (Decl) = N_Subprogram_Declaration
4354 and then Present (Body_To_Inline (Decl))
4358 -- Functions that return unconstrained composite types require
4359 -- secondary stack handling, and cannot currently be inlined, unless
4360 -- all return statements return a local variable that is the first
4361 -- local declaration in the body.
4363 elsif Ekind (Subp) = E_Function
4364 and then not Is_Scalar_Type (Etype (Subp))
4365 and then not Is_Access_Type (Etype (Subp))
4366 and then not Is_Constrained (Etype (Subp))
4368 if not Has_Single_Return then
4370 ("cannot inline & (unconstrained return type)?", N, Subp);
4374 -- Ditto for functions that return controlled types, where controlled
4375 -- actions interfere in complex ways with inlining.
4377 elsif Ekind (Subp) = E_Function
4378 and then Needs_Finalization (Etype (Subp))
4381 ("cannot inline & (controlled return type)?", N, Subp);
4385 if Present (Declarations (N))
4386 and then Has_Excluded_Declaration (Declarations (N))
4391 if Present (Handled_Statement_Sequence (N)) then
4392 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4394 ("cannot inline& (exception handler)?",
4395 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4399 Has_Excluded_Statement
4400 (Statements (Handled_Statement_Sequence (N)))
4406 -- We do not inline a subprogram that is too large, unless it is
4407 -- marked Inline_Always. This pragma does not suppress the other
4408 -- checks on inlining (forbidden declarations, handlers, etc).
4410 if Stat_Count > Max_Size
4411 and then not Has_Pragma_Inline_Always (Subp)
4413 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4417 if Has_Pending_Instantiation then
4419 ("cannot inline& (forward instance within enclosing body)?",
4424 -- Within an instance, the body to inline must be treated as a nested
4425 -- generic, so that the proper global references are preserved.
4427 -- Note that we do not do this at the library level, because it is not
4428 -- needed, and furthermore this causes trouble if front end inlining
4429 -- is activated (-gnatN).
4431 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4432 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4433 Original_Body := Copy_Generic_Node (N, Empty, True);
4435 Original_Body := Copy_Separate_Tree (N);
4438 -- We need to capture references to the formals in order to substitute
4439 -- the actuals at the point of inlining, i.e. instantiation. To treat
4440 -- the formals as globals to the body to inline, we nest it within
4441 -- a dummy parameterless subprogram, declared within the real one.
4442 -- To avoid generating an internal name (which is never public, and
4443 -- which affects serial numbers of other generated names), we use
4444 -- an internal symbol that cannot conflict with user declarations.
4446 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4447 Set_Defining_Unit_Name
4448 (Specification (Original_Body),
4449 Make_Defining_Identifier (Sloc (N), Name_uParent));
4450 Set_Corresponding_Spec (Original_Body, Empty);
4452 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4454 -- Set return type of function, which is also global and does not need
4457 if Ekind (Subp) = E_Function then
4458 Set_Result_Definition (Specification (Body_To_Analyze),
4459 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4462 if No (Declarations (N)) then
4463 Set_Declarations (N, New_List (Body_To_Analyze));
4465 Append (Body_To_Analyze, Declarations (N));
4468 Expander_Mode_Save_And_Set (False);
4471 Analyze (Body_To_Analyze);
4472 Push_Scope (Defining_Entity (Body_To_Analyze));
4473 Save_Global_References (Original_Body);
4475 Remove (Body_To_Analyze);
4477 Expander_Mode_Restore;
4479 -- Restore environment if previously saved
4481 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4485 -- If secondary stk used there is no point in inlining. We have
4486 -- already issued the warning in this case, so nothing to do.
4488 if Uses_Secondary_Stack (Body_To_Analyze) then
4492 Set_Body_To_Inline (Decl, Original_Body);
4493 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4494 Set_Is_Inlined (Subp);
4495 end Build_Body_To_Inline;
4501 procedure Cannot_Inline
4505 Is_Serious : Boolean := False)
4508 pragma Assert (Msg (Msg'Last) = '?');
4512 if not Debug_Flag_Dot_K then
4514 -- Do not emit warning if this is a predefined unit which is not
4515 -- the main unit. With validity checks enabled, some predefined
4516 -- subprograms may contain nested subprograms and become ineligible
4519 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4520 and then not In_Extended_Main_Source_Unit (Subp)
4524 elsif Has_Pragma_Inline_Always (Subp) then
4526 -- Remove last character (question mark) to make this into an
4527 -- error, because the Inline_Always pragma cannot be obeyed.
4529 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4531 elsif Ineffective_Inline_Warnings then
4532 Error_Msg_NE (Msg & "p?", N, Subp);
4539 elsif Is_Serious then
4541 -- Remove last character (question mark) to make this into an error.
4543 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4545 elsif Optimization_Level = 0 then
4547 -- Do not emit warning if this is a predefined unit which is not
4548 -- the main unit. This behavior is currently provided for backward
4549 -- compatibility but it will be removed when we enforce the
4550 -- strictness of the new rules.
4552 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4553 and then not In_Extended_Main_Source_Unit (Subp)
4557 elsif Has_Pragma_Inline_Always (Subp) then
4559 -- Emit a warning if this is a call to a runtime subprogram
4560 -- which is located inside a generic. Previously this call
4561 -- was silently skipped!
4563 if Is_Generic_Instance (Subp) then
4565 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4567 if Is_Predefined_File_Name
4568 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4570 Set_Is_Inlined (Subp, False);
4571 Error_Msg_NE (Msg & "p?", N, Subp);
4577 -- Remove last character (question mark) to make this into an
4578 -- error, because the Inline_Always pragma cannot be obeyed.
4580 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4582 else pragma Assert (Front_End_Inlining);
4583 Set_Is_Inlined (Subp, False);
4585 -- When inlining cannot take place we must issue an error.
4586 -- For backward compatibility we still report a warning.
4588 if Ineffective_Inline_Warnings then
4589 Error_Msg_NE (Msg & "p?", N, Subp);
4593 -- Compiling with optimizations enabled it is too early to report
4594 -- problems since the backend may still perform inlining. In order
4595 -- to report unhandled inlinings the program must be compiled with
4596 -- -Winline and the error is reported by the backend.
4603 ------------------------------------
4604 -- Check_And_Build_Body_To_Inline --
4605 ------------------------------------
4607 procedure Check_And_Build_Body_To_Inline
4609 Spec_Id : Entity_Id;
4610 Body_Id : Entity_Id)
4612 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4613 -- Use generic machinery to build an unexpanded body for the subprogram.
4614 -- This body is subsequently used for inline expansions at call sites.
4616 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4617 -- Return true if we generate code for the function body N, the function
4618 -- body N has no local declarations and its unique statement is a single
4619 -- extended return statement with a handled statements sequence.
4621 function Check_Body_To_Inline
4623 Subp : Entity_Id) return Boolean;
4624 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4625 -- inlined by the frontend. These are the rules:
4626 -- * At -O0 use fe inlining when inline_always is specified except if
4627 -- the function returns a controlled type.
4628 -- * At other optimization levels use the fe inlining for both inline
4629 -- and inline_always in the following cases:
4630 -- - function returning a known at compile time constant
4631 -- - function returning a call to an intrinsic function
4632 -- - function returning an unconstrained type (see Can_Split
4633 -- Unconstrained_Function).
4634 -- - function returning a call to a frontend-inlined function
4635 -- Use the back-end mechanism otherwise
4637 -- In addition, in the following cases the function cannot be inlined by
4639 -- - functions that uses the secondary stack
4640 -- - functions that have declarations of:
4641 -- - Concurrent types
4645 -- - functions that have some of the following statements:
4647 -- - asynchronous-select
4648 -- - conditional-entry-call
4651 -- - selective-accept
4652 -- - timed-entry-call
4653 -- - functions that have exception handlers
4654 -- - functions that have some enclosing body containing instantiations
4655 -- that appear before the corresponding generic body.
4657 procedure Generate_Body_To_Inline
4659 Body_To_Inline : out Node_Id);
4660 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4661 -- of pragmas referencing the formals are removed since they have no
4662 -- meaning when the body is inlined and the formals are rewritten (the
4663 -- analysis of the non-inlined body will handle these pragmas properly).
4664 -- A new internal name is associated with Body_To_Inline.
4666 procedure Split_Unconstrained_Function
4668 Spec_Id : Entity_Id);
4669 -- N is an inlined function body that returns an unconstrained type and
4670 -- has a single extended return statement. Split N in two subprograms:
4671 -- a procedure P' and a function F'. The formals of P' duplicate the
4672 -- formals of N plus an extra formal which is used return a value;
4673 -- its body is composed by the declarations and list of statements
4674 -- of the extended return statement of N.
4676 --------------------------
4677 -- Build_Body_To_Inline --
4678 --------------------------
4680 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4681 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4682 Original_Body : Node_Id;
4683 Body_To_Analyze : Node_Id;
4686 pragma Assert (Current_Scope = Spec_Id);
4688 -- Within an instance, the body to inline must be treated as a nested
4689 -- generic, so that the proper global references are preserved. We
4690 -- do not do this at the library level, because it is not needed, and
4691 -- furthermore this causes trouble if front end inlining is activated
4695 and then Scope (Current_Scope) /= Standard_Standard
4697 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4700 -- We need to capture references to the formals in order
4701 -- to substitute the actuals at the point of inlining, i.e.
4702 -- instantiation. To treat the formals as globals to the body to
4703 -- inline, we nest it within a dummy parameterless subprogram,
4704 -- declared within the real one.
4706 Generate_Body_To_Inline (N, Original_Body);
4707 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4709 -- Set return type of function, which is also global and does not
4710 -- need to be resolved.
4712 if Ekind (Spec_Id) = E_Function then
4713 Set_Result_Definition (Specification (Body_To_Analyze),
4714 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4717 if No (Declarations (N)) then
4718 Set_Declarations (N, New_List (Body_To_Analyze));
4720 Append_To (Declarations (N), Body_To_Analyze);
4723 Preanalyze (Body_To_Analyze);
4725 Push_Scope (Defining_Entity (Body_To_Analyze));
4726 Save_Global_References (Original_Body);
4728 Remove (Body_To_Analyze);
4730 -- Restore environment if previously saved
4733 and then Scope (Current_Scope) /= Standard_Standard
4738 pragma Assert (No (Body_To_Inline (Decl)));
4739 Set_Body_To_Inline (Decl, Original_Body);
4740 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4741 end Build_Body_To_Inline;
4743 --------------------------
4744 -- Check_Body_To_Inline --
4745 --------------------------
4747 function Check_Body_To_Inline
4749 Subp : Entity_Id) return Boolean
4751 Max_Size : constant := 10;
4752 Stat_Count : Integer := 0;
4754 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4755 -- Check for declarations that make inlining not worthwhile
4757 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4758 -- Check for statements that make inlining not worthwhile: any
4759 -- tasking statement, nested at any level. Keep track of total
4760 -- number of elementary statements, as a measure of acceptable size.
4762 function Has_Pending_Instantiation return Boolean;
4763 -- Return True if some enclosing body contains instantiations that
4764 -- appear before the corresponding generic body.
4766 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4767 -- Return True if all the return statements of the function body N
4768 -- are simple return statements and return a compile time constant
4770 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4771 -- Return True if all the return statements of the function body N
4772 -- are simple return statements and return an intrinsic function call
4774 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4775 -- If the body of the subprogram includes a call that returns an
4776 -- unconstrained type, the secondary stack is involved, and it
4777 -- is not worth inlining.
4779 ------------------------------
4780 -- Has_Excluded_Declaration --
4781 ------------------------------
4783 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4786 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4787 -- Nested subprograms make a given body ineligible for inlining,
4788 -- but we make an exception for instantiations of unchecked
4789 -- conversion. The body has not been analyzed yet, so check the
4790 -- name, and verify that the visible entity with that name is the
4793 -----------------------------
4794 -- Is_Unchecked_Conversion --
4795 -----------------------------
4797 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4798 Id : constant Node_Id := Name (D);
4802 if Nkind (Id) = N_Identifier
4803 and then Chars (Id) = Name_Unchecked_Conversion
4805 Conv := Current_Entity (Id);
4807 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4809 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4811 Conv := Current_Entity (Selector_Name (Id));
4816 return Present (Conv)
4817 and then Is_Predefined_File_Name
4818 (Unit_File_Name (Get_Source_Unit (Conv)))
4819 and then Is_Intrinsic_Subprogram (Conv);
4820 end Is_Unchecked_Conversion;
4822 -- Start of processing for Has_Excluded_Declaration
4826 while Present (D) loop
4827 if (Nkind (D) = N_Function_Instantiation
4828 and then not Is_Unchecked_Conversion (D))
4829 or else Nkind_In (D, N_Protected_Type_Declaration,
4830 N_Package_Declaration,
4831 N_Package_Instantiation,
4833 N_Procedure_Instantiation,
4834 N_Task_Type_Declaration)
4837 ("cannot inline & (non-allowed declaration)?", D, Subp);
4846 end Has_Excluded_Declaration;
4848 ----------------------------
4849 -- Has_Excluded_Statement --
4850 ----------------------------
4852 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4858 while Present (S) loop
4859 Stat_Count := Stat_Count + 1;
4861 if Nkind_In (S, N_Abort_Statement,
4862 N_Asynchronous_Select,
4863 N_Conditional_Entry_Call,
4864 N_Delay_Relative_Statement,
4865 N_Delay_Until_Statement,
4870 ("cannot inline & (non-allowed statement)?", S, Subp);
4873 elsif Nkind (S) = N_Block_Statement then
4874 if Present (Declarations (S))
4875 and then Has_Excluded_Declaration (Declarations (S))
4879 elsif Present (Handled_Statement_Sequence (S)) then
4881 (Exception_Handlers (Handled_Statement_Sequence (S)))
4884 ("cannot inline& (exception handler)?",
4885 First (Exception_Handlers
4886 (Handled_Statement_Sequence (S))),
4890 elsif Has_Excluded_Statement
4891 (Statements (Handled_Statement_Sequence (S)))
4897 elsif Nkind (S) = N_Case_Statement then
4898 E := First (Alternatives (S));
4899 while Present (E) loop
4900 if Has_Excluded_Statement (Statements (E)) then
4907 elsif Nkind (S) = N_If_Statement then
4908 if Has_Excluded_Statement (Then_Statements (S)) then
4912 if Present (Elsif_Parts (S)) then
4913 E := First (Elsif_Parts (S));
4914 while Present (E) loop
4915 if Has_Excluded_Statement (Then_Statements (E)) then
4922 if Present (Else_Statements (S))
4923 and then Has_Excluded_Statement (Else_Statements (S))
4928 elsif Nkind (S) = N_Loop_Statement
4929 and then Has_Excluded_Statement (Statements (S))
4933 elsif Nkind (S) = N_Extended_Return_Statement then
4934 if Present (Handled_Statement_Sequence (S))
4936 Has_Excluded_Statement
4937 (Statements (Handled_Statement_Sequence (S)))
4941 elsif Present (Handled_Statement_Sequence (S))
4943 Present (Exception_Handlers
4944 (Handled_Statement_Sequence (S)))
4947 ("cannot inline& (exception handler)?",
4948 First (Exception_Handlers
4949 (Handled_Statement_Sequence (S))),
4959 end Has_Excluded_Statement;
4961 -------------------------------
4962 -- Has_Pending_Instantiation --
4963 -------------------------------
4965 function Has_Pending_Instantiation return Boolean is
4970 while Present (S) loop
4971 if Is_Compilation_Unit (S)
4972 or else Is_Child_Unit (S)
4976 elsif Ekind (S) = E_Package
4977 and then Has_Forward_Instantiation (S)
4986 end Has_Pending_Instantiation;
4988 ------------------------------------
4989 -- Returns_Compile_Time_Constant --
4990 ------------------------------------
4992 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4994 function Check_Return (N : Node_Id) return Traverse_Result;
5000 function Check_Return (N : Node_Id) return Traverse_Result is
5002 if Nkind (N) = N_Extended_Return_Statement then
5005 elsif Nkind (N) = N_Simple_Return_Statement then
5006 if Present (Expression (N)) then
5008 Orig_Expr : constant Node_Id :=
5009 Original_Node (Expression (N));
5012 if Nkind_In (Orig_Expr, N_Integer_Literal,
5014 N_Character_Literal)
5018 elsif Is_Entity_Name (Orig_Expr)
5019 and then Ekind (Entity (Orig_Expr)) = E_Constant
5020 and then Is_Static_Expression (Orig_Expr)
5028 -- Expression has wrong form
5034 -- Continue analyzing statements
5041 function Check_All_Returns is new Traverse_Func (Check_Return);
5043 -- Start of processing for Returns_Compile_Time_Constant
5046 return Check_All_Returns (N) = OK;
5047 end Returns_Compile_Time_Constant;
5049 --------------------------------------
5050 -- Returns_Intrinsic_Function_Call --
5051 --------------------------------------
5053 function Returns_Intrinsic_Function_Call
5054 (N : Node_Id) return Boolean
5056 function Check_Return (N : Node_Id) return Traverse_Result;
5062 function Check_Return (N : Node_Id) return Traverse_Result is
5064 if Nkind (N) = N_Extended_Return_Statement then
5067 elsif Nkind (N) = N_Simple_Return_Statement then
5068 if Present (Expression (N)) then
5070 Orig_Expr : constant Node_Id :=
5071 Original_Node (Expression (N));
5074 if Nkind (Orig_Expr) in N_Op
5075 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5079 elsif Nkind (Orig_Expr) in N_Has_Entity
5080 and then Present (Entity (Orig_Expr))
5081 and then Ekind (Entity (Orig_Expr)) = E_Function
5082 and then Is_Inlined (Entity (Orig_Expr))
5086 elsif Nkind (Orig_Expr) in N_Has_Entity
5087 and then Present (Entity (Orig_Expr))
5088 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5097 -- Expression has wrong form
5103 -- Continue analyzing statements
5110 function Check_All_Returns is new Traverse_Func (Check_Return);
5112 -- Start of processing for Returns_Intrinsic_Function_Call
5115 return Check_All_Returns (N) = OK;
5116 end Returns_Intrinsic_Function_Call;
5118 --------------------------
5119 -- Uses_Secondary_Stack --
5120 --------------------------
5122 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5124 function Check_Call (N : Node_Id) return Traverse_Result;
5125 -- Look for function calls that return an unconstrained type
5131 function Check_Call (N : Node_Id) return Traverse_Result is
5133 if Nkind (N) = N_Function_Call
5134 and then Is_Entity_Name (Name (N))
5135 and then Is_Composite_Type (Etype (Entity (Name (N))))
5136 and then not Is_Constrained (Etype (Entity (Name (N))))
5139 ("cannot inline & (call returns unconstrained type)?",
5148 function Check_Calls is new Traverse_Func (Check_Call);
5150 -- Start of processing for Uses_Secondary_Stack
5153 return Check_Calls (N) = Abandon;
5154 end Uses_Secondary_Stack;
5158 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5159 May_Inline : constant Boolean :=
5160 Has_Pragma_Inline_Always (Spec_Id)
5161 or else (Has_Pragma_Inline (Spec_Id)
5162 and then ((Optimization_Level > 0
5163 and then Ekind (Spec_Id)
5165 or else Front_End_Inlining));
5166 Body_To_Analyze : Node_Id;
5168 -- Start of processing for Check_Body_To_Inline
5171 -- No action needed in stubs since the attribute Body_To_Inline
5174 if Nkind (Decl) = N_Subprogram_Body_Stub then
5177 -- Cannot build the body to inline if the attribute is already set.
5178 -- This attribute may have been set if this is a subprogram renaming
5179 -- declarations (see Freeze.Build_Renamed_Body).
5181 elsif Present (Body_To_Inline (Decl)) then
5184 -- No action needed if the subprogram does not fulfill the minimum
5185 -- conditions to be inlined by the frontend
5187 elsif not May_Inline then
5191 -- Check excluded declarations
5193 if Present (Declarations (N))
5194 and then Has_Excluded_Declaration (Declarations (N))
5199 -- Check excluded statements
5201 if Present (Handled_Statement_Sequence (N)) then
5203 (Exception_Handlers (Handled_Statement_Sequence (N)))
5206 ("cannot inline& (exception handler)?",
5208 (Exception_Handlers (Handled_Statement_Sequence (N))),
5213 elsif Has_Excluded_Statement
5214 (Statements (Handled_Statement_Sequence (N)))
5220 -- For backward compatibility, compiling under -gnatN we do not
5221 -- inline a subprogram that is too large, unless it is marked
5222 -- Inline_Always. This pragma does not suppress the other checks
5223 -- on inlining (forbidden declarations, handlers, etc).
5225 if Front_End_Inlining
5226 and then not Has_Pragma_Inline_Always (Subp)
5227 and then Stat_Count > Max_Size
5229 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5233 -- If some enclosing body contains instantiations that appear before
5234 -- the corresponding generic body, the enclosing body has a freeze
5235 -- node so that it can be elaborated after the generic itself. This
5236 -- might conflict with subsequent inlinings, so that it is unsafe to
5237 -- try to inline in such a case.
5239 if Has_Pending_Instantiation then
5241 ("cannot inline& (forward instance within enclosing body)?",
5247 -- Generate and preanalyze the body to inline (needed to perform
5248 -- the rest of the checks)
5250 Generate_Body_To_Inline (N, Body_To_Analyze);
5252 if Ekind (Subp) = E_Function then
5253 Set_Result_Definition (Specification (Body_To_Analyze),
5254 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5257 -- Nest the body to analyze within the real one
5259 if No (Declarations (N)) then
5260 Set_Declarations (N, New_List (Body_To_Analyze));
5262 Append_To (Declarations (N), Body_To_Analyze);
5265 Preanalyze (Body_To_Analyze);
5266 Remove (Body_To_Analyze);
5268 -- Keep separate checks needed when compiling without optimizations
5270 if Optimization_Level = 0
5272 -- AAMP and VM targets have no support for inlining in the backend
5273 -- and hence we use frontend inlining at all optimization levels.
5275 or else AAMP_On_Target
5276 or else VM_Target /= No_VM
5278 -- Cannot inline functions whose body has a call that returns an
5279 -- unconstrained type since the secondary stack is involved, and
5280 -- it is not worth inlining.
5282 if Uses_Secondary_Stack (Body_To_Analyze) then
5285 -- Cannot inline functions that return controlled types since
5286 -- controlled actions interfere in complex ways with inlining.
5288 elsif Ekind (Subp) = E_Function
5289 and then Needs_Finalization (Etype (Subp))
5292 ("cannot inline & (controlled return type)?", N, Subp);
5295 elsif Returns_Unconstrained_Type (Subp) then
5297 ("cannot inline & (unconstrained return type)?", N, Subp);
5301 -- Compiling with optimizations enabled
5304 -- Procedures are never frontend inlined in this case!
5306 if Ekind (Subp) /= E_Function then
5309 -- Functions returning unconstrained types are tested
5310 -- separately (see Can_Split_Unconstrained_Function).
5312 elsif Returns_Unconstrained_Type (Subp) then
5315 -- Check supported cases
5317 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5318 and then Convention (Subp) /= Convention_Intrinsic
5319 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5326 end Check_Body_To_Inline;
5328 --------------------------------------
5329 -- Can_Split_Unconstrained_Function --
5330 --------------------------------------
5332 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5334 Ret_Node : constant Node_Id :=
5335 First (Statements (Handled_Statement_Sequence (N)));
5339 -- No user defined declarations allowed in the function except inside
5340 -- the unique return statement; implicit labels are the only allowed
5343 if not Is_Empty_List (Declarations (N)) then
5344 D := First (Declarations (N));
5345 while Present (D) loop
5346 if Nkind (D) /= N_Implicit_Label_Declaration then
5354 -- We only split the inlined function when we are generating the code
5355 -- of its body; otherwise we leave duplicated split subprograms in
5356 -- the tree which (if referenced) generate wrong references at link
5359 return In_Extended_Main_Code_Unit (N)
5360 and then Present (Ret_Node)
5361 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5362 and then No (Next (Ret_Node))
5363 and then Present (Handled_Statement_Sequence (Ret_Node));
5364 end Can_Split_Unconstrained_Function;
5366 -----------------------------
5367 -- Generate_Body_To_Inline --
5368 -----------------------------
5370 procedure Generate_Body_To_Inline
5372 Body_To_Inline : out Node_Id)
5374 procedure Remove_Pragmas (N : Node_Id);
5375 -- Remove occurrences of pragmas that may reference the formals of
5376 -- N. The analysis of the non-inlined body will handle these pragmas
5379 --------------------
5380 -- Remove_Pragmas --
5381 --------------------
5383 procedure Remove_Pragmas (N : Node_Id) is
5388 Decl := First (Declarations (N));
5389 while Present (Decl) loop
5392 if Nkind (Decl) = N_Pragma
5393 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5403 -- Start of processing for Generate_Body_To_Inline
5406 -- Within an instance, the body to inline must be treated as a nested
5407 -- generic, so that the proper global references are preserved.
5409 -- Note that we do not do this at the library level, because it
5410 -- is not needed, and furthermore this causes trouble if front
5411 -- end inlining is activated (-gnatN).
5414 and then Scope (Current_Scope) /= Standard_Standard
5416 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5418 Body_To_Inline := Copy_Separate_Tree (N);
5421 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5422 -- parameter has no meaning when the body is inlined and the formals
5423 -- are rewritten. Remove it from body to inline. The analysis of the
5424 -- non-inlined body will handle the pragma properly.
5426 Remove_Pragmas (Body_To_Inline);
5428 -- We need to capture references to the formals in order
5429 -- to substitute the actuals at the point of inlining, i.e.
5430 -- instantiation. To treat the formals as globals to the body to
5431 -- inline, we nest it within a dummy parameterless subprogram,
5432 -- declared within the real one.
5434 Set_Parameter_Specifications
5435 (Specification (Body_To_Inline), No_List);
5437 -- A new internal name is associated with Body_To_Inline to avoid
5438 -- conflicts when the non-inlined body N is analyzed.
5440 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5441 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5442 Set_Corresponding_Spec (Body_To_Inline, Empty);
5443 end Generate_Body_To_Inline;
5445 ----------------------------------
5446 -- Split_Unconstrained_Function --
5447 ----------------------------------
5449 procedure Split_Unconstrained_Function
5451 Spec_Id : Entity_Id)
5453 Loc : constant Source_Ptr := Sloc (N);
5454 Ret_Node : constant Node_Id :=
5455 First (Statements (Handled_Statement_Sequence (N)));
5456 Ret_Obj : constant Node_Id :=
5457 First (Return_Object_Declarations (Ret_Node));
5459 procedure Build_Procedure
5460 (Proc_Id : out Entity_Id;
5461 Decl_List : out List_Id);
5462 -- Build a procedure containing the statements found in the extended
5463 -- return statement of the unconstrained function body N.
5465 procedure Build_Procedure
5466 (Proc_Id : out Entity_Id;
5467 Decl_List : out List_Id)
5470 Formal_List : constant List_Id := New_List;
5471 Proc_Spec : Node_Id;
5472 Proc_Body : Node_Id;
5473 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5474 Body_Decl_List : List_Id := No_List;
5475 Param_Type : Node_Id;
5478 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5479 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5482 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5485 Append_To (Formal_List,
5486 Make_Parameter_Specification (Loc,
5487 Defining_Identifier =>
5488 Make_Defining_Identifier (Loc,
5489 Chars => Chars (Defining_Identifier (Ret_Obj))),
5490 In_Present => False,
5491 Out_Present => True,
5492 Null_Exclusion_Present => False,
5493 Parameter_Type => Param_Type));
5495 Formal := First_Formal (Spec_Id);
5496 while Present (Formal) loop
5497 Append_To (Formal_List,
5498 Make_Parameter_Specification (Loc,
5499 Defining_Identifier =>
5500 Make_Defining_Identifier (Sloc (Formal),
5501 Chars => Chars (Formal)),
5502 In_Present => In_Present (Parent (Formal)),
5503 Out_Present => Out_Present (Parent (Formal)),
5504 Null_Exclusion_Present =>
5505 Null_Exclusion_Present (Parent (Formal)),
5507 New_Reference_To (Etype (Formal), Loc),
5509 Copy_Separate_Tree (Expression (Parent (Formal)))));
5511 Next_Formal (Formal);
5515 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5518 Make_Procedure_Specification (Loc,
5519 Defining_Unit_Name => Proc_Id,
5520 Parameter_Specifications => Formal_List);
5522 Decl_List := New_List;
5524 Append_To (Decl_List,
5525 Make_Subprogram_Declaration (Loc, Proc_Spec));
5527 -- Can_Convert_Unconstrained_Function checked that the function
5528 -- has no local declarations except implicit label declarations.
5529 -- Copy these declarations to the built procedure.
5531 if Present (Declarations (N)) then
5532 Body_Decl_List := New_List;
5539 D := First (Declarations (N));
5540 while Present (D) loop
5541 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5544 Make_Implicit_Label_Declaration (Loc,
5545 Make_Defining_Identifier (Loc,
5546 Chars => Chars (Defining_Identifier (D))),
5547 Label_Construct => Empty);
5548 Append_To (Body_Decl_List, New_D);
5555 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5558 Make_Subprogram_Body (Loc,
5559 Specification => Copy_Separate_Tree (Proc_Spec),
5560 Declarations => Body_Decl_List,
5561 Handled_Statement_Sequence =>
5562 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5564 Set_Defining_Unit_Name (Specification (Proc_Body),
5565 Make_Defining_Identifier (Loc, Subp_Name));
5567 Append_To (Decl_List, Proc_Body);
5568 end Build_Procedure;
5572 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5574 Proc_Id : Entity_Id;
5575 Proc_Call : Node_Id;
5577 -- Start of processing for Split_Unconstrained_Function
5580 -- Build the associated procedure, analyze it and insert it before
5581 -- the function body N
5584 Scope : constant Entity_Id := Current_Scope;
5585 Decl_List : List_Id;
5588 Build_Procedure (Proc_Id, Decl_List);
5589 Insert_Actions (N, Decl_List);
5593 -- Build the call to the generated procedure
5596 Actual_List : constant List_Id := New_List;
5600 Append_To (Actual_List,
5601 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5603 Formal := First_Formal (Spec_Id);
5604 while Present (Formal) loop
5605 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5607 -- Avoid spurious warning on unreferenced formals
5609 Set_Referenced (Formal);
5610 Next_Formal (Formal);
5614 Make_Procedure_Call_Statement (Loc,
5615 Name => New_Reference_To (Proc_Id, Loc),
5616 Parameter_Associations => Actual_List);
5624 -- main_1__F1b (New_Obj, ...);
5629 Make_Block_Statement (Loc,
5630 Declarations => New_List (New_Obj),
5631 Handled_Statement_Sequence =>
5632 Make_Handled_Sequence_Of_Statements (Loc,
5633 Statements => New_List (
5637 Make_Simple_Return_Statement (Loc,
5640 (Defining_Identifier (New_Obj), Loc)))));
5642 Rewrite (Ret_Node, Blk_Stmt);
5643 end Split_Unconstrained_Function;
5645 -- Start of processing for Check_And_Build_Body_To_Inline
5648 -- Do not inline any subprogram that contains nested subprograms, since
5649 -- the backend inlining circuit seems to generate uninitialized
5650 -- references in this case. We know this happens in the case of front
5651 -- end ZCX support, but it also appears it can happen in other cases as
5652 -- well. The backend often rejects attempts to inline in the case of
5653 -- nested procedures anyway, so little if anything is lost by this.
5654 -- Note that this is test is for the benefit of the back-end. There is
5655 -- a separate test for front-end inlining that also rejects nested
5658 -- Do not do this test if errors have been detected, because in some
5659 -- error cases, this code blows up, and we don't need it anyway if
5660 -- there have been errors, since we won't get to the linker anyway.
5662 if Comes_From_Source (Body_Id)
5663 and then (Has_Pragma_Inline_Always (Spec_Id)
5664 or else Optimization_Level > 0)
5665 and then Serious_Errors_Detected = 0
5673 P_Ent := Scope (P_Ent);
5674 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5676 if Is_Subprogram (P_Ent) then
5677 Set_Is_Inlined (P_Ent, False);
5679 if Comes_From_Source (P_Ent)
5680 and then Has_Pragma_Inline (P_Ent)
5683 ("cannot inline& (nested subprogram)?", N, P_Ent,
5684 Is_Serious => True);
5691 -- Build the body to inline only if really needed!
5693 if Check_Body_To_Inline (N, Spec_Id)
5694 and then Serious_Errors_Detected = 0
5696 if Returns_Unconstrained_Type (Spec_Id) then
5697 if Can_Split_Unconstrained_Function (N) then
5698 Split_Unconstrained_Function (N, Spec_Id);
5699 Build_Body_To_Inline (N, Spec_Id);
5700 Set_Is_Inlined (Spec_Id);
5703 Build_Body_To_Inline (N, Spec_Id);
5704 Set_Is_Inlined (Spec_Id);
5707 end Check_And_Build_Body_To_Inline;
5709 -----------------------
5710 -- Check_Conformance --
5711 -----------------------
5713 procedure Check_Conformance
5714 (New_Id : Entity_Id;
5716 Ctype : Conformance_Type;
5718 Conforms : out Boolean;
5719 Err_Loc : Node_Id := Empty;
5720 Get_Inst : Boolean := False;
5721 Skip_Controlling_Formals : Boolean := False)
5723 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5724 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5725 -- If Errmsg is True, then processing continues to post an error message
5726 -- for conformance error on given node. Two messages are output. The
5727 -- first message points to the previous declaration with a general "no
5728 -- conformance" message. The second is the detailed reason, supplied as
5729 -- Msg. The parameter N provide information for a possible & insertion
5730 -- in the message, and also provides the location for posting the
5731 -- message in the absence of a specified Err_Loc location.
5733 -----------------------
5734 -- Conformance_Error --
5735 -----------------------
5737 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5744 if No (Err_Loc) then
5750 Error_Msg_Sloc := Sloc (Old_Id);
5753 when Type_Conformant =>
5754 Error_Msg_N -- CODEFIX
5755 ("not type conformant with declaration#!", Enode);
5757 when Mode_Conformant =>
5758 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5760 ("not mode conformant with operation inherited#!",
5764 ("not mode conformant with declaration#!", Enode);
5767 when Subtype_Conformant =>
5768 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5770 ("not subtype conformant with operation inherited#!",
5774 ("not subtype conformant with declaration#!", Enode);
5777 when Fully_Conformant =>
5778 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5779 Error_Msg_N -- CODEFIX
5780 ("not fully conformant with operation inherited#!",
5783 Error_Msg_N -- CODEFIX
5784 ("not fully conformant with declaration#!", Enode);
5788 Error_Msg_NE (Msg, Enode, N);
5790 end Conformance_Error;
5794 Old_Type : constant Entity_Id := Etype (Old_Id);
5795 New_Type : constant Entity_Id := Etype (New_Id);
5796 Old_Formal : Entity_Id;
5797 New_Formal : Entity_Id;
5798 Access_Types_Match : Boolean;
5799 Old_Formal_Base : Entity_Id;
5800 New_Formal_Base : Entity_Id;
5802 -- Start of processing for Check_Conformance
5807 -- We need a special case for operators, since they don't appear
5810 if Ctype = Type_Conformant then
5811 if Ekind (New_Id) = E_Operator
5812 and then Operator_Matches_Spec (New_Id, Old_Id)
5818 -- If both are functions/operators, check return types conform
5820 if Old_Type /= Standard_Void_Type
5821 and then New_Type /= Standard_Void_Type
5824 -- If we are checking interface conformance we omit controlling
5825 -- arguments and result, because we are only checking the conformance
5826 -- of the remaining parameters.
5828 if Has_Controlling_Result (Old_Id)
5829 and then Has_Controlling_Result (New_Id)
5830 and then Skip_Controlling_Formals
5834 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5835 Conformance_Error ("\return type does not match!", New_Id);
5839 -- Ada 2005 (AI-231): In case of anonymous access types check the
5840 -- null-exclusion and access-to-constant attributes match.
5842 if Ada_Version >= Ada_2005
5843 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5845 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5846 or else Is_Access_Constant (Etype (Old_Type)) /=
5847 Is_Access_Constant (Etype (New_Type)))
5849 Conformance_Error ("\return type does not match!", New_Id);
5853 -- If either is a function/operator and the other isn't, error
5855 elsif Old_Type /= Standard_Void_Type
5856 or else New_Type /= Standard_Void_Type
5858 Conformance_Error ("\functions can only match functions!", New_Id);
5862 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5863 -- If this is a renaming as body, refine error message to indicate that
5864 -- the conflict is with the original declaration. If the entity is not
5865 -- frozen, the conventions don't have to match, the one of the renamed
5866 -- entity is inherited.
5868 if Ctype >= Subtype_Conformant then
5869 if Convention (Old_Id) /= Convention (New_Id) then
5870 if not Is_Frozen (New_Id) then
5873 elsif Present (Err_Loc)
5874 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5875 and then Present (Corresponding_Spec (Err_Loc))
5877 Error_Msg_Name_1 := Chars (New_Id);
5879 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5880 Conformance_Error ("\prior declaration for% has convention %!");
5883 Conformance_Error ("\calling conventions do not match!");
5888 elsif Is_Formal_Subprogram (Old_Id)
5889 or else Is_Formal_Subprogram (New_Id)
5891 Conformance_Error ("\formal subprograms not allowed!");
5896 -- Deal with parameters
5898 -- Note: we use the entity information, rather than going directly
5899 -- to the specification in the tree. This is not only simpler, but
5900 -- absolutely necessary for some cases of conformance tests between
5901 -- operators, where the declaration tree simply does not exist!
5903 Old_Formal := First_Formal (Old_Id);
5904 New_Formal := First_Formal (New_Id);
5905 while Present (Old_Formal) and then Present (New_Formal) loop
5906 if Is_Controlling_Formal (Old_Formal)
5907 and then Is_Controlling_Formal (New_Formal)
5908 and then Skip_Controlling_Formals
5910 -- The controlling formals will have different types when
5911 -- comparing an interface operation with its match, but both
5912 -- or neither must be access parameters.
5914 if Is_Access_Type (Etype (Old_Formal))
5916 Is_Access_Type (Etype (New_Formal))
5918 goto Skip_Controlling_Formal;
5921 ("\access parameter does not match!", New_Formal);
5925 -- Ada 2012: Mode conformance also requires that formal parameters
5926 -- be both aliased, or neither.
5928 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5929 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5931 ("\aliased parameter mismatch!", New_Formal);
5935 if Ctype = Fully_Conformant then
5937 -- Names must match. Error message is more accurate if we do
5938 -- this before checking that the types of the formals match.
5940 if Chars (Old_Formal) /= Chars (New_Formal) then
5941 Conformance_Error ("\name & does not match!", New_Formal);
5943 -- Set error posted flag on new formal as well to stop
5944 -- junk cascaded messages in some cases.
5946 Set_Error_Posted (New_Formal);
5950 -- Null exclusion must match
5952 if Null_Exclusion_Present (Parent (Old_Formal))
5954 Null_Exclusion_Present (Parent (New_Formal))
5956 -- Only give error if both come from source. This should be
5957 -- investigated some time, since it should not be needed ???
5959 if Comes_From_Source (Old_Formal)
5961 Comes_From_Source (New_Formal)
5964 ("\null exclusion for & does not match", New_Formal);
5966 -- Mark error posted on the new formal to avoid duplicated
5967 -- complaint about types not matching.
5969 Set_Error_Posted (New_Formal);
5974 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5975 -- case occurs whenever a subprogram is being renamed and one of its
5976 -- parameters imposes a null exclusion. For example:
5978 -- type T is null record;
5979 -- type Acc_T is access T;
5980 -- subtype Acc_T_Sub is Acc_T;
5982 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5983 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5986 Old_Formal_Base := Etype (Old_Formal);
5987 New_Formal_Base := Etype (New_Formal);
5990 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5991 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5994 Access_Types_Match := Ada_Version >= Ada_2005
5996 -- Ensure that this rule is only applied when New_Id is a
5997 -- renaming of Old_Id.
5999 and then Nkind (Parent (Parent (New_Id))) =
6000 N_Subprogram_Renaming_Declaration
6001 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
6002 and then Present (Entity (Name (Parent (Parent (New_Id)))))
6003 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
6005 -- Now handle the allowed access-type case
6007 and then Is_Access_Type (Old_Formal_Base)
6008 and then Is_Access_Type (New_Formal_Base)
6010 -- The type kinds must match. The only exception occurs with
6011 -- multiple generics of the form:
6014 -- type F is private; type A is private;
6015 -- type F_Ptr is access F; type A_Ptr is access A;
6016 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6017 -- package F_Pack is ... package A_Pack is
6018 -- package F_Inst is
6019 -- new F_Pack (A, A_Ptr, A_P);
6021 -- When checking for conformance between the parameters of A_P
6022 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6023 -- because the compiler has transformed A_Ptr into a subtype of
6024 -- F_Ptr. We catch this case in the code below.
6026 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
6028 (Is_Generic_Type (Old_Formal_Base)
6029 and then Is_Generic_Type (New_Formal_Base)
6030 and then Is_Internal (New_Formal_Base)
6031 and then Etype (Etype (New_Formal_Base)) =
6033 and then Directly_Designated_Type (Old_Formal_Base) =
6034 Directly_Designated_Type (New_Formal_Base)
6035 and then ((Is_Itype (Old_Formal_Base)
6036 and then Can_Never_Be_Null (Old_Formal_Base))
6038 (Is_Itype (New_Formal_Base)
6039 and then Can_Never_Be_Null (New_Formal_Base)));
6041 -- Types must always match. In the visible part of an instance,
6042 -- usual overloading rules for dispatching operations apply, and
6043 -- we check base types (not the actual subtypes).
6045 if In_Instance_Visible_Part
6046 and then Is_Dispatching_Operation (New_Id)
6048 if not Conforming_Types
6049 (T1 => Base_Type (Etype (Old_Formal)),
6050 T2 => Base_Type (Etype (New_Formal)),
6052 Get_Inst => Get_Inst)
6053 and then not Access_Types_Match
6055 Conformance_Error ("\type of & does not match!", New_Formal);
6059 elsif not Conforming_Types
6060 (T1 => Old_Formal_Base,
6061 T2 => New_Formal_Base,
6063 Get_Inst => Get_Inst)
6064 and then not Access_Types_Match
6066 -- Don't give error message if old type is Any_Type. This test
6067 -- avoids some cascaded errors, e.g. in case of a bad spec.
6069 if Errmsg and then Old_Formal_Base = Any_Type then
6072 Conformance_Error ("\type of & does not match!", New_Formal);
6078 -- For mode conformance, mode must match
6080 if Ctype >= Mode_Conformant then
6081 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6082 if not Ekind_In (New_Id, E_Function, E_Procedure)
6083 or else not Is_Primitive_Wrapper (New_Id)
6085 Conformance_Error ("\mode of & does not match!", New_Formal);
6089 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6091 if Is_Protected_Type
6092 (Corresponding_Concurrent_Type (T))
6094 Error_Msg_PT (T, New_Id);
6097 ("\mode of & does not match!", New_Formal);
6104 -- Part of mode conformance for access types is having the same
6105 -- constant modifier.
6107 elsif Access_Types_Match
6108 and then Is_Access_Constant (Old_Formal_Base) /=
6109 Is_Access_Constant (New_Formal_Base)
6112 ("\constant modifier does not match!", New_Formal);
6117 if Ctype >= Subtype_Conformant then
6119 -- Ada 2005 (AI-231): In case of anonymous access types check
6120 -- the null-exclusion and access-to-constant attributes must
6121 -- match. For null exclusion, we test the types rather than the
6122 -- formals themselves, since the attribute is only set reliably
6123 -- on the formals in the Ada 95 case, and we exclude the case
6124 -- where Old_Formal is marked as controlling, to avoid errors
6125 -- when matching completing bodies with dispatching declarations
6126 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6128 if Ada_Version >= Ada_2005
6129 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6130 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6132 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6133 Can_Never_Be_Null (Etype (New_Formal))
6135 not Is_Controlling_Formal (Old_Formal))
6137 Is_Access_Constant (Etype (Old_Formal)) /=
6138 Is_Access_Constant (Etype (New_Formal)))
6140 -- Do not complain if error already posted on New_Formal. This
6141 -- avoids some redundant error messages.
6143 and then not Error_Posted (New_Formal)
6145 -- It is allowed to omit the null-exclusion in case of stream
6146 -- attribute subprograms. We recognize stream subprograms
6147 -- through their TSS-generated suffix.
6150 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6153 if TSS_Name /= TSS_Stream_Read
6154 and then TSS_Name /= TSS_Stream_Write
6155 and then TSS_Name /= TSS_Stream_Input
6156 and then TSS_Name /= TSS_Stream_Output
6158 -- Here we have a definite conformance error. It is worth
6159 -- special casing the error message for the case of a
6160 -- controlling formal (which excludes null).
6162 if Is_Controlling_Formal (New_Formal) then
6163 Error_Msg_Node_2 := Scope (New_Formal);
6165 ("\controlling formal& of& excludes null, "
6166 & "declaration must exclude null as well",
6169 -- Normal case (couldn't we give more detail here???)
6173 ("\type of & does not match!", New_Formal);
6182 -- Full conformance checks
6184 if Ctype = Fully_Conformant then
6186 -- We have checked already that names match
6188 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6190 -- Check default expressions for in parameters
6193 NewD : constant Boolean :=
6194 Present (Default_Value (New_Formal));
6195 OldD : constant Boolean :=
6196 Present (Default_Value (Old_Formal));
6198 if NewD or OldD then
6200 -- The old default value has been analyzed because the
6201 -- current full declaration will have frozen everything
6202 -- before. The new default value has not been analyzed,
6203 -- so analyze it now before we check for conformance.
6206 Push_Scope (New_Id);
6207 Preanalyze_Spec_Expression
6208 (Default_Value (New_Formal), Etype (New_Formal));
6212 if not (NewD and OldD)
6213 or else not Fully_Conformant_Expressions
6214 (Default_Value (Old_Formal),
6215 Default_Value (New_Formal))
6218 ("\default expression for & does not match!",
6227 -- A couple of special checks for Ada 83 mode. These checks are
6228 -- skipped if either entity is an operator in package Standard,
6229 -- or if either old or new instance is not from the source program.
6231 if Ada_Version = Ada_83
6232 and then Sloc (Old_Id) > Standard_Location
6233 and then Sloc (New_Id) > Standard_Location
6234 and then Comes_From_Source (Old_Id)
6235 and then Comes_From_Source (New_Id)
6238 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6239 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6242 -- Explicit IN must be present or absent in both cases. This
6243 -- test is required only in the full conformance case.
6245 if In_Present (Old_Param) /= In_Present (New_Param)
6246 and then Ctype = Fully_Conformant
6249 ("\(Ada 83) IN must appear in both declarations",
6254 -- Grouping (use of comma in param lists) must be the same
6255 -- This is where we catch a misconformance like:
6258 -- A : Integer; B : Integer
6260 -- which are represented identically in the tree except
6261 -- for the setting of the flags More_Ids and Prev_Ids.
6263 if More_Ids (Old_Param) /= More_Ids (New_Param)
6264 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6267 ("\grouping of & does not match!", New_Formal);
6273 -- This label is required when skipping controlling formals
6275 <<Skip_Controlling_Formal>>
6277 Next_Formal (Old_Formal);
6278 Next_Formal (New_Formal);
6281 if Present (Old_Formal) then
6282 Conformance_Error ("\too few parameters!");
6285 elsif Present (New_Formal) then
6286 Conformance_Error ("\too many parameters!", New_Formal);
6289 end Check_Conformance;
6291 -----------------------
6292 -- Check_Conventions --
6293 -----------------------
6295 procedure Check_Conventions (Typ : Entity_Id) is
6296 Ifaces_List : Elist_Id;
6298 procedure Check_Convention (Op : Entity_Id);
6299 -- Verify that the convention of inherited dispatching operation Op is
6300 -- consistent among all subprograms it overrides. In order to minimize
6301 -- the search, Search_From is utilized to designate a specific point in
6302 -- the list rather than iterating over the whole list once more.
6304 ----------------------
6305 -- Check_Convention --
6306 ----------------------
6308 procedure Check_Convention (Op : Entity_Id) is
6309 function Convention_Of (Id : Entity_Id) return Convention_Id;
6310 -- Given an entity, return its convention. The function treats Ghost
6311 -- as convention Ada because the two have the same dynamic semantics.
6317 function Convention_Of (Id : Entity_Id) return Convention_Id is
6318 Conv : constant Convention_Id := Convention (Id);
6320 if Conv = Convention_Ghost then
6321 return Convention_Ada;
6329 Op_Conv : constant Convention_Id := Convention_Of (Op);
6330 Iface_Conv : Convention_Id;
6331 Iface_Elmt : Elmt_Id;
6332 Iface_Prim_Elmt : Elmt_Id;
6333 Iface_Prim : Entity_Id;
6335 -- Start of processing for Check_Convention
6338 Iface_Elmt := First_Elmt (Ifaces_List);
6339 while Present (Iface_Elmt) loop
6341 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6342 while Present (Iface_Prim_Elmt) loop
6343 Iface_Prim := Node (Iface_Prim_Elmt);
6344 Iface_Conv := Convention_Of (Iface_Prim);
6346 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6347 and then Iface_Conv /= Op_Conv
6350 ("inconsistent conventions in primitive operations", Typ);
6352 Error_Msg_Name_1 := Chars (Op);
6353 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6354 Error_Msg_Sloc := Sloc (Op);
6356 if Comes_From_Source (Op) or else No (Alias (Op)) then
6357 if not Present (Overridden_Operation (Op)) then
6358 Error_Msg_N ("\\primitive % defined #", Typ);
6361 ("\\overriding operation % with " &
6362 "convention % defined #", Typ);
6365 else pragma Assert (Present (Alias (Op)));
6366 Error_Msg_Sloc := Sloc (Alias (Op));
6368 ("\\inherited operation % with " &
6369 "convention % defined #", Typ);
6372 Error_Msg_Name_1 := Chars (Op);
6373 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6374 Error_Msg_Sloc := Sloc (Iface_Prim);
6376 ("\\overridden operation % with " &
6377 "convention % defined #", Typ);
6379 -- Avoid cascading errors
6384 Next_Elmt (Iface_Prim_Elmt);
6387 Next_Elmt (Iface_Elmt);
6389 end Check_Convention;
6393 Prim_Op : Entity_Id;
6394 Prim_Op_Elmt : Elmt_Id;
6396 -- Start of processing for Check_Conventions
6399 if not Has_Interfaces (Typ) then
6403 Collect_Interfaces (Typ, Ifaces_List);
6405 -- The algorithm checks every overriding dispatching operation against
6406 -- all the corresponding overridden dispatching operations, detecting
6407 -- differences in conventions.
6409 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6410 while Present (Prim_Op_Elmt) loop
6411 Prim_Op := Node (Prim_Op_Elmt);
6413 -- A small optimization: skip the predefined dispatching operations
6414 -- since they always have the same convention.
6416 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6417 Check_Convention (Prim_Op);
6420 Next_Elmt (Prim_Op_Elmt);
6422 end Check_Conventions;
6424 ------------------------------
6425 -- Check_Delayed_Subprogram --
6426 ------------------------------
6428 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6431 procedure Possible_Freeze (T : Entity_Id);
6432 -- T is the type of either a formal parameter or of the return type.
6433 -- If T is not yet frozen and needs a delayed freeze, then the
6434 -- subprogram itself must be delayed. If T is the limited view of an
6435 -- incomplete type the subprogram must be frozen as well, because
6436 -- T may depend on local types that have not been frozen yet.
6438 ---------------------
6439 -- Possible_Freeze --
6440 ---------------------
6442 procedure Possible_Freeze (T : Entity_Id) is
6444 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6445 Set_Has_Delayed_Freeze (Designator);
6447 elsif Is_Access_Type (T)
6448 and then Has_Delayed_Freeze (Designated_Type (T))
6449 and then not Is_Frozen (Designated_Type (T))
6451 Set_Has_Delayed_Freeze (Designator);
6453 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
6454 Set_Has_Delayed_Freeze (Designator);
6456 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6457 -- of a subprogram or entry declaration.
6459 elsif Ekind (T) = E_Incomplete_Type
6460 and then Ada_Version >= Ada_2012
6462 Set_Has_Delayed_Freeze (Designator);
6465 end Possible_Freeze;
6467 -- Start of processing for Check_Delayed_Subprogram
6470 -- All subprograms, including abstract subprograms, may need a freeze
6471 -- node if some formal type or the return type needs one.
6473 Possible_Freeze (Etype (Designator));
6474 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6476 -- Need delayed freeze if any of the formal types themselves need
6477 -- a delayed freeze and are not yet frozen.
6479 F := First_Formal (Designator);
6480 while Present (F) loop
6481 Possible_Freeze (Etype (F));
6482 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6486 -- Mark functions that return by reference. Note that it cannot be
6487 -- done for delayed_freeze subprograms because the underlying
6488 -- returned type may not be known yet (for private types)
6490 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6492 Typ : constant Entity_Id := Etype (Designator);
6493 Utyp : constant Entity_Id := Underlying_Type (Typ);
6495 if Is_Immutably_Limited_Type (Typ) then
6496 Set_Returns_By_Ref (Designator);
6497 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6498 Set_Returns_By_Ref (Designator);
6502 end Check_Delayed_Subprogram;
6504 ------------------------------------
6505 -- Check_Discriminant_Conformance --
6506 ------------------------------------
6508 procedure Check_Discriminant_Conformance
6513 Old_Discr : Entity_Id := First_Discriminant (Prev);
6514 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6515 New_Discr_Id : Entity_Id;
6516 New_Discr_Type : Entity_Id;
6518 procedure Conformance_Error (Msg : String; N : Node_Id);
6519 -- Post error message for conformance error on given node. Two messages
6520 -- are output. The first points to the previous declaration with a
6521 -- general "no conformance" message. The second is the detailed reason,
6522 -- supplied as Msg. The parameter N provide information for a possible
6523 -- & insertion in the message.
6525 -----------------------
6526 -- Conformance_Error --
6527 -----------------------
6529 procedure Conformance_Error (Msg : String; N : Node_Id) is
6531 Error_Msg_Sloc := Sloc (Prev_Loc);
6532 Error_Msg_N -- CODEFIX
6533 ("not fully conformant with declaration#!", N);
6534 Error_Msg_NE (Msg, N, N);
6535 end Conformance_Error;
6537 -- Start of processing for Check_Discriminant_Conformance
6540 while Present (Old_Discr) and then Present (New_Discr) loop
6541 New_Discr_Id := Defining_Identifier (New_Discr);
6543 -- The subtype mark of the discriminant on the full type has not
6544 -- been analyzed so we do it here. For an access discriminant a new
6547 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6549 Access_Definition (N, Discriminant_Type (New_Discr));
6552 Analyze (Discriminant_Type (New_Discr));
6553 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6555 -- Ada 2005: if the discriminant definition carries a null
6556 -- exclusion, create an itype to check properly for consistency
6557 -- with partial declaration.
6559 if Is_Access_Type (New_Discr_Type)
6560 and then Null_Exclusion_Present (New_Discr)
6563 Create_Null_Excluding_Itype
6564 (T => New_Discr_Type,
6565 Related_Nod => New_Discr,
6566 Scope_Id => Current_Scope);
6570 if not Conforming_Types
6571 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6573 Conformance_Error ("type of & does not match!", New_Discr_Id);
6576 -- Treat the new discriminant as an occurrence of the old one,
6577 -- for navigation purposes, and fill in some semantic
6578 -- information, for completeness.
6580 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6581 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6582 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6587 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6588 Conformance_Error ("name & does not match!", New_Discr_Id);
6592 -- Default expressions must match
6595 NewD : constant Boolean :=
6596 Present (Expression (New_Discr));
6597 OldD : constant Boolean :=
6598 Present (Expression (Parent (Old_Discr)));
6601 if NewD or OldD then
6603 -- The old default value has been analyzed and expanded,
6604 -- because the current full declaration will have frozen
6605 -- everything before. The new default values have not been
6606 -- expanded, so expand now to check conformance.
6609 Preanalyze_Spec_Expression
6610 (Expression (New_Discr), New_Discr_Type);
6613 if not (NewD and OldD)
6614 or else not Fully_Conformant_Expressions
6615 (Expression (Parent (Old_Discr)),
6616 Expression (New_Discr))
6620 ("default expression for & does not match!",
6627 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6629 if Ada_Version = Ada_83 then
6631 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6634 -- Grouping (use of comma in param lists) must be the same
6635 -- This is where we catch a misconformance like:
6638 -- A : Integer; B : Integer
6640 -- which are represented identically in the tree except
6641 -- for the setting of the flags More_Ids and Prev_Ids.
6643 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6644 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6647 ("grouping of & does not match!", New_Discr_Id);
6653 Next_Discriminant (Old_Discr);
6657 if Present (Old_Discr) then
6658 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6661 elsif Present (New_Discr) then
6663 ("too many discriminants!", Defining_Identifier (New_Discr));
6666 end Check_Discriminant_Conformance;
6668 ----------------------------
6669 -- Check_Fully_Conformant --
6670 ----------------------------
6672 procedure Check_Fully_Conformant
6673 (New_Id : Entity_Id;
6675 Err_Loc : Node_Id := Empty)
6678 pragma Warnings (Off, Result);
6681 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6682 end Check_Fully_Conformant;
6684 ---------------------------
6685 -- Check_Mode_Conformant --
6686 ---------------------------
6688 procedure Check_Mode_Conformant
6689 (New_Id : Entity_Id;
6691 Err_Loc : Node_Id := Empty;
6692 Get_Inst : Boolean := False)
6695 pragma Warnings (Off, Result);
6698 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6699 end Check_Mode_Conformant;
6701 --------------------------------
6702 -- Check_Overriding_Indicator --
6703 --------------------------------
6705 procedure Check_Overriding_Indicator
6707 Overridden_Subp : Entity_Id;
6708 Is_Primitive : Boolean)
6714 -- No overriding indicator for literals
6716 if Ekind (Subp) = E_Enumeration_Literal then
6719 elsif Ekind (Subp) = E_Entry then
6720 Decl := Parent (Subp);
6722 -- No point in analyzing a malformed operator
6724 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6725 and then Error_Posted (Subp)
6730 Decl := Unit_Declaration_Node (Subp);
6733 if Nkind_In (Decl, N_Subprogram_Body,
6734 N_Subprogram_Body_Stub,
6735 N_Subprogram_Declaration,
6736 N_Abstract_Subprogram_Declaration,
6737 N_Subprogram_Renaming_Declaration)
6739 Spec := Specification (Decl);
6741 elsif Nkind (Decl) = N_Entry_Declaration then
6748 -- The overriding operation is type conformant with the overridden one,
6749 -- but the names of the formals are not required to match. If the names
6750 -- appear permuted in the overriding operation, this is a possible
6751 -- source of confusion that is worth diagnosing. Controlling formals
6752 -- often carry names that reflect the type, and it is not worthwhile
6753 -- requiring that their names match.
6755 if Present (Overridden_Subp)
6756 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6763 Form1 := First_Formal (Subp);
6764 Form2 := First_Formal (Overridden_Subp);
6766 -- If the overriding operation is a synchronized operation, skip
6767 -- the first parameter of the overridden operation, which is
6768 -- implicit in the new one. If the operation is declared in the
6769 -- body it is not primitive and all formals must match.
6771 if Is_Concurrent_Type (Scope (Subp))
6772 and then Is_Tagged_Type (Scope (Subp))
6773 and then not Has_Completion (Scope (Subp))
6775 Form2 := Next_Formal (Form2);
6778 if Present (Form1) then
6779 Form1 := Next_Formal (Form1);
6780 Form2 := Next_Formal (Form2);
6783 while Present (Form1) loop
6784 if not Is_Controlling_Formal (Form1)
6785 and then Present (Next_Formal (Form2))
6786 and then Chars (Form1) = Chars (Next_Formal (Form2))
6788 Error_Msg_Node_2 := Alias (Overridden_Subp);
6789 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6791 ("& does not match corresponding formal of&#",
6796 Next_Formal (Form1);
6797 Next_Formal (Form2);
6802 -- If there is an overridden subprogram, then check that there is no
6803 -- "not overriding" indicator, and mark the subprogram as overriding.
6804 -- This is not done if the overridden subprogram is marked as hidden,
6805 -- which can occur for the case of inherited controlled operations
6806 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6807 -- subprogram is not itself hidden. (Note: This condition could probably
6808 -- be simplified, leaving out the testing for the specific controlled
6809 -- cases, but it seems safer and clearer this way, and echoes similar
6810 -- special-case tests of this kind in other places.)
6812 if Present (Overridden_Subp)
6813 and then (not Is_Hidden (Overridden_Subp)
6815 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6818 and then Present (Alias (Overridden_Subp))
6819 and then not Is_Hidden (Alias (Overridden_Subp))))
6821 if Must_Not_Override (Spec) then
6822 Error_Msg_Sloc := Sloc (Overridden_Subp);
6824 if Ekind (Subp) = E_Entry then
6826 ("entry & overrides inherited operation #", Spec, Subp);
6829 ("subprogram & overrides inherited operation #", Spec, Subp);
6832 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6833 -- as an extension of Root_Controlled, and thus has a useless Adjust
6834 -- operation. This operation should not be inherited by other limited
6835 -- controlled types. An explicit Adjust for them is not overriding.
6837 elsif Must_Override (Spec)
6838 and then Chars (Overridden_Subp) = Name_Adjust
6839 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6840 and then Present (Alias (Overridden_Subp))
6842 Is_Predefined_File_Name
6843 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6845 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6847 elsif Is_Subprogram (Subp) then
6848 if Is_Init_Proc (Subp) then
6851 elsif No (Overridden_Operation (Subp)) then
6853 -- For entities generated by Derive_Subprograms the overridden
6854 -- operation is the inherited primitive (which is available
6855 -- through the attribute alias)
6857 if (Is_Dispatching_Operation (Subp)
6858 or else Is_Dispatching_Operation (Overridden_Subp))
6859 and then not Comes_From_Source (Overridden_Subp)
6860 and then Find_Dispatching_Type (Overridden_Subp) =
6861 Find_Dispatching_Type (Subp)
6862 and then Present (Alias (Overridden_Subp))
6863 and then Comes_From_Source (Alias (Overridden_Subp))
6865 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6868 Set_Overridden_Operation (Subp, Overridden_Subp);
6873 -- If primitive flag is set or this is a protected operation, then
6874 -- the operation is overriding at the point of its declaration, so
6875 -- warn if necessary. Otherwise it may have been declared before the
6876 -- operation it overrides and no check is required.
6879 and then not Must_Override (Spec)
6880 and then (Is_Primitive
6881 or else Ekind (Scope (Subp)) = E_Protected_Type)
6883 Style.Missing_Overriding (Decl, Subp);
6886 -- If Subp is an operator, it may override a predefined operation, if
6887 -- it is defined in the same scope as the type to which it applies.
6888 -- In that case Overridden_Subp is empty because of our implicit
6889 -- representation for predefined operators. We have to check whether the
6890 -- signature of Subp matches that of a predefined operator. Note that
6891 -- first argument provides the name of the operator, and the second
6892 -- argument the signature that may match that of a standard operation.
6893 -- If the indicator is overriding, then the operator must match a
6894 -- predefined signature, because we know already that there is no
6895 -- explicit overridden operation.
6897 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6898 if Must_Not_Override (Spec) then
6900 -- If this is not a primitive or a protected subprogram, then
6901 -- "not overriding" is illegal.
6904 and then Ekind (Scope (Subp)) /= E_Protected_Type
6907 ("overriding indicator only allowed "
6908 & "if subprogram is primitive", Subp);
6910 elsif Can_Override_Operator (Subp) then
6912 ("subprogram& overrides predefined operator ", Spec, Subp);
6915 elsif Must_Override (Spec) then
6916 if No (Overridden_Operation (Subp))
6917 and then not Can_Override_Operator (Subp)
6919 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6922 elsif not Error_Posted (Subp)
6923 and then Style_Check
6924 and then Can_Override_Operator (Subp)
6926 not Is_Predefined_File_Name
6927 (Unit_File_Name (Get_Source_Unit (Subp)))
6929 -- If style checks are enabled, indicate that the indicator is
6930 -- missing. However, at the point of declaration, the type of
6931 -- which this is a primitive operation may be private, in which
6932 -- case the indicator would be premature.
6934 if Has_Private_Declaration (Etype (Subp))
6935 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6939 Style.Missing_Overriding (Decl, Subp);
6943 elsif Must_Override (Spec) then
6944 if Ekind (Subp) = E_Entry then
6945 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6947 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6950 -- If the operation is marked "not overriding" and it's not primitive
6951 -- then an error is issued, unless this is an operation of a task or
6952 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6953 -- has been specified have already been checked above.
6955 elsif Must_Not_Override (Spec)
6956 and then not Is_Primitive
6957 and then Ekind (Subp) /= E_Entry
6958 and then Ekind (Scope (Subp)) /= E_Protected_Type
6961 ("overriding indicator only allowed if subprogram is primitive",
6965 end Check_Overriding_Indicator;
6971 -- Note: this procedure needs to know far too much about how the expander
6972 -- messes with exceptions. The use of the flag Exception_Junk and the
6973 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6974 -- works, but is not very clean. It would be better if the expansion
6975 -- routines would leave Original_Node working nicely, and we could use
6976 -- Original_Node here to ignore all the peculiar expander messing ???
6978 procedure Check_Returns
6982 Proc : Entity_Id := Empty)
6986 procedure Check_Statement_Sequence (L : List_Id);
6987 -- Internal recursive procedure to check a list of statements for proper
6988 -- termination by a return statement (or a transfer of control or a
6989 -- compound statement that is itself internally properly terminated).
6991 ------------------------------
6992 -- Check_Statement_Sequence --
6993 ------------------------------
6995 procedure Check_Statement_Sequence (L : List_Id) is
7000 Raise_Exception_Call : Boolean;
7001 -- Set True if statement sequence terminated by Raise_Exception call
7002 -- or a Reraise_Occurrence call.
7005 Raise_Exception_Call := False;
7007 -- Get last real statement
7009 Last_Stm := Last (L);
7011 -- Deal with digging out exception handler statement sequences that
7012 -- have been transformed by the local raise to goto optimization.
7013 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7014 -- optimization has occurred, we are looking at something like:
7017 -- original stmts in block
7021 -- goto L1; | omitted if No_Exception_Propagation
7026 -- goto L3; -- skip handler when exception not raised
7028 -- <<L1>> -- target label for local exception
7042 -- and what we have to do is to dig out the estmts1 and estmts2
7043 -- sequences (which were the original sequences of statements in
7044 -- the exception handlers) and check them.
7046 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7051 exit when Nkind (Stm) /= N_Block_Statement;
7052 exit when not Exception_Junk (Stm);
7055 exit when Nkind (Stm) /= N_Label;
7056 exit when not Exception_Junk (Stm);
7057 Check_Statement_Sequence
7058 (Statements (Handled_Statement_Sequence (Next (Stm))));
7063 exit when Nkind (Stm) /= N_Goto_Statement;
7064 exit when not Exception_Junk (Stm);
7068 -- Don't count pragmas
7070 while Nkind (Last_Stm) = N_Pragma
7072 -- Don't count call to SS_Release (can happen after Raise_Exception)
7075 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7077 Nkind (Name (Last_Stm)) = N_Identifier
7079 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7081 -- Don't count exception junk
7084 (Nkind_In (Last_Stm, N_Goto_Statement,
7086 N_Object_Declaration)
7087 and then Exception_Junk (Last_Stm))
7088 or else Nkind (Last_Stm) in N_Push_xxx_Label
7089 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7091 -- Inserted code, such as finalization calls, is irrelevant: we only
7092 -- need to check original source.
7094 or else Is_Rewrite_Insertion (Last_Stm)
7099 -- Here we have the "real" last statement
7101 Kind := Nkind (Last_Stm);
7103 -- Transfer of control, OK. Note that in the No_Return procedure
7104 -- case, we already diagnosed any explicit return statements, so
7105 -- we can treat them as OK in this context.
7107 if Is_Transfer (Last_Stm) then
7110 -- Check cases of explicit non-indirect procedure calls
7112 elsif Kind = N_Procedure_Call_Statement
7113 and then Is_Entity_Name (Name (Last_Stm))
7115 -- Check call to Raise_Exception procedure which is treated
7116 -- specially, as is a call to Reraise_Occurrence.
7118 -- We suppress the warning in these cases since it is likely that
7119 -- the programmer really does not expect to deal with the case
7120 -- of Null_Occurrence, and thus would find a warning about a
7121 -- missing return curious, and raising Program_Error does not
7122 -- seem such a bad behavior if this does occur.
7124 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7125 -- behavior will be to raise Constraint_Error (see AI-329).
7127 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7129 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7131 Raise_Exception_Call := True;
7133 -- For Raise_Exception call, test first argument, if it is
7134 -- an attribute reference for a 'Identity call, then we know
7135 -- that the call cannot possibly return.
7138 Arg : constant Node_Id :=
7139 Original_Node (First_Actual (Last_Stm));
7141 if Nkind (Arg) = N_Attribute_Reference
7142 and then Attribute_Name (Arg) = Name_Identity
7149 -- If statement, need to look inside if there is an else and check
7150 -- each constituent statement sequence for proper termination.
7152 elsif Kind = N_If_Statement
7153 and then Present (Else_Statements (Last_Stm))
7155 Check_Statement_Sequence (Then_Statements (Last_Stm));
7156 Check_Statement_Sequence (Else_Statements (Last_Stm));
7158 if Present (Elsif_Parts (Last_Stm)) then
7160 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7163 while Present (Elsif_Part) loop
7164 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7172 -- Case statement, check each case for proper termination
7174 elsif Kind = N_Case_Statement then
7178 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7179 while Present (Case_Alt) loop
7180 Check_Statement_Sequence (Statements (Case_Alt));
7181 Next_Non_Pragma (Case_Alt);
7187 -- Block statement, check its handled sequence of statements
7189 elsif Kind = N_Block_Statement then
7195 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7204 -- Loop statement. If there is an iteration scheme, we can definitely
7205 -- fall out of the loop. Similarly if there is an exit statement, we
7206 -- can fall out. In either case we need a following return.
7208 elsif Kind = N_Loop_Statement then
7209 if Present (Iteration_Scheme (Last_Stm))
7210 or else Has_Exit (Entity (Identifier (Last_Stm)))
7214 -- A loop with no exit statement or iteration scheme is either
7215 -- an infinite loop, or it has some other exit (raise/return).
7216 -- In either case, no warning is required.
7222 -- Timed entry call, check entry call and delay alternatives
7224 -- Note: in expanded code, the timed entry call has been converted
7225 -- to a set of expanded statements on which the check will work
7226 -- correctly in any case.
7228 elsif Kind = N_Timed_Entry_Call then
7230 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7231 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7234 -- If statement sequence of entry call alternative is missing,
7235 -- then we can definitely fall through, and we post the error
7236 -- message on the entry call alternative itself.
7238 if No (Statements (ECA)) then
7241 -- If statement sequence of delay alternative is missing, then
7242 -- we can definitely fall through, and we post the error
7243 -- message on the delay alternative itself.
7245 -- Note: if both ECA and DCA are missing the return, then we
7246 -- post only one message, should be enough to fix the bugs.
7247 -- If not we will get a message next time on the DCA when the
7250 elsif No (Statements (DCA)) then
7253 -- Else check both statement sequences
7256 Check_Statement_Sequence (Statements (ECA));
7257 Check_Statement_Sequence (Statements (DCA));
7262 -- Conditional entry call, check entry call and else part
7264 -- Note: in expanded code, the conditional entry call has been
7265 -- converted to a set of expanded statements on which the check
7266 -- will work correctly in any case.
7268 elsif Kind = N_Conditional_Entry_Call then
7270 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7273 -- If statement sequence of entry call alternative is missing,
7274 -- then we can definitely fall through, and we post the error
7275 -- message on the entry call alternative itself.
7277 if No (Statements (ECA)) then
7280 -- Else check statement sequence and else part
7283 Check_Statement_Sequence (Statements (ECA));
7284 Check_Statement_Sequence (Else_Statements (Last_Stm));
7290 -- If we fall through, issue appropriate message
7293 if not Raise_Exception_Call then
7295 ("RETURN statement missing following this statement??!",
7298 ("\Program_Error may be raised at run time??!",
7302 -- Note: we set Err even though we have not issued a warning
7303 -- because we still have a case of a missing return. This is
7304 -- an extremely marginal case, probably will never be noticed
7305 -- but we might as well get it right.
7309 -- Otherwise we have the case of a procedure marked No_Return
7312 if not Raise_Exception_Call then
7314 ("implied return after this statement " &
7315 "will raise Program_Error??",
7318 ("\procedure & is marked as No_Return??!",
7323 RE : constant Node_Id :=
7324 Make_Raise_Program_Error (Sloc (Last_Stm),
7325 Reason => PE_Implicit_Return);
7327 Insert_After (Last_Stm, RE);
7331 end Check_Statement_Sequence;
7333 -- Start of processing for Check_Returns
7337 Check_Statement_Sequence (Statements (HSS));
7339 if Present (Exception_Handlers (HSS)) then
7340 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7341 while Present (Handler) loop
7342 Check_Statement_Sequence (Statements (Handler));
7343 Next_Non_Pragma (Handler);
7348 ----------------------------
7349 -- Check_Subprogram_Order --
7350 ----------------------------
7352 procedure Check_Subprogram_Order (N : Node_Id) is
7354 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7355 -- This is used to check if S1 > S2 in the sense required by this test,
7356 -- for example nameab < namec, but name2 < name10.
7358 -----------------------------
7359 -- Subprogram_Name_Greater --
7360 -----------------------------
7362 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7367 -- Deal with special case where names are identical except for a
7368 -- numerical suffix. These are handled specially, taking the numeric
7369 -- ordering from the suffix into account.
7372 while S1 (L1) in '0' .. '9' loop
7377 while S2 (L2) in '0' .. '9' loop
7381 -- If non-numeric parts non-equal, do straight compare
7383 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7386 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7387 -- that a missing suffix is treated as numeric zero in this test.
7391 while L1 < S1'Last loop
7393 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7397 while L2 < S2'Last loop
7399 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7404 end Subprogram_Name_Greater;
7406 -- Start of processing for Check_Subprogram_Order
7409 -- Check body in alpha order if this is option
7412 and then Style_Check_Order_Subprograms
7413 and then Nkind (N) = N_Subprogram_Body
7414 and then Comes_From_Source (N)
7415 and then In_Extended_Main_Source_Unit (N)
7419 renames Scope_Stack.Table
7420 (Scope_Stack.Last).Last_Subprogram_Name;
7422 Body_Id : constant Entity_Id :=
7423 Defining_Entity (Specification (N));
7426 Get_Decoded_Name_String (Chars (Body_Id));
7429 if Subprogram_Name_Greater
7430 (LSN.all, Name_Buffer (1 .. Name_Len))
7432 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7438 LSN := new String'(Name_Buffer (1 .. Name_Len));
7441 end Check_Subprogram_Order;
7443 ------------------------------
7444 -- Check_Subtype_Conformant --
7445 ------------------------------
7447 procedure Check_Subtype_Conformant
7448 (New_Id : Entity_Id;
7450 Err_Loc : Node_Id := Empty;
7451 Skip_Controlling_Formals : Boolean := False;
7452 Get_Inst : Boolean := False)
7455 pragma Warnings (Off, Result);
7458 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7459 Skip_Controlling_Formals => Skip_Controlling_Formals,
7460 Get_Inst => Get_Inst);
7461 end Check_Subtype_Conformant;
7463 ---------------------------
7464 -- Check_Type_Conformant --
7465 ---------------------------
7467 procedure Check_Type_Conformant
7468 (New_Id : Entity_Id;
7470 Err_Loc : Node_Id := Empty)
7473 pragma Warnings (Off, Result);
7476 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7477 end Check_Type_Conformant;
7479 ---------------------------
7480 -- Can_Override_Operator --
7481 ---------------------------
7483 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7487 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7491 Typ := Base_Type (Etype (First_Formal (Subp)));
7493 -- Check explicitly that the operation is a primitive of the type
7495 return Operator_Matches_Spec (Subp, Subp)
7496 and then not Is_Generic_Type (Typ)
7497 and then Scope (Subp) = Scope (Typ)
7498 and then not Is_Class_Wide_Type (Typ);
7500 end Can_Override_Operator;
7502 ----------------------
7503 -- Conforming_Types --
7504 ----------------------
7506 function Conforming_Types
7509 Ctype : Conformance_Type;
7510 Get_Inst : Boolean := False) return Boolean
7512 Type_1 : Entity_Id := T1;
7513 Type_2 : Entity_Id := T2;
7514 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7516 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7517 -- If neither T1 nor T2 are generic actual types, or if they are in
7518 -- different scopes (e.g. parent and child instances), then verify that
7519 -- the base types are equal. Otherwise T1 and T2 must be on the same
7520 -- subtype chain. The whole purpose of this procedure is to prevent
7521 -- spurious ambiguities in an instantiation that may arise if two
7522 -- distinct generic types are instantiated with the same actual.
7524 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7525 -- An access parameter can designate an incomplete type. If the
7526 -- incomplete type is the limited view of a type from a limited_
7527 -- with_clause, check whether the non-limited view is available. If
7528 -- it is a (non-limited) incomplete type, get the full view.
7530 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7531 -- Returns True if and only if either T1 denotes a limited view of T2
7532 -- or T2 denotes a limited view of T1. This can arise when the limited
7533 -- with view of a type is used in a subprogram declaration and the
7534 -- subprogram body is in the scope of a regular with clause for the
7535 -- same unit. In such a case, the two type entities can be considered
7536 -- identical for purposes of conformance checking.
7538 ----------------------
7539 -- Base_Types_Match --
7540 ----------------------
7542 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7543 BT1 : constant Entity_Id := Base_Type (T1);
7544 BT2 : constant Entity_Id := Base_Type (T2);
7550 elsif BT1 = BT2 then
7552 -- The following is too permissive. A more precise test should
7553 -- check that the generic actual is an ancestor subtype of the
7556 -- See code in Find_Corresponding_Spec that applies an additional
7557 -- filter to handle accidental amiguities in instances.
7559 return not Is_Generic_Actual_Type (T1)
7560 or else not Is_Generic_Actual_Type (T2)
7561 or else Scope (T1) /= Scope (T2);
7563 -- If T2 is a generic actual type it is declared as the subtype of
7564 -- the actual. If that actual is itself a subtype we need to use its
7565 -- own base type to check for compatibility.
7567 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7570 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7576 end Base_Types_Match;
7578 --------------------------
7579 -- Find_Designated_Type --
7580 --------------------------
7582 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7586 Desig := Directly_Designated_Type (T);
7588 if Ekind (Desig) = E_Incomplete_Type then
7590 -- If regular incomplete type, get full view if available
7592 if Present (Full_View (Desig)) then
7593 Desig := Full_View (Desig);
7595 -- If limited view of a type, get non-limited view if available,
7596 -- and check again for a regular incomplete type.
7598 elsif Present (Non_Limited_View (Desig)) then
7599 Desig := Get_Full_View (Non_Limited_View (Desig));
7604 end Find_Designated_Type;
7606 -------------------------------
7607 -- Matches_Limited_With_View --
7608 -------------------------------
7610 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7612 -- In some cases a type imported through a limited_with clause, and
7613 -- its nonlimited view are both visible, for example in an anonymous
7614 -- access-to-class-wide type in a formal. Both entities designate the
7617 if From_With_Type (T1) and then T2 = Available_View (T1) then
7620 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7623 elsif From_With_Type (T1)
7624 and then From_With_Type (T2)
7625 and then Available_View (T1) = Available_View (T2)
7632 end Matches_Limited_With_View;
7634 -- Start of processing for Conforming_Types
7637 -- The context is an instance association for a formal access-to-
7638 -- subprogram type; the formal parameter types require mapping because
7639 -- they may denote other formal parameters of the generic unit.
7642 Type_1 := Get_Instance_Of (T1);
7643 Type_2 := Get_Instance_Of (T2);
7646 -- If one of the types is a view of the other introduced by a limited
7647 -- with clause, treat these as conforming for all purposes.
7649 if Matches_Limited_With_View (T1, T2) then
7652 elsif Base_Types_Match (Type_1, Type_2) then
7653 return Ctype <= Mode_Conformant
7654 or else Subtypes_Statically_Match (Type_1, Type_2);
7656 elsif Is_Incomplete_Or_Private_Type (Type_1)
7657 and then Present (Full_View (Type_1))
7658 and then Base_Types_Match (Full_View (Type_1), Type_2)
7660 return Ctype <= Mode_Conformant
7661 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7663 elsif Ekind (Type_2) = E_Incomplete_Type
7664 and then Present (Full_View (Type_2))
7665 and then Base_Types_Match (Type_1, Full_View (Type_2))
7667 return Ctype <= Mode_Conformant
7668 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7670 elsif Is_Private_Type (Type_2)
7671 and then In_Instance
7672 and then Present (Full_View (Type_2))
7673 and then Base_Types_Match (Type_1, Full_View (Type_2))
7675 return Ctype <= Mode_Conformant
7676 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7679 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7680 -- treated recursively because they carry a signature. As far as
7681 -- conformance is concerned, convention plays no role, and either
7682 -- or both could be access to protected subprograms.
7684 Are_Anonymous_Access_To_Subprogram_Types :=
7685 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7686 E_Anonymous_Access_Protected_Subprogram_Type)
7688 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7689 E_Anonymous_Access_Protected_Subprogram_Type);
7691 -- Test anonymous access type case. For this case, static subtype
7692 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7693 -- the base types because we may have built internal subtype entities
7694 -- to handle null-excluding types (see Process_Formals).
7696 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7698 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7700 -- Ada 2005 (AI-254)
7702 or else Are_Anonymous_Access_To_Subprogram_Types
7705 Desig_1 : Entity_Id;
7706 Desig_2 : Entity_Id;
7709 -- In Ada 2005, access constant indicators must match for
7710 -- subtype conformance.
7712 if Ada_Version >= Ada_2005
7713 and then Ctype >= Subtype_Conformant
7715 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7720 Desig_1 := Find_Designated_Type (Type_1);
7721 Desig_2 := Find_Designated_Type (Type_2);
7723 -- If the context is an instance association for a formal
7724 -- access-to-subprogram type; formal access parameter designated
7725 -- types require mapping because they may denote other formal
7726 -- parameters of the generic unit.
7729 Desig_1 := Get_Instance_Of (Desig_1);
7730 Desig_2 := Get_Instance_Of (Desig_2);
7733 -- It is possible for a Class_Wide_Type to be introduced for an
7734 -- incomplete type, in which case there is a separate class_ wide
7735 -- type for the full view. The types conform if their Etypes
7736 -- conform, i.e. one may be the full view of the other. This can
7737 -- only happen in the context of an access parameter, other uses
7738 -- of an incomplete Class_Wide_Type are illegal.
7740 if Is_Class_Wide_Type (Desig_1)
7742 Is_Class_Wide_Type (Desig_2)
7746 (Etype (Base_Type (Desig_1)),
7747 Etype (Base_Type (Desig_2)), Ctype);
7749 elsif Are_Anonymous_Access_To_Subprogram_Types then
7750 if Ada_Version < Ada_2005 then
7751 return Ctype = Type_Conformant
7753 Subtypes_Statically_Match (Desig_1, Desig_2);
7755 -- We must check the conformance of the signatures themselves
7759 Conformant : Boolean;
7762 (Desig_1, Desig_2, Ctype, False, Conformant);
7768 return Base_Type (Desig_1) = Base_Type (Desig_2)
7769 and then (Ctype = Type_Conformant
7771 Subtypes_Statically_Match (Desig_1, Desig_2));
7775 -- Otherwise definitely no match
7778 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7779 and then Is_Access_Type (Type_2))
7780 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7781 and then Is_Access_Type (Type_1)))
7784 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7786 May_Hide_Profile := True;
7791 end Conforming_Types;
7793 --------------------------
7794 -- Create_Extra_Formals --
7795 --------------------------
7797 procedure Create_Extra_Formals (E : Entity_Id) is
7799 First_Extra : Entity_Id := Empty;
7800 Last_Extra : Entity_Id;
7801 Formal_Type : Entity_Id;
7802 P_Formal : Entity_Id := Empty;
7804 function Add_Extra_Formal
7805 (Assoc_Entity : Entity_Id;
7808 Suffix : String) return Entity_Id;
7809 -- Add an extra formal to the current list of formals and extra formals.
7810 -- The extra formal is added to the end of the list of extra formals,
7811 -- and also returned as the result. These formals are always of mode IN.
7812 -- The new formal has the type Typ, is declared in Scope, and its name
7813 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7814 -- The following suffixes are currently used. They should not be changed
7815 -- without coordinating with CodePeer, which makes use of these to
7816 -- provide better messages.
7818 -- O denotes the Constrained bit.
7819 -- L denotes the accessibility level.
7820 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7821 -- the full list in exp_ch6.BIP_Formal_Kind.
7823 ----------------------
7824 -- Add_Extra_Formal --
7825 ----------------------
7827 function Add_Extra_Formal
7828 (Assoc_Entity : Entity_Id;
7831 Suffix : String) return Entity_Id
7833 EF : constant Entity_Id :=
7834 Make_Defining_Identifier (Sloc (Assoc_Entity),
7835 Chars => New_External_Name (Chars (Assoc_Entity),
7839 -- A little optimization. Never generate an extra formal for the
7840 -- _init operand of an initialization procedure, since it could
7843 if Chars (Formal) = Name_uInit then
7847 Set_Ekind (EF, E_In_Parameter);
7848 Set_Actual_Subtype (EF, Typ);
7849 Set_Etype (EF, Typ);
7850 Set_Scope (EF, Scope);
7851 Set_Mechanism (EF, Default_Mechanism);
7852 Set_Formal_Validity (EF);
7854 if No (First_Extra) then
7856 Set_Extra_Formals (Scope, First_Extra);
7859 if Present (Last_Extra) then
7860 Set_Extra_Formal (Last_Extra, EF);
7866 end Add_Extra_Formal;
7868 -- Start of processing for Create_Extra_Formals
7871 -- We never generate extra formals if expansion is not active because we
7872 -- don't need them unless we are generating code.
7874 if not Expander_Active then
7878 -- No need to generate extra formals in interface thunks whose target
7879 -- primitive has no extra formals.
7881 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7885 -- If this is a derived subprogram then the subtypes of the parent
7886 -- subprogram's formal parameters will be used to determine the need
7887 -- for extra formals.
7889 if Is_Overloadable (E) and then Present (Alias (E)) then
7890 P_Formal := First_Formal (Alias (E));
7893 Last_Extra := Empty;
7894 Formal := First_Formal (E);
7895 while Present (Formal) loop
7896 Last_Extra := Formal;
7897 Next_Formal (Formal);
7900 -- If Extra_formals were already created, don't do it again. This
7901 -- situation may arise for subprogram types created as part of
7902 -- dispatching calls (see Expand_Dispatching_Call)
7904 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7908 -- If the subprogram is a predefined dispatching subprogram then don't
7909 -- generate any extra constrained or accessibility level formals. In
7910 -- general we suppress these for internal subprograms (by not calling
7911 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7912 -- generated stream attributes do get passed through because extra
7913 -- build-in-place formals are needed in some cases (limited 'Input).
7915 if Is_Predefined_Internal_Operation (E) then
7916 goto Test_For_Func_Result_Extras;
7919 Formal := First_Formal (E);
7920 while Present (Formal) loop
7922 -- Create extra formal for supporting the attribute 'Constrained.
7923 -- The case of a private type view without discriminants also
7924 -- requires the extra formal if the underlying type has defaulted
7927 if Ekind (Formal) /= E_In_Parameter then
7928 if Present (P_Formal) then
7929 Formal_Type := Etype (P_Formal);
7931 Formal_Type := Etype (Formal);
7934 -- Do not produce extra formals for Unchecked_Union parameters.
7935 -- Jump directly to the end of the loop.
7937 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7938 goto Skip_Extra_Formal_Generation;
7941 if not Has_Discriminants (Formal_Type)
7942 and then Ekind (Formal_Type) in Private_Kind
7943 and then Present (Underlying_Type (Formal_Type))
7945 Formal_Type := Underlying_Type (Formal_Type);
7948 -- Suppress the extra formal if formal's subtype is constrained or
7949 -- indefinite, or we're compiling for Ada 2012 and the underlying
7950 -- type is tagged and limited. In Ada 2012, a limited tagged type
7951 -- can have defaulted discriminants, but 'Constrained is required
7952 -- to return True, so the formal is never needed (see AI05-0214).
7953 -- Note that this ensures consistency of calling sequences for
7954 -- dispatching operations when some types in a class have defaults
7955 -- on discriminants and others do not (and requiring the extra
7956 -- formal would introduce distributed overhead).
7958 -- If the type does not have a completion yet, treat as prior to
7959 -- Ada 2012 for consistency.
7961 if Has_Discriminants (Formal_Type)
7962 and then not Is_Constrained (Formal_Type)
7963 and then not Is_Indefinite_Subtype (Formal_Type)
7964 and then (Ada_Version < Ada_2012
7965 or else No (Underlying_Type (Formal_Type))
7967 (Is_Limited_Type (Formal_Type)
7970 (Underlying_Type (Formal_Type)))))
7972 Set_Extra_Constrained
7973 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7977 -- Create extra formal for supporting accessibility checking. This
7978 -- is done for both anonymous access formals and formals of named
7979 -- access types that are marked as controlling formals. The latter
7980 -- case can occur when Expand_Dispatching_Call creates a subprogram
7981 -- type and substitutes the types of access-to-class-wide actuals
7982 -- for the anonymous access-to-specific-type of controlling formals.
7983 -- Base_Type is applied because in cases where there is a null
7984 -- exclusion the formal may have an access subtype.
7986 -- This is suppressed if we specifically suppress accessibility
7987 -- checks at the package level for either the subprogram, or the
7988 -- package in which it resides. However, we do not suppress it
7989 -- simply if the scope has accessibility checks suppressed, since
7990 -- this could cause trouble when clients are compiled with a
7991 -- different suppression setting. The explicit checks at the
7992 -- package level are safe from this point of view.
7994 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7995 or else (Is_Controlling_Formal (Formal)
7996 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7998 (Explicit_Suppress (E, Accessibility_Check)
8000 Explicit_Suppress (Scope (E), Accessibility_Check))
8003 or else Present (Extra_Accessibility (P_Formal)))
8005 Set_Extra_Accessibility
8006 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8009 -- This label is required when skipping extra formal generation for
8010 -- Unchecked_Union parameters.
8012 <<Skip_Extra_Formal_Generation>>
8014 if Present (P_Formal) then
8015 Next_Formal (P_Formal);
8018 Next_Formal (Formal);
8021 <<Test_For_Func_Result_Extras>>
8023 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8024 -- function call is ... determined by the point of call ...".
8026 if Needs_Result_Accessibility_Level (E) then
8027 Set_Extra_Accessibility_Of_Result
8028 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8031 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8032 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8034 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8036 Result_Subt : constant Entity_Id := Etype (E);
8037 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8038 Formal_Typ : Entity_Id;
8040 Discard : Entity_Id;
8041 pragma Warnings (Off, Discard);
8044 -- In the case of functions with unconstrained result subtypes,
8045 -- add a 4-state formal indicating whether the return object is
8046 -- allocated by the caller (1), or should be allocated by the
8047 -- callee on the secondary stack (2), in the global heap (3), or
8048 -- in a user-defined storage pool (4). For the moment we just use
8049 -- Natural for the type of this formal. Note that this formal
8050 -- isn't usually needed in the case where the result subtype is
8051 -- constrained, but it is needed when the function has a tagged
8052 -- result, because generally such functions can be called in a
8053 -- dispatching context and such calls must be handled like calls
8054 -- to a class-wide function.
8056 if Needs_BIP_Alloc_Form (E) then
8059 (E, Standard_Natural,
8060 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8062 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8063 -- use a user-defined pool. This formal is not added on
8064 -- .NET/JVM/ZFP as those targets do not support pools.
8066 if VM_Target = No_VM
8067 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8071 (E, RTE (RE_Root_Storage_Pool_Ptr),
8072 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8076 -- In the case of functions whose result type needs finalization,
8077 -- add an extra formal which represents the finalization master.
8079 if Needs_BIP_Finalization_Master (E) then
8082 (E, RTE (RE_Finalization_Master_Ptr),
8083 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8086 -- When the result type contains tasks, add two extra formals: the
8087 -- master of the tasks to be created, and the caller's activation
8090 if Has_Task (Full_Subt) then
8093 (E, RTE (RE_Master_Id),
8094 E, BIP_Formal_Suffix (BIP_Task_Master));
8097 (E, RTE (RE_Activation_Chain_Access),
8098 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8101 -- All build-in-place functions get an extra formal that will be
8102 -- passed the address of the return object within the caller.
8105 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8107 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8108 Set_Etype (Formal_Typ, Formal_Typ);
8109 Set_Depends_On_Private
8110 (Formal_Typ, Has_Private_Component (Formal_Typ));
8111 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8112 Set_Is_Access_Constant (Formal_Typ, False);
8114 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8115 -- the designated type comes from the limited view (for back-end
8118 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
8120 Layout_Type (Formal_Typ);
8124 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8127 end Create_Extra_Formals;
8129 -----------------------------
8130 -- Enter_Overloaded_Entity --
8131 -----------------------------
8133 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8134 E : Entity_Id := Current_Entity_In_Scope (S);
8135 C_E : Entity_Id := Current_Entity (S);
8139 Set_Has_Homonym (E);
8140 Set_Has_Homonym (S);
8143 Set_Is_Immediately_Visible (S);
8144 Set_Scope (S, Current_Scope);
8146 -- Chain new entity if front of homonym in current scope, so that
8147 -- homonyms are contiguous.
8149 if Present (E) and then E /= C_E then
8150 while Homonym (C_E) /= E loop
8151 C_E := Homonym (C_E);
8154 Set_Homonym (C_E, S);
8158 Set_Current_Entity (S);
8163 if Is_Inherited_Operation (S) then
8164 Append_Inherited_Subprogram (S);
8166 Append_Entity (S, Current_Scope);
8169 Set_Public_Status (S);
8171 if Debug_Flag_E then
8172 Write_Str ("New overloaded entity chain: ");
8173 Write_Name (Chars (S));
8176 while Present (E) loop
8177 Write_Str (" "); Write_Int (Int (E));
8184 -- Generate warning for hiding
8187 and then Comes_From_Source (S)
8188 and then In_Extended_Main_Source_Unit (S)
8195 -- Warn unless genuine overloading. Do not emit warning on
8196 -- hiding predefined operators in Standard (these are either an
8197 -- (artifact of our implicit declarations, or simple noise) but
8198 -- keep warning on a operator defined on a local subtype, because
8199 -- of the real danger that different operators may be applied in
8200 -- various parts of the program.
8202 -- Note that if E and S have the same scope, there is never any
8203 -- hiding. Either the two conflict, and the program is illegal,
8204 -- or S is overriding an implicit inherited subprogram.
8206 if Scope (E) /= Scope (S)
8207 and then (not Is_Overloadable (E)
8208 or else Subtype_Conformant (E, S))
8209 and then (Is_Immediately_Visible (E)
8211 Is_Potentially_Use_Visible (S))
8213 if Scope (E) /= Standard_Standard then
8214 Error_Msg_Sloc := Sloc (E);
8215 Error_Msg_N ("declaration of & hides one#?h?", S);
8217 elsif Nkind (S) = N_Defining_Operator_Symbol
8219 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8222 ("declaration of & hides predefined operator?h?", S);
8227 end Enter_Overloaded_Entity;
8229 -----------------------------
8230 -- Check_Untagged_Equality --
8231 -----------------------------
8233 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8234 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8235 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8239 if Nkind (Decl) = N_Subprogram_Declaration
8240 and then Is_Record_Type (Typ)
8241 and then not Is_Tagged_Type (Typ)
8243 -- If the type is not declared in a package, or if we are in the
8244 -- body of the package or in some other scope, the new operation is
8245 -- not primitive, and therefore legal, though suspicious. If the
8246 -- type is a generic actual (sub)type, the operation is not primitive
8247 -- either because the base type is declared elsewhere.
8249 if Is_Frozen (Typ) then
8250 if Ekind (Scope (Typ)) /= E_Package
8251 or else Scope (Typ) /= Current_Scope
8255 elsif Is_Generic_Actual_Type (Typ) then
8258 elsif In_Package_Body (Scope (Typ)) then
8260 ("equality operator must be declared "
8261 & "before type& is frozen", Eq_Op, Typ);
8263 ("\move declaration to package spec", Eq_Op);
8267 ("equality operator must be declared "
8268 & "before type& is frozen", Eq_Op, Typ);
8270 Obj_Decl := Next (Parent (Typ));
8271 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8272 if Nkind (Obj_Decl) = N_Object_Declaration
8273 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8276 ("type& is frozen by declaration??", Obj_Decl, Typ);
8278 ("\an equality operator cannot be declared after this "
8279 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8287 elsif not In_Same_List (Parent (Typ), Decl)
8288 and then not Is_Limited_Type (Typ)
8291 -- This makes it illegal to have a primitive equality declared in
8292 -- the private part if the type is visible.
8294 Error_Msg_N ("equality operator appears too late", Eq_Op);
8297 end Check_Untagged_Equality;
8299 -----------------------------
8300 -- Find_Corresponding_Spec --
8301 -----------------------------
8303 function Find_Corresponding_Spec
8305 Post_Error : Boolean := True) return Entity_Id
8307 Spec : constant Node_Id := Specification (N);
8308 Designator : constant Entity_Id := Defining_Entity (Spec);
8312 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8313 -- Even if fully conformant, a body may depend on a generic actual when
8314 -- the spec does not, or vice versa, in which case they were distinct
8315 -- entities in the generic.
8317 -------------------------------
8318 -- Different_Generic_Profile --
8319 -------------------------------
8321 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8324 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8325 -- Check that the types of corresponding formals have the same
8326 -- generic actual if any. We have to account for subtypes of a
8327 -- generic formal, declared between a spec and a body, which may
8328 -- appear distinct in an instance but matched in the generic.
8330 -------------------------
8331 -- Same_Generic_Actual --
8332 -------------------------
8334 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8336 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8338 (Present (Parent (T1))
8339 and then Comes_From_Source (Parent (T1))
8340 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8341 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8342 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8343 end Same_Generic_Actual;
8345 -- Start of processing for Different_Generic_Profile
8348 if not In_Instance then
8351 elsif Ekind (E) = E_Function
8352 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8357 F1 := First_Formal (Designator);
8358 F2 := First_Formal (E);
8359 while Present (F1) loop
8360 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8369 end Different_Generic_Profile;
8371 -- Start of processing for Find_Corresponding_Spec
8374 E := Current_Entity (Designator);
8375 while Present (E) loop
8377 -- We are looking for a matching spec. It must have the same scope,
8378 -- and the same name, and either be type conformant, or be the case
8379 -- of a library procedure spec and its body (which belong to one
8380 -- another regardless of whether they are type conformant or not).
8382 if Scope (E) = Current_Scope then
8383 if Current_Scope = Standard_Standard
8384 or else (Ekind (E) = Ekind (Designator)
8385 and then Type_Conformant (E, Designator))
8387 -- Within an instantiation, we know that spec and body are
8388 -- subtype conformant, because they were subtype conformant in
8389 -- the generic. We choose the subtype-conformant entity here as
8390 -- well, to resolve spurious ambiguities in the instance that
8391 -- were not present in the generic (i.e. when two different
8392 -- types are given the same actual). If we are looking for a
8393 -- spec to match a body, full conformance is expected.
8396 Set_Convention (Designator, Convention (E));
8398 -- Skip past subprogram bodies and subprogram renamings that
8399 -- may appear to have a matching spec, but that aren't fully
8400 -- conformant with it. That can occur in cases where an
8401 -- actual type causes unrelated homographs in the instance.
8403 if Nkind_In (N, N_Subprogram_Body,
8404 N_Subprogram_Renaming_Declaration)
8405 and then Present (Homonym (E))
8406 and then not Fully_Conformant (Designator, E)
8410 elsif not Subtype_Conformant (Designator, E) then
8413 elsif Different_Generic_Profile (E) then
8418 -- Ada 2012 (AI05-0165): For internally generated bodies of
8419 -- null procedures locate the internally generated spec. We
8420 -- enforce mode conformance since a tagged type may inherit
8421 -- from interfaces several null primitives which differ only
8422 -- in the mode of the formals.
8424 if not (Comes_From_Source (E))
8425 and then Is_Null_Procedure (E)
8426 and then not Mode_Conformant (Designator, E)
8430 -- For null procedures coming from source that are completions,
8431 -- analysis of the generated body will establish the link.
8433 elsif Comes_From_Source (E)
8434 and then Nkind (Spec) = N_Procedure_Specification
8435 and then Null_Present (Spec)
8439 elsif not Has_Completion (E) then
8440 if Nkind (N) /= N_Subprogram_Body_Stub then
8441 Set_Corresponding_Spec (N, E);
8444 Set_Has_Completion (E);
8447 elsif Nkind (Parent (N)) = N_Subunit then
8449 -- If this is the proper body of a subunit, the completion
8450 -- flag is set when analyzing the stub.
8454 -- If E is an internal function with a controlling result that
8455 -- was created for an operation inherited by a null extension,
8456 -- it may be overridden by a body without a previous spec (one
8457 -- more reason why these should be shunned). In that case we
8458 -- remove the generated body if present, because the current
8459 -- one is the explicit overriding.
8461 elsif Ekind (E) = E_Function
8462 and then Ada_Version >= Ada_2005
8463 and then not Comes_From_Source (E)
8464 and then Has_Controlling_Result (E)
8465 and then Is_Null_Extension (Etype (E))
8466 and then Comes_From_Source (Spec)
8468 Set_Has_Completion (E, False);
8471 and then Nkind (Parent (E)) = N_Function_Specification
8474 (Unit_Declaration_Node
8475 (Corresponding_Body (Unit_Declaration_Node (E))));
8479 -- If expansion is disabled, or if the wrapper function has
8480 -- not been generated yet, this a late body overriding an
8481 -- inherited operation, or it is an overriding by some other
8482 -- declaration before the controlling result is frozen. In
8483 -- either case this is a declaration of a new entity.
8489 -- If the body already exists, then this is an error unless
8490 -- the previous declaration is the implicit declaration of a
8491 -- derived subprogram. It is also legal for an instance to
8492 -- contain type conformant overloadable declarations (but the
8493 -- generic declaration may not), per 8.3(26/2).
8495 elsif No (Alias (E))
8496 and then not Is_Intrinsic_Subprogram (E)
8497 and then not In_Instance
8500 Error_Msg_Sloc := Sloc (E);
8502 if Is_Imported (E) then
8504 ("body not allowed for imported subprogram & declared#",
8507 Error_Msg_NE ("duplicate body for & declared#", N, E);
8511 -- Child units cannot be overloaded, so a conformance mismatch
8512 -- between body and a previous spec is an error.
8514 elsif Is_Child_Unit (E)
8516 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8518 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8523 ("body of child unit does not match previous declaration", N);
8531 -- On exit, we know that no previous declaration of subprogram exists
8534 end Find_Corresponding_Spec;
8536 ----------------------
8537 -- Fully_Conformant --
8538 ----------------------
8540 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8543 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8545 end Fully_Conformant;
8547 ----------------------------------
8548 -- Fully_Conformant_Expressions --
8549 ----------------------------------
8551 function Fully_Conformant_Expressions
8552 (Given_E1 : Node_Id;
8553 Given_E2 : Node_Id) return Boolean
8555 E1 : constant Node_Id := Original_Node (Given_E1);
8556 E2 : constant Node_Id := Original_Node (Given_E2);
8557 -- We always test conformance on original nodes, since it is possible
8558 -- for analysis and/or expansion to make things look as though they
8559 -- conform when they do not, e.g. by converting 1+2 into 3.
8561 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8562 renames Fully_Conformant_Expressions;
8564 function FCL (L1, L2 : List_Id) return Boolean;
8565 -- Compare elements of two lists for conformance. Elements have to be
8566 -- conformant, and actuals inserted as default parameters do not match
8567 -- explicit actuals with the same value.
8569 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8570 -- Compare an operator node with a function call
8576 function FCL (L1, L2 : List_Id) return Boolean is
8580 if L1 = No_List then
8586 if L2 = No_List then
8592 -- Compare two lists, skipping rewrite insertions (we want to compare
8593 -- the original trees, not the expanded versions!)
8596 if Is_Rewrite_Insertion (N1) then
8598 elsif Is_Rewrite_Insertion (N2) then
8604 elsif not FCE (N1, N2) then
8617 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8618 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8623 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8628 Act := First (Actuals);
8630 if Nkind (Op_Node) in N_Binary_Op then
8631 if not FCE (Left_Opnd (Op_Node), Act) then
8638 return Present (Act)
8639 and then FCE (Right_Opnd (Op_Node), Act)
8640 and then No (Next (Act));
8644 -- Start of processing for Fully_Conformant_Expressions
8647 -- Non-conformant if paren count does not match. Note: if some idiot
8648 -- complains that we don't do this right for more than 3 levels of
8649 -- parentheses, they will be treated with the respect they deserve!
8651 if Paren_Count (E1) /= Paren_Count (E2) then
8654 -- If same entities are referenced, then they are conformant even if
8655 -- they have different forms (RM 8.3.1(19-20)).
8657 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8658 if Present (Entity (E1)) then
8659 return Entity (E1) = Entity (E2)
8660 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8661 and then Ekind (Entity (E1)) = E_Discriminant
8662 and then Ekind (Entity (E2)) = E_In_Parameter);
8664 elsif Nkind (E1) = N_Expanded_Name
8665 and then Nkind (E2) = N_Expanded_Name
8666 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8667 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8669 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8672 -- Identifiers in component associations don't always have
8673 -- entities, but their names must conform.
8675 return Nkind (E1) = N_Identifier
8676 and then Nkind (E2) = N_Identifier
8677 and then Chars (E1) = Chars (E2);
8680 elsif Nkind (E1) = N_Character_Literal
8681 and then Nkind (E2) = N_Expanded_Name
8683 return Nkind (Selector_Name (E2)) = N_Character_Literal
8684 and then Chars (E1) = Chars (Selector_Name (E2));
8686 elsif Nkind (E2) = N_Character_Literal
8687 and then Nkind (E1) = N_Expanded_Name
8689 return Nkind (Selector_Name (E1)) = N_Character_Literal
8690 and then Chars (E2) = Chars (Selector_Name (E1));
8692 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8693 return FCO (E1, E2);
8695 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8696 return FCO (E2, E1);
8698 -- Otherwise we must have the same syntactic entity
8700 elsif Nkind (E1) /= Nkind (E2) then
8703 -- At this point, we specialize by node type
8710 FCL (Expressions (E1), Expressions (E2))
8712 FCL (Component_Associations (E1),
8713 Component_Associations (E2));
8716 if Nkind (Expression (E1)) = N_Qualified_Expression
8718 Nkind (Expression (E2)) = N_Qualified_Expression
8720 return FCE (Expression (E1), Expression (E2));
8722 -- Check that the subtype marks and any constraints
8727 Indic1 : constant Node_Id := Expression (E1);
8728 Indic2 : constant Node_Id := Expression (E2);
8733 if Nkind (Indic1) /= N_Subtype_Indication then
8735 Nkind (Indic2) /= N_Subtype_Indication
8736 and then Entity (Indic1) = Entity (Indic2);
8738 elsif Nkind (Indic2) /= N_Subtype_Indication then
8740 Nkind (Indic1) /= N_Subtype_Indication
8741 and then Entity (Indic1) = Entity (Indic2);
8744 if Entity (Subtype_Mark (Indic1)) /=
8745 Entity (Subtype_Mark (Indic2))
8750 Elt1 := First (Constraints (Constraint (Indic1)));
8751 Elt2 := First (Constraints (Constraint (Indic2)));
8752 while Present (Elt1) and then Present (Elt2) loop
8753 if not FCE (Elt1, Elt2) then
8766 when N_Attribute_Reference =>
8768 Attribute_Name (E1) = Attribute_Name (E2)
8769 and then FCL (Expressions (E1), Expressions (E2));
8773 Entity (E1) = Entity (E2)
8774 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8775 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8777 when N_Short_Circuit | N_Membership_Test =>
8779 FCE (Left_Opnd (E1), Left_Opnd (E2))
8781 FCE (Right_Opnd (E1), Right_Opnd (E2));
8783 when N_Case_Expression =>
8789 if not FCE (Expression (E1), Expression (E2)) then
8793 Alt1 := First (Alternatives (E1));
8794 Alt2 := First (Alternatives (E2));
8796 if Present (Alt1) /= Present (Alt2) then
8798 elsif No (Alt1) then
8802 if not FCE (Expression (Alt1), Expression (Alt2))
8803 or else not FCL (Discrete_Choices (Alt1),
8804 Discrete_Choices (Alt2))
8815 when N_Character_Literal =>
8817 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8819 when N_Component_Association =>
8821 FCL (Choices (E1), Choices (E2))
8823 FCE (Expression (E1), Expression (E2));
8825 when N_Explicit_Dereference =>
8827 FCE (Prefix (E1), Prefix (E2));
8829 when N_Extension_Aggregate =>
8831 FCL (Expressions (E1), Expressions (E2))
8832 and then Null_Record_Present (E1) =
8833 Null_Record_Present (E2)
8834 and then FCL (Component_Associations (E1),
8835 Component_Associations (E2));
8837 when N_Function_Call =>
8839 FCE (Name (E1), Name (E2))
8841 FCL (Parameter_Associations (E1),
8842 Parameter_Associations (E2));
8844 when N_If_Expression =>
8846 FCL (Expressions (E1), Expressions (E2));
8848 when N_Indexed_Component =>
8850 FCE (Prefix (E1), Prefix (E2))
8852 FCL (Expressions (E1), Expressions (E2));
8854 when N_Integer_Literal =>
8855 return (Intval (E1) = Intval (E2));
8860 when N_Operator_Symbol =>
8862 Chars (E1) = Chars (E2);
8864 when N_Others_Choice =>
8867 when N_Parameter_Association =>
8869 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8870 and then FCE (Explicit_Actual_Parameter (E1),
8871 Explicit_Actual_Parameter (E2));
8873 when N_Qualified_Expression =>
8875 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8877 FCE (Expression (E1), Expression (E2));
8879 when N_Quantified_Expression =>
8880 if not FCE (Condition (E1), Condition (E2)) then
8884 if Present (Loop_Parameter_Specification (E1))
8885 and then Present (Loop_Parameter_Specification (E2))
8888 L1 : constant Node_Id :=
8889 Loop_Parameter_Specification (E1);
8890 L2 : constant Node_Id :=
8891 Loop_Parameter_Specification (E2);
8895 Reverse_Present (L1) = Reverse_Present (L2)
8897 FCE (Defining_Identifier (L1),
8898 Defining_Identifier (L2))
8900 FCE (Discrete_Subtype_Definition (L1),
8901 Discrete_Subtype_Definition (L2));
8904 elsif Present (Iterator_Specification (E1))
8905 and then Present (Iterator_Specification (E2))
8908 I1 : constant Node_Id := Iterator_Specification (E1);
8909 I2 : constant Node_Id := Iterator_Specification (E2);
8913 FCE (Defining_Identifier (I1),
8914 Defining_Identifier (I2))
8916 Of_Present (I1) = Of_Present (I2)
8918 Reverse_Present (I1) = Reverse_Present (I2)
8919 and then FCE (Name (I1), Name (I2))
8920 and then FCE (Subtype_Indication (I1),
8921 Subtype_Indication (I2));
8924 -- The quantified expressions used different specifications to
8925 -- walk their respective ranges.
8933 FCE (Low_Bound (E1), Low_Bound (E2))
8935 FCE (High_Bound (E1), High_Bound (E2));
8937 when N_Real_Literal =>
8938 return (Realval (E1) = Realval (E2));
8940 when N_Selected_Component =>
8942 FCE (Prefix (E1), Prefix (E2))
8944 FCE (Selector_Name (E1), Selector_Name (E2));
8948 FCE (Prefix (E1), Prefix (E2))
8950 FCE (Discrete_Range (E1), Discrete_Range (E2));
8952 when N_String_Literal =>
8954 S1 : constant String_Id := Strval (E1);
8955 S2 : constant String_Id := Strval (E2);
8956 L1 : constant Nat := String_Length (S1);
8957 L2 : constant Nat := String_Length (S2);
8964 for J in 1 .. L1 loop
8965 if Get_String_Char (S1, J) /=
8966 Get_String_Char (S2, J)
8976 when N_Type_Conversion =>
8978 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8980 FCE (Expression (E1), Expression (E2));
8984 Entity (E1) = Entity (E2)
8986 FCE (Right_Opnd (E1), Right_Opnd (E2));
8988 when N_Unchecked_Type_Conversion =>
8990 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8992 FCE (Expression (E1), Expression (E2));
8994 -- All other node types cannot appear in this context. Strictly
8995 -- we should raise a fatal internal error. Instead we just ignore
8996 -- the nodes. This means that if anyone makes a mistake in the
8997 -- expander and mucks an expression tree irretrievably, the result
8998 -- will be a failure to detect a (probably very obscure) case
8999 -- of non-conformance, which is better than bombing on some
9000 -- case where two expressions do in fact conform.
9007 end Fully_Conformant_Expressions;
9009 ----------------------------------------
9010 -- Fully_Conformant_Discrete_Subtypes --
9011 ----------------------------------------
9013 function Fully_Conformant_Discrete_Subtypes
9014 (Given_S1 : Node_Id;
9015 Given_S2 : Node_Id) return Boolean
9017 S1 : constant Node_Id := Original_Node (Given_S1);
9018 S2 : constant Node_Id := Original_Node (Given_S2);
9020 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9021 -- Special-case for a bound given by a discriminant, which in the body
9022 -- is replaced with the discriminal of the enclosing type.
9024 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9025 -- Check both bounds
9027 -----------------------
9028 -- Conforming_Bounds --
9029 -----------------------
9031 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9033 if Is_Entity_Name (B1)
9034 and then Is_Entity_Name (B2)
9035 and then Ekind (Entity (B1)) = E_Discriminant
9037 return Chars (B1) = Chars (B2);
9040 return Fully_Conformant_Expressions (B1, B2);
9042 end Conforming_Bounds;
9044 -----------------------
9045 -- Conforming_Ranges --
9046 -----------------------
9048 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9051 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9053 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9054 end Conforming_Ranges;
9056 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9059 if Nkind (S1) /= Nkind (S2) then
9062 elsif Is_Entity_Name (S1) then
9063 return Entity (S1) = Entity (S2);
9065 elsif Nkind (S1) = N_Range then
9066 return Conforming_Ranges (S1, S2);
9068 elsif Nkind (S1) = N_Subtype_Indication then
9070 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9073 (Range_Expression (Constraint (S1)),
9074 Range_Expression (Constraint (S2)));
9078 end Fully_Conformant_Discrete_Subtypes;
9080 --------------------
9081 -- Install_Entity --
9082 --------------------
9084 procedure Install_Entity (E : Entity_Id) is
9085 Prev : constant Entity_Id := Current_Entity (E);
9087 Set_Is_Immediately_Visible (E);
9088 Set_Current_Entity (E);
9089 Set_Homonym (E, Prev);
9092 ---------------------
9093 -- Install_Formals --
9094 ---------------------
9096 procedure Install_Formals (Id : Entity_Id) is
9099 F := First_Formal (Id);
9100 while Present (F) loop
9104 end Install_Formals;
9106 -----------------------------
9107 -- Is_Interface_Conformant --
9108 -----------------------------
9110 function Is_Interface_Conformant
9111 (Tagged_Type : Entity_Id;
9112 Iface_Prim : Entity_Id;
9113 Prim : Entity_Id) return Boolean
9115 -- The operation may in fact be an inherited (implicit) operation
9116 -- rather than the original interface primitive, so retrieve the
9117 -- ultimate ancestor.
9119 Iface : constant Entity_Id :=
9120 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9121 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9123 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9124 -- Return the controlling formal of Prim
9126 ------------------------
9127 -- Controlling_Formal --
9128 ------------------------
9130 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9134 E := First_Entity (Prim);
9135 while Present (E) loop
9136 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9144 end Controlling_Formal;
9148 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9149 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9151 -- Start of processing for Is_Interface_Conformant
9154 pragma Assert (Is_Subprogram (Iface_Prim)
9155 and then Is_Subprogram (Prim)
9156 and then Is_Dispatching_Operation (Iface_Prim)
9157 and then Is_Dispatching_Operation (Prim));
9159 pragma Assert (Is_Interface (Iface)
9160 or else (Present (Alias (Iface_Prim))
9163 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9165 if Prim = Iface_Prim
9166 or else not Is_Subprogram (Prim)
9167 or else Ekind (Prim) /= Ekind (Iface_Prim)
9168 or else not Is_Dispatching_Operation (Prim)
9169 or else Scope (Prim) /= Scope (Tagged_Type)
9171 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9172 or else not Primitive_Names_Match (Iface_Prim, Prim)
9176 -- The mode of the controlling formals must match
9178 elsif Present (Iface_Ctrl_F)
9179 and then Present (Prim_Ctrl_F)
9180 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9184 -- Case of a procedure, or a function whose result type matches the
9185 -- result type of the interface primitive, or a function that has no
9186 -- controlling result (I or access I).
9188 elsif Ekind (Iface_Prim) = E_Procedure
9189 or else Etype (Prim) = Etype (Iface_Prim)
9190 or else not Has_Controlling_Result (Prim)
9192 return Type_Conformant
9193 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9195 -- Case of a function returning an interface, or an access to one. Check
9196 -- that the return types correspond.
9198 elsif Implements_Interface (Typ, Iface) then
9199 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9201 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9206 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9207 Skip_Controlling_Formals => True);
9213 end Is_Interface_Conformant;
9215 ---------------------------------
9216 -- Is_Non_Overriding_Operation --
9217 ---------------------------------
9219 function Is_Non_Overriding_Operation
9220 (Prev_E : Entity_Id;
9221 New_E : Entity_Id) return Boolean
9225 G_Typ : Entity_Id := Empty;
9227 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9228 -- If F_Type is a derived type associated with a generic actual subtype,
9229 -- then return its Generic_Parent_Type attribute, else return Empty.
9231 function Types_Correspond
9232 (P_Type : Entity_Id;
9233 N_Type : Entity_Id) return Boolean;
9234 -- Returns true if and only if the types (or designated types in the
9235 -- case of anonymous access types) are the same or N_Type is derived
9236 -- directly or indirectly from P_Type.
9238 -----------------------------
9239 -- Get_Generic_Parent_Type --
9240 -----------------------------
9242 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9248 if Is_Derived_Type (F_Typ)
9249 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9251 -- The tree must be traversed to determine the parent subtype in
9252 -- the generic unit, which unfortunately isn't always available
9253 -- via semantic attributes. ??? (Note: The use of Original_Node
9254 -- is needed for cases where a full derived type has been
9257 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9258 if Nkind (Defn) = N_Derived_Type_Definition then
9259 Indic := Subtype_Indication (Defn);
9261 if Nkind (Indic) = N_Subtype_Indication then
9262 G_Typ := Entity (Subtype_Mark (Indic));
9264 G_Typ := Entity (Indic);
9267 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9268 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9270 return Generic_Parent_Type (Parent (G_Typ));
9276 end Get_Generic_Parent_Type;
9278 ----------------------
9279 -- Types_Correspond --
9280 ----------------------
9282 function Types_Correspond
9283 (P_Type : Entity_Id;
9284 N_Type : Entity_Id) return Boolean
9286 Prev_Type : Entity_Id := Base_Type (P_Type);
9287 New_Type : Entity_Id := Base_Type (N_Type);
9290 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9291 Prev_Type := Designated_Type (Prev_Type);
9294 if Ekind (New_Type) = E_Anonymous_Access_Type then
9295 New_Type := Designated_Type (New_Type);
9298 if Prev_Type = New_Type then
9301 elsif not Is_Class_Wide_Type (New_Type) then
9302 while Etype (New_Type) /= New_Type loop
9303 New_Type := Etype (New_Type);
9304 if New_Type = Prev_Type then
9310 end Types_Correspond;
9312 -- Start of processing for Is_Non_Overriding_Operation
9315 -- In the case where both operations are implicit derived subprograms
9316 -- then neither overrides the other. This can only occur in certain
9317 -- obscure cases (e.g., derivation from homographs created in a generic
9320 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9323 elsif Ekind (Current_Scope) = E_Package
9324 and then Is_Generic_Instance (Current_Scope)
9325 and then In_Private_Part (Current_Scope)
9326 and then Comes_From_Source (New_E)
9328 -- We examine the formals and result type of the inherited operation,
9329 -- to determine whether their type is derived from (the instance of)
9330 -- a generic type. The first such formal or result type is the one
9333 Formal := First_Formal (Prev_E);
9334 while Present (Formal) loop
9335 F_Typ := Base_Type (Etype (Formal));
9337 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9338 F_Typ := Designated_Type (F_Typ);
9341 G_Typ := Get_Generic_Parent_Type (F_Typ);
9342 exit when Present (G_Typ);
9344 Next_Formal (Formal);
9347 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9348 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9355 -- If the generic type is a private type, then the original operation
9356 -- was not overriding in the generic, because there was no primitive
9357 -- operation to override.
9359 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9360 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9361 N_Formal_Private_Type_Definition
9365 -- The generic parent type is the ancestor of a formal derived
9366 -- type declaration. We need to check whether it has a primitive
9367 -- operation that should be overridden by New_E in the generic.
9371 P_Formal : Entity_Id;
9372 N_Formal : Entity_Id;
9376 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9379 while Present (Prim_Elt) loop
9380 P_Prim := Node (Prim_Elt);
9382 if Chars (P_Prim) = Chars (New_E)
9383 and then Ekind (P_Prim) = Ekind (New_E)
9385 P_Formal := First_Formal (P_Prim);
9386 N_Formal := First_Formal (New_E);
9387 while Present (P_Formal) and then Present (N_Formal) loop
9388 P_Typ := Etype (P_Formal);
9389 N_Typ := Etype (N_Formal);
9391 if not Types_Correspond (P_Typ, N_Typ) then
9395 Next_Entity (P_Formal);
9396 Next_Entity (N_Formal);
9399 -- Found a matching primitive operation belonging to the
9400 -- formal ancestor type, so the new subprogram is
9404 and then No (N_Formal)
9405 and then (Ekind (New_E) /= E_Function
9408 (Etype (P_Prim), Etype (New_E)))
9414 Next_Elmt (Prim_Elt);
9417 -- If no match found, then the new subprogram does not override
9418 -- in the generic (nor in the instance).
9420 -- If the type in question is not abstract, and the subprogram
9421 -- is, this will be an error if the new operation is in the
9422 -- private part of the instance. Emit a warning now, which will
9423 -- make the subsequent error message easier to understand.
9425 if not Is_Abstract_Type (F_Typ)
9426 and then Is_Abstract_Subprogram (Prev_E)
9427 and then In_Private_Part (Current_Scope)
9429 Error_Msg_Node_2 := F_Typ;
9431 ("private operation& in generic unit does not override " &
9432 "any primitive operation of& (RM 12.3 (18))??",
9442 end Is_Non_Overriding_Operation;
9444 -------------------------------------
9445 -- List_Inherited_Pre_Post_Aspects --
9446 -------------------------------------
9448 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9450 if Opt.List_Inherited_Aspects
9451 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9454 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9458 for J in Inherited'Range loop
9459 P := Pre_Post_Conditions (Contract (Inherited (J)));
9460 while Present (P) loop
9461 Error_Msg_Sloc := Sloc (P);
9463 if Class_Present (P) and then not Split_PPC (P) then
9464 if Pragma_Name (P) = Name_Precondition then
9466 ("info: & inherits `Pre''Class` aspect from #?L?",
9470 ("info: & inherits `Post''Class` aspect from #?L?",
9475 P := Next_Pragma (P);
9480 end List_Inherited_Pre_Post_Aspects;
9482 ------------------------------
9483 -- Make_Inequality_Operator --
9484 ------------------------------
9486 -- S is the defining identifier of an equality operator. We build a
9487 -- subprogram declaration with the right signature. This operation is
9488 -- intrinsic, because it is always expanded as the negation of the
9489 -- call to the equality function.
9491 procedure Make_Inequality_Operator (S : Entity_Id) is
9492 Loc : constant Source_Ptr := Sloc (S);
9495 Op_Name : Entity_Id;
9497 FF : constant Entity_Id := First_Formal (S);
9498 NF : constant Entity_Id := Next_Formal (FF);
9501 -- Check that equality was properly defined, ignore call if not
9508 A : constant Entity_Id :=
9509 Make_Defining_Identifier (Sloc (FF),
9510 Chars => Chars (FF));
9512 B : constant Entity_Id :=
9513 Make_Defining_Identifier (Sloc (NF),
9514 Chars => Chars (NF));
9517 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9519 Formals := New_List (
9520 Make_Parameter_Specification (Loc,
9521 Defining_Identifier => A,
9523 New_Reference_To (Etype (First_Formal (S)),
9524 Sloc (Etype (First_Formal (S))))),
9526 Make_Parameter_Specification (Loc,
9527 Defining_Identifier => B,
9529 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9530 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9533 Make_Subprogram_Declaration (Loc,
9535 Make_Function_Specification (Loc,
9536 Defining_Unit_Name => Op_Name,
9537 Parameter_Specifications => Formals,
9538 Result_Definition =>
9539 New_Reference_To (Standard_Boolean, Loc)));
9541 -- Insert inequality right after equality if it is explicit or after
9542 -- the derived type when implicit. These entities are created only
9543 -- for visibility purposes, and eventually replaced in the course
9544 -- of expansion, so they do not need to be attached to the tree and
9545 -- seen by the back-end. Keeping them internal also avoids spurious
9546 -- freezing problems. The declaration is inserted in the tree for
9547 -- analysis, and removed afterwards. If the equality operator comes
9548 -- from an explicit declaration, attach the inequality immediately
9549 -- after. Else the equality is inherited from a derived type
9550 -- declaration, so insert inequality after that declaration.
9552 if No (Alias (S)) then
9553 Insert_After (Unit_Declaration_Node (S), Decl);
9554 elsif Is_List_Member (Parent (S)) then
9555 Insert_After (Parent (S), Decl);
9557 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9560 Mark_Rewrite_Insertion (Decl);
9561 Set_Is_Intrinsic_Subprogram (Op_Name);
9564 Set_Has_Completion (Op_Name);
9565 Set_Corresponding_Equality (Op_Name, S);
9566 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9568 end Make_Inequality_Operator;
9570 ----------------------
9571 -- May_Need_Actuals --
9572 ----------------------
9574 procedure May_Need_Actuals (Fun : Entity_Id) is
9579 F := First_Formal (Fun);
9581 while Present (F) loop
9582 if No (Default_Value (F)) then
9590 Set_Needs_No_Actuals (Fun, B);
9591 end May_Need_Actuals;
9593 ---------------------
9594 -- Mode_Conformant --
9595 ---------------------
9597 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9600 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9602 end Mode_Conformant;
9604 ---------------------------
9605 -- New_Overloaded_Entity --
9606 ---------------------------
9608 procedure New_Overloaded_Entity
9610 Derived_Type : Entity_Id := Empty)
9612 Overridden_Subp : Entity_Id := Empty;
9613 -- Set if the current scope has an operation that is type-conformant
9614 -- with S, and becomes hidden by S.
9616 Is_Primitive_Subp : Boolean;
9617 -- Set to True if the new subprogram is primitive
9620 -- Entity that S overrides
9622 Prev_Vis : Entity_Id := Empty;
9623 -- Predecessor of E in Homonym chain
9625 procedure Check_For_Primitive_Subprogram
9626 (Is_Primitive : out Boolean;
9627 Is_Overriding : Boolean := False);
9628 -- If the subprogram being analyzed is a primitive operation of the type
9629 -- of a formal or result, set the Has_Primitive_Operations flag on the
9630 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9631 -- corresponding flag on the entity itself for later use.
9633 procedure Check_Synchronized_Overriding
9634 (Def_Id : Entity_Id;
9635 Overridden_Subp : out Entity_Id);
9636 -- First determine if Def_Id is an entry or a subprogram either defined
9637 -- in the scope of a task or protected type, or is a primitive of such
9638 -- a type. Check whether Def_Id overrides a subprogram of an interface
9639 -- implemented by the synchronized type, return the overridden entity
9642 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9643 -- Check that E is declared in the private part of the current package,
9644 -- or in the package body, where it may hide a previous declaration.
9645 -- We can't use In_Private_Part by itself because this flag is also
9646 -- set when freezing entities, so we must examine the place of the
9647 -- declaration in the tree, and recognize wrapper packages as well.
9649 function Is_Overriding_Alias
9651 New_E : Entity_Id) return Boolean;
9652 -- Check whether new subprogram and old subprogram are both inherited
9653 -- from subprograms that have distinct dispatch table entries. This can
9654 -- occur with derivations from instances with accidental homonyms. The
9655 -- function is conservative given that the converse is only true within
9656 -- instances that contain accidental overloadings.
9658 ------------------------------------
9659 -- Check_For_Primitive_Subprogram --
9660 ------------------------------------
9662 procedure Check_For_Primitive_Subprogram
9663 (Is_Primitive : out Boolean;
9664 Is_Overriding : Boolean := False)
9670 function Visible_Part_Type (T : Entity_Id) return Boolean;
9671 -- Returns true if T is declared in the visible part of the current
9672 -- package scope; otherwise returns false. Assumes that T is declared
9675 procedure Check_Private_Overriding (T : Entity_Id);
9676 -- Checks that if a primitive abstract subprogram of a visible
9677 -- abstract type is declared in a private part, then it must override
9678 -- an abstract subprogram declared in the visible part. Also checks
9679 -- that if a primitive function with a controlling result is declared
9680 -- in a private part, then it must override a function declared in
9681 -- the visible part.
9683 ------------------------------
9684 -- Check_Private_Overriding --
9685 ------------------------------
9687 procedure Check_Private_Overriding (T : Entity_Id) is
9689 if Is_Package_Or_Generic_Package (Current_Scope)
9690 and then In_Private_Part (Current_Scope)
9691 and then Visible_Part_Type (T)
9692 and then not In_Instance
9694 if Is_Abstract_Type (T)
9695 and then Is_Abstract_Subprogram (S)
9696 and then (not Is_Overriding
9697 or else not Is_Abstract_Subprogram (E))
9700 ("abstract subprograms must be visible "
9701 & "(RM 3.9.3(10))!", S);
9703 elsif Ekind (S) = E_Function and then not Is_Overriding then
9704 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9706 ("private function with tagged result must"
9707 & " override visible-part function", S);
9709 ("\move subprogram to the visible part"
9710 & " (RM 3.9.3(10))", S);
9712 -- AI05-0073: extend this test to the case of a function
9713 -- with a controlling access result.
9715 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9716 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9718 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9719 and then Ada_Version >= Ada_2012
9722 ("private function with controlling access result "
9723 & "must override visible-part function", S);
9725 ("\move subprogram to the visible part"
9726 & " (RM 3.9.3(10))", S);
9730 end Check_Private_Overriding;
9732 -----------------------
9733 -- Visible_Part_Type --
9734 -----------------------
9736 function Visible_Part_Type (T : Entity_Id) return Boolean is
9737 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9741 -- If the entity is a private type, then it must be declared in a
9744 if Ekind (T) in Private_Kind then
9748 -- Otherwise, we traverse the visible part looking for its
9749 -- corresponding declaration. We cannot use the declaration
9750 -- node directly because in the private part the entity of a
9751 -- private type is the one in the full view, which does not
9752 -- indicate that it is the completion of something visible.
9754 N := First (Visible_Declarations (Specification (P)));
9755 while Present (N) loop
9756 if Nkind (N) = N_Full_Type_Declaration
9757 and then Present (Defining_Identifier (N))
9758 and then T = Defining_Identifier (N)
9762 elsif Nkind_In (N, N_Private_Type_Declaration,
9763 N_Private_Extension_Declaration)
9764 and then Present (Defining_Identifier (N))
9765 and then T = Full_View (Defining_Identifier (N))
9774 end Visible_Part_Type;
9776 -- Start of processing for Check_For_Primitive_Subprogram
9779 Is_Primitive := False;
9781 if not Comes_From_Source (S) then
9784 -- If subprogram is at library level, it is not primitive operation
9786 elsif Current_Scope = Standard_Standard then
9789 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9790 and then not In_Package_Body (Current_Scope))
9791 or else Is_Overriding
9793 -- For function, check return type
9795 if Ekind (S) = E_Function then
9796 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9797 F_Typ := Designated_Type (Etype (S));
9802 B_Typ := Base_Type (F_Typ);
9804 if Scope (B_Typ) = Current_Scope
9805 and then not Is_Class_Wide_Type (B_Typ)
9806 and then not Is_Generic_Type (B_Typ)
9808 Is_Primitive := True;
9809 Set_Has_Primitive_Operations (B_Typ);
9810 Set_Is_Primitive (S);
9811 Check_Private_Overriding (B_Typ);
9815 -- For all subprograms, check formals
9817 Formal := First_Formal (S);
9818 while Present (Formal) loop
9819 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9820 F_Typ := Designated_Type (Etype (Formal));
9822 F_Typ := Etype (Formal);
9825 B_Typ := Base_Type (F_Typ);
9827 if Ekind (B_Typ) = E_Access_Subtype then
9828 B_Typ := Base_Type (B_Typ);
9831 if Scope (B_Typ) = Current_Scope
9832 and then not Is_Class_Wide_Type (B_Typ)
9833 and then not Is_Generic_Type (B_Typ)
9835 Is_Primitive := True;
9836 Set_Is_Primitive (S);
9837 Set_Has_Primitive_Operations (B_Typ);
9838 Check_Private_Overriding (B_Typ);
9841 Next_Formal (Formal);
9844 -- Special case: An equality function can be redefined for a type
9845 -- occurring in a declarative part, and won't otherwise be treated as
9846 -- a primitive because it doesn't occur in a package spec and doesn't
9847 -- override an inherited subprogram. It's important that we mark it
9848 -- primitive so it can be returned by Collect_Primitive_Operations
9849 -- and be used in composing the equality operation of later types
9850 -- that have a component of the type.
9852 elsif Chars (S) = Name_Op_Eq
9853 and then Etype (S) = Standard_Boolean
9855 B_Typ := Base_Type (Etype (First_Formal (S)));
9857 if Scope (B_Typ) = Current_Scope
9859 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9860 and then not Is_Limited_Type (B_Typ)
9862 Is_Primitive := True;
9863 Set_Is_Primitive (S);
9864 Set_Has_Primitive_Operations (B_Typ);
9865 Check_Private_Overriding (B_Typ);
9868 end Check_For_Primitive_Subprogram;
9870 -----------------------------------
9871 -- Check_Synchronized_Overriding --
9872 -----------------------------------
9874 procedure Check_Synchronized_Overriding
9875 (Def_Id : Entity_Id;
9876 Overridden_Subp : out Entity_Id)
9878 Ifaces_List : Elist_Id;
9882 function Matches_Prefixed_View_Profile
9883 (Prim_Params : List_Id;
9884 Iface_Params : List_Id) return Boolean;
9885 -- Determine whether a subprogram's parameter profile Prim_Params
9886 -- matches that of a potentially overridden interface subprogram
9887 -- Iface_Params. Also determine if the type of first parameter of
9888 -- Iface_Params is an implemented interface.
9890 -----------------------------------
9891 -- Matches_Prefixed_View_Profile --
9892 -----------------------------------
9894 function Matches_Prefixed_View_Profile
9895 (Prim_Params : List_Id;
9896 Iface_Params : List_Id) return Boolean
9898 Iface_Id : Entity_Id;
9899 Iface_Param : Node_Id;
9900 Iface_Typ : Entity_Id;
9901 Prim_Id : Entity_Id;
9902 Prim_Param : Node_Id;
9903 Prim_Typ : Entity_Id;
9905 function Is_Implemented
9906 (Ifaces_List : Elist_Id;
9907 Iface : Entity_Id) return Boolean;
9908 -- Determine if Iface is implemented by the current task or
9911 --------------------
9912 -- Is_Implemented --
9913 --------------------
9915 function Is_Implemented
9916 (Ifaces_List : Elist_Id;
9917 Iface : Entity_Id) return Boolean
9919 Iface_Elmt : Elmt_Id;
9922 Iface_Elmt := First_Elmt (Ifaces_List);
9923 while Present (Iface_Elmt) loop
9924 if Node (Iface_Elmt) = Iface then
9928 Next_Elmt (Iface_Elmt);
9934 -- Start of processing for Matches_Prefixed_View_Profile
9937 Iface_Param := First (Iface_Params);
9938 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9940 if Is_Access_Type (Iface_Typ) then
9941 Iface_Typ := Designated_Type (Iface_Typ);
9944 Prim_Param := First (Prim_Params);
9946 -- The first parameter of the potentially overridden subprogram
9947 -- must be an interface implemented by Prim.
9949 if not Is_Interface (Iface_Typ)
9950 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9955 -- The checks on the object parameters are done, move onto the
9956 -- rest of the parameters.
9958 if not In_Scope then
9959 Prim_Param := Next (Prim_Param);
9962 Iface_Param := Next (Iface_Param);
9963 while Present (Iface_Param) and then Present (Prim_Param) loop
9964 Iface_Id := Defining_Identifier (Iface_Param);
9965 Iface_Typ := Find_Parameter_Type (Iface_Param);
9967 Prim_Id := Defining_Identifier (Prim_Param);
9968 Prim_Typ := Find_Parameter_Type (Prim_Param);
9970 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9971 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9972 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9974 Iface_Typ := Designated_Type (Iface_Typ);
9975 Prim_Typ := Designated_Type (Prim_Typ);
9978 -- Case of multiple interface types inside a parameter profile
9980 -- (Obj_Param : in out Iface; ...; Param : Iface)
9982 -- If the interface type is implemented, then the matching type
9983 -- in the primitive should be the implementing record type.
9985 if Ekind (Iface_Typ) = E_Record_Type
9986 and then Is_Interface (Iface_Typ)
9987 and then Is_Implemented (Ifaces_List, Iface_Typ)
9989 if Prim_Typ /= Typ then
9993 -- The two parameters must be both mode and subtype conformant
9995 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9997 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
10002 Next (Iface_Param);
10006 -- One of the two lists contains more parameters than the other
10008 if Present (Iface_Param) or else Present (Prim_Param) then
10013 end Matches_Prefixed_View_Profile;
10015 -- Start of processing for Check_Synchronized_Overriding
10018 Overridden_Subp := Empty;
10020 -- Def_Id must be an entry or a subprogram. We should skip predefined
10021 -- primitives internally generated by the frontend; however at this
10022 -- stage predefined primitives are still not fully decorated. As a
10023 -- minor optimization we skip here internally generated subprograms.
10025 if (Ekind (Def_Id) /= E_Entry
10026 and then Ekind (Def_Id) /= E_Function
10027 and then Ekind (Def_Id) /= E_Procedure)
10028 or else not Comes_From_Source (Def_Id)
10033 -- Search for the concurrent declaration since it contains the list
10034 -- of all implemented interfaces. In this case, the subprogram is
10035 -- declared within the scope of a protected or a task type.
10037 if Present (Scope (Def_Id))
10038 and then Is_Concurrent_Type (Scope (Def_Id))
10039 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10041 Typ := Scope (Def_Id);
10044 -- The enclosing scope is not a synchronized type and the subprogram
10047 elsif No (First_Formal (Def_Id)) then
10050 -- The subprogram has formals and hence it may be a primitive of a
10051 -- concurrent type.
10054 Typ := Etype (First_Formal (Def_Id));
10056 if Is_Access_Type (Typ) then
10057 Typ := Directly_Designated_Type (Typ);
10060 if Is_Concurrent_Type (Typ)
10061 and then not Is_Generic_Actual_Type (Typ)
10065 -- This case occurs when the concurrent type is declared within
10066 -- a generic unit. As a result the corresponding record has been
10067 -- built and used as the type of the first formal, we just have
10068 -- to retrieve the corresponding concurrent type.
10070 elsif Is_Concurrent_Record_Type (Typ)
10071 and then not Is_Class_Wide_Type (Typ)
10072 and then Present (Corresponding_Concurrent_Type (Typ))
10074 Typ := Corresponding_Concurrent_Type (Typ);
10082 -- There is no overriding to check if is an inherited operation in a
10083 -- type derivation on for a generic actual.
10085 Collect_Interfaces (Typ, Ifaces_List);
10087 if Is_Empty_Elmt_List (Ifaces_List) then
10091 -- Determine whether entry or subprogram Def_Id overrides a primitive
10092 -- operation that belongs to one of the interfaces in Ifaces_List.
10095 Candidate : Entity_Id := Empty;
10096 Hom : Entity_Id := Empty;
10097 Iface_Typ : Entity_Id;
10098 Subp : Entity_Id := Empty;
10101 -- Traverse the homonym chain, looking for a potentially
10102 -- overridden subprogram that belongs to an implemented
10105 Hom := Current_Entity_In_Scope (Def_Id);
10106 while Present (Hom) loop
10110 or else not Is_Overloadable (Subp)
10111 or else not Is_Primitive (Subp)
10112 or else not Is_Dispatching_Operation (Subp)
10113 or else not Present (Find_Dispatching_Type (Subp))
10114 or else not Is_Interface (Find_Dispatching_Type (Subp))
10118 -- Entries and procedures can override abstract or null
10119 -- interface procedures.
10121 elsif (Ekind (Def_Id) = E_Procedure
10122 or else Ekind (Def_Id) = E_Entry)
10123 and then Ekind (Subp) = E_Procedure
10124 and then Matches_Prefixed_View_Profile
10125 (Parameter_Specifications (Parent (Def_Id)),
10126 Parameter_Specifications (Parent (Subp)))
10130 -- For an overridden subprogram Subp, check whether the mode
10131 -- of its first parameter is correct depending on the kind
10132 -- of synchronized type.
10135 Formal : constant Node_Id := First_Formal (Candidate);
10138 -- In order for an entry or a protected procedure to
10139 -- override, the first parameter of the overridden
10140 -- routine must be of mode "out", "in out" or
10141 -- access-to-variable.
10143 if Ekind_In (Candidate, E_Entry, E_Procedure)
10144 and then Is_Protected_Type (Typ)
10145 and then Ekind (Formal) /= E_In_Out_Parameter
10146 and then Ekind (Formal) /= E_Out_Parameter
10147 and then Nkind (Parameter_Type (Parent (Formal))) /=
10148 N_Access_Definition
10152 -- All other cases are OK since a task entry or routine
10153 -- does not have a restriction on the mode of the first
10154 -- parameter of the overridden interface routine.
10157 Overridden_Subp := Candidate;
10162 -- Functions can override abstract interface functions
10164 elsif Ekind (Def_Id) = E_Function
10165 and then Ekind (Subp) = E_Function
10166 and then Matches_Prefixed_View_Profile
10167 (Parameter_Specifications (Parent (Def_Id)),
10168 Parameter_Specifications (Parent (Subp)))
10169 and then Etype (Result_Definition (Parent (Def_Id))) =
10170 Etype (Result_Definition (Parent (Subp)))
10172 Overridden_Subp := Subp;
10176 Hom := Homonym (Hom);
10179 -- After examining all candidates for overriding, we are left with
10180 -- the best match which is a mode incompatible interface routine.
10181 -- Do not emit an error if the Expander is active since this error
10182 -- will be detected later on after all concurrent types are
10183 -- expanded and all wrappers are built. This check is meant for
10184 -- spec-only compilations.
10186 if Present (Candidate) and then not Expander_Active then
10188 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10190 -- Def_Id is primitive of a protected type, declared inside the
10191 -- type, and the candidate is primitive of a limited or
10192 -- synchronized interface.
10195 and then Is_Protected_Type (Typ)
10197 (Is_Limited_Interface (Iface_Typ)
10198 or else Is_Protected_Interface (Iface_Typ)
10199 or else Is_Synchronized_Interface (Iface_Typ)
10200 or else Is_Task_Interface (Iface_Typ))
10202 Error_Msg_PT (Parent (Typ), Candidate);
10206 Overridden_Subp := Candidate;
10209 end Check_Synchronized_Overriding;
10211 ----------------------------
10212 -- Is_Private_Declaration --
10213 ----------------------------
10215 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10216 Priv_Decls : List_Id;
10217 Decl : constant Node_Id := Unit_Declaration_Node (E);
10220 if Is_Package_Or_Generic_Package (Current_Scope)
10221 and then In_Private_Part (Current_Scope)
10224 Private_Declarations
10225 (Specification (Unit_Declaration_Node (Current_Scope)));
10227 return In_Package_Body (Current_Scope)
10229 (Is_List_Member (Decl)
10230 and then List_Containing (Decl) = Priv_Decls)
10231 or else (Nkind (Parent (Decl)) = N_Package_Specification
10233 Is_Compilation_Unit
10234 (Defining_Entity (Parent (Decl)))
10235 and then List_Containing (Parent (Parent (Decl))) =
10240 end Is_Private_Declaration;
10242 --------------------------
10243 -- Is_Overriding_Alias --
10244 --------------------------
10246 function Is_Overriding_Alias
10247 (Old_E : Entity_Id;
10248 New_E : Entity_Id) return Boolean
10250 AO : constant Entity_Id := Alias (Old_E);
10251 AN : constant Entity_Id := Alias (New_E);
10254 return Scope (AO) /= Scope (AN)
10255 or else No (DTC_Entity (AO))
10256 or else No (DTC_Entity (AN))
10257 or else DT_Position (AO) = DT_Position (AN);
10258 end Is_Overriding_Alias;
10260 -- Start of processing for New_Overloaded_Entity
10263 -- We need to look for an entity that S may override. This must be a
10264 -- homonym in the current scope, so we look for the first homonym of
10265 -- S in the current scope as the starting point for the search.
10267 E := Current_Entity_In_Scope (S);
10269 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10270 -- They are directly added to the list of primitive operations of
10271 -- Derived_Type, unless this is a rederivation in the private part
10272 -- of an operation that was already derived in the visible part of
10273 -- the current package.
10275 if Ada_Version >= Ada_2005
10276 and then Present (Derived_Type)
10277 and then Present (Alias (S))
10278 and then Is_Dispatching_Operation (Alias (S))
10279 and then Present (Find_Dispatching_Type (Alias (S)))
10280 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10282 -- For private types, when the full-view is processed we propagate to
10283 -- the full view the non-overridden entities whose attribute "alias"
10284 -- references an interface primitive. These entities were added by
10285 -- Derive_Subprograms to ensure that interface primitives are
10288 -- Inside_Freeze_Actions is non zero when S corresponds with an
10289 -- internal entity that links an interface primitive with its
10290 -- covering primitive through attribute Interface_Alias (see
10291 -- Add_Internal_Interface_Entities).
10293 if Inside_Freezing_Actions = 0
10294 and then Is_Package_Or_Generic_Package (Current_Scope)
10295 and then In_Private_Part (Current_Scope)
10296 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10297 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10298 and then Full_View (Defining_Identifier (Parent (E)))
10299 = Defining_Identifier (Parent (S))
10300 and then Alias (E) = Alias (S)
10302 Check_Operation_From_Private_View (S, E);
10303 Set_Is_Dispatching_Operation (S);
10308 Enter_Overloaded_Entity (S);
10309 Check_Dispatching_Operation (S, Empty);
10310 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10316 -- If there is no homonym then this is definitely not overriding
10319 Enter_Overloaded_Entity (S);
10320 Check_Dispatching_Operation (S, Empty);
10321 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10323 -- If subprogram has an explicit declaration, check whether it has an
10324 -- overriding indicator.
10326 if Comes_From_Source (S) then
10327 Check_Synchronized_Overriding (S, Overridden_Subp);
10329 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10330 -- it may have overridden some hidden inherited primitive. Update
10331 -- Overridden_Subp to avoid spurious errors when checking the
10332 -- overriding indicator.
10334 if Ada_Version >= Ada_2012
10335 and then No (Overridden_Subp)
10336 and then Is_Dispatching_Operation (S)
10337 and then Present (Overridden_Operation (S))
10339 Overridden_Subp := Overridden_Operation (S);
10342 Check_Overriding_Indicator
10343 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10346 -- If there is a homonym that is not overloadable, then we have an
10347 -- error, except for the special cases checked explicitly below.
10349 elsif not Is_Overloadable (E) then
10351 -- Check for spurious conflict produced by a subprogram that has the
10352 -- same name as that of the enclosing generic package. The conflict
10353 -- occurs within an instance, between the subprogram and the renaming
10354 -- declaration for the package. After the subprogram, the package
10355 -- renaming declaration becomes hidden.
10357 if Ekind (E) = E_Package
10358 and then Present (Renamed_Object (E))
10359 and then Renamed_Object (E) = Current_Scope
10360 and then Nkind (Parent (Renamed_Object (E))) =
10361 N_Package_Specification
10362 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10365 Set_Is_Immediately_Visible (E, False);
10366 Enter_Overloaded_Entity (S);
10367 Set_Homonym (S, Homonym (E));
10368 Check_Dispatching_Operation (S, Empty);
10369 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10371 -- If the subprogram is implicit it is hidden by the previous
10372 -- declaration. However if it is dispatching, it must appear in the
10373 -- dispatch table anyway, because it can be dispatched to even if it
10374 -- cannot be called directly.
10376 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10377 Set_Scope (S, Current_Scope);
10379 if Is_Dispatching_Operation (Alias (S)) then
10380 Check_Dispatching_Operation (S, Empty);
10386 Error_Msg_Sloc := Sloc (E);
10388 -- Generate message, with useful additional warning if in generic
10390 if Is_Generic_Unit (E) then
10391 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10392 Error_Msg_N ("\& conflicts with declaration#", S);
10394 Error_Msg_N ("& conflicts with declaration#", S);
10400 -- E exists and is overloadable
10403 Check_Synchronized_Overriding (S, Overridden_Subp);
10405 -- Loop through E and its homonyms to determine if any of them is
10406 -- the candidate for overriding by S.
10408 while Present (E) loop
10410 -- Definitely not interesting if not in the current scope
10412 if Scope (E) /= Current_Scope then
10415 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10416 -- procedures locate the internally generated spec. We enforce
10417 -- mode conformance since a tagged type may inherit from
10418 -- interfaces several null primitives which differ only in
10419 -- the mode of the formals.
10421 elsif not Comes_From_Source (S)
10422 and then Is_Null_Procedure (S)
10423 and then not Mode_Conformant (E, S)
10427 -- Check if we have type conformance
10429 elsif Type_Conformant (E, S) then
10431 -- If the old and new entities have the same profile and one
10432 -- is not the body of the other, then this is an error, unless
10433 -- one of them is implicitly declared.
10435 -- There are some cases when both can be implicit, for example
10436 -- when both a literal and a function that overrides it are
10437 -- inherited in a derivation, or when an inherited operation
10438 -- of a tagged full type overrides the inherited operation of
10439 -- a private extension. Ada 83 had a special rule for the
10440 -- literal case. In Ada 95, the later implicit operation hides
10441 -- the former, and the literal is always the former. In the
10442 -- odd case where both are derived operations declared at the
10443 -- same point, both operations should be declared, and in that
10444 -- case we bypass the following test and proceed to the next
10445 -- part. This can only occur for certain obscure cases in
10446 -- instances, when an operation on a type derived from a formal
10447 -- private type does not override a homograph inherited from
10448 -- the actual. In subsequent derivations of such a type, the
10449 -- DT positions of these operations remain distinct, if they
10452 if Present (Alias (S))
10453 and then (No (Alias (E))
10454 or else Comes_From_Source (E)
10455 or else Is_Abstract_Subprogram (S)
10457 (Is_Dispatching_Operation (E)
10458 and then Is_Overriding_Alias (E, S)))
10459 and then Ekind (E) /= E_Enumeration_Literal
10461 -- When an derived operation is overloaded it may be due to
10462 -- the fact that the full view of a private extension
10463 -- re-inherits. It has to be dealt with.
10465 if Is_Package_Or_Generic_Package (Current_Scope)
10466 and then In_Private_Part (Current_Scope)
10468 Check_Operation_From_Private_View (S, E);
10471 -- In any case the implicit operation remains hidden by the
10472 -- existing declaration, which is overriding. Indicate that
10473 -- E overrides the operation from which S is inherited.
10475 if Present (Alias (S)) then
10476 Set_Overridden_Operation (E, Alias (S));
10478 Set_Overridden_Operation (E, S);
10481 if Comes_From_Source (E) then
10482 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10487 -- Within an instance, the renaming declarations for actual
10488 -- subprograms may become ambiguous, but they do not hide each
10491 elsif Ekind (E) /= E_Entry
10492 and then not Comes_From_Source (E)
10493 and then not Is_Generic_Instance (E)
10494 and then (Present (Alias (E))
10495 or else Is_Intrinsic_Subprogram (E))
10496 and then (not In_Instance
10497 or else No (Parent (E))
10498 or else Nkind (Unit_Declaration_Node (E)) /=
10499 N_Subprogram_Renaming_Declaration)
10501 -- A subprogram child unit is not allowed to override an
10502 -- inherited subprogram (10.1.1(20)).
10504 if Is_Child_Unit (S) then
10506 ("child unit overrides inherited subprogram in parent",
10511 if Is_Non_Overriding_Operation (E, S) then
10512 Enter_Overloaded_Entity (S);
10514 if No (Derived_Type)
10515 or else Is_Tagged_Type (Derived_Type)
10517 Check_Dispatching_Operation (S, Empty);
10523 -- E is a derived operation or an internal operator which
10524 -- is being overridden. Remove E from further visibility.
10525 -- Furthermore, if E is a dispatching operation, it must be
10526 -- replaced in the list of primitive operations of its type
10527 -- (see Override_Dispatching_Operation).
10529 Overridden_Subp := E;
10535 Prev := First_Entity (Current_Scope);
10536 while Present (Prev) and then Next_Entity (Prev) /= E loop
10537 Next_Entity (Prev);
10540 -- It is possible for E to be in the current scope and
10541 -- yet not in the entity chain. This can only occur in a
10542 -- generic context where E is an implicit concatenation
10543 -- in the formal part, because in a generic body the
10544 -- entity chain starts with the formals.
10547 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10549 -- E must be removed both from the entity_list of the
10550 -- current scope, and from the visibility chain
10552 if Debug_Flag_E then
10553 Write_Str ("Override implicit operation ");
10554 Write_Int (Int (E));
10558 -- If E is a predefined concatenation, it stands for four
10559 -- different operations. As a result, a single explicit
10560 -- declaration does not hide it. In a possible ambiguous
10561 -- situation, Disambiguate chooses the user-defined op,
10562 -- so it is correct to retain the previous internal one.
10564 if Chars (E) /= Name_Op_Concat
10565 or else Ekind (E) /= E_Operator
10567 -- For nondispatching derived operations that are
10568 -- overridden by a subprogram declared in the private
10569 -- part of a package, we retain the derived subprogram
10570 -- but mark it as not immediately visible. If the
10571 -- derived operation was declared in the visible part
10572 -- then this ensures that it will still be visible
10573 -- outside the package with the proper signature
10574 -- (calls from outside must also be directed to this
10575 -- version rather than the overriding one, unlike the
10576 -- dispatching case). Calls from inside the package
10577 -- will still resolve to the overriding subprogram
10578 -- since the derived one is marked as not visible
10579 -- within the package.
10581 -- If the private operation is dispatching, we achieve
10582 -- the overriding by keeping the implicit operation
10583 -- but setting its alias to be the overriding one. In
10584 -- this fashion the proper body is executed in all
10585 -- cases, but the original signature is used outside
10588 -- If the overriding is not in the private part, we
10589 -- remove the implicit operation altogether.
10591 if Is_Private_Declaration (S) then
10592 if not Is_Dispatching_Operation (E) then
10593 Set_Is_Immediately_Visible (E, False);
10595 -- Work done in Override_Dispatching_Operation,
10596 -- so nothing else needs to be done here.
10602 -- Find predecessor of E in Homonym chain
10604 if E = Current_Entity (E) then
10607 Prev_Vis := Current_Entity (E);
10608 while Homonym (Prev_Vis) /= E loop
10609 Prev_Vis := Homonym (Prev_Vis);
10613 if Prev_Vis /= Empty then
10615 -- Skip E in the visibility chain
10617 Set_Homonym (Prev_Vis, Homonym (E));
10620 Set_Name_Entity_Id (Chars (E), Homonym (E));
10623 Set_Next_Entity (Prev, Next_Entity (E));
10625 if No (Next_Entity (Prev)) then
10626 Set_Last_Entity (Current_Scope, Prev);
10631 Enter_Overloaded_Entity (S);
10633 -- For entities generated by Derive_Subprograms the
10634 -- overridden operation is the inherited primitive
10635 -- (which is available through the attribute alias).
10637 if not (Comes_From_Source (E))
10638 and then Is_Dispatching_Operation (E)
10639 and then Find_Dispatching_Type (E) =
10640 Find_Dispatching_Type (S)
10641 and then Present (Alias (E))
10642 and then Comes_From_Source (Alias (E))
10644 Set_Overridden_Operation (S, Alias (E));
10646 -- Normal case of setting entity as overridden
10648 -- Note: Static_Initialization and Overridden_Operation
10649 -- attributes use the same field in subprogram entities.
10650 -- Static_Initialization is only defined for internal
10651 -- initialization procedures, where Overridden_Operation
10652 -- is irrelevant. Therefore the setting of this attribute
10653 -- must check whether the target is an init_proc.
10655 elsif not Is_Init_Proc (S) then
10656 Set_Overridden_Operation (S, E);
10659 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10661 -- If S is a user-defined subprogram or a null procedure
10662 -- expanded to override an inherited null procedure, or a
10663 -- predefined dispatching primitive then indicate that E
10664 -- overrides the operation from which S is inherited.
10666 if Comes_From_Source (S)
10668 (Present (Parent (S))
10670 Nkind (Parent (S)) = N_Procedure_Specification
10672 Null_Present (Parent (S)))
10674 (Present (Alias (E))
10676 Is_Predefined_Dispatching_Operation (Alias (E)))
10678 if Present (Alias (E)) then
10679 Set_Overridden_Operation (S, Alias (E));
10683 if Is_Dispatching_Operation (E) then
10685 -- An overriding dispatching subprogram inherits the
10686 -- convention of the overridden subprogram (AI-117).
10688 Set_Convention (S, Convention (E));
10689 Check_Dispatching_Operation (S, E);
10692 Check_Dispatching_Operation (S, Empty);
10695 Check_For_Primitive_Subprogram
10696 (Is_Primitive_Subp, Is_Overriding => True);
10697 goto Check_Inequality;
10700 -- Apparent redeclarations in instances can occur when two
10701 -- formal types get the same actual type. The subprograms in
10702 -- in the instance are legal, even if not callable from the
10703 -- outside. Calls from within are disambiguated elsewhere.
10704 -- For dispatching operations in the visible part, the usual
10705 -- rules apply, and operations with the same profile are not
10706 -- legal (B830001).
10708 elsif (In_Instance_Visible_Part
10709 and then not Is_Dispatching_Operation (E))
10710 or else In_Instance_Not_Visible
10714 -- Here we have a real error (identical profile)
10717 Error_Msg_Sloc := Sloc (E);
10719 -- Avoid cascaded errors if the entity appears in
10720 -- subsequent calls.
10722 Set_Scope (S, Current_Scope);
10724 -- Generate error, with extra useful warning for the case
10725 -- of a generic instance with no completion.
10727 if Is_Generic_Instance (S)
10728 and then not Has_Completion (E)
10731 ("instantiation cannot provide body for&", S);
10732 Error_Msg_N ("\& conflicts with declaration#", S);
10734 Error_Msg_N ("& conflicts with declaration#", S);
10741 -- If one subprogram has an access parameter and the other
10742 -- a parameter of an access type, calls to either might be
10743 -- ambiguous. Verify that parameters match except for the
10744 -- access parameter.
10746 if May_Hide_Profile then
10752 F1 := First_Formal (S);
10753 F2 := First_Formal (E);
10754 while Present (F1) and then Present (F2) loop
10755 if Is_Access_Type (Etype (F1)) then
10756 if not Is_Access_Type (Etype (F2))
10757 or else not Conforming_Types
10758 (Designated_Type (Etype (F1)),
10759 Designated_Type (Etype (F2)),
10762 May_Hide_Profile := False;
10766 not Conforming_Types
10767 (Etype (F1), Etype (F2), Type_Conformant)
10769 May_Hide_Profile := False;
10776 if May_Hide_Profile
10780 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10789 -- On exit, we know that S is a new entity
10791 Enter_Overloaded_Entity (S);
10792 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10793 Check_Overriding_Indicator
10794 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10796 -- Overloading is not allowed in SPARK, except for operators
10798 if Nkind (S) /= N_Defining_Operator_Symbol then
10799 Error_Msg_Sloc := Sloc (Homonym (S));
10800 Check_SPARK_Restriction
10801 ("overloading not allowed with entity#", S);
10804 -- If S is a derived operation for an untagged type then by
10805 -- definition it's not a dispatching operation (even if the parent
10806 -- operation was dispatching), so Check_Dispatching_Operation is not
10807 -- called in that case.
10809 if No (Derived_Type)
10810 or else Is_Tagged_Type (Derived_Type)
10812 Check_Dispatching_Operation (S, Empty);
10816 -- If this is a user-defined equality operator that is not a derived
10817 -- subprogram, create the corresponding inequality. If the operation is
10818 -- dispatching, the expansion is done elsewhere, and we do not create
10819 -- an explicit inequality operation.
10821 <<Check_Inequality>>
10822 if Chars (S) = Name_Op_Eq
10823 and then Etype (S) = Standard_Boolean
10824 and then Present (Parent (S))
10825 and then not Is_Dispatching_Operation (S)
10827 Make_Inequality_Operator (S);
10829 if Ada_Version >= Ada_2012 then
10830 Check_Untagged_Equality (S);
10833 end New_Overloaded_Entity;
10835 ---------------------
10836 -- Process_Formals --
10837 ---------------------
10839 procedure Process_Formals
10841 Related_Nod : Node_Id)
10843 Param_Spec : Node_Id;
10844 Formal : Entity_Id;
10845 Formal_Type : Entity_Id;
10849 Num_Out_Params : Nat := 0;
10850 First_Out_Param : Entity_Id := Empty;
10851 -- Used for setting Is_Only_Out_Parameter
10853 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10854 -- Determine whether an access type designates a type coming from a
10857 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10858 -- Check whether the default has a class-wide type. After analysis the
10859 -- default has the type of the formal, so we must also check explicitly
10860 -- for an access attribute.
10862 -------------------------------
10863 -- Designates_From_With_Type --
10864 -------------------------------
10866 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10867 Desig : Entity_Id := Typ;
10870 if Is_Access_Type (Desig) then
10871 Desig := Directly_Designated_Type (Desig);
10874 if Is_Class_Wide_Type (Desig) then
10875 Desig := Root_Type (Desig);
10879 Ekind (Desig) = E_Incomplete_Type and then From_With_Type (Desig);
10880 end Designates_From_With_Type;
10882 ---------------------------
10883 -- Is_Class_Wide_Default --
10884 ---------------------------
10886 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10888 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10889 or else (Nkind (D) = N_Attribute_Reference
10890 and then Attribute_Name (D) = Name_Access
10891 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10892 end Is_Class_Wide_Default;
10894 -- Start of processing for Process_Formals
10897 -- In order to prevent premature use of the formals in the same formal
10898 -- part, the Ekind is left undefined until all default expressions are
10899 -- analyzed. The Ekind is established in a separate loop at the end.
10901 Param_Spec := First (T);
10902 while Present (Param_Spec) loop
10903 Formal := Defining_Identifier (Param_Spec);
10904 Set_Never_Set_In_Source (Formal, True);
10905 Enter_Name (Formal);
10907 -- Case of ordinary parameters
10909 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10910 Find_Type (Parameter_Type (Param_Spec));
10911 Ptype := Parameter_Type (Param_Spec);
10913 if Ptype = Error then
10917 Formal_Type := Entity (Ptype);
10919 if Is_Incomplete_Type (Formal_Type)
10921 (Is_Class_Wide_Type (Formal_Type)
10922 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10924 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10925 -- primitive operations, as long as their completion is
10926 -- in the same declarative part. If in the private part
10927 -- this means that the type cannot be a Taft-amendment type.
10928 -- Check is done on package exit. For access to subprograms,
10929 -- the use is legal for Taft-amendment types.
10931 -- Ada 2012: tagged incomplete types are allowed as generic
10932 -- formal types. They do not introduce dependencies and the
10933 -- corresponding generic subprogram does not have a delayed
10934 -- freeze, because it does not need a freeze node.
10936 if Is_Tagged_Type (Formal_Type) then
10937 if Ekind (Scope (Current_Scope)) = E_Package
10938 and then not From_With_Type (Formal_Type)
10939 and then not Is_Generic_Type (Formal_Type)
10940 and then not Is_Class_Wide_Type (Formal_Type)
10943 (Parent (T), N_Access_Function_Definition,
10944 N_Access_Procedure_Definition)
10948 Private_Dependents (Base_Type (Formal_Type)));
10950 -- Freezing is delayed to ensure that Register_Prim
10951 -- will get called for this operation, which is needed
10952 -- in cases where static dispatch tables aren't built.
10953 -- (Note that the same is done for controlling access
10954 -- parameter cases in function Access_Definition.)
10956 Set_Has_Delayed_Freeze (Current_Scope);
10960 -- Special handling of Value_Type for CIL case
10962 elsif Is_Value_Type (Formal_Type) then
10965 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10966 N_Access_Procedure_Definition)
10968 -- AI05-0151: Tagged incomplete types are allowed in all
10969 -- formal parts. Untagged incomplete types are not allowed
10972 if Ada_Version >= Ada_2012 then
10973 if Is_Tagged_Type (Formal_Type) then
10976 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10981 ("invalid use of untagged incomplete type&",
10982 Ptype, Formal_Type);
10987 ("invalid use of incomplete type&",
10988 Param_Spec, Formal_Type);
10990 -- Further checks on the legality of incomplete types
10991 -- in formal parts are delayed until the freeze point
10992 -- of the enclosing subprogram or access to subprogram.
10996 elsif Ekind (Formal_Type) = E_Void then
10998 ("premature use of&",
10999 Parameter_Type (Param_Spec), Formal_Type);
11002 -- Ada 2012 (AI-142): Handle aliased parameters
11004 if Ada_Version >= Ada_2012
11005 and then Aliased_Present (Param_Spec)
11007 Set_Is_Aliased (Formal);
11010 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11011 -- declaration corresponding to the null-excluding type of the
11012 -- formal in the enclosing scope. Finally, replace the parameter
11013 -- type of the formal with the internal subtype.
11015 if Ada_Version >= Ada_2005
11016 and then Null_Exclusion_Present (Param_Spec)
11018 if not Is_Access_Type (Formal_Type) then
11020 ("`NOT NULL` allowed only for an access type", Param_Spec);
11023 if Can_Never_Be_Null (Formal_Type)
11024 and then Comes_From_Source (Related_Nod)
11027 ("`NOT NULL` not allowed (& already excludes null)",
11028 Param_Spec, Formal_Type);
11032 Create_Null_Excluding_Itype
11034 Related_Nod => Related_Nod,
11035 Scope_Id => Scope (Current_Scope));
11037 -- If the designated type of the itype is an itype that is
11038 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11039 -- on the access subtype, to prevent order-of-elaboration
11040 -- issues in the backend.
11043 -- type T is access procedure;
11044 -- procedure Op (O : not null T);
11046 if Is_Itype (Directly_Designated_Type (Formal_Type))
11048 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11050 Set_Has_Delayed_Freeze (Formal_Type);
11055 -- An access formal type
11059 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11061 -- No need to continue if we already notified errors
11063 if not Present (Formal_Type) then
11067 -- Ada 2005 (AI-254)
11070 AD : constant Node_Id :=
11071 Access_To_Subprogram_Definition
11072 (Parameter_Type (Param_Spec));
11074 if Present (AD) and then Protected_Present (AD) then
11076 Replace_Anonymous_Access_To_Protected_Subprogram
11082 Set_Etype (Formal, Formal_Type);
11084 -- Deal with default expression if present
11086 Default := Expression (Param_Spec);
11088 if Present (Default) then
11089 Check_SPARK_Restriction
11090 ("default expression is not allowed", Default);
11092 if Out_Present (Param_Spec) then
11094 ("default initialization only allowed for IN parameters",
11098 -- Do the special preanalysis of the expression (see section on
11099 -- "Handling of Default Expressions" in the spec of package Sem).
11101 Preanalyze_Spec_Expression (Default, Formal_Type);
11103 -- An access to constant cannot be the default for
11104 -- an access parameter that is an access to variable.
11106 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11107 and then not Is_Access_Constant (Formal_Type)
11108 and then Is_Access_Type (Etype (Default))
11109 and then Is_Access_Constant (Etype (Default))
11112 ("formal that is access to variable cannot be initialized " &
11113 "with an access-to-constant expression", Default);
11116 -- Check that the designated type of an access parameter's default
11117 -- is not a class-wide type unless the parameter's designated type
11118 -- is also class-wide.
11120 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11121 and then not Designates_From_With_Type (Formal_Type)
11122 and then Is_Class_Wide_Default (Default)
11123 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11126 ("access to class-wide expression not allowed here", Default);
11129 -- Check incorrect use of dynamically tagged expressions
11131 if Is_Tagged_Type (Formal_Type) then
11132 Check_Dynamically_Tagged_Expression
11134 Typ => Formal_Type,
11135 Related_Nod => Default);
11139 -- Ada 2005 (AI-231): Static checks
11141 if Ada_Version >= Ada_2005
11142 and then Is_Access_Type (Etype (Formal))
11143 and then Can_Never_Be_Null (Etype (Formal))
11145 Null_Exclusion_Static_Checks (Param_Spec);
11152 -- If this is the formal part of a function specification, analyze the
11153 -- subtype mark in the context where the formals are visible but not
11154 -- yet usable, and may hide outer homographs.
11156 if Nkind (Related_Nod) = N_Function_Specification then
11157 Analyze_Return_Type (Related_Nod);
11160 -- Now set the kind (mode) of each formal
11162 Param_Spec := First (T);
11163 while Present (Param_Spec) loop
11164 Formal := Defining_Identifier (Param_Spec);
11165 Set_Formal_Mode (Formal);
11167 if Ekind (Formal) = E_In_Parameter then
11168 Set_Default_Value (Formal, Expression (Param_Spec));
11170 if Present (Expression (Param_Spec)) then
11171 Default := Expression (Param_Spec);
11173 if Is_Scalar_Type (Etype (Default)) then
11174 if Nkind (Parameter_Type (Param_Spec)) /=
11175 N_Access_Definition
11177 Formal_Type := Entity (Parameter_Type (Param_Spec));
11181 (Related_Nod, Parameter_Type (Param_Spec));
11184 Apply_Scalar_Range_Check (Default, Formal_Type);
11188 elsif Ekind (Formal) = E_Out_Parameter then
11189 Num_Out_Params := Num_Out_Params + 1;
11191 if Num_Out_Params = 1 then
11192 First_Out_Param := Formal;
11195 elsif Ekind (Formal) = E_In_Out_Parameter then
11196 Num_Out_Params := Num_Out_Params + 1;
11199 -- Skip remaining processing if formal type was in error
11201 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11202 goto Next_Parameter;
11205 -- Force call by reference if aliased
11207 if Is_Aliased (Formal) then
11208 Set_Mechanism (Formal, By_Reference);
11210 -- Warn if user asked this to be passed by copy
11212 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11214 ("cannot pass aliased parameter & by copy?", Formal);
11217 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11219 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11220 Set_Mechanism (Formal, By_Copy);
11222 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11223 Set_Mechanism (Formal, By_Reference);
11230 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11231 Set_Is_Only_Out_Parameter (First_Out_Param);
11233 end Process_Formals;
11239 procedure Process_PPCs
11241 Spec_Id : Entity_Id;
11242 Body_Id : Entity_Id)
11244 Loc : constant Source_Ptr := Sloc (N);
11248 Designator : Entity_Id;
11249 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11251 Precond : Node_Id := Empty;
11252 -- Set non-Empty if we prepend precondition to the declarations. This
11253 -- is used to hook up inherited preconditions (adding the condition
11254 -- expression with OR ELSE, and adding the message).
11256 Inherited_Precond : Node_Id;
11257 -- Precondition inherited from parent subprogram
11259 Inherited : constant Subprogram_List :=
11260 Inherited_Subprograms (Spec_Id);
11261 -- List of subprograms inherited by this subprogram
11263 Plist : List_Id := No_List;
11264 -- List of generated postconditions
11266 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
11267 -- Append a node to a list. If there is no list, create a new one. When
11268 -- the item denotes a pragma, it is added to the list only when it is
11271 procedure Check_Access_Invariants (E : Entity_Id);
11272 -- If the subprogram returns an access to a type with invariants, or
11273 -- has access parameters whose designated type has an invariant, then
11274 -- under the same visibility conditions as for other invariant checks,
11275 -- the type invariant must be applied to the returned value.
11277 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11278 -- Prag contains an analyzed precondition or postcondition pragma. This
11279 -- function copies the pragma, changes it to the corresponding Check
11280 -- pragma and returns the Check pragma as the result. If Pspec is non-
11281 -- empty, this is the case of inheriting a PPC, where we must change
11282 -- references to parameters of the inherited subprogram to point to the
11283 -- corresponding parameters of the current subprogram.
11285 function Has_Checked_Predicate (Typ : Entity_Id) return Boolean;
11286 -- Determine whether type Typ has or inherits at least one predicate
11287 -- aspect or pragma, for which the applicable policy is Checked.
11289 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
11290 -- Determine whether the body of procedure Proc_Id contains a sole null
11291 -- statement, possibly followed by an optional return.
11293 procedure Insert_After_Last_Declaration (Nod : Node_Id);
11294 -- Insert node Nod after the last declaration of the context
11296 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11297 -- T is the entity for a private type for which invariants are defined.
11298 -- This function returns True if the procedure corresponding to the
11299 -- value of Designator is a public procedure from the point of view of
11300 -- this type (i.e. its spec is in the visible part of the package that
11301 -- contains the declaration of the private type). A True value means
11302 -- that an invariant check is required (for an IN OUT parameter, or
11303 -- the returned value of a function.
11305 -------------------------
11306 -- Append_Enabled_Item --
11307 -------------------------
11309 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
11311 -- Do not chain ignored or disabled pragmas
11313 if Nkind (Item) = N_Pragma
11314 and then (Is_Ignored (Item) or else Is_Disabled (Item))
11325 Append (Item, List);
11327 end Append_Enabled_Item;
11329 -----------------------------
11330 -- Check_Access_Invariants --
11331 -----------------------------
11333 procedure Check_Access_Invariants (E : Entity_Id) is
11339 if Is_Access_Type (Etype (E))
11340 and then not Is_Access_Constant (Etype (E))
11342 Typ := Designated_Type (Etype (E));
11344 if Has_Invariants (Typ)
11345 and then Present (Invariant_Procedure (Typ))
11346 and then not Has_Null_Body (Invariant_Procedure (Typ))
11347 and then Is_Public_Subprogram_For (Typ)
11350 Make_Explicit_Dereference (Loc,
11351 Prefix => New_Occurrence_Of (E, Loc));
11352 Set_Etype (Obj, Typ);
11354 Call := Make_Invariant_Call (Obj);
11356 Append_Enabled_Item
11357 (Make_If_Statement (Loc,
11360 Left_Opnd => Make_Null (Loc),
11361 Right_Opnd => New_Occurrence_Of (E, Loc)),
11362 Then_Statements => New_List (Call)),
11366 end Check_Access_Invariants;
11372 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
11373 Nam : constant Name_Id := Pragma_Name (Prag);
11378 -- Effective name of pragma (maybe Pre/Post rather than Precondition/
11379 -- Postcodition if the pragma came from a Pre/Post aspect). We need
11380 -- the name right when we generate the Check pragma, since we want
11381 -- the right set of check policies to apply.
11384 -- Prepare map if this is the case where we have to map entities of
11385 -- arguments in the overridden subprogram to corresponding entities
11386 -- of the current subprogram.
11397 Map := New_Elmt_List;
11398 PF := First_Formal (Pspec);
11399 CF := First_Formal (Designator);
11400 while Present (PF) loop
11401 Append_Elmt (PF, Map);
11402 Append_Elmt (CF, Map);
11409 -- Now we can copy the tree, doing any required substitutions
11411 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
11413 -- Set Analyzed to false, since we want to reanalyze the check
11414 -- procedure. Note that it is only at the outer level that we
11415 -- do this fiddling, for the spec cases, the already preanalyzed
11416 -- parameters are not affected.
11418 Set_Analyzed (CP, False);
11420 -- We also make sure Comes_From_Source is False for the copy
11422 Set_Comes_From_Source (CP, False);
11424 -- For a postcondition pragma within a generic, preserve the pragma
11425 -- for later expansion. This is also used when an error was detected,
11426 -- thus setting Expander_Active to False.
11428 if Nam = Name_Postcondition
11429 and then not Expander_Active
11434 -- Get effective name of aspect
11436 if Present (Corresponding_Aspect (Prag)) then
11437 Ename := Chars (Identifier (Corresponding_Aspect (Prag)));
11442 -- Change copy of pragma into corresponding pragma Check
11444 Prepend_To (Pragma_Argument_Associations (CP),
11445 Make_Pragma_Argument_Association (Sloc (Prag),
11446 Expression => Make_Identifier (Loc, Ename)));
11447 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
11449 -- If this is inherited case and the current message starts with
11450 -- "failed p", we change it to "failed inherited p...".
11452 if Present (Pspec) then
11454 Msg : constant Node_Id :=
11455 Last (Pragma_Argument_Associations (CP));
11458 if Chars (Msg) = Name_Message then
11459 String_To_Name_Buffer (Strval (Expression (Msg)));
11461 if Name_Buffer (1 .. 8) = "failed p" then
11462 Insert_Str_In_Name_Buffer ("inherited ", 8);
11464 (Expression (Last (Pragma_Argument_Associations (CP))),
11465 String_From_Name_Buffer);
11471 -- Return the check pragma
11476 ---------------------------
11477 -- Has_Checked_Predicate --
11478 ---------------------------
11480 function Has_Checked_Predicate (Typ : Entity_Id) return Boolean is
11485 -- Climb the ancestor type chain staring from the input. This is done
11486 -- because the input type may lack aspect/pragma predicate and simply
11487 -- inherit those from its ancestor.
11489 -- Note that predicate pragmas include all three cases of predicate
11490 -- aspects (Predicate, Dynamic_Predicate, Static_Predicate), so this
11491 -- routine checks for all three cases.
11494 while Present (Anc) loop
11495 Pred := Get_Pragma (Anc, Pragma_Predicate);
11497 if Present (Pred) and then not Is_Ignored (Pred) then
11501 Anc := Nearest_Ancestor (Anc);
11505 end Has_Checked_Predicate;
11507 -------------------
11508 -- Has_Null_Body --
11509 -------------------
11511 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
11512 Body_Id : Entity_Id;
11519 Spec := Parent (Proc_Id);
11520 Decl := Parent (Spec);
11522 -- Retrieve the entity of the invariant procedure body
11524 if Nkind (Spec) = N_Procedure_Specification
11525 and then Nkind (Decl) = N_Subprogram_Declaration
11527 Body_Id := Corresponding_Body (Decl);
11529 -- The body acts as a spec
11532 Body_Id := Proc_Id;
11535 -- The body will be generated later
11537 if No (Body_Id) then
11541 Spec := Parent (Body_Id);
11542 Decl := Parent (Spec);
11545 (Nkind (Spec) = N_Procedure_Specification
11546 and then Nkind (Decl) = N_Subprogram_Body);
11548 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11550 -- Look for a null statement followed by an optional return statement
11552 if Nkind (Stmt1) = N_Null_Statement then
11553 Stmt2 := Next (Stmt1);
11555 if Present (Stmt2) then
11556 return Nkind (Stmt2) = N_Simple_Return_Statement;
11565 -----------------------------------
11566 -- Insert_After_Last_Declaration --
11567 -----------------------------------
11569 procedure Insert_After_Last_Declaration (Nod : Node_Id) is
11570 Decls : constant List_Id := Declarations (N);
11574 Set_Declarations (N, New_List (Nod));
11576 Append_To (Decls, Nod);
11578 end Insert_After_Last_Declaration;
11580 ------------------------------
11581 -- Is_Public_Subprogram_For --
11582 ------------------------------
11584 -- The type T is a private type, its declaration is therefore in
11585 -- the list of public declarations of some package. The test for a
11586 -- public subprogram is that its declaration is in this same list
11587 -- of declarations for the same package (note that all the public
11588 -- declarations are in one list, and all the private declarations
11589 -- in another, so this deals with the public/private distinction).
11591 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
11592 DD : constant Node_Id := Unit_Declaration_Node (Designator);
11593 -- The subprogram declaration for the subprogram in question
11595 TL : constant List_Id :=
11596 Visible_Declarations
11597 (Specification (Unit_Declaration_Node (Scope (T))));
11598 -- The list of declarations containing the private declaration of
11599 -- the type. We know it is a private type, so we know its scope is
11600 -- the package in question, and we know it must be in the visible
11601 -- declarations of this package.
11604 -- If the subprogram declaration is not a list member, it must be
11605 -- an Init_Proc, in which case we want to consider it to be a
11606 -- public subprogram, since we do get initializations to deal with.
11607 -- Other internally generated subprograms are not public.
11609 if not Is_List_Member (DD)
11610 and then Is_Init_Proc (Defining_Entity (DD))
11614 -- The declaration may have been generated for an expression function
11615 -- so check whether that function comes from source.
11617 elsif not Comes_From_Source (DD)
11619 (Nkind (Original_Node (DD)) /= N_Expression_Function
11620 or else not Comes_From_Source (Defining_Entity (DD)))
11624 -- Otherwise we test whether the subprogram is declared in the
11625 -- visible declarations of the package containing the type.
11628 return TL = List_Containing (DD);
11630 end Is_Public_Subprogram_For;
11635 Formal_Typ : Entity_Id;
11636 Func_Typ : Entity_Id;
11637 Post_Proc : Entity_Id;
11640 -- Start of processing for Process_PPCs
11643 -- Capture designator from spec if present, else from body
11645 if Present (Spec_Id) then
11646 Designator := Spec_Id;
11648 Designator := Body_Id;
11651 -- Do not process a predicate function as its body will contain a
11652 -- recursive call to itself and blow up the stack.
11654 if Ekind (Designator) = E_Function
11655 and then Is_Predicate_Function (Designator)
11659 -- Internally generated subprograms, such as type-specific functions,
11660 -- don't get assertion checks.
11662 elsif Get_TSS_Name (Designator) /= TSS_Null then
11666 -- Grab preconditions from spec
11668 if Present (Spec_Id) then
11670 -- Loop through PPC pragmas from spec. Note that preconditions from
11671 -- the body will be analyzed and converted when we scan the body
11672 -- declarations below.
11674 Prag := Pre_Post_Conditions (Contract (Spec_Id));
11675 while Present (Prag) loop
11676 if Pragma_Name (Prag) = Name_Precondition then
11678 -- For Pre (or Precondition pragma), we simply prepend the
11679 -- pragma to the list of declarations right away so that it
11680 -- will be executed at the start of the procedure. Note that
11681 -- this processing reverses the order of the list, which is
11682 -- what we want since new entries were chained to the head of
11683 -- the list. There can be more than one precondition when we
11684 -- use pragma Precondition.
11686 if not Class_Present (Prag) then
11687 Prepend (Grab_PPC, Declarations (N));
11689 -- For Pre'Class there can only be one pragma, and we save
11690 -- it in Precond for now. We will add inherited Pre'Class
11691 -- stuff before inserting this pragma in the declarations.
11693 Precond := Grab_PPC;
11697 Prag := Next_Pragma (Prag);
11700 -- Now deal with inherited preconditions
11702 for J in Inherited'Range loop
11703 Prag := Pre_Post_Conditions (Contract (Inherited (J)));
11705 while Present (Prag) loop
11706 if Pragma_Name (Prag) = Name_Precondition
11707 and then Class_Present (Prag)
11709 Inherited_Precond := Grab_PPC (Inherited (J));
11711 -- No precondition so far, so establish this as the first
11713 if No (Precond) then
11714 Precond := Inherited_Precond;
11716 -- Here we already have a precondition, add inherited one
11719 -- Add new precondition to old one using OR ELSE
11722 New_Expr : constant Node_Id :=
11724 (Next (First (Pragma_Argument_Associations
11725 (Inherited_Precond))));
11726 Old_Expr : constant Node_Id :=
11728 (Next (First (Pragma_Argument_Associations
11732 if Paren_Count (Old_Expr) = 0 then
11733 Set_Paren_Count (Old_Expr, 1);
11736 if Paren_Count (New_Expr) = 0 then
11737 Set_Paren_Count (New_Expr, 1);
11741 Make_Or_Else (Sloc (Old_Expr),
11742 Left_Opnd => Relocate_Node (Old_Expr),
11743 Right_Opnd => New_Expr));
11746 -- Add new message in the form:
11748 -- failed precondition from bla
11749 -- also failed inherited precondition from bla
11752 -- Skip this if exception locations are suppressed
11754 if not Exception_Locations_Suppressed then
11756 New_Msg : constant Node_Id :=
11759 (Pragma_Argument_Associations
11760 (Inherited_Precond)));
11761 Old_Msg : constant Node_Id :=
11764 (Pragma_Argument_Associations
11767 Start_String (Strval (Old_Msg));
11768 Store_String_Chars (ASCII.LF & " also ");
11769 Store_String_Chars (Strval (New_Msg));
11770 Set_Strval (Old_Msg, End_String);
11776 Prag := Next_Pragma (Prag);
11780 -- If we have built a precondition for Pre'Class (including any
11781 -- Pre'Class aspects inherited from parent subprograms), then we
11782 -- insert this composite precondition at this stage.
11784 if Present (Precond) then
11785 Prepend (Precond, Declarations (N));
11789 -- Build postconditions procedure if needed and prepend the following
11790 -- declaration to the start of the declarations for the subprogram.
11792 -- procedure _postconditions [(_Result : resulttype)] is
11794 -- pragma Check (Postcondition, condition [,message]);
11795 -- pragma Check (Postcondition, condition [,message]);
11797 -- Invariant_Procedure (_Result) ...
11798 -- Invariant_Procedure (Arg1)
11802 -- First we deal with the postconditions in the body
11804 if Is_Non_Empty_List (Declarations (N)) then
11806 -- Loop through declarations
11808 Prag := First (Declarations (N));
11809 while Present (Prag) loop
11810 if Nkind (Prag) = N_Pragma then
11812 -- Capture postcondition pragmas
11814 if Pragma_Name (Prag) = Name_Postcondition then
11817 -- If expansion is disabled, as in a generic unit, save
11818 -- pragma for later expansion.
11820 if not Expander_Active then
11821 Prepend (Grab_PPC, Declarations (N));
11823 Append_Enabled_Item (Grab_PPC, Plist);
11829 -- Not a pragma, if comes from source, then end scan
11831 elsif Comes_From_Source (Prag) then
11834 -- Skip stuff not coming from source
11842 -- Now deal with any postconditions from the spec
11844 if Present (Spec_Id) then
11845 Spec_Postconditions : declare
11846 procedure Process_Contract_Cases (Spec : Node_Id);
11847 -- This processes the Contract_Test_Cases from Spec, processing
11848 -- any contract-cases from the list. The caller has checked that
11849 -- Contract_Test_Cases is non-Empty.
11851 procedure Process_Post_Conditions
11854 -- This processes the Pre_Post_Conditions from Spec, processing
11855 -- any postconditions from the list. If Class is True, then only
11856 -- postconditions marked with Class_Present are considered. The
11857 -- caller has checked that Pre_Post_Conditions is non-Empty.
11859 ----------------------------
11860 -- Process_Contract_Cases --
11861 ----------------------------
11863 procedure Process_Contract_Cases (Spec : Node_Id) is
11865 -- Loop through Contract_Cases pragmas from spec
11867 Prag := Contract_Test_Cases (Contract (Spec));
11869 if Pragma_Name (Prag) = Name_Contract_Cases then
11870 Expand_Contract_Cases
11872 Subp_Id => Spec_Id,
11873 Decls => Declarations (N),
11877 Prag := Next_Pragma (Prag);
11878 exit when No (Prag);
11880 end Process_Contract_Cases;
11882 -----------------------------
11883 -- Process_Post_Conditions --
11884 -----------------------------
11886 procedure Process_Post_Conditions
11899 -- Loop through PPC pragmas from spec
11901 Prag := Pre_Post_Conditions (Contract (Spec));
11903 if Pragma_Name (Prag) = Name_Postcondition
11904 and then (not Class or else Class_Present (Prag))
11906 if not Expander_Active then
11907 Prepend (Grab_PPC (Pspec), Declarations (N));
11909 Append_Enabled_Item (Grab_PPC (Pspec), Plist);
11913 Prag := Next_Pragma (Prag);
11914 exit when No (Prag);
11916 end Process_Post_Conditions;
11918 -- Start of processing for Spec_Postconditions
11921 -- Process postconditions expressed as contract-cases
11923 if Present (Contract_Test_Cases (Contract (Spec_Id))) then
11924 Process_Contract_Cases (Spec_Id);
11927 -- Process spec postconditions
11929 if Present (Pre_Post_Conditions (Contract (Spec_Id))) then
11930 Process_Post_Conditions (Spec_Id, Class => False);
11933 -- Process inherited postconditions
11935 for J in Inherited'Range loop
11936 if Present (Pre_Post_Conditions (Contract (Inherited (J)))) then
11937 Process_Post_Conditions (Inherited (J), Class => True);
11940 end Spec_Postconditions;
11943 -- Add an invariant call to check the result of a function
11945 if Ekind (Designator) /= E_Procedure and then Expander_Active then
11946 Func_Typ := Etype (Designator);
11947 Result := Make_Defining_Identifier (Loc, Name_uResult);
11949 Set_Etype (Result, Func_Typ);
11951 -- Add argument for return
11953 Parms := New_List (
11954 Make_Parameter_Specification (Loc,
11955 Defining_Identifier => Result,
11956 Parameter_Type => New_Occurrence_Of (Func_Typ, Loc)));
11958 -- Add invariant call if returning type with invariants and this is a
11959 -- public function, i.e. a function declared in the visible part of
11960 -- the package defining the private type.
11962 if Has_Invariants (Func_Typ)
11963 and then Present (Invariant_Procedure (Func_Typ))
11964 and then not Has_Null_Body (Invariant_Procedure (Func_Typ))
11965 and then Is_Public_Subprogram_For (Func_Typ)
11967 Append_Enabled_Item
11968 (Make_Invariant_Call (New_Occurrence_Of (Result, Loc)), Plist);
11971 -- Same if return value is an access to type with invariants
11973 Check_Access_Invariants (Result);
11981 -- Add invariant calls and predicate calls for parameters. Note that
11982 -- this is done for functions as well, since in Ada 2012 they can have
11985 if Expander_Active then
11986 Formal := First_Formal (Designator);
11987 while Present (Formal) loop
11988 if Ekind (Formal) /= E_In_Parameter
11989 or else Is_Access_Type (Etype (Formal))
11991 Formal_Typ := Etype (Formal);
11993 if Has_Invariants (Formal_Typ)
11994 and then Present (Invariant_Procedure (Formal_Typ))
11995 and then not Has_Null_Body (Invariant_Procedure (Formal_Typ))
11996 and then Is_Public_Subprogram_For (Formal_Typ)
11998 Append_Enabled_Item
11999 (Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
12003 Check_Access_Invariants (Formal);
12005 if Has_Predicates (Formal_Typ)
12006 and then Present (Predicate_Function (Formal_Typ))
12007 and then Has_Checked_Predicate (Formal_Typ)
12009 Append_Enabled_Item
12010 (Make_Predicate_Check
12011 (Formal_Typ, New_Occurrence_Of (Formal, Loc)),
12016 Next_Formal (Formal);
12020 -- Build and insert postcondition procedure
12022 if Expander_Active and then Present (Plist) then
12024 Make_Defining_Identifier (Loc, Chars => Name_uPostconditions);
12026 -- Insert the corresponding body of a post condition pragma after the
12027 -- last declaration of the context. This ensures that the body will
12028 -- not cause any premature freezing as it may mention types:
12030 -- procedure Proc (Obj : Array_Typ) is
12031 -- procedure _postconditions is
12034 -- end _postconditions;
12036 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12039 -- In the example above, Obj is of type T but the incorrect placement
12040 -- of _postconditions will cause a crash in gigi due to an out of
12041 -- order reference. The body of _postconditions must be placed after
12042 -- the declaration of Temp to preserve correct visibility.
12044 Insert_After_Last_Declaration (
12045 Make_Subprogram_Body (Loc,
12047 Make_Procedure_Specification (Loc,
12048 Defining_Unit_Name => Post_Proc,
12049 Parameter_Specifications => Parms),
12051 Declarations => Empty_List,
12053 Handled_Statement_Sequence =>
12054 Make_Handled_Sequence_Of_Statements (Loc,
12055 Statements => Plist)));
12057 Set_Ekind (Post_Proc, E_Procedure);
12059 -- If this is a procedure, set the Postcondition_Proc attribute on
12060 -- the proper defining entity for the subprogram.
12062 if Ekind (Designator) = E_Procedure then
12063 Set_Postcondition_Proc (Designator, Post_Proc);
12066 Set_Has_Postconditions (Designator);
12070 ----------------------------
12071 -- Reference_Body_Formals --
12072 ----------------------------
12074 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12079 if Error_Posted (Spec) then
12083 -- Iterate over both lists. They may be of different lengths if the two
12084 -- specs are not conformant.
12086 Fs := First_Formal (Spec);
12087 Fb := First_Formal (Bod);
12088 while Present (Fs) and then Present (Fb) loop
12089 Generate_Reference (Fs, Fb, 'b');
12091 if Style_Check then
12092 Style.Check_Identifier (Fb, Fs);
12095 Set_Spec_Entity (Fb, Fs);
12096 Set_Referenced (Fs, False);
12100 end Reference_Body_Formals;
12102 -------------------------
12103 -- Set_Actual_Subtypes --
12104 -------------------------
12106 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12108 Formal : Entity_Id;
12110 First_Stmt : Node_Id := Empty;
12111 AS_Needed : Boolean;
12114 -- If this is an empty initialization procedure, no need to create
12115 -- actual subtypes (small optimization).
12117 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12121 Formal := First_Formal (Subp);
12122 while Present (Formal) loop
12123 T := Etype (Formal);
12125 -- We never need an actual subtype for a constrained formal
12127 if Is_Constrained (T) then
12128 AS_Needed := False;
12130 -- If we have unknown discriminants, then we do not need an actual
12131 -- subtype, or more accurately we cannot figure it out! Note that
12132 -- all class-wide types have unknown discriminants.
12134 elsif Has_Unknown_Discriminants (T) then
12135 AS_Needed := False;
12137 -- At this stage we have an unconstrained type that may need an
12138 -- actual subtype. For sure the actual subtype is needed if we have
12139 -- an unconstrained array type.
12141 elsif Is_Array_Type (T) then
12144 -- The only other case needing an actual subtype is an unconstrained
12145 -- record type which is an IN parameter (we cannot generate actual
12146 -- subtypes for the OUT or IN OUT case, since an assignment can
12147 -- change the discriminant values. However we exclude the case of
12148 -- initialization procedures, since discriminants are handled very
12149 -- specially in this context, see the section entitled "Handling of
12150 -- Discriminants" in Einfo.
12152 -- We also exclude the case of Discrim_SO_Functions (functions used
12153 -- in front end layout mode for size/offset values), since in such
12154 -- functions only discriminants are referenced, and not only are such
12155 -- subtypes not needed, but they cannot always be generated, because
12156 -- of order of elaboration issues.
12158 elsif Is_Record_Type (T)
12159 and then Ekind (Formal) = E_In_Parameter
12160 and then Chars (Formal) /= Name_uInit
12161 and then not Is_Unchecked_Union (T)
12162 and then not Is_Discrim_SO_Function (Subp)
12166 -- All other cases do not need an actual subtype
12169 AS_Needed := False;
12172 -- Generate actual subtypes for unconstrained arrays and
12173 -- unconstrained discriminated records.
12176 if Nkind (N) = N_Accept_Statement then
12178 -- If expansion is active, the formal is replaced by a local
12179 -- variable that renames the corresponding entry of the
12180 -- parameter block, and it is this local variable that may
12181 -- require an actual subtype.
12183 if Full_Expander_Active then
12184 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12186 Decl := Build_Actual_Subtype (T, Formal);
12189 if Present (Handled_Statement_Sequence (N)) then
12191 First (Statements (Handled_Statement_Sequence (N)));
12192 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12193 Mark_Rewrite_Insertion (Decl);
12195 -- If the accept statement has no body, there will be no
12196 -- reference to the actuals, so no need to compute actual
12203 Decl := Build_Actual_Subtype (T, Formal);
12204 Prepend (Decl, Declarations (N));
12205 Mark_Rewrite_Insertion (Decl);
12208 -- The declaration uses the bounds of an existing object, and
12209 -- therefore needs no constraint checks.
12211 Analyze (Decl, Suppress => All_Checks);
12213 -- We need to freeze manually the generated type when it is
12214 -- inserted anywhere else than in a declarative part.
12216 if Present (First_Stmt) then
12217 Insert_List_Before_And_Analyze (First_Stmt,
12218 Freeze_Entity (Defining_Identifier (Decl), N));
12221 if Nkind (N) = N_Accept_Statement
12222 and then Full_Expander_Active
12224 Set_Actual_Subtype (Renamed_Object (Formal),
12225 Defining_Identifier (Decl));
12227 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12231 Next_Formal (Formal);
12233 end Set_Actual_Subtypes;
12235 ---------------------
12236 -- Set_Formal_Mode --
12237 ---------------------
12239 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12240 Spec : constant Node_Id := Parent (Formal_Id);
12243 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12244 -- since we ensure that corresponding actuals are always valid at the
12245 -- point of the call.
12247 if Out_Present (Spec) then
12248 if Ekind (Scope (Formal_Id)) = E_Function
12249 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
12251 -- [IN] OUT parameters allowed for functions in Ada 2012
12253 if Ada_Version >= Ada_2012 then
12255 -- Even in Ada 2012 operators can only have IN parameters
12257 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12258 Error_Msg_N ("operators can only have IN parameters", Spec);
12261 if In_Present (Spec) then
12262 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12264 Set_Ekind (Formal_Id, E_Out_Parameter);
12267 -- But not in earlier versions of Ada
12270 Error_Msg_N ("functions can only have IN parameters", Spec);
12271 Set_Ekind (Formal_Id, E_In_Parameter);
12274 elsif In_Present (Spec) then
12275 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12278 Set_Ekind (Formal_Id, E_Out_Parameter);
12279 Set_Never_Set_In_Source (Formal_Id, True);
12280 Set_Is_True_Constant (Formal_Id, False);
12281 Set_Current_Value (Formal_Id, Empty);
12285 Set_Ekind (Formal_Id, E_In_Parameter);
12288 -- Set Is_Known_Non_Null for access parameters since the language
12289 -- guarantees that access parameters are always non-null. We also set
12290 -- Can_Never_Be_Null, since there is no way to change the value.
12292 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12294 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12295 -- null; In Ada 2005, only if then null_exclusion is explicit.
12297 if Ada_Version < Ada_2005
12298 or else Can_Never_Be_Null (Etype (Formal_Id))
12300 Set_Is_Known_Non_Null (Formal_Id);
12301 Set_Can_Never_Be_Null (Formal_Id);
12304 -- Ada 2005 (AI-231): Null-exclusion access subtype
12306 elsif Is_Access_Type (Etype (Formal_Id))
12307 and then Can_Never_Be_Null (Etype (Formal_Id))
12309 Set_Is_Known_Non_Null (Formal_Id);
12311 -- We can also set Can_Never_Be_Null (thus preventing some junk
12312 -- access checks) for the case of an IN parameter, which cannot
12313 -- be changed, or for an IN OUT parameter, which can be changed but
12314 -- not to a null value. But for an OUT parameter, the initial value
12315 -- passed in can be null, so we can't set this flag in that case.
12317 if Ekind (Formal_Id) /= E_Out_Parameter then
12318 Set_Can_Never_Be_Null (Formal_Id);
12322 Set_Mechanism (Formal_Id, Default_Mechanism);
12323 Set_Formal_Validity (Formal_Id);
12324 end Set_Formal_Mode;
12326 -------------------------
12327 -- Set_Formal_Validity --
12328 -------------------------
12330 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12332 -- If no validity checking, then we cannot assume anything about the
12333 -- validity of parameters, since we do not know there is any checking
12334 -- of the validity on the call side.
12336 if not Validity_Checks_On then
12339 -- If validity checking for parameters is enabled, this means we are
12340 -- not supposed to make any assumptions about argument values.
12342 elsif Validity_Check_Parameters then
12345 -- If we are checking in parameters, we will assume that the caller is
12346 -- also checking parameters, so we can assume the parameter is valid.
12348 elsif Ekind (Formal_Id) = E_In_Parameter
12349 and then Validity_Check_In_Params
12351 Set_Is_Known_Valid (Formal_Id, True);
12353 -- Similar treatment for IN OUT parameters
12355 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12356 and then Validity_Check_In_Out_Params
12358 Set_Is_Known_Valid (Formal_Id, True);
12360 end Set_Formal_Validity;
12362 ------------------------
12363 -- Subtype_Conformant --
12364 ------------------------
12366 function Subtype_Conformant
12367 (New_Id : Entity_Id;
12368 Old_Id : Entity_Id;
12369 Skip_Controlling_Formals : Boolean := False) return Boolean
12373 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12374 Skip_Controlling_Formals => Skip_Controlling_Formals);
12376 end Subtype_Conformant;
12378 ---------------------
12379 -- Type_Conformant --
12380 ---------------------
12382 function Type_Conformant
12383 (New_Id : Entity_Id;
12384 Old_Id : Entity_Id;
12385 Skip_Controlling_Formals : Boolean := False) return Boolean
12389 May_Hide_Profile := False;
12392 (New_Id, Old_Id, Type_Conformant, False, Result,
12393 Skip_Controlling_Formals => Skip_Controlling_Formals);
12395 end Type_Conformant;
12397 -------------------------------
12398 -- Valid_Operator_Definition --
12399 -------------------------------
12401 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12404 Id : constant Name_Id := Chars (Designator);
12408 F := First_Formal (Designator);
12409 while Present (F) loop
12412 if Present (Default_Value (F)) then
12414 ("default values not allowed for operator parameters",
12421 -- Verify that user-defined operators have proper number of arguments
12422 -- First case of operators which can only be unary
12424 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12427 -- Case of operators which can be unary or binary
12429 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12430 N_OK := (N in 1 .. 2);
12432 -- All other operators can only be binary
12440 ("incorrect number of arguments for operator", Designator);
12444 and then Base_Type (Etype (Designator)) = Standard_Boolean
12445 and then not Is_Intrinsic_Subprogram (Designator)
12448 ("explicit definition of inequality not allowed", Designator);
12450 end Valid_Operator_Definition;