1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Fname; use Fname;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Inline; use Inline;
46 with Itypes; use Itypes;
47 with Lib.Xref; use Lib.Xref;
48 with Layout; use Layout;
49 with Namet; use Namet;
51 with Nlists; use Nlists;
52 with Nmake; use Nmake;
54 with Output; use Output;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Cat; use Sem_Cat;
61 with Sem_Ch3; use Sem_Ch3;
62 with Sem_Ch4; use Sem_Ch4;
63 with Sem_Ch5; use Sem_Ch5;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch10; use Sem_Ch10;
66 with Sem_Ch12; use Sem_Ch12;
67 with Sem_Ch13; use Sem_Ch13;
68 with Sem_Dim; use Sem_Dim;
69 with Sem_Disp; use Sem_Disp;
70 with Sem_Dist; use Sem_Dist;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Prag; use Sem_Prag;
75 with Sem_Res; use Sem_Res;
76 with Sem_Util; use Sem_Util;
77 with Sem_Type; use Sem_Type;
78 with Sem_Warn; use Sem_Warn;
79 with Sinput; use Sinput;
80 with Stand; use Stand;
81 with Sinfo; use Sinfo;
82 with Sinfo.CN; use Sinfo.CN;
83 with Snames; use Snames;
84 with Stringt; use Stringt;
86 with Stylesw; use Stylesw;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
138 Ctype : Conformance_Type;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
156 procedure Check_Subprogram_Order (N : Node_Id);
157 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
158 -- the alpha ordering rule for N if this ordering requirement applicable.
160 procedure Check_Returns
164 Proc : Entity_Id := Empty);
165 -- Called to check for missing return statements in a function body, or for
166 -- returns present in a procedure body which has No_Return set. HSS is the
167 -- handled statement sequence for the subprogram body. This procedure
168 -- checks all flow paths to make sure they either have return (Mode = 'F',
169 -- used for functions) or do not have a return (Mode = 'P', used for
170 -- No_Return procedures). The flag Err is set if there are any control
171 -- paths not explicitly terminated by a return in the function case, and is
172 -- True otherwise. Proc is the entity for the procedure case and is used
173 -- in posting the warning message.
175 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
176 -- In Ada 2012, a primitive equality operator on an untagged record type
177 -- must appear before the type is frozen, and have the same visibility as
178 -- that of the type. This procedure checks that this rule is met, and
179 -- otherwise emits an error on the subprogram declaration and a warning
180 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
181 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
182 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
183 -- is set, otherwise the call has no effect.
185 procedure Enter_Overloaded_Entity (S : Entity_Id);
186 -- This procedure makes S, a new overloaded entity, into the first visible
187 -- entity with that name.
189 function Is_Non_Overriding_Operation
191 New_E : Entity_Id) return Boolean;
192 -- Enforce the rule given in 12.3(18): a private operation in an instance
193 -- overrides an inherited operation only if the corresponding operation
194 -- was overriding in the generic. This needs to be checked for primitive
195 -- operations of types derived (in the generic unit) from formal private
196 -- or formal derived types.
198 procedure Make_Inequality_Operator (S : Entity_Id);
199 -- Create the declaration for an inequality operator that is implicitly
200 -- created by a user-defined equality operator that yields a boolean.
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ---------------------------------------------
208 -- Analyze_Abstract_Subprogram_Declaration --
209 ---------------------------------------------
211 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
212 Scop : constant Entity_Id := Current_Scope;
213 Subp_Id : constant Entity_Id :=
214 Analyze_Subprogram_Specification (Specification (N));
217 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
219 Generate_Definition (Subp_Id);
221 Set_Is_Abstract_Subprogram (Subp_Id);
222 New_Overloaded_Entity (Subp_Id);
223 Check_Delayed_Subprogram (Subp_Id);
225 Set_Categorization_From_Scope (Subp_Id, Scop);
227 -- An abstract subprogram declared within a Ghost region is rendered
228 -- Ghost (SPARK RM 6.9(2)).
230 if Ghost_Mode > None then
231 Set_Is_Ghost_Entity (Subp_Id);
234 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
235 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
237 -- Issue a warning if the abstract subprogram is neither a dispatching
238 -- operation nor an operation that overrides an inherited subprogram or
239 -- predefined operator, since this most likely indicates a mistake.
241 elsif Warn_On_Redundant_Constructs
242 and then not Is_Dispatching_Operation (Subp_Id)
243 and then not Present (Overridden_Operation (Subp_Id))
244 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
245 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
248 ("abstract subprogram is not dispatching or overriding?r?", N);
251 Generate_Reference_To_Formals (Subp_Id);
252 Check_Eliminated (Subp_Id);
254 if Has_Aspects (N) then
255 Analyze_Aspect_Specifications (N, Subp_Id);
257 end Analyze_Abstract_Subprogram_Declaration;
259 ---------------------------------
260 -- Analyze_Expression_Function --
261 ---------------------------------
263 procedure Analyze_Expression_Function (N : Node_Id) is
264 Expr : constant Node_Id := Expression (N);
265 Loc : constant Source_Ptr := Sloc (N);
266 LocX : constant Source_Ptr := Sloc (Expr);
267 Spec : constant Node_Id := Specification (N);
272 -- If the expression is a completion, Prev is the entity whose
273 -- declaration is completed. Def_Id is needed to analyze the spec.
281 -- This is one of the occasions on which we transform the tree during
282 -- semantic analysis. If this is a completion, transform the expression
283 -- function into an equivalent subprogram body, and analyze it.
285 -- Expression functions are inlined unconditionally. The back-end will
286 -- determine whether this is possible.
288 Inline_Processing_Required := True;
290 -- Create a specification for the generated body. This must be done
291 -- prior to the analysis of the initial declaration.
293 New_Spec := Copy_Subprogram_Spec (Spec);
294 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
296 -- If there are previous overloadable entities with the same name,
297 -- check whether any of them is completed by the expression function.
298 -- In a generic context a formal subprogram has no completion.
301 and then Is_Overloadable (Prev)
302 and then not Is_Formal_Subprogram (Prev)
304 Def_Id := Analyze_Subprogram_Specification (Spec);
305 Prev := Find_Corresponding_Spec (N);
307 -- The previous entity may be an expression function as well, in
308 -- which case the redeclaration is illegal.
311 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
312 N_Expression_Function
314 Error_Msg_Sloc := Sloc (Prev);
315 Error_Msg_N ("& conflicts with declaration#", Def_Id);
320 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
323 Make_Subprogram_Body (Loc,
324 Specification => New_Spec,
325 Declarations => Empty_List,
326 Handled_Statement_Sequence =>
327 Make_Handled_Sequence_Of_Statements (LocX,
328 Statements => New_List (Ret)));
330 -- If the expression completes a generic subprogram, we must create a
331 -- separate node for the body, because at instantiation the original
332 -- node of the generic copy must be a generic subprogram body, and
333 -- cannot be a expression function. Otherwise we just rewrite the
334 -- expression with the non-generic body.
336 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
337 Insert_After (N, New_Body);
339 -- Propagate any aspects or pragmas that apply to the expression
340 -- function to the proper body when the expression function acts
343 if Has_Aspects (N) then
344 Move_Aspects (N, To => New_Body);
347 Relocate_Pragmas_To_Body (New_Body);
349 Rewrite (N, Make_Null_Statement (Loc));
350 Set_Has_Completion (Prev, False);
353 Set_Is_Inlined (Prev);
355 -- If the expression function is a completion, the previous declaration
356 -- must come from source. We know already that appears in the current
357 -- scope. The entity itself may be internally created if within a body
361 and then Comes_From_Source (Parent (Prev))
362 and then not Is_Formal_Subprogram (Prev)
364 Set_Has_Completion (Prev, False);
366 -- An expression function that is a completion freezes the
367 -- expression. This means freezing the return type, and if it is
368 -- an access type, freezing its designated type as well.
370 -- Note that we cannot defer this freezing to the analysis of the
371 -- expression itself, because a freeze node might appear in a nested
372 -- scope, leading to an elaboration order issue in gigi.
374 Freeze_Before (N, Etype (Prev));
376 if Is_Access_Type (Etype (Prev)) then
377 Freeze_Before (N, Designated_Type (Etype (Prev)));
380 -- For navigation purposes, indicate that the function is a body
382 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
383 Rewrite (N, New_Body);
385 -- Correct the parent pointer of the aspect specification list to
386 -- reference the rewritten node.
388 if Has_Aspects (N) then
389 Set_Parent (Aspect_Specifications (N), N);
392 -- Propagate any pragmas that apply to the expression function to the
393 -- proper body when the expression function acts as a completion.
394 -- Aspects are automatically transfered because of node rewriting.
396 Relocate_Pragmas_To_Body (N);
399 -- Prev is the previous entity with the same name, but it is can
400 -- be an unrelated spec that is not completed by the expression
401 -- function. In that case the relevant entity is the one in the body.
402 -- Not clear that the backend can inline it in this case ???
404 if Has_Completion (Prev) then
405 Set_Is_Inlined (Prev);
407 -- The formals of the expression function are body formals,
408 -- and do not appear in the ali file, which will only contain
409 -- references to the formals of the original subprogram spec.
416 F1 := First_Formal (Def_Id);
417 F2 := First_Formal (Prev);
419 while Present (F1) loop
420 Set_Spec_Entity (F1, F2);
427 Set_Is_Inlined (Defining_Entity (New_Body));
430 -- If this is not a completion, create both a declaration and a body, so
431 -- that the expression can be inlined whenever possible.
434 -- An expression function that is not a completion is not a
435 -- subprogram declaration, and thus cannot appear in a protected
438 if Nkind (Parent (N)) = N_Protected_Definition then
440 ("an expression function is not a legal protected operation", N);
443 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
445 -- Correct the parent pointer of the aspect specification list to
446 -- reference the rewritten node.
448 if Has_Aspects (N) then
449 Set_Parent (Aspect_Specifications (N), N);
454 -- If aspect SPARK_Mode was specified on the body, it needs to be
455 -- repeated both on the generated spec and the body.
457 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
459 if Present (Asp) then
460 Asp := New_Copy_Tree (Asp);
461 Set_Analyzed (Asp, False);
462 Set_Aspect_Specifications (New_Body, New_List (Asp));
465 -- Within a generic pre-analyze the original expression for name
466 -- capture. The body is also generated but plays no role in
467 -- this because it is not part of the original source.
469 if Inside_A_Generic then
471 Id : constant Entity_Id := Defining_Entity (N);
474 Set_Has_Completion (Id);
476 Install_Formals (Id);
477 Preanalyze_Spec_Expression (Expr, Etype (Id));
482 Set_Is_Inlined (Defining_Entity (N));
484 -- Establish the linkages between the spec and the body. These are
485 -- used when the expression function acts as the prefix of attribute
486 -- 'Access in order to freeze the original expression which has been
487 -- moved to the generated body.
489 Set_Corresponding_Body (N, Defining_Entity (New_Body));
490 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
492 -- To prevent premature freeze action, insert the new body at the end
493 -- of the current declarations, or at the end of the package spec.
494 -- However, resolve usage names now, to prevent spurious visibility
495 -- on later entities. Note that the function can now be called in
496 -- the current declarative part, which will appear to be prior to
497 -- the presence of the body in the code. There are nevertheless no
498 -- order of elaboration issues because all name resolution has taken
499 -- place at the point of declaration.
502 Decls : List_Id := List_Containing (N);
503 Par : constant Node_Id := Parent (Decls);
504 Id : constant Entity_Id := Defining_Entity (N);
507 -- If this is a wrapper created for in an instance for a formal
508 -- subprogram, insert body after declaration, to be analyzed when
509 -- the enclosing instance is analyzed.
512 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
514 Insert_After (N, New_Body);
517 if Nkind (Par) = N_Package_Specification
518 and then Decls = Visible_Declarations (Par)
519 and then Present (Private_Declarations (Par))
520 and then not Is_Empty_List (Private_Declarations (Par))
522 Decls := Private_Declarations (Par);
525 Insert_After (Last (Decls), New_Body);
527 Install_Formals (Id);
529 -- Preanalyze the expression for name capture, except in an
530 -- instance, where this has been done during generic analysis,
531 -- and will be redone when analyzing the body.
534 Expr : constant Node_Id := Expression (Ret);
537 Set_Parent (Expr, Ret);
539 if not In_Instance then
540 Preanalyze_Spec_Expression (Expr, Etype (Id));
549 -- If the return expression is a static constant, we suppress warning
550 -- messages on unused formals, which in most cases will be noise.
552 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
553 Is_OK_Static_Expression (Expr));
554 end Analyze_Expression_Function;
556 ----------------------------------------
557 -- Analyze_Extended_Return_Statement --
558 ----------------------------------------
560 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
562 Check_Compiler_Unit ("extended return statement", N);
563 Analyze_Return_Statement (N);
564 end Analyze_Extended_Return_Statement;
566 ----------------------------
567 -- Analyze_Function_Call --
568 ----------------------------
570 procedure Analyze_Function_Call (N : Node_Id) is
571 Actuals : constant List_Id := Parameter_Associations (N);
572 Func_Nam : constant Node_Id := Name (N);
578 -- A call of the form A.B (X) may be an Ada 2005 call, which is
579 -- rewritten as B (A, X). If the rewriting is successful, the call
580 -- has been analyzed and we just return.
582 if Nkind (Func_Nam) = N_Selected_Component
583 and then Name (N) /= Func_Nam
584 and then Is_Rewrite_Substitution (N)
585 and then Present (Etype (N))
590 -- If error analyzing name, then set Any_Type as result type and return
592 if Etype (Func_Nam) = Any_Type then
593 Set_Etype (N, Any_Type);
597 -- Otherwise analyze the parameters
599 if Present (Actuals) then
600 Actual := First (Actuals);
601 while Present (Actual) loop
603 Check_Parameterless_Call (Actual);
609 end Analyze_Function_Call;
611 -----------------------------
612 -- Analyze_Function_Return --
613 -----------------------------
615 procedure Analyze_Function_Return (N : Node_Id) is
616 Loc : constant Source_Ptr := Sloc (N);
617 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
618 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
620 R_Type : constant Entity_Id := Etype (Scope_Id);
621 -- Function result subtype
623 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
624 -- Apply legality rule of 6.5 (8.2) to the access discriminants of an
625 -- aggregate in a return statement.
627 procedure Check_Limited_Return (Expr : Node_Id);
628 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
629 -- limited types. Used only for simple return statements.
630 -- Expr is the expression returned.
632 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
633 -- Check that the return_subtype_indication properly matches the result
634 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
636 -----------------------------------
637 -- Check_Aggregate_Accessibility --
638 -----------------------------------
640 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
641 Typ : constant Entity_Id := Etype (Aggr);
648 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
649 Discr := First_Discriminant (Typ);
650 Assoc := First (Component_Associations (Aggr));
651 while Present (Discr) loop
652 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
653 Expr := Expression (Assoc);
654 if Nkind (Expr) = N_Attribute_Reference
655 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
657 Obj := Prefix (Expr);
658 while Nkind_In (Obj, N_Indexed_Component,
659 N_Selected_Component)
664 -- No check needed for an aliased formal.
665 -- A run-time check may still be needed ???
667 if Is_Entity_Name (Obj)
668 and then Is_Formal (Entity (Obj))
669 and then Is_Aliased (Entity (Obj))
673 elsif Object_Access_Level (Obj) >
674 Scope_Depth (Scope (Scope_Id))
677 ("access discriminant in return aggregate will be "
678 & "a dangling reference", Obj);
683 Next_Discriminant (Discr);
686 end Check_Aggregate_Accessibility;
688 --------------------------
689 -- Check_Limited_Return --
690 --------------------------
692 procedure Check_Limited_Return (Expr : Node_Id) is
694 -- Ada 2005 (AI-318-02): Return-by-reference types have been
695 -- removed and replaced by anonymous access results. This is an
696 -- incompatibility with Ada 95. Not clear whether this should be
697 -- enforced yet or perhaps controllable with special switch. ???
699 -- A limited interface that is not immutably limited is OK.
701 if Is_Limited_Interface (R_Type)
703 not (Is_Task_Interface (R_Type)
704 or else Is_Protected_Interface (R_Type)
705 or else Is_Synchronized_Interface (R_Type))
709 elsif Is_Limited_Type (R_Type)
710 and then not Is_Interface (R_Type)
711 and then Comes_From_Source (N)
712 and then not In_Instance_Body
713 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
717 if Ada_Version >= Ada_2005
718 and then not Debug_Flag_Dot_L
719 and then not GNAT_Mode
722 ("(Ada 2005) cannot copy object of a limited type "
723 & "(RM-2005 6.5(5.5/2))", Expr);
725 if Is_Limited_View (R_Type) then
727 ("\return by reference not permitted in Ada 2005", Expr);
730 -- Warn in Ada 95 mode, to give folks a heads up about this
733 -- In GNAT mode, this is just a warning, to allow it to be
734 -- evilly turned off. Otherwise it is a real error.
736 -- In a generic context, simplify the warning because it makes
737 -- no sense to discuss pass-by-reference or copy.
739 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
740 if Inside_A_Generic then
742 ("return of limited object not permitted in Ada 2005 "
743 & "(RM-2005 6.5(5.5/2))?y?", Expr);
745 elsif Is_Limited_View (R_Type) then
747 ("return by reference not permitted in Ada 2005 "
748 & "(RM-2005 6.5(5.5/2))?y?", Expr);
751 ("cannot copy object of a limited type in Ada 2005 "
752 & "(RM-2005 6.5(5.5/2))?y?", Expr);
755 -- Ada 95 mode, compatibility warnings disabled
758 return; -- skip continuation messages below
761 if not Inside_A_Generic then
763 ("\consider switching to return of access type", Expr);
764 Explain_Limited_Type (R_Type, Expr);
767 end Check_Limited_Return;
769 -------------------------------------
770 -- Check_Return_Subtype_Indication --
771 -------------------------------------
773 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
774 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
776 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
777 -- Subtype given in the extended return statement (must match R_Type)
779 Subtype_Ind : constant Node_Id :=
780 Object_Definition (Original_Node (Obj_Decl));
782 R_Type_Is_Anon_Access : constant Boolean :=
784 E_Anonymous_Access_Subprogram_Type,
785 E_Anonymous_Access_Protected_Subprogram_Type,
786 E_Anonymous_Access_Type);
787 -- True if return type of the function is an anonymous access type
788 -- Can't we make Is_Anonymous_Access_Type in einfo ???
790 R_Stm_Type_Is_Anon_Access : constant Boolean :=
791 Ekind_In (R_Stm_Type,
792 E_Anonymous_Access_Subprogram_Type,
793 E_Anonymous_Access_Protected_Subprogram_Type,
794 E_Anonymous_Access_Type);
795 -- True if type of the return object is an anonymous access type
797 procedure Error_No_Match (N : Node_Id);
798 -- Output error messages for case where types do not statically
799 -- match. N is the location for the messages.
805 procedure Error_No_Match (N : Node_Id) is
808 ("subtype must statically match function result subtype", N);
810 if not Predicates_Match (R_Stm_Type, R_Type) then
811 Error_Msg_Node_2 := R_Type;
813 ("\predicate of& does not match predicate of&",
818 -- Start of processing for Check_Return_Subtype_Indication
821 -- First, avoid cascaded errors
823 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
827 -- "return access T" case; check that the return statement also has
828 -- "access T", and that the subtypes statically match:
829 -- if this is an access to subprogram the signatures must match.
831 if R_Type_Is_Anon_Access then
832 if R_Stm_Type_Is_Anon_Access then
834 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
836 if Base_Type (Designated_Type (R_Stm_Type)) /=
837 Base_Type (Designated_Type (R_Type))
838 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
840 Error_No_Match (Subtype_Mark (Subtype_Ind));
844 -- For two anonymous access to subprogram types, the
845 -- types themselves must be type conformant.
847 if not Conforming_Types
848 (R_Stm_Type, R_Type, Fully_Conformant)
850 Error_No_Match (Subtype_Ind);
855 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
858 -- If the return object is of an anonymous access type, then report
859 -- an error if the function's result type is not also anonymous.
861 elsif R_Stm_Type_Is_Anon_Access
862 and then not R_Type_Is_Anon_Access
864 Error_Msg_N ("anonymous access not allowed for function with "
865 & "named access result", Subtype_Ind);
867 -- Subtype indication case: check that the return object's type is
868 -- covered by the result type, and that the subtypes statically match
869 -- when the result subtype is constrained. Also handle record types
870 -- with unknown discriminants for which we have built the underlying
871 -- record view. Coverage is needed to allow specific-type return
872 -- objects when the result type is class-wide (see AI05-32).
874 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
875 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
879 Underlying_Record_View (Base_Type (R_Stm_Type))))
881 -- A null exclusion may be present on the return type, on the
882 -- function specification, on the object declaration or on the
885 if Is_Access_Type (R_Type)
887 (Can_Never_Be_Null (R_Type)
888 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
889 Can_Never_Be_Null (R_Stm_Type)
891 Error_No_Match (Subtype_Ind);
894 -- AI05-103: for elementary types, subtypes must statically match
896 if Is_Constrained (R_Type)
897 or else Is_Access_Type (R_Type)
899 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
900 Error_No_Match (Subtype_Ind);
904 -- All remaining cases are illegal
906 -- Note: previous versions of this subprogram allowed the return
907 -- value to be the ancestor of the return type if the return type
908 -- was a null extension. This was plainly incorrect.
912 ("wrong type for return_subtype_indication", Subtype_Ind);
914 end Check_Return_Subtype_Indication;
916 ---------------------
917 -- Local Variables --
918 ---------------------
923 -- Start of processing for Analyze_Function_Return
926 Set_Return_Present (Scope_Id);
928 if Nkind (N) = N_Simple_Return_Statement then
929 Expr := Expression (N);
931 -- Guard against a malformed expression. The parser may have tried to
932 -- recover but the node is not analyzable.
934 if Nkind (Expr) = N_Error then
935 Set_Etype (Expr, Any_Type);
936 Expander_Mode_Save_And_Set (False);
940 -- The resolution of a controlled [extension] aggregate associated
941 -- with a return statement creates a temporary which needs to be
942 -- finalized on function exit. Wrap the return statement inside a
943 -- block so that the finalization machinery can detect this case.
944 -- This early expansion is done only when the return statement is
945 -- not part of a handled sequence of statements.
947 if Nkind_In (Expr, N_Aggregate,
948 N_Extension_Aggregate)
949 and then Needs_Finalization (R_Type)
950 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
953 Make_Block_Statement (Loc,
954 Handled_Statement_Sequence =>
955 Make_Handled_Sequence_Of_Statements (Loc,
956 Statements => New_List (Relocate_Node (N)))));
964 -- Ada 2005 (AI-251): If the type of the returned object is
965 -- an access to an interface type then we add an implicit type
966 -- conversion to force the displacement of the "this" pointer to
967 -- reference the secondary dispatch table. We cannot delay the
968 -- generation of this implicit conversion until the expansion
969 -- because in this case the type resolution changes the decoration
970 -- of the expression node to match R_Type; by contrast, if the
971 -- returned object is a class-wide interface type then it is too
972 -- early to generate here the implicit conversion since the return
973 -- statement may be rewritten by the expander into an extended
974 -- return statement whose expansion takes care of adding the
975 -- implicit type conversion to displace the pointer to the object.
978 and then Serious_Errors_Detected = 0
979 and then Is_Access_Type (R_Type)
980 and then Nkind (Expr) /= N_Null
981 and then Is_Interface (Designated_Type (R_Type))
982 and then Is_Progenitor (Designated_Type (R_Type),
983 Designated_Type (Etype (Expr)))
985 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
989 Resolve (Expr, R_Type);
990 Check_Limited_Return (Expr);
992 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
993 Check_Aggregate_Accessibility (Expr);
997 -- RETURN only allowed in SPARK as the last statement in function
999 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1001 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
1002 or else Present (Next (N)))
1004 Check_SPARK_05_Restriction
1005 ("RETURN should be the last statement in function", N);
1009 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
1010 Obj_Decl := Last (Return_Object_Declarations (N));
1012 -- Analyze parts specific to extended_return_statement:
1015 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1016 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1019 Expr := Expression (Obj_Decl);
1021 -- Note: The check for OK_For_Limited_Init will happen in
1022 -- Analyze_Object_Declaration; we treat it as a normal
1023 -- object declaration.
1025 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1028 Check_Return_Subtype_Indication (Obj_Decl);
1030 if Present (HSS) then
1033 if Present (Exception_Handlers (HSS)) then
1035 -- ???Has_Nested_Block_With_Handler needs to be set.
1036 -- Probably by creating an actual N_Block_Statement.
1037 -- Probably in Expand.
1043 -- Mark the return object as referenced, since the return is an
1044 -- implicit reference of the object.
1046 Set_Referenced (Defining_Identifier (Obj_Decl));
1048 Check_References (Stm_Entity);
1050 -- Check RM 6.5 (5.9/3)
1053 if Ada_Version < Ada_2012 then
1055 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1056 -- Can it really happen (extended return???)
1059 ("aliased only allowed for limited return objects "
1060 & "in Ada 2012??", N);
1062 elsif not Is_Limited_View (R_Type) then
1064 ("aliased only allowed for limited return objects", N);
1070 -- Case of Expr present
1074 -- Defend against previous errors
1076 and then Nkind (Expr) /= N_Empty
1077 and then Present (Etype (Expr))
1079 -- Apply constraint check. Note that this is done before the implicit
1080 -- conversion of the expression done for anonymous access types to
1081 -- ensure correct generation of the null-excluding check associated
1082 -- with null-excluding expressions found in return statements.
1084 Apply_Constraint_Check (Expr, R_Type);
1086 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1087 -- type, apply an implicit conversion of the expression to that type
1088 -- to force appropriate static and run-time accessibility checks.
1090 if Ada_Version >= Ada_2005
1091 and then Ekind (R_Type) = E_Anonymous_Access_Type
1093 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1094 Analyze_And_Resolve (Expr, R_Type);
1096 -- If this is a local anonymous access to subprogram, the
1097 -- accessibility check can be applied statically. The return is
1098 -- illegal if the access type of the return expression is declared
1099 -- inside of the subprogram (except if it is the subtype indication
1100 -- of an extended return statement).
1102 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1103 if not Comes_From_Source (Current_Scope)
1104 or else Ekind (Current_Scope) = E_Return_Statement
1109 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1111 Error_Msg_N ("cannot return local access to subprogram", N);
1114 -- The expression cannot be of a formal incomplete type
1116 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1117 and then Is_Generic_Type (Etype (Expr))
1120 ("cannot return expression of a formal incomplete type", N);
1123 -- If the result type is class-wide, then check that the return
1124 -- expression's type is not declared at a deeper level than the
1125 -- function (RM05-6.5(5.6/2)).
1127 if Ada_Version >= Ada_2005
1128 and then Is_Class_Wide_Type (R_Type)
1130 if Type_Access_Level (Etype (Expr)) >
1131 Subprogram_Access_Level (Scope_Id)
1134 ("level of return expression type is deeper than "
1135 & "class-wide function!", Expr);
1139 -- Check incorrect use of dynamically tagged expression
1141 if Is_Tagged_Type (R_Type) then
1142 Check_Dynamically_Tagged_Expression
1148 -- ??? A real run-time accessibility check is needed in cases
1149 -- involving dereferences of access parameters. For now we just
1150 -- check the static cases.
1152 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1153 and then Is_Limited_View (Etype (Scope_Id))
1154 and then Object_Access_Level (Expr) >
1155 Subprogram_Access_Level (Scope_Id)
1157 -- Suppress the message in a generic, where the rewriting
1160 if Inside_A_Generic then
1165 Make_Raise_Program_Error (Loc,
1166 Reason => PE_Accessibility_Check_Failed));
1169 Error_Msg_Warn := SPARK_Mode /= On;
1170 Error_Msg_N ("cannot return a local value by reference<<", N);
1171 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1175 if Known_Null (Expr)
1176 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1177 and then Null_Exclusion_Present (Parent (Scope_Id))
1179 Apply_Compile_Time_Constraint_Error
1181 Msg => "(Ada 2005) null not allowed for "
1182 & "null-excluding return??",
1183 Reason => CE_Null_Not_Allowed);
1186 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1187 -- has no initializing expression.
1189 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1190 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1191 Subprogram_Access_Level (Scope_Id)
1194 ("level of return expression type is deeper than "
1195 & "class-wide function!", Obj_Decl);
1198 end Analyze_Function_Return;
1200 -------------------------------------
1201 -- Analyze_Generic_Subprogram_Body --
1202 -------------------------------------
1204 procedure Analyze_Generic_Subprogram_Body
1208 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1209 Kind : constant Entity_Kind := Ekind (Gen_Id);
1210 Body_Id : Entity_Id;
1215 -- Copy body and disable expansion while analyzing the generic For a
1216 -- stub, do not copy the stub (which would load the proper body), this
1217 -- will be done when the proper body is analyzed.
1219 if Nkind (N) /= N_Subprogram_Body_Stub then
1220 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1223 -- Once the contents of the generic copy and the template are
1224 -- swapped, do the same for their respective aspect specifications.
1226 Exchange_Aspects (N, New_N);
1228 -- Collect all contract-related source pragmas found within the
1229 -- template and attach them to the contract of the subprogram body.
1230 -- This contract is used in the capture of global references within
1233 Create_Generic_Contract (N);
1238 Spec := Specification (N);
1240 -- Within the body of the generic, the subprogram is callable, and
1241 -- behaves like the corresponding non-generic unit.
1243 Body_Id := Defining_Entity (Spec);
1245 if Kind = E_Generic_Procedure
1246 and then Nkind (Spec) /= N_Procedure_Specification
1248 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1251 elsif Kind = E_Generic_Function
1252 and then Nkind (Spec) /= N_Function_Specification
1254 Error_Msg_N ("invalid body for generic function ", Body_Id);
1258 Set_Corresponding_Body (Gen_Decl, Body_Id);
1260 if Has_Completion (Gen_Id)
1261 and then Nkind (Parent (N)) /= N_Subunit
1263 Error_Msg_N ("duplicate generic body", N);
1266 Set_Has_Completion (Gen_Id);
1269 if Nkind (N) = N_Subprogram_Body_Stub then
1270 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1272 Set_Corresponding_Spec (N, Gen_Id);
1275 if Nkind (Parent (N)) = N_Compilation_Unit then
1276 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1279 -- Make generic parameters immediately visible in the body. They are
1280 -- needed to process the formals declarations. Then make the formals
1281 -- visible in a separate step.
1283 Push_Scope (Gen_Id);
1287 First_Ent : Entity_Id;
1290 First_Ent := First_Entity (Gen_Id);
1293 while Present (E) and then not Is_Formal (E) loop
1298 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1300 -- Now generic formals are visible, and the specification can be
1301 -- analyzed, for subsequent conformance check.
1303 Body_Id := Analyze_Subprogram_Specification (Spec);
1305 -- Make formal parameters visible
1309 -- E is the first formal parameter, we loop through the formals
1310 -- installing them so that they will be visible.
1312 Set_First_Entity (Gen_Id, E);
1313 while Present (E) loop
1319 -- Visible generic entity is callable within its own body
1321 Set_Ekind (Gen_Id, Ekind (Body_Id));
1322 Set_Ekind (Body_Id, E_Subprogram_Body);
1323 Set_Convention (Body_Id, Convention (Gen_Id));
1324 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1325 Set_Scope (Body_Id, Scope (Gen_Id));
1327 -- Inherit the "ghostness" of the generic spec. Note that this
1328 -- property is not directly inherited as the body may be subject
1329 -- to a different Ghost assertion policy.
1331 if Ghost_Mode > None or else Is_Ghost_Entity (Gen_Id) then
1332 Set_Is_Ghost_Entity (Body_Id);
1334 -- The Ghost policy in effect at the point of declaration and at
1335 -- the point of completion must match (SPARK RM 6.9(14)).
1337 Check_Ghost_Completion (Gen_Id, Body_Id);
1340 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1342 if Nkind (N) = N_Subprogram_Body_Stub then
1344 -- No body to analyze, so restore state of generic unit
1346 Set_Ekind (Gen_Id, Kind);
1347 Set_Ekind (Body_Id, Kind);
1349 if Present (First_Ent) then
1350 Set_First_Entity (Gen_Id, First_Ent);
1357 -- If this is a compilation unit, it must be made visible explicitly,
1358 -- because the compilation of the declaration, unlike other library
1359 -- unit declarations, does not. If it is not a unit, the following
1360 -- is redundant but harmless.
1362 Set_Is_Immediately_Visible (Gen_Id);
1363 Reference_Body_Formals (Gen_Id, Body_Id);
1365 if Is_Child_Unit (Gen_Id) then
1366 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1369 Set_Actual_Subtypes (N, Current_Scope);
1371 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1372 Set_SPARK_Pragma_Inherited (Body_Id);
1374 -- Analyze any aspect specifications that appear on the generic
1377 if Has_Aspects (N) then
1378 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1381 Analyze_Declarations (Declarations (N));
1384 -- Process the contract of the subprogram body after all declarations
1385 -- have been analyzed. This ensures that any contract-related pragmas
1386 -- are available through the N_Contract node of the body.
1388 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1390 Analyze (Handled_Statement_Sequence (N));
1391 Save_Global_References (Original_Node (N));
1393 -- Prior to exiting the scope, include generic formals again (if any
1394 -- are present) in the set of local entities.
1396 if Present (First_Ent) then
1397 Set_First_Entity (Gen_Id, First_Ent);
1400 Check_References (Gen_Id);
1403 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1405 Check_Subprogram_Order (N);
1407 -- Outside of its body, unit is generic again
1409 Set_Ekind (Gen_Id, Kind);
1410 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1413 Style.Check_Identifier (Body_Id, Gen_Id);
1417 end Analyze_Generic_Subprogram_Body;
1419 ----------------------------
1420 -- Analyze_Null_Procedure --
1421 ----------------------------
1423 procedure Analyze_Null_Procedure
1425 Is_Completion : out Boolean)
1427 Loc : constant Source_Ptr := Sloc (N);
1428 Spec : constant Node_Id := Specification (N);
1429 Designator : Entity_Id;
1431 Null_Body : Node_Id := Empty;
1435 -- Capture the profile of the null procedure before analysis, for
1436 -- expansion at the freeze point and at each point of call. The body is
1437 -- used if the procedure has preconditions, or if it is a completion. In
1438 -- the first case the body is analyzed at the freeze point, in the other
1439 -- it replaces the null procedure declaration.
1442 Make_Subprogram_Body (Loc,
1443 Specification => New_Copy_Tree (Spec),
1444 Declarations => New_List,
1445 Handled_Statement_Sequence =>
1446 Make_Handled_Sequence_Of_Statements (Loc,
1447 Statements => New_List (Make_Null_Statement (Loc))));
1449 -- Create new entities for body and formals
1451 Set_Defining_Unit_Name (Specification (Null_Body),
1452 Make_Defining_Identifier
1453 (Sloc (Defining_Entity (N)),
1454 Chars (Defining_Entity (N))));
1456 Form := First (Parameter_Specifications (Specification (Null_Body)));
1457 while Present (Form) loop
1458 Set_Defining_Identifier (Form,
1459 Make_Defining_Identifier
1460 (Sloc (Defining_Identifier (Form)),
1461 Chars (Defining_Identifier (Form))));
1465 -- Determine whether the null procedure may be a completion of a generic
1466 -- suprogram, in which case we use the new null body as the completion
1467 -- and set minimal semantic information on the original declaration,
1468 -- which is rewritten as a null statement.
1470 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1472 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1473 Insert_Before (N, Null_Body);
1474 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1476 Rewrite (N, Make_Null_Statement (Loc));
1477 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1478 Is_Completion := True;
1482 -- Resolve the types of the formals now, because the freeze point
1483 -- may appear in a different context, e.g. an instantiation.
1485 Form := First (Parameter_Specifications (Specification (Null_Body)));
1486 while Present (Form) loop
1487 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1488 Find_Type (Parameter_Type (Form));
1491 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1493 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1496 -- The case of a null procedure with a formal that is an
1497 -- access_to_subprogram type, and that is used as an actual
1498 -- in an instantiation is left to the enthusiastic reader.
1507 -- If there are previous overloadable entities with the same name,
1508 -- check whether any of them is completed by the null procedure.
1510 if Present (Prev) and then Is_Overloadable (Prev) then
1511 Designator := Analyze_Subprogram_Specification (Spec);
1512 Prev := Find_Corresponding_Spec (N);
1515 if No (Prev) or else not Comes_From_Source (Prev) then
1516 Designator := Analyze_Subprogram_Specification (Spec);
1517 Set_Has_Completion (Designator);
1519 -- Signal to caller that this is a procedure declaration
1521 Is_Completion := False;
1523 -- Null procedures are always inlined, but generic formal subprograms
1524 -- which appear as such in the internal instance of formal packages,
1525 -- need no completion and are not marked Inline.
1528 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1530 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1531 Set_Body_To_Inline (N, Null_Body);
1532 Set_Is_Inlined (Designator);
1536 -- The null procedure is a completion. We unconditionally rewrite
1537 -- this as a null body (even if expansion is not active), because
1538 -- there are various error checks that are applied on this body
1539 -- when it is analyzed (e.g. correct aspect placement).
1541 if Has_Completion (Prev) then
1542 Error_Msg_Sloc := Sloc (Prev);
1543 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1546 Is_Completion := True;
1547 Rewrite (N, Null_Body);
1550 end Analyze_Null_Procedure;
1552 -----------------------------
1553 -- Analyze_Operator_Symbol --
1554 -----------------------------
1556 -- An operator symbol such as "+" or "and" may appear in context where the
1557 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1558 -- is just a string, as in (conjunction = "or"). In these cases the parser
1559 -- generates this node, and the semantics does the disambiguation. Other
1560 -- such case are actuals in an instantiation, the generic unit in an
1561 -- instantiation, and pragma arguments.
1563 procedure Analyze_Operator_Symbol (N : Node_Id) is
1564 Par : constant Node_Id := Parent (N);
1567 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1568 or else Nkind (Par) = N_Function_Instantiation
1569 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1570 or else (Nkind (Par) = N_Pragma_Argument_Association
1571 and then not Is_Pragma_String_Literal (Par))
1572 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1573 or else (Nkind (Par) = N_Attribute_Reference
1574 and then Attribute_Name (Par) /= Name_Value)
1576 Find_Direct_Name (N);
1579 Change_Operator_Symbol_To_String_Literal (N);
1582 end Analyze_Operator_Symbol;
1584 -----------------------------------
1585 -- Analyze_Parameter_Association --
1586 -----------------------------------
1588 procedure Analyze_Parameter_Association (N : Node_Id) is
1590 Analyze (Explicit_Actual_Parameter (N));
1591 end Analyze_Parameter_Association;
1593 ----------------------------
1594 -- Analyze_Procedure_Call --
1595 ----------------------------
1597 procedure Analyze_Procedure_Call (N : Node_Id) is
1598 procedure Analyze_Call_And_Resolve;
1599 -- Do Analyze and Resolve calls for procedure call
1600 -- At end, check illegal order dependence.
1602 ------------------------------
1603 -- Analyze_Call_And_Resolve --
1604 ------------------------------
1606 procedure Analyze_Call_And_Resolve is
1608 if Nkind (N) = N_Procedure_Call_Statement then
1610 Resolve (N, Standard_Void_Type);
1614 end Analyze_Call_And_Resolve;
1618 Actuals : constant List_Id := Parameter_Associations (N);
1619 Loc : constant Source_Ptr := Sloc (N);
1620 P : constant Node_Id := Name (N);
1624 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1626 -- Start of processing for Analyze_Procedure_Call
1629 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1630 -- a procedure call or an entry call. The prefix may denote an access
1631 -- to subprogram type, in which case an implicit dereference applies.
1632 -- If the prefix is an indexed component (without implicit dereference)
1633 -- then the construct denotes a call to a member of an entire family.
1634 -- If the prefix is a simple name, it may still denote a call to a
1635 -- parameterless member of an entry family. Resolution of these various
1636 -- interpretations is delicate.
1640 -- If this is a call of the form Obj.Op, the call may have been
1641 -- analyzed and possibly rewritten into a block, in which case
1644 if Analyzed (N) then
1648 -- If there is an error analyzing the name (which may have been
1649 -- rewritten if the original call was in prefix notation) then error
1650 -- has been emitted already, mark node and return.
1652 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1653 Set_Etype (N, Any_Type);
1657 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1658 -- Set the mode now to ensure that any nodes generated during analysis
1659 -- and expansion are properly marked as Ghost.
1663 -- Otherwise analyze the parameters
1665 if Present (Actuals) then
1666 Actual := First (Actuals);
1668 while Present (Actual) loop
1670 Check_Parameterless_Call (Actual);
1675 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1677 if Nkind (P) = N_Attribute_Reference
1678 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1680 Name_Elab_Subp_Body)
1682 if Present (Actuals) then
1684 ("no parameters allowed for this call", First (Actuals));
1688 Set_Etype (N, Standard_Void_Type);
1691 elsif Is_Entity_Name (P)
1692 and then Is_Record_Type (Etype (Entity (P)))
1693 and then Remote_AST_I_Dereference (P)
1695 Ghost_Mode := Save_Ghost_Mode;
1698 elsif Is_Entity_Name (P)
1699 and then Ekind (Entity (P)) /= E_Entry_Family
1701 if Is_Access_Type (Etype (P))
1702 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1703 and then No (Actuals)
1704 and then Comes_From_Source (N)
1706 Error_Msg_N ("missing explicit dereference in call", N);
1709 Analyze_Call_And_Resolve;
1711 -- If the prefix is the simple name of an entry family, this is
1712 -- a parameterless call from within the task body itself.
1714 elsif Is_Entity_Name (P)
1715 and then Nkind (P) = N_Identifier
1716 and then Ekind (Entity (P)) = E_Entry_Family
1717 and then Present (Actuals)
1718 and then No (Next (First (Actuals)))
1720 -- Can be call to parameterless entry family. What appears to be the
1721 -- sole argument is in fact the entry index. Rewrite prefix of node
1722 -- accordingly. Source representation is unchanged by this
1726 Make_Indexed_Component (Loc,
1728 Make_Selected_Component (Loc,
1729 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1730 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1731 Expressions => Actuals);
1732 Set_Name (N, New_N);
1733 Set_Etype (New_N, Standard_Void_Type);
1734 Set_Parameter_Associations (N, No_List);
1735 Analyze_Call_And_Resolve;
1737 elsif Nkind (P) = N_Explicit_Dereference then
1738 if Ekind (Etype (P)) = E_Subprogram_Type then
1739 Analyze_Call_And_Resolve;
1741 Error_Msg_N ("expect access to procedure in call", P);
1744 -- The name can be a selected component or an indexed component that
1745 -- yields an access to subprogram. Such a prefix is legal if the call
1746 -- has parameter associations.
1748 elsif Is_Access_Type (Etype (P))
1749 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1751 if Present (Actuals) then
1752 Analyze_Call_And_Resolve;
1754 Error_Msg_N ("missing explicit dereference in call ", N);
1757 -- If not an access to subprogram, then the prefix must resolve to the
1758 -- name of an entry, entry family, or protected operation.
1760 -- For the case of a simple entry call, P is a selected component where
1761 -- the prefix is the task and the selector name is the entry. A call to
1762 -- a protected procedure will have the same syntax. If the protected
1763 -- object contains overloaded operations, the entity may appear as a
1764 -- function, the context will select the operation whose type is Void.
1766 elsif Nkind (P) = N_Selected_Component
1767 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1771 Analyze_Call_And_Resolve;
1773 elsif Nkind (P) = N_Selected_Component
1774 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1775 and then Present (Actuals)
1776 and then No (Next (First (Actuals)))
1778 -- Can be call to parameterless entry family. What appears to be the
1779 -- sole argument is in fact the entry index. Rewrite prefix of node
1780 -- accordingly. Source representation is unchanged by this
1784 Make_Indexed_Component (Loc,
1785 Prefix => New_Copy (P),
1786 Expressions => Actuals);
1787 Set_Name (N, New_N);
1788 Set_Etype (New_N, Standard_Void_Type);
1789 Set_Parameter_Associations (N, No_List);
1790 Analyze_Call_And_Resolve;
1792 -- For the case of a reference to an element of an entry family, P is
1793 -- an indexed component whose prefix is a selected component (task and
1794 -- entry family), and whose index is the entry family index.
1796 elsif Nkind (P) = N_Indexed_Component
1797 and then Nkind (Prefix (P)) = N_Selected_Component
1798 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1800 Analyze_Call_And_Resolve;
1802 -- If the prefix is the name of an entry family, it is a call from
1803 -- within the task body itself.
1805 elsif Nkind (P) = N_Indexed_Component
1806 and then Nkind (Prefix (P)) = N_Identifier
1807 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1810 Make_Selected_Component (Loc,
1811 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1812 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1813 Rewrite (Prefix (P), New_N);
1815 Analyze_Call_And_Resolve;
1817 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1818 -- procedure name, so the construct can only be a qualified expression.
1820 elsif Nkind (P) = N_Qualified_Expression
1821 and then Ada_Version >= Ada_2012
1823 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1826 -- Anything else is an error
1829 Error_Msg_N ("invalid procedure or entry call", N);
1832 Ghost_Mode := Save_Ghost_Mode;
1833 end Analyze_Procedure_Call;
1835 ------------------------------
1836 -- Analyze_Return_Statement --
1837 ------------------------------
1839 procedure Analyze_Return_Statement (N : Node_Id) is
1841 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1842 N_Extended_Return_Statement));
1844 Returns_Object : constant Boolean :=
1845 Nkind (N) = N_Extended_Return_Statement
1847 (Nkind (N) = N_Simple_Return_Statement
1848 and then Present (Expression (N)));
1849 -- True if we're returning something; that is, "return <expression>;"
1850 -- or "return Result : T [:= ...]". False for "return;". Used for error
1851 -- checking: If Returns_Object is True, N should apply to a function
1852 -- body; otherwise N should apply to a procedure body, entry body,
1853 -- accept statement, or extended return statement.
1855 function Find_What_It_Applies_To return Entity_Id;
1856 -- Find the entity representing the innermost enclosing body, accept
1857 -- statement, or extended return statement. If the result is a callable
1858 -- construct or extended return statement, then this will be the value
1859 -- of the Return_Applies_To attribute. Otherwise, the program is
1860 -- illegal. See RM-6.5(4/2).
1862 -----------------------------
1863 -- Find_What_It_Applies_To --
1864 -----------------------------
1866 function Find_What_It_Applies_To return Entity_Id is
1867 Result : Entity_Id := Empty;
1870 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1871 -- and postconditions.
1873 for J in reverse 0 .. Scope_Stack.Last loop
1874 Result := Scope_Stack.Table (J).Entity;
1875 exit when not Ekind_In (Result, E_Block, E_Loop)
1876 and then Chars (Result) /= Name_uPostconditions;
1879 pragma Assert (Present (Result));
1881 end Find_What_It_Applies_To;
1883 -- Local declarations
1885 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1886 Kind : constant Entity_Kind := Ekind (Scope_Id);
1887 Loc : constant Source_Ptr := Sloc (N);
1888 Stm_Entity : constant Entity_Id :=
1890 (E_Return_Statement, Current_Scope, Loc, 'R');
1892 -- Start of processing for Analyze_Return_Statement
1895 Set_Return_Statement_Entity (N, Stm_Entity);
1897 Set_Etype (Stm_Entity, Standard_Void_Type);
1898 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1900 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1901 -- (4/2): an inner return statement will apply to this extended return.
1903 if Nkind (N) = N_Extended_Return_Statement then
1904 Push_Scope (Stm_Entity);
1907 -- Check that pragma No_Return is obeyed. Don't complain about the
1908 -- implicitly-generated return that is placed at the end.
1910 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1911 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1914 -- Warn on any unassigned OUT parameters if in procedure
1916 if Ekind (Scope_Id) = E_Procedure then
1917 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1920 -- Check that functions return objects, and other things do not
1922 if Kind = E_Function or else Kind = E_Generic_Function then
1923 if not Returns_Object then
1924 Error_Msg_N ("missing expression in return from function", N);
1927 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1928 if Returns_Object then
1929 Error_Msg_N ("procedure cannot return value (use function)", N);
1932 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1933 if Returns_Object then
1934 if Is_Protected_Type (Scope (Scope_Id)) then
1935 Error_Msg_N ("entry body cannot return value", N);
1937 Error_Msg_N ("accept statement cannot return value", N);
1941 elsif Kind = E_Return_Statement then
1943 -- We are nested within another return statement, which must be an
1944 -- extended_return_statement.
1946 if Returns_Object then
1947 if Nkind (N) = N_Extended_Return_Statement then
1949 ("extended return statement cannot be nested (use `RETURN;`)",
1952 -- Case of a simple return statement with a value inside extended
1953 -- return statement.
1957 ("return nested in extended return statement cannot return "
1958 & "value (use `RETURN;`)", N);
1963 Error_Msg_N ("illegal context for return statement", N);
1966 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1967 Analyze_Function_Return (N);
1969 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1970 Set_Return_Present (Scope_Id);
1973 if Nkind (N) = N_Extended_Return_Statement then
1977 Kill_Current_Values (Last_Assignment_Only => True);
1978 Check_Unreachable_Code (N);
1980 Analyze_Dimension (N);
1981 end Analyze_Return_Statement;
1983 -------------------------------------
1984 -- Analyze_Simple_Return_Statement --
1985 -------------------------------------
1987 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1989 if Present (Expression (N)) then
1990 Mark_Coextensions (N, Expression (N));
1993 Analyze_Return_Statement (N);
1994 end Analyze_Simple_Return_Statement;
1996 -------------------------
1997 -- Analyze_Return_Type --
1998 -------------------------
2000 procedure Analyze_Return_Type (N : Node_Id) is
2001 Designator : constant Entity_Id := Defining_Entity (N);
2002 Typ : Entity_Id := Empty;
2005 -- Normal case where result definition does not indicate an error
2007 if Result_Definition (N) /= Error then
2008 if Nkind (Result_Definition (N)) = N_Access_Definition then
2009 Check_SPARK_05_Restriction
2010 ("access result is not allowed", Result_Definition (N));
2012 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2015 AD : constant Node_Id :=
2016 Access_To_Subprogram_Definition (Result_Definition (N));
2018 if Present (AD) and then Protected_Present (AD) then
2019 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2021 Typ := Access_Definition (N, Result_Definition (N));
2025 Set_Parent (Typ, Result_Definition (N));
2026 Set_Is_Local_Anonymous_Access (Typ);
2027 Set_Etype (Designator, Typ);
2029 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2031 Null_Exclusion_Static_Checks (N);
2033 -- Subtype_Mark case
2036 Find_Type (Result_Definition (N));
2037 Typ := Entity (Result_Definition (N));
2038 Set_Etype (Designator, Typ);
2040 -- Unconstrained array as result is not allowed in SPARK
2042 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2043 Check_SPARK_05_Restriction
2044 ("returning an unconstrained array is not allowed",
2045 Result_Definition (N));
2048 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2050 Null_Exclusion_Static_Checks (N);
2052 -- If a null exclusion is imposed on the result type, then create
2053 -- a null-excluding itype (an access subtype) and use it as the
2054 -- function's Etype. Note that the null exclusion checks are done
2055 -- right before this, because they don't get applied to types that
2056 -- do not come from source.
2058 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2059 Set_Etype (Designator,
2060 Create_Null_Excluding_Itype
2063 Scope_Id => Scope (Current_Scope)));
2065 -- The new subtype must be elaborated before use because
2066 -- it is visible outside of the function. However its base
2067 -- type may not be frozen yet, so the reference that will
2068 -- force elaboration must be attached to the freezing of
2071 -- If the return specification appears on a proper body,
2072 -- the subtype will have been created already on the spec.
2074 if Is_Frozen (Typ) then
2075 if Nkind (Parent (N)) = N_Subprogram_Body
2076 and then Nkind (Parent (Parent (N))) = N_Subunit
2080 Build_Itype_Reference (Etype (Designator), Parent (N));
2084 Ensure_Freeze_Node (Typ);
2087 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2089 Set_Itype (IR, Etype (Designator));
2090 Append_Freeze_Actions (Typ, New_List (IR));
2095 Set_Etype (Designator, Typ);
2098 if Ekind (Typ) = E_Incomplete_Type
2099 or else (Is_Class_Wide_Type (Typ)
2100 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2102 -- AI05-0151: Tagged incomplete types are allowed in all formal
2103 -- parts. Untagged incomplete types are not allowed in bodies.
2104 -- As a consequence, limited views cannot appear in a basic
2105 -- declaration that is itself within a body, because there is
2106 -- no point at which the non-limited view will become visible.
2108 if Ada_Version >= Ada_2012 then
2109 if From_Limited_With (Typ) and then In_Package_Body then
2111 ("invalid use of incomplete type&",
2112 Result_Definition (N), Typ);
2114 -- The return type of a subprogram body cannot be of a
2115 -- formal incomplete type.
2117 elsif Is_Generic_Type (Typ)
2118 and then Nkind (Parent (N)) = N_Subprogram_Body
2121 ("return type cannot be a formal incomplete type",
2122 Result_Definition (N));
2124 elsif Is_Class_Wide_Type (Typ)
2125 and then Is_Generic_Type (Root_Type (Typ))
2126 and then Nkind (Parent (N)) = N_Subprogram_Body
2129 ("return type cannot be a formal incomplete type",
2130 Result_Definition (N));
2132 elsif Is_Tagged_Type (Typ) then
2135 -- Use is legal in a thunk generated for an operation
2136 -- inherited from a progenitor.
2138 elsif Is_Thunk (Designator)
2139 and then Present (Non_Limited_View (Typ))
2143 elsif Nkind (Parent (N)) = N_Subprogram_Body
2144 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2148 ("invalid use of untagged incomplete type&",
2152 -- The type must be completed in the current package. This
2153 -- is checked at the end of the package declaration when
2154 -- Taft-amendment types are identified. If the return type
2155 -- is class-wide, there is no required check, the type can
2156 -- be a bona fide TAT.
2158 if Ekind (Scope (Current_Scope)) = E_Package
2159 and then In_Private_Part (Scope (Current_Scope))
2160 and then not Is_Class_Wide_Type (Typ)
2162 Append_Elmt (Designator, Private_Dependents (Typ));
2167 ("invalid use of incomplete type&", Designator, Typ);
2172 -- Case where result definition does indicate an error
2175 Set_Etype (Designator, Any_Type);
2177 end Analyze_Return_Type;
2179 -----------------------------
2180 -- Analyze_Subprogram_Body --
2181 -----------------------------
2183 procedure Analyze_Subprogram_Body (N : Node_Id) is
2184 Loc : constant Source_Ptr := Sloc (N);
2185 Body_Spec : constant Node_Id := Specification (N);
2186 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2189 if Debug_Flag_C then
2190 Write_Str ("==> subprogram body ");
2191 Write_Name (Chars (Body_Id));
2192 Write_Str (" from ");
2193 Write_Location (Loc);
2198 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2200 -- The real work is split out into the helper, so it can do "return;"
2201 -- without skipping the debug output:
2203 Analyze_Subprogram_Body_Helper (N);
2205 if Debug_Flag_C then
2207 Write_Str ("<== subprogram body ");
2208 Write_Name (Chars (Body_Id));
2209 Write_Str (" from ");
2210 Write_Location (Loc);
2213 end Analyze_Subprogram_Body;
2215 ------------------------------------
2216 -- Analyze_Subprogram_Body_Helper --
2217 ------------------------------------
2219 -- This procedure is called for regular subprogram bodies, generic bodies,
2220 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2221 -- specification matters, and is used to create a proper declaration for
2222 -- the subprogram, or to perform conformance checks.
2224 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2225 Loc : constant Source_Ptr := Sloc (N);
2226 Body_Spec : Node_Id := Specification (N);
2227 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2228 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2229 Conformant : Boolean;
2231 Prot_Typ : Entity_Id := Empty;
2232 Spec_Id : Entity_Id;
2233 Spec_Decl : Node_Id := Empty;
2235 Last_Real_Spec_Entity : Entity_Id := Empty;
2236 -- When we analyze a separate spec, the entity chain ends up containing
2237 -- the formals, as well as any itypes generated during analysis of the
2238 -- default expressions for parameters, or the arguments of associated
2239 -- precondition/postcondition pragmas (which are analyzed in the context
2240 -- of the spec since they have visibility on formals).
2242 -- These entities belong with the spec and not the body. However we do
2243 -- the analysis of the body in the context of the spec (again to obtain
2244 -- visibility to the formals), and all the entities generated during
2245 -- this analysis end up also chained to the entity chain of the spec.
2246 -- But they really belong to the body, and there is circuitry to move
2247 -- them from the spec to the body.
2249 -- However, when we do this move, we don't want to move the real spec
2250 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2251 -- variable points to the last real spec entity, so we only move those
2252 -- chained beyond that point. It is initialized to Empty to deal with
2253 -- the case where there is no separate spec.
2255 function Body_Has_Contract return Boolean;
2256 -- Check whether unanalyzed body has an aspect or pragma that may
2257 -- generate a SPARK contract.
2259 procedure Build_Subprogram_Declaration;
2260 -- Create a matching subprogram declaration for subprogram body N
2262 procedure Check_Anonymous_Return;
2263 -- Ada 2005: if a function returns an access type that denotes a task,
2264 -- or a type that contains tasks, we must create a master entity for
2265 -- the anonymous type, which typically will be used in an allocator
2266 -- in the body of the function.
2268 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2269 -- Look ahead to recognize a pragma that may appear after the body.
2270 -- If there is a previous spec, check that it appears in the same
2271 -- declarative part. If the pragma is Inline_Always, perform inlining
2272 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2273 -- If the body acts as a spec, and inlining is required, we create a
2274 -- subprogram declaration for it, in order to attach the body to inline.
2275 -- If pragma does not appear after the body, check whether there is
2276 -- an inline pragma before any local declarations.
2278 procedure Check_Missing_Return;
2279 -- Checks for a function with a no return statements, and also performs
2280 -- the warning checks implemented by Check_Returns. In formal mode, also
2281 -- verify that a function ends with a RETURN and that a procedure does
2282 -- not contain any RETURN.
2284 function Disambiguate_Spec return Entity_Id;
2285 -- When a primitive is declared between the private view and the full
2286 -- view of a concurrent type which implements an interface, a special
2287 -- mechanism is used to find the corresponding spec of the primitive
2290 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2291 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2292 -- incomplete types coming from a limited context and swap their limited
2293 -- views with the non-limited ones.
2295 function Is_Private_Concurrent_Primitive
2296 (Subp_Id : Entity_Id) return Boolean;
2297 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2298 -- type that implements an interface and has a private view.
2300 procedure Set_Trivial_Subprogram (N : Node_Id);
2301 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2302 -- subprogram whose body is being analyzed. N is the statement node
2303 -- causing the flag to be set, if the following statement is a return
2304 -- of an entity, we mark the entity as set in source to suppress any
2305 -- warning on the stylized use of function stubs with a dummy return.
2307 procedure Verify_Overriding_Indicator;
2308 -- If there was a previous spec, the entity has been entered in the
2309 -- current scope previously. If the body itself carries an overriding
2310 -- indicator, check that it is consistent with the known status of the
2313 -----------------------
2314 -- Body_Has_Contract --
2315 -----------------------
2317 function Body_Has_Contract return Boolean is
2318 Decls : constant List_Id := Declarations (N);
2322 -- Check for aspects that may generate a contract
2324 if Present (Aspect_Specifications (N)) then
2325 Item := First (Aspect_Specifications (N));
2326 while Present (Item) loop
2327 if Is_Subprogram_Contract_Annotation (Item) then
2335 -- Check for pragmas that may generate a contract
2337 if Present (Decls) then
2338 Item := First (Decls);
2339 while Present (Item) loop
2340 if Nkind (Item) = N_Pragma
2341 and then Is_Subprogram_Contract_Annotation (Item)
2351 end Body_Has_Contract;
2353 ----------------------------------
2354 -- Build_Subprogram_Declaration --
2355 ----------------------------------
2357 procedure Build_Subprogram_Declaration is
2360 Subp_Decl : Node_Id;
2363 -- Create a matching subprogram spec using the profile of the body.
2364 -- The structure of the tree is identical, but has new entities for
2365 -- the defining unit name and formal parameters.
2368 Make_Subprogram_Declaration (Loc,
2369 Specification => Copy_Subprogram_Spec (Body_Spec));
2370 Set_Comes_From_Source (Subp_Decl, True);
2372 -- Relocate the aspects of the subprogram body to the new subprogram
2373 -- spec because it acts as the initial declaration.
2374 -- ??? what about pragmas
2376 Move_Aspects (N, To => Subp_Decl);
2377 Insert_Before_And_Analyze (N, Subp_Decl);
2379 -- The analysis of the subprogram spec aspects may introduce pragmas
2380 -- that need to be analyzed.
2382 Decl := Next (Subp_Decl);
2383 while Present (Decl) loop
2385 -- Stop the search for pragmas once the body has been reached as
2386 -- this terminates the region where pragmas may appear.
2391 elsif Nkind (Decl) = N_Pragma then
2398 Spec_Id := Defining_Entity (Subp_Decl);
2399 Set_Corresponding_Spec (N, Spec_Id);
2401 -- Mark the generated spec as a source construct to ensure that all
2402 -- calls to it are properly registered in ALI files for GNATprove.
2404 Set_Comes_From_Source (Spec_Id, True);
2406 -- If aspect SPARK_Mode was specified on the body, it needs to be
2407 -- repeated both on the generated spec and the body.
2409 Asp := Find_Aspect (Spec_Id, Aspect_SPARK_Mode);
2411 if Present (Asp) then
2412 Asp := New_Copy_Tree (Asp);
2413 Set_Analyzed (Asp, False);
2414 Set_Aspect_Specifications (N, New_List (Asp));
2417 -- Ensure that the specs of the subprogram declaration and its body
2418 -- are identical, otherwise they will appear non-conformant due to
2419 -- rewritings in the default values of formal parameters.
2421 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2422 Set_Specification (N, Body_Spec);
2423 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2424 end Build_Subprogram_Declaration;
2426 ----------------------------
2427 -- Check_Anonymous_Return --
2428 ----------------------------
2430 procedure Check_Anonymous_Return is
2436 if Present (Spec_Id) then
2442 if Ekind (Scop) = E_Function
2443 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2444 and then not Is_Thunk (Scop)
2446 -- Skip internally built functions which handle the case of
2447 -- a null access (see Expand_Interface_Conversion)
2449 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2450 and then not Comes_From_Source (Parent (Scop)))
2452 and then (Has_Task (Designated_Type (Etype (Scop)))
2454 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2456 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2457 and then Expander_Active
2459 -- Avoid cases with no tasking support
2461 and then RTE_Available (RE_Current_Master)
2462 and then not Restriction_Active (No_Task_Hierarchy)
2465 Make_Object_Declaration (Loc,
2466 Defining_Identifier =>
2467 Make_Defining_Identifier (Loc, Name_uMaster),
2468 Constant_Present => True,
2469 Object_Definition =>
2470 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2472 Make_Explicit_Dereference (Loc,
2473 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2475 if Present (Declarations (N)) then
2476 Prepend (Decl, Declarations (N));
2478 Set_Declarations (N, New_List (Decl));
2481 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2482 Set_Has_Master_Entity (Scop);
2484 -- Now mark the containing scope as a task master
2487 while Nkind (Par) /= N_Compilation_Unit loop
2488 Par := Parent (Par);
2489 pragma Assert (Present (Par));
2491 -- If we fall off the top, we are at the outer level, and
2492 -- the environment task is our effective master, so nothing
2496 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2498 Set_Is_Task_Master (Par, True);
2503 end Check_Anonymous_Return;
2505 -------------------------
2506 -- Check_Inline_Pragma --
2507 -------------------------
2509 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2513 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2514 -- True when N is a pragma Inline or Inline_Always that applies
2515 -- to this subprogram.
2517 -----------------------
2518 -- Is_Inline_Pragma --
2519 -----------------------
2521 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2524 Nkind (N) = N_Pragma
2526 (Pragma_Name (N) = Name_Inline_Always
2527 or else (Front_End_Inlining
2528 and then Pragma_Name (N) = Name_Inline))
2531 (Expression (First (Pragma_Argument_Associations (N)))) =
2533 end Is_Inline_Pragma;
2535 -- Start of processing for Check_Inline_Pragma
2538 if not Expander_Active then
2542 if Is_List_Member (N)
2543 and then Present (Next (N))
2544 and then Is_Inline_Pragma (Next (N))
2548 elsif Nkind (N) /= N_Subprogram_Body_Stub
2549 and then Present (Declarations (N))
2550 and then Is_Inline_Pragma (First (Declarations (N)))
2552 Prag := First (Declarations (N));
2558 if Present (Prag) then
2559 if Present (Spec_Id) then
2560 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2565 -- Create a subprogram declaration, to make treatment uniform
2568 Subp : constant Entity_Id :=
2569 Make_Defining_Identifier (Loc, Chars (Body_Id));
2570 Decl : constant Node_Id :=
2571 Make_Subprogram_Declaration (Loc,
2573 New_Copy_Tree (Specification (N)));
2576 Set_Defining_Unit_Name (Specification (Decl), Subp);
2578 if Present (First_Formal (Body_Id)) then
2579 Plist := Copy_Parameter_List (Body_Id);
2580 Set_Parameter_Specifications
2581 (Specification (Decl), Plist);
2584 Insert_Before (N, Decl);
2587 Set_Has_Pragma_Inline (Subp);
2589 if Pragma_Name (Prag) = Name_Inline_Always then
2590 Set_Is_Inlined (Subp);
2591 Set_Has_Pragma_Inline_Always (Subp);
2594 -- Prior to copying the subprogram body to create a template
2595 -- for it for subsequent inlining, remove the pragma from
2596 -- the current body so that the copy that will produce the
2597 -- new body will start from a completely unanalyzed tree.
2599 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2600 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2607 end Check_Inline_Pragma;
2609 --------------------------
2610 -- Check_Missing_Return --
2611 --------------------------
2613 procedure Check_Missing_Return is
2615 Missing_Ret : Boolean;
2618 if Nkind (Body_Spec) = N_Function_Specification then
2619 if Present (Spec_Id) then
2625 if Return_Present (Id) then
2626 Check_Returns (HSS, 'F', Missing_Ret);
2629 Set_Has_Missing_Return (Id);
2632 -- Within a premature instantiation of a package with no body, we
2633 -- build completions of the functions therein, with a Raise
2634 -- statement. No point in complaining about a missing return in
2637 elsif Ekind (Id) = E_Function
2638 and then In_Instance
2639 and then Present (Statements (HSS))
2640 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2644 elsif Is_Generic_Subprogram (Id)
2645 or else not Is_Machine_Code_Subprogram (Id)
2647 Error_Msg_N ("missing RETURN statement in function body", N);
2650 -- If procedure with No_Return, check returns
2652 elsif Nkind (Body_Spec) = N_Procedure_Specification
2653 and then Present (Spec_Id)
2654 and then No_Return (Spec_Id)
2656 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2659 -- Special checks in SPARK mode
2661 if Nkind (Body_Spec) = N_Function_Specification then
2663 -- In SPARK mode, last statement of a function should be a return
2666 Stat : constant Node_Id := Last_Source_Statement (HSS);
2669 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2670 N_Extended_Return_Statement)
2672 Check_SPARK_05_Restriction
2673 ("last statement in function should be RETURN", Stat);
2677 -- In SPARK mode, verify that a procedure has no return
2679 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2680 if Present (Spec_Id) then
2686 -- Would be nice to point to return statement here, can we
2687 -- borrow the Check_Returns procedure here ???
2689 if Return_Present (Id) then
2690 Check_SPARK_05_Restriction
2691 ("procedure should not have RETURN", N);
2694 end Check_Missing_Return;
2696 -----------------------
2697 -- Disambiguate_Spec --
2698 -----------------------
2700 function Disambiguate_Spec return Entity_Id is
2701 Priv_Spec : Entity_Id;
2704 procedure Replace_Types (To_Corresponding : Boolean);
2705 -- Depending on the flag, replace the type of formal parameters of
2706 -- Body_Id if it is a concurrent type implementing interfaces with
2707 -- the corresponding record type or the other way around.
2709 procedure Replace_Types (To_Corresponding : Boolean) is
2711 Formal_Typ : Entity_Id;
2714 Formal := First_Formal (Body_Id);
2715 while Present (Formal) loop
2716 Formal_Typ := Etype (Formal);
2718 if Is_Class_Wide_Type (Formal_Typ) then
2719 Formal_Typ := Root_Type (Formal_Typ);
2722 -- From concurrent type to corresponding record
2724 if To_Corresponding then
2725 if Is_Concurrent_Type (Formal_Typ)
2726 and then Present (Corresponding_Record_Type (Formal_Typ))
2729 (Corresponding_Record_Type (Formal_Typ)))
2732 Corresponding_Record_Type (Formal_Typ));
2735 -- From corresponding record to concurrent type
2738 if Is_Concurrent_Record_Type (Formal_Typ)
2739 and then Present (Interfaces (Formal_Typ))
2742 Corresponding_Concurrent_Type (Formal_Typ));
2746 Next_Formal (Formal);
2750 -- Start of processing for Disambiguate_Spec
2753 -- Try to retrieve the specification of the body as is. All error
2754 -- messages are suppressed because the body may not have a spec in
2755 -- its current state.
2757 Spec_N := Find_Corresponding_Spec (N, False);
2759 -- It is possible that this is the body of a primitive declared
2760 -- between a private and a full view of a concurrent type. The
2761 -- controlling parameter of the spec carries the concurrent type,
2762 -- not the corresponding record type as transformed by Analyze_
2763 -- Subprogram_Specification. In such cases, we undo the change
2764 -- made by the analysis of the specification and try to find the
2767 -- Note that wrappers already have their corresponding specs and
2768 -- bodies set during their creation, so if the candidate spec is
2769 -- a wrapper, then we definitely need to swap all types to their
2770 -- original concurrent status.
2773 or else Is_Primitive_Wrapper (Spec_N)
2775 -- Restore all references of corresponding record types to the
2776 -- original concurrent types.
2778 Replace_Types (To_Corresponding => False);
2779 Priv_Spec := Find_Corresponding_Spec (N, False);
2781 -- The current body truly belongs to a primitive declared between
2782 -- a private and a full view. We leave the modified body as is,
2783 -- and return the true spec.
2785 if Present (Priv_Spec)
2786 and then Is_Private_Primitive (Priv_Spec)
2791 -- In case that this is some sort of error, restore the original
2792 -- state of the body.
2794 Replace_Types (To_Corresponding => True);
2798 end Disambiguate_Spec;
2800 ----------------------------
2801 -- Exchange_Limited_Views --
2802 ----------------------------
2804 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2805 procedure Detect_And_Exchange (Id : Entity_Id);
2806 -- Determine whether Id's type denotes an incomplete type associated
2807 -- with a limited with clause and exchange the limited view with the
2808 -- non-limited one when available.
2810 -------------------------
2811 -- Detect_And_Exchange --
2812 -------------------------
2814 procedure Detect_And_Exchange (Id : Entity_Id) is
2815 Typ : constant Entity_Id := Etype (Id);
2817 if From_Limited_With (Typ) and then Has_Non_Limited_View (Typ) then
2818 Set_Etype (Id, Non_Limited_View (Typ));
2820 end Detect_And_Exchange;
2826 -- Start of processing for Exchange_Limited_Views
2829 if No (Subp_Id) then
2832 -- Do not process subprogram bodies as they already use the non-
2833 -- limited view of types.
2835 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2839 -- Examine all formals and swap views when applicable
2841 Formal := First_Formal (Subp_Id);
2842 while Present (Formal) loop
2843 Detect_And_Exchange (Formal);
2845 Next_Formal (Formal);
2848 -- Process the return type of a function
2850 if Ekind (Subp_Id) = E_Function then
2851 Detect_And_Exchange (Subp_Id);
2853 end Exchange_Limited_Views;
2855 -------------------------------------
2856 -- Is_Private_Concurrent_Primitive --
2857 -------------------------------------
2859 function Is_Private_Concurrent_Primitive
2860 (Subp_Id : Entity_Id) return Boolean
2862 Formal_Typ : Entity_Id;
2865 if Present (First_Formal (Subp_Id)) then
2866 Formal_Typ := Etype (First_Formal (Subp_Id));
2868 if Is_Concurrent_Record_Type (Formal_Typ) then
2869 if Is_Class_Wide_Type (Formal_Typ) then
2870 Formal_Typ := Root_Type (Formal_Typ);
2873 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2876 -- The type of the first formal is a concurrent tagged type with
2880 Is_Concurrent_Type (Formal_Typ)
2881 and then Is_Tagged_Type (Formal_Typ)
2882 and then Has_Private_Declaration (Formal_Typ);
2886 end Is_Private_Concurrent_Primitive;
2888 ----------------------------
2889 -- Set_Trivial_Subprogram --
2890 ----------------------------
2892 procedure Set_Trivial_Subprogram (N : Node_Id) is
2893 Nxt : constant Node_Id := Next (N);
2896 Set_Is_Trivial_Subprogram (Body_Id);
2898 if Present (Spec_Id) then
2899 Set_Is_Trivial_Subprogram (Spec_Id);
2903 and then Nkind (Nxt) = N_Simple_Return_Statement
2904 and then No (Next (Nxt))
2905 and then Present (Expression (Nxt))
2906 and then Is_Entity_Name (Expression (Nxt))
2908 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2910 end Set_Trivial_Subprogram;
2912 ---------------------------------
2913 -- Verify_Overriding_Indicator --
2914 ---------------------------------
2916 procedure Verify_Overriding_Indicator is
2918 if Must_Override (Body_Spec) then
2919 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2920 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2924 elsif not Present (Overridden_Operation (Spec_Id)) then
2926 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2928 -- Overriding indicators aren't allowed for protected subprogram
2929 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2930 -- this to a warning if -gnatd.E is enabled.
2932 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2933 Error_Msg_Warn := Error_To_Warning;
2935 ("<<overriding indicator not allowed for protected "
2936 & "subprogram body", Body_Spec);
2939 elsif Must_Not_Override (Body_Spec) then
2940 if Present (Overridden_Operation (Spec_Id)) then
2942 ("subprogram& overrides inherited operation",
2943 Body_Spec, Spec_Id);
2945 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2946 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2949 ("subprogram& overrides predefined operator ",
2950 Body_Spec, Spec_Id);
2952 -- Overriding indicators aren't allowed for protected subprogram
2953 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2954 -- this to a warning if -gnatd.E is enabled.
2956 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2957 Error_Msg_Warn := Error_To_Warning;
2960 ("<<overriding indicator not allowed "
2961 & "for protected subprogram body", Body_Spec);
2963 -- If this is not a primitive operation, then the overriding
2964 -- indicator is altogether illegal.
2966 elsif not Is_Primitive (Spec_Id) then
2968 ("overriding indicator only allowed "
2969 & "if subprogram is primitive", Body_Spec);
2972 -- If checking the style rule and the operation overrides, then
2973 -- issue a warning about a missing overriding_indicator. Protected
2974 -- subprogram bodies are excluded from this style checking, since
2975 -- they aren't primitives (even though their declarations can
2976 -- override) and aren't allowed to have an overriding_indicator.
2979 and then Present (Overridden_Operation (Spec_Id))
2980 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2982 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2983 Style.Missing_Overriding (N, Body_Id);
2986 and then Can_Override_Operator (Spec_Id)
2987 and then not Is_Predefined_File_Name
2988 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2990 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2991 Style.Missing_Overriding (N, Body_Id);
2993 end Verify_Overriding_Indicator;
2997 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
2999 -- Start of processing for Analyze_Subprogram_Body_Helper
3002 -- A [generic] subprogram body "freezes" the contract of the nearest
3003 -- enclosing package body:
3005 -- package body Nearest_Enclosing_Package
3006 -- with Refined_State => (State => Constit)
3010 -- procedure Freezes_Enclosing_Package_Body
3011 -- with Refined_Depends => (Input => Constit) ...
3013 -- This ensures that any annotations referenced by the contract of the
3014 -- [generic] subprogram body are available. This form of "freezing" is
3015 -- decoupled from the usual Freeze_xxx mechanism because it must also
3016 -- work in the context of generics where normal freezing is disabled.
3018 -- Only bodies coming from source should cause this type of "freezing".
3019 -- Expression functions that act as bodies and complete an initial
3020 -- declaration must be included in this category, hence the use of
3023 if Comes_From_Source (Original_Node (N)) then
3024 Analyze_Enclosing_Package_Body_Contract (N);
3027 -- Generic subprograms are handled separately. They always have a
3028 -- generic specification. Determine whether current scope has a
3029 -- previous declaration.
3031 -- If the subprogram body is defined within an instance of the same
3032 -- name, the instance appears as a package renaming, and will be hidden
3033 -- within the subprogram.
3035 if Present (Prev_Id)
3036 and then not Is_Overloadable (Prev_Id)
3037 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3038 or else Comes_From_Source (Prev_Id))
3040 if Is_Generic_Subprogram (Prev_Id) then
3043 -- A subprogram body is Ghost when it is stand alone and subject
3044 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3045 -- the mode now to ensure that any nodes generated during analysis
3046 -- and expansion are properly marked as Ghost.
3048 Set_Ghost_Mode (N, Spec_Id);
3049 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3050 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3052 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3054 if Nkind (N) = N_Subprogram_Body then
3055 HSS := Handled_Statement_Sequence (N);
3056 Check_Missing_Return;
3059 Ghost_Mode := Save_Ghost_Mode;
3063 -- Previous entity conflicts with subprogram name. Attempting to
3064 -- enter name will post error.
3066 Enter_Name (Body_Id);
3067 Ghost_Mode := Save_Ghost_Mode;
3071 -- Non-generic case, find the subprogram declaration, if one was seen,
3072 -- or enter new overloaded entity in the current scope. If the
3073 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3074 -- part of the context of one of its subunits. No need to redo the
3077 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3078 Ghost_Mode := Save_Ghost_Mode;
3082 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3084 if Nkind (N) = N_Subprogram_Body_Stub
3085 or else No (Corresponding_Spec (N))
3087 if Is_Private_Concurrent_Primitive (Body_Id) then
3088 Spec_Id := Disambiguate_Spec;
3090 -- A subprogram body is Ghost when it is stand alone and
3091 -- subject to pragma Ghost or when the corresponding spec is
3092 -- Ghost. Set the mode now to ensure that any nodes generated
3093 -- during analysis and expansion are properly marked as Ghost.
3095 Set_Ghost_Mode (N, Spec_Id);
3098 Spec_Id := Find_Corresponding_Spec (N);
3100 -- A subprogram body is Ghost when it is stand alone and
3101 -- subject to pragma Ghost or when the corresponding spec is
3102 -- Ghost. Set the mode now to ensure that any nodes generated
3103 -- during analysis and expansion are properly marked as Ghost.
3105 Set_Ghost_Mode (N, Spec_Id);
3107 -- In GNATprove mode, if the body has no previous spec, create
3108 -- one so that the inlining machinery can operate properly.
3109 -- Transfer aspects, if any, to the new spec, so that they
3110 -- are legal and can be processed ahead of the body.
3111 -- We make two copies of the given spec, one for the new
3112 -- declaration, and one for the body.
3114 if No (Spec_Id) and then GNATprove_Mode
3116 -- Inlining does not apply during pre-analysis of code
3118 and then Full_Analysis
3120 -- Inlining only applies to full bodies, not stubs
3122 and then Nkind (N) /= N_Subprogram_Body_Stub
3124 -- Inlining only applies to bodies in the source code, not to
3125 -- those generated by the compiler. In particular, expression
3126 -- functions, whose body is generated by the compiler, are
3127 -- treated specially by GNATprove.
3129 and then Comes_From_Source (Body_Id)
3131 -- This cannot be done for a compilation unit, which is not
3132 -- in a context where we can insert a new spec.
3134 and then Is_List_Member (N)
3136 -- Inlining only applies to subprograms without contracts,
3137 -- as a contract is a sign that GNATprove should perform a
3138 -- modular analysis of the subprogram instead of a contextual
3139 -- analysis at each call site. The same test is performed in
3140 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3141 -- here in another form (because the contract has not
3142 -- been attached to the body) to avoid frontend errors in
3143 -- case pragmas are used instead of aspects, because the
3144 -- corresponding pragmas in the body would not be transferred
3145 -- to the spec, leading to legality errors.
3147 and then not Body_Has_Contract
3148 and then not Inside_A_Generic
3150 Build_Subprogram_Declaration;
3152 -- If this is a function that returns a constrained array, and
3153 -- we are generating SPARK_For_C, create subprogram declaration
3154 -- to simplify subsequent C generation.
3157 and then Modify_Tree_For_C
3158 and then Nkind (Body_Spec) = N_Function_Specification
3159 and then Is_Array_Type (Etype (Body_Id))
3160 and then Is_Constrained (Etype (Body_Id))
3162 Build_Subprogram_Declaration;
3166 -- If this is a duplicate body, no point in analyzing it
3168 if Error_Posted (N) then
3169 Ghost_Mode := Save_Ghost_Mode;
3173 -- A subprogram body should cause freezing of its own declaration,
3174 -- but if there was no previous explicit declaration, then the
3175 -- subprogram will get frozen too late (there may be code within
3176 -- the body that depends on the subprogram having been frozen,
3177 -- such as uses of extra formals), so we force it to be frozen
3178 -- here. Same holds if the body and spec are compilation units.
3179 -- Finally, if the return type is an anonymous access to protected
3180 -- subprogram, it must be frozen before the body because its
3181 -- expansion has generated an equivalent type that is used when
3182 -- elaborating the body.
3184 -- An exception in the case of Ada 2012, AI05-177: The bodies
3185 -- created for expression functions do not freeze.
3188 and then Nkind (Original_Node (N)) /= N_Expression_Function
3190 Freeze_Before (N, Body_Id);
3192 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3193 Freeze_Before (N, Spec_Id);
3195 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3196 Freeze_Before (N, Etype (Body_Id));
3200 Spec_Id := Corresponding_Spec (N);
3202 -- A subprogram body is Ghost when it is stand alone and subject
3203 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3204 -- the mode now to ensure that any nodes generated during analysis
3205 -- and expansion are properly marked as Ghost.
3207 Set_Ghost_Mode (N, Spec_Id);
3211 -- Previously we scanned the body to look for nested subprograms, and
3212 -- rejected an inline directive if nested subprograms were present,
3213 -- because the back-end would generate conflicting symbols for the
3214 -- nested bodies. This is now unnecessary.
3216 -- Look ahead to recognize a pragma Inline that appears after the body
3218 Check_Inline_Pragma (Spec_Id);
3220 -- Deal with special case of a fully private operation in the body of
3221 -- the protected type. We must create a declaration for the subprogram,
3222 -- in order to attach the protected subprogram that will be used in
3223 -- internal calls. We exclude compiler generated bodies from the
3224 -- expander since the issue does not arise for those cases.
3227 and then Comes_From_Source (N)
3228 and then Is_Protected_Type (Current_Scope)
3230 Spec_Id := Build_Private_Protected_Declaration (N);
3233 -- If a separate spec is present, then deal with freezing issues
3235 if Present (Spec_Id) then
3236 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3237 Verify_Overriding_Indicator;
3239 -- In general, the spec will be frozen when we start analyzing the
3240 -- body. However, for internally generated operations, such as
3241 -- wrapper functions for inherited operations with controlling
3242 -- results, the spec may not have been frozen by the time we expand
3243 -- the freeze actions that include the bodies. In particular, extra
3244 -- formals for accessibility or for return-in-place may need to be
3245 -- generated. Freeze nodes, if any, are inserted before the current
3246 -- body. These freeze actions are also needed in ASIS mode and in
3247 -- Compile_Only mode to enable the proper back-end type annotations.
3248 -- They are necessary in any case to insure order of elaboration
3251 if not Is_Frozen (Spec_Id)
3252 and then (Expander_Active
3254 or else (Operating_Mode = Check_Semantics
3255 and then Serious_Errors_Detected = 0))
3257 Set_Has_Delayed_Freeze (Spec_Id);
3258 Freeze_Before (N, Spec_Id);
3262 -- Place subprogram on scope stack, and make formals visible. If there
3263 -- is a spec, the visible entity remains that of the spec.
3265 if Present (Spec_Id) then
3266 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3268 if Is_Child_Unit (Spec_Id) then
3269 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3273 Style.Check_Identifier (Body_Id, Spec_Id);
3276 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3277 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3279 if Is_Abstract_Subprogram (Spec_Id) then
3280 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3281 Ghost_Mode := Save_Ghost_Mode;
3285 Set_Convention (Body_Id, Convention (Spec_Id));
3286 Set_Has_Completion (Spec_Id);
3288 -- Inherit the "ghostness" of the subprogram spec. Note that this
3289 -- property is not directly inherited as the body may be subject
3290 -- to a different Ghost assertion policy.
3292 if Ghost_Mode > None or else Is_Ghost_Entity (Spec_Id) then
3293 Set_Is_Ghost_Entity (Body_Id);
3295 -- The Ghost policy in effect at the point of declaration and
3296 -- at the point of completion must match (SPARK RM 6.9(14)).
3298 Check_Ghost_Completion (Spec_Id, Body_Id);
3301 if Is_Protected_Type (Scope (Spec_Id)) then
3302 Prot_Typ := Scope (Spec_Id);
3305 -- If this is a body generated for a renaming, do not check for
3306 -- full conformance. The check is redundant, because the spec of
3307 -- the body is a copy of the spec in the renaming declaration,
3308 -- and the test can lead to spurious errors on nested defaults.
3310 if Present (Spec_Decl)
3311 and then not Comes_From_Source (N)
3313 (Nkind (Original_Node (Spec_Decl)) =
3314 N_Subprogram_Renaming_Declaration
3315 or else (Present (Corresponding_Body (Spec_Decl))
3317 Nkind (Unit_Declaration_Node
3318 (Corresponding_Body (Spec_Decl))) =
3319 N_Subprogram_Renaming_Declaration))
3323 -- Conversely, the spec may have been generated for specless body
3324 -- with an inline pragma.
3326 elsif Comes_From_Source (N)
3327 and then not Comes_From_Source (Spec_Id)
3328 and then Has_Pragma_Inline (Spec_Id)
3335 Fully_Conformant, True, Conformant, Body_Id);
3338 -- If the body is not fully conformant, we have to decide if we
3339 -- should analyze it or not. If it has a really messed up profile
3340 -- then we probably should not analyze it, since we will get too
3341 -- many bogus messages.
3343 -- Our decision is to go ahead in the non-fully conformant case
3344 -- only if it is at least mode conformant with the spec. Note
3345 -- that the call to Check_Fully_Conformant has issued the proper
3346 -- error messages to complain about the lack of conformance.
3349 and then not Mode_Conformant (Body_Id, Spec_Id)
3351 Ghost_Mode := Save_Ghost_Mode;
3356 if Spec_Id /= Body_Id then
3357 Reference_Body_Formals (Spec_Id, Body_Id);
3360 Set_Ekind (Body_Id, E_Subprogram_Body);
3362 if Nkind (N) = N_Subprogram_Body_Stub then
3363 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3368 Set_Corresponding_Spec (N, Spec_Id);
3370 -- Ada 2005 (AI-345): If the operation is a primitive operation
3371 -- of a concurrent type, the type of the first parameter has been
3372 -- replaced with the corresponding record, which is the proper
3373 -- run-time structure to use. However, within the body there may
3374 -- be uses of the formals that depend on primitive operations
3375 -- of the type (in particular calls in prefixed form) for which
3376 -- we need the original concurrent type. The operation may have
3377 -- several controlling formals, so the replacement must be done
3380 if Comes_From_Source (Spec_Id)
3381 and then Present (First_Entity (Spec_Id))
3382 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3383 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3384 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3385 and then Present (Corresponding_Concurrent_Type
3386 (Etype (First_Entity (Spec_Id))))
3389 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3393 Form := First_Formal (Spec_Id);
3394 while Present (Form) loop
3395 if Etype (Form) = Typ then
3396 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3404 -- Make the formals visible, and place subprogram on scope stack.
3405 -- This is also the point at which we set Last_Real_Spec_Entity
3406 -- to mark the entities which will not be moved to the body.
3408 Install_Formals (Spec_Id);
3409 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3411 -- Within an instance, add local renaming declarations so that
3412 -- gdb can retrieve the values of actuals more easily. This is
3413 -- only relevant if generating code (and indeed we definitely
3414 -- do not want these definitions -gnatc mode, because that would
3417 if Is_Generic_Instance (Spec_Id)
3418 and then Is_Wrapper_Package (Current_Scope)
3419 and then Expander_Active
3421 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3424 Push_Scope (Spec_Id);
3426 -- Make sure that the subprogram is immediately visible. For
3427 -- child units that have no separate spec this is indispensable.
3428 -- Otherwise it is safe albeit redundant.
3430 Set_Is_Immediately_Visible (Spec_Id);
3433 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3434 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3435 Set_Scope (Body_Id, Scope (Spec_Id));
3437 -- Case of subprogram body with no previous spec
3440 -- Check for style warning required
3444 -- Only apply check for source level subprograms for which checks
3445 -- have not been suppressed.
3447 and then Comes_From_Source (Body_Id)
3448 and then not Suppress_Style_Checks (Body_Id)
3450 -- No warnings within an instance
3452 and then not In_Instance
3454 -- No warnings for expression functions
3456 and then Nkind (Original_Node (N)) /= N_Expression_Function
3458 Style.Body_With_No_Spec (N);
3461 New_Overloaded_Entity (Body_Id);
3463 -- A subprogram body declared within a Ghost region is automatically
3464 -- Ghost (SPARK RM 6.9(2)).
3466 if Ghost_Mode > None then
3467 Set_Is_Ghost_Entity (Body_Id);
3470 if Nkind (N) /= N_Subprogram_Body_Stub then
3471 Set_Acts_As_Spec (N);
3472 Generate_Definition (Body_Id);
3474 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3475 Install_Formals (Body_Id);
3477 Push_Scope (Body_Id);
3480 -- For stubs and bodies with no previous spec, generate references to
3483 Generate_Reference_To_Formals (Body_Id);
3486 -- Set the SPARK_Mode from the current context (may be overwritten later
3487 -- with explicit pragma). This is not done for entry barrier functions
3488 -- because they are generated outside the protected type and should not
3489 -- carry the mode of the enclosing context.
3491 if Nkind (N) = N_Subprogram_Body
3492 and then Is_Entry_Barrier_Function (N)
3496 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3497 Set_SPARK_Pragma_Inherited (Body_Id);
3500 -- If the return type is an anonymous access type whose designated type
3501 -- is the limited view of a class-wide type and the non-limited view is
3502 -- available, update the return type accordingly.
3504 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3510 Rtyp := Etype (Current_Scope);
3512 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3513 Etyp := Directly_Designated_Type (Rtyp);
3515 if Is_Class_Wide_Type (Etyp)
3516 and then From_Limited_With (Etyp)
3518 Set_Directly_Designated_Type
3519 (Etype (Current_Scope), Available_View (Etyp));
3525 -- If this is the proper body of a stub, we must verify that the stub
3526 -- conforms to the body, and to the previous spec if one was present.
3527 -- We know already that the body conforms to that spec. This test is
3528 -- only required for subprograms that come from source.
3530 if Nkind (Parent (N)) = N_Subunit
3531 and then Comes_From_Source (N)
3532 and then not Error_Posted (Body_Id)
3533 and then Nkind (Corresponding_Stub (Parent (N))) =
3534 N_Subprogram_Body_Stub
3537 Old_Id : constant Entity_Id :=
3539 (Specification (Corresponding_Stub (Parent (N))));
3541 Conformant : Boolean := False;
3544 if No (Spec_Id) then
3545 Check_Fully_Conformant (Body_Id, Old_Id);
3549 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3551 if not Conformant then
3553 -- The stub was taken to be a new declaration. Indicate that
3556 Set_Has_Completion (Old_Id, False);
3562 Set_Has_Completion (Body_Id);
3563 Check_Eliminated (Body_Id);
3565 -- Analyze any aspect specifications that appear on the subprogram body
3566 -- stub. Stop the analysis now as the stub does not have a declarative
3567 -- or a statement part, and it cannot be inlined.
3569 if Nkind (N) = N_Subprogram_Body_Stub then
3570 if Has_Aspects (N) then
3571 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3574 Ghost_Mode := Save_Ghost_Mode;
3578 -- Handle frontend inlining
3580 -- Note: Normally we don't do any inlining if expansion is off, since
3581 -- we won't generate code in any case. An exception arises in GNATprove
3582 -- mode where we want to expand some calls in place, even with expansion
3583 -- disabled, since the inlining eases formal verification.
3585 if not GNATprove_Mode
3586 and then Expander_Active
3587 and then Serious_Errors_Detected = 0
3588 and then Present (Spec_Id)
3589 and then Has_Pragma_Inline (Spec_Id)
3591 -- Legacy implementation (relying on frontend inlining)
3593 if not Back_End_Inlining then
3594 if (Has_Pragma_Inline_Always (Spec_Id)
3595 and then not Opt.Disable_FE_Inline_Always)
3597 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3598 and then not Opt.Disable_FE_Inline)
3600 Build_Body_To_Inline (N, Spec_Id);
3603 -- New implementation (relying on backend inlining)
3606 if Has_Pragma_Inline_Always (Spec_Id)
3607 or else Optimization_Level > 0
3609 -- Handle function returning an unconstrained type
3611 if Comes_From_Source (Body_Id)
3612 and then Ekind (Spec_Id) = E_Function
3613 and then Returns_Unconstrained_Type (Spec_Id)
3615 -- If function builds in place, i.e. returns a limited type,
3616 -- inlining cannot be done.
3618 and then not Is_Limited_Type (Etype (Spec_Id))
3620 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3624 Subp_Body : constant Node_Id :=
3625 Unit_Declaration_Node (Body_Id);
3626 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3629 -- Do not pass inlining to the backend if the subprogram
3630 -- has declarations or statements which cannot be inlined
3631 -- by the backend. This check is done here to emit an
3632 -- error instead of the generic warning message reported
3633 -- by the GCC backend (ie. "function might not be
3636 if Present (Subp_Decl)
3637 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3641 elsif Has_Excluded_Statement
3644 (Handled_Statement_Sequence (Subp_Body)))
3648 -- If the backend inlining is available then at this
3649 -- stage we only have to mark the subprogram as inlined.
3650 -- The expander will take care of registering it in the
3651 -- table of subprograms inlined by the backend a part of
3652 -- processing calls to it (cf. Expand_Call)
3655 Set_Is_Inlined (Spec_Id);
3662 -- In GNATprove mode, inline only when there is a separate subprogram
3663 -- declaration for now, as inlining of subprogram bodies acting as
3664 -- declarations, or subprogram stubs, are not supported by frontend
3665 -- inlining. This inlining should occur after analysis of the body, so
3666 -- that it is known whether the value of SPARK_Mode, which can be
3667 -- defined by a pragma inside the body, is applicable to the body.
3669 elsif GNATprove_Mode
3670 and then Full_Analysis
3671 and then not Inside_A_Generic
3672 and then Present (Spec_Id)
3674 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
3675 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3676 and then not Body_Has_Contract
3678 Build_Body_To_Inline (N, Spec_Id);
3681 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3682 -- of the specification we have to install the private withed units.
3683 -- This holds for child units as well.
3685 if Is_Compilation_Unit (Body_Id)
3686 or else Nkind (Parent (N)) = N_Compilation_Unit
3688 Install_Private_With_Clauses (Body_Id);
3691 Check_Anonymous_Return;
3693 -- Set the Protected_Formal field of each extra formal of the protected
3694 -- subprogram to reference the corresponding extra formal of the
3695 -- subprogram that implements it. For regular formals this occurs when
3696 -- the protected subprogram's declaration is expanded, but the extra
3697 -- formals don't get created until the subprogram is frozen. We need to
3698 -- do this before analyzing the protected subprogram's body so that any
3699 -- references to the original subprogram's extra formals will be changed
3700 -- refer to the implementing subprogram's formals (see Expand_Formal).
3702 if Present (Spec_Id)
3703 and then Is_Protected_Type (Scope (Spec_Id))
3704 and then Present (Protected_Body_Subprogram (Spec_Id))
3707 Impl_Subp : constant Entity_Id :=
3708 Protected_Body_Subprogram (Spec_Id);
3709 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3710 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3712 while Present (Prot_Ext_Formal) loop
3713 pragma Assert (Present (Impl_Ext_Formal));
3714 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3715 Next_Formal_With_Extras (Prot_Ext_Formal);
3716 Next_Formal_With_Extras (Impl_Ext_Formal);
3721 -- Now we can go on to analyze the body
3723 HSS := Handled_Statement_Sequence (N);
3724 Set_Actual_Subtypes (N, Current_Scope);
3726 -- Add a declaration for the Protection object, renaming declarations
3727 -- for discriminals and privals and finally a declaration for the entry
3728 -- family index (if applicable). This form of early expansion is done
3729 -- when the Expander is active because Install_Private_Data_Declarations
3730 -- references entities which were created during regular expansion. The
3731 -- subprogram entity must come from source, and not be an internally
3732 -- generated subprogram.
3735 and then Present (Prot_Typ)
3736 and then Present (Spec_Id)
3737 and then Comes_From_Source (Spec_Id)
3738 and then not Is_Eliminated (Spec_Id)
3740 Install_Private_Data_Declarations
3741 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3744 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3745 -- may now appear in parameter and result profiles. Since the analysis
3746 -- of a subprogram body may use the parameter and result profile of the
3747 -- spec, swap any limited views with their non-limited counterpart.
3749 if Ada_Version >= Ada_2012 then
3750 Exchange_Limited_Views (Spec_Id);
3753 -- Analyze any aspect specifications that appear on the subprogram body
3755 if Has_Aspects (N) then
3756 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3759 Analyze_Declarations (Declarations (N));
3761 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3763 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3764 if Present (SPARK_Pragma (Spec_Id)) then
3765 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3767 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3769 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3770 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3771 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3773 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3776 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3780 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3781 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3782 Error_Msg_Sloc := Sloc (Spec_Id);
3784 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3788 -- A subprogram body "freezes" its own contract. Analyze the contract
3789 -- after the declarations of the body have been processed as pragmas
3790 -- are now chained on the contract of the subprogram body.
3792 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
3794 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3795 -- subprogram in GNATprove mode, as its body should not be analyzed.
3798 and then GNATprove_Mode
3799 and then Present (Spec_Id)
3800 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3802 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3803 Set_Is_Inlined_Always (Spec_Id, False);
3806 -- Check completion, and analyze the statements
3809 Inspect_Deferred_Constant_Completion (Declarations (N));
3812 -- Deal with end of scope processing for the body
3814 Process_End_Label (HSS, 't', Current_Scope);
3816 Check_Subprogram_Order (N);
3817 Set_Analyzed (Body_Id);
3819 -- If we have a separate spec, then the analysis of the declarations
3820 -- caused the entities in the body to be chained to the spec id, but
3821 -- we want them chained to the body id. Only the formal parameters
3822 -- end up chained to the spec id in this case.
3824 if Present (Spec_Id) then
3826 -- We must conform to the categorization of our spec
3828 Validate_Categorization_Dependency (N, Spec_Id);
3830 -- And if this is a child unit, the parent units must conform
3832 if Is_Child_Unit (Spec_Id) then
3833 Validate_Categorization_Dependency
3834 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3837 -- Here is where we move entities from the spec to the body
3839 -- Case where there are entities that stay with the spec
3841 if Present (Last_Real_Spec_Entity) then
3843 -- No body entities (happens when the only real spec entities come
3844 -- from precondition and postcondition pragmas).
3846 if No (Last_Entity (Body_Id)) then
3847 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3849 -- Body entities present (formals), so chain stuff past them
3853 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3856 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3857 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3858 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3860 -- Case where there are no spec entities, in this case there can be
3861 -- no body entities either, so just move everything.
3863 -- If the body is generated for an expression function, it may have
3864 -- been preanalyzed already, if 'access was applied to it.
3867 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3868 N_Expression_Function
3870 pragma Assert (No (Last_Entity (Body_Id)));
3874 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3875 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3876 Set_First_Entity (Spec_Id, Empty);
3877 Set_Last_Entity (Spec_Id, Empty);
3881 Check_Missing_Return;
3883 -- Now we are going to check for variables that are never modified in
3884 -- the body of the procedure. But first we deal with a special case
3885 -- where we want to modify this check. If the body of the subprogram
3886 -- starts with a raise statement or its equivalent, or if the body
3887 -- consists entirely of a null statement, then it is pretty obvious that
3888 -- it is OK to not reference the parameters. For example, this might be
3889 -- the following common idiom for a stubbed function: statement of the
3890 -- procedure raises an exception. In particular this deals with the
3891 -- common idiom of a stubbed function, which appears something like:
3893 -- function F (A : Integer) return Some_Type;
3896 -- raise Program_Error;
3900 -- Here the purpose of X is simply to satisfy the annoying requirement
3901 -- in Ada that there be at least one return, and we certainly do not
3902 -- want to go posting warnings on X that it is not initialized. On
3903 -- the other hand, if X is entirely unreferenced that should still
3906 -- What we do is to detect these cases, and if we find them, flag the
3907 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3908 -- suppress unwanted warnings. For the case of the function stub above
3909 -- we have a special test to set X as apparently assigned to suppress
3916 -- Skip initial labels (for one thing this occurs when we are in
3917 -- front end ZCX mode, but in any case it is irrelevant), and also
3918 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3920 Stm := First (Statements (HSS));
3921 while Nkind (Stm) = N_Label
3922 or else Nkind (Stm) in N_Push_xxx_Label
3927 -- Do the test on the original statement before expansion
3930 Ostm : constant Node_Id := Original_Node (Stm);
3933 -- If explicit raise statement, turn on flag
3935 if Nkind (Ostm) = N_Raise_Statement then
3936 Set_Trivial_Subprogram (Stm);
3938 -- If null statement, and no following statements, turn on flag
3940 elsif Nkind (Stm) = N_Null_Statement
3941 and then Comes_From_Source (Stm)
3942 and then No (Next (Stm))
3944 Set_Trivial_Subprogram (Stm);
3946 -- Check for explicit call cases which likely raise an exception
3948 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3949 if Is_Entity_Name (Name (Ostm)) then
3951 Ent : constant Entity_Id := Entity (Name (Ostm));
3954 -- If the procedure is marked No_Return, then likely it
3955 -- raises an exception, but in any case it is not coming
3956 -- back here, so turn on the flag.
3959 and then Ekind (Ent) = E_Procedure
3960 and then No_Return (Ent)
3962 Set_Trivial_Subprogram (Stm);
3970 -- Check for variables that are never modified
3976 -- If there is a separate spec, then transfer Never_Set_In_Source
3977 -- flags from out parameters to the corresponding entities in the
3978 -- body. The reason we do that is we want to post error flags on
3979 -- the body entities, not the spec entities.
3981 if Present (Spec_Id) then
3982 E1 := First_Entity (Spec_Id);
3983 while Present (E1) loop
3984 if Ekind (E1) = E_Out_Parameter then
3985 E2 := First_Entity (Body_Id);
3986 while Present (E2) loop
3987 exit when Chars (E1) = Chars (E2);
3991 if Present (E2) then
3992 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4000 -- Check references in body
4002 Check_References (Body_Id);
4005 -- Check for nested subprogram, and mark outer level subprogram if so
4011 if Present (Spec_Id) then
4018 Ent := Enclosing_Subprogram (Ent);
4019 exit when No (Ent) or else Is_Subprogram (Ent);
4022 if Present (Ent) then
4023 Set_Has_Nested_Subprogram (Ent);
4027 Ghost_Mode := Save_Ghost_Mode;
4028 end Analyze_Subprogram_Body_Helper;
4030 ------------------------------------
4031 -- Analyze_Subprogram_Declaration --
4032 ------------------------------------
4034 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4035 Scop : constant Entity_Id := Current_Scope;
4036 Designator : Entity_Id;
4038 Is_Completion : Boolean;
4039 -- Indicates whether a null procedure declaration is a completion
4042 -- Null procedures are not allowed in SPARK
4044 if Nkind (Specification (N)) = N_Procedure_Specification
4045 and then Null_Present (Specification (N))
4047 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4049 -- Null procedures are allowed in protected types, following the
4050 -- recent AI12-0147.
4052 if Is_Protected_Type (Current_Scope)
4053 and then Ada_Version < Ada_2012
4055 Error_Msg_N ("protected operation cannot be a null procedure", N);
4058 Analyze_Null_Procedure (N, Is_Completion);
4060 -- The null procedure acts as a body, nothing further is needed
4062 if Is_Completion then
4067 Designator := Analyze_Subprogram_Specification (Specification (N));
4069 -- A reference may already have been generated for the unit name, in
4070 -- which case the following call is redundant. However it is needed for
4071 -- declarations that are the rewriting of an expression function.
4073 Generate_Definition (Designator);
4075 -- Set the SPARK mode from the current context (may be overwritten later
4076 -- with explicit pragma). This is not done for entry barrier functions
4077 -- because they are generated outside the protected type and should not
4078 -- carry the mode of the enclosing context.
4080 if Nkind (N) = N_Subprogram_Declaration
4081 and then Is_Entry_Barrier_Function (N)
4085 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4086 Set_SPARK_Pragma_Inherited (Designator);
4089 -- A subprogram declared within a Ghost region is automatically Ghost
4090 -- (SPARK RM 6.9(2)).
4092 if Ghost_Mode > None then
4093 Set_Is_Ghost_Entity (Designator);
4096 if Debug_Flag_C then
4097 Write_Str ("==> subprogram spec ");
4098 Write_Name (Chars (Designator));
4099 Write_Str (" from ");
4100 Write_Location (Sloc (N));
4105 Validate_RCI_Subprogram_Declaration (N);
4106 New_Overloaded_Entity (Designator);
4107 Check_Delayed_Subprogram (Designator);
4109 -- If the type of the first formal of the current subprogram is a non-
4110 -- generic tagged private type, mark the subprogram as being a private
4111 -- primitive. Ditto if this is a function with controlling result, and
4112 -- the return type is currently private. In both cases, the type of the
4113 -- controlling argument or result must be in the current scope for the
4114 -- operation to be primitive.
4116 if Has_Controlling_Result (Designator)
4117 and then Is_Private_Type (Etype (Designator))
4118 and then Scope (Etype (Designator)) = Current_Scope
4119 and then not Is_Generic_Actual_Type (Etype (Designator))
4121 Set_Is_Private_Primitive (Designator);
4123 elsif Present (First_Formal (Designator)) then
4125 Formal_Typ : constant Entity_Id :=
4126 Etype (First_Formal (Designator));
4128 Set_Is_Private_Primitive (Designator,
4129 Is_Tagged_Type (Formal_Typ)
4130 and then Scope (Formal_Typ) = Current_Scope
4131 and then Is_Private_Type (Formal_Typ)
4132 and then not Is_Generic_Actual_Type (Formal_Typ));
4136 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4139 if Ada_Version >= Ada_2005
4140 and then Comes_From_Source (N)
4141 and then Is_Dispatching_Operation (Designator)
4148 if Has_Controlling_Result (Designator) then
4149 Etyp := Etype (Designator);
4152 E := First_Entity (Designator);
4154 and then Is_Formal (E)
4155 and then not Is_Controlling_Formal (E)
4163 if Is_Access_Type (Etyp) then
4164 Etyp := Directly_Designated_Type (Etyp);
4167 if Is_Interface (Etyp)
4168 and then not Is_Abstract_Subprogram (Designator)
4169 and then not (Ekind (Designator) = E_Procedure
4170 and then Null_Present (Specification (N)))
4172 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4174 -- Specialize error message based on procedures vs. functions,
4175 -- since functions can't be null subprograms.
4177 if Ekind (Designator) = E_Procedure then
4179 ("interface procedure % must be abstract or null", N);
4182 ("interface function % must be abstract", N);
4188 -- What is the following code for, it used to be
4190 -- ??? Set_Suppress_Elaboration_Checks
4191 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4193 -- The following seems equivalent, but a bit dubious
4195 if Elaboration_Checks_Suppressed (Designator) then
4196 Set_Kill_Elaboration_Checks (Designator);
4199 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4200 Set_Categorization_From_Scope (Designator, Scop);
4203 -- For a compilation unit, check for library-unit pragmas
4205 Push_Scope (Designator);
4206 Set_Categorization_From_Pragmas (N);
4207 Validate_Categorization_Dependency (N, Designator);
4211 -- For a compilation unit, set body required. This flag will only be
4212 -- reset if a valid Import or Interface pragma is processed later on.
4214 if Nkind (Parent (N)) = N_Compilation_Unit then
4215 Set_Body_Required (Parent (N), True);
4217 if Ada_Version >= Ada_2005
4218 and then Nkind (Specification (N)) = N_Procedure_Specification
4219 and then Null_Present (Specification (N))
4222 ("null procedure cannot be declared at library level", N);
4226 Generate_Reference_To_Formals (Designator);
4227 Check_Eliminated (Designator);
4229 if Debug_Flag_C then
4231 Write_Str ("<== subprogram spec ");
4232 Write_Name (Chars (Designator));
4233 Write_Str (" from ");
4234 Write_Location (Sloc (N));
4238 if Is_Protected_Type (Current_Scope) then
4240 -- Indicate that this is a protected operation, because it may be
4241 -- used in subsequent declarations within the protected type.
4243 Set_Convention (Designator, Convention_Protected);
4246 List_Inherited_Pre_Post_Aspects (Designator);
4248 if Has_Aspects (N) then
4249 Analyze_Aspect_Specifications (N, Designator);
4251 end Analyze_Subprogram_Declaration;
4253 --------------------------------------
4254 -- Analyze_Subprogram_Specification --
4255 --------------------------------------
4257 -- Reminder: N here really is a subprogram specification (not a subprogram
4258 -- declaration). This procedure is called to analyze the specification in
4259 -- both subprogram bodies and subprogram declarations (specs).
4261 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4262 Designator : constant Entity_Id := Defining_Entity (N);
4263 Formals : constant List_Id := Parameter_Specifications (N);
4265 -- Start of processing for Analyze_Subprogram_Specification
4268 -- User-defined operator is not allowed in SPARK, except as a renaming
4270 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4271 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4273 Check_SPARK_05_Restriction
4274 ("user-defined operator is not allowed", N);
4277 -- Proceed with analysis. Do not emit a cross-reference entry if the
4278 -- specification comes from an expression function, because it may be
4279 -- the completion of a previous declaration. It is not, the cross-
4280 -- reference entry will be emitted for the new subprogram declaration.
4282 if Nkind (Parent (N)) /= N_Expression_Function then
4283 Generate_Definition (Designator);
4286 if Nkind (N) = N_Function_Specification then
4287 Set_Ekind (Designator, E_Function);
4288 Set_Mechanism (Designator, Default_Mechanism);
4290 Set_Ekind (Designator, E_Procedure);
4291 Set_Etype (Designator, Standard_Void_Type);
4294 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4295 -- those subprograms which could be inlined in GNATprove mode (because
4296 -- Body_To_Inline is non-Empty) but should not be inlined.
4298 if GNATprove_Mode then
4299 Set_Is_Inlined_Always (Designator);
4302 -- Introduce new scope for analysis of the formals and the return type
4304 Set_Scope (Designator, Current_Scope);
4306 if Present (Formals) then
4307 Push_Scope (Designator);
4308 Process_Formals (Formals, N);
4310 -- Check dimensions in N for formals with default expression
4312 Analyze_Dimension_Formals (N, Formals);
4314 -- Ada 2005 (AI-345): If this is an overriding operation of an
4315 -- inherited interface operation, and the controlling type is
4316 -- a synchronized type, replace the type with its corresponding
4317 -- record, to match the proper signature of an overriding operation.
4318 -- Same processing for an access parameter whose designated type is
4319 -- derived from a synchronized interface.
4321 if Ada_Version >= Ada_2005 then
4324 Formal_Typ : Entity_Id;
4325 Rec_Typ : Entity_Id;
4326 Desig_Typ : Entity_Id;
4329 Formal := First_Formal (Designator);
4330 while Present (Formal) loop
4331 Formal_Typ := Etype (Formal);
4333 if Is_Concurrent_Type (Formal_Typ)
4334 and then Present (Corresponding_Record_Type (Formal_Typ))
4336 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4338 if Present (Interfaces (Rec_Typ)) then
4339 Set_Etype (Formal, Rec_Typ);
4342 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4343 Desig_Typ := Designated_Type (Formal_Typ);
4345 if Is_Concurrent_Type (Desig_Typ)
4346 and then Present (Corresponding_Record_Type (Desig_Typ))
4348 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4350 if Present (Interfaces (Rec_Typ)) then
4351 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4356 Next_Formal (Formal);
4363 -- The subprogram scope is pushed and popped around the processing of
4364 -- the return type for consistency with call above to Process_Formals
4365 -- (which itself can call Analyze_Return_Type), and to ensure that any
4366 -- itype created for the return type will be associated with the proper
4369 elsif Nkind (N) = N_Function_Specification then
4370 Push_Scope (Designator);
4371 Analyze_Return_Type (N);
4377 if Nkind (N) = N_Function_Specification then
4379 -- Deal with operator symbol case
4381 if Nkind (Designator) = N_Defining_Operator_Symbol then
4382 Valid_Operator_Definition (Designator);
4385 May_Need_Actuals (Designator);
4387 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4388 -- the subprogram is abstract also. This does not apply to renaming
4389 -- declarations, where abstractness is inherited, and to subprogram
4390 -- bodies generated for stream operations, which become renamings as
4393 -- In case of primitives associated with abstract interface types
4394 -- the check is applied later (see Analyze_Subprogram_Declaration).
4396 if not Nkind_In (Original_Node (Parent (N)),
4397 N_Abstract_Subprogram_Declaration,
4398 N_Formal_Abstract_Subprogram_Declaration,
4399 N_Subprogram_Renaming_Declaration)
4401 if Is_Abstract_Type (Etype (Designator))
4402 and then not Is_Interface (Etype (Designator))
4405 ("function that returns abstract type must be abstract", N);
4407 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4408 -- access result whose designated type is abstract.
4410 elsif Ada_Version >= Ada_2012
4411 and then Nkind (Result_Definition (N)) = N_Access_Definition
4413 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4414 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4417 ("function whose access result designates abstract type "
4418 & "must be abstract", N);
4424 end Analyze_Subprogram_Specification;
4426 -----------------------
4427 -- Check_Conformance --
4428 -----------------------
4430 procedure Check_Conformance
4431 (New_Id : Entity_Id;
4433 Ctype : Conformance_Type;
4435 Conforms : out Boolean;
4436 Err_Loc : Node_Id := Empty;
4437 Get_Inst : Boolean := False;
4438 Skip_Controlling_Formals : Boolean := False)
4440 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4441 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4442 -- If Errmsg is True, then processing continues to post an error message
4443 -- for conformance error on given node. Two messages are output. The
4444 -- first message points to the previous declaration with a general "no
4445 -- conformance" message. The second is the detailed reason, supplied as
4446 -- Msg. The parameter N provide information for a possible & insertion
4447 -- in the message, and also provides the location for posting the
4448 -- message in the absence of a specified Err_Loc location.
4450 -----------------------
4451 -- Conformance_Error --
4452 -----------------------
4454 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4461 if No (Err_Loc) then
4467 Error_Msg_Sloc := Sloc (Old_Id);
4470 when Type_Conformant =>
4471 Error_Msg_N -- CODEFIX
4472 ("not type conformant with declaration#!", Enode);
4474 when Mode_Conformant =>
4475 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4477 ("not mode conformant with operation inherited#!",
4481 ("not mode conformant with declaration#!", Enode);
4484 when Subtype_Conformant =>
4485 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4487 ("not subtype conformant with operation inherited#!",
4491 ("not subtype conformant with declaration#!", Enode);
4494 when Fully_Conformant =>
4495 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4496 Error_Msg_N -- CODEFIX
4497 ("not fully conformant with operation inherited#!",
4500 Error_Msg_N -- CODEFIX
4501 ("not fully conformant with declaration#!", Enode);
4505 Error_Msg_NE (Msg, Enode, N);
4507 end Conformance_Error;
4511 Old_Type : constant Entity_Id := Etype (Old_Id);
4512 New_Type : constant Entity_Id := Etype (New_Id);
4513 Old_Formal : Entity_Id;
4514 New_Formal : Entity_Id;
4515 Access_Types_Match : Boolean;
4516 Old_Formal_Base : Entity_Id;
4517 New_Formal_Base : Entity_Id;
4519 -- Start of processing for Check_Conformance
4524 -- We need a special case for operators, since they don't appear
4527 if Ctype = Type_Conformant then
4528 if Ekind (New_Id) = E_Operator
4529 and then Operator_Matches_Spec (New_Id, Old_Id)
4535 -- If both are functions/operators, check return types conform
4537 if Old_Type /= Standard_Void_Type
4539 New_Type /= Standard_Void_Type
4541 -- If we are checking interface conformance we omit controlling
4542 -- arguments and result, because we are only checking the conformance
4543 -- of the remaining parameters.
4545 if Has_Controlling_Result (Old_Id)
4546 and then Has_Controlling_Result (New_Id)
4547 and then Skip_Controlling_Formals
4551 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4552 if Ctype >= Subtype_Conformant
4553 and then not Predicates_Match (Old_Type, New_Type)
4556 ("\predicate of return type does not match!", New_Id);
4559 ("\return type does not match!", New_Id);
4565 -- Ada 2005 (AI-231): In case of anonymous access types check the
4566 -- null-exclusion and access-to-constant attributes match.
4568 if Ada_Version >= Ada_2005
4569 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4571 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4572 or else Is_Access_Constant (Etype (Old_Type)) /=
4573 Is_Access_Constant (Etype (New_Type)))
4575 Conformance_Error ("\return type does not match!", New_Id);
4579 -- If either is a function/operator and the other isn't, error
4581 elsif Old_Type /= Standard_Void_Type
4582 or else New_Type /= Standard_Void_Type
4584 Conformance_Error ("\functions can only match functions!", New_Id);
4588 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4589 -- If this is a renaming as body, refine error message to indicate that
4590 -- the conflict is with the original declaration. If the entity is not
4591 -- frozen, the conventions don't have to match, the one of the renamed
4592 -- entity is inherited.
4594 if Ctype >= Subtype_Conformant then
4595 if Convention (Old_Id) /= Convention (New_Id) then
4596 if not Is_Frozen (New_Id) then
4599 elsif Present (Err_Loc)
4600 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4601 and then Present (Corresponding_Spec (Err_Loc))
4603 Error_Msg_Name_1 := Chars (New_Id);
4605 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4606 Conformance_Error ("\prior declaration for% has convention %!");
4609 Conformance_Error ("\calling conventions do not match!");
4614 elsif Is_Formal_Subprogram (Old_Id)
4615 or else Is_Formal_Subprogram (New_Id)
4617 Conformance_Error ("\formal subprograms not allowed!");
4620 -- Pragma Ghost behaves as a convention in the context of subtype
4621 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4622 -- subprograms as their spec may reside in a Ghost region and their
4623 -- body not, or vice versa.
4625 elsif Comes_From_Source (Old_Id)
4626 and then Comes_From_Source (New_Id)
4627 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4629 Conformance_Error ("\ghost modes do not match!");
4634 -- Deal with parameters
4636 -- Note: we use the entity information, rather than going directly
4637 -- to the specification in the tree. This is not only simpler, but
4638 -- absolutely necessary for some cases of conformance tests between
4639 -- operators, where the declaration tree simply does not exist.
4641 Old_Formal := First_Formal (Old_Id);
4642 New_Formal := First_Formal (New_Id);
4643 while Present (Old_Formal) and then Present (New_Formal) loop
4644 if Is_Controlling_Formal (Old_Formal)
4645 and then Is_Controlling_Formal (New_Formal)
4646 and then Skip_Controlling_Formals
4648 -- The controlling formals will have different types when
4649 -- comparing an interface operation with its match, but both
4650 -- or neither must be access parameters.
4652 if Is_Access_Type (Etype (Old_Formal))
4654 Is_Access_Type (Etype (New_Formal))
4656 goto Skip_Controlling_Formal;
4659 ("\access parameter does not match!", New_Formal);
4663 -- Ada 2012: Mode conformance also requires that formal parameters
4664 -- be both aliased, or neither.
4666 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4667 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4669 ("\aliased parameter mismatch!", New_Formal);
4673 if Ctype = Fully_Conformant then
4675 -- Names must match. Error message is more accurate if we do
4676 -- this before checking that the types of the formals match.
4678 if Chars (Old_Formal) /= Chars (New_Formal) then
4679 Conformance_Error ("\name& does not match!", New_Formal);
4681 -- Set error posted flag on new formal as well to stop
4682 -- junk cascaded messages in some cases.
4684 Set_Error_Posted (New_Formal);
4688 -- Null exclusion must match
4690 if Null_Exclusion_Present (Parent (Old_Formal))
4692 Null_Exclusion_Present (Parent (New_Formal))
4694 -- Only give error if both come from source. This should be
4695 -- investigated some time, since it should not be needed ???
4697 if Comes_From_Source (Old_Formal)
4699 Comes_From_Source (New_Formal)
4702 ("\null exclusion for& does not match", New_Formal);
4704 -- Mark error posted on the new formal to avoid duplicated
4705 -- complaint about types not matching.
4707 Set_Error_Posted (New_Formal);
4712 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4713 -- case occurs whenever a subprogram is being renamed and one of its
4714 -- parameters imposes a null exclusion. For example:
4716 -- type T is null record;
4717 -- type Acc_T is access T;
4718 -- subtype Acc_T_Sub is Acc_T;
4720 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4721 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4724 Old_Formal_Base := Etype (Old_Formal);
4725 New_Formal_Base := Etype (New_Formal);
4728 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4729 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4732 Access_Types_Match := Ada_Version >= Ada_2005
4734 -- Ensure that this rule is only applied when New_Id is a
4735 -- renaming of Old_Id.
4737 and then Nkind (Parent (Parent (New_Id))) =
4738 N_Subprogram_Renaming_Declaration
4739 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4740 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4741 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4743 -- Now handle the allowed access-type case
4745 and then Is_Access_Type (Old_Formal_Base)
4746 and then Is_Access_Type (New_Formal_Base)
4748 -- The type kinds must match. The only exception occurs with
4749 -- multiple generics of the form:
4752 -- type F is private; type A is private;
4753 -- type F_Ptr is access F; type A_Ptr is access A;
4754 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4755 -- package F_Pack is ... package A_Pack is
4756 -- package F_Inst is
4757 -- new F_Pack (A, A_Ptr, A_P);
4759 -- When checking for conformance between the parameters of A_P
4760 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4761 -- because the compiler has transformed A_Ptr into a subtype of
4762 -- F_Ptr. We catch this case in the code below.
4764 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4766 (Is_Generic_Type (Old_Formal_Base)
4767 and then Is_Generic_Type (New_Formal_Base)
4768 and then Is_Internal (New_Formal_Base)
4769 and then Etype (Etype (New_Formal_Base)) =
4771 and then Directly_Designated_Type (Old_Formal_Base) =
4772 Directly_Designated_Type (New_Formal_Base)
4773 and then ((Is_Itype (Old_Formal_Base)
4774 and then Can_Never_Be_Null (Old_Formal_Base))
4776 (Is_Itype (New_Formal_Base)
4777 and then Can_Never_Be_Null (New_Formal_Base)));
4779 -- Types must always match. In the visible part of an instance,
4780 -- usual overloading rules for dispatching operations apply, and
4781 -- we check base types (not the actual subtypes).
4783 if In_Instance_Visible_Part
4784 and then Is_Dispatching_Operation (New_Id)
4786 if not Conforming_Types
4787 (T1 => Base_Type (Etype (Old_Formal)),
4788 T2 => Base_Type (Etype (New_Formal)),
4790 Get_Inst => Get_Inst)
4791 and then not Access_Types_Match
4793 Conformance_Error ("\type of & does not match!", New_Formal);
4797 elsif not Conforming_Types
4798 (T1 => Old_Formal_Base,
4799 T2 => New_Formal_Base,
4801 Get_Inst => Get_Inst)
4802 and then not Access_Types_Match
4804 -- Don't give error message if old type is Any_Type. This test
4805 -- avoids some cascaded errors, e.g. in case of a bad spec.
4807 if Errmsg and then Old_Formal_Base = Any_Type then
4810 if Ctype >= Subtype_Conformant
4812 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4815 ("\predicate of & does not match!", New_Formal);
4818 ("\type of & does not match!", New_Formal);
4825 -- For mode conformance, mode must match
4827 if Ctype >= Mode_Conformant then
4828 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4829 if not Ekind_In (New_Id, E_Function, E_Procedure)
4830 or else not Is_Primitive_Wrapper (New_Id)
4832 Conformance_Error ("\mode of & does not match!", New_Formal);
4836 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4838 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
4840 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
4843 ("\mode of & does not match!", New_Formal);
4850 -- Part of mode conformance for access types is having the same
4851 -- constant modifier.
4853 elsif Access_Types_Match
4854 and then Is_Access_Constant (Old_Formal_Base) /=
4855 Is_Access_Constant (New_Formal_Base)
4858 ("\constant modifier does not match!", New_Formal);
4863 if Ctype >= Subtype_Conformant then
4865 -- Ada 2005 (AI-231): In case of anonymous access types check
4866 -- the null-exclusion and access-to-constant attributes must
4867 -- match. For null exclusion, we test the types rather than the
4868 -- formals themselves, since the attribute is only set reliably
4869 -- on the formals in the Ada 95 case, and we exclude the case
4870 -- where Old_Formal is marked as controlling, to avoid errors
4871 -- when matching completing bodies with dispatching declarations
4872 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4874 if Ada_Version >= Ada_2005
4875 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4876 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4878 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4879 Can_Never_Be_Null (Etype (New_Formal))
4881 not Is_Controlling_Formal (Old_Formal))
4883 Is_Access_Constant (Etype (Old_Formal)) /=
4884 Is_Access_Constant (Etype (New_Formal)))
4886 -- Do not complain if error already posted on New_Formal. This
4887 -- avoids some redundant error messages.
4889 and then not Error_Posted (New_Formal)
4891 -- It is allowed to omit the null-exclusion in case of stream
4892 -- attribute subprograms. We recognize stream subprograms
4893 -- through their TSS-generated suffix.
4896 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4899 if TSS_Name /= TSS_Stream_Read
4900 and then TSS_Name /= TSS_Stream_Write
4901 and then TSS_Name /= TSS_Stream_Input
4902 and then TSS_Name /= TSS_Stream_Output
4904 -- Here we have a definite conformance error. It is worth
4905 -- special casing the error message for the case of a
4906 -- controlling formal (which excludes null).
4908 if Is_Controlling_Formal (New_Formal) then
4909 Error_Msg_Node_2 := Scope (New_Formal);
4911 ("\controlling formal & of & excludes null, "
4912 & "declaration must exclude null as well",
4915 -- Normal case (couldn't we give more detail here???)
4919 ("\type of & does not match!", New_Formal);
4928 -- Full conformance checks
4930 if Ctype = Fully_Conformant then
4932 -- We have checked already that names match
4934 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4936 -- Check default expressions for in parameters
4939 NewD : constant Boolean :=
4940 Present (Default_Value (New_Formal));
4941 OldD : constant Boolean :=
4942 Present (Default_Value (Old_Formal));
4944 if NewD or OldD then
4946 -- The old default value has been analyzed because the
4947 -- current full declaration will have frozen everything
4948 -- before. The new default value has not been analyzed,
4949 -- so analyze it now before we check for conformance.
4952 Push_Scope (New_Id);
4953 Preanalyze_Spec_Expression
4954 (Default_Value (New_Formal), Etype (New_Formal));
4958 if not (NewD and OldD)
4959 or else not Fully_Conformant_Expressions
4960 (Default_Value (Old_Formal),
4961 Default_Value (New_Formal))
4964 ("\default expression for & does not match!",
4973 -- A couple of special checks for Ada 83 mode. These checks are
4974 -- skipped if either entity is an operator in package Standard,
4975 -- or if either old or new instance is not from the source program.
4977 if Ada_Version = Ada_83
4978 and then Sloc (Old_Id) > Standard_Location
4979 and then Sloc (New_Id) > Standard_Location
4980 and then Comes_From_Source (Old_Id)
4981 and then Comes_From_Source (New_Id)
4984 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4985 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4988 -- Explicit IN must be present or absent in both cases. This
4989 -- test is required only in the full conformance case.
4991 if In_Present (Old_Param) /= In_Present (New_Param)
4992 and then Ctype = Fully_Conformant
4995 ("\(Ada 83) IN must appear in both declarations",
5000 -- Grouping (use of comma in param lists) must be the same
5001 -- This is where we catch a misconformance like:
5004 -- A : Integer; B : Integer
5006 -- which are represented identically in the tree except
5007 -- for the setting of the flags More_Ids and Prev_Ids.
5009 if More_Ids (Old_Param) /= More_Ids (New_Param)
5010 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5013 ("\grouping of & does not match!", New_Formal);
5019 -- This label is required when skipping controlling formals
5021 <<Skip_Controlling_Formal>>
5023 Next_Formal (Old_Formal);
5024 Next_Formal (New_Formal);
5027 if Present (Old_Formal) then
5028 Conformance_Error ("\too few parameters!");
5031 elsif Present (New_Formal) then
5032 Conformance_Error ("\too many parameters!", New_Formal);
5035 end Check_Conformance;
5037 -----------------------
5038 -- Check_Conventions --
5039 -----------------------
5041 procedure Check_Conventions (Typ : Entity_Id) is
5042 Ifaces_List : Elist_Id;
5044 procedure Check_Convention (Op : Entity_Id);
5045 -- Verify that the convention of inherited dispatching operation Op is
5046 -- consistent among all subprograms it overrides. In order to minimize
5047 -- the search, Search_From is utilized to designate a specific point in
5048 -- the list rather than iterating over the whole list once more.
5050 ----------------------
5051 -- Check_Convention --
5052 ----------------------
5054 procedure Check_Convention (Op : Entity_Id) is
5055 Op_Conv : constant Convention_Id := Convention (Op);
5056 Iface_Conv : Convention_Id;
5057 Iface_Elmt : Elmt_Id;
5058 Iface_Prim_Elmt : Elmt_Id;
5059 Iface_Prim : Entity_Id;
5062 Iface_Elmt := First_Elmt (Ifaces_List);
5063 while Present (Iface_Elmt) loop
5065 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5066 while Present (Iface_Prim_Elmt) loop
5067 Iface_Prim := Node (Iface_Prim_Elmt);
5068 Iface_Conv := Convention (Iface_Prim);
5070 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5071 and then Iface_Conv /= Op_Conv
5074 ("inconsistent conventions in primitive operations", Typ);
5076 Error_Msg_Name_1 := Chars (Op);
5077 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5078 Error_Msg_Sloc := Sloc (Op);
5080 if Comes_From_Source (Op) or else No (Alias (Op)) then
5081 if not Present (Overridden_Operation (Op)) then
5082 Error_Msg_N ("\\primitive % defined #", Typ);
5085 ("\\overriding operation % with "
5086 & "convention % defined #", Typ);
5089 else pragma Assert (Present (Alias (Op)));
5090 Error_Msg_Sloc := Sloc (Alias (Op));
5091 Error_Msg_N ("\\inherited operation % with "
5092 & "convention % defined #", Typ);
5095 Error_Msg_Name_1 := Chars (Op);
5096 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5097 Error_Msg_Sloc := Sloc (Iface_Prim);
5098 Error_Msg_N ("\\overridden operation % with "
5099 & "convention % defined #", Typ);
5101 -- Avoid cascading errors
5106 Next_Elmt (Iface_Prim_Elmt);
5109 Next_Elmt (Iface_Elmt);
5111 end Check_Convention;
5115 Prim_Op : Entity_Id;
5116 Prim_Op_Elmt : Elmt_Id;
5118 -- Start of processing for Check_Conventions
5121 if not Has_Interfaces (Typ) then
5125 Collect_Interfaces (Typ, Ifaces_List);
5127 -- The algorithm checks every overriding dispatching operation against
5128 -- all the corresponding overridden dispatching operations, detecting
5129 -- differences in conventions.
5131 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5132 while Present (Prim_Op_Elmt) loop
5133 Prim_Op := Node (Prim_Op_Elmt);
5135 -- A small optimization: skip the predefined dispatching operations
5136 -- since they always have the same convention.
5138 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5139 Check_Convention (Prim_Op);
5142 Next_Elmt (Prim_Op_Elmt);
5144 end Check_Conventions;
5146 ------------------------------
5147 -- Check_Delayed_Subprogram --
5148 ------------------------------
5150 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5153 procedure Possible_Freeze (T : Entity_Id);
5154 -- T is the type of either a formal parameter or of the return type.
5155 -- If T is not yet frozen and needs a delayed freeze, then the
5156 -- subprogram itself must be delayed. If T is the limited view of an
5157 -- incomplete type the subprogram must be frozen as well, because
5158 -- T may depend on local types that have not been frozen yet.
5160 ---------------------
5161 -- Possible_Freeze --
5162 ---------------------
5164 procedure Possible_Freeze (T : Entity_Id) is
5166 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5167 Set_Has_Delayed_Freeze (Designator);
5169 elsif Is_Access_Type (T)
5170 and then Has_Delayed_Freeze (Designated_Type (T))
5171 and then not Is_Frozen (Designated_Type (T))
5173 Set_Has_Delayed_Freeze (Designator);
5175 elsif Ekind (T) = E_Incomplete_Type
5176 and then From_Limited_With (T)
5178 Set_Has_Delayed_Freeze (Designator);
5180 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5181 -- of a subprogram or entry declaration.
5183 elsif Ekind (T) = E_Incomplete_Type
5184 and then Ada_Version >= Ada_2012
5186 Set_Has_Delayed_Freeze (Designator);
5189 end Possible_Freeze;
5191 -- Start of processing for Check_Delayed_Subprogram
5194 -- All subprograms, including abstract subprograms, may need a freeze
5195 -- node if some formal type or the return type needs one.
5197 Possible_Freeze (Etype (Designator));
5198 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5200 -- Need delayed freeze if any of the formal types themselves need
5201 -- a delayed freeze and are not yet frozen.
5203 F := First_Formal (Designator);
5204 while Present (F) loop
5205 Possible_Freeze (Etype (F));
5206 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5210 -- Mark functions that return by reference. Note that it cannot be
5211 -- done for delayed_freeze subprograms because the underlying
5212 -- returned type may not be known yet (for private types)
5214 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5216 Typ : constant Entity_Id := Etype (Designator);
5217 Utyp : constant Entity_Id := Underlying_Type (Typ);
5219 if Is_Limited_View (Typ) then
5220 Set_Returns_By_Ref (Designator);
5221 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5222 Set_Returns_By_Ref (Designator);
5226 end Check_Delayed_Subprogram;
5228 ------------------------------------
5229 -- Check_Discriminant_Conformance --
5230 ------------------------------------
5232 procedure Check_Discriminant_Conformance
5237 Old_Discr : Entity_Id := First_Discriminant (Prev);
5238 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5239 New_Discr_Id : Entity_Id;
5240 New_Discr_Type : Entity_Id;
5242 procedure Conformance_Error (Msg : String; N : Node_Id);
5243 -- Post error message for conformance error on given node. Two messages
5244 -- are output. The first points to the previous declaration with a
5245 -- general "no conformance" message. The second is the detailed reason,
5246 -- supplied as Msg. The parameter N provide information for a possible
5247 -- & insertion in the message.
5249 -----------------------
5250 -- Conformance_Error --
5251 -----------------------
5253 procedure Conformance_Error (Msg : String; N : Node_Id) is
5255 Error_Msg_Sloc := Sloc (Prev_Loc);
5256 Error_Msg_N -- CODEFIX
5257 ("not fully conformant with declaration#!", N);
5258 Error_Msg_NE (Msg, N, N);
5259 end Conformance_Error;
5261 -- Start of processing for Check_Discriminant_Conformance
5264 while Present (Old_Discr) and then Present (New_Discr) loop
5265 New_Discr_Id := Defining_Identifier (New_Discr);
5267 -- The subtype mark of the discriminant on the full type has not
5268 -- been analyzed so we do it here. For an access discriminant a new
5271 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5273 Access_Definition (N, Discriminant_Type (New_Discr));
5276 Analyze (Discriminant_Type (New_Discr));
5277 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5279 -- Ada 2005: if the discriminant definition carries a null
5280 -- exclusion, create an itype to check properly for consistency
5281 -- with partial declaration.
5283 if Is_Access_Type (New_Discr_Type)
5284 and then Null_Exclusion_Present (New_Discr)
5287 Create_Null_Excluding_Itype
5288 (T => New_Discr_Type,
5289 Related_Nod => New_Discr,
5290 Scope_Id => Current_Scope);
5294 if not Conforming_Types
5295 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5297 Conformance_Error ("type of & does not match!", New_Discr_Id);
5300 -- Treat the new discriminant as an occurrence of the old one,
5301 -- for navigation purposes, and fill in some semantic
5302 -- information, for completeness.
5304 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5305 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5306 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5311 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5312 Conformance_Error ("name & does not match!", New_Discr_Id);
5316 -- Default expressions must match
5319 NewD : constant Boolean :=
5320 Present (Expression (New_Discr));
5321 OldD : constant Boolean :=
5322 Present (Expression (Parent (Old_Discr)));
5325 if NewD or OldD then
5327 -- The old default value has been analyzed and expanded,
5328 -- because the current full declaration will have frozen
5329 -- everything before. The new default values have not been
5330 -- expanded, so expand now to check conformance.
5333 Preanalyze_Spec_Expression
5334 (Expression (New_Discr), New_Discr_Type);
5337 if not (NewD and OldD)
5338 or else not Fully_Conformant_Expressions
5339 (Expression (Parent (Old_Discr)),
5340 Expression (New_Discr))
5344 ("default expression for & does not match!",
5351 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5353 if Ada_Version = Ada_83 then
5355 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5358 -- Grouping (use of comma in param lists) must be the same
5359 -- This is where we catch a misconformance like:
5362 -- A : Integer; B : Integer
5364 -- which are represented identically in the tree except
5365 -- for the setting of the flags More_Ids and Prev_Ids.
5367 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5368 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5371 ("grouping of & does not match!", New_Discr_Id);
5377 Next_Discriminant (Old_Discr);
5381 if Present (Old_Discr) then
5382 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5385 elsif Present (New_Discr) then
5387 ("too many discriminants!", Defining_Identifier (New_Discr));
5390 end Check_Discriminant_Conformance;
5392 ----------------------------
5393 -- Check_Fully_Conformant --
5394 ----------------------------
5396 procedure Check_Fully_Conformant
5397 (New_Id : Entity_Id;
5399 Err_Loc : Node_Id := Empty)
5402 pragma Warnings (Off, Result);
5405 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5406 end Check_Fully_Conformant;
5408 ---------------------------
5409 -- Check_Mode_Conformant --
5410 ---------------------------
5412 procedure Check_Mode_Conformant
5413 (New_Id : Entity_Id;
5415 Err_Loc : Node_Id := Empty;
5416 Get_Inst : Boolean := False)
5419 pragma Warnings (Off, Result);
5422 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5423 end Check_Mode_Conformant;
5425 --------------------------------
5426 -- Check_Overriding_Indicator --
5427 --------------------------------
5429 procedure Check_Overriding_Indicator
5431 Overridden_Subp : Entity_Id;
5432 Is_Primitive : Boolean)
5438 -- No overriding indicator for literals
5440 if Ekind (Subp) = E_Enumeration_Literal then
5443 elsif Ekind (Subp) = E_Entry then
5444 Decl := Parent (Subp);
5446 -- No point in analyzing a malformed operator
5448 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5449 and then Error_Posted (Subp)
5454 Decl := Unit_Declaration_Node (Subp);
5457 if Nkind_In (Decl, N_Subprogram_Body,
5458 N_Subprogram_Body_Stub,
5459 N_Subprogram_Declaration,
5460 N_Abstract_Subprogram_Declaration,
5461 N_Subprogram_Renaming_Declaration)
5463 Spec := Specification (Decl);
5465 elsif Nkind (Decl) = N_Entry_Declaration then
5472 -- The overriding operation is type conformant with the overridden one,
5473 -- but the names of the formals are not required to match. If the names
5474 -- appear permuted in the overriding operation, this is a possible
5475 -- source of confusion that is worth diagnosing. Controlling formals
5476 -- often carry names that reflect the type, and it is not worthwhile
5477 -- requiring that their names match.
5479 if Present (Overridden_Subp)
5480 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5487 Form1 := First_Formal (Subp);
5488 Form2 := First_Formal (Overridden_Subp);
5490 -- If the overriding operation is a synchronized operation, skip
5491 -- the first parameter of the overridden operation, which is
5492 -- implicit in the new one. If the operation is declared in the
5493 -- body it is not primitive and all formals must match.
5495 if Is_Concurrent_Type (Scope (Subp))
5496 and then Is_Tagged_Type (Scope (Subp))
5497 and then not Has_Completion (Scope (Subp))
5499 Form2 := Next_Formal (Form2);
5502 if Present (Form1) then
5503 Form1 := Next_Formal (Form1);
5504 Form2 := Next_Formal (Form2);
5507 while Present (Form1) loop
5508 if not Is_Controlling_Formal (Form1)
5509 and then Present (Next_Formal (Form2))
5510 and then Chars (Form1) = Chars (Next_Formal (Form2))
5512 Error_Msg_Node_2 := Alias (Overridden_Subp);
5513 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5515 ("& does not match corresponding formal of&#",
5520 Next_Formal (Form1);
5521 Next_Formal (Form2);
5526 -- If there is an overridden subprogram, then check that there is no
5527 -- "not overriding" indicator, and mark the subprogram as overriding.
5528 -- This is not done if the overridden subprogram is marked as hidden,
5529 -- which can occur for the case of inherited controlled operations
5530 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5531 -- subprogram is not itself hidden. (Note: This condition could probably
5532 -- be simplified, leaving out the testing for the specific controlled
5533 -- cases, but it seems safer and clearer this way, and echoes similar
5534 -- special-case tests of this kind in other places.)
5536 if Present (Overridden_Subp)
5537 and then (not Is_Hidden (Overridden_Subp)
5539 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5542 and then Present (Alias (Overridden_Subp))
5543 and then not Is_Hidden (Alias (Overridden_Subp))))
5545 if Must_Not_Override (Spec) then
5546 Error_Msg_Sloc := Sloc (Overridden_Subp);
5548 if Ekind (Subp) = E_Entry then
5550 ("entry & overrides inherited operation #", Spec, Subp);
5553 ("subprogram & overrides inherited operation #", Spec, Subp);
5556 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5557 -- as an extension of Root_Controlled, and thus has a useless Adjust
5558 -- operation. This operation should not be inherited by other limited
5559 -- controlled types. An explicit Adjust for them is not overriding.
5561 elsif Must_Override (Spec)
5562 and then Chars (Overridden_Subp) = Name_Adjust
5563 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5564 and then Present (Alias (Overridden_Subp))
5566 Is_Predefined_File_Name
5567 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5569 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5571 elsif Is_Subprogram (Subp) then
5572 if Is_Init_Proc (Subp) then
5575 elsif No (Overridden_Operation (Subp)) then
5577 -- For entities generated by Derive_Subprograms the overridden
5578 -- operation is the inherited primitive (which is available
5579 -- through the attribute alias)
5581 if (Is_Dispatching_Operation (Subp)
5582 or else Is_Dispatching_Operation (Overridden_Subp))
5583 and then not Comes_From_Source (Overridden_Subp)
5584 and then Find_Dispatching_Type (Overridden_Subp) =
5585 Find_Dispatching_Type (Subp)
5586 and then Present (Alias (Overridden_Subp))
5587 and then Comes_From_Source (Alias (Overridden_Subp))
5589 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5590 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5593 Set_Overridden_Operation (Subp, Overridden_Subp);
5594 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5599 -- If primitive flag is set or this is a protected operation, then
5600 -- the operation is overriding at the point of its declaration, so
5601 -- warn if necessary. Otherwise it may have been declared before the
5602 -- operation it overrides and no check is required.
5605 and then not Must_Override (Spec)
5606 and then (Is_Primitive
5607 or else Ekind (Scope (Subp)) = E_Protected_Type)
5609 Style.Missing_Overriding (Decl, Subp);
5612 -- If Subp is an operator, it may override a predefined operation, if
5613 -- it is defined in the same scope as the type to which it applies.
5614 -- In that case Overridden_Subp is empty because of our implicit
5615 -- representation for predefined operators. We have to check whether the
5616 -- signature of Subp matches that of a predefined operator. Note that
5617 -- first argument provides the name of the operator, and the second
5618 -- argument the signature that may match that of a standard operation.
5619 -- If the indicator is overriding, then the operator must match a
5620 -- predefined signature, because we know already that there is no
5621 -- explicit overridden operation.
5623 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5624 if Must_Not_Override (Spec) then
5626 -- If this is not a primitive or a protected subprogram, then
5627 -- "not overriding" is illegal.
5630 and then Ekind (Scope (Subp)) /= E_Protected_Type
5632 Error_Msg_N ("overriding indicator only allowed "
5633 & "if subprogram is primitive", Subp);
5635 elsif Can_Override_Operator (Subp) then
5637 ("subprogram& overrides predefined operator ", Spec, Subp);
5640 elsif Must_Override (Spec) then
5641 if No (Overridden_Operation (Subp))
5642 and then not Can_Override_Operator (Subp)
5644 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5647 elsif not Error_Posted (Subp)
5648 and then Style_Check
5649 and then Can_Override_Operator (Subp)
5651 not Is_Predefined_File_Name
5652 (Unit_File_Name (Get_Source_Unit (Subp)))
5654 -- If style checks are enabled, indicate that the indicator is
5655 -- missing. However, at the point of declaration, the type of
5656 -- which this is a primitive operation may be private, in which
5657 -- case the indicator would be premature.
5659 if Has_Private_Declaration (Etype (Subp))
5660 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5664 Style.Missing_Overriding (Decl, Subp);
5668 elsif Must_Override (Spec) then
5669 if Ekind (Subp) = E_Entry then
5670 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5672 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5675 -- If the operation is marked "not overriding" and it's not primitive
5676 -- then an error is issued, unless this is an operation of a task or
5677 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5678 -- has been specified have already been checked above.
5680 elsif Must_Not_Override (Spec)
5681 and then not Is_Primitive
5682 and then Ekind (Subp) /= E_Entry
5683 and then Ekind (Scope (Subp)) /= E_Protected_Type
5686 ("overriding indicator only allowed if subprogram is primitive",
5690 end Check_Overriding_Indicator;
5696 -- Note: this procedure needs to know far too much about how the expander
5697 -- messes with exceptions. The use of the flag Exception_Junk and the
5698 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5699 -- works, but is not very clean. It would be better if the expansion
5700 -- routines would leave Original_Node working nicely, and we could use
5701 -- Original_Node here to ignore all the peculiar expander messing ???
5703 procedure Check_Returns
5707 Proc : Entity_Id := Empty)
5711 procedure Check_Statement_Sequence (L : List_Id);
5712 -- Internal recursive procedure to check a list of statements for proper
5713 -- termination by a return statement (or a transfer of control or a
5714 -- compound statement that is itself internally properly terminated).
5716 ------------------------------
5717 -- Check_Statement_Sequence --
5718 ------------------------------
5720 procedure Check_Statement_Sequence (L : List_Id) is
5725 function Assert_False return Boolean;
5726 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5727 -- rewritten as a null statement when assertions are off. The assert
5728 -- is not active, but it is still enough to kill the warning.
5734 function Assert_False return Boolean is
5735 Orig : constant Node_Id := Original_Node (Last_Stm);
5738 if Nkind (Orig) = N_Pragma
5739 and then Pragma_Name (Orig) = Name_Assert
5740 and then not Error_Posted (Orig)
5743 Arg : constant Node_Id :=
5744 First (Pragma_Argument_Associations (Orig));
5745 Exp : constant Node_Id := Expression (Arg);
5747 return Nkind (Exp) = N_Identifier
5748 and then Chars (Exp) = Name_False;
5758 Raise_Exception_Call : Boolean;
5759 -- Set True if statement sequence terminated by Raise_Exception call
5760 -- or a Reraise_Occurrence call.
5762 -- Start of processing for Check_Statement_Sequence
5765 Raise_Exception_Call := False;
5767 -- Get last real statement
5769 Last_Stm := Last (L);
5771 -- Deal with digging out exception handler statement sequences that
5772 -- have been transformed by the local raise to goto optimization.
5773 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5774 -- optimization has occurred, we are looking at something like:
5777 -- original stmts in block
5781 -- goto L1; | omitted if No_Exception_Propagation
5786 -- goto L3; -- skip handler when exception not raised
5788 -- <<L1>> -- target label for local exception
5802 -- and what we have to do is to dig out the estmts1 and estmts2
5803 -- sequences (which were the original sequences of statements in
5804 -- the exception handlers) and check them.
5806 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
5811 exit when Nkind (Stm) /= N_Block_Statement;
5812 exit when not Exception_Junk (Stm);
5815 exit when Nkind (Stm) /= N_Label;
5816 exit when not Exception_Junk (Stm);
5817 Check_Statement_Sequence
5818 (Statements (Handled_Statement_Sequence (Next (Stm))));
5823 exit when Nkind (Stm) /= N_Goto_Statement;
5824 exit when not Exception_Junk (Stm);
5828 -- Don't count pragmas
5830 while Nkind (Last_Stm) = N_Pragma
5832 -- Don't count call to SS_Release (can happen after Raise_Exception)
5835 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5837 Nkind (Name (Last_Stm)) = N_Identifier
5839 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5841 -- Don't count exception junk
5844 (Nkind_In (Last_Stm, N_Goto_Statement,
5846 N_Object_Declaration)
5847 and then Exception_Junk (Last_Stm))
5848 or else Nkind (Last_Stm) in N_Push_xxx_Label
5849 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5851 -- Inserted code, such as finalization calls, is irrelevant: we only
5852 -- need to check original source.
5854 or else Is_Rewrite_Insertion (Last_Stm)
5859 -- Here we have the "real" last statement
5861 Kind := Nkind (Last_Stm);
5863 -- Transfer of control, OK. Note that in the No_Return procedure
5864 -- case, we already diagnosed any explicit return statements, so
5865 -- we can treat them as OK in this context.
5867 if Is_Transfer (Last_Stm) then
5870 -- Check cases of explicit non-indirect procedure calls
5872 elsif Kind = N_Procedure_Call_Statement
5873 and then Is_Entity_Name (Name (Last_Stm))
5875 -- Check call to Raise_Exception procedure which is treated
5876 -- specially, as is a call to Reraise_Occurrence.
5878 -- We suppress the warning in these cases since it is likely that
5879 -- the programmer really does not expect to deal with the case
5880 -- of Null_Occurrence, and thus would find a warning about a
5881 -- missing return curious, and raising Program_Error does not
5882 -- seem such a bad behavior if this does occur.
5884 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5885 -- behavior will be to raise Constraint_Error (see AI-329).
5887 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5889 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5891 Raise_Exception_Call := True;
5893 -- For Raise_Exception call, test first argument, if it is
5894 -- an attribute reference for a 'Identity call, then we know
5895 -- that the call cannot possibly return.
5898 Arg : constant Node_Id :=
5899 Original_Node (First_Actual (Last_Stm));
5901 if Nkind (Arg) = N_Attribute_Reference
5902 and then Attribute_Name (Arg) = Name_Identity
5909 -- If statement, need to look inside if there is an else and check
5910 -- each constituent statement sequence for proper termination.
5912 elsif Kind = N_If_Statement
5913 and then Present (Else_Statements (Last_Stm))
5915 Check_Statement_Sequence (Then_Statements (Last_Stm));
5916 Check_Statement_Sequence (Else_Statements (Last_Stm));
5918 if Present (Elsif_Parts (Last_Stm)) then
5920 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5923 while Present (Elsif_Part) loop
5924 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5932 -- Case statement, check each case for proper termination
5934 elsif Kind = N_Case_Statement then
5938 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5939 while Present (Case_Alt) loop
5940 Check_Statement_Sequence (Statements (Case_Alt));
5941 Next_Non_Pragma (Case_Alt);
5947 -- Block statement, check its handled sequence of statements
5949 elsif Kind = N_Block_Statement then
5955 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5964 -- Loop statement. If there is an iteration scheme, we can definitely
5965 -- fall out of the loop. Similarly if there is an exit statement, we
5966 -- can fall out. In either case we need a following return.
5968 elsif Kind = N_Loop_Statement then
5969 if Present (Iteration_Scheme (Last_Stm))
5970 or else Has_Exit (Entity (Identifier (Last_Stm)))
5974 -- A loop with no exit statement or iteration scheme is either
5975 -- an infinite loop, or it has some other exit (raise/return).
5976 -- In either case, no warning is required.
5982 -- Timed entry call, check entry call and delay alternatives
5984 -- Note: in expanded code, the timed entry call has been converted
5985 -- to a set of expanded statements on which the check will work
5986 -- correctly in any case.
5988 elsif Kind = N_Timed_Entry_Call then
5990 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5991 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5994 -- If statement sequence of entry call alternative is missing,
5995 -- then we can definitely fall through, and we post the error
5996 -- message on the entry call alternative itself.
5998 if No (Statements (ECA)) then
6001 -- If statement sequence of delay alternative is missing, then
6002 -- we can definitely fall through, and we post the error
6003 -- message on the delay alternative itself.
6005 -- Note: if both ECA and DCA are missing the return, then we
6006 -- post only one message, should be enough to fix the bugs.
6007 -- If not we will get a message next time on the DCA when the
6010 elsif No (Statements (DCA)) then
6013 -- Else check both statement sequences
6016 Check_Statement_Sequence (Statements (ECA));
6017 Check_Statement_Sequence (Statements (DCA));
6022 -- Conditional entry call, check entry call and else part
6024 -- Note: in expanded code, the conditional entry call has been
6025 -- converted to a set of expanded statements on which the check
6026 -- will work correctly in any case.
6028 elsif Kind = N_Conditional_Entry_Call then
6030 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6033 -- If statement sequence of entry call alternative is missing,
6034 -- then we can definitely fall through, and we post the error
6035 -- message on the entry call alternative itself.
6037 if No (Statements (ECA)) then
6040 -- Else check statement sequence and else part
6043 Check_Statement_Sequence (Statements (ECA));
6044 Check_Statement_Sequence (Else_Statements (Last_Stm));
6050 -- If we fall through, issue appropriate message
6054 -- Kill warning if last statement is a raise exception call,
6055 -- or a pragma Assert (False). Note that with assertions enabled,
6056 -- such a pragma has been converted into a raise exception call
6057 -- already, so the Assert_False is for the assertions off case.
6059 if not Raise_Exception_Call and then not Assert_False then
6061 -- In GNATprove mode, it is an error to have a missing return
6063 Error_Msg_Warn := SPARK_Mode /= On;
6065 -- Issue error message or warning
6068 ("RETURN statement missing following this statement<<!",
6071 ("\Program_Error ]<<!", Last_Stm);
6074 -- Note: we set Err even though we have not issued a warning
6075 -- because we still have a case of a missing return. This is
6076 -- an extremely marginal case, probably will never be noticed
6077 -- but we might as well get it right.
6081 -- Otherwise we have the case of a procedure marked No_Return
6084 if not Raise_Exception_Call then
6085 if GNATprove_Mode then
6087 ("implied return after this statement "
6088 & "would have raised Program_Error", Last_Stm);
6091 ("implied return after this statement "
6092 & "will raise Program_Error??", Last_Stm);
6095 Error_Msg_Warn := SPARK_Mode /= On;
6097 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6101 RE : constant Node_Id :=
6102 Make_Raise_Program_Error (Sloc (Last_Stm),
6103 Reason => PE_Implicit_Return);
6105 Insert_After (Last_Stm, RE);
6109 end Check_Statement_Sequence;
6111 -- Start of processing for Check_Returns
6115 Check_Statement_Sequence (Statements (HSS));
6117 if Present (Exception_Handlers (HSS)) then
6118 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6119 while Present (Handler) loop
6120 Check_Statement_Sequence (Statements (Handler));
6121 Next_Non_Pragma (Handler);
6126 ----------------------------
6127 -- Check_Subprogram_Order --
6128 ----------------------------
6130 procedure Check_Subprogram_Order (N : Node_Id) is
6132 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6133 -- This is used to check if S1 > S2 in the sense required by this test,
6134 -- for example nameab < namec, but name2 < name10.
6136 -----------------------------
6137 -- Subprogram_Name_Greater --
6138 -----------------------------
6140 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6145 -- Deal with special case where names are identical except for a
6146 -- numerical suffix. These are handled specially, taking the numeric
6147 -- ordering from the suffix into account.
6150 while S1 (L1) in '0' .. '9' loop
6155 while S2 (L2) in '0' .. '9' loop
6159 -- If non-numeric parts non-equal, do straight compare
6161 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6164 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6165 -- that a missing suffix is treated as numeric zero in this test.
6169 while L1 < S1'Last loop
6171 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6175 while L2 < S2'Last loop
6177 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6182 end Subprogram_Name_Greater;
6184 -- Start of processing for Check_Subprogram_Order
6187 -- Check body in alpha order if this is option
6190 and then Style_Check_Order_Subprograms
6191 and then Nkind (N) = N_Subprogram_Body
6192 and then Comes_From_Source (N)
6193 and then In_Extended_Main_Source_Unit (N)
6197 renames Scope_Stack.Table
6198 (Scope_Stack.Last).Last_Subprogram_Name;
6200 Body_Id : constant Entity_Id :=
6201 Defining_Entity (Specification (N));
6204 Get_Decoded_Name_String (Chars (Body_Id));
6207 if Subprogram_Name_Greater
6208 (LSN.all, Name_Buffer (1 .. Name_Len))
6210 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6216 LSN := new String'(Name_Buffer (1 .. Name_Len));
6219 end Check_Subprogram_Order;
6221 ------------------------------
6222 -- Check_Subtype_Conformant --
6223 ------------------------------
6225 procedure Check_Subtype_Conformant
6226 (New_Id : Entity_Id;
6228 Err_Loc : Node_Id := Empty;
6229 Skip_Controlling_Formals : Boolean := False;
6230 Get_Inst : Boolean := False)
6233 pragma Warnings (Off, Result);
6236 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6237 Skip_Controlling_Formals => Skip_Controlling_Formals,
6238 Get_Inst => Get_Inst);
6239 end Check_Subtype_Conformant;
6241 ---------------------------
6242 -- Check_Type_Conformant --
6243 ---------------------------
6245 procedure Check_Type_Conformant
6246 (New_Id : Entity_Id;
6248 Err_Loc : Node_Id := Empty)
6251 pragma Warnings (Off, Result);
6254 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6255 end Check_Type_Conformant;
6257 ---------------------------
6258 -- Can_Override_Operator --
6259 ---------------------------
6261 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6265 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6269 Typ := Base_Type (Etype (First_Formal (Subp)));
6271 -- Check explicitly that the operation is a primitive of the type
6273 return Operator_Matches_Spec (Subp, Subp)
6274 and then not Is_Generic_Type (Typ)
6275 and then Scope (Subp) = Scope (Typ)
6276 and then not Is_Class_Wide_Type (Typ);
6278 end Can_Override_Operator;
6280 ----------------------
6281 -- Conforming_Types --
6282 ----------------------
6284 function Conforming_Types
6287 Ctype : Conformance_Type;
6288 Get_Inst : Boolean := False) return Boolean
6290 Type_1 : Entity_Id := T1;
6291 Type_2 : Entity_Id := T2;
6292 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6294 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6295 -- If neither T1 nor T2 are generic actual types, or if they are in
6296 -- different scopes (e.g. parent and child instances), then verify that
6297 -- the base types are equal. Otherwise T1 and T2 must be on the same
6298 -- subtype chain. The whole purpose of this procedure is to prevent
6299 -- spurious ambiguities in an instantiation that may arise if two
6300 -- distinct generic types are instantiated with the same actual.
6302 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6303 -- An access parameter can designate an incomplete type. If the
6304 -- incomplete type is the limited view of a type from a limited_
6305 -- with_clause, check whether the non-limited view is available. If
6306 -- it is a (non-limited) incomplete type, get the full view.
6308 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6309 -- Returns True if and only if either T1 denotes a limited view of T2
6310 -- or T2 denotes a limited view of T1. This can arise when the limited
6311 -- with view of a type is used in a subprogram declaration and the
6312 -- subprogram body is in the scope of a regular with clause for the
6313 -- same unit. In such a case, the two type entities can be considered
6314 -- identical for purposes of conformance checking.
6316 ----------------------
6317 -- Base_Types_Match --
6318 ----------------------
6320 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6321 BT1 : constant Entity_Id := Base_Type (T1);
6322 BT2 : constant Entity_Id := Base_Type (T2);
6328 elsif BT1 = BT2 then
6330 -- The following is too permissive. A more precise test should
6331 -- check that the generic actual is an ancestor subtype of the
6334 -- See code in Find_Corresponding_Spec that applies an additional
6335 -- filter to handle accidental amiguities in instances.
6337 return not Is_Generic_Actual_Type (T1)
6338 or else not Is_Generic_Actual_Type (T2)
6339 or else Scope (T1) /= Scope (T2);
6341 -- If T2 is a generic actual type it is declared as the subtype of
6342 -- the actual. If that actual is itself a subtype we need to use its
6343 -- own base type to check for compatibility.
6345 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6348 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6354 end Base_Types_Match;
6356 --------------------------
6357 -- Find_Designated_Type --
6358 --------------------------
6360 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6364 Desig := Directly_Designated_Type (T);
6366 if Ekind (Desig) = E_Incomplete_Type then
6368 -- If regular incomplete type, get full view if available
6370 if Present (Full_View (Desig)) then
6371 Desig := Full_View (Desig);
6373 -- If limited view of a type, get non-limited view if available,
6374 -- and check again for a regular incomplete type.
6376 elsif Present (Non_Limited_View (Desig)) then
6377 Desig := Get_Full_View (Non_Limited_View (Desig));
6382 end Find_Designated_Type;
6384 -------------------------------
6385 -- Matches_Limited_With_View --
6386 -------------------------------
6388 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6390 -- In some cases a type imported through a limited_with clause, and
6391 -- its nonlimited view are both visible, for example in an anonymous
6392 -- access-to-class-wide type in a formal, or when building the body
6393 -- for a subprogram renaming after the subprogram has been frozen.
6394 -- In these cases Both entities designate the same type. In addition,
6395 -- if one of them is an actual in an instance, it may be a subtype of
6396 -- the non-limited view of the other.
6398 if From_Limited_With (T1)
6399 and then (T2 = Available_View (T1)
6400 or else Is_Subtype_Of (T2, Available_View (T1)))
6404 elsif From_Limited_With (T2)
6405 and then (T1 = Available_View (T2)
6406 or else Is_Subtype_Of (T1, Available_View (T2)))
6410 elsif From_Limited_With (T1)
6411 and then From_Limited_With (T2)
6412 and then Available_View (T1) = Available_View (T2)
6419 end Matches_Limited_With_View;
6421 -- Start of processing for Conforming_Types
6424 -- The context is an instance association for a formal access-to-
6425 -- subprogram type; the formal parameter types require mapping because
6426 -- they may denote other formal parameters of the generic unit.
6429 Type_1 := Get_Instance_Of (T1);
6430 Type_2 := Get_Instance_Of (T2);
6433 -- If one of the types is a view of the other introduced by a limited
6434 -- with clause, treat these as conforming for all purposes.
6436 if Matches_Limited_With_View (T1, T2) then
6439 elsif Base_Types_Match (Type_1, Type_2) then
6440 return Ctype <= Mode_Conformant
6441 or else Subtypes_Statically_Match (Type_1, Type_2);
6443 elsif Is_Incomplete_Or_Private_Type (Type_1)
6444 and then Present (Full_View (Type_1))
6445 and then Base_Types_Match (Full_View (Type_1), Type_2)
6447 return Ctype <= Mode_Conformant
6448 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6450 elsif Ekind (Type_2) = E_Incomplete_Type
6451 and then Present (Full_View (Type_2))
6452 and then Base_Types_Match (Type_1, Full_View (Type_2))
6454 return Ctype <= Mode_Conformant
6455 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6457 elsif Is_Private_Type (Type_2)
6458 and then In_Instance
6459 and then Present (Full_View (Type_2))
6460 and then Base_Types_Match (Type_1, Full_View (Type_2))
6462 return Ctype <= Mode_Conformant
6463 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6465 -- In Ada 2012, incomplete types (including limited views) can appear
6466 -- as actuals in instantiations.
6468 elsif Is_Incomplete_Type (Type_1)
6469 and then Is_Incomplete_Type (Type_2)
6470 and then (Used_As_Generic_Actual (Type_1)
6471 or else Used_As_Generic_Actual (Type_2))
6476 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6477 -- treated recursively because they carry a signature. As far as
6478 -- conformance is concerned, convention plays no role, and either
6479 -- or both could be access to protected subprograms.
6481 Are_Anonymous_Access_To_Subprogram_Types :=
6482 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6483 E_Anonymous_Access_Protected_Subprogram_Type)
6485 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6486 E_Anonymous_Access_Protected_Subprogram_Type);
6488 -- Test anonymous access type case. For this case, static subtype
6489 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6490 -- the base types because we may have built internal subtype entities
6491 -- to handle null-excluding types (see Process_Formals).
6493 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6495 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6497 -- Ada 2005 (AI-254)
6499 or else Are_Anonymous_Access_To_Subprogram_Types
6502 Desig_1 : Entity_Id;
6503 Desig_2 : Entity_Id;
6506 -- In Ada 2005, access constant indicators must match for
6507 -- subtype conformance.
6509 if Ada_Version >= Ada_2005
6510 and then Ctype >= Subtype_Conformant
6512 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6517 Desig_1 := Find_Designated_Type (Type_1);
6518 Desig_2 := Find_Designated_Type (Type_2);
6520 -- If the context is an instance association for a formal
6521 -- access-to-subprogram type; formal access parameter designated
6522 -- types require mapping because they may denote other formal
6523 -- parameters of the generic unit.
6526 Desig_1 := Get_Instance_Of (Desig_1);
6527 Desig_2 := Get_Instance_Of (Desig_2);
6530 -- It is possible for a Class_Wide_Type to be introduced for an
6531 -- incomplete type, in which case there is a separate class_ wide
6532 -- type for the full view. The types conform if their Etypes
6533 -- conform, i.e. one may be the full view of the other. This can
6534 -- only happen in the context of an access parameter, other uses
6535 -- of an incomplete Class_Wide_Type are illegal.
6537 if Is_Class_Wide_Type (Desig_1)
6539 Is_Class_Wide_Type (Desig_2)
6543 (Etype (Base_Type (Desig_1)),
6544 Etype (Base_Type (Desig_2)), Ctype);
6546 elsif Are_Anonymous_Access_To_Subprogram_Types then
6547 if Ada_Version < Ada_2005 then
6548 return Ctype = Type_Conformant
6550 Subtypes_Statically_Match (Desig_1, Desig_2);
6552 -- We must check the conformance of the signatures themselves
6556 Conformant : Boolean;
6559 (Desig_1, Desig_2, Ctype, False, Conformant);
6564 -- A limited view of an actual matches the corresponding
6565 -- incomplete formal.
6567 elsif Ekind (Desig_2) = E_Incomplete_Subtype
6568 and then From_Limited_With (Desig_2)
6569 and then Used_As_Generic_Actual (Etype (Desig_2))
6574 return Base_Type (Desig_1) = Base_Type (Desig_2)
6575 and then (Ctype = Type_Conformant
6577 Subtypes_Statically_Match (Desig_1, Desig_2));
6581 -- Otherwise definitely no match
6584 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6585 and then Is_Access_Type (Type_2))
6586 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6587 and then Is_Access_Type (Type_1)))
6590 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6592 May_Hide_Profile := True;
6597 end Conforming_Types;
6599 --------------------------
6600 -- Create_Extra_Formals --
6601 --------------------------
6603 procedure Create_Extra_Formals (E : Entity_Id) is
6605 First_Extra : Entity_Id := Empty;
6606 Last_Extra : Entity_Id;
6607 Formal_Type : Entity_Id;
6608 P_Formal : Entity_Id := Empty;
6610 function Add_Extra_Formal
6611 (Assoc_Entity : Entity_Id;
6614 Suffix : String) return Entity_Id;
6615 -- Add an extra formal to the current list of formals and extra formals.
6616 -- The extra formal is added to the end of the list of extra formals,
6617 -- and also returned as the result. These formals are always of mode IN.
6618 -- The new formal has the type Typ, is declared in Scope, and its name
6619 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6620 -- The following suffixes are currently used. They should not be changed
6621 -- without coordinating with CodePeer, which makes use of these to
6622 -- provide better messages.
6624 -- O denotes the Constrained bit.
6625 -- L denotes the accessibility level.
6626 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6627 -- the full list in exp_ch6.BIP_Formal_Kind.
6629 ----------------------
6630 -- Add_Extra_Formal --
6631 ----------------------
6633 function Add_Extra_Formal
6634 (Assoc_Entity : Entity_Id;
6637 Suffix : String) return Entity_Id
6639 EF : constant Entity_Id :=
6640 Make_Defining_Identifier (Sloc (Assoc_Entity),
6641 Chars => New_External_Name (Chars (Assoc_Entity),
6645 -- A little optimization. Never generate an extra formal for the
6646 -- _init operand of an initialization procedure, since it could
6649 if Chars (Formal) = Name_uInit then
6653 Set_Ekind (EF, E_In_Parameter);
6654 Set_Actual_Subtype (EF, Typ);
6655 Set_Etype (EF, Typ);
6656 Set_Scope (EF, Scope);
6657 Set_Mechanism (EF, Default_Mechanism);
6658 Set_Formal_Validity (EF);
6660 if No (First_Extra) then
6662 Set_Extra_Formals (Scope, First_Extra);
6665 if Present (Last_Extra) then
6666 Set_Extra_Formal (Last_Extra, EF);
6672 end Add_Extra_Formal;
6674 -- Start of processing for Create_Extra_Formals
6677 -- We never generate extra formals if expansion is not active because we
6678 -- don't need them unless we are generating code.
6680 if not Expander_Active then
6684 -- No need to generate extra formals in interface thunks whose target
6685 -- primitive has no extra formals.
6687 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6691 -- If this is a derived subprogram then the subtypes of the parent
6692 -- subprogram's formal parameters will be used to determine the need
6693 -- for extra formals.
6695 if Is_Overloadable (E) and then Present (Alias (E)) then
6696 P_Formal := First_Formal (Alias (E));
6699 Last_Extra := Empty;
6700 Formal := First_Formal (E);
6701 while Present (Formal) loop
6702 Last_Extra := Formal;
6703 Next_Formal (Formal);
6706 -- If Extra_Formals were already created, don't do it again. This
6707 -- situation may arise for subprogram types created as part of
6708 -- dispatching calls (see Expand_Dispatching_Call)
6710 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6714 -- If the subprogram is a predefined dispatching subprogram then don't
6715 -- generate any extra constrained or accessibility level formals. In
6716 -- general we suppress these for internal subprograms (by not calling
6717 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6718 -- generated stream attributes do get passed through because extra
6719 -- build-in-place formals are needed in some cases (limited 'Input).
6721 if Is_Predefined_Internal_Operation (E) then
6722 goto Test_For_Func_Result_Extras;
6725 Formal := First_Formal (E);
6726 while Present (Formal) loop
6728 -- Create extra formal for supporting the attribute 'Constrained.
6729 -- The case of a private type view without discriminants also
6730 -- requires the extra formal if the underlying type has defaulted
6733 if Ekind (Formal) /= E_In_Parameter then
6734 if Present (P_Formal) then
6735 Formal_Type := Etype (P_Formal);
6737 Formal_Type := Etype (Formal);
6740 -- Do not produce extra formals for Unchecked_Union parameters.
6741 -- Jump directly to the end of the loop.
6743 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6744 goto Skip_Extra_Formal_Generation;
6747 if not Has_Discriminants (Formal_Type)
6748 and then Ekind (Formal_Type) in Private_Kind
6749 and then Present (Underlying_Type (Formal_Type))
6751 Formal_Type := Underlying_Type (Formal_Type);
6754 -- Suppress the extra formal if formal's subtype is constrained or
6755 -- indefinite, or we're compiling for Ada 2012 and the underlying
6756 -- type is tagged and limited. In Ada 2012, a limited tagged type
6757 -- can have defaulted discriminants, but 'Constrained is required
6758 -- to return True, so the formal is never needed (see AI05-0214).
6759 -- Note that this ensures consistency of calling sequences for
6760 -- dispatching operations when some types in a class have defaults
6761 -- on discriminants and others do not (and requiring the extra
6762 -- formal would introduce distributed overhead).
6764 -- If the type does not have a completion yet, treat as prior to
6765 -- Ada 2012 for consistency.
6767 if Has_Discriminants (Formal_Type)
6768 and then not Is_Constrained (Formal_Type)
6769 and then Is_Definite_Subtype (Formal_Type)
6770 and then (Ada_Version < Ada_2012
6771 or else No (Underlying_Type (Formal_Type))
6773 (Is_Limited_Type (Formal_Type)
6776 (Underlying_Type (Formal_Type)))))
6778 Set_Extra_Constrained
6779 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6783 -- Create extra formal for supporting accessibility checking. This
6784 -- is done for both anonymous access formals and formals of named
6785 -- access types that are marked as controlling formals. The latter
6786 -- case can occur when Expand_Dispatching_Call creates a subprogram
6787 -- type and substitutes the types of access-to-class-wide actuals
6788 -- for the anonymous access-to-specific-type of controlling formals.
6789 -- Base_Type is applied because in cases where there is a null
6790 -- exclusion the formal may have an access subtype.
6792 -- This is suppressed if we specifically suppress accessibility
6793 -- checks at the package level for either the subprogram, or the
6794 -- package in which it resides. However, we do not suppress it
6795 -- simply if the scope has accessibility checks suppressed, since
6796 -- this could cause trouble when clients are compiled with a
6797 -- different suppression setting. The explicit checks at the
6798 -- package level are safe from this point of view.
6800 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6801 or else (Is_Controlling_Formal (Formal)
6802 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6804 (Explicit_Suppress (E, Accessibility_Check)
6806 Explicit_Suppress (Scope (E), Accessibility_Check))
6809 or else Present (Extra_Accessibility (P_Formal)))
6811 Set_Extra_Accessibility
6812 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6815 -- This label is required when skipping extra formal generation for
6816 -- Unchecked_Union parameters.
6818 <<Skip_Extra_Formal_Generation>>
6820 if Present (P_Formal) then
6821 Next_Formal (P_Formal);
6824 Next_Formal (Formal);
6827 <<Test_For_Func_Result_Extras>>
6829 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6830 -- function call is ... determined by the point of call ...".
6832 if Needs_Result_Accessibility_Level (E) then
6833 Set_Extra_Accessibility_Of_Result
6834 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6837 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6838 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6840 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6842 Result_Subt : constant Entity_Id := Etype (E);
6843 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
6844 Formal_Typ : Entity_Id;
6846 Discard : Entity_Id;
6847 pragma Warnings (Off, Discard);
6850 -- In the case of functions with unconstrained result subtypes,
6851 -- add a 4-state formal indicating whether the return object is
6852 -- allocated by the caller (1), or should be allocated by the
6853 -- callee on the secondary stack (2), in the global heap (3), or
6854 -- in a user-defined storage pool (4). For the moment we just use
6855 -- Natural for the type of this formal. Note that this formal
6856 -- isn't usually needed in the case where the result subtype is
6857 -- constrained, but it is needed when the function has a tagged
6858 -- result, because generally such functions can be called in a
6859 -- dispatching context and such calls must be handled like calls
6860 -- to a class-wide function.
6862 if Needs_BIP_Alloc_Form (E) then
6865 (E, Standard_Natural,
6866 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6868 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
6869 -- use a user-defined pool. This formal is not added on
6870 -- ZFP as those targets do not support pools.
6872 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
6875 (E, RTE (RE_Root_Storage_Pool_Ptr),
6876 E, BIP_Formal_Suffix (BIP_Storage_Pool));
6880 -- In the case of functions whose result type needs finalization,
6881 -- add an extra formal which represents the finalization master.
6883 if Needs_BIP_Finalization_Master (E) then
6886 (E, RTE (RE_Finalization_Master_Ptr),
6887 E, BIP_Formal_Suffix (BIP_Finalization_Master));
6890 -- When the result type contains tasks, add two extra formals: the
6891 -- master of the tasks to be created, and the caller's activation
6894 if Has_Task (Full_Subt) then
6897 (E, RTE (RE_Master_Id),
6898 E, BIP_Formal_Suffix (BIP_Task_Master));
6901 (E, RTE (RE_Activation_Chain_Access),
6902 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6905 -- All build-in-place functions get an extra formal that will be
6906 -- passed the address of the return object within the caller.
6909 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
6911 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
6912 Set_Etype (Formal_Typ, Formal_Typ);
6913 Set_Depends_On_Private
6914 (Formal_Typ, Has_Private_Component (Formal_Typ));
6915 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
6916 Set_Is_Access_Constant (Formal_Typ, False);
6918 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6919 -- the designated type comes from the limited view (for back-end
6922 Set_From_Limited_With
6923 (Formal_Typ, From_Limited_With (Result_Subt));
6925 Layout_Type (Formal_Typ);
6929 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
6932 end Create_Extra_Formals;
6934 -----------------------------
6935 -- Enter_Overloaded_Entity --
6936 -----------------------------
6938 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6939 E : Entity_Id := Current_Entity_In_Scope (S);
6940 C_E : Entity_Id := Current_Entity (S);
6944 Set_Has_Homonym (E);
6945 Set_Has_Homonym (S);
6948 Set_Is_Immediately_Visible (S);
6949 Set_Scope (S, Current_Scope);
6951 -- Chain new entity if front of homonym in current scope, so that
6952 -- homonyms are contiguous.
6954 if Present (E) and then E /= C_E then
6955 while Homonym (C_E) /= E loop
6956 C_E := Homonym (C_E);
6959 Set_Homonym (C_E, S);
6963 Set_Current_Entity (S);
6968 if Is_Inherited_Operation (S) then
6969 Append_Inherited_Subprogram (S);
6971 Append_Entity (S, Current_Scope);
6974 Set_Public_Status (S);
6976 if Debug_Flag_E then
6977 Write_Str ("New overloaded entity chain: ");
6978 Write_Name (Chars (S));
6981 while Present (E) loop
6982 Write_Str (" "); Write_Int (Int (E));
6989 -- Generate warning for hiding
6992 and then Comes_From_Source (S)
6993 and then In_Extended_Main_Source_Unit (S)
7000 -- Warn unless genuine overloading. Do not emit warning on
7001 -- hiding predefined operators in Standard (these are either an
7002 -- (artifact of our implicit declarations, or simple noise) but
7003 -- keep warning on a operator defined on a local subtype, because
7004 -- of the real danger that different operators may be applied in
7005 -- various parts of the program.
7007 -- Note that if E and S have the same scope, there is never any
7008 -- hiding. Either the two conflict, and the program is illegal,
7009 -- or S is overriding an implicit inherited subprogram.
7011 if Scope (E) /= Scope (S)
7012 and then (not Is_Overloadable (E)
7013 or else Subtype_Conformant (E, S))
7014 and then (Is_Immediately_Visible (E)
7016 Is_Potentially_Use_Visible (S))
7018 if Scope (E) /= Standard_Standard then
7019 Error_Msg_Sloc := Sloc (E);
7020 Error_Msg_N ("declaration of & hides one #?h?", S);
7022 elsif Nkind (S) = N_Defining_Operator_Symbol
7024 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7027 ("declaration of & hides predefined operator?h?", S);
7032 end Enter_Overloaded_Entity;
7034 -----------------------------
7035 -- Check_Untagged_Equality --
7036 -----------------------------
7038 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7039 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7040 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7044 -- This check applies only if we have a subprogram declaration with an
7045 -- untagged record type.
7047 if Nkind (Decl) /= N_Subprogram_Declaration
7048 or else not Is_Record_Type (Typ)
7049 or else Is_Tagged_Type (Typ)
7054 -- In Ada 2012 case, we will output errors or warnings depending on
7055 -- the setting of debug flag -gnatd.E.
7057 if Ada_Version >= Ada_2012 then
7058 Error_Msg_Warn := Debug_Flag_Dot_EE;
7060 -- In earlier versions of Ada, nothing to do unless we are warning on
7061 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7064 if not Warn_On_Ada_2012_Compatibility then
7069 -- Cases where the type has already been frozen
7071 if Is_Frozen (Typ) then
7073 -- If the type is not declared in a package, or if we are in the body
7074 -- of the package or in some other scope, the new operation is not
7075 -- primitive, and therefore legal, though suspicious. Should we
7076 -- generate a warning in this case ???
7078 if Ekind (Scope (Typ)) /= E_Package
7079 or else Scope (Typ) /= Current_Scope
7083 -- If the type is a generic actual (sub)type, the operation is not
7084 -- primitive either because the base type is declared elsewhere.
7086 elsif Is_Generic_Actual_Type (Typ) then
7089 -- Here we have a definite error of declaration after freezing
7092 if Ada_Version >= Ada_2012 then
7094 ("equality operator must be declared before type & is "
7095 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7097 -- In Ada 2012 mode with error turned to warning, output one
7098 -- more warning to warn that the equality operation may not
7099 -- compose. This is the consequence of ignoring the error.
7101 if Error_Msg_Warn then
7102 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7107 ("equality operator must be declared before type& is "
7108 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7111 -- If we are in the package body, we could just move the
7112 -- declaration to the package spec, so add a message saying that.
7114 if In_Package_Body (Scope (Typ)) then
7115 if Ada_Version >= Ada_2012 then
7117 ("\move declaration to package spec<<", Eq_Op);
7120 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7123 -- Otherwise try to find the freezing point
7126 Obj_Decl := Next (Parent (Typ));
7127 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7128 if Nkind (Obj_Decl) = N_Object_Declaration
7129 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7131 -- Freezing point, output warnings
7133 if Ada_Version >= Ada_2012 then
7135 ("type& is frozen by declaration??", Obj_Decl, Typ);
7137 ("\an equality operator cannot be declared after "
7142 ("type& is frozen by declaration (Ada 2012)?y?",
7145 ("\an equality operator cannot be declared after "
7146 & "this point (Ada 2012)?y?",
7158 -- Here if type is not frozen yet. It is illegal to have a primitive
7159 -- equality declared in the private part if the type is visible.
7161 elsif not In_Same_List (Parent (Typ), Decl)
7162 and then not Is_Limited_Type (Typ)
7164 -- Shouldn't we give an RM reference here???
7166 if Ada_Version >= Ada_2012 then
7168 ("equality operator appears too late<<", Eq_Op);
7171 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7174 -- No error detected
7179 end Check_Untagged_Equality;
7181 -----------------------------
7182 -- Find_Corresponding_Spec --
7183 -----------------------------
7185 function Find_Corresponding_Spec
7187 Post_Error : Boolean := True) return Entity_Id
7189 Spec : constant Node_Id := Specification (N);
7190 Designator : constant Entity_Id := Defining_Entity (Spec);
7194 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7195 -- Even if fully conformant, a body may depend on a generic actual when
7196 -- the spec does not, or vice versa, in which case they were distinct
7197 -- entities in the generic.
7199 -------------------------------
7200 -- Different_Generic_Profile --
7201 -------------------------------
7203 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7206 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7207 -- Check that the types of corresponding formals have the same
7208 -- generic actual if any. We have to account for subtypes of a
7209 -- generic formal, declared between a spec and a body, which may
7210 -- appear distinct in an instance but matched in the generic, and
7211 -- the subtype may be used either in the spec or the body of the
7212 -- subprogram being checked.
7214 -------------------------
7215 -- Same_Generic_Actual --
7216 -------------------------
7218 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7220 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7221 -- Predicate to check whether S1 is a subtype of S2 in the source
7224 -------------------------
7225 -- Is_Declared_Subtype --
7226 -------------------------
7228 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7230 return Comes_From_Source (Parent (S1))
7231 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7232 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7233 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7234 end Is_Declared_Subtype;
7236 -- Start of processing for Same_Generic_Actual
7239 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7240 or else Is_Declared_Subtype (T1, T2)
7241 or else Is_Declared_Subtype (T2, T1);
7242 end Same_Generic_Actual;
7244 -- Start of processing for Different_Generic_Profile
7247 if not In_Instance then
7250 elsif Ekind (E) = E_Function
7251 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7256 F1 := First_Formal (Designator);
7257 F2 := First_Formal (E);
7258 while Present (F1) loop
7259 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7268 end Different_Generic_Profile;
7270 -- Start of processing for Find_Corresponding_Spec
7273 E := Current_Entity (Designator);
7274 while Present (E) loop
7276 -- We are looking for a matching spec. It must have the same scope,
7277 -- and the same name, and either be type conformant, or be the case
7278 -- of a library procedure spec and its body (which belong to one
7279 -- another regardless of whether they are type conformant or not).
7281 if Scope (E) = Current_Scope then
7282 if Current_Scope = Standard_Standard
7283 or else (Ekind (E) = Ekind (Designator)
7284 and then Type_Conformant (E, Designator))
7286 -- Within an instantiation, we know that spec and body are
7287 -- subtype conformant, because they were subtype conformant in
7288 -- the generic. We choose the subtype-conformant entity here as
7289 -- well, to resolve spurious ambiguities in the instance that
7290 -- were not present in the generic (i.e. when two different
7291 -- types are given the same actual). If we are looking for a
7292 -- spec to match a body, full conformance is expected.
7296 -- Inherit the convention and "ghostness" of the matching
7297 -- spec to ensure proper full and subtype conformance.
7299 Set_Convention (Designator, Convention (E));
7301 if Is_Ghost_Entity (E) then
7302 Set_Is_Ghost_Entity (Designator);
7305 -- Skip past subprogram bodies and subprogram renamings that
7306 -- may appear to have a matching spec, but that aren't fully
7307 -- conformant with it. That can occur in cases where an
7308 -- actual type causes unrelated homographs in the instance.
7310 if Nkind_In (N, N_Subprogram_Body,
7311 N_Subprogram_Renaming_Declaration)
7312 and then Present (Homonym (E))
7313 and then not Fully_Conformant (Designator, E)
7317 elsif not Subtype_Conformant (Designator, E) then
7320 elsif Different_Generic_Profile (E) then
7325 -- Ada 2012 (AI05-0165): For internally generated bodies of
7326 -- null procedures locate the internally generated spec. We
7327 -- enforce mode conformance since a tagged type may inherit
7328 -- from interfaces several null primitives which differ only
7329 -- in the mode of the formals.
7331 if not (Comes_From_Source (E))
7332 and then Is_Null_Procedure (E)
7333 and then not Mode_Conformant (Designator, E)
7337 -- For null procedures coming from source that are completions,
7338 -- analysis of the generated body will establish the link.
7340 elsif Comes_From_Source (E)
7341 and then Nkind (Spec) = N_Procedure_Specification
7342 and then Null_Present (Spec)
7346 -- Expression functions can be completions, but cannot be
7347 -- completed by an explicit body.
7349 elsif Comes_From_Source (E)
7350 and then Comes_From_Source (N)
7351 and then Nkind (N) = N_Subprogram_Body
7352 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
7353 N_Expression_Function
7355 Error_Msg_Sloc := Sloc (E);
7356 Error_Msg_N ("body conflicts with expression function#", N);
7359 elsif not Has_Completion (E) then
7360 if Nkind (N) /= N_Subprogram_Body_Stub then
7361 Set_Corresponding_Spec (N, E);
7364 Set_Has_Completion (E);
7367 elsif Nkind (Parent (N)) = N_Subunit then
7369 -- If this is the proper body of a subunit, the completion
7370 -- flag is set when analyzing the stub.
7374 -- If E is an internal function with a controlling result that
7375 -- was created for an operation inherited by a null extension,
7376 -- it may be overridden by a body without a previous spec (one
7377 -- more reason why these should be shunned). In that case we
7378 -- remove the generated body if present, because the current
7379 -- one is the explicit overriding.
7381 elsif Ekind (E) = E_Function
7382 and then Ada_Version >= Ada_2005
7383 and then not Comes_From_Source (E)
7384 and then Has_Controlling_Result (E)
7385 and then Is_Null_Extension (Etype (E))
7386 and then Comes_From_Source (Spec)
7388 Set_Has_Completion (E, False);
7391 and then Nkind (Parent (E)) = N_Function_Specification
7394 (Unit_Declaration_Node
7395 (Corresponding_Body (Unit_Declaration_Node (E))));
7399 -- If expansion is disabled, or if the wrapper function has
7400 -- not been generated yet, this a late body overriding an
7401 -- inherited operation, or it is an overriding by some other
7402 -- declaration before the controlling result is frozen. In
7403 -- either case this is a declaration of a new entity.
7409 -- If the body already exists, then this is an error unless
7410 -- the previous declaration is the implicit declaration of a
7411 -- derived subprogram. It is also legal for an instance to
7412 -- contain type conformant overloadable declarations (but the
7413 -- generic declaration may not), per 8.3(26/2).
7415 elsif No (Alias (E))
7416 and then not Is_Intrinsic_Subprogram (E)
7417 and then not In_Instance
7420 Error_Msg_Sloc := Sloc (E);
7422 if Is_Imported (E) then
7424 ("body not allowed for imported subprogram & declared#",
7427 Error_Msg_NE ("duplicate body for & declared#", N, E);
7431 -- Child units cannot be overloaded, so a conformance mismatch
7432 -- between body and a previous spec is an error.
7434 elsif Is_Child_Unit (E)
7436 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7438 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7443 ("body of child unit does not match previous declaration", N);
7451 -- On exit, we know that no previous declaration of subprogram exists
7454 end Find_Corresponding_Spec;
7456 ----------------------
7457 -- Fully_Conformant --
7458 ----------------------
7460 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7463 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7465 end Fully_Conformant;
7467 ----------------------------------
7468 -- Fully_Conformant_Expressions --
7469 ----------------------------------
7471 function Fully_Conformant_Expressions
7472 (Given_E1 : Node_Id;
7473 Given_E2 : Node_Id) return Boolean
7475 E1 : constant Node_Id := Original_Node (Given_E1);
7476 E2 : constant Node_Id := Original_Node (Given_E2);
7477 -- We always test conformance on original nodes, since it is possible
7478 -- for analysis and/or expansion to make things look as though they
7479 -- conform when they do not, e.g. by converting 1+2 into 3.
7481 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7482 renames Fully_Conformant_Expressions;
7484 function FCL (L1, L2 : List_Id) return Boolean;
7485 -- Compare elements of two lists for conformance. Elements have to be
7486 -- conformant, and actuals inserted as default parameters do not match
7487 -- explicit actuals with the same value.
7489 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7490 -- Compare an operator node with a function call
7496 function FCL (L1, L2 : List_Id) return Boolean is
7500 if L1 = No_List then
7506 if L2 = No_List then
7512 -- Compare two lists, skipping rewrite insertions (we want to compare
7513 -- the original trees, not the expanded versions).
7516 if Is_Rewrite_Insertion (N1) then
7518 elsif Is_Rewrite_Insertion (N2) then
7524 elsif not FCE (N1, N2) then
7537 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7538 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7543 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7548 Act := First (Actuals);
7550 if Nkind (Op_Node) in N_Binary_Op then
7551 if not FCE (Left_Opnd (Op_Node), Act) then
7558 return Present (Act)
7559 and then FCE (Right_Opnd (Op_Node), Act)
7560 and then No (Next (Act));
7564 -- Start of processing for Fully_Conformant_Expressions
7567 -- Non-conformant if paren count does not match. Note: if some idiot
7568 -- complains that we don't do this right for more than 3 levels of
7569 -- parentheses, they will be treated with the respect they deserve.
7571 if Paren_Count (E1) /= Paren_Count (E2) then
7574 -- If same entities are referenced, then they are conformant even if
7575 -- they have different forms (RM 8.3.1(19-20)).
7577 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7578 if Present (Entity (E1)) then
7579 return Entity (E1) = Entity (E2)
7580 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7581 and then Ekind (Entity (E1)) = E_Discriminant
7582 and then Ekind (Entity (E2)) = E_In_Parameter);
7584 elsif Nkind (E1) = N_Expanded_Name
7585 and then Nkind (E2) = N_Expanded_Name
7586 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7587 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7589 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7592 -- Identifiers in component associations don't always have
7593 -- entities, but their names must conform.
7595 return Nkind (E1) = N_Identifier
7596 and then Nkind (E2) = N_Identifier
7597 and then Chars (E1) = Chars (E2);
7600 elsif Nkind (E1) = N_Character_Literal
7601 and then Nkind (E2) = N_Expanded_Name
7603 return Nkind (Selector_Name (E2)) = N_Character_Literal
7604 and then Chars (E1) = Chars (Selector_Name (E2));
7606 elsif Nkind (E2) = N_Character_Literal
7607 and then Nkind (E1) = N_Expanded_Name
7609 return Nkind (Selector_Name (E1)) = N_Character_Literal
7610 and then Chars (E2) = Chars (Selector_Name (E1));
7612 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7613 return FCO (E1, E2);
7615 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7616 return FCO (E2, E1);
7618 -- Otherwise we must have the same syntactic entity
7620 elsif Nkind (E1) /= Nkind (E2) then
7623 -- At this point, we specialize by node type
7630 FCL (Expressions (E1), Expressions (E2))
7632 FCL (Component_Associations (E1),
7633 Component_Associations (E2));
7636 if Nkind (Expression (E1)) = N_Qualified_Expression
7638 Nkind (Expression (E2)) = N_Qualified_Expression
7640 return FCE (Expression (E1), Expression (E2));
7642 -- Check that the subtype marks and any constraints
7647 Indic1 : constant Node_Id := Expression (E1);
7648 Indic2 : constant Node_Id := Expression (E2);
7653 if Nkind (Indic1) /= N_Subtype_Indication then
7655 Nkind (Indic2) /= N_Subtype_Indication
7656 and then Entity (Indic1) = Entity (Indic2);
7658 elsif Nkind (Indic2) /= N_Subtype_Indication then
7660 Nkind (Indic1) /= N_Subtype_Indication
7661 and then Entity (Indic1) = Entity (Indic2);
7664 if Entity (Subtype_Mark (Indic1)) /=
7665 Entity (Subtype_Mark (Indic2))
7670 Elt1 := First (Constraints (Constraint (Indic1)));
7671 Elt2 := First (Constraints (Constraint (Indic2)));
7672 while Present (Elt1) and then Present (Elt2) loop
7673 if not FCE (Elt1, Elt2) then
7686 when N_Attribute_Reference =>
7688 Attribute_Name (E1) = Attribute_Name (E2)
7689 and then FCL (Expressions (E1), Expressions (E2));
7693 Entity (E1) = Entity (E2)
7694 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7695 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7697 when N_Short_Circuit | N_Membership_Test =>
7699 FCE (Left_Opnd (E1), Left_Opnd (E2))
7701 FCE (Right_Opnd (E1), Right_Opnd (E2));
7703 when N_Case_Expression =>
7709 if not FCE (Expression (E1), Expression (E2)) then
7713 Alt1 := First (Alternatives (E1));
7714 Alt2 := First (Alternatives (E2));
7716 if Present (Alt1) /= Present (Alt2) then
7718 elsif No (Alt1) then
7722 if not FCE (Expression (Alt1), Expression (Alt2))
7723 or else not FCL (Discrete_Choices (Alt1),
7724 Discrete_Choices (Alt2))
7735 when N_Character_Literal =>
7737 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7739 when N_Component_Association =>
7741 FCL (Choices (E1), Choices (E2))
7743 FCE (Expression (E1), Expression (E2));
7745 when N_Explicit_Dereference =>
7747 FCE (Prefix (E1), Prefix (E2));
7749 when N_Extension_Aggregate =>
7751 FCL (Expressions (E1), Expressions (E2))
7752 and then Null_Record_Present (E1) =
7753 Null_Record_Present (E2)
7754 and then FCL (Component_Associations (E1),
7755 Component_Associations (E2));
7757 when N_Function_Call =>
7759 FCE (Name (E1), Name (E2))
7761 FCL (Parameter_Associations (E1),
7762 Parameter_Associations (E2));
7764 when N_If_Expression =>
7766 FCL (Expressions (E1), Expressions (E2));
7768 when N_Indexed_Component =>
7770 FCE (Prefix (E1), Prefix (E2))
7772 FCL (Expressions (E1), Expressions (E2));
7774 when N_Integer_Literal =>
7775 return (Intval (E1) = Intval (E2));
7780 when N_Operator_Symbol =>
7782 Chars (E1) = Chars (E2);
7784 when N_Others_Choice =>
7787 when N_Parameter_Association =>
7789 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7790 and then FCE (Explicit_Actual_Parameter (E1),
7791 Explicit_Actual_Parameter (E2));
7793 when N_Qualified_Expression =>
7795 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7797 FCE (Expression (E1), Expression (E2));
7799 when N_Quantified_Expression =>
7800 if not FCE (Condition (E1), Condition (E2)) then
7804 if Present (Loop_Parameter_Specification (E1))
7805 and then Present (Loop_Parameter_Specification (E2))
7808 L1 : constant Node_Id :=
7809 Loop_Parameter_Specification (E1);
7810 L2 : constant Node_Id :=
7811 Loop_Parameter_Specification (E2);
7815 Reverse_Present (L1) = Reverse_Present (L2)
7817 FCE (Defining_Identifier (L1),
7818 Defining_Identifier (L2))
7820 FCE (Discrete_Subtype_Definition (L1),
7821 Discrete_Subtype_Definition (L2));
7824 elsif Present (Iterator_Specification (E1))
7825 and then Present (Iterator_Specification (E2))
7828 I1 : constant Node_Id := Iterator_Specification (E1);
7829 I2 : constant Node_Id := Iterator_Specification (E2);
7833 FCE (Defining_Identifier (I1),
7834 Defining_Identifier (I2))
7836 Of_Present (I1) = Of_Present (I2)
7838 Reverse_Present (I1) = Reverse_Present (I2)
7839 and then FCE (Name (I1), Name (I2))
7840 and then FCE (Subtype_Indication (I1),
7841 Subtype_Indication (I2));
7844 -- The quantified expressions used different specifications to
7845 -- walk their respective ranges.
7853 FCE (Low_Bound (E1), Low_Bound (E2))
7855 FCE (High_Bound (E1), High_Bound (E2));
7857 when N_Real_Literal =>
7858 return (Realval (E1) = Realval (E2));
7860 when N_Selected_Component =>
7862 FCE (Prefix (E1), Prefix (E2))
7864 FCE (Selector_Name (E1), Selector_Name (E2));
7868 FCE (Prefix (E1), Prefix (E2))
7870 FCE (Discrete_Range (E1), Discrete_Range (E2));
7872 when N_String_Literal =>
7874 S1 : constant String_Id := Strval (E1);
7875 S2 : constant String_Id := Strval (E2);
7876 L1 : constant Nat := String_Length (S1);
7877 L2 : constant Nat := String_Length (S2);
7884 for J in 1 .. L1 loop
7885 if Get_String_Char (S1, J) /=
7886 Get_String_Char (S2, J)
7896 when N_Type_Conversion =>
7898 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7900 FCE (Expression (E1), Expression (E2));
7904 Entity (E1) = Entity (E2)
7906 FCE (Right_Opnd (E1), Right_Opnd (E2));
7908 when N_Unchecked_Type_Conversion =>
7910 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7912 FCE (Expression (E1), Expression (E2));
7914 -- All other node types cannot appear in this context. Strictly
7915 -- we should raise a fatal internal error. Instead we just ignore
7916 -- the nodes. This means that if anyone makes a mistake in the
7917 -- expander and mucks an expression tree irretrievably, the result
7918 -- will be a failure to detect a (probably very obscure) case
7919 -- of non-conformance, which is better than bombing on some
7920 -- case where two expressions do in fact conform.
7927 end Fully_Conformant_Expressions;
7929 ----------------------------------------
7930 -- Fully_Conformant_Discrete_Subtypes --
7931 ----------------------------------------
7933 function Fully_Conformant_Discrete_Subtypes
7934 (Given_S1 : Node_Id;
7935 Given_S2 : Node_Id) return Boolean
7937 S1 : constant Node_Id := Original_Node (Given_S1);
7938 S2 : constant Node_Id := Original_Node (Given_S2);
7940 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
7941 -- Special-case for a bound given by a discriminant, which in the body
7942 -- is replaced with the discriminal of the enclosing type.
7944 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
7945 -- Check both bounds
7947 -----------------------
7948 -- Conforming_Bounds --
7949 -----------------------
7951 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
7953 if Is_Entity_Name (B1)
7954 and then Is_Entity_Name (B2)
7955 and then Ekind (Entity (B1)) = E_Discriminant
7957 return Chars (B1) = Chars (B2);
7960 return Fully_Conformant_Expressions (B1, B2);
7962 end Conforming_Bounds;
7964 -----------------------
7965 -- Conforming_Ranges --
7966 -----------------------
7968 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
7971 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
7973 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
7974 end Conforming_Ranges;
7976 -- Start of processing for Fully_Conformant_Discrete_Subtypes
7979 if Nkind (S1) /= Nkind (S2) then
7982 elsif Is_Entity_Name (S1) then
7983 return Entity (S1) = Entity (S2);
7985 elsif Nkind (S1) = N_Range then
7986 return Conforming_Ranges (S1, S2);
7988 elsif Nkind (S1) = N_Subtype_Indication then
7990 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
7993 (Range_Expression (Constraint (S1)),
7994 Range_Expression (Constraint (S2)));
7998 end Fully_Conformant_Discrete_Subtypes;
8000 --------------------
8001 -- Install_Entity --
8002 --------------------
8004 procedure Install_Entity (E : Entity_Id) is
8005 Prev : constant Entity_Id := Current_Entity (E);
8007 Set_Is_Immediately_Visible (E);
8008 Set_Current_Entity (E);
8009 Set_Homonym (E, Prev);
8012 ---------------------
8013 -- Install_Formals --
8014 ---------------------
8016 procedure Install_Formals (Id : Entity_Id) is
8019 F := First_Formal (Id);
8020 while Present (F) loop
8024 end Install_Formals;
8026 -----------------------------
8027 -- Is_Interface_Conformant --
8028 -----------------------------
8030 function Is_Interface_Conformant
8031 (Tagged_Type : Entity_Id;
8032 Iface_Prim : Entity_Id;
8033 Prim : Entity_Id) return Boolean
8035 -- The operation may in fact be an inherited (implicit) operation
8036 -- rather than the original interface primitive, so retrieve the
8037 -- ultimate ancestor.
8039 Iface : constant Entity_Id :=
8040 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8041 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8043 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8044 -- Return the controlling formal of Prim
8046 ------------------------
8047 -- Controlling_Formal --
8048 ------------------------
8050 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8054 E := First_Entity (Prim);
8055 while Present (E) loop
8056 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8064 end Controlling_Formal;
8068 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8069 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8071 -- Start of processing for Is_Interface_Conformant
8074 pragma Assert (Is_Subprogram (Iface_Prim)
8075 and then Is_Subprogram (Prim)
8076 and then Is_Dispatching_Operation (Iface_Prim)
8077 and then Is_Dispatching_Operation (Prim));
8079 pragma Assert (Is_Interface (Iface)
8080 or else (Present (Alias (Iface_Prim))
8083 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8085 if Prim = Iface_Prim
8086 or else not Is_Subprogram (Prim)
8087 or else Ekind (Prim) /= Ekind (Iface_Prim)
8088 or else not Is_Dispatching_Operation (Prim)
8089 or else Scope (Prim) /= Scope (Tagged_Type)
8091 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8092 or else not Primitive_Names_Match (Iface_Prim, Prim)
8096 -- The mode of the controlling formals must match
8098 elsif Present (Iface_Ctrl_F)
8099 and then Present (Prim_Ctrl_F)
8100 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8104 -- Case of a procedure, or a function whose result type matches the
8105 -- result type of the interface primitive, or a function that has no
8106 -- controlling result (I or access I).
8108 elsif Ekind (Iface_Prim) = E_Procedure
8109 or else Etype (Prim) = Etype (Iface_Prim)
8110 or else not Has_Controlling_Result (Prim)
8112 return Type_Conformant
8113 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8115 -- Case of a function returning an interface, or an access to one. Check
8116 -- that the return types correspond.
8118 elsif Implements_Interface (Typ, Iface) then
8119 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8121 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8126 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8127 Skip_Controlling_Formals => True);
8133 end Is_Interface_Conformant;
8135 ---------------------------------
8136 -- Is_Non_Overriding_Operation --
8137 ---------------------------------
8139 function Is_Non_Overriding_Operation
8140 (Prev_E : Entity_Id;
8141 New_E : Entity_Id) return Boolean
8145 G_Typ : Entity_Id := Empty;
8147 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8148 -- If F_Type is a derived type associated with a generic actual subtype,
8149 -- then return its Generic_Parent_Type attribute, else return Empty.
8151 function Types_Correspond
8152 (P_Type : Entity_Id;
8153 N_Type : Entity_Id) return Boolean;
8154 -- Returns true if and only if the types (or designated types in the
8155 -- case of anonymous access types) are the same or N_Type is derived
8156 -- directly or indirectly from P_Type.
8158 -----------------------------
8159 -- Get_Generic_Parent_Type --
8160 -----------------------------
8162 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8168 if Is_Derived_Type (F_Typ)
8169 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8171 -- The tree must be traversed to determine the parent subtype in
8172 -- the generic unit, which unfortunately isn't always available
8173 -- via semantic attributes. ??? (Note: The use of Original_Node
8174 -- is needed for cases where a full derived type has been
8177 -- If the parent type is a scalar type, the derivation creates
8178 -- an anonymous base type for it, and the source type is its
8181 if Is_Scalar_Type (F_Typ)
8182 and then not Comes_From_Source (F_Typ)
8186 (Original_Node (Parent (First_Subtype (F_Typ))));
8188 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8190 if Nkind (Defn) = N_Derived_Type_Definition then
8191 Indic := Subtype_Indication (Defn);
8193 if Nkind (Indic) = N_Subtype_Indication then
8194 G_Typ := Entity (Subtype_Mark (Indic));
8196 G_Typ := Entity (Indic);
8199 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8200 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8202 return Generic_Parent_Type (Parent (G_Typ));
8208 end Get_Generic_Parent_Type;
8210 ----------------------
8211 -- Types_Correspond --
8212 ----------------------
8214 function Types_Correspond
8215 (P_Type : Entity_Id;
8216 N_Type : Entity_Id) return Boolean
8218 Prev_Type : Entity_Id := Base_Type (P_Type);
8219 New_Type : Entity_Id := Base_Type (N_Type);
8222 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8223 Prev_Type := Designated_Type (Prev_Type);
8226 if Ekind (New_Type) = E_Anonymous_Access_Type then
8227 New_Type := Designated_Type (New_Type);
8230 if Prev_Type = New_Type then
8233 elsif not Is_Class_Wide_Type (New_Type) then
8234 while Etype (New_Type) /= New_Type loop
8235 New_Type := Etype (New_Type);
8237 if New_Type = Prev_Type then
8243 end Types_Correspond;
8245 -- Start of processing for Is_Non_Overriding_Operation
8248 -- In the case where both operations are implicit derived subprograms
8249 -- then neither overrides the other. This can only occur in certain
8250 -- obscure cases (e.g., derivation from homographs created in a generic
8253 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8256 elsif Ekind (Current_Scope) = E_Package
8257 and then Is_Generic_Instance (Current_Scope)
8258 and then In_Private_Part (Current_Scope)
8259 and then Comes_From_Source (New_E)
8261 -- We examine the formals and result type of the inherited operation,
8262 -- to determine whether their type is derived from (the instance of)
8263 -- a generic type. The first such formal or result type is the one
8266 Formal := First_Formal (Prev_E);
8267 while Present (Formal) loop
8268 F_Typ := Base_Type (Etype (Formal));
8270 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8271 F_Typ := Designated_Type (F_Typ);
8274 G_Typ := Get_Generic_Parent_Type (F_Typ);
8275 exit when Present (G_Typ);
8277 Next_Formal (Formal);
8280 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8281 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8288 -- If the generic type is a private type, then the original operation
8289 -- was not overriding in the generic, because there was no primitive
8290 -- operation to override.
8292 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8293 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8294 N_Formal_Private_Type_Definition
8298 -- The generic parent type is the ancestor of a formal derived
8299 -- type declaration. We need to check whether it has a primitive
8300 -- operation that should be overridden by New_E in the generic.
8304 P_Formal : Entity_Id;
8305 N_Formal : Entity_Id;
8309 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8312 while Present (Prim_Elt) loop
8313 P_Prim := Node (Prim_Elt);
8315 if Chars (P_Prim) = Chars (New_E)
8316 and then Ekind (P_Prim) = Ekind (New_E)
8318 P_Formal := First_Formal (P_Prim);
8319 N_Formal := First_Formal (New_E);
8320 while Present (P_Formal) and then Present (N_Formal) loop
8321 P_Typ := Etype (P_Formal);
8322 N_Typ := Etype (N_Formal);
8324 if not Types_Correspond (P_Typ, N_Typ) then
8328 Next_Entity (P_Formal);
8329 Next_Entity (N_Formal);
8332 -- Found a matching primitive operation belonging to the
8333 -- formal ancestor type, so the new subprogram is
8337 and then No (N_Formal)
8338 and then (Ekind (New_E) /= E_Function
8341 (Etype (P_Prim), Etype (New_E)))
8347 Next_Elmt (Prim_Elt);
8350 -- If no match found, then the new subprogram does not override
8351 -- in the generic (nor in the instance).
8353 -- If the type in question is not abstract, and the subprogram
8354 -- is, this will be an error if the new operation is in the
8355 -- private part of the instance. Emit a warning now, which will
8356 -- make the subsequent error message easier to understand.
8358 if not Is_Abstract_Type (F_Typ)
8359 and then Is_Abstract_Subprogram (Prev_E)
8360 and then In_Private_Part (Current_Scope)
8362 Error_Msg_Node_2 := F_Typ;
8364 ("private operation& in generic unit does not override "
8365 & "any primitive operation of& (RM 12.3 (18))??",
8375 end Is_Non_Overriding_Operation;
8377 -------------------------------------
8378 -- List_Inherited_Pre_Post_Aspects --
8379 -------------------------------------
8381 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8383 if Opt.List_Inherited_Aspects
8384 and then Is_Subprogram_Or_Generic_Subprogram (E)
8387 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8392 for Index in Subps'Range loop
8393 Items := Contract (Subps (Index));
8395 if Present (Items) then
8396 Prag := Pre_Post_Conditions (Items);
8397 while Present (Prag) loop
8398 Error_Msg_Sloc := Sloc (Prag);
8400 if Class_Present (Prag)
8401 and then not Split_PPC (Prag)
8403 if Pragma_Name (Prag) = Name_Precondition then
8405 ("info: & inherits `Pre''Class` aspect from "
8409 ("info: & inherits `Post''Class` aspect from "
8414 Prag := Next_Pragma (Prag);
8420 end List_Inherited_Pre_Post_Aspects;
8422 ------------------------------
8423 -- Make_Inequality_Operator --
8424 ------------------------------
8426 -- S is the defining identifier of an equality operator. We build a
8427 -- subprogram declaration with the right signature. This operation is
8428 -- intrinsic, because it is always expanded as the negation of the
8429 -- call to the equality function.
8431 procedure Make_Inequality_Operator (S : Entity_Id) is
8432 Loc : constant Source_Ptr := Sloc (S);
8435 Op_Name : Entity_Id;
8437 FF : constant Entity_Id := First_Formal (S);
8438 NF : constant Entity_Id := Next_Formal (FF);
8441 -- Check that equality was properly defined, ignore call if not
8448 A : constant Entity_Id :=
8449 Make_Defining_Identifier (Sloc (FF),
8450 Chars => Chars (FF));
8452 B : constant Entity_Id :=
8453 Make_Defining_Identifier (Sloc (NF),
8454 Chars => Chars (NF));
8457 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8459 Formals := New_List (
8460 Make_Parameter_Specification (Loc,
8461 Defining_Identifier => A,
8463 New_Occurrence_Of (Etype (First_Formal (S)),
8464 Sloc (Etype (First_Formal (S))))),
8466 Make_Parameter_Specification (Loc,
8467 Defining_Identifier => B,
8469 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8470 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8473 Make_Subprogram_Declaration (Loc,
8475 Make_Function_Specification (Loc,
8476 Defining_Unit_Name => Op_Name,
8477 Parameter_Specifications => Formals,
8478 Result_Definition =>
8479 New_Occurrence_Of (Standard_Boolean, Loc)));
8481 -- Insert inequality right after equality if it is explicit or after
8482 -- the derived type when implicit. These entities are created only
8483 -- for visibility purposes, and eventually replaced in the course
8484 -- of expansion, so they do not need to be attached to the tree and
8485 -- seen by the back-end. Keeping them internal also avoids spurious
8486 -- freezing problems. The declaration is inserted in the tree for
8487 -- analysis, and removed afterwards. If the equality operator comes
8488 -- from an explicit declaration, attach the inequality immediately
8489 -- after. Else the equality is inherited from a derived type
8490 -- declaration, so insert inequality after that declaration.
8492 if No (Alias (S)) then
8493 Insert_After (Unit_Declaration_Node (S), Decl);
8494 elsif Is_List_Member (Parent (S)) then
8495 Insert_After (Parent (S), Decl);
8497 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8500 Mark_Rewrite_Insertion (Decl);
8501 Set_Is_Intrinsic_Subprogram (Op_Name);
8504 Set_Has_Completion (Op_Name);
8505 Set_Corresponding_Equality (Op_Name, S);
8506 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8508 end Make_Inequality_Operator;
8510 ----------------------
8511 -- May_Need_Actuals --
8512 ----------------------
8514 procedure May_Need_Actuals (Fun : Entity_Id) is
8519 F := First_Formal (Fun);
8521 while Present (F) loop
8522 if No (Default_Value (F)) then
8530 Set_Needs_No_Actuals (Fun, B);
8531 end May_Need_Actuals;
8533 ---------------------
8534 -- Mode_Conformant --
8535 ---------------------
8537 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8540 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8542 end Mode_Conformant;
8544 ---------------------------
8545 -- New_Overloaded_Entity --
8546 ---------------------------
8548 procedure New_Overloaded_Entity
8550 Derived_Type : Entity_Id := Empty)
8552 Overridden_Subp : Entity_Id := Empty;
8553 -- Set if the current scope has an operation that is type-conformant
8554 -- with S, and becomes hidden by S.
8556 Is_Primitive_Subp : Boolean;
8557 -- Set to True if the new subprogram is primitive
8560 -- Entity that S overrides
8562 Prev_Vis : Entity_Id := Empty;
8563 -- Predecessor of E in Homonym chain
8565 procedure Check_For_Primitive_Subprogram
8566 (Is_Primitive : out Boolean;
8567 Is_Overriding : Boolean := False);
8568 -- If the subprogram being analyzed is a primitive operation of the type
8569 -- of a formal or result, set the Has_Primitive_Operations flag on the
8570 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8571 -- corresponding flag on the entity itself for later use.
8573 procedure Check_Synchronized_Overriding
8574 (Def_Id : Entity_Id;
8575 Overridden_Subp : out Entity_Id);
8576 -- First determine if Def_Id is an entry or a subprogram either defined
8577 -- in the scope of a task or protected type, or is a primitive of such
8578 -- a type. Check whether Def_Id overrides a subprogram of an interface
8579 -- implemented by the synchronized type, return the overridden entity
8582 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8583 -- Check that E is declared in the private part of the current package,
8584 -- or in the package body, where it may hide a previous declaration.
8585 -- We can't use In_Private_Part by itself because this flag is also
8586 -- set when freezing entities, so we must examine the place of the
8587 -- declaration in the tree, and recognize wrapper packages as well.
8589 function Is_Overriding_Alias
8591 New_E : Entity_Id) return Boolean;
8592 -- Check whether new subprogram and old subprogram are both inherited
8593 -- from subprograms that have distinct dispatch table entries. This can
8594 -- occur with derivations from instances with accidental homonyms. The
8595 -- function is conservative given that the converse is only true within
8596 -- instances that contain accidental overloadings.
8598 ------------------------------------
8599 -- Check_For_Primitive_Subprogram --
8600 ------------------------------------
8602 procedure Check_For_Primitive_Subprogram
8603 (Is_Primitive : out Boolean;
8604 Is_Overriding : Boolean := False)
8610 function Visible_Part_Type (T : Entity_Id) return Boolean;
8611 -- Returns true if T is declared in the visible part of the current
8612 -- package scope; otherwise returns false. Assumes that T is declared
8615 procedure Check_Private_Overriding (T : Entity_Id);
8616 -- Checks that if a primitive abstract subprogram of a visible
8617 -- abstract type is declared in a private part, then it must override
8618 -- an abstract subprogram declared in the visible part. Also checks
8619 -- that if a primitive function with a controlling result is declared
8620 -- in a private part, then it must override a function declared in
8621 -- the visible part.
8623 ------------------------------
8624 -- Check_Private_Overriding --
8625 ------------------------------
8627 procedure Check_Private_Overriding (T : Entity_Id) is
8629 function Overrides_Visible_Function
8630 (Partial_View : Entity_Id) return Boolean;
8631 -- True if S overrides a function in the visible part. The
8632 -- overridden function could be explicitly or implicitly declared.
8634 function Overrides_Visible_Function
8635 (Partial_View : Entity_Id) return Boolean
8638 if not Is_Overriding or else not Has_Homonym (S) then
8642 if not Present (Partial_View) then
8646 -- Search through all the homonyms H of S in the current
8647 -- package spec, and return True if we find one that matches.
8648 -- Note that Parent (H) will be the declaration of the
8649 -- partial view of T for a match.
8656 exit when not Present (H) or else Scope (H) /= Scope (S);
8660 N_Private_Extension_Declaration,
8661 N_Private_Type_Declaration)
8662 and then Defining_Identifier (Parent (H)) = Partial_View
8670 end Overrides_Visible_Function;
8672 -- Start of processing for Check_Private_Overriding
8675 if Is_Package_Or_Generic_Package (Current_Scope)
8676 and then In_Private_Part (Current_Scope)
8677 and then Visible_Part_Type (T)
8678 and then not In_Instance
8680 if Is_Abstract_Type (T)
8681 and then Is_Abstract_Subprogram (S)
8682 and then (not Is_Overriding
8683 or else not Is_Abstract_Subprogram (E))
8685 Error_Msg_N ("abstract subprograms must be visible "
8686 & "(RM 3.9.3(10))!", S);
8688 elsif Ekind (S) = E_Function then
8690 Partial_View : constant Entity_Id :=
8691 Incomplete_Or_Partial_View (T);
8694 if not Overrides_Visible_Function (Partial_View) then
8696 -- Here, S is "function ... return T;" declared in
8697 -- the private part, not overriding some visible
8698 -- operation. That's illegal in the tagged case
8699 -- (but not if the private type is untagged).
8701 if ((Present (Partial_View)
8702 and then Is_Tagged_Type (Partial_View))
8703 or else (not Present (Partial_View)
8704 and then Is_Tagged_Type (T)))
8705 and then T = Base_Type (Etype (S))
8708 ("private function with tagged result must"
8709 & " override visible-part function", S);
8711 ("\move subprogram to the visible part"
8712 & " (RM 3.9.3(10))", S);
8714 -- AI05-0073: extend this test to the case of a
8715 -- function with a controlling access result.
8717 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8718 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8720 not Is_Class_Wide_Type
8721 (Designated_Type (Etype (S)))
8722 and then Ada_Version >= Ada_2012
8725 ("private function with controlling access "
8726 & "result must override visible-part function",
8729 ("\move subprogram to the visible part"
8730 & " (RM 3.9.3(10))", S);
8736 end Check_Private_Overriding;
8738 -----------------------
8739 -- Visible_Part_Type --
8740 -----------------------
8742 function Visible_Part_Type (T : Entity_Id) return Boolean is
8743 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8747 -- If the entity is a private type, then it must be declared in a
8750 if Ekind (T) in Private_Kind then
8754 -- Otherwise, we traverse the visible part looking for its
8755 -- corresponding declaration. We cannot use the declaration
8756 -- node directly because in the private part the entity of a
8757 -- private type is the one in the full view, which does not
8758 -- indicate that it is the completion of something visible.
8760 N := First (Visible_Declarations (Specification (P)));
8761 while Present (N) loop
8762 if Nkind (N) = N_Full_Type_Declaration
8763 and then Present (Defining_Identifier (N))
8764 and then T = Defining_Identifier (N)
8768 elsif Nkind_In (N, N_Private_Type_Declaration,
8769 N_Private_Extension_Declaration)
8770 and then Present (Defining_Identifier (N))
8771 and then T = Full_View (Defining_Identifier (N))
8780 end Visible_Part_Type;
8782 -- Start of processing for Check_For_Primitive_Subprogram
8785 Is_Primitive := False;
8787 if not Comes_From_Source (S) then
8790 -- If subprogram is at library level, it is not primitive operation
8792 elsif Current_Scope = Standard_Standard then
8795 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8796 and then not In_Package_Body (Current_Scope))
8797 or else Is_Overriding
8799 -- For function, check return type
8801 if Ekind (S) = E_Function then
8802 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8803 F_Typ := Designated_Type (Etype (S));
8808 B_Typ := Base_Type (F_Typ);
8810 if Scope (B_Typ) = Current_Scope
8811 and then not Is_Class_Wide_Type (B_Typ)
8812 and then not Is_Generic_Type (B_Typ)
8814 Is_Primitive := True;
8815 Set_Has_Primitive_Operations (B_Typ);
8816 Set_Is_Primitive (S);
8817 Check_Private_Overriding (B_Typ);
8821 -- For all subprograms, check formals
8823 Formal := First_Formal (S);
8824 while Present (Formal) loop
8825 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8826 F_Typ := Designated_Type (Etype (Formal));
8828 F_Typ := Etype (Formal);
8831 B_Typ := Base_Type (F_Typ);
8833 if Ekind (B_Typ) = E_Access_Subtype then
8834 B_Typ := Base_Type (B_Typ);
8837 if Scope (B_Typ) = Current_Scope
8838 and then not Is_Class_Wide_Type (B_Typ)
8839 and then not Is_Generic_Type (B_Typ)
8841 Is_Primitive := True;
8842 Set_Is_Primitive (S);
8843 Set_Has_Primitive_Operations (B_Typ);
8844 Check_Private_Overriding (B_Typ);
8847 Next_Formal (Formal);
8850 -- Special case: An equality function can be redefined for a type
8851 -- occurring in a declarative part, and won't otherwise be treated as
8852 -- a primitive because it doesn't occur in a package spec and doesn't
8853 -- override an inherited subprogram. It's important that we mark it
8854 -- primitive so it can be returned by Collect_Primitive_Operations
8855 -- and be used in composing the equality operation of later types
8856 -- that have a component of the type.
8858 elsif Chars (S) = Name_Op_Eq
8859 and then Etype (S) = Standard_Boolean
8861 B_Typ := Base_Type (Etype (First_Formal (S)));
8863 if Scope (B_Typ) = Current_Scope
8865 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8866 and then not Is_Limited_Type (B_Typ)
8868 Is_Primitive := True;
8869 Set_Is_Primitive (S);
8870 Set_Has_Primitive_Operations (B_Typ);
8871 Check_Private_Overriding (B_Typ);
8874 end Check_For_Primitive_Subprogram;
8876 -----------------------------------
8877 -- Check_Synchronized_Overriding --
8878 -----------------------------------
8880 procedure Check_Synchronized_Overriding
8881 (Def_Id : Entity_Id;
8882 Overridden_Subp : out Entity_Id)
8884 Ifaces_List : Elist_Id;
8888 function Matches_Prefixed_View_Profile
8889 (Prim_Params : List_Id;
8890 Iface_Params : List_Id) return Boolean;
8891 -- Determine whether a subprogram's parameter profile Prim_Params
8892 -- matches that of a potentially overridden interface subprogram
8893 -- Iface_Params. Also determine if the type of first parameter of
8894 -- Iface_Params is an implemented interface.
8896 -----------------------------------
8897 -- Matches_Prefixed_View_Profile --
8898 -----------------------------------
8900 function Matches_Prefixed_View_Profile
8901 (Prim_Params : List_Id;
8902 Iface_Params : List_Id) return Boolean
8904 Iface_Id : Entity_Id;
8905 Iface_Param : Node_Id;
8906 Iface_Typ : Entity_Id;
8907 Prim_Id : Entity_Id;
8908 Prim_Param : Node_Id;
8909 Prim_Typ : Entity_Id;
8911 function Is_Implemented
8912 (Ifaces_List : Elist_Id;
8913 Iface : Entity_Id) return Boolean;
8914 -- Determine if Iface is implemented by the current task or
8917 --------------------
8918 -- Is_Implemented --
8919 --------------------
8921 function Is_Implemented
8922 (Ifaces_List : Elist_Id;
8923 Iface : Entity_Id) return Boolean
8925 Iface_Elmt : Elmt_Id;
8928 Iface_Elmt := First_Elmt (Ifaces_List);
8929 while Present (Iface_Elmt) loop
8930 if Node (Iface_Elmt) = Iface then
8934 Next_Elmt (Iface_Elmt);
8940 -- Start of processing for Matches_Prefixed_View_Profile
8943 Iface_Param := First (Iface_Params);
8944 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8946 if Is_Access_Type (Iface_Typ) then
8947 Iface_Typ := Designated_Type (Iface_Typ);
8950 Prim_Param := First (Prim_Params);
8952 -- The first parameter of the potentially overridden subprogram
8953 -- must be an interface implemented by Prim.
8955 if not Is_Interface (Iface_Typ)
8956 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8961 -- The checks on the object parameters are done, move onto the
8962 -- rest of the parameters.
8964 if not In_Scope then
8965 Prim_Param := Next (Prim_Param);
8968 Iface_Param := Next (Iface_Param);
8969 while Present (Iface_Param) and then Present (Prim_Param) loop
8970 Iface_Id := Defining_Identifier (Iface_Param);
8971 Iface_Typ := Find_Parameter_Type (Iface_Param);
8973 Prim_Id := Defining_Identifier (Prim_Param);
8974 Prim_Typ := Find_Parameter_Type (Prim_Param);
8976 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8977 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8978 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8980 Iface_Typ := Designated_Type (Iface_Typ);
8981 Prim_Typ := Designated_Type (Prim_Typ);
8984 -- Case of multiple interface types inside a parameter profile
8986 -- (Obj_Param : in out Iface; ...; Param : Iface)
8988 -- If the interface type is implemented, then the matching type
8989 -- in the primitive should be the implementing record type.
8991 if Ekind (Iface_Typ) = E_Record_Type
8992 and then Is_Interface (Iface_Typ)
8993 and then Is_Implemented (Ifaces_List, Iface_Typ)
8995 if Prim_Typ /= Typ then
8999 -- The two parameters must be both mode and subtype conformant
9001 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9003 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9012 -- One of the two lists contains more parameters than the other
9014 if Present (Iface_Param) or else Present (Prim_Param) then
9019 end Matches_Prefixed_View_Profile;
9021 -- Start of processing for Check_Synchronized_Overriding
9024 Overridden_Subp := Empty;
9026 -- Def_Id must be an entry or a subprogram. We should skip predefined
9027 -- primitives internally generated by the frontend; however at this
9028 -- stage predefined primitives are still not fully decorated. As a
9029 -- minor optimization we skip here internally generated subprograms.
9031 if (Ekind (Def_Id) /= E_Entry
9032 and then Ekind (Def_Id) /= E_Function
9033 and then Ekind (Def_Id) /= E_Procedure)
9034 or else not Comes_From_Source (Def_Id)
9039 -- Search for the concurrent declaration since it contains the list
9040 -- of all implemented interfaces. In this case, the subprogram is
9041 -- declared within the scope of a protected or a task type.
9043 if Present (Scope (Def_Id))
9044 and then Is_Concurrent_Type (Scope (Def_Id))
9045 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9047 Typ := Scope (Def_Id);
9050 -- The enclosing scope is not a synchronized type and the subprogram
9053 elsif No (First_Formal (Def_Id)) then
9056 -- The subprogram has formals and hence it may be a primitive of a
9060 Typ := Etype (First_Formal (Def_Id));
9062 if Is_Access_Type (Typ) then
9063 Typ := Directly_Designated_Type (Typ);
9066 if Is_Concurrent_Type (Typ)
9067 and then not Is_Generic_Actual_Type (Typ)
9071 -- This case occurs when the concurrent type is declared within
9072 -- a generic unit. As a result the corresponding record has been
9073 -- built and used as the type of the first formal, we just have
9074 -- to retrieve the corresponding concurrent type.
9076 elsif Is_Concurrent_Record_Type (Typ)
9077 and then not Is_Class_Wide_Type (Typ)
9078 and then Present (Corresponding_Concurrent_Type (Typ))
9080 Typ := Corresponding_Concurrent_Type (Typ);
9088 -- There is no overriding to check if is an inherited operation in a
9089 -- type derivation on for a generic actual.
9091 Collect_Interfaces (Typ, Ifaces_List);
9093 if Is_Empty_Elmt_List (Ifaces_List) then
9097 -- Determine whether entry or subprogram Def_Id overrides a primitive
9098 -- operation that belongs to one of the interfaces in Ifaces_List.
9101 Candidate : Entity_Id := Empty;
9102 Hom : Entity_Id := Empty;
9103 Subp : Entity_Id := Empty;
9106 -- Traverse the homonym chain, looking for a potentially
9107 -- overridden subprogram that belongs to an implemented
9110 Hom := Current_Entity_In_Scope (Def_Id);
9111 while Present (Hom) loop
9115 or else not Is_Overloadable (Subp)
9116 or else not Is_Primitive (Subp)
9117 or else not Is_Dispatching_Operation (Subp)
9118 or else not Present (Find_Dispatching_Type (Subp))
9119 or else not Is_Interface (Find_Dispatching_Type (Subp))
9123 -- Entries and procedures can override abstract or null
9124 -- interface procedures.
9126 elsif (Ekind (Def_Id) = E_Procedure
9127 or else Ekind (Def_Id) = E_Entry)
9128 and then Ekind (Subp) = E_Procedure
9129 and then Matches_Prefixed_View_Profile
9130 (Parameter_Specifications (Parent (Def_Id)),
9131 Parameter_Specifications (Parent (Subp)))
9135 -- For an overridden subprogram Subp, check whether the mode
9136 -- of its first parameter is correct depending on the kind
9137 -- of synchronized type.
9140 Formal : constant Node_Id := First_Formal (Candidate);
9143 -- In order for an entry or a protected procedure to
9144 -- override, the first parameter of the overridden
9145 -- routine must be of mode "out", "in out" or
9146 -- access-to-variable.
9148 if Ekind_In (Candidate, E_Entry, E_Procedure)
9149 and then Is_Protected_Type (Typ)
9150 and then Ekind (Formal) /= E_In_Out_Parameter
9151 and then Ekind (Formal) /= E_Out_Parameter
9152 and then Nkind (Parameter_Type (Parent (Formal))) /=
9157 -- All other cases are OK since a task entry or routine
9158 -- does not have a restriction on the mode of the first
9159 -- parameter of the overridden interface routine.
9162 Overridden_Subp := Candidate;
9167 -- Functions can override abstract interface functions
9169 elsif Ekind (Def_Id) = E_Function
9170 and then Ekind (Subp) = E_Function
9171 and then Matches_Prefixed_View_Profile
9172 (Parameter_Specifications (Parent (Def_Id)),
9173 Parameter_Specifications (Parent (Subp)))
9174 and then Etype (Result_Definition (Parent (Def_Id))) =
9175 Etype (Result_Definition (Parent (Subp)))
9179 -- If an inherited subprogram is implemented by a protected
9180 -- function, then the first parameter of the inherited
9181 -- subprogram shall be of mode in, but not an
9182 -- access-to-variable parameter (RM 9.4(11/9)
9184 if Present (First_Formal (Subp))
9185 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9187 (not Is_Access_Type (Etype (First_Formal (Subp)))
9189 Is_Access_Constant (Etype (First_Formal (Subp))))
9191 Overridden_Subp := Subp;
9196 Hom := Homonym (Hom);
9199 -- After examining all candidates for overriding, we are left with
9200 -- the best match which is a mode incompatible interface routine.
9202 if In_Scope and then Present (Candidate) then
9203 Error_Msg_PT (Def_Id, Candidate);
9206 Overridden_Subp := Candidate;
9209 end Check_Synchronized_Overriding;
9211 ----------------------------
9212 -- Is_Private_Declaration --
9213 ----------------------------
9215 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9216 Priv_Decls : List_Id;
9217 Decl : constant Node_Id := Unit_Declaration_Node (E);
9220 if Is_Package_Or_Generic_Package (Current_Scope)
9221 and then In_Private_Part (Current_Scope)
9224 Private_Declarations (Package_Specification (Current_Scope));
9226 return In_Package_Body (Current_Scope)
9228 (Is_List_Member (Decl)
9229 and then List_Containing (Decl) = Priv_Decls)
9230 or else (Nkind (Parent (Decl)) = N_Package_Specification
9233 (Defining_Entity (Parent (Decl)))
9234 and then List_Containing (Parent (Parent (Decl))) =
9239 end Is_Private_Declaration;
9241 --------------------------
9242 -- Is_Overriding_Alias --
9243 --------------------------
9245 function Is_Overriding_Alias
9247 New_E : Entity_Id) return Boolean
9249 AO : constant Entity_Id := Alias (Old_E);
9250 AN : constant Entity_Id := Alias (New_E);
9252 return Scope (AO) /= Scope (AN)
9253 or else No (DTC_Entity (AO))
9254 or else No (DTC_Entity (AN))
9255 or else DT_Position (AO) = DT_Position (AN);
9256 end Is_Overriding_Alias;
9258 -- Start of processing for New_Overloaded_Entity
9261 -- We need to look for an entity that S may override. This must be a
9262 -- homonym in the current scope, so we look for the first homonym of
9263 -- S in the current scope as the starting point for the search.
9265 E := Current_Entity_In_Scope (S);
9267 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9268 -- They are directly added to the list of primitive operations of
9269 -- Derived_Type, unless this is a rederivation in the private part
9270 -- of an operation that was already derived in the visible part of
9271 -- the current package.
9273 if Ada_Version >= Ada_2005
9274 and then Present (Derived_Type)
9275 and then Present (Alias (S))
9276 and then Is_Dispatching_Operation (Alias (S))
9277 and then Present (Find_Dispatching_Type (Alias (S)))
9278 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9280 -- For private types, when the full-view is processed we propagate to
9281 -- the full view the non-overridden entities whose attribute "alias"
9282 -- references an interface primitive. These entities were added by
9283 -- Derive_Subprograms to ensure that interface primitives are
9286 -- Inside_Freeze_Actions is non zero when S corresponds with an
9287 -- internal entity that links an interface primitive with its
9288 -- covering primitive through attribute Interface_Alias (see
9289 -- Add_Internal_Interface_Entities).
9291 if Inside_Freezing_Actions = 0
9292 and then Is_Package_Or_Generic_Package (Current_Scope)
9293 and then In_Private_Part (Current_Scope)
9294 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9295 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9296 and then Full_View (Defining_Identifier (Parent (E)))
9297 = Defining_Identifier (Parent (S))
9298 and then Alias (E) = Alias (S)
9300 Check_Operation_From_Private_View (S, E);
9301 Set_Is_Dispatching_Operation (S);
9306 Enter_Overloaded_Entity (S);
9307 Check_Dispatching_Operation (S, Empty);
9308 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9314 -- If there is no homonym then this is definitely not overriding
9317 Enter_Overloaded_Entity (S);
9318 Check_Dispatching_Operation (S, Empty);
9319 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9321 -- If subprogram has an explicit declaration, check whether it has an
9322 -- overriding indicator.
9324 if Comes_From_Source (S) then
9325 Check_Synchronized_Overriding (S, Overridden_Subp);
9327 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9328 -- it may have overridden some hidden inherited primitive. Update
9329 -- Overridden_Subp to avoid spurious errors when checking the
9330 -- overriding indicator.
9332 if Ada_Version >= Ada_2012
9333 and then No (Overridden_Subp)
9334 and then Is_Dispatching_Operation (S)
9335 and then Present (Overridden_Operation (S))
9337 Overridden_Subp := Overridden_Operation (S);
9340 Check_Overriding_Indicator
9341 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9343 -- The Ghost policy in effect at the point of declaration of a
9344 -- parent subprogram and an overriding subprogram must match
9345 -- (SPARK RM 6.9(17)).
9347 Check_Ghost_Overriding (S, Overridden_Subp);
9350 -- If there is a homonym that is not overloadable, then we have an
9351 -- error, except for the special cases checked explicitly below.
9353 elsif not Is_Overloadable (E) then
9355 -- Check for spurious conflict produced by a subprogram that has the
9356 -- same name as that of the enclosing generic package. The conflict
9357 -- occurs within an instance, between the subprogram and the renaming
9358 -- declaration for the package. After the subprogram, the package
9359 -- renaming declaration becomes hidden.
9361 if Ekind (E) = E_Package
9362 and then Present (Renamed_Object (E))
9363 and then Renamed_Object (E) = Current_Scope
9364 and then Nkind (Parent (Renamed_Object (E))) =
9365 N_Package_Specification
9366 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9369 Set_Is_Immediately_Visible (E, False);
9370 Enter_Overloaded_Entity (S);
9371 Set_Homonym (S, Homonym (E));
9372 Check_Dispatching_Operation (S, Empty);
9373 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9375 -- If the subprogram is implicit it is hidden by the previous
9376 -- declaration. However if it is dispatching, it must appear in the
9377 -- dispatch table anyway, because it can be dispatched to even if it
9378 -- cannot be called directly.
9380 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9381 Set_Scope (S, Current_Scope);
9383 if Is_Dispatching_Operation (Alias (S)) then
9384 Check_Dispatching_Operation (S, Empty);
9390 Error_Msg_Sloc := Sloc (E);
9392 -- Generate message, with useful additional warning if in generic
9394 if Is_Generic_Unit (E) then
9395 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9396 Error_Msg_N ("\& conflicts with declaration#", S);
9398 Error_Msg_N ("& conflicts with declaration#", S);
9404 -- E exists and is overloadable
9407 Check_Synchronized_Overriding (S, Overridden_Subp);
9409 -- Loop through E and its homonyms to determine if any of them is
9410 -- the candidate for overriding by S.
9412 while Present (E) loop
9414 -- Definitely not interesting if not in the current scope
9416 if Scope (E) /= Current_Scope then
9419 -- A function can overload the name of an abstract state. The
9420 -- state can be viewed as a function with a profile that cannot
9421 -- be matched by anything.
9423 elsif Ekind (S) = E_Function
9424 and then Ekind (E) = E_Abstract_State
9426 Enter_Overloaded_Entity (S);
9429 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9430 -- procedures locate the internally generated spec. We enforce
9431 -- mode conformance since a tagged type may inherit from
9432 -- interfaces several null primitives which differ only in
9433 -- the mode of the formals.
9435 elsif not Comes_From_Source (S)
9436 and then Is_Null_Procedure (S)
9437 and then not Mode_Conformant (E, S)
9441 -- Check if we have type conformance
9443 elsif Type_Conformant (E, S) then
9445 -- If the old and new entities have the same profile and one
9446 -- is not the body of the other, then this is an error, unless
9447 -- one of them is implicitly declared.
9449 -- There are some cases when both can be implicit, for example
9450 -- when both a literal and a function that overrides it are
9451 -- inherited in a derivation, or when an inherited operation
9452 -- of a tagged full type overrides the inherited operation of
9453 -- a private extension. Ada 83 had a special rule for the
9454 -- literal case. In Ada 95, the later implicit operation hides
9455 -- the former, and the literal is always the former. In the
9456 -- odd case where both are derived operations declared at the
9457 -- same point, both operations should be declared, and in that
9458 -- case we bypass the following test and proceed to the next
9459 -- part. This can only occur for certain obscure cases in
9460 -- instances, when an operation on a type derived from a formal
9461 -- private type does not override a homograph inherited from
9462 -- the actual. In subsequent derivations of such a type, the
9463 -- DT positions of these operations remain distinct, if they
9466 if Present (Alias (S))
9467 and then (No (Alias (E))
9468 or else Comes_From_Source (E)
9469 or else Is_Abstract_Subprogram (S)
9471 (Is_Dispatching_Operation (E)
9472 and then Is_Overriding_Alias (E, S)))
9473 and then Ekind (E) /= E_Enumeration_Literal
9475 -- When an derived operation is overloaded it may be due to
9476 -- the fact that the full view of a private extension
9477 -- re-inherits. It has to be dealt with.
9479 if Is_Package_Or_Generic_Package (Current_Scope)
9480 and then In_Private_Part (Current_Scope)
9482 Check_Operation_From_Private_View (S, E);
9485 -- In any case the implicit operation remains hidden by the
9486 -- existing declaration, which is overriding. Indicate that
9487 -- E overrides the operation from which S is inherited.
9489 if Present (Alias (S)) then
9490 Set_Overridden_Operation (E, Alias (S));
9491 Inherit_Subprogram_Contract (E, Alias (S));
9494 Set_Overridden_Operation (E, S);
9495 Inherit_Subprogram_Contract (E, S);
9498 if Comes_From_Source (E) then
9499 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9501 -- The Ghost policy in effect at the point of declaration
9502 -- of a parent subprogram and an overriding subprogram
9503 -- must match (SPARK RM 6.9(17)).
9505 Check_Ghost_Overriding (E, S);
9510 -- Within an instance, the renaming declarations for actual
9511 -- subprograms may become ambiguous, but they do not hide each
9514 elsif Ekind (E) /= E_Entry
9515 and then not Comes_From_Source (E)
9516 and then not Is_Generic_Instance (E)
9517 and then (Present (Alias (E))
9518 or else Is_Intrinsic_Subprogram (E))
9519 and then (not In_Instance
9520 or else No (Parent (E))
9521 or else Nkind (Unit_Declaration_Node (E)) /=
9522 N_Subprogram_Renaming_Declaration)
9524 -- A subprogram child unit is not allowed to override an
9525 -- inherited subprogram (10.1.1(20)).
9527 if Is_Child_Unit (S) then
9529 ("child unit overrides inherited subprogram in parent",
9534 if Is_Non_Overriding_Operation (E, S) then
9535 Enter_Overloaded_Entity (S);
9537 if No (Derived_Type)
9538 or else Is_Tagged_Type (Derived_Type)
9540 Check_Dispatching_Operation (S, Empty);
9546 -- E is a derived operation or an internal operator which
9547 -- is being overridden. Remove E from further visibility.
9548 -- Furthermore, if E is a dispatching operation, it must be
9549 -- replaced in the list of primitive operations of its type
9550 -- (see Override_Dispatching_Operation).
9552 Overridden_Subp := E;
9558 Prev := First_Entity (Current_Scope);
9559 while Present (Prev) and then Next_Entity (Prev) /= E loop
9563 -- It is possible for E to be in the current scope and
9564 -- yet not in the entity chain. This can only occur in a
9565 -- generic context where E is an implicit concatenation
9566 -- in the formal part, because in a generic body the
9567 -- entity chain starts with the formals.
9569 -- In GNATprove mode, a wrapper for an operation with
9570 -- axiomatization may be a homonym of another declaration
9571 -- for an actual subprogram (needs refinement ???).
9575 and then GNATprove_Mode
9577 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9578 N_Subprogram_Renaming_Declaration
9582 pragma Assert (Chars (E) = Name_Op_Concat);
9587 -- E must be removed both from the entity_list of the
9588 -- current scope, and from the visibility chain.
9590 if Debug_Flag_E then
9591 Write_Str ("Override implicit operation ");
9592 Write_Int (Int (E));
9596 -- If E is a predefined concatenation, it stands for four
9597 -- different operations. As a result, a single explicit
9598 -- declaration does not hide it. In a possible ambiguous
9599 -- situation, Disambiguate chooses the user-defined op,
9600 -- so it is correct to retain the previous internal one.
9602 if Chars (E) /= Name_Op_Concat
9603 or else Ekind (E) /= E_Operator
9605 -- For nondispatching derived operations that are
9606 -- overridden by a subprogram declared in the private
9607 -- part of a package, we retain the derived subprogram
9608 -- but mark it as not immediately visible. If the
9609 -- derived operation was declared in the visible part
9610 -- then this ensures that it will still be visible
9611 -- outside the package with the proper signature
9612 -- (calls from outside must also be directed to this
9613 -- version rather than the overriding one, unlike the
9614 -- dispatching case). Calls from inside the package
9615 -- will still resolve to the overriding subprogram
9616 -- since the derived one is marked as not visible
9617 -- within the package.
9619 -- If the private operation is dispatching, we achieve
9620 -- the overriding by keeping the implicit operation
9621 -- but setting its alias to be the overriding one. In
9622 -- this fashion the proper body is executed in all
9623 -- cases, but the original signature is used outside
9626 -- If the overriding is not in the private part, we
9627 -- remove the implicit operation altogether.
9629 if Is_Private_Declaration (S) then
9630 if not Is_Dispatching_Operation (E) then
9631 Set_Is_Immediately_Visible (E, False);
9633 -- Work done in Override_Dispatching_Operation,
9634 -- so nothing else needs to be done here.
9640 -- Find predecessor of E in Homonym chain
9642 if E = Current_Entity (E) then
9645 Prev_Vis := Current_Entity (E);
9646 while Homonym (Prev_Vis) /= E loop
9647 Prev_Vis := Homonym (Prev_Vis);
9651 if Prev_Vis /= Empty then
9653 -- Skip E in the visibility chain
9655 Set_Homonym (Prev_Vis, Homonym (E));
9658 Set_Name_Entity_Id (Chars (E), Homonym (E));
9661 Set_Next_Entity (Prev, Next_Entity (E));
9663 if No (Next_Entity (Prev)) then
9664 Set_Last_Entity (Current_Scope, Prev);
9669 Enter_Overloaded_Entity (S);
9671 -- For entities generated by Derive_Subprograms the
9672 -- overridden operation is the inherited primitive
9673 -- (which is available through the attribute alias).
9675 if not (Comes_From_Source (E))
9676 and then Is_Dispatching_Operation (E)
9677 and then Find_Dispatching_Type (E) =
9678 Find_Dispatching_Type (S)
9679 and then Present (Alias (E))
9680 and then Comes_From_Source (Alias (E))
9682 Set_Overridden_Operation (S, Alias (E));
9683 Inherit_Subprogram_Contract (S, Alias (E));
9685 -- Normal case of setting entity as overridden
9687 -- Note: Static_Initialization and Overridden_Operation
9688 -- attributes use the same field in subprogram entities.
9689 -- Static_Initialization is only defined for internal
9690 -- initialization procedures, where Overridden_Operation
9691 -- is irrelevant. Therefore the setting of this attribute
9692 -- must check whether the target is an init_proc.
9694 elsif not Is_Init_Proc (S) then
9695 Set_Overridden_Operation (S, E);
9696 Inherit_Subprogram_Contract (S, E);
9699 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9701 -- The Ghost policy in effect at the point of declaration
9702 -- of a parent subprogram and an overriding subprogram
9703 -- must match (SPARK RM 6.9(17)).
9705 Check_Ghost_Overriding (S, E);
9707 -- If S is a user-defined subprogram or a null procedure
9708 -- expanded to override an inherited null procedure, or a
9709 -- predefined dispatching primitive then indicate that E
9710 -- overrides the operation from which S is inherited.
9712 if Comes_From_Source (S)
9714 (Present (Parent (S))
9716 Nkind (Parent (S)) = N_Procedure_Specification
9718 Null_Present (Parent (S)))
9720 (Present (Alias (E))
9722 Is_Predefined_Dispatching_Operation (Alias (E)))
9724 if Present (Alias (E)) then
9725 Set_Overridden_Operation (S, Alias (E));
9726 Inherit_Subprogram_Contract (S, Alias (E));
9730 if Is_Dispatching_Operation (E) then
9732 -- An overriding dispatching subprogram inherits the
9733 -- convention of the overridden subprogram (AI-117).
9735 Set_Convention (S, Convention (E));
9736 Check_Dispatching_Operation (S, E);
9739 Check_Dispatching_Operation (S, Empty);
9742 Check_For_Primitive_Subprogram
9743 (Is_Primitive_Subp, Is_Overriding => True);
9744 goto Check_Inequality;
9747 -- Apparent redeclarations in instances can occur when two
9748 -- formal types get the same actual type. The subprograms in
9749 -- in the instance are legal, even if not callable from the
9750 -- outside. Calls from within are disambiguated elsewhere.
9751 -- For dispatching operations in the visible part, the usual
9752 -- rules apply, and operations with the same profile are not
9755 elsif (In_Instance_Visible_Part
9756 and then not Is_Dispatching_Operation (E))
9757 or else In_Instance_Not_Visible
9761 -- Here we have a real error (identical profile)
9764 Error_Msg_Sloc := Sloc (E);
9766 -- Avoid cascaded errors if the entity appears in
9767 -- subsequent calls.
9769 Set_Scope (S, Current_Scope);
9771 -- Generate error, with extra useful warning for the case
9772 -- of a generic instance with no completion.
9774 if Is_Generic_Instance (S)
9775 and then not Has_Completion (E)
9778 ("instantiation cannot provide body for&", S);
9779 Error_Msg_N ("\& conflicts with declaration#", S);
9781 Error_Msg_N ("& conflicts with declaration#", S);
9788 -- If one subprogram has an access parameter and the other
9789 -- a parameter of an access type, calls to either might be
9790 -- ambiguous. Verify that parameters match except for the
9791 -- access parameter.
9793 if May_Hide_Profile then
9799 F1 := First_Formal (S);
9800 F2 := First_Formal (E);
9801 while Present (F1) and then Present (F2) loop
9802 if Is_Access_Type (Etype (F1)) then
9803 if not Is_Access_Type (Etype (F2))
9804 or else not Conforming_Types
9805 (Designated_Type (Etype (F1)),
9806 Designated_Type (Etype (F2)),
9809 May_Hide_Profile := False;
9813 not Conforming_Types
9814 (Etype (F1), Etype (F2), Type_Conformant)
9816 May_Hide_Profile := False;
9827 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9836 -- On exit, we know that S is a new entity
9838 Enter_Overloaded_Entity (S);
9839 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9840 Check_Overriding_Indicator
9841 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9843 -- The Ghost policy in effect at the point of declaration of a parent
9844 -- subprogram and an overriding subprogram must match
9845 -- (SPARK RM 6.9(17)).
9847 Check_Ghost_Overriding (S, Overridden_Subp);
9849 -- Overloading is not allowed in SPARK, except for operators
9851 if Nkind (S) /= N_Defining_Operator_Symbol then
9852 Error_Msg_Sloc := Sloc (Homonym (S));
9853 Check_SPARK_05_Restriction
9854 ("overloading not allowed with entity#", S);
9857 -- If S is a derived operation for an untagged type then by
9858 -- definition it's not a dispatching operation (even if the parent
9859 -- operation was dispatching), so Check_Dispatching_Operation is not
9860 -- called in that case.
9862 if No (Derived_Type)
9863 or else Is_Tagged_Type (Derived_Type)
9865 Check_Dispatching_Operation (S, Empty);
9869 -- If this is a user-defined equality operator that is not a derived
9870 -- subprogram, create the corresponding inequality. If the operation is
9871 -- dispatching, the expansion is done elsewhere, and we do not create
9872 -- an explicit inequality operation.
9874 <<Check_Inequality>>
9875 if Chars (S) = Name_Op_Eq
9876 and then Etype (S) = Standard_Boolean
9877 and then Present (Parent (S))
9878 and then not Is_Dispatching_Operation (S)
9880 Make_Inequality_Operator (S);
9881 Check_Untagged_Equality (S);
9883 end New_Overloaded_Entity;
9885 ---------------------
9886 -- Process_Formals --
9887 ---------------------
9889 procedure Process_Formals
9891 Related_Nod : Node_Id)
9893 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
9894 -- Determine whether an access type designates a type coming from a
9897 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
9898 -- Check whether the default has a class-wide type. After analysis the
9899 -- default has the type of the formal, so we must also check explicitly
9900 -- for an access attribute.
9902 ----------------------------------
9903 -- Designates_From_Limited_With --
9904 ----------------------------------
9906 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
9907 Desig : Entity_Id := Typ;
9910 if Is_Access_Type (Desig) then
9911 Desig := Directly_Designated_Type (Desig);
9914 if Is_Class_Wide_Type (Desig) then
9915 Desig := Root_Type (Desig);
9919 Ekind (Desig) = E_Incomplete_Type
9920 and then From_Limited_With (Desig);
9921 end Designates_From_Limited_With;
9923 ---------------------------
9924 -- Is_Class_Wide_Default --
9925 ---------------------------
9927 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9929 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9930 or else (Nkind (D) = N_Attribute_Reference
9931 and then Attribute_Name (D) = Name_Access
9932 and then Is_Class_Wide_Type (Etype (Prefix (D))));
9933 end Is_Class_Wide_Default;
9937 Context : constant Node_Id := Parent (Parent (T));
9940 Formal_Type : Entity_Id;
9941 Param_Spec : Node_Id;
9944 Num_Out_Params : Nat := 0;
9945 First_Out_Param : Entity_Id := Empty;
9946 -- Used for setting Is_Only_Out_Parameter
9948 -- Start of processing for Process_Formals
9951 -- In order to prevent premature use of the formals in the same formal
9952 -- part, the Ekind is left undefined until all default expressions are
9953 -- analyzed. The Ekind is established in a separate loop at the end.
9955 Param_Spec := First (T);
9956 while Present (Param_Spec) loop
9957 Formal := Defining_Identifier (Param_Spec);
9958 Set_Never_Set_In_Source (Formal, True);
9959 Enter_Name (Formal);
9961 -- Case of ordinary parameters
9963 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9964 Find_Type (Parameter_Type (Param_Spec));
9965 Ptype := Parameter_Type (Param_Spec);
9967 if Ptype = Error then
9971 Formal_Type := Entity (Ptype);
9973 if Is_Incomplete_Type (Formal_Type)
9975 (Is_Class_Wide_Type (Formal_Type)
9976 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
9978 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
9979 -- primitive operations, as long as their completion is
9980 -- in the same declarative part. If in the private part
9981 -- this means that the type cannot be a Taft-amendment type.
9982 -- Check is done on package exit. For access to subprograms,
9983 -- the use is legal for Taft-amendment types.
9985 -- Ada 2012: tagged incomplete types are allowed as generic
9986 -- formal types. They do not introduce dependencies and the
9987 -- corresponding generic subprogram does not have a delayed
9988 -- freeze, because it does not need a freeze node. However,
9989 -- it is still the case that untagged incomplete types cannot
9990 -- be Taft-amendment types and must be completed in private
9991 -- part, so the subprogram must appear in the list of private
9992 -- dependents of the type. If the type is class-wide, it is
9993 -- not a primitive, but the freezing of the subprogram must
9994 -- also be delayed to force the creation of a freeze node.
9996 if Is_Tagged_Type (Formal_Type)
9997 or else (Ada_Version >= Ada_2012
9998 and then not From_Limited_With (Formal_Type)
9999 and then not Is_Generic_Type (Formal_Type))
10001 if Ekind (Scope (Current_Scope)) = E_Package
10002 and then not Is_Generic_Type (Formal_Type)
10005 (Parent (T), N_Access_Function_Definition,
10006 N_Access_Procedure_Definition)
10008 -- A limited view has no private dependents
10010 if not Is_Class_Wide_Type (Formal_Type)
10011 and then not From_Limited_With (Formal_Type)
10013 Append_Elmt (Current_Scope,
10014 Private_Dependents (Base_Type (Formal_Type)));
10017 -- Freezing is delayed to ensure that Register_Prim
10018 -- will get called for this operation, which is needed
10019 -- in cases where static dispatch tables aren't built.
10020 -- (Note that the same is done for controlling access
10021 -- parameter cases in function Access_Definition.)
10023 if not Is_Thunk (Current_Scope) then
10024 Set_Has_Delayed_Freeze (Current_Scope);
10029 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10030 N_Access_Procedure_Definition)
10032 -- AI05-0151: Tagged incomplete types are allowed in all
10033 -- formal parts. Untagged incomplete types are not allowed
10034 -- in bodies. Limited views of either kind are not allowed
10035 -- if there is no place at which the non-limited view can
10036 -- become available.
10038 -- Incomplete formal untagged types are not allowed in
10039 -- subprogram bodies (but are legal in their declarations).
10040 -- This excludes bodies created for null procedures, which
10041 -- are basic declarations.
10043 if Is_Generic_Type (Formal_Type)
10044 and then not Is_Tagged_Type (Formal_Type)
10045 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10048 ("invalid use of formal incomplete type", Param_Spec);
10050 elsif Ada_Version >= Ada_2012 then
10051 if Is_Tagged_Type (Formal_Type)
10052 and then (not From_Limited_With (Formal_Type)
10053 or else not In_Package_Body)
10057 elsif Nkind_In (Context, N_Accept_Statement,
10058 N_Accept_Alternative,
10060 or else (Nkind (Context) = N_Subprogram_Body
10061 and then Comes_From_Source (Context))
10064 ("invalid use of untagged incomplete type &",
10065 Ptype, Formal_Type);
10070 ("invalid use of incomplete type&",
10071 Param_Spec, Formal_Type);
10073 -- Further checks on the legality of incomplete types
10074 -- in formal parts are delayed until the freeze point
10075 -- of the enclosing subprogram or access to subprogram.
10079 elsif Ekind (Formal_Type) = E_Void then
10081 ("premature use of&",
10082 Parameter_Type (Param_Spec), Formal_Type);
10085 -- Ada 2012 (AI-142): Handle aliased parameters
10087 if Ada_Version >= Ada_2012
10088 and then Aliased_Present (Param_Spec)
10090 Set_Is_Aliased (Formal);
10093 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10094 -- declaration corresponding to the null-excluding type of the
10095 -- formal in the enclosing scope. Finally, replace the parameter
10096 -- type of the formal with the internal subtype.
10098 if Ada_Version >= Ada_2005
10099 and then Null_Exclusion_Present (Param_Spec)
10101 if not Is_Access_Type (Formal_Type) then
10103 ("`NOT NULL` allowed only for an access type", Param_Spec);
10106 if Can_Never_Be_Null (Formal_Type)
10107 and then Comes_From_Source (Related_Nod)
10110 ("`NOT NULL` not allowed (& already excludes null)",
10111 Param_Spec, Formal_Type);
10115 Create_Null_Excluding_Itype
10117 Related_Nod => Related_Nod,
10118 Scope_Id => Scope (Current_Scope));
10120 -- If the designated type of the itype is an itype that is
10121 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10122 -- on the access subtype, to prevent order-of-elaboration
10123 -- issues in the backend.
10126 -- type T is access procedure;
10127 -- procedure Op (O : not null T);
10129 if Is_Itype (Directly_Designated_Type (Formal_Type))
10131 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10133 Set_Has_Delayed_Freeze (Formal_Type);
10138 -- An access formal type
10142 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10144 -- No need to continue if we already notified errors
10146 if not Present (Formal_Type) then
10150 -- Ada 2005 (AI-254)
10153 AD : constant Node_Id :=
10154 Access_To_Subprogram_Definition
10155 (Parameter_Type (Param_Spec));
10157 if Present (AD) and then Protected_Present (AD) then
10159 Replace_Anonymous_Access_To_Protected_Subprogram
10165 Set_Etype (Formal, Formal_Type);
10167 -- Deal with default expression if present
10169 Default := Expression (Param_Spec);
10171 if Present (Default) then
10172 Check_SPARK_05_Restriction
10173 ("default expression is not allowed", Default);
10175 if Out_Present (Param_Spec) then
10177 ("default initialization only allowed for IN parameters",
10181 -- Do the special preanalysis of the expression (see section on
10182 -- "Handling of Default Expressions" in the spec of package Sem).
10184 Preanalyze_Spec_Expression (Default, Formal_Type);
10186 -- An access to constant cannot be the default for
10187 -- an access parameter that is an access to variable.
10189 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10190 and then not Is_Access_Constant (Formal_Type)
10191 and then Is_Access_Type (Etype (Default))
10192 and then Is_Access_Constant (Etype (Default))
10195 ("formal that is access to variable cannot be initialized "
10196 & "with an access-to-constant expression", Default);
10199 -- Check that the designated type of an access parameter's default
10200 -- is not a class-wide type unless the parameter's designated type
10201 -- is also class-wide.
10203 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10204 and then not Designates_From_Limited_With (Formal_Type)
10205 and then Is_Class_Wide_Default (Default)
10206 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10209 ("access to class-wide expression not allowed here", Default);
10212 -- Check incorrect use of dynamically tagged expressions
10214 if Is_Tagged_Type (Formal_Type) then
10215 Check_Dynamically_Tagged_Expression
10217 Typ => Formal_Type,
10218 Related_Nod => Default);
10222 -- Ada 2005 (AI-231): Static checks
10224 if Ada_Version >= Ada_2005
10225 and then Is_Access_Type (Etype (Formal))
10226 and then Can_Never_Be_Null (Etype (Formal))
10228 Null_Exclusion_Static_Checks (Param_Spec);
10231 -- The following checks are relevant only when SPARK_Mode is on as
10232 -- these are not standard Ada legality rules.
10234 if SPARK_Mode = On then
10235 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10237 -- A function cannot have a parameter of mode IN OUT or OUT
10240 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10242 ("function cannot have parameter of mode `OUT` or "
10243 & "`IN OUT`", Formal);
10246 -- A procedure cannot have an effectively volatile formal
10247 -- parameter of mode IN because it behaves as a constant
10248 -- (SPARK RM 7.1.3(6)).
10250 elsif Ekind (Scope (Formal)) = E_Procedure
10251 and then Ekind (Formal) = E_In_Parameter
10252 and then Is_Effectively_Volatile (Formal)
10255 ("formal parameter of mode `IN` cannot be volatile", Formal);
10263 -- If this is the formal part of a function specification, analyze the
10264 -- subtype mark in the context where the formals are visible but not
10265 -- yet usable, and may hide outer homographs.
10267 if Nkind (Related_Nod) = N_Function_Specification then
10268 Analyze_Return_Type (Related_Nod);
10271 -- Now set the kind (mode) of each formal
10273 Param_Spec := First (T);
10274 while Present (Param_Spec) loop
10275 Formal := Defining_Identifier (Param_Spec);
10276 Set_Formal_Mode (Formal);
10278 if Ekind (Formal) = E_In_Parameter then
10279 Set_Default_Value (Formal, Expression (Param_Spec));
10281 if Present (Expression (Param_Spec)) then
10282 Default := Expression (Param_Spec);
10284 if Is_Scalar_Type (Etype (Default)) then
10285 if Nkind (Parameter_Type (Param_Spec)) /=
10286 N_Access_Definition
10288 Formal_Type := Entity (Parameter_Type (Param_Spec));
10292 (Related_Nod, Parameter_Type (Param_Spec));
10295 Apply_Scalar_Range_Check (Default, Formal_Type);
10299 elsif Ekind (Formal) = E_Out_Parameter then
10300 Num_Out_Params := Num_Out_Params + 1;
10302 if Num_Out_Params = 1 then
10303 First_Out_Param := Formal;
10306 elsif Ekind (Formal) = E_In_Out_Parameter then
10307 Num_Out_Params := Num_Out_Params + 1;
10310 -- Skip remaining processing if formal type was in error
10312 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10313 goto Next_Parameter;
10316 -- Force call by reference if aliased
10318 if Is_Aliased (Formal) then
10319 Set_Mechanism (Formal, By_Reference);
10321 -- Warn if user asked this to be passed by copy
10323 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10325 ("cannot pass aliased parameter & by copy??", Formal);
10328 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10330 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10331 Set_Mechanism (Formal, By_Copy);
10333 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10334 Set_Mechanism (Formal, By_Reference);
10341 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10342 Set_Is_Only_Out_Parameter (First_Out_Param);
10344 end Process_Formals;
10346 ----------------------------
10347 -- Reference_Body_Formals --
10348 ----------------------------
10350 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10355 if Error_Posted (Spec) then
10359 -- Iterate over both lists. They may be of different lengths if the two
10360 -- specs are not conformant.
10362 Fs := First_Formal (Spec);
10363 Fb := First_Formal (Bod);
10364 while Present (Fs) and then Present (Fb) loop
10365 Generate_Reference (Fs, Fb, 'b');
10367 if Style_Check then
10368 Style.Check_Identifier (Fb, Fs);
10371 Set_Spec_Entity (Fb, Fs);
10372 Set_Referenced (Fs, False);
10376 end Reference_Body_Formals;
10378 -------------------------
10379 -- Set_Actual_Subtypes --
10380 -------------------------
10382 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10384 Formal : Entity_Id;
10386 First_Stmt : Node_Id := Empty;
10387 AS_Needed : Boolean;
10390 -- If this is an empty initialization procedure, no need to create
10391 -- actual subtypes (small optimization).
10393 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10397 Formal := First_Formal (Subp);
10398 while Present (Formal) loop
10399 T := Etype (Formal);
10401 -- We never need an actual subtype for a constrained formal
10403 if Is_Constrained (T) then
10404 AS_Needed := False;
10406 -- If we have unknown discriminants, then we do not need an actual
10407 -- subtype, or more accurately we cannot figure it out. Note that
10408 -- all class-wide types have unknown discriminants.
10410 elsif Has_Unknown_Discriminants (T) then
10411 AS_Needed := False;
10413 -- At this stage we have an unconstrained type that may need an
10414 -- actual subtype. For sure the actual subtype is needed if we have
10415 -- an unconstrained array type.
10417 elsif Is_Array_Type (T) then
10420 -- The only other case needing an actual subtype is an unconstrained
10421 -- record type which is an IN parameter (we cannot generate actual
10422 -- subtypes for the OUT or IN OUT case, since an assignment can
10423 -- change the discriminant values. However we exclude the case of
10424 -- initialization procedures, since discriminants are handled very
10425 -- specially in this context, see the section entitled "Handling of
10426 -- Discriminants" in Einfo.
10428 -- We also exclude the case of Discrim_SO_Functions (functions used
10429 -- in front end layout mode for size/offset values), since in such
10430 -- functions only discriminants are referenced, and not only are such
10431 -- subtypes not needed, but they cannot always be generated, because
10432 -- of order of elaboration issues.
10434 elsif Is_Record_Type (T)
10435 and then Ekind (Formal) = E_In_Parameter
10436 and then Chars (Formal) /= Name_uInit
10437 and then not Is_Unchecked_Union (T)
10438 and then not Is_Discrim_SO_Function (Subp)
10442 -- All other cases do not need an actual subtype
10445 AS_Needed := False;
10448 -- Generate actual subtypes for unconstrained arrays and
10449 -- unconstrained discriminated records.
10452 if Nkind (N) = N_Accept_Statement then
10454 -- If expansion is active, the formal is replaced by a local
10455 -- variable that renames the corresponding entry of the
10456 -- parameter block, and it is this local variable that may
10457 -- require an actual subtype.
10459 if Expander_Active then
10460 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10462 Decl := Build_Actual_Subtype (T, Formal);
10465 if Present (Handled_Statement_Sequence (N)) then
10467 First (Statements (Handled_Statement_Sequence (N)));
10468 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10469 Mark_Rewrite_Insertion (Decl);
10471 -- If the accept statement has no body, there will be no
10472 -- reference to the actuals, so no need to compute actual
10479 Decl := Build_Actual_Subtype (T, Formal);
10480 Prepend (Decl, Declarations (N));
10481 Mark_Rewrite_Insertion (Decl);
10484 -- The declaration uses the bounds of an existing object, and
10485 -- therefore needs no constraint checks.
10487 Analyze (Decl, Suppress => All_Checks);
10489 -- We need to freeze manually the generated type when it is
10490 -- inserted anywhere else than in a declarative part.
10492 if Present (First_Stmt) then
10493 Insert_List_Before_And_Analyze (First_Stmt,
10494 Freeze_Entity (Defining_Identifier (Decl), N));
10496 -- Ditto if the type has a dynamic predicate, because the
10497 -- generated function will mention the actual subtype.
10499 elsif Has_Dynamic_Predicate_Aspect (T) then
10500 Insert_List_Before_And_Analyze (Decl,
10501 Freeze_Entity (Defining_Identifier (Decl), N));
10504 if Nkind (N) = N_Accept_Statement
10505 and then Expander_Active
10507 Set_Actual_Subtype (Renamed_Object (Formal),
10508 Defining_Identifier (Decl));
10510 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10514 Next_Formal (Formal);
10516 end Set_Actual_Subtypes;
10518 ---------------------
10519 -- Set_Formal_Mode --
10520 ---------------------
10522 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10523 Spec : constant Node_Id := Parent (Formal_Id);
10524 Id : constant Entity_Id := Scope (Formal_Id);
10527 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10528 -- since we ensure that corresponding actuals are always valid at the
10529 -- point of the call.
10531 if Out_Present (Spec) then
10532 if Ekind_In (Id, E_Entry, E_Entry_Family)
10533 or else Is_Subprogram_Or_Generic_Subprogram (Id)
10535 Set_Has_Out_Or_In_Out_Parameter (Id, True);
10538 if Ekind_In (Id, E_Function, E_Generic_Function) then
10540 -- [IN] OUT parameters allowed for functions in Ada 2012
10542 if Ada_Version >= Ada_2012 then
10544 -- Even in Ada 2012 operators can only have IN parameters
10546 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10547 Error_Msg_N ("operators can only have IN parameters", Spec);
10550 if In_Present (Spec) then
10551 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10553 Set_Ekind (Formal_Id, E_Out_Parameter);
10556 -- But not in earlier versions of Ada
10559 Error_Msg_N ("functions can only have IN parameters", Spec);
10560 Set_Ekind (Formal_Id, E_In_Parameter);
10563 elsif In_Present (Spec) then
10564 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10567 Set_Ekind (Formal_Id, E_Out_Parameter);
10568 Set_Never_Set_In_Source (Formal_Id, True);
10569 Set_Is_True_Constant (Formal_Id, False);
10570 Set_Current_Value (Formal_Id, Empty);
10574 Set_Ekind (Formal_Id, E_In_Parameter);
10577 -- Set Is_Known_Non_Null for access parameters since the language
10578 -- guarantees that access parameters are always non-null. We also set
10579 -- Can_Never_Be_Null, since there is no way to change the value.
10581 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10583 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10584 -- null; In Ada 2005, only if then null_exclusion is explicit.
10586 if Ada_Version < Ada_2005
10587 or else Can_Never_Be_Null (Etype (Formal_Id))
10589 Set_Is_Known_Non_Null (Formal_Id);
10590 Set_Can_Never_Be_Null (Formal_Id);
10593 -- Ada 2005 (AI-231): Null-exclusion access subtype
10595 elsif Is_Access_Type (Etype (Formal_Id))
10596 and then Can_Never_Be_Null (Etype (Formal_Id))
10598 Set_Is_Known_Non_Null (Formal_Id);
10600 -- We can also set Can_Never_Be_Null (thus preventing some junk
10601 -- access checks) for the case of an IN parameter, which cannot
10602 -- be changed, or for an IN OUT parameter, which can be changed but
10603 -- not to a null value. But for an OUT parameter, the initial value
10604 -- passed in can be null, so we can't set this flag in that case.
10606 if Ekind (Formal_Id) /= E_Out_Parameter then
10607 Set_Can_Never_Be_Null (Formal_Id);
10611 Set_Mechanism (Formal_Id, Default_Mechanism);
10612 Set_Formal_Validity (Formal_Id);
10613 end Set_Formal_Mode;
10615 -------------------------
10616 -- Set_Formal_Validity --
10617 -------------------------
10619 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10621 -- If no validity checking, then we cannot assume anything about the
10622 -- validity of parameters, since we do not know there is any checking
10623 -- of the validity on the call side.
10625 if not Validity_Checks_On then
10628 -- If validity checking for parameters is enabled, this means we are
10629 -- not supposed to make any assumptions about argument values.
10631 elsif Validity_Check_Parameters then
10634 -- If we are checking in parameters, we will assume that the caller is
10635 -- also checking parameters, so we can assume the parameter is valid.
10637 elsif Ekind (Formal_Id) = E_In_Parameter
10638 and then Validity_Check_In_Params
10640 Set_Is_Known_Valid (Formal_Id, True);
10642 -- Similar treatment for IN OUT parameters
10644 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10645 and then Validity_Check_In_Out_Params
10647 Set_Is_Known_Valid (Formal_Id, True);
10649 end Set_Formal_Validity;
10651 ------------------------
10652 -- Subtype_Conformant --
10653 ------------------------
10655 function Subtype_Conformant
10656 (New_Id : Entity_Id;
10657 Old_Id : Entity_Id;
10658 Skip_Controlling_Formals : Boolean := False) return Boolean
10662 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10663 Skip_Controlling_Formals => Skip_Controlling_Formals);
10665 end Subtype_Conformant;
10667 ---------------------
10668 -- Type_Conformant --
10669 ---------------------
10671 function Type_Conformant
10672 (New_Id : Entity_Id;
10673 Old_Id : Entity_Id;
10674 Skip_Controlling_Formals : Boolean := False) return Boolean
10678 May_Hide_Profile := False;
10680 (New_Id, Old_Id, Type_Conformant, False, Result,
10681 Skip_Controlling_Formals => Skip_Controlling_Formals);
10683 end Type_Conformant;
10685 -------------------------------
10686 -- Valid_Operator_Definition --
10687 -------------------------------
10689 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10692 Id : constant Name_Id := Chars (Designator);
10696 F := First_Formal (Designator);
10697 while Present (F) loop
10700 if Present (Default_Value (F)) then
10702 ("default values not allowed for operator parameters",
10705 -- For function instantiations that are operators, we must check
10706 -- separately that the corresponding generic only has in-parameters.
10707 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10708 -- an error could not arise in earlier versions of the language.
10710 elsif Ekind (F) /= E_In_Parameter then
10711 Error_Msg_N ("operators can only have IN parameters", F);
10717 -- Verify that user-defined operators have proper number of arguments
10718 -- First case of operators which can only be unary
10720 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10723 -- Case of operators which can be unary or binary
10725 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10726 N_OK := (N in 1 .. 2);
10728 -- All other operators can only be binary
10736 ("incorrect number of arguments for operator", Designator);
10740 and then Base_Type (Etype (Designator)) = Standard_Boolean
10741 and then not Is_Intrinsic_Subprogram (Designator)
10744 ("explicit definition of inequality not allowed", Designator);
10746 end Valid_Operator_Definition;