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_Ekind (Body_Id, E_Subprogram_Body);
1111 Set_Convention (Body_Id, Convention (Gen_Id));
1112 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1113 Set_Scope (Body_Id, Scope (Gen_Id));
1114 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1116 if Nkind (N) = N_Subprogram_Body_Stub then
1118 -- No body to analyze, so restore state of generic unit
1120 Set_Ekind (Gen_Id, Kind);
1121 Set_Ekind (Body_Id, Kind);
1123 if Present (First_Ent) then
1124 Set_First_Entity (Gen_Id, First_Ent);
1131 -- If this is a compilation unit, it must be made visible explicitly,
1132 -- because the compilation of the declaration, unlike other library
1133 -- unit declarations, does not. If it is not a unit, the following
1134 -- is redundant but harmless.
1136 Set_Is_Immediately_Visible (Gen_Id);
1137 Reference_Body_Formals (Gen_Id, Body_Id);
1139 if Is_Child_Unit (Gen_Id) then
1140 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1143 Set_Actual_Subtypes (N, Current_Scope);
1145 -- Deal with preconditions and postconditions. In formal verification
1146 -- mode, we keep pre- and postconditions attached to entities rather
1147 -- than inserted in the code, in order to facilitate a distinct
1148 -- treatment for them.
1150 if not Alfa_Mode then
1151 Process_PPCs (N, Gen_Id, Body_Id);
1154 -- If the generic unit carries pre- or post-conditions, copy them
1155 -- to the original generic tree, so that they are properly added
1156 -- to any instantiation.
1159 Orig : constant Node_Id := Original_Node (N);
1163 Cond := First (Declarations (N));
1164 while Present (Cond) loop
1165 if Nkind (Cond) = N_Pragma
1166 and then Pragma_Name (Cond) = Name_Check
1168 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1170 elsif Nkind (Cond) = N_Pragma
1171 and then Pragma_Name (Cond) = Name_Postcondition
1173 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1174 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1183 Analyze_Declarations (Declarations (N));
1185 Analyze (Handled_Statement_Sequence (N));
1187 Save_Global_References (Original_Node (N));
1189 -- Prior to exiting the scope, include generic formals again (if any
1190 -- are present) in the set of local entities.
1192 if Present (First_Ent) then
1193 Set_First_Entity (Gen_Id, First_Ent);
1196 Check_References (Gen_Id);
1199 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1201 Check_Subprogram_Order (N);
1203 -- Outside of its body, unit is generic again
1205 Set_Ekind (Gen_Id, Kind);
1206 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1209 Style.Check_Identifier (Body_Id, Gen_Id);
1213 end Analyze_Generic_Subprogram_Body;
1215 ----------------------------
1216 -- Analyze_Null_Procedure --
1217 ----------------------------
1219 procedure Analyze_Null_Procedure
1221 Is_Completion : out Boolean)
1223 Loc : constant Source_Ptr := Sloc (N);
1224 Spec : constant Node_Id := Specification (N);
1225 Designator : Entity_Id;
1227 Null_Body : Node_Id := Empty;
1231 -- Capture the profile of the null procedure before analysis, for
1232 -- expansion at the freeze point and at each point of call. The body is
1233 -- used if the procedure has preconditions, or if it is a completion. In
1234 -- the first case the body is analyzed at the freeze point, in the other
1235 -- it replaces the null procedure declaration.
1238 Make_Subprogram_Body (Loc,
1239 Specification => New_Copy_Tree (Spec),
1240 Declarations => New_List,
1241 Handled_Statement_Sequence =>
1242 Make_Handled_Sequence_Of_Statements (Loc,
1243 Statements => New_List (Make_Null_Statement (Loc))));
1245 -- Create new entities for body and formals
1247 Set_Defining_Unit_Name (Specification (Null_Body),
1248 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1250 Form := First (Parameter_Specifications (Specification (Null_Body)));
1251 while Present (Form) loop
1252 Set_Defining_Identifier (Form,
1253 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1257 -- Determine whether the null procedure may be a completion of a generic
1258 -- suprogram, in which case we use the new null body as the completion
1259 -- and set minimal semantic information on the original declaration,
1260 -- which is rewritten as a null statement.
1262 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1264 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1265 Insert_Before (N, Null_Body);
1266 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1267 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1269 Rewrite (N, Make_Null_Statement (Loc));
1270 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1271 Is_Completion := True;
1276 -- Resolve the types of the formals now, because the freeze point
1277 -- may appear in a different context, e.g. an instantiation.
1279 Form := First (Parameter_Specifications (Specification (Null_Body)));
1280 while Present (Form) loop
1281 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1282 Find_Type (Parameter_Type (Form));
1285 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1287 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1290 -- The case of a null procedure with a formal that is an
1291 -- access_to_subprogram type, and that is used as an actual
1292 -- in an instantiation is left to the enthusiastic reader.
1301 -- If there are previous overloadable entities with the same name,
1302 -- check whether any of them is completed by the null procedure.
1304 if Present (Prev) and then Is_Overloadable (Prev) then
1305 Designator := Analyze_Subprogram_Specification (Spec);
1306 Prev := Find_Corresponding_Spec (N);
1309 if No (Prev) or else not Comes_From_Source (Prev) then
1310 Designator := Analyze_Subprogram_Specification (Spec);
1311 Set_Has_Completion (Designator);
1313 -- Signal to caller that this is a procedure declaration
1315 Is_Completion := False;
1317 -- Null procedures are always inlined, but generic formal subprograms
1318 -- which appear as such in the internal instance of formal packages,
1319 -- need no completion and are not marked Inline.
1322 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1324 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1325 Set_Body_To_Inline (N, Null_Body);
1326 Set_Is_Inlined (Designator);
1330 -- The null procedure is a completion
1332 Is_Completion := True;
1334 if Expander_Active then
1335 Rewrite (N, Null_Body);
1339 Designator := Analyze_Subprogram_Specification (Spec);
1340 Set_Has_Completion (Designator);
1341 Set_Has_Completion (Prev);
1344 end Analyze_Null_Procedure;
1346 -----------------------------
1347 -- Analyze_Operator_Symbol --
1348 -----------------------------
1350 -- An operator symbol such as "+" or "and" may appear in context where the
1351 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1352 -- is just a string, as in (conjunction = "or"). In these cases the parser
1353 -- generates this node, and the semantics does the disambiguation. Other
1354 -- such case are actuals in an instantiation, the generic unit in an
1355 -- instantiation, and pragma arguments.
1357 procedure Analyze_Operator_Symbol (N : Node_Id) is
1358 Par : constant Node_Id := Parent (N);
1361 if (Nkind (Par) = N_Function_Call
1362 and then N = Name (Par))
1363 or else Nkind (Par) = N_Function_Instantiation
1364 or else (Nkind (Par) = N_Indexed_Component
1365 and then N = Prefix (Par))
1366 or else (Nkind (Par) = N_Pragma_Argument_Association
1367 and then not Is_Pragma_String_Literal (Par))
1368 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1369 or else (Nkind (Par) = N_Attribute_Reference
1370 and then Attribute_Name (Par) /= Name_Value)
1372 Find_Direct_Name (N);
1375 Change_Operator_Symbol_To_String_Literal (N);
1378 end Analyze_Operator_Symbol;
1380 -----------------------------------
1381 -- Analyze_Parameter_Association --
1382 -----------------------------------
1384 procedure Analyze_Parameter_Association (N : Node_Id) is
1386 Analyze (Explicit_Actual_Parameter (N));
1387 end Analyze_Parameter_Association;
1389 ----------------------------
1390 -- Analyze_Procedure_Call --
1391 ----------------------------
1393 procedure Analyze_Procedure_Call (N : Node_Id) is
1394 Loc : constant Source_Ptr := Sloc (N);
1395 P : constant Node_Id := Name (N);
1396 Actuals : constant List_Id := Parameter_Associations (N);
1400 procedure Analyze_Call_And_Resolve;
1401 -- Do Analyze and Resolve calls for procedure call
1402 -- At end, check illegal order dependence.
1404 ------------------------------
1405 -- Analyze_Call_And_Resolve --
1406 ------------------------------
1408 procedure Analyze_Call_And_Resolve is
1410 if Nkind (N) = N_Procedure_Call_Statement then
1412 Resolve (N, Standard_Void_Type);
1416 end Analyze_Call_And_Resolve;
1418 -- Start of processing for Analyze_Procedure_Call
1421 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1422 -- a procedure call or an entry call. The prefix may denote an access
1423 -- to subprogram type, in which case an implicit dereference applies.
1424 -- If the prefix is an indexed component (without implicit dereference)
1425 -- then the construct denotes a call to a member of an entire family.
1426 -- If the prefix is a simple name, it may still denote a call to a
1427 -- parameterless member of an entry family. Resolution of these various
1428 -- interpretations is delicate.
1432 -- If this is a call of the form Obj.Op, the call may have been
1433 -- analyzed and possibly rewritten into a block, in which case
1436 if Analyzed (N) then
1440 -- If there is an error analyzing the name (which may have been
1441 -- rewritten if the original call was in prefix notation) then error
1442 -- has been emitted already, mark node and return.
1444 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1445 Set_Etype (N, Any_Type);
1449 -- Otherwise analyze the parameters
1451 if Present (Actuals) then
1452 Actual := First (Actuals);
1454 while Present (Actual) loop
1456 Check_Parameterless_Call (Actual);
1461 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1463 if Nkind (P) = N_Attribute_Reference
1464 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1466 Name_Elab_Subp_Body)
1468 if Present (Actuals) then
1470 ("no parameters allowed for this call", First (Actuals));
1474 Set_Etype (N, Standard_Void_Type);
1477 elsif Is_Entity_Name (P)
1478 and then Is_Record_Type (Etype (Entity (P)))
1479 and then Remote_AST_I_Dereference (P)
1483 elsif Is_Entity_Name (P)
1484 and then Ekind (Entity (P)) /= E_Entry_Family
1486 if Is_Access_Type (Etype (P))
1487 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1488 and then No (Actuals)
1489 and then Comes_From_Source (N)
1491 Error_Msg_N ("missing explicit dereference in call", N);
1494 Analyze_Call_And_Resolve;
1496 -- If the prefix is the simple name of an entry family, this is
1497 -- a parameterless call from within the task body itself.
1499 elsif Is_Entity_Name (P)
1500 and then Nkind (P) = N_Identifier
1501 and then Ekind (Entity (P)) = E_Entry_Family
1502 and then Present (Actuals)
1503 and then No (Next (First (Actuals)))
1505 -- Can be call to parameterless entry family. What appears to be the
1506 -- sole argument is in fact the entry index. Rewrite prefix of node
1507 -- accordingly. Source representation is unchanged by this
1511 Make_Indexed_Component (Loc,
1513 Make_Selected_Component (Loc,
1514 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1515 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1516 Expressions => Actuals);
1517 Set_Name (N, New_N);
1518 Set_Etype (New_N, Standard_Void_Type);
1519 Set_Parameter_Associations (N, No_List);
1520 Analyze_Call_And_Resolve;
1522 elsif Nkind (P) = N_Explicit_Dereference then
1523 if Ekind (Etype (P)) = E_Subprogram_Type then
1524 Analyze_Call_And_Resolve;
1526 Error_Msg_N ("expect access to procedure in call", P);
1529 -- The name can be a selected component or an indexed component that
1530 -- yields an access to subprogram. Such a prefix is legal if the call
1531 -- has parameter associations.
1533 elsif Is_Access_Type (Etype (P))
1534 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1536 if Present (Actuals) then
1537 Analyze_Call_And_Resolve;
1539 Error_Msg_N ("missing explicit dereference in call ", N);
1542 -- If not an access to subprogram, then the prefix must resolve to the
1543 -- name of an entry, entry family, or protected operation.
1545 -- For the case of a simple entry call, P is a selected component where
1546 -- the prefix is the task and the selector name is the entry. A call to
1547 -- a protected procedure will have the same syntax. If the protected
1548 -- object contains overloaded operations, the entity may appear as a
1549 -- function, the context will select the operation whose type is Void.
1551 elsif Nkind (P) = N_Selected_Component
1552 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1556 Analyze_Call_And_Resolve;
1558 elsif Nkind (P) = N_Selected_Component
1559 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1560 and then Present (Actuals)
1561 and then No (Next (First (Actuals)))
1563 -- Can be call to parameterless entry family. What appears to be the
1564 -- sole argument is in fact the entry index. Rewrite prefix of node
1565 -- accordingly. Source representation is unchanged by this
1569 Make_Indexed_Component (Loc,
1570 Prefix => New_Copy (P),
1571 Expressions => Actuals);
1572 Set_Name (N, New_N);
1573 Set_Etype (New_N, Standard_Void_Type);
1574 Set_Parameter_Associations (N, No_List);
1575 Analyze_Call_And_Resolve;
1577 -- For the case of a reference to an element of an entry family, P is
1578 -- an indexed component whose prefix is a selected component (task and
1579 -- entry family), and whose index is the entry family index.
1581 elsif Nkind (P) = N_Indexed_Component
1582 and then Nkind (Prefix (P)) = N_Selected_Component
1583 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1585 Analyze_Call_And_Resolve;
1587 -- If the prefix is the name of an entry family, it is a call from
1588 -- within the task body itself.
1590 elsif Nkind (P) = N_Indexed_Component
1591 and then Nkind (Prefix (P)) = N_Identifier
1592 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1595 Make_Selected_Component (Loc,
1596 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1597 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1598 Rewrite (Prefix (P), New_N);
1600 Analyze_Call_And_Resolve;
1602 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1603 -- procedure name, so the construct can only be a qualified expression.
1605 elsif Nkind (P) = N_Qualified_Expression
1606 and then Ada_Version >= Ada_2012
1608 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1611 -- Anything else is an error
1614 Error_Msg_N ("invalid procedure or entry call", N);
1616 end Analyze_Procedure_Call;
1618 ------------------------------
1619 -- Analyze_Return_Statement --
1620 ------------------------------
1622 procedure Analyze_Return_Statement (N : Node_Id) is
1624 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1625 N_Extended_Return_Statement));
1627 Returns_Object : constant Boolean :=
1628 Nkind (N) = N_Extended_Return_Statement
1630 (Nkind (N) = N_Simple_Return_Statement
1631 and then Present (Expression (N)));
1632 -- True if we're returning something; that is, "return <expression>;"
1633 -- or "return Result : T [:= ...]". False for "return;". Used for error
1634 -- checking: If Returns_Object is True, N should apply to a function
1635 -- body; otherwise N should apply to a procedure body, entry body,
1636 -- accept statement, or extended return statement.
1638 function Find_What_It_Applies_To return Entity_Id;
1639 -- Find the entity representing the innermost enclosing body, accept
1640 -- statement, or extended return statement. If the result is a callable
1641 -- construct or extended return statement, then this will be the value
1642 -- of the Return_Applies_To attribute. Otherwise, the program is
1643 -- illegal. See RM-6.5(4/2).
1645 -----------------------------
1646 -- Find_What_It_Applies_To --
1647 -----------------------------
1649 function Find_What_It_Applies_To return Entity_Id is
1650 Result : Entity_Id := Empty;
1653 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1654 -- and postconditions.
1656 for J in reverse 0 .. Scope_Stack.Last loop
1657 Result := Scope_Stack.Table (J).Entity;
1658 exit when not Ekind_In (Result, E_Block, E_Loop)
1659 and then Chars (Result) /= Name_uPostconditions;
1662 pragma Assert (Present (Result));
1664 end Find_What_It_Applies_To;
1666 -- Local declarations
1668 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1669 Kind : constant Entity_Kind := Ekind (Scope_Id);
1670 Loc : constant Source_Ptr := Sloc (N);
1671 Stm_Entity : constant Entity_Id :=
1673 (E_Return_Statement, Current_Scope, Loc, 'R');
1675 -- Start of processing for Analyze_Return_Statement
1678 Set_Return_Statement_Entity (N, Stm_Entity);
1680 Set_Etype (Stm_Entity, Standard_Void_Type);
1681 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1683 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1684 -- (4/2): an inner return statement will apply to this extended return.
1686 if Nkind (N) = N_Extended_Return_Statement then
1687 Push_Scope (Stm_Entity);
1690 -- Check that pragma No_Return is obeyed. Don't complain about the
1691 -- implicitly-generated return that is placed at the end.
1693 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1694 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1697 -- Warn on any unassigned OUT parameters if in procedure
1699 if Ekind (Scope_Id) = E_Procedure then
1700 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1703 -- Check that functions return objects, and other things do not
1705 if Kind = E_Function or else Kind = E_Generic_Function then
1706 if not Returns_Object then
1707 Error_Msg_N ("missing expression in return from function", N);
1710 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1711 if Returns_Object then
1712 Error_Msg_N ("procedure cannot return value (use function)", N);
1715 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1716 if Returns_Object then
1717 if Is_Protected_Type (Scope (Scope_Id)) then
1718 Error_Msg_N ("entry body cannot return value", N);
1720 Error_Msg_N ("accept statement cannot return value", N);
1724 elsif Kind = E_Return_Statement then
1726 -- We are nested within another return statement, which must be an
1727 -- extended_return_statement.
1729 if Returns_Object then
1730 if Nkind (N) = N_Extended_Return_Statement then
1732 ("extended return statement cannot be nested (use `RETURN;`)",
1735 -- Case of a simple return statement with a value inside extended
1736 -- return statement.
1740 ("return nested in extended return statement cannot return " &
1741 "value (use `RETURN;`)", N);
1746 Error_Msg_N ("illegal context for return statement", N);
1749 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1750 Analyze_Function_Return (N);
1752 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1753 Set_Return_Present (Scope_Id);
1756 if Nkind (N) = N_Extended_Return_Statement then
1760 Kill_Current_Values (Last_Assignment_Only => True);
1761 Check_Unreachable_Code (N);
1763 Analyze_Dimension (N);
1764 end Analyze_Return_Statement;
1766 -------------------------------------
1767 -- Analyze_Simple_Return_Statement --
1768 -------------------------------------
1770 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1772 if Present (Expression (N)) then
1773 Mark_Coextensions (N, Expression (N));
1776 Analyze_Return_Statement (N);
1777 end Analyze_Simple_Return_Statement;
1779 -------------------------
1780 -- Analyze_Return_Type --
1781 -------------------------
1783 procedure Analyze_Return_Type (N : Node_Id) is
1784 Designator : constant Entity_Id := Defining_Entity (N);
1785 Typ : Entity_Id := Empty;
1788 -- Normal case where result definition does not indicate an error
1790 if Result_Definition (N) /= Error then
1791 if Nkind (Result_Definition (N)) = N_Access_Definition then
1792 Check_SPARK_Restriction
1793 ("access result is not allowed", Result_Definition (N));
1795 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1798 AD : constant Node_Id :=
1799 Access_To_Subprogram_Definition (Result_Definition (N));
1801 if Present (AD) and then Protected_Present (AD) then
1802 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1804 Typ := Access_Definition (N, Result_Definition (N));
1808 Set_Parent (Typ, Result_Definition (N));
1809 Set_Is_Local_Anonymous_Access (Typ);
1810 Set_Etype (Designator, Typ);
1812 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1814 Null_Exclusion_Static_Checks (N);
1816 -- Subtype_Mark case
1819 Find_Type (Result_Definition (N));
1820 Typ := Entity (Result_Definition (N));
1821 Set_Etype (Designator, Typ);
1823 -- Unconstrained array as result is not allowed in SPARK
1825 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1826 Check_SPARK_Restriction
1827 ("returning an unconstrained array is not allowed",
1828 Result_Definition (N));
1831 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1833 Null_Exclusion_Static_Checks (N);
1835 -- If a null exclusion is imposed on the result type, then create
1836 -- a null-excluding itype (an access subtype) and use it as the
1837 -- function's Etype. Note that the null exclusion checks are done
1838 -- right before this, because they don't get applied to types that
1839 -- do not come from source.
1841 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1842 Set_Etype (Designator,
1843 Create_Null_Excluding_Itype
1846 Scope_Id => Scope (Current_Scope)));
1848 -- The new subtype must be elaborated before use because
1849 -- it is visible outside of the function. However its base
1850 -- type may not be frozen yet, so the reference that will
1851 -- force elaboration must be attached to the freezing of
1854 -- If the return specification appears on a proper body,
1855 -- the subtype will have been created already on the spec.
1857 if Is_Frozen (Typ) then
1858 if Nkind (Parent (N)) = N_Subprogram_Body
1859 and then Nkind (Parent (Parent (N))) = N_Subunit
1863 Build_Itype_Reference (Etype (Designator), Parent (N));
1867 Ensure_Freeze_Node (Typ);
1870 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1872 Set_Itype (IR, Etype (Designator));
1873 Append_Freeze_Actions (Typ, New_List (IR));
1878 Set_Etype (Designator, Typ);
1881 if Ekind (Typ) = E_Incomplete_Type
1882 and then Is_Value_Type (Typ)
1886 elsif Ekind (Typ) = E_Incomplete_Type
1887 or else (Is_Class_Wide_Type (Typ)
1888 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1890 -- AI05-0151: Tagged incomplete types are allowed in all formal
1891 -- parts. Untagged incomplete types are not allowed in bodies.
1893 if Ada_Version >= Ada_2012 then
1894 if Is_Tagged_Type (Typ) then
1897 elsif Nkind_In (Parent (Parent (N)),
1903 ("invalid use of untagged incomplete type&",
1907 -- The type must be completed in the current package. This
1908 -- is checked at the end of the package declaraton, when
1909 -- Taft-amendment types are identified. If the return type
1910 -- is class-wide, there is no required check, the type can
1911 -- be a bona fide TAT.
1913 if Ekind (Scope (Current_Scope)) = E_Package
1914 and then In_Private_Part (Scope (Current_Scope))
1915 and then not Is_Class_Wide_Type (Typ)
1917 Append_Elmt (Designator, Private_Dependents (Typ));
1922 ("invalid use of incomplete type&", Designator, Typ);
1927 -- Case where result definition does indicate an error
1930 Set_Etype (Designator, Any_Type);
1932 end Analyze_Return_Type;
1934 -----------------------------
1935 -- Analyze_Subprogram_Body --
1936 -----------------------------
1938 procedure Analyze_Subprogram_Body (N : Node_Id) is
1939 Loc : constant Source_Ptr := Sloc (N);
1940 Body_Spec : constant Node_Id := Specification (N);
1941 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1944 if Debug_Flag_C then
1945 Write_Str ("==> subprogram body ");
1946 Write_Name (Chars (Body_Id));
1947 Write_Str (" from ");
1948 Write_Location (Loc);
1953 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1955 -- The real work is split out into the helper, so it can do "return;"
1956 -- without skipping the debug output:
1958 Analyze_Subprogram_Body_Helper (N);
1960 if Debug_Flag_C then
1962 Write_Str ("<== subprogram body ");
1963 Write_Name (Chars (Body_Id));
1964 Write_Str (" from ");
1965 Write_Location (Loc);
1968 end Analyze_Subprogram_Body;
1970 ------------------------------------
1971 -- Analyze_Subprogram_Body_Helper --
1972 ------------------------------------
1974 -- This procedure is called for regular subprogram bodies, generic bodies,
1975 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1976 -- specification matters, and is used to create a proper declaration for
1977 -- the subprogram, or to perform conformance checks.
1979 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1980 Loc : constant Source_Ptr := Sloc (N);
1981 Body_Spec : constant Node_Id := Specification (N);
1982 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1983 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1984 Conformant : Boolean;
1986 Prot_Typ : Entity_Id := Empty;
1987 Spec_Id : Entity_Id;
1988 Spec_Decl : Node_Id := Empty;
1990 Last_Real_Spec_Entity : Entity_Id := Empty;
1991 -- When we analyze a separate spec, the entity chain ends up containing
1992 -- the formals, as well as any itypes generated during analysis of the
1993 -- default expressions for parameters, or the arguments of associated
1994 -- precondition/postcondition pragmas (which are analyzed in the context
1995 -- of the spec since they have visibility on formals).
1997 -- These entities belong with the spec and not the body. However we do
1998 -- the analysis of the body in the context of the spec (again to obtain
1999 -- visibility to the formals), and all the entities generated during
2000 -- this analysis end up also chained to the entity chain of the spec.
2001 -- But they really belong to the body, and there is circuitry to move
2002 -- them from the spec to the body.
2004 -- However, when we do this move, we don't want to move the real spec
2005 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2006 -- variable points to the last real spec entity, so we only move those
2007 -- chained beyond that point. It is initialized to Empty to deal with
2008 -- the case where there is no separate spec.
2010 procedure Check_Anonymous_Return;
2011 -- Ada 2005: if a function returns an access type that denotes a task,
2012 -- or a type that contains tasks, we must create a master entity for
2013 -- the anonymous type, which typically will be used in an allocator
2014 -- in the body of the function.
2016 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2017 -- Look ahead to recognize a pragma that may appear after the body.
2018 -- If there is a previous spec, check that it appears in the same
2019 -- declarative part. If the pragma is Inline_Always, perform inlining
2020 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2021 -- If the body acts as a spec, and inlining is required, we create a
2022 -- subprogram declaration for it, in order to attach the body to inline.
2023 -- If pragma does not appear after the body, check whether there is
2024 -- an inline pragma before any local declarations.
2026 procedure Check_Missing_Return;
2027 -- Checks for a function with a no return statements, and also performs
2028 -- the warning checks implemented by Check_Returns. In formal mode, also
2029 -- verify that a function ends with a RETURN and that a procedure does
2030 -- not contain any RETURN.
2032 function Disambiguate_Spec return Entity_Id;
2033 -- When a primitive is declared between the private view and the full
2034 -- view of a concurrent type which implements an interface, a special
2035 -- mechanism is used to find the corresponding spec of the primitive
2038 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2039 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2040 -- incomplete types coming from a limited context and swap their limited
2041 -- views with the non-limited ones.
2043 function Is_Private_Concurrent_Primitive
2044 (Subp_Id : Entity_Id) return Boolean;
2045 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2046 -- type that implements an interface and has a private view.
2048 procedure Set_Trivial_Subprogram (N : Node_Id);
2049 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2050 -- subprogram whose body is being analyzed. N is the statement node
2051 -- causing the flag to be set, if the following statement is a return
2052 -- of an entity, we mark the entity as set in source to suppress any
2053 -- warning on the stylized use of function stubs with a dummy return.
2055 procedure Verify_Overriding_Indicator;
2056 -- If there was a previous spec, the entity has been entered in the
2057 -- current scope previously. If the body itself carries an overriding
2058 -- indicator, check that it is consistent with the known status of the
2061 ----------------------------
2062 -- Check_Anonymous_Return --
2063 ----------------------------
2065 procedure Check_Anonymous_Return is
2071 if Present (Spec_Id) then
2077 if Ekind (Scop) = E_Function
2078 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2079 and then not Is_Thunk (Scop)
2080 and then (Has_Task (Designated_Type (Etype (Scop)))
2082 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2084 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2085 and then Expander_Active
2087 -- Avoid cases with no tasking support
2089 and then RTE_Available (RE_Current_Master)
2090 and then not Restriction_Active (No_Task_Hierarchy)
2093 Make_Object_Declaration (Loc,
2094 Defining_Identifier =>
2095 Make_Defining_Identifier (Loc, Name_uMaster),
2096 Constant_Present => True,
2097 Object_Definition =>
2098 New_Reference_To (RTE (RE_Master_Id), Loc),
2100 Make_Explicit_Dereference (Loc,
2101 New_Reference_To (RTE (RE_Current_Master), Loc)));
2103 if Present (Declarations (N)) then
2104 Prepend (Decl, Declarations (N));
2106 Set_Declarations (N, New_List (Decl));
2109 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2110 Set_Has_Master_Entity (Scop);
2112 -- Now mark the containing scope as a task master
2115 while Nkind (Par) /= N_Compilation_Unit loop
2116 Par := Parent (Par);
2117 pragma Assert (Present (Par));
2119 -- If we fall off the top, we are at the outer level, and
2120 -- the environment task is our effective master, so nothing
2124 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2126 Set_Is_Task_Master (Par, True);
2131 end Check_Anonymous_Return;
2133 -------------------------
2134 -- Check_Inline_Pragma --
2135 -------------------------
2137 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2141 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2142 -- True when N is a pragma Inline or Inline_Always that applies
2143 -- to this subprogram.
2145 -----------------------
2146 -- Is_Inline_Pragma --
2147 -----------------------
2149 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2152 Nkind (N) = N_Pragma
2154 (Pragma_Name (N) = Name_Inline_Always
2157 and then Pragma_Name (N) = Name_Inline))
2160 (Expression (First (Pragma_Argument_Associations (N)))) =
2162 end Is_Inline_Pragma;
2164 -- Start of processing for Check_Inline_Pragma
2167 if not Expander_Active then
2171 if Is_List_Member (N)
2172 and then Present (Next (N))
2173 and then Is_Inline_Pragma (Next (N))
2177 elsif Nkind (N) /= N_Subprogram_Body_Stub
2178 and then Present (Declarations (N))
2179 and then Is_Inline_Pragma (First (Declarations (N)))
2181 Prag := First (Declarations (N));
2187 if Present (Prag) then
2188 if Present (Spec_Id) then
2189 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2194 -- Create a subprogram declaration, to make treatment uniform
2197 Subp : constant Entity_Id :=
2198 Make_Defining_Identifier (Loc, Chars (Body_Id));
2199 Decl : constant Node_Id :=
2200 Make_Subprogram_Declaration (Loc,
2202 New_Copy_Tree (Specification (N)));
2205 Set_Defining_Unit_Name (Specification (Decl), Subp);
2207 if Present (First_Formal (Body_Id)) then
2208 Plist := Copy_Parameter_List (Body_Id);
2209 Set_Parameter_Specifications
2210 (Specification (Decl), Plist);
2213 Insert_Before (N, Decl);
2216 Set_Has_Pragma_Inline (Subp);
2218 if Pragma_Name (Prag) = Name_Inline_Always then
2219 Set_Is_Inlined (Subp);
2220 Set_Has_Pragma_Inline_Always (Subp);
2227 end Check_Inline_Pragma;
2229 --------------------------
2230 -- Check_Missing_Return --
2231 --------------------------
2233 procedure Check_Missing_Return is
2235 Missing_Ret : Boolean;
2238 if Nkind (Body_Spec) = N_Function_Specification then
2239 if Present (Spec_Id) then
2245 if Return_Present (Id) then
2246 Check_Returns (HSS, 'F', Missing_Ret);
2249 Set_Has_Missing_Return (Id);
2252 elsif Is_Generic_Subprogram (Id)
2253 or else not Is_Machine_Code_Subprogram (Id)
2255 Error_Msg_N ("missing RETURN statement in function body", N);
2258 -- If procedure with No_Return, check returns
2260 elsif Nkind (Body_Spec) = N_Procedure_Specification
2261 and then Present (Spec_Id)
2262 and then No_Return (Spec_Id)
2264 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2267 -- Special checks in SPARK mode
2269 if Nkind (Body_Spec) = N_Function_Specification then
2271 -- In SPARK mode, last statement of a function should be a return
2274 Stat : constant Node_Id := Last_Source_Statement (HSS);
2277 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2278 N_Extended_Return_Statement)
2280 Check_SPARK_Restriction
2281 ("last statement in function should be RETURN", Stat);
2285 -- In SPARK mode, verify that a procedure has no return
2287 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2288 if Present (Spec_Id) then
2294 -- Would be nice to point to return statement here, can we
2295 -- borrow the Check_Returns procedure here ???
2297 if Return_Present (Id) then
2298 Check_SPARK_Restriction
2299 ("procedure should not have RETURN", N);
2302 end Check_Missing_Return;
2304 -----------------------
2305 -- Disambiguate_Spec --
2306 -----------------------
2308 function Disambiguate_Spec return Entity_Id is
2309 Priv_Spec : Entity_Id;
2312 procedure Replace_Types (To_Corresponding : Boolean);
2313 -- Depending on the flag, replace the type of formal parameters of
2314 -- Body_Id if it is a concurrent type implementing interfaces with
2315 -- the corresponding record type or the other way around.
2317 procedure Replace_Types (To_Corresponding : Boolean) is
2319 Formal_Typ : Entity_Id;
2322 Formal := First_Formal (Body_Id);
2323 while Present (Formal) loop
2324 Formal_Typ := Etype (Formal);
2326 if Is_Class_Wide_Type (Formal_Typ) then
2327 Formal_Typ := Root_Type (Formal_Typ);
2330 -- From concurrent type to corresponding record
2332 if To_Corresponding then
2333 if Is_Concurrent_Type (Formal_Typ)
2334 and then Present (Corresponding_Record_Type (Formal_Typ))
2335 and then Present (Interfaces (
2336 Corresponding_Record_Type (Formal_Typ)))
2339 Corresponding_Record_Type (Formal_Typ));
2342 -- From corresponding record to concurrent type
2345 if Is_Concurrent_Record_Type (Formal_Typ)
2346 and then Present (Interfaces (Formal_Typ))
2349 Corresponding_Concurrent_Type (Formal_Typ));
2353 Next_Formal (Formal);
2357 -- Start of processing for Disambiguate_Spec
2360 -- Try to retrieve the specification of the body as is. All error
2361 -- messages are suppressed because the body may not have a spec in
2362 -- its current state.
2364 Spec_N := Find_Corresponding_Spec (N, False);
2366 -- It is possible that this is the body of a primitive declared
2367 -- between a private and a full view of a concurrent type. The
2368 -- controlling parameter of the spec carries the concurrent type,
2369 -- not the corresponding record type as transformed by Analyze_
2370 -- Subprogram_Specification. In such cases, we undo the change
2371 -- made by the analysis of the specification and try to find the
2374 -- Note that wrappers already have their corresponding specs and
2375 -- bodies set during their creation, so if the candidate spec is
2376 -- a wrapper, then we definitely need to swap all types to their
2377 -- original concurrent status.
2380 or else Is_Primitive_Wrapper (Spec_N)
2382 -- Restore all references of corresponding record types to the
2383 -- original concurrent types.
2385 Replace_Types (To_Corresponding => False);
2386 Priv_Spec := Find_Corresponding_Spec (N, False);
2388 -- The current body truly belongs to a primitive declared between
2389 -- a private and a full view. We leave the modified body as is,
2390 -- and return the true spec.
2392 if Present (Priv_Spec)
2393 and then Is_Private_Primitive (Priv_Spec)
2398 -- In case that this is some sort of error, restore the original
2399 -- state of the body.
2401 Replace_Types (To_Corresponding => True);
2405 end Disambiguate_Spec;
2407 ----------------------------
2408 -- Exchange_Limited_Views --
2409 ----------------------------
2411 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2412 procedure Detect_And_Exchange (Id : Entity_Id);
2413 -- Determine whether Id's type denotes an incomplete type associated
2414 -- with a limited with clause and exchange the limited view with the
2417 -------------------------
2418 -- Detect_And_Exchange --
2419 -------------------------
2421 procedure Detect_And_Exchange (Id : Entity_Id) is
2422 Typ : constant Entity_Id := Etype (Id);
2425 if Ekind (Typ) = E_Incomplete_Type
2426 and then From_With_Type (Typ)
2427 and then Present (Non_Limited_View (Typ))
2429 Set_Etype (Id, Non_Limited_View (Typ));
2431 end Detect_And_Exchange;
2437 -- Start of processing for Exchange_Limited_Views
2440 if No (Subp_Id) then
2443 -- Do not process subprogram bodies as they already use the non-
2444 -- limited view of types.
2446 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2450 -- Examine all formals and swap views when applicable
2452 Formal := First_Formal (Subp_Id);
2453 while Present (Formal) loop
2454 Detect_And_Exchange (Formal);
2456 Next_Formal (Formal);
2459 -- Process the return type of a function
2461 if Ekind (Subp_Id) = E_Function then
2462 Detect_And_Exchange (Subp_Id);
2464 end Exchange_Limited_Views;
2466 -------------------------------------
2467 -- Is_Private_Concurrent_Primitive --
2468 -------------------------------------
2470 function Is_Private_Concurrent_Primitive
2471 (Subp_Id : Entity_Id) return Boolean
2473 Formal_Typ : Entity_Id;
2476 if Present (First_Formal (Subp_Id)) then
2477 Formal_Typ := Etype (First_Formal (Subp_Id));
2479 if Is_Concurrent_Record_Type (Formal_Typ) then
2480 if Is_Class_Wide_Type (Formal_Typ) then
2481 Formal_Typ := Root_Type (Formal_Typ);
2484 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2487 -- The type of the first formal is a concurrent tagged type with
2491 Is_Concurrent_Type (Formal_Typ)
2492 and then Is_Tagged_Type (Formal_Typ)
2493 and then Has_Private_Declaration (Formal_Typ);
2497 end Is_Private_Concurrent_Primitive;
2499 ----------------------------
2500 -- Set_Trivial_Subprogram --
2501 ----------------------------
2503 procedure Set_Trivial_Subprogram (N : Node_Id) is
2504 Nxt : constant Node_Id := Next (N);
2507 Set_Is_Trivial_Subprogram (Body_Id);
2509 if Present (Spec_Id) then
2510 Set_Is_Trivial_Subprogram (Spec_Id);
2514 and then Nkind (Nxt) = N_Simple_Return_Statement
2515 and then No (Next (Nxt))
2516 and then Present (Expression (Nxt))
2517 and then Is_Entity_Name (Expression (Nxt))
2519 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2521 end Set_Trivial_Subprogram;
2523 ---------------------------------
2524 -- Verify_Overriding_Indicator --
2525 ---------------------------------
2527 procedure Verify_Overriding_Indicator is
2529 if Must_Override (Body_Spec) then
2530 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2531 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2535 elsif not Present (Overridden_Operation (Spec_Id)) then
2537 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2540 elsif Must_Not_Override (Body_Spec) then
2541 if Present (Overridden_Operation (Spec_Id)) then
2543 ("subprogram& overrides inherited operation",
2544 Body_Spec, Spec_Id);
2546 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2547 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2550 ("subprogram & overrides predefined operator ",
2551 Body_Spec, Spec_Id);
2553 -- If this is not a primitive operation or protected subprogram,
2554 -- then the overriding indicator is altogether illegal.
2556 elsif not Is_Primitive (Spec_Id)
2557 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2560 ("overriding indicator only allowed " &
2561 "if subprogram is primitive",
2566 and then Present (Overridden_Operation (Spec_Id))
2568 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2569 Style.Missing_Overriding (N, Body_Id);
2572 and then Can_Override_Operator (Spec_Id)
2573 and then not Is_Predefined_File_Name
2574 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2576 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2577 Style.Missing_Overriding (N, Body_Id);
2579 end Verify_Overriding_Indicator;
2581 -- Start of processing for Analyze_Subprogram_Body_Helper
2584 -- Generic subprograms are handled separately. They always have a
2585 -- generic specification. Determine whether current scope has a
2586 -- previous declaration.
2588 -- If the subprogram body is defined within an instance of the same
2589 -- name, the instance appears as a package renaming, and will be hidden
2590 -- within the subprogram.
2592 if Present (Prev_Id)
2593 and then not Is_Overloadable (Prev_Id)
2594 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2595 or else Comes_From_Source (Prev_Id))
2597 if Is_Generic_Subprogram (Prev_Id) then
2599 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2600 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2602 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2604 if Nkind (N) = N_Subprogram_Body then
2605 HSS := Handled_Statement_Sequence (N);
2606 Check_Missing_Return;
2612 -- Previous entity conflicts with subprogram name. Attempting to
2613 -- enter name will post error.
2615 Enter_Name (Body_Id);
2619 -- Non-generic case, find the subprogram declaration, if one was seen,
2620 -- or enter new overloaded entity in the current scope. If the
2621 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2622 -- part of the context of one of its subunits. No need to redo the
2625 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2629 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2631 if Nkind (N) = N_Subprogram_Body_Stub
2632 or else No (Corresponding_Spec (N))
2634 if Is_Private_Concurrent_Primitive (Body_Id) then
2635 Spec_Id := Disambiguate_Spec;
2637 Spec_Id := Find_Corresponding_Spec (N);
2640 -- If this is a duplicate body, no point in analyzing it
2642 if Error_Posted (N) then
2646 -- A subprogram body should cause freezing of its own declaration,
2647 -- but if there was no previous explicit declaration, then the
2648 -- subprogram will get frozen too late (there may be code within
2649 -- the body that depends on the subprogram having been frozen,
2650 -- such as uses of extra formals), so we force it to be frozen
2651 -- here. Same holds if the body and spec are compilation units.
2652 -- Finally, if the return type is an anonymous access to protected
2653 -- subprogram, it must be frozen before the body because its
2654 -- expansion has generated an equivalent type that is used when
2655 -- elaborating the body.
2657 -- An exception in the case of Ada 2012, AI05-177: The bodies
2658 -- created for expression functions do not freeze.
2661 and then Nkind (Original_Node (N)) /= N_Expression_Function
2663 Freeze_Before (N, Body_Id);
2665 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2666 Freeze_Before (N, Spec_Id);
2668 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2669 Freeze_Before (N, Etype (Body_Id));
2673 Spec_Id := Corresponding_Spec (N);
2677 -- Ada 2012 aspects may appear in a subprogram body, but only if there
2678 -- is no previous spec. Ditto for a subprogram stub that does not have
2679 -- a corresponding spec, but for which there may also be a spec_id.
2681 if Has_Aspects (N) then
2682 if Present (Spec_Id) then
2684 ("aspect specifications must appear in subprogram declaration",
2687 Analyze_Aspect_Specifications (N, Body_Id);
2691 -- Previously we scanned the body to look for nested subprograms, and
2692 -- rejected an inline directive if nested subprograms were present,
2693 -- because the back-end would generate conflicting symbols for the
2694 -- nested bodies. This is now unnecessary.
2696 -- Look ahead to recognize a pragma Inline that appears after the body
2698 Check_Inline_Pragma (Spec_Id);
2700 -- Deal with special case of a fully private operation in the body of
2701 -- the protected type. We must create a declaration for the subprogram,
2702 -- in order to attach the protected subprogram that will be used in
2703 -- internal calls. We exclude compiler generated bodies from the
2704 -- expander since the issue does not arise for those cases.
2707 and then Comes_From_Source (N)
2708 and then Is_Protected_Type (Current_Scope)
2710 Spec_Id := Build_Private_Protected_Declaration (N);
2713 -- If a separate spec is present, then deal with freezing issues
2715 if Present (Spec_Id) then
2716 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2717 Verify_Overriding_Indicator;
2719 -- In general, the spec will be frozen when we start analyzing the
2720 -- body. However, for internally generated operations, such as
2721 -- wrapper functions for inherited operations with controlling
2722 -- results, the spec may not have been frozen by the time we expand
2723 -- the freeze actions that include the bodies. In particular, extra
2724 -- formals for accessibility or for return-in-place may need to be
2725 -- generated. Freeze nodes, if any, are inserted before the current
2726 -- body. These freeze actions are also needed in ASIS mode to enable
2727 -- the proper back-annotations.
2729 if not Is_Frozen (Spec_Id)
2730 and then (Expander_Active or ASIS_Mode)
2732 -- Force the generation of its freezing node to ensure proper
2733 -- management of access types in the backend.
2735 -- This is definitely needed for some cases, but it is not clear
2736 -- why, to be investigated further???
2738 Set_Has_Delayed_Freeze (Spec_Id);
2739 Freeze_Before (N, Spec_Id);
2743 -- Mark presence of postcondition procedure in current scope and mark
2744 -- the procedure itself as needing debug info. The latter is important
2745 -- when analyzing decision coverage (for example, for MC/DC coverage).
2747 if Chars (Body_Id) = Name_uPostconditions then
2748 Set_Has_Postconditions (Current_Scope);
2749 Set_Debug_Info_Needed (Body_Id);
2752 -- Place subprogram on scope stack, and make formals visible. If there
2753 -- is a spec, the visible entity remains that of the spec.
2755 if Present (Spec_Id) then
2756 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2758 if Is_Child_Unit (Spec_Id) then
2759 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2763 Style.Check_Identifier (Body_Id, Spec_Id);
2766 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2767 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2769 if Is_Abstract_Subprogram (Spec_Id) then
2770 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2774 Set_Convention (Body_Id, Convention (Spec_Id));
2775 Set_Has_Completion (Spec_Id);
2777 if Is_Protected_Type (Scope (Spec_Id)) then
2778 Prot_Typ := Scope (Spec_Id);
2781 -- If this is a body generated for a renaming, do not check for
2782 -- full conformance. The check is redundant, because the spec of
2783 -- the body is a copy of the spec in the renaming declaration,
2784 -- and the test can lead to spurious errors on nested defaults.
2786 if Present (Spec_Decl)
2787 and then not Comes_From_Source (N)
2789 (Nkind (Original_Node (Spec_Decl)) =
2790 N_Subprogram_Renaming_Declaration
2791 or else (Present (Corresponding_Body (Spec_Decl))
2793 Nkind (Unit_Declaration_Node
2794 (Corresponding_Body (Spec_Decl))) =
2795 N_Subprogram_Renaming_Declaration))
2799 -- Conversely, the spec may have been generated for specless body
2800 -- with an inline pragma.
2802 elsif Comes_From_Source (N)
2803 and then not Comes_From_Source (Spec_Id)
2804 and then Has_Pragma_Inline (Spec_Id)
2811 Fully_Conformant, True, Conformant, Body_Id);
2814 -- If the body is not fully conformant, we have to decide if we
2815 -- should analyze it or not. If it has a really messed up profile
2816 -- then we probably should not analyze it, since we will get too
2817 -- many bogus messages.
2819 -- Our decision is to go ahead in the non-fully conformant case
2820 -- only if it is at least mode conformant with the spec. Note
2821 -- that the call to Check_Fully_Conformant has issued the proper
2822 -- error messages to complain about the lack of conformance.
2825 and then not Mode_Conformant (Body_Id, Spec_Id)
2831 if Spec_Id /= Body_Id then
2832 Reference_Body_Formals (Spec_Id, Body_Id);
2835 if Nkind (N) /= N_Subprogram_Body_Stub then
2836 Set_Corresponding_Spec (N, Spec_Id);
2838 -- Ada 2005 (AI-345): If the operation is a primitive operation
2839 -- of a concurrent type, the type of the first parameter has been
2840 -- replaced with the corresponding record, which is the proper
2841 -- run-time structure to use. However, within the body there may
2842 -- be uses of the formals that depend on primitive operations
2843 -- of the type (in particular calls in prefixed form) for which
2844 -- we need the original concurrent type. The operation may have
2845 -- several controlling formals, so the replacement must be done
2848 if Comes_From_Source (Spec_Id)
2849 and then Present (First_Entity (Spec_Id))
2850 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2851 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2853 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2856 (Corresponding_Concurrent_Type
2857 (Etype (First_Entity (Spec_Id))))
2860 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2864 Form := First_Formal (Spec_Id);
2865 while Present (Form) loop
2866 if Etype (Form) = Typ then
2867 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2875 -- Make the formals visible, and place subprogram on scope stack.
2876 -- This is also the point at which we set Last_Real_Spec_Entity
2877 -- to mark the entities which will not be moved to the body.
2879 Install_Formals (Spec_Id);
2880 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2882 -- Within an instance, add local renaming declarations so that
2883 -- gdb can retrieve the values of actuals more easily. This is
2884 -- only relevant if generating code (and indeed we definitely
2885 -- do not want these definitions -gnatc mode, because that would
2888 if Is_Generic_Instance (Spec_Id)
2889 and then Is_Wrapper_Package (Current_Scope)
2890 and then Expander_Active
2892 Build_Subprogram_Instance_Renamings (N, Current_Scope);
2895 Push_Scope (Spec_Id);
2897 -- Make sure that the subprogram is immediately visible. For
2898 -- child units that have no separate spec this is indispensable.
2899 -- Otherwise it is safe albeit redundant.
2901 Set_Is_Immediately_Visible (Spec_Id);
2904 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2905 Set_Ekind (Body_Id, E_Subprogram_Body);
2906 Set_Scope (Body_Id, Scope (Spec_Id));
2907 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2909 -- Case of subprogram body with no previous spec
2912 -- Check for style warning required
2916 -- Only apply check for source level subprograms for which checks
2917 -- have not been suppressed.
2919 and then Comes_From_Source (Body_Id)
2920 and then not Suppress_Style_Checks (Body_Id)
2922 -- No warnings within an instance
2924 and then not In_Instance
2926 -- No warnings for expression functions
2928 and then Nkind (Original_Node (N)) /= N_Expression_Function
2930 Style.Body_With_No_Spec (N);
2933 New_Overloaded_Entity (Body_Id);
2935 if Nkind (N) /= N_Subprogram_Body_Stub then
2936 Set_Acts_As_Spec (N);
2937 Generate_Definition (Body_Id);
2938 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
2940 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2941 Install_Formals (Body_Id);
2942 Push_Scope (Body_Id);
2945 -- For stubs and bodies with no previous spec, generate references to
2948 Generate_Reference_To_Formals (Body_Id);
2951 -- If the return type is an anonymous access type whose designated type
2952 -- is the limited view of a class-wide type and the non-limited view is
2953 -- available, update the return type accordingly.
2955 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
2961 Rtyp := Etype (Current_Scope);
2963 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2964 Etyp := Directly_Designated_Type (Rtyp);
2966 if Is_Class_Wide_Type (Etyp) and then From_With_Type (Etyp) then
2967 Set_Directly_Designated_Type
2968 (Etype (Current_Scope), Available_View (Etyp));
2974 -- If this is the proper body of a stub, we must verify that the stub
2975 -- conforms to the body, and to the previous spec if one was present.
2976 -- We know already that the body conforms to that spec. This test is
2977 -- only required for subprograms that come from source.
2979 if Nkind (Parent (N)) = N_Subunit
2980 and then Comes_From_Source (N)
2981 and then not Error_Posted (Body_Id)
2982 and then Nkind (Corresponding_Stub (Parent (N))) =
2983 N_Subprogram_Body_Stub
2986 Old_Id : constant Entity_Id :=
2988 (Specification (Corresponding_Stub (Parent (N))));
2990 Conformant : Boolean := False;
2993 if No (Spec_Id) then
2994 Check_Fully_Conformant (Body_Id, Old_Id);
2998 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3000 if not Conformant then
3002 -- The stub was taken to be a new declaration. Indicate that
3005 Set_Has_Completion (Old_Id, False);
3011 Set_Has_Completion (Body_Id);
3012 Check_Eliminated (Body_Id);
3014 if Nkind (N) = N_Subprogram_Body_Stub then
3018 -- Handle frontend inlining. There is no need to prepare us for inlining
3019 -- if we will not generate the code.
3023 if not Debug_Flag_Dot_K then
3024 if Present (Spec_Id)
3025 and then Expander_Active
3027 (Has_Pragma_Inline_Always (Spec_Id)
3028 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3030 Build_Body_To_Inline (N, Spec_Id);
3035 elsif Expander_Active
3036 and then Serious_Errors_Detected = 0
3037 and then Present (Spec_Id)
3038 and then Has_Pragma_Inline (Spec_Id)
3040 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3043 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3044 -- of the specification we have to install the private withed units.
3045 -- This holds for child units as well.
3047 if Is_Compilation_Unit (Body_Id)
3048 or else Nkind (Parent (N)) = N_Compilation_Unit
3050 Install_Private_With_Clauses (Body_Id);
3053 Check_Anonymous_Return;
3055 -- Set the Protected_Formal field of each extra formal of the protected
3056 -- subprogram to reference the corresponding extra formal of the
3057 -- subprogram that implements it. For regular formals this occurs when
3058 -- the protected subprogram's declaration is expanded, but the extra
3059 -- formals don't get created until the subprogram is frozen. We need to
3060 -- do this before analyzing the protected subprogram's body so that any
3061 -- references to the original subprogram's extra formals will be changed
3062 -- refer to the implementing subprogram's formals (see Expand_Formal).
3064 if Present (Spec_Id)
3065 and then Is_Protected_Type (Scope (Spec_Id))
3066 and then Present (Protected_Body_Subprogram (Spec_Id))
3069 Impl_Subp : constant Entity_Id :=
3070 Protected_Body_Subprogram (Spec_Id);
3071 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3072 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3074 while Present (Prot_Ext_Formal) loop
3075 pragma Assert (Present (Impl_Ext_Formal));
3076 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3077 Next_Formal_With_Extras (Prot_Ext_Formal);
3078 Next_Formal_With_Extras (Impl_Ext_Formal);
3083 -- Now we can go on to analyze the body
3085 HSS := Handled_Statement_Sequence (N);
3086 Set_Actual_Subtypes (N, Current_Scope);
3088 -- Deal with preconditions and postconditions. In formal verification
3089 -- mode, we keep pre- and postconditions attached to entities rather
3090 -- than inserted in the code, in order to facilitate a distinct
3091 -- treatment for them.
3093 if not Alfa_Mode then
3094 Process_PPCs (N, Spec_Id, Body_Id);
3097 -- Add a declaration for the Protection object, renaming declarations
3098 -- for discriminals and privals and finally a declaration for the entry
3099 -- family index (if applicable). This form of early expansion is done
3100 -- when the Expander is active because Install_Private_Data_Declarations
3101 -- references entities which were created during regular expansion. The
3102 -- body may be the rewritting of an expression function, and we need to
3103 -- verify that the original node is in the source.
3105 if Full_Expander_Active
3106 and then Comes_From_Source (Original_Node (N))
3107 and then Present (Prot_Typ)
3108 and then Present (Spec_Id)
3109 and then not Is_Eliminated (Spec_Id)
3111 Install_Private_Data_Declarations
3112 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3115 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3116 -- may now appear in parameter and result profiles. Since the analysis
3117 -- of a subprogram body may use the parameter and result profile of the
3118 -- spec, swap any limited views with their non-limited counterpart.
3120 if Ada_Version >= Ada_2012 then
3121 Exchange_Limited_Views (Spec_Id);
3124 -- Analyze the declarations (this call will analyze the precondition
3125 -- Check pragmas we prepended to the list, as well as the declaration
3126 -- of the _Postconditions procedure).
3128 Analyze_Declarations (Declarations (N));
3130 -- Check completion, and analyze the statements
3133 Inspect_Deferred_Constant_Completion (Declarations (N));
3136 -- Deal with end of scope processing for the body
3138 Process_End_Label (HSS, 't', Current_Scope);
3140 Check_Subprogram_Order (N);
3141 Set_Analyzed (Body_Id);
3143 -- If we have a separate spec, then the analysis of the declarations
3144 -- caused the entities in the body to be chained to the spec id, but
3145 -- we want them chained to the body id. Only the formal parameters
3146 -- end up chained to the spec id in this case.
3148 if Present (Spec_Id) then
3150 -- We must conform to the categorization of our spec
3152 Validate_Categorization_Dependency (N, Spec_Id);
3154 -- And if this is a child unit, the parent units must conform
3156 if Is_Child_Unit (Spec_Id) then
3157 Validate_Categorization_Dependency
3158 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3161 -- Here is where we move entities from the spec to the body
3163 -- Case where there are entities that stay with the spec
3165 if Present (Last_Real_Spec_Entity) then
3167 -- No body entities (happens when the only real spec entities come
3168 -- from precondition and postcondition pragmas).
3170 if No (Last_Entity (Body_Id)) then
3172 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3174 -- Body entities present (formals), so chain stuff past them
3178 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3181 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3182 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3183 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3185 -- Case where there are no spec entities, in this case there can be
3186 -- no body entities either, so just move everything.
3189 pragma Assert (No (Last_Entity (Body_Id)));
3190 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3191 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3192 Set_First_Entity (Spec_Id, Empty);
3193 Set_Last_Entity (Spec_Id, Empty);
3197 Check_Missing_Return;
3199 -- Now we are going to check for variables that are never modified in
3200 -- the body of the procedure. But first we deal with a special case
3201 -- where we want to modify this check. If the body of the subprogram
3202 -- starts with a raise statement or its equivalent, or if the body
3203 -- consists entirely of a null statement, then it is pretty obvious
3204 -- that it is OK to not reference the parameters. For example, this
3205 -- might be the following common idiom for a stubbed function:
3206 -- statement of the procedure raises an exception. In particular this
3207 -- deals with the common idiom of a stubbed function, which might
3208 -- appear as something like:
3210 -- function F (A : Integer) return Some_Type;
3213 -- raise Program_Error;
3217 -- Here the purpose of X is simply to satisfy the annoying requirement
3218 -- in Ada that there be at least one return, and we certainly do not
3219 -- want to go posting warnings on X that it is not initialized! On
3220 -- the other hand, if X is entirely unreferenced that should still
3223 -- What we do is to detect these cases, and if we find them, flag the
3224 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3225 -- suppress unwanted warnings. For the case of the function stub above
3226 -- we have a special test to set X as apparently assigned to suppress
3233 -- Skip initial labels (for one thing this occurs when we are in
3234 -- front end ZCX mode, but in any case it is irrelevant), and also
3235 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3237 Stm := First (Statements (HSS));
3238 while Nkind (Stm) = N_Label
3239 or else Nkind (Stm) in N_Push_xxx_Label
3244 -- Do the test on the original statement before expansion
3247 Ostm : constant Node_Id := Original_Node (Stm);
3250 -- If explicit raise statement, turn on flag
3252 if Nkind (Ostm) = N_Raise_Statement then
3253 Set_Trivial_Subprogram (Stm);
3255 -- If null statement, and no following statements, turn on flag
3257 elsif Nkind (Stm) = N_Null_Statement
3258 and then Comes_From_Source (Stm)
3259 and then No (Next (Stm))
3261 Set_Trivial_Subprogram (Stm);
3263 -- Check for explicit call cases which likely raise an exception
3265 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3266 if Is_Entity_Name (Name (Ostm)) then
3268 Ent : constant Entity_Id := Entity (Name (Ostm));
3271 -- If the procedure is marked No_Return, then likely it
3272 -- raises an exception, but in any case it is not coming
3273 -- back here, so turn on the flag.
3276 and then Ekind (Ent) = E_Procedure
3277 and then No_Return (Ent)
3279 Set_Trivial_Subprogram (Stm);
3287 -- Check for variables that are never modified
3293 -- If there is a separate spec, then transfer Never_Set_In_Source
3294 -- flags from out parameters to the corresponding entities in the
3295 -- body. The reason we do that is we want to post error flags on
3296 -- the body entities, not the spec entities.
3298 if Present (Spec_Id) then
3299 E1 := First_Entity (Spec_Id);
3300 while Present (E1) loop
3301 if Ekind (E1) = E_Out_Parameter then
3302 E2 := First_Entity (Body_Id);
3303 while Present (E2) loop
3304 exit when Chars (E1) = Chars (E2);
3308 if Present (E2) then
3309 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3317 -- Check references in body
3319 Check_References (Body_Id);
3321 end Analyze_Subprogram_Body_Helper;
3323 ------------------------------------
3324 -- Analyze_Subprogram_Declaration --
3325 ------------------------------------
3327 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3328 Scop : constant Entity_Id := Current_Scope;
3329 Designator : Entity_Id;
3330 Is_Completion : Boolean;
3331 -- Indicates whether a null procedure declaration is a completion
3334 -- Null procedures are not allowed in SPARK
3336 if Nkind (Specification (N)) = N_Procedure_Specification
3337 and then Null_Present (Specification (N))
3339 Check_SPARK_Restriction ("null procedure is not allowed", N);
3341 if Is_Protected_Type (Current_Scope) then
3342 Error_Msg_N ("protected operation cannot be a null procedure", N);
3345 Analyze_Null_Procedure (N, Is_Completion);
3347 if Is_Completion then
3349 -- The null procedure acts as a body, nothing further is needed.
3355 Designator := Analyze_Subprogram_Specification (Specification (N));
3357 -- A reference may already have been generated for the unit name, in
3358 -- which case the following call is redundant. However it is needed for
3359 -- declarations that are the rewriting of an expression function.
3361 Generate_Definition (Designator);
3363 if Debug_Flag_C then
3364 Write_Str ("==> subprogram spec ");
3365 Write_Name (Chars (Designator));
3366 Write_Str (" from ");
3367 Write_Location (Sloc (N));
3372 Validate_RCI_Subprogram_Declaration (N);
3373 New_Overloaded_Entity (Designator);
3374 Check_Delayed_Subprogram (Designator);
3376 -- If the type of the first formal of the current subprogram is a
3377 -- non-generic tagged private type, mark the subprogram as being a
3378 -- private primitive. Ditto if this is a function with controlling
3379 -- result, and the return type is currently private. In both cases,
3380 -- the type of the controlling argument or result must be in the
3381 -- current scope for the operation to be primitive.
3383 if Has_Controlling_Result (Designator)
3384 and then Is_Private_Type (Etype (Designator))
3385 and then Scope (Etype (Designator)) = Current_Scope
3386 and then not Is_Generic_Actual_Type (Etype (Designator))
3388 Set_Is_Private_Primitive (Designator);
3390 elsif Present (First_Formal (Designator)) then
3392 Formal_Typ : constant Entity_Id :=
3393 Etype (First_Formal (Designator));
3395 Set_Is_Private_Primitive (Designator,
3396 Is_Tagged_Type (Formal_Typ)
3397 and then Scope (Formal_Typ) = Current_Scope
3398 and then Is_Private_Type (Formal_Typ)
3399 and then not Is_Generic_Actual_Type (Formal_Typ));
3403 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3406 if Ada_Version >= Ada_2005
3407 and then Comes_From_Source (N)
3408 and then Is_Dispatching_Operation (Designator)
3415 if Has_Controlling_Result (Designator) then
3416 Etyp := Etype (Designator);
3419 E := First_Entity (Designator);
3421 and then Is_Formal (E)
3422 and then not Is_Controlling_Formal (E)
3430 if Is_Access_Type (Etyp) then
3431 Etyp := Directly_Designated_Type (Etyp);
3434 if Is_Interface (Etyp)
3435 and then not Is_Abstract_Subprogram (Designator)
3436 and then not (Ekind (Designator) = E_Procedure
3437 and then Null_Present (Specification (N)))
3439 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3441 -- Specialize error message based on procedures vs. functions,
3442 -- since functions can't be null subprograms.
3444 if Ekind (Designator) = E_Procedure then
3446 ("interface procedure % must be abstract or null", N);
3448 Error_Msg_N ("interface function % must be abstract", N);
3454 -- What is the following code for, it used to be
3456 -- ??? Set_Suppress_Elaboration_Checks
3457 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3459 -- The following seems equivalent, but a bit dubious
3461 if Elaboration_Checks_Suppressed (Designator) then
3462 Set_Kill_Elaboration_Checks (Designator);
3465 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3466 Set_Categorization_From_Scope (Designator, Scop);
3469 -- For a compilation unit, check for library-unit pragmas
3471 Push_Scope (Designator);
3472 Set_Categorization_From_Pragmas (N);
3473 Validate_Categorization_Dependency (N, Designator);
3477 -- For a compilation unit, set body required. This flag will only be
3478 -- reset if a valid Import or Interface pragma is processed later on.
3480 if Nkind (Parent (N)) = N_Compilation_Unit then
3481 Set_Body_Required (Parent (N), True);
3483 if Ada_Version >= Ada_2005
3484 and then Nkind (Specification (N)) = N_Procedure_Specification
3485 and then Null_Present (Specification (N))
3488 ("null procedure cannot be declared at library level", N);
3492 Generate_Reference_To_Formals (Designator);
3493 Check_Eliminated (Designator);
3495 if Debug_Flag_C then
3497 Write_Str ("<== subprogram spec ");
3498 Write_Name (Chars (Designator));
3499 Write_Str (" from ");
3500 Write_Location (Sloc (N));
3504 if Is_Protected_Type (Current_Scope) then
3506 -- Indicate that this is a protected operation, because it may be
3507 -- used in subsequent declarations within the protected type.
3509 Set_Convention (Designator, Convention_Protected);
3512 List_Inherited_Pre_Post_Aspects (Designator);
3514 if Has_Aspects (N) then
3515 Analyze_Aspect_Specifications (N, Designator);
3517 end Analyze_Subprogram_Declaration;
3519 --------------------------------------
3520 -- Analyze_Subprogram_Specification --
3521 --------------------------------------
3523 -- Reminder: N here really is a subprogram specification (not a subprogram
3524 -- declaration). This procedure is called to analyze the specification in
3525 -- both subprogram bodies and subprogram declarations (specs).
3527 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3528 Designator : constant Entity_Id := Defining_Entity (N);
3529 Formals : constant List_Id := Parameter_Specifications (N);
3531 -- Start of processing for Analyze_Subprogram_Specification
3534 -- User-defined operator is not allowed in SPARK, except as a renaming
3536 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3537 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3539 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3542 -- Proceed with analysis. Do not emit a cross-reference entry if the
3543 -- specification comes from an expression function, because it may be
3544 -- the completion of a previous declaration. It is is not, the cross-
3545 -- reference entry will be emitted for the new subprogram declaration.
3547 if Nkind (Parent (N)) /= N_Expression_Function then
3548 Generate_Definition (Designator);
3551 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3553 if Nkind (N) = N_Function_Specification then
3554 Set_Ekind (Designator, E_Function);
3555 Set_Mechanism (Designator, Default_Mechanism);
3557 Set_Ekind (Designator, E_Procedure);
3558 Set_Etype (Designator, Standard_Void_Type);
3561 -- Introduce new scope for analysis of the formals and the return type
3563 Set_Scope (Designator, Current_Scope);
3565 if Present (Formals) then
3566 Push_Scope (Designator);
3567 Process_Formals (Formals, N);
3569 -- Check dimensions in N for formals with default expression
3571 Analyze_Dimension_Formals (N, Formals);
3573 -- Ada 2005 (AI-345): If this is an overriding operation of an
3574 -- inherited interface operation, and the controlling type is
3575 -- a synchronized type, replace the type with its corresponding
3576 -- record, to match the proper signature of an overriding operation.
3577 -- Same processing for an access parameter whose designated type is
3578 -- derived from a synchronized interface.
3580 if Ada_Version >= Ada_2005 then
3583 Formal_Typ : Entity_Id;
3584 Rec_Typ : Entity_Id;
3585 Desig_Typ : Entity_Id;
3588 Formal := First_Formal (Designator);
3589 while Present (Formal) loop
3590 Formal_Typ := Etype (Formal);
3592 if Is_Concurrent_Type (Formal_Typ)
3593 and then Present (Corresponding_Record_Type (Formal_Typ))
3595 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3597 if Present (Interfaces (Rec_Typ)) then
3598 Set_Etype (Formal, Rec_Typ);
3601 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3602 Desig_Typ := Designated_Type (Formal_Typ);
3604 if Is_Concurrent_Type (Desig_Typ)
3605 and then Present (Corresponding_Record_Type (Desig_Typ))
3607 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3609 if Present (Interfaces (Rec_Typ)) then
3610 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3615 Next_Formal (Formal);
3622 -- The subprogram scope is pushed and popped around the processing of
3623 -- the return type for consistency with call above to Process_Formals
3624 -- (which itself can call Analyze_Return_Type), and to ensure that any
3625 -- itype created for the return type will be associated with the proper
3628 elsif Nkind (N) = N_Function_Specification then
3629 Push_Scope (Designator);
3630 Analyze_Return_Type (N);
3636 if Nkind (N) = N_Function_Specification then
3638 -- Deal with operator symbol case
3640 if Nkind (Designator) = N_Defining_Operator_Symbol then
3641 Valid_Operator_Definition (Designator);
3644 May_Need_Actuals (Designator);
3646 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3647 -- the subprogram is abstract also. This does not apply to renaming
3648 -- declarations, where abstractness is inherited, and to subprogram
3649 -- bodies generated for stream operations, which become renamings as
3652 -- In case of primitives associated with abstract interface types
3653 -- the check is applied later (see Analyze_Subprogram_Declaration).
3655 if not Nkind_In (Original_Node (Parent (N)),
3656 N_Subprogram_Renaming_Declaration,
3657 N_Abstract_Subprogram_Declaration,
3658 N_Formal_Abstract_Subprogram_Declaration)
3660 if Is_Abstract_Type (Etype (Designator))
3661 and then not Is_Interface (Etype (Designator))
3664 ("function that returns abstract type must be abstract", N);
3666 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3667 -- access result whose designated type is abstract.
3669 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3671 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3672 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3673 and then Ada_Version >= Ada_2012
3675 Error_Msg_N ("function whose access result designates "
3676 & "abstract type must be abstract", N);
3682 end Analyze_Subprogram_Specification;
3684 --------------------------
3685 -- Build_Body_To_Inline --
3686 --------------------------
3688 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3689 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3690 Original_Body : Node_Id;
3691 Body_To_Analyze : Node_Id;
3692 Max_Size : constant := 10;
3693 Stat_Count : Integer := 0;
3695 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3696 -- Check for declarations that make inlining not worthwhile
3698 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3699 -- Check for statements that make inlining not worthwhile: any tasking
3700 -- statement, nested at any level. Keep track of total number of
3701 -- elementary statements, as a measure of acceptable size.
3703 function Has_Pending_Instantiation return Boolean;
3704 -- If some enclosing body contains instantiations that appear before the
3705 -- corresponding generic body, the enclosing body has a freeze node so
3706 -- that it can be elaborated after the generic itself. This might
3707 -- conflict with subsequent inlinings, so that it is unsafe to try to
3708 -- inline in such a case.
3710 function Has_Single_Return return Boolean;
3711 -- In general we cannot inline functions that return unconstrained type.
3712 -- However, we can handle such functions if all return statements return
3713 -- a local variable that is the only declaration in the body of the
3714 -- function. In that case the call can be replaced by that local
3715 -- variable as is done for other inlined calls.
3717 procedure Remove_Pragmas;
3718 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3719 -- parameter has no meaning when the body is inlined and the formals
3720 -- are rewritten. Remove it from body to inline. The analysis of the
3721 -- non-inlined body will handle the pragma properly.
3723 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3724 -- If the body of the subprogram includes a call that returns an
3725 -- unconstrained type, the secondary stack is involved, and it
3726 -- is not worth inlining.
3728 ------------------------------
3729 -- Has_Excluded_Declaration --
3730 ------------------------------
3732 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3735 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3736 -- Nested subprograms make a given body ineligible for inlining, but
3737 -- we make an exception for instantiations of unchecked conversion.
3738 -- The body has not been analyzed yet, so check the name, and verify
3739 -- that the visible entity with that name is the predefined unit.
3741 -----------------------------
3742 -- Is_Unchecked_Conversion --
3743 -----------------------------
3745 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3746 Id : constant Node_Id := Name (D);
3750 if Nkind (Id) = N_Identifier
3751 and then Chars (Id) = Name_Unchecked_Conversion
3753 Conv := Current_Entity (Id);
3755 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3756 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3758 Conv := Current_Entity (Selector_Name (Id));
3763 return Present (Conv)
3764 and then Is_Predefined_File_Name
3765 (Unit_File_Name (Get_Source_Unit (Conv)))
3766 and then Is_Intrinsic_Subprogram (Conv);
3767 end Is_Unchecked_Conversion;
3769 -- Start of processing for Has_Excluded_Declaration
3773 while Present (D) loop
3774 if (Nkind (D) = N_Function_Instantiation
3775 and then not Is_Unchecked_Conversion (D))
3776 or else Nkind_In (D, N_Protected_Type_Declaration,
3777 N_Package_Declaration,
3778 N_Package_Instantiation,
3780 N_Procedure_Instantiation,
3781 N_Task_Type_Declaration)
3784 ("cannot inline & (non-allowed declaration)?", D, Subp);
3792 end Has_Excluded_Declaration;
3794 ----------------------------
3795 -- Has_Excluded_Statement --
3796 ----------------------------
3798 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3804 while Present (S) loop
3805 Stat_Count := Stat_Count + 1;
3807 if Nkind_In (S, N_Abort_Statement,
3808 N_Asynchronous_Select,
3809 N_Conditional_Entry_Call,
3810 N_Delay_Relative_Statement,
3811 N_Delay_Until_Statement,
3816 ("cannot inline & (non-allowed statement)?", S, Subp);
3819 elsif Nkind (S) = N_Block_Statement then
3820 if Present (Declarations (S))
3821 and then Has_Excluded_Declaration (Declarations (S))
3825 elsif Present (Handled_Statement_Sequence (S))
3828 (Exception_Handlers (Handled_Statement_Sequence (S)))
3830 Has_Excluded_Statement
3831 (Statements (Handled_Statement_Sequence (S))))
3836 elsif Nkind (S) = N_Case_Statement then
3837 E := First (Alternatives (S));
3838 while Present (E) loop
3839 if Has_Excluded_Statement (Statements (E)) then
3846 elsif Nkind (S) = N_If_Statement then
3847 if Has_Excluded_Statement (Then_Statements (S)) then
3851 if Present (Elsif_Parts (S)) then
3852 E := First (Elsif_Parts (S));
3853 while Present (E) loop
3854 if Has_Excluded_Statement (Then_Statements (E)) then
3862 if Present (Else_Statements (S))
3863 and then Has_Excluded_Statement (Else_Statements (S))
3868 elsif Nkind (S) = N_Loop_Statement
3869 and then Has_Excluded_Statement (Statements (S))
3873 elsif Nkind (S) = N_Extended_Return_Statement then
3874 if Has_Excluded_Statement
3875 (Statements (Handled_Statement_Sequence (S)))
3877 (Exception_Handlers (Handled_Statement_Sequence (S)))
3887 end Has_Excluded_Statement;
3889 -------------------------------
3890 -- Has_Pending_Instantiation --
3891 -------------------------------
3893 function Has_Pending_Instantiation return Boolean is
3898 while Present (S) loop
3899 if Is_Compilation_Unit (S)
3900 or else Is_Child_Unit (S)
3904 elsif Ekind (S) = E_Package
3905 and then Has_Forward_Instantiation (S)
3914 end Has_Pending_Instantiation;
3916 ------------------------
3917 -- Has_Single_Return --
3918 ------------------------
3920 function Has_Single_Return return Boolean is
3921 Return_Statement : Node_Id := Empty;
3923 function Check_Return (N : Node_Id) return Traverse_Result;
3929 function Check_Return (N : Node_Id) return Traverse_Result is
3931 if Nkind (N) = N_Simple_Return_Statement then
3932 if Present (Expression (N))
3933 and then Is_Entity_Name (Expression (N))
3935 if No (Return_Statement) then
3936 Return_Statement := N;
3939 elsif Chars (Expression (N)) =
3940 Chars (Expression (Return_Statement))
3948 -- A return statement within an extended return is a noop
3951 elsif No (Expression (N))
3952 and then Nkind (Parent (Parent (N))) =
3953 N_Extended_Return_Statement
3958 -- Expression has wrong form
3963 -- We can only inline a build-in-place function if
3964 -- it has a single extended return.
3966 elsif Nkind (N) = N_Extended_Return_Statement then
3967 if No (Return_Statement) then
3968 Return_Statement := N;
3980 function Check_All_Returns is new Traverse_Func (Check_Return);
3982 -- Start of processing for Has_Single_Return
3985 if Check_All_Returns (N) /= OK then
3988 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3992 return Present (Declarations (N))
3993 and then Present (First (Declarations (N)))
3994 and then Chars (Expression (Return_Statement)) =
3995 Chars (Defining_Identifier (First (Declarations (N))));
3997 end Has_Single_Return;
3999 --------------------
4000 -- Remove_Pragmas --
4001 --------------------
4003 procedure Remove_Pragmas is
4008 Decl := First (Declarations (Body_To_Analyze));
4009 while Present (Decl) loop
4012 if Nkind (Decl) = N_Pragma
4013 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4023 --------------------------
4024 -- Uses_Secondary_Stack --
4025 --------------------------
4027 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4028 function Check_Call (N : Node_Id) return Traverse_Result;
4029 -- Look for function calls that return an unconstrained type
4035 function Check_Call (N : Node_Id) return Traverse_Result is
4037 if Nkind (N) = N_Function_Call
4038 and then Is_Entity_Name (Name (N))
4039 and then Is_Composite_Type (Etype (Entity (Name (N))))
4040 and then not Is_Constrained (Etype (Entity (Name (N))))
4043 ("cannot inline & (call returns unconstrained type)?",
4051 function Check_Calls is new Traverse_Func (Check_Call);
4054 return Check_Calls (Bod) = Abandon;
4055 end Uses_Secondary_Stack;
4057 -- Start of processing for Build_Body_To_Inline
4060 -- Return immediately if done already
4062 if Nkind (Decl) = N_Subprogram_Declaration
4063 and then Present (Body_To_Inline (Decl))
4067 -- Functions that return unconstrained composite types require
4068 -- secondary stack handling, and cannot currently be inlined, unless
4069 -- all return statements return a local variable that is the first
4070 -- local declaration in the body.
4072 elsif Ekind (Subp) = E_Function
4073 and then not Is_Scalar_Type (Etype (Subp))
4074 and then not Is_Access_Type (Etype (Subp))
4075 and then not Is_Constrained (Etype (Subp))
4077 if not Has_Single_Return then
4079 ("cannot inline & (unconstrained return type)?", N, Subp);
4083 -- Ditto for functions that return controlled types, where controlled
4084 -- actions interfere in complex ways with inlining.
4086 elsif Ekind (Subp) = E_Function
4087 and then Needs_Finalization (Etype (Subp))
4090 ("cannot inline & (controlled return type)?", N, Subp);
4094 if Present (Declarations (N))
4095 and then Has_Excluded_Declaration (Declarations (N))
4100 if Present (Handled_Statement_Sequence (N)) then
4101 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4103 ("cannot inline& (exception handler)?",
4104 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4108 Has_Excluded_Statement
4109 (Statements (Handled_Statement_Sequence (N)))
4115 -- We do not inline a subprogram that is too large, unless it is
4116 -- marked Inline_Always. This pragma does not suppress the other
4117 -- checks on inlining (forbidden declarations, handlers, etc).
4119 if Stat_Count > Max_Size
4120 and then not Has_Pragma_Inline_Always (Subp)
4122 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4126 if Has_Pending_Instantiation then
4128 ("cannot inline& (forward instance within enclosing body)?",
4133 -- Within an instance, the body to inline must be treated as a nested
4134 -- generic, so that the proper global references are preserved.
4136 -- Note that we do not do this at the library level, because it is not
4137 -- needed, and furthermore this causes trouble if front end inlining
4138 -- is activated (-gnatN).
4140 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4141 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4142 Original_Body := Copy_Generic_Node (N, Empty, True);
4144 Original_Body := Copy_Separate_Tree (N);
4147 -- We need to capture references to the formals in order to substitute
4148 -- the actuals at the point of inlining, i.e. instantiation. To treat
4149 -- the formals as globals to the body to inline, we nest it within
4150 -- a dummy parameterless subprogram, declared within the real one.
4151 -- To avoid generating an internal name (which is never public, and
4152 -- which affects serial numbers of other generated names), we use
4153 -- an internal symbol that cannot conflict with user declarations.
4155 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4156 Set_Defining_Unit_Name
4157 (Specification (Original_Body),
4158 Make_Defining_Identifier (Sloc (N), Name_uParent));
4159 Set_Corresponding_Spec (Original_Body, Empty);
4161 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4163 -- Set return type of function, which is also global and does not need
4166 if Ekind (Subp) = E_Function then
4167 Set_Result_Definition (Specification (Body_To_Analyze),
4168 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4171 if No (Declarations (N)) then
4172 Set_Declarations (N, New_List (Body_To_Analyze));
4174 Append (Body_To_Analyze, Declarations (N));
4177 Expander_Mode_Save_And_Set (False);
4180 Analyze (Body_To_Analyze);
4181 Push_Scope (Defining_Entity (Body_To_Analyze));
4182 Save_Global_References (Original_Body);
4184 Remove (Body_To_Analyze);
4186 Expander_Mode_Restore;
4188 -- Restore environment if previously saved
4190 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4194 -- If secondary stk used there is no point in inlining. We have
4195 -- already issued the warning in this case, so nothing to do.
4197 if Uses_Secondary_Stack (Body_To_Analyze) then
4201 Set_Body_To_Inline (Decl, Original_Body);
4202 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4203 Set_Is_Inlined (Subp);
4204 end Build_Body_To_Inline;
4210 procedure Cannot_Inline
4214 Is_Serious : Boolean := False)
4217 pragma Assert (Msg (Msg'Last) = '?');
4221 if not Debug_Flag_Dot_K then
4223 -- Do not emit warning if this is a predefined unit which is not
4224 -- the main unit. With validity checks enabled, some predefined
4225 -- subprograms may contain nested subprograms and become ineligible
4228 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4229 and then not In_Extended_Main_Source_Unit (Subp)
4233 elsif Has_Pragma_Inline_Always (Subp) then
4235 -- Remove last character (question mark) to make this into an
4236 -- error, because the Inline_Always pragma cannot be obeyed.
4238 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4240 elsif Ineffective_Inline_Warnings then
4241 Error_Msg_NE (Msg & "p?", N, Subp);
4248 elsif Is_Serious then
4250 -- Remove last character (question mark) to make this into an error.
4252 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4254 elsif Optimization_Level = 0 then
4256 -- Do not emit warning if this is a predefined unit which is not
4257 -- the main unit. This behavior is currently provided for backward
4258 -- compatibility but it will be removed when we enforce the
4259 -- strictness of the new rules.
4261 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4262 and then not In_Extended_Main_Source_Unit (Subp)
4266 elsif Has_Pragma_Inline_Always (Subp) then
4268 -- Emit a warning if this is a call to a runtime subprogram
4269 -- which is located inside a generic. Previously this call
4270 -- was silently skipped!
4272 if Is_Generic_Instance (Subp) then
4274 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4276 if Is_Predefined_File_Name
4277 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4279 Set_Is_Inlined (Subp, False);
4280 Error_Msg_NE (Msg & "p?", N, Subp);
4286 -- Remove last character (question mark) to make this into an
4287 -- error, because the Inline_Always pragma cannot be obeyed.
4289 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4291 else pragma Assert (Front_End_Inlining);
4292 Set_Is_Inlined (Subp, False);
4294 -- When inlining cannot take place we must issue an error.
4295 -- For backward compatibility we still report a warning.
4297 if Ineffective_Inline_Warnings then
4298 Error_Msg_NE (Msg & "p?", N, Subp);
4302 -- Compiling with optimizations enabled it is too early to report
4303 -- problems since the backend may still perform inlining. In order
4304 -- to report unhandled inlinings the program must be compiled with
4305 -- -Winline and the error is reported by the backend.
4312 ------------------------------------
4313 -- Check_And_Build_Body_To_Inline --
4314 ------------------------------------
4316 procedure Check_And_Build_Body_To_Inline
4318 Spec_Id : Entity_Id;
4319 Body_Id : Entity_Id)
4321 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4322 -- Use generic machinery to build an unexpanded body for the subprogram.
4323 -- This body is subsequently used for inline expansions at call sites.
4325 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4326 -- Return true if we generate code for the function body N, the function
4327 -- body N has no local declarations and its unique statement is a single
4328 -- extended return statement with a handled statements sequence.
4330 function Check_Body_To_Inline
4332 Subp : Entity_Id) return Boolean;
4333 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4334 -- inlined by the frontend. These are the rules:
4335 -- * At -O0 use fe inlining when inline_always is specified except if
4336 -- the function returns a controlled type.
4337 -- * At other optimization levels use the fe inlining for both inline
4338 -- and inline_always in the following cases:
4339 -- - function returning a known at compile time constant
4340 -- - function returning a call to an intrinsic function
4341 -- - function returning an unconstrained type (see Can_Split
4342 -- Unconstrained_Function).
4343 -- - function returning a call to a frontend-inlined function
4344 -- Use the back-end mechanism otherwise
4346 -- In addition, in the following cases the function cannot be inlined by
4348 -- - functions that uses the secondary stack
4349 -- - functions that have declarations of:
4350 -- - Concurrent types
4354 -- - functions that have some of the following statements:
4356 -- - asynchronous-select
4357 -- - conditional-entry-call
4360 -- - selective-accept
4361 -- - timed-entry-call
4362 -- - functions that have exception handlers
4363 -- - functions that have some enclosing body containing instantiations
4364 -- that appear before the corresponding generic body.
4366 procedure Generate_Body_To_Inline
4368 Body_To_Inline : out Node_Id);
4369 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4370 -- of pragmas referencing the formals are removed since they have no
4371 -- meaning when the body is inlined and the formals are rewritten (the
4372 -- analysis of the non-inlined body will handle these pragmas properly).
4373 -- A new internal name is associated with Body_To_Inline.
4375 procedure Split_Unconstrained_Function
4377 Spec_Id : Entity_Id);
4378 -- N is an inlined function body that returns an unconstrained type and
4379 -- has a single extended return statement. Split N in two subprograms:
4380 -- a procedure P' and a function F'. The formals of P' duplicate the
4381 -- formals of N plus an extra formal which is used return a value;
4382 -- its body is composed by the declarations and list of statements
4383 -- of the extended return statement of N.
4385 --------------------------
4386 -- Build_Body_To_Inline --
4387 --------------------------
4389 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4390 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4391 Original_Body : Node_Id;
4392 Body_To_Analyze : Node_Id;
4395 pragma Assert (Current_Scope = Spec_Id);
4397 -- Within an instance, the body to inline must be treated as a nested
4398 -- generic, so that the proper global references are preserved. We
4399 -- do not do this at the library level, because it is not needed, and
4400 -- furthermore this causes trouble if front end inlining is activated
4404 and then Scope (Current_Scope) /= Standard_Standard
4406 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4409 -- We need to capture references to the formals in order
4410 -- to substitute the actuals at the point of inlining, i.e.
4411 -- instantiation. To treat the formals as globals to the body to
4412 -- inline, we nest it within a dummy parameterless subprogram,
4413 -- declared within the real one.
4415 Generate_Body_To_Inline (N, Original_Body);
4416 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4418 -- Set return type of function, which is also global and does not
4419 -- need to be resolved.
4421 if Ekind (Spec_Id) = E_Function then
4422 Set_Result_Definition (Specification (Body_To_Analyze),
4423 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4426 if No (Declarations (N)) then
4427 Set_Declarations (N, New_List (Body_To_Analyze));
4429 Append_To (Declarations (N), Body_To_Analyze);
4432 Preanalyze (Body_To_Analyze);
4434 Push_Scope (Defining_Entity (Body_To_Analyze));
4435 Save_Global_References (Original_Body);
4437 Remove (Body_To_Analyze);
4439 -- Restore environment if previously saved
4442 and then Scope (Current_Scope) /= Standard_Standard
4447 pragma Assert (No (Body_To_Inline (Decl)));
4448 Set_Body_To_Inline (Decl, Original_Body);
4449 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4450 end Build_Body_To_Inline;
4452 --------------------------
4453 -- Check_Body_To_Inline --
4454 --------------------------
4456 function Check_Body_To_Inline
4458 Subp : Entity_Id) return Boolean
4460 Max_Size : constant := 10;
4461 Stat_Count : Integer := 0;
4463 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4464 -- Check for declarations that make inlining not worthwhile
4466 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4467 -- Check for statements that make inlining not worthwhile: any
4468 -- tasking statement, nested at any level. Keep track of total
4469 -- number of elementary statements, as a measure of acceptable size.
4471 function Has_Pending_Instantiation return Boolean;
4472 -- Return True if some enclosing body contains instantiations that
4473 -- appear before the corresponding generic body.
4475 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4476 -- Return True if all the return statements of the function body N
4477 -- are simple return statements and return a compile time constant
4479 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4480 -- Return True if all the return statements of the function body N
4481 -- are simple return statements and return an intrinsic function call
4483 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4484 -- If the body of the subprogram includes a call that returns an
4485 -- unconstrained type, the secondary stack is involved, and it
4486 -- is not worth inlining.
4488 ------------------------------
4489 -- Has_Excluded_Declaration --
4490 ------------------------------
4492 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4495 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4496 -- Nested subprograms make a given body ineligible for inlining,
4497 -- but we make an exception for instantiations of unchecked
4498 -- conversion. The body has not been analyzed yet, so check the
4499 -- name, and verify that the visible entity with that name is the
4502 -----------------------------
4503 -- Is_Unchecked_Conversion --
4504 -----------------------------
4506 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4507 Id : constant Node_Id := Name (D);
4511 if Nkind (Id) = N_Identifier
4512 and then Chars (Id) = Name_Unchecked_Conversion
4514 Conv := Current_Entity (Id);
4516 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4518 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4520 Conv := Current_Entity (Selector_Name (Id));
4525 return Present (Conv)
4526 and then Is_Predefined_File_Name
4527 (Unit_File_Name (Get_Source_Unit (Conv)))
4528 and then Is_Intrinsic_Subprogram (Conv);
4529 end Is_Unchecked_Conversion;
4531 -- Start of processing for Has_Excluded_Declaration
4535 while Present (D) loop
4536 if (Nkind (D) = N_Function_Instantiation
4537 and then not Is_Unchecked_Conversion (D))
4538 or else Nkind_In (D, N_Protected_Type_Declaration,
4539 N_Package_Declaration,
4540 N_Package_Instantiation,
4542 N_Procedure_Instantiation,
4543 N_Task_Type_Declaration)
4546 ("cannot inline & (non-allowed declaration)?", D, Subp);
4555 end Has_Excluded_Declaration;
4557 ----------------------------
4558 -- Has_Excluded_Statement --
4559 ----------------------------
4561 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4567 while Present (S) loop
4568 Stat_Count := Stat_Count + 1;
4570 if Nkind_In (S, N_Abort_Statement,
4571 N_Asynchronous_Select,
4572 N_Conditional_Entry_Call,
4573 N_Delay_Relative_Statement,
4574 N_Delay_Until_Statement,
4579 ("cannot inline & (non-allowed statement)?", S, Subp);
4582 elsif Nkind (S) = N_Block_Statement then
4583 if Present (Declarations (S))
4584 and then Has_Excluded_Declaration (Declarations (S))
4588 elsif Present (Handled_Statement_Sequence (S)) then
4590 (Exception_Handlers (Handled_Statement_Sequence (S)))
4593 ("cannot inline& (exception handler)?",
4594 First (Exception_Handlers
4595 (Handled_Statement_Sequence (S))),
4599 elsif Has_Excluded_Statement
4600 (Statements (Handled_Statement_Sequence (S)))
4606 elsif Nkind (S) = N_Case_Statement then
4607 E := First (Alternatives (S));
4608 while Present (E) loop
4609 if Has_Excluded_Statement (Statements (E)) then
4616 elsif Nkind (S) = N_If_Statement then
4617 if Has_Excluded_Statement (Then_Statements (S)) then
4621 if Present (Elsif_Parts (S)) then
4622 E := First (Elsif_Parts (S));
4623 while Present (E) loop
4624 if Has_Excluded_Statement (Then_Statements (E)) then
4631 if Present (Else_Statements (S))
4632 and then Has_Excluded_Statement (Else_Statements (S))
4637 elsif Nkind (S) = N_Loop_Statement
4638 and then Has_Excluded_Statement (Statements (S))
4642 elsif Nkind (S) = N_Extended_Return_Statement then
4643 if Present (Handled_Statement_Sequence (S))
4645 Has_Excluded_Statement
4646 (Statements (Handled_Statement_Sequence (S)))
4650 elsif Present (Handled_Statement_Sequence (S))
4652 Present (Exception_Handlers
4653 (Handled_Statement_Sequence (S)))
4656 ("cannot inline& (exception handler)?",
4657 First (Exception_Handlers
4658 (Handled_Statement_Sequence (S))),
4668 end Has_Excluded_Statement;
4670 -------------------------------
4671 -- Has_Pending_Instantiation --
4672 -------------------------------
4674 function Has_Pending_Instantiation return Boolean is
4679 while Present (S) loop
4680 if Is_Compilation_Unit (S)
4681 or else Is_Child_Unit (S)
4685 elsif Ekind (S) = E_Package
4686 and then Has_Forward_Instantiation (S)
4695 end Has_Pending_Instantiation;
4697 ------------------------------------
4698 -- Returns_Compile_Time_Constant --
4699 ------------------------------------
4701 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
4703 function Check_Return (N : Node_Id) return Traverse_Result;
4709 function Check_Return (N : Node_Id) return Traverse_Result is
4711 if Nkind (N) = N_Extended_Return_Statement then
4714 elsif Nkind (N) = N_Simple_Return_Statement then
4715 if Present (Expression (N)) then
4717 Orig_Expr : constant Node_Id :=
4718 Original_Node (Expression (N));
4721 if Nkind_In (Orig_Expr, N_Integer_Literal,
4723 N_Character_Literal)
4727 elsif Is_Entity_Name (Orig_Expr)
4728 and then Ekind (Entity (Orig_Expr)) = E_Constant
4729 and then Is_Static_Expression (Orig_Expr)
4737 -- Expression has wrong form
4743 -- Continue analyzing statements
4750 function Check_All_Returns is new Traverse_Func (Check_Return);
4752 -- Start of processing for Returns_Compile_Time_Constant
4755 return Check_All_Returns (N) = OK;
4756 end Returns_Compile_Time_Constant;
4758 --------------------------------------
4759 -- Returns_Intrinsic_Function_Call --
4760 --------------------------------------
4762 function Returns_Intrinsic_Function_Call
4763 (N : Node_Id) return Boolean
4765 function Check_Return (N : Node_Id) return Traverse_Result;
4771 function Check_Return (N : Node_Id) return Traverse_Result is
4773 if Nkind (N) = N_Extended_Return_Statement then
4776 elsif Nkind (N) = N_Simple_Return_Statement then
4777 if Present (Expression (N)) then
4779 Orig_Expr : constant Node_Id :=
4780 Original_Node (Expression (N));
4783 if Nkind (Orig_Expr) in N_Op
4784 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
4788 elsif Nkind (Orig_Expr) in N_Has_Entity
4789 and then Present (Entity (Orig_Expr))
4790 and then Ekind (Entity (Orig_Expr)) = E_Function
4791 and then Is_Inlined (Entity (Orig_Expr))
4795 elsif Nkind (Orig_Expr) in N_Has_Entity
4796 and then Present (Entity (Orig_Expr))
4797 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
4806 -- Expression has wrong form
4812 -- Continue analyzing statements
4819 function Check_All_Returns is new Traverse_Func (Check_Return);
4821 -- Start of processing for Returns_Intrinsic_Function_Call
4824 return Check_All_Returns (N) = OK;
4825 end Returns_Intrinsic_Function_Call;
4827 --------------------------
4828 -- Uses_Secondary_Stack --
4829 --------------------------
4831 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
4833 function Check_Call (N : Node_Id) return Traverse_Result;
4834 -- Look for function calls that return an unconstrained type
4840 function Check_Call (N : Node_Id) return Traverse_Result is
4842 if Nkind (N) = N_Function_Call
4843 and then Is_Entity_Name (Name (N))
4844 and then Is_Composite_Type (Etype (Entity (Name (N))))
4845 and then not Is_Constrained (Etype (Entity (Name (N))))
4848 ("cannot inline & (call returns unconstrained type)?",
4857 function Check_Calls is new Traverse_Func (Check_Call);
4859 -- Start of processing for Uses_Secondary_Stack
4862 return Check_Calls (N) = Abandon;
4863 end Uses_Secondary_Stack;
4867 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4868 May_Inline : constant Boolean :=
4869 Has_Pragma_Inline_Always (Spec_Id)
4870 or else (Has_Pragma_Inline (Spec_Id)
4871 and then ((Optimization_Level > 0
4872 and then Ekind (Spec_Id)
4874 or else Front_End_Inlining));
4875 Body_To_Analyze : Node_Id;
4877 -- Start of processing for Check_Body_To_Inline
4880 -- No action needed in stubs since the attribute Body_To_Inline
4883 if Nkind (Decl) = N_Subprogram_Body_Stub then
4886 -- Cannot build the body to inline if the attribute is already set.
4887 -- This attribute may have been set if this is a subprogram renaming
4888 -- declarations (see Freeze.Build_Renamed_Body).
4890 elsif Present (Body_To_Inline (Decl)) then
4893 -- No action needed if the subprogram does not fulfill the minimum
4894 -- conditions to be inlined by the frontend
4896 elsif not May_Inline then
4900 -- Check excluded declarations
4902 if Present (Declarations (N))
4903 and then Has_Excluded_Declaration (Declarations (N))
4908 -- Check excluded statements
4910 if Present (Handled_Statement_Sequence (N)) then
4912 (Exception_Handlers (Handled_Statement_Sequence (N)))
4915 ("cannot inline& (exception handler)?",
4917 (Exception_Handlers (Handled_Statement_Sequence (N))),
4922 elsif Has_Excluded_Statement
4923 (Statements (Handled_Statement_Sequence (N)))
4929 -- For backward compatibility, compiling under -gnatN we do not
4930 -- inline a subprogram that is too large, unless it is marked
4931 -- Inline_Always. This pragma does not suppress the other checks
4932 -- on inlining (forbidden declarations, handlers, etc).
4934 if Front_End_Inlining
4935 and then not Has_Pragma_Inline_Always (Subp)
4936 and then Stat_Count > Max_Size
4938 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4942 -- If some enclosing body contains instantiations that appear before
4943 -- the corresponding generic body, the enclosing body has a freeze
4944 -- node so that it can be elaborated after the generic itself. This
4945 -- might conflict with subsequent inlinings, so that it is unsafe to
4946 -- try to inline in such a case.
4948 if Has_Pending_Instantiation then
4950 ("cannot inline& (forward instance within enclosing body)?",
4956 -- Generate and preanalyze the body to inline (needed to perform
4957 -- the rest of the checks)
4959 Generate_Body_To_Inline (N, Body_To_Analyze);
4961 if Ekind (Subp) = E_Function then
4962 Set_Result_Definition (Specification (Body_To_Analyze),
4963 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4966 -- Nest the body to analyze within the real one
4968 if No (Declarations (N)) then
4969 Set_Declarations (N, New_List (Body_To_Analyze));
4971 Append_To (Declarations (N), Body_To_Analyze);
4974 Preanalyze (Body_To_Analyze);
4975 Remove (Body_To_Analyze);
4977 -- Keep separate checks needed when compiling without optimizations
4979 if Optimization_Level = 0
4981 -- AAMP and VM targets have no support for inlining in the backend
4982 -- and hence we use frontend inlining at all optimization levels.
4984 or else AAMP_On_Target
4985 or else VM_Target /= No_VM
4987 -- Cannot inline functions whose body has a call that returns an
4988 -- unconstrained type since the secondary stack is involved, and
4989 -- it is not worth inlining.
4991 if Uses_Secondary_Stack (Body_To_Analyze) then
4994 -- Cannot inline functions that return controlled types since
4995 -- controlled actions interfere in complex ways with inlining.
4997 elsif Ekind (Subp) = E_Function
4998 and then Needs_Finalization (Etype (Subp))
5001 ("cannot inline & (controlled return type)?", N, Subp);
5004 elsif Returns_Unconstrained_Type (Subp) then
5006 ("cannot inline & (unconstrained return type)?", N, Subp);
5010 -- Compiling with optimizations enabled
5013 -- Procedures are never frontend inlined in this case!
5015 if Ekind (Subp) /= E_Function then
5018 -- Functions returning unconstrained types are tested
5019 -- separately (see Can_Split_Unconstrained_Function).
5021 elsif Returns_Unconstrained_Type (Subp) then
5024 -- Check supported cases
5026 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5027 and then Convention (Subp) /= Convention_Intrinsic
5028 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5035 end Check_Body_To_Inline;
5037 --------------------------------------
5038 -- Can_Split_Unconstrained_Function --
5039 --------------------------------------
5041 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5043 Ret_Node : constant Node_Id :=
5044 First (Statements (Handled_Statement_Sequence (N)));
5048 -- No user defined declarations allowed in the function except inside
5049 -- the unique return statement; implicit labels are the only allowed
5052 if not Is_Empty_List (Declarations (N)) then
5053 D := First (Declarations (N));
5054 while Present (D) loop
5055 if Nkind (D) /= N_Implicit_Label_Declaration then
5063 -- We only split the inlined function when we are generating the code
5064 -- of its body; otherwise we leave duplicated split subprograms in
5065 -- the tree which (if referenced) generate wrong references at link
5068 return In_Extended_Main_Code_Unit (N)
5069 and then Present (Ret_Node)
5070 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5071 and then No (Next (Ret_Node))
5072 and then Present (Handled_Statement_Sequence (Ret_Node));
5073 end Can_Split_Unconstrained_Function;
5075 -----------------------------
5076 -- Generate_Body_To_Inline --
5077 -----------------------------
5079 procedure Generate_Body_To_Inline
5081 Body_To_Inline : out Node_Id)
5083 procedure Remove_Pragmas (N : Node_Id);
5084 -- Remove occurrences of pragmas that may reference the formals of
5085 -- N. The analysis of the non-inlined body will handle these pragmas
5088 --------------------
5089 -- Remove_Pragmas --
5090 --------------------
5092 procedure Remove_Pragmas (N : Node_Id) is
5097 Decl := First (Declarations (N));
5098 while Present (Decl) loop
5101 if Nkind (Decl) = N_Pragma
5102 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5112 -- Start of processing for Generate_Body_To_Inline
5115 -- Within an instance, the body to inline must be treated as a nested
5116 -- generic, so that the proper global references are preserved.
5118 -- Note that we do not do this at the library level, because it
5119 -- is not needed, and furthermore this causes trouble if front
5120 -- end inlining is activated (-gnatN).
5123 and then Scope (Current_Scope) /= Standard_Standard
5125 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5127 Body_To_Inline := Copy_Separate_Tree (N);
5130 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5131 -- parameter has no meaning when the body is inlined and the formals
5132 -- are rewritten. Remove it from body to inline. The analysis of the
5133 -- non-inlined body will handle the pragma properly.
5135 Remove_Pragmas (Body_To_Inline);
5137 -- We need to capture references to the formals in order
5138 -- to substitute the actuals at the point of inlining, i.e.
5139 -- instantiation. To treat the formals as globals to the body to
5140 -- inline, we nest it within a dummy parameterless subprogram,
5141 -- declared within the real one.
5143 Set_Parameter_Specifications
5144 (Specification (Body_To_Inline), No_List);
5146 -- A new internal name is associated with Body_To_Inline to avoid
5147 -- conflicts when the non-inlined body N is analyzed.
5149 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5150 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5151 Set_Corresponding_Spec (Body_To_Inline, Empty);
5152 end Generate_Body_To_Inline;
5154 ----------------------------------
5155 -- Split_Unconstrained_Function --
5156 ----------------------------------
5158 procedure Split_Unconstrained_Function
5160 Spec_Id : Entity_Id)
5162 Loc : constant Source_Ptr := Sloc (N);
5163 Ret_Node : constant Node_Id :=
5164 First (Statements (Handled_Statement_Sequence (N)));
5165 Ret_Obj : constant Node_Id :=
5166 First (Return_Object_Declarations (Ret_Node));
5168 procedure Build_Procedure
5169 (Proc_Id : out Entity_Id;
5170 Decl_List : out List_Id);
5171 -- Build a procedure containing the statements found in the extended
5172 -- return statement of the unconstrained function body N.
5174 procedure Build_Procedure
5175 (Proc_Id : out Entity_Id;
5176 Decl_List : out List_Id)
5179 Formal_List : constant List_Id := New_List;
5180 Proc_Spec : Node_Id;
5181 Proc_Body : Node_Id;
5182 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5183 Body_Decl_List : List_Id := No_List;
5184 Param_Type : Node_Id;
5187 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5188 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5191 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5194 Append_To (Formal_List,
5195 Make_Parameter_Specification (Loc,
5196 Defining_Identifier =>
5197 Make_Defining_Identifier (Loc,
5198 Chars => Chars (Defining_Identifier (Ret_Obj))),
5199 In_Present => False,
5200 Out_Present => True,
5201 Null_Exclusion_Present => False,
5202 Parameter_Type => Param_Type));
5204 Formal := First_Formal (Spec_Id);
5205 while Present (Formal) loop
5206 Append_To (Formal_List,
5207 Make_Parameter_Specification (Loc,
5208 Defining_Identifier =>
5209 Make_Defining_Identifier (Sloc (Formal),
5210 Chars => Chars (Formal)),
5211 In_Present => In_Present (Parent (Formal)),
5212 Out_Present => Out_Present (Parent (Formal)),
5213 Null_Exclusion_Present =>
5214 Null_Exclusion_Present (Parent (Formal)),
5216 New_Reference_To (Etype (Formal), Loc),
5218 Copy_Separate_Tree (Expression (Parent (Formal)))));
5220 Next_Formal (Formal);
5224 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5227 Make_Procedure_Specification (Loc,
5228 Defining_Unit_Name => Proc_Id,
5229 Parameter_Specifications => Formal_List);
5231 Decl_List := New_List;
5233 Append_To (Decl_List,
5234 Make_Subprogram_Declaration (Loc, Proc_Spec));
5236 -- Can_Convert_Unconstrained_Function checked that the function
5237 -- has no local declarations except implicit label declarations.
5238 -- Copy these declarations to the built procedure.
5240 if Present (Declarations (N)) then
5241 Body_Decl_List := New_List;
5248 D := First (Declarations (N));
5249 while Present (D) loop
5250 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5253 Make_Implicit_Label_Declaration (Loc,
5254 Make_Defining_Identifier (Loc,
5255 Chars => Chars (Defining_Identifier (D))),
5256 Label_Construct => Empty);
5257 Append_To (Body_Decl_List, New_D);
5264 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5267 Make_Subprogram_Body (Loc,
5268 Specification => Copy_Separate_Tree (Proc_Spec),
5269 Declarations => Body_Decl_List,
5270 Handled_Statement_Sequence =>
5271 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5273 Set_Defining_Unit_Name (Specification (Proc_Body),
5274 Make_Defining_Identifier (Loc, Subp_Name));
5276 Append_To (Decl_List, Proc_Body);
5277 end Build_Procedure;
5281 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5283 Proc_Id : Entity_Id;
5284 Proc_Call : Node_Id;
5286 -- Start of processing for Split_Unconstrained_Function
5289 -- Build the associated procedure, analyze it and insert it before
5290 -- the function body N
5293 Scope : constant Entity_Id := Current_Scope;
5294 Decl_List : List_Id;
5297 Build_Procedure (Proc_Id, Decl_List);
5298 Insert_Actions (N, Decl_List);
5302 -- Build the call to the generated procedure
5305 Actual_List : constant List_Id := New_List;
5309 Append_To (Actual_List,
5310 New_Reference_To (Defining_Identifier (New_Obj), Loc));
5312 Formal := First_Formal (Spec_Id);
5313 while Present (Formal) loop
5314 Append_To (Actual_List, New_Reference_To (Formal, Loc));
5316 -- Avoid spurious warning on unreferenced formals
5318 Set_Referenced (Formal);
5319 Next_Formal (Formal);
5323 Make_Procedure_Call_Statement (Loc,
5324 Name => New_Reference_To (Proc_Id, Loc),
5325 Parameter_Associations => Actual_List);
5333 -- main_1__F1b (New_Obj, ...);
5338 Make_Block_Statement (Loc,
5339 Declarations => New_List (New_Obj),
5340 Handled_Statement_Sequence =>
5341 Make_Handled_Sequence_Of_Statements (Loc,
5342 Statements => New_List (
5346 Make_Simple_Return_Statement (Loc,
5349 (Defining_Identifier (New_Obj), Loc)))));
5351 Rewrite (Ret_Node, Blk_Stmt);
5352 end Split_Unconstrained_Function;
5354 -- Start of processing for Check_And_Build_Body_To_Inline
5357 -- Do not inline any subprogram that contains nested subprograms, since
5358 -- the backend inlining circuit seems to generate uninitialized
5359 -- references in this case. We know this happens in the case of front
5360 -- end ZCX support, but it also appears it can happen in other cases as
5361 -- well. The backend often rejects attempts to inline in the case of
5362 -- nested procedures anyway, so little if anything is lost by this.
5363 -- Note that this is test is for the benefit of the back-end. There is
5364 -- a separate test for front-end inlining that also rejects nested
5367 -- Do not do this test if errors have been detected, because in some
5368 -- error cases, this code blows up, and we don't need it anyway if
5369 -- there have been errors, since we won't get to the linker anyway.
5371 if Comes_From_Source (Body_Id)
5372 and then (Has_Pragma_Inline_Always (Spec_Id)
5373 or else Optimization_Level > 0)
5374 and then Serious_Errors_Detected = 0
5382 P_Ent := Scope (P_Ent);
5383 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5385 if Is_Subprogram (P_Ent) then
5386 Set_Is_Inlined (P_Ent, False);
5388 if Comes_From_Source (P_Ent)
5389 and then Has_Pragma_Inline (P_Ent)
5392 ("cannot inline& (nested subprogram)?", N, P_Ent,
5393 Is_Serious => True);
5400 -- Build the body to inline only if really needed!
5402 if Check_Body_To_Inline (N, Spec_Id)
5403 and then Serious_Errors_Detected = 0
5405 if Returns_Unconstrained_Type (Spec_Id) then
5406 if Can_Split_Unconstrained_Function (N) then
5407 Split_Unconstrained_Function (N, Spec_Id);
5408 Build_Body_To_Inline (N, Spec_Id);
5409 Set_Is_Inlined (Spec_Id);
5412 Build_Body_To_Inline (N, Spec_Id);
5413 Set_Is_Inlined (Spec_Id);
5416 end Check_And_Build_Body_To_Inline;
5418 -----------------------
5419 -- Check_Conformance --
5420 -----------------------
5422 procedure Check_Conformance
5423 (New_Id : Entity_Id;
5425 Ctype : Conformance_Type;
5427 Conforms : out Boolean;
5428 Err_Loc : Node_Id := Empty;
5429 Get_Inst : Boolean := False;
5430 Skip_Controlling_Formals : Boolean := False)
5432 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5433 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5434 -- If Errmsg is True, then processing continues to post an error message
5435 -- for conformance error on given node. Two messages are output. The
5436 -- first message points to the previous declaration with a general "no
5437 -- conformance" message. The second is the detailed reason, supplied as
5438 -- Msg. The parameter N provide information for a possible & insertion
5439 -- in the message, and also provides the location for posting the
5440 -- message in the absence of a specified Err_Loc location.
5442 -----------------------
5443 -- Conformance_Error --
5444 -----------------------
5446 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5453 if No (Err_Loc) then
5459 Error_Msg_Sloc := Sloc (Old_Id);
5462 when Type_Conformant =>
5463 Error_Msg_N -- CODEFIX
5464 ("not type conformant with declaration#!", Enode);
5466 when Mode_Conformant =>
5467 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5469 ("not mode conformant with operation inherited#!",
5473 ("not mode conformant with declaration#!", Enode);
5476 when Subtype_Conformant =>
5477 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5479 ("not subtype conformant with operation inherited#!",
5483 ("not subtype conformant with declaration#!", Enode);
5486 when Fully_Conformant =>
5487 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5488 Error_Msg_N -- CODEFIX
5489 ("not fully conformant with operation inherited#!",
5492 Error_Msg_N -- CODEFIX
5493 ("not fully conformant with declaration#!", Enode);
5497 Error_Msg_NE (Msg, Enode, N);
5499 end Conformance_Error;
5503 Old_Type : constant Entity_Id := Etype (Old_Id);
5504 New_Type : constant Entity_Id := Etype (New_Id);
5505 Old_Formal : Entity_Id;
5506 New_Formal : Entity_Id;
5507 Access_Types_Match : Boolean;
5508 Old_Formal_Base : Entity_Id;
5509 New_Formal_Base : Entity_Id;
5511 -- Start of processing for Check_Conformance
5516 -- We need a special case for operators, since they don't appear
5519 if Ctype = Type_Conformant then
5520 if Ekind (New_Id) = E_Operator
5521 and then Operator_Matches_Spec (New_Id, Old_Id)
5527 -- If both are functions/operators, check return types conform
5529 if Old_Type /= Standard_Void_Type
5530 and then New_Type /= Standard_Void_Type
5533 -- If we are checking interface conformance we omit controlling
5534 -- arguments and result, because we are only checking the conformance
5535 -- of the remaining parameters.
5537 if Has_Controlling_Result (Old_Id)
5538 and then Has_Controlling_Result (New_Id)
5539 and then Skip_Controlling_Formals
5543 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5544 Conformance_Error ("\return type does not match!", New_Id);
5548 -- Ada 2005 (AI-231): In case of anonymous access types check the
5549 -- null-exclusion and access-to-constant attributes match.
5551 if Ada_Version >= Ada_2005
5552 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5554 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5555 or else Is_Access_Constant (Etype (Old_Type)) /=
5556 Is_Access_Constant (Etype (New_Type)))
5558 Conformance_Error ("\return type does not match!", New_Id);
5562 -- If either is a function/operator and the other isn't, error
5564 elsif Old_Type /= Standard_Void_Type
5565 or else New_Type /= Standard_Void_Type
5567 Conformance_Error ("\functions can only match functions!", New_Id);
5571 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5572 -- If this is a renaming as body, refine error message to indicate that
5573 -- the conflict is with the original declaration. If the entity is not
5574 -- frozen, the conventions don't have to match, the one of the renamed
5575 -- entity is inherited.
5577 if Ctype >= Subtype_Conformant then
5578 if Convention (Old_Id) /= Convention (New_Id) then
5579 if not Is_Frozen (New_Id) then
5582 elsif Present (Err_Loc)
5583 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5584 and then Present (Corresponding_Spec (Err_Loc))
5586 Error_Msg_Name_1 := Chars (New_Id);
5588 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5589 Conformance_Error ("\prior declaration for% has convention %!");
5592 Conformance_Error ("\calling conventions do not match!");
5597 elsif Is_Formal_Subprogram (Old_Id)
5598 or else Is_Formal_Subprogram (New_Id)
5600 Conformance_Error ("\formal subprograms not allowed!");
5605 -- Deal with parameters
5607 -- Note: we use the entity information, rather than going directly
5608 -- to the specification in the tree. This is not only simpler, but
5609 -- absolutely necessary for some cases of conformance tests between
5610 -- operators, where the declaration tree simply does not exist!
5612 Old_Formal := First_Formal (Old_Id);
5613 New_Formal := First_Formal (New_Id);
5614 while Present (Old_Formal) and then Present (New_Formal) loop
5615 if Is_Controlling_Formal (Old_Formal)
5616 and then Is_Controlling_Formal (New_Formal)
5617 and then Skip_Controlling_Formals
5619 -- The controlling formals will have different types when
5620 -- comparing an interface operation with its match, but both
5621 -- or neither must be access parameters.
5623 if Is_Access_Type (Etype (Old_Formal))
5625 Is_Access_Type (Etype (New_Formal))
5627 goto Skip_Controlling_Formal;
5630 ("\access parameter does not match!", New_Formal);
5634 -- Ada 2012: Mode conformance also requires that formal parameters
5635 -- be both aliased, or neither.
5637 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5638 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5640 ("\aliased parameter mismatch!", New_Formal);
5644 if Ctype = Fully_Conformant then
5646 -- Names must match. Error message is more accurate if we do
5647 -- this before checking that the types of the formals match.
5649 if Chars (Old_Formal) /= Chars (New_Formal) then
5650 Conformance_Error ("\name & does not match!", New_Formal);
5652 -- Set error posted flag on new formal as well to stop
5653 -- junk cascaded messages in some cases.
5655 Set_Error_Posted (New_Formal);
5659 -- Null exclusion must match
5661 if Null_Exclusion_Present (Parent (Old_Formal))
5663 Null_Exclusion_Present (Parent (New_Formal))
5665 -- Only give error if both come from source. This should be
5666 -- investigated some time, since it should not be needed ???
5668 if Comes_From_Source (Old_Formal)
5670 Comes_From_Source (New_Formal)
5673 ("\null exclusion for & does not match", New_Formal);
5675 -- Mark error posted on the new formal to avoid duplicated
5676 -- complaint about types not matching.
5678 Set_Error_Posted (New_Formal);
5683 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5684 -- case occurs whenever a subprogram is being renamed and one of its
5685 -- parameters imposes a null exclusion. For example:
5687 -- type T is null record;
5688 -- type Acc_T is access T;
5689 -- subtype Acc_T_Sub is Acc_T;
5691 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5692 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5695 Old_Formal_Base := Etype (Old_Formal);
5696 New_Formal_Base := Etype (New_Formal);
5699 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5700 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5703 Access_Types_Match := Ada_Version >= Ada_2005
5705 -- Ensure that this rule is only applied when New_Id is a
5706 -- renaming of Old_Id.
5708 and then Nkind (Parent (Parent (New_Id))) =
5709 N_Subprogram_Renaming_Declaration
5710 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5711 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5712 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5714 -- Now handle the allowed access-type case
5716 and then Is_Access_Type (Old_Formal_Base)
5717 and then Is_Access_Type (New_Formal_Base)
5719 -- The type kinds must match. The only exception occurs with
5720 -- multiple generics of the form:
5723 -- type F is private; type A is private;
5724 -- type F_Ptr is access F; type A_Ptr is access A;
5725 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5726 -- package F_Pack is ... package A_Pack is
5727 -- package F_Inst is
5728 -- new F_Pack (A, A_Ptr, A_P);
5730 -- When checking for conformance between the parameters of A_P
5731 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5732 -- because the compiler has transformed A_Ptr into a subtype of
5733 -- F_Ptr. We catch this case in the code below.
5735 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5737 (Is_Generic_Type (Old_Formal_Base)
5738 and then Is_Generic_Type (New_Formal_Base)
5739 and then Is_Internal (New_Formal_Base)
5740 and then Etype (Etype (New_Formal_Base)) =
5742 and then Directly_Designated_Type (Old_Formal_Base) =
5743 Directly_Designated_Type (New_Formal_Base)
5744 and then ((Is_Itype (Old_Formal_Base)
5745 and then Can_Never_Be_Null (Old_Formal_Base))
5747 (Is_Itype (New_Formal_Base)
5748 and then Can_Never_Be_Null (New_Formal_Base)));
5750 -- Types must always match. In the visible part of an instance,
5751 -- usual overloading rules for dispatching operations apply, and
5752 -- we check base types (not the actual subtypes).
5754 if In_Instance_Visible_Part
5755 and then Is_Dispatching_Operation (New_Id)
5757 if not Conforming_Types
5758 (T1 => Base_Type (Etype (Old_Formal)),
5759 T2 => Base_Type (Etype (New_Formal)),
5761 Get_Inst => Get_Inst)
5762 and then not Access_Types_Match
5764 Conformance_Error ("\type of & does not match!", New_Formal);
5768 elsif not Conforming_Types
5769 (T1 => Old_Formal_Base,
5770 T2 => New_Formal_Base,
5772 Get_Inst => Get_Inst)
5773 and then not Access_Types_Match
5775 -- Don't give error message if old type is Any_Type. This test
5776 -- avoids some cascaded errors, e.g. in case of a bad spec.
5778 if Errmsg and then Old_Formal_Base = Any_Type then
5781 Conformance_Error ("\type of & does not match!", New_Formal);
5787 -- For mode conformance, mode must match
5789 if Ctype >= Mode_Conformant then
5790 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5791 if not Ekind_In (New_Id, E_Function, E_Procedure)
5792 or else not Is_Primitive_Wrapper (New_Id)
5794 Conformance_Error ("\mode of & does not match!", New_Formal);
5798 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5800 if Is_Protected_Type
5801 (Corresponding_Concurrent_Type (T))
5803 Error_Msg_PT (T, New_Id);
5806 ("\mode of & does not match!", New_Formal);
5813 -- Part of mode conformance for access types is having the same
5814 -- constant modifier.
5816 elsif Access_Types_Match
5817 and then Is_Access_Constant (Old_Formal_Base) /=
5818 Is_Access_Constant (New_Formal_Base)
5821 ("\constant modifier does not match!", New_Formal);
5826 if Ctype >= Subtype_Conformant then
5828 -- Ada 2005 (AI-231): In case of anonymous access types check
5829 -- the null-exclusion and access-to-constant attributes must
5830 -- match. For null exclusion, we test the types rather than the
5831 -- formals themselves, since the attribute is only set reliably
5832 -- on the formals in the Ada 95 case, and we exclude the case
5833 -- where Old_Formal is marked as controlling, to avoid errors
5834 -- when matching completing bodies with dispatching declarations
5835 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5837 if Ada_Version >= Ada_2005
5838 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5839 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5841 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5842 Can_Never_Be_Null (Etype (New_Formal))
5844 not Is_Controlling_Formal (Old_Formal))
5846 Is_Access_Constant (Etype (Old_Formal)) /=
5847 Is_Access_Constant (Etype (New_Formal)))
5849 -- Do not complain if error already posted on New_Formal. This
5850 -- avoids some redundant error messages.
5852 and then not Error_Posted (New_Formal)
5854 -- It is allowed to omit the null-exclusion in case of stream
5855 -- attribute subprograms. We recognize stream subprograms
5856 -- through their TSS-generated suffix.
5859 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5862 if TSS_Name /= TSS_Stream_Read
5863 and then TSS_Name /= TSS_Stream_Write
5864 and then TSS_Name /= TSS_Stream_Input
5865 and then TSS_Name /= TSS_Stream_Output
5867 -- Here we have a definite conformance error. It is worth
5868 -- special casing the error message for the case of a
5869 -- controlling formal (which excludes null).
5871 if Is_Controlling_Formal (New_Formal) then
5872 Error_Msg_Node_2 := Scope (New_Formal);
5874 ("\controlling formal& of& excludes null, "
5875 & "declaration must exclude null as well",
5878 -- Normal case (couldn't we give more detail here???)
5882 ("\type of & does not match!", New_Formal);
5891 -- Full conformance checks
5893 if Ctype = Fully_Conformant then
5895 -- We have checked already that names match
5897 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5899 -- Check default expressions for in parameters
5902 NewD : constant Boolean :=
5903 Present (Default_Value (New_Formal));
5904 OldD : constant Boolean :=
5905 Present (Default_Value (Old_Formal));
5907 if NewD or OldD then
5909 -- The old default value has been analyzed because the
5910 -- current full declaration will have frozen everything
5911 -- before. The new default value has not been analyzed,
5912 -- so analyze it now before we check for conformance.
5915 Push_Scope (New_Id);
5916 Preanalyze_Spec_Expression
5917 (Default_Value (New_Formal), Etype (New_Formal));
5921 if not (NewD and OldD)
5922 or else not Fully_Conformant_Expressions
5923 (Default_Value (Old_Formal),
5924 Default_Value (New_Formal))
5927 ("\default expression for & does not match!",
5936 -- A couple of special checks for Ada 83 mode. These checks are
5937 -- skipped if either entity is an operator in package Standard,
5938 -- or if either old or new instance is not from the source program.
5940 if Ada_Version = Ada_83
5941 and then Sloc (Old_Id) > Standard_Location
5942 and then Sloc (New_Id) > Standard_Location
5943 and then Comes_From_Source (Old_Id)
5944 and then Comes_From_Source (New_Id)
5947 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5948 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5951 -- Explicit IN must be present or absent in both cases. This
5952 -- test is required only in the full conformance case.
5954 if In_Present (Old_Param) /= In_Present (New_Param)
5955 and then Ctype = Fully_Conformant
5958 ("\(Ada 83) IN must appear in both declarations",
5963 -- Grouping (use of comma in param lists) must be the same
5964 -- This is where we catch a misconformance like:
5967 -- A : Integer; B : Integer
5969 -- which are represented identically in the tree except
5970 -- for the setting of the flags More_Ids and Prev_Ids.
5972 if More_Ids (Old_Param) /= More_Ids (New_Param)
5973 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5976 ("\grouping of & does not match!", New_Formal);
5982 -- This label is required when skipping controlling formals
5984 <<Skip_Controlling_Formal>>
5986 Next_Formal (Old_Formal);
5987 Next_Formal (New_Formal);
5990 if Present (Old_Formal) then
5991 Conformance_Error ("\too few parameters!");
5994 elsif Present (New_Formal) then
5995 Conformance_Error ("\too many parameters!", New_Formal);
5998 end Check_Conformance;
6000 -----------------------
6001 -- Check_Conventions --
6002 -----------------------
6004 procedure Check_Conventions (Typ : Entity_Id) is
6005 Ifaces_List : Elist_Id;
6007 procedure Check_Convention (Op : Entity_Id);
6008 -- Verify that the convention of inherited dispatching operation Op is
6009 -- consistent among all subprograms it overrides. In order to minimize
6010 -- the search, Search_From is utilized to designate a specific point in
6011 -- the list rather than iterating over the whole list once more.
6013 ----------------------
6014 -- Check_Convention --
6015 ----------------------
6017 procedure Check_Convention (Op : Entity_Id) is
6018 Iface_Elmt : Elmt_Id;
6019 Iface_Prim_Elmt : Elmt_Id;
6020 Iface_Prim : Entity_Id;
6023 Iface_Elmt := First_Elmt (Ifaces_List);
6024 while Present (Iface_Elmt) loop
6026 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6027 while Present (Iface_Prim_Elmt) loop
6028 Iface_Prim := Node (Iface_Prim_Elmt);
6030 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6031 and then Convention (Iface_Prim) /= Convention (Op)
6034 ("inconsistent conventions in primitive operations", Typ);
6036 Error_Msg_Name_1 := Chars (Op);
6037 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
6038 Error_Msg_Sloc := Sloc (Op);
6040 if Comes_From_Source (Op) or else No (Alias (Op)) then
6041 if not Present (Overridden_Operation (Op)) then
6042 Error_Msg_N ("\\primitive % defined #", Typ);
6045 ("\\overriding operation % with " &
6046 "convention % defined #", Typ);
6049 else pragma Assert (Present (Alias (Op)));
6050 Error_Msg_Sloc := Sloc (Alias (Op));
6052 ("\\inherited operation % with " &
6053 "convention % defined #", Typ);
6056 Error_Msg_Name_1 := Chars (Op);
6058 Get_Convention_Name (Convention (Iface_Prim));
6059 Error_Msg_Sloc := Sloc (Iface_Prim);
6061 ("\\overridden operation % with " &
6062 "convention % defined #", Typ);
6064 -- Avoid cascading errors
6069 Next_Elmt (Iface_Prim_Elmt);
6072 Next_Elmt (Iface_Elmt);
6074 end Check_Convention;
6078 Prim_Op : Entity_Id;
6079 Prim_Op_Elmt : Elmt_Id;
6081 -- Start of processing for Check_Conventions
6084 if not Has_Interfaces (Typ) then
6088 Collect_Interfaces (Typ, Ifaces_List);
6090 -- The algorithm checks every overriding dispatching operation against
6091 -- all the corresponding overridden dispatching operations, detecting
6092 -- differences in conventions.
6094 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6095 while Present (Prim_Op_Elmt) loop
6096 Prim_Op := Node (Prim_Op_Elmt);
6098 -- A small optimization: skip the predefined dispatching operations
6099 -- since they always have the same convention.
6101 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6102 Check_Convention (Prim_Op);
6105 Next_Elmt (Prim_Op_Elmt);
6107 end Check_Conventions;
6109 ------------------------------
6110 -- Check_Delayed_Subprogram --
6111 ------------------------------
6113 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6116 procedure Possible_Freeze (T : Entity_Id);
6117 -- T is the type of either a formal parameter or of the return type.
6118 -- If T is not yet frozen and needs a delayed freeze, then the
6119 -- subprogram itself must be delayed. If T is the limited view of an
6120 -- incomplete type the subprogram must be frozen as well, because
6121 -- T may depend on local types that have not been frozen yet.
6123 ---------------------
6124 -- Possible_Freeze --
6125 ---------------------
6127 procedure Possible_Freeze (T : Entity_Id) is
6129 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6130 Set_Has_Delayed_Freeze (Designator);
6132 elsif Is_Access_Type (T)
6133 and then Has_Delayed_Freeze (Designated_Type (T))
6134 and then not Is_Frozen (Designated_Type (T))
6136 Set_Has_Delayed_Freeze (Designator);
6138 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
6139 Set_Has_Delayed_Freeze (Designator);
6141 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6142 -- of a subprogram or entry declaration.
6144 elsif Ekind (T) = E_Incomplete_Type
6145 and then Ada_Version >= Ada_2012
6147 Set_Has_Delayed_Freeze (Designator);
6150 end Possible_Freeze;
6152 -- Start of processing for Check_Delayed_Subprogram
6155 -- All subprograms, including abstract subprograms, may need a freeze
6156 -- node if some formal type or the return type needs one.
6158 Possible_Freeze (Etype (Designator));
6159 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6161 -- Need delayed freeze if any of the formal types themselves need
6162 -- a delayed freeze and are not yet frozen.
6164 F := First_Formal (Designator);
6165 while Present (F) loop
6166 Possible_Freeze (Etype (F));
6167 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6171 -- Mark functions that return by reference. Note that it cannot be
6172 -- done for delayed_freeze subprograms because the underlying
6173 -- returned type may not be known yet (for private types)
6175 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6177 Typ : constant Entity_Id := Etype (Designator);
6178 Utyp : constant Entity_Id := Underlying_Type (Typ);
6180 if Is_Immutably_Limited_Type (Typ) then
6181 Set_Returns_By_Ref (Designator);
6182 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6183 Set_Returns_By_Ref (Designator);
6187 end Check_Delayed_Subprogram;
6189 ------------------------------------
6190 -- Check_Discriminant_Conformance --
6191 ------------------------------------
6193 procedure Check_Discriminant_Conformance
6198 Old_Discr : Entity_Id := First_Discriminant (Prev);
6199 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6200 New_Discr_Id : Entity_Id;
6201 New_Discr_Type : Entity_Id;
6203 procedure Conformance_Error (Msg : String; N : Node_Id);
6204 -- Post error message for conformance error on given node. Two messages
6205 -- are output. The first points to the previous declaration with a
6206 -- general "no conformance" message. The second is the detailed reason,
6207 -- supplied as Msg. The parameter N provide information for a possible
6208 -- & insertion in the message.
6210 -----------------------
6211 -- Conformance_Error --
6212 -----------------------
6214 procedure Conformance_Error (Msg : String; N : Node_Id) is
6216 Error_Msg_Sloc := Sloc (Prev_Loc);
6217 Error_Msg_N -- CODEFIX
6218 ("not fully conformant with declaration#!", N);
6219 Error_Msg_NE (Msg, N, N);
6220 end Conformance_Error;
6222 -- Start of processing for Check_Discriminant_Conformance
6225 while Present (Old_Discr) and then Present (New_Discr) loop
6226 New_Discr_Id := Defining_Identifier (New_Discr);
6228 -- The subtype mark of the discriminant on the full type has not
6229 -- been analyzed so we do it here. For an access discriminant a new
6232 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6234 Access_Definition (N, Discriminant_Type (New_Discr));
6237 Analyze (Discriminant_Type (New_Discr));
6238 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6240 -- Ada 2005: if the discriminant definition carries a null
6241 -- exclusion, create an itype to check properly for consistency
6242 -- with partial declaration.
6244 if Is_Access_Type (New_Discr_Type)
6245 and then Null_Exclusion_Present (New_Discr)
6248 Create_Null_Excluding_Itype
6249 (T => New_Discr_Type,
6250 Related_Nod => New_Discr,
6251 Scope_Id => Current_Scope);
6255 if not Conforming_Types
6256 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6258 Conformance_Error ("type of & does not match!", New_Discr_Id);
6261 -- Treat the new discriminant as an occurrence of the old one,
6262 -- for navigation purposes, and fill in some semantic
6263 -- information, for completeness.
6265 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6266 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6267 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6272 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6273 Conformance_Error ("name & does not match!", New_Discr_Id);
6277 -- Default expressions must match
6280 NewD : constant Boolean :=
6281 Present (Expression (New_Discr));
6282 OldD : constant Boolean :=
6283 Present (Expression (Parent (Old_Discr)));
6286 if NewD or OldD then
6288 -- The old default value has been analyzed and expanded,
6289 -- because the current full declaration will have frozen
6290 -- everything before. The new default values have not been
6291 -- expanded, so expand now to check conformance.
6294 Preanalyze_Spec_Expression
6295 (Expression (New_Discr), New_Discr_Type);
6298 if not (NewD and OldD)
6299 or else not Fully_Conformant_Expressions
6300 (Expression (Parent (Old_Discr)),
6301 Expression (New_Discr))
6305 ("default expression for & does not match!",
6312 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6314 if Ada_Version = Ada_83 then
6316 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6319 -- Grouping (use of comma in param lists) must be the same
6320 -- This is where we catch a misconformance like:
6323 -- A : Integer; B : Integer
6325 -- which are represented identically in the tree except
6326 -- for the setting of the flags More_Ids and Prev_Ids.
6328 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6329 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6332 ("grouping of & does not match!", New_Discr_Id);
6338 Next_Discriminant (Old_Discr);
6342 if Present (Old_Discr) then
6343 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6346 elsif Present (New_Discr) then
6348 ("too many discriminants!", Defining_Identifier (New_Discr));
6351 end Check_Discriminant_Conformance;
6353 ----------------------------
6354 -- Check_Fully_Conformant --
6355 ----------------------------
6357 procedure Check_Fully_Conformant
6358 (New_Id : Entity_Id;
6360 Err_Loc : Node_Id := Empty)
6363 pragma Warnings (Off, Result);
6366 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6367 end Check_Fully_Conformant;
6369 ---------------------------
6370 -- Check_Mode_Conformant --
6371 ---------------------------
6373 procedure Check_Mode_Conformant
6374 (New_Id : Entity_Id;
6376 Err_Loc : Node_Id := Empty;
6377 Get_Inst : Boolean := False)
6380 pragma Warnings (Off, Result);
6383 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6384 end Check_Mode_Conformant;
6386 --------------------------------
6387 -- Check_Overriding_Indicator --
6388 --------------------------------
6390 procedure Check_Overriding_Indicator
6392 Overridden_Subp : Entity_Id;
6393 Is_Primitive : Boolean)
6399 -- No overriding indicator for literals
6401 if Ekind (Subp) = E_Enumeration_Literal then
6404 elsif Ekind (Subp) = E_Entry then
6405 Decl := Parent (Subp);
6407 -- No point in analyzing a malformed operator
6409 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6410 and then Error_Posted (Subp)
6415 Decl := Unit_Declaration_Node (Subp);
6418 if Nkind_In (Decl, N_Subprogram_Body,
6419 N_Subprogram_Body_Stub,
6420 N_Subprogram_Declaration,
6421 N_Abstract_Subprogram_Declaration,
6422 N_Subprogram_Renaming_Declaration)
6424 Spec := Specification (Decl);
6426 elsif Nkind (Decl) = N_Entry_Declaration then
6433 -- The overriding operation is type conformant with the overridden one,
6434 -- but the names of the formals are not required to match. If the names
6435 -- appear permuted in the overriding operation, this is a possible
6436 -- source of confusion that is worth diagnosing. Controlling formals
6437 -- often carry names that reflect the type, and it is not worthwhile
6438 -- requiring that their names match.
6440 if Present (Overridden_Subp)
6441 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6448 Form1 := First_Formal (Subp);
6449 Form2 := First_Formal (Overridden_Subp);
6451 -- If the overriding operation is a synchronized operation, skip
6452 -- the first parameter of the overridden operation, which is
6453 -- implicit in the new one. If the operation is declared in the
6454 -- body it is not primitive and all formals must match.
6456 if Is_Concurrent_Type (Scope (Subp))
6457 and then Is_Tagged_Type (Scope (Subp))
6458 and then not Has_Completion (Scope (Subp))
6460 Form2 := Next_Formal (Form2);
6463 if Present (Form1) then
6464 Form1 := Next_Formal (Form1);
6465 Form2 := Next_Formal (Form2);
6468 while Present (Form1) loop
6469 if not Is_Controlling_Formal (Form1)
6470 and then Present (Next_Formal (Form2))
6471 and then Chars (Form1) = Chars (Next_Formal (Form2))
6473 Error_Msg_Node_2 := Alias (Overridden_Subp);
6474 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6476 ("& does not match corresponding formal of&#",
6481 Next_Formal (Form1);
6482 Next_Formal (Form2);
6487 -- If there is an overridden subprogram, then check that there is no
6488 -- "not overriding" indicator, and mark the subprogram as overriding.
6489 -- This is not done if the overridden subprogram is marked as hidden,
6490 -- which can occur for the case of inherited controlled operations
6491 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6492 -- subprogram is not itself hidden. (Note: This condition could probably
6493 -- be simplified, leaving out the testing for the specific controlled
6494 -- cases, but it seems safer and clearer this way, and echoes similar
6495 -- special-case tests of this kind in other places.)
6497 if Present (Overridden_Subp)
6498 and then (not Is_Hidden (Overridden_Subp)
6500 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6503 and then Present (Alias (Overridden_Subp))
6504 and then not Is_Hidden (Alias (Overridden_Subp))))
6506 if Must_Not_Override (Spec) then
6507 Error_Msg_Sloc := Sloc (Overridden_Subp);
6509 if Ekind (Subp) = E_Entry then
6511 ("entry & overrides inherited operation #", Spec, Subp);
6514 ("subprogram & overrides inherited operation #", Spec, Subp);
6517 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6518 -- as an extension of Root_Controlled, and thus has a useless Adjust
6519 -- operation. This operation should not be inherited by other limited
6520 -- controlled types. An explicit Adjust for them is not overriding.
6522 elsif Must_Override (Spec)
6523 and then Chars (Overridden_Subp) = Name_Adjust
6524 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6525 and then Present (Alias (Overridden_Subp))
6527 Is_Predefined_File_Name
6528 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6530 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6532 elsif Is_Subprogram (Subp) then
6533 if Is_Init_Proc (Subp) then
6536 elsif No (Overridden_Operation (Subp)) then
6538 -- For entities generated by Derive_Subprograms the overridden
6539 -- operation is the inherited primitive (which is available
6540 -- through the attribute alias)
6542 if (Is_Dispatching_Operation (Subp)
6543 or else Is_Dispatching_Operation (Overridden_Subp))
6544 and then not Comes_From_Source (Overridden_Subp)
6545 and then Find_Dispatching_Type (Overridden_Subp) =
6546 Find_Dispatching_Type (Subp)
6547 and then Present (Alias (Overridden_Subp))
6548 and then Comes_From_Source (Alias (Overridden_Subp))
6550 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6553 Set_Overridden_Operation (Subp, Overridden_Subp);
6556 -- Ensure that a ghost function is not overriding another routine
6558 elsif Is_Ghost_Function (Subp) then
6559 Error_Msg_N ("ghost function & cannot be overriding", Subp);
6563 -- If primitive flag is set or this is a protected operation, then
6564 -- the operation is overriding at the point of its declaration, so
6565 -- warn if necessary. Otherwise it may have been declared before the
6566 -- operation it overrides and no check is required.
6569 and then not Must_Override (Spec)
6570 and then (Is_Primitive
6571 or else Ekind (Scope (Subp)) = E_Protected_Type)
6573 Style.Missing_Overriding (Decl, Subp);
6576 -- If Subp is an operator, it may override a predefined operation, if
6577 -- it is defined in the same scope as the type to which it applies.
6578 -- In that case Overridden_Subp is empty because of our implicit
6579 -- representation for predefined operators. We have to check whether the
6580 -- signature of Subp matches that of a predefined operator. Note that
6581 -- first argument provides the name of the operator, and the second
6582 -- argument the signature that may match that of a standard operation.
6583 -- If the indicator is overriding, then the operator must match a
6584 -- predefined signature, because we know already that there is no
6585 -- explicit overridden operation.
6587 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6588 if Must_Not_Override (Spec) then
6590 -- If this is not a primitive or a protected subprogram, then
6591 -- "not overriding" is illegal.
6594 and then Ekind (Scope (Subp)) /= E_Protected_Type
6597 ("overriding indicator only allowed "
6598 & "if subprogram is primitive", Subp);
6600 elsif Can_Override_Operator (Subp) then
6602 ("subprogram& overrides predefined operator ", Spec, Subp);
6605 elsif Must_Override (Spec) then
6606 if No (Overridden_Operation (Subp))
6607 and then not Can_Override_Operator (Subp)
6609 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6612 elsif not Error_Posted (Subp)
6613 and then Style_Check
6614 and then Can_Override_Operator (Subp)
6616 not Is_Predefined_File_Name
6617 (Unit_File_Name (Get_Source_Unit (Subp)))
6619 -- If style checks are enabled, indicate that the indicator is
6620 -- missing. However, at the point of declaration, the type of
6621 -- which this is a primitive operation may be private, in which
6622 -- case the indicator would be premature.
6624 if Has_Private_Declaration (Etype (Subp))
6625 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6629 Style.Missing_Overriding (Decl, Subp);
6633 elsif Must_Override (Spec) then
6634 if Ekind (Subp) = E_Entry then
6635 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6637 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6640 -- If the operation is marked "not overriding" and it's not primitive
6641 -- then an error is issued, unless this is an operation of a task or
6642 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6643 -- has been specified have already been checked above.
6645 elsif Must_Not_Override (Spec)
6646 and then not Is_Primitive
6647 and then Ekind (Subp) /= E_Entry
6648 and then Ekind (Scope (Subp)) /= E_Protected_Type
6651 ("overriding indicator only allowed if subprogram is primitive",
6655 end Check_Overriding_Indicator;
6661 -- Note: this procedure needs to know far too much about how the expander
6662 -- messes with exceptions. The use of the flag Exception_Junk and the
6663 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6664 -- works, but is not very clean. It would be better if the expansion
6665 -- routines would leave Original_Node working nicely, and we could use
6666 -- Original_Node here to ignore all the peculiar expander messing ???
6668 procedure Check_Returns
6672 Proc : Entity_Id := Empty)
6676 procedure Check_Statement_Sequence (L : List_Id);
6677 -- Internal recursive procedure to check a list of statements for proper
6678 -- termination by a return statement (or a transfer of control or a
6679 -- compound statement that is itself internally properly terminated).
6681 ------------------------------
6682 -- Check_Statement_Sequence --
6683 ------------------------------
6685 procedure Check_Statement_Sequence (L : List_Id) is
6690 Raise_Exception_Call : Boolean;
6691 -- Set True if statement sequence terminated by Raise_Exception call
6692 -- or a Reraise_Occurrence call.
6695 Raise_Exception_Call := False;
6697 -- Get last real statement
6699 Last_Stm := Last (L);
6701 -- Deal with digging out exception handler statement sequences that
6702 -- have been transformed by the local raise to goto optimization.
6703 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6704 -- optimization has occurred, we are looking at something like:
6707 -- original stmts in block
6711 -- goto L1; | omitted if No_Exception_Propagation
6716 -- goto L3; -- skip handler when exception not raised
6718 -- <<L1>> -- target label for local exception
6732 -- and what we have to do is to dig out the estmts1 and estmts2
6733 -- sequences (which were the original sequences of statements in
6734 -- the exception handlers) and check them.
6736 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6741 exit when Nkind (Stm) /= N_Block_Statement;
6742 exit when not Exception_Junk (Stm);
6745 exit when Nkind (Stm) /= N_Label;
6746 exit when not Exception_Junk (Stm);
6747 Check_Statement_Sequence
6748 (Statements (Handled_Statement_Sequence (Next (Stm))));
6753 exit when Nkind (Stm) /= N_Goto_Statement;
6754 exit when not Exception_Junk (Stm);
6758 -- Don't count pragmas
6760 while Nkind (Last_Stm) = N_Pragma
6762 -- Don't count call to SS_Release (can happen after Raise_Exception)
6765 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6767 Nkind (Name (Last_Stm)) = N_Identifier
6769 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6771 -- Don't count exception junk
6774 (Nkind_In (Last_Stm, N_Goto_Statement,
6776 N_Object_Declaration)
6777 and then Exception_Junk (Last_Stm))
6778 or else Nkind (Last_Stm) in N_Push_xxx_Label
6779 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6781 -- Inserted code, such as finalization calls, is irrelevant: we only
6782 -- need to check original source.
6784 or else Is_Rewrite_Insertion (Last_Stm)
6789 -- Here we have the "real" last statement
6791 Kind := Nkind (Last_Stm);
6793 -- Transfer of control, OK. Note that in the No_Return procedure
6794 -- case, we already diagnosed any explicit return statements, so
6795 -- we can treat them as OK in this context.
6797 if Is_Transfer (Last_Stm) then
6800 -- Check cases of explicit non-indirect procedure calls
6802 elsif Kind = N_Procedure_Call_Statement
6803 and then Is_Entity_Name (Name (Last_Stm))
6805 -- Check call to Raise_Exception procedure which is treated
6806 -- specially, as is a call to Reraise_Occurrence.
6808 -- We suppress the warning in these cases since it is likely that
6809 -- the programmer really does not expect to deal with the case
6810 -- of Null_Occurrence, and thus would find a warning about a
6811 -- missing return curious, and raising Program_Error does not
6812 -- seem such a bad behavior if this does occur.
6814 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6815 -- behavior will be to raise Constraint_Error (see AI-329).
6817 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6819 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6821 Raise_Exception_Call := True;
6823 -- For Raise_Exception call, test first argument, if it is
6824 -- an attribute reference for a 'Identity call, then we know
6825 -- that the call cannot possibly return.
6828 Arg : constant Node_Id :=
6829 Original_Node (First_Actual (Last_Stm));
6831 if Nkind (Arg) = N_Attribute_Reference
6832 and then Attribute_Name (Arg) = Name_Identity
6839 -- If statement, need to look inside if there is an else and check
6840 -- each constituent statement sequence for proper termination.
6842 elsif Kind = N_If_Statement
6843 and then Present (Else_Statements (Last_Stm))
6845 Check_Statement_Sequence (Then_Statements (Last_Stm));
6846 Check_Statement_Sequence (Else_Statements (Last_Stm));
6848 if Present (Elsif_Parts (Last_Stm)) then
6850 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6853 while Present (Elsif_Part) loop
6854 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6862 -- Case statement, check each case for proper termination
6864 elsif Kind = N_Case_Statement then
6868 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6869 while Present (Case_Alt) loop
6870 Check_Statement_Sequence (Statements (Case_Alt));
6871 Next_Non_Pragma (Case_Alt);
6877 -- Block statement, check its handled sequence of statements
6879 elsif Kind = N_Block_Statement then
6885 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6894 -- Loop statement. If there is an iteration scheme, we can definitely
6895 -- fall out of the loop. Similarly if there is an exit statement, we
6896 -- can fall out. In either case we need a following return.
6898 elsif Kind = N_Loop_Statement then
6899 if Present (Iteration_Scheme (Last_Stm))
6900 or else Has_Exit (Entity (Identifier (Last_Stm)))
6904 -- A loop with no exit statement or iteration scheme is either
6905 -- an infinite loop, or it has some other exit (raise/return).
6906 -- In either case, no warning is required.
6912 -- Timed entry call, check entry call and delay alternatives
6914 -- Note: in expanded code, the timed entry call has been converted
6915 -- to a set of expanded statements on which the check will work
6916 -- correctly in any case.
6918 elsif Kind = N_Timed_Entry_Call then
6920 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6921 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6924 -- If statement sequence of entry call alternative is missing,
6925 -- then we can definitely fall through, and we post the error
6926 -- message on the entry call alternative itself.
6928 if No (Statements (ECA)) then
6931 -- If statement sequence of delay alternative is missing, then
6932 -- we can definitely fall through, and we post the error
6933 -- message on the delay alternative itself.
6935 -- Note: if both ECA and DCA are missing the return, then we
6936 -- post only one message, should be enough to fix the bugs.
6937 -- If not we will get a message next time on the DCA when the
6940 elsif No (Statements (DCA)) then
6943 -- Else check both statement sequences
6946 Check_Statement_Sequence (Statements (ECA));
6947 Check_Statement_Sequence (Statements (DCA));
6952 -- Conditional entry call, check entry call and else part
6954 -- Note: in expanded code, the conditional entry call has been
6955 -- converted to a set of expanded statements on which the check
6956 -- will work correctly in any case.
6958 elsif Kind = N_Conditional_Entry_Call then
6960 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6963 -- If statement sequence of entry call alternative is missing,
6964 -- then we can definitely fall through, and we post the error
6965 -- message on the entry call alternative itself.
6967 if No (Statements (ECA)) then
6970 -- Else check statement sequence and else part
6973 Check_Statement_Sequence (Statements (ECA));
6974 Check_Statement_Sequence (Else_Statements (Last_Stm));
6980 -- If we fall through, issue appropriate message
6983 if not Raise_Exception_Call then
6985 ("RETURN statement missing following this statement??!",
6988 ("\Program_Error may be raised at run time??!",
6992 -- Note: we set Err even though we have not issued a warning
6993 -- because we still have a case of a missing return. This is
6994 -- an extremely marginal case, probably will never be noticed
6995 -- but we might as well get it right.
6999 -- Otherwise we have the case of a procedure marked No_Return
7002 if not Raise_Exception_Call then
7004 ("implied return after this statement " &
7005 "will raise Program_Error??",
7008 ("\procedure & is marked as No_Return??!",
7013 RE : constant Node_Id :=
7014 Make_Raise_Program_Error (Sloc (Last_Stm),
7015 Reason => PE_Implicit_Return);
7017 Insert_After (Last_Stm, RE);
7021 end Check_Statement_Sequence;
7023 -- Start of processing for Check_Returns
7027 Check_Statement_Sequence (Statements (HSS));
7029 if Present (Exception_Handlers (HSS)) then
7030 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7031 while Present (Handler) loop
7032 Check_Statement_Sequence (Statements (Handler));
7033 Next_Non_Pragma (Handler);
7038 -------------------------------
7039 -- Check_Subprogram_Contract --
7040 -------------------------------
7042 procedure Check_Subprogram_Contract (Spec_Id : Entity_Id) is
7044 -- Code is currently commented out as, in some cases, it causes crashes
7045 -- because Direct_Primitive_Operations is not available for a private
7046 -- type. This may cause more warnings to be issued than necessary. See
7047 -- below for the intended use of this variable. ???
7049 -- Inherited : constant Subprogram_List :=
7050 -- Inherited_Subprograms (Spec_Id);
7051 -- -- List of subprograms inherited by this subprogram
7053 -- We ignore postconditions "True" or "False" and contract-cases which
7054 -- have similar consequence expressions, which we call "trivial", when
7055 -- issuing warnings, since these postconditions and contract-cases
7056 -- purposedly ignore the post-state.
7058 Last_Postcondition : Node_Id := Empty;
7059 -- Last non-trivial postcondition on the subprogram, or else Empty if
7060 -- either no non-trivial postcondition or only inherited postconditions.
7062 Last_Contract_Cases : Node_Id := Empty;
7063 -- Last non-trivial contract-cases on the subprogram, or else Empty
7065 Attribute_Result_Mentioned : Boolean := False;
7066 -- True if 'Result used in a non-trivial postcondition or contract-cases
7068 No_Warning_On_Some_Postcondition : Boolean := False;
7069 -- True if there is a non-trivial postcondition or contract-cases
7070 -- without a corresponding warning.
7072 Post_State_Mentioned : Boolean := False;
7073 -- True if expression mentioned in a postcondition or contract-cases
7074 -- can have a different value in the post-state than in the pre-state.
7076 function Check_Attr_Result (N : Node_Id) return Traverse_Result;
7077 -- Check if N is a reference to the attribute 'Result, and if so set
7078 -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK.
7080 function Check_Post_State (N : Node_Id) return Traverse_Result;
7081 -- Check whether the value of evaluating N can be different in the
7082 -- post-state, compared to the same evaluation in the pre-state, and
7083 -- if so set Post_State_Mentioned and return Abandon. Return Skip on
7084 -- reference to attribute 'Old, in order to ignore its prefix, which
7085 -- is precisely evaluated in the pre-state. Otherwise return OK.
7087 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean;
7088 -- Return True if node N is trivially "True" or "False", and it comes
7089 -- from source. In particular, nodes that are statically known "True" or
7090 -- "False" by the compiler but not written as such in source code are
7091 -- not considered as trivial.
7093 procedure Process_Contract_Cases (Spec : Node_Id);
7094 -- This processes the Spec_CTC_List from Spec, processing any contract
7095 -- case from the list. The caller has checked that Spec_CTC_List is
7098 procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean);
7099 -- This processes the Spec_PPC_List from Spec, processing any
7100 -- postcondition from the list. If Class is True, then only
7101 -- postconditions marked with Class_Present are considered. The
7102 -- caller has checked that Spec_PPC_List is non-Empty.
7104 function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result);
7106 function Find_Post_State is new Traverse_Func (Check_Post_State);
7108 -----------------------
7109 -- Check_Attr_Result --
7110 -----------------------
7112 function Check_Attr_Result (N : Node_Id) return Traverse_Result is
7114 if Nkind (N) = N_Attribute_Reference
7115 and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result
7117 Attribute_Result_Mentioned := True;
7122 end Check_Attr_Result;
7124 ----------------------
7125 -- Check_Post_State --
7126 ----------------------
7128 function Check_Post_State (N : Node_Id) return Traverse_Result is
7129 Found : Boolean := False;
7133 when N_Function_Call |
7134 N_Explicit_Dereference =>
7141 E : constant Entity_Id := Entity (N);
7144 -- ???Quantified expressions get analyzed later, so E can
7145 -- be empty at this point. In this case, we suppress the
7146 -- warning, just in case E is assignable. It seems better to
7147 -- have false negatives than false positives. At some point,
7148 -- we should make the warning more accurate, either by
7149 -- analyzing quantified expressions earlier, or moving
7150 -- this processing later.
7155 and then Ekind (E) in Assignable_Kind)
7161 when N_Attribute_Reference =>
7162 case Get_Attribute_Id (Attribute_Name (N)) is
7163 when Attribute_Old =>
7165 when Attribute_Result =>
7176 Post_State_Mentioned := True;
7181 end Check_Post_State;
7183 --------------------------------
7184 -- Is_Trivial_Post_Or_Ensures --
7185 --------------------------------
7187 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean is
7189 return Is_Entity_Name (N)
7190 and then (Entity (N) = Standard_True
7192 Entity (N) = Standard_False)
7193 and then Comes_From_Source (N);
7194 end Is_Trivial_Post_Or_Ensures;
7196 ----------------------------
7197 -- Process_Contract_Cases --
7198 ----------------------------
7200 procedure Process_Contract_Cases (Spec : Node_Id) is
7204 Post_Case : Node_Id;
7206 Ignored : Traverse_Final_Result;
7207 pragma Unreferenced (Ignored);
7210 Prag := Spec_CTC_List (Contract (Spec));
7212 if Pragma_Name (Prag) = Name_Contract_Cases then
7214 Expression (First (Pragma_Argument_Associations (Prag)));
7216 Post_Case := First (Component_Associations (Aggr));
7217 while Present (Post_Case) loop
7218 Conseq := Expression (Post_Case);
7220 -- Ignore trivial contract-cases when consequence is "True"
7223 if not Is_Trivial_Post_Or_Ensures (Conseq) then
7224 Last_Contract_Cases := Prag;
7226 -- For functions, look for presence of 'Result in
7227 -- consequence expression.
7229 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7230 Ignored := Find_Attribute_Result (Conseq);
7233 -- For each individual case, look for presence of an
7234 -- expression that could be evaluated differently in
7237 Post_State_Mentioned := False;
7238 Ignored := Find_Post_State (Conseq);
7240 if Post_State_Mentioned then
7241 No_Warning_On_Some_Postcondition := True;
7244 ("contract case refers only to pre-state?T?",
7253 Prag := Next_Pragma (Prag);
7254 exit when No (Prag);
7256 end Process_Contract_Cases;
7258 -----------------------------
7259 -- Process_Post_Conditions --
7260 -----------------------------
7262 procedure Process_Post_Conditions
7268 Ignored : Traverse_Final_Result;
7269 pragma Unreferenced (Ignored);
7272 Prag := Spec_PPC_List (Contract (Spec));
7274 Arg := First (Pragma_Argument_Associations (Prag));
7276 -- Ignore trivial postcondition of "True" or "False"
7278 if Pragma_Name (Prag) = Name_Postcondition
7279 and then not Is_Trivial_Post_Or_Ensures (Expression (Arg))
7281 -- Since pre- and post-conditions are listed in reverse order,
7282 -- the first postcondition in the list is last in the source.
7284 if not Class and then No (Last_Postcondition) then
7285 Last_Postcondition := Prag;
7288 -- For functions, look for presence of 'Result in postcondition
7290 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7291 Ignored := Find_Attribute_Result (Arg);
7294 -- For each individual non-inherited postcondition, look
7295 -- for presence of an expression that could be evaluated
7296 -- differently in post-state.
7299 Post_State_Mentioned := False;
7300 Ignored := Find_Post_State (Arg);
7302 if Post_State_Mentioned then
7303 No_Warning_On_Some_Postcondition := True;
7306 ("postcondition refers only to pre-state?T?", Prag);
7311 Prag := Next_Pragma (Prag);
7312 exit when No (Prag);
7314 end Process_Post_Conditions;
7316 -- Start of processing for Check_Subprogram_Contract
7319 if not Warn_On_Suspicious_Contract then
7323 -- Process spec postconditions
7325 if Present (Spec_PPC_List (Contract (Spec_Id))) then
7326 Process_Post_Conditions (Spec_Id, Class => False);
7329 -- Process inherited postconditions
7331 -- Code is currently commented out as, in some cases, it causes crashes
7332 -- because Direct_Primitive_Operations is not available for a private
7333 -- type. This may cause more warnings to be issued than necessary. ???
7335 -- for J in Inherited'Range loop
7336 -- if Present (Spec_PPC_List (Contract (Inherited (J)))) then
7337 -- Process_Post_Conditions (Inherited (J), Class => True);
7341 -- Process contract cases
7343 if Present (Spec_CTC_List (Contract (Spec_Id))) then
7344 Process_Contract_Cases (Spec_Id);
7347 -- Issue warning for functions whose postcondition does not mention
7348 -- 'Result after all postconditions have been processed, and provided
7349 -- all postconditions do not already get a warning that they only refer
7352 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
7353 and then (Present (Last_Postcondition)
7354 or else Present (Last_Contract_Cases))
7355 and then not Attribute_Result_Mentioned
7356 and then No_Warning_On_Some_Postcondition
7358 if Present (Last_Postcondition) then
7359 if Present (Last_Contract_Cases) then
7361 ("neither function postcondition nor "
7362 & "contract cases mention result?T?", Last_Postcondition);
7366 ("function postcondition does not mention result?T?",
7367 Last_Postcondition);
7371 ("contract cases do not mention result?T?", Last_Contract_Cases);
7374 end Check_Subprogram_Contract;
7376 ----------------------------
7377 -- Check_Subprogram_Order --
7378 ----------------------------
7380 procedure Check_Subprogram_Order (N : Node_Id) is
7382 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7383 -- This is used to check if S1 > S2 in the sense required by this test,
7384 -- for example nameab < namec, but name2 < name10.
7386 -----------------------------
7387 -- Subprogram_Name_Greater --
7388 -----------------------------
7390 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7395 -- Deal with special case where names are identical except for a
7396 -- numerical suffix. These are handled specially, taking the numeric
7397 -- ordering from the suffix into account.
7400 while S1 (L1) in '0' .. '9' loop
7405 while S2 (L2) in '0' .. '9' loop
7409 -- If non-numeric parts non-equal, do straight compare
7411 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7414 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7415 -- that a missing suffix is treated as numeric zero in this test.
7419 while L1 < S1'Last loop
7421 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7425 while L2 < S2'Last loop
7427 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7432 end Subprogram_Name_Greater;
7434 -- Start of processing for Check_Subprogram_Order
7437 -- Check body in alpha order if this is option
7440 and then Style_Check_Order_Subprograms
7441 and then Nkind (N) = N_Subprogram_Body
7442 and then Comes_From_Source (N)
7443 and then In_Extended_Main_Source_Unit (N)
7447 renames Scope_Stack.Table
7448 (Scope_Stack.Last).Last_Subprogram_Name;
7450 Body_Id : constant Entity_Id :=
7451 Defining_Entity (Specification (N));
7454 Get_Decoded_Name_String (Chars (Body_Id));
7457 if Subprogram_Name_Greater
7458 (LSN.all, Name_Buffer (1 .. Name_Len))
7460 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7466 LSN := new String'(Name_Buffer (1 .. Name_Len));
7469 end Check_Subprogram_Order;
7471 ------------------------------
7472 -- Check_Subtype_Conformant --
7473 ------------------------------
7475 procedure Check_Subtype_Conformant
7476 (New_Id : Entity_Id;
7478 Err_Loc : Node_Id := Empty;
7479 Skip_Controlling_Formals : Boolean := False;
7480 Get_Inst : Boolean := False)
7483 pragma Warnings (Off, Result);
7486 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7487 Skip_Controlling_Formals => Skip_Controlling_Formals,
7488 Get_Inst => Get_Inst);
7489 end Check_Subtype_Conformant;
7491 ---------------------------
7492 -- Check_Type_Conformant --
7493 ---------------------------
7495 procedure Check_Type_Conformant
7496 (New_Id : Entity_Id;
7498 Err_Loc : Node_Id := Empty)
7501 pragma Warnings (Off, Result);
7504 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7505 end Check_Type_Conformant;
7507 ---------------------------
7508 -- Can_Override_Operator --
7509 ---------------------------
7511 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7515 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7519 Typ := Base_Type (Etype (First_Formal (Subp)));
7521 -- Check explicitly that the operation is a primitive of the type
7523 return Operator_Matches_Spec (Subp, Subp)
7524 and then not Is_Generic_Type (Typ)
7525 and then Scope (Subp) = Scope (Typ)
7526 and then not Is_Class_Wide_Type (Typ);
7528 end Can_Override_Operator;
7530 ----------------------
7531 -- Conforming_Types --
7532 ----------------------
7534 function Conforming_Types
7537 Ctype : Conformance_Type;
7538 Get_Inst : Boolean := False) return Boolean
7540 Type_1 : Entity_Id := T1;
7541 Type_2 : Entity_Id := T2;
7542 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7544 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7545 -- If neither T1 nor T2 are generic actual types, or if they are in
7546 -- different scopes (e.g. parent and child instances), then verify that
7547 -- the base types are equal. Otherwise T1 and T2 must be on the same
7548 -- subtype chain. The whole purpose of this procedure is to prevent
7549 -- spurious ambiguities in an instantiation that may arise if two
7550 -- distinct generic types are instantiated with the same actual.
7552 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7553 -- An access parameter can designate an incomplete type. If the
7554 -- incomplete type is the limited view of a type from a limited_
7555 -- with_clause, check whether the non-limited view is available. If
7556 -- it is a (non-limited) incomplete type, get the full view.
7558 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7559 -- Returns True if and only if either T1 denotes a limited view of T2
7560 -- or T2 denotes a limited view of T1. This can arise when the limited
7561 -- with view of a type is used in a subprogram declaration and the
7562 -- subprogram body is in the scope of a regular with clause for the
7563 -- same unit. In such a case, the two type entities can be considered
7564 -- identical for purposes of conformance checking.
7566 ----------------------
7567 -- Base_Types_Match --
7568 ----------------------
7570 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7571 BT1 : constant Entity_Id := Base_Type (T1);
7572 BT2 : constant Entity_Id := Base_Type (T2);
7578 elsif BT1 = BT2 then
7580 -- The following is too permissive. A more precise test should
7581 -- check that the generic actual is an ancestor subtype of the
7584 -- See code in Find_Corresponding_Spec that applies an additional
7585 -- filter to handle accidental amiguities in instances.
7587 return not Is_Generic_Actual_Type (T1)
7588 or else not Is_Generic_Actual_Type (T2)
7589 or else Scope (T1) /= Scope (T2);
7591 -- If T2 is a generic actual type it is declared as the subtype of
7592 -- the actual. If that actual is itself a subtype we need to use
7593 -- its own base type to check for compatibility.
7595 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7598 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7604 end Base_Types_Match;
7606 --------------------------
7607 -- Find_Designated_Type --
7608 --------------------------
7610 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7614 Desig := Directly_Designated_Type (T);
7616 if Ekind (Desig) = E_Incomplete_Type then
7618 -- If regular incomplete type, get full view if available
7620 if Present (Full_View (Desig)) then
7621 Desig := Full_View (Desig);
7623 -- If limited view of a type, get non-limited view if available,
7624 -- and check again for a regular incomplete type.
7626 elsif Present (Non_Limited_View (Desig)) then
7627 Desig := Get_Full_View (Non_Limited_View (Desig));
7632 end Find_Designated_Type;
7634 -------------------------------
7635 -- Matches_Limited_With_View --
7636 -------------------------------
7638 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7640 -- In some cases a type imported through a limited_with clause, and
7641 -- its nonlimited view are both visible, for example in an anonymous
7642 -- access-to-class-wide type in a formal. Both entities designate the
7645 if From_With_Type (T1) and then T2 = Available_View (T1) then
7648 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7651 elsif From_With_Type (T1)
7652 and then From_With_Type (T2)
7653 and then Available_View (T1) = Available_View (T2)
7660 end Matches_Limited_With_View;
7662 -- Start of processing for Conforming_Types
7665 -- The context is an instance association for a formal access-to-
7666 -- subprogram type; the formal parameter types require mapping because
7667 -- they may denote other formal parameters of the generic unit.
7670 Type_1 := Get_Instance_Of (T1);
7671 Type_2 := Get_Instance_Of (T2);
7674 -- If one of the types is a view of the other introduced by a limited
7675 -- with clause, treat these as conforming for all purposes.
7677 if Matches_Limited_With_View (T1, T2) then
7680 elsif Base_Types_Match (Type_1, Type_2) then
7681 return Ctype <= Mode_Conformant
7682 or else Subtypes_Statically_Match (Type_1, Type_2);
7684 elsif Is_Incomplete_Or_Private_Type (Type_1)
7685 and then Present (Full_View (Type_1))
7686 and then Base_Types_Match (Full_View (Type_1), Type_2)
7688 return Ctype <= Mode_Conformant
7689 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7691 elsif Ekind (Type_2) = E_Incomplete_Type
7692 and then Present (Full_View (Type_2))
7693 and then Base_Types_Match (Type_1, Full_View (Type_2))
7695 return Ctype <= Mode_Conformant
7696 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7698 elsif Is_Private_Type (Type_2)
7699 and then In_Instance
7700 and then Present (Full_View (Type_2))
7701 and then Base_Types_Match (Type_1, Full_View (Type_2))
7703 return Ctype <= Mode_Conformant
7704 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7707 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7708 -- treated recursively because they carry a signature.
7710 Are_Anonymous_Access_To_Subprogram_Types :=
7711 Ekind (Type_1) = Ekind (Type_2)
7713 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7714 E_Anonymous_Access_Protected_Subprogram_Type);
7716 -- Test anonymous access type case. For this case, static subtype
7717 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7718 -- the base types because we may have built internal subtype entities
7719 -- to handle null-excluding types (see Process_Formals).
7721 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7723 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7725 -- Ada 2005 (AI-254)
7727 or else Are_Anonymous_Access_To_Subprogram_Types
7730 Desig_1 : Entity_Id;
7731 Desig_2 : Entity_Id;
7734 -- In Ada 2005, access constant indicators must match for
7735 -- subtype conformance.
7737 if Ada_Version >= Ada_2005
7738 and then Ctype >= Subtype_Conformant
7740 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7745 Desig_1 := Find_Designated_Type (Type_1);
7746 Desig_2 := Find_Designated_Type (Type_2);
7748 -- If the context is an instance association for a formal
7749 -- access-to-subprogram type; formal access parameter designated
7750 -- types require mapping because they may denote other formal
7751 -- parameters of the generic unit.
7754 Desig_1 := Get_Instance_Of (Desig_1);
7755 Desig_2 := Get_Instance_Of (Desig_2);
7758 -- It is possible for a Class_Wide_Type to be introduced for an
7759 -- incomplete type, in which case there is a separate class_ wide
7760 -- type for the full view. The types conform if their Etypes
7761 -- conform, i.e. one may be the full view of the other. This can
7762 -- only happen in the context of an access parameter, other uses
7763 -- of an incomplete Class_Wide_Type are illegal.
7765 if Is_Class_Wide_Type (Desig_1)
7767 Is_Class_Wide_Type (Desig_2)
7771 (Etype (Base_Type (Desig_1)),
7772 Etype (Base_Type (Desig_2)), Ctype);
7774 elsif Are_Anonymous_Access_To_Subprogram_Types then
7775 if Ada_Version < Ada_2005 then
7776 return Ctype = Type_Conformant
7778 Subtypes_Statically_Match (Desig_1, Desig_2);
7780 -- We must check the conformance of the signatures themselves
7784 Conformant : Boolean;
7787 (Desig_1, Desig_2, Ctype, False, Conformant);
7793 return Base_Type (Desig_1) = Base_Type (Desig_2)
7794 and then (Ctype = Type_Conformant
7796 Subtypes_Statically_Match (Desig_1, Desig_2));
7800 -- Otherwise definitely no match
7803 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7804 and then Is_Access_Type (Type_2))
7805 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7806 and then Is_Access_Type (Type_1)))
7809 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7811 May_Hide_Profile := True;
7816 end Conforming_Types;
7818 --------------------------
7819 -- Create_Extra_Formals --
7820 --------------------------
7822 procedure Create_Extra_Formals (E : Entity_Id) is
7824 First_Extra : Entity_Id := Empty;
7825 Last_Extra : Entity_Id;
7826 Formal_Type : Entity_Id;
7827 P_Formal : Entity_Id := Empty;
7829 function Add_Extra_Formal
7830 (Assoc_Entity : Entity_Id;
7833 Suffix : String) return Entity_Id;
7834 -- Add an extra formal to the current list of formals and extra formals.
7835 -- The extra formal is added to the end of the list of extra formals,
7836 -- and also returned as the result. These formals are always of mode IN.
7837 -- The new formal has the type Typ, is declared in Scope, and its name
7838 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7839 -- The following suffixes are currently used. They should not be changed
7840 -- without coordinating with CodePeer, which makes use of these to
7841 -- provide better messages.
7843 -- O denotes the Constrained bit.
7844 -- L denotes the accessibility level.
7845 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7846 -- the full list in exp_ch6.BIP_Formal_Kind.
7848 ----------------------
7849 -- Add_Extra_Formal --
7850 ----------------------
7852 function Add_Extra_Formal
7853 (Assoc_Entity : Entity_Id;
7856 Suffix : String) return Entity_Id
7858 EF : constant Entity_Id :=
7859 Make_Defining_Identifier (Sloc (Assoc_Entity),
7860 Chars => New_External_Name (Chars (Assoc_Entity),
7864 -- A little optimization. Never generate an extra formal for the
7865 -- _init operand of an initialization procedure, since it could
7868 if Chars (Formal) = Name_uInit then
7872 Set_Ekind (EF, E_In_Parameter);
7873 Set_Actual_Subtype (EF, Typ);
7874 Set_Etype (EF, Typ);
7875 Set_Scope (EF, Scope);
7876 Set_Mechanism (EF, Default_Mechanism);
7877 Set_Formal_Validity (EF);
7879 if No (First_Extra) then
7881 Set_Extra_Formals (Scope, First_Extra);
7884 if Present (Last_Extra) then
7885 Set_Extra_Formal (Last_Extra, EF);
7891 end Add_Extra_Formal;
7893 -- Start of processing for Create_Extra_Formals
7896 -- We never generate extra formals if expansion is not active because we
7897 -- don't need them unless we are generating code.
7899 if not Expander_Active then
7903 -- No need to generate extra formals in interface thunks whose target
7904 -- primitive has no extra formals.
7906 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7910 -- If this is a derived subprogram then the subtypes of the parent
7911 -- subprogram's formal parameters will be used to determine the need
7912 -- for extra formals.
7914 if Is_Overloadable (E) and then Present (Alias (E)) then
7915 P_Formal := First_Formal (Alias (E));
7918 Last_Extra := Empty;
7919 Formal := First_Formal (E);
7920 while Present (Formal) loop
7921 Last_Extra := Formal;
7922 Next_Formal (Formal);
7925 -- If Extra_formals were already created, don't do it again. This
7926 -- situation may arise for subprogram types created as part of
7927 -- dispatching calls (see Expand_Dispatching_Call)
7929 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7933 -- If the subprogram is a predefined dispatching subprogram then don't
7934 -- generate any extra constrained or accessibility level formals. In
7935 -- general we suppress these for internal subprograms (by not calling
7936 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7937 -- generated stream attributes do get passed through because extra
7938 -- build-in-place formals are needed in some cases (limited 'Input).
7940 if Is_Predefined_Internal_Operation (E) then
7941 goto Test_For_Func_Result_Extras;
7944 Formal := First_Formal (E);
7945 while Present (Formal) loop
7947 -- Create extra formal for supporting the attribute 'Constrained.
7948 -- The case of a private type view without discriminants also
7949 -- requires the extra formal if the underlying type has defaulted
7952 if Ekind (Formal) /= E_In_Parameter then
7953 if Present (P_Formal) then
7954 Formal_Type := Etype (P_Formal);
7956 Formal_Type := Etype (Formal);
7959 -- Do not produce extra formals for Unchecked_Union parameters.
7960 -- Jump directly to the end of the loop.
7962 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7963 goto Skip_Extra_Formal_Generation;
7966 if not Has_Discriminants (Formal_Type)
7967 and then Ekind (Formal_Type) in Private_Kind
7968 and then Present (Underlying_Type (Formal_Type))
7970 Formal_Type := Underlying_Type (Formal_Type);
7973 -- Suppress the extra formal if formal's subtype is constrained or
7974 -- indefinite, or we're compiling for Ada 2012 and the underlying
7975 -- type is tagged and limited. In Ada 2012, a limited tagged type
7976 -- can have defaulted discriminants, but 'Constrained is required
7977 -- to return True, so the formal is never needed (see AI05-0214).
7978 -- Note that this ensures consistency of calling sequences for
7979 -- dispatching operations when some types in a class have defaults
7980 -- on discriminants and others do not (and requiring the extra
7981 -- formal would introduce distributed overhead).
7983 if Has_Discriminants (Formal_Type)
7984 and then not Is_Constrained (Formal_Type)
7985 and then not Is_Indefinite_Subtype (Formal_Type)
7986 and then (Ada_Version < Ada_2012
7988 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
7989 and then Is_Limited_Type (Formal_Type)))
7991 Set_Extra_Constrained
7992 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7996 -- Create extra formal for supporting accessibility checking. This
7997 -- is done for both anonymous access formals and formals of named
7998 -- access types that are marked as controlling formals. The latter
7999 -- case can occur when Expand_Dispatching_Call creates a subprogram
8000 -- type and substitutes the types of access-to-class-wide actuals
8001 -- for the anonymous access-to-specific-type of controlling formals.
8002 -- Base_Type is applied because in cases where there is a null
8003 -- exclusion the formal may have an access subtype.
8005 -- This is suppressed if we specifically suppress accessibility
8006 -- checks at the package level for either the subprogram, or the
8007 -- package in which it resides. However, we do not suppress it
8008 -- simply if the scope has accessibility checks suppressed, since
8009 -- this could cause trouble when clients are compiled with a
8010 -- different suppression setting. The explicit checks at the
8011 -- package level are safe from this point of view.
8013 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8014 or else (Is_Controlling_Formal (Formal)
8015 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8017 (Explicit_Suppress (E, Accessibility_Check)
8019 Explicit_Suppress (Scope (E), Accessibility_Check))
8022 or else Present (Extra_Accessibility (P_Formal)))
8024 Set_Extra_Accessibility
8025 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8028 -- This label is required when skipping extra formal generation for
8029 -- Unchecked_Union parameters.
8031 <<Skip_Extra_Formal_Generation>>
8033 if Present (P_Formal) then
8034 Next_Formal (P_Formal);
8037 Next_Formal (Formal);
8040 <<Test_For_Func_Result_Extras>>
8042 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8043 -- function call is ... determined by the point of call ...".
8045 if Needs_Result_Accessibility_Level (E) then
8046 Set_Extra_Accessibility_Of_Result
8047 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8050 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8051 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8053 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8055 Result_Subt : constant Entity_Id := Etype (E);
8056 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8057 Formal_Typ : Entity_Id;
8059 Discard : Entity_Id;
8060 pragma Warnings (Off, Discard);
8063 -- In the case of functions with unconstrained result subtypes,
8064 -- add a 4-state formal indicating whether the return object is
8065 -- allocated by the caller (1), or should be allocated by the
8066 -- callee on the secondary stack (2), in the global heap (3), or
8067 -- in a user-defined storage pool (4). For the moment we just use
8068 -- Natural for the type of this formal. Note that this formal
8069 -- isn't usually needed in the case where the result subtype is
8070 -- constrained, but it is needed when the function has a tagged
8071 -- result, because generally such functions can be called in a
8072 -- dispatching context and such calls must be handled like calls
8073 -- to a class-wide function.
8075 if Needs_BIP_Alloc_Form (E) then
8078 (E, Standard_Natural,
8079 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8081 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8082 -- use a user-defined pool. This formal is not added on
8083 -- .NET/JVM/ZFP as those targets do not support pools.
8085 if VM_Target = No_VM
8086 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8090 (E, RTE (RE_Root_Storage_Pool_Ptr),
8091 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8095 -- In the case of functions whose result type needs finalization,
8096 -- add an extra formal which represents the finalization master.
8098 if Needs_BIP_Finalization_Master (E) then
8101 (E, RTE (RE_Finalization_Master_Ptr),
8102 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8105 -- When the result type contains tasks, add two extra formals: the
8106 -- master of the tasks to be created, and the caller's activation
8109 if Has_Task (Full_Subt) then
8112 (E, RTE (RE_Master_Id),
8113 E, BIP_Formal_Suffix (BIP_Task_Master));
8116 (E, RTE (RE_Activation_Chain_Access),
8117 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8120 -- All build-in-place functions get an extra formal that will be
8121 -- passed the address of the return object within the caller.
8124 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8126 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8127 Set_Etype (Formal_Typ, Formal_Typ);
8128 Set_Depends_On_Private
8129 (Formal_Typ, Has_Private_Component (Formal_Typ));
8130 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8131 Set_Is_Access_Constant (Formal_Typ, False);
8133 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8134 -- the designated type comes from the limited view (for back-end
8137 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
8139 Layout_Type (Formal_Typ);
8143 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8146 end Create_Extra_Formals;
8148 -----------------------------
8149 -- Enter_Overloaded_Entity --
8150 -----------------------------
8152 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8153 E : Entity_Id := Current_Entity_In_Scope (S);
8154 C_E : Entity_Id := Current_Entity (S);
8158 Set_Has_Homonym (E);
8159 Set_Has_Homonym (S);
8162 Set_Is_Immediately_Visible (S);
8163 Set_Scope (S, Current_Scope);
8165 -- Chain new entity if front of homonym in current scope, so that
8166 -- homonyms are contiguous.
8168 if Present (E) and then E /= C_E then
8169 while Homonym (C_E) /= E loop
8170 C_E := Homonym (C_E);
8173 Set_Homonym (C_E, S);
8177 Set_Current_Entity (S);
8182 if Is_Inherited_Operation (S) then
8183 Append_Inherited_Subprogram (S);
8185 Append_Entity (S, Current_Scope);
8188 Set_Public_Status (S);
8190 if Debug_Flag_E then
8191 Write_Str ("New overloaded entity chain: ");
8192 Write_Name (Chars (S));
8195 while Present (E) loop
8196 Write_Str (" "); Write_Int (Int (E));
8203 -- Generate warning for hiding
8206 and then Comes_From_Source (S)
8207 and then In_Extended_Main_Source_Unit (S)
8214 -- Warn unless genuine overloading. Do not emit warning on
8215 -- hiding predefined operators in Standard (these are either an
8216 -- (artifact of our implicit declarations, or simple noise) but
8217 -- keep warning on a operator defined on a local subtype, because
8218 -- of the real danger that different operators may be applied in
8219 -- various parts of the program.
8221 -- Note that if E and S have the same scope, there is never any
8222 -- hiding. Either the two conflict, and the program is illegal,
8223 -- or S is overriding an implicit inherited subprogram.
8225 if Scope (E) /= Scope (S)
8226 and then (not Is_Overloadable (E)
8227 or else Subtype_Conformant (E, S))
8228 and then (Is_Immediately_Visible (E)
8230 Is_Potentially_Use_Visible (S))
8232 if Scope (E) /= Standard_Standard then
8233 Error_Msg_Sloc := Sloc (E);
8234 Error_Msg_N ("declaration of & hides one#?h?", S);
8236 elsif Nkind (S) = N_Defining_Operator_Symbol
8238 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8241 ("declaration of & hides predefined operator?h?", S);
8246 end Enter_Overloaded_Entity;
8248 -----------------------------
8249 -- Check_Untagged_Equality --
8250 -----------------------------
8252 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8253 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8254 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8258 if Nkind (Decl) = N_Subprogram_Declaration
8259 and then Is_Record_Type (Typ)
8260 and then not Is_Tagged_Type (Typ)
8262 -- If the type is not declared in a package, or if we are in the
8263 -- body of the package or in some other scope, the new operation is
8264 -- not primitive, and therefore legal, though suspicious. If the
8265 -- type is a generic actual (sub)type, the operation is not primitive
8266 -- either because the base type is declared elsewhere.
8268 if Is_Frozen (Typ) then
8269 if Ekind (Scope (Typ)) /= E_Package
8270 or else Scope (Typ) /= Current_Scope
8274 elsif Is_Generic_Actual_Type (Typ) then
8277 elsif In_Package_Body (Scope (Typ)) then
8279 ("equality operator must be declared "
8280 & "before type& is frozen", Eq_Op, Typ);
8282 ("\move declaration to package spec", Eq_Op);
8286 ("equality operator must be declared "
8287 & "before type& is frozen", Eq_Op, Typ);
8289 Obj_Decl := Next (Parent (Typ));
8290 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8291 if Nkind (Obj_Decl) = N_Object_Declaration
8292 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8295 ("type& is frozen by declaration??", Obj_Decl, Typ);
8297 ("\an equality operator cannot be declared after this "
8298 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8306 elsif not In_Same_List (Parent (Typ), Decl)
8307 and then not Is_Limited_Type (Typ)
8310 -- This makes it illegal to have a primitive equality declared in
8311 -- the private part if the type is visible.
8313 Error_Msg_N ("equality operator appears too late", Eq_Op);
8316 end Check_Untagged_Equality;
8318 -----------------------------
8319 -- Find_Corresponding_Spec --
8320 -----------------------------
8322 function Find_Corresponding_Spec
8324 Post_Error : Boolean := True) return Entity_Id
8326 Spec : constant Node_Id := Specification (N);
8327 Designator : constant Entity_Id := Defining_Entity (Spec);
8331 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8332 -- Even if fully conformant, a body may depend on a generic actual when
8333 -- the spec does not, or vice versa, in which case they were distinct
8334 -- entities in the generic.
8336 -------------------------------
8337 -- Different_Generic_Profile --
8338 -------------------------------
8340 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8344 if Ekind (E) = E_Function
8345 and then Is_Generic_Actual_Type (Etype (E)) /=
8346 Is_Generic_Actual_Type (Etype (Designator))
8351 F1 := First_Formal (Designator);
8352 F2 := First_Formal (E);
8353 while Present (F1) loop
8354 if Is_Generic_Actual_Type (Etype (F1)) /=
8355 Is_Generic_Actual_Type (Etype (F2))
8365 end Different_Generic_Profile;
8367 -- Start of processing for Find_Corresponding_Spec
8370 E := Current_Entity (Designator);
8371 while Present (E) loop
8373 -- We are looking for a matching spec. It must have the same scope,
8374 -- and the same name, and either be type conformant, or be the case
8375 -- of a library procedure spec and its body (which belong to one
8376 -- another regardless of whether they are type conformant or not).
8378 if Scope (E) = Current_Scope then
8379 if Current_Scope = Standard_Standard
8380 or else (Ekind (E) = Ekind (Designator)
8381 and then Type_Conformant (E, Designator))
8383 -- Within an instantiation, we know that spec and body are
8384 -- subtype conformant, because they were subtype conformant in
8385 -- the generic. We choose the subtype-conformant entity here as
8386 -- well, to resolve spurious ambiguities in the instance that
8387 -- were not present in the generic (i.e. when two different
8388 -- types are given the same actual). If we are looking for a
8389 -- spec to match a body, full conformance is expected.
8392 Set_Convention (Designator, Convention (E));
8394 -- Skip past subprogram bodies and subprogram renamings that
8395 -- may appear to have a matching spec, but that aren't fully
8396 -- conformant with it. That can occur in cases where an
8397 -- actual type causes unrelated homographs in the instance.
8399 if Nkind_In (N, N_Subprogram_Body,
8400 N_Subprogram_Renaming_Declaration)
8401 and then Present (Homonym (E))
8402 and then not Fully_Conformant (Designator, E)
8406 elsif not Subtype_Conformant (Designator, E) then
8409 elsif Different_Generic_Profile (E) then
8414 -- Ada 2012 (AI05-0165): For internally generated bodies of
8415 -- null procedures locate the internally generated spec. We
8416 -- enforce mode conformance since a tagged type may inherit
8417 -- from interfaces several null primitives which differ only
8418 -- in the mode of the formals.
8420 if not (Comes_From_Source (E))
8421 and then Is_Null_Procedure (E)
8422 and then not Mode_Conformant (Designator, E)
8426 -- For null procedures coming from source that are completions,
8427 -- analysis of the generated body will establish the link.
8429 elsif Comes_From_Source (E)
8430 and then Nkind (Spec) = N_Procedure_Specification
8431 and then Null_Present (Spec)
8435 elsif not Has_Completion (E) then
8436 if Nkind (N) /= N_Subprogram_Body_Stub then
8437 Set_Corresponding_Spec (N, E);
8440 Set_Has_Completion (E);
8443 elsif Nkind (Parent (N)) = N_Subunit then
8445 -- If this is the proper body of a subunit, the completion
8446 -- flag is set when analyzing the stub.
8450 -- If E is an internal function with a controlling result that
8451 -- was created for an operation inherited by a null extension,
8452 -- it may be overridden by a body without a previous spec (one
8453 -- more reason why these should be shunned). In that case
8454 -- remove the generated body if present, because the current
8455 -- one is the explicit overriding.
8457 elsif Ekind (E) = E_Function
8458 and then Ada_Version >= Ada_2005
8459 and then not Comes_From_Source (E)
8460 and then Has_Controlling_Result (E)
8461 and then Is_Null_Extension (Etype (E))
8462 and then Comes_From_Source (Spec)
8464 Set_Has_Completion (E, False);
8467 and then Nkind (Parent (E)) = N_Function_Specification
8470 (Unit_Declaration_Node
8471 (Corresponding_Body (Unit_Declaration_Node (E))));
8475 -- If expansion is disabled, or if the wrapper function has
8476 -- not been generated yet, this a late body overriding an
8477 -- inherited operation, or it is an overriding by some other
8478 -- declaration before the controlling result is frozen. In
8479 -- either case this is a declaration of a new entity.
8485 -- If the body already exists, then this is an error unless
8486 -- the previous declaration is the implicit declaration of a
8487 -- derived subprogram. It is also legal for an instance to
8488 -- contain type conformant overloadable declarations (but the
8489 -- generic declaration may not), per 8.3(26/2).
8491 elsif No (Alias (E))
8492 and then not Is_Intrinsic_Subprogram (E)
8493 and then not In_Instance
8496 Error_Msg_Sloc := Sloc (E);
8498 if Is_Imported (E) then
8500 ("body not allowed for imported subprogram & declared#",
8503 Error_Msg_NE ("duplicate body for & declared#", N, E);
8507 -- Child units cannot be overloaded, so a conformance mismatch
8508 -- between body and a previous spec is an error.
8510 elsif Is_Child_Unit (E)
8512 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8514 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8519 ("body of child unit does not match previous declaration", N);
8527 -- On exit, we know that no previous declaration of subprogram exists
8530 end Find_Corresponding_Spec;
8532 ----------------------
8533 -- Fully_Conformant --
8534 ----------------------
8536 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8539 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8541 end Fully_Conformant;
8543 ----------------------------------
8544 -- Fully_Conformant_Expressions --
8545 ----------------------------------
8547 function Fully_Conformant_Expressions
8548 (Given_E1 : Node_Id;
8549 Given_E2 : Node_Id) return Boolean
8551 E1 : constant Node_Id := Original_Node (Given_E1);
8552 E2 : constant Node_Id := Original_Node (Given_E2);
8553 -- We always test conformance on original nodes, since it is possible
8554 -- for analysis and/or expansion to make things look as though they
8555 -- conform when they do not, e.g. by converting 1+2 into 3.
8557 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8558 renames Fully_Conformant_Expressions;
8560 function FCL (L1, L2 : List_Id) return Boolean;
8561 -- Compare elements of two lists for conformance. Elements have to be
8562 -- conformant, and actuals inserted as default parameters do not match
8563 -- explicit actuals with the same value.
8565 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8566 -- Compare an operator node with a function call
8572 function FCL (L1, L2 : List_Id) return Boolean is
8576 if L1 = No_List then
8582 if L2 = No_List then
8588 -- Compare two lists, skipping rewrite insertions (we want to compare
8589 -- the original trees, not the expanded versions!)
8592 if Is_Rewrite_Insertion (N1) then
8594 elsif Is_Rewrite_Insertion (N2) then
8600 elsif not FCE (N1, N2) then
8613 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8614 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8619 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8624 Act := First (Actuals);
8626 if Nkind (Op_Node) in N_Binary_Op then
8627 if not FCE (Left_Opnd (Op_Node), Act) then
8634 return Present (Act)
8635 and then FCE (Right_Opnd (Op_Node), Act)
8636 and then No (Next (Act));
8640 -- Start of processing for Fully_Conformant_Expressions
8643 -- Non-conformant if paren count does not match. Note: if some idiot
8644 -- complains that we don't do this right for more than 3 levels of
8645 -- parentheses, they will be treated with the respect they deserve!
8647 if Paren_Count (E1) /= Paren_Count (E2) then
8650 -- If same entities are referenced, then they are conformant even if
8651 -- they have different forms (RM 8.3.1(19-20)).
8653 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8654 if Present (Entity (E1)) then
8655 return Entity (E1) = Entity (E2)
8656 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8657 and then Ekind (Entity (E1)) = E_Discriminant
8658 and then Ekind (Entity (E2)) = E_In_Parameter);
8660 elsif Nkind (E1) = N_Expanded_Name
8661 and then Nkind (E2) = N_Expanded_Name
8662 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8663 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8665 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8668 -- Identifiers in component associations don't always have
8669 -- entities, but their names must conform.
8671 return Nkind (E1) = N_Identifier
8672 and then Nkind (E2) = N_Identifier
8673 and then Chars (E1) = Chars (E2);
8676 elsif Nkind (E1) = N_Character_Literal
8677 and then Nkind (E2) = N_Expanded_Name
8679 return Nkind (Selector_Name (E2)) = N_Character_Literal
8680 and then Chars (E1) = Chars (Selector_Name (E2));
8682 elsif Nkind (E2) = N_Character_Literal
8683 and then Nkind (E1) = N_Expanded_Name
8685 return Nkind (Selector_Name (E1)) = N_Character_Literal
8686 and then Chars (E2) = Chars (Selector_Name (E1));
8688 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8689 return FCO (E1, E2);
8691 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8692 return FCO (E2, E1);
8694 -- Otherwise we must have the same syntactic entity
8696 elsif Nkind (E1) /= Nkind (E2) then
8699 -- At this point, we specialize by node type
8706 FCL (Expressions (E1), Expressions (E2))
8708 FCL (Component_Associations (E1),
8709 Component_Associations (E2));
8712 if Nkind (Expression (E1)) = N_Qualified_Expression
8714 Nkind (Expression (E2)) = N_Qualified_Expression
8716 return FCE (Expression (E1), Expression (E2));
8718 -- Check that the subtype marks and any constraints
8723 Indic1 : constant Node_Id := Expression (E1);
8724 Indic2 : constant Node_Id := Expression (E2);
8729 if Nkind (Indic1) /= N_Subtype_Indication then
8731 Nkind (Indic2) /= N_Subtype_Indication
8732 and then Entity (Indic1) = Entity (Indic2);
8734 elsif Nkind (Indic2) /= N_Subtype_Indication then
8736 Nkind (Indic1) /= N_Subtype_Indication
8737 and then Entity (Indic1) = Entity (Indic2);
8740 if Entity (Subtype_Mark (Indic1)) /=
8741 Entity (Subtype_Mark (Indic2))
8746 Elt1 := First (Constraints (Constraint (Indic1)));
8747 Elt2 := First (Constraints (Constraint (Indic2)));
8748 while Present (Elt1) and then Present (Elt2) loop
8749 if not FCE (Elt1, Elt2) then
8762 when N_Attribute_Reference =>
8764 Attribute_Name (E1) = Attribute_Name (E2)
8765 and then FCL (Expressions (E1), Expressions (E2));
8769 Entity (E1) = Entity (E2)
8770 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8771 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8773 when N_Short_Circuit | N_Membership_Test =>
8775 FCE (Left_Opnd (E1), Left_Opnd (E2))
8777 FCE (Right_Opnd (E1), Right_Opnd (E2));
8779 when N_Case_Expression =>
8785 if not FCE (Expression (E1), Expression (E2)) then
8789 Alt1 := First (Alternatives (E1));
8790 Alt2 := First (Alternatives (E2));
8792 if Present (Alt1) /= Present (Alt2) then
8794 elsif No (Alt1) then
8798 if not FCE (Expression (Alt1), Expression (Alt2))
8799 or else not FCL (Discrete_Choices (Alt1),
8800 Discrete_Choices (Alt2))
8811 when N_Character_Literal =>
8813 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8815 when N_Component_Association =>
8817 FCL (Choices (E1), Choices (E2))
8819 FCE (Expression (E1), Expression (E2));
8821 when N_Explicit_Dereference =>
8823 FCE (Prefix (E1), Prefix (E2));
8825 when N_Extension_Aggregate =>
8827 FCL (Expressions (E1), Expressions (E2))
8828 and then Null_Record_Present (E1) =
8829 Null_Record_Present (E2)
8830 and then FCL (Component_Associations (E1),
8831 Component_Associations (E2));
8833 when N_Function_Call =>
8835 FCE (Name (E1), Name (E2))
8837 FCL (Parameter_Associations (E1),
8838 Parameter_Associations (E2));
8840 when N_If_Expression =>
8842 FCL (Expressions (E1), Expressions (E2));
8844 when N_Indexed_Component =>
8846 FCE (Prefix (E1), Prefix (E2))
8848 FCL (Expressions (E1), Expressions (E2));
8850 when N_Integer_Literal =>
8851 return (Intval (E1) = Intval (E2));
8856 when N_Operator_Symbol =>
8858 Chars (E1) = Chars (E2);
8860 when N_Others_Choice =>
8863 when N_Parameter_Association =>
8865 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8866 and then FCE (Explicit_Actual_Parameter (E1),
8867 Explicit_Actual_Parameter (E2));
8869 when N_Qualified_Expression =>
8871 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8873 FCE (Expression (E1), Expression (E2));
8875 when N_Quantified_Expression =>
8876 if not FCE (Condition (E1), Condition (E2)) then
8880 if Present (Loop_Parameter_Specification (E1))
8881 and then Present (Loop_Parameter_Specification (E2))
8884 L1 : constant Node_Id :=
8885 Loop_Parameter_Specification (E1);
8886 L2 : constant Node_Id :=
8887 Loop_Parameter_Specification (E2);
8891 Reverse_Present (L1) = Reverse_Present (L2)
8893 FCE (Defining_Identifier (L1),
8894 Defining_Identifier (L2))
8896 FCE (Discrete_Subtype_Definition (L1),
8897 Discrete_Subtype_Definition (L2));
8900 elsif Present (Iterator_Specification (E1))
8901 and then Present (Iterator_Specification (E2))
8904 I1 : constant Node_Id := Iterator_Specification (E1);
8905 I2 : constant Node_Id := Iterator_Specification (E2);
8909 FCE (Defining_Identifier (I1),
8910 Defining_Identifier (I2))
8912 Of_Present (I1) = Of_Present (I2)
8914 Reverse_Present (I1) = Reverse_Present (I2)
8915 and then FCE (Name (I1), Name (I2))
8916 and then FCE (Subtype_Indication (I1),
8917 Subtype_Indication (I2));
8920 -- The quantified expressions used different specifications to
8921 -- walk their respective ranges.
8929 FCE (Low_Bound (E1), Low_Bound (E2))
8931 FCE (High_Bound (E1), High_Bound (E2));
8933 when N_Real_Literal =>
8934 return (Realval (E1) = Realval (E2));
8936 when N_Selected_Component =>
8938 FCE (Prefix (E1), Prefix (E2))
8940 FCE (Selector_Name (E1), Selector_Name (E2));
8944 FCE (Prefix (E1), Prefix (E2))
8946 FCE (Discrete_Range (E1), Discrete_Range (E2));
8948 when N_String_Literal =>
8950 S1 : constant String_Id := Strval (E1);
8951 S2 : constant String_Id := Strval (E2);
8952 L1 : constant Nat := String_Length (S1);
8953 L2 : constant Nat := String_Length (S2);
8960 for J in 1 .. L1 loop
8961 if Get_String_Char (S1, J) /=
8962 Get_String_Char (S2, J)
8972 when N_Type_Conversion =>
8974 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8976 FCE (Expression (E1), Expression (E2));
8980 Entity (E1) = Entity (E2)
8982 FCE (Right_Opnd (E1), Right_Opnd (E2));
8984 when N_Unchecked_Type_Conversion =>
8986 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8988 FCE (Expression (E1), Expression (E2));
8990 -- All other node types cannot appear in this context. Strictly
8991 -- we should raise a fatal internal error. Instead we just ignore
8992 -- the nodes. This means that if anyone makes a mistake in the
8993 -- expander and mucks an expression tree irretrievably, the
8994 -- result will be a failure to detect a (probably very obscure)
8995 -- case of non-conformance, which is better than bombing on some
8996 -- case where two expressions do in fact conform.
9003 end Fully_Conformant_Expressions;
9005 ----------------------------------------
9006 -- Fully_Conformant_Discrete_Subtypes --
9007 ----------------------------------------
9009 function Fully_Conformant_Discrete_Subtypes
9010 (Given_S1 : Node_Id;
9011 Given_S2 : Node_Id) return Boolean
9013 S1 : constant Node_Id := Original_Node (Given_S1);
9014 S2 : constant Node_Id := Original_Node (Given_S2);
9016 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9017 -- Special-case for a bound given by a discriminant, which in the body
9018 -- is replaced with the discriminal of the enclosing type.
9020 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9021 -- Check both bounds
9023 -----------------------
9024 -- Conforming_Bounds --
9025 -----------------------
9027 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9029 if Is_Entity_Name (B1)
9030 and then Is_Entity_Name (B2)
9031 and then Ekind (Entity (B1)) = E_Discriminant
9033 return Chars (B1) = Chars (B2);
9036 return Fully_Conformant_Expressions (B1, B2);
9038 end Conforming_Bounds;
9040 -----------------------
9041 -- Conforming_Ranges --
9042 -----------------------
9044 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9047 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9049 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9050 end Conforming_Ranges;
9052 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9055 if Nkind (S1) /= Nkind (S2) then
9058 elsif Is_Entity_Name (S1) then
9059 return Entity (S1) = Entity (S2);
9061 elsif Nkind (S1) = N_Range then
9062 return Conforming_Ranges (S1, S2);
9064 elsif Nkind (S1) = N_Subtype_Indication then
9066 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9069 (Range_Expression (Constraint (S1)),
9070 Range_Expression (Constraint (S2)));
9074 end Fully_Conformant_Discrete_Subtypes;
9076 --------------------
9077 -- Install_Entity --
9078 --------------------
9080 procedure Install_Entity (E : Entity_Id) is
9081 Prev : constant Entity_Id := Current_Entity (E);
9083 Set_Is_Immediately_Visible (E);
9084 Set_Current_Entity (E);
9085 Set_Homonym (E, Prev);
9088 ---------------------
9089 -- Install_Formals --
9090 ---------------------
9092 procedure Install_Formals (Id : Entity_Id) is
9095 F := First_Formal (Id);
9096 while Present (F) loop
9100 end Install_Formals;
9102 -----------------------------
9103 -- Is_Interface_Conformant --
9104 -----------------------------
9106 function Is_Interface_Conformant
9107 (Tagged_Type : Entity_Id;
9108 Iface_Prim : Entity_Id;
9109 Prim : Entity_Id) return Boolean
9111 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
9112 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9114 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9115 -- Return the controlling formal of Prim
9117 ------------------------
9118 -- Controlling_Formal --
9119 ------------------------
9121 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9122 E : Entity_Id := First_Entity (Prim);
9125 while Present (E) loop
9126 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9134 end Controlling_Formal;
9138 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9139 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9141 -- Start of processing for Is_Interface_Conformant
9144 pragma Assert (Is_Subprogram (Iface_Prim)
9145 and then Is_Subprogram (Prim)
9146 and then Is_Dispatching_Operation (Iface_Prim)
9147 and then Is_Dispatching_Operation (Prim));
9149 pragma Assert (Is_Interface (Iface)
9150 or else (Present (Alias (Iface_Prim))
9153 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9155 if Prim = Iface_Prim
9156 or else not Is_Subprogram (Prim)
9157 or else Ekind (Prim) /= Ekind (Iface_Prim)
9158 or else not Is_Dispatching_Operation (Prim)
9159 or else Scope (Prim) /= Scope (Tagged_Type)
9161 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9162 or else not Primitive_Names_Match (Iface_Prim, Prim)
9166 -- The mode of the controlling formals must match
9168 elsif Present (Iface_Ctrl_F)
9169 and then Present (Prim_Ctrl_F)
9170 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9174 -- Case of a procedure, or a function whose result type matches the
9175 -- result type of the interface primitive, or a function that has no
9176 -- controlling result (I or access I).
9178 elsif Ekind (Iface_Prim) = E_Procedure
9179 or else Etype (Prim) = Etype (Iface_Prim)
9180 or else not Has_Controlling_Result (Prim)
9182 return Type_Conformant
9183 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9185 -- Case of a function returning an interface, or an access to one.
9186 -- Check that the return types correspond.
9188 elsif Implements_Interface (Typ, Iface) then
9189 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9191 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9196 Type_Conformant (Prim, Iface_Prim,
9197 Skip_Controlling_Formals => True);
9203 end Is_Interface_Conformant;
9205 ---------------------------------
9206 -- Is_Non_Overriding_Operation --
9207 ---------------------------------
9209 function Is_Non_Overriding_Operation
9210 (Prev_E : Entity_Id;
9211 New_E : Entity_Id) return Boolean
9215 G_Typ : Entity_Id := Empty;
9217 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9218 -- If F_Type is a derived type associated with a generic actual subtype,
9219 -- then return its Generic_Parent_Type attribute, else return Empty.
9221 function Types_Correspond
9222 (P_Type : Entity_Id;
9223 N_Type : Entity_Id) return Boolean;
9224 -- Returns true if and only if the types (or designated types in the
9225 -- case of anonymous access types) are the same or N_Type is derived
9226 -- directly or indirectly from P_Type.
9228 -----------------------------
9229 -- Get_Generic_Parent_Type --
9230 -----------------------------
9232 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9238 if Is_Derived_Type (F_Typ)
9239 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9241 -- The tree must be traversed to determine the parent subtype in
9242 -- the generic unit, which unfortunately isn't always available
9243 -- via semantic attributes. ??? (Note: The use of Original_Node
9244 -- is needed for cases where a full derived type has been
9247 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9248 if Nkind (Defn) = N_Derived_Type_Definition then
9249 Indic := Subtype_Indication (Defn);
9251 if Nkind (Indic) = N_Subtype_Indication then
9252 G_Typ := Entity (Subtype_Mark (Indic));
9254 G_Typ := Entity (Indic);
9257 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9258 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9260 return Generic_Parent_Type (Parent (G_Typ));
9266 end Get_Generic_Parent_Type;
9268 ----------------------
9269 -- Types_Correspond --
9270 ----------------------
9272 function Types_Correspond
9273 (P_Type : Entity_Id;
9274 N_Type : Entity_Id) return Boolean
9276 Prev_Type : Entity_Id := Base_Type (P_Type);
9277 New_Type : Entity_Id := Base_Type (N_Type);
9280 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9281 Prev_Type := Designated_Type (Prev_Type);
9284 if Ekind (New_Type) = E_Anonymous_Access_Type then
9285 New_Type := Designated_Type (New_Type);
9288 if Prev_Type = New_Type then
9291 elsif not Is_Class_Wide_Type (New_Type) then
9292 while Etype (New_Type) /= New_Type loop
9293 New_Type := Etype (New_Type);
9294 if New_Type = Prev_Type then
9300 end Types_Correspond;
9302 -- Start of processing for Is_Non_Overriding_Operation
9305 -- In the case where both operations are implicit derived subprograms
9306 -- then neither overrides the other. This can only occur in certain
9307 -- obscure cases (e.g., derivation from homographs created in a generic
9310 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9313 elsif Ekind (Current_Scope) = E_Package
9314 and then Is_Generic_Instance (Current_Scope)
9315 and then In_Private_Part (Current_Scope)
9316 and then Comes_From_Source (New_E)
9318 -- We examine the formals and result type of the inherited operation,
9319 -- to determine whether their type is derived from (the instance of)
9320 -- a generic type. The first such formal or result type is the one
9323 Formal := First_Formal (Prev_E);
9324 while Present (Formal) loop
9325 F_Typ := Base_Type (Etype (Formal));
9327 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9328 F_Typ := Designated_Type (F_Typ);
9331 G_Typ := Get_Generic_Parent_Type (F_Typ);
9332 exit when Present (G_Typ);
9334 Next_Formal (Formal);
9337 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9338 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9345 -- If the generic type is a private type, then the original operation
9346 -- was not overriding in the generic, because there was no primitive
9347 -- operation to override.
9349 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9350 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9351 N_Formal_Private_Type_Definition
9355 -- The generic parent type is the ancestor of a formal derived
9356 -- type declaration. We need to check whether it has a primitive
9357 -- operation that should be overridden by New_E in the generic.
9361 P_Formal : Entity_Id;
9362 N_Formal : Entity_Id;
9366 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9369 while Present (Prim_Elt) loop
9370 P_Prim := Node (Prim_Elt);
9372 if Chars (P_Prim) = Chars (New_E)
9373 and then Ekind (P_Prim) = Ekind (New_E)
9375 P_Formal := First_Formal (P_Prim);
9376 N_Formal := First_Formal (New_E);
9377 while Present (P_Formal) and then Present (N_Formal) loop
9378 P_Typ := Etype (P_Formal);
9379 N_Typ := Etype (N_Formal);
9381 if not Types_Correspond (P_Typ, N_Typ) then
9385 Next_Entity (P_Formal);
9386 Next_Entity (N_Formal);
9389 -- Found a matching primitive operation belonging to the
9390 -- formal ancestor type, so the new subprogram is
9394 and then No (N_Formal)
9395 and then (Ekind (New_E) /= E_Function
9398 (Etype (P_Prim), Etype (New_E)))
9404 Next_Elmt (Prim_Elt);
9407 -- If no match found, then the new subprogram does not
9408 -- override in the generic (nor in the instance).
9410 -- If the type in question is not abstract, and the subprogram
9411 -- is, this will be an error if the new operation is in the
9412 -- private part of the instance. Emit a warning now, which will
9413 -- make the subsequent error message easier to understand.
9415 if not Is_Abstract_Type (F_Typ)
9416 and then Is_Abstract_Subprogram (Prev_E)
9417 and then In_Private_Part (Current_Scope)
9419 Error_Msg_Node_2 := F_Typ;
9421 ("private operation& in generic unit does not override " &
9422 "any primitive operation of& (RM 12.3 (18))??",
9432 end Is_Non_Overriding_Operation;
9434 -------------------------------------
9435 -- List_Inherited_Pre_Post_Aspects --
9436 -------------------------------------
9438 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9440 if Opt.List_Inherited_Aspects
9441 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9444 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9448 for J in Inherited'Range loop
9449 P := Spec_PPC_List (Contract (Inherited (J)));
9450 while Present (P) loop
9451 Error_Msg_Sloc := Sloc (P);
9453 if Class_Present (P) and then not Split_PPC (P) then
9454 if Pragma_Name (P) = Name_Precondition then
9456 ("info: & inherits `Pre''Class` aspect from #?L?",
9460 ("info: & inherits `Post''Class` aspect from #?L?",
9465 P := Next_Pragma (P);
9470 end List_Inherited_Pre_Post_Aspects;
9472 ------------------------------
9473 -- Make_Inequality_Operator --
9474 ------------------------------
9476 -- S is the defining identifier of an equality operator. We build a
9477 -- subprogram declaration with the right signature. This operation is
9478 -- intrinsic, because it is always expanded as the negation of the
9479 -- call to the equality function.
9481 procedure Make_Inequality_Operator (S : Entity_Id) is
9482 Loc : constant Source_Ptr := Sloc (S);
9485 Op_Name : Entity_Id;
9487 FF : constant Entity_Id := First_Formal (S);
9488 NF : constant Entity_Id := Next_Formal (FF);
9491 -- Check that equality was properly defined, ignore call if not
9498 A : constant Entity_Id :=
9499 Make_Defining_Identifier (Sloc (FF),
9500 Chars => Chars (FF));
9502 B : constant Entity_Id :=
9503 Make_Defining_Identifier (Sloc (NF),
9504 Chars => Chars (NF));
9507 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9509 Formals := New_List (
9510 Make_Parameter_Specification (Loc,
9511 Defining_Identifier => A,
9513 New_Reference_To (Etype (First_Formal (S)),
9514 Sloc (Etype (First_Formal (S))))),
9516 Make_Parameter_Specification (Loc,
9517 Defining_Identifier => B,
9519 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9520 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9523 Make_Subprogram_Declaration (Loc,
9525 Make_Function_Specification (Loc,
9526 Defining_Unit_Name => Op_Name,
9527 Parameter_Specifications => Formals,
9528 Result_Definition =>
9529 New_Reference_To (Standard_Boolean, Loc)));
9531 -- Insert inequality right after equality if it is explicit or after
9532 -- the derived type when implicit. These entities are created only
9533 -- for visibility purposes, and eventually replaced in the course of
9534 -- expansion, so they do not need to be attached to the tree and seen
9535 -- by the back-end. Keeping them internal also avoids spurious
9536 -- freezing problems. The declaration is inserted in the tree for
9537 -- analysis, and removed afterwards. If the equality operator comes
9538 -- from an explicit declaration, attach the inequality immediately
9539 -- after. Else the equality is inherited from a derived type
9540 -- declaration, so insert inequality after that declaration.
9542 if No (Alias (S)) then
9543 Insert_After (Unit_Declaration_Node (S), Decl);
9544 elsif Is_List_Member (Parent (S)) then
9545 Insert_After (Parent (S), Decl);
9547 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9550 Mark_Rewrite_Insertion (Decl);
9551 Set_Is_Intrinsic_Subprogram (Op_Name);
9554 Set_Has_Completion (Op_Name);
9555 Set_Corresponding_Equality (Op_Name, S);
9556 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9558 end Make_Inequality_Operator;
9560 ----------------------
9561 -- May_Need_Actuals --
9562 ----------------------
9564 procedure May_Need_Actuals (Fun : Entity_Id) is
9569 F := First_Formal (Fun);
9571 while Present (F) loop
9572 if No (Default_Value (F)) then
9580 Set_Needs_No_Actuals (Fun, B);
9581 end May_Need_Actuals;
9583 ---------------------
9584 -- Mode_Conformant --
9585 ---------------------
9587 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9590 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9592 end Mode_Conformant;
9594 ---------------------------
9595 -- New_Overloaded_Entity --
9596 ---------------------------
9598 procedure New_Overloaded_Entity
9600 Derived_Type : Entity_Id := Empty)
9602 Overridden_Subp : Entity_Id := Empty;
9603 -- Set if the current scope has an operation that is type-conformant
9604 -- with S, and becomes hidden by S.
9606 Is_Primitive_Subp : Boolean;
9607 -- Set to True if the new subprogram is primitive
9610 -- Entity that S overrides
9612 Prev_Vis : Entity_Id := Empty;
9613 -- Predecessor of E in Homonym chain
9615 procedure Check_For_Primitive_Subprogram
9616 (Is_Primitive : out Boolean;
9617 Is_Overriding : Boolean := False);
9618 -- If the subprogram being analyzed is a primitive operation of the type
9619 -- of a formal or result, set the Has_Primitive_Operations flag on the
9620 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9621 -- corresponding flag on the entity itself for later use.
9623 procedure Check_Synchronized_Overriding
9624 (Def_Id : Entity_Id;
9625 Overridden_Subp : out Entity_Id);
9626 -- First determine if Def_Id is an entry or a subprogram either defined
9627 -- in the scope of a task or protected type, or is a primitive of such
9628 -- a type. Check whether Def_Id overrides a subprogram of an interface
9629 -- implemented by the synchronized type, return the overridden entity
9632 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9633 -- Check that E is declared in the private part of the current package,
9634 -- or in the package body, where it may hide a previous declaration.
9635 -- We can't use In_Private_Part by itself because this flag is also
9636 -- set when freezing entities, so we must examine the place of the
9637 -- declaration in the tree, and recognize wrapper packages as well.
9639 function Is_Overriding_Alias
9641 New_E : Entity_Id) return Boolean;
9642 -- Check whether new subprogram and old subprogram are both inherited
9643 -- from subprograms that have distinct dispatch table entries. This can
9644 -- occur with derivations from instances with accidental homonyms.
9645 -- The function is conservative given that the converse is only true
9646 -- within instances that contain accidental overloadings.
9648 ------------------------------------
9649 -- Check_For_Primitive_Subprogram --
9650 ------------------------------------
9652 procedure Check_For_Primitive_Subprogram
9653 (Is_Primitive : out Boolean;
9654 Is_Overriding : Boolean := False)
9660 function Visible_Part_Type (T : Entity_Id) return Boolean;
9661 -- Returns true if T is declared in the visible part of the current
9662 -- package scope; otherwise returns false. Assumes that T is declared
9665 procedure Check_Private_Overriding (T : Entity_Id);
9666 -- Checks that if a primitive abstract subprogram of a visible
9667 -- abstract type is declared in a private part, then it must override
9668 -- an abstract subprogram declared in the visible part. Also checks
9669 -- that if a primitive function with a controlling result is declared
9670 -- in a private part, then it must override a function declared in
9671 -- the visible part.
9673 ------------------------------
9674 -- Check_Private_Overriding --
9675 ------------------------------
9677 procedure Check_Private_Overriding (T : Entity_Id) is
9679 if Is_Package_Or_Generic_Package (Current_Scope)
9680 and then In_Private_Part (Current_Scope)
9681 and then Visible_Part_Type (T)
9682 and then not In_Instance
9684 if Is_Abstract_Type (T)
9685 and then Is_Abstract_Subprogram (S)
9686 and then (not Is_Overriding
9687 or else not Is_Abstract_Subprogram (E))
9690 ("abstract subprograms must be visible "
9691 & "(RM 3.9.3(10))!", S);
9693 elsif Ekind (S) = E_Function and then not Is_Overriding then
9694 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9696 ("private function with tagged result must"
9697 & " override visible-part function", S);
9699 ("\move subprogram to the visible part"
9700 & " (RM 3.9.3(10))", S);
9702 -- AI05-0073: extend this test to the case of a function
9703 -- with a controlling access result.
9705 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9706 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9708 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9709 and then Ada_Version >= Ada_2012
9712 ("private function with controlling access result "
9713 & "must override visible-part function", S);
9715 ("\move subprogram to the visible part"
9716 & " (RM 3.9.3(10))", S);
9720 end Check_Private_Overriding;
9722 -----------------------
9723 -- Visible_Part_Type --
9724 -----------------------
9726 function Visible_Part_Type (T : Entity_Id) return Boolean is
9727 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9731 -- If the entity is a private type, then it must be declared in a
9734 if Ekind (T) in Private_Kind then
9738 -- Otherwise, we traverse the visible part looking for its
9739 -- corresponding declaration. We cannot use the declaration
9740 -- node directly because in the private part the entity of a
9741 -- private type is the one in the full view, which does not
9742 -- indicate that it is the completion of something visible.
9744 N := First (Visible_Declarations (Specification (P)));
9745 while Present (N) loop
9746 if Nkind (N) = N_Full_Type_Declaration
9747 and then Present (Defining_Identifier (N))
9748 and then T = Defining_Identifier (N)
9752 elsif Nkind_In (N, N_Private_Type_Declaration,
9753 N_Private_Extension_Declaration)
9754 and then Present (Defining_Identifier (N))
9755 and then T = Full_View (Defining_Identifier (N))
9764 end Visible_Part_Type;
9766 -- Start of processing for Check_For_Primitive_Subprogram
9769 Is_Primitive := False;
9771 if not Comes_From_Source (S) then
9774 -- If subprogram is at library level, it is not primitive operation
9776 elsif Current_Scope = Standard_Standard then
9779 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9780 and then not In_Package_Body (Current_Scope))
9781 or else Is_Overriding
9783 -- For function, check return type
9785 if Ekind (S) = E_Function then
9786 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9787 F_Typ := Designated_Type (Etype (S));
9792 B_Typ := Base_Type (F_Typ);
9794 if Scope (B_Typ) = Current_Scope
9795 and then not Is_Class_Wide_Type (B_Typ)
9796 and then not Is_Generic_Type (B_Typ)
9798 Is_Primitive := True;
9799 Set_Has_Primitive_Operations (B_Typ);
9800 Set_Is_Primitive (S);
9801 Check_Private_Overriding (B_Typ);
9805 -- For all subprograms, check formals
9807 Formal := First_Formal (S);
9808 while Present (Formal) loop
9809 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9810 F_Typ := Designated_Type (Etype (Formal));
9812 F_Typ := Etype (Formal);
9815 B_Typ := Base_Type (F_Typ);
9817 if Ekind (B_Typ) = E_Access_Subtype then
9818 B_Typ := Base_Type (B_Typ);
9821 if Scope (B_Typ) = Current_Scope
9822 and then not Is_Class_Wide_Type (B_Typ)
9823 and then not Is_Generic_Type (B_Typ)
9825 Is_Primitive := True;
9826 Set_Is_Primitive (S);
9827 Set_Has_Primitive_Operations (B_Typ);
9828 Check_Private_Overriding (B_Typ);
9831 Next_Formal (Formal);
9834 -- Special case: An equality function can be redefined for a type
9835 -- occurring in a declarative part, and won't otherwise be treated as
9836 -- a primitive because it doesn't occur in a package spec and doesn't
9837 -- override an inherited subprogram. It's important that we mark it
9838 -- primitive so it can be returned by Collect_Primitive_Operations
9839 -- and be used in composing the equality operation of later types
9840 -- that have a component of the type.
9842 elsif Chars (S) = Name_Op_Eq
9843 and then Etype (S) = Standard_Boolean
9845 B_Typ := Base_Type (Etype (First_Formal (S)));
9847 if Scope (B_Typ) = Current_Scope
9849 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9850 and then not Is_Limited_Type (B_Typ)
9852 Is_Primitive := True;
9853 Set_Is_Primitive (S);
9854 Set_Has_Primitive_Operations (B_Typ);
9855 Check_Private_Overriding (B_Typ);
9858 end Check_For_Primitive_Subprogram;
9860 -----------------------------------
9861 -- Check_Synchronized_Overriding --
9862 -----------------------------------
9864 procedure Check_Synchronized_Overriding
9865 (Def_Id : Entity_Id;
9866 Overridden_Subp : out Entity_Id)
9868 Ifaces_List : Elist_Id;
9872 function Matches_Prefixed_View_Profile
9873 (Prim_Params : List_Id;
9874 Iface_Params : List_Id) return Boolean;
9875 -- Determine whether a subprogram's parameter profile Prim_Params
9876 -- matches that of a potentially overridden interface subprogram
9877 -- Iface_Params. Also determine if the type of first parameter of
9878 -- Iface_Params is an implemented interface.
9880 -----------------------------------
9881 -- Matches_Prefixed_View_Profile --
9882 -----------------------------------
9884 function Matches_Prefixed_View_Profile
9885 (Prim_Params : List_Id;
9886 Iface_Params : List_Id) return Boolean
9888 Iface_Id : Entity_Id;
9889 Iface_Param : Node_Id;
9890 Iface_Typ : Entity_Id;
9891 Prim_Id : Entity_Id;
9892 Prim_Param : Node_Id;
9893 Prim_Typ : Entity_Id;
9895 function Is_Implemented
9896 (Ifaces_List : Elist_Id;
9897 Iface : Entity_Id) return Boolean;
9898 -- Determine if Iface is implemented by the current task or
9901 --------------------
9902 -- Is_Implemented --
9903 --------------------
9905 function Is_Implemented
9906 (Ifaces_List : Elist_Id;
9907 Iface : Entity_Id) return Boolean
9909 Iface_Elmt : Elmt_Id;
9912 Iface_Elmt := First_Elmt (Ifaces_List);
9913 while Present (Iface_Elmt) loop
9914 if Node (Iface_Elmt) = Iface then
9918 Next_Elmt (Iface_Elmt);
9924 -- Start of processing for Matches_Prefixed_View_Profile
9927 Iface_Param := First (Iface_Params);
9928 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9930 if Is_Access_Type (Iface_Typ) then
9931 Iface_Typ := Designated_Type (Iface_Typ);
9934 Prim_Param := First (Prim_Params);
9936 -- The first parameter of the potentially overridden subprogram
9937 -- must be an interface implemented by Prim.
9939 if not Is_Interface (Iface_Typ)
9940 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9945 -- The checks on the object parameters are done, move onto the
9946 -- rest of the parameters.
9948 if not In_Scope then
9949 Prim_Param := Next (Prim_Param);
9952 Iface_Param := Next (Iface_Param);
9953 while Present (Iface_Param) and then Present (Prim_Param) loop
9954 Iface_Id := Defining_Identifier (Iface_Param);
9955 Iface_Typ := Find_Parameter_Type (Iface_Param);
9957 Prim_Id := Defining_Identifier (Prim_Param);
9958 Prim_Typ := Find_Parameter_Type (Prim_Param);
9960 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9961 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9962 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9964 Iface_Typ := Designated_Type (Iface_Typ);
9965 Prim_Typ := Designated_Type (Prim_Typ);
9968 -- Case of multiple interface types inside a parameter profile
9970 -- (Obj_Param : in out Iface; ...; Param : Iface)
9972 -- If the interface type is implemented, then the matching type
9973 -- in the primitive should be the implementing record type.
9975 if Ekind (Iface_Typ) = E_Record_Type
9976 and then Is_Interface (Iface_Typ)
9977 and then Is_Implemented (Ifaces_List, Iface_Typ)
9979 if Prim_Typ /= Typ then
9983 -- The two parameters must be both mode and subtype conformant
9985 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9987 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9996 -- One of the two lists contains more parameters than the other
9998 if Present (Iface_Param) or else Present (Prim_Param) then
10003 end Matches_Prefixed_View_Profile;
10005 -- Start of processing for Check_Synchronized_Overriding
10008 Overridden_Subp := Empty;
10010 -- Def_Id must be an entry or a subprogram. We should skip predefined
10011 -- primitives internally generated by the frontend; however at this
10012 -- stage predefined primitives are still not fully decorated. As a
10013 -- minor optimization we skip here internally generated subprograms.
10015 if (Ekind (Def_Id) /= E_Entry
10016 and then Ekind (Def_Id) /= E_Function
10017 and then Ekind (Def_Id) /= E_Procedure)
10018 or else not Comes_From_Source (Def_Id)
10023 -- Search for the concurrent declaration since it contains the list
10024 -- of all implemented interfaces. In this case, the subprogram is
10025 -- declared within the scope of a protected or a task type.
10027 if Present (Scope (Def_Id))
10028 and then Is_Concurrent_Type (Scope (Def_Id))
10029 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10031 Typ := Scope (Def_Id);
10034 -- The enclosing scope is not a synchronized type and the subprogram
10037 elsif No (First_Formal (Def_Id)) then
10040 -- The subprogram has formals and hence it may be a primitive of a
10041 -- concurrent type.
10044 Typ := Etype (First_Formal (Def_Id));
10046 if Is_Access_Type (Typ) then
10047 Typ := Directly_Designated_Type (Typ);
10050 if Is_Concurrent_Type (Typ)
10051 and then not Is_Generic_Actual_Type (Typ)
10055 -- This case occurs when the concurrent type is declared within
10056 -- a generic unit. As a result the corresponding record has been
10057 -- built and used as the type of the first formal, we just have
10058 -- to retrieve the corresponding concurrent type.
10060 elsif Is_Concurrent_Record_Type (Typ)
10061 and then not Is_Class_Wide_Type (Typ)
10062 and then Present (Corresponding_Concurrent_Type (Typ))
10064 Typ := Corresponding_Concurrent_Type (Typ);
10072 -- There is no overriding to check if is an inherited operation in a
10073 -- type derivation on for a generic actual.
10075 Collect_Interfaces (Typ, Ifaces_List);
10077 if Is_Empty_Elmt_List (Ifaces_List) then
10081 -- Determine whether entry or subprogram Def_Id overrides a primitive
10082 -- operation that belongs to one of the interfaces in Ifaces_List.
10085 Candidate : Entity_Id := Empty;
10086 Hom : Entity_Id := Empty;
10087 Iface_Typ : Entity_Id;
10088 Subp : Entity_Id := Empty;
10091 -- Traverse the homonym chain, looking for a potentially
10092 -- overridden subprogram that belongs to an implemented
10095 Hom := Current_Entity_In_Scope (Def_Id);
10096 while Present (Hom) loop
10100 or else not Is_Overloadable (Subp)
10101 or else not Is_Primitive (Subp)
10102 or else not Is_Dispatching_Operation (Subp)
10103 or else not Present (Find_Dispatching_Type (Subp))
10104 or else not Is_Interface (Find_Dispatching_Type (Subp))
10108 -- Entries and procedures can override abstract or null
10109 -- interface procedures.
10111 elsif (Ekind (Def_Id) = E_Procedure
10112 or else Ekind (Def_Id) = E_Entry)
10113 and then Ekind (Subp) = E_Procedure
10114 and then Matches_Prefixed_View_Profile
10115 (Parameter_Specifications (Parent (Def_Id)),
10116 Parameter_Specifications (Parent (Subp)))
10120 -- For an overridden subprogram Subp, check whether the mode
10121 -- of its first parameter is correct depending on the kind
10122 -- of synchronized type.
10125 Formal : constant Node_Id := First_Formal (Candidate);
10128 -- In order for an entry or a protected procedure to
10129 -- override, the first parameter of the overridden
10130 -- routine must be of mode "out", "in out" or
10131 -- access-to-variable.
10133 if Ekind_In (Candidate, E_Entry, E_Procedure)
10134 and then Is_Protected_Type (Typ)
10135 and then Ekind (Formal) /= E_In_Out_Parameter
10136 and then Ekind (Formal) /= E_Out_Parameter
10137 and then Nkind (Parameter_Type (Parent (Formal))) /=
10138 N_Access_Definition
10142 -- All other cases are OK since a task entry or routine
10143 -- does not have a restriction on the mode of the first
10144 -- parameter of the overridden interface routine.
10147 Overridden_Subp := Candidate;
10152 -- Functions can override abstract interface functions
10154 elsif Ekind (Def_Id) = E_Function
10155 and then Ekind (Subp) = E_Function
10156 and then Matches_Prefixed_View_Profile
10157 (Parameter_Specifications (Parent (Def_Id)),
10158 Parameter_Specifications (Parent (Subp)))
10159 and then Etype (Result_Definition (Parent (Def_Id))) =
10160 Etype (Result_Definition (Parent (Subp)))
10162 Overridden_Subp := Subp;
10166 Hom := Homonym (Hom);
10169 -- After examining all candidates for overriding, we are left with
10170 -- the best match which is a mode incompatible interface routine.
10171 -- Do not emit an error if the Expander is active since this error
10172 -- will be detected later on after all concurrent types are
10173 -- expanded and all wrappers are built. This check is meant for
10174 -- spec-only compilations.
10176 if Present (Candidate) and then not Expander_Active then
10178 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10180 -- Def_Id is primitive of a protected type, declared inside the
10181 -- type, and the candidate is primitive of a limited or
10182 -- synchronized interface.
10185 and then Is_Protected_Type (Typ)
10187 (Is_Limited_Interface (Iface_Typ)
10188 or else Is_Protected_Interface (Iface_Typ)
10189 or else Is_Synchronized_Interface (Iface_Typ)
10190 or else Is_Task_Interface (Iface_Typ))
10192 Error_Msg_PT (Parent (Typ), Candidate);
10196 Overridden_Subp := Candidate;
10199 end Check_Synchronized_Overriding;
10201 ----------------------------
10202 -- Is_Private_Declaration --
10203 ----------------------------
10205 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10206 Priv_Decls : List_Id;
10207 Decl : constant Node_Id := Unit_Declaration_Node (E);
10210 if Is_Package_Or_Generic_Package (Current_Scope)
10211 and then In_Private_Part (Current_Scope)
10214 Private_Declarations
10215 (Specification (Unit_Declaration_Node (Current_Scope)));
10217 return In_Package_Body (Current_Scope)
10219 (Is_List_Member (Decl)
10220 and then List_Containing (Decl) = Priv_Decls)
10221 or else (Nkind (Parent (Decl)) = N_Package_Specification
10223 Is_Compilation_Unit
10224 (Defining_Entity (Parent (Decl)))
10225 and then List_Containing (Parent (Parent (Decl))) =
10230 end Is_Private_Declaration;
10232 --------------------------
10233 -- Is_Overriding_Alias --
10234 --------------------------
10236 function Is_Overriding_Alias
10237 (Old_E : Entity_Id;
10238 New_E : Entity_Id) return Boolean
10240 AO : constant Entity_Id := Alias (Old_E);
10241 AN : constant Entity_Id := Alias (New_E);
10244 return Scope (AO) /= Scope (AN)
10245 or else No (DTC_Entity (AO))
10246 or else No (DTC_Entity (AN))
10247 or else DT_Position (AO) = DT_Position (AN);
10248 end Is_Overriding_Alias;
10250 -- Start of processing for New_Overloaded_Entity
10253 -- We need to look for an entity that S may override. This must be a
10254 -- homonym in the current scope, so we look for the first homonym of
10255 -- S in the current scope as the starting point for the search.
10257 E := Current_Entity_In_Scope (S);
10259 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10260 -- They are directly added to the list of primitive operations of
10261 -- Derived_Type, unless this is a rederivation in the private part
10262 -- of an operation that was already derived in the visible part of
10263 -- the current package.
10265 if Ada_Version >= Ada_2005
10266 and then Present (Derived_Type)
10267 and then Present (Alias (S))
10268 and then Is_Dispatching_Operation (Alias (S))
10269 and then Present (Find_Dispatching_Type (Alias (S)))
10270 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10272 -- For private types, when the full-view is processed we propagate to
10273 -- the full view the non-overridden entities whose attribute "alias"
10274 -- references an interface primitive. These entities were added by
10275 -- Derive_Subprograms to ensure that interface primitives are
10278 -- Inside_Freeze_Actions is non zero when S corresponds with an
10279 -- internal entity that links an interface primitive with its
10280 -- covering primitive through attribute Interface_Alias (see
10281 -- Add_Internal_Interface_Entities).
10283 if Inside_Freezing_Actions = 0
10284 and then Is_Package_Or_Generic_Package (Current_Scope)
10285 and then In_Private_Part (Current_Scope)
10286 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10287 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10288 and then Full_View (Defining_Identifier (Parent (E)))
10289 = Defining_Identifier (Parent (S))
10290 and then Alias (E) = Alias (S)
10292 Check_Operation_From_Private_View (S, E);
10293 Set_Is_Dispatching_Operation (S);
10298 Enter_Overloaded_Entity (S);
10299 Check_Dispatching_Operation (S, Empty);
10300 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10306 -- If there is no homonym then this is definitely not overriding
10309 Enter_Overloaded_Entity (S);
10310 Check_Dispatching_Operation (S, Empty);
10311 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10313 -- If subprogram has an explicit declaration, check whether it
10314 -- has an overriding indicator.
10316 if Comes_From_Source (S) then
10317 Check_Synchronized_Overriding (S, Overridden_Subp);
10319 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10320 -- it may have overridden some hidden inherited primitive. Update
10321 -- Overridden_Subp to avoid spurious errors when checking the
10322 -- overriding indicator.
10324 if Ada_Version >= Ada_2012
10325 and then No (Overridden_Subp)
10326 and then Is_Dispatching_Operation (S)
10327 and then Present (Overridden_Operation (S))
10329 Overridden_Subp := Overridden_Operation (S);
10332 Check_Overriding_Indicator
10333 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10336 -- If there is a homonym that is not overloadable, then we have an
10337 -- error, except for the special cases checked explicitly below.
10339 elsif not Is_Overloadable (E) then
10341 -- Check for spurious conflict produced by a subprogram that has the
10342 -- same name as that of the enclosing generic package. The conflict
10343 -- occurs within an instance, between the subprogram and the renaming
10344 -- declaration for the package. After the subprogram, the package
10345 -- renaming declaration becomes hidden.
10347 if Ekind (E) = E_Package
10348 and then Present (Renamed_Object (E))
10349 and then Renamed_Object (E) = Current_Scope
10350 and then Nkind (Parent (Renamed_Object (E))) =
10351 N_Package_Specification
10352 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10355 Set_Is_Immediately_Visible (E, False);
10356 Enter_Overloaded_Entity (S);
10357 Set_Homonym (S, Homonym (E));
10358 Check_Dispatching_Operation (S, Empty);
10359 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10361 -- If the subprogram is implicit it is hidden by the previous
10362 -- declaration. However if it is dispatching, it must appear in the
10363 -- dispatch table anyway, because it can be dispatched to even if it
10364 -- cannot be called directly.
10366 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10367 Set_Scope (S, Current_Scope);
10369 if Is_Dispatching_Operation (Alias (S)) then
10370 Check_Dispatching_Operation (S, Empty);
10376 Error_Msg_Sloc := Sloc (E);
10378 -- Generate message, with useful additional warning if in generic
10380 if Is_Generic_Unit (E) then
10381 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10382 Error_Msg_N ("\& conflicts with declaration#", S);
10384 Error_Msg_N ("& conflicts with declaration#", S);
10390 -- E exists and is overloadable
10393 Check_Synchronized_Overriding (S, Overridden_Subp);
10395 -- Loop through E and its homonyms to determine if any of them is
10396 -- the candidate for overriding by S.
10398 while Present (E) loop
10400 -- Definitely not interesting if not in the current scope
10402 if Scope (E) /= Current_Scope then
10405 -- Ada 2012 (AI05-0165): For internally generated bodies of
10406 -- null procedures locate the internally generated spec. We
10407 -- enforce mode conformance since a tagged type may inherit
10408 -- from interfaces several null primitives which differ only
10409 -- in the mode of the formals.
10411 elsif not Comes_From_Source (S)
10412 and then Is_Null_Procedure (S)
10413 and then not Mode_Conformant (E, S)
10417 -- Check if we have type conformance
10419 elsif Type_Conformant (E, S) then
10421 -- If the old and new entities have the same profile and one
10422 -- is not the body of the other, then this is an error, unless
10423 -- one of them is implicitly declared.
10425 -- There are some cases when both can be implicit, for example
10426 -- when both a literal and a function that overrides it are
10427 -- inherited in a derivation, or when an inherited operation
10428 -- of a tagged full type overrides the inherited operation of
10429 -- a private extension. Ada 83 had a special rule for the
10430 -- literal case. In Ada 95, the later implicit operation hides
10431 -- the former, and the literal is always the former. In the
10432 -- odd case where both are derived operations declared at the
10433 -- same point, both operations should be declared, and in that
10434 -- case we bypass the following test and proceed to the next
10435 -- part. This can only occur for certain obscure cases in
10436 -- instances, when an operation on a type derived from a formal
10437 -- private type does not override a homograph inherited from
10438 -- the actual. In subsequent derivations of such a type, the
10439 -- DT positions of these operations remain distinct, if they
10442 if Present (Alias (S))
10443 and then (No (Alias (E))
10444 or else Comes_From_Source (E)
10445 or else Is_Abstract_Subprogram (S)
10447 (Is_Dispatching_Operation (E)
10448 and then Is_Overriding_Alias (E, S)))
10449 and then Ekind (E) /= E_Enumeration_Literal
10451 -- When an derived operation is overloaded it may be due to
10452 -- the fact that the full view of a private extension
10453 -- re-inherits. It has to be dealt with.
10455 if Is_Package_Or_Generic_Package (Current_Scope)
10456 and then In_Private_Part (Current_Scope)
10458 Check_Operation_From_Private_View (S, E);
10461 -- In any case the implicit operation remains hidden by the
10462 -- existing declaration, which is overriding. Indicate that
10463 -- E overrides the operation from which S is inherited.
10465 if Present (Alias (S)) then
10466 Set_Overridden_Operation (E, Alias (S));
10468 Set_Overridden_Operation (E, S);
10471 if Comes_From_Source (E) then
10472 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10477 -- Within an instance, the renaming declarations for actual
10478 -- subprograms may become ambiguous, but they do not hide each
10481 elsif Ekind (E) /= E_Entry
10482 and then not Comes_From_Source (E)
10483 and then not Is_Generic_Instance (E)
10484 and then (Present (Alias (E))
10485 or else Is_Intrinsic_Subprogram (E))
10486 and then (not In_Instance
10487 or else No (Parent (E))
10488 or else Nkind (Unit_Declaration_Node (E)) /=
10489 N_Subprogram_Renaming_Declaration)
10491 -- A subprogram child unit is not allowed to override an
10492 -- inherited subprogram (10.1.1(20)).
10494 if Is_Child_Unit (S) then
10496 ("child unit overrides inherited subprogram in parent",
10501 if Is_Non_Overriding_Operation (E, S) then
10502 Enter_Overloaded_Entity (S);
10504 if No (Derived_Type)
10505 or else Is_Tagged_Type (Derived_Type)
10507 Check_Dispatching_Operation (S, Empty);
10513 -- E is a derived operation or an internal operator which
10514 -- is being overridden. Remove E from further visibility.
10515 -- Furthermore, if E is a dispatching operation, it must be
10516 -- replaced in the list of primitive operations of its type
10517 -- (see Override_Dispatching_Operation).
10519 Overridden_Subp := E;
10525 Prev := First_Entity (Current_Scope);
10526 while Present (Prev) and then Next_Entity (Prev) /= E loop
10527 Next_Entity (Prev);
10530 -- It is possible for E to be in the current scope and
10531 -- yet not in the entity chain. This can only occur in a
10532 -- generic context where E is an implicit concatenation
10533 -- in the formal part, because in a generic body the
10534 -- entity chain starts with the formals.
10537 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10539 -- E must be removed both from the entity_list of the
10540 -- current scope, and from the visibility chain
10542 if Debug_Flag_E then
10543 Write_Str ("Override implicit operation ");
10544 Write_Int (Int (E));
10548 -- If E is a predefined concatenation, it stands for four
10549 -- different operations. As a result, a single explicit
10550 -- declaration does not hide it. In a possible ambiguous
10551 -- situation, Disambiguate chooses the user-defined op,
10552 -- so it is correct to retain the previous internal one.
10554 if Chars (E) /= Name_Op_Concat
10555 or else Ekind (E) /= E_Operator
10557 -- For nondispatching derived operations that are
10558 -- overridden by a subprogram declared in the private
10559 -- part of a package, we retain the derived subprogram
10560 -- but mark it as not immediately visible. If the
10561 -- derived operation was declared in the visible part
10562 -- then this ensures that it will still be visible
10563 -- outside the package with the proper signature
10564 -- (calls from outside must also be directed to this
10565 -- version rather than the overriding one, unlike the
10566 -- dispatching case). Calls from inside the package
10567 -- will still resolve to the overriding subprogram
10568 -- since the derived one is marked as not visible
10569 -- within the package.
10571 -- If the private operation is dispatching, we achieve
10572 -- the overriding by keeping the implicit operation
10573 -- but setting its alias to be the overriding one. In
10574 -- this fashion the proper body is executed in all
10575 -- cases, but the original signature is used outside
10578 -- If the overriding is not in the private part, we
10579 -- remove the implicit operation altogether.
10581 if Is_Private_Declaration (S) then
10582 if not Is_Dispatching_Operation (E) then
10583 Set_Is_Immediately_Visible (E, False);
10585 -- Work done in Override_Dispatching_Operation,
10586 -- so nothing else needs to be done here.
10592 -- Find predecessor of E in Homonym chain
10594 if E = Current_Entity (E) then
10597 Prev_Vis := Current_Entity (E);
10598 while Homonym (Prev_Vis) /= E loop
10599 Prev_Vis := Homonym (Prev_Vis);
10603 if Prev_Vis /= Empty then
10605 -- Skip E in the visibility chain
10607 Set_Homonym (Prev_Vis, Homonym (E));
10610 Set_Name_Entity_Id (Chars (E), Homonym (E));
10613 Set_Next_Entity (Prev, Next_Entity (E));
10615 if No (Next_Entity (Prev)) then
10616 Set_Last_Entity (Current_Scope, Prev);
10621 Enter_Overloaded_Entity (S);
10623 -- For entities generated by Derive_Subprograms the
10624 -- overridden operation is the inherited primitive
10625 -- (which is available through the attribute alias).
10627 if not (Comes_From_Source (E))
10628 and then Is_Dispatching_Operation (E)
10629 and then Find_Dispatching_Type (E) =
10630 Find_Dispatching_Type (S)
10631 and then Present (Alias (E))
10632 and then Comes_From_Source (Alias (E))
10634 Set_Overridden_Operation (S, Alias (E));
10636 -- Normal case of setting entity as overridden
10638 -- Note: Static_Initialization and Overridden_Operation
10639 -- attributes use the same field in subprogram entities.
10640 -- Static_Initialization is only defined for internal
10641 -- initialization procedures, where Overridden_Operation
10642 -- is irrelevant. Therefore the setting of this attribute
10643 -- must check whether the target is an init_proc.
10645 elsif not Is_Init_Proc (S) then
10646 Set_Overridden_Operation (S, E);
10649 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10651 -- If S is a user-defined subprogram or a null procedure
10652 -- expanded to override an inherited null procedure, or a
10653 -- predefined dispatching primitive then indicate that E
10654 -- overrides the operation from which S is inherited.
10656 if Comes_From_Source (S)
10658 (Present (Parent (S))
10660 Nkind (Parent (S)) = N_Procedure_Specification
10662 Null_Present (Parent (S)))
10664 (Present (Alias (E))
10666 Is_Predefined_Dispatching_Operation (Alias (E)))
10668 if Present (Alias (E)) then
10669 Set_Overridden_Operation (S, Alias (E));
10673 if Is_Dispatching_Operation (E) then
10675 -- An overriding dispatching subprogram inherits the
10676 -- convention of the overridden subprogram (AI-117).
10678 Set_Convention (S, Convention (E));
10679 Check_Dispatching_Operation (S, E);
10682 Check_Dispatching_Operation (S, Empty);
10685 Check_For_Primitive_Subprogram
10686 (Is_Primitive_Subp, Is_Overriding => True);
10687 goto Check_Inequality;
10690 -- Apparent redeclarations in instances can occur when two
10691 -- formal types get the same actual type. The subprograms in
10692 -- in the instance are legal, even if not callable from the
10693 -- outside. Calls from within are disambiguated elsewhere.
10694 -- For dispatching operations in the visible part, the usual
10695 -- rules apply, and operations with the same profile are not
10696 -- legal (B830001).
10698 elsif (In_Instance_Visible_Part
10699 and then not Is_Dispatching_Operation (E))
10700 or else In_Instance_Not_Visible
10704 -- Here we have a real error (identical profile)
10707 Error_Msg_Sloc := Sloc (E);
10709 -- Avoid cascaded errors if the entity appears in
10710 -- subsequent calls.
10712 Set_Scope (S, Current_Scope);
10714 -- Generate error, with extra useful warning for the case
10715 -- of a generic instance with no completion.
10717 if Is_Generic_Instance (S)
10718 and then not Has_Completion (E)
10721 ("instantiation cannot provide body for&", S);
10722 Error_Msg_N ("\& conflicts with declaration#", S);
10724 Error_Msg_N ("& conflicts with declaration#", S);
10731 -- If one subprogram has an access parameter and the other
10732 -- a parameter of an access type, calls to either might be
10733 -- ambiguous. Verify that parameters match except for the
10734 -- access parameter.
10736 if May_Hide_Profile then
10742 F1 := First_Formal (S);
10743 F2 := First_Formal (E);
10744 while Present (F1) and then Present (F2) loop
10745 if Is_Access_Type (Etype (F1)) then
10746 if not Is_Access_Type (Etype (F2))
10747 or else not Conforming_Types
10748 (Designated_Type (Etype (F1)),
10749 Designated_Type (Etype (F2)),
10752 May_Hide_Profile := False;
10756 not Conforming_Types
10757 (Etype (F1), Etype (F2), Type_Conformant)
10759 May_Hide_Profile := False;
10766 if May_Hide_Profile
10770 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10779 -- On exit, we know that S is a new entity
10781 Enter_Overloaded_Entity (S);
10782 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10783 Check_Overriding_Indicator
10784 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10786 -- Overloading is not allowed in SPARK, except for operators
10788 if Nkind (S) /= N_Defining_Operator_Symbol then
10789 Error_Msg_Sloc := Sloc (Homonym (S));
10790 Check_SPARK_Restriction
10791 ("overloading not allowed with entity#", S);
10794 -- If S is a derived operation for an untagged type then by
10795 -- definition it's not a dispatching operation (even if the parent
10796 -- operation was dispatching), so Check_Dispatching_Operation is not
10797 -- called in that case.
10799 if No (Derived_Type)
10800 or else Is_Tagged_Type (Derived_Type)
10802 Check_Dispatching_Operation (S, Empty);
10806 -- If this is a user-defined equality operator that is not a derived
10807 -- subprogram, create the corresponding inequality. If the operation is
10808 -- dispatching, the expansion is done elsewhere, and we do not create
10809 -- an explicit inequality operation.
10811 <<Check_Inequality>>
10812 if Chars (S) = Name_Op_Eq
10813 and then Etype (S) = Standard_Boolean
10814 and then Present (Parent (S))
10815 and then not Is_Dispatching_Operation (S)
10817 Make_Inequality_Operator (S);
10819 if Ada_Version >= Ada_2012 then
10820 Check_Untagged_Equality (S);
10823 end New_Overloaded_Entity;
10825 ---------------------
10826 -- Process_Formals --
10827 ---------------------
10829 procedure Process_Formals
10831 Related_Nod : Node_Id)
10833 Param_Spec : Node_Id;
10834 Formal : Entity_Id;
10835 Formal_Type : Entity_Id;
10839 Num_Out_Params : Nat := 0;
10840 First_Out_Param : Entity_Id := Empty;
10841 -- Used for setting Is_Only_Out_Parameter
10843 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10844 -- Determine whether an access type designates a type coming from a
10847 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10848 -- Check whether the default has a class-wide type. After analysis the
10849 -- default has the type of the formal, so we must also check explicitly
10850 -- for an access attribute.
10852 -------------------------------
10853 -- Designates_From_With_Type --
10854 -------------------------------
10856 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10857 Desig : Entity_Id := Typ;
10860 if Is_Access_Type (Desig) then
10861 Desig := Directly_Designated_Type (Desig);
10864 if Is_Class_Wide_Type (Desig) then
10865 Desig := Root_Type (Desig);
10869 Ekind (Desig) = E_Incomplete_Type and then From_With_Type (Desig);
10870 end Designates_From_With_Type;
10872 ---------------------------
10873 -- Is_Class_Wide_Default --
10874 ---------------------------
10876 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10878 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10879 or else (Nkind (D) = N_Attribute_Reference
10880 and then Attribute_Name (D) = Name_Access
10881 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10882 end Is_Class_Wide_Default;
10884 -- Start of processing for Process_Formals
10887 -- In order to prevent premature use of the formals in the same formal
10888 -- part, the Ekind is left undefined until all default expressions are
10889 -- analyzed. The Ekind is established in a separate loop at the end.
10891 Param_Spec := First (T);
10892 while Present (Param_Spec) loop
10893 Formal := Defining_Identifier (Param_Spec);
10894 Set_Never_Set_In_Source (Formal, True);
10895 Enter_Name (Formal);
10897 -- Case of ordinary parameters
10899 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10900 Find_Type (Parameter_Type (Param_Spec));
10901 Ptype := Parameter_Type (Param_Spec);
10903 if Ptype = Error then
10907 Formal_Type := Entity (Ptype);
10909 if Is_Incomplete_Type (Formal_Type)
10911 (Is_Class_Wide_Type (Formal_Type)
10912 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10914 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10915 -- primitive operations, as long as their completion is
10916 -- in the same declarative part. If in the private part
10917 -- this means that the type cannot be a Taft-amendment type.
10918 -- Check is done on package exit. For access to subprograms,
10919 -- the use is legal for Taft-amendment types.
10921 -- Ada 2012: tagged incomplete types are allowed as generic
10922 -- formal types. They do not introduce dependencies and the
10923 -- corresponding generic subprogram does not have a delayed
10924 -- freeze, because it does not need a freeze node.
10926 if Is_Tagged_Type (Formal_Type) then
10927 if Ekind (Scope (Current_Scope)) = E_Package
10928 and then not From_With_Type (Formal_Type)
10929 and then not Is_Generic_Type (Formal_Type)
10930 and then not Is_Class_Wide_Type (Formal_Type)
10933 (Parent (T), N_Access_Function_Definition,
10934 N_Access_Procedure_Definition)
10938 Private_Dependents (Base_Type (Formal_Type)));
10940 -- Freezing is delayed to ensure that Register_Prim
10941 -- will get called for this operation, which is needed
10942 -- in cases where static dispatch tables aren't built.
10943 -- (Note that the same is done for controlling access
10944 -- parameter cases in function Access_Definition.)
10946 Set_Has_Delayed_Freeze (Current_Scope);
10950 -- Special handling of Value_Type for CIL case
10952 elsif Is_Value_Type (Formal_Type) then
10955 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10956 N_Access_Procedure_Definition)
10958 -- AI05-0151: Tagged incomplete types are allowed in all
10959 -- formal parts. Untagged incomplete types are not allowed
10962 if Ada_Version >= Ada_2012 then
10963 if Is_Tagged_Type (Formal_Type) then
10966 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10971 ("invalid use of untagged incomplete type&",
10972 Ptype, Formal_Type);
10977 ("invalid use of incomplete type&",
10978 Param_Spec, Formal_Type);
10980 -- Further checks on the legality of incomplete types
10981 -- in formal parts are delayed until the freeze point
10982 -- of the enclosing subprogram or access to subprogram.
10986 elsif Ekind (Formal_Type) = E_Void then
10988 ("premature use of&",
10989 Parameter_Type (Param_Spec), Formal_Type);
10992 -- Ada 2012 (AI-142): Handle aliased parameters
10994 if Ada_Version >= Ada_2012
10995 and then Aliased_Present (Param_Spec)
10997 Set_Is_Aliased (Formal);
11000 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11001 -- declaration corresponding to the null-excluding type of the
11002 -- formal in the enclosing scope. Finally, replace the parameter
11003 -- type of the formal with the internal subtype.
11005 if Ada_Version >= Ada_2005
11006 and then Null_Exclusion_Present (Param_Spec)
11008 if not Is_Access_Type (Formal_Type) then
11010 ("`NOT NULL` allowed only for an access type", Param_Spec);
11013 if Can_Never_Be_Null (Formal_Type)
11014 and then Comes_From_Source (Related_Nod)
11017 ("`NOT NULL` not allowed (& already excludes null)",
11018 Param_Spec, Formal_Type);
11022 Create_Null_Excluding_Itype
11024 Related_Nod => Related_Nod,
11025 Scope_Id => Scope (Current_Scope));
11027 -- If the designated type of the itype is an itype that is
11028 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11029 -- on the access subtype, to prevent order-of-elaboration
11030 -- issues in the backend.
11033 -- type T is access procedure;
11034 -- procedure Op (O : not null T);
11036 if Is_Itype (Directly_Designated_Type (Formal_Type))
11038 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11040 Set_Has_Delayed_Freeze (Formal_Type);
11045 -- An access formal type
11049 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11051 -- No need to continue if we already notified errors
11053 if not Present (Formal_Type) then
11057 -- Ada 2005 (AI-254)
11060 AD : constant Node_Id :=
11061 Access_To_Subprogram_Definition
11062 (Parameter_Type (Param_Spec));
11064 if Present (AD) and then Protected_Present (AD) then
11066 Replace_Anonymous_Access_To_Protected_Subprogram
11072 Set_Etype (Formal, Formal_Type);
11074 -- Deal with default expression if present
11076 Default := Expression (Param_Spec);
11078 if Present (Default) then
11079 Check_SPARK_Restriction
11080 ("default expression is not allowed", Default);
11082 if Out_Present (Param_Spec) then
11084 ("default initialization only allowed for IN parameters",
11088 -- Do the special preanalysis of the expression (see section on
11089 -- "Handling of Default Expressions" in the spec of package Sem).
11091 Preanalyze_Spec_Expression (Default, Formal_Type);
11093 -- An access to constant cannot be the default for
11094 -- an access parameter that is an access to variable.
11096 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11097 and then not Is_Access_Constant (Formal_Type)
11098 and then Is_Access_Type (Etype (Default))
11099 and then Is_Access_Constant (Etype (Default))
11102 ("formal that is access to variable cannot be initialized " &
11103 "with an access-to-constant expression", Default);
11106 -- Check that the designated type of an access parameter's default
11107 -- is not a class-wide type unless the parameter's designated type
11108 -- is also class-wide.
11110 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11111 and then not Designates_From_With_Type (Formal_Type)
11112 and then Is_Class_Wide_Default (Default)
11113 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11116 ("access to class-wide expression not allowed here", Default);
11119 -- Check incorrect use of dynamically tagged expressions
11121 if Is_Tagged_Type (Formal_Type) then
11122 Check_Dynamically_Tagged_Expression
11124 Typ => Formal_Type,
11125 Related_Nod => Default);
11129 -- Ada 2005 (AI-231): Static checks
11131 if Ada_Version >= Ada_2005
11132 and then Is_Access_Type (Etype (Formal))
11133 and then Can_Never_Be_Null (Etype (Formal))
11135 Null_Exclusion_Static_Checks (Param_Spec);
11142 -- If this is the formal part of a function specification, analyze the
11143 -- subtype mark in the context where the formals are visible but not
11144 -- yet usable, and may hide outer homographs.
11146 if Nkind (Related_Nod) = N_Function_Specification then
11147 Analyze_Return_Type (Related_Nod);
11150 -- Now set the kind (mode) of each formal
11152 Param_Spec := First (T);
11153 while Present (Param_Spec) loop
11154 Formal := Defining_Identifier (Param_Spec);
11155 Set_Formal_Mode (Formal);
11157 if Ekind (Formal) = E_In_Parameter then
11158 Set_Default_Value (Formal, Expression (Param_Spec));
11160 if Present (Expression (Param_Spec)) then
11161 Default := Expression (Param_Spec);
11163 if Is_Scalar_Type (Etype (Default)) then
11164 if Nkind (Parameter_Type (Param_Spec)) /=
11165 N_Access_Definition
11167 Formal_Type := Entity (Parameter_Type (Param_Spec));
11171 (Related_Nod, Parameter_Type (Param_Spec));
11174 Apply_Scalar_Range_Check (Default, Formal_Type);
11178 elsif Ekind (Formal) = E_Out_Parameter then
11179 Num_Out_Params := Num_Out_Params + 1;
11181 if Num_Out_Params = 1 then
11182 First_Out_Param := Formal;
11185 elsif Ekind (Formal) = E_In_Out_Parameter then
11186 Num_Out_Params := Num_Out_Params + 1;
11189 -- Skip remaining processing if formal type was in error
11191 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11192 goto Next_Parameter;
11195 -- Force call by reference if aliased
11197 if Is_Aliased (Formal) then
11198 Set_Mechanism (Formal, By_Reference);
11200 -- Warn if user asked this to be passed by copy
11202 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11204 ("cannot pass aliased parameter & by copy?", Formal);
11207 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11209 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11210 Set_Mechanism (Formal, By_Copy);
11212 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11213 Set_Mechanism (Formal, By_Reference);
11220 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11221 Set_Is_Only_Out_Parameter (First_Out_Param);
11223 end Process_Formals;
11229 procedure Process_PPCs
11231 Spec_Id : Entity_Id;
11232 Body_Id : Entity_Id)
11234 Loc : constant Source_Ptr := Sloc (N);
11238 Designator : Entity_Id;
11239 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11241 Precond : Node_Id := Empty;
11242 -- Set non-Empty if we prepend precondition to the declarations. This
11243 -- is used to hook up inherited preconditions (adding the condition
11244 -- expression with OR ELSE, and adding the message).
11246 Inherited_Precond : Node_Id;
11247 -- Precondition inherited from parent subprogram
11249 Inherited : constant Subprogram_List :=
11250 Inherited_Subprograms (Spec_Id);
11251 -- List of subprograms inherited by this subprogram
11253 Plist : List_Id := No_List;
11254 -- List of generated postconditions
11256 procedure Check_Access_Invariants (E : Entity_Id);
11257 -- If the subprogram returns an access to a type with invariants, or
11258 -- has access parameters whose designated type has an invariant, then
11259 -- under the same visibility conditions as for other invariant checks,
11260 -- the type invariant must be applied to the returned value.
11262 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id);
11263 -- Given pragma Contract_Cases CCs, create the circuitry needed to
11264 -- evaluate case guards and trigger consequence expressions. Subp_Id
11265 -- denotes the related subprogram.
11267 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11268 -- Prag contains an analyzed precondition or postcondition pragma. This
11269 -- function copies the pragma, changes it to the corresponding Check
11270 -- pragma and returns the Check pragma as the result. If Pspec is non-
11271 -- empty, this is the case of inheriting a PPC, where we must change
11272 -- references to parameters of the inherited subprogram to point to the
11273 -- corresponding parameters of the current subprogram.
11275 procedure Insert_After_Last_Declaration (Nod : Node_Id);
11276 -- Insert node Nod after the last declaration of the context
11278 function Invariants_Or_Predicates_Present return Boolean;
11279 -- Determines if any invariants or predicates are present for any OUT
11280 -- or IN OUT parameters of the subprogram, or (for a function) if the
11281 -- return value has an invariant.
11283 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11284 -- T is the entity for a private type for which invariants are defined.
11285 -- This function returns True if the procedure corresponding to the
11286 -- value of Designator is a public procedure from the point of view of
11287 -- this type (i.e. its spec is in the visible part of the package that
11288 -- contains the declaration of the private type). A True value means
11289 -- that an invariant check is required (for an IN OUT parameter, or
11290 -- the returned value of a function.
11292 -----------------------------
11293 -- Check_Access_Invariants --
11294 -----------------------------
11296 procedure Check_Access_Invariants (E : Entity_Id) is
11302 if Is_Access_Type (Etype (E))
11303 and then not Is_Access_Constant (Etype (E))
11305 Typ := Designated_Type (Etype (E));
11307 if Has_Invariants (Typ)
11308 and then Present (Invariant_Procedure (Typ))
11309 and then Is_Public_Subprogram_For (Typ)
11312 Make_Explicit_Dereference (Loc,
11313 Prefix => New_Occurrence_Of (E, Loc));
11314 Set_Etype (Obj, Typ);
11316 Call := Make_Invariant_Call (Obj);
11319 Make_If_Statement (Loc,
11322 Left_Opnd => Make_Null (Loc),
11323 Right_Opnd => New_Occurrence_Of (E, Loc)),
11324 Then_Statements => New_List (Call)));
11327 end Check_Access_Invariants;
11329 ---------------------------
11330 -- Expand_Contract_Cases --
11331 ---------------------------
11333 -- Pragma Contract_Cases is expanded in the following manner:
11336 -- Flag_1 : Boolean := False;
11338 -- Flag_N : Boolean := False;
11339 -- Flag_N+1 : Boolean := False; -- when "others" present
11340 -- Count : Natural := 0;
11342 -- <preconditions (if any)>
11344 -- if Case_Guard_1 then
11346 -- Count := Count + 1;
11349 -- if Case_Guard_N then
11351 -- Count := Count + 1;
11354 -- if Count = 0 then
11355 -- raise Assertion_Error with "xxx contract cases incomplete";
11357 -- Flag_N+1 := True; -- when "others" present
11359 -- elsif Count > 1 then
11361 -- Str0 : constant String :=
11362 -- "contract cases overlap for subprogram ABC";
11363 -- Str1 : constant String :=
11365 -- Str0 & "case guard at xxx evaluates to True"
11367 -- StrN : constant String :=
11369 -- StrN-1 & "case guard at xxx evaluates to True"
11372 -- raise Assertion_Error with StrN;
11376 -- procedure _Postconditions is
11378 -- <postconditions (if any)>
11380 -- if Flag_1 and then not Consequence_1 then
11381 -- raise Assertion_Error with "failed contract case at xxx";
11384 -- if Flag_N[+1] and then not Consequence_N[+1] then
11385 -- raise Assertion_Error with "failed contract case at xxx";
11387 -- end _Postconditions;
11392 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id) is
11393 Loc : constant Source_Ptr := Sloc (CCs);
11395 procedure Case_Guard_Error
11398 Error_Loc : Source_Ptr;
11399 Msg : in out Entity_Id);
11400 -- Given a declarative list Decls, status flag Flag, the location of
11401 -- the error and a string Msg, construct the following check:
11402 -- Msg : constant String :=
11404 -- Msg & "case guard at Error_Loc evaluates to True"
11406 -- The resulting code is added to Decls
11408 procedure Consequence_Error
11409 (Checks : in out Node_Id;
11412 -- Given an if statement Checks, status flag Flag and a consequence
11413 -- Conseq, construct the following check:
11414 -- [els]if Flag and then not Conseq then
11415 -- raise Assertion_Error
11416 -- with "failed contract case at Sloc (Conseq)";
11418 -- The resulting code is added to Checks
11420 function Declaration_Of (Id : Entity_Id) return Node_Id;
11421 -- Given the entity Id of a boolean flag, generate:
11422 -- Id : Boolean := False;
11424 function Increment (Id : Entity_Id) return Node_Id;
11425 -- Given the entity Id of a numerical variable, generate:
11428 function Set (Id : Entity_Id) return Node_Id;
11429 -- Given the entity Id of a boolean variable, generate:
11432 ----------------------
11433 -- Case_Guard_Error --
11434 ----------------------
11436 procedure Case_Guard_Error
11439 Error_Loc : Source_Ptr;
11440 Msg : in out Entity_Id)
11442 New_Line : constant Character := Character'Val (10);
11443 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
11447 Store_String_Char (New_Line);
11448 Store_String_Chars (" case guard at ");
11449 Store_String_Chars (Build_Location_String (Error_Loc));
11450 Store_String_Chars (" evaluates to True");
11453 -- New_Msg : constant String :=
11455 -- Msg & "case guard at Error_Loc evaluates to True"
11459 Make_Object_Declaration (Loc,
11460 Defining_Identifier => New_Msg,
11461 Constant_Present => True,
11462 Object_Definition => New_Reference_To (Standard_String, Loc),
11464 Make_If_Expression (Loc,
11465 Expressions => New_List (
11466 New_Reference_To (Flag, Loc),
11468 Make_Op_Concat (Loc,
11469 Left_Opnd => New_Reference_To (Msg, Loc),
11470 Right_Opnd => Make_String_Literal (Loc, End_String)),
11472 New_Reference_To (Msg, Loc)))));
11475 end Case_Guard_Error;
11477 -----------------------
11478 -- Consequence_Error --
11479 -----------------------
11481 procedure Consequence_Error
11482 (Checks : in out Node_Id;
11491 -- Flag and then not Conseq
11494 Make_And_Then (Loc,
11495 Left_Opnd => New_Reference_To (Flag, Loc),
11498 Right_Opnd => Relocate_Node (Conseq)));
11501 -- raise Assertion_Error
11502 -- with "failed contract case at Sloc (Conseq)";
11505 Store_String_Chars ("failed contract case at ");
11506 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
11509 Make_Procedure_Call_Statement (Loc,
11511 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11512 Parameter_Associations => New_List (
11513 Make_String_Literal (Loc, End_String)));
11515 if No (Checks) then
11517 Make_If_Statement (Loc,
11519 Then_Statements => New_List (Error));
11522 if No (Elsif_Parts (Checks)) then
11523 Set_Elsif_Parts (Checks, New_List);
11526 Append_To (Elsif_Parts (Checks),
11527 Make_Elsif_Part (Loc,
11529 Then_Statements => New_List (Error)));
11531 end Consequence_Error;
11533 --------------------
11534 -- Declaration_Of --
11535 --------------------
11537 function Declaration_Of (Id : Entity_Id) return Node_Id is
11540 Make_Object_Declaration (Loc,
11541 Defining_Identifier => Id,
11542 Object_Definition =>
11543 New_Reference_To (Standard_Boolean, Loc),
11545 New_Reference_To (Standard_False, Loc));
11546 end Declaration_Of;
11552 function Increment (Id : Entity_Id) return Node_Id is
11555 Make_Assignment_Statement (Loc,
11556 Name => New_Reference_To (Id, Loc),
11559 Left_Opnd => New_Reference_To (Id, Loc),
11560 Right_Opnd => Make_Integer_Literal (Loc, 1)));
11567 function Set (Id : Entity_Id) return Node_Id is
11570 Make_Assignment_Statement (Loc,
11571 Name => New_Reference_To (Id, Loc),
11572 Expression => New_Reference_To (Standard_True, Loc));
11577 Aggr : constant Node_Id :=
11579 (Pragma_Argument_Associations (CCs)));
11580 Decls : constant List_Id := Declarations (N);
11581 Multiple_PCs : constant Boolean :=
11582 List_Length (Component_Associations (Aggr)) > 1;
11583 Case_Guard : Node_Id;
11584 CG_Checks : Node_Id;
11585 CG_Stmts : List_Id;
11587 Conseq_Checks : Node_Id := Empty;
11589 Error_Decls : List_Id;
11591 Msg_Str : Entity_Id;
11592 Others_Flag : Entity_Id := Empty;
11593 Post_Case : Node_Id;
11595 -- Start of processing for Expand_Contract_Cases
11598 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
11599 -- already been rewritten as a Null statement.
11601 if Is_Ignored (CCs) then
11605 -- Create the counter which tracks the number of case guards that
11606 -- evaluate to True.
11608 -- Count : Natural := 0;
11610 Count := Make_Temporary (Loc, 'C');
11613 Make_Object_Declaration (Loc,
11614 Defining_Identifier => Count,
11615 Object_Definition => New_Reference_To (Standard_Natural, Loc),
11616 Expression => Make_Integer_Literal (Loc, 0)));
11618 -- Create the base error message for multiple overlapping case
11621 -- Msg_Str : constant String :=
11622 -- "contract cases overlap for subprogram Subp_Id";
11624 if Multiple_PCs then
11625 Msg_Str := Make_Temporary (Loc, 'S');
11628 Store_String_Chars ("contract cases overlap for subprogram ");
11629 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
11631 Error_Decls := New_List (
11632 Make_Object_Declaration (Loc,
11633 Defining_Identifier => Msg_Str,
11634 Constant_Present => True,
11635 Object_Definition => New_Reference_To (Standard_String, Loc),
11636 Expression => Make_String_Literal (Loc, End_String)));
11639 -- Process individual post cases
11641 Post_Case := First (Component_Associations (Aggr));
11642 while Present (Post_Case) loop
11643 Case_Guard := First (Choices (Post_Case));
11644 Conseq := Expression (Post_Case);
11646 -- The "others" choice requires special processing
11648 if Nkind (Case_Guard) = N_Others_Choice then
11649 Others_Flag := Make_Temporary (Loc, 'F');
11650 Prepend_To (Decls, Declaration_Of (Others_Flag));
11652 -- Check possible overlap between a case guard and "others"
11654 if Multiple_PCs then
11656 (Decls => Error_Decls,
11657 Flag => Others_Flag,
11658 Error_Loc => Sloc (Case_Guard),
11662 -- Check the corresponding consequence of "others"
11665 (Checks => Conseq_Checks,
11666 Flag => Others_Flag,
11669 -- Regular post case
11672 -- Create the flag which tracks the state of its associated
11675 Flag := Make_Temporary (Loc, 'F');
11676 Prepend_To (Decls, Declaration_Of (Flag));
11678 -- The flag is set when the case guard is evaluated to True
11679 -- if Case_Guard then
11681 -- Count := Count + 1;
11685 Make_If_Statement (Loc,
11686 Condition => Relocate_Node (Case_Guard),
11687 Then_Statements => New_List (
11689 Increment (Count))));
11691 -- Check whether this case guard overlaps with another case
11694 if Multiple_PCs then
11696 (Decls => Error_Decls,
11698 Error_Loc => Sloc (Case_Guard),
11702 -- The corresponding consequence of the case guard which
11703 -- evaluated to True must hold on exit from the subprogram.
11705 Consequence_Error (Conseq_Checks, Flag, Conseq);
11711 -- Raise Assertion_Error when none of the case guards evaluate to
11712 -- True. The only exception is when we have "others", in which case
11713 -- there is no error because "others" acts as a default True.
11718 if Present (Others_Flag) then
11719 CG_Stmts := New_List (Set (Others_Flag));
11722 -- raise Assertion_Error with "xxx contract cases incomplete";
11726 Store_String_Chars (Build_Location_String (Loc));
11727 Store_String_Chars (" contract cases incomplete");
11729 CG_Stmts := New_List (
11730 Make_Procedure_Call_Statement (Loc,
11732 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11733 Parameter_Associations => New_List (
11734 Make_String_Literal (Loc, End_String))));
11738 Make_If_Statement (Loc,
11741 Left_Opnd => New_Reference_To (Count, Loc),
11742 Right_Opnd => Make_Integer_Literal (Loc, 0)),
11743 Then_Statements => CG_Stmts);
11745 -- Detect a possible failure due to several case guards evaluating to
11749 -- elsif Count > 0 then
11753 -- raise Assertion_Error with <Msg_Str>;
11756 if Multiple_PCs then
11757 Set_Elsif_Parts (CG_Checks, New_List (
11758 Make_Elsif_Part (Loc,
11761 Left_Opnd => New_Reference_To (Count, Loc),
11762 Right_Opnd => Make_Integer_Literal (Loc, 1)),
11764 Then_Statements => New_List (
11765 Make_Block_Statement (Loc,
11766 Declarations => Error_Decls,
11767 Handled_Statement_Sequence =>
11768 Make_Handled_Sequence_Of_Statements (Loc,
11769 Statements => New_List (
11770 Make_Procedure_Call_Statement (Loc,
11773 (RTE (RE_Raise_Assert_Failure), Loc),
11774 Parameter_Associations => New_List (
11775 New_Reference_To (Msg_Str, Loc))))))))));
11778 Append_To (Decls, CG_Checks);
11780 -- Raise Assertion_Error when the corresponding consequence of a case
11781 -- guard that evaluated to True fails.
11787 Append_To (Plist, Conseq_Checks);
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 -- Insert_After_Last_Declaration --
11900 -----------------------------------
11902 procedure Insert_After_Last_Declaration (Nod : Node_Id) is
11903 Decls : constant List_Id := Declarations (N);
11907 Set_Declarations (N, New_List (Nod));
11909 Append_To (Decls, Nod);
11911 end Insert_After_Last_Declaration;
11913 --------------------------------------
11914 -- Invariants_Or_Predicates_Present --
11915 --------------------------------------
11917 function Invariants_Or_Predicates_Present return Boolean is
11918 Formal : Entity_Id;
11921 -- Check function return result. If result is an access type there
11922 -- may be invariants on the designated type.
11924 if Ekind (Designator) /= E_Procedure
11925 and then Has_Invariants (Etype (Designator))
11929 elsif Ekind (Designator) /= E_Procedure
11930 and then Is_Access_Type (Etype (Designator))
11931 and then Has_Invariants (Designated_Type (Etype (Designator)))
11936 -- Check parameters
11938 Formal := First_Formal (Designator);
11939 while Present (Formal) loop
11940 if Ekind (Formal) /= E_In_Parameter
11941 and then (Has_Invariants (Etype (Formal))
11942 or else Present (Predicate_Function (Etype (Formal))))
11946 elsif Is_Access_Type (Etype (Formal))
11947 and then Has_Invariants (Designated_Type (Etype (Formal)))
11952 Next_Formal (Formal);
11956 end Invariants_Or_Predicates_Present;
11958 ------------------------------
11959 -- Is_Public_Subprogram_For --
11960 ------------------------------
11962 -- The type T is a private type, its declaration is therefore in
11963 -- the list of public declarations of some package. The test for a
11964 -- public subprogram is that its declaration is in this same list
11965 -- of declarations for the same package (note that all the public
11966 -- declarations are in one list, and all the private declarations
11967 -- in another, so this deals with the public/private distinction).
11969 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
11970 DD : constant Node_Id := Unit_Declaration_Node (Designator);
11971 -- The subprogram declaration for the subprogram in question
11973 TL : constant List_Id :=
11974 Visible_Declarations
11975 (Specification (Unit_Declaration_Node (Scope (T))));
11976 -- The list of declarations containing the private declaration of
11977 -- the type. We know it is a private type, so we know its scope is
11978 -- the package in question, and we know it must be in the visible
11979 -- declarations of this package.
11982 -- If the subprogram declaration is not a list member, it must be
11983 -- an Init_Proc, in which case we want to consider it to be a
11984 -- public subprogram, since we do get initializations to deal with.
11985 -- Other internally generated subprograms are not public.
11987 if not Is_List_Member (DD)
11988 and then Is_Init_Proc (Defining_Entity (DD))
11992 -- The declaration may have been generated for an expression function
11993 -- so check whether that function comes from source.
11995 elsif not Comes_From_Source (DD)
11997 (Nkind (Original_Node (DD)) /= N_Expression_Function
11998 or else not Comes_From_Source (Defining_Entity (DD)))
12002 -- Otherwise we test whether the subprogram is declared in the
12003 -- visible declarations of the package containing the type.
12006 return TL = List_Containing (DD);
12008 end Is_Public_Subprogram_For;
12010 -- Start of processing for Process_PPCs
12013 -- Capture designator from spec if present, else from body
12015 if Present (Spec_Id) then
12016 Designator := Spec_Id;
12018 Designator := Body_Id;
12021 -- Internally generated subprograms, such as type-specific functions,
12022 -- don't get assertion checks.
12024 if Get_TSS_Name (Designator) /= TSS_Null then
12028 -- Grab preconditions from spec
12030 if Present (Spec_Id) then
12032 -- Loop through PPC pragmas from spec. Note that preconditions from
12033 -- the body will be analyzed and converted when we scan the body
12034 -- declarations below.
12036 Prag := Spec_PPC_List (Contract (Spec_Id));
12037 while Present (Prag) loop
12038 if Pragma_Name (Prag) = Name_Precondition then
12040 -- For Pre (or Precondition pragma), we simply prepend the
12041 -- pragma to the list of declarations right away so that it
12042 -- will be executed at the start of the procedure. Note that
12043 -- this processing reverses the order of the list, which is
12044 -- what we want since new entries were chained to the head of
12045 -- the list. There can be more than one precondition when we
12046 -- use pragma Precondition.
12048 if not Class_Present (Prag) then
12049 Prepend (Grab_PPC, Declarations (N));
12051 -- For Pre'Class there can only be one pragma, and we save
12052 -- it in Precond for now. We will add inherited Pre'Class
12053 -- stuff before inserting this pragma in the declarations.
12055 Precond := Grab_PPC;
12059 Prag := Next_Pragma (Prag);
12062 -- Now deal with inherited preconditions
12064 for J in Inherited'Range loop
12065 Prag := Spec_PPC_List (Contract (Inherited (J)));
12067 while Present (Prag) loop
12068 if Pragma_Name (Prag) = Name_Precondition
12069 and then Class_Present (Prag)
12071 Inherited_Precond := Grab_PPC (Inherited (J));
12073 -- No precondition so far, so establish this as the first
12075 if No (Precond) then
12076 Precond := Inherited_Precond;
12078 -- Here we already have a precondition, add inherited one
12081 -- Add new precondition to old one using OR ELSE
12084 New_Expr : constant Node_Id :=
12088 (Pragma_Argument_Associations
12089 (Inherited_Precond))));
12090 Old_Expr : constant Node_Id :=
12094 (Pragma_Argument_Associations
12098 if Paren_Count (Old_Expr) = 0 then
12099 Set_Paren_Count (Old_Expr, 1);
12102 if Paren_Count (New_Expr) = 0 then
12103 Set_Paren_Count (New_Expr, 1);
12107 Make_Or_Else (Sloc (Old_Expr),
12108 Left_Opnd => Relocate_Node (Old_Expr),
12109 Right_Opnd => New_Expr));
12112 -- Add new message in the form:
12114 -- failed precondition from bla
12115 -- also failed inherited precondition from bla
12118 -- Skip this if exception locations are suppressed
12120 if not Exception_Locations_Suppressed then
12122 New_Msg : constant Node_Id :=
12125 (Pragma_Argument_Associations
12126 (Inherited_Precond)));
12127 Old_Msg : constant Node_Id :=
12130 (Pragma_Argument_Associations
12133 Start_String (Strval (Old_Msg));
12134 Store_String_Chars (ASCII.LF & " also ");
12135 Store_String_Chars (Strval (New_Msg));
12136 Set_Strval (Old_Msg, End_String);
12142 Prag := Next_Pragma (Prag);
12146 -- If we have built a precondition for Pre'Class (including any
12147 -- Pre'Class aspects inherited from parent subprograms), then we
12148 -- insert this composite precondition at this stage.
12150 if Present (Precond) then
12151 Prepend (Precond, Declarations (N));
12155 -- Build postconditions procedure if needed and prepend the following
12156 -- declaration to the start of the declarations for the subprogram.
12158 -- procedure _postconditions [(_Result : resulttype)] is
12160 -- pragma Check (Postcondition, condition [,message]);
12161 -- pragma Check (Postcondition, condition [,message]);
12163 -- Invariant_Procedure (_Result) ...
12164 -- Invariant_Procedure (Arg1)
12168 -- First we deal with the postconditions in the body
12170 if Is_Non_Empty_List (Declarations (N)) then
12172 -- Loop through declarations
12174 Prag := First (Declarations (N));
12175 while Present (Prag) loop
12176 if Nkind (Prag) = N_Pragma then
12177 Check_Applicable_Policy (Prag);
12179 -- If pragma, capture if postconditions enabled, else ignore
12181 if Pragma_Name (Prag) = Name_Postcondition
12182 and then not Is_Ignored (Prag)
12184 if Plist = No_List then
12185 Plist := Empty_List;
12190 -- If expansion is disabled, as in a generic unit, save
12191 -- pragma for later expansion.
12193 if not Expander_Active then
12194 Prepend (Grab_PPC, Declarations (N));
12196 Append (Grab_PPC, Plist);
12202 -- Not a pragma, if comes from source, then end scan
12204 elsif Comes_From_Source (Prag) then
12207 -- Skip stuff not coming from source
12215 -- Now deal with any postconditions from the spec
12217 if Present (Spec_Id) then
12218 Spec_Postconditions : declare
12219 procedure Process_Contract_Cases (Spec : Node_Id);
12220 -- This processes the Spec_CTC_List from Spec, processing any
12221 -- contract-cases from the list. The caller has checked that
12222 -- Spec_CTC_List is non-Empty.
12224 procedure Process_Post_Conditions
12227 -- This processes the Spec_PPC_List from Spec, processing any
12228 -- postconditions from the list. If Class is True, then only
12229 -- postconditions marked with Class_Present are considered.
12230 -- The caller has checked that Spec_PPC_List is non-Empty.
12232 ----------------------------
12233 -- Process_Contract_Cases --
12234 ----------------------------
12236 procedure Process_Contract_Cases (Spec : Node_Id) is
12238 -- Loop through Contract_Cases pragmas from spec
12240 Prag := Spec_CTC_List (Contract (Spec));
12242 if Pragma_Name (Prag) = Name_Contract_Cases then
12243 Expand_Contract_Cases (Prag, Spec_Id);
12246 Prag := Next_Pragma (Prag);
12247 exit when No (Prag);
12249 end Process_Contract_Cases;
12251 -----------------------------
12252 -- Process_Post_Conditions --
12253 -----------------------------
12255 procedure Process_Post_Conditions
12268 -- Loop through PPC pragmas from spec
12270 Prag := Spec_PPC_List (Contract (Spec));
12272 if Pragma_Name (Prag) = Name_Postcondition
12273 and then (not Class or else Class_Present (Prag))
12275 if Plist = No_List then
12276 Plist := Empty_List;
12279 if not Expander_Active then
12280 Prepend (Grab_PPC (Pspec), Declarations (N));
12282 Append (Grab_PPC (Pspec), Plist);
12286 Prag := Next_Pragma (Prag);
12287 exit when No (Prag);
12289 end Process_Post_Conditions;
12291 -- Start of processing for Spec_Postconditions
12294 -- Process postconditions expressed as contract-cases
12296 if Present (Spec_CTC_List (Contract (Spec_Id))) then
12297 Process_Contract_Cases (Spec_Id);
12300 -- Process spec postconditions
12302 if Present (Spec_PPC_List (Contract (Spec_Id))) then
12303 Process_Post_Conditions (Spec_Id, Class => False);
12306 -- Process inherited postconditions
12308 for J in Inherited'Range loop
12309 if Present (Spec_PPC_List (Contract (Inherited (J)))) then
12310 Process_Post_Conditions (Inherited (J), Class => True);
12313 end Spec_Postconditions;
12316 -- If we had any postconditions and expansion is enabled, or if the
12317 -- subprogram has invariants, then build the _Postconditions procedure.
12319 if (Present (Plist) or else Invariants_Or_Predicates_Present)
12320 and then Expander_Active
12323 Plist := Empty_List;
12326 -- Special processing for function return
12328 if Ekind (Designator) /= E_Procedure then
12330 Rent : constant Entity_Id :=
12331 Make_Defining_Identifier (Loc, Name_uResult);
12332 Ftyp : constant Entity_Id := Etype (Designator);
12335 Set_Etype (Rent, Ftyp);
12337 -- Add argument for return
12341 Make_Parameter_Specification (Loc,
12342 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
12343 Defining_Identifier => Rent));
12345 -- Add invariant call if returning type with invariants and
12346 -- this is a public function, i.e. a function declared in the
12347 -- visible part of the package defining the private type.
12349 if Has_Invariants (Etype (Rent))
12350 and then Present (Invariant_Procedure (Etype (Rent)))
12351 and then Is_Public_Subprogram_For (Etype (Rent))
12354 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
12357 -- Same if return value is an access to type with invariants
12359 Check_Access_Invariants (Rent);
12362 -- Procedure rather than a function
12368 -- Add invariant calls and predicate calls for parameters. Note that
12369 -- this is done for functions as well, since in Ada 2012 they can
12370 -- have IN OUT args.
12373 Formal : Entity_Id;
12377 Formal := First_Formal (Designator);
12378 while Present (Formal) loop
12379 if Ekind (Formal) /= E_In_Parameter
12380 or else Is_Access_Type (Etype (Formal))
12382 Ftype := Etype (Formal);
12384 if Has_Invariants (Ftype)
12385 and then Present (Invariant_Procedure (Ftype))
12386 and then Is_Public_Subprogram_For (Ftype)
12389 Make_Invariant_Call
12390 (New_Occurrence_Of (Formal, Loc)));
12393 Check_Access_Invariants (Formal);
12395 if Present (Predicate_Function (Ftype)) then
12397 Make_Predicate_Check
12398 (Ftype, New_Occurrence_Of (Formal, Loc)));
12402 Next_Formal (Formal);
12406 -- Build and insert postcondition procedure
12409 Post_Proc : constant Entity_Id :=
12410 Make_Defining_Identifier (Loc,
12411 Chars => Name_uPostconditions);
12412 -- The entity for the _Postconditions procedure
12415 -- Insert the corresponding body of a post condition pragma after
12416 -- the last declaration of the context. This ensures that the body
12417 -- will not cause any premature freezing as it may mention types:
12419 -- procedure Proc (Obj : Array_Typ) is
12420 -- procedure _postconditions is
12423 -- end _postconditions;
12425 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12428 -- In the example above, Obj is of type T but the incorrect
12429 -- placement of _postconditions will cause a crash in gigi due to
12430 -- an out of order reference. The body of _postconditions must be
12431 -- placed after the declaration of Temp to preserve correct
12434 Insert_After_Last_Declaration (
12435 Make_Subprogram_Body (Loc,
12437 Make_Procedure_Specification (Loc,
12438 Defining_Unit_Name => Post_Proc,
12439 Parameter_Specifications => Parms),
12441 Declarations => Empty_List,
12443 Handled_Statement_Sequence =>
12444 Make_Handled_Sequence_Of_Statements (Loc,
12445 Statements => Plist)));
12447 Set_Ekind (Post_Proc, E_Procedure);
12449 -- If this is a procedure, set the Postcondition_Proc attribute on
12450 -- the proper defining entity for the subprogram.
12452 if Ekind (Designator) = E_Procedure then
12453 Set_Postcondition_Proc (Designator, Post_Proc);
12457 Set_Has_Postconditions (Designator);
12461 ----------------------------
12462 -- Reference_Body_Formals --
12463 ----------------------------
12465 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12470 if Error_Posted (Spec) then
12474 -- Iterate over both lists. They may be of different lengths if the two
12475 -- specs are not conformant.
12477 Fs := First_Formal (Spec);
12478 Fb := First_Formal (Bod);
12479 while Present (Fs) and then Present (Fb) loop
12480 Generate_Reference (Fs, Fb, 'b');
12482 if Style_Check then
12483 Style.Check_Identifier (Fb, Fs);
12486 Set_Spec_Entity (Fb, Fs);
12487 Set_Referenced (Fs, False);
12491 end Reference_Body_Formals;
12493 -------------------------
12494 -- Set_Actual_Subtypes --
12495 -------------------------
12497 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12499 Formal : Entity_Id;
12501 First_Stmt : Node_Id := Empty;
12502 AS_Needed : Boolean;
12505 -- If this is an empty initialization procedure, no need to create
12506 -- actual subtypes (small optimization).
12508 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12512 Formal := First_Formal (Subp);
12513 while Present (Formal) loop
12514 T := Etype (Formal);
12516 -- We never need an actual subtype for a constrained formal
12518 if Is_Constrained (T) then
12519 AS_Needed := False;
12521 -- If we have unknown discriminants, then we do not need an actual
12522 -- subtype, or more accurately we cannot figure it out! Note that
12523 -- all class-wide types have unknown discriminants.
12525 elsif Has_Unknown_Discriminants (T) then
12526 AS_Needed := False;
12528 -- At this stage we have an unconstrained type that may need an
12529 -- actual subtype. For sure the actual subtype is needed if we have
12530 -- an unconstrained array type.
12532 elsif Is_Array_Type (T) then
12535 -- The only other case needing an actual subtype is an unconstrained
12536 -- record type which is an IN parameter (we cannot generate actual
12537 -- subtypes for the OUT or IN OUT case, since an assignment can
12538 -- change the discriminant values. However we exclude the case of
12539 -- initialization procedures, since discriminants are handled very
12540 -- specially in this context, see the section entitled "Handling of
12541 -- Discriminants" in Einfo.
12543 -- We also exclude the case of Discrim_SO_Functions (functions used
12544 -- in front end layout mode for size/offset values), since in such
12545 -- functions only discriminants are referenced, and not only are such
12546 -- subtypes not needed, but they cannot always be generated, because
12547 -- of order of elaboration issues.
12549 elsif Is_Record_Type (T)
12550 and then Ekind (Formal) = E_In_Parameter
12551 and then Chars (Formal) /= Name_uInit
12552 and then not Is_Unchecked_Union (T)
12553 and then not Is_Discrim_SO_Function (Subp)
12557 -- All other cases do not need an actual subtype
12560 AS_Needed := False;
12563 -- Generate actual subtypes for unconstrained arrays and
12564 -- unconstrained discriminated records.
12567 if Nkind (N) = N_Accept_Statement then
12569 -- If expansion is active, the formal is replaced by a local
12570 -- variable that renames the corresponding entry of the
12571 -- parameter block, and it is this local variable that may
12572 -- require an actual subtype.
12574 if Full_Expander_Active then
12575 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12577 Decl := Build_Actual_Subtype (T, Formal);
12580 if Present (Handled_Statement_Sequence (N)) then
12582 First (Statements (Handled_Statement_Sequence (N)));
12583 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12584 Mark_Rewrite_Insertion (Decl);
12586 -- If the accept statement has no body, there will be no
12587 -- reference to the actuals, so no need to compute actual
12594 Decl := Build_Actual_Subtype (T, Formal);
12595 Prepend (Decl, Declarations (N));
12596 Mark_Rewrite_Insertion (Decl);
12599 -- The declaration uses the bounds of an existing object, and
12600 -- therefore needs no constraint checks.
12602 Analyze (Decl, Suppress => All_Checks);
12604 -- We need to freeze manually the generated type when it is
12605 -- inserted anywhere else than in a declarative part.
12607 if Present (First_Stmt) then
12608 Insert_List_Before_And_Analyze (First_Stmt,
12609 Freeze_Entity (Defining_Identifier (Decl), N));
12612 if Nkind (N) = N_Accept_Statement
12613 and then Full_Expander_Active
12615 Set_Actual_Subtype (Renamed_Object (Formal),
12616 Defining_Identifier (Decl));
12618 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12622 Next_Formal (Formal);
12624 end Set_Actual_Subtypes;
12626 ---------------------
12627 -- Set_Formal_Mode --
12628 ---------------------
12630 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12631 Spec : constant Node_Id := Parent (Formal_Id);
12634 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12635 -- since we ensure that corresponding actuals are always valid at the
12636 -- point of the call.
12638 if Out_Present (Spec) then
12639 if Ekind (Scope (Formal_Id)) = E_Function
12640 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
12642 -- [IN] OUT parameters allowed for functions in Ada 2012
12644 if Ada_Version >= Ada_2012 then
12646 -- Even in Ada 2012 operators can only have IN parameters
12648 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12649 Error_Msg_N ("operators can only have IN parameters", Spec);
12652 if In_Present (Spec) then
12653 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12655 Set_Ekind (Formal_Id, E_Out_Parameter);
12658 -- But not in earlier versions of Ada
12661 Error_Msg_N ("functions can only have IN parameters", Spec);
12662 Set_Ekind (Formal_Id, E_In_Parameter);
12665 elsif In_Present (Spec) then
12666 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12669 Set_Ekind (Formal_Id, E_Out_Parameter);
12670 Set_Never_Set_In_Source (Formal_Id, True);
12671 Set_Is_True_Constant (Formal_Id, False);
12672 Set_Current_Value (Formal_Id, Empty);
12676 Set_Ekind (Formal_Id, E_In_Parameter);
12679 -- Set Is_Known_Non_Null for access parameters since the language
12680 -- guarantees that access parameters are always non-null. We also set
12681 -- Can_Never_Be_Null, since there is no way to change the value.
12683 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12685 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12686 -- null; In Ada 2005, only if then null_exclusion is explicit.
12688 if Ada_Version < Ada_2005
12689 or else Can_Never_Be_Null (Etype (Formal_Id))
12691 Set_Is_Known_Non_Null (Formal_Id);
12692 Set_Can_Never_Be_Null (Formal_Id);
12695 -- Ada 2005 (AI-231): Null-exclusion access subtype
12697 elsif Is_Access_Type (Etype (Formal_Id))
12698 and then Can_Never_Be_Null (Etype (Formal_Id))
12700 Set_Is_Known_Non_Null (Formal_Id);
12702 -- We can also set Can_Never_Be_Null (thus preventing some junk
12703 -- access checks) for the case of an IN parameter, which cannot
12704 -- be changed, or for an IN OUT parameter, which can be changed but
12705 -- not to a null value. But for an OUT parameter, the initial value
12706 -- passed in can be null, so we can't set this flag in that case.
12708 if Ekind (Formal_Id) /= E_Out_Parameter then
12709 Set_Can_Never_Be_Null (Formal_Id);
12713 Set_Mechanism (Formal_Id, Default_Mechanism);
12714 Set_Formal_Validity (Formal_Id);
12715 end Set_Formal_Mode;
12717 -------------------------
12718 -- Set_Formal_Validity --
12719 -------------------------
12721 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12723 -- If no validity checking, then we cannot assume anything about the
12724 -- validity of parameters, since we do not know there is any checking
12725 -- of the validity on the call side.
12727 if not Validity_Checks_On then
12730 -- If validity checking for parameters is enabled, this means we are
12731 -- not supposed to make any assumptions about argument values.
12733 elsif Validity_Check_Parameters then
12736 -- If we are checking in parameters, we will assume that the caller is
12737 -- also checking parameters, so we can assume the parameter is valid.
12739 elsif Ekind (Formal_Id) = E_In_Parameter
12740 and then Validity_Check_In_Params
12742 Set_Is_Known_Valid (Formal_Id, True);
12744 -- Similar treatment for IN OUT parameters
12746 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12747 and then Validity_Check_In_Out_Params
12749 Set_Is_Known_Valid (Formal_Id, True);
12751 end Set_Formal_Validity;
12753 ------------------------
12754 -- Subtype_Conformant --
12755 ------------------------
12757 function Subtype_Conformant
12758 (New_Id : Entity_Id;
12759 Old_Id : Entity_Id;
12760 Skip_Controlling_Formals : Boolean := False) return Boolean
12764 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12765 Skip_Controlling_Formals => Skip_Controlling_Formals);
12767 end Subtype_Conformant;
12769 ---------------------
12770 -- Type_Conformant --
12771 ---------------------
12773 function Type_Conformant
12774 (New_Id : Entity_Id;
12775 Old_Id : Entity_Id;
12776 Skip_Controlling_Formals : Boolean := False) return Boolean
12780 May_Hide_Profile := False;
12783 (New_Id, Old_Id, Type_Conformant, False, Result,
12784 Skip_Controlling_Formals => Skip_Controlling_Formals);
12786 end Type_Conformant;
12788 -------------------------------
12789 -- Valid_Operator_Definition --
12790 -------------------------------
12792 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12795 Id : constant Name_Id := Chars (Designator);
12799 F := First_Formal (Designator);
12800 while Present (F) loop
12803 if Present (Default_Value (F)) then
12805 ("default values not allowed for operator parameters",
12812 -- Verify that user-defined operators have proper number of arguments
12813 -- First case of operators which can only be unary
12815 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12818 -- Case of operators which can be unary or binary
12820 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12821 N_OK := (N in 1 .. 2);
12823 -- All other operators can only be binary
12831 ("incorrect number of arguments for operator", Designator);
12835 and then Base_Type (Etype (Designator)) = Standard_Boolean
12836 and then not Is_Intrinsic_Subprogram (Designator)
12839 ("explicit definition of inequality not allowed", Designator);
12841 end Valid_Operator_Definition;