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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Dbug; use Exp_Dbug;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Layout; use Layout;
45 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
50 with Output; use Output;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch4; use Sem_Ch4;
59 with Sem_Ch5; use Sem_Ch5;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch10; use Sem_Ch10;
62 with Sem_Ch12; use Sem_Ch12;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Elim; use Sem_Elim;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Mech; use Sem_Mech;
70 with Sem_Prag; use Sem_Prag;
71 with Sem_Res; use Sem_Res;
72 with Sem_Util; use Sem_Util;
73 with Sem_Type; use Sem_Type;
74 with Sem_Warn; use Sem_Warn;
75 with Sinput; use Sinput;
76 with Stand; use Stand;
77 with Sinfo; use Sinfo;
78 with Sinfo.CN; use Sinfo.CN;
79 with Snames; use Snames;
80 with Stringt; use Stringt;
82 with Stylesw; use Stylesw;
83 with Targparm; use Targparm;
84 with Tbuild; use Tbuild;
85 with Uintp; use Uintp;
86 with Urealp; use Urealp;
87 with Validsw; use Validsw;
89 package body Sem_Ch6 is
91 May_Hide_Profile : Boolean := False;
92 -- This flag is used to indicate that two formals in two subprograms being
93 -- checked for conformance differ only in that one is an access parameter
94 -- while the other is of a general access type with the same designated
95 -- type. In this case, if the rest of the signatures match, a call to
96 -- either subprogram may be ambiguous, which is worth a warning. The flag
97 -- is set in Compatible_Types, and the warning emitted in
98 -- New_Overloaded_Entity.
100 -----------------------
101 -- Local Subprograms --
102 -----------------------
104 procedure Analyze_Null_Procedure
106 Is_Completion : out Boolean);
107 -- A null procedure can be a declaration or (Ada 2012) a completion.
109 procedure Analyze_Return_Statement (N : Node_Id);
110 -- Common processing for simple and extended return statements
112 procedure Analyze_Function_Return (N : Node_Id);
113 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
114 -- applies to a [generic] function.
116 procedure Analyze_Return_Type (N : Node_Id);
117 -- Subsidiary to Process_Formals: analyze subtype mark in function
118 -- specification in a context where the formals are visible and hide
121 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
122 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
123 -- that we can use RETURN but not skip the debug output at the end.
125 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
126 -- Analyze a generic subprogram body. N is the body to be analyzed, and
127 -- Gen_Id is the defining entity Id for the corresponding spec.
129 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
130 -- If a subprogram has pragma Inline and inlining is active, use generic
131 -- machinery to build an unexpanded body for the subprogram. This body is
132 -- subsequently used for inline expansions at call sites. If subprogram can
133 -- be inlined (depending on size and nature of local declarations) this
134 -- function returns true. Otherwise subprogram body is treated normally.
135 -- If proper warnings are enabled and the subprogram contains a construct
136 -- that cannot be inlined, the offending construct is flagged accordingly.
138 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
139 -- Returns true if Subp can override a predefined operator.
141 procedure Check_And_Build_Body_To_Inline
144 Body_Id : Entity_Id);
145 -- Spec_Id and Body_Id are the entities of the specification and body of
146 -- the subprogram body N. If N can be inlined by the frontend (supported
147 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
148 -- associated with N and attach it to the declaration node of Spec_Id.
150 procedure Check_Conformance
153 Ctype : Conformance_Type;
155 Conforms : out Boolean;
156 Err_Loc : Node_Id := Empty;
157 Get_Inst : Boolean := False;
158 Skip_Controlling_Formals : Boolean := False);
159 -- Given two entities, this procedure checks that the profiles associated
160 -- with these entities meet the conformance criterion given by the third
161 -- parameter. If they conform, Conforms is set True and control returns
162 -- to the caller. If they do not conform, Conforms is set to False, and
163 -- in addition, if Errmsg is True on the call, proper messages are output
164 -- to complain about the conformance failure. If Err_Loc is non_Empty
165 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
166 -- error messages are placed on the appropriate part of the construct
167 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
168 -- against a formal access-to-subprogram type so Get_Instance_Of must
171 procedure Check_Subprogram_Order (N : Node_Id);
172 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
173 -- the alpha ordering rule for N if this ordering requirement applicable.
175 procedure Check_Returns
179 Proc : Entity_Id := Empty);
180 -- Called to check for missing return statements in a function body, or for
181 -- returns present in a procedure body which has No_Return set. HSS is the
182 -- handled statement sequence for the subprogram body. This procedure
183 -- checks all flow paths to make sure they either have return (Mode = 'F',
184 -- used for functions) or do not have a return (Mode = 'P', used for
185 -- No_Return procedures). The flag Err is set if there are any control
186 -- paths not explicitly terminated by a return in the function case, and is
187 -- True otherwise. Proc is the entity for the procedure case and is used
188 -- in posting the warning message.
190 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
191 -- In Ada 2012, a primitive equality operator on an untagged record type
192 -- must appear before the type is frozen, and have the same visibility as
193 -- that of the type. This procedure checks that this rule is met, and
194 -- otherwise emits an error on the subprogram declaration and a warning
195 -- on the earlier freeze point if it is easy to locate.
197 procedure Enter_Overloaded_Entity (S : Entity_Id);
198 -- This procedure makes S, a new overloaded entity, into the first visible
199 -- entity with that name.
201 function Is_Non_Overriding_Operation
203 New_E : Entity_Id) return Boolean;
204 -- Enforce the rule given in 12.3(18): a private operation in an instance
205 -- overrides an inherited operation only if the corresponding operation
206 -- was overriding in the generic. This needs to be checked for primitive
207 -- operations of types derived (in the generic unit) from formal private
208 -- or formal derived types.
210 procedure Make_Inequality_Operator (S : Entity_Id);
211 -- Create the declaration for an inequality operator that is implicitly
212 -- created by a user-defined equality operator that yields a boolean.
214 procedure May_Need_Actuals (Fun : Entity_Id);
215 -- Flag functions that can be called without parameters, i.e. those that
216 -- have no parameters, or those for which defaults exist for all parameters
218 procedure Process_PPCs
221 Body_Id : Entity_Id);
222 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
223 -- conditions for the body and assembling and inserting the _postconditions
224 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
225 -- the entities for the body and separate spec (if there is no separate
226 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
227 -- provide postconditions, and are also handled in this procedure.
229 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
230 -- Formal_Id is an formal parameter entity. This procedure deals with
231 -- setting the proper validity status for this entity, which depends on
232 -- the kind of parameter and the validity checking mode.
234 ---------------------------------------------
235 -- Analyze_Abstract_Subprogram_Declaration --
236 ---------------------------------------------
238 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
239 Designator : constant Entity_Id :=
240 Analyze_Subprogram_Specification (Specification (N));
241 Scop : constant Entity_Id := Current_Scope;
244 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
246 Generate_Definition (Designator);
247 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
248 Set_Is_Abstract_Subprogram (Designator);
249 New_Overloaded_Entity (Designator);
250 Check_Delayed_Subprogram (Designator);
252 Set_Categorization_From_Scope (Designator, Scop);
254 if Ekind (Scope (Designator)) = E_Protected_Type then
256 ("abstract subprogram not allowed in protected type", N);
258 -- Issue a warning if the abstract subprogram is neither a dispatching
259 -- operation nor an operation that overrides an inherited subprogram or
260 -- predefined operator, since this most likely indicates a mistake.
262 elsif Warn_On_Redundant_Constructs
263 and then not Is_Dispatching_Operation (Designator)
264 and then not Present (Overridden_Operation (Designator))
265 and then (not Is_Operator_Symbol_Name (Chars (Designator))
266 or else Scop /= Scope (Etype (First_Formal (Designator))))
269 ("abstract subprogram is not dispatching or overriding?r?", N);
272 Generate_Reference_To_Formals (Designator);
273 Check_Eliminated (Designator);
275 if Has_Aspects (N) then
276 Analyze_Aspect_Specifications (N, Designator);
278 end Analyze_Abstract_Subprogram_Declaration;
280 ---------------------------------
281 -- Analyze_Expression_Function --
282 ---------------------------------
284 procedure Analyze_Expression_Function (N : Node_Id) is
285 Loc : constant Source_Ptr := Sloc (N);
286 LocX : constant Source_Ptr := Sloc (Expression (N));
287 Expr : constant Node_Id := Expression (N);
288 Spec : constant Node_Id := Specification (N);
293 -- If the expression is a completion, Prev is the entity whose
294 -- declaration is completed. Def_Id is needed to analyze the spec.
302 -- This is one of the occasions on which we transform the tree during
303 -- semantic analysis. If this is a completion, transform the expression
304 -- function into an equivalent subprogram body, and analyze it.
306 -- Expression functions are inlined unconditionally. The back-end will
307 -- determine whether this is possible.
309 Inline_Processing_Required := True;
311 -- Create a specification for the generated body. Types and defauts in
312 -- the profile are copies of the spec, but new entities must be created
313 -- for the unit name and the formals.
315 New_Spec := New_Copy_Tree (Spec);
316 Set_Defining_Unit_Name (New_Spec,
317 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
318 Chars (Defining_Unit_Name (Spec))));
320 if Present (Parameter_Specifications (New_Spec)) then
322 Formal_Spec : Node_Id;
324 Formal_Spec := First (Parameter_Specifications (New_Spec));
325 while Present (Formal_Spec) loop
326 Set_Defining_Identifier
328 Make_Defining_Identifier (Sloc (Formal_Spec),
329 Chars => Chars (Defining_Identifier (Formal_Spec))));
335 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
337 -- If there are previous overloadable entities with the same name,
338 -- check whether any of them is completed by the expression function.
340 if Present (Prev) and then Is_Overloadable (Prev) then
341 Def_Id := Analyze_Subprogram_Specification (Spec);
342 Prev := Find_Corresponding_Spec (N);
345 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
348 Make_Subprogram_Body (Loc,
349 Specification => New_Spec,
350 Declarations => Empty_List,
351 Handled_Statement_Sequence =>
352 Make_Handled_Sequence_Of_Statements (LocX,
353 Statements => New_List (Ret)));
355 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
357 -- If the expression completes a generic subprogram, we must create a
358 -- separate node for the body, because at instantiation the original
359 -- node of the generic copy must be a generic subprogram body, and
360 -- cannot be a expression function. Otherwise we just rewrite the
361 -- expression with the non-generic body.
363 Insert_After (N, New_Body);
364 Rewrite (N, Make_Null_Statement (Loc));
365 Set_Has_Completion (Prev, False);
368 Set_Is_Inlined (Prev);
370 elsif Present (Prev) and then Comes_From_Source (Prev) then
371 Set_Has_Completion (Prev, False);
373 -- For navigation purposes, indicate that the function is a body
375 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
376 Rewrite (N, New_Body);
379 -- Prev is the previous entity with the same name, but it is can
380 -- be an unrelated spec that is not completed by the expression
381 -- function. In that case the relevant entity is the one in the body.
382 -- Not clear that the backend can inline it in this case ???
384 if Has_Completion (Prev) then
385 Set_Is_Inlined (Prev);
387 -- The formals of the expression function are body formals,
388 -- and do not appear in the ali file, which will only contain
389 -- references to the formals of the original subprogram spec.
396 F1 := First_Formal (Def_Id);
397 F2 := First_Formal (Prev);
399 while Present (F1) loop
400 Set_Spec_Entity (F1, F2);
407 Set_Is_Inlined (Defining_Entity (New_Body));
410 -- If this is not a completion, create both a declaration and a body, so
411 -- that the expression can be inlined whenever possible.
414 -- An expression function that is not a completion is not a
415 -- subprogram declaration, and thus cannot appear in a protected
418 if Nkind (Parent (N)) = N_Protected_Definition then
420 ("an expression function is not a legal protected operation", N);
424 Make_Subprogram_Declaration (Loc, Specification => Spec);
426 Rewrite (N, New_Decl);
428 Set_Is_Inlined (Defining_Entity (New_Decl));
430 -- To prevent premature freeze action, insert the new body at the end
431 -- of the current declarations, or at the end of the package spec.
432 -- However, resolve usage names now, to prevent spurious visibility
433 -- on later entities.
436 Decls : List_Id := List_Containing (N);
437 Par : constant Node_Id := Parent (Decls);
438 Id : constant Entity_Id := Defining_Entity (New_Decl);
441 if Nkind (Par) = N_Package_Specification
442 and then Decls = Visible_Declarations (Par)
443 and then Present (Private_Declarations (Par))
444 and then not Is_Empty_List (Private_Declarations (Par))
446 Decls := Private_Declarations (Par);
449 Insert_After (Last (Decls), New_Body);
451 Install_Formals (Id);
453 -- Do a preanalysis of the expression on a separate copy, to
454 -- prevent visibility issues later with operators in instances.
455 -- Attach copy to tree so that parent links are available.
458 Expr : constant Node_Id := New_Copy_Tree (Expression (Ret));
460 Set_Parent (Expr, Ret);
461 Preanalyze_Spec_Expression (Expr, Etype (Id));
468 -- If the return expression is a static constant, we suppress warning
469 -- messages on unused formals, which in most cases will be noise.
471 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
472 Is_OK_Static_Expression (Expr));
473 end Analyze_Expression_Function;
475 ----------------------------------------
476 -- Analyze_Extended_Return_Statement --
477 ----------------------------------------
479 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
481 Analyze_Return_Statement (N);
482 end Analyze_Extended_Return_Statement;
484 ----------------------------
485 -- Analyze_Function_Call --
486 ----------------------------
488 procedure Analyze_Function_Call (N : Node_Id) is
489 Actuals : constant List_Id := Parameter_Associations (N);
490 Func_Nam : constant Node_Id := Name (N);
496 -- A call of the form A.B (X) may be an Ada 2005 call, which is
497 -- rewritten as B (A, X). If the rewriting is successful, the call
498 -- has been analyzed and we just return.
500 if Nkind (Func_Nam) = N_Selected_Component
501 and then Name (N) /= Func_Nam
502 and then Is_Rewrite_Substitution (N)
503 and then Present (Etype (N))
508 -- If error analyzing name, then set Any_Type as result type and return
510 if Etype (Func_Nam) = Any_Type then
511 Set_Etype (N, Any_Type);
515 -- Otherwise analyze the parameters
517 if Present (Actuals) then
518 Actual := First (Actuals);
519 while Present (Actual) loop
521 Check_Parameterless_Call (Actual);
527 end Analyze_Function_Call;
529 -----------------------------
530 -- Analyze_Function_Return --
531 -----------------------------
533 procedure Analyze_Function_Return (N : Node_Id) is
534 Loc : constant Source_Ptr := Sloc (N);
535 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
536 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
538 R_Type : constant Entity_Id := Etype (Scope_Id);
539 -- Function result subtype
541 procedure Check_Limited_Return (Expr : Node_Id);
542 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
543 -- limited types. Used only for simple return statements.
544 -- Expr is the expression returned.
546 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
547 -- Check that the return_subtype_indication properly matches the result
548 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
550 --------------------------
551 -- Check_Limited_Return --
552 --------------------------
554 procedure Check_Limited_Return (Expr : Node_Id) is
556 -- Ada 2005 (AI-318-02): Return-by-reference types have been
557 -- removed and replaced by anonymous access results. This is an
558 -- incompatibility with Ada 95. Not clear whether this should be
559 -- enforced yet or perhaps controllable with special switch. ???
561 -- A limited interface that is not immutably limited is OK.
563 if Is_Limited_Interface (R_Type)
565 not (Is_Task_Interface (R_Type)
566 or else Is_Protected_Interface (R_Type)
567 or else Is_Synchronized_Interface (R_Type))
571 elsif Is_Limited_Type (R_Type)
572 and then not Is_Interface (R_Type)
573 and then Comes_From_Source (N)
574 and then not In_Instance_Body
575 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
579 if Ada_Version >= Ada_2005
580 and then not Debug_Flag_Dot_L
581 and then not GNAT_Mode
584 ("(Ada 2005) cannot copy object of a limited type " &
585 "(RM-2005 6.5(5.5/2))", Expr);
587 if Is_Immutably_Limited_Type (R_Type) then
589 ("\return by reference not permitted in Ada 2005", Expr);
592 -- Warn in Ada 95 mode, to give folks a heads up about this
595 -- In GNAT mode, this is just a warning, to allow it to be
596 -- evilly turned off. Otherwise it is a real error.
598 -- In a generic context, simplify the warning because it makes
599 -- no sense to discuss pass-by-reference or copy.
601 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
602 if Inside_A_Generic then
604 ("return of limited object not permitted in Ada 2005 "
605 & "(RM-2005 6.5(5.5/2))?y?", Expr);
607 elsif Is_Immutably_Limited_Type (R_Type) then
609 ("return by reference not permitted in Ada 2005 "
610 & "(RM-2005 6.5(5.5/2))?y?", Expr);
613 ("cannot copy object of a limited type in Ada 2005 "
614 & "(RM-2005 6.5(5.5/2))?y?", Expr);
617 -- Ada 95 mode, compatibility warnings disabled
620 return; -- skip continuation messages below
623 if not Inside_A_Generic then
625 ("\consider switching to return of access type", Expr);
626 Explain_Limited_Type (R_Type, Expr);
629 end Check_Limited_Return;
631 -------------------------------------
632 -- Check_Return_Subtype_Indication --
633 -------------------------------------
635 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
636 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
638 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
639 -- Subtype given in the extended return statement (must match R_Type)
641 Subtype_Ind : constant Node_Id :=
642 Object_Definition (Original_Node (Obj_Decl));
644 R_Type_Is_Anon_Access :
646 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
648 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
650 Ekind (R_Type) = E_Anonymous_Access_Type;
651 -- True if return type of the function is an anonymous access type
652 -- Can't we make Is_Anonymous_Access_Type in einfo ???
654 R_Stm_Type_Is_Anon_Access :
656 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
658 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
660 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
661 -- True if type of the return object is an anonymous access type
664 -- First, avoid cascaded errors
666 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
670 -- "return access T" case; check that the return statement also has
671 -- "access T", and that the subtypes statically match:
672 -- if this is an access to subprogram the signatures must match.
674 if R_Type_Is_Anon_Access then
675 if R_Stm_Type_Is_Anon_Access then
677 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
679 if Base_Type (Designated_Type (R_Stm_Type)) /=
680 Base_Type (Designated_Type (R_Type))
681 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
684 ("subtype must statically match function result subtype",
685 Subtype_Mark (Subtype_Ind));
689 -- For two anonymous access to subprogram types, the
690 -- types themselves must be type conformant.
692 if not Conforming_Types
693 (R_Stm_Type, R_Type, Fully_Conformant)
696 ("subtype must statically match function result subtype",
702 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
705 -- If the return object is of an anonymous access type, then report
706 -- an error if the function's result type is not also anonymous.
708 elsif R_Stm_Type_Is_Anon_Access
709 and then not R_Type_Is_Anon_Access
711 Error_Msg_N ("anonymous access not allowed for function with " &
712 "named access result", Subtype_Ind);
714 -- Subtype indication case: check that the return object's type is
715 -- covered by the result type, and that the subtypes statically match
716 -- when the result subtype is constrained. Also handle record types
717 -- with unknown discriminants for which we have built the underlying
718 -- record view. Coverage is needed to allow specific-type return
719 -- objects when the result type is class-wide (see AI05-32).
721 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
722 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
726 Underlying_Record_View (Base_Type (R_Stm_Type))))
728 -- A null exclusion may be present on the return type, on the
729 -- function specification, on the object declaration or on the
732 if Is_Access_Type (R_Type)
734 (Can_Never_Be_Null (R_Type)
735 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
736 Can_Never_Be_Null (R_Stm_Type)
739 ("subtype must statically match function result subtype",
743 -- AI05-103: for elementary types, subtypes must statically match
745 if Is_Constrained (R_Type)
746 or else Is_Access_Type (R_Type)
748 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
750 ("subtype must statically match function result subtype",
755 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
756 and then Is_Null_Extension (Base_Type (R_Type))
762 ("wrong type for return_subtype_indication", Subtype_Ind);
764 end Check_Return_Subtype_Indication;
766 ---------------------
767 -- Local Variables --
768 ---------------------
772 -- Start of processing for Analyze_Function_Return
775 Set_Return_Present (Scope_Id);
777 if Nkind (N) = N_Simple_Return_Statement then
778 Expr := Expression (N);
780 -- Guard against a malformed expression. The parser may have tried to
781 -- recover but the node is not analyzable.
783 if Nkind (Expr) = N_Error then
784 Set_Etype (Expr, Any_Type);
785 Expander_Mode_Save_And_Set (False);
789 -- The resolution of a controlled [extension] aggregate associated
790 -- with a return statement creates a temporary which needs to be
791 -- finalized on function exit. Wrap the return statement inside a
792 -- block so that the finalization machinery can detect this case.
793 -- This early expansion is done only when the return statement is
794 -- not part of a handled sequence of statements.
796 if Nkind_In (Expr, N_Aggregate,
797 N_Extension_Aggregate)
798 and then Needs_Finalization (R_Type)
799 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
802 Make_Block_Statement (Loc,
803 Handled_Statement_Sequence =>
804 Make_Handled_Sequence_Of_Statements (Loc,
805 Statements => New_List (Relocate_Node (N)))));
811 Analyze_And_Resolve (Expr, R_Type);
812 Check_Limited_Return (Expr);
815 -- RETURN only allowed in SPARK as the last statement in function
817 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
819 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
820 or else Present (Next (N)))
822 Check_SPARK_Restriction
823 ("RETURN should be the last statement in function", N);
827 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
829 -- Analyze parts specific to extended_return_statement:
832 Obj_Decl : constant Node_Id :=
833 Last (Return_Object_Declarations (N));
834 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
835 HSS : constant Node_Id := Handled_Statement_Sequence (N);
838 Expr := Expression (Obj_Decl);
840 -- Note: The check for OK_For_Limited_Init will happen in
841 -- Analyze_Object_Declaration; we treat it as a normal
842 -- object declaration.
844 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
847 Check_Return_Subtype_Indication (Obj_Decl);
849 if Present (HSS) then
852 if Present (Exception_Handlers (HSS)) then
854 -- ???Has_Nested_Block_With_Handler needs to be set.
855 -- Probably by creating an actual N_Block_Statement.
856 -- Probably in Expand.
862 -- Mark the return object as referenced, since the return is an
863 -- implicit reference of the object.
865 Set_Referenced (Defining_Identifier (Obj_Decl));
867 Check_References (Stm_Entity);
869 -- Check RM 6.5 (5.9/3)
872 if Ada_Version < Ada_2012 then
874 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
875 -- Can it really happen (extended return???)
878 ("aliased only allowed for limited"
879 & " return objects in Ada 2012?", N);
881 elsif not Is_Immutably_Limited_Type (R_Type) then
882 Error_Msg_N ("aliased only allowed for limited"
883 & " return objects", N);
889 -- Case of Expr present
893 -- Defend against previous errors
895 and then Nkind (Expr) /= N_Empty
896 and then Present (Etype (Expr))
898 -- Apply constraint check. Note that this is done before the implicit
899 -- conversion of the expression done for anonymous access types to
900 -- ensure correct generation of the null-excluding check associated
901 -- with null-excluding expressions found in return statements.
903 Apply_Constraint_Check (Expr, R_Type);
905 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
906 -- type, apply an implicit conversion of the expression to that type
907 -- to force appropriate static and run-time accessibility checks.
909 if Ada_Version >= Ada_2005
910 and then Ekind (R_Type) = E_Anonymous_Access_Type
912 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
913 Analyze_And_Resolve (Expr, R_Type);
915 -- If this is a local anonymous access to subprogram, the
916 -- accessibility check can be applied statically. The return is
917 -- illegal if the access type of the return expression is declared
918 -- inside of the subprogram (except if it is the subtype indication
919 -- of an extended return statement).
921 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
922 if not Comes_From_Source (Current_Scope)
923 or else Ekind (Current_Scope) = E_Return_Statement
928 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
930 Error_Msg_N ("cannot return local access to subprogram", N);
934 -- If the result type is class-wide, then check that the return
935 -- expression's type is not declared at a deeper level than the
936 -- function (RM05-6.5(5.6/2)).
938 if Ada_Version >= Ada_2005
939 and then Is_Class_Wide_Type (R_Type)
941 if Type_Access_Level (Etype (Expr)) >
942 Subprogram_Access_Level (Scope_Id)
945 ("level of return expression type is deeper than " &
946 "class-wide function!", Expr);
950 -- Check incorrect use of dynamically tagged expression
952 if Is_Tagged_Type (R_Type) then
953 Check_Dynamically_Tagged_Expression
959 -- ??? A real run-time accessibility check is needed in cases
960 -- involving dereferences of access parameters. For now we just
961 -- check the static cases.
963 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
964 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
965 and then Object_Access_Level (Expr) >
966 Subprogram_Access_Level (Scope_Id)
968 -- Suppress the message in a generic, where the rewriting
971 if Inside_A_Generic then
976 Make_Raise_Program_Error (Loc,
977 Reason => PE_Accessibility_Check_Failed));
981 ("cannot return a local value by reference??", N);
983 ("\& will be raised at run time??",
984 N, Standard_Program_Error);
989 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
990 and then Null_Exclusion_Present (Parent (Scope_Id))
992 Apply_Compile_Time_Constraint_Error
994 Msg => "(Ada 2005) null not allowed for "
995 & "null-excluding return??",
996 Reason => CE_Null_Not_Allowed);
999 end Analyze_Function_Return;
1001 -------------------------------------
1002 -- Analyze_Generic_Subprogram_Body --
1003 -------------------------------------
1005 procedure Analyze_Generic_Subprogram_Body
1009 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1010 Kind : constant Entity_Kind := Ekind (Gen_Id);
1011 Body_Id : Entity_Id;
1016 -- Copy body and disable expansion while analyzing the generic For a
1017 -- stub, do not copy the stub (which would load the proper body), this
1018 -- will be done when the proper body is analyzed.
1020 if Nkind (N) /= N_Subprogram_Body_Stub then
1021 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1026 Spec := Specification (N);
1028 -- Within the body of the generic, the subprogram is callable, and
1029 -- behaves like the corresponding non-generic unit.
1031 Body_Id := Defining_Entity (Spec);
1033 if Kind = E_Generic_Procedure
1034 and then Nkind (Spec) /= N_Procedure_Specification
1036 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1039 elsif Kind = E_Generic_Function
1040 and then Nkind (Spec) /= N_Function_Specification
1042 Error_Msg_N ("invalid body for generic function ", Body_Id);
1046 Set_Corresponding_Body (Gen_Decl, Body_Id);
1048 if Has_Completion (Gen_Id)
1049 and then Nkind (Parent (N)) /= N_Subunit
1051 Error_Msg_N ("duplicate generic body", N);
1054 Set_Has_Completion (Gen_Id);
1057 if Nkind (N) = N_Subprogram_Body_Stub then
1058 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1060 Set_Corresponding_Spec (N, Gen_Id);
1063 if Nkind (Parent (N)) = N_Compilation_Unit then
1064 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1067 -- Make generic parameters immediately visible in the body. They are
1068 -- needed to process the formals declarations. Then make the formals
1069 -- visible in a separate step.
1071 Push_Scope (Gen_Id);
1075 First_Ent : Entity_Id;
1078 First_Ent := First_Entity (Gen_Id);
1081 while Present (E) and then not Is_Formal (E) loop
1086 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1088 -- Now generic formals are visible, and the specification can be
1089 -- analyzed, for subsequent conformance check.
1091 Body_Id := Analyze_Subprogram_Specification (Spec);
1093 -- Make formal parameters visible
1097 -- E is the first formal parameter, we loop through the formals
1098 -- installing them so that they will be visible.
1100 Set_First_Entity (Gen_Id, E);
1101 while Present (E) loop
1107 -- Visible generic entity is callable within its own body
1109 Set_Ekind (Gen_Id, Ekind (Body_Id));
1110 Set_Contract (Body_Id, Empty);
1111 Set_Ekind (Body_Id, E_Subprogram_Body);
1112 Set_Convention (Body_Id, Convention (Gen_Id));
1113 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1114 Set_Scope (Body_Id, Scope (Gen_Id));
1115 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1117 if Nkind (N) = N_Subprogram_Body_Stub then
1119 -- No body to analyze, so restore state of generic unit
1121 Set_Ekind (Gen_Id, Kind);
1122 Set_Ekind (Body_Id, Kind);
1124 if Present (First_Ent) then
1125 Set_First_Entity (Gen_Id, First_Ent);
1132 -- If this is a compilation unit, it must be made visible explicitly,
1133 -- because the compilation of the declaration, unlike other library
1134 -- unit declarations, does not. If it is not a unit, the following
1135 -- is redundant but harmless.
1137 Set_Is_Immediately_Visible (Gen_Id);
1138 Reference_Body_Formals (Gen_Id, Body_Id);
1140 if Is_Child_Unit (Gen_Id) then
1141 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1144 Set_Actual_Subtypes (N, Current_Scope);
1146 -- Deal with preconditions and postconditions. In formal verification
1147 -- mode, we keep pre- and postconditions attached to entities rather
1148 -- than inserted in the code, in order to facilitate a distinct
1149 -- treatment for them.
1151 if not SPARK_Mode then
1152 Process_PPCs (N, Gen_Id, Body_Id);
1155 -- If the generic unit carries pre- or post-conditions, copy them
1156 -- to the original generic tree, so that they are properly added
1157 -- to any instantiation.
1160 Orig : constant Node_Id := Original_Node (N);
1164 Cond := First (Declarations (N));
1165 while Present (Cond) loop
1166 if Nkind (Cond) = N_Pragma
1167 and then Pragma_Name (Cond) = Name_Check
1169 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1171 elsif Nkind (Cond) = N_Pragma
1172 and then Pragma_Name (Cond) = Name_Postcondition
1174 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1175 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1184 Analyze_Declarations (Declarations (N));
1186 Analyze (Handled_Statement_Sequence (N));
1188 Save_Global_References (Original_Node (N));
1190 -- Prior to exiting the scope, include generic formals again (if any
1191 -- are present) in the set of local entities.
1193 if Present (First_Ent) then
1194 Set_First_Entity (Gen_Id, First_Ent);
1197 Check_References (Gen_Id);
1200 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1202 Check_Subprogram_Order (N);
1204 -- Outside of its body, unit is generic again
1206 Set_Ekind (Gen_Id, Kind);
1207 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1210 Style.Check_Identifier (Body_Id, Gen_Id);
1214 end Analyze_Generic_Subprogram_Body;
1216 ----------------------------
1217 -- Analyze_Null_Procedure --
1218 ----------------------------
1220 procedure Analyze_Null_Procedure
1222 Is_Completion : out Boolean)
1224 Loc : constant Source_Ptr := Sloc (N);
1225 Spec : constant Node_Id := Specification (N);
1226 Designator : Entity_Id;
1228 Null_Body : Node_Id := Empty;
1232 -- Capture the profile of the null procedure before analysis, for
1233 -- expansion at the freeze point and at each point of call. The body is
1234 -- used if the procedure has preconditions, or if it is a completion. In
1235 -- the first case the body is analyzed at the freeze point, in the other
1236 -- it replaces the null procedure declaration.
1239 Make_Subprogram_Body (Loc,
1240 Specification => New_Copy_Tree (Spec),
1241 Declarations => New_List,
1242 Handled_Statement_Sequence =>
1243 Make_Handled_Sequence_Of_Statements (Loc,
1244 Statements => New_List (Make_Null_Statement (Loc))));
1246 -- Create new entities for body and formals
1248 Set_Defining_Unit_Name (Specification (Null_Body),
1249 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1251 Form := First (Parameter_Specifications (Specification (Null_Body)));
1252 while Present (Form) loop
1253 Set_Defining_Identifier (Form,
1254 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1258 -- Determine whether the null procedure may be a completion of a generic
1259 -- suprogram, in which case we use the new null body as the completion
1260 -- and set minimal semantic information on the original declaration,
1261 -- which is rewritten as a null statement.
1263 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1265 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1266 Insert_Before (N, Null_Body);
1267 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1268 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1270 Rewrite (N, Make_Null_Statement (Loc));
1271 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1272 Is_Completion := True;
1277 -- Resolve the types of the formals now, because the freeze point
1278 -- may appear in a different context, e.g. an instantiation.
1280 Form := First (Parameter_Specifications (Specification (Null_Body)));
1281 while Present (Form) loop
1282 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1283 Find_Type (Parameter_Type (Form));
1286 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1288 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1291 -- The case of a null procedure with a formal that is an
1292 -- access_to_subprogram type, and that is used as an actual
1293 -- in an instantiation is left to the enthusiastic reader.
1302 -- If there are previous overloadable entities with the same name,
1303 -- check whether any of them is completed by the null procedure.
1305 if Present (Prev) and then Is_Overloadable (Prev) then
1306 Designator := Analyze_Subprogram_Specification (Spec);
1307 Prev := Find_Corresponding_Spec (N);
1310 if No (Prev) or else not Comes_From_Source (Prev) then
1311 Designator := Analyze_Subprogram_Specification (Spec);
1312 Set_Has_Completion (Designator);
1314 -- Signal to caller that this is a procedure declaration
1316 Is_Completion := False;
1318 -- Null procedures are always inlined, but generic formal subprograms
1319 -- which appear as such in the internal instance of formal packages,
1320 -- need no completion and are not marked Inline.
1323 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1325 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1326 Set_Body_To_Inline (N, Null_Body);
1327 Set_Is_Inlined (Designator);
1331 -- The null procedure is a completion
1333 Is_Completion := True;
1335 if Expander_Active then
1336 Rewrite (N, Null_Body);
1340 Designator := Analyze_Subprogram_Specification (Spec);
1341 Set_Has_Completion (Designator);
1342 Set_Has_Completion (Prev);
1345 end Analyze_Null_Procedure;
1347 -----------------------------
1348 -- Analyze_Operator_Symbol --
1349 -----------------------------
1351 -- An operator symbol such as "+" or "and" may appear in context where the
1352 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1353 -- is just a string, as in (conjunction = "or"). In these cases the parser
1354 -- generates this node, and the semantics does the disambiguation. Other
1355 -- such case are actuals in an instantiation, the generic unit in an
1356 -- instantiation, and pragma arguments.
1358 procedure Analyze_Operator_Symbol (N : Node_Id) is
1359 Par : constant Node_Id := Parent (N);
1362 if (Nkind (Par) = N_Function_Call
1363 and then N = Name (Par))
1364 or else Nkind (Par) = N_Function_Instantiation
1365 or else (Nkind (Par) = N_Indexed_Component
1366 and then N = Prefix (Par))
1367 or else (Nkind (Par) = N_Pragma_Argument_Association
1368 and then not Is_Pragma_String_Literal (Par))
1369 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1370 or else (Nkind (Par) = N_Attribute_Reference
1371 and then Attribute_Name (Par) /= Name_Value)
1373 Find_Direct_Name (N);
1376 Change_Operator_Symbol_To_String_Literal (N);
1379 end Analyze_Operator_Symbol;
1381 -----------------------------------
1382 -- Analyze_Parameter_Association --
1383 -----------------------------------
1385 procedure Analyze_Parameter_Association (N : Node_Id) is
1387 Analyze (Explicit_Actual_Parameter (N));
1388 end Analyze_Parameter_Association;
1390 ----------------------------
1391 -- Analyze_Procedure_Call --
1392 ----------------------------
1394 procedure Analyze_Procedure_Call (N : Node_Id) is
1395 Loc : constant Source_Ptr := Sloc (N);
1396 P : constant Node_Id := Name (N);
1397 Actuals : constant List_Id := Parameter_Associations (N);
1401 procedure Analyze_Call_And_Resolve;
1402 -- Do Analyze and Resolve calls for procedure call
1403 -- At end, check illegal order dependence.
1405 ------------------------------
1406 -- Analyze_Call_And_Resolve --
1407 ------------------------------
1409 procedure Analyze_Call_And_Resolve is
1411 if Nkind (N) = N_Procedure_Call_Statement then
1413 Resolve (N, Standard_Void_Type);
1417 end Analyze_Call_And_Resolve;
1419 -- Start of processing for Analyze_Procedure_Call
1422 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1423 -- a procedure call or an entry call. The prefix may denote an access
1424 -- to subprogram type, in which case an implicit dereference applies.
1425 -- If the prefix is an indexed component (without implicit dereference)
1426 -- then the construct denotes a call to a member of an entire family.
1427 -- If the prefix is a simple name, it may still denote a call to a
1428 -- parameterless member of an entry family. Resolution of these various
1429 -- interpretations is delicate.
1433 -- If this is a call of the form Obj.Op, the call may have been
1434 -- analyzed and possibly rewritten into a block, in which case
1437 if Analyzed (N) then
1441 -- If there is an error analyzing the name (which may have been
1442 -- rewritten if the original call was in prefix notation) then error
1443 -- has been emitted already, mark node and return.
1445 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1446 Set_Etype (N, Any_Type);
1450 -- Otherwise analyze the parameters
1452 if Present (Actuals) then
1453 Actual := First (Actuals);
1455 while Present (Actual) loop
1457 Check_Parameterless_Call (Actual);
1462 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1464 if Nkind (P) = N_Attribute_Reference
1465 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1467 Name_Elab_Subp_Body)
1469 if Present (Actuals) then
1471 ("no parameters allowed for this call", First (Actuals));
1475 Set_Etype (N, Standard_Void_Type);
1478 elsif Is_Entity_Name (P)
1479 and then Is_Record_Type (Etype (Entity (P)))
1480 and then Remote_AST_I_Dereference (P)
1484 elsif Is_Entity_Name (P)
1485 and then Ekind (Entity (P)) /= E_Entry_Family
1487 if Is_Access_Type (Etype (P))
1488 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1489 and then No (Actuals)
1490 and then Comes_From_Source (N)
1492 Error_Msg_N ("missing explicit dereference in call", N);
1495 Analyze_Call_And_Resolve;
1497 -- If the prefix is the simple name of an entry family, this is
1498 -- a parameterless call from within the task body itself.
1500 elsif Is_Entity_Name (P)
1501 and then Nkind (P) = N_Identifier
1502 and then Ekind (Entity (P)) = E_Entry_Family
1503 and then Present (Actuals)
1504 and then No (Next (First (Actuals)))
1506 -- Can be call to parameterless entry family. What appears to be the
1507 -- sole argument is in fact the entry index. Rewrite prefix of node
1508 -- accordingly. Source representation is unchanged by this
1512 Make_Indexed_Component (Loc,
1514 Make_Selected_Component (Loc,
1515 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1516 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1517 Expressions => Actuals);
1518 Set_Name (N, New_N);
1519 Set_Etype (New_N, Standard_Void_Type);
1520 Set_Parameter_Associations (N, No_List);
1521 Analyze_Call_And_Resolve;
1523 elsif Nkind (P) = N_Explicit_Dereference then
1524 if Ekind (Etype (P)) = E_Subprogram_Type then
1525 Analyze_Call_And_Resolve;
1527 Error_Msg_N ("expect access to procedure in call", P);
1530 -- The name can be a selected component or an indexed component that
1531 -- yields an access to subprogram. Such a prefix is legal if the call
1532 -- has parameter associations.
1534 elsif Is_Access_Type (Etype (P))
1535 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1537 if Present (Actuals) then
1538 Analyze_Call_And_Resolve;
1540 Error_Msg_N ("missing explicit dereference in call ", N);
1543 -- If not an access to subprogram, then the prefix must resolve to the
1544 -- name of an entry, entry family, or protected operation.
1546 -- For the case of a simple entry call, P is a selected component where
1547 -- the prefix is the task and the selector name is the entry. A call to
1548 -- a protected procedure will have the same syntax. If the protected
1549 -- object contains overloaded operations, the entity may appear as a
1550 -- function, the context will select the operation whose type is Void.
1552 elsif Nkind (P) = N_Selected_Component
1553 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1557 Analyze_Call_And_Resolve;
1559 elsif Nkind (P) = N_Selected_Component
1560 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1561 and then Present (Actuals)
1562 and then No (Next (First (Actuals)))
1564 -- Can be call to parameterless entry family. What appears to be the
1565 -- sole argument is in fact the entry index. Rewrite prefix of node
1566 -- accordingly. Source representation is unchanged by this
1570 Make_Indexed_Component (Loc,
1571 Prefix => New_Copy (P),
1572 Expressions => Actuals);
1573 Set_Name (N, New_N);
1574 Set_Etype (New_N, Standard_Void_Type);
1575 Set_Parameter_Associations (N, No_List);
1576 Analyze_Call_And_Resolve;
1578 -- For the case of a reference to an element of an entry family, P is
1579 -- an indexed component whose prefix is a selected component (task and
1580 -- entry family), and whose index is the entry family index.
1582 elsif Nkind (P) = N_Indexed_Component
1583 and then Nkind (Prefix (P)) = N_Selected_Component
1584 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1586 Analyze_Call_And_Resolve;
1588 -- If the prefix is the name of an entry family, it is a call from
1589 -- within the task body itself.
1591 elsif Nkind (P) = N_Indexed_Component
1592 and then Nkind (Prefix (P)) = N_Identifier
1593 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1596 Make_Selected_Component (Loc,
1597 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1598 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1599 Rewrite (Prefix (P), New_N);
1601 Analyze_Call_And_Resolve;
1603 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1604 -- procedure name, so the construct can only be a qualified expression.
1606 elsif Nkind (P) = N_Qualified_Expression
1607 and then Ada_Version >= Ada_2012
1609 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1612 -- Anything else is an error
1615 Error_Msg_N ("invalid procedure or entry call", N);
1617 end Analyze_Procedure_Call;
1619 ------------------------------
1620 -- Analyze_Return_Statement --
1621 ------------------------------
1623 procedure Analyze_Return_Statement (N : Node_Id) is
1625 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1626 N_Extended_Return_Statement));
1628 Returns_Object : constant Boolean :=
1629 Nkind (N) = N_Extended_Return_Statement
1631 (Nkind (N) = N_Simple_Return_Statement
1632 and then Present (Expression (N)));
1633 -- True if we're returning something; that is, "return <expression>;"
1634 -- or "return Result : T [:= ...]". False for "return;". Used for error
1635 -- checking: If Returns_Object is True, N should apply to a function
1636 -- body; otherwise N should apply to a procedure body, entry body,
1637 -- accept statement, or extended return statement.
1639 function Find_What_It_Applies_To return Entity_Id;
1640 -- Find the entity representing the innermost enclosing body, accept
1641 -- statement, or extended return statement. If the result is a callable
1642 -- construct or extended return statement, then this will be the value
1643 -- of the Return_Applies_To attribute. Otherwise, the program is
1644 -- illegal. See RM-6.5(4/2).
1646 -----------------------------
1647 -- Find_What_It_Applies_To --
1648 -----------------------------
1650 function Find_What_It_Applies_To return Entity_Id is
1651 Result : Entity_Id := Empty;
1654 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1655 -- and postconditions.
1657 for J in reverse 0 .. Scope_Stack.Last loop
1658 Result := Scope_Stack.Table (J).Entity;
1659 exit when not Ekind_In (Result, E_Block, E_Loop)
1660 and then Chars (Result) /= Name_uPostconditions;
1663 pragma Assert (Present (Result));
1665 end Find_What_It_Applies_To;
1667 -- Local declarations
1669 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1670 Kind : constant Entity_Kind := Ekind (Scope_Id);
1671 Loc : constant Source_Ptr := Sloc (N);
1672 Stm_Entity : constant Entity_Id :=
1674 (E_Return_Statement, Current_Scope, Loc, 'R');
1676 -- Start of processing for Analyze_Return_Statement
1679 Set_Return_Statement_Entity (N, Stm_Entity);
1681 Set_Etype (Stm_Entity, Standard_Void_Type);
1682 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1684 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1685 -- (4/2): an inner return statement will apply to this extended return.
1687 if Nkind (N) = N_Extended_Return_Statement then
1688 Push_Scope (Stm_Entity);
1691 -- Check that pragma No_Return is obeyed. Don't complain about the
1692 -- implicitly-generated return that is placed at the end.
1694 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1695 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1698 -- Warn on any unassigned OUT parameters if in procedure
1700 if Ekind (Scope_Id) = E_Procedure then
1701 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1704 -- Check that functions return objects, and other things do not
1706 if Kind = E_Function or else Kind = E_Generic_Function then
1707 if not Returns_Object then
1708 Error_Msg_N ("missing expression in return from function", N);
1711 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1712 if Returns_Object then
1713 Error_Msg_N ("procedure cannot return value (use function)", N);
1716 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1717 if Returns_Object then
1718 if Is_Protected_Type (Scope (Scope_Id)) then
1719 Error_Msg_N ("entry body cannot return value", N);
1721 Error_Msg_N ("accept statement cannot return value", N);
1725 elsif Kind = E_Return_Statement then
1727 -- We are nested within another return statement, which must be an
1728 -- extended_return_statement.
1730 if Returns_Object then
1731 if Nkind (N) = N_Extended_Return_Statement then
1733 ("extended return statement cannot be nested (use `RETURN;`)",
1736 -- Case of a simple return statement with a value inside extended
1737 -- return statement.
1741 ("return nested in extended return statement cannot return " &
1742 "value (use `RETURN;`)", N);
1747 Error_Msg_N ("illegal context for return statement", N);
1750 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1751 Analyze_Function_Return (N);
1753 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1754 Set_Return_Present (Scope_Id);
1757 if Nkind (N) = N_Extended_Return_Statement then
1761 Kill_Current_Values (Last_Assignment_Only => True);
1762 Check_Unreachable_Code (N);
1764 Analyze_Dimension (N);
1765 end Analyze_Return_Statement;
1767 -------------------------------------
1768 -- Analyze_Simple_Return_Statement --
1769 -------------------------------------
1771 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1773 if Present (Expression (N)) then
1774 Mark_Coextensions (N, Expression (N));
1777 Analyze_Return_Statement (N);
1778 end Analyze_Simple_Return_Statement;
1780 -------------------------
1781 -- Analyze_Return_Type --
1782 -------------------------
1784 procedure Analyze_Return_Type (N : Node_Id) is
1785 Designator : constant Entity_Id := Defining_Entity (N);
1786 Typ : Entity_Id := Empty;
1789 -- Normal case where result definition does not indicate an error
1791 if Result_Definition (N) /= Error then
1792 if Nkind (Result_Definition (N)) = N_Access_Definition then
1793 Check_SPARK_Restriction
1794 ("access result is not allowed", Result_Definition (N));
1796 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1799 AD : constant Node_Id :=
1800 Access_To_Subprogram_Definition (Result_Definition (N));
1802 if Present (AD) and then Protected_Present (AD) then
1803 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1805 Typ := Access_Definition (N, Result_Definition (N));
1809 Set_Parent (Typ, Result_Definition (N));
1810 Set_Is_Local_Anonymous_Access (Typ);
1811 Set_Etype (Designator, Typ);
1813 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1815 Null_Exclusion_Static_Checks (N);
1817 -- Subtype_Mark case
1820 Find_Type (Result_Definition (N));
1821 Typ := Entity (Result_Definition (N));
1822 Set_Etype (Designator, Typ);
1824 -- Unconstrained array as result is not allowed in SPARK
1826 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1827 Check_SPARK_Restriction
1828 ("returning an unconstrained array is not allowed",
1829 Result_Definition (N));
1832 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1834 Null_Exclusion_Static_Checks (N);
1836 -- If a null exclusion is imposed on the result type, then create
1837 -- a null-excluding itype (an access subtype) and use it as the
1838 -- function's Etype. Note that the null exclusion checks are done
1839 -- right before this, because they don't get applied to types that
1840 -- do not come from source.
1842 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1843 Set_Etype (Designator,
1844 Create_Null_Excluding_Itype
1847 Scope_Id => Scope (Current_Scope)));
1849 -- The new subtype must be elaborated before use because
1850 -- it is visible outside of the function. However its base
1851 -- type may not be frozen yet, so the reference that will
1852 -- force elaboration must be attached to the freezing of
1855 -- If the return specification appears on a proper body,
1856 -- the subtype will have been created already on the spec.
1858 if Is_Frozen (Typ) then
1859 if Nkind (Parent (N)) = N_Subprogram_Body
1860 and then Nkind (Parent (Parent (N))) = N_Subunit
1864 Build_Itype_Reference (Etype (Designator), Parent (N));
1868 Ensure_Freeze_Node (Typ);
1871 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1873 Set_Itype (IR, Etype (Designator));
1874 Append_Freeze_Actions (Typ, New_List (IR));
1879 Set_Etype (Designator, Typ);
1882 if Ekind (Typ) = E_Incomplete_Type
1883 and then Is_Value_Type (Typ)
1887 elsif Ekind (Typ) = E_Incomplete_Type
1888 or else (Is_Class_Wide_Type (Typ)
1889 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1891 -- AI05-0151: Tagged incomplete types are allowed in all formal
1892 -- parts. Untagged incomplete types are not allowed in bodies.
1894 if Ada_Version >= Ada_2012 then
1895 if Is_Tagged_Type (Typ) then
1898 elsif Nkind_In (Parent (Parent (N)),
1904 ("invalid use of untagged incomplete type&",
1908 -- The type must be completed in the current package. This
1909 -- is checked at the end of the package declaraton, when
1910 -- Taft-amendment types are identified. If the return type
1911 -- is class-wide, there is no required check, the type can
1912 -- be a bona fide TAT.
1914 if Ekind (Scope (Current_Scope)) = E_Package
1915 and then In_Private_Part (Scope (Current_Scope))
1916 and then not Is_Class_Wide_Type (Typ)
1918 Append_Elmt (Designator, Private_Dependents (Typ));
1923 ("invalid use of incomplete type&", Designator, Typ);
1928 -- Case where result definition does indicate an error
1931 Set_Etype (Designator, Any_Type);
1933 end Analyze_Return_Type;
1935 -----------------------------
1936 -- Analyze_Subprogram_Body --
1937 -----------------------------
1939 procedure Analyze_Subprogram_Body (N : Node_Id) is
1940 Loc : constant Source_Ptr := Sloc (N);
1941 Body_Spec : constant Node_Id := Specification (N);
1942 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1945 if Debug_Flag_C then
1946 Write_Str ("==> subprogram body ");
1947 Write_Name (Chars (Body_Id));
1948 Write_Str (" from ");
1949 Write_Location (Loc);
1954 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1956 -- The real work is split out into the helper, so it can do "return;"
1957 -- without skipping the debug output:
1959 Analyze_Subprogram_Body_Helper (N);
1961 if Debug_Flag_C then
1963 Write_Str ("<== subprogram body ");
1964 Write_Name (Chars (Body_Id));
1965 Write_Str (" from ");
1966 Write_Location (Loc);
1969 end Analyze_Subprogram_Body;
1971 ------------------------------------
1972 -- Analyze_Subprogram_Body_Helper --
1973 ------------------------------------
1975 -- This procedure is called for regular subprogram bodies, generic bodies,
1976 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1977 -- specification matters, and is used to create a proper declaration for
1978 -- the subprogram, or to perform conformance checks.
1980 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1981 Loc : constant Source_Ptr := Sloc (N);
1982 Body_Spec : constant Node_Id := Specification (N);
1983 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1984 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1985 Conformant : Boolean;
1987 Prot_Typ : Entity_Id := Empty;
1988 Spec_Id : Entity_Id;
1989 Spec_Decl : Node_Id := Empty;
1991 Last_Real_Spec_Entity : Entity_Id := Empty;
1992 -- When we analyze a separate spec, the entity chain ends up containing
1993 -- the formals, as well as any itypes generated during analysis of the
1994 -- default expressions for parameters, or the arguments of associated
1995 -- precondition/postcondition pragmas (which are analyzed in the context
1996 -- of the spec since they have visibility on formals).
1998 -- These entities belong with the spec and not the body. However we do
1999 -- the analysis of the body in the context of the spec (again to obtain
2000 -- visibility to the formals), and all the entities generated during
2001 -- this analysis end up also chained to the entity chain of the spec.
2002 -- But they really belong to the body, and there is circuitry to move
2003 -- them from the spec to the body.
2005 -- However, when we do this move, we don't want to move the real spec
2006 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2007 -- variable points to the last real spec entity, so we only move those
2008 -- chained beyond that point. It is initialized to Empty to deal with
2009 -- the case where there is no separate spec.
2011 procedure Check_Anonymous_Return;
2012 -- Ada 2005: if a function returns an access type that denotes a task,
2013 -- or a type that contains tasks, we must create a master entity for
2014 -- the anonymous type, which typically will be used in an allocator
2015 -- in the body of the function.
2017 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2018 -- Look ahead to recognize a pragma that may appear after the body.
2019 -- If there is a previous spec, check that it appears in the same
2020 -- declarative part. If the pragma is Inline_Always, perform inlining
2021 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2022 -- If the body acts as a spec, and inlining is required, we create a
2023 -- subprogram declaration for it, in order to attach the body to inline.
2024 -- If pragma does not appear after the body, check whether there is
2025 -- an inline pragma before any local declarations.
2027 procedure Check_Missing_Return;
2028 -- Checks for a function with a no return statements, and also performs
2029 -- the warning checks implemented by Check_Returns. In formal mode, also
2030 -- verify that a function ends with a RETURN and that a procedure does
2031 -- not contain any RETURN.
2033 function Disambiguate_Spec return Entity_Id;
2034 -- When a primitive is declared between the private view and the full
2035 -- view of a concurrent type which implements an interface, a special
2036 -- mechanism is used to find the corresponding spec of the primitive
2039 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2040 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2041 -- incomplete types coming from a limited context and swap their limited
2042 -- views with the non-limited ones.
2044 function Is_Private_Concurrent_Primitive
2045 (Subp_Id : Entity_Id) return Boolean;
2046 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2047 -- type that implements an interface and has a private view.
2049 procedure Set_Trivial_Subprogram (N : Node_Id);
2050 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2051 -- subprogram whose body is being analyzed. N is the statement node
2052 -- causing the flag to be set, if the following statement is a return
2053 -- of an entity, we mark the entity as set in source to suppress any
2054 -- warning on the stylized use of function stubs with a dummy return.
2056 procedure Verify_Overriding_Indicator;
2057 -- If there was a previous spec, the entity has been entered in the
2058 -- current scope previously. If the body itself carries an overriding
2059 -- indicator, check that it is consistent with the known status of the
2062 ----------------------------
2063 -- Check_Anonymous_Return --
2064 ----------------------------
2066 procedure Check_Anonymous_Return is
2072 if Present (Spec_Id) then
2078 if Ekind (Scop) = E_Function
2079 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2080 and then not Is_Thunk (Scop)
2081 and then (Has_Task (Designated_Type (Etype (Scop)))
2083 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2085 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2086 and then Expander_Active
2088 -- Avoid cases with no tasking support
2090 and then RTE_Available (RE_Current_Master)
2091 and then not Restriction_Active (No_Task_Hierarchy)
2094 Make_Object_Declaration (Loc,
2095 Defining_Identifier =>
2096 Make_Defining_Identifier (Loc, Name_uMaster),
2097 Constant_Present => True,
2098 Object_Definition =>
2099 New_Reference_To (RTE (RE_Master_Id), Loc),
2101 Make_Explicit_Dereference (Loc,
2102 New_Reference_To (RTE (RE_Current_Master), Loc)));
2104 if Present (Declarations (N)) then
2105 Prepend (Decl, Declarations (N));
2107 Set_Declarations (N, New_List (Decl));
2110 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2111 Set_Has_Master_Entity (Scop);
2113 -- Now mark the containing scope as a task master
2116 while Nkind (Par) /= N_Compilation_Unit loop
2117 Par := Parent (Par);
2118 pragma Assert (Present (Par));
2120 -- If we fall off the top, we are at the outer level, and
2121 -- the environment task is our effective master, so nothing
2125 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2127 Set_Is_Task_Master (Par, True);
2132 end Check_Anonymous_Return;
2134 -------------------------
2135 -- Check_Inline_Pragma --
2136 -------------------------
2138 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2142 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2143 -- True when N is a pragma Inline or Inline_Always that applies
2144 -- to this subprogram.
2146 -----------------------
2147 -- Is_Inline_Pragma --
2148 -----------------------
2150 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2153 Nkind (N) = N_Pragma
2155 (Pragma_Name (N) = Name_Inline_Always
2158 and then Pragma_Name (N) = Name_Inline))
2161 (Expression (First (Pragma_Argument_Associations (N)))) =
2163 end Is_Inline_Pragma;
2165 -- Start of processing for Check_Inline_Pragma
2168 if not Expander_Active then
2172 if Is_List_Member (N)
2173 and then Present (Next (N))
2174 and then Is_Inline_Pragma (Next (N))
2178 elsif Nkind (N) /= N_Subprogram_Body_Stub
2179 and then Present (Declarations (N))
2180 and then Is_Inline_Pragma (First (Declarations (N)))
2182 Prag := First (Declarations (N));
2188 if Present (Prag) then
2189 if Present (Spec_Id) then
2190 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2195 -- Create a subprogram declaration, to make treatment uniform
2198 Subp : constant Entity_Id :=
2199 Make_Defining_Identifier (Loc, Chars (Body_Id));
2200 Decl : constant Node_Id :=
2201 Make_Subprogram_Declaration (Loc,
2203 New_Copy_Tree (Specification (N)));
2206 Set_Defining_Unit_Name (Specification (Decl), Subp);
2208 if Present (First_Formal (Body_Id)) then
2209 Plist := Copy_Parameter_List (Body_Id);
2210 Set_Parameter_Specifications
2211 (Specification (Decl), Plist);
2214 Insert_Before (N, Decl);
2217 Set_Has_Pragma_Inline (Subp);
2219 if Pragma_Name (Prag) = Name_Inline_Always then
2220 Set_Is_Inlined (Subp);
2221 Set_Has_Pragma_Inline_Always (Subp);
2228 end Check_Inline_Pragma;
2230 --------------------------
2231 -- Check_Missing_Return --
2232 --------------------------
2234 procedure Check_Missing_Return is
2236 Missing_Ret : Boolean;
2239 if Nkind (Body_Spec) = N_Function_Specification then
2240 if Present (Spec_Id) then
2246 if Return_Present (Id) then
2247 Check_Returns (HSS, 'F', Missing_Ret);
2250 Set_Has_Missing_Return (Id);
2253 elsif Is_Generic_Subprogram (Id)
2254 or else not Is_Machine_Code_Subprogram (Id)
2256 Error_Msg_N ("missing RETURN statement in function body", N);
2259 -- If procedure with No_Return, check returns
2261 elsif Nkind (Body_Spec) = N_Procedure_Specification
2262 and then Present (Spec_Id)
2263 and then No_Return (Spec_Id)
2265 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2268 -- Special checks in SPARK mode
2270 if Nkind (Body_Spec) = N_Function_Specification then
2272 -- In SPARK mode, last statement of a function should be a return
2275 Stat : constant Node_Id := Last_Source_Statement (HSS);
2278 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2279 N_Extended_Return_Statement)
2281 Check_SPARK_Restriction
2282 ("last statement in function should be RETURN", Stat);
2286 -- In SPARK mode, verify that a procedure has no return
2288 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2289 if Present (Spec_Id) then
2295 -- Would be nice to point to return statement here, can we
2296 -- borrow the Check_Returns procedure here ???
2298 if Return_Present (Id) then
2299 Check_SPARK_Restriction
2300 ("procedure should not have RETURN", N);
2303 end Check_Missing_Return;
2305 -----------------------
2306 -- Disambiguate_Spec --
2307 -----------------------
2309 function Disambiguate_Spec return Entity_Id is
2310 Priv_Spec : Entity_Id;
2313 procedure Replace_Types (To_Corresponding : Boolean);
2314 -- Depending on the flag, replace the type of formal parameters of
2315 -- Body_Id if it is a concurrent type implementing interfaces with
2316 -- the corresponding record type or the other way around.
2318 procedure Replace_Types (To_Corresponding : Boolean) is
2320 Formal_Typ : Entity_Id;
2323 Formal := First_Formal (Body_Id);
2324 while Present (Formal) loop
2325 Formal_Typ := Etype (Formal);
2327 if Is_Class_Wide_Type (Formal_Typ) then
2328 Formal_Typ := Root_Type (Formal_Typ);
2331 -- From concurrent type to corresponding record
2333 if To_Corresponding then
2334 if Is_Concurrent_Type (Formal_Typ)
2335 and then Present (Corresponding_Record_Type (Formal_Typ))
2336 and then Present (Interfaces (
2337 Corresponding_Record_Type (Formal_Typ)))
2340 Corresponding_Record_Type (Formal_Typ));
2343 -- From corresponding record to concurrent type
2346 if Is_Concurrent_Record_Type (Formal_Typ)
2347 and then Present (Interfaces (Formal_Typ))
2350 Corresponding_Concurrent_Type (Formal_Typ));
2354 Next_Formal (Formal);
2358 -- Start of processing for Disambiguate_Spec
2361 -- Try to retrieve the specification of the body as is. All error
2362 -- messages are suppressed because the body may not have a spec in
2363 -- its current state.
2365 Spec_N := Find_Corresponding_Spec (N, False);
2367 -- It is possible that this is the body of a primitive declared
2368 -- between a private and a full view of a concurrent type. The
2369 -- controlling parameter of the spec carries the concurrent type,
2370 -- not the corresponding record type as transformed by Analyze_
2371 -- Subprogram_Specification. In such cases, we undo the change
2372 -- made by the analysis of the specification and try to find the
2375 -- Note that wrappers already have their corresponding specs and
2376 -- bodies set during their creation, so if the candidate spec is
2377 -- a wrapper, then we definitely need to swap all types to their
2378 -- original concurrent status.
2381 or else Is_Primitive_Wrapper (Spec_N)
2383 -- Restore all references of corresponding record types to the
2384 -- original concurrent types.
2386 Replace_Types (To_Corresponding => False);
2387 Priv_Spec := Find_Corresponding_Spec (N, False);
2389 -- The current body truly belongs to a primitive declared between
2390 -- a private and a full view. We leave the modified body as is,
2391 -- and return the true spec.
2393 if Present (Priv_Spec)
2394 and then Is_Private_Primitive (Priv_Spec)
2399 -- In case that this is some sort of error, restore the original
2400 -- state of the body.
2402 Replace_Types (To_Corresponding => True);
2406 end Disambiguate_Spec;
2408 ----------------------------
2409 -- Exchange_Limited_Views --
2410 ----------------------------
2412 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2413 procedure Detect_And_Exchange (Id : Entity_Id);
2414 -- Determine whether Id's type denotes an incomplete type associated
2415 -- with a limited with clause and exchange the limited view with the
2418 -------------------------
2419 -- Detect_And_Exchange --
2420 -------------------------
2422 procedure Detect_And_Exchange (Id : Entity_Id) is
2423 Typ : constant Entity_Id := Etype (Id);
2426 if Ekind (Typ) = E_Incomplete_Type
2427 and then From_With_Type (Typ)
2428 and then Present (Non_Limited_View (Typ))
2430 Set_Etype (Id, Non_Limited_View (Typ));
2432 end Detect_And_Exchange;
2438 -- Start of processing for Exchange_Limited_Views
2441 if No (Subp_Id) then
2444 -- Do not process subprogram bodies as they already use the non-
2445 -- limited view of types.
2447 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2451 -- Examine all formals and swap views when applicable
2453 Formal := First_Formal (Subp_Id);
2454 while Present (Formal) loop
2455 Detect_And_Exchange (Formal);
2457 Next_Formal (Formal);
2460 -- Process the return type of a function
2462 if Ekind (Subp_Id) = E_Function then
2463 Detect_And_Exchange (Subp_Id);
2465 end Exchange_Limited_Views;
2467 -------------------------------------
2468 -- Is_Private_Concurrent_Primitive --
2469 -------------------------------------
2471 function Is_Private_Concurrent_Primitive
2472 (Subp_Id : Entity_Id) return Boolean
2474 Formal_Typ : Entity_Id;
2477 if Present (First_Formal (Subp_Id)) then
2478 Formal_Typ := Etype (First_Formal (Subp_Id));
2480 if Is_Concurrent_Record_Type (Formal_Typ) then
2481 if Is_Class_Wide_Type (Formal_Typ) then
2482 Formal_Typ := Root_Type (Formal_Typ);
2485 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2488 -- The type of the first formal is a concurrent tagged type with
2492 Is_Concurrent_Type (Formal_Typ)
2493 and then Is_Tagged_Type (Formal_Typ)
2494 and then Has_Private_Declaration (Formal_Typ);
2498 end Is_Private_Concurrent_Primitive;
2500 ----------------------------
2501 -- Set_Trivial_Subprogram --
2502 ----------------------------
2504 procedure Set_Trivial_Subprogram (N : Node_Id) is
2505 Nxt : constant Node_Id := Next (N);
2508 Set_Is_Trivial_Subprogram (Body_Id);
2510 if Present (Spec_Id) then
2511 Set_Is_Trivial_Subprogram (Spec_Id);
2515 and then Nkind (Nxt) = N_Simple_Return_Statement
2516 and then No (Next (Nxt))
2517 and then Present (Expression (Nxt))
2518 and then Is_Entity_Name (Expression (Nxt))
2520 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2522 end Set_Trivial_Subprogram;
2524 ---------------------------------
2525 -- Verify_Overriding_Indicator --
2526 ---------------------------------
2528 procedure Verify_Overriding_Indicator is
2530 if Must_Override (Body_Spec) then
2531 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2532 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2536 elsif not Present (Overridden_Operation (Spec_Id)) then
2538 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2541 elsif Must_Not_Override (Body_Spec) then
2542 if Present (Overridden_Operation (Spec_Id)) then
2544 ("subprogram& overrides inherited operation",
2545 Body_Spec, Spec_Id);
2547 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2548 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2551 ("subprogram & overrides predefined operator ",
2552 Body_Spec, Spec_Id);
2554 -- If this is not a primitive operation or protected subprogram,
2555 -- then the overriding indicator is altogether illegal.
2557 elsif not Is_Primitive (Spec_Id)
2558 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2561 ("overriding indicator only allowed " &
2562 "if subprogram is primitive",
2567 and then Present (Overridden_Operation (Spec_Id))
2569 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2570 Style.Missing_Overriding (N, Body_Id);
2573 and then Can_Override_Operator (Spec_Id)
2574 and then not Is_Predefined_File_Name
2575 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2577 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2578 Style.Missing_Overriding (N, Body_Id);
2580 end Verify_Overriding_Indicator;
2582 -- Start of processing for Analyze_Subprogram_Body_Helper
2585 -- Generic subprograms are handled separately. They always have a
2586 -- generic specification. Determine whether current scope has a
2587 -- previous declaration.
2589 -- If the subprogram body is defined within an instance of the same
2590 -- name, the instance appears as a package renaming, and will be hidden
2591 -- within the subprogram.
2593 if Present (Prev_Id)
2594 and then not Is_Overloadable (Prev_Id)
2595 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2596 or else Comes_From_Source (Prev_Id))
2598 if Is_Generic_Subprogram (Prev_Id) then
2600 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2601 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2603 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2605 if Nkind (N) = N_Subprogram_Body then
2606 HSS := Handled_Statement_Sequence (N);
2607 Check_Missing_Return;
2613 -- Previous entity conflicts with subprogram name. Attempting to
2614 -- enter name will post error.
2616 Enter_Name (Body_Id);
2620 -- Non-generic case, find the subprogram declaration, if one was seen,
2621 -- or enter new overloaded entity in the current scope. If the
2622 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2623 -- part of the context of one of its subunits. No need to redo the
2626 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2630 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2632 if Nkind (N) = N_Subprogram_Body_Stub
2633 or else No (Corresponding_Spec (N))
2635 if Is_Private_Concurrent_Primitive (Body_Id) then
2636 Spec_Id := Disambiguate_Spec;
2638 Spec_Id := Find_Corresponding_Spec (N);
2641 -- If this is a duplicate body, no point in analyzing it
2643 if Error_Posted (N) then
2647 -- A subprogram body should cause freezing of its own declaration,
2648 -- but if there was no previous explicit declaration, then the
2649 -- subprogram will get frozen too late (there may be code within
2650 -- the body that depends on the subprogram having been frozen,
2651 -- such as uses of extra formals), so we force it to be frozen
2652 -- here. Same holds if the body and spec are compilation units.
2653 -- Finally, if the return type is an anonymous access to protected
2654 -- subprogram, it must be frozen before the body because its
2655 -- expansion has generated an equivalent type that is used when
2656 -- elaborating the body.
2658 -- An exception in the case of Ada 2012, AI05-177: The bodies
2659 -- created for expression functions do not freeze.
2662 and then Nkind (Original_Node (N)) /= N_Expression_Function
2664 Freeze_Before (N, Body_Id);
2666 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2667 Freeze_Before (N, Spec_Id);
2669 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2670 Freeze_Before (N, Etype (Body_Id));
2674 Spec_Id := Corresponding_Spec (N);
2678 -- Ada 2012 aspects may appear in a subprogram body, but only if there
2679 -- is no previous spec. Ditto for a subprogram stub that does not have
2680 -- a corresponding spec, but for which there may also be a spec_id.
2682 if Has_Aspects (N) then
2683 if Present (Spec_Id) then
2685 ("aspect specifications must appear in subprogram declaration",
2688 Analyze_Aspect_Specifications (N, Body_Id);
2692 -- Previously we scanned the body to look for nested subprograms, and
2693 -- rejected an inline directive if nested subprograms were present,
2694 -- because the back-end would generate conflicting symbols for the
2695 -- nested bodies. This is now unnecessary.
2697 -- Look ahead to recognize a pragma Inline that appears after the body
2699 Check_Inline_Pragma (Spec_Id);
2701 -- Deal with special case of a fully private operation in the body of
2702 -- the protected type. We must create a declaration for the subprogram,
2703 -- in order to attach the protected subprogram that will be used in
2704 -- internal calls. We exclude compiler generated bodies from the
2705 -- expander since the issue does not arise for those cases.
2708 and then Comes_From_Source (N)
2709 and then Is_Protected_Type (Current_Scope)
2711 Spec_Id := Build_Private_Protected_Declaration (N);
2714 -- If a separate spec is present, then deal with freezing issues
2716 if Present (Spec_Id) then
2717 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2718 Verify_Overriding_Indicator;
2720 -- In general, the spec will be frozen when we start analyzing the
2721 -- body. However, for internally generated operations, such as
2722 -- wrapper functions for inherited operations with controlling
2723 -- results, the spec may not have been frozen by the time we expand
2724 -- the freeze actions that include the bodies. In particular, extra
2725 -- formals for accessibility or for return-in-place may need to be
2726 -- generated. Freeze nodes, if any, are inserted before the current
2727 -- body. These freeze actions are also needed in ASIS mode to enable
2728 -- the proper back-annotations.
2730 if not Is_Frozen (Spec_Id)
2731 and then (Expander_Active or ASIS_Mode)
2733 -- Force the generation of its freezing node to ensure proper
2734 -- management of access types in the backend.
2736 -- This is definitely needed for some cases, but it is not clear
2737 -- why, to be investigated further???
2739 Set_Has_Delayed_Freeze (Spec_Id);
2740 Freeze_Before (N, Spec_Id);
2744 -- Mark presence of postcondition procedure in current scope and mark
2745 -- the procedure itself as needing debug info. The latter is important
2746 -- when analyzing decision coverage (for example, for MC/DC coverage).
2748 if Chars (Body_Id) = Name_uPostconditions then
2749 Set_Has_Postconditions (Current_Scope);
2750 Set_Debug_Info_Needed (Body_Id);
2753 -- Place subprogram on scope stack, and make formals visible. If there
2754 -- is a spec, the visible entity remains that of the spec.
2756 if Present (Spec_Id) then
2757 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2759 if Is_Child_Unit (Spec_Id) then
2760 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2764 Style.Check_Identifier (Body_Id, Spec_Id);
2767 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2768 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2770 if Is_Abstract_Subprogram (Spec_Id) then
2771 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2775 Set_Convention (Body_Id, Convention (Spec_Id));
2776 Set_Has_Completion (Spec_Id);
2778 if Is_Protected_Type (Scope (Spec_Id)) then
2779 Prot_Typ := Scope (Spec_Id);
2782 -- If this is a body generated for a renaming, do not check for
2783 -- full conformance. The check is redundant, because the spec of
2784 -- the body is a copy of the spec in the renaming declaration,
2785 -- and the test can lead to spurious errors on nested defaults.
2787 if Present (Spec_Decl)
2788 and then not Comes_From_Source (N)
2790 (Nkind (Original_Node (Spec_Decl)) =
2791 N_Subprogram_Renaming_Declaration
2792 or else (Present (Corresponding_Body (Spec_Decl))
2794 Nkind (Unit_Declaration_Node
2795 (Corresponding_Body (Spec_Decl))) =
2796 N_Subprogram_Renaming_Declaration))
2800 -- Conversely, the spec may have been generated for specless body
2801 -- with an inline pragma.
2803 elsif Comes_From_Source (N)
2804 and then not Comes_From_Source (Spec_Id)
2805 and then Has_Pragma_Inline (Spec_Id)
2812 Fully_Conformant, True, Conformant, Body_Id);
2815 -- If the body is not fully conformant, we have to decide if we
2816 -- should analyze it or not. If it has a really messed up profile
2817 -- then we probably should not analyze it, since we will get too
2818 -- many bogus messages.
2820 -- Our decision is to go ahead in the non-fully conformant case
2821 -- only if it is at least mode conformant with the spec. Note
2822 -- that the call to Check_Fully_Conformant has issued the proper
2823 -- error messages to complain about the lack of conformance.
2826 and then not Mode_Conformant (Body_Id, Spec_Id)
2832 if Spec_Id /= Body_Id then
2833 Reference_Body_Formals (Spec_Id, Body_Id);
2836 if Nkind (N) /= N_Subprogram_Body_Stub then
2837 Set_Corresponding_Spec (N, Spec_Id);
2839 -- Ada 2005 (AI-345): If the operation is a primitive operation
2840 -- of a concurrent type, the type of the first parameter has been
2841 -- replaced with the corresponding record, which is the proper
2842 -- run-time structure to use. However, within the body there may
2843 -- be uses of the formals that depend on primitive operations
2844 -- of the type (in particular calls in prefixed form) for which
2845 -- we need the original concurrent type. The operation may have
2846 -- several controlling formals, so the replacement must be done
2849 if Comes_From_Source (Spec_Id)
2850 and then Present (First_Entity (Spec_Id))
2851 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2852 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2854 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2857 (Corresponding_Concurrent_Type
2858 (Etype (First_Entity (Spec_Id))))
2861 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2865 Form := First_Formal (Spec_Id);
2866 while Present (Form) loop
2867 if Etype (Form) = Typ then
2868 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2876 -- Make the formals visible, and place subprogram on scope stack.
2877 -- This is also the point at which we set Last_Real_Spec_Entity
2878 -- to mark the entities which will not be moved to the body.
2880 Install_Formals (Spec_Id);
2881 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2883 -- Within an instance, add local renaming declarations so that
2884 -- gdb can retrieve the values of actuals more easily. This is
2885 -- only relevant if generating code (and indeed we definitely
2886 -- do not want these definitions -gnatc mode, because that would
2889 if Is_Generic_Instance (Spec_Id)
2890 and then Is_Wrapper_Package (Current_Scope)
2891 and then Expander_Active
2893 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2896 Push_Scope (Spec_Id);
2898 -- Make sure that the subprogram is immediately visible. For
2899 -- child units that have no separate spec this is indispensable.
2900 -- Otherwise it is safe albeit redundant.
2902 Set_Is_Immediately_Visible (Spec_Id);
2905 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2906 Set_Contract (Body_Id, Empty);
2907 Set_Ekind (Body_Id, E_Subprogram_Body);
2908 Set_Scope (Body_Id, Scope (Spec_Id));
2909 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2911 -- Case of subprogram body with no previous spec
2914 -- Check for style warning required
2918 -- Only apply check for source level subprograms for which checks
2919 -- have not been suppressed.
2921 and then Comes_From_Source (Body_Id)
2922 and then not Suppress_Style_Checks (Body_Id)
2924 -- No warnings within an instance
2926 and then not In_Instance
2928 -- No warnings for expression functions
2930 and then Nkind (Original_Node (N)) /= N_Expression_Function
2932 Style.Body_With_No_Spec (N);
2935 New_Overloaded_Entity (Body_Id);
2937 if Nkind (N) /= N_Subprogram_Body_Stub then
2938 Set_Acts_As_Spec (N);
2939 Generate_Definition (Body_Id);
2940 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
2942 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2943 Install_Formals (Body_Id);
2944 Push_Scope (Body_Id);
2947 -- For stubs and bodies with no previous spec, generate references to
2950 Generate_Reference_To_Formals (Body_Id);
2953 -- If the return type is an anonymous access type whose designated type
2954 -- is the limited view of a class-wide type and the non-limited view is
2955 -- available, update the return type accordingly.
2957 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
2963 Rtyp := Etype (Current_Scope);
2965 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2966 Etyp := Directly_Designated_Type (Rtyp);
2968 if Is_Class_Wide_Type (Etyp) and then From_With_Type (Etyp) then
2969 Set_Directly_Designated_Type
2970 (Etype (Current_Scope), Available_View (Etyp));
2976 -- If this is the proper body of a stub, we must verify that the stub
2977 -- conforms to the body, and to the previous spec if one was present.
2978 -- We know already that the body conforms to that spec. This test is
2979 -- only required for subprograms that come from source.
2981 if Nkind (Parent (N)) = N_Subunit
2982 and then Comes_From_Source (N)
2983 and then not Error_Posted (Body_Id)
2984 and then Nkind (Corresponding_Stub (Parent (N))) =
2985 N_Subprogram_Body_Stub
2988 Old_Id : constant Entity_Id :=
2990 (Specification (Corresponding_Stub (Parent (N))));
2992 Conformant : Boolean := False;
2995 if No (Spec_Id) then
2996 Check_Fully_Conformant (Body_Id, Old_Id);
3000 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3002 if not Conformant then
3004 -- The stub was taken to be a new declaration. Indicate that
3007 Set_Has_Completion (Old_Id, False);
3013 Set_Has_Completion (Body_Id);
3014 Check_Eliminated (Body_Id);
3016 if Nkind (N) = N_Subprogram_Body_Stub then
3020 -- Handle frontend inlining. There is no need to prepare us for inlining
3021 -- if we will not generate the code.
3025 if not Debug_Flag_Dot_K then
3026 if Present (Spec_Id)
3027 and then Expander_Active
3029 (Has_Pragma_Inline_Always (Spec_Id)
3030 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3032 Build_Body_To_Inline (N, Spec_Id);
3037 elsif Expander_Active
3038 and then Serious_Errors_Detected = 0
3039 and then Present (Spec_Id)
3040 and then Has_Pragma_Inline (Spec_Id)
3042 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3045 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3046 -- of the specification we have to install the private withed units.
3047 -- This holds for child units as well.
3049 if Is_Compilation_Unit (Body_Id)
3050 or else Nkind (Parent (N)) = N_Compilation_Unit
3052 Install_Private_With_Clauses (Body_Id);
3055 Check_Anonymous_Return;
3057 -- Set the Protected_Formal field of each extra formal of the protected
3058 -- subprogram to reference the corresponding extra formal of the
3059 -- subprogram that implements it. For regular formals this occurs when
3060 -- the protected subprogram's declaration is expanded, but the extra
3061 -- formals don't get created until the subprogram is frozen. We need to
3062 -- do this before analyzing the protected subprogram's body so that any
3063 -- references to the original subprogram's extra formals will be changed
3064 -- refer to the implementing subprogram's formals (see Expand_Formal).
3066 if Present (Spec_Id)
3067 and then Is_Protected_Type (Scope (Spec_Id))
3068 and then Present (Protected_Body_Subprogram (Spec_Id))
3071 Impl_Subp : constant Entity_Id :=
3072 Protected_Body_Subprogram (Spec_Id);
3073 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3074 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3076 while Present (Prot_Ext_Formal) loop
3077 pragma Assert (Present (Impl_Ext_Formal));
3078 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3079 Next_Formal_With_Extras (Prot_Ext_Formal);
3080 Next_Formal_With_Extras (Impl_Ext_Formal);
3085 -- Now we can go on to analyze the body
3087 HSS := Handled_Statement_Sequence (N);
3088 Set_Actual_Subtypes (N, Current_Scope);
3090 -- Deal with preconditions and postconditions. In formal verification
3091 -- mode, we keep pre- and postconditions attached to entities rather
3092 -- than inserted in the code, in order to facilitate a distinct
3093 -- treatment for them.
3095 if not SPARK_Mode then
3096 Process_PPCs (N, Spec_Id, Body_Id);
3099 -- Add a declaration for the Protection object, renaming declarations
3100 -- for discriminals and privals and finally a declaration for the entry
3101 -- family index (if applicable). This form of early expansion is done
3102 -- when the Expander is active because Install_Private_Data_Declarations
3103 -- references entities which were created during regular expansion. The
3104 -- body may be the rewritting of an expression function, and we need to
3105 -- verify that the original node is in the source.
3107 if Full_Expander_Active
3108 and then Comes_From_Source (Original_Node (N))
3109 and then Present (Prot_Typ)
3110 and then Present (Spec_Id)
3111 and then not Is_Eliminated (Spec_Id)
3113 Install_Private_Data_Declarations
3114 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3117 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3118 -- may now appear in parameter and result profiles. Since the analysis
3119 -- of a subprogram body may use the parameter and result profile of the
3120 -- spec, swap any limited views with their non-limited counterpart.
3122 if Ada_Version >= Ada_2012 then
3123 Exchange_Limited_Views (Spec_Id);
3126 -- Analyze the declarations (this call will analyze the precondition
3127 -- Check pragmas we prepended to the list, as well as the declaration
3128 -- of the _Postconditions procedure).
3130 Analyze_Declarations (Declarations (N));
3132 -- Check completion, and analyze the statements
3135 Inspect_Deferred_Constant_Completion (Declarations (N));
3138 -- Deal with end of scope processing for the body
3140 Process_End_Label (HSS, 't', Current_Scope);
3142 Check_Subprogram_Order (N);
3143 Set_Analyzed (Body_Id);
3145 -- If we have a separate spec, then the analysis of the declarations
3146 -- caused the entities in the body to be chained to the spec id, but
3147 -- we want them chained to the body id. Only the formal parameters
3148 -- end up chained to the spec id in this case.
3150 if Present (Spec_Id) then
3152 -- We must conform to the categorization of our spec
3154 Validate_Categorization_Dependency (N, Spec_Id);
3156 -- And if this is a child unit, the parent units must conform
3158 if Is_Child_Unit (Spec_Id) then
3159 Validate_Categorization_Dependency
3160 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3163 -- Here is where we move entities from the spec to the body
3165 -- Case where there are entities that stay with the spec
3167 if Present (Last_Real_Spec_Entity) then
3169 -- No body entities (happens when the only real spec entities come
3170 -- from precondition and postcondition pragmas).
3172 if No (Last_Entity (Body_Id)) then
3174 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3176 -- Body entities present (formals), so chain stuff past them
3180 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3183 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3184 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3185 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3187 -- Case where there are no spec entities, in this case there can be
3188 -- no body entities either, so just move everything.
3191 pragma Assert (No (Last_Entity (Body_Id)));
3192 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3193 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3194 Set_First_Entity (Spec_Id, Empty);
3195 Set_Last_Entity (Spec_Id, Empty);
3199 Check_Missing_Return;
3201 -- Now we are going to check for variables that are never modified in
3202 -- the body of the procedure. But first we deal with a special case
3203 -- where we want to modify this check. If the body of the subprogram
3204 -- starts with a raise statement or its equivalent, or if the body
3205 -- consists entirely of a null statement, then it is pretty obvious
3206 -- that it is OK to not reference the parameters. For example, this
3207 -- might be the following common idiom for a stubbed function:
3208 -- statement of the procedure raises an exception. In particular this
3209 -- deals with the common idiom of a stubbed function, which might
3210 -- appear as something like:
3212 -- function F (A : Integer) return Some_Type;
3215 -- raise Program_Error;
3219 -- Here the purpose of X is simply to satisfy the annoying requirement
3220 -- in Ada that there be at least one return, and we certainly do not
3221 -- want to go posting warnings on X that it is not initialized! On
3222 -- the other hand, if X is entirely unreferenced that should still
3225 -- What we do is to detect these cases, and if we find them, flag the
3226 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3227 -- suppress unwanted warnings. For the case of the function stub above
3228 -- we have a special test to set X as apparently assigned to suppress
3235 -- Skip initial labels (for one thing this occurs when we are in
3236 -- front end ZCX mode, but in any case it is irrelevant), and also
3237 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3239 Stm := First (Statements (HSS));
3240 while Nkind (Stm) = N_Label
3241 or else Nkind (Stm) in N_Push_xxx_Label
3246 -- Do the test on the original statement before expansion
3249 Ostm : constant Node_Id := Original_Node (Stm);
3252 -- If explicit raise statement, turn on flag
3254 if Nkind (Ostm) = N_Raise_Statement then
3255 Set_Trivial_Subprogram (Stm);
3257 -- If null statement, and no following statements, turn on flag
3259 elsif Nkind (Stm) = N_Null_Statement
3260 and then Comes_From_Source (Stm)
3261 and then No (Next (Stm))
3263 Set_Trivial_Subprogram (Stm);
3265 -- Check for explicit call cases which likely raise an exception
3267 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3268 if Is_Entity_Name (Name (Ostm)) then
3270 Ent : constant Entity_Id := Entity (Name (Ostm));
3273 -- If the procedure is marked No_Return, then likely it
3274 -- raises an exception, but in any case it is not coming
3275 -- back here, so turn on the flag.
3278 and then Ekind (Ent) = E_Procedure
3279 and then No_Return (Ent)
3281 Set_Trivial_Subprogram (Stm);
3289 -- Check for variables that are never modified
3295 -- If there is a separate spec, then transfer Never_Set_In_Source
3296 -- flags from out parameters to the corresponding entities in the
3297 -- body. The reason we do that is we want to post error flags on
3298 -- the body entities, not the spec entities.
3300 if Present (Spec_Id) then
3301 E1 := First_Entity (Spec_Id);
3302 while Present (E1) loop
3303 if Ekind (E1) = E_Out_Parameter then
3304 E2 := First_Entity (Body_Id);
3305 while Present (E2) loop
3306 exit when Chars (E1) = Chars (E2);
3310 if Present (E2) then
3311 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3319 -- Check references in body
3321 Check_References (Body_Id);
3323 end Analyze_Subprogram_Body_Helper;
3325 ---------------------------------
3326 -- Analyze_Subprogram_Contract --
3327 ---------------------------------
3329 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3330 Result_Seen : Boolean := False;
3331 -- A flag which keeps track of whether at least one postcondition or
3332 -- contract-case mentions attribute 'Result (set True if so).
3334 procedure Check_Result_And_Post_State
3336 Error_Nod : in out Node_Id);
3337 -- Determine whether pragma Prag mentions attribute 'Result and whether
3338 -- the pragma contains an expression that evaluates differently in pre-
3339 -- and post-state. Prag is a postcondition or a contract-cases pragma.
3340 -- Error_Nod denotes the proper error node.
3342 ---------------------------------
3343 -- Check_Result_And_Post_State --
3344 ---------------------------------
3346 procedure Check_Result_And_Post_State
3348 Error_Nod : in out Node_Id)
3350 procedure Check_Expression (Expr : Node_Id);
3351 -- Perform the 'Result and post-state checks on a given expression
3353 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3354 -- Attempt to find attribute 'Result in a subtree denoted by N
3356 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3357 -- Determine whether source node N denotes "True" or "False"
3359 function Mentions_Post_State (N : Node_Id) return Boolean;
3360 -- Determine whether a subtree denoted by N mentions any construct
3361 -- that denotes a post-state.
3363 procedure Check_Function_Result is
3364 new Traverse_Proc (Is_Function_Result);
3366 ----------------------
3367 -- Check_Expression --
3368 ----------------------
3370 procedure Check_Expression (Expr : Node_Id) is
3372 if not Is_Trivial_Boolean (Expr) then
3373 Check_Function_Result (Expr);
3375 if not Mentions_Post_State (Expr) then
3376 if Pragma_Name (Prag) = Name_Contract_Cases then
3378 ("contract case refers only to pre-state?T?", Expr);
3381 ("postcondition refers only to pre-state?T?", Prag);
3385 end Check_Expression;
3387 ------------------------
3388 -- Is_Function_Result --
3389 ------------------------
3391 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3393 if Nkind (N) = N_Attribute_Reference
3394 and then Attribute_Name (N) = Name_Result
3396 Result_Seen := True;
3399 -- Continue the traversal
3404 end Is_Function_Result;
3406 ------------------------
3407 -- Is_Trivial_Boolean --
3408 ------------------------
3410 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3413 Comes_From_Source (N)
3414 and then Is_Entity_Name (N)
3415 and then (Entity (N) = Standard_True
3416 or else Entity (N) = Standard_False);
3417 end Is_Trivial_Boolean;
3419 -------------------------
3420 -- Mentions_Post_State --
3421 -------------------------
3423 function Mentions_Post_State (N : Node_Id) return Boolean is
3424 Post_State_Seen : Boolean := False;
3426 function Is_Post_State (N : Node_Id) return Traverse_Result;
3427 -- Attempt to find a construct that denotes a post-state. If this
3428 -- is the case, set flag Post_State_Seen.
3434 function Is_Post_State (N : Node_Id) return Traverse_Result is
3438 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3439 Post_State_Seen := True;
3442 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3445 if No (Ent) or else Ekind (Ent) in Assignable_Kind then
3446 Post_State_Seen := True;
3450 elsif Nkind (N) = N_Attribute_Reference then
3451 if Attribute_Name (N) = Name_Old then
3453 elsif Attribute_Name (N) = Name_Result then
3454 Post_State_Seen := True;
3462 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3464 -- Start of processing for Mentions_Post_State
3467 Find_Post_State (N);
3468 return Post_State_Seen;
3469 end Mentions_Post_State;
3473 Expr : constant Node_Id :=
3474 Expression (First (Pragma_Argument_Associations (Prag)));
3475 Nam : constant Name_Id := Pragma_Name (Prag);
3478 -- Start of processing for Check_Result_And_Post_State
3481 if No (Error_Nod) then
3485 -- Examine all consequences
3487 if Nam = Name_Contract_Cases then
3488 CCase := First (Component_Associations (Expr));
3489 while Present (CCase) loop
3490 Check_Expression (Expression (CCase));
3495 -- Examine the expression of a postcondition
3498 pragma Assert (Nam = Name_Postcondition);
3499 Check_Expression (Expr);
3501 end Check_Result_And_Post_State;
3505 Items : constant Node_Id := Contract (Subp);
3506 Error_CCase : Node_Id;
3507 Error_Post : Node_Id;
3510 -- Start of processing for Analyze_Subprogram_Contract
3513 Error_CCase := Empty;
3514 Error_Post := Empty;
3516 if Present (Items) then
3518 -- Analyze pre- and postconditions
3520 Prag := Pre_Post_Conditions (Items);
3521 while Present (Prag) loop
3522 Analyze_PPC_In_Decl_Part (Prag, Subp);
3524 -- Verify whether a postcondition mentions attribute 'Result and
3525 -- its expression introduces a post-state.
3527 if Warn_On_Suspicious_Contract
3528 and then Pragma_Name (Prag) = Name_Postcondition
3530 Check_Result_And_Post_State (Prag, Error_Post);
3533 Prag := Next_Pragma (Prag);
3536 -- Analyze contract-cases and test-cases
3538 Prag := Contract_Test_Cases (Items);
3539 while Present (Prag) loop
3540 if Pragma_Name (Prag) = Name_Contract_Cases then
3541 Analyze_Contract_Cases_In_Decl_Part (Prag);
3543 -- Verify whether contract-cases mention attribute 'Result and
3544 -- its expression introduces a post-state. Perform the check
3545 -- only when the pragma is legal.
3547 if Warn_On_Suspicious_Contract
3548 and then not Error_Posted (Prag)
3550 Check_Result_And_Post_State (Prag, Error_CCase);
3554 pragma Assert (Pragma_Name (Prag) = Name_Test_Case);
3555 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3558 Prag := Next_Pragma (Prag);
3561 -- Analyze classification pragmas
3563 Prag := Classifications (Contract (Subp));
3564 while Present (Prag) loop
3565 if Pragma_Name (Prag) = Name_Depends then
3566 Analyze_Depends_In_Decl_Part (Prag);
3568 pragma Assert (Pragma_Name (Prag) = Name_Global);
3569 Analyze_Global_In_Decl_Part (Prag);
3572 Prag := Next_Pragma (Prag);
3576 -- Emit an error when none of the postconditions or contract-cases
3577 -- mention attribute 'Result in the context of a function.
3579 if Warn_On_Suspicious_Contract
3580 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3581 and then not Result_Seen
3583 if Present (Error_Post) and then Present (Error_CCase) then
3585 ("neither function postcondition nor contract cases mention "
3586 & "result?T?", Error_Post);
3588 elsif Present (Error_Post) then
3590 ("function postcondition does not mention result?T?",
3593 elsif Present (Error_CCase) then
3595 ("contract cases do not mention result?T?", Error_CCase);
3598 end Analyze_Subprogram_Contract;
3600 ------------------------------------
3601 -- Analyze_Subprogram_Declaration --
3602 ------------------------------------
3604 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3605 Scop : constant Entity_Id := Current_Scope;
3606 Designator : Entity_Id;
3607 Is_Completion : Boolean;
3608 -- Indicates whether a null procedure declaration is a completion
3611 -- Null procedures are not allowed in SPARK
3613 if Nkind (Specification (N)) = N_Procedure_Specification
3614 and then Null_Present (Specification (N))
3616 Check_SPARK_Restriction ("null procedure is not allowed", N);
3618 if Is_Protected_Type (Current_Scope) then
3619 Error_Msg_N ("protected operation cannot be a null procedure", N);
3622 Analyze_Null_Procedure (N, Is_Completion);
3624 if Is_Completion then
3626 -- The null procedure acts as a body, nothing further is needed.
3632 Designator := Analyze_Subprogram_Specification (Specification (N));
3634 -- A reference may already have been generated for the unit name, in
3635 -- which case the following call is redundant. However it is needed for
3636 -- declarations that are the rewriting of an expression function.
3638 Generate_Definition (Designator);
3640 if Debug_Flag_C then
3641 Write_Str ("==> subprogram spec ");
3642 Write_Name (Chars (Designator));
3643 Write_Str (" from ");
3644 Write_Location (Sloc (N));
3649 Validate_RCI_Subprogram_Declaration (N);
3650 New_Overloaded_Entity (Designator);
3651 Check_Delayed_Subprogram (Designator);
3653 -- If the type of the first formal of the current subprogram is a
3654 -- non-generic tagged private type, mark the subprogram as being a
3655 -- private primitive. Ditto if this is a function with controlling
3656 -- result, and the return type is currently private. In both cases,
3657 -- the type of the controlling argument or result must be in the
3658 -- current scope for the operation to be primitive.
3660 if Has_Controlling_Result (Designator)
3661 and then Is_Private_Type (Etype (Designator))
3662 and then Scope (Etype (Designator)) = Current_Scope
3663 and then not Is_Generic_Actual_Type (Etype (Designator))
3665 Set_Is_Private_Primitive (Designator);
3667 elsif Present (First_Formal (Designator)) then
3669 Formal_Typ : constant Entity_Id :=
3670 Etype (First_Formal (Designator));
3672 Set_Is_Private_Primitive (Designator,
3673 Is_Tagged_Type (Formal_Typ)
3674 and then Scope (Formal_Typ) = Current_Scope
3675 and then Is_Private_Type (Formal_Typ)
3676 and then not Is_Generic_Actual_Type (Formal_Typ));
3680 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3683 if Ada_Version >= Ada_2005
3684 and then Comes_From_Source (N)
3685 and then Is_Dispatching_Operation (Designator)
3692 if Has_Controlling_Result (Designator) then
3693 Etyp := Etype (Designator);
3696 E := First_Entity (Designator);
3698 and then Is_Formal (E)
3699 and then not Is_Controlling_Formal (E)
3707 if Is_Access_Type (Etyp) then
3708 Etyp := Directly_Designated_Type (Etyp);
3711 if Is_Interface (Etyp)
3712 and then not Is_Abstract_Subprogram (Designator)
3713 and then not (Ekind (Designator) = E_Procedure
3714 and then Null_Present (Specification (N)))
3716 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3718 -- Specialize error message based on procedures vs. functions,
3719 -- since functions can't be null subprograms.
3721 if Ekind (Designator) = E_Procedure then
3723 ("interface procedure % must be abstract or null", N);
3725 Error_Msg_N ("interface function % must be abstract", N);
3731 -- What is the following code for, it used to be
3733 -- ??? Set_Suppress_Elaboration_Checks
3734 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3736 -- The following seems equivalent, but a bit dubious
3738 if Elaboration_Checks_Suppressed (Designator) then
3739 Set_Kill_Elaboration_Checks (Designator);
3742 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3743 Set_Categorization_From_Scope (Designator, Scop);
3746 -- For a compilation unit, check for library-unit pragmas
3748 Push_Scope (Designator);
3749 Set_Categorization_From_Pragmas (N);
3750 Validate_Categorization_Dependency (N, Designator);
3754 -- For a compilation unit, set body required. This flag will only be
3755 -- reset if a valid Import or Interface pragma is processed later on.
3757 if Nkind (Parent (N)) = N_Compilation_Unit then
3758 Set_Body_Required (Parent (N), True);
3760 if Ada_Version >= Ada_2005
3761 and then Nkind (Specification (N)) = N_Procedure_Specification
3762 and then Null_Present (Specification (N))
3765 ("null procedure cannot be declared at library level", N);
3769 Generate_Reference_To_Formals (Designator);
3770 Check_Eliminated (Designator);
3772 if Debug_Flag_C then
3774 Write_Str ("<== subprogram spec ");
3775 Write_Name (Chars (Designator));
3776 Write_Str (" from ");
3777 Write_Location (Sloc (N));
3781 if Is_Protected_Type (Current_Scope) then
3783 -- Indicate that this is a protected operation, because it may be
3784 -- used in subsequent declarations within the protected type.
3786 Set_Convention (Designator, Convention_Protected);
3789 List_Inherited_Pre_Post_Aspects (Designator);
3791 if Has_Aspects (N) then
3792 Analyze_Aspect_Specifications (N, Designator);
3794 end Analyze_Subprogram_Declaration;
3796 --------------------------------------
3797 -- Analyze_Subprogram_Specification --
3798 --------------------------------------
3800 -- Reminder: N here really is a subprogram specification (not a subprogram
3801 -- declaration). This procedure is called to analyze the specification in
3802 -- both subprogram bodies and subprogram declarations (specs).
3804 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3805 Designator : constant Entity_Id := Defining_Entity (N);
3806 Formals : constant List_Id := Parameter_Specifications (N);
3808 -- Start of processing for Analyze_Subprogram_Specification
3811 -- User-defined operator is not allowed in SPARK, except as a renaming
3813 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3814 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3816 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3819 -- Proceed with analysis. Do not emit a cross-reference entry if the
3820 -- specification comes from an expression function, because it may be
3821 -- the completion of a previous declaration. It is is not, the cross-
3822 -- reference entry will be emitted for the new subprogram declaration.
3824 if Nkind (Parent (N)) /= N_Expression_Function then
3825 Generate_Definition (Designator);
3828 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3830 if Nkind (N) = N_Function_Specification then
3831 Set_Ekind (Designator, E_Function);
3832 Set_Mechanism (Designator, Default_Mechanism);
3834 Set_Ekind (Designator, E_Procedure);
3835 Set_Etype (Designator, Standard_Void_Type);
3838 -- Introduce new scope for analysis of the formals and the return type
3840 Set_Scope (Designator, Current_Scope);
3842 if Present (Formals) then
3843 Push_Scope (Designator);
3844 Process_Formals (Formals, N);
3846 -- Check dimensions in N for formals with default expression
3848 Analyze_Dimension_Formals (N, Formals);
3850 -- Ada 2005 (AI-345): If this is an overriding operation of an
3851 -- inherited interface operation, and the controlling type is
3852 -- a synchronized type, replace the type with its corresponding
3853 -- record, to match the proper signature of an overriding operation.
3854 -- Same processing for an access parameter whose designated type is
3855 -- derived from a synchronized interface.
3857 if Ada_Version >= Ada_2005 then
3860 Formal_Typ : Entity_Id;
3861 Rec_Typ : Entity_Id;
3862 Desig_Typ : Entity_Id;
3865 Formal := First_Formal (Designator);
3866 while Present (Formal) loop
3867 Formal_Typ := Etype (Formal);
3869 if Is_Concurrent_Type (Formal_Typ)
3870 and then Present (Corresponding_Record_Type (Formal_Typ))
3872 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3874 if Present (Interfaces (Rec_Typ)) then
3875 Set_Etype (Formal, Rec_Typ);
3878 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3879 Desig_Typ := Designated_Type (Formal_Typ);
3881 if Is_Concurrent_Type (Desig_Typ)
3882 and then Present (Corresponding_Record_Type (Desig_Typ))
3884 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3886 if Present (Interfaces (Rec_Typ)) then
3887 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3892 Next_Formal (Formal);
3899 -- The subprogram scope is pushed and popped around the processing of
3900 -- the return type for consistency with call above to Process_Formals
3901 -- (which itself can call Analyze_Return_Type), and to ensure that any
3902 -- itype created for the return type will be associated with the proper
3905 elsif Nkind (N) = N_Function_Specification then
3906 Push_Scope (Designator);
3907 Analyze_Return_Type (N);
3913 if Nkind (N) = N_Function_Specification then
3915 -- Deal with operator symbol case
3917 if Nkind (Designator) = N_Defining_Operator_Symbol then
3918 Valid_Operator_Definition (Designator);
3921 May_Need_Actuals (Designator);
3923 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3924 -- the subprogram is abstract also. This does not apply to renaming
3925 -- declarations, where abstractness is inherited, and to subprogram
3926 -- bodies generated for stream operations, which become renamings as
3929 -- In case of primitives associated with abstract interface types
3930 -- the check is applied later (see Analyze_Subprogram_Declaration).
3932 if not Nkind_In (Original_Node (Parent (N)),
3933 N_Subprogram_Renaming_Declaration,
3934 N_Abstract_Subprogram_Declaration,
3935 N_Formal_Abstract_Subprogram_Declaration)
3937 if Is_Abstract_Type (Etype (Designator))
3938 and then not Is_Interface (Etype (Designator))
3941 ("function that returns abstract type must be abstract", N);
3943 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3944 -- access result whose designated type is abstract.
3946 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3948 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3949 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3950 and then Ada_Version >= Ada_2012
3952 Error_Msg_N ("function whose access result designates "
3953 & "abstract type must be abstract", N);
3959 end Analyze_Subprogram_Specification;
3961 --------------------------
3962 -- Build_Body_To_Inline --
3963 --------------------------
3965 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3966 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3967 Original_Body : Node_Id;
3968 Body_To_Analyze : Node_Id;
3969 Max_Size : constant := 10;
3970 Stat_Count : Integer := 0;
3972 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3973 -- Check for declarations that make inlining not worthwhile
3975 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3976 -- Check for statements that make inlining not worthwhile: any tasking
3977 -- statement, nested at any level. Keep track of total number of
3978 -- elementary statements, as a measure of acceptable size.
3980 function Has_Pending_Instantiation return Boolean;
3981 -- If some enclosing body contains instantiations that appear before the
3982 -- corresponding generic body, the enclosing body has a freeze node so
3983 -- that it can be elaborated after the generic itself. This might
3984 -- conflict with subsequent inlinings, so that it is unsafe to try to
3985 -- inline in such a case.
3987 function Has_Single_Return return Boolean;
3988 -- In general we cannot inline functions that return unconstrained type.
3989 -- However, we can handle such functions if all return statements return
3990 -- a local variable that is the only declaration in the body of the
3991 -- function. In that case the call can be replaced by that local
3992 -- variable as is done for other inlined calls.
3994 procedure Remove_Pragmas;
3995 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3996 -- parameter has no meaning when the body is inlined and the formals
3997 -- are rewritten. Remove it from body to inline. The analysis of the
3998 -- non-inlined body will handle the pragma properly.
4000 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
4001 -- If the body of the subprogram includes a call that returns an
4002 -- unconstrained type, the secondary stack is involved, and it
4003 -- is not worth inlining.
4005 ------------------------------
4006 -- Has_Excluded_Declaration --
4007 ------------------------------
4009 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4012 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4013 -- Nested subprograms make a given body ineligible for inlining, but
4014 -- we make an exception for instantiations of unchecked conversion.
4015 -- The body has not been analyzed yet, so check the name, and verify
4016 -- that the visible entity with that name is the predefined unit.
4018 -----------------------------
4019 -- Is_Unchecked_Conversion --
4020 -----------------------------
4022 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4023 Id : constant Node_Id := Name (D);
4027 if Nkind (Id) = N_Identifier
4028 and then Chars (Id) = Name_Unchecked_Conversion
4030 Conv := Current_Entity (Id);
4032 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4033 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4035 Conv := Current_Entity (Selector_Name (Id));
4040 return Present (Conv)
4041 and then Is_Predefined_File_Name
4042 (Unit_File_Name (Get_Source_Unit (Conv)))
4043 and then Is_Intrinsic_Subprogram (Conv);
4044 end Is_Unchecked_Conversion;
4046 -- Start of processing for Has_Excluded_Declaration
4050 while Present (D) loop
4051 if (Nkind (D) = N_Function_Instantiation
4052 and then not Is_Unchecked_Conversion (D))
4053 or else Nkind_In (D, N_Protected_Type_Declaration,
4054 N_Package_Declaration,
4055 N_Package_Instantiation,
4057 N_Procedure_Instantiation,
4058 N_Task_Type_Declaration)
4061 ("cannot inline & (non-allowed declaration)?", D, Subp);
4069 end Has_Excluded_Declaration;
4071 ----------------------------
4072 -- Has_Excluded_Statement --
4073 ----------------------------
4075 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4081 while Present (S) loop
4082 Stat_Count := Stat_Count + 1;
4084 if Nkind_In (S, N_Abort_Statement,
4085 N_Asynchronous_Select,
4086 N_Conditional_Entry_Call,
4087 N_Delay_Relative_Statement,
4088 N_Delay_Until_Statement,
4093 ("cannot inline & (non-allowed statement)?", S, Subp);
4096 elsif Nkind (S) = N_Block_Statement then
4097 if Present (Declarations (S))
4098 and then Has_Excluded_Declaration (Declarations (S))
4102 elsif Present (Handled_Statement_Sequence (S))
4105 (Exception_Handlers (Handled_Statement_Sequence (S)))
4107 Has_Excluded_Statement
4108 (Statements (Handled_Statement_Sequence (S))))
4113 elsif Nkind (S) = N_Case_Statement then
4114 E := First (Alternatives (S));
4115 while Present (E) loop
4116 if Has_Excluded_Statement (Statements (E)) then
4123 elsif Nkind (S) = N_If_Statement then
4124 if Has_Excluded_Statement (Then_Statements (S)) then
4128 if Present (Elsif_Parts (S)) then
4129 E := First (Elsif_Parts (S));
4130 while Present (E) loop
4131 if Has_Excluded_Statement (Then_Statements (E)) then
4139 if Present (Else_Statements (S))
4140 and then Has_Excluded_Statement (Else_Statements (S))
4145 elsif Nkind (S) = N_Loop_Statement
4146 and then Has_Excluded_Statement (Statements (S))
4150 elsif Nkind (S) = N_Extended_Return_Statement then
4151 if Has_Excluded_Statement
4152 (Statements (Handled_Statement_Sequence (S)))
4154 (Exception_Handlers (Handled_Statement_Sequence (S)))
4164 end Has_Excluded_Statement;
4166 -------------------------------
4167 -- Has_Pending_Instantiation --
4168 -------------------------------
4170 function Has_Pending_Instantiation return Boolean is
4175 while Present (S) loop
4176 if Is_Compilation_Unit (S)
4177 or else Is_Child_Unit (S)
4181 elsif Ekind (S) = E_Package
4182 and then Has_Forward_Instantiation (S)
4191 end Has_Pending_Instantiation;
4193 ------------------------
4194 -- Has_Single_Return --
4195 ------------------------
4197 function Has_Single_Return return Boolean is
4198 Return_Statement : Node_Id := Empty;
4200 function Check_Return (N : Node_Id) return Traverse_Result;
4206 function Check_Return (N : Node_Id) return Traverse_Result is
4208 if Nkind (N) = N_Simple_Return_Statement then
4209 if Present (Expression (N))
4210 and then Is_Entity_Name (Expression (N))
4212 if No (Return_Statement) then
4213 Return_Statement := N;
4216 elsif Chars (Expression (N)) =
4217 Chars (Expression (Return_Statement))
4225 -- A return statement within an extended return is a noop
4228 elsif No (Expression (N))
4229 and then Nkind (Parent (Parent (N))) =
4230 N_Extended_Return_Statement
4235 -- Expression has wrong form
4240 -- We can only inline a build-in-place function if
4241 -- it has a single extended return.
4243 elsif Nkind (N) = N_Extended_Return_Statement then
4244 if No (Return_Statement) then
4245 Return_Statement := N;
4257 function Check_All_Returns is new Traverse_Func (Check_Return);
4259 -- Start of processing for Has_Single_Return
4262 if Check_All_Returns (N) /= OK then
4265 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4269 return Present (Declarations (N))
4270 and then Present (First (Declarations (N)))
4271 and then Chars (Expression (Return_Statement)) =
4272 Chars (Defining_Identifier (First (Declarations (N))));
4274 end Has_Single_Return;
4276 --------------------
4277 -- Remove_Pragmas --
4278 --------------------
4280 procedure Remove_Pragmas is
4285 Decl := First (Declarations (Body_To_Analyze));
4286 while Present (Decl) loop
4289 if Nkind (Decl) = N_Pragma
4290 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4300 --------------------------
4301 -- Uses_Secondary_Stack --
4302 --------------------------
4304 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4305 function Check_Call (N : Node_Id) return Traverse_Result;
4306 -- Look for function calls that return an unconstrained type
4312 function Check_Call (N : Node_Id) return Traverse_Result is
4314 if Nkind (N) = N_Function_Call
4315 and then Is_Entity_Name (Name (N))
4316 and then Is_Composite_Type (Etype (Entity (Name (N))))
4317 and then not Is_Constrained (Etype (Entity (Name (N))))
4320 ("cannot inline & (call returns unconstrained type)?",
4328 function Check_Calls is new Traverse_Func (Check_Call);
4331 return Check_Calls (Bod) = Abandon;
4332 end Uses_Secondary_Stack;
4334 -- Start of processing for Build_Body_To_Inline
4337 -- Return immediately if done already
4339 if Nkind (Decl) = N_Subprogram_Declaration
4340 and then Present (Body_To_Inline (Decl))
4344 -- Functions that return unconstrained composite types require
4345 -- secondary stack handling, and cannot currently be inlined, unless
4346 -- all return statements return a local variable that is the first
4347 -- local declaration in the body.
4349 elsif Ekind (Subp) = E_Function
4350 and then not Is_Scalar_Type (Etype (Subp))
4351 and then not Is_Access_Type (Etype (Subp))
4352 and then not Is_Constrained (Etype (Subp))
4354 if not Has_Single_Return then
4356 ("cannot inline & (unconstrained return type)?", N, Subp);
4360 -- Ditto for functions that return controlled types, where controlled
4361 -- actions interfere in complex ways with inlining.
4363 elsif Ekind (Subp) = E_Function
4364 and then Needs_Finalization (Etype (Subp))
4367 ("cannot inline & (controlled return type)?", N, Subp);
4371 if Present (Declarations (N))
4372 and then Has_Excluded_Declaration (Declarations (N))
4377 if Present (Handled_Statement_Sequence (N)) then
4378 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4380 ("cannot inline& (exception handler)?",
4381 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4385 Has_Excluded_Statement
4386 (Statements (Handled_Statement_Sequence (N)))
4392 -- We do not inline a subprogram that is too large, unless it is
4393 -- marked Inline_Always. This pragma does not suppress the other
4394 -- checks on inlining (forbidden declarations, handlers, etc).
4396 if Stat_Count > Max_Size
4397 and then not Has_Pragma_Inline_Always (Subp)
4399 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4403 if Has_Pending_Instantiation then
4405 ("cannot inline& (forward instance within enclosing body)?",
4410 -- Within an instance, the body to inline must be treated as a nested
4411 -- generic, so that the proper global references are preserved.
4413 -- Note that we do not do this at the library level, because it is not
4414 -- needed, and furthermore this causes trouble if front end inlining
4415 -- is activated (-gnatN).
4417 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4418 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4419 Original_Body := Copy_Generic_Node (N, Empty, True);
4421 Original_Body := Copy_Separate_Tree (N);
4424 -- We need to capture references to the formals in order to substitute
4425 -- the actuals at the point of inlining, i.e. instantiation. To treat
4426 -- the formals as globals to the body to inline, we nest it within
4427 -- a dummy parameterless subprogram, declared within the real one.
4428 -- To avoid generating an internal name (which is never public, and
4429 -- which affects serial numbers of other generated names), we use
4430 -- an internal symbol that cannot conflict with user declarations.
4432 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4433 Set_Defining_Unit_Name
4434 (Specification (Original_Body),
4435 Make_Defining_Identifier (Sloc (N), Name_uParent));
4436 Set_Corresponding_Spec (Original_Body, Empty);
4438 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4440 -- Set return type of function, which is also global and does not need
4443 if Ekind (Subp) = E_Function then
4444 Set_Result_Definition (Specification (Body_To_Analyze),
4445 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4448 if No (Declarations (N)) then
4449 Set_Declarations (N, New_List (Body_To_Analyze));
4451 Append (Body_To_Analyze, Declarations (N));
4454 Expander_Mode_Save_And_Set (False);
4457 Analyze (Body_To_Analyze);
4458 Push_Scope (Defining_Entity (Body_To_Analyze));
4459 Save_Global_References (Original_Body);
4461 Remove (Body_To_Analyze);
4463 Expander_Mode_Restore;
4465 -- Restore environment if previously saved
4467 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4471 -- If secondary stk used there is no point in inlining. We have
4472 -- already issued the warning in this case, so nothing to do.
4474 if Uses_Secondary_Stack (Body_To_Analyze) then
4478 Set_Body_To_Inline (Decl, Original_Body);
4479 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4480 Set_Is_Inlined (Subp);
4481 end Build_Body_To_Inline;
4487 procedure Cannot_Inline
4491 Is_Serious : Boolean := False)
4494 pragma Assert (Msg (Msg'Last) = '?');
4498 if not Debug_Flag_Dot_K then
4500 -- Do not emit warning if this is a predefined unit which is not
4501 -- the main unit. With validity checks enabled, some predefined
4502 -- subprograms may contain nested subprograms and become ineligible
4505 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4506 and then not In_Extended_Main_Source_Unit (Subp)
4510 elsif Has_Pragma_Inline_Always (Subp) then
4512 -- Remove last character (question mark) to make this into an
4513 -- error, because the Inline_Always pragma cannot be obeyed.
4515 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4517 elsif Ineffective_Inline_Warnings then
4518 Error_Msg_NE (Msg & "p?", N, Subp);
4525 elsif Is_Serious then
4527 -- Remove last character (question mark) to make this into an error.
4529 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4531 elsif Optimization_Level = 0 then
4533 -- Do not emit warning if this is a predefined unit which is not
4534 -- the main unit. This behavior is currently provided for backward
4535 -- compatibility but it will be removed when we enforce the
4536 -- strictness of the new rules.
4538 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4539 and then not In_Extended_Main_Source_Unit (Subp)
4543 elsif Has_Pragma_Inline_Always (Subp) then
4545 -- Emit a warning if this is a call to a runtime subprogram
4546 -- which is located inside a generic. Previously this call
4547 -- was silently skipped!
4549 if Is_Generic_Instance (Subp) then
4551 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4553 if Is_Predefined_File_Name
4554 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4556 Set_Is_Inlined (Subp, False);
4557 Error_Msg_NE (Msg & "p?", N, Subp);
4563 -- Remove last character (question mark) to make this into an
4564 -- error, because the Inline_Always pragma cannot be obeyed.
4566 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4568 else pragma Assert (Front_End_Inlining);
4569 Set_Is_Inlined (Subp, False);
4571 -- When inlining cannot take place we must issue an error.
4572 -- For backward compatibility we still report a warning.
4574 if Ineffective_Inline_Warnings then
4575 Error_Msg_NE (Msg & "p?", N, Subp);
4579 -- Compiling with optimizations enabled it is too early to report
4580 -- problems since the backend may still perform inlining. In order
4581 -- to report unhandled inlinings the program must be compiled with
4582 -- -Winline and the error is reported by the backend.
4589 ------------------------------------
4590 -- Check_And_Build_Body_To_Inline --
4591 ------------------------------------
4593 procedure Check_And_Build_Body_To_Inline
4595 Spec_Id : Entity_Id;
4596 Body_Id : Entity_Id)
4598 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4599 -- Use generic machinery to build an unexpanded body for the subprogram.
4600 -- This body is subsequently used for inline expansions at call sites.
4602 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4603 -- Return true if we generate code for the function body N, the function
4604 -- body N has no local declarations and its unique statement is a single
4605 -- extended return statement with a handled statements sequence.
4607 function Check_Body_To_Inline
4609 Subp : Entity_Id) return Boolean;
4610 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4611 -- inlined by the frontend. These are the rules:
4612 -- * At -O0 use fe inlining when inline_always is specified except if
4613 -- the function returns a controlled type.
4614 -- * At other optimization levels use the fe inlining for both inline
4615 -- and inline_always in the following cases:
4616 -- - function returning a known at compile time constant
4617 -- - function returning a call to an intrinsic function
4618 -- - function returning an unconstrained type (see Can_Split
4619 -- Unconstrained_Function).
4620 -- - function returning a call to a frontend-inlined function
4621 -- Use the back-end mechanism otherwise
4623 -- In addition, in the following cases the function cannot be inlined by
4625 -- - functions that uses the secondary stack
4626 -- - functions that have declarations of:
4627 -- - Concurrent types
4631 -- - functions that have some of the following statements:
4633 -- - asynchronous-select
4634 -- - conditional-entry-call
4637 -- - selective-accept
4638 -- - timed-entry-call
4639 -- - functions that have exception handlers
4640 -- - functions that have some enclosing body containing instantiations
4641 -- that appear before the corresponding generic body.
4643 procedure Generate_Body_To_Inline
4645 Body_To_Inline : out Node_Id);
4646 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4647 -- of pragmas referencing the formals are removed since they have no
4648 -- meaning when the body is inlined and the formals are rewritten (the
4649 -- analysis of the non-inlined body will handle these pragmas properly).
4650 -- A new internal name is associated with Body_To_Inline.
4652 procedure Split_Unconstrained_Function
4654 Spec_Id : Entity_Id);
4655 -- N is an inlined function body that returns an unconstrained type and
4656 -- has a single extended return statement. Split N in two subprograms:
4657 -- a procedure P' and a function F'. The formals of P' duplicate the
4658 -- formals of N plus an extra formal which is used return a value;
4659 -- its body is composed by the declarations and list of statements
4660 -- of the extended return statement of N.
4662 --------------------------
4663 -- Build_Body_To_Inline --
4664 --------------------------
4666 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4667 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4668 Original_Body : Node_Id;
4669 Body_To_Analyze : Node_Id;
4672 pragma Assert (Current_Scope = Spec_Id);
4674 -- Within an instance, the body to inline must be treated as a nested
4675 -- generic, so that the proper global references are preserved. We
4676 -- do not do this at the library level, because it is not needed, and
4677 -- furthermore this causes trouble if front end inlining is activated
4681 and then Scope (Current_Scope) /= Standard_Standard
4683 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4686 -- We need to capture references to the formals in order
4687 -- to substitute the actuals at the point of inlining, i.e.
4688 -- instantiation. To treat the formals as globals to the body to
4689 -- inline, we nest it within a dummy parameterless subprogram,
4690 -- declared within the real one.
4692 Generate_Body_To_Inline (N, Original_Body);
4693 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4695 -- Set return type of function, which is also global and does not
4696 -- need to be resolved.
4698 if Ekind (Spec_Id) = E_Function then
4699 Set_Result_Definition (Specification (Body_To_Analyze),
4700 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4703 if No (Declarations (N)) then
4704 Set_Declarations (N, New_List (Body_To_Analyze));
4706 Append_To (Declarations (N), Body_To_Analyze);
4709 Preanalyze (Body_To_Analyze);
4711 Push_Scope (Defining_Entity (Body_To_Analyze));
4712 Save_Global_References (Original_Body);
4714 Remove (Body_To_Analyze);
4716 -- Restore environment if previously saved
4719 and then Scope (Current_Scope) /= Standard_Standard
4724 pragma Assert (No (Body_To_Inline (Decl)));
4725 Set_Body_To_Inline (Decl, Original_Body);
4726 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4727 end Build_Body_To_Inline;
4729 --------------------------
4730 -- Check_Body_To_Inline --
4731 --------------------------
4733 function Check_Body_To_Inline
4735 Subp : Entity_Id) return Boolean
4737 Max_Size : constant := 10;
4738 Stat_Count : Integer := 0;
4740 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4741 -- Check for declarations that make inlining not worthwhile
4743 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4744 -- Check for statements that make inlining not worthwhile: any
4745 -- tasking statement, nested at any level. Keep track of total
4746 -- number of elementary statements, as a measure of acceptable size.
4748 function Has_Pending_Instantiation return Boolean;
4749 -- Return True if some enclosing body contains instantiations that
4750 -- appear before the corresponding generic body.
4752 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4753 -- Return True if all the return statements of the function body N
4754 -- are simple return statements and return a compile time constant
4756 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4757 -- Return True if all the return statements of the function body N
4758 -- are simple return statements and return an intrinsic function call
4760 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4761 -- If the body of the subprogram includes a call that returns an
4762 -- unconstrained type, the secondary stack is involved, and it
4763 -- is not worth inlining.
4765 ------------------------------
4766 -- Has_Excluded_Declaration --
4767 ------------------------------
4769 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4772 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4773 -- Nested subprograms make a given body ineligible for inlining,
4774 -- but we make an exception for instantiations of unchecked
4775 -- conversion. The body has not been analyzed yet, so check the
4776 -- name, and verify that the visible entity with that name is the
4779 -----------------------------
4780 -- Is_Unchecked_Conversion --
4781 -----------------------------
4783 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4784 Id : constant Node_Id := Name (D);
4788 if Nkind (Id) = N_Identifier
4789 and then Chars (Id) = Name_Unchecked_Conversion
4791 Conv := Current_Entity (Id);
4793 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4795 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4797 Conv := Current_Entity (Selector_Name (Id));
4802 return Present (Conv)
4803 and then Is_Predefined_File_Name
4804 (Unit_File_Name (Get_Source_Unit (Conv)))
4805 and then Is_Intrinsic_Subprogram (Conv);
4806 end Is_Unchecked_Conversion;
4808 -- Start of processing for Has_Excluded_Declaration
4812 while Present (D) loop
4813 if (Nkind (D) = N_Function_Instantiation
4814 and then not Is_Unchecked_Conversion (D))
4815 or else Nkind_In (D, N_Protected_Type_Declaration,
4816 N_Package_Declaration,
4817 N_Package_Instantiation,
4819 N_Procedure_Instantiation,
4820 N_Task_Type_Declaration)
4823 ("cannot inline & (non-allowed declaration)?", D, Subp);
4832 end Has_Excluded_Declaration;
4834 ----------------------------
4835 -- Has_Excluded_Statement --
4836 ----------------------------
4838 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4844 while Present (S) loop
4845 Stat_Count := Stat_Count + 1;
4847 if Nkind_In (S, N_Abort_Statement,
4848 N_Asynchronous_Select,
4849 N_Conditional_Entry_Call,
4850 N_Delay_Relative_Statement,
4851 N_Delay_Until_Statement,
4856 ("cannot inline & (non-allowed statement)?", S, Subp);
4859 elsif Nkind (S) = N_Block_Statement then
4860 if Present (Declarations (S))
4861 and then Has_Excluded_Declaration (Declarations (S))
4865 elsif Present (Handled_Statement_Sequence (S)) then
4867 (Exception_Handlers (Handled_Statement_Sequence (S)))
4870 ("cannot inline& (exception handler)?",
4871 First (Exception_Handlers
4872 (Handled_Statement_Sequence (S))),
4876 elsif Has_Excluded_Statement
4877 (Statements (Handled_Statement_Sequence (S)))
4883 elsif Nkind (S) = N_Case_Statement then
4884 E := First (Alternatives (S));
4885 while Present (E) loop
4886 if Has_Excluded_Statement (Statements (E)) then
4893 elsif Nkind (S) = N_If_Statement then
4894 if Has_Excluded_Statement (Then_Statements (S)) then
4898 if Present (Elsif_Parts (S)) then
4899 E := First (Elsif_Parts (S));
4900 while Present (E) loop
4901 if Has_Excluded_Statement (Then_Statements (E)) then
4908 if Present (Else_Statements (S))
4909 and then Has_Excluded_Statement (Else_Statements (S))
4914 elsif Nkind (S) = N_Loop_Statement
4915 and then Has_Excluded_Statement (Statements (S))
4919 elsif Nkind (S) = N_Extended_Return_Statement then
4920 if Present (Handled_Statement_Sequence (S))
4922 Has_Excluded_Statement
4923 (Statements (Handled_Statement_Sequence (S)))
4927 elsif Present (Handled_Statement_Sequence (S))
4929 Present (Exception_Handlers
4930 (Handled_Statement_Sequence (S)))
4933 ("cannot inline& (exception handler)?",
4934 First (Exception_Handlers
4935 (Handled_Statement_Sequence (S))),
4945 end Has_Excluded_Statement;
4947 -------------------------------
4948 -- Has_Pending_Instantiation --
4949 -------------------------------
4951 function Has_Pending_Instantiation return Boolean is
4956 while Present (S) loop
4957 if Is_Compilation_Unit (S)
4958 or else Is_Child_Unit (S)
4962 elsif Ekind (S) = E_Package
4963 and then Has_Forward_Instantiation (S)
4972 end Has_Pending_Instantiation;
4974 ------------------------------------
4975 -- Returns_Compile_Time_Constant --
4976 ------------------------------------
4978 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4980 function Check_Return (N : Node_Id) return Traverse_Result;
4986 function Check_Return (N : Node_Id) return Traverse_Result is
4988 if Nkind (N) = N_Extended_Return_Statement then
4991 elsif Nkind (N) = N_Simple_Return_Statement then
4992 if Present (Expression (N)) then
4994 Orig_Expr : constant Node_Id :=
4995 Original_Node (Expression (N));
4998 if Nkind_In (Orig_Expr, N_Integer_Literal,
5000 N_Character_Literal)
5004 elsif Is_Entity_Name (Orig_Expr)
5005 and then Ekind (Entity (Orig_Expr)) = E_Constant
5006 and then Is_Static_Expression (Orig_Expr)
5014 -- Expression has wrong form
5020 -- Continue analyzing statements
5027 function Check_All_Returns is new Traverse_Func (Check_Return);
5029 -- Start of processing for Returns_Compile_Time_Constant
5032 return Check_All_Returns (N) = OK;
5033 end Returns_Compile_Time_Constant;
5035 --------------------------------------
5036 -- Returns_Intrinsic_Function_Call --
5037 --------------------------------------
5039 function Returns_Intrinsic_Function_Call
5040 (N : Node_Id) return Boolean
5042 function Check_Return (N : Node_Id) return Traverse_Result;
5048 function Check_Return (N : Node_Id) return Traverse_Result is
5050 if Nkind (N) = N_Extended_Return_Statement then
5053 elsif Nkind (N) = N_Simple_Return_Statement then
5054 if Present (Expression (N)) then
5056 Orig_Expr : constant Node_Id :=
5057 Original_Node (Expression (N));
5060 if Nkind (Orig_Expr) in N_Op
5061 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5065 elsif Nkind (Orig_Expr) in N_Has_Entity
5066 and then Present (Entity (Orig_Expr))
5067 and then Ekind (Entity (Orig_Expr)) = E_Function
5068 and then Is_Inlined (Entity (Orig_Expr))
5072 elsif Nkind (Orig_Expr) in N_Has_Entity
5073 and then Present (Entity (Orig_Expr))
5074 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5083 -- Expression has wrong form
5089 -- Continue analyzing statements
5096 function Check_All_Returns is new Traverse_Func (Check_Return);
5098 -- Start of processing for Returns_Intrinsic_Function_Call
5101 return Check_All_Returns (N) = OK;
5102 end Returns_Intrinsic_Function_Call;
5104 --------------------------
5105 -- Uses_Secondary_Stack --
5106 --------------------------
5108 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5110 function Check_Call (N : Node_Id) return Traverse_Result;
5111 -- Look for function calls that return an unconstrained type
5117 function Check_Call (N : Node_Id) return Traverse_Result is
5119 if Nkind (N) = N_Function_Call
5120 and then Is_Entity_Name (Name (N))
5121 and then Is_Composite_Type (Etype (Entity (Name (N))))
5122 and then not Is_Constrained (Etype (Entity (Name (N))))
5125 ("cannot inline & (call returns unconstrained type)?",
5134 function Check_Calls is new Traverse_Func (Check_Call);
5136 -- Start of processing for Uses_Secondary_Stack
5139 return Check_Calls (N) = Abandon;
5140 end Uses_Secondary_Stack;
5144 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5145 May_Inline : constant Boolean :=
5146 Has_Pragma_Inline_Always (Spec_Id)
5147 or else (Has_Pragma_Inline (Spec_Id)
5148 and then ((Optimization_Level > 0
5149 and then Ekind (Spec_Id)
5151 or else Front_End_Inlining));
5152 Body_To_Analyze : Node_Id;
5154 -- Start of processing for Check_Body_To_Inline
5157 -- No action needed in stubs since the attribute Body_To_Inline
5160 if Nkind (Decl) = N_Subprogram_Body_Stub then
5163 -- Cannot build the body to inline if the attribute is already set.
5164 -- This attribute may have been set if this is a subprogram renaming
5165 -- declarations (see Freeze.Build_Renamed_Body).
5167 elsif Present (Body_To_Inline (Decl)) then
5170 -- No action needed if the subprogram does not fulfill the minimum
5171 -- conditions to be inlined by the frontend
5173 elsif not May_Inline then
5177 -- Check excluded declarations
5179 if Present (Declarations (N))
5180 and then Has_Excluded_Declaration (Declarations (N))
5185 -- Check excluded statements
5187 if Present (Handled_Statement_Sequence (N)) then
5189 (Exception_Handlers (Handled_Statement_Sequence (N)))
5192 ("cannot inline& (exception handler)?",
5194 (Exception_Handlers (Handled_Statement_Sequence (N))),
5199 elsif Has_Excluded_Statement
5200 (Statements (Handled_Statement_Sequence (N)))
5206 -- For backward compatibility, compiling under -gnatN we do not
5207 -- inline a subprogram that is too large, unless it is marked
5208 -- Inline_Always. This pragma does not suppress the other checks
5209 -- on inlining (forbidden declarations, handlers, etc).
5211 if Front_End_Inlining
5212 and then not Has_Pragma_Inline_Always (Subp)
5213 and then Stat_Count > Max_Size
5215 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5219 -- If some enclosing body contains instantiations that appear before
5220 -- the corresponding generic body, the enclosing body has a freeze
5221 -- node so that it can be elaborated after the generic itself. This
5222 -- might conflict with subsequent inlinings, so that it is unsafe to
5223 -- try to inline in such a case.
5225 if Has_Pending_Instantiation then
5227 ("cannot inline& (forward instance within enclosing body)?",
5233 -- Generate and preanalyze the body to inline (needed to perform
5234 -- the rest of the checks)
5236 Generate_Body_To_Inline (N, Body_To_Analyze);
5238 if Ekind (Subp) = E_Function then
5239 Set_Result_Definition (Specification (Body_To_Analyze),
5240 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5243 -- Nest the body to analyze within the real one
5245 if No (Declarations (N)) then
5246 Set_Declarations (N, New_List (Body_To_Analyze));
5248 Append_To (Declarations (N), Body_To_Analyze);
5251 Preanalyze (Body_To_Analyze);
5252 Remove (Body_To_Analyze);
5254 -- Keep separate checks needed when compiling without optimizations
5256 if Optimization_Level = 0
5258 -- AAMP and VM targets have no support for inlining in the backend
5259 -- and hence we use frontend inlining at all optimization levels.
5261 or else AAMP_On_Target
5262 or else VM_Target /= No_VM
5264 -- Cannot inline functions whose body has a call that returns an
5265 -- unconstrained type since the secondary stack is involved, and
5266 -- it is not worth inlining.
5268 if Uses_Secondary_Stack (Body_To_Analyze) then
5271 -- Cannot inline functions that return controlled types since
5272 -- controlled actions interfere in complex ways with inlining.
5274 elsif Ekind (Subp) = E_Function
5275 and then Needs_Finalization (Etype (Subp))
5278 ("cannot inline & (controlled return type)?", N, Subp);
5281 elsif Returns_Unconstrained_Type (Subp) then
5283 ("cannot inline & (unconstrained return type)?", N, Subp);
5287 -- Compiling with optimizations enabled
5290 -- Procedures are never frontend inlined in this case!
5292 if Ekind (Subp) /= E_Function then
5295 -- Functions returning unconstrained types are tested
5296 -- separately (see Can_Split_Unconstrained_Function).
5298 elsif Returns_Unconstrained_Type (Subp) then
5301 -- Check supported cases
5303 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5304 and then Convention (Subp) /= Convention_Intrinsic
5305 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5312 end Check_Body_To_Inline;
5314 --------------------------------------
5315 -- Can_Split_Unconstrained_Function --
5316 --------------------------------------
5318 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5320 Ret_Node : constant Node_Id :=
5321 First (Statements (Handled_Statement_Sequence (N)));
5325 -- No user defined declarations allowed in the function except inside
5326 -- the unique return statement; implicit labels are the only allowed
5329 if not Is_Empty_List (Declarations (N)) then
5330 D := First (Declarations (N));
5331 while Present (D) loop
5332 if Nkind (D) /= N_Implicit_Label_Declaration then
5340 -- We only split the inlined function when we are generating the code
5341 -- of its body; otherwise we leave duplicated split subprograms in
5342 -- the tree which (if referenced) generate wrong references at link
5345 return In_Extended_Main_Code_Unit (N)
5346 and then Present (Ret_Node)
5347 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5348 and then No (Next (Ret_Node))
5349 and then Present (Handled_Statement_Sequence (Ret_Node));
5350 end Can_Split_Unconstrained_Function;
5352 -----------------------------
5353 -- Generate_Body_To_Inline --
5354 -----------------------------
5356 procedure Generate_Body_To_Inline
5358 Body_To_Inline : out Node_Id)
5360 procedure Remove_Pragmas (N : Node_Id);
5361 -- Remove occurrences of pragmas that may reference the formals of
5362 -- N. The analysis of the non-inlined body will handle these pragmas
5365 --------------------
5366 -- Remove_Pragmas --
5367 --------------------
5369 procedure Remove_Pragmas (N : Node_Id) is
5374 Decl := First (Declarations (N));
5375 while Present (Decl) loop
5378 if Nkind (Decl) = N_Pragma
5379 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5389 -- Start of processing for Generate_Body_To_Inline
5392 -- Within an instance, the body to inline must be treated as a nested
5393 -- generic, so that the proper global references are preserved.
5395 -- Note that we do not do this at the library level, because it
5396 -- is not needed, and furthermore this causes trouble if front
5397 -- end inlining is activated (-gnatN).
5400 and then Scope (Current_Scope) /= Standard_Standard
5402 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5404 Body_To_Inline := Copy_Separate_Tree (N);
5407 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5408 -- parameter has no meaning when the body is inlined and the formals
5409 -- are rewritten. Remove it from body to inline. The analysis of the
5410 -- non-inlined body will handle the pragma properly.
5412 Remove_Pragmas (Body_To_Inline);
5414 -- We need to capture references to the formals in order
5415 -- to substitute the actuals at the point of inlining, i.e.
5416 -- instantiation. To treat the formals as globals to the body to
5417 -- inline, we nest it within a dummy parameterless subprogram,
5418 -- declared within the real one.
5420 Set_Parameter_Specifications
5421 (Specification (Body_To_Inline), No_List);
5423 -- A new internal name is associated with Body_To_Inline to avoid
5424 -- conflicts when the non-inlined body N is analyzed.
5426 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5427 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5428 Set_Corresponding_Spec (Body_To_Inline, Empty);
5429 end Generate_Body_To_Inline;
5431 ----------------------------------
5432 -- Split_Unconstrained_Function --
5433 ----------------------------------
5435 procedure Split_Unconstrained_Function
5437 Spec_Id : Entity_Id)
5439 Loc : constant Source_Ptr := Sloc (N);
5440 Ret_Node : constant Node_Id :=
5441 First (Statements (Handled_Statement_Sequence (N)));
5442 Ret_Obj : constant Node_Id :=
5443 First (Return_Object_Declarations (Ret_Node));
5445 procedure Build_Procedure
5446 (Proc_Id : out Entity_Id;
5447 Decl_List : out List_Id);
5448 -- Build a procedure containing the statements found in the extended
5449 -- return statement of the unconstrained function body N.
5451 procedure Build_Procedure
5452 (Proc_Id : out Entity_Id;
5453 Decl_List : out List_Id)
5456 Formal_List : constant List_Id := New_List;
5457 Proc_Spec : Node_Id;
5458 Proc_Body : Node_Id;
5459 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5460 Body_Decl_List : List_Id := No_List;
5461 Param_Type : Node_Id;
5464 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5465 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5468 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5471 Append_To (Formal_List,
5472 Make_Parameter_Specification (Loc,
5473 Defining_Identifier =>
5474 Make_Defining_Identifier (Loc,
5475 Chars => Chars (Defining_Identifier (Ret_Obj))),
5476 In_Present => False,
5477 Out_Present => True,
5478 Null_Exclusion_Present => False,
5479 Parameter_Type => Param_Type));
5481 Formal := First_Formal (Spec_Id);
5482 while Present (Formal) loop
5483 Append_To (Formal_List,
5484 Make_Parameter_Specification (Loc,
5485 Defining_Identifier =>
5486 Make_Defining_Identifier (Sloc (Formal),
5487 Chars => Chars (Formal)),
5488 In_Present => In_Present (Parent (Formal)),
5489 Out_Present => Out_Present (Parent (Formal)),
5490 Null_Exclusion_Present =>
5491 Null_Exclusion_Present (Parent (Formal)),
5493 New_Reference_To (Etype (Formal), Loc),
5495 Copy_Separate_Tree (Expression (Parent (Formal)))));
5497 Next_Formal (Formal);
5501 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5504 Make_Procedure_Specification (Loc,
5505 Defining_Unit_Name => Proc_Id,
5506 Parameter_Specifications => Formal_List);
5508 Decl_List := New_List;
5510 Append_To (Decl_List,
5511 Make_Subprogram_Declaration (Loc, Proc_Spec));
5513 -- Can_Convert_Unconstrained_Function checked that the function
5514 -- has no local declarations except implicit label declarations.
5515 -- Copy these declarations to the built procedure.
5517 if Present (Declarations (N)) then
5518 Body_Decl_List := New_List;
5525 D := First (Declarations (N));
5526 while Present (D) loop
5527 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5530 Make_Implicit_Label_Declaration (Loc,
5531 Make_Defining_Identifier (Loc,
5532 Chars => Chars (Defining_Identifier (D))),
5533 Label_Construct => Empty);
5534 Append_To (Body_Decl_List, New_D);
5541 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5544 Make_Subprogram_Body (Loc,
5545 Specification => Copy_Separate_Tree (Proc_Spec),
5546 Declarations => Body_Decl_List,
5547 Handled_Statement_Sequence =>
5548 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5550 Set_Defining_Unit_Name (Specification (Proc_Body),
5551 Make_Defining_Identifier (Loc, Subp_Name));
5553 Append_To (Decl_List, Proc_Body);
5554 end Build_Procedure;
5558 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5560 Proc_Id : Entity_Id;
5561 Proc_Call : Node_Id;
5563 -- Start of processing for Split_Unconstrained_Function
5566 -- Build the associated procedure, analyze it and insert it before
5567 -- the function body N
5570 Scope : constant Entity_Id := Current_Scope;
5571 Decl_List : List_Id;
5574 Build_Procedure (Proc_Id, Decl_List);
5575 Insert_Actions (N, Decl_List);
5579 -- Build the call to the generated procedure
5582 Actual_List : constant List_Id := New_List;
5586 Append_To (Actual_List,
5587 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5589 Formal := First_Formal (Spec_Id);
5590 while Present (Formal) loop
5591 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5593 -- Avoid spurious warning on unreferenced formals
5595 Set_Referenced (Formal);
5596 Next_Formal (Formal);
5600 Make_Procedure_Call_Statement (Loc,
5601 Name => New_Reference_To (Proc_Id, Loc),
5602 Parameter_Associations => Actual_List);
5610 -- main_1__F1b (New_Obj, ...);
5615 Make_Block_Statement (Loc,
5616 Declarations => New_List (New_Obj),
5617 Handled_Statement_Sequence =>
5618 Make_Handled_Sequence_Of_Statements (Loc,
5619 Statements => New_List (
5623 Make_Simple_Return_Statement (Loc,
5626 (Defining_Identifier (New_Obj), Loc)))));
5628 Rewrite (Ret_Node, Blk_Stmt);
5629 end Split_Unconstrained_Function;
5631 -- Start of processing for Check_And_Build_Body_To_Inline
5634 -- Do not inline any subprogram that contains nested subprograms, since
5635 -- the backend inlining circuit seems to generate uninitialized
5636 -- references in this case. We know this happens in the case of front
5637 -- end ZCX support, but it also appears it can happen in other cases as
5638 -- well. The backend often rejects attempts to inline in the case of
5639 -- nested procedures anyway, so little if anything is lost by this.
5640 -- Note that this is test is for the benefit of the back-end. There is
5641 -- a separate test for front-end inlining that also rejects nested
5644 -- Do not do this test if errors have been detected, because in some
5645 -- error cases, this code blows up, and we don't need it anyway if
5646 -- there have been errors, since we won't get to the linker anyway.
5648 if Comes_From_Source (Body_Id)
5649 and then (Has_Pragma_Inline_Always (Spec_Id)
5650 or else Optimization_Level > 0)
5651 and then Serious_Errors_Detected = 0
5659 P_Ent := Scope (P_Ent);
5660 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5662 if Is_Subprogram (P_Ent) then
5663 Set_Is_Inlined (P_Ent, False);
5665 if Comes_From_Source (P_Ent)
5666 and then Has_Pragma_Inline (P_Ent)
5669 ("cannot inline& (nested subprogram)?", N, P_Ent,
5670 Is_Serious => True);
5677 -- Build the body to inline only if really needed!
5679 if Check_Body_To_Inline (N, Spec_Id)
5680 and then Serious_Errors_Detected = 0
5682 if Returns_Unconstrained_Type (Spec_Id) then
5683 if Can_Split_Unconstrained_Function (N) then
5684 Split_Unconstrained_Function (N, Spec_Id);
5685 Build_Body_To_Inline (N, Spec_Id);
5686 Set_Is_Inlined (Spec_Id);
5689 Build_Body_To_Inline (N, Spec_Id);
5690 Set_Is_Inlined (Spec_Id);
5693 end Check_And_Build_Body_To_Inline;
5695 -----------------------
5696 -- Check_Conformance --
5697 -----------------------
5699 procedure Check_Conformance
5700 (New_Id : Entity_Id;
5702 Ctype : Conformance_Type;
5704 Conforms : out Boolean;
5705 Err_Loc : Node_Id := Empty;
5706 Get_Inst : Boolean := False;
5707 Skip_Controlling_Formals : Boolean := False)
5709 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5710 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5711 -- If Errmsg is True, then processing continues to post an error message
5712 -- for conformance error on given node. Two messages are output. The
5713 -- first message points to the previous declaration with a general "no
5714 -- conformance" message. The second is the detailed reason, supplied as
5715 -- Msg. The parameter N provide information for a possible & insertion
5716 -- in the message, and also provides the location for posting the
5717 -- message in the absence of a specified Err_Loc location.
5719 -----------------------
5720 -- Conformance_Error --
5721 -----------------------
5723 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5730 if No (Err_Loc) then
5736 Error_Msg_Sloc := Sloc (Old_Id);
5739 when Type_Conformant =>
5740 Error_Msg_N -- CODEFIX
5741 ("not type conformant with declaration#!", Enode);
5743 when Mode_Conformant =>
5744 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5746 ("not mode conformant with operation inherited#!",
5750 ("not mode conformant with declaration#!", Enode);
5753 when Subtype_Conformant =>
5754 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5756 ("not subtype conformant with operation inherited#!",
5760 ("not subtype conformant with declaration#!", Enode);
5763 when Fully_Conformant =>
5764 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5765 Error_Msg_N -- CODEFIX
5766 ("not fully conformant with operation inherited#!",
5769 Error_Msg_N -- CODEFIX
5770 ("not fully conformant with declaration#!", Enode);
5774 Error_Msg_NE (Msg, Enode, N);
5776 end Conformance_Error;
5780 Old_Type : constant Entity_Id := Etype (Old_Id);
5781 New_Type : constant Entity_Id := Etype (New_Id);
5782 Old_Formal : Entity_Id;
5783 New_Formal : Entity_Id;
5784 Access_Types_Match : Boolean;
5785 Old_Formal_Base : Entity_Id;
5786 New_Formal_Base : Entity_Id;
5788 -- Start of processing for Check_Conformance
5793 -- We need a special case for operators, since they don't appear
5796 if Ctype = Type_Conformant then
5797 if Ekind (New_Id) = E_Operator
5798 and then Operator_Matches_Spec (New_Id, Old_Id)
5804 -- If both are functions/operators, check return types conform
5806 if Old_Type /= Standard_Void_Type
5807 and then New_Type /= Standard_Void_Type
5810 -- If we are checking interface conformance we omit controlling
5811 -- arguments and result, because we are only checking the conformance
5812 -- of the remaining parameters.
5814 if Has_Controlling_Result (Old_Id)
5815 and then Has_Controlling_Result (New_Id)
5816 and then Skip_Controlling_Formals
5820 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5821 Conformance_Error ("\return type does not match!", New_Id);
5825 -- Ada 2005 (AI-231): In case of anonymous access types check the
5826 -- null-exclusion and access-to-constant attributes match.
5828 if Ada_Version >= Ada_2005
5829 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5831 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5832 or else Is_Access_Constant (Etype (Old_Type)) /=
5833 Is_Access_Constant (Etype (New_Type)))
5835 Conformance_Error ("\return type does not match!", New_Id);
5839 -- If either is a function/operator and the other isn't, error
5841 elsif Old_Type /= Standard_Void_Type
5842 or else New_Type /= Standard_Void_Type
5844 Conformance_Error ("\functions can only match functions!", New_Id);
5848 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5849 -- If this is a renaming as body, refine error message to indicate that
5850 -- the conflict is with the original declaration. If the entity is not
5851 -- frozen, the conventions don't have to match, the one of the renamed
5852 -- entity is inherited.
5854 if Ctype >= Subtype_Conformant then
5855 if Convention (Old_Id) /= Convention (New_Id) then
5856 if not Is_Frozen (New_Id) then
5859 elsif Present (Err_Loc)
5860 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5861 and then Present (Corresponding_Spec (Err_Loc))
5863 Error_Msg_Name_1 := Chars (New_Id);
5865 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5866 Conformance_Error ("\prior declaration for% has convention %!");
5869 Conformance_Error ("\calling conventions do not match!");
5874 elsif Is_Formal_Subprogram (Old_Id)
5875 or else Is_Formal_Subprogram (New_Id)
5877 Conformance_Error ("\formal subprograms not allowed!");
5882 -- Deal with parameters
5884 -- Note: we use the entity information, rather than going directly
5885 -- to the specification in the tree. This is not only simpler, but
5886 -- absolutely necessary for some cases of conformance tests between
5887 -- operators, where the declaration tree simply does not exist!
5889 Old_Formal := First_Formal (Old_Id);
5890 New_Formal := First_Formal (New_Id);
5891 while Present (Old_Formal) and then Present (New_Formal) loop
5892 if Is_Controlling_Formal (Old_Formal)
5893 and then Is_Controlling_Formal (New_Formal)
5894 and then Skip_Controlling_Formals
5896 -- The controlling formals will have different types when
5897 -- comparing an interface operation with its match, but both
5898 -- or neither must be access parameters.
5900 if Is_Access_Type (Etype (Old_Formal))
5902 Is_Access_Type (Etype (New_Formal))
5904 goto Skip_Controlling_Formal;
5907 ("\access parameter does not match!", New_Formal);
5911 -- Ada 2012: Mode conformance also requires that formal parameters
5912 -- be both aliased, or neither.
5914 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5915 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5917 ("\aliased parameter mismatch!", New_Formal);
5921 if Ctype = Fully_Conformant then
5923 -- Names must match. Error message is more accurate if we do
5924 -- this before checking that the types of the formals match.
5926 if Chars (Old_Formal) /= Chars (New_Formal) then
5927 Conformance_Error ("\name & does not match!", New_Formal);
5929 -- Set error posted flag on new formal as well to stop
5930 -- junk cascaded messages in some cases.
5932 Set_Error_Posted (New_Formal);
5936 -- Null exclusion must match
5938 if Null_Exclusion_Present (Parent (Old_Formal))
5940 Null_Exclusion_Present (Parent (New_Formal))
5942 -- Only give error if both come from source. This should be
5943 -- investigated some time, since it should not be needed ???
5945 if Comes_From_Source (Old_Formal)
5947 Comes_From_Source (New_Formal)
5950 ("\null exclusion for & does not match", New_Formal);
5952 -- Mark error posted on the new formal to avoid duplicated
5953 -- complaint about types not matching.
5955 Set_Error_Posted (New_Formal);
5960 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5961 -- case occurs whenever a subprogram is being renamed and one of its
5962 -- parameters imposes a null exclusion. For example:
5964 -- type T is null record;
5965 -- type Acc_T is access T;
5966 -- subtype Acc_T_Sub is Acc_T;
5968 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5969 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5972 Old_Formal_Base := Etype (Old_Formal);
5973 New_Formal_Base := Etype (New_Formal);
5976 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5977 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5980 Access_Types_Match := Ada_Version >= Ada_2005
5982 -- Ensure that this rule is only applied when New_Id is a
5983 -- renaming of Old_Id.
5985 and then Nkind (Parent (Parent (New_Id))) =
5986 N_Subprogram_Renaming_Declaration
5987 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5988 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5989 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5991 -- Now handle the allowed access-type case
5993 and then Is_Access_Type (Old_Formal_Base)
5994 and then Is_Access_Type (New_Formal_Base)
5996 -- The type kinds must match. The only exception occurs with
5997 -- multiple generics of the form:
6000 -- type F is private; type A is private;
6001 -- type F_Ptr is access F; type A_Ptr is access A;
6002 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6003 -- package F_Pack is ... package A_Pack is
6004 -- package F_Inst is
6005 -- new F_Pack (A, A_Ptr, A_P);
6007 -- When checking for conformance between the parameters of A_P
6008 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6009 -- because the compiler has transformed A_Ptr into a subtype of
6010 -- F_Ptr. We catch this case in the code below.
6012 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
6014 (Is_Generic_Type (Old_Formal_Base)
6015 and then Is_Generic_Type (New_Formal_Base)
6016 and then Is_Internal (New_Formal_Base)
6017 and then Etype (Etype (New_Formal_Base)) =
6019 and then Directly_Designated_Type (Old_Formal_Base) =
6020 Directly_Designated_Type (New_Formal_Base)
6021 and then ((Is_Itype (Old_Formal_Base)
6022 and then Can_Never_Be_Null (Old_Formal_Base))
6024 (Is_Itype (New_Formal_Base)
6025 and then Can_Never_Be_Null (New_Formal_Base)));
6027 -- Types must always match. In the visible part of an instance,
6028 -- usual overloading rules for dispatching operations apply, and
6029 -- we check base types (not the actual subtypes).
6031 if In_Instance_Visible_Part
6032 and then Is_Dispatching_Operation (New_Id)
6034 if not Conforming_Types
6035 (T1 => Base_Type (Etype (Old_Formal)),
6036 T2 => Base_Type (Etype (New_Formal)),
6038 Get_Inst => Get_Inst)
6039 and then not Access_Types_Match
6041 Conformance_Error ("\type of & does not match!", New_Formal);
6045 elsif not Conforming_Types
6046 (T1 => Old_Formal_Base,
6047 T2 => New_Formal_Base,
6049 Get_Inst => Get_Inst)
6050 and then not Access_Types_Match
6052 -- Don't give error message if old type is Any_Type. This test
6053 -- avoids some cascaded errors, e.g. in case of a bad spec.
6055 if Errmsg and then Old_Formal_Base = Any_Type then
6058 Conformance_Error ("\type of & does not match!", New_Formal);
6064 -- For mode conformance, mode must match
6066 if Ctype >= Mode_Conformant then
6067 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6068 if not Ekind_In (New_Id, E_Function, E_Procedure)
6069 or else not Is_Primitive_Wrapper (New_Id)
6071 Conformance_Error ("\mode of & does not match!", New_Formal);
6075 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6077 if Is_Protected_Type
6078 (Corresponding_Concurrent_Type (T))
6080 Error_Msg_PT (T, New_Id);
6083 ("\mode of & does not match!", New_Formal);
6090 -- Part of mode conformance for access types is having the same
6091 -- constant modifier.
6093 elsif Access_Types_Match
6094 and then Is_Access_Constant (Old_Formal_Base) /=
6095 Is_Access_Constant (New_Formal_Base)
6098 ("\constant modifier does not match!", New_Formal);
6103 if Ctype >= Subtype_Conformant then
6105 -- Ada 2005 (AI-231): In case of anonymous access types check
6106 -- the null-exclusion and access-to-constant attributes must
6107 -- match. For null exclusion, we test the types rather than the
6108 -- formals themselves, since the attribute is only set reliably
6109 -- on the formals in the Ada 95 case, and we exclude the case
6110 -- where Old_Formal is marked as controlling, to avoid errors
6111 -- when matching completing bodies with dispatching declarations
6112 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6114 if Ada_Version >= Ada_2005
6115 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6116 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6118 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6119 Can_Never_Be_Null (Etype (New_Formal))
6121 not Is_Controlling_Formal (Old_Formal))
6123 Is_Access_Constant (Etype (Old_Formal)) /=
6124 Is_Access_Constant (Etype (New_Formal)))
6126 -- Do not complain if error already posted on New_Formal. This
6127 -- avoids some redundant error messages.
6129 and then not Error_Posted (New_Formal)
6131 -- It is allowed to omit the null-exclusion in case of stream
6132 -- attribute subprograms. We recognize stream subprograms
6133 -- through their TSS-generated suffix.
6136 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6139 if TSS_Name /= TSS_Stream_Read
6140 and then TSS_Name /= TSS_Stream_Write
6141 and then TSS_Name /= TSS_Stream_Input
6142 and then TSS_Name /= TSS_Stream_Output
6144 -- Here we have a definite conformance error. It is worth
6145 -- special casing the error message for the case of a
6146 -- controlling formal (which excludes null).
6148 if Is_Controlling_Formal (New_Formal) then
6149 Error_Msg_Node_2 := Scope (New_Formal);
6151 ("\controlling formal& of& excludes null, "
6152 & "declaration must exclude null as well",
6155 -- Normal case (couldn't we give more detail here???)
6159 ("\type of & does not match!", New_Formal);
6168 -- Full conformance checks
6170 if Ctype = Fully_Conformant then
6172 -- We have checked already that names match
6174 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6176 -- Check default expressions for in parameters
6179 NewD : constant Boolean :=
6180 Present (Default_Value (New_Formal));
6181 OldD : constant Boolean :=
6182 Present (Default_Value (Old_Formal));
6184 if NewD or OldD then
6186 -- The old default value has been analyzed because the
6187 -- current full declaration will have frozen everything
6188 -- before. The new default value has not been analyzed,
6189 -- so analyze it now before we check for conformance.
6192 Push_Scope (New_Id);
6193 Preanalyze_Spec_Expression
6194 (Default_Value (New_Formal), Etype (New_Formal));
6198 if not (NewD and OldD)
6199 or else not Fully_Conformant_Expressions
6200 (Default_Value (Old_Formal),
6201 Default_Value (New_Formal))
6204 ("\default expression for & does not match!",
6213 -- A couple of special checks for Ada 83 mode. These checks are
6214 -- skipped if either entity is an operator in package Standard,
6215 -- or if either old or new instance is not from the source program.
6217 if Ada_Version = Ada_83
6218 and then Sloc (Old_Id) > Standard_Location
6219 and then Sloc (New_Id) > Standard_Location
6220 and then Comes_From_Source (Old_Id)
6221 and then Comes_From_Source (New_Id)
6224 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6225 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6228 -- Explicit IN must be present or absent in both cases. This
6229 -- test is required only in the full conformance case.
6231 if In_Present (Old_Param) /= In_Present (New_Param)
6232 and then Ctype = Fully_Conformant
6235 ("\(Ada 83) IN must appear in both declarations",
6240 -- Grouping (use of comma in param lists) must be the same
6241 -- This is where we catch a misconformance like:
6244 -- A : Integer; B : Integer
6246 -- which are represented identically in the tree except
6247 -- for the setting of the flags More_Ids and Prev_Ids.
6249 if More_Ids (Old_Param) /= More_Ids (New_Param)
6250 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6253 ("\grouping of & does not match!", New_Formal);
6259 -- This label is required when skipping controlling formals
6261 <<Skip_Controlling_Formal>>
6263 Next_Formal (Old_Formal);
6264 Next_Formal (New_Formal);
6267 if Present (Old_Formal) then
6268 Conformance_Error ("\too few parameters!");
6271 elsif Present (New_Formal) then
6272 Conformance_Error ("\too many parameters!", New_Formal);
6275 end Check_Conformance;
6277 -----------------------
6278 -- Check_Conventions --
6279 -----------------------
6281 procedure Check_Conventions (Typ : Entity_Id) is
6282 Ifaces_List : Elist_Id;
6284 procedure Check_Convention (Op : Entity_Id);
6285 -- Verify that the convention of inherited dispatching operation Op is
6286 -- consistent among all subprograms it overrides. In order to minimize
6287 -- the search, Search_From is utilized to designate a specific point in
6288 -- the list rather than iterating over the whole list once more.
6290 ----------------------
6291 -- Check_Convention --
6292 ----------------------
6294 procedure Check_Convention (Op : Entity_Id) is
6295 function Convention_Of (Id : Entity_Id) return Convention_Id;
6296 -- Given an entity, return its convention. The function treats Ghost
6297 -- as convention Ada because the two have the same dynamic semantics.
6303 function Convention_Of (Id : Entity_Id) return Convention_Id is
6304 Conv : constant Convention_Id := Convention (Id);
6306 if Conv = Convention_Ghost then
6307 return Convention_Ada;
6315 Op_Conv : constant Convention_Id := Convention_Of (Op);
6316 Iface_Conv : Convention_Id;
6317 Iface_Elmt : Elmt_Id;
6318 Iface_Prim_Elmt : Elmt_Id;
6319 Iface_Prim : Entity_Id;
6321 -- Start of processing for Check_Convention
6324 Iface_Elmt := First_Elmt (Ifaces_List);
6325 while Present (Iface_Elmt) loop
6327 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6328 while Present (Iface_Prim_Elmt) loop
6329 Iface_Prim := Node (Iface_Prim_Elmt);
6330 Iface_Conv := Convention_Of (Iface_Prim);
6332 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6333 and then Iface_Conv /= Op_Conv
6336 ("inconsistent conventions in primitive operations", Typ);
6338 Error_Msg_Name_1 := Chars (Op);
6339 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6340 Error_Msg_Sloc := Sloc (Op);
6342 if Comes_From_Source (Op) or else No (Alias (Op)) then
6343 if not Present (Overridden_Operation (Op)) then
6344 Error_Msg_N ("\\primitive % defined #", Typ);
6347 ("\\overriding operation % with " &
6348 "convention % defined #", Typ);
6351 else pragma Assert (Present (Alias (Op)));
6352 Error_Msg_Sloc := Sloc (Alias (Op));
6354 ("\\inherited operation % with " &
6355 "convention % defined #", Typ);
6358 Error_Msg_Name_1 := Chars (Op);
6359 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6360 Error_Msg_Sloc := Sloc (Iface_Prim);
6362 ("\\overridden operation % with " &
6363 "convention % defined #", Typ);
6365 -- Avoid cascading errors
6370 Next_Elmt (Iface_Prim_Elmt);
6373 Next_Elmt (Iface_Elmt);
6375 end Check_Convention;
6379 Prim_Op : Entity_Id;
6380 Prim_Op_Elmt : Elmt_Id;
6382 -- Start of processing for Check_Conventions
6385 if not Has_Interfaces (Typ) then
6389 Collect_Interfaces (Typ, Ifaces_List);
6391 -- The algorithm checks every overriding dispatching operation against
6392 -- all the corresponding overridden dispatching operations, detecting
6393 -- differences in conventions.
6395 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6396 while Present (Prim_Op_Elmt) loop
6397 Prim_Op := Node (Prim_Op_Elmt);
6399 -- A small optimization: skip the predefined dispatching operations
6400 -- since they always have the same convention.
6402 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6403 Check_Convention (Prim_Op);
6406 Next_Elmt (Prim_Op_Elmt);
6408 end Check_Conventions;
6410 ------------------------------
6411 -- Check_Delayed_Subprogram --
6412 ------------------------------
6414 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6417 procedure Possible_Freeze (T : Entity_Id);
6418 -- T is the type of either a formal parameter or of the return type.
6419 -- If T is not yet frozen and needs a delayed freeze, then the
6420 -- subprogram itself must be delayed. If T is the limited view of an
6421 -- incomplete type the subprogram must be frozen as well, because
6422 -- T may depend on local types that have not been frozen yet.
6424 ---------------------
6425 -- Possible_Freeze --
6426 ---------------------
6428 procedure Possible_Freeze (T : Entity_Id) is
6430 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6431 Set_Has_Delayed_Freeze (Designator);
6433 elsif Is_Access_Type (T)
6434 and then Has_Delayed_Freeze (Designated_Type (T))
6435 and then not Is_Frozen (Designated_Type (T))
6437 Set_Has_Delayed_Freeze (Designator);
6439 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
6440 Set_Has_Delayed_Freeze (Designator);
6442 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6443 -- of a subprogram or entry declaration.
6445 elsif Ekind (T) = E_Incomplete_Type
6446 and then Ada_Version >= Ada_2012
6448 Set_Has_Delayed_Freeze (Designator);
6451 end Possible_Freeze;
6453 -- Start of processing for Check_Delayed_Subprogram
6456 -- All subprograms, including abstract subprograms, may need a freeze
6457 -- node if some formal type or the return type needs one.
6459 Possible_Freeze (Etype (Designator));
6460 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6462 -- Need delayed freeze if any of the formal types themselves need
6463 -- a delayed freeze and are not yet frozen.
6465 F := First_Formal (Designator);
6466 while Present (F) loop
6467 Possible_Freeze (Etype (F));
6468 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6472 -- Mark functions that return by reference. Note that it cannot be
6473 -- done for delayed_freeze subprograms because the underlying
6474 -- returned type may not be known yet (for private types)
6476 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6478 Typ : constant Entity_Id := Etype (Designator);
6479 Utyp : constant Entity_Id := Underlying_Type (Typ);
6481 if Is_Immutably_Limited_Type (Typ) then
6482 Set_Returns_By_Ref (Designator);
6483 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6484 Set_Returns_By_Ref (Designator);
6488 end Check_Delayed_Subprogram;
6490 ------------------------------------
6491 -- Check_Discriminant_Conformance --
6492 ------------------------------------
6494 procedure Check_Discriminant_Conformance
6499 Old_Discr : Entity_Id := First_Discriminant (Prev);
6500 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6501 New_Discr_Id : Entity_Id;
6502 New_Discr_Type : Entity_Id;
6504 procedure Conformance_Error (Msg : String; N : Node_Id);
6505 -- Post error message for conformance error on given node. Two messages
6506 -- are output. The first points to the previous declaration with a
6507 -- general "no conformance" message. The second is the detailed reason,
6508 -- supplied as Msg. The parameter N provide information for a possible
6509 -- & insertion in the message.
6511 -----------------------
6512 -- Conformance_Error --
6513 -----------------------
6515 procedure Conformance_Error (Msg : String; N : Node_Id) is
6517 Error_Msg_Sloc := Sloc (Prev_Loc);
6518 Error_Msg_N -- CODEFIX
6519 ("not fully conformant with declaration#!", N);
6520 Error_Msg_NE (Msg, N, N);
6521 end Conformance_Error;
6523 -- Start of processing for Check_Discriminant_Conformance
6526 while Present (Old_Discr) and then Present (New_Discr) loop
6527 New_Discr_Id := Defining_Identifier (New_Discr);
6529 -- The subtype mark of the discriminant on the full type has not
6530 -- been analyzed so we do it here. For an access discriminant a new
6533 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6535 Access_Definition (N, Discriminant_Type (New_Discr));
6538 Analyze (Discriminant_Type (New_Discr));
6539 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6541 -- Ada 2005: if the discriminant definition carries a null
6542 -- exclusion, create an itype to check properly for consistency
6543 -- with partial declaration.
6545 if Is_Access_Type (New_Discr_Type)
6546 and then Null_Exclusion_Present (New_Discr)
6549 Create_Null_Excluding_Itype
6550 (T => New_Discr_Type,
6551 Related_Nod => New_Discr,
6552 Scope_Id => Current_Scope);
6556 if not Conforming_Types
6557 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6559 Conformance_Error ("type of & does not match!", New_Discr_Id);
6562 -- Treat the new discriminant as an occurrence of the old one,
6563 -- for navigation purposes, and fill in some semantic
6564 -- information, for completeness.
6566 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6567 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6568 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6573 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6574 Conformance_Error ("name & does not match!", New_Discr_Id);
6578 -- Default expressions must match
6581 NewD : constant Boolean :=
6582 Present (Expression (New_Discr));
6583 OldD : constant Boolean :=
6584 Present (Expression (Parent (Old_Discr)));
6587 if NewD or OldD then
6589 -- The old default value has been analyzed and expanded,
6590 -- because the current full declaration will have frozen
6591 -- everything before. The new default values have not been
6592 -- expanded, so expand now to check conformance.
6595 Preanalyze_Spec_Expression
6596 (Expression (New_Discr), New_Discr_Type);
6599 if not (NewD and OldD)
6600 or else not Fully_Conformant_Expressions
6601 (Expression (Parent (Old_Discr)),
6602 Expression (New_Discr))
6606 ("default expression for & does not match!",
6613 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6615 if Ada_Version = Ada_83 then
6617 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6620 -- Grouping (use of comma in param lists) must be the same
6621 -- This is where we catch a misconformance like:
6624 -- A : Integer; B : Integer
6626 -- which are represented identically in the tree except
6627 -- for the setting of the flags More_Ids and Prev_Ids.
6629 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6630 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6633 ("grouping of & does not match!", New_Discr_Id);
6639 Next_Discriminant (Old_Discr);
6643 if Present (Old_Discr) then
6644 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6647 elsif Present (New_Discr) then
6649 ("too many discriminants!", Defining_Identifier (New_Discr));
6652 end Check_Discriminant_Conformance;
6654 ----------------------------
6655 -- Check_Fully_Conformant --
6656 ----------------------------
6658 procedure Check_Fully_Conformant
6659 (New_Id : Entity_Id;
6661 Err_Loc : Node_Id := Empty)
6664 pragma Warnings (Off, Result);
6667 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6668 end Check_Fully_Conformant;
6670 ---------------------------
6671 -- Check_Mode_Conformant --
6672 ---------------------------
6674 procedure Check_Mode_Conformant
6675 (New_Id : Entity_Id;
6677 Err_Loc : Node_Id := Empty;
6678 Get_Inst : Boolean := False)
6681 pragma Warnings (Off, Result);
6684 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6685 end Check_Mode_Conformant;
6687 --------------------------------
6688 -- Check_Overriding_Indicator --
6689 --------------------------------
6691 procedure Check_Overriding_Indicator
6693 Overridden_Subp : Entity_Id;
6694 Is_Primitive : Boolean)
6700 -- No overriding indicator for literals
6702 if Ekind (Subp) = E_Enumeration_Literal then
6705 elsif Ekind (Subp) = E_Entry then
6706 Decl := Parent (Subp);
6708 -- No point in analyzing a malformed operator
6710 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6711 and then Error_Posted (Subp)
6716 Decl := Unit_Declaration_Node (Subp);
6719 if Nkind_In (Decl, N_Subprogram_Body,
6720 N_Subprogram_Body_Stub,
6721 N_Subprogram_Declaration,
6722 N_Abstract_Subprogram_Declaration,
6723 N_Subprogram_Renaming_Declaration)
6725 Spec := Specification (Decl);
6727 elsif Nkind (Decl) = N_Entry_Declaration then
6734 -- The overriding operation is type conformant with the overridden one,
6735 -- but the names of the formals are not required to match. If the names
6736 -- appear permuted in the overriding operation, this is a possible
6737 -- source of confusion that is worth diagnosing. Controlling formals
6738 -- often carry names that reflect the type, and it is not worthwhile
6739 -- requiring that their names match.
6741 if Present (Overridden_Subp)
6742 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6749 Form1 := First_Formal (Subp);
6750 Form2 := First_Formal (Overridden_Subp);
6752 -- If the overriding operation is a synchronized operation, skip
6753 -- the first parameter of the overridden operation, which is
6754 -- implicit in the new one. If the operation is declared in the
6755 -- body it is not primitive and all formals must match.
6757 if Is_Concurrent_Type (Scope (Subp))
6758 and then Is_Tagged_Type (Scope (Subp))
6759 and then not Has_Completion (Scope (Subp))
6761 Form2 := Next_Formal (Form2);
6764 if Present (Form1) then
6765 Form1 := Next_Formal (Form1);
6766 Form2 := Next_Formal (Form2);
6769 while Present (Form1) loop
6770 if not Is_Controlling_Formal (Form1)
6771 and then Present (Next_Formal (Form2))
6772 and then Chars (Form1) = Chars (Next_Formal (Form2))
6774 Error_Msg_Node_2 := Alias (Overridden_Subp);
6775 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6777 ("& does not match corresponding formal of&#",
6782 Next_Formal (Form1);
6783 Next_Formal (Form2);
6788 -- If there is an overridden subprogram, then check that there is no
6789 -- "not overriding" indicator, and mark the subprogram as overriding.
6790 -- This is not done if the overridden subprogram is marked as hidden,
6791 -- which can occur for the case of inherited controlled operations
6792 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6793 -- subprogram is not itself hidden. (Note: This condition could probably
6794 -- be simplified, leaving out the testing for the specific controlled
6795 -- cases, but it seems safer and clearer this way, and echoes similar
6796 -- special-case tests of this kind in other places.)
6798 if Present (Overridden_Subp)
6799 and then (not Is_Hidden (Overridden_Subp)
6801 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6804 and then Present (Alias (Overridden_Subp))
6805 and then not Is_Hidden (Alias (Overridden_Subp))))
6807 if Must_Not_Override (Spec) then
6808 Error_Msg_Sloc := Sloc (Overridden_Subp);
6810 if Ekind (Subp) = E_Entry then
6812 ("entry & overrides inherited operation #", Spec, Subp);
6815 ("subprogram & overrides inherited operation #", Spec, Subp);
6818 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6819 -- as an extension of Root_Controlled, and thus has a useless Adjust
6820 -- operation. This operation should not be inherited by other limited
6821 -- controlled types. An explicit Adjust for them is not overriding.
6823 elsif Must_Override (Spec)
6824 and then Chars (Overridden_Subp) = Name_Adjust
6825 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6826 and then Present (Alias (Overridden_Subp))
6828 Is_Predefined_File_Name
6829 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6831 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6833 elsif Is_Subprogram (Subp) then
6834 if Is_Init_Proc (Subp) then
6837 elsif No (Overridden_Operation (Subp)) then
6839 -- For entities generated by Derive_Subprograms the overridden
6840 -- operation is the inherited primitive (which is available
6841 -- through the attribute alias)
6843 if (Is_Dispatching_Operation (Subp)
6844 or else Is_Dispatching_Operation (Overridden_Subp))
6845 and then not Comes_From_Source (Overridden_Subp)
6846 and then Find_Dispatching_Type (Overridden_Subp) =
6847 Find_Dispatching_Type (Subp)
6848 and then Present (Alias (Overridden_Subp))
6849 and then Comes_From_Source (Alias (Overridden_Subp))
6851 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6854 Set_Overridden_Operation (Subp, Overridden_Subp);
6859 -- If primitive flag is set or this is a protected operation, then
6860 -- the operation is overriding at the point of its declaration, so
6861 -- warn if necessary. Otherwise it may have been declared before the
6862 -- operation it overrides and no check is required.
6865 and then not Must_Override (Spec)
6866 and then (Is_Primitive
6867 or else Ekind (Scope (Subp)) = E_Protected_Type)
6869 Style.Missing_Overriding (Decl, Subp);
6872 -- If Subp is an operator, it may override a predefined operation, if
6873 -- it is defined in the same scope as the type to which it applies.
6874 -- In that case Overridden_Subp is empty because of our implicit
6875 -- representation for predefined operators. We have to check whether the
6876 -- signature of Subp matches that of a predefined operator. Note that
6877 -- first argument provides the name of the operator, and the second
6878 -- argument the signature that may match that of a standard operation.
6879 -- If the indicator is overriding, then the operator must match a
6880 -- predefined signature, because we know already that there is no
6881 -- explicit overridden operation.
6883 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6884 if Must_Not_Override (Spec) then
6886 -- If this is not a primitive or a protected subprogram, then
6887 -- "not overriding" is illegal.
6890 and then Ekind (Scope (Subp)) /= E_Protected_Type
6893 ("overriding indicator only allowed "
6894 & "if subprogram is primitive", Subp);
6896 elsif Can_Override_Operator (Subp) then
6898 ("subprogram& overrides predefined operator ", Spec, Subp);
6901 elsif Must_Override (Spec) then
6902 if No (Overridden_Operation (Subp))
6903 and then not Can_Override_Operator (Subp)
6905 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6908 elsif not Error_Posted (Subp)
6909 and then Style_Check
6910 and then Can_Override_Operator (Subp)
6912 not Is_Predefined_File_Name
6913 (Unit_File_Name (Get_Source_Unit (Subp)))
6915 -- If style checks are enabled, indicate that the indicator is
6916 -- missing. However, at the point of declaration, the type of
6917 -- which this is a primitive operation may be private, in which
6918 -- case the indicator would be premature.
6920 if Has_Private_Declaration (Etype (Subp))
6921 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6925 Style.Missing_Overriding (Decl, Subp);
6929 elsif Must_Override (Spec) then
6930 if Ekind (Subp) = E_Entry then
6931 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6933 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6936 -- If the operation is marked "not overriding" and it's not primitive
6937 -- then an error is issued, unless this is an operation of a task or
6938 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6939 -- has been specified have already been checked above.
6941 elsif Must_Not_Override (Spec)
6942 and then not Is_Primitive
6943 and then Ekind (Subp) /= E_Entry
6944 and then Ekind (Scope (Subp)) /= E_Protected_Type
6947 ("overriding indicator only allowed if subprogram is primitive",
6951 end Check_Overriding_Indicator;
6957 -- Note: this procedure needs to know far too much about how the expander
6958 -- messes with exceptions. The use of the flag Exception_Junk and the
6959 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6960 -- works, but is not very clean. It would be better if the expansion
6961 -- routines would leave Original_Node working nicely, and we could use
6962 -- Original_Node here to ignore all the peculiar expander messing ???
6964 procedure Check_Returns
6968 Proc : Entity_Id := Empty)
6972 procedure Check_Statement_Sequence (L : List_Id);
6973 -- Internal recursive procedure to check a list of statements for proper
6974 -- termination by a return statement (or a transfer of control or a
6975 -- compound statement that is itself internally properly terminated).
6977 ------------------------------
6978 -- Check_Statement_Sequence --
6979 ------------------------------
6981 procedure Check_Statement_Sequence (L : List_Id) is
6986 Raise_Exception_Call : Boolean;
6987 -- Set True if statement sequence terminated by Raise_Exception call
6988 -- or a Reraise_Occurrence call.
6991 Raise_Exception_Call := False;
6993 -- Get last real statement
6995 Last_Stm := Last (L);
6997 -- Deal with digging out exception handler statement sequences that
6998 -- have been transformed by the local raise to goto optimization.
6999 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7000 -- optimization has occurred, we are looking at something like:
7003 -- original stmts in block
7007 -- goto L1; | omitted if No_Exception_Propagation
7012 -- goto L3; -- skip handler when exception not raised
7014 -- <<L1>> -- target label for local exception
7028 -- and what we have to do is to dig out the estmts1 and estmts2
7029 -- sequences (which were the original sequences of statements in
7030 -- the exception handlers) and check them.
7032 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7037 exit when Nkind (Stm) /= N_Block_Statement;
7038 exit when not Exception_Junk (Stm);
7041 exit when Nkind (Stm) /= N_Label;
7042 exit when not Exception_Junk (Stm);
7043 Check_Statement_Sequence
7044 (Statements (Handled_Statement_Sequence (Next (Stm))));
7049 exit when Nkind (Stm) /= N_Goto_Statement;
7050 exit when not Exception_Junk (Stm);
7054 -- Don't count pragmas
7056 while Nkind (Last_Stm) = N_Pragma
7058 -- Don't count call to SS_Release (can happen after Raise_Exception)
7061 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7063 Nkind (Name (Last_Stm)) = N_Identifier
7065 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7067 -- Don't count exception junk
7070 (Nkind_In (Last_Stm, N_Goto_Statement,
7072 N_Object_Declaration)
7073 and then Exception_Junk (Last_Stm))
7074 or else Nkind (Last_Stm) in N_Push_xxx_Label
7075 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7077 -- Inserted code, such as finalization calls, is irrelevant: we only
7078 -- need to check original source.
7080 or else Is_Rewrite_Insertion (Last_Stm)
7085 -- Here we have the "real" last statement
7087 Kind := Nkind (Last_Stm);
7089 -- Transfer of control, OK. Note that in the No_Return procedure
7090 -- case, we already diagnosed any explicit return statements, so
7091 -- we can treat them as OK in this context.
7093 if Is_Transfer (Last_Stm) then
7096 -- Check cases of explicit non-indirect procedure calls
7098 elsif Kind = N_Procedure_Call_Statement
7099 and then Is_Entity_Name (Name (Last_Stm))
7101 -- Check call to Raise_Exception procedure which is treated
7102 -- specially, as is a call to Reraise_Occurrence.
7104 -- We suppress the warning in these cases since it is likely that
7105 -- the programmer really does not expect to deal with the case
7106 -- of Null_Occurrence, and thus would find a warning about a
7107 -- missing return curious, and raising Program_Error does not
7108 -- seem such a bad behavior if this does occur.
7110 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7111 -- behavior will be to raise Constraint_Error (see AI-329).
7113 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7115 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7117 Raise_Exception_Call := True;
7119 -- For Raise_Exception call, test first argument, if it is
7120 -- an attribute reference for a 'Identity call, then we know
7121 -- that the call cannot possibly return.
7124 Arg : constant Node_Id :=
7125 Original_Node (First_Actual (Last_Stm));
7127 if Nkind (Arg) = N_Attribute_Reference
7128 and then Attribute_Name (Arg) = Name_Identity
7135 -- If statement, need to look inside if there is an else and check
7136 -- each constituent statement sequence for proper termination.
7138 elsif Kind = N_If_Statement
7139 and then Present (Else_Statements (Last_Stm))
7141 Check_Statement_Sequence (Then_Statements (Last_Stm));
7142 Check_Statement_Sequence (Else_Statements (Last_Stm));
7144 if Present (Elsif_Parts (Last_Stm)) then
7146 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7149 while Present (Elsif_Part) loop
7150 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7158 -- Case statement, check each case for proper termination
7160 elsif Kind = N_Case_Statement then
7164 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7165 while Present (Case_Alt) loop
7166 Check_Statement_Sequence (Statements (Case_Alt));
7167 Next_Non_Pragma (Case_Alt);
7173 -- Block statement, check its handled sequence of statements
7175 elsif Kind = N_Block_Statement then
7181 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7190 -- Loop statement. If there is an iteration scheme, we can definitely
7191 -- fall out of the loop. Similarly if there is an exit statement, we
7192 -- can fall out. In either case we need a following return.
7194 elsif Kind = N_Loop_Statement then
7195 if Present (Iteration_Scheme (Last_Stm))
7196 or else Has_Exit (Entity (Identifier (Last_Stm)))
7200 -- A loop with no exit statement or iteration scheme is either
7201 -- an infinite loop, or it has some other exit (raise/return).
7202 -- In either case, no warning is required.
7208 -- Timed entry call, check entry call and delay alternatives
7210 -- Note: in expanded code, the timed entry call has been converted
7211 -- to a set of expanded statements on which the check will work
7212 -- correctly in any case.
7214 elsif Kind = N_Timed_Entry_Call then
7216 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7217 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7220 -- If statement sequence of entry call alternative is missing,
7221 -- then we can definitely fall through, and we post the error
7222 -- message on the entry call alternative itself.
7224 if No (Statements (ECA)) then
7227 -- If statement sequence of delay alternative is missing, then
7228 -- we can definitely fall through, and we post the error
7229 -- message on the delay alternative itself.
7231 -- Note: if both ECA and DCA are missing the return, then we
7232 -- post only one message, should be enough to fix the bugs.
7233 -- If not we will get a message next time on the DCA when the
7236 elsif No (Statements (DCA)) then
7239 -- Else check both statement sequences
7242 Check_Statement_Sequence (Statements (ECA));
7243 Check_Statement_Sequence (Statements (DCA));
7248 -- Conditional entry call, check entry call and else part
7250 -- Note: in expanded code, the conditional entry call has been
7251 -- converted to a set of expanded statements on which the check
7252 -- will work correctly in any case.
7254 elsif Kind = N_Conditional_Entry_Call then
7256 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7259 -- If statement sequence of entry call alternative is missing,
7260 -- then we can definitely fall through, and we post the error
7261 -- message on the entry call alternative itself.
7263 if No (Statements (ECA)) then
7266 -- Else check statement sequence and else part
7269 Check_Statement_Sequence (Statements (ECA));
7270 Check_Statement_Sequence (Else_Statements (Last_Stm));
7276 -- If we fall through, issue appropriate message
7279 if not Raise_Exception_Call then
7281 ("RETURN statement missing following this statement??!",
7284 ("\Program_Error may be raised at run time??!",
7288 -- Note: we set Err even though we have not issued a warning
7289 -- because we still have a case of a missing return. This is
7290 -- an extremely marginal case, probably will never be noticed
7291 -- but we might as well get it right.
7295 -- Otherwise we have the case of a procedure marked No_Return
7298 if not Raise_Exception_Call then
7300 ("implied return after this statement " &
7301 "will raise Program_Error??",
7304 ("\procedure & is marked as No_Return??!",
7309 RE : constant Node_Id :=
7310 Make_Raise_Program_Error (Sloc (Last_Stm),
7311 Reason => PE_Implicit_Return);
7313 Insert_After (Last_Stm, RE);
7317 end Check_Statement_Sequence;
7319 -- Start of processing for Check_Returns
7323 Check_Statement_Sequence (Statements (HSS));
7325 if Present (Exception_Handlers (HSS)) then
7326 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7327 while Present (Handler) loop
7328 Check_Statement_Sequence (Statements (Handler));
7329 Next_Non_Pragma (Handler);
7334 ----------------------------
7335 -- Check_Subprogram_Order --
7336 ----------------------------
7338 procedure Check_Subprogram_Order (N : Node_Id) is
7340 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7341 -- This is used to check if S1 > S2 in the sense required by this test,
7342 -- for example nameab < namec, but name2 < name10.
7344 -----------------------------
7345 -- Subprogram_Name_Greater --
7346 -----------------------------
7348 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7353 -- Deal with special case where names are identical except for a
7354 -- numerical suffix. These are handled specially, taking the numeric
7355 -- ordering from the suffix into account.
7358 while S1 (L1) in '0' .. '9' loop
7363 while S2 (L2) in '0' .. '9' loop
7367 -- If non-numeric parts non-equal, do straight compare
7369 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7372 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7373 -- that a missing suffix is treated as numeric zero in this test.
7377 while L1 < S1'Last loop
7379 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7383 while L2 < S2'Last loop
7385 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7390 end Subprogram_Name_Greater;
7392 -- Start of processing for Check_Subprogram_Order
7395 -- Check body in alpha order if this is option
7398 and then Style_Check_Order_Subprograms
7399 and then Nkind (N) = N_Subprogram_Body
7400 and then Comes_From_Source (N)
7401 and then In_Extended_Main_Source_Unit (N)
7405 renames Scope_Stack.Table
7406 (Scope_Stack.Last).Last_Subprogram_Name;
7408 Body_Id : constant Entity_Id :=
7409 Defining_Entity (Specification (N));
7412 Get_Decoded_Name_String (Chars (Body_Id));
7415 if Subprogram_Name_Greater
7416 (LSN.all, Name_Buffer (1 .. Name_Len))
7418 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7424 LSN := new String'(Name_Buffer (1 .. Name_Len));
7427 end Check_Subprogram_Order;
7429 ------------------------------
7430 -- Check_Subtype_Conformant --
7431 ------------------------------
7433 procedure Check_Subtype_Conformant
7434 (New_Id : Entity_Id;
7436 Err_Loc : Node_Id := Empty;
7437 Skip_Controlling_Formals : Boolean := False;
7438 Get_Inst : Boolean := False)
7441 pragma Warnings (Off, Result);
7444 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7445 Skip_Controlling_Formals => Skip_Controlling_Formals,
7446 Get_Inst => Get_Inst);
7447 end Check_Subtype_Conformant;
7449 ---------------------------
7450 -- Check_Type_Conformant --
7451 ---------------------------
7453 procedure Check_Type_Conformant
7454 (New_Id : Entity_Id;
7456 Err_Loc : Node_Id := Empty)
7459 pragma Warnings (Off, Result);
7462 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7463 end Check_Type_Conformant;
7465 ---------------------------
7466 -- Can_Override_Operator --
7467 ---------------------------
7469 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7473 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7477 Typ := Base_Type (Etype (First_Formal (Subp)));
7479 -- Check explicitly that the operation is a primitive of the type
7481 return Operator_Matches_Spec (Subp, Subp)
7482 and then not Is_Generic_Type (Typ)
7483 and then Scope (Subp) = Scope (Typ)
7484 and then not Is_Class_Wide_Type (Typ);
7486 end Can_Override_Operator;
7488 ----------------------
7489 -- Conforming_Types --
7490 ----------------------
7492 function Conforming_Types
7495 Ctype : Conformance_Type;
7496 Get_Inst : Boolean := False) return Boolean
7498 Type_1 : Entity_Id := T1;
7499 Type_2 : Entity_Id := T2;
7500 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7502 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7503 -- If neither T1 nor T2 are generic actual types, or if they are in
7504 -- different scopes (e.g. parent and child instances), then verify that
7505 -- the base types are equal. Otherwise T1 and T2 must be on the same
7506 -- subtype chain. The whole purpose of this procedure is to prevent
7507 -- spurious ambiguities in an instantiation that may arise if two
7508 -- distinct generic types are instantiated with the same actual.
7510 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7511 -- An access parameter can designate an incomplete type. If the
7512 -- incomplete type is the limited view of a type from a limited_
7513 -- with_clause, check whether the non-limited view is available. If
7514 -- it is a (non-limited) incomplete type, get the full view.
7516 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7517 -- Returns True if and only if either T1 denotes a limited view of T2
7518 -- or T2 denotes a limited view of T1. This can arise when the limited
7519 -- with view of a type is used in a subprogram declaration and the
7520 -- subprogram body is in the scope of a regular with clause for the
7521 -- same unit. In such a case, the two type entities can be considered
7522 -- identical for purposes of conformance checking.
7524 ----------------------
7525 -- Base_Types_Match --
7526 ----------------------
7528 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7529 BT1 : constant Entity_Id := Base_Type (T1);
7530 BT2 : constant Entity_Id := Base_Type (T2);
7536 elsif BT1 = BT2 then
7538 -- The following is too permissive. A more precise test should
7539 -- check that the generic actual is an ancestor subtype of the
7542 -- See code in Find_Corresponding_Spec that applies an additional
7543 -- filter to handle accidental amiguities in instances.
7545 return not Is_Generic_Actual_Type (T1)
7546 or else not Is_Generic_Actual_Type (T2)
7547 or else Scope (T1) /= Scope (T2);
7549 -- If T2 is a generic actual type it is declared as the subtype of
7550 -- the actual. If that actual is itself a subtype we need to use
7551 -- its own base type to check for compatibility.
7553 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7556 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7562 end Base_Types_Match;
7564 --------------------------
7565 -- Find_Designated_Type --
7566 --------------------------
7568 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7572 Desig := Directly_Designated_Type (T);
7574 if Ekind (Desig) = E_Incomplete_Type then
7576 -- If regular incomplete type, get full view if available
7578 if Present (Full_View (Desig)) then
7579 Desig := Full_View (Desig);
7581 -- If limited view of a type, get non-limited view if available,
7582 -- and check again for a regular incomplete type.
7584 elsif Present (Non_Limited_View (Desig)) then
7585 Desig := Get_Full_View (Non_Limited_View (Desig));
7590 end Find_Designated_Type;
7592 -------------------------------
7593 -- Matches_Limited_With_View --
7594 -------------------------------
7596 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7598 -- In some cases a type imported through a limited_with clause, and
7599 -- its nonlimited view are both visible, for example in an anonymous
7600 -- access-to-class-wide type in a formal. Both entities designate the
7603 if From_With_Type (T1) and then T2 = Available_View (T1) then
7606 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7609 elsif From_With_Type (T1)
7610 and then From_With_Type (T2)
7611 and then Available_View (T1) = Available_View (T2)
7618 end Matches_Limited_With_View;
7620 -- Start of processing for Conforming_Types
7623 -- The context is an instance association for a formal access-to-
7624 -- subprogram type; the formal parameter types require mapping because
7625 -- they may denote other formal parameters of the generic unit.
7628 Type_1 := Get_Instance_Of (T1);
7629 Type_2 := Get_Instance_Of (T2);
7632 -- If one of the types is a view of the other introduced by a limited
7633 -- with clause, treat these as conforming for all purposes.
7635 if Matches_Limited_With_View (T1, T2) then
7638 elsif Base_Types_Match (Type_1, Type_2) then
7639 return Ctype <= Mode_Conformant
7640 or else Subtypes_Statically_Match (Type_1, Type_2);
7642 elsif Is_Incomplete_Or_Private_Type (Type_1)
7643 and then Present (Full_View (Type_1))
7644 and then Base_Types_Match (Full_View (Type_1), Type_2)
7646 return Ctype <= Mode_Conformant
7647 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7649 elsif Ekind (Type_2) = E_Incomplete_Type
7650 and then Present (Full_View (Type_2))
7651 and then Base_Types_Match (Type_1, Full_View (Type_2))
7653 return Ctype <= Mode_Conformant
7654 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7656 elsif Is_Private_Type (Type_2)
7657 and then In_Instance
7658 and then Present (Full_View (Type_2))
7659 and then Base_Types_Match (Type_1, Full_View (Type_2))
7661 return Ctype <= Mode_Conformant
7662 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7665 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7666 -- treated recursively because they carry a signature.
7668 Are_Anonymous_Access_To_Subprogram_Types :=
7669 Ekind (Type_1) = Ekind (Type_2)
7671 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7672 E_Anonymous_Access_Protected_Subprogram_Type);
7674 -- Test anonymous access type case. For this case, static subtype
7675 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7676 -- the base types because we may have built internal subtype entities
7677 -- to handle null-excluding types (see Process_Formals).
7679 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7681 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7683 -- Ada 2005 (AI-254)
7685 or else Are_Anonymous_Access_To_Subprogram_Types
7688 Desig_1 : Entity_Id;
7689 Desig_2 : Entity_Id;
7692 -- In Ada 2005, access constant indicators must match for
7693 -- subtype conformance.
7695 if Ada_Version >= Ada_2005
7696 and then Ctype >= Subtype_Conformant
7698 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7703 Desig_1 := Find_Designated_Type (Type_1);
7704 Desig_2 := Find_Designated_Type (Type_2);
7706 -- If the context is an instance association for a formal
7707 -- access-to-subprogram type; formal access parameter designated
7708 -- types require mapping because they may denote other formal
7709 -- parameters of the generic unit.
7712 Desig_1 := Get_Instance_Of (Desig_1);
7713 Desig_2 := Get_Instance_Of (Desig_2);
7716 -- It is possible for a Class_Wide_Type to be introduced for an
7717 -- incomplete type, in which case there is a separate class_ wide
7718 -- type for the full view. The types conform if their Etypes
7719 -- conform, i.e. one may be the full view of the other. This can
7720 -- only happen in the context of an access parameter, other uses
7721 -- of an incomplete Class_Wide_Type are illegal.
7723 if Is_Class_Wide_Type (Desig_1)
7725 Is_Class_Wide_Type (Desig_2)
7729 (Etype (Base_Type (Desig_1)),
7730 Etype (Base_Type (Desig_2)), Ctype);
7732 elsif Are_Anonymous_Access_To_Subprogram_Types then
7733 if Ada_Version < Ada_2005 then
7734 return Ctype = Type_Conformant
7736 Subtypes_Statically_Match (Desig_1, Desig_2);
7738 -- We must check the conformance of the signatures themselves
7742 Conformant : Boolean;
7745 (Desig_1, Desig_2, Ctype, False, Conformant);
7751 return Base_Type (Desig_1) = Base_Type (Desig_2)
7752 and then (Ctype = Type_Conformant
7754 Subtypes_Statically_Match (Desig_1, Desig_2));
7758 -- Otherwise definitely no match
7761 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7762 and then Is_Access_Type (Type_2))
7763 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7764 and then Is_Access_Type (Type_1)))
7767 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7769 May_Hide_Profile := True;
7774 end Conforming_Types;
7776 --------------------------
7777 -- Create_Extra_Formals --
7778 --------------------------
7780 procedure Create_Extra_Formals (E : Entity_Id) is
7782 First_Extra : Entity_Id := Empty;
7783 Last_Extra : Entity_Id;
7784 Formal_Type : Entity_Id;
7785 P_Formal : Entity_Id := Empty;
7787 function Add_Extra_Formal
7788 (Assoc_Entity : Entity_Id;
7791 Suffix : String) return Entity_Id;
7792 -- Add an extra formal to the current list of formals and extra formals.
7793 -- The extra formal is added to the end of the list of extra formals,
7794 -- and also returned as the result. These formals are always of mode IN.
7795 -- The new formal has the type Typ, is declared in Scope, and its name
7796 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7797 -- The following suffixes are currently used. They should not be changed
7798 -- without coordinating with CodePeer, which makes use of these to
7799 -- provide better messages.
7801 -- O denotes the Constrained bit.
7802 -- L denotes the accessibility level.
7803 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7804 -- the full list in exp_ch6.BIP_Formal_Kind.
7806 ----------------------
7807 -- Add_Extra_Formal --
7808 ----------------------
7810 function Add_Extra_Formal
7811 (Assoc_Entity : Entity_Id;
7814 Suffix : String) return Entity_Id
7816 EF : constant Entity_Id :=
7817 Make_Defining_Identifier (Sloc (Assoc_Entity),
7818 Chars => New_External_Name (Chars (Assoc_Entity),
7822 -- A little optimization. Never generate an extra formal for the
7823 -- _init operand of an initialization procedure, since it could
7826 if Chars (Formal) = Name_uInit then
7830 Set_Ekind (EF, E_In_Parameter);
7831 Set_Actual_Subtype (EF, Typ);
7832 Set_Etype (EF, Typ);
7833 Set_Scope (EF, Scope);
7834 Set_Mechanism (EF, Default_Mechanism);
7835 Set_Formal_Validity (EF);
7837 if No (First_Extra) then
7839 Set_Extra_Formals (Scope, First_Extra);
7842 if Present (Last_Extra) then
7843 Set_Extra_Formal (Last_Extra, EF);
7849 end Add_Extra_Formal;
7851 -- Start of processing for Create_Extra_Formals
7854 -- We never generate extra formals if expansion is not active because we
7855 -- don't need them unless we are generating code.
7857 if not Expander_Active then
7861 -- No need to generate extra formals in interface thunks whose target
7862 -- primitive has no extra formals.
7864 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7868 -- If this is a derived subprogram then the subtypes of the parent
7869 -- subprogram's formal parameters will be used to determine the need
7870 -- for extra formals.
7872 if Is_Overloadable (E) and then Present (Alias (E)) then
7873 P_Formal := First_Formal (Alias (E));
7876 Last_Extra := Empty;
7877 Formal := First_Formal (E);
7878 while Present (Formal) loop
7879 Last_Extra := Formal;
7880 Next_Formal (Formal);
7883 -- If Extra_formals were already created, don't do it again. This
7884 -- situation may arise for subprogram types created as part of
7885 -- dispatching calls (see Expand_Dispatching_Call)
7887 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7891 -- If the subprogram is a predefined dispatching subprogram then don't
7892 -- generate any extra constrained or accessibility level formals. In
7893 -- general we suppress these for internal subprograms (by not calling
7894 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7895 -- generated stream attributes do get passed through because extra
7896 -- build-in-place formals are needed in some cases (limited 'Input).
7898 if Is_Predefined_Internal_Operation (E) then
7899 goto Test_For_Func_Result_Extras;
7902 Formal := First_Formal (E);
7903 while Present (Formal) loop
7905 -- Create extra formal for supporting the attribute 'Constrained.
7906 -- The case of a private type view without discriminants also
7907 -- requires the extra formal if the underlying type has defaulted
7910 if Ekind (Formal) /= E_In_Parameter then
7911 if Present (P_Formal) then
7912 Formal_Type := Etype (P_Formal);
7914 Formal_Type := Etype (Formal);
7917 -- Do not produce extra formals for Unchecked_Union parameters.
7918 -- Jump directly to the end of the loop.
7920 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7921 goto Skip_Extra_Formal_Generation;
7924 if not Has_Discriminants (Formal_Type)
7925 and then Ekind (Formal_Type) in Private_Kind
7926 and then Present (Underlying_Type (Formal_Type))
7928 Formal_Type := Underlying_Type (Formal_Type);
7931 -- Suppress the extra formal if formal's subtype is constrained or
7932 -- indefinite, or we're compiling for Ada 2012 and the underlying
7933 -- type is tagged and limited. In Ada 2012, a limited tagged type
7934 -- can have defaulted discriminants, but 'Constrained is required
7935 -- to return True, so the formal is never needed (see AI05-0214).
7936 -- Note that this ensures consistency of calling sequences for
7937 -- dispatching operations when some types in a class have defaults
7938 -- on discriminants and others do not (and requiring the extra
7939 -- formal would introduce distributed overhead).
7941 -- If the type does not have a completion yet, treat as prior to
7942 -- Ada 2012 for consistency.
7944 if Has_Discriminants (Formal_Type)
7945 and then not Is_Constrained (Formal_Type)
7946 and then not Is_Indefinite_Subtype (Formal_Type)
7947 and then (Ada_Version < Ada_2012
7948 or else No (Underlying_Type (Formal_Type))
7950 (Is_Limited_Type (Formal_Type)
7953 (Underlying_Type (Formal_Type)))))
7955 Set_Extra_Constrained
7956 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7960 -- Create extra formal for supporting accessibility checking. This
7961 -- is done for both anonymous access formals and formals of named
7962 -- access types that are marked as controlling formals. The latter
7963 -- case can occur when Expand_Dispatching_Call creates a subprogram
7964 -- type and substitutes the types of access-to-class-wide actuals
7965 -- for the anonymous access-to-specific-type of controlling formals.
7966 -- Base_Type is applied because in cases where there is a null
7967 -- exclusion the formal may have an access subtype.
7969 -- This is suppressed if we specifically suppress accessibility
7970 -- checks at the package level for either the subprogram, or the
7971 -- package in which it resides. However, we do not suppress it
7972 -- simply if the scope has accessibility checks suppressed, since
7973 -- this could cause trouble when clients are compiled with a
7974 -- different suppression setting. The explicit checks at the
7975 -- package level are safe from this point of view.
7977 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7978 or else (Is_Controlling_Formal (Formal)
7979 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7981 (Explicit_Suppress (E, Accessibility_Check)
7983 Explicit_Suppress (Scope (E), Accessibility_Check))
7986 or else Present (Extra_Accessibility (P_Formal)))
7988 Set_Extra_Accessibility
7989 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7992 -- This label is required when skipping extra formal generation for
7993 -- Unchecked_Union parameters.
7995 <<Skip_Extra_Formal_Generation>>
7997 if Present (P_Formal) then
7998 Next_Formal (P_Formal);
8001 Next_Formal (Formal);
8004 <<Test_For_Func_Result_Extras>>
8006 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8007 -- function call is ... determined by the point of call ...".
8009 if Needs_Result_Accessibility_Level (E) then
8010 Set_Extra_Accessibility_Of_Result
8011 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8014 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8015 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8017 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8019 Result_Subt : constant Entity_Id := Etype (E);
8020 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8021 Formal_Typ : Entity_Id;
8023 Discard : Entity_Id;
8024 pragma Warnings (Off, Discard);
8027 -- In the case of functions with unconstrained result subtypes,
8028 -- add a 4-state formal indicating whether the return object is
8029 -- allocated by the caller (1), or should be allocated by the
8030 -- callee on the secondary stack (2), in the global heap (3), or
8031 -- in a user-defined storage pool (4). For the moment we just use
8032 -- Natural for the type of this formal. Note that this formal
8033 -- isn't usually needed in the case where the result subtype is
8034 -- constrained, but it is needed when the function has a tagged
8035 -- result, because generally such functions can be called in a
8036 -- dispatching context and such calls must be handled like calls
8037 -- to a class-wide function.
8039 if Needs_BIP_Alloc_Form (E) then
8042 (E, Standard_Natural,
8043 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8045 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8046 -- use a user-defined pool. This formal is not added on
8047 -- .NET/JVM/ZFP as those targets do not support pools.
8049 if VM_Target = No_VM
8050 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8054 (E, RTE (RE_Root_Storage_Pool_Ptr),
8055 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8059 -- In the case of functions whose result type needs finalization,
8060 -- add an extra formal which represents the finalization master.
8062 if Needs_BIP_Finalization_Master (E) then
8065 (E, RTE (RE_Finalization_Master_Ptr),
8066 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8069 -- When the result type contains tasks, add two extra formals: the
8070 -- master of the tasks to be created, and the caller's activation
8073 if Has_Task (Full_Subt) then
8076 (E, RTE (RE_Master_Id),
8077 E, BIP_Formal_Suffix (BIP_Task_Master));
8080 (E, RTE (RE_Activation_Chain_Access),
8081 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8084 -- All build-in-place functions get an extra formal that will be
8085 -- passed the address of the return object within the caller.
8088 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8090 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8091 Set_Etype (Formal_Typ, Formal_Typ);
8092 Set_Depends_On_Private
8093 (Formal_Typ, Has_Private_Component (Formal_Typ));
8094 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8095 Set_Is_Access_Constant (Formal_Typ, False);
8097 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8098 -- the designated type comes from the limited view (for back-end
8101 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
8103 Layout_Type (Formal_Typ);
8107 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8110 end Create_Extra_Formals;
8112 -----------------------------
8113 -- Enter_Overloaded_Entity --
8114 -----------------------------
8116 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8117 E : Entity_Id := Current_Entity_In_Scope (S);
8118 C_E : Entity_Id := Current_Entity (S);
8122 Set_Has_Homonym (E);
8123 Set_Has_Homonym (S);
8126 Set_Is_Immediately_Visible (S);
8127 Set_Scope (S, Current_Scope);
8129 -- Chain new entity if front of homonym in current scope, so that
8130 -- homonyms are contiguous.
8132 if Present (E) and then E /= C_E then
8133 while Homonym (C_E) /= E loop
8134 C_E := Homonym (C_E);
8137 Set_Homonym (C_E, S);
8141 Set_Current_Entity (S);
8146 if Is_Inherited_Operation (S) then
8147 Append_Inherited_Subprogram (S);
8149 Append_Entity (S, Current_Scope);
8152 Set_Public_Status (S);
8154 if Debug_Flag_E then
8155 Write_Str ("New overloaded entity chain: ");
8156 Write_Name (Chars (S));
8159 while Present (E) loop
8160 Write_Str (" "); Write_Int (Int (E));
8167 -- Generate warning for hiding
8170 and then Comes_From_Source (S)
8171 and then In_Extended_Main_Source_Unit (S)
8178 -- Warn unless genuine overloading. Do not emit warning on
8179 -- hiding predefined operators in Standard (these are either an
8180 -- (artifact of our implicit declarations, or simple noise) but
8181 -- keep warning on a operator defined on a local subtype, because
8182 -- of the real danger that different operators may be applied in
8183 -- various parts of the program.
8185 -- Note that if E and S have the same scope, there is never any
8186 -- hiding. Either the two conflict, and the program is illegal,
8187 -- or S is overriding an implicit inherited subprogram.
8189 if Scope (E) /= Scope (S)
8190 and then (not Is_Overloadable (E)
8191 or else Subtype_Conformant (E, S))
8192 and then (Is_Immediately_Visible (E)
8194 Is_Potentially_Use_Visible (S))
8196 if Scope (E) /= Standard_Standard then
8197 Error_Msg_Sloc := Sloc (E);
8198 Error_Msg_N ("declaration of & hides one#?h?", S);
8200 elsif Nkind (S) = N_Defining_Operator_Symbol
8202 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8205 ("declaration of & hides predefined operator?h?", S);
8210 end Enter_Overloaded_Entity;
8212 -----------------------------
8213 -- Check_Untagged_Equality --
8214 -----------------------------
8216 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8217 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8218 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8222 if Nkind (Decl) = N_Subprogram_Declaration
8223 and then Is_Record_Type (Typ)
8224 and then not Is_Tagged_Type (Typ)
8226 -- If the type is not declared in a package, or if we are in the
8227 -- body of the package or in some other scope, the new operation is
8228 -- not primitive, and therefore legal, though suspicious. If the
8229 -- type is a generic actual (sub)type, the operation is not primitive
8230 -- either because the base type is declared elsewhere.
8232 if Is_Frozen (Typ) then
8233 if Ekind (Scope (Typ)) /= E_Package
8234 or else Scope (Typ) /= Current_Scope
8238 elsif Is_Generic_Actual_Type (Typ) then
8241 elsif In_Package_Body (Scope (Typ)) then
8243 ("equality operator must be declared "
8244 & "before type& is frozen", Eq_Op, Typ);
8246 ("\move declaration to package spec", Eq_Op);
8250 ("equality operator must be declared "
8251 & "before type& is frozen", Eq_Op, Typ);
8253 Obj_Decl := Next (Parent (Typ));
8254 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8255 if Nkind (Obj_Decl) = N_Object_Declaration
8256 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8259 ("type& is frozen by declaration??", Obj_Decl, Typ);
8261 ("\an equality operator cannot be declared after this "
8262 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8270 elsif not In_Same_List (Parent (Typ), Decl)
8271 and then not Is_Limited_Type (Typ)
8274 -- This makes it illegal to have a primitive equality declared in
8275 -- the private part if the type is visible.
8277 Error_Msg_N ("equality operator appears too late", Eq_Op);
8280 end Check_Untagged_Equality;
8282 -----------------------------
8283 -- Find_Corresponding_Spec --
8284 -----------------------------
8286 function Find_Corresponding_Spec
8288 Post_Error : Boolean := True) return Entity_Id
8290 Spec : constant Node_Id := Specification (N);
8291 Designator : constant Entity_Id := Defining_Entity (Spec);
8295 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8296 -- Even if fully conformant, a body may depend on a generic actual when
8297 -- the spec does not, or vice versa, in which case they were distinct
8298 -- entities in the generic.
8300 -------------------------------
8301 -- Different_Generic_Profile --
8302 -------------------------------
8304 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8308 if Ekind (E) = E_Function
8309 and then Is_Generic_Actual_Type (Etype (E)) /=
8310 Is_Generic_Actual_Type (Etype (Designator))
8315 F1 := First_Formal (Designator);
8316 F2 := First_Formal (E);
8317 while Present (F1) loop
8318 if Is_Generic_Actual_Type (Etype (F1)) /=
8319 Is_Generic_Actual_Type (Etype (F2))
8329 end Different_Generic_Profile;
8331 -- Start of processing for Find_Corresponding_Spec
8334 E := Current_Entity (Designator);
8335 while Present (E) loop
8337 -- We are looking for a matching spec. It must have the same scope,
8338 -- and the same name, and either be type conformant, or be the case
8339 -- of a library procedure spec and its body (which belong to one
8340 -- another regardless of whether they are type conformant or not).
8342 if Scope (E) = Current_Scope then
8343 if Current_Scope = Standard_Standard
8344 or else (Ekind (E) = Ekind (Designator)
8345 and then Type_Conformant (E, Designator))
8347 -- Within an instantiation, we know that spec and body are
8348 -- subtype conformant, because they were subtype conformant in
8349 -- the generic. We choose the subtype-conformant entity here as
8350 -- well, to resolve spurious ambiguities in the instance that
8351 -- were not present in the generic (i.e. when two different
8352 -- types are given the same actual). If we are looking for a
8353 -- spec to match a body, full conformance is expected.
8356 Set_Convention (Designator, Convention (E));
8358 -- Skip past subprogram bodies and subprogram renamings that
8359 -- may appear to have a matching spec, but that aren't fully
8360 -- conformant with it. That can occur in cases where an
8361 -- actual type causes unrelated homographs in the instance.
8363 if Nkind_In (N, N_Subprogram_Body,
8364 N_Subprogram_Renaming_Declaration)
8365 and then Present (Homonym (E))
8366 and then not Fully_Conformant (Designator, E)
8370 elsif not Subtype_Conformant (Designator, E) then
8373 elsif Different_Generic_Profile (E) then
8378 -- Ada 2012 (AI05-0165): For internally generated bodies of
8379 -- null procedures locate the internally generated spec. We
8380 -- enforce mode conformance since a tagged type may inherit
8381 -- from interfaces several null primitives which differ only
8382 -- in the mode of the formals.
8384 if not (Comes_From_Source (E))
8385 and then Is_Null_Procedure (E)
8386 and then not Mode_Conformant (Designator, E)
8390 -- For null procedures coming from source that are completions,
8391 -- analysis of the generated body will establish the link.
8393 elsif Comes_From_Source (E)
8394 and then Nkind (Spec) = N_Procedure_Specification
8395 and then Null_Present (Spec)
8399 elsif not Has_Completion (E) then
8400 if Nkind (N) /= N_Subprogram_Body_Stub then
8401 Set_Corresponding_Spec (N, E);
8404 Set_Has_Completion (E);
8407 elsif Nkind (Parent (N)) = N_Subunit then
8409 -- If this is the proper body of a subunit, the completion
8410 -- flag is set when analyzing the stub.
8414 -- If E is an internal function with a controlling result that
8415 -- was created for an operation inherited by a null extension,
8416 -- it may be overridden by a body without a previous spec (one
8417 -- more reason why these should be shunned). In that case
8418 -- remove the generated body if present, because the current
8419 -- one is the explicit overriding.
8421 elsif Ekind (E) = E_Function
8422 and then Ada_Version >= Ada_2005
8423 and then not Comes_From_Source (E)
8424 and then Has_Controlling_Result (E)
8425 and then Is_Null_Extension (Etype (E))
8426 and then Comes_From_Source (Spec)
8428 Set_Has_Completion (E, False);
8431 and then Nkind (Parent (E)) = N_Function_Specification
8434 (Unit_Declaration_Node
8435 (Corresponding_Body (Unit_Declaration_Node (E))));
8439 -- If expansion is disabled, or if the wrapper function has
8440 -- not been generated yet, this a late body overriding an
8441 -- inherited operation, or it is an overriding by some other
8442 -- declaration before the controlling result is frozen. In
8443 -- either case this is a declaration of a new entity.
8449 -- If the body already exists, then this is an error unless
8450 -- the previous declaration is the implicit declaration of a
8451 -- derived subprogram. It is also legal for an instance to
8452 -- contain type conformant overloadable declarations (but the
8453 -- generic declaration may not), per 8.3(26/2).
8455 elsif No (Alias (E))
8456 and then not Is_Intrinsic_Subprogram (E)
8457 and then not In_Instance
8460 Error_Msg_Sloc := Sloc (E);
8462 if Is_Imported (E) then
8464 ("body not allowed for imported subprogram & declared#",
8467 Error_Msg_NE ("duplicate body for & declared#", N, E);
8471 -- Child units cannot be overloaded, so a conformance mismatch
8472 -- between body and a previous spec is an error.
8474 elsif Is_Child_Unit (E)
8476 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8478 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8483 ("body of child unit does not match previous declaration", N);
8491 -- On exit, we know that no previous declaration of subprogram exists
8494 end Find_Corresponding_Spec;
8496 ----------------------
8497 -- Fully_Conformant --
8498 ----------------------
8500 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8503 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8505 end Fully_Conformant;
8507 ----------------------------------
8508 -- Fully_Conformant_Expressions --
8509 ----------------------------------
8511 function Fully_Conformant_Expressions
8512 (Given_E1 : Node_Id;
8513 Given_E2 : Node_Id) return Boolean
8515 E1 : constant Node_Id := Original_Node (Given_E1);
8516 E2 : constant Node_Id := Original_Node (Given_E2);
8517 -- We always test conformance on original nodes, since it is possible
8518 -- for analysis and/or expansion to make things look as though they
8519 -- conform when they do not, e.g. by converting 1+2 into 3.
8521 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8522 renames Fully_Conformant_Expressions;
8524 function FCL (L1, L2 : List_Id) return Boolean;
8525 -- Compare elements of two lists for conformance. Elements have to be
8526 -- conformant, and actuals inserted as default parameters do not match
8527 -- explicit actuals with the same value.
8529 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8530 -- Compare an operator node with a function call
8536 function FCL (L1, L2 : List_Id) return Boolean is
8540 if L1 = No_List then
8546 if L2 = No_List then
8552 -- Compare two lists, skipping rewrite insertions (we want to compare
8553 -- the original trees, not the expanded versions!)
8556 if Is_Rewrite_Insertion (N1) then
8558 elsif Is_Rewrite_Insertion (N2) then
8564 elsif not FCE (N1, N2) then
8577 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8578 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8583 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8588 Act := First (Actuals);
8590 if Nkind (Op_Node) in N_Binary_Op then
8591 if not FCE (Left_Opnd (Op_Node), Act) then
8598 return Present (Act)
8599 and then FCE (Right_Opnd (Op_Node), Act)
8600 and then No (Next (Act));
8604 -- Start of processing for Fully_Conformant_Expressions
8607 -- Non-conformant if paren count does not match. Note: if some idiot
8608 -- complains that we don't do this right for more than 3 levels of
8609 -- parentheses, they will be treated with the respect they deserve!
8611 if Paren_Count (E1) /= Paren_Count (E2) then
8614 -- If same entities are referenced, then they are conformant even if
8615 -- they have different forms (RM 8.3.1(19-20)).
8617 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8618 if Present (Entity (E1)) then
8619 return Entity (E1) = Entity (E2)
8620 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8621 and then Ekind (Entity (E1)) = E_Discriminant
8622 and then Ekind (Entity (E2)) = E_In_Parameter);
8624 elsif Nkind (E1) = N_Expanded_Name
8625 and then Nkind (E2) = N_Expanded_Name
8626 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8627 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8629 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8632 -- Identifiers in component associations don't always have
8633 -- entities, but their names must conform.
8635 return Nkind (E1) = N_Identifier
8636 and then Nkind (E2) = N_Identifier
8637 and then Chars (E1) = Chars (E2);
8640 elsif Nkind (E1) = N_Character_Literal
8641 and then Nkind (E2) = N_Expanded_Name
8643 return Nkind (Selector_Name (E2)) = N_Character_Literal
8644 and then Chars (E1) = Chars (Selector_Name (E2));
8646 elsif Nkind (E2) = N_Character_Literal
8647 and then Nkind (E1) = N_Expanded_Name
8649 return Nkind (Selector_Name (E1)) = N_Character_Literal
8650 and then Chars (E2) = Chars (Selector_Name (E1));
8652 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8653 return FCO (E1, E2);
8655 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8656 return FCO (E2, E1);
8658 -- Otherwise we must have the same syntactic entity
8660 elsif Nkind (E1) /= Nkind (E2) then
8663 -- At this point, we specialize by node type
8670 FCL (Expressions (E1), Expressions (E2))
8672 FCL (Component_Associations (E1),
8673 Component_Associations (E2));
8676 if Nkind (Expression (E1)) = N_Qualified_Expression
8678 Nkind (Expression (E2)) = N_Qualified_Expression
8680 return FCE (Expression (E1), Expression (E2));
8682 -- Check that the subtype marks and any constraints
8687 Indic1 : constant Node_Id := Expression (E1);
8688 Indic2 : constant Node_Id := Expression (E2);
8693 if Nkind (Indic1) /= N_Subtype_Indication then
8695 Nkind (Indic2) /= N_Subtype_Indication
8696 and then Entity (Indic1) = Entity (Indic2);
8698 elsif Nkind (Indic2) /= N_Subtype_Indication then
8700 Nkind (Indic1) /= N_Subtype_Indication
8701 and then Entity (Indic1) = Entity (Indic2);
8704 if Entity (Subtype_Mark (Indic1)) /=
8705 Entity (Subtype_Mark (Indic2))
8710 Elt1 := First (Constraints (Constraint (Indic1)));
8711 Elt2 := First (Constraints (Constraint (Indic2)));
8712 while Present (Elt1) and then Present (Elt2) loop
8713 if not FCE (Elt1, Elt2) then
8726 when N_Attribute_Reference =>
8728 Attribute_Name (E1) = Attribute_Name (E2)
8729 and then FCL (Expressions (E1), Expressions (E2));
8733 Entity (E1) = Entity (E2)
8734 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8735 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8737 when N_Short_Circuit | N_Membership_Test =>
8739 FCE (Left_Opnd (E1), Left_Opnd (E2))
8741 FCE (Right_Opnd (E1), Right_Opnd (E2));
8743 when N_Case_Expression =>
8749 if not FCE (Expression (E1), Expression (E2)) then
8753 Alt1 := First (Alternatives (E1));
8754 Alt2 := First (Alternatives (E2));
8756 if Present (Alt1) /= Present (Alt2) then
8758 elsif No (Alt1) then
8762 if not FCE (Expression (Alt1), Expression (Alt2))
8763 or else not FCL (Discrete_Choices (Alt1),
8764 Discrete_Choices (Alt2))
8775 when N_Character_Literal =>
8777 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8779 when N_Component_Association =>
8781 FCL (Choices (E1), Choices (E2))
8783 FCE (Expression (E1), Expression (E2));
8785 when N_Explicit_Dereference =>
8787 FCE (Prefix (E1), Prefix (E2));
8789 when N_Extension_Aggregate =>
8791 FCL (Expressions (E1), Expressions (E2))
8792 and then Null_Record_Present (E1) =
8793 Null_Record_Present (E2)
8794 and then FCL (Component_Associations (E1),
8795 Component_Associations (E2));
8797 when N_Function_Call =>
8799 FCE (Name (E1), Name (E2))
8801 FCL (Parameter_Associations (E1),
8802 Parameter_Associations (E2));
8804 when N_If_Expression =>
8806 FCL (Expressions (E1), Expressions (E2));
8808 when N_Indexed_Component =>
8810 FCE (Prefix (E1), Prefix (E2))
8812 FCL (Expressions (E1), Expressions (E2));
8814 when N_Integer_Literal =>
8815 return (Intval (E1) = Intval (E2));
8820 when N_Operator_Symbol =>
8822 Chars (E1) = Chars (E2);
8824 when N_Others_Choice =>
8827 when N_Parameter_Association =>
8829 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8830 and then FCE (Explicit_Actual_Parameter (E1),
8831 Explicit_Actual_Parameter (E2));
8833 when N_Qualified_Expression =>
8835 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8837 FCE (Expression (E1), Expression (E2));
8839 when N_Quantified_Expression =>
8840 if not FCE (Condition (E1), Condition (E2)) then
8844 if Present (Loop_Parameter_Specification (E1))
8845 and then Present (Loop_Parameter_Specification (E2))
8848 L1 : constant Node_Id :=
8849 Loop_Parameter_Specification (E1);
8850 L2 : constant Node_Id :=
8851 Loop_Parameter_Specification (E2);
8855 Reverse_Present (L1) = Reverse_Present (L2)
8857 FCE (Defining_Identifier (L1),
8858 Defining_Identifier (L2))
8860 FCE (Discrete_Subtype_Definition (L1),
8861 Discrete_Subtype_Definition (L2));
8864 elsif Present (Iterator_Specification (E1))
8865 and then Present (Iterator_Specification (E2))
8868 I1 : constant Node_Id := Iterator_Specification (E1);
8869 I2 : constant Node_Id := Iterator_Specification (E2);
8873 FCE (Defining_Identifier (I1),
8874 Defining_Identifier (I2))
8876 Of_Present (I1) = Of_Present (I2)
8878 Reverse_Present (I1) = Reverse_Present (I2)
8879 and then FCE (Name (I1), Name (I2))
8880 and then FCE (Subtype_Indication (I1),
8881 Subtype_Indication (I2));
8884 -- The quantified expressions used different specifications to
8885 -- walk their respective ranges.
8893 FCE (Low_Bound (E1), Low_Bound (E2))
8895 FCE (High_Bound (E1), High_Bound (E2));
8897 when N_Real_Literal =>
8898 return (Realval (E1) = Realval (E2));
8900 when N_Selected_Component =>
8902 FCE (Prefix (E1), Prefix (E2))
8904 FCE (Selector_Name (E1), Selector_Name (E2));
8908 FCE (Prefix (E1), Prefix (E2))
8910 FCE (Discrete_Range (E1), Discrete_Range (E2));
8912 when N_String_Literal =>
8914 S1 : constant String_Id := Strval (E1);
8915 S2 : constant String_Id := Strval (E2);
8916 L1 : constant Nat := String_Length (S1);
8917 L2 : constant Nat := String_Length (S2);
8924 for J in 1 .. L1 loop
8925 if Get_String_Char (S1, J) /=
8926 Get_String_Char (S2, J)
8936 when N_Type_Conversion =>
8938 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8940 FCE (Expression (E1), Expression (E2));
8944 Entity (E1) = Entity (E2)
8946 FCE (Right_Opnd (E1), Right_Opnd (E2));
8948 when N_Unchecked_Type_Conversion =>
8950 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8952 FCE (Expression (E1), Expression (E2));
8954 -- All other node types cannot appear in this context. Strictly
8955 -- we should raise a fatal internal error. Instead we just ignore
8956 -- the nodes. This means that if anyone makes a mistake in the
8957 -- expander and mucks an expression tree irretrievably, the
8958 -- result will be a failure to detect a (probably very obscure)
8959 -- case of non-conformance, which is better than bombing on some
8960 -- case where two expressions do in fact conform.
8967 end Fully_Conformant_Expressions;
8969 ----------------------------------------
8970 -- Fully_Conformant_Discrete_Subtypes --
8971 ----------------------------------------
8973 function Fully_Conformant_Discrete_Subtypes
8974 (Given_S1 : Node_Id;
8975 Given_S2 : Node_Id) return Boolean
8977 S1 : constant Node_Id := Original_Node (Given_S1);
8978 S2 : constant Node_Id := Original_Node (Given_S2);
8980 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8981 -- Special-case for a bound given by a discriminant, which in the body
8982 -- is replaced with the discriminal of the enclosing type.
8984 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8985 -- Check both bounds
8987 -----------------------
8988 -- Conforming_Bounds --
8989 -----------------------
8991 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8993 if Is_Entity_Name (B1)
8994 and then Is_Entity_Name (B2)
8995 and then Ekind (Entity (B1)) = E_Discriminant
8997 return Chars (B1) = Chars (B2);
9000 return Fully_Conformant_Expressions (B1, B2);
9002 end Conforming_Bounds;
9004 -----------------------
9005 -- Conforming_Ranges --
9006 -----------------------
9008 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9011 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9013 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9014 end Conforming_Ranges;
9016 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9019 if Nkind (S1) /= Nkind (S2) then
9022 elsif Is_Entity_Name (S1) then
9023 return Entity (S1) = Entity (S2);
9025 elsif Nkind (S1) = N_Range then
9026 return Conforming_Ranges (S1, S2);
9028 elsif Nkind (S1) = N_Subtype_Indication then
9030 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9033 (Range_Expression (Constraint (S1)),
9034 Range_Expression (Constraint (S2)));
9038 end Fully_Conformant_Discrete_Subtypes;
9040 --------------------
9041 -- Install_Entity --
9042 --------------------
9044 procedure Install_Entity (E : Entity_Id) is
9045 Prev : constant Entity_Id := Current_Entity (E);
9047 Set_Is_Immediately_Visible (E);
9048 Set_Current_Entity (E);
9049 Set_Homonym (E, Prev);
9052 ---------------------
9053 -- Install_Formals --
9054 ---------------------
9056 procedure Install_Formals (Id : Entity_Id) is
9059 F := First_Formal (Id);
9060 while Present (F) loop
9064 end Install_Formals;
9066 -----------------------------
9067 -- Is_Interface_Conformant --
9068 -----------------------------
9070 function Is_Interface_Conformant
9071 (Tagged_Type : Entity_Id;
9072 Iface_Prim : Entity_Id;
9073 Prim : Entity_Id) return Boolean
9075 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
9076 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9078 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9079 -- Return the controlling formal of Prim
9081 ------------------------
9082 -- Controlling_Formal --
9083 ------------------------
9085 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9086 E : Entity_Id := First_Entity (Prim);
9089 while Present (E) loop
9090 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9098 end Controlling_Formal;
9102 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9103 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9105 -- Start of processing for Is_Interface_Conformant
9108 pragma Assert (Is_Subprogram (Iface_Prim)
9109 and then Is_Subprogram (Prim)
9110 and then Is_Dispatching_Operation (Iface_Prim)
9111 and then Is_Dispatching_Operation (Prim));
9113 pragma Assert (Is_Interface (Iface)
9114 or else (Present (Alias (Iface_Prim))
9117 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9119 if Prim = Iface_Prim
9120 or else not Is_Subprogram (Prim)
9121 or else Ekind (Prim) /= Ekind (Iface_Prim)
9122 or else not Is_Dispatching_Operation (Prim)
9123 or else Scope (Prim) /= Scope (Tagged_Type)
9125 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9126 or else not Primitive_Names_Match (Iface_Prim, Prim)
9130 -- The mode of the controlling formals must match
9132 elsif Present (Iface_Ctrl_F)
9133 and then Present (Prim_Ctrl_F)
9134 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9138 -- Case of a procedure, or a function whose result type matches the
9139 -- result type of the interface primitive, or a function that has no
9140 -- controlling result (I or access I).
9142 elsif Ekind (Iface_Prim) = E_Procedure
9143 or else Etype (Prim) = Etype (Iface_Prim)
9144 or else not Has_Controlling_Result (Prim)
9146 return Type_Conformant
9147 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9149 -- Case of a function returning an interface, or an access to one.
9150 -- Check that the return types correspond.
9152 elsif Implements_Interface (Typ, Iface) then
9153 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9155 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9160 Type_Conformant (Prim, Iface_Prim,
9161 Skip_Controlling_Formals => True);
9167 end Is_Interface_Conformant;
9169 ---------------------------------
9170 -- Is_Non_Overriding_Operation --
9171 ---------------------------------
9173 function Is_Non_Overriding_Operation
9174 (Prev_E : Entity_Id;
9175 New_E : Entity_Id) return Boolean
9179 G_Typ : Entity_Id := Empty;
9181 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9182 -- If F_Type is a derived type associated with a generic actual subtype,
9183 -- then return its Generic_Parent_Type attribute, else return Empty.
9185 function Types_Correspond
9186 (P_Type : Entity_Id;
9187 N_Type : Entity_Id) return Boolean;
9188 -- Returns true if and only if the types (or designated types in the
9189 -- case of anonymous access types) are the same or N_Type is derived
9190 -- directly or indirectly from P_Type.
9192 -----------------------------
9193 -- Get_Generic_Parent_Type --
9194 -----------------------------
9196 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9202 if Is_Derived_Type (F_Typ)
9203 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9205 -- The tree must be traversed to determine the parent subtype in
9206 -- the generic unit, which unfortunately isn't always available
9207 -- via semantic attributes. ??? (Note: The use of Original_Node
9208 -- is needed for cases where a full derived type has been
9211 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9212 if Nkind (Defn) = N_Derived_Type_Definition then
9213 Indic := Subtype_Indication (Defn);
9215 if Nkind (Indic) = N_Subtype_Indication then
9216 G_Typ := Entity (Subtype_Mark (Indic));
9218 G_Typ := Entity (Indic);
9221 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9222 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9224 return Generic_Parent_Type (Parent (G_Typ));
9230 end Get_Generic_Parent_Type;
9232 ----------------------
9233 -- Types_Correspond --
9234 ----------------------
9236 function Types_Correspond
9237 (P_Type : Entity_Id;
9238 N_Type : Entity_Id) return Boolean
9240 Prev_Type : Entity_Id := Base_Type (P_Type);
9241 New_Type : Entity_Id := Base_Type (N_Type);
9244 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9245 Prev_Type := Designated_Type (Prev_Type);
9248 if Ekind (New_Type) = E_Anonymous_Access_Type then
9249 New_Type := Designated_Type (New_Type);
9252 if Prev_Type = New_Type then
9255 elsif not Is_Class_Wide_Type (New_Type) then
9256 while Etype (New_Type) /= New_Type loop
9257 New_Type := Etype (New_Type);
9258 if New_Type = Prev_Type then
9264 end Types_Correspond;
9266 -- Start of processing for Is_Non_Overriding_Operation
9269 -- In the case where both operations are implicit derived subprograms
9270 -- then neither overrides the other. This can only occur in certain
9271 -- obscure cases (e.g., derivation from homographs created in a generic
9274 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9277 elsif Ekind (Current_Scope) = E_Package
9278 and then Is_Generic_Instance (Current_Scope)
9279 and then In_Private_Part (Current_Scope)
9280 and then Comes_From_Source (New_E)
9282 -- We examine the formals and result type of the inherited operation,
9283 -- to determine whether their type is derived from (the instance of)
9284 -- a generic type. The first such formal or result type is the one
9287 Formal := First_Formal (Prev_E);
9288 while Present (Formal) loop
9289 F_Typ := Base_Type (Etype (Formal));
9291 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9292 F_Typ := Designated_Type (F_Typ);
9295 G_Typ := Get_Generic_Parent_Type (F_Typ);
9296 exit when Present (G_Typ);
9298 Next_Formal (Formal);
9301 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9302 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9309 -- If the generic type is a private type, then the original operation
9310 -- was not overriding in the generic, because there was no primitive
9311 -- operation to override.
9313 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9314 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9315 N_Formal_Private_Type_Definition
9319 -- The generic parent type is the ancestor of a formal derived
9320 -- type declaration. We need to check whether it has a primitive
9321 -- operation that should be overridden by New_E in the generic.
9325 P_Formal : Entity_Id;
9326 N_Formal : Entity_Id;
9330 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9333 while Present (Prim_Elt) loop
9334 P_Prim := Node (Prim_Elt);
9336 if Chars (P_Prim) = Chars (New_E)
9337 and then Ekind (P_Prim) = Ekind (New_E)
9339 P_Formal := First_Formal (P_Prim);
9340 N_Formal := First_Formal (New_E);
9341 while Present (P_Formal) and then Present (N_Formal) loop
9342 P_Typ := Etype (P_Formal);
9343 N_Typ := Etype (N_Formal);
9345 if not Types_Correspond (P_Typ, N_Typ) then
9349 Next_Entity (P_Formal);
9350 Next_Entity (N_Formal);
9353 -- Found a matching primitive operation belonging to the
9354 -- formal ancestor type, so the new subprogram is
9358 and then No (N_Formal)
9359 and then (Ekind (New_E) /= E_Function
9362 (Etype (P_Prim), Etype (New_E)))
9368 Next_Elmt (Prim_Elt);
9371 -- If no match found, then the new subprogram does not
9372 -- override in the generic (nor in the instance).
9374 -- If the type in question is not abstract, and the subprogram
9375 -- is, this will be an error if the new operation is in the
9376 -- private part of the instance. Emit a warning now, which will
9377 -- make the subsequent error message easier to understand.
9379 if not Is_Abstract_Type (F_Typ)
9380 and then Is_Abstract_Subprogram (Prev_E)
9381 and then In_Private_Part (Current_Scope)
9383 Error_Msg_Node_2 := F_Typ;
9385 ("private operation& in generic unit does not override " &
9386 "any primitive operation of& (RM 12.3 (18))??",
9396 end Is_Non_Overriding_Operation;
9398 -------------------------------------
9399 -- List_Inherited_Pre_Post_Aspects --
9400 -------------------------------------
9402 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9404 if Opt.List_Inherited_Aspects
9405 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9408 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9412 for J in Inherited'Range loop
9413 P := Pre_Post_Conditions (Contract (Inherited (J)));
9414 while Present (P) loop
9415 Error_Msg_Sloc := Sloc (P);
9417 if Class_Present (P) and then not Split_PPC (P) then
9418 if Pragma_Name (P) = Name_Precondition then
9420 ("info: & inherits `Pre''Class` aspect from #?L?",
9424 ("info: & inherits `Post''Class` aspect from #?L?",
9429 P := Next_Pragma (P);
9434 end List_Inherited_Pre_Post_Aspects;
9436 ------------------------------
9437 -- Make_Inequality_Operator --
9438 ------------------------------
9440 -- S is the defining identifier of an equality operator. We build a
9441 -- subprogram declaration with the right signature. This operation is
9442 -- intrinsic, because it is always expanded as the negation of the
9443 -- call to the equality function.
9445 procedure Make_Inequality_Operator (S : Entity_Id) is
9446 Loc : constant Source_Ptr := Sloc (S);
9449 Op_Name : Entity_Id;
9451 FF : constant Entity_Id := First_Formal (S);
9452 NF : constant Entity_Id := Next_Formal (FF);
9455 -- Check that equality was properly defined, ignore call if not
9462 A : constant Entity_Id :=
9463 Make_Defining_Identifier (Sloc (FF),
9464 Chars => Chars (FF));
9466 B : constant Entity_Id :=
9467 Make_Defining_Identifier (Sloc (NF),
9468 Chars => Chars (NF));
9471 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9473 Formals := New_List (
9474 Make_Parameter_Specification (Loc,
9475 Defining_Identifier => A,
9477 New_Reference_To (Etype (First_Formal (S)),
9478 Sloc (Etype (First_Formal (S))))),
9480 Make_Parameter_Specification (Loc,
9481 Defining_Identifier => B,
9483 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9484 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9487 Make_Subprogram_Declaration (Loc,
9489 Make_Function_Specification (Loc,
9490 Defining_Unit_Name => Op_Name,
9491 Parameter_Specifications => Formals,
9492 Result_Definition =>
9493 New_Reference_To (Standard_Boolean, Loc)));
9495 -- Insert inequality right after equality if it is explicit or after
9496 -- the derived type when implicit. These entities are created only
9497 -- for visibility purposes, and eventually replaced in the course of
9498 -- expansion, so they do not need to be attached to the tree and seen
9499 -- by the back-end. Keeping them internal also avoids spurious
9500 -- freezing problems. The declaration is inserted in the tree for
9501 -- analysis, and removed afterwards. If the equality operator comes
9502 -- from an explicit declaration, attach the inequality immediately
9503 -- after. Else the equality is inherited from a derived type
9504 -- declaration, so insert inequality after that declaration.
9506 if No (Alias (S)) then
9507 Insert_After (Unit_Declaration_Node (S), Decl);
9508 elsif Is_List_Member (Parent (S)) then
9509 Insert_After (Parent (S), Decl);
9511 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9514 Mark_Rewrite_Insertion (Decl);
9515 Set_Is_Intrinsic_Subprogram (Op_Name);
9518 Set_Has_Completion (Op_Name);
9519 Set_Corresponding_Equality (Op_Name, S);
9520 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9522 end Make_Inequality_Operator;
9524 ----------------------
9525 -- May_Need_Actuals --
9526 ----------------------
9528 procedure May_Need_Actuals (Fun : Entity_Id) is
9533 F := First_Formal (Fun);
9535 while Present (F) loop
9536 if No (Default_Value (F)) then
9544 Set_Needs_No_Actuals (Fun, B);
9545 end May_Need_Actuals;
9547 ---------------------
9548 -- Mode_Conformant --
9549 ---------------------
9551 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9554 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9556 end Mode_Conformant;
9558 ---------------------------
9559 -- New_Overloaded_Entity --
9560 ---------------------------
9562 procedure New_Overloaded_Entity
9564 Derived_Type : Entity_Id := Empty)
9566 Overridden_Subp : Entity_Id := Empty;
9567 -- Set if the current scope has an operation that is type-conformant
9568 -- with S, and becomes hidden by S.
9570 Is_Primitive_Subp : Boolean;
9571 -- Set to True if the new subprogram is primitive
9574 -- Entity that S overrides
9576 Prev_Vis : Entity_Id := Empty;
9577 -- Predecessor of E in Homonym chain
9579 procedure Check_For_Primitive_Subprogram
9580 (Is_Primitive : out Boolean;
9581 Is_Overriding : Boolean := False);
9582 -- If the subprogram being analyzed is a primitive operation of the type
9583 -- of a formal or result, set the Has_Primitive_Operations flag on the
9584 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9585 -- corresponding flag on the entity itself for later use.
9587 procedure Check_Synchronized_Overriding
9588 (Def_Id : Entity_Id;
9589 Overridden_Subp : out Entity_Id);
9590 -- First determine if Def_Id is an entry or a subprogram either defined
9591 -- in the scope of a task or protected type, or is a primitive of such
9592 -- a type. Check whether Def_Id overrides a subprogram of an interface
9593 -- implemented by the synchronized type, return the overridden entity
9596 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9597 -- Check that E is declared in the private part of the current package,
9598 -- or in the package body, where it may hide a previous declaration.
9599 -- We can't use In_Private_Part by itself because this flag is also
9600 -- set when freezing entities, so we must examine the place of the
9601 -- declaration in the tree, and recognize wrapper packages as well.
9603 function Is_Overriding_Alias
9605 New_E : Entity_Id) return Boolean;
9606 -- Check whether new subprogram and old subprogram are both inherited
9607 -- from subprograms that have distinct dispatch table entries. This can
9608 -- occur with derivations from instances with accidental homonyms.
9609 -- The function is conservative given that the converse is only true
9610 -- within instances that contain accidental overloadings.
9612 ------------------------------------
9613 -- Check_For_Primitive_Subprogram --
9614 ------------------------------------
9616 procedure Check_For_Primitive_Subprogram
9617 (Is_Primitive : out Boolean;
9618 Is_Overriding : Boolean := False)
9624 function Visible_Part_Type (T : Entity_Id) return Boolean;
9625 -- Returns true if T is declared in the visible part of the current
9626 -- package scope; otherwise returns false. Assumes that T is declared
9629 procedure Check_Private_Overriding (T : Entity_Id);
9630 -- Checks that if a primitive abstract subprogram of a visible
9631 -- abstract type is declared in a private part, then it must override
9632 -- an abstract subprogram declared in the visible part. Also checks
9633 -- that if a primitive function with a controlling result is declared
9634 -- in a private part, then it must override a function declared in
9635 -- the visible part.
9637 ------------------------------
9638 -- Check_Private_Overriding --
9639 ------------------------------
9641 procedure Check_Private_Overriding (T : Entity_Id) is
9643 if Is_Package_Or_Generic_Package (Current_Scope)
9644 and then In_Private_Part (Current_Scope)
9645 and then Visible_Part_Type (T)
9646 and then not In_Instance
9648 if Is_Abstract_Type (T)
9649 and then Is_Abstract_Subprogram (S)
9650 and then (not Is_Overriding
9651 or else not Is_Abstract_Subprogram (E))
9654 ("abstract subprograms must be visible "
9655 & "(RM 3.9.3(10))!", S);
9657 elsif Ekind (S) = E_Function and then not Is_Overriding then
9658 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9660 ("private function with tagged result must"
9661 & " override visible-part function", S);
9663 ("\move subprogram to the visible part"
9664 & " (RM 3.9.3(10))", S);
9666 -- AI05-0073: extend this test to the case of a function
9667 -- with a controlling access result.
9669 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9670 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9672 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9673 and then Ada_Version >= Ada_2012
9676 ("private function with controlling access result "
9677 & "must override visible-part function", S);
9679 ("\move subprogram to the visible part"
9680 & " (RM 3.9.3(10))", S);
9684 end Check_Private_Overriding;
9686 -----------------------
9687 -- Visible_Part_Type --
9688 -----------------------
9690 function Visible_Part_Type (T : Entity_Id) return Boolean is
9691 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9695 -- If the entity is a private type, then it must be declared in a
9698 if Ekind (T) in Private_Kind then
9702 -- Otherwise, we traverse the visible part looking for its
9703 -- corresponding declaration. We cannot use the declaration
9704 -- node directly because in the private part the entity of a
9705 -- private type is the one in the full view, which does not
9706 -- indicate that it is the completion of something visible.
9708 N := First (Visible_Declarations (Specification (P)));
9709 while Present (N) loop
9710 if Nkind (N) = N_Full_Type_Declaration
9711 and then Present (Defining_Identifier (N))
9712 and then T = Defining_Identifier (N)
9716 elsif Nkind_In (N, N_Private_Type_Declaration,
9717 N_Private_Extension_Declaration)
9718 and then Present (Defining_Identifier (N))
9719 and then T = Full_View (Defining_Identifier (N))
9728 end Visible_Part_Type;
9730 -- Start of processing for Check_For_Primitive_Subprogram
9733 Is_Primitive := False;
9735 if not Comes_From_Source (S) then
9738 -- If subprogram is at library level, it is not primitive operation
9740 elsif Current_Scope = Standard_Standard then
9743 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9744 and then not In_Package_Body (Current_Scope))
9745 or else Is_Overriding
9747 -- For function, check return type
9749 if Ekind (S) = E_Function then
9750 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9751 F_Typ := Designated_Type (Etype (S));
9756 B_Typ := Base_Type (F_Typ);
9758 if Scope (B_Typ) = Current_Scope
9759 and then not Is_Class_Wide_Type (B_Typ)
9760 and then not Is_Generic_Type (B_Typ)
9762 Is_Primitive := True;
9763 Set_Has_Primitive_Operations (B_Typ);
9764 Set_Is_Primitive (S);
9765 Check_Private_Overriding (B_Typ);
9769 -- For all subprograms, check formals
9771 Formal := First_Formal (S);
9772 while Present (Formal) loop
9773 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9774 F_Typ := Designated_Type (Etype (Formal));
9776 F_Typ := Etype (Formal);
9779 B_Typ := Base_Type (F_Typ);
9781 if Ekind (B_Typ) = E_Access_Subtype then
9782 B_Typ := Base_Type (B_Typ);
9785 if Scope (B_Typ) = Current_Scope
9786 and then not Is_Class_Wide_Type (B_Typ)
9787 and then not Is_Generic_Type (B_Typ)
9789 Is_Primitive := True;
9790 Set_Is_Primitive (S);
9791 Set_Has_Primitive_Operations (B_Typ);
9792 Check_Private_Overriding (B_Typ);
9795 Next_Formal (Formal);
9798 -- Special case: An equality function can be redefined for a type
9799 -- occurring in a declarative part, and won't otherwise be treated as
9800 -- a primitive because it doesn't occur in a package spec and doesn't
9801 -- override an inherited subprogram. It's important that we mark it
9802 -- primitive so it can be returned by Collect_Primitive_Operations
9803 -- and be used in composing the equality operation of later types
9804 -- that have a component of the type.
9806 elsif Chars (S) = Name_Op_Eq
9807 and then Etype (S) = Standard_Boolean
9809 B_Typ := Base_Type (Etype (First_Formal (S)));
9811 if Scope (B_Typ) = Current_Scope
9813 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9814 and then not Is_Limited_Type (B_Typ)
9816 Is_Primitive := True;
9817 Set_Is_Primitive (S);
9818 Set_Has_Primitive_Operations (B_Typ);
9819 Check_Private_Overriding (B_Typ);
9822 end Check_For_Primitive_Subprogram;
9824 -----------------------------------
9825 -- Check_Synchronized_Overriding --
9826 -----------------------------------
9828 procedure Check_Synchronized_Overriding
9829 (Def_Id : Entity_Id;
9830 Overridden_Subp : out Entity_Id)
9832 Ifaces_List : Elist_Id;
9836 function Matches_Prefixed_View_Profile
9837 (Prim_Params : List_Id;
9838 Iface_Params : List_Id) return Boolean;
9839 -- Determine whether a subprogram's parameter profile Prim_Params
9840 -- matches that of a potentially overridden interface subprogram
9841 -- Iface_Params. Also determine if the type of first parameter of
9842 -- Iface_Params is an implemented interface.
9844 -----------------------------------
9845 -- Matches_Prefixed_View_Profile --
9846 -----------------------------------
9848 function Matches_Prefixed_View_Profile
9849 (Prim_Params : List_Id;
9850 Iface_Params : List_Id) return Boolean
9852 Iface_Id : Entity_Id;
9853 Iface_Param : Node_Id;
9854 Iface_Typ : Entity_Id;
9855 Prim_Id : Entity_Id;
9856 Prim_Param : Node_Id;
9857 Prim_Typ : Entity_Id;
9859 function Is_Implemented
9860 (Ifaces_List : Elist_Id;
9861 Iface : Entity_Id) return Boolean;
9862 -- Determine if Iface is implemented by the current task or
9865 --------------------
9866 -- Is_Implemented --
9867 --------------------
9869 function Is_Implemented
9870 (Ifaces_List : Elist_Id;
9871 Iface : Entity_Id) return Boolean
9873 Iface_Elmt : Elmt_Id;
9876 Iface_Elmt := First_Elmt (Ifaces_List);
9877 while Present (Iface_Elmt) loop
9878 if Node (Iface_Elmt) = Iface then
9882 Next_Elmt (Iface_Elmt);
9888 -- Start of processing for Matches_Prefixed_View_Profile
9891 Iface_Param := First (Iface_Params);
9892 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9894 if Is_Access_Type (Iface_Typ) then
9895 Iface_Typ := Designated_Type (Iface_Typ);
9898 Prim_Param := First (Prim_Params);
9900 -- The first parameter of the potentially overridden subprogram
9901 -- must be an interface implemented by Prim.
9903 if not Is_Interface (Iface_Typ)
9904 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9909 -- The checks on the object parameters are done, move onto the
9910 -- rest of the parameters.
9912 if not In_Scope then
9913 Prim_Param := Next (Prim_Param);
9916 Iface_Param := Next (Iface_Param);
9917 while Present (Iface_Param) and then Present (Prim_Param) loop
9918 Iface_Id := Defining_Identifier (Iface_Param);
9919 Iface_Typ := Find_Parameter_Type (Iface_Param);
9921 Prim_Id := Defining_Identifier (Prim_Param);
9922 Prim_Typ := Find_Parameter_Type (Prim_Param);
9924 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9925 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9926 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9928 Iface_Typ := Designated_Type (Iface_Typ);
9929 Prim_Typ := Designated_Type (Prim_Typ);
9932 -- Case of multiple interface types inside a parameter profile
9934 -- (Obj_Param : in out Iface; ...; Param : Iface)
9936 -- If the interface type is implemented, then the matching type
9937 -- in the primitive should be the implementing record type.
9939 if Ekind (Iface_Typ) = E_Record_Type
9940 and then Is_Interface (Iface_Typ)
9941 and then Is_Implemented (Ifaces_List, Iface_Typ)
9943 if Prim_Typ /= Typ then
9947 -- The two parameters must be both mode and subtype conformant
9949 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9951 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9960 -- One of the two lists contains more parameters than the other
9962 if Present (Iface_Param) or else Present (Prim_Param) then
9967 end Matches_Prefixed_View_Profile;
9969 -- Start of processing for Check_Synchronized_Overriding
9972 Overridden_Subp := Empty;
9974 -- Def_Id must be an entry or a subprogram. We should skip predefined
9975 -- primitives internally generated by the frontend; however at this
9976 -- stage predefined primitives are still not fully decorated. As a
9977 -- minor optimization we skip here internally generated subprograms.
9979 if (Ekind (Def_Id) /= E_Entry
9980 and then Ekind (Def_Id) /= E_Function
9981 and then Ekind (Def_Id) /= E_Procedure)
9982 or else not Comes_From_Source (Def_Id)
9987 -- Search for the concurrent declaration since it contains the list
9988 -- of all implemented interfaces. In this case, the subprogram is
9989 -- declared within the scope of a protected or a task type.
9991 if Present (Scope (Def_Id))
9992 and then Is_Concurrent_Type (Scope (Def_Id))
9993 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9995 Typ := Scope (Def_Id);
9998 -- The enclosing scope is not a synchronized type and the subprogram
10001 elsif No (First_Formal (Def_Id)) then
10004 -- The subprogram has formals and hence it may be a primitive of a
10005 -- concurrent type.
10008 Typ := Etype (First_Formal (Def_Id));
10010 if Is_Access_Type (Typ) then
10011 Typ := Directly_Designated_Type (Typ);
10014 if Is_Concurrent_Type (Typ)
10015 and then not Is_Generic_Actual_Type (Typ)
10019 -- This case occurs when the concurrent type is declared within
10020 -- a generic unit. As a result the corresponding record has been
10021 -- built and used as the type of the first formal, we just have
10022 -- to retrieve the corresponding concurrent type.
10024 elsif Is_Concurrent_Record_Type (Typ)
10025 and then not Is_Class_Wide_Type (Typ)
10026 and then Present (Corresponding_Concurrent_Type (Typ))
10028 Typ := Corresponding_Concurrent_Type (Typ);
10036 -- There is no overriding to check if is an inherited operation in a
10037 -- type derivation on for a generic actual.
10039 Collect_Interfaces (Typ, Ifaces_List);
10041 if Is_Empty_Elmt_List (Ifaces_List) then
10045 -- Determine whether entry or subprogram Def_Id overrides a primitive
10046 -- operation that belongs to one of the interfaces in Ifaces_List.
10049 Candidate : Entity_Id := Empty;
10050 Hom : Entity_Id := Empty;
10051 Iface_Typ : Entity_Id;
10052 Subp : Entity_Id := Empty;
10055 -- Traverse the homonym chain, looking for a potentially
10056 -- overridden subprogram that belongs to an implemented
10059 Hom := Current_Entity_In_Scope (Def_Id);
10060 while Present (Hom) loop
10064 or else not Is_Overloadable (Subp)
10065 or else not Is_Primitive (Subp)
10066 or else not Is_Dispatching_Operation (Subp)
10067 or else not Present (Find_Dispatching_Type (Subp))
10068 or else not Is_Interface (Find_Dispatching_Type (Subp))
10072 -- Entries and procedures can override abstract or null
10073 -- interface procedures.
10075 elsif (Ekind (Def_Id) = E_Procedure
10076 or else Ekind (Def_Id) = E_Entry)
10077 and then Ekind (Subp) = E_Procedure
10078 and then Matches_Prefixed_View_Profile
10079 (Parameter_Specifications (Parent (Def_Id)),
10080 Parameter_Specifications (Parent (Subp)))
10084 -- For an overridden subprogram Subp, check whether the mode
10085 -- of its first parameter is correct depending on the kind
10086 -- of synchronized type.
10089 Formal : constant Node_Id := First_Formal (Candidate);
10092 -- In order for an entry or a protected procedure to
10093 -- override, the first parameter of the overridden
10094 -- routine must be of mode "out", "in out" or
10095 -- access-to-variable.
10097 if Ekind_In (Candidate, E_Entry, E_Procedure)
10098 and then Is_Protected_Type (Typ)
10099 and then Ekind (Formal) /= E_In_Out_Parameter
10100 and then Ekind (Formal) /= E_Out_Parameter
10101 and then Nkind (Parameter_Type (Parent (Formal))) /=
10102 N_Access_Definition
10106 -- All other cases are OK since a task entry or routine
10107 -- does not have a restriction on the mode of the first
10108 -- parameter of the overridden interface routine.
10111 Overridden_Subp := Candidate;
10116 -- Functions can override abstract interface functions
10118 elsif Ekind (Def_Id) = E_Function
10119 and then Ekind (Subp) = E_Function
10120 and then Matches_Prefixed_View_Profile
10121 (Parameter_Specifications (Parent (Def_Id)),
10122 Parameter_Specifications (Parent (Subp)))
10123 and then Etype (Result_Definition (Parent (Def_Id))) =
10124 Etype (Result_Definition (Parent (Subp)))
10126 Overridden_Subp := Subp;
10130 Hom := Homonym (Hom);
10133 -- After examining all candidates for overriding, we are left with
10134 -- the best match which is a mode incompatible interface routine.
10135 -- Do not emit an error if the Expander is active since this error
10136 -- will be detected later on after all concurrent types are
10137 -- expanded and all wrappers are built. This check is meant for
10138 -- spec-only compilations.
10140 if Present (Candidate) and then not Expander_Active then
10142 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10144 -- Def_Id is primitive of a protected type, declared inside the
10145 -- type, and the candidate is primitive of a limited or
10146 -- synchronized interface.
10149 and then Is_Protected_Type (Typ)
10151 (Is_Limited_Interface (Iface_Typ)
10152 or else Is_Protected_Interface (Iface_Typ)
10153 or else Is_Synchronized_Interface (Iface_Typ)
10154 or else Is_Task_Interface (Iface_Typ))
10156 Error_Msg_PT (Parent (Typ), Candidate);
10160 Overridden_Subp := Candidate;
10163 end Check_Synchronized_Overriding;
10165 ----------------------------
10166 -- Is_Private_Declaration --
10167 ----------------------------
10169 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10170 Priv_Decls : List_Id;
10171 Decl : constant Node_Id := Unit_Declaration_Node (E);
10174 if Is_Package_Or_Generic_Package (Current_Scope)
10175 and then In_Private_Part (Current_Scope)
10178 Private_Declarations
10179 (Specification (Unit_Declaration_Node (Current_Scope)));
10181 return In_Package_Body (Current_Scope)
10183 (Is_List_Member (Decl)
10184 and then List_Containing (Decl) = Priv_Decls)
10185 or else (Nkind (Parent (Decl)) = N_Package_Specification
10187 Is_Compilation_Unit
10188 (Defining_Entity (Parent (Decl)))
10189 and then List_Containing (Parent (Parent (Decl))) =
10194 end Is_Private_Declaration;
10196 --------------------------
10197 -- Is_Overriding_Alias --
10198 --------------------------
10200 function Is_Overriding_Alias
10201 (Old_E : Entity_Id;
10202 New_E : Entity_Id) return Boolean
10204 AO : constant Entity_Id := Alias (Old_E);
10205 AN : constant Entity_Id := Alias (New_E);
10208 return Scope (AO) /= Scope (AN)
10209 or else No (DTC_Entity (AO))
10210 or else No (DTC_Entity (AN))
10211 or else DT_Position (AO) = DT_Position (AN);
10212 end Is_Overriding_Alias;
10214 -- Start of processing for New_Overloaded_Entity
10217 -- We need to look for an entity that S may override. This must be a
10218 -- homonym in the current scope, so we look for the first homonym of
10219 -- S in the current scope as the starting point for the search.
10221 E := Current_Entity_In_Scope (S);
10223 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10224 -- They are directly added to the list of primitive operations of
10225 -- Derived_Type, unless this is a rederivation in the private part
10226 -- of an operation that was already derived in the visible part of
10227 -- the current package.
10229 if Ada_Version >= Ada_2005
10230 and then Present (Derived_Type)
10231 and then Present (Alias (S))
10232 and then Is_Dispatching_Operation (Alias (S))
10233 and then Present (Find_Dispatching_Type (Alias (S)))
10234 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10236 -- For private types, when the full-view is processed we propagate to
10237 -- the full view the non-overridden entities whose attribute "alias"
10238 -- references an interface primitive. These entities were added by
10239 -- Derive_Subprograms to ensure that interface primitives are
10242 -- Inside_Freeze_Actions is non zero when S corresponds with an
10243 -- internal entity that links an interface primitive with its
10244 -- covering primitive through attribute Interface_Alias (see
10245 -- Add_Internal_Interface_Entities).
10247 if Inside_Freezing_Actions = 0
10248 and then Is_Package_Or_Generic_Package (Current_Scope)
10249 and then In_Private_Part (Current_Scope)
10250 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10251 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10252 and then Full_View (Defining_Identifier (Parent (E)))
10253 = Defining_Identifier (Parent (S))
10254 and then Alias (E) = Alias (S)
10256 Check_Operation_From_Private_View (S, E);
10257 Set_Is_Dispatching_Operation (S);
10262 Enter_Overloaded_Entity (S);
10263 Check_Dispatching_Operation (S, Empty);
10264 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10270 -- If there is no homonym then this is definitely not overriding
10273 Enter_Overloaded_Entity (S);
10274 Check_Dispatching_Operation (S, Empty);
10275 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10277 -- If subprogram has an explicit declaration, check whether it
10278 -- has an overriding indicator.
10280 if Comes_From_Source (S) then
10281 Check_Synchronized_Overriding (S, Overridden_Subp);
10283 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10284 -- it may have overridden some hidden inherited primitive. Update
10285 -- Overridden_Subp to avoid spurious errors when checking the
10286 -- overriding indicator.
10288 if Ada_Version >= Ada_2012
10289 and then No (Overridden_Subp)
10290 and then Is_Dispatching_Operation (S)
10291 and then Present (Overridden_Operation (S))
10293 Overridden_Subp := Overridden_Operation (S);
10296 Check_Overriding_Indicator
10297 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10300 -- If there is a homonym that is not overloadable, then we have an
10301 -- error, except for the special cases checked explicitly below.
10303 elsif not Is_Overloadable (E) then
10305 -- Check for spurious conflict produced by a subprogram that has the
10306 -- same name as that of the enclosing generic package. The conflict
10307 -- occurs within an instance, between the subprogram and the renaming
10308 -- declaration for the package. After the subprogram, the package
10309 -- renaming declaration becomes hidden.
10311 if Ekind (E) = E_Package
10312 and then Present (Renamed_Object (E))
10313 and then Renamed_Object (E) = Current_Scope
10314 and then Nkind (Parent (Renamed_Object (E))) =
10315 N_Package_Specification
10316 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10319 Set_Is_Immediately_Visible (E, False);
10320 Enter_Overloaded_Entity (S);
10321 Set_Homonym (S, Homonym (E));
10322 Check_Dispatching_Operation (S, Empty);
10323 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10325 -- If the subprogram is implicit it is hidden by the previous
10326 -- declaration. However if it is dispatching, it must appear in the
10327 -- dispatch table anyway, because it can be dispatched to even if it
10328 -- cannot be called directly.
10330 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10331 Set_Scope (S, Current_Scope);
10333 if Is_Dispatching_Operation (Alias (S)) then
10334 Check_Dispatching_Operation (S, Empty);
10340 Error_Msg_Sloc := Sloc (E);
10342 -- Generate message, with useful additional warning if in generic
10344 if Is_Generic_Unit (E) then
10345 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10346 Error_Msg_N ("\& conflicts with declaration#", S);
10348 Error_Msg_N ("& conflicts with declaration#", S);
10354 -- E exists and is overloadable
10357 Check_Synchronized_Overriding (S, Overridden_Subp);
10359 -- Loop through E and its homonyms to determine if any of them is
10360 -- the candidate for overriding by S.
10362 while Present (E) loop
10364 -- Definitely not interesting if not in the current scope
10366 if Scope (E) /= Current_Scope then
10369 -- Ada 2012 (AI05-0165): For internally generated bodies of
10370 -- null procedures locate the internally generated spec. We
10371 -- enforce mode conformance since a tagged type may inherit
10372 -- from interfaces several null primitives which differ only
10373 -- in the mode of the formals.
10375 elsif not Comes_From_Source (S)
10376 and then Is_Null_Procedure (S)
10377 and then not Mode_Conformant (E, S)
10381 -- Check if we have type conformance
10383 elsif Type_Conformant (E, S) then
10385 -- If the old and new entities have the same profile and one
10386 -- is not the body of the other, then this is an error, unless
10387 -- one of them is implicitly declared.
10389 -- There are some cases when both can be implicit, for example
10390 -- when both a literal and a function that overrides it are
10391 -- inherited in a derivation, or when an inherited operation
10392 -- of a tagged full type overrides the inherited operation of
10393 -- a private extension. Ada 83 had a special rule for the
10394 -- literal case. In Ada 95, the later implicit operation hides
10395 -- the former, and the literal is always the former. In the
10396 -- odd case where both are derived operations declared at the
10397 -- same point, both operations should be declared, and in that
10398 -- case we bypass the following test and proceed to the next
10399 -- part. This can only occur for certain obscure cases in
10400 -- instances, when an operation on a type derived from a formal
10401 -- private type does not override a homograph inherited from
10402 -- the actual. In subsequent derivations of such a type, the
10403 -- DT positions of these operations remain distinct, if they
10406 if Present (Alias (S))
10407 and then (No (Alias (E))
10408 or else Comes_From_Source (E)
10409 or else Is_Abstract_Subprogram (S)
10411 (Is_Dispatching_Operation (E)
10412 and then Is_Overriding_Alias (E, S)))
10413 and then Ekind (E) /= E_Enumeration_Literal
10415 -- When an derived operation is overloaded it may be due to
10416 -- the fact that the full view of a private extension
10417 -- re-inherits. It has to be dealt with.
10419 if Is_Package_Or_Generic_Package (Current_Scope)
10420 and then In_Private_Part (Current_Scope)
10422 Check_Operation_From_Private_View (S, E);
10425 -- In any case the implicit operation remains hidden by the
10426 -- existing declaration, which is overriding. Indicate that
10427 -- E overrides the operation from which S is inherited.
10429 if Present (Alias (S)) then
10430 Set_Overridden_Operation (E, Alias (S));
10432 Set_Overridden_Operation (E, S);
10435 if Comes_From_Source (E) then
10436 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10441 -- Within an instance, the renaming declarations for actual
10442 -- subprograms may become ambiguous, but they do not hide each
10445 elsif Ekind (E) /= E_Entry
10446 and then not Comes_From_Source (E)
10447 and then not Is_Generic_Instance (E)
10448 and then (Present (Alias (E))
10449 or else Is_Intrinsic_Subprogram (E))
10450 and then (not In_Instance
10451 or else No (Parent (E))
10452 or else Nkind (Unit_Declaration_Node (E)) /=
10453 N_Subprogram_Renaming_Declaration)
10455 -- A subprogram child unit is not allowed to override an
10456 -- inherited subprogram (10.1.1(20)).
10458 if Is_Child_Unit (S) then
10460 ("child unit overrides inherited subprogram in parent",
10465 if Is_Non_Overriding_Operation (E, S) then
10466 Enter_Overloaded_Entity (S);
10468 if No (Derived_Type)
10469 or else Is_Tagged_Type (Derived_Type)
10471 Check_Dispatching_Operation (S, Empty);
10477 -- E is a derived operation or an internal operator which
10478 -- is being overridden. Remove E from further visibility.
10479 -- Furthermore, if E is a dispatching operation, it must be
10480 -- replaced in the list of primitive operations of its type
10481 -- (see Override_Dispatching_Operation).
10483 Overridden_Subp := E;
10489 Prev := First_Entity (Current_Scope);
10490 while Present (Prev) and then Next_Entity (Prev) /= E loop
10491 Next_Entity (Prev);
10494 -- It is possible for E to be in the current scope and
10495 -- yet not in the entity chain. This can only occur in a
10496 -- generic context where E is an implicit concatenation
10497 -- in the formal part, because in a generic body the
10498 -- entity chain starts with the formals.
10501 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10503 -- E must be removed both from the entity_list of the
10504 -- current scope, and from the visibility chain
10506 if Debug_Flag_E then
10507 Write_Str ("Override implicit operation ");
10508 Write_Int (Int (E));
10512 -- If E is a predefined concatenation, it stands for four
10513 -- different operations. As a result, a single explicit
10514 -- declaration does not hide it. In a possible ambiguous
10515 -- situation, Disambiguate chooses the user-defined op,
10516 -- so it is correct to retain the previous internal one.
10518 if Chars (E) /= Name_Op_Concat
10519 or else Ekind (E) /= E_Operator
10521 -- For nondispatching derived operations that are
10522 -- overridden by a subprogram declared in the private
10523 -- part of a package, we retain the derived subprogram
10524 -- but mark it as not immediately visible. If the
10525 -- derived operation was declared in the visible part
10526 -- then this ensures that it will still be visible
10527 -- outside the package with the proper signature
10528 -- (calls from outside must also be directed to this
10529 -- version rather than the overriding one, unlike the
10530 -- dispatching case). Calls from inside the package
10531 -- will still resolve to the overriding subprogram
10532 -- since the derived one is marked as not visible
10533 -- within the package.
10535 -- If the private operation is dispatching, we achieve
10536 -- the overriding by keeping the implicit operation
10537 -- but setting its alias to be the overriding one. In
10538 -- this fashion the proper body is executed in all
10539 -- cases, but the original signature is used outside
10542 -- If the overriding is not in the private part, we
10543 -- remove the implicit operation altogether.
10545 if Is_Private_Declaration (S) then
10546 if not Is_Dispatching_Operation (E) then
10547 Set_Is_Immediately_Visible (E, False);
10549 -- Work done in Override_Dispatching_Operation,
10550 -- so nothing else needs to be done here.
10556 -- Find predecessor of E in Homonym chain
10558 if E = Current_Entity (E) then
10561 Prev_Vis := Current_Entity (E);
10562 while Homonym (Prev_Vis) /= E loop
10563 Prev_Vis := Homonym (Prev_Vis);
10567 if Prev_Vis /= Empty then
10569 -- Skip E in the visibility chain
10571 Set_Homonym (Prev_Vis, Homonym (E));
10574 Set_Name_Entity_Id (Chars (E), Homonym (E));
10577 Set_Next_Entity (Prev, Next_Entity (E));
10579 if No (Next_Entity (Prev)) then
10580 Set_Last_Entity (Current_Scope, Prev);
10585 Enter_Overloaded_Entity (S);
10587 -- For entities generated by Derive_Subprograms the
10588 -- overridden operation is the inherited primitive
10589 -- (which is available through the attribute alias).
10591 if not (Comes_From_Source (E))
10592 and then Is_Dispatching_Operation (E)
10593 and then Find_Dispatching_Type (E) =
10594 Find_Dispatching_Type (S)
10595 and then Present (Alias (E))
10596 and then Comes_From_Source (Alias (E))
10598 Set_Overridden_Operation (S, Alias (E));
10600 -- Normal case of setting entity as overridden
10602 -- Note: Static_Initialization and Overridden_Operation
10603 -- attributes use the same field in subprogram entities.
10604 -- Static_Initialization is only defined for internal
10605 -- initialization procedures, where Overridden_Operation
10606 -- is irrelevant. Therefore the setting of this attribute
10607 -- must check whether the target is an init_proc.
10609 elsif not Is_Init_Proc (S) then
10610 Set_Overridden_Operation (S, E);
10613 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10615 -- If S is a user-defined subprogram or a null procedure
10616 -- expanded to override an inherited null procedure, or a
10617 -- predefined dispatching primitive then indicate that E
10618 -- overrides the operation from which S is inherited.
10620 if Comes_From_Source (S)
10622 (Present (Parent (S))
10624 Nkind (Parent (S)) = N_Procedure_Specification
10626 Null_Present (Parent (S)))
10628 (Present (Alias (E))
10630 Is_Predefined_Dispatching_Operation (Alias (E)))
10632 if Present (Alias (E)) then
10633 Set_Overridden_Operation (S, Alias (E));
10637 if Is_Dispatching_Operation (E) then
10639 -- An overriding dispatching subprogram inherits the
10640 -- convention of the overridden subprogram (AI-117).
10642 Set_Convention (S, Convention (E));
10643 Check_Dispatching_Operation (S, E);
10646 Check_Dispatching_Operation (S, Empty);
10649 Check_For_Primitive_Subprogram
10650 (Is_Primitive_Subp, Is_Overriding => True);
10651 goto Check_Inequality;
10654 -- Apparent redeclarations in instances can occur when two
10655 -- formal types get the same actual type. The subprograms in
10656 -- in the instance are legal, even if not callable from the
10657 -- outside. Calls from within are disambiguated elsewhere.
10658 -- For dispatching operations in the visible part, the usual
10659 -- rules apply, and operations with the same profile are not
10660 -- legal (B830001).
10662 elsif (In_Instance_Visible_Part
10663 and then not Is_Dispatching_Operation (E))
10664 or else In_Instance_Not_Visible
10668 -- Here we have a real error (identical profile)
10671 Error_Msg_Sloc := Sloc (E);
10673 -- Avoid cascaded errors if the entity appears in
10674 -- subsequent calls.
10676 Set_Scope (S, Current_Scope);
10678 -- Generate error, with extra useful warning for the case
10679 -- of a generic instance with no completion.
10681 if Is_Generic_Instance (S)
10682 and then not Has_Completion (E)
10685 ("instantiation cannot provide body for&", S);
10686 Error_Msg_N ("\& conflicts with declaration#", S);
10688 Error_Msg_N ("& conflicts with declaration#", S);
10695 -- If one subprogram has an access parameter and the other
10696 -- a parameter of an access type, calls to either might be
10697 -- ambiguous. Verify that parameters match except for the
10698 -- access parameter.
10700 if May_Hide_Profile then
10706 F1 := First_Formal (S);
10707 F2 := First_Formal (E);
10708 while Present (F1) and then Present (F2) loop
10709 if Is_Access_Type (Etype (F1)) then
10710 if not Is_Access_Type (Etype (F2))
10711 or else not Conforming_Types
10712 (Designated_Type (Etype (F1)),
10713 Designated_Type (Etype (F2)),
10716 May_Hide_Profile := False;
10720 not Conforming_Types
10721 (Etype (F1), Etype (F2), Type_Conformant)
10723 May_Hide_Profile := False;
10730 if May_Hide_Profile
10734 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10743 -- On exit, we know that S is a new entity
10745 Enter_Overloaded_Entity (S);
10746 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10747 Check_Overriding_Indicator
10748 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10750 -- Overloading is not allowed in SPARK, except for operators
10752 if Nkind (S) /= N_Defining_Operator_Symbol then
10753 Error_Msg_Sloc := Sloc (Homonym (S));
10754 Check_SPARK_Restriction
10755 ("overloading not allowed with entity#", S);
10758 -- If S is a derived operation for an untagged type then by
10759 -- definition it's not a dispatching operation (even if the parent
10760 -- operation was dispatching), so Check_Dispatching_Operation is not
10761 -- called in that case.
10763 if No (Derived_Type)
10764 or else Is_Tagged_Type (Derived_Type)
10766 Check_Dispatching_Operation (S, Empty);
10770 -- If this is a user-defined equality operator that is not a derived
10771 -- subprogram, create the corresponding inequality. If the operation is
10772 -- dispatching, the expansion is done elsewhere, and we do not create
10773 -- an explicit inequality operation.
10775 <<Check_Inequality>>
10776 if Chars (S) = Name_Op_Eq
10777 and then Etype (S) = Standard_Boolean
10778 and then Present (Parent (S))
10779 and then not Is_Dispatching_Operation (S)
10781 Make_Inequality_Operator (S);
10783 if Ada_Version >= Ada_2012 then
10784 Check_Untagged_Equality (S);
10787 end New_Overloaded_Entity;
10789 ---------------------
10790 -- Process_Formals --
10791 ---------------------
10793 procedure Process_Formals
10795 Related_Nod : Node_Id)
10797 Param_Spec : Node_Id;
10798 Formal : Entity_Id;
10799 Formal_Type : Entity_Id;
10803 Num_Out_Params : Nat := 0;
10804 First_Out_Param : Entity_Id := Empty;
10805 -- Used for setting Is_Only_Out_Parameter
10807 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10808 -- Determine whether an access type designates a type coming from a
10811 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10812 -- Check whether the default has a class-wide type. After analysis the
10813 -- default has the type of the formal, so we must also check explicitly
10814 -- for an access attribute.
10816 -------------------------------
10817 -- Designates_From_With_Type --
10818 -------------------------------
10820 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10821 Desig : Entity_Id := Typ;
10824 if Is_Access_Type (Desig) then
10825 Desig := Directly_Designated_Type (Desig);
10828 if Is_Class_Wide_Type (Desig) then
10829 Desig := Root_Type (Desig);
10833 Ekind (Desig) = E_Incomplete_Type and then From_With_Type (Desig);
10834 end Designates_From_With_Type;
10836 ---------------------------
10837 -- Is_Class_Wide_Default --
10838 ---------------------------
10840 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10842 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10843 or else (Nkind (D) = N_Attribute_Reference
10844 and then Attribute_Name (D) = Name_Access
10845 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10846 end Is_Class_Wide_Default;
10848 -- Start of processing for Process_Formals
10851 -- In order to prevent premature use of the formals in the same formal
10852 -- part, the Ekind is left undefined until all default expressions are
10853 -- analyzed. The Ekind is established in a separate loop at the end.
10855 Param_Spec := First (T);
10856 while Present (Param_Spec) loop
10857 Formal := Defining_Identifier (Param_Spec);
10858 Set_Never_Set_In_Source (Formal, True);
10859 Enter_Name (Formal);
10861 -- Case of ordinary parameters
10863 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10864 Find_Type (Parameter_Type (Param_Spec));
10865 Ptype := Parameter_Type (Param_Spec);
10867 if Ptype = Error then
10871 Formal_Type := Entity (Ptype);
10873 if Is_Incomplete_Type (Formal_Type)
10875 (Is_Class_Wide_Type (Formal_Type)
10876 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10878 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10879 -- primitive operations, as long as their completion is
10880 -- in the same declarative part. If in the private part
10881 -- this means that the type cannot be a Taft-amendment type.
10882 -- Check is done on package exit. For access to subprograms,
10883 -- the use is legal for Taft-amendment types.
10885 -- Ada 2012: tagged incomplete types are allowed as generic
10886 -- formal types. They do not introduce dependencies and the
10887 -- corresponding generic subprogram does not have a delayed
10888 -- freeze, because it does not need a freeze node.
10890 if Is_Tagged_Type (Formal_Type) then
10891 if Ekind (Scope (Current_Scope)) = E_Package
10892 and then not From_With_Type (Formal_Type)
10893 and then not Is_Generic_Type (Formal_Type)
10894 and then not Is_Class_Wide_Type (Formal_Type)
10897 (Parent (T), N_Access_Function_Definition,
10898 N_Access_Procedure_Definition)
10902 Private_Dependents (Base_Type (Formal_Type)));
10904 -- Freezing is delayed to ensure that Register_Prim
10905 -- will get called for this operation, which is needed
10906 -- in cases where static dispatch tables aren't built.
10907 -- (Note that the same is done for controlling access
10908 -- parameter cases in function Access_Definition.)
10910 Set_Has_Delayed_Freeze (Current_Scope);
10914 -- Special handling of Value_Type for CIL case
10916 elsif Is_Value_Type (Formal_Type) then
10919 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10920 N_Access_Procedure_Definition)
10922 -- AI05-0151: Tagged incomplete types are allowed in all
10923 -- formal parts. Untagged incomplete types are not allowed
10926 if Ada_Version >= Ada_2012 then
10927 if Is_Tagged_Type (Formal_Type) then
10930 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10935 ("invalid use of untagged incomplete type&",
10936 Ptype, Formal_Type);
10941 ("invalid use of incomplete type&",
10942 Param_Spec, Formal_Type);
10944 -- Further checks on the legality of incomplete types
10945 -- in formal parts are delayed until the freeze point
10946 -- of the enclosing subprogram or access to subprogram.
10950 elsif Ekind (Formal_Type) = E_Void then
10952 ("premature use of&",
10953 Parameter_Type (Param_Spec), Formal_Type);
10956 -- Ada 2012 (AI-142): Handle aliased parameters
10958 if Ada_Version >= Ada_2012
10959 and then Aliased_Present (Param_Spec)
10961 Set_Is_Aliased (Formal);
10964 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10965 -- declaration corresponding to the null-excluding type of the
10966 -- formal in the enclosing scope. Finally, replace the parameter
10967 -- type of the formal with the internal subtype.
10969 if Ada_Version >= Ada_2005
10970 and then Null_Exclusion_Present (Param_Spec)
10972 if not Is_Access_Type (Formal_Type) then
10974 ("`NOT NULL` allowed only for an access type", Param_Spec);
10977 if Can_Never_Be_Null (Formal_Type)
10978 and then Comes_From_Source (Related_Nod)
10981 ("`NOT NULL` not allowed (& already excludes null)",
10982 Param_Spec, Formal_Type);
10986 Create_Null_Excluding_Itype
10988 Related_Nod => Related_Nod,
10989 Scope_Id => Scope (Current_Scope));
10991 -- If the designated type of the itype is an itype that is
10992 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10993 -- on the access subtype, to prevent order-of-elaboration
10994 -- issues in the backend.
10997 -- type T is access procedure;
10998 -- procedure Op (O : not null T);
11000 if Is_Itype (Directly_Designated_Type (Formal_Type))
11002 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11004 Set_Has_Delayed_Freeze (Formal_Type);
11009 -- An access formal type
11013 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11015 -- No need to continue if we already notified errors
11017 if not Present (Formal_Type) then
11021 -- Ada 2005 (AI-254)
11024 AD : constant Node_Id :=
11025 Access_To_Subprogram_Definition
11026 (Parameter_Type (Param_Spec));
11028 if Present (AD) and then Protected_Present (AD) then
11030 Replace_Anonymous_Access_To_Protected_Subprogram
11036 Set_Etype (Formal, Formal_Type);
11038 -- Deal with default expression if present
11040 Default := Expression (Param_Spec);
11042 if Present (Default) then
11043 Check_SPARK_Restriction
11044 ("default expression is not allowed", Default);
11046 if Out_Present (Param_Spec) then
11048 ("default initialization only allowed for IN parameters",
11052 -- Do the special preanalysis of the expression (see section on
11053 -- "Handling of Default Expressions" in the spec of package Sem).
11055 Preanalyze_Spec_Expression (Default, Formal_Type);
11057 -- An access to constant cannot be the default for
11058 -- an access parameter that is an access to variable.
11060 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11061 and then not Is_Access_Constant (Formal_Type)
11062 and then Is_Access_Type (Etype (Default))
11063 and then Is_Access_Constant (Etype (Default))
11066 ("formal that is access to variable cannot be initialized " &
11067 "with an access-to-constant expression", Default);
11070 -- Check that the designated type of an access parameter's default
11071 -- is not a class-wide type unless the parameter's designated type
11072 -- is also class-wide.
11074 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11075 and then not Designates_From_With_Type (Formal_Type)
11076 and then Is_Class_Wide_Default (Default)
11077 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11080 ("access to class-wide expression not allowed here", Default);
11083 -- Check incorrect use of dynamically tagged expressions
11085 if Is_Tagged_Type (Formal_Type) then
11086 Check_Dynamically_Tagged_Expression
11088 Typ => Formal_Type,
11089 Related_Nod => Default);
11093 -- Ada 2005 (AI-231): Static checks
11095 if Ada_Version >= Ada_2005
11096 and then Is_Access_Type (Etype (Formal))
11097 and then Can_Never_Be_Null (Etype (Formal))
11099 Null_Exclusion_Static_Checks (Param_Spec);
11106 -- If this is the formal part of a function specification, analyze the
11107 -- subtype mark in the context where the formals are visible but not
11108 -- yet usable, and may hide outer homographs.
11110 if Nkind (Related_Nod) = N_Function_Specification then
11111 Analyze_Return_Type (Related_Nod);
11114 -- Now set the kind (mode) of each formal
11116 Param_Spec := First (T);
11117 while Present (Param_Spec) loop
11118 Formal := Defining_Identifier (Param_Spec);
11119 Set_Formal_Mode (Formal);
11121 if Ekind (Formal) = E_In_Parameter then
11122 Set_Default_Value (Formal, Expression (Param_Spec));
11124 if Present (Expression (Param_Spec)) then
11125 Default := Expression (Param_Spec);
11127 if Is_Scalar_Type (Etype (Default)) then
11128 if Nkind (Parameter_Type (Param_Spec)) /=
11129 N_Access_Definition
11131 Formal_Type := Entity (Parameter_Type (Param_Spec));
11135 (Related_Nod, Parameter_Type (Param_Spec));
11138 Apply_Scalar_Range_Check (Default, Formal_Type);
11142 elsif Ekind (Formal) = E_Out_Parameter then
11143 Num_Out_Params := Num_Out_Params + 1;
11145 if Num_Out_Params = 1 then
11146 First_Out_Param := Formal;
11149 elsif Ekind (Formal) = E_In_Out_Parameter then
11150 Num_Out_Params := Num_Out_Params + 1;
11153 -- Skip remaining processing if formal type was in error
11155 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11156 goto Next_Parameter;
11159 -- Force call by reference if aliased
11161 if Is_Aliased (Formal) then
11162 Set_Mechanism (Formal, By_Reference);
11164 -- Warn if user asked this to be passed by copy
11166 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11168 ("cannot pass aliased parameter & by copy?", Formal);
11171 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11173 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11174 Set_Mechanism (Formal, By_Copy);
11176 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11177 Set_Mechanism (Formal, By_Reference);
11184 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11185 Set_Is_Only_Out_Parameter (First_Out_Param);
11187 end Process_Formals;
11193 procedure Process_PPCs
11195 Spec_Id : Entity_Id;
11196 Body_Id : Entity_Id)
11198 Loc : constant Source_Ptr := Sloc (N);
11202 Designator : Entity_Id;
11203 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11205 Precond : Node_Id := Empty;
11206 -- Set non-Empty if we prepend precondition to the declarations. This
11207 -- is used to hook up inherited preconditions (adding the condition
11208 -- expression with OR ELSE, and adding the message).
11210 Inherited_Precond : Node_Id;
11211 -- Precondition inherited from parent subprogram
11213 Inherited : constant Subprogram_List :=
11214 Inherited_Subprograms (Spec_Id);
11215 -- List of subprograms inherited by this subprogram
11217 Plist : List_Id := No_List;
11218 -- List of generated postconditions
11220 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
11221 -- Append a node to a list. If there is no list, create a new one. When
11222 -- the item denotes a pragma, it is added to the list only when it is
11225 procedure Check_Access_Invariants (E : Entity_Id);
11226 -- If the subprogram returns an access to a type with invariants, or
11227 -- has access parameters whose designated type has an invariant, then
11228 -- under the same visibility conditions as for other invariant checks,
11229 -- the type invariant must be applied to the returned value.
11231 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id);
11232 -- Given pragma Contract_Cases CCs, create the circuitry needed to
11233 -- evaluate case guards and trigger consequence expressions. Subp_Id
11234 -- denotes the related subprogram.
11236 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11237 -- Prag contains an analyzed precondition or postcondition pragma. This
11238 -- function copies the pragma, changes it to the corresponding Check
11239 -- pragma and returns the Check pragma as the result. If Pspec is non-
11240 -- empty, this is the case of inheriting a PPC, where we must change
11241 -- references to parameters of the inherited subprogram to point to the
11242 -- corresponding parameters of the current subprogram.
11244 function Has_Checked_Predicate (Typ : Entity_Id) return Boolean;
11245 -- Determine whether type Typ has or inherits at least one predicate
11246 -- aspect or pragma, for which the applicable policy is Checked.
11248 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
11249 -- Determine whether the body of procedure Proc_Id contains a sole null
11250 -- statement, possibly followed by an optional return.
11252 procedure Insert_After_Last_Declaration (Nod : Node_Id);
11253 -- Insert node Nod after the last declaration of the context
11255 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11256 -- T is the entity for a private type for which invariants are defined.
11257 -- This function returns True if the procedure corresponding to the
11258 -- value of Designator is a public procedure from the point of view of
11259 -- this type (i.e. its spec is in the visible part of the package that
11260 -- contains the declaration of the private type). A True value means
11261 -- that an invariant check is required (for an IN OUT parameter, or
11262 -- the returned value of a function.
11264 -------------------------
11265 -- Append_Enabled_Item --
11266 -------------------------
11268 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
11270 -- Do not chain ignored or disabled pragmas
11272 if Nkind (Item) = N_Pragma
11273 and then (Is_Ignored (Item) or else Is_Disabled (Item))
11284 Append (Item, List);
11286 end Append_Enabled_Item;
11288 -----------------------------
11289 -- Check_Access_Invariants --
11290 -----------------------------
11292 procedure Check_Access_Invariants (E : Entity_Id) is
11298 if Is_Access_Type (Etype (E))
11299 and then not Is_Access_Constant (Etype (E))
11301 Typ := Designated_Type (Etype (E));
11303 if Has_Invariants (Typ)
11304 and then Present (Invariant_Procedure (Typ))
11305 and then not Has_Null_Body (Invariant_Procedure (Typ))
11306 and then Is_Public_Subprogram_For (Typ)
11309 Make_Explicit_Dereference (Loc,
11310 Prefix => New_Occurrence_Of (E, Loc));
11311 Set_Etype (Obj, Typ);
11313 Call := Make_Invariant_Call (Obj);
11315 Append_Enabled_Item
11316 (Make_If_Statement (Loc,
11319 Left_Opnd => Make_Null (Loc),
11320 Right_Opnd => New_Occurrence_Of (E, Loc)),
11321 Then_Statements => New_List (Call)),
11325 end Check_Access_Invariants;
11327 ---------------------------
11328 -- Expand_Contract_Cases --
11329 ---------------------------
11331 -- Pragma Contract_Cases is expanded in the following manner:
11334 -- Flag_1 : Boolean := False;
11336 -- Flag_N : Boolean := False;
11337 -- Flag_N+1 : Boolean := False; -- when "others" present
11338 -- Count : Natural := 0;
11340 -- <preconditions (if any)>
11342 -- if Case_Guard_1 then
11344 -- Count := Count + 1;
11347 -- if Case_Guard_N then
11349 -- Count := Count + 1;
11352 -- if Count = 0 then
11353 -- raise Assertion_Error with "xxx contract cases incomplete";
11355 -- Flag_N+1 := True; -- when "others" present
11357 -- elsif Count > 1 then
11359 -- Str0 : constant String :=
11360 -- "contract cases overlap for subprogram ABC";
11361 -- Str1 : constant String :=
11363 -- Str0 & "case guard at xxx evaluates to True"
11365 -- StrN : constant String :=
11367 -- StrN-1 & "case guard at xxx evaluates to True"
11370 -- raise Assertion_Error with StrN;
11374 -- procedure _Postconditions is
11376 -- <postconditions (if any)>
11378 -- if Flag_1 and then not Consequence_1 then
11379 -- raise Assertion_Error with "failed contract case at xxx";
11382 -- if Flag_N[+1] and then not Consequence_N[+1] then
11383 -- raise Assertion_Error with "failed contract case at xxx";
11385 -- end _Postconditions;
11390 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id) is
11391 Loc : constant Source_Ptr := Sloc (CCs);
11393 procedure Case_Guard_Error
11396 Error_Loc : Source_Ptr;
11397 Msg : in out Entity_Id);
11398 -- Given a declarative list Decls, status flag Flag, the location of
11399 -- the error and a string Msg, construct the following check:
11400 -- Msg : constant String :=
11402 -- Msg & "case guard at Error_Loc evaluates to True"
11404 -- The resulting code is added to Decls
11406 procedure Consequence_Error
11407 (Checks : in out Node_Id;
11410 -- Given an if statement Checks, status flag Flag and a consequence
11411 -- Conseq, construct the following check:
11412 -- [els]if Flag and then not Conseq then
11413 -- raise Assertion_Error
11414 -- with "failed contract case at Sloc (Conseq)";
11416 -- The resulting code is added to Checks
11418 function Declaration_Of (Id : Entity_Id) return Node_Id;
11419 -- Given the entity Id of a boolean flag, generate:
11420 -- Id : Boolean := False;
11422 function Increment (Id : Entity_Id) return Node_Id;
11423 -- Given the entity Id of a numerical variable, generate:
11426 function Set (Id : Entity_Id) return Node_Id;
11427 -- Given the entity Id of a boolean variable, generate:
11430 ----------------------
11431 -- Case_Guard_Error --
11432 ----------------------
11434 procedure Case_Guard_Error
11437 Error_Loc : Source_Ptr;
11438 Msg : in out Entity_Id)
11440 New_Line : constant Character := Character'Val (10);
11441 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
11445 Store_String_Char (New_Line);
11446 Store_String_Chars (" case guard at ");
11447 Store_String_Chars (Build_Location_String (Error_Loc));
11448 Store_String_Chars (" evaluates to True");
11451 -- New_Msg : constant String :=
11453 -- Msg & "case guard at Error_Loc evaluates to True"
11457 Make_Object_Declaration (Loc,
11458 Defining_Identifier => New_Msg,
11459 Constant_Present => True,
11460 Object_Definition => New_Reference_To (Standard_String, Loc),
11462 Make_If_Expression (Loc,
11463 Expressions => New_List (
11464 New_Reference_To (Flag, Loc),
11466 Make_Op_Concat (Loc,
11467 Left_Opnd => New_Reference_To (Msg, Loc),
11468 Right_Opnd => Make_String_Literal (Loc, End_String)),
11470 New_Reference_To (Msg, Loc)))));
11473 end Case_Guard_Error;
11475 -----------------------
11476 -- Consequence_Error --
11477 -----------------------
11479 procedure Consequence_Error
11480 (Checks : in out Node_Id;
11489 -- Flag and then not Conseq
11492 Make_And_Then (Loc,
11493 Left_Opnd => New_Reference_To (Flag, Loc),
11496 Right_Opnd => Relocate_Node (Conseq)));
11499 -- raise Assertion_Error
11500 -- with "failed contract case at Sloc (Conseq)";
11503 Store_String_Chars ("failed contract case at ");
11504 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
11507 Make_Procedure_Call_Statement (Loc,
11509 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11510 Parameter_Associations => New_List (
11511 Make_String_Literal (Loc, End_String)));
11513 if No (Checks) then
11515 Make_If_Statement (Loc,
11517 Then_Statements => New_List (Error));
11520 if No (Elsif_Parts (Checks)) then
11521 Set_Elsif_Parts (Checks, New_List);
11524 Append_To (Elsif_Parts (Checks),
11525 Make_Elsif_Part (Loc,
11527 Then_Statements => New_List (Error)));
11529 end Consequence_Error;
11531 --------------------
11532 -- Declaration_Of --
11533 --------------------
11535 function Declaration_Of (Id : Entity_Id) return Node_Id is
11538 Make_Object_Declaration (Loc,
11539 Defining_Identifier => Id,
11540 Object_Definition =>
11541 New_Reference_To (Standard_Boolean, Loc),
11543 New_Reference_To (Standard_False, Loc));
11544 end Declaration_Of;
11550 function Increment (Id : Entity_Id) return Node_Id is
11553 Make_Assignment_Statement (Loc,
11554 Name => New_Reference_To (Id, Loc),
11557 Left_Opnd => New_Reference_To (Id, Loc),
11558 Right_Opnd => Make_Integer_Literal (Loc, 1)));
11565 function Set (Id : Entity_Id) return Node_Id is
11568 Make_Assignment_Statement (Loc,
11569 Name => New_Reference_To (Id, Loc),
11570 Expression => New_Reference_To (Standard_True, Loc));
11575 Aggr : constant Node_Id :=
11577 (Pragma_Argument_Associations (CCs)));
11578 Decls : constant List_Id := Declarations (N);
11579 Case_Guard : Node_Id;
11580 CG_Checks : Node_Id;
11581 CG_Stmts : List_Id;
11583 Conseq_Checks : Node_Id := Empty;
11585 Error_Decls : List_Id;
11587 Msg_Str : Entity_Id;
11588 Multiple_PCs : Boolean;
11589 Others_Flag : Entity_Id := Empty;
11590 Post_Case : Node_Id;
11592 -- Start of processing for Expand_Contract_Cases
11595 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
11596 -- already been rewritten as a Null statement.
11598 if Is_Ignored (CCs) then
11601 -- Guard against malformed contract cases
11603 elsif Nkind (Aggr) /= N_Aggregate then
11607 Multiple_PCs := List_Length (Component_Associations (Aggr)) > 1;
11609 -- Create the counter which tracks the number of case guards that
11610 -- evaluate to True.
11612 -- Count : Natural := 0;
11614 Count := Make_Temporary (Loc, 'C');
11617 Make_Object_Declaration (Loc,
11618 Defining_Identifier => Count,
11619 Object_Definition => New_Reference_To (Standard_Natural, Loc),
11620 Expression => Make_Integer_Literal (Loc, 0)));
11622 -- Create the base error message for multiple overlapping case
11625 -- Msg_Str : constant String :=
11626 -- "contract cases overlap for subprogram Subp_Id";
11628 if Multiple_PCs then
11629 Msg_Str := Make_Temporary (Loc, 'S');
11632 Store_String_Chars ("contract cases overlap for subprogram ");
11633 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
11635 Error_Decls := New_List (
11636 Make_Object_Declaration (Loc,
11637 Defining_Identifier => Msg_Str,
11638 Constant_Present => True,
11639 Object_Definition => New_Reference_To (Standard_String, Loc),
11640 Expression => Make_String_Literal (Loc, End_String)));
11643 -- Process individual post cases
11645 Post_Case := First (Component_Associations (Aggr));
11646 while Present (Post_Case) loop
11647 Case_Guard := First (Choices (Post_Case));
11648 Conseq := Expression (Post_Case);
11650 -- The "others" choice requires special processing
11652 if Nkind (Case_Guard) = N_Others_Choice then
11653 Others_Flag := Make_Temporary (Loc, 'F');
11654 Prepend_To (Decls, Declaration_Of (Others_Flag));
11656 -- Check possible overlap between a case guard and "others"
11658 if Multiple_PCs then
11660 (Decls => Error_Decls,
11661 Flag => Others_Flag,
11662 Error_Loc => Sloc (Case_Guard),
11666 -- Check the corresponding consequence of "others"
11669 (Checks => Conseq_Checks,
11670 Flag => Others_Flag,
11673 -- Regular post case
11676 -- Create the flag which tracks the state of its associated
11679 Flag := Make_Temporary (Loc, 'F');
11680 Prepend_To (Decls, Declaration_Of (Flag));
11682 -- The flag is set when the case guard is evaluated to True
11683 -- if Case_Guard then
11685 -- Count := Count + 1;
11689 Make_If_Statement (Loc,
11690 Condition => Relocate_Node (Case_Guard),
11691 Then_Statements => New_List (
11693 Increment (Count))));
11695 -- Check whether this case guard overlaps with another case
11698 if Multiple_PCs then
11700 (Decls => Error_Decls,
11702 Error_Loc => Sloc (Case_Guard),
11706 -- The corresponding consequence of the case guard which
11707 -- evaluated to True must hold on exit from the subprogram.
11709 Consequence_Error (Conseq_Checks, Flag, Conseq);
11715 -- Raise Assertion_Error when none of the case guards evaluate to
11716 -- True. The only exception is when we have "others", in which case
11717 -- there is no error because "others" acts as a default True.
11722 if Present (Others_Flag) then
11723 CG_Stmts := New_List (Set (Others_Flag));
11726 -- raise Assertion_Error with "xxx contract cases incomplete";
11730 Store_String_Chars (Build_Location_String (Loc));
11731 Store_String_Chars (" contract cases incomplete");
11733 CG_Stmts := New_List (
11734 Make_Procedure_Call_Statement (Loc,
11736 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11737 Parameter_Associations => New_List (
11738 Make_String_Literal (Loc, End_String))));
11742 Make_If_Statement (Loc,
11745 Left_Opnd => New_Reference_To (Count, Loc),
11746 Right_Opnd => Make_Integer_Literal (Loc, 0)),
11747 Then_Statements => CG_Stmts);
11749 -- Detect a possible failure due to several case guards evaluating to
11753 -- elsif Count > 0 then
11757 -- raise Assertion_Error with <Msg_Str>;
11760 if Multiple_PCs then
11761 Set_Elsif_Parts (CG_Checks, New_List (
11762 Make_Elsif_Part (Loc,
11765 Left_Opnd => New_Reference_To (Count, Loc),
11766 Right_Opnd => Make_Integer_Literal (Loc, 1)),
11768 Then_Statements => New_List (
11769 Make_Block_Statement (Loc,
11770 Declarations => Error_Decls,
11771 Handled_Statement_Sequence =>
11772 Make_Handled_Sequence_Of_Statements (Loc,
11773 Statements => New_List (
11774 Make_Procedure_Call_Statement (Loc,
11777 (RTE (RE_Raise_Assert_Failure), Loc),
11778 Parameter_Associations => New_List (
11779 New_Reference_To (Msg_Str, Loc))))))))));
11782 Append_To (Decls, CG_Checks);
11784 -- Raise Assertion_Error when the corresponding consequence of a case
11785 -- guard that evaluated to True fails.
11787 Append_Enabled_Item (Conseq_Checks, Plist);
11788 end Expand_Contract_Cases;
11794 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
11795 Nam : constant Name_Id := Pragma_Name (Prag);
11800 -- Effective name of pragma (maybe Pre/Post rather than Precondition/
11801 -- Postcodition if the pragma came from a Pre/Post aspect). We need
11802 -- the name right when we generate the Check pragma, since we want
11803 -- the right set of check policies to apply.
11806 -- Prepare map if this is the case where we have to map entities of
11807 -- arguments in the overridden subprogram to corresponding entities
11808 -- of the current subprogram.
11819 Map := New_Elmt_List;
11820 PF := First_Formal (Pspec);
11821 CF := First_Formal (Designator);
11822 while Present (PF) loop
11823 Append_Elmt (PF, Map);
11824 Append_Elmt (CF, Map);
11831 -- Now we can copy the tree, doing any required substitutions
11833 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
11835 -- Set Analyzed to false, since we want to reanalyze the check
11836 -- procedure. Note that it is only at the outer level that we
11837 -- do this fiddling, for the spec cases, the already preanalyzed
11838 -- parameters are not affected.
11840 Set_Analyzed (CP, False);
11842 -- We also make sure Comes_From_Source is False for the copy
11844 Set_Comes_From_Source (CP, False);
11846 -- For a postcondition pragma within a generic, preserve the pragma
11847 -- for later expansion. This is also used when an error was detected,
11848 -- thus setting Expander_Active to False.
11850 if Nam = Name_Postcondition
11851 and then not Expander_Active
11856 -- Get effective name of aspect
11858 if Present (Corresponding_Aspect (Prag)) then
11859 Ename := Chars (Identifier (Corresponding_Aspect (Prag)));
11864 -- Change copy of pragma into corresponding pragma Check
11866 Prepend_To (Pragma_Argument_Associations (CP),
11867 Make_Pragma_Argument_Association (Sloc (Prag),
11868 Expression => Make_Identifier (Loc, Ename)));
11869 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
11871 -- If this is inherited case and the current message starts with
11872 -- "failed p", we change it to "failed inherited p...".
11874 if Present (Pspec) then
11876 Msg : constant Node_Id :=
11877 Last (Pragma_Argument_Associations (CP));
11880 if Chars (Msg) = Name_Message then
11881 String_To_Name_Buffer (Strval (Expression (Msg)));
11883 if Name_Buffer (1 .. 8) = "failed p" then
11884 Insert_Str_In_Name_Buffer ("inherited ", 8);
11886 (Expression (Last (Pragma_Argument_Associations (CP))),
11887 String_From_Name_Buffer);
11893 -- Return the check pragma
11898 ---------------------------
11899 -- Has_Checked_Predicate --
11900 ---------------------------
11902 function Has_Checked_Predicate (Typ : Entity_Id) return Boolean is
11907 -- Climb the ancestor type chain staring from the input. This is done
11908 -- because the input type may lack aspect/pragma predicate and simply
11909 -- inherit those from its ancestor.
11911 -- Note that predicate pragmas include all three cases of predicate
11912 -- aspects (Predicate, Dynamic_Predicate, Static_Predicate), so this
11913 -- routine checks for all three cases.
11916 while Present (Anc) loop
11917 Pred := Get_Pragma (Anc, Pragma_Predicate);
11919 if Present (Pred) and then not Is_Ignored (Pred) then
11923 Anc := Nearest_Ancestor (Anc);
11927 end Has_Checked_Predicate;
11929 -------------------
11930 -- Has_Null_Body --
11931 -------------------
11933 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
11934 Body_Id : Entity_Id;
11941 Spec := Parent (Proc_Id);
11942 Decl := Parent (Spec);
11944 -- Retrieve the entity of the invariant procedure body
11946 if Nkind (Spec) = N_Procedure_Specification
11947 and then Nkind (Decl) = N_Subprogram_Declaration
11949 Body_Id := Corresponding_Body (Decl);
11951 -- The body acts as a spec
11954 Body_Id := Proc_Id;
11957 -- The body will be generated later
11959 if No (Body_Id) then
11963 Spec := Parent (Body_Id);
11964 Decl := Parent (Spec);
11967 (Nkind (Spec) = N_Procedure_Specification
11968 and then Nkind (Decl) = N_Subprogram_Body);
11970 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
11972 -- Look for a null statement followed by an optional return statement
11974 if Nkind (Stmt1) = N_Null_Statement then
11975 Stmt2 := Next (Stmt1);
11977 if Present (Stmt2) then
11978 return Nkind (Stmt2) = N_Simple_Return_Statement;
11987 -----------------------------------
11988 -- Insert_After_Last_Declaration --
11989 -----------------------------------
11991 procedure Insert_After_Last_Declaration (Nod : Node_Id) is
11992 Decls : constant List_Id := Declarations (N);
11996 Set_Declarations (N, New_List (Nod));
11998 Append_To (Decls, Nod);
12000 end Insert_After_Last_Declaration;
12002 ------------------------------
12003 -- Is_Public_Subprogram_For --
12004 ------------------------------
12006 -- The type T is a private type, its declaration is therefore in
12007 -- the list of public declarations of some package. The test for a
12008 -- public subprogram is that its declaration is in this same list
12009 -- of declarations for the same package (note that all the public
12010 -- declarations are in one list, and all the private declarations
12011 -- in another, so this deals with the public/private distinction).
12013 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
12014 DD : constant Node_Id := Unit_Declaration_Node (Designator);
12015 -- The subprogram declaration for the subprogram in question
12017 TL : constant List_Id :=
12018 Visible_Declarations
12019 (Specification (Unit_Declaration_Node (Scope (T))));
12020 -- The list of declarations containing the private declaration of
12021 -- the type. We know it is a private type, so we know its scope is
12022 -- the package in question, and we know it must be in the visible
12023 -- declarations of this package.
12026 -- If the subprogram declaration is not a list member, it must be
12027 -- an Init_Proc, in which case we want to consider it to be a
12028 -- public subprogram, since we do get initializations to deal with.
12029 -- Other internally generated subprograms are not public.
12031 if not Is_List_Member (DD)
12032 and then Is_Init_Proc (Defining_Entity (DD))
12036 -- The declaration may have been generated for an expression function
12037 -- so check whether that function comes from source.
12039 elsif not Comes_From_Source (DD)
12041 (Nkind (Original_Node (DD)) /= N_Expression_Function
12042 or else not Comes_From_Source (Defining_Entity (DD)))
12046 -- Otherwise we test whether the subprogram is declared in the
12047 -- visible declarations of the package containing the type.
12050 return TL = List_Containing (DD);
12052 end Is_Public_Subprogram_For;
12057 Formal_Typ : Entity_Id;
12058 Func_Typ : Entity_Id;
12059 Post_Proc : Entity_Id;
12062 -- Start of processing for Process_PPCs
12065 -- Capture designator from spec if present, else from body
12067 if Present (Spec_Id) then
12068 Designator := Spec_Id;
12070 Designator := Body_Id;
12073 -- Do not process a predicate function as its body will contain a
12074 -- recursive call to itself and blow up the stack.
12076 if Ekind (Designator) = E_Function
12077 and then Is_Predicate_Function (Designator)
12081 -- Internally generated subprograms, such as type-specific functions,
12082 -- don't get assertion checks.
12084 elsif Get_TSS_Name (Designator) /= TSS_Null then
12088 -- Grab preconditions from spec
12090 if Present (Spec_Id) then
12092 -- Loop through PPC pragmas from spec. Note that preconditions from
12093 -- the body will be analyzed and converted when we scan the body
12094 -- declarations below.
12096 Prag := Pre_Post_Conditions (Contract (Spec_Id));
12097 while Present (Prag) loop
12098 if Pragma_Name (Prag) = Name_Precondition then
12100 -- For Pre (or Precondition pragma), we simply prepend the
12101 -- pragma to the list of declarations right away so that it
12102 -- will be executed at the start of the procedure. Note that
12103 -- this processing reverses the order of the list, which is
12104 -- what we want since new entries were chained to the head of
12105 -- the list. There can be more than one precondition when we
12106 -- use pragma Precondition.
12108 if not Class_Present (Prag) then
12109 Prepend (Grab_PPC, Declarations (N));
12111 -- For Pre'Class there can only be one pragma, and we save
12112 -- it in Precond for now. We will add inherited Pre'Class
12113 -- stuff before inserting this pragma in the declarations.
12115 Precond := Grab_PPC;
12119 Prag := Next_Pragma (Prag);
12122 -- Now deal with inherited preconditions
12124 for J in Inherited'Range loop
12125 Prag := Pre_Post_Conditions (Contract (Inherited (J)));
12127 while Present (Prag) loop
12128 if Pragma_Name (Prag) = Name_Precondition
12129 and then Class_Present (Prag)
12131 Inherited_Precond := Grab_PPC (Inherited (J));
12133 -- No precondition so far, so establish this as the first
12135 if No (Precond) then
12136 Precond := Inherited_Precond;
12138 -- Here we already have a precondition, add inherited one
12141 -- Add new precondition to old one using OR ELSE
12144 New_Expr : constant Node_Id :=
12146 (Next (First (Pragma_Argument_Associations
12147 (Inherited_Precond))));
12148 Old_Expr : constant Node_Id :=
12150 (Next (First (Pragma_Argument_Associations
12154 if Paren_Count (Old_Expr) = 0 then
12155 Set_Paren_Count (Old_Expr, 1);
12158 if Paren_Count (New_Expr) = 0 then
12159 Set_Paren_Count (New_Expr, 1);
12163 Make_Or_Else (Sloc (Old_Expr),
12164 Left_Opnd => Relocate_Node (Old_Expr),
12165 Right_Opnd => New_Expr));
12168 -- Add new message in the form:
12170 -- failed precondition from bla
12171 -- also failed inherited precondition from bla
12174 -- Skip this if exception locations are suppressed
12176 if not Exception_Locations_Suppressed then
12178 New_Msg : constant Node_Id :=
12181 (Pragma_Argument_Associations
12182 (Inherited_Precond)));
12183 Old_Msg : constant Node_Id :=
12186 (Pragma_Argument_Associations
12189 Start_String (Strval (Old_Msg));
12190 Store_String_Chars (ASCII.LF & " also ");
12191 Store_String_Chars (Strval (New_Msg));
12192 Set_Strval (Old_Msg, End_String);
12198 Prag := Next_Pragma (Prag);
12202 -- If we have built a precondition for Pre'Class (including any
12203 -- Pre'Class aspects inherited from parent subprograms), then we
12204 -- insert this composite precondition at this stage.
12206 if Present (Precond) then
12207 Prepend (Precond, Declarations (N));
12211 -- Build postconditions procedure if needed and prepend the following
12212 -- declaration to the start of the declarations for the subprogram.
12214 -- procedure _postconditions [(_Result : resulttype)] is
12216 -- pragma Check (Postcondition, condition [,message]);
12217 -- pragma Check (Postcondition, condition [,message]);
12219 -- Invariant_Procedure (_Result) ...
12220 -- Invariant_Procedure (Arg1)
12224 -- First we deal with the postconditions in the body
12226 if Is_Non_Empty_List (Declarations (N)) then
12228 -- Loop through declarations
12230 Prag := First (Declarations (N));
12231 while Present (Prag) loop
12232 if Nkind (Prag) = N_Pragma then
12234 -- Capture postcondition pragmas
12236 if Pragma_Name (Prag) = Name_Postcondition then
12239 -- If expansion is disabled, as in a generic unit, save
12240 -- pragma for later expansion.
12242 if not Expander_Active then
12243 Prepend (Grab_PPC, Declarations (N));
12245 Append_Enabled_Item (Grab_PPC, Plist);
12251 -- Not a pragma, if comes from source, then end scan
12253 elsif Comes_From_Source (Prag) then
12256 -- Skip stuff not coming from source
12264 -- Now deal with any postconditions from the spec
12266 if Present (Spec_Id) then
12267 Spec_Postconditions : declare
12268 procedure Process_Contract_Cases (Spec : Node_Id);
12269 -- This processes the Contract_Test_Cases from Spec, processing
12270 -- any contract-cases from the list. The caller has checked that
12271 -- Contract_Test_Cases is non-Empty.
12273 procedure Process_Post_Conditions
12276 -- This processes the Pre_Post_Conditions from Spec, processing
12277 -- any postconditions from the list. If Class is True, then only
12278 -- postconditions marked with Class_Present are considered. The
12279 -- caller has checked that Pre_Post_Conditions is non-Empty.
12281 ----------------------------
12282 -- Process_Contract_Cases --
12283 ----------------------------
12285 procedure Process_Contract_Cases (Spec : Node_Id) is
12287 -- Loop through Contract_Cases pragmas from spec
12289 Prag := Contract_Test_Cases (Contract (Spec));
12291 if Pragma_Name (Prag) = Name_Contract_Cases then
12292 Expand_Contract_Cases (Prag, Spec_Id);
12295 Prag := Next_Pragma (Prag);
12296 exit when No (Prag);
12298 end Process_Contract_Cases;
12300 -----------------------------
12301 -- Process_Post_Conditions --
12302 -----------------------------
12304 procedure Process_Post_Conditions
12317 -- Loop through PPC pragmas from spec
12319 Prag := Pre_Post_Conditions (Contract (Spec));
12321 if Pragma_Name (Prag) = Name_Postcondition
12322 and then (not Class or else Class_Present (Prag))
12324 if not Expander_Active then
12325 Prepend (Grab_PPC (Pspec), Declarations (N));
12327 Append_Enabled_Item (Grab_PPC (Pspec), Plist);
12331 Prag := Next_Pragma (Prag);
12332 exit when No (Prag);
12334 end Process_Post_Conditions;
12336 -- Start of processing for Spec_Postconditions
12339 -- Process postconditions expressed as contract-cases
12341 if Present (Contract_Test_Cases (Contract (Spec_Id))) then
12342 Process_Contract_Cases (Spec_Id);
12345 -- Process spec postconditions
12347 if Present (Pre_Post_Conditions (Contract (Spec_Id))) then
12348 Process_Post_Conditions (Spec_Id, Class => False);
12351 -- Process inherited postconditions
12353 for J in Inherited'Range loop
12354 if Present (Pre_Post_Conditions (Contract (Inherited (J)))) then
12355 Process_Post_Conditions (Inherited (J), Class => True);
12358 end Spec_Postconditions;
12361 -- Add an invariant call to check the result of a function
12363 if Ekind (Designator) /= E_Procedure and then Expander_Active then
12364 Func_Typ := Etype (Designator);
12365 Result := Make_Defining_Identifier (Loc, Name_uResult);
12367 Set_Etype (Result, Func_Typ);
12369 -- Add argument for return
12371 Parms := New_List (
12372 Make_Parameter_Specification (Loc,
12373 Defining_Identifier => Result,
12374 Parameter_Type => New_Occurrence_Of (Func_Typ, Loc)));
12376 -- Add invariant call if returning type with invariants and this is a
12377 -- public function, i.e. a function declared in the visible part of
12378 -- the package defining the private type.
12380 if Has_Invariants (Func_Typ)
12381 and then Present (Invariant_Procedure (Func_Typ))
12382 and then not Has_Null_Body (Invariant_Procedure (Func_Typ))
12383 and then Is_Public_Subprogram_For (Func_Typ)
12385 Append_Enabled_Item
12386 (Make_Invariant_Call (New_Occurrence_Of (Result, Loc)), Plist);
12389 -- Same if return value is an access to type with invariants
12391 Check_Access_Invariants (Result);
12399 -- Add invariant calls and predicate calls for parameters. Note that
12400 -- this is done for functions as well, since in Ada 2012 they can have
12403 if Expander_Active then
12404 Formal := First_Formal (Designator);
12405 while Present (Formal) loop
12406 if Ekind (Formal) /= E_In_Parameter
12407 or else Is_Access_Type (Etype (Formal))
12409 Formal_Typ := Etype (Formal);
12411 if Has_Invariants (Formal_Typ)
12412 and then Present (Invariant_Procedure (Formal_Typ))
12413 and then not Has_Null_Body (Invariant_Procedure (Formal_Typ))
12414 and then Is_Public_Subprogram_For (Formal_Typ)
12416 Append_Enabled_Item
12417 (Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
12421 Check_Access_Invariants (Formal);
12423 if Has_Predicates (Formal_Typ)
12424 and then Present (Predicate_Function (Formal_Typ))
12425 and then Has_Checked_Predicate (Formal_Typ)
12427 Append_Enabled_Item
12428 (Make_Predicate_Check
12429 (Formal_Typ, New_Occurrence_Of (Formal, Loc)),
12434 Next_Formal (Formal);
12438 -- Build and insert postcondition procedure
12440 if Expander_Active and then Present (Plist) then
12442 Make_Defining_Identifier (Loc, Chars => Name_uPostconditions);
12444 -- Insert the corresponding body of a post condition pragma after the
12445 -- last declaration of the context. This ensures that the body will
12446 -- not cause any premature freezing as it may mention types:
12448 -- procedure Proc (Obj : Array_Typ) is
12449 -- procedure _postconditions is
12452 -- end _postconditions;
12454 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12457 -- In the example above, Obj is of type T but the incorrect placement
12458 -- of _postconditions will cause a crash in gigi due to an out of
12459 -- order reference. The body of _postconditions must be placed after
12460 -- the declaration of Temp to preserve correct visibility.
12462 Insert_After_Last_Declaration (
12463 Make_Subprogram_Body (Loc,
12465 Make_Procedure_Specification (Loc,
12466 Defining_Unit_Name => Post_Proc,
12467 Parameter_Specifications => Parms),
12469 Declarations => Empty_List,
12471 Handled_Statement_Sequence =>
12472 Make_Handled_Sequence_Of_Statements (Loc,
12473 Statements => Plist)));
12475 Set_Ekind (Post_Proc, E_Procedure);
12477 -- If this is a procedure, set the Postcondition_Proc attribute on
12478 -- the proper defining entity for the subprogram.
12480 if Ekind (Designator) = E_Procedure then
12481 Set_Postcondition_Proc (Designator, Post_Proc);
12484 Set_Has_Postconditions (Designator);
12488 ----------------------------
12489 -- Reference_Body_Formals --
12490 ----------------------------
12492 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12497 if Error_Posted (Spec) then
12501 -- Iterate over both lists. They may be of different lengths if the two
12502 -- specs are not conformant.
12504 Fs := First_Formal (Spec);
12505 Fb := First_Formal (Bod);
12506 while Present (Fs) and then Present (Fb) loop
12507 Generate_Reference (Fs, Fb, 'b');
12509 if Style_Check then
12510 Style.Check_Identifier (Fb, Fs);
12513 Set_Spec_Entity (Fb, Fs);
12514 Set_Referenced (Fs, False);
12518 end Reference_Body_Formals;
12520 -------------------------
12521 -- Set_Actual_Subtypes --
12522 -------------------------
12524 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12526 Formal : Entity_Id;
12528 First_Stmt : Node_Id := Empty;
12529 AS_Needed : Boolean;
12532 -- If this is an empty initialization procedure, no need to create
12533 -- actual subtypes (small optimization).
12535 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12539 Formal := First_Formal (Subp);
12540 while Present (Formal) loop
12541 T := Etype (Formal);
12543 -- We never need an actual subtype for a constrained formal
12545 if Is_Constrained (T) then
12546 AS_Needed := False;
12548 -- If we have unknown discriminants, then we do not need an actual
12549 -- subtype, or more accurately we cannot figure it out! Note that
12550 -- all class-wide types have unknown discriminants.
12552 elsif Has_Unknown_Discriminants (T) then
12553 AS_Needed := False;
12555 -- At this stage we have an unconstrained type that may need an
12556 -- actual subtype. For sure the actual subtype is needed if we have
12557 -- an unconstrained array type.
12559 elsif Is_Array_Type (T) then
12562 -- The only other case needing an actual subtype is an unconstrained
12563 -- record type which is an IN parameter (we cannot generate actual
12564 -- subtypes for the OUT or IN OUT case, since an assignment can
12565 -- change the discriminant values. However we exclude the case of
12566 -- initialization procedures, since discriminants are handled very
12567 -- specially in this context, see the section entitled "Handling of
12568 -- Discriminants" in Einfo.
12570 -- We also exclude the case of Discrim_SO_Functions (functions used
12571 -- in front end layout mode for size/offset values), since in such
12572 -- functions only discriminants are referenced, and not only are such
12573 -- subtypes not needed, but they cannot always be generated, because
12574 -- of order of elaboration issues.
12576 elsif Is_Record_Type (T)
12577 and then Ekind (Formal) = E_In_Parameter
12578 and then Chars (Formal) /= Name_uInit
12579 and then not Is_Unchecked_Union (T)
12580 and then not Is_Discrim_SO_Function (Subp)
12584 -- All other cases do not need an actual subtype
12587 AS_Needed := False;
12590 -- Generate actual subtypes for unconstrained arrays and
12591 -- unconstrained discriminated records.
12594 if Nkind (N) = N_Accept_Statement then
12596 -- If expansion is active, the formal is replaced by a local
12597 -- variable that renames the corresponding entry of the
12598 -- parameter block, and it is this local variable that may
12599 -- require an actual subtype.
12601 if Full_Expander_Active then
12602 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12604 Decl := Build_Actual_Subtype (T, Formal);
12607 if Present (Handled_Statement_Sequence (N)) then
12609 First (Statements (Handled_Statement_Sequence (N)));
12610 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12611 Mark_Rewrite_Insertion (Decl);
12613 -- If the accept statement has no body, there will be no
12614 -- reference to the actuals, so no need to compute actual
12621 Decl := Build_Actual_Subtype (T, Formal);
12622 Prepend (Decl, Declarations (N));
12623 Mark_Rewrite_Insertion (Decl);
12626 -- The declaration uses the bounds of an existing object, and
12627 -- therefore needs no constraint checks.
12629 Analyze (Decl, Suppress => All_Checks);
12631 -- We need to freeze manually the generated type when it is
12632 -- inserted anywhere else than in a declarative part.
12634 if Present (First_Stmt) then
12635 Insert_List_Before_And_Analyze (First_Stmt,
12636 Freeze_Entity (Defining_Identifier (Decl), N));
12639 if Nkind (N) = N_Accept_Statement
12640 and then Full_Expander_Active
12642 Set_Actual_Subtype (Renamed_Object (Formal),
12643 Defining_Identifier (Decl));
12645 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12649 Next_Formal (Formal);
12651 end Set_Actual_Subtypes;
12653 ---------------------
12654 -- Set_Formal_Mode --
12655 ---------------------
12657 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12658 Spec : constant Node_Id := Parent (Formal_Id);
12661 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12662 -- since we ensure that corresponding actuals are always valid at the
12663 -- point of the call.
12665 if Out_Present (Spec) then
12666 if Ekind (Scope (Formal_Id)) = E_Function
12667 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
12669 -- [IN] OUT parameters allowed for functions in Ada 2012
12671 if Ada_Version >= Ada_2012 then
12673 -- Even in Ada 2012 operators can only have IN parameters
12675 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12676 Error_Msg_N ("operators can only have IN parameters", Spec);
12679 if In_Present (Spec) then
12680 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12682 Set_Ekind (Formal_Id, E_Out_Parameter);
12685 -- But not in earlier versions of Ada
12688 Error_Msg_N ("functions can only have IN parameters", Spec);
12689 Set_Ekind (Formal_Id, E_In_Parameter);
12692 elsif In_Present (Spec) then
12693 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12696 Set_Ekind (Formal_Id, E_Out_Parameter);
12697 Set_Never_Set_In_Source (Formal_Id, True);
12698 Set_Is_True_Constant (Formal_Id, False);
12699 Set_Current_Value (Formal_Id, Empty);
12703 Set_Ekind (Formal_Id, E_In_Parameter);
12706 -- Set Is_Known_Non_Null for access parameters since the language
12707 -- guarantees that access parameters are always non-null. We also set
12708 -- Can_Never_Be_Null, since there is no way to change the value.
12710 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12712 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12713 -- null; In Ada 2005, only if then null_exclusion is explicit.
12715 if Ada_Version < Ada_2005
12716 or else Can_Never_Be_Null (Etype (Formal_Id))
12718 Set_Is_Known_Non_Null (Formal_Id);
12719 Set_Can_Never_Be_Null (Formal_Id);
12722 -- Ada 2005 (AI-231): Null-exclusion access subtype
12724 elsif Is_Access_Type (Etype (Formal_Id))
12725 and then Can_Never_Be_Null (Etype (Formal_Id))
12727 Set_Is_Known_Non_Null (Formal_Id);
12729 -- We can also set Can_Never_Be_Null (thus preventing some junk
12730 -- access checks) for the case of an IN parameter, which cannot
12731 -- be changed, or for an IN OUT parameter, which can be changed but
12732 -- not to a null value. But for an OUT parameter, the initial value
12733 -- passed in can be null, so we can't set this flag in that case.
12735 if Ekind (Formal_Id) /= E_Out_Parameter then
12736 Set_Can_Never_Be_Null (Formal_Id);
12740 Set_Mechanism (Formal_Id, Default_Mechanism);
12741 Set_Formal_Validity (Formal_Id);
12742 end Set_Formal_Mode;
12744 -------------------------
12745 -- Set_Formal_Validity --
12746 -------------------------
12748 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12750 -- If no validity checking, then we cannot assume anything about the
12751 -- validity of parameters, since we do not know there is any checking
12752 -- of the validity on the call side.
12754 if not Validity_Checks_On then
12757 -- If validity checking for parameters is enabled, this means we are
12758 -- not supposed to make any assumptions about argument values.
12760 elsif Validity_Check_Parameters then
12763 -- If we are checking in parameters, we will assume that the caller is
12764 -- also checking parameters, so we can assume the parameter is valid.
12766 elsif Ekind (Formal_Id) = E_In_Parameter
12767 and then Validity_Check_In_Params
12769 Set_Is_Known_Valid (Formal_Id, True);
12771 -- Similar treatment for IN OUT parameters
12773 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12774 and then Validity_Check_In_Out_Params
12776 Set_Is_Known_Valid (Formal_Id, True);
12778 end Set_Formal_Validity;
12780 ------------------------
12781 -- Subtype_Conformant --
12782 ------------------------
12784 function Subtype_Conformant
12785 (New_Id : Entity_Id;
12786 Old_Id : Entity_Id;
12787 Skip_Controlling_Formals : Boolean := False) return Boolean
12791 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12792 Skip_Controlling_Formals => Skip_Controlling_Formals);
12794 end Subtype_Conformant;
12796 ---------------------
12797 -- Type_Conformant --
12798 ---------------------
12800 function Type_Conformant
12801 (New_Id : Entity_Id;
12802 Old_Id : Entity_Id;
12803 Skip_Controlling_Formals : Boolean := False) return Boolean
12807 May_Hide_Profile := False;
12810 (New_Id, Old_Id, Type_Conformant, False, Result,
12811 Skip_Controlling_Formals => Skip_Controlling_Formals);
12813 end Type_Conformant;
12815 -------------------------------
12816 -- Valid_Operator_Definition --
12817 -------------------------------
12819 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12822 Id : constant Name_Id := Chars (Designator);
12826 F := First_Formal (Designator);
12827 while Present (F) loop
12830 if Present (Default_Value (F)) then
12832 ("default values not allowed for operator parameters",
12839 -- Verify that user-defined operators have proper number of arguments
12840 -- First case of operators which can only be unary
12842 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12845 -- Case of operators which can be unary or binary
12847 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12848 N_OK := (N in 1 .. 2);
12850 -- All other operators can only be binary
12858 ("incorrect number of arguments for operator", Designator);
12862 and then Base_Type (Etype (Designator)) = Standard_Boolean
12863 and then not Is_Intrinsic_Subprogram (Designator)
12866 ("explicit definition of inequality not allowed", Designator);
12868 end Valid_Operator_Definition;