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 -- Whether attribute 'Result is mentioned in a non-trivial postcondition
7067 -- or contract-cases.
7069 No_Warning_On_Some_Postcondition : Boolean := False;
7070 -- Whether there exists a non-trivial postcondition or contract-cases
7071 -- without a corresponding warning.
7073 Post_State_Mentioned : Boolean := False;
7074 -- Whether some expression mentioned in a postcondition or
7075 -- contract-cases can have a different value in the post-state than
7076 -- in the pre-state.
7078 function Check_Attr_Result (N : Node_Id) return Traverse_Result;
7079 -- Check if N is a reference to the attribute 'Result, and if so set
7080 -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK.
7082 function Check_Post_State (N : Node_Id) return Traverse_Result;
7083 -- Check whether the value of evaluating N can be different in the
7084 -- post-state, compared to the same evaluation in the pre-state, and
7085 -- if so set Post_State_Mentioned and return Abandon. Return Skip on
7086 -- reference to attribute 'Old, in order to ignore its prefix, which
7087 -- is precisely evaluated in the pre-state. Otherwise return OK.
7089 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean;
7090 -- Return True if node N is trivially "True" or "False", and it comes
7091 -- from source. In particular, nodes that are statically known "True" or
7092 -- "False" by the compiler but not written as such in source code are
7093 -- not considered as trivial.
7095 procedure Process_Contract_Cases (Spec : Node_Id);
7096 -- This processes the Spec_CTC_List from Spec, processing any contract
7097 -- case from the list. The caller has checked that Spec_CTC_List is
7100 procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean);
7101 -- This processes the Spec_PPC_List from Spec, processing any
7102 -- postcondition from the list. If Class is True, then only
7103 -- postconditions marked with Class_Present are considered. The
7104 -- caller has checked that Spec_PPC_List is non-Empty.
7106 function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result);
7108 function Find_Post_State is new Traverse_Func (Check_Post_State);
7110 -----------------------
7111 -- Check_Attr_Result --
7112 -----------------------
7114 function Check_Attr_Result (N : Node_Id) return Traverse_Result is
7116 if Nkind (N) = N_Attribute_Reference
7117 and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result
7119 Attribute_Result_Mentioned := True;
7124 end Check_Attr_Result;
7126 ----------------------
7127 -- Check_Post_State --
7128 ----------------------
7130 function Check_Post_State (N : Node_Id) return Traverse_Result is
7131 Found : Boolean := False;
7135 when N_Function_Call |
7136 N_Explicit_Dereference =>
7143 E : constant Entity_Id := Entity (N);
7146 -- ???Quantified expressions get analyzed later, so E can
7147 -- be empty at this point. In this case, we suppress the
7148 -- warning, just in case E is assignable. It seems better to
7149 -- have false negatives than false positives. At some point,
7150 -- we should make the warning more accurate, either by
7151 -- analyzing quantified expressions earlier, or moving
7152 -- this processing later.
7157 and then Ekind (E) in Assignable_Kind)
7163 when N_Attribute_Reference =>
7164 case Get_Attribute_Id (Attribute_Name (N)) is
7165 when Attribute_Old =>
7167 when Attribute_Result =>
7178 Post_State_Mentioned := True;
7183 end Check_Post_State;
7185 --------------------------------
7186 -- Is_Trivial_Post_Or_Ensures --
7187 --------------------------------
7189 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean is
7191 return Is_Entity_Name (N)
7192 and then (Entity (N) = Standard_True
7194 Entity (N) = Standard_False)
7195 and then Comes_From_Source (N);
7196 end Is_Trivial_Post_Or_Ensures;
7198 ----------------------------
7199 -- Process_Contract_Cases --
7200 ----------------------------
7202 procedure Process_Contract_Cases (Spec : Node_Id) is
7206 Post_Case : Node_Id;
7208 Ignored : Traverse_Final_Result;
7209 pragma Unreferenced (Ignored);
7212 Prag := Spec_CTC_List (Contract (Spec));
7214 if Pragma_Name (Prag) = Name_Contract_Cases then
7216 Expression (First (Pragma_Argument_Associations (Prag)));
7218 Post_Case := First (Component_Associations (Aggr));
7219 while Present (Post_Case) loop
7220 Conseq := Expression (Post_Case);
7222 -- Ignore trivial contract-cases when consequence is "True"
7225 if not Is_Trivial_Post_Or_Ensures (Conseq) then
7227 Last_Contract_Cases := Prag;
7229 -- For functions, look for presence of 'Result in
7230 -- consequence expression.
7232 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7233 Ignored := Find_Attribute_Result (Conseq);
7236 -- For each individual case, look for presence of an
7237 -- expression that could be evaluated differently in
7240 Post_State_Mentioned := False;
7241 Ignored := Find_Post_State (Conseq);
7243 if Post_State_Mentioned then
7244 No_Warning_On_Some_Postcondition := True;
7247 ("contract case refers only to pre-state?T?",
7256 Prag := Next_Pragma (Prag);
7257 exit when No (Prag);
7259 end Process_Contract_Cases;
7261 -----------------------------
7262 -- Process_Post_Conditions --
7263 -----------------------------
7265 procedure Process_Post_Conditions
7271 Ignored : Traverse_Final_Result;
7272 pragma Unreferenced (Ignored);
7275 Prag := Spec_PPC_List (Contract (Spec));
7277 Arg := First (Pragma_Argument_Associations (Prag));
7279 -- Ignore trivial postcondition of "True" or "False"
7281 if Pragma_Name (Prag) = Name_Postcondition
7282 and then not Is_Trivial_Post_Or_Ensures (Expression (Arg))
7284 -- Since pre- and post-conditions are listed in reverse order,
7285 -- the first postcondition in the list is last in the source.
7287 if not Class and then No (Last_Postcondition) then
7288 Last_Postcondition := Prag;
7291 -- For functions, look for presence of 'Result in postcondition
7293 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7294 Ignored := Find_Attribute_Result (Arg);
7297 -- For each individual non-inherited postcondition, look
7298 -- for presence of an expression that could be evaluated
7299 -- differently in post-state.
7302 Post_State_Mentioned := False;
7303 Ignored := Find_Post_State (Arg);
7305 if Post_State_Mentioned then
7306 No_Warning_On_Some_Postcondition := True;
7309 ("postcondition refers only to pre-state?T?", Prag);
7314 Prag := Next_Pragma (Prag);
7315 exit when No (Prag);
7317 end Process_Post_Conditions;
7319 -- Start of processing for Check_Subprogram_Contract
7322 if not Warn_On_Suspicious_Contract then
7326 -- Process spec postconditions
7328 if Present (Spec_PPC_List (Contract (Spec_Id))) then
7329 Process_Post_Conditions (Spec_Id, Class => False);
7332 -- Process inherited postconditions
7334 -- Code is currently commented out as, in some cases, it causes crashes
7335 -- because Direct_Primitive_Operations is not available for a private
7336 -- type. This may cause more warnings to be issued than necessary. ???
7338 -- for J in Inherited'Range loop
7339 -- if Present (Spec_PPC_List (Contract (Inherited (J)))) then
7340 -- Process_Post_Conditions (Inherited (J), Class => True);
7344 -- Process contract cases
7346 if Present (Spec_CTC_List (Contract (Spec_Id))) then
7347 Process_Contract_Cases (Spec_Id);
7350 -- Issue warning for functions whose postcondition does not mention
7351 -- 'Result after all postconditions have been processed, and provided
7352 -- all postconditions do not already get a warning that they only refer
7355 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
7356 and then (Present (Last_Postcondition)
7357 or else Present (Last_Contract_Cases))
7358 and then not Attribute_Result_Mentioned
7359 and then No_Warning_On_Some_Postcondition
7361 if Present (Last_Postcondition) then
7362 if Present (Last_Contract_Cases) then
7364 ("neither function postcondition nor "
7365 & "contract cases mention result?T?", Last_Postcondition);
7369 ("function postcondition does not mention result?T?",
7370 Last_Postcondition);
7374 ("contract cases do not mention result?T?", Last_Contract_Cases);
7377 end Check_Subprogram_Contract;
7379 ----------------------------
7380 -- Check_Subprogram_Order --
7381 ----------------------------
7383 procedure Check_Subprogram_Order (N : Node_Id) is
7385 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7386 -- This is used to check if S1 > S2 in the sense required by this test,
7387 -- for example nameab < namec, but name2 < name10.
7389 -----------------------------
7390 -- Subprogram_Name_Greater --
7391 -----------------------------
7393 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7398 -- Deal with special case where names are identical except for a
7399 -- numerical suffix. These are handled specially, taking the numeric
7400 -- ordering from the suffix into account.
7403 while S1 (L1) in '0' .. '9' loop
7408 while S2 (L2) in '0' .. '9' loop
7412 -- If non-numeric parts non-equal, do straight compare
7414 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7417 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7418 -- that a missing suffix is treated as numeric zero in this test.
7422 while L1 < S1'Last loop
7424 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7428 while L2 < S2'Last loop
7430 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7435 end Subprogram_Name_Greater;
7437 -- Start of processing for Check_Subprogram_Order
7440 -- Check body in alpha order if this is option
7443 and then Style_Check_Order_Subprograms
7444 and then Nkind (N) = N_Subprogram_Body
7445 and then Comes_From_Source (N)
7446 and then In_Extended_Main_Source_Unit (N)
7450 renames Scope_Stack.Table
7451 (Scope_Stack.Last).Last_Subprogram_Name;
7453 Body_Id : constant Entity_Id :=
7454 Defining_Entity (Specification (N));
7457 Get_Decoded_Name_String (Chars (Body_Id));
7460 if Subprogram_Name_Greater
7461 (LSN.all, Name_Buffer (1 .. Name_Len))
7463 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7469 LSN := new String'(Name_Buffer (1 .. Name_Len));
7472 end Check_Subprogram_Order;
7474 ------------------------------
7475 -- Check_Subtype_Conformant --
7476 ------------------------------
7478 procedure Check_Subtype_Conformant
7479 (New_Id : Entity_Id;
7481 Err_Loc : Node_Id := Empty;
7482 Skip_Controlling_Formals : Boolean := False;
7483 Get_Inst : Boolean := False)
7486 pragma Warnings (Off, Result);
7489 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7490 Skip_Controlling_Formals => Skip_Controlling_Formals,
7491 Get_Inst => Get_Inst);
7492 end Check_Subtype_Conformant;
7494 ---------------------------
7495 -- Check_Type_Conformant --
7496 ---------------------------
7498 procedure Check_Type_Conformant
7499 (New_Id : Entity_Id;
7501 Err_Loc : Node_Id := Empty)
7504 pragma Warnings (Off, Result);
7507 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7508 end Check_Type_Conformant;
7510 ---------------------------
7511 -- Can_Override_Operator --
7512 ---------------------------
7514 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7518 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7522 Typ := Base_Type (Etype (First_Formal (Subp)));
7524 -- Check explicitly that the operation is a primitive of the type
7526 return Operator_Matches_Spec (Subp, Subp)
7527 and then not Is_Generic_Type (Typ)
7528 and then Scope (Subp) = Scope (Typ)
7529 and then not Is_Class_Wide_Type (Typ);
7531 end Can_Override_Operator;
7533 ----------------------
7534 -- Conforming_Types --
7535 ----------------------
7537 function Conforming_Types
7540 Ctype : Conformance_Type;
7541 Get_Inst : Boolean := False) return Boolean
7543 Type_1 : Entity_Id := T1;
7544 Type_2 : Entity_Id := T2;
7545 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7547 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7548 -- If neither T1 nor T2 are generic actual types, or if they are in
7549 -- different scopes (e.g. parent and child instances), then verify that
7550 -- the base types are equal. Otherwise T1 and T2 must be on the same
7551 -- subtype chain. The whole purpose of this procedure is to prevent
7552 -- spurious ambiguities in an instantiation that may arise if two
7553 -- distinct generic types are instantiated with the same actual.
7555 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7556 -- An access parameter can designate an incomplete type. If the
7557 -- incomplete type is the limited view of a type from a limited_
7558 -- with_clause, check whether the non-limited view is available. If
7559 -- it is a (non-limited) incomplete type, get the full view.
7561 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7562 -- Returns True if and only if either T1 denotes a limited view of T2
7563 -- or T2 denotes a limited view of T1. This can arise when the limited
7564 -- with view of a type is used in a subprogram declaration and the
7565 -- subprogram body is in the scope of a regular with clause for the
7566 -- same unit. In such a case, the two type entities can be considered
7567 -- identical for purposes of conformance checking.
7569 ----------------------
7570 -- Base_Types_Match --
7571 ----------------------
7573 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7574 BT1 : constant Entity_Id := Base_Type (T1);
7575 BT2 : constant Entity_Id := Base_Type (T2);
7581 elsif BT1 = BT2 then
7583 -- The following is too permissive. A more precise test should
7584 -- check that the generic actual is an ancestor subtype of the
7587 -- See code in Find_Corresponding_Spec that applies an additional
7588 -- filter to handle accidental amiguities in instances.
7590 return not Is_Generic_Actual_Type (T1)
7591 or else not Is_Generic_Actual_Type (T2)
7592 or else Scope (T1) /= Scope (T2);
7594 -- If T2 is a generic actual type it is declared as the subtype of
7595 -- the actual. If that actual is itself a subtype we need to use
7596 -- its own base type to check for compatibility.
7598 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7601 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7607 end Base_Types_Match;
7609 --------------------------
7610 -- Find_Designated_Type --
7611 --------------------------
7613 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7617 Desig := Directly_Designated_Type (T);
7619 if Ekind (Desig) = E_Incomplete_Type then
7621 -- If regular incomplete type, get full view if available
7623 if Present (Full_View (Desig)) then
7624 Desig := Full_View (Desig);
7626 -- If limited view of a type, get non-limited view if available,
7627 -- and check again for a regular incomplete type.
7629 elsif Present (Non_Limited_View (Desig)) then
7630 Desig := Get_Full_View (Non_Limited_View (Desig));
7635 end Find_Designated_Type;
7637 -------------------------------
7638 -- Matches_Limited_With_View --
7639 -------------------------------
7641 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7643 -- In some cases a type imported through a limited_with clause, and
7644 -- its nonlimited view are both visible, for example in an anonymous
7645 -- access-to-class-wide type in a formal. Both entities designate the
7648 if From_With_Type (T1) and then T2 = Available_View (T1) then
7651 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7654 elsif From_With_Type (T1)
7655 and then From_With_Type (T2)
7656 and then Available_View (T1) = Available_View (T2)
7663 end Matches_Limited_With_View;
7665 -- Start of processing for Conforming_Types
7668 -- The context is an instance association for a formal access-to-
7669 -- subprogram type; the formal parameter types require mapping because
7670 -- they may denote other formal parameters of the generic unit.
7673 Type_1 := Get_Instance_Of (T1);
7674 Type_2 := Get_Instance_Of (T2);
7677 -- If one of the types is a view of the other introduced by a limited
7678 -- with clause, treat these as conforming for all purposes.
7680 if Matches_Limited_With_View (T1, T2) then
7683 elsif Base_Types_Match (Type_1, Type_2) then
7684 return Ctype <= Mode_Conformant
7685 or else Subtypes_Statically_Match (Type_1, Type_2);
7687 elsif Is_Incomplete_Or_Private_Type (Type_1)
7688 and then Present (Full_View (Type_1))
7689 and then Base_Types_Match (Full_View (Type_1), Type_2)
7691 return Ctype <= Mode_Conformant
7692 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7694 elsif Ekind (Type_2) = E_Incomplete_Type
7695 and then Present (Full_View (Type_2))
7696 and then Base_Types_Match (Type_1, Full_View (Type_2))
7698 return Ctype <= Mode_Conformant
7699 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7701 elsif Is_Private_Type (Type_2)
7702 and then In_Instance
7703 and then Present (Full_View (Type_2))
7704 and then Base_Types_Match (Type_1, Full_View (Type_2))
7706 return Ctype <= Mode_Conformant
7707 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7710 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7711 -- treated recursively because they carry a signature.
7713 Are_Anonymous_Access_To_Subprogram_Types :=
7714 Ekind (Type_1) = Ekind (Type_2)
7716 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7717 E_Anonymous_Access_Protected_Subprogram_Type);
7719 -- Test anonymous access type case. For this case, static subtype
7720 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7721 -- the base types because we may have built internal subtype entities
7722 -- to handle null-excluding types (see Process_Formals).
7724 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7726 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7728 -- Ada 2005 (AI-254)
7730 or else Are_Anonymous_Access_To_Subprogram_Types
7733 Desig_1 : Entity_Id;
7734 Desig_2 : Entity_Id;
7737 -- In Ada 2005, access constant indicators must match for
7738 -- subtype conformance.
7740 if Ada_Version >= Ada_2005
7741 and then Ctype >= Subtype_Conformant
7743 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7748 Desig_1 := Find_Designated_Type (Type_1);
7749 Desig_2 := Find_Designated_Type (Type_2);
7751 -- If the context is an instance association for a formal
7752 -- access-to-subprogram type; formal access parameter designated
7753 -- types require mapping because they may denote other formal
7754 -- parameters of the generic unit.
7757 Desig_1 := Get_Instance_Of (Desig_1);
7758 Desig_2 := Get_Instance_Of (Desig_2);
7761 -- It is possible for a Class_Wide_Type to be introduced for an
7762 -- incomplete type, in which case there is a separate class_ wide
7763 -- type for the full view. The types conform if their Etypes
7764 -- conform, i.e. one may be the full view of the other. This can
7765 -- only happen in the context of an access parameter, other uses
7766 -- of an incomplete Class_Wide_Type are illegal.
7768 if Is_Class_Wide_Type (Desig_1)
7770 Is_Class_Wide_Type (Desig_2)
7774 (Etype (Base_Type (Desig_1)),
7775 Etype (Base_Type (Desig_2)), Ctype);
7777 elsif Are_Anonymous_Access_To_Subprogram_Types then
7778 if Ada_Version < Ada_2005 then
7779 return Ctype = Type_Conformant
7781 Subtypes_Statically_Match (Desig_1, Desig_2);
7783 -- We must check the conformance of the signatures themselves
7787 Conformant : Boolean;
7790 (Desig_1, Desig_2, Ctype, False, Conformant);
7796 return Base_Type (Desig_1) = Base_Type (Desig_2)
7797 and then (Ctype = Type_Conformant
7799 Subtypes_Statically_Match (Desig_1, Desig_2));
7803 -- Otherwise definitely no match
7806 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7807 and then Is_Access_Type (Type_2))
7808 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7809 and then Is_Access_Type (Type_1)))
7812 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7814 May_Hide_Profile := True;
7819 end Conforming_Types;
7821 --------------------------
7822 -- Create_Extra_Formals --
7823 --------------------------
7825 procedure Create_Extra_Formals (E : Entity_Id) is
7827 First_Extra : Entity_Id := Empty;
7828 Last_Extra : Entity_Id;
7829 Formal_Type : Entity_Id;
7830 P_Formal : Entity_Id := Empty;
7832 function Add_Extra_Formal
7833 (Assoc_Entity : Entity_Id;
7836 Suffix : String) return Entity_Id;
7837 -- Add an extra formal to the current list of formals and extra formals.
7838 -- The extra formal is added to the end of the list of extra formals,
7839 -- and also returned as the result. These formals are always of mode IN.
7840 -- The new formal has the type Typ, is declared in Scope, and its name
7841 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7842 -- The following suffixes are currently used. They should not be changed
7843 -- without coordinating with CodePeer, which makes use of these to
7844 -- provide better messages.
7846 -- O denotes the Constrained bit.
7847 -- L denotes the accessibility level.
7848 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7849 -- the full list in exp_ch6.BIP_Formal_Kind.
7851 ----------------------
7852 -- Add_Extra_Formal --
7853 ----------------------
7855 function Add_Extra_Formal
7856 (Assoc_Entity : Entity_Id;
7859 Suffix : String) return Entity_Id
7861 EF : constant Entity_Id :=
7862 Make_Defining_Identifier (Sloc (Assoc_Entity),
7863 Chars => New_External_Name (Chars (Assoc_Entity),
7867 -- A little optimization. Never generate an extra formal for the
7868 -- _init operand of an initialization procedure, since it could
7871 if Chars (Formal) = Name_uInit then
7875 Set_Ekind (EF, E_In_Parameter);
7876 Set_Actual_Subtype (EF, Typ);
7877 Set_Etype (EF, Typ);
7878 Set_Scope (EF, Scope);
7879 Set_Mechanism (EF, Default_Mechanism);
7880 Set_Formal_Validity (EF);
7882 if No (First_Extra) then
7884 Set_Extra_Formals (Scope, First_Extra);
7887 if Present (Last_Extra) then
7888 Set_Extra_Formal (Last_Extra, EF);
7894 end Add_Extra_Formal;
7896 -- Start of processing for Create_Extra_Formals
7899 -- We never generate extra formals if expansion is not active because we
7900 -- don't need them unless we are generating code.
7902 if not Expander_Active then
7906 -- No need to generate extra formals in interface thunks whose target
7907 -- primitive has no extra formals.
7909 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7913 -- If this is a derived subprogram then the subtypes of the parent
7914 -- subprogram's formal parameters will be used to determine the need
7915 -- for extra formals.
7917 if Is_Overloadable (E) and then Present (Alias (E)) then
7918 P_Formal := First_Formal (Alias (E));
7921 Last_Extra := Empty;
7922 Formal := First_Formal (E);
7923 while Present (Formal) loop
7924 Last_Extra := Formal;
7925 Next_Formal (Formal);
7928 -- If Extra_formals were already created, don't do it again. This
7929 -- situation may arise for subprogram types created as part of
7930 -- dispatching calls (see Expand_Dispatching_Call)
7932 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7936 -- If the subprogram is a predefined dispatching subprogram then don't
7937 -- generate any extra constrained or accessibility level formals. In
7938 -- general we suppress these for internal subprograms (by not calling
7939 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7940 -- generated stream attributes do get passed through because extra
7941 -- build-in-place formals are needed in some cases (limited 'Input).
7943 if Is_Predefined_Internal_Operation (E) then
7944 goto Test_For_Func_Result_Extras;
7947 Formal := First_Formal (E);
7948 while Present (Formal) loop
7950 -- Create extra formal for supporting the attribute 'Constrained.
7951 -- The case of a private type view without discriminants also
7952 -- requires the extra formal if the underlying type has defaulted
7955 if Ekind (Formal) /= E_In_Parameter then
7956 if Present (P_Formal) then
7957 Formal_Type := Etype (P_Formal);
7959 Formal_Type := Etype (Formal);
7962 -- Do not produce extra formals for Unchecked_Union parameters.
7963 -- Jump directly to the end of the loop.
7965 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7966 goto Skip_Extra_Formal_Generation;
7969 if not Has_Discriminants (Formal_Type)
7970 and then Ekind (Formal_Type) in Private_Kind
7971 and then Present (Underlying_Type (Formal_Type))
7973 Formal_Type := Underlying_Type (Formal_Type);
7976 -- Suppress the extra formal if formal's subtype is constrained or
7977 -- indefinite, or we're compiling for Ada 2012 and the underlying
7978 -- type is tagged and limited. In Ada 2012, a limited tagged type
7979 -- can have defaulted discriminants, but 'Constrained is required
7980 -- to return True, so the formal is never needed (see AI05-0214).
7981 -- Note that this ensures consistency of calling sequences for
7982 -- dispatching operations when some types in a class have defaults
7983 -- on discriminants and others do not (and requiring the extra
7984 -- formal would introduce distributed overhead).
7986 if Has_Discriminants (Formal_Type)
7987 and then not Is_Constrained (Formal_Type)
7988 and then not Is_Indefinite_Subtype (Formal_Type)
7989 and then (Ada_Version < Ada_2012
7991 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
7992 and then Is_Limited_Type (Formal_Type)))
7994 Set_Extra_Constrained
7995 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7999 -- Create extra formal for supporting accessibility checking. This
8000 -- is done for both anonymous access formals and formals of named
8001 -- access types that are marked as controlling formals. The latter
8002 -- case can occur when Expand_Dispatching_Call creates a subprogram
8003 -- type and substitutes the types of access-to-class-wide actuals
8004 -- for the anonymous access-to-specific-type of controlling formals.
8005 -- Base_Type is applied because in cases where there is a null
8006 -- exclusion the formal may have an access subtype.
8008 -- This is suppressed if we specifically suppress accessibility
8009 -- checks at the package level for either the subprogram, or the
8010 -- package in which it resides. However, we do not suppress it
8011 -- simply if the scope has accessibility checks suppressed, since
8012 -- this could cause trouble when clients are compiled with a
8013 -- different suppression setting. The explicit checks at the
8014 -- package level are safe from this point of view.
8016 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8017 or else (Is_Controlling_Formal (Formal)
8018 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8020 (Explicit_Suppress (E, Accessibility_Check)
8022 Explicit_Suppress (Scope (E), Accessibility_Check))
8025 or else Present (Extra_Accessibility (P_Formal)))
8027 Set_Extra_Accessibility
8028 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8031 -- This label is required when skipping extra formal generation for
8032 -- Unchecked_Union parameters.
8034 <<Skip_Extra_Formal_Generation>>
8036 if Present (P_Formal) then
8037 Next_Formal (P_Formal);
8040 Next_Formal (Formal);
8043 <<Test_For_Func_Result_Extras>>
8045 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8046 -- function call is ... determined by the point of call ...".
8048 if Needs_Result_Accessibility_Level (E) then
8049 Set_Extra_Accessibility_Of_Result
8050 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8053 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8054 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8056 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8058 Result_Subt : constant Entity_Id := Etype (E);
8059 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8060 Formal_Typ : Entity_Id;
8062 Discard : Entity_Id;
8063 pragma Warnings (Off, Discard);
8066 -- In the case of functions with unconstrained result subtypes,
8067 -- add a 4-state formal indicating whether the return object is
8068 -- allocated by the caller (1), or should be allocated by the
8069 -- callee on the secondary stack (2), in the global heap (3), or
8070 -- in a user-defined storage pool (4). For the moment we just use
8071 -- Natural for the type of this formal. Note that this formal
8072 -- isn't usually needed in the case where the result subtype is
8073 -- constrained, but it is needed when the function has a tagged
8074 -- result, because generally such functions can be called in a
8075 -- dispatching context and such calls must be handled like calls
8076 -- to a class-wide function.
8078 if Needs_BIP_Alloc_Form (E) then
8081 (E, Standard_Natural,
8082 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8084 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8085 -- use a user-defined pool. This formal is not added on
8086 -- .NET/JVM/ZFP as those targets do not support pools.
8088 if VM_Target = No_VM
8089 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8093 (E, RTE (RE_Root_Storage_Pool_Ptr),
8094 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8098 -- In the case of functions whose result type needs finalization,
8099 -- add an extra formal which represents the finalization master.
8101 if Needs_BIP_Finalization_Master (E) then
8104 (E, RTE (RE_Finalization_Master_Ptr),
8105 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8108 -- When the result type contains tasks, add two extra formals: the
8109 -- master of the tasks to be created, and the caller's activation
8112 if Has_Task (Full_Subt) then
8115 (E, RTE (RE_Master_Id),
8116 E, BIP_Formal_Suffix (BIP_Task_Master));
8119 (E, RTE (RE_Activation_Chain_Access),
8120 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8123 -- All build-in-place functions get an extra formal that will be
8124 -- passed the address of the return object within the caller.
8127 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8129 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8130 Set_Etype (Formal_Typ, Formal_Typ);
8131 Set_Depends_On_Private
8132 (Formal_Typ, Has_Private_Component (Formal_Typ));
8133 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8134 Set_Is_Access_Constant (Formal_Typ, False);
8136 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8137 -- the designated type comes from the limited view (for back-end
8140 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
8142 Layout_Type (Formal_Typ);
8146 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8149 end Create_Extra_Formals;
8151 -----------------------------
8152 -- Enter_Overloaded_Entity --
8153 -----------------------------
8155 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8156 E : Entity_Id := Current_Entity_In_Scope (S);
8157 C_E : Entity_Id := Current_Entity (S);
8161 Set_Has_Homonym (E);
8162 Set_Has_Homonym (S);
8165 Set_Is_Immediately_Visible (S);
8166 Set_Scope (S, Current_Scope);
8168 -- Chain new entity if front of homonym in current scope, so that
8169 -- homonyms are contiguous.
8171 if Present (E) and then E /= C_E then
8172 while Homonym (C_E) /= E loop
8173 C_E := Homonym (C_E);
8176 Set_Homonym (C_E, S);
8180 Set_Current_Entity (S);
8185 if Is_Inherited_Operation (S) then
8186 Append_Inherited_Subprogram (S);
8188 Append_Entity (S, Current_Scope);
8191 Set_Public_Status (S);
8193 if Debug_Flag_E then
8194 Write_Str ("New overloaded entity chain: ");
8195 Write_Name (Chars (S));
8198 while Present (E) loop
8199 Write_Str (" "); Write_Int (Int (E));
8206 -- Generate warning for hiding
8209 and then Comes_From_Source (S)
8210 and then In_Extended_Main_Source_Unit (S)
8217 -- Warn unless genuine overloading. Do not emit warning on
8218 -- hiding predefined operators in Standard (these are either an
8219 -- (artifact of our implicit declarations, or simple noise) but
8220 -- keep warning on a operator defined on a local subtype, because
8221 -- of the real danger that different operators may be applied in
8222 -- various parts of the program.
8224 -- Note that if E and S have the same scope, there is never any
8225 -- hiding. Either the two conflict, and the program is illegal,
8226 -- or S is overriding an implicit inherited subprogram.
8228 if Scope (E) /= Scope (S)
8229 and then (not Is_Overloadable (E)
8230 or else Subtype_Conformant (E, S))
8231 and then (Is_Immediately_Visible (E)
8233 Is_Potentially_Use_Visible (S))
8235 if Scope (E) /= Standard_Standard then
8236 Error_Msg_Sloc := Sloc (E);
8237 Error_Msg_N ("declaration of & hides one#?h?", S);
8239 elsif Nkind (S) = N_Defining_Operator_Symbol
8241 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8244 ("declaration of & hides predefined operator?h?", S);
8249 end Enter_Overloaded_Entity;
8251 -----------------------------
8252 -- Check_Untagged_Equality --
8253 -----------------------------
8255 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8256 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8257 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8261 if Nkind (Decl) = N_Subprogram_Declaration
8262 and then Is_Record_Type (Typ)
8263 and then not Is_Tagged_Type (Typ)
8265 -- If the type is not declared in a package, or if we are in the
8266 -- body of the package or in some other scope, the new operation is
8267 -- not primitive, and therefore legal, though suspicious. If the
8268 -- type is a generic actual (sub)type, the operation is not primitive
8269 -- either because the base type is declared elsewhere.
8271 if Is_Frozen (Typ) then
8272 if Ekind (Scope (Typ)) /= E_Package
8273 or else Scope (Typ) /= Current_Scope
8277 elsif Is_Generic_Actual_Type (Typ) then
8280 elsif In_Package_Body (Scope (Typ)) then
8282 ("equality operator must be declared "
8283 & "before type& is frozen", Eq_Op, Typ);
8285 ("\move declaration to package spec", Eq_Op);
8289 ("equality operator must be declared "
8290 & "before type& is frozen", Eq_Op, Typ);
8292 Obj_Decl := Next (Parent (Typ));
8293 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8294 if Nkind (Obj_Decl) = N_Object_Declaration
8295 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8298 ("type& is frozen by declaration??", Obj_Decl, Typ);
8300 ("\an equality operator cannot be declared after this "
8301 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8309 elsif not In_Same_List (Parent (Typ), Decl)
8310 and then not Is_Limited_Type (Typ)
8313 -- This makes it illegal to have a primitive equality declared in
8314 -- the private part if the type is visible.
8316 Error_Msg_N ("equality operator appears too late", Eq_Op);
8319 end Check_Untagged_Equality;
8321 -----------------------------
8322 -- Find_Corresponding_Spec --
8323 -----------------------------
8325 function Find_Corresponding_Spec
8327 Post_Error : Boolean := True) return Entity_Id
8329 Spec : constant Node_Id := Specification (N);
8330 Designator : constant Entity_Id := Defining_Entity (Spec);
8334 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8335 -- Even if fully conformant, a body may depend on a generic actual when
8336 -- the spec does not, or vice versa, in which case they were distinct
8337 -- entities in the generic.
8339 -------------------------------
8340 -- Different_Generic_Profile --
8341 -------------------------------
8343 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8347 if Ekind (E) = E_Function
8348 and then Is_Generic_Actual_Type (Etype (E)) /=
8349 Is_Generic_Actual_Type (Etype (Designator))
8354 F1 := First_Formal (Designator);
8355 F2 := First_Formal (E);
8356 while Present (F1) loop
8357 if Is_Generic_Actual_Type (Etype (F1)) /=
8358 Is_Generic_Actual_Type (Etype (F2))
8368 end Different_Generic_Profile;
8370 -- Start of processing for Find_Corresponding_Spec
8373 E := Current_Entity (Designator);
8374 while Present (E) loop
8376 -- We are looking for a matching spec. It must have the same scope,
8377 -- and the same name, and either be type conformant, or be the case
8378 -- of a library procedure spec and its body (which belong to one
8379 -- another regardless of whether they are type conformant or not).
8381 if Scope (E) = Current_Scope then
8382 if Current_Scope = Standard_Standard
8383 or else (Ekind (E) = Ekind (Designator)
8384 and then Type_Conformant (E, Designator))
8386 -- Within an instantiation, we know that spec and body are
8387 -- subtype conformant, because they were subtype conformant in
8388 -- the generic. We choose the subtype-conformant entity here as
8389 -- well, to resolve spurious ambiguities in the instance that
8390 -- were not present in the generic (i.e. when two different
8391 -- types are given the same actual). If we are looking for a
8392 -- spec to match a body, full conformance is expected.
8395 Set_Convention (Designator, Convention (E));
8397 -- Skip past subprogram bodies and subprogram renamings that
8398 -- may appear to have a matching spec, but that aren't fully
8399 -- conformant with it. That can occur in cases where an
8400 -- actual type causes unrelated homographs in the instance.
8402 if Nkind_In (N, N_Subprogram_Body,
8403 N_Subprogram_Renaming_Declaration)
8404 and then Present (Homonym (E))
8405 and then not Fully_Conformant (Designator, E)
8409 elsif not Subtype_Conformant (Designator, E) then
8412 elsif Different_Generic_Profile (E) then
8417 -- Ada 2012 (AI05-0165): For internally generated bodies of
8418 -- null procedures locate the internally generated spec. We
8419 -- enforce mode conformance since a tagged type may inherit
8420 -- from interfaces several null primitives which differ only
8421 -- in the mode of the formals.
8423 if not (Comes_From_Source (E))
8424 and then Is_Null_Procedure (E)
8425 and then not Mode_Conformant (Designator, E)
8429 -- For null procedures coming from source that are completions,
8430 -- analysis of the generated body will establish the link.
8432 elsif Comes_From_Source (E)
8433 and then Nkind (Spec) = N_Procedure_Specification
8434 and then Null_Present (Spec)
8438 elsif not Has_Completion (E) then
8439 if Nkind (N) /= N_Subprogram_Body_Stub then
8440 Set_Corresponding_Spec (N, E);
8443 Set_Has_Completion (E);
8446 elsif Nkind (Parent (N)) = N_Subunit then
8448 -- If this is the proper body of a subunit, the completion
8449 -- flag is set when analyzing the stub.
8453 -- If E is an internal function with a controlling result that
8454 -- was created for an operation inherited by a null extension,
8455 -- it may be overridden by a body without a previous spec (one
8456 -- more reason why these should be shunned). In that case
8457 -- remove the generated body if present, because the current
8458 -- one is the explicit overriding.
8460 elsif Ekind (E) = E_Function
8461 and then Ada_Version >= Ada_2005
8462 and then not Comes_From_Source (E)
8463 and then Has_Controlling_Result (E)
8464 and then Is_Null_Extension (Etype (E))
8465 and then Comes_From_Source (Spec)
8467 Set_Has_Completion (E, False);
8470 and then Nkind (Parent (E)) = N_Function_Specification
8473 (Unit_Declaration_Node
8474 (Corresponding_Body (Unit_Declaration_Node (E))));
8478 -- If expansion is disabled, or if the wrapper function has
8479 -- not been generated yet, this a late body overriding an
8480 -- inherited operation, or it is an overriding by some other
8481 -- declaration before the controlling result is frozen. In
8482 -- either case this is a declaration of a new entity.
8488 -- If the body already exists, then this is an error unless
8489 -- the previous declaration is the implicit declaration of a
8490 -- derived subprogram. It is also legal for an instance to
8491 -- contain type conformant overloadable declarations (but the
8492 -- generic declaration may not), per 8.3(26/2).
8494 elsif No (Alias (E))
8495 and then not Is_Intrinsic_Subprogram (E)
8496 and then not In_Instance
8499 Error_Msg_Sloc := Sloc (E);
8501 if Is_Imported (E) then
8503 ("body not allowed for imported subprogram & declared#",
8506 Error_Msg_NE ("duplicate body for & declared#", N, E);
8510 -- Child units cannot be overloaded, so a conformance mismatch
8511 -- between body and a previous spec is an error.
8513 elsif Is_Child_Unit (E)
8515 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8517 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8522 ("body of child unit does not match previous declaration", N);
8530 -- On exit, we know that no previous declaration of subprogram exists
8533 end Find_Corresponding_Spec;
8535 ----------------------
8536 -- Fully_Conformant --
8537 ----------------------
8539 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8542 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8544 end Fully_Conformant;
8546 ----------------------------------
8547 -- Fully_Conformant_Expressions --
8548 ----------------------------------
8550 function Fully_Conformant_Expressions
8551 (Given_E1 : Node_Id;
8552 Given_E2 : Node_Id) return Boolean
8554 E1 : constant Node_Id := Original_Node (Given_E1);
8555 E2 : constant Node_Id := Original_Node (Given_E2);
8556 -- We always test conformance on original nodes, since it is possible
8557 -- for analysis and/or expansion to make things look as though they
8558 -- conform when they do not, e.g. by converting 1+2 into 3.
8560 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8561 renames Fully_Conformant_Expressions;
8563 function FCL (L1, L2 : List_Id) return Boolean;
8564 -- Compare elements of two lists for conformance. Elements have to be
8565 -- conformant, and actuals inserted as default parameters do not match
8566 -- explicit actuals with the same value.
8568 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8569 -- Compare an operator node with a function call
8575 function FCL (L1, L2 : List_Id) return Boolean is
8579 if L1 = No_List then
8585 if L2 = No_List then
8591 -- Compare two lists, skipping rewrite insertions (we want to compare
8592 -- the original trees, not the expanded versions!)
8595 if Is_Rewrite_Insertion (N1) then
8597 elsif Is_Rewrite_Insertion (N2) then
8603 elsif not FCE (N1, N2) then
8616 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8617 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8622 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8627 Act := First (Actuals);
8629 if Nkind (Op_Node) in N_Binary_Op then
8630 if not FCE (Left_Opnd (Op_Node), Act) then
8637 return Present (Act)
8638 and then FCE (Right_Opnd (Op_Node), Act)
8639 and then No (Next (Act));
8643 -- Start of processing for Fully_Conformant_Expressions
8646 -- Non-conformant if paren count does not match. Note: if some idiot
8647 -- complains that we don't do this right for more than 3 levels of
8648 -- parentheses, they will be treated with the respect they deserve!
8650 if Paren_Count (E1) /= Paren_Count (E2) then
8653 -- If same entities are referenced, then they are conformant even if
8654 -- they have different forms (RM 8.3.1(19-20)).
8656 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8657 if Present (Entity (E1)) then
8658 return Entity (E1) = Entity (E2)
8659 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8660 and then Ekind (Entity (E1)) = E_Discriminant
8661 and then Ekind (Entity (E2)) = E_In_Parameter);
8663 elsif Nkind (E1) = N_Expanded_Name
8664 and then Nkind (E2) = N_Expanded_Name
8665 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8666 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8668 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8671 -- Identifiers in component associations don't always have
8672 -- entities, but their names must conform.
8674 return Nkind (E1) = N_Identifier
8675 and then Nkind (E2) = N_Identifier
8676 and then Chars (E1) = Chars (E2);
8679 elsif Nkind (E1) = N_Character_Literal
8680 and then Nkind (E2) = N_Expanded_Name
8682 return Nkind (Selector_Name (E2)) = N_Character_Literal
8683 and then Chars (E1) = Chars (Selector_Name (E2));
8685 elsif Nkind (E2) = N_Character_Literal
8686 and then Nkind (E1) = N_Expanded_Name
8688 return Nkind (Selector_Name (E1)) = N_Character_Literal
8689 and then Chars (E2) = Chars (Selector_Name (E1));
8691 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8692 return FCO (E1, E2);
8694 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8695 return FCO (E2, E1);
8697 -- Otherwise we must have the same syntactic entity
8699 elsif Nkind (E1) /= Nkind (E2) then
8702 -- At this point, we specialize by node type
8709 FCL (Expressions (E1), Expressions (E2))
8711 FCL (Component_Associations (E1),
8712 Component_Associations (E2));
8715 if Nkind (Expression (E1)) = N_Qualified_Expression
8717 Nkind (Expression (E2)) = N_Qualified_Expression
8719 return FCE (Expression (E1), Expression (E2));
8721 -- Check that the subtype marks and any constraints
8726 Indic1 : constant Node_Id := Expression (E1);
8727 Indic2 : constant Node_Id := Expression (E2);
8732 if Nkind (Indic1) /= N_Subtype_Indication then
8734 Nkind (Indic2) /= N_Subtype_Indication
8735 and then Entity (Indic1) = Entity (Indic2);
8737 elsif Nkind (Indic2) /= N_Subtype_Indication then
8739 Nkind (Indic1) /= N_Subtype_Indication
8740 and then Entity (Indic1) = Entity (Indic2);
8743 if Entity (Subtype_Mark (Indic1)) /=
8744 Entity (Subtype_Mark (Indic2))
8749 Elt1 := First (Constraints (Constraint (Indic1)));
8750 Elt2 := First (Constraints (Constraint (Indic2)));
8751 while Present (Elt1) and then Present (Elt2) loop
8752 if not FCE (Elt1, Elt2) then
8765 when N_Attribute_Reference =>
8767 Attribute_Name (E1) = Attribute_Name (E2)
8768 and then FCL (Expressions (E1), Expressions (E2));
8772 Entity (E1) = Entity (E2)
8773 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8774 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8776 when N_Short_Circuit | N_Membership_Test =>
8778 FCE (Left_Opnd (E1), Left_Opnd (E2))
8780 FCE (Right_Opnd (E1), Right_Opnd (E2));
8782 when N_Case_Expression =>
8788 if not FCE (Expression (E1), Expression (E2)) then
8792 Alt1 := First (Alternatives (E1));
8793 Alt2 := First (Alternatives (E2));
8795 if Present (Alt1) /= Present (Alt2) then
8797 elsif No (Alt1) then
8801 if not FCE (Expression (Alt1), Expression (Alt2))
8802 or else not FCL (Discrete_Choices (Alt1),
8803 Discrete_Choices (Alt2))
8814 when N_Character_Literal =>
8816 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8818 when N_Component_Association =>
8820 FCL (Choices (E1), Choices (E2))
8822 FCE (Expression (E1), Expression (E2));
8824 when N_Explicit_Dereference =>
8826 FCE (Prefix (E1), Prefix (E2));
8828 when N_Extension_Aggregate =>
8830 FCL (Expressions (E1), Expressions (E2))
8831 and then Null_Record_Present (E1) =
8832 Null_Record_Present (E2)
8833 and then FCL (Component_Associations (E1),
8834 Component_Associations (E2));
8836 when N_Function_Call =>
8838 FCE (Name (E1), Name (E2))
8840 FCL (Parameter_Associations (E1),
8841 Parameter_Associations (E2));
8843 when N_If_Expression =>
8845 FCL (Expressions (E1), Expressions (E2));
8847 when N_Indexed_Component =>
8849 FCE (Prefix (E1), Prefix (E2))
8851 FCL (Expressions (E1), Expressions (E2));
8853 when N_Integer_Literal =>
8854 return (Intval (E1) = Intval (E2));
8859 when N_Operator_Symbol =>
8861 Chars (E1) = Chars (E2);
8863 when N_Others_Choice =>
8866 when N_Parameter_Association =>
8868 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8869 and then FCE (Explicit_Actual_Parameter (E1),
8870 Explicit_Actual_Parameter (E2));
8872 when N_Qualified_Expression =>
8874 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8876 FCE (Expression (E1), Expression (E2));
8878 when N_Quantified_Expression =>
8879 if not FCE (Condition (E1), Condition (E2)) then
8883 if Present (Loop_Parameter_Specification (E1))
8884 and then Present (Loop_Parameter_Specification (E2))
8887 L1 : constant Node_Id :=
8888 Loop_Parameter_Specification (E1);
8889 L2 : constant Node_Id :=
8890 Loop_Parameter_Specification (E2);
8894 Reverse_Present (L1) = Reverse_Present (L2)
8896 FCE (Defining_Identifier (L1),
8897 Defining_Identifier (L2))
8899 FCE (Discrete_Subtype_Definition (L1),
8900 Discrete_Subtype_Definition (L2));
8903 elsif Present (Iterator_Specification (E1))
8904 and then Present (Iterator_Specification (E2))
8907 I1 : constant Node_Id := Iterator_Specification (E1);
8908 I2 : constant Node_Id := Iterator_Specification (E2);
8912 FCE (Defining_Identifier (I1),
8913 Defining_Identifier (I2))
8915 Of_Present (I1) = Of_Present (I2)
8917 Reverse_Present (I1) = Reverse_Present (I2)
8918 and then FCE (Name (I1), Name (I2))
8919 and then FCE (Subtype_Indication (I1),
8920 Subtype_Indication (I2));
8923 -- The quantified expressions used different specifications to
8924 -- walk their respective ranges.
8932 FCE (Low_Bound (E1), Low_Bound (E2))
8934 FCE (High_Bound (E1), High_Bound (E2));
8936 when N_Real_Literal =>
8937 return (Realval (E1) = Realval (E2));
8939 when N_Selected_Component =>
8941 FCE (Prefix (E1), Prefix (E2))
8943 FCE (Selector_Name (E1), Selector_Name (E2));
8947 FCE (Prefix (E1), Prefix (E2))
8949 FCE (Discrete_Range (E1), Discrete_Range (E2));
8951 when N_String_Literal =>
8953 S1 : constant String_Id := Strval (E1);
8954 S2 : constant String_Id := Strval (E2);
8955 L1 : constant Nat := String_Length (S1);
8956 L2 : constant Nat := String_Length (S2);
8963 for J in 1 .. L1 loop
8964 if Get_String_Char (S1, J) /=
8965 Get_String_Char (S2, J)
8975 when N_Type_Conversion =>
8977 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8979 FCE (Expression (E1), Expression (E2));
8983 Entity (E1) = Entity (E2)
8985 FCE (Right_Opnd (E1), Right_Opnd (E2));
8987 when N_Unchecked_Type_Conversion =>
8989 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8991 FCE (Expression (E1), Expression (E2));
8993 -- All other node types cannot appear in this context. Strictly
8994 -- we should raise a fatal internal error. Instead we just ignore
8995 -- the nodes. This means that if anyone makes a mistake in the
8996 -- expander and mucks an expression tree irretrievably, the
8997 -- result will be a failure to detect a (probably very obscure)
8998 -- case of non-conformance, which is better than bombing on some
8999 -- case where two expressions do in fact conform.
9006 end Fully_Conformant_Expressions;
9008 ----------------------------------------
9009 -- Fully_Conformant_Discrete_Subtypes --
9010 ----------------------------------------
9012 function Fully_Conformant_Discrete_Subtypes
9013 (Given_S1 : Node_Id;
9014 Given_S2 : Node_Id) return Boolean
9016 S1 : constant Node_Id := Original_Node (Given_S1);
9017 S2 : constant Node_Id := Original_Node (Given_S2);
9019 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9020 -- Special-case for a bound given by a discriminant, which in the body
9021 -- is replaced with the discriminal of the enclosing type.
9023 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9024 -- Check both bounds
9026 -----------------------
9027 -- Conforming_Bounds --
9028 -----------------------
9030 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9032 if Is_Entity_Name (B1)
9033 and then Is_Entity_Name (B2)
9034 and then Ekind (Entity (B1)) = E_Discriminant
9036 return Chars (B1) = Chars (B2);
9039 return Fully_Conformant_Expressions (B1, B2);
9041 end Conforming_Bounds;
9043 -----------------------
9044 -- Conforming_Ranges --
9045 -----------------------
9047 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9050 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9052 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9053 end Conforming_Ranges;
9055 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9058 if Nkind (S1) /= Nkind (S2) then
9061 elsif Is_Entity_Name (S1) then
9062 return Entity (S1) = Entity (S2);
9064 elsif Nkind (S1) = N_Range then
9065 return Conforming_Ranges (S1, S2);
9067 elsif Nkind (S1) = N_Subtype_Indication then
9069 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9072 (Range_Expression (Constraint (S1)),
9073 Range_Expression (Constraint (S2)));
9077 end Fully_Conformant_Discrete_Subtypes;
9079 --------------------
9080 -- Install_Entity --
9081 --------------------
9083 procedure Install_Entity (E : Entity_Id) is
9084 Prev : constant Entity_Id := Current_Entity (E);
9086 Set_Is_Immediately_Visible (E);
9087 Set_Current_Entity (E);
9088 Set_Homonym (E, Prev);
9091 ---------------------
9092 -- Install_Formals --
9093 ---------------------
9095 procedure Install_Formals (Id : Entity_Id) is
9098 F := First_Formal (Id);
9099 while Present (F) loop
9103 end Install_Formals;
9105 -----------------------------
9106 -- Is_Interface_Conformant --
9107 -----------------------------
9109 function Is_Interface_Conformant
9110 (Tagged_Type : Entity_Id;
9111 Iface_Prim : Entity_Id;
9112 Prim : Entity_Id) return Boolean
9114 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
9115 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9117 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9118 -- Return the controlling formal of Prim
9120 ------------------------
9121 -- Controlling_Formal --
9122 ------------------------
9124 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9125 E : Entity_Id := First_Entity (Prim);
9128 while Present (E) loop
9129 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9137 end Controlling_Formal;
9141 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9142 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9144 -- Start of processing for Is_Interface_Conformant
9147 pragma Assert (Is_Subprogram (Iface_Prim)
9148 and then Is_Subprogram (Prim)
9149 and then Is_Dispatching_Operation (Iface_Prim)
9150 and then Is_Dispatching_Operation (Prim));
9152 pragma Assert (Is_Interface (Iface)
9153 or else (Present (Alias (Iface_Prim))
9156 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9158 if Prim = Iface_Prim
9159 or else not Is_Subprogram (Prim)
9160 or else Ekind (Prim) /= Ekind (Iface_Prim)
9161 or else not Is_Dispatching_Operation (Prim)
9162 or else Scope (Prim) /= Scope (Tagged_Type)
9164 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9165 or else not Primitive_Names_Match (Iface_Prim, Prim)
9169 -- The mode of the controlling formals must match
9171 elsif Present (Iface_Ctrl_F)
9172 and then Present (Prim_Ctrl_F)
9173 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9177 -- Case of a procedure, or a function whose result type matches the
9178 -- result type of the interface primitive, or a function that has no
9179 -- controlling result (I or access I).
9181 elsif Ekind (Iface_Prim) = E_Procedure
9182 or else Etype (Prim) = Etype (Iface_Prim)
9183 or else not Has_Controlling_Result (Prim)
9185 return Type_Conformant
9186 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9188 -- Case of a function returning an interface, or an access to one.
9189 -- Check that the return types correspond.
9191 elsif Implements_Interface (Typ, Iface) then
9192 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9194 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9199 Type_Conformant (Prim, Iface_Prim,
9200 Skip_Controlling_Formals => True);
9206 end Is_Interface_Conformant;
9208 ---------------------------------
9209 -- Is_Non_Overriding_Operation --
9210 ---------------------------------
9212 function Is_Non_Overriding_Operation
9213 (Prev_E : Entity_Id;
9214 New_E : Entity_Id) return Boolean
9218 G_Typ : Entity_Id := Empty;
9220 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9221 -- If F_Type is a derived type associated with a generic actual subtype,
9222 -- then return its Generic_Parent_Type attribute, else return Empty.
9224 function Types_Correspond
9225 (P_Type : Entity_Id;
9226 N_Type : Entity_Id) return Boolean;
9227 -- Returns true if and only if the types (or designated types in the
9228 -- case of anonymous access types) are the same or N_Type is derived
9229 -- directly or indirectly from P_Type.
9231 -----------------------------
9232 -- Get_Generic_Parent_Type --
9233 -----------------------------
9235 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9241 if Is_Derived_Type (F_Typ)
9242 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9244 -- The tree must be traversed to determine the parent subtype in
9245 -- the generic unit, which unfortunately isn't always available
9246 -- via semantic attributes. ??? (Note: The use of Original_Node
9247 -- is needed for cases where a full derived type has been
9250 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9251 if Nkind (Defn) = N_Derived_Type_Definition then
9252 Indic := Subtype_Indication (Defn);
9254 if Nkind (Indic) = N_Subtype_Indication then
9255 G_Typ := Entity (Subtype_Mark (Indic));
9257 G_Typ := Entity (Indic);
9260 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9261 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9263 return Generic_Parent_Type (Parent (G_Typ));
9269 end Get_Generic_Parent_Type;
9271 ----------------------
9272 -- Types_Correspond --
9273 ----------------------
9275 function Types_Correspond
9276 (P_Type : Entity_Id;
9277 N_Type : Entity_Id) return Boolean
9279 Prev_Type : Entity_Id := Base_Type (P_Type);
9280 New_Type : Entity_Id := Base_Type (N_Type);
9283 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9284 Prev_Type := Designated_Type (Prev_Type);
9287 if Ekind (New_Type) = E_Anonymous_Access_Type then
9288 New_Type := Designated_Type (New_Type);
9291 if Prev_Type = New_Type then
9294 elsif not Is_Class_Wide_Type (New_Type) then
9295 while Etype (New_Type) /= New_Type loop
9296 New_Type := Etype (New_Type);
9297 if New_Type = Prev_Type then
9303 end Types_Correspond;
9305 -- Start of processing for Is_Non_Overriding_Operation
9308 -- In the case where both operations are implicit derived subprograms
9309 -- then neither overrides the other. This can only occur in certain
9310 -- obscure cases (e.g., derivation from homographs created in a generic
9313 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9316 elsif Ekind (Current_Scope) = E_Package
9317 and then Is_Generic_Instance (Current_Scope)
9318 and then In_Private_Part (Current_Scope)
9319 and then Comes_From_Source (New_E)
9321 -- We examine the formals and result type of the inherited operation,
9322 -- to determine whether their type is derived from (the instance of)
9323 -- a generic type. The first such formal or result type is the one
9326 Formal := First_Formal (Prev_E);
9327 while Present (Formal) loop
9328 F_Typ := Base_Type (Etype (Formal));
9330 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9331 F_Typ := Designated_Type (F_Typ);
9334 G_Typ := Get_Generic_Parent_Type (F_Typ);
9335 exit when Present (G_Typ);
9337 Next_Formal (Formal);
9340 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9341 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9348 -- If the generic type is a private type, then the original operation
9349 -- was not overriding in the generic, because there was no primitive
9350 -- operation to override.
9352 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9353 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9354 N_Formal_Private_Type_Definition
9358 -- The generic parent type is the ancestor of a formal derived
9359 -- type declaration. We need to check whether it has a primitive
9360 -- operation that should be overridden by New_E in the generic.
9364 P_Formal : Entity_Id;
9365 N_Formal : Entity_Id;
9369 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9372 while Present (Prim_Elt) loop
9373 P_Prim := Node (Prim_Elt);
9375 if Chars (P_Prim) = Chars (New_E)
9376 and then Ekind (P_Prim) = Ekind (New_E)
9378 P_Formal := First_Formal (P_Prim);
9379 N_Formal := First_Formal (New_E);
9380 while Present (P_Formal) and then Present (N_Formal) loop
9381 P_Typ := Etype (P_Formal);
9382 N_Typ := Etype (N_Formal);
9384 if not Types_Correspond (P_Typ, N_Typ) then
9388 Next_Entity (P_Formal);
9389 Next_Entity (N_Formal);
9392 -- Found a matching primitive operation belonging to the
9393 -- formal ancestor type, so the new subprogram is
9397 and then No (N_Formal)
9398 and then (Ekind (New_E) /= E_Function
9401 (Etype (P_Prim), Etype (New_E)))
9407 Next_Elmt (Prim_Elt);
9410 -- If no match found, then the new subprogram does not
9411 -- override in the generic (nor in the instance).
9413 -- If the type in question is not abstract, and the subprogram
9414 -- is, this will be an error if the new operation is in the
9415 -- private part of the instance. Emit a warning now, which will
9416 -- make the subsequent error message easier to understand.
9418 if not Is_Abstract_Type (F_Typ)
9419 and then Is_Abstract_Subprogram (Prev_E)
9420 and then In_Private_Part (Current_Scope)
9422 Error_Msg_Node_2 := F_Typ;
9424 ("private operation& in generic unit does not override " &
9425 "any primitive operation of& (RM 12.3 (18))??",
9435 end Is_Non_Overriding_Operation;
9437 -------------------------------------
9438 -- List_Inherited_Pre_Post_Aspects --
9439 -------------------------------------
9441 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9443 if Opt.List_Inherited_Aspects
9444 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9447 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9451 for J in Inherited'Range loop
9452 P := Spec_PPC_List (Contract (Inherited (J)));
9453 while Present (P) loop
9454 Error_Msg_Sloc := Sloc (P);
9456 if Class_Present (P) and then not Split_PPC (P) then
9457 if Pragma_Name (P) = Name_Precondition then
9459 ("info: & inherits `Pre''Class` aspect from #?L?",
9463 ("info: & inherits `Post''Class` aspect from #?L?",
9468 P := Next_Pragma (P);
9473 end List_Inherited_Pre_Post_Aspects;
9475 ------------------------------
9476 -- Make_Inequality_Operator --
9477 ------------------------------
9479 -- S is the defining identifier of an equality operator. We build a
9480 -- subprogram declaration with the right signature. This operation is
9481 -- intrinsic, because it is always expanded as the negation of the
9482 -- call to the equality function.
9484 procedure Make_Inequality_Operator (S : Entity_Id) is
9485 Loc : constant Source_Ptr := Sloc (S);
9488 Op_Name : Entity_Id;
9490 FF : constant Entity_Id := First_Formal (S);
9491 NF : constant Entity_Id := Next_Formal (FF);
9494 -- Check that equality was properly defined, ignore call if not
9501 A : constant Entity_Id :=
9502 Make_Defining_Identifier (Sloc (FF),
9503 Chars => Chars (FF));
9505 B : constant Entity_Id :=
9506 Make_Defining_Identifier (Sloc (NF),
9507 Chars => Chars (NF));
9510 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9512 Formals := New_List (
9513 Make_Parameter_Specification (Loc,
9514 Defining_Identifier => A,
9516 New_Reference_To (Etype (First_Formal (S)),
9517 Sloc (Etype (First_Formal (S))))),
9519 Make_Parameter_Specification (Loc,
9520 Defining_Identifier => B,
9522 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9523 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9526 Make_Subprogram_Declaration (Loc,
9528 Make_Function_Specification (Loc,
9529 Defining_Unit_Name => Op_Name,
9530 Parameter_Specifications => Formals,
9531 Result_Definition =>
9532 New_Reference_To (Standard_Boolean, Loc)));
9534 -- Insert inequality right after equality if it is explicit or after
9535 -- the derived type when implicit. These entities are created only
9536 -- for visibility purposes, and eventually replaced in the course of
9537 -- expansion, so they do not need to be attached to the tree and seen
9538 -- by the back-end. Keeping them internal also avoids spurious
9539 -- freezing problems. The declaration is inserted in the tree for
9540 -- analysis, and removed afterwards. If the equality operator comes
9541 -- from an explicit declaration, attach the inequality immediately
9542 -- after. Else the equality is inherited from a derived type
9543 -- declaration, so insert inequality after that declaration.
9545 if No (Alias (S)) then
9546 Insert_After (Unit_Declaration_Node (S), Decl);
9547 elsif Is_List_Member (Parent (S)) then
9548 Insert_After (Parent (S), Decl);
9550 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9553 Mark_Rewrite_Insertion (Decl);
9554 Set_Is_Intrinsic_Subprogram (Op_Name);
9557 Set_Has_Completion (Op_Name);
9558 Set_Corresponding_Equality (Op_Name, S);
9559 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9561 end Make_Inequality_Operator;
9563 ----------------------
9564 -- May_Need_Actuals --
9565 ----------------------
9567 procedure May_Need_Actuals (Fun : Entity_Id) is
9572 F := First_Formal (Fun);
9574 while Present (F) loop
9575 if No (Default_Value (F)) then
9583 Set_Needs_No_Actuals (Fun, B);
9584 end May_Need_Actuals;
9586 ---------------------
9587 -- Mode_Conformant --
9588 ---------------------
9590 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9593 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9595 end Mode_Conformant;
9597 ---------------------------
9598 -- New_Overloaded_Entity --
9599 ---------------------------
9601 procedure New_Overloaded_Entity
9603 Derived_Type : Entity_Id := Empty)
9605 Overridden_Subp : Entity_Id := Empty;
9606 -- Set if the current scope has an operation that is type-conformant
9607 -- with S, and becomes hidden by S.
9609 Is_Primitive_Subp : Boolean;
9610 -- Set to True if the new subprogram is primitive
9613 -- Entity that S overrides
9615 Prev_Vis : Entity_Id := Empty;
9616 -- Predecessor of E in Homonym chain
9618 procedure Check_For_Primitive_Subprogram
9619 (Is_Primitive : out Boolean;
9620 Is_Overriding : Boolean := False);
9621 -- If the subprogram being analyzed is a primitive operation of the type
9622 -- of a formal or result, set the Has_Primitive_Operations flag on the
9623 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9624 -- corresponding flag on the entity itself for later use.
9626 procedure Check_Synchronized_Overriding
9627 (Def_Id : Entity_Id;
9628 Overridden_Subp : out Entity_Id);
9629 -- First determine if Def_Id is an entry or a subprogram either defined
9630 -- in the scope of a task or protected type, or is a primitive of such
9631 -- a type. Check whether Def_Id overrides a subprogram of an interface
9632 -- implemented by the synchronized type, return the overridden entity
9635 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9636 -- Check that E is declared in the private part of the current package,
9637 -- or in the package body, where it may hide a previous declaration.
9638 -- We can't use In_Private_Part by itself because this flag is also
9639 -- set when freezing entities, so we must examine the place of the
9640 -- declaration in the tree, and recognize wrapper packages as well.
9642 function Is_Overriding_Alias
9644 New_E : Entity_Id) return Boolean;
9645 -- Check whether new subprogram and old subprogram are both inherited
9646 -- from subprograms that have distinct dispatch table entries. This can
9647 -- occur with derivations from instances with accidental homonyms.
9648 -- The function is conservative given that the converse is only true
9649 -- within instances that contain accidental overloadings.
9651 ------------------------------------
9652 -- Check_For_Primitive_Subprogram --
9653 ------------------------------------
9655 procedure Check_For_Primitive_Subprogram
9656 (Is_Primitive : out Boolean;
9657 Is_Overriding : Boolean := False)
9663 function Visible_Part_Type (T : Entity_Id) return Boolean;
9664 -- Returns true if T is declared in the visible part of the current
9665 -- package scope; otherwise returns false. Assumes that T is declared
9668 procedure Check_Private_Overriding (T : Entity_Id);
9669 -- Checks that if a primitive abstract subprogram of a visible
9670 -- abstract type is declared in a private part, then it must override
9671 -- an abstract subprogram declared in the visible part. Also checks
9672 -- that if a primitive function with a controlling result is declared
9673 -- in a private part, then it must override a function declared in
9674 -- the visible part.
9676 ------------------------------
9677 -- Check_Private_Overriding --
9678 ------------------------------
9680 procedure Check_Private_Overriding (T : Entity_Id) is
9682 if Is_Package_Or_Generic_Package (Current_Scope)
9683 and then In_Private_Part (Current_Scope)
9684 and then Visible_Part_Type (T)
9685 and then not In_Instance
9687 if Is_Abstract_Type (T)
9688 and then Is_Abstract_Subprogram (S)
9689 and then (not Is_Overriding
9690 or else not Is_Abstract_Subprogram (E))
9693 ("abstract subprograms must be visible "
9694 & "(RM 3.9.3(10))!", S);
9696 elsif Ekind (S) = E_Function and then not Is_Overriding then
9697 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9699 ("private function with tagged result must"
9700 & " override visible-part function", S);
9702 ("\move subprogram to the visible part"
9703 & " (RM 3.9.3(10))", S);
9705 -- AI05-0073: extend this test to the case of a function
9706 -- with a controlling access result.
9708 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9709 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9711 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9712 and then Ada_Version >= Ada_2012
9715 ("private function with controlling access result "
9716 & "must override visible-part function", S);
9718 ("\move subprogram to the visible part"
9719 & " (RM 3.9.3(10))", S);
9723 end Check_Private_Overriding;
9725 -----------------------
9726 -- Visible_Part_Type --
9727 -----------------------
9729 function Visible_Part_Type (T : Entity_Id) return Boolean is
9730 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9734 -- If the entity is a private type, then it must be declared in a
9737 if Ekind (T) in Private_Kind then
9741 -- Otherwise, we traverse the visible part looking for its
9742 -- corresponding declaration. We cannot use the declaration
9743 -- node directly because in the private part the entity of a
9744 -- private type is the one in the full view, which does not
9745 -- indicate that it is the completion of something visible.
9747 N := First (Visible_Declarations (Specification (P)));
9748 while Present (N) loop
9749 if Nkind (N) = N_Full_Type_Declaration
9750 and then Present (Defining_Identifier (N))
9751 and then T = Defining_Identifier (N)
9755 elsif Nkind_In (N, N_Private_Type_Declaration,
9756 N_Private_Extension_Declaration)
9757 and then Present (Defining_Identifier (N))
9758 and then T = Full_View (Defining_Identifier (N))
9767 end Visible_Part_Type;
9769 -- Start of processing for Check_For_Primitive_Subprogram
9772 Is_Primitive := False;
9774 if not Comes_From_Source (S) then
9777 -- If subprogram is at library level, it is not primitive operation
9779 elsif Current_Scope = Standard_Standard then
9782 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9783 and then not In_Package_Body (Current_Scope))
9784 or else Is_Overriding
9786 -- For function, check return type
9788 if Ekind (S) = E_Function then
9789 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9790 F_Typ := Designated_Type (Etype (S));
9795 B_Typ := Base_Type (F_Typ);
9797 if Scope (B_Typ) = Current_Scope
9798 and then not Is_Class_Wide_Type (B_Typ)
9799 and then not Is_Generic_Type (B_Typ)
9801 Is_Primitive := True;
9802 Set_Has_Primitive_Operations (B_Typ);
9803 Set_Is_Primitive (S);
9804 Check_Private_Overriding (B_Typ);
9808 -- For all subprograms, check formals
9810 Formal := First_Formal (S);
9811 while Present (Formal) loop
9812 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9813 F_Typ := Designated_Type (Etype (Formal));
9815 F_Typ := Etype (Formal);
9818 B_Typ := Base_Type (F_Typ);
9820 if Ekind (B_Typ) = E_Access_Subtype then
9821 B_Typ := Base_Type (B_Typ);
9824 if Scope (B_Typ) = Current_Scope
9825 and then not Is_Class_Wide_Type (B_Typ)
9826 and then not Is_Generic_Type (B_Typ)
9828 Is_Primitive := True;
9829 Set_Is_Primitive (S);
9830 Set_Has_Primitive_Operations (B_Typ);
9831 Check_Private_Overriding (B_Typ);
9834 Next_Formal (Formal);
9837 -- Special case: An equality function can be redefined for a type
9838 -- occurring in a declarative part, and won't otherwise be treated as
9839 -- a primitive because it doesn't occur in a package spec and doesn't
9840 -- override an inherited subprogram. It's important that we mark it
9841 -- primitive so it can be returned by Collect_Primitive_Operations
9842 -- and be used in composing the equality operation of later types
9843 -- that have a component of the type.
9845 elsif Chars (S) = Name_Op_Eq
9846 and then Etype (S) = Standard_Boolean
9848 B_Typ := Base_Type (Etype (First_Formal (S)));
9850 if Scope (B_Typ) = Current_Scope
9852 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9853 and then not Is_Limited_Type (B_Typ)
9855 Is_Primitive := True;
9856 Set_Is_Primitive (S);
9857 Set_Has_Primitive_Operations (B_Typ);
9858 Check_Private_Overriding (B_Typ);
9861 end Check_For_Primitive_Subprogram;
9863 -----------------------------------
9864 -- Check_Synchronized_Overriding --
9865 -----------------------------------
9867 procedure Check_Synchronized_Overriding
9868 (Def_Id : Entity_Id;
9869 Overridden_Subp : out Entity_Id)
9871 Ifaces_List : Elist_Id;
9875 function Matches_Prefixed_View_Profile
9876 (Prim_Params : List_Id;
9877 Iface_Params : List_Id) return Boolean;
9878 -- Determine whether a subprogram's parameter profile Prim_Params
9879 -- matches that of a potentially overridden interface subprogram
9880 -- Iface_Params. Also determine if the type of first parameter of
9881 -- Iface_Params is an implemented interface.
9883 -----------------------------------
9884 -- Matches_Prefixed_View_Profile --
9885 -----------------------------------
9887 function Matches_Prefixed_View_Profile
9888 (Prim_Params : List_Id;
9889 Iface_Params : List_Id) return Boolean
9891 Iface_Id : Entity_Id;
9892 Iface_Param : Node_Id;
9893 Iface_Typ : Entity_Id;
9894 Prim_Id : Entity_Id;
9895 Prim_Param : Node_Id;
9896 Prim_Typ : Entity_Id;
9898 function Is_Implemented
9899 (Ifaces_List : Elist_Id;
9900 Iface : Entity_Id) return Boolean;
9901 -- Determine if Iface is implemented by the current task or
9904 --------------------
9905 -- Is_Implemented --
9906 --------------------
9908 function Is_Implemented
9909 (Ifaces_List : Elist_Id;
9910 Iface : Entity_Id) return Boolean
9912 Iface_Elmt : Elmt_Id;
9915 Iface_Elmt := First_Elmt (Ifaces_List);
9916 while Present (Iface_Elmt) loop
9917 if Node (Iface_Elmt) = Iface then
9921 Next_Elmt (Iface_Elmt);
9927 -- Start of processing for Matches_Prefixed_View_Profile
9930 Iface_Param := First (Iface_Params);
9931 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9933 if Is_Access_Type (Iface_Typ) then
9934 Iface_Typ := Designated_Type (Iface_Typ);
9937 Prim_Param := First (Prim_Params);
9939 -- The first parameter of the potentially overridden subprogram
9940 -- must be an interface implemented by Prim.
9942 if not Is_Interface (Iface_Typ)
9943 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9948 -- The checks on the object parameters are done, move onto the
9949 -- rest of the parameters.
9951 if not In_Scope then
9952 Prim_Param := Next (Prim_Param);
9955 Iface_Param := Next (Iface_Param);
9956 while Present (Iface_Param) and then Present (Prim_Param) loop
9957 Iface_Id := Defining_Identifier (Iface_Param);
9958 Iface_Typ := Find_Parameter_Type (Iface_Param);
9960 Prim_Id := Defining_Identifier (Prim_Param);
9961 Prim_Typ := Find_Parameter_Type (Prim_Param);
9963 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9964 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9965 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9967 Iface_Typ := Designated_Type (Iface_Typ);
9968 Prim_Typ := Designated_Type (Prim_Typ);
9971 -- Case of multiple interface types inside a parameter profile
9973 -- (Obj_Param : in out Iface; ...; Param : Iface)
9975 -- If the interface type is implemented, then the matching type
9976 -- in the primitive should be the implementing record type.
9978 if Ekind (Iface_Typ) = E_Record_Type
9979 and then Is_Interface (Iface_Typ)
9980 and then Is_Implemented (Ifaces_List, Iface_Typ)
9982 if Prim_Typ /= Typ then
9986 -- The two parameters must be both mode and subtype conformant
9988 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9990 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9999 -- One of the two lists contains more parameters than the other
10001 if Present (Iface_Param) or else Present (Prim_Param) then
10006 end Matches_Prefixed_View_Profile;
10008 -- Start of processing for Check_Synchronized_Overriding
10011 Overridden_Subp := Empty;
10013 -- Def_Id must be an entry or a subprogram. We should skip predefined
10014 -- primitives internally generated by the frontend; however at this
10015 -- stage predefined primitives are still not fully decorated. As a
10016 -- minor optimization we skip here internally generated subprograms.
10018 if (Ekind (Def_Id) /= E_Entry
10019 and then Ekind (Def_Id) /= E_Function
10020 and then Ekind (Def_Id) /= E_Procedure)
10021 or else not Comes_From_Source (Def_Id)
10026 -- Search for the concurrent declaration since it contains the list
10027 -- of all implemented interfaces. In this case, the subprogram is
10028 -- declared within the scope of a protected or a task type.
10030 if Present (Scope (Def_Id))
10031 and then Is_Concurrent_Type (Scope (Def_Id))
10032 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10034 Typ := Scope (Def_Id);
10037 -- The enclosing scope is not a synchronized type and the subprogram
10040 elsif No (First_Formal (Def_Id)) then
10043 -- The subprogram has formals and hence it may be a primitive of a
10044 -- concurrent type.
10047 Typ := Etype (First_Formal (Def_Id));
10049 if Is_Access_Type (Typ) then
10050 Typ := Directly_Designated_Type (Typ);
10053 if Is_Concurrent_Type (Typ)
10054 and then not Is_Generic_Actual_Type (Typ)
10058 -- This case occurs when the concurrent type is declared within
10059 -- a generic unit. As a result the corresponding record has been
10060 -- built and used as the type of the first formal, we just have
10061 -- to retrieve the corresponding concurrent type.
10063 elsif Is_Concurrent_Record_Type (Typ)
10064 and then not Is_Class_Wide_Type (Typ)
10065 and then Present (Corresponding_Concurrent_Type (Typ))
10067 Typ := Corresponding_Concurrent_Type (Typ);
10075 -- There is no overriding to check if is an inherited operation in a
10076 -- type derivation on for a generic actual.
10078 Collect_Interfaces (Typ, Ifaces_List);
10080 if Is_Empty_Elmt_List (Ifaces_List) then
10084 -- Determine whether entry or subprogram Def_Id overrides a primitive
10085 -- operation that belongs to one of the interfaces in Ifaces_List.
10088 Candidate : Entity_Id := Empty;
10089 Hom : Entity_Id := Empty;
10090 Iface_Typ : Entity_Id;
10091 Subp : Entity_Id := Empty;
10094 -- Traverse the homonym chain, looking for a potentially
10095 -- overridden subprogram that belongs to an implemented
10098 Hom := Current_Entity_In_Scope (Def_Id);
10099 while Present (Hom) loop
10103 or else not Is_Overloadable (Subp)
10104 or else not Is_Primitive (Subp)
10105 or else not Is_Dispatching_Operation (Subp)
10106 or else not Present (Find_Dispatching_Type (Subp))
10107 or else not Is_Interface (Find_Dispatching_Type (Subp))
10111 -- Entries and procedures can override abstract or null
10112 -- interface procedures.
10114 elsif (Ekind (Def_Id) = E_Procedure
10115 or else Ekind (Def_Id) = E_Entry)
10116 and then Ekind (Subp) = E_Procedure
10117 and then Matches_Prefixed_View_Profile
10118 (Parameter_Specifications (Parent (Def_Id)),
10119 Parameter_Specifications (Parent (Subp)))
10123 -- For an overridden subprogram Subp, check whether the mode
10124 -- of its first parameter is correct depending on the kind
10125 -- of synchronized type.
10128 Formal : constant Node_Id := First_Formal (Candidate);
10131 -- In order for an entry or a protected procedure to
10132 -- override, the first parameter of the overridden
10133 -- routine must be of mode "out", "in out" or
10134 -- access-to-variable.
10136 if Ekind_In (Candidate, E_Entry, E_Procedure)
10137 and then Is_Protected_Type (Typ)
10138 and then Ekind (Formal) /= E_In_Out_Parameter
10139 and then Ekind (Formal) /= E_Out_Parameter
10140 and then Nkind (Parameter_Type (Parent (Formal))) /=
10141 N_Access_Definition
10145 -- All other cases are OK since a task entry or routine
10146 -- does not have a restriction on the mode of the first
10147 -- parameter of the overridden interface routine.
10150 Overridden_Subp := Candidate;
10155 -- Functions can override abstract interface functions
10157 elsif Ekind (Def_Id) = E_Function
10158 and then Ekind (Subp) = E_Function
10159 and then Matches_Prefixed_View_Profile
10160 (Parameter_Specifications (Parent (Def_Id)),
10161 Parameter_Specifications (Parent (Subp)))
10162 and then Etype (Result_Definition (Parent (Def_Id))) =
10163 Etype (Result_Definition (Parent (Subp)))
10165 Overridden_Subp := Subp;
10169 Hom := Homonym (Hom);
10172 -- After examining all candidates for overriding, we are left with
10173 -- the best match which is a mode incompatible interface routine.
10174 -- Do not emit an error if the Expander is active since this error
10175 -- will be detected later on after all concurrent types are
10176 -- expanded and all wrappers are built. This check is meant for
10177 -- spec-only compilations.
10179 if Present (Candidate) and then not Expander_Active then
10181 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10183 -- Def_Id is primitive of a protected type, declared inside the
10184 -- type, and the candidate is primitive of a limited or
10185 -- synchronized interface.
10188 and then Is_Protected_Type (Typ)
10190 (Is_Limited_Interface (Iface_Typ)
10191 or else Is_Protected_Interface (Iface_Typ)
10192 or else Is_Synchronized_Interface (Iface_Typ)
10193 or else Is_Task_Interface (Iface_Typ))
10195 Error_Msg_PT (Parent (Typ), Candidate);
10199 Overridden_Subp := Candidate;
10202 end Check_Synchronized_Overriding;
10204 ----------------------------
10205 -- Is_Private_Declaration --
10206 ----------------------------
10208 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10209 Priv_Decls : List_Id;
10210 Decl : constant Node_Id := Unit_Declaration_Node (E);
10213 if Is_Package_Or_Generic_Package (Current_Scope)
10214 and then In_Private_Part (Current_Scope)
10217 Private_Declarations
10218 (Specification (Unit_Declaration_Node (Current_Scope)));
10220 return In_Package_Body (Current_Scope)
10222 (Is_List_Member (Decl)
10223 and then List_Containing (Decl) = Priv_Decls)
10224 or else (Nkind (Parent (Decl)) = N_Package_Specification
10226 Is_Compilation_Unit
10227 (Defining_Entity (Parent (Decl)))
10228 and then List_Containing (Parent (Parent (Decl))) =
10233 end Is_Private_Declaration;
10235 --------------------------
10236 -- Is_Overriding_Alias --
10237 --------------------------
10239 function Is_Overriding_Alias
10240 (Old_E : Entity_Id;
10241 New_E : Entity_Id) return Boolean
10243 AO : constant Entity_Id := Alias (Old_E);
10244 AN : constant Entity_Id := Alias (New_E);
10247 return Scope (AO) /= Scope (AN)
10248 or else No (DTC_Entity (AO))
10249 or else No (DTC_Entity (AN))
10250 or else DT_Position (AO) = DT_Position (AN);
10251 end Is_Overriding_Alias;
10253 -- Start of processing for New_Overloaded_Entity
10256 -- We need to look for an entity that S may override. This must be a
10257 -- homonym in the current scope, so we look for the first homonym of
10258 -- S in the current scope as the starting point for the search.
10260 E := Current_Entity_In_Scope (S);
10262 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10263 -- They are directly added to the list of primitive operations of
10264 -- Derived_Type, unless this is a rederivation in the private part
10265 -- of an operation that was already derived in the visible part of
10266 -- the current package.
10268 if Ada_Version >= Ada_2005
10269 and then Present (Derived_Type)
10270 and then Present (Alias (S))
10271 and then Is_Dispatching_Operation (Alias (S))
10272 and then Present (Find_Dispatching_Type (Alias (S)))
10273 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10275 -- For private types, when the full-view is processed we propagate to
10276 -- the full view the non-overridden entities whose attribute "alias"
10277 -- references an interface primitive. These entities were added by
10278 -- Derive_Subprograms to ensure that interface primitives are
10281 -- Inside_Freeze_Actions is non zero when S corresponds with an
10282 -- internal entity that links an interface primitive with its
10283 -- covering primitive through attribute Interface_Alias (see
10284 -- Add_Internal_Interface_Entities).
10286 if Inside_Freezing_Actions = 0
10287 and then Is_Package_Or_Generic_Package (Current_Scope)
10288 and then In_Private_Part (Current_Scope)
10289 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10290 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10291 and then Full_View (Defining_Identifier (Parent (E)))
10292 = Defining_Identifier (Parent (S))
10293 and then Alias (E) = Alias (S)
10295 Check_Operation_From_Private_View (S, E);
10296 Set_Is_Dispatching_Operation (S);
10301 Enter_Overloaded_Entity (S);
10302 Check_Dispatching_Operation (S, Empty);
10303 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10309 -- If there is no homonym then this is definitely not overriding
10312 Enter_Overloaded_Entity (S);
10313 Check_Dispatching_Operation (S, Empty);
10314 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10316 -- If subprogram has an explicit declaration, check whether it
10317 -- has an overriding indicator.
10319 if Comes_From_Source (S) then
10320 Check_Synchronized_Overriding (S, Overridden_Subp);
10322 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10323 -- it may have overridden some hidden inherited primitive. Update
10324 -- Overridden_Subp to avoid spurious errors when checking the
10325 -- overriding indicator.
10327 if Ada_Version >= Ada_2012
10328 and then No (Overridden_Subp)
10329 and then Is_Dispatching_Operation (S)
10330 and then Present (Overridden_Operation (S))
10332 Overridden_Subp := Overridden_Operation (S);
10335 Check_Overriding_Indicator
10336 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10339 -- If there is a homonym that is not overloadable, then we have an
10340 -- error, except for the special cases checked explicitly below.
10342 elsif not Is_Overloadable (E) then
10344 -- Check for spurious conflict produced by a subprogram that has the
10345 -- same name as that of the enclosing generic package. The conflict
10346 -- occurs within an instance, between the subprogram and the renaming
10347 -- declaration for the package. After the subprogram, the package
10348 -- renaming declaration becomes hidden.
10350 if Ekind (E) = E_Package
10351 and then Present (Renamed_Object (E))
10352 and then Renamed_Object (E) = Current_Scope
10353 and then Nkind (Parent (Renamed_Object (E))) =
10354 N_Package_Specification
10355 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10358 Set_Is_Immediately_Visible (E, False);
10359 Enter_Overloaded_Entity (S);
10360 Set_Homonym (S, Homonym (E));
10361 Check_Dispatching_Operation (S, Empty);
10362 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10364 -- If the subprogram is implicit it is hidden by the previous
10365 -- declaration. However if it is dispatching, it must appear in the
10366 -- dispatch table anyway, because it can be dispatched to even if it
10367 -- cannot be called directly.
10369 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10370 Set_Scope (S, Current_Scope);
10372 if Is_Dispatching_Operation (Alias (S)) then
10373 Check_Dispatching_Operation (S, Empty);
10379 Error_Msg_Sloc := Sloc (E);
10381 -- Generate message, with useful additional warning if in generic
10383 if Is_Generic_Unit (E) then
10384 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10385 Error_Msg_N ("\& conflicts with declaration#", S);
10387 Error_Msg_N ("& conflicts with declaration#", S);
10393 -- E exists and is overloadable
10396 Check_Synchronized_Overriding (S, Overridden_Subp);
10398 -- Loop through E and its homonyms to determine if any of them is
10399 -- the candidate for overriding by S.
10401 while Present (E) loop
10403 -- Definitely not interesting if not in the current scope
10405 if Scope (E) /= Current_Scope then
10408 -- Ada 2012 (AI05-0165): For internally generated bodies of
10409 -- null procedures locate the internally generated spec. We
10410 -- enforce mode conformance since a tagged type may inherit
10411 -- from interfaces several null primitives which differ only
10412 -- in the mode of the formals.
10414 elsif not Comes_From_Source (S)
10415 and then Is_Null_Procedure (S)
10416 and then not Mode_Conformant (E, S)
10420 -- Check if we have type conformance
10422 elsif Type_Conformant (E, S) then
10424 -- If the old and new entities have the same profile and one
10425 -- is not the body of the other, then this is an error, unless
10426 -- one of them is implicitly declared.
10428 -- There are some cases when both can be implicit, for example
10429 -- when both a literal and a function that overrides it are
10430 -- inherited in a derivation, or when an inherited operation
10431 -- of a tagged full type overrides the inherited operation of
10432 -- a private extension. Ada 83 had a special rule for the
10433 -- literal case. In Ada 95, the later implicit operation hides
10434 -- the former, and the literal is always the former. In the
10435 -- odd case where both are derived operations declared at the
10436 -- same point, both operations should be declared, and in that
10437 -- case we bypass the following test and proceed to the next
10438 -- part. This can only occur for certain obscure cases in
10439 -- instances, when an operation on a type derived from a formal
10440 -- private type does not override a homograph inherited from
10441 -- the actual. In subsequent derivations of such a type, the
10442 -- DT positions of these operations remain distinct, if they
10445 if Present (Alias (S))
10446 and then (No (Alias (E))
10447 or else Comes_From_Source (E)
10448 or else Is_Abstract_Subprogram (S)
10450 (Is_Dispatching_Operation (E)
10451 and then Is_Overriding_Alias (E, S)))
10452 and then Ekind (E) /= E_Enumeration_Literal
10454 -- When an derived operation is overloaded it may be due to
10455 -- the fact that the full view of a private extension
10456 -- re-inherits. It has to be dealt with.
10458 if Is_Package_Or_Generic_Package (Current_Scope)
10459 and then In_Private_Part (Current_Scope)
10461 Check_Operation_From_Private_View (S, E);
10464 -- In any case the implicit operation remains hidden by the
10465 -- existing declaration, which is overriding. Indicate that
10466 -- E overrides the operation from which S is inherited.
10468 if Present (Alias (S)) then
10469 Set_Overridden_Operation (E, Alias (S));
10471 Set_Overridden_Operation (E, S);
10474 if Comes_From_Source (E) then
10475 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10480 -- Within an instance, the renaming declarations for actual
10481 -- subprograms may become ambiguous, but they do not hide each
10484 elsif Ekind (E) /= E_Entry
10485 and then not Comes_From_Source (E)
10486 and then not Is_Generic_Instance (E)
10487 and then (Present (Alias (E))
10488 or else Is_Intrinsic_Subprogram (E))
10489 and then (not In_Instance
10490 or else No (Parent (E))
10491 or else Nkind (Unit_Declaration_Node (E)) /=
10492 N_Subprogram_Renaming_Declaration)
10494 -- A subprogram child unit is not allowed to override an
10495 -- inherited subprogram (10.1.1(20)).
10497 if Is_Child_Unit (S) then
10499 ("child unit overrides inherited subprogram in parent",
10504 if Is_Non_Overriding_Operation (E, S) then
10505 Enter_Overloaded_Entity (S);
10507 if No (Derived_Type)
10508 or else Is_Tagged_Type (Derived_Type)
10510 Check_Dispatching_Operation (S, Empty);
10516 -- E is a derived operation or an internal operator which
10517 -- is being overridden. Remove E from further visibility.
10518 -- Furthermore, if E is a dispatching operation, it must be
10519 -- replaced in the list of primitive operations of its type
10520 -- (see Override_Dispatching_Operation).
10522 Overridden_Subp := E;
10528 Prev := First_Entity (Current_Scope);
10529 while Present (Prev) and then Next_Entity (Prev) /= E loop
10530 Next_Entity (Prev);
10533 -- It is possible for E to be in the current scope and
10534 -- yet not in the entity chain. This can only occur in a
10535 -- generic context where E is an implicit concatenation
10536 -- in the formal part, because in a generic body the
10537 -- entity chain starts with the formals.
10540 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10542 -- E must be removed both from the entity_list of the
10543 -- current scope, and from the visibility chain
10545 if Debug_Flag_E then
10546 Write_Str ("Override implicit operation ");
10547 Write_Int (Int (E));
10551 -- If E is a predefined concatenation, it stands for four
10552 -- different operations. As a result, a single explicit
10553 -- declaration does not hide it. In a possible ambiguous
10554 -- situation, Disambiguate chooses the user-defined op,
10555 -- so it is correct to retain the previous internal one.
10557 if Chars (E) /= Name_Op_Concat
10558 or else Ekind (E) /= E_Operator
10560 -- For nondispatching derived operations that are
10561 -- overridden by a subprogram declared in the private
10562 -- part of a package, we retain the derived subprogram
10563 -- but mark it as not immediately visible. If the
10564 -- derived operation was declared in the visible part
10565 -- then this ensures that it will still be visible
10566 -- outside the package with the proper signature
10567 -- (calls from outside must also be directed to this
10568 -- version rather than the overriding one, unlike the
10569 -- dispatching case). Calls from inside the package
10570 -- will still resolve to the overriding subprogram
10571 -- since the derived one is marked as not visible
10572 -- within the package.
10574 -- If the private operation is dispatching, we achieve
10575 -- the overriding by keeping the implicit operation
10576 -- but setting its alias to be the overriding one. In
10577 -- this fashion the proper body is executed in all
10578 -- cases, but the original signature is used outside
10581 -- If the overriding is not in the private part, we
10582 -- remove the implicit operation altogether.
10584 if Is_Private_Declaration (S) then
10585 if not Is_Dispatching_Operation (E) then
10586 Set_Is_Immediately_Visible (E, False);
10588 -- Work done in Override_Dispatching_Operation,
10589 -- so nothing else needs to be done here.
10595 -- Find predecessor of E in Homonym chain
10597 if E = Current_Entity (E) then
10600 Prev_Vis := Current_Entity (E);
10601 while Homonym (Prev_Vis) /= E loop
10602 Prev_Vis := Homonym (Prev_Vis);
10606 if Prev_Vis /= Empty then
10608 -- Skip E in the visibility chain
10610 Set_Homonym (Prev_Vis, Homonym (E));
10613 Set_Name_Entity_Id (Chars (E), Homonym (E));
10616 Set_Next_Entity (Prev, Next_Entity (E));
10618 if No (Next_Entity (Prev)) then
10619 Set_Last_Entity (Current_Scope, Prev);
10624 Enter_Overloaded_Entity (S);
10626 -- For entities generated by Derive_Subprograms the
10627 -- overridden operation is the inherited primitive
10628 -- (which is available through the attribute alias).
10630 if not (Comes_From_Source (E))
10631 and then Is_Dispatching_Operation (E)
10632 and then Find_Dispatching_Type (E) =
10633 Find_Dispatching_Type (S)
10634 and then Present (Alias (E))
10635 and then Comes_From_Source (Alias (E))
10637 Set_Overridden_Operation (S, Alias (E));
10639 -- Normal case of setting entity as overridden
10641 -- Note: Static_Initialization and Overridden_Operation
10642 -- attributes use the same field in subprogram entities.
10643 -- Static_Initialization is only defined for internal
10644 -- initialization procedures, where Overridden_Operation
10645 -- is irrelevant. Therefore the setting of this attribute
10646 -- must check whether the target is an init_proc.
10648 elsif not Is_Init_Proc (S) then
10649 Set_Overridden_Operation (S, E);
10652 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10654 -- If S is a user-defined subprogram or a null procedure
10655 -- expanded to override an inherited null procedure, or a
10656 -- predefined dispatching primitive then indicate that E
10657 -- overrides the operation from which S is inherited.
10659 if Comes_From_Source (S)
10661 (Present (Parent (S))
10663 Nkind (Parent (S)) = N_Procedure_Specification
10665 Null_Present (Parent (S)))
10667 (Present (Alias (E))
10669 Is_Predefined_Dispatching_Operation (Alias (E)))
10671 if Present (Alias (E)) then
10672 Set_Overridden_Operation (S, Alias (E));
10676 if Is_Dispatching_Operation (E) then
10678 -- An overriding dispatching subprogram inherits the
10679 -- convention of the overridden subprogram (AI-117).
10681 Set_Convention (S, Convention (E));
10682 Check_Dispatching_Operation (S, E);
10685 Check_Dispatching_Operation (S, Empty);
10688 Check_For_Primitive_Subprogram
10689 (Is_Primitive_Subp, Is_Overriding => True);
10690 goto Check_Inequality;
10693 -- Apparent redeclarations in instances can occur when two
10694 -- formal types get the same actual type. The subprograms in
10695 -- in the instance are legal, even if not callable from the
10696 -- outside. Calls from within are disambiguated elsewhere.
10697 -- For dispatching operations in the visible part, the usual
10698 -- rules apply, and operations with the same profile are not
10699 -- legal (B830001).
10701 elsif (In_Instance_Visible_Part
10702 and then not Is_Dispatching_Operation (E))
10703 or else In_Instance_Not_Visible
10707 -- Here we have a real error (identical profile)
10710 Error_Msg_Sloc := Sloc (E);
10712 -- Avoid cascaded errors if the entity appears in
10713 -- subsequent calls.
10715 Set_Scope (S, Current_Scope);
10717 -- Generate error, with extra useful warning for the case
10718 -- of a generic instance with no completion.
10720 if Is_Generic_Instance (S)
10721 and then not Has_Completion (E)
10724 ("instantiation cannot provide body for&", S);
10725 Error_Msg_N ("\& conflicts with declaration#", S);
10727 Error_Msg_N ("& conflicts with declaration#", S);
10734 -- If one subprogram has an access parameter and the other
10735 -- a parameter of an access type, calls to either might be
10736 -- ambiguous. Verify that parameters match except for the
10737 -- access parameter.
10739 if May_Hide_Profile then
10745 F1 := First_Formal (S);
10746 F2 := First_Formal (E);
10747 while Present (F1) and then Present (F2) loop
10748 if Is_Access_Type (Etype (F1)) then
10749 if not Is_Access_Type (Etype (F2))
10750 or else not Conforming_Types
10751 (Designated_Type (Etype (F1)),
10752 Designated_Type (Etype (F2)),
10755 May_Hide_Profile := False;
10759 not Conforming_Types
10760 (Etype (F1), Etype (F2), Type_Conformant)
10762 May_Hide_Profile := False;
10769 if May_Hide_Profile
10773 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10782 -- On exit, we know that S is a new entity
10784 Enter_Overloaded_Entity (S);
10785 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10786 Check_Overriding_Indicator
10787 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10789 -- Overloading is not allowed in SPARK, except for operators
10791 if Nkind (S) /= N_Defining_Operator_Symbol then
10792 Error_Msg_Sloc := Sloc (Homonym (S));
10793 Check_SPARK_Restriction
10794 ("overloading not allowed with entity#", S);
10797 -- If S is a derived operation for an untagged type then by
10798 -- definition it's not a dispatching operation (even if the parent
10799 -- operation was dispatching), so Check_Dispatching_Operation is not
10800 -- called in that case.
10802 if No (Derived_Type)
10803 or else Is_Tagged_Type (Derived_Type)
10805 Check_Dispatching_Operation (S, Empty);
10809 -- If this is a user-defined equality operator that is not a derived
10810 -- subprogram, create the corresponding inequality. If the operation is
10811 -- dispatching, the expansion is done elsewhere, and we do not create
10812 -- an explicit inequality operation.
10814 <<Check_Inequality>>
10815 if Chars (S) = Name_Op_Eq
10816 and then Etype (S) = Standard_Boolean
10817 and then Present (Parent (S))
10818 and then not Is_Dispatching_Operation (S)
10820 Make_Inequality_Operator (S);
10822 if Ada_Version >= Ada_2012 then
10823 Check_Untagged_Equality (S);
10826 end New_Overloaded_Entity;
10828 ---------------------
10829 -- Process_Formals --
10830 ---------------------
10832 procedure Process_Formals
10834 Related_Nod : Node_Id)
10836 Param_Spec : Node_Id;
10837 Formal : Entity_Id;
10838 Formal_Type : Entity_Id;
10842 Num_Out_Params : Nat := 0;
10843 First_Out_Param : Entity_Id := Empty;
10844 -- Used for setting Is_Only_Out_Parameter
10846 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10847 -- Determine whether an access type designates a type coming from a
10850 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10851 -- Check whether the default has a class-wide type. After analysis the
10852 -- default has the type of the formal, so we must also check explicitly
10853 -- for an access attribute.
10855 -------------------------------
10856 -- Designates_From_With_Type --
10857 -------------------------------
10859 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10860 Desig : Entity_Id := Typ;
10863 if Is_Access_Type (Desig) then
10864 Desig := Directly_Designated_Type (Desig);
10867 if Is_Class_Wide_Type (Desig) then
10868 Desig := Root_Type (Desig);
10872 Ekind (Desig) = E_Incomplete_Type and then From_With_Type (Desig);
10873 end Designates_From_With_Type;
10875 ---------------------------
10876 -- Is_Class_Wide_Default --
10877 ---------------------------
10879 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10881 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10882 or else (Nkind (D) = N_Attribute_Reference
10883 and then Attribute_Name (D) = Name_Access
10884 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10885 end Is_Class_Wide_Default;
10887 -- Start of processing for Process_Formals
10890 -- In order to prevent premature use of the formals in the same formal
10891 -- part, the Ekind is left undefined until all default expressions are
10892 -- analyzed. The Ekind is established in a separate loop at the end.
10894 Param_Spec := First (T);
10895 while Present (Param_Spec) loop
10896 Formal := Defining_Identifier (Param_Spec);
10897 Set_Never_Set_In_Source (Formal, True);
10898 Enter_Name (Formal);
10900 -- Case of ordinary parameters
10902 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10903 Find_Type (Parameter_Type (Param_Spec));
10904 Ptype := Parameter_Type (Param_Spec);
10906 if Ptype = Error then
10910 Formal_Type := Entity (Ptype);
10912 if Is_Incomplete_Type (Formal_Type)
10914 (Is_Class_Wide_Type (Formal_Type)
10915 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10917 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10918 -- primitive operations, as long as their completion is
10919 -- in the same declarative part. If in the private part
10920 -- this means that the type cannot be a Taft-amendment type.
10921 -- Check is done on package exit. For access to subprograms,
10922 -- the use is legal for Taft-amendment types.
10924 -- Ada 2012: tagged incomplete types are allowed as generic
10925 -- formal types. They do not introduce dependencies and the
10926 -- corresponding generic subprogram does not have a delayed
10927 -- freeze, because it does not need a freeze node.
10929 if Is_Tagged_Type (Formal_Type) then
10930 if Ekind (Scope (Current_Scope)) = E_Package
10931 and then not From_With_Type (Formal_Type)
10932 and then not Is_Generic_Type (Formal_Type)
10933 and then not Is_Class_Wide_Type (Formal_Type)
10936 (Parent (T), N_Access_Function_Definition,
10937 N_Access_Procedure_Definition)
10941 Private_Dependents (Base_Type (Formal_Type)));
10943 -- Freezing is delayed to ensure that Register_Prim
10944 -- will get called for this operation, which is needed
10945 -- in cases where static dispatch tables aren't built.
10946 -- (Note that the same is done for controlling access
10947 -- parameter cases in function Access_Definition.)
10949 Set_Has_Delayed_Freeze (Current_Scope);
10953 -- Special handling of Value_Type for CIL case
10955 elsif Is_Value_Type (Formal_Type) then
10958 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10959 N_Access_Procedure_Definition)
10961 -- AI05-0151: Tagged incomplete types are allowed in all
10962 -- formal parts. Untagged incomplete types are not allowed
10965 if Ada_Version >= Ada_2012 then
10966 if Is_Tagged_Type (Formal_Type) then
10969 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10974 ("invalid use of untagged incomplete type&",
10975 Ptype, Formal_Type);
10980 ("invalid use of incomplete type&",
10981 Param_Spec, Formal_Type);
10983 -- Further checks on the legality of incomplete types
10984 -- in formal parts are delayed until the freeze point
10985 -- of the enclosing subprogram or access to subprogram.
10989 elsif Ekind (Formal_Type) = E_Void then
10991 ("premature use of&",
10992 Parameter_Type (Param_Spec), Formal_Type);
10995 -- Ada 2012 (AI-142): Handle aliased parameters
10997 if Ada_Version >= Ada_2012
10998 and then Aliased_Present (Param_Spec)
11000 Set_Is_Aliased (Formal);
11003 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11004 -- declaration corresponding to the null-excluding type of the
11005 -- formal in the enclosing scope. Finally, replace the parameter
11006 -- type of the formal with the internal subtype.
11008 if Ada_Version >= Ada_2005
11009 and then Null_Exclusion_Present (Param_Spec)
11011 if not Is_Access_Type (Formal_Type) then
11013 ("`NOT NULL` allowed only for an access type", Param_Spec);
11016 if Can_Never_Be_Null (Formal_Type)
11017 and then Comes_From_Source (Related_Nod)
11020 ("`NOT NULL` not allowed (& already excludes null)",
11021 Param_Spec, Formal_Type);
11025 Create_Null_Excluding_Itype
11027 Related_Nod => Related_Nod,
11028 Scope_Id => Scope (Current_Scope));
11030 -- If the designated type of the itype is an itype that is
11031 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11032 -- on the access subtype, to prevent order-of-elaboration
11033 -- issues in the backend.
11036 -- type T is access procedure;
11037 -- procedure Op (O : not null T);
11039 if Is_Itype (Directly_Designated_Type (Formal_Type))
11041 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11043 Set_Has_Delayed_Freeze (Formal_Type);
11048 -- An access formal type
11052 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11054 -- No need to continue if we already notified errors
11056 if not Present (Formal_Type) then
11060 -- Ada 2005 (AI-254)
11063 AD : constant Node_Id :=
11064 Access_To_Subprogram_Definition
11065 (Parameter_Type (Param_Spec));
11067 if Present (AD) and then Protected_Present (AD) then
11069 Replace_Anonymous_Access_To_Protected_Subprogram
11075 Set_Etype (Formal, Formal_Type);
11077 -- Deal with default expression if present
11079 Default := Expression (Param_Spec);
11081 if Present (Default) then
11082 Check_SPARK_Restriction
11083 ("default expression is not allowed", Default);
11085 if Out_Present (Param_Spec) then
11087 ("default initialization only allowed for IN parameters",
11091 -- Do the special preanalysis of the expression (see section on
11092 -- "Handling of Default Expressions" in the spec of package Sem).
11094 Preanalyze_Spec_Expression (Default, Formal_Type);
11096 -- An access to constant cannot be the default for
11097 -- an access parameter that is an access to variable.
11099 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11100 and then not Is_Access_Constant (Formal_Type)
11101 and then Is_Access_Type (Etype (Default))
11102 and then Is_Access_Constant (Etype (Default))
11105 ("formal that is access to variable cannot be initialized " &
11106 "with an access-to-constant expression", Default);
11109 -- Check that the designated type of an access parameter's default
11110 -- is not a class-wide type unless the parameter's designated type
11111 -- is also class-wide.
11113 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11114 and then not Designates_From_With_Type (Formal_Type)
11115 and then Is_Class_Wide_Default (Default)
11116 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11119 ("access to class-wide expression not allowed here", Default);
11122 -- Check incorrect use of dynamically tagged expressions
11124 if Is_Tagged_Type (Formal_Type) then
11125 Check_Dynamically_Tagged_Expression
11127 Typ => Formal_Type,
11128 Related_Nod => Default);
11132 -- Ada 2005 (AI-231): Static checks
11134 if Ada_Version >= Ada_2005
11135 and then Is_Access_Type (Etype (Formal))
11136 and then Can_Never_Be_Null (Etype (Formal))
11138 Null_Exclusion_Static_Checks (Param_Spec);
11145 -- If this is the formal part of a function specification, analyze the
11146 -- subtype mark in the context where the formals are visible but not
11147 -- yet usable, and may hide outer homographs.
11149 if Nkind (Related_Nod) = N_Function_Specification then
11150 Analyze_Return_Type (Related_Nod);
11153 -- Now set the kind (mode) of each formal
11155 Param_Spec := First (T);
11156 while Present (Param_Spec) loop
11157 Formal := Defining_Identifier (Param_Spec);
11158 Set_Formal_Mode (Formal);
11160 if Ekind (Formal) = E_In_Parameter then
11161 Set_Default_Value (Formal, Expression (Param_Spec));
11163 if Present (Expression (Param_Spec)) then
11164 Default := Expression (Param_Spec);
11166 if Is_Scalar_Type (Etype (Default)) then
11167 if Nkind (Parameter_Type (Param_Spec)) /=
11168 N_Access_Definition
11170 Formal_Type := Entity (Parameter_Type (Param_Spec));
11174 (Related_Nod, Parameter_Type (Param_Spec));
11177 Apply_Scalar_Range_Check (Default, Formal_Type);
11181 elsif Ekind (Formal) = E_Out_Parameter then
11182 Num_Out_Params := Num_Out_Params + 1;
11184 if Num_Out_Params = 1 then
11185 First_Out_Param := Formal;
11188 elsif Ekind (Formal) = E_In_Out_Parameter then
11189 Num_Out_Params := Num_Out_Params + 1;
11192 -- Skip remaining processing if formal type was in error
11194 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11195 goto Next_Parameter;
11198 -- Force call by reference if aliased
11200 if Is_Aliased (Formal) then
11201 Set_Mechanism (Formal, By_Reference);
11203 -- Warn if user asked this to be passed by copy
11205 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11207 ("cannot pass aliased parameter & by copy?", Formal);
11210 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11212 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11213 Set_Mechanism (Formal, By_Copy);
11215 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11216 Set_Mechanism (Formal, By_Reference);
11223 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11224 Set_Is_Only_Out_Parameter (First_Out_Param);
11226 end Process_Formals;
11232 procedure Process_PPCs
11234 Spec_Id : Entity_Id;
11235 Body_Id : Entity_Id)
11237 Loc : constant Source_Ptr := Sloc (N);
11241 Designator : Entity_Id;
11242 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11244 Precond : Node_Id := Empty;
11245 -- Set non-Empty if we prepend precondition to the declarations. This
11246 -- is used to hook up inherited preconditions (adding the condition
11247 -- expression with OR ELSE, and adding the message).
11249 Inherited_Precond : Node_Id;
11250 -- Precondition inherited from parent subprogram
11252 Inherited : constant Subprogram_List :=
11253 Inherited_Subprograms (Spec_Id);
11254 -- List of subprograms inherited by this subprogram
11256 Plist : List_Id := No_List;
11257 -- List of generated postconditions
11259 procedure Check_Access_Invariants (E : Entity_Id);
11260 -- If the subprogram returns an access to a type with invariants, or
11261 -- has access parameters whose designated type has an invariant, then
11262 -- under the same visibility conditions as for other invariant checks,
11263 -- the type invariant must be applied to the returned value.
11265 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id);
11266 -- Given pragma Contract_Cases CCs, create the circuitry needed to
11267 -- evaluate case guards and trigger consequence expressions. Subp_Id
11268 -- denotes the related subprogram.
11270 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11271 -- Prag contains an analyzed precondition or postcondition pragma. This
11272 -- function copies the pragma, changes it to the corresponding Check
11273 -- pragma and returns the Check pragma as the result. If Pspec is non-
11274 -- empty, this is the case of inheriting a PPC, where we must change
11275 -- references to parameters of the inherited subprogram to point to the
11276 -- corresponding parameters of the current subprogram.
11278 procedure Insert_After_Last_Declaration (Nod : Node_Id);
11279 -- Insert node Nod after the last declaration of the context
11281 function Invariants_Or_Predicates_Present return Boolean;
11282 -- Determines if any invariants or predicates are present for any OUT
11283 -- or IN OUT parameters of the subprogram, or (for a function) if the
11284 -- return value has an invariant.
11286 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11287 -- T is the entity for a private type for which invariants are defined.
11288 -- This function returns True if the procedure corresponding to the
11289 -- value of Designator is a public procedure from the point of view of
11290 -- this type (i.e. its spec is in the visible part of the package that
11291 -- contains the declaration of the private type). A True value means
11292 -- that an invariant check is required (for an IN OUT parameter, or
11293 -- the returned value of a function.
11295 -----------------------------
11296 -- Check_Access_Invariants --
11297 -----------------------------
11299 procedure Check_Access_Invariants (E : Entity_Id) is
11305 if Is_Access_Type (Etype (E))
11306 and then not Is_Access_Constant (Etype (E))
11308 Typ := Designated_Type (Etype (E));
11310 if Has_Invariants (Typ)
11311 and then Present (Invariant_Procedure (Typ))
11312 and then Is_Public_Subprogram_For (Typ)
11315 Make_Explicit_Dereference (Loc,
11316 Prefix => New_Occurrence_Of (E, Loc));
11317 Set_Etype (Obj, Typ);
11319 Call := Make_Invariant_Call (Obj);
11322 Make_If_Statement (Loc,
11325 Left_Opnd => Make_Null (Loc),
11326 Right_Opnd => New_Occurrence_Of (E, Loc)),
11327 Then_Statements => New_List (Call)));
11330 end Check_Access_Invariants;
11332 ---------------------------
11333 -- Expand_Contract_Cases --
11334 ---------------------------
11336 -- Pragma Contract_Cases is expanded in the following manner:
11339 -- Flag_1 : Boolean := False;
11341 -- Flag_N : Boolean := False;
11342 -- Flag_N+1 : Boolean := False; -- when "others" present
11343 -- Count : Natural := 0;
11345 -- <preconditions (if any)>
11347 -- if Case_Guard_1 then
11349 -- Count := Count + 1;
11352 -- if Case_Guard_N then
11354 -- Count := Count + 1;
11357 -- if Count = 0 then
11358 -- raise Assertion_Error with "contract cases incomplete";
11360 -- Flag_N+1 := True; -- when "others" present
11362 -- elsif Count > 1 then
11364 -- Str0 : constant String :=
11365 -- "contract cases overlap for subprogram ABC";
11366 -- Str1 : constant String :=
11368 -- Str0 & "case guard at xxx evaluates to True"
11370 -- StrN : constant String :=
11372 -- StrN-1 & "case guard at xxx evaluates to True"
11375 -- raise Assertion_Error with StrN;
11379 -- procedure _Postconditions is
11381 -- <postconditions (if any)>
11383 -- if Flag_1 and then not Consequence_1 then
11384 -- raise Assertion_Error with "failed contract case at xxx";
11387 -- if Flag_N[+1] and then not Consequence_N[+1] then
11388 -- raise Assertion_Error with "failed contract case at xxx";
11390 -- end _Postconditions;
11395 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id) is
11396 Loc : constant Source_Ptr := Sloc (CCs);
11398 procedure Case_Guard_Error
11401 Error_Loc : Source_Ptr;
11402 Msg : in out Entity_Id);
11403 -- Given a declarative list Decls, status flag Flag, the location of
11404 -- the error and a string Msg, construct the following check:
11405 -- Msg : constant String :=
11407 -- Msg & "case guard at Error_Loc evaluates to True"
11409 -- The resulting code is added to Decls
11411 procedure Consequence_Error
11412 (Checks : in out Node_Id;
11415 -- Given an if statement Checks, status flag Flag and a consequence
11416 -- Conseq, construct the following check:
11417 -- [els]if Flag and then not Conseq then
11418 -- raise Assertion_Error
11419 -- with "failed contract case at Sloc (Conseq)";
11421 -- The resulting code is added to Checks
11423 function Declaration_Of (Id : Entity_Id) return Node_Id;
11424 -- Given the entity Id of a boolean flag, generate:
11425 -- Id : Boolean := False;
11427 function Increment (Id : Entity_Id) return Node_Id;
11428 -- Given the entity Id of a numerical variable, generate:
11431 function Set (Id : Entity_Id) return Node_Id;
11432 -- Given the entity Id of a boolean variable, generate:
11435 ----------------------
11436 -- Case_Guard_Error --
11437 ----------------------
11439 procedure Case_Guard_Error
11442 Error_Loc : Source_Ptr;
11443 Msg : in out Entity_Id)
11445 New_Line : constant Character := Character'Val (10);
11446 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
11450 Store_String_Char (New_Line);
11451 Store_String_Chars (" case guard at ");
11452 Store_String_Chars (Build_Location_String (Error_Loc));
11453 Store_String_Chars (" evaluates to True");
11456 -- New_Msg : constant String :=
11458 -- Msg & "case guard at Error_Loc evaluates to True"
11462 Make_Object_Declaration (Loc,
11463 Defining_Identifier => New_Msg,
11464 Constant_Present => True,
11465 Object_Definition => New_Reference_To (Standard_String, Loc),
11467 Make_If_Expression (Loc,
11468 Expressions => New_List (
11469 New_Reference_To (Flag, Loc),
11471 Make_Op_Concat (Loc,
11472 Left_Opnd => New_Reference_To (Msg, Loc),
11473 Right_Opnd => Make_String_Literal (Loc, End_String)),
11475 New_Reference_To (Msg, Loc)))));
11478 end Case_Guard_Error;
11480 -----------------------
11481 -- Consequence_Error --
11482 -----------------------
11484 procedure Consequence_Error
11485 (Checks : in out Node_Id;
11494 -- Flag and then not Conseq
11497 Make_And_Then (Loc,
11498 Left_Opnd => New_Reference_To (Flag, Loc),
11501 Right_Opnd => Relocate_Node (Conseq)));
11504 -- raise Assertion_Error
11505 -- with "failed contract case at Sloc (Conseq)";
11508 Store_String_Chars ("failed contract case at ");
11509 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
11512 Make_Procedure_Call_Statement (Loc,
11514 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11515 Parameter_Associations => New_List (
11516 Make_String_Literal (Loc, End_String)));
11518 if No (Checks) then
11520 Make_If_Statement (Loc,
11522 Then_Statements => New_List (Error));
11525 if No (Elsif_Parts (Checks)) then
11526 Set_Elsif_Parts (Checks, New_List);
11529 Append_To (Elsif_Parts (Checks),
11530 Make_Elsif_Part (Loc,
11532 Then_Statements => New_List (Error)));
11534 end Consequence_Error;
11536 --------------------
11537 -- Declaration_Of --
11538 --------------------
11540 function Declaration_Of (Id : Entity_Id) return Node_Id is
11543 Make_Object_Declaration (Loc,
11544 Defining_Identifier => Id,
11545 Object_Definition =>
11546 New_Reference_To (Standard_Boolean, Loc),
11548 New_Reference_To (Standard_False, Loc));
11549 end Declaration_Of;
11555 function Increment (Id : Entity_Id) return Node_Id is
11558 Make_Assignment_Statement (Loc,
11559 Name => New_Reference_To (Id, Loc),
11562 Left_Opnd => New_Reference_To (Id, Loc),
11563 Right_Opnd => Make_Integer_Literal (Loc, 1)));
11570 function Set (Id : Entity_Id) return Node_Id is
11573 Make_Assignment_Statement (Loc,
11574 Name => New_Reference_To (Id, Loc),
11575 Expression => New_Reference_To (Standard_True, Loc));
11580 Aggr : constant Node_Id :=
11582 (Pragma_Argument_Associations (CCs)));
11583 Decls : constant List_Id := Declarations (N);
11584 Multiple_PCs : constant Boolean :=
11585 List_Length (Component_Associations (Aggr)) > 1;
11586 Case_Guard : Node_Id;
11587 CG_Checks : Node_Id;
11588 CG_Stmts : List_Id;
11590 Conseq_Checks : Node_Id := Empty;
11592 Error_Decls : List_Id;
11594 Msg_Str : Entity_Id;
11595 Others_Flag : Entity_Id := Empty;
11596 Post_Case : Node_Id;
11598 -- Start of processing for Expand_Contract_Cases
11601 -- Create the counter which tracks the number of case guards that
11602 -- evaluate to True.
11604 -- Count : Natural := 0;
11606 Count := Make_Temporary (Loc, 'C');
11609 Make_Object_Declaration (Loc,
11610 Defining_Identifier => Count,
11611 Object_Definition => New_Reference_To (Standard_Natural, Loc),
11612 Expression => Make_Integer_Literal (Loc, 0)));
11614 -- Create the base error message for multiple overlapping case
11617 -- Msg_Str : constant String :=
11618 -- "contract cases overlap for subprogram Subp_Id";
11620 if Multiple_PCs then
11621 Msg_Str := Make_Temporary (Loc, 'S');
11624 Store_String_Chars ("contract cases overlap for subprogram ");
11625 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
11627 Error_Decls := New_List (
11628 Make_Object_Declaration (Loc,
11629 Defining_Identifier => Msg_Str,
11630 Constant_Present => True,
11631 Object_Definition => New_Reference_To (Standard_String, Loc),
11632 Expression => Make_String_Literal (Loc, End_String)));
11635 -- Process individual post cases
11637 Post_Case := First (Component_Associations (Aggr));
11638 while Present (Post_Case) loop
11639 Case_Guard := First (Choices (Post_Case));
11640 Conseq := Expression (Post_Case);
11642 -- The "others" choice requires special processing
11644 if Nkind (Case_Guard) = N_Others_Choice then
11645 Others_Flag := Make_Temporary (Loc, 'F');
11646 Prepend_To (Decls, Declaration_Of (Others_Flag));
11648 -- Check possible overlap between a case guard and "others"
11650 if Multiple_PCs then
11652 (Decls => Error_Decls,
11653 Flag => Others_Flag,
11654 Error_Loc => Sloc (Case_Guard),
11658 -- Check the corresponding consequence of "others"
11661 (Checks => Conseq_Checks,
11662 Flag => Others_Flag,
11665 -- Regular post case
11668 -- Create the flag which tracks the state of its associated
11671 Flag := Make_Temporary (Loc, 'F');
11672 Prepend_To (Decls, Declaration_Of (Flag));
11674 -- The flag is set when the case guard is evaluated to True
11675 -- if Case_Guard then
11677 -- Count := Count + 1;
11681 Make_If_Statement (Loc,
11682 Condition => Relocate_Node (Case_Guard),
11683 Then_Statements => New_List (
11685 Increment (Count))));
11687 -- Check whether this case guard overlaps with another case
11690 if Multiple_PCs then
11692 (Decls => Error_Decls,
11694 Error_Loc => Sloc (Case_Guard),
11698 -- The corresponding consequence of the case guard which
11699 -- evaluated to True must hold on exit from the subprogram.
11701 Consequence_Error (Conseq_Checks, Flag, Conseq);
11707 -- Raise Assertion_Error when none of the case guards evaluate to
11708 -- True. The only exception is when we have "others", in which case
11709 -- there is no error because "others" acts as a default True.
11714 if Present (Others_Flag) then
11715 CG_Stmts := New_List (Set (Others_Flag));
11718 -- raise Assetion_Error with "contract cases incomplete";
11722 Store_String_Chars ("contract cases incomplete");
11724 CG_Stmts := New_List (
11725 Make_Procedure_Call_Statement (Loc,
11727 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11728 Parameter_Associations => New_List (
11729 Make_String_Literal (Loc, End_String))));
11733 Make_If_Statement (Loc,
11736 Left_Opnd => New_Reference_To (Count, Loc),
11737 Right_Opnd => Make_Integer_Literal (Loc, 0)),
11738 Then_Statements => CG_Stmts);
11740 -- Detect a possible failure due to several case guards evaluating to
11744 -- elsif Count > 0 then
11748 -- raise Assertion_Error with <Msg_Str>;
11751 if Multiple_PCs then
11752 Set_Elsif_Parts (CG_Checks, New_List (
11753 Make_Elsif_Part (Loc,
11756 Left_Opnd => New_Reference_To (Count, Loc),
11757 Right_Opnd => Make_Integer_Literal (Loc, 1)),
11759 Then_Statements => New_List (
11760 Make_Block_Statement (Loc,
11761 Declarations => Error_Decls,
11762 Handled_Statement_Sequence =>
11763 Make_Handled_Sequence_Of_Statements (Loc,
11764 Statements => New_List (
11765 Make_Procedure_Call_Statement (Loc,
11768 (RTE (RE_Raise_Assert_Failure), Loc),
11769 Parameter_Associations => New_List (
11770 New_Reference_To (Msg_Str, Loc))))))))));
11773 Append_To (Decls, CG_Checks);
11775 -- Raise Assertion_Error when the corresponding consequence of a case
11776 -- guard that evaluated to True fails.
11782 Append_To (Plist, Conseq_Checks);
11783 end Expand_Contract_Cases;
11789 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
11790 Nam : constant Name_Id := Pragma_Name (Prag);
11795 -- Effective name of pragma (maybe Pre/Post rather than Precondition/
11796 -- Postcodition if the pragma came from a Pre/Post aspect). We need
11797 -- the name right when we generate the Check pragma, since we want
11798 -- the right set of check policies to apply.
11801 -- Prepare map if this is the case where we have to map entities of
11802 -- arguments in the overridden subprogram to corresponding entities
11803 -- of the current subprogram.
11814 Map := New_Elmt_List;
11815 PF := First_Formal (Pspec);
11816 CF := First_Formal (Designator);
11817 while Present (PF) loop
11818 Append_Elmt (PF, Map);
11819 Append_Elmt (CF, Map);
11826 -- Now we can copy the tree, doing any required substitutions
11828 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
11830 -- Set Analyzed to false, since we want to reanalyze the check
11831 -- procedure. Note that it is only at the outer level that we
11832 -- do this fiddling, for the spec cases, the already preanalyzed
11833 -- parameters are not affected.
11835 Set_Analyzed (CP, False);
11837 -- We also make sure Comes_From_Source is False for the copy
11839 Set_Comes_From_Source (CP, False);
11841 -- For a postcondition pragma within a generic, preserve the pragma
11842 -- for later expansion. This is also used when an error was detected,
11843 -- thus setting Expander_Active to False.
11845 if Nam = Name_Postcondition
11846 and then not Expander_Active
11851 -- Get effective name of aspect
11853 if Present (Corresponding_Aspect (Prag)) then
11854 Ename := Chars (Identifier (Corresponding_Aspect (Prag)));
11859 -- Change copy of pragma into corresponding pragma Check
11861 Prepend_To (Pragma_Argument_Associations (CP),
11862 Make_Pragma_Argument_Association (Sloc (Prag),
11863 Expression => Make_Identifier (Loc, Ename)));
11864 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
11866 -- If this is inherited case and the current message starts with
11867 -- "failed p", we change it to "failed inherited p...".
11869 if Present (Pspec) then
11871 Msg : constant Node_Id :=
11872 Last (Pragma_Argument_Associations (CP));
11875 if Chars (Msg) = Name_Message then
11876 String_To_Name_Buffer (Strval (Expression (Msg)));
11878 if Name_Buffer (1 .. 8) = "failed p" then
11879 Insert_Str_In_Name_Buffer ("inherited ", 8);
11881 (Expression (Last (Pragma_Argument_Associations (CP))),
11882 String_From_Name_Buffer);
11888 -- Return the check pragma
11893 -----------------------------------
11894 -- Insert_After_Last_Declaration --
11895 -----------------------------------
11897 procedure Insert_After_Last_Declaration (Nod : Node_Id) is
11898 Decls : constant List_Id := Declarations (N);
11902 Set_Declarations (N, New_List (Nod));
11904 Append_To (Decls, Nod);
11906 end Insert_After_Last_Declaration;
11908 --------------------------------------
11909 -- Invariants_Or_Predicates_Present --
11910 --------------------------------------
11912 function Invariants_Or_Predicates_Present return Boolean is
11913 Formal : Entity_Id;
11916 -- Check function return result. If result is an access type there
11917 -- may be invariants on the designated type.
11919 if Ekind (Designator) /= E_Procedure
11920 and then Has_Invariants (Etype (Designator))
11924 elsif Ekind (Designator) /= E_Procedure
11925 and then Is_Access_Type (Etype (Designator))
11926 and then Has_Invariants (Designated_Type (Etype (Designator)))
11931 -- Check parameters
11933 Formal := First_Formal (Designator);
11934 while Present (Formal) loop
11935 if Ekind (Formal) /= E_In_Parameter
11936 and then (Has_Invariants (Etype (Formal))
11937 or else Present (Predicate_Function (Etype (Formal))))
11941 elsif Is_Access_Type (Etype (Formal))
11942 and then Has_Invariants (Designated_Type (Etype (Formal)))
11947 Next_Formal (Formal);
11951 end Invariants_Or_Predicates_Present;
11953 ------------------------------
11954 -- Is_Public_Subprogram_For --
11955 ------------------------------
11957 -- The type T is a private type, its declaration is therefore in
11958 -- the list of public declarations of some package. The test for a
11959 -- public subprogram is that its declaration is in this same list
11960 -- of declarations for the same package (note that all the public
11961 -- declarations are in one list, and all the private declarations
11962 -- in another, so this deals with the public/private distinction).
11964 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
11965 DD : constant Node_Id := Unit_Declaration_Node (Designator);
11966 -- The subprogram declaration for the subprogram in question
11968 TL : constant List_Id :=
11969 Visible_Declarations
11970 (Specification (Unit_Declaration_Node (Scope (T))));
11971 -- The list of declarations containing the private declaration of
11972 -- the type. We know it is a private type, so we know its scope is
11973 -- the package in question, and we know it must be in the visible
11974 -- declarations of this package.
11977 -- If the subprogram declaration is not a list member, it must be
11978 -- an Init_Proc, in which case we want to consider it to be a
11979 -- public subprogram, since we do get initializations to deal with.
11980 -- Other internally generated subprograms are not public.
11982 if not Is_List_Member (DD)
11983 and then Is_Init_Proc (Defining_Entity (DD))
11987 -- The declaration may have been generated for an expression function
11988 -- so check whether that function comes from source.
11990 elsif not Comes_From_Source (DD)
11992 (Nkind (Original_Node (DD)) /= N_Expression_Function
11993 or else not Comes_From_Source (Defining_Entity (DD)))
11997 -- Otherwise we test whether the subprogram is declared in the
11998 -- visible declarations of the package containing the type.
12001 return TL = List_Containing (DD);
12003 end Is_Public_Subprogram_For;
12005 -- Start of processing for Process_PPCs
12008 -- Capture designator from spec if present, else from body
12010 if Present (Spec_Id) then
12011 Designator := Spec_Id;
12013 Designator := Body_Id;
12016 -- Internally generated subprograms, such as type-specific functions,
12017 -- don't get assertion checks.
12019 if Get_TSS_Name (Designator) /= TSS_Null then
12023 -- Grab preconditions from spec
12025 if Present (Spec_Id) then
12027 -- Loop through PPC pragmas from spec. Note that preconditions from
12028 -- the body will be analyzed and converted when we scan the body
12029 -- declarations below.
12031 Prag := Spec_PPC_List (Contract (Spec_Id));
12032 while Present (Prag) loop
12033 if Pragma_Name (Prag) = Name_Precondition then
12035 -- For Pre (or Precondition pragma), we simply prepend the
12036 -- pragma to the list of declarations right away so that it
12037 -- will be executed at the start of the procedure. Note that
12038 -- this processing reverses the order of the list, which is
12039 -- what we want since new entries were chained to the head of
12040 -- the list. There can be more than one precondition when we
12041 -- use pragma Precondition.
12043 if not Class_Present (Prag) then
12044 Prepend (Grab_PPC, Declarations (N));
12046 -- For Pre'Class there can only be one pragma, and we save
12047 -- it in Precond for now. We will add inherited Pre'Class
12048 -- stuff before inserting this pragma in the declarations.
12050 Precond := Grab_PPC;
12054 Prag := Next_Pragma (Prag);
12057 -- Now deal with inherited preconditions
12059 for J in Inherited'Range loop
12060 Prag := Spec_PPC_List (Contract (Inherited (J)));
12062 while Present (Prag) loop
12063 if Pragma_Name (Prag) = Name_Precondition
12064 and then Class_Present (Prag)
12066 Inherited_Precond := Grab_PPC (Inherited (J));
12068 -- No precondition so far, so establish this as the first
12070 if No (Precond) then
12071 Precond := Inherited_Precond;
12073 -- Here we already have a precondition, add inherited one
12076 -- Add new precondition to old one using OR ELSE
12079 New_Expr : constant Node_Id :=
12083 (Pragma_Argument_Associations
12084 (Inherited_Precond))));
12085 Old_Expr : constant Node_Id :=
12089 (Pragma_Argument_Associations
12093 if Paren_Count (Old_Expr) = 0 then
12094 Set_Paren_Count (Old_Expr, 1);
12097 if Paren_Count (New_Expr) = 0 then
12098 Set_Paren_Count (New_Expr, 1);
12102 Make_Or_Else (Sloc (Old_Expr),
12103 Left_Opnd => Relocate_Node (Old_Expr),
12104 Right_Opnd => New_Expr));
12107 -- Add new message in the form:
12109 -- failed precondition from bla
12110 -- also failed inherited precondition from bla
12113 -- Skip this if exception locations are suppressed
12115 if not Exception_Locations_Suppressed then
12117 New_Msg : constant Node_Id :=
12120 (Pragma_Argument_Associations
12121 (Inherited_Precond)));
12122 Old_Msg : constant Node_Id :=
12125 (Pragma_Argument_Associations
12128 Start_String (Strval (Old_Msg));
12129 Store_String_Chars (ASCII.LF & " also ");
12130 Store_String_Chars (Strval (New_Msg));
12131 Set_Strval (Old_Msg, End_String);
12137 Prag := Next_Pragma (Prag);
12141 -- If we have built a precondition for Pre'Class (including any
12142 -- Pre'Class aspects inherited from parent subprograms), then we
12143 -- insert this composite precondition at this stage.
12145 if Present (Precond) then
12146 Prepend (Precond, Declarations (N));
12150 -- Build postconditions procedure if needed and prepend the following
12151 -- declaration to the start of the declarations for the subprogram.
12153 -- procedure _postconditions [(_Result : resulttype)] is
12155 -- pragma Check (Postcondition, condition [,message]);
12156 -- pragma Check (Postcondition, condition [,message]);
12158 -- Invariant_Procedure (_Result) ...
12159 -- Invariant_Procedure (Arg1)
12163 -- First we deal with the postconditions in the body
12165 if Is_Non_Empty_List (Declarations (N)) then
12167 -- Loop through declarations
12169 Prag := First (Declarations (N));
12170 while Present (Prag) loop
12171 if Nkind (Prag) = N_Pragma then
12172 Check_Applicable_Policy (Prag);
12174 -- If pragma, capture if postconditions enabled, else ignore
12176 if Pragma_Name (Prag) = Name_Postcondition
12177 and then not Is_Ignored (Prag)
12179 if Plist = No_List then
12180 Plist := Empty_List;
12185 -- If expansion is disabled, as in a generic unit, save
12186 -- pragma for later expansion.
12188 if not Expander_Active then
12189 Prepend (Grab_PPC, Declarations (N));
12191 Append (Grab_PPC, Plist);
12197 -- Not a pragma, if comes from source, then end scan
12199 elsif Comes_From_Source (Prag) then
12202 -- Skip stuff not coming from source
12210 -- Now deal with any postconditions from the spec
12212 if Present (Spec_Id) then
12213 Spec_Postconditions : declare
12214 procedure Process_Contract_Cases (Spec : Node_Id);
12215 -- This processes the Spec_CTC_List from Spec, processing any
12216 -- contract-cases from the list. The caller has checked that
12217 -- Spec_CTC_List is non-Empty.
12219 procedure Process_Post_Conditions
12222 -- This processes the Spec_PPC_List from Spec, processing any
12223 -- postconditions from the list. If Class is True, then only
12224 -- postconditions marked with Class_Present are considered.
12225 -- The caller has checked that Spec_PPC_List is non-Empty.
12227 ----------------------------
12228 -- Process_Contract_Cases --
12229 ----------------------------
12231 procedure Process_Contract_Cases (Spec : Node_Id) is
12233 -- Loop through Contract_Cases pragmas from spec
12235 Prag := Spec_CTC_List (Contract (Spec));
12237 if Pragma_Name (Prag) = Name_Contract_Cases then
12238 Expand_Contract_Cases (Prag, Spec_Id);
12241 Prag := Next_Pragma (Prag);
12242 exit when No (Prag);
12244 end Process_Contract_Cases;
12246 -----------------------------
12247 -- Process_Post_Conditions --
12248 -----------------------------
12250 procedure Process_Post_Conditions
12263 -- Loop through PPC pragmas from spec
12265 Prag := Spec_PPC_List (Contract (Spec));
12267 if Pragma_Name (Prag) = Name_Postcondition
12268 and then (not Class or else Class_Present (Prag))
12270 if Plist = No_List then
12271 Plist := Empty_List;
12274 if not Expander_Active then
12276 (Grab_PPC (Pspec), Declarations (N));
12278 Append (Grab_PPC (Pspec), Plist);
12282 Prag := Next_Pragma (Prag);
12283 exit when No (Prag);
12285 end Process_Post_Conditions;
12287 -- Start of processing for Spec_Postconditions
12290 -- Process postconditions expressed as contract-cases
12292 if Present (Spec_CTC_List (Contract (Spec_Id))) then
12293 Process_Contract_Cases (Spec_Id);
12296 -- Process spec postconditions
12298 if Present (Spec_PPC_List (Contract (Spec_Id))) then
12299 Process_Post_Conditions (Spec_Id, Class => False);
12302 -- Process inherited postconditions
12304 for J in Inherited'Range loop
12305 if Present (Spec_PPC_List (Contract (Inherited (J)))) then
12306 Process_Post_Conditions (Inherited (J), Class => True);
12309 end Spec_Postconditions;
12312 -- If we had any postconditions and expansion is enabled, or if the
12313 -- subprogram has invariants, then build the _Postconditions procedure.
12315 if (Present (Plist) or else Invariants_Or_Predicates_Present)
12316 and then Expander_Active
12319 Plist := Empty_List;
12322 -- Special processing for function return
12324 if Ekind (Designator) /= E_Procedure then
12326 Rent : constant Entity_Id :=
12327 Make_Defining_Identifier (Loc, Name_uResult);
12328 Ftyp : constant Entity_Id := Etype (Designator);
12331 Set_Etype (Rent, Ftyp);
12333 -- Add argument for return
12337 Make_Parameter_Specification (Loc,
12338 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
12339 Defining_Identifier => Rent));
12341 -- Add invariant call if returning type with invariants and
12342 -- this is a public function, i.e. a function declared in the
12343 -- visible part of the package defining the private type.
12345 if Has_Invariants (Etype (Rent))
12346 and then Present (Invariant_Procedure (Etype (Rent)))
12347 and then Is_Public_Subprogram_For (Etype (Rent))
12350 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
12353 -- Same if return value is an access to type with invariants
12355 Check_Access_Invariants (Rent);
12358 -- Procedure rather than a function
12364 -- Add invariant calls and predicate calls for parameters. Note that
12365 -- this is done for functions as well, since in Ada 2012 they can
12366 -- have IN OUT args.
12369 Formal : Entity_Id;
12373 Formal := First_Formal (Designator);
12374 while Present (Formal) loop
12375 if Ekind (Formal) /= E_In_Parameter
12376 or else Is_Access_Type (Etype (Formal))
12378 Ftype := Etype (Formal);
12380 if Has_Invariants (Ftype)
12381 and then Present (Invariant_Procedure (Ftype))
12382 and then Is_Public_Subprogram_For (Ftype)
12385 Make_Invariant_Call
12386 (New_Occurrence_Of (Formal, Loc)));
12389 Check_Access_Invariants (Formal);
12391 if Present (Predicate_Function (Ftype)) then
12393 Make_Predicate_Check
12394 (Ftype, New_Occurrence_Of (Formal, Loc)));
12398 Next_Formal (Formal);
12402 -- Build and insert postcondition procedure
12405 Post_Proc : constant Entity_Id :=
12406 Make_Defining_Identifier (Loc,
12407 Chars => Name_uPostconditions);
12408 -- The entity for the _Postconditions procedure
12411 -- Insert the corresponding body of a post condition pragma after
12412 -- the last declaration of the context. This ensures that the body
12413 -- will not cause any premature freezing as it may mention types:
12415 -- procedure Proc (Obj : Array_Typ) is
12416 -- procedure _postconditions is
12419 -- end _postconditions;
12421 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12424 -- In the example above, Obj is of type T but the incorrect
12425 -- placement of _postconditions will cause a crash in gigi due to
12426 -- an out of order reference. The body of _postconditions must be
12427 -- placed after the declaration of Temp to preserve correct
12430 Insert_After_Last_Declaration (
12431 Make_Subprogram_Body (Loc,
12433 Make_Procedure_Specification (Loc,
12434 Defining_Unit_Name => Post_Proc,
12435 Parameter_Specifications => Parms),
12437 Declarations => Empty_List,
12439 Handled_Statement_Sequence =>
12440 Make_Handled_Sequence_Of_Statements (Loc,
12441 Statements => Plist)));
12443 Set_Ekind (Post_Proc, E_Procedure);
12445 -- If this is a procedure, set the Postcondition_Proc attribute on
12446 -- the proper defining entity for the subprogram.
12448 if Ekind (Designator) = E_Procedure then
12449 Set_Postcondition_Proc (Designator, Post_Proc);
12453 Set_Has_Postconditions (Designator);
12457 ----------------------------
12458 -- Reference_Body_Formals --
12459 ----------------------------
12461 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12466 if Error_Posted (Spec) then
12470 -- Iterate over both lists. They may be of different lengths if the two
12471 -- specs are not conformant.
12473 Fs := First_Formal (Spec);
12474 Fb := First_Formal (Bod);
12475 while Present (Fs) and then Present (Fb) loop
12476 Generate_Reference (Fs, Fb, 'b');
12478 if Style_Check then
12479 Style.Check_Identifier (Fb, Fs);
12482 Set_Spec_Entity (Fb, Fs);
12483 Set_Referenced (Fs, False);
12487 end Reference_Body_Formals;
12489 -------------------------
12490 -- Set_Actual_Subtypes --
12491 -------------------------
12493 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12495 Formal : Entity_Id;
12497 First_Stmt : Node_Id := Empty;
12498 AS_Needed : Boolean;
12501 -- If this is an empty initialization procedure, no need to create
12502 -- actual subtypes (small optimization).
12504 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12508 Formal := First_Formal (Subp);
12509 while Present (Formal) loop
12510 T := Etype (Formal);
12512 -- We never need an actual subtype for a constrained formal
12514 if Is_Constrained (T) then
12515 AS_Needed := False;
12517 -- If we have unknown discriminants, then we do not need an actual
12518 -- subtype, or more accurately we cannot figure it out! Note that
12519 -- all class-wide types have unknown discriminants.
12521 elsif Has_Unknown_Discriminants (T) then
12522 AS_Needed := False;
12524 -- At this stage we have an unconstrained type that may need an
12525 -- actual subtype. For sure the actual subtype is needed if we have
12526 -- an unconstrained array type.
12528 elsif Is_Array_Type (T) then
12531 -- The only other case needing an actual subtype is an unconstrained
12532 -- record type which is an IN parameter (we cannot generate actual
12533 -- subtypes for the OUT or IN OUT case, since an assignment can
12534 -- change the discriminant values. However we exclude the case of
12535 -- initialization procedures, since discriminants are handled very
12536 -- specially in this context, see the section entitled "Handling of
12537 -- Discriminants" in Einfo.
12539 -- We also exclude the case of Discrim_SO_Functions (functions used
12540 -- in front end layout mode for size/offset values), since in such
12541 -- functions only discriminants are referenced, and not only are such
12542 -- subtypes not needed, but they cannot always be generated, because
12543 -- of order of elaboration issues.
12545 elsif Is_Record_Type (T)
12546 and then Ekind (Formal) = E_In_Parameter
12547 and then Chars (Formal) /= Name_uInit
12548 and then not Is_Unchecked_Union (T)
12549 and then not Is_Discrim_SO_Function (Subp)
12553 -- All other cases do not need an actual subtype
12556 AS_Needed := False;
12559 -- Generate actual subtypes for unconstrained arrays and
12560 -- unconstrained discriminated records.
12563 if Nkind (N) = N_Accept_Statement then
12565 -- If expansion is active, the formal is replaced by a local
12566 -- variable that renames the corresponding entry of the
12567 -- parameter block, and it is this local variable that may
12568 -- require an actual subtype.
12570 if Full_Expander_Active then
12571 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12573 Decl := Build_Actual_Subtype (T, Formal);
12576 if Present (Handled_Statement_Sequence (N)) then
12578 First (Statements (Handled_Statement_Sequence (N)));
12579 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12580 Mark_Rewrite_Insertion (Decl);
12582 -- If the accept statement has no body, there will be no
12583 -- reference to the actuals, so no need to compute actual
12590 Decl := Build_Actual_Subtype (T, Formal);
12591 Prepend (Decl, Declarations (N));
12592 Mark_Rewrite_Insertion (Decl);
12595 -- The declaration uses the bounds of an existing object, and
12596 -- therefore needs no constraint checks.
12598 Analyze (Decl, Suppress => All_Checks);
12600 -- We need to freeze manually the generated type when it is
12601 -- inserted anywhere else than in a declarative part.
12603 if Present (First_Stmt) then
12604 Insert_List_Before_And_Analyze (First_Stmt,
12605 Freeze_Entity (Defining_Identifier (Decl), N));
12608 if Nkind (N) = N_Accept_Statement
12609 and then Full_Expander_Active
12611 Set_Actual_Subtype (Renamed_Object (Formal),
12612 Defining_Identifier (Decl));
12614 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12618 Next_Formal (Formal);
12620 end Set_Actual_Subtypes;
12622 ---------------------
12623 -- Set_Formal_Mode --
12624 ---------------------
12626 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12627 Spec : constant Node_Id := Parent (Formal_Id);
12630 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12631 -- since we ensure that corresponding actuals are always valid at the
12632 -- point of the call.
12634 if Out_Present (Spec) then
12635 if Ekind (Scope (Formal_Id)) = E_Function
12636 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
12638 -- [IN] OUT parameters allowed for functions in Ada 2012
12640 if Ada_Version >= Ada_2012 then
12642 -- Even in Ada 2012 operators can only have IN parameters
12644 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12645 Error_Msg_N ("operators can only have IN parameters", Spec);
12648 if In_Present (Spec) then
12649 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12651 Set_Ekind (Formal_Id, E_Out_Parameter);
12654 -- But not in earlier versions of Ada
12657 Error_Msg_N ("functions can only have IN parameters", Spec);
12658 Set_Ekind (Formal_Id, E_In_Parameter);
12661 elsif In_Present (Spec) then
12662 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12665 Set_Ekind (Formal_Id, E_Out_Parameter);
12666 Set_Never_Set_In_Source (Formal_Id, True);
12667 Set_Is_True_Constant (Formal_Id, False);
12668 Set_Current_Value (Formal_Id, Empty);
12672 Set_Ekind (Formal_Id, E_In_Parameter);
12675 -- Set Is_Known_Non_Null for access parameters since the language
12676 -- guarantees that access parameters are always non-null. We also set
12677 -- Can_Never_Be_Null, since there is no way to change the value.
12679 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12681 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12682 -- null; In Ada 2005, only if then null_exclusion is explicit.
12684 if Ada_Version < Ada_2005
12685 or else Can_Never_Be_Null (Etype (Formal_Id))
12687 Set_Is_Known_Non_Null (Formal_Id);
12688 Set_Can_Never_Be_Null (Formal_Id);
12691 -- Ada 2005 (AI-231): Null-exclusion access subtype
12693 elsif Is_Access_Type (Etype (Formal_Id))
12694 and then Can_Never_Be_Null (Etype (Formal_Id))
12696 Set_Is_Known_Non_Null (Formal_Id);
12698 -- We can also set Can_Never_Be_Null (thus preventing some junk
12699 -- access checks) for the case of an IN parameter, which cannot
12700 -- be changed, or for an IN OUT parameter, which can be changed but
12701 -- not to a null value. But for an OUT parameter, the initial value
12702 -- passed in can be null, so we can't set this flag in that case.
12704 if Ekind (Formal_Id) /= E_Out_Parameter then
12705 Set_Can_Never_Be_Null (Formal_Id);
12709 Set_Mechanism (Formal_Id, Default_Mechanism);
12710 Set_Formal_Validity (Formal_Id);
12711 end Set_Formal_Mode;
12713 -------------------------
12714 -- Set_Formal_Validity --
12715 -------------------------
12717 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12719 -- If no validity checking, then we cannot assume anything about the
12720 -- validity of parameters, since we do not know there is any checking
12721 -- of the validity on the call side.
12723 if not Validity_Checks_On then
12726 -- If validity checking for parameters is enabled, this means we are
12727 -- not supposed to make any assumptions about argument values.
12729 elsif Validity_Check_Parameters then
12732 -- If we are checking in parameters, we will assume that the caller is
12733 -- also checking parameters, so we can assume the parameter is valid.
12735 elsif Ekind (Formal_Id) = E_In_Parameter
12736 and then Validity_Check_In_Params
12738 Set_Is_Known_Valid (Formal_Id, True);
12740 -- Similar treatment for IN OUT parameters
12742 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12743 and then Validity_Check_In_Out_Params
12745 Set_Is_Known_Valid (Formal_Id, True);
12747 end Set_Formal_Validity;
12749 ------------------------
12750 -- Subtype_Conformant --
12751 ------------------------
12753 function Subtype_Conformant
12754 (New_Id : Entity_Id;
12755 Old_Id : Entity_Id;
12756 Skip_Controlling_Formals : Boolean := False) return Boolean
12760 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12761 Skip_Controlling_Formals => Skip_Controlling_Formals);
12763 end Subtype_Conformant;
12765 ---------------------
12766 -- Type_Conformant --
12767 ---------------------
12769 function Type_Conformant
12770 (New_Id : Entity_Id;
12771 Old_Id : Entity_Id;
12772 Skip_Controlling_Formals : Boolean := False) return Boolean
12776 May_Hide_Profile := False;
12779 (New_Id, Old_Id, Type_Conformant, False, Result,
12780 Skip_Controlling_Formals => Skip_Controlling_Formals);
12782 end Type_Conformant;
12784 -------------------------------
12785 -- Valid_Operator_Definition --
12786 -------------------------------
12788 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12791 Id : constant Name_Id := Chars (Designator);
12795 F := First_Formal (Designator);
12796 while Present (F) loop
12799 if Present (Default_Value (F)) then
12801 ("default values not allowed for operator parameters",
12808 -- Verify that user-defined operators have proper number of arguments
12809 -- First case of operators which can only be unary
12811 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12814 -- Case of operators which can be unary or binary
12816 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12817 N_OK := (N in 1 .. 2);
12819 -- All other operators can only be binary
12827 ("incorrect number of arguments for operator", Designator);
12831 and then Base_Type (Etype (Designator)) = Standard_Boolean
12832 and then not Is_Intrinsic_Subprogram (Designator)
12835 ("explicit definition of inequality not allowed", Designator);
12837 end Valid_Operator_Definition;