1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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_Limited_Return
160 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
161 -- types. Used only for simple return statements. Expr is the expression
164 procedure Check_Subprogram_Order (N : Node_Id);
165 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
166 -- the alpha ordering rule for N if this ordering requirement applicable.
168 procedure Check_Returns
172 Proc : Entity_Id := Empty);
173 -- Called to check for missing return statements in a function body, or for
174 -- returns present in a procedure body which has No_Return set. HSS is the
175 -- handled statement sequence for the subprogram body. This procedure
176 -- checks all flow paths to make sure they either have return (Mode = 'F',
177 -- used for functions) or do not have a return (Mode = 'P', used for
178 -- No_Return procedures). The flag Err is set if there are any control
179 -- paths not explicitly terminated by a return in the function case, and is
180 -- True otherwise. Proc is the entity for the procedure case and is used
181 -- in posting the warning message.
183 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
184 -- In Ada 2012, a primitive equality operator on an untagged record type
185 -- must appear before the type is frozen, and have the same visibility as
186 -- that of the type. This procedure checks that this rule is met, and
187 -- otherwise emits an error on the subprogram declaration and a warning
188 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
189 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
190 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
191 -- is set, otherwise the call has no effect.
193 procedure Enter_Overloaded_Entity (S : Entity_Id);
194 -- This procedure makes S, a new overloaded entity, into the first visible
195 -- entity with that name.
197 function Is_Non_Overriding_Operation
199 New_E : Entity_Id) return Boolean;
200 -- Enforce the rule given in 12.3(18): a private operation in an instance
201 -- overrides an inherited operation only if the corresponding operation
202 -- was overriding in the generic. This needs to be checked for primitive
203 -- operations of types derived (in the generic unit) from formal private
204 -- or formal derived types.
206 procedure Make_Inequality_Operator (S : Entity_Id);
207 -- Create the declaration for an inequality operator that is implicitly
208 -- created by a user-defined equality operator that yields a boolean.
210 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
211 -- Formal_Id is an formal parameter entity. This procedure deals with
212 -- setting the proper validity status for this entity, which depends on
213 -- the kind of parameter and the validity checking mode.
215 ---------------------------------------------
216 -- Analyze_Abstract_Subprogram_Declaration --
217 ---------------------------------------------
219 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
220 Scop : constant Entity_Id := Current_Scope;
221 Subp_Id : constant Entity_Id :=
222 Analyze_Subprogram_Specification (Specification (N));
225 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
227 Generate_Definition (Subp_Id);
229 Set_Is_Abstract_Subprogram (Subp_Id);
230 New_Overloaded_Entity (Subp_Id);
231 Check_Delayed_Subprogram (Subp_Id);
233 Set_Categorization_From_Scope (Subp_Id, Scop);
235 -- An abstract subprogram declared within a Ghost region is rendered
236 -- Ghost (SPARK RM 6.9(2)).
238 if Ghost_Mode > None then
239 Set_Is_Ghost_Entity (Subp_Id);
242 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
243 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
245 -- Issue a warning if the abstract subprogram is neither a dispatching
246 -- operation nor an operation that overrides an inherited subprogram or
247 -- predefined operator, since this most likely indicates a mistake.
249 elsif Warn_On_Redundant_Constructs
250 and then not Is_Dispatching_Operation (Subp_Id)
251 and then not Present (Overridden_Operation (Subp_Id))
252 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
253 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
256 ("abstract subprogram is not dispatching or overriding?r?", N);
259 Generate_Reference_To_Formals (Subp_Id);
260 Check_Eliminated (Subp_Id);
262 if Has_Aspects (N) then
263 Analyze_Aspect_Specifications (N, Subp_Id);
265 end Analyze_Abstract_Subprogram_Declaration;
267 ---------------------------------
268 -- Analyze_Expression_Function --
269 ---------------------------------
271 procedure Analyze_Expression_Function (N : Node_Id) is
272 Expr : constant Node_Id := Expression (N);
273 Loc : constant Source_Ptr := Sloc (N);
274 LocX : constant Source_Ptr := Sloc (Expr);
275 Spec : constant Node_Id := Specification (N);
280 -- If the expression is a completion, Prev is the entity whose
281 -- declaration is completed. Def_Id is needed to analyze the spec.
289 -- This is one of the occasions on which we transform the tree during
290 -- semantic analysis. If this is a completion, transform the expression
291 -- function into an equivalent subprogram body, and analyze it.
293 -- Expression functions are inlined unconditionally. The back-end will
294 -- determine whether this is possible.
296 Inline_Processing_Required := True;
298 -- Create a specification for the generated body. This must be done
299 -- prior to the analysis of the initial declaration.
301 New_Spec := Copy_Subprogram_Spec (Spec);
302 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
304 -- If there are previous overloadable entities with the same name,
305 -- check whether any of them is completed by the expression function.
306 -- In a generic context a formal subprogram has no completion.
309 and then Is_Overloadable (Prev)
310 and then not Is_Formal_Subprogram (Prev)
312 Def_Id := Analyze_Subprogram_Specification (Spec);
313 Prev := Find_Corresponding_Spec (N);
315 -- The previous entity may be an expression function as well, in
316 -- which case the redeclaration is illegal.
319 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
320 N_Expression_Function
322 Error_Msg_Sloc := Sloc (Prev);
323 Error_Msg_N ("& conflicts with declaration#", Def_Id);
328 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
331 Make_Subprogram_Body (Loc,
332 Specification => New_Spec,
333 Declarations => Empty_List,
334 Handled_Statement_Sequence =>
335 Make_Handled_Sequence_Of_Statements (LocX,
336 Statements => New_List (Ret)));
337 Set_Was_Expression_Function (New_Body);
339 -- If the expression completes a generic subprogram, we must create a
340 -- separate node for the body, because at instantiation the original
341 -- node of the generic copy must be a generic subprogram body, and
342 -- cannot be a expression function. Otherwise we just rewrite the
343 -- expression with the non-generic body.
345 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
346 Insert_After (N, New_Body);
348 -- Propagate any aspects or pragmas that apply to the expression
349 -- function to the proper body when the expression function acts
352 if Has_Aspects (N) then
353 Move_Aspects (N, To => New_Body);
356 Relocate_Pragmas_To_Body (New_Body);
358 Rewrite (N, Make_Null_Statement (Loc));
359 Set_Has_Completion (Prev, False);
362 Set_Is_Inlined (Prev);
364 -- If the expression function is a completion, the previous declaration
365 -- must come from source. We know already that it appears in the current
366 -- scope. The entity itself may be internally created if within a body
370 and then Comes_From_Source (Parent (Prev))
371 and then not Is_Formal_Subprogram (Prev)
373 Set_Has_Completion (Prev, False);
374 Set_Is_Inlined (Prev);
376 -- An expression function that is a completion freezes the
377 -- expression. This means freezing the return type, and if it is
378 -- an access type, freezing its designated type as well.
380 -- Note that we cannot defer this freezing to the analysis of the
381 -- expression itself, because a freeze node might appear in a nested
382 -- scope, leading to an elaboration order issue in gigi.
384 Freeze_Before (N, Etype (Prev));
386 if Is_Access_Type (Etype (Prev)) then
387 Freeze_Before (N, Designated_Type (Etype (Prev)));
390 -- For navigation purposes, indicate that the function is a body
392 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
393 Rewrite (N, New_Body);
395 -- Correct the parent pointer of the aspect specification list to
396 -- reference the rewritten node.
398 if Has_Aspects (N) then
399 Set_Parent (Aspect_Specifications (N), N);
402 -- Propagate any pragmas that apply to the expression function to the
403 -- proper body when the expression function acts as a completion.
404 -- Aspects are automatically transfered because of node rewriting.
406 Relocate_Pragmas_To_Body (N);
409 -- Prev is the previous entity with the same name, but it is can
410 -- be an unrelated spec that is not completed by the expression
411 -- function. In that case the relevant entity is the one in the body.
412 -- Not clear that the backend can inline it in this case ???
414 if Has_Completion (Prev) then
416 -- The formals of the expression function are body formals,
417 -- and do not appear in the ali file, which will only contain
418 -- references to the formals of the original subprogram spec.
425 F1 := First_Formal (Def_Id);
426 F2 := First_Formal (Prev);
428 while Present (F1) loop
429 Set_Spec_Entity (F1, F2);
436 Set_Is_Inlined (Defining_Entity (New_Body));
439 -- If this is not a completion, create both a declaration and a body, so
440 -- that the expression can be inlined whenever possible.
443 -- An expression function that is not a completion is not a
444 -- subprogram declaration, and thus cannot appear in a protected
447 if Nkind (Parent (N)) = N_Protected_Definition then
449 ("an expression function is not a legal protected operation", N);
452 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
454 -- Correct the parent pointer of the aspect specification list to
455 -- reference the rewritten node.
457 if Has_Aspects (N) then
458 Set_Parent (Aspect_Specifications (N), N);
462 Def_Id := Defining_Entity (N);
464 -- If aspect SPARK_Mode was specified on the body, it needs to be
465 -- repeated both on the generated spec and the body.
467 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
469 if Present (Asp) then
470 Asp := New_Copy_Tree (Asp);
471 Set_Analyzed (Asp, False);
472 Set_Aspect_Specifications (New_Body, New_List (Asp));
475 -- Within a generic pre-analyze the original expression for name
476 -- capture. The body is also generated but plays no role in
477 -- this because it is not part of the original source.
479 if Inside_A_Generic then
480 Set_Has_Completion (Def_Id);
482 Install_Formals (Def_Id);
483 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
487 Set_Is_Inlined (Defining_Entity (N));
489 -- Establish the linkages between the spec and the body. These are
490 -- used when the expression function acts as the prefix of attribute
491 -- 'Access in order to freeze the original expression which has been
492 -- moved to the generated body.
494 Set_Corresponding_Body (N, Defining_Entity (New_Body));
495 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
497 -- To prevent premature freeze action, insert the new body at the end
498 -- of the current declarations, or at the end of the package spec.
499 -- However, resolve usage names now, to prevent spurious visibility
500 -- on later entities. Note that the function can now be called in
501 -- the current declarative part, which will appear to be prior to
502 -- the presence of the body in the code. There are nevertheless no
503 -- order of elaboration issues because all name resolution has taken
504 -- place at the point of declaration.
507 Decls : List_Id := List_Containing (N);
508 Expr : constant Node_Id := Expression (Ret);
509 Par : constant Node_Id := Parent (Decls);
510 Typ : constant Entity_Id := Etype (Def_Id);
513 -- If this is a wrapper created for in an instance for a formal
514 -- subprogram, insert body after declaration, to be analyzed when
515 -- the enclosing instance is analyzed.
518 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
520 Insert_After (N, New_Body);
523 if Nkind (Par) = N_Package_Specification
524 and then Decls = Visible_Declarations (Par)
525 and then Present (Private_Declarations (Par))
526 and then not Is_Empty_List (Private_Declarations (Par))
528 Decls := Private_Declarations (Par);
531 Insert_After (Last (Decls), New_Body);
533 -- Preanalyze the expression for name capture, except in an
534 -- instance, where this has been done during generic analysis,
535 -- and will be redone when analyzing the body.
537 Set_Parent (Expr, Ret);
539 Install_Formals (Def_Id);
541 if not In_Instance then
542 Preanalyze_Spec_Expression (Expr, Typ);
543 Check_Limited_Return (Original_Node (N), Expr, Typ);
551 -- If the return expression is a static constant, we suppress warning
552 -- messages on unused formals, which in most cases will be noise.
554 Set_Is_Trivial_Subprogram
555 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
556 end Analyze_Expression_Function;
558 ----------------------------------------
559 -- Analyze_Extended_Return_Statement --
560 ----------------------------------------
562 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
564 Check_Compiler_Unit ("extended return statement", N);
565 Analyze_Return_Statement (N);
566 end Analyze_Extended_Return_Statement;
568 ----------------------------
569 -- Analyze_Function_Call --
570 ----------------------------
572 procedure Analyze_Function_Call (N : Node_Id) is
573 Actuals : constant List_Id := Parameter_Associations (N);
574 Func_Nam : constant Node_Id := Name (N);
580 -- A call of the form A.B (X) may be an Ada 2005 call, which is
581 -- rewritten as B (A, X). If the rewriting is successful, the call
582 -- has been analyzed and we just return.
584 if Nkind (Func_Nam) = N_Selected_Component
585 and then Name (N) /= Func_Nam
586 and then Is_Rewrite_Substitution (N)
587 and then Present (Etype (N))
592 -- If error analyzing name, then set Any_Type as result type and return
594 if Etype (Func_Nam) = Any_Type then
595 Set_Etype (N, Any_Type);
599 -- Otherwise analyze the parameters
601 if Present (Actuals) then
602 Actual := First (Actuals);
603 while Present (Actual) loop
605 Check_Parameterless_Call (Actual);
611 end Analyze_Function_Call;
613 -----------------------------
614 -- Analyze_Function_Return --
615 -----------------------------
617 procedure Analyze_Function_Return (N : Node_Id) is
618 Loc : constant Source_Ptr := Sloc (N);
619 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
620 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
622 R_Type : constant Entity_Id := Etype (Scope_Id);
623 -- Function result subtype
625 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
626 -- Apply legality rule of 6.5 (8.2) to the access discriminants of an
627 -- aggregate in a return statement.
629 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
630 -- Check that the return_subtype_indication properly matches the result
631 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
633 -----------------------------------
634 -- Check_Aggregate_Accessibility --
635 -----------------------------------
637 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
638 Typ : constant Entity_Id := Etype (Aggr);
645 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
646 Discr := First_Discriminant (Typ);
647 Assoc := First (Component_Associations (Aggr));
648 while Present (Discr) loop
649 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
650 Expr := Expression (Assoc);
651 if Nkind (Expr) = N_Attribute_Reference
652 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
654 Obj := Prefix (Expr);
655 while Nkind_In (Obj, N_Indexed_Component,
656 N_Selected_Component)
661 -- No check needed for an aliased formal.
662 -- A run-time check may still be needed ???
664 if Is_Entity_Name (Obj)
665 and then Is_Formal (Entity (Obj))
666 and then Is_Aliased (Entity (Obj))
670 elsif Object_Access_Level (Obj) >
671 Scope_Depth (Scope (Scope_Id))
674 ("access discriminant in return aggregate would be "
675 & "a dangling reference", Obj);
680 Next_Discriminant (Discr);
683 end Check_Aggregate_Accessibility;
685 -------------------------------------
686 -- Check_Return_Subtype_Indication --
687 -------------------------------------
689 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
690 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
692 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
693 -- Subtype given in the extended return statement (must match R_Type)
695 Subtype_Ind : constant Node_Id :=
696 Object_Definition (Original_Node (Obj_Decl));
698 R_Type_Is_Anon_Access : constant Boolean :=
700 E_Anonymous_Access_Subprogram_Type,
701 E_Anonymous_Access_Protected_Subprogram_Type,
702 E_Anonymous_Access_Type);
703 -- True if return type of the function is an anonymous access type
704 -- Can't we make Is_Anonymous_Access_Type in einfo ???
706 R_Stm_Type_Is_Anon_Access : constant Boolean :=
707 Ekind_In (R_Stm_Type,
708 E_Anonymous_Access_Subprogram_Type,
709 E_Anonymous_Access_Protected_Subprogram_Type,
710 E_Anonymous_Access_Type);
711 -- True if type of the return object is an anonymous access type
713 procedure Error_No_Match (N : Node_Id);
714 -- Output error messages for case where types do not statically
715 -- match. N is the location for the messages.
721 procedure Error_No_Match (N : Node_Id) is
724 ("subtype must statically match function result subtype", N);
726 if not Predicates_Match (R_Stm_Type, R_Type) then
727 Error_Msg_Node_2 := R_Type;
729 ("\predicate of& does not match predicate of&",
734 -- Start of processing for Check_Return_Subtype_Indication
737 -- First, avoid cascaded errors
739 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
743 -- "return access T" case; check that the return statement also has
744 -- "access T", and that the subtypes statically match:
745 -- if this is an access to subprogram the signatures must match.
747 if R_Type_Is_Anon_Access then
748 if R_Stm_Type_Is_Anon_Access then
750 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
752 if Base_Type (Designated_Type (R_Stm_Type)) /=
753 Base_Type (Designated_Type (R_Type))
754 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
756 Error_No_Match (Subtype_Mark (Subtype_Ind));
760 -- For two anonymous access to subprogram types, the
761 -- types themselves must be type conformant.
763 if not Conforming_Types
764 (R_Stm_Type, R_Type, Fully_Conformant)
766 Error_No_Match (Subtype_Ind);
771 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
774 -- If the return object is of an anonymous access type, then report
775 -- an error if the function's result type is not also anonymous.
777 elsif R_Stm_Type_Is_Anon_Access
778 and then not R_Type_Is_Anon_Access
780 Error_Msg_N ("anonymous access not allowed for function with "
781 & "named access result", Subtype_Ind);
783 -- Subtype indication case: check that the return object's type is
784 -- covered by the result type, and that the subtypes statically match
785 -- when the result subtype is constrained. Also handle record types
786 -- with unknown discriminants for which we have built the underlying
787 -- record view. Coverage is needed to allow specific-type return
788 -- objects when the result type is class-wide (see AI05-32).
790 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
791 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
795 Underlying_Record_View (Base_Type (R_Stm_Type))))
797 -- A null exclusion may be present on the return type, on the
798 -- function specification, on the object declaration or on the
801 if Is_Access_Type (R_Type)
803 (Can_Never_Be_Null (R_Type)
804 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
805 Can_Never_Be_Null (R_Stm_Type)
807 Error_No_Match (Subtype_Ind);
810 -- AI05-103: for elementary types, subtypes must statically match
812 if Is_Constrained (R_Type)
813 or else Is_Access_Type (R_Type)
815 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
816 Error_No_Match (Subtype_Ind);
820 -- All remaining cases are illegal
822 -- Note: previous versions of this subprogram allowed the return
823 -- value to be the ancestor of the return type if the return type
824 -- was a null extension. This was plainly incorrect.
828 ("wrong type for return_subtype_indication", Subtype_Ind);
830 end Check_Return_Subtype_Indication;
832 ---------------------
833 -- Local Variables --
834 ---------------------
839 -- Start of processing for Analyze_Function_Return
842 Set_Return_Present (Scope_Id);
844 if Nkind (N) = N_Simple_Return_Statement then
845 Expr := Expression (N);
847 -- Guard against a malformed expression. The parser may have tried to
848 -- recover but the node is not analyzable.
850 if Nkind (Expr) = N_Error then
851 Set_Etype (Expr, Any_Type);
852 Expander_Mode_Save_And_Set (False);
856 -- The resolution of a controlled [extension] aggregate associated
857 -- with a return statement creates a temporary which needs to be
858 -- finalized on function exit. Wrap the return statement inside a
859 -- block so that the finalization machinery can detect this case.
860 -- This early expansion is done only when the return statement is
861 -- not part of a handled sequence of statements.
863 if Nkind_In (Expr, N_Aggregate,
864 N_Extension_Aggregate)
865 and then Needs_Finalization (R_Type)
866 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
869 Make_Block_Statement (Loc,
870 Handled_Statement_Sequence =>
871 Make_Handled_Sequence_Of_Statements (Loc,
872 Statements => New_List (Relocate_Node (N)))));
880 -- Ada 2005 (AI-251): If the type of the returned object is
881 -- an access to an interface type then we add an implicit type
882 -- conversion to force the displacement of the "this" pointer to
883 -- reference the secondary dispatch table. We cannot delay the
884 -- generation of this implicit conversion until the expansion
885 -- because in this case the type resolution changes the decoration
886 -- of the expression node to match R_Type; by contrast, if the
887 -- returned object is a class-wide interface type then it is too
888 -- early to generate here the implicit conversion since the return
889 -- statement may be rewritten by the expander into an extended
890 -- return statement whose expansion takes care of adding the
891 -- implicit type conversion to displace the pointer to the object.
894 and then Serious_Errors_Detected = 0
895 and then Is_Access_Type (R_Type)
896 and then Nkind (Expr) /= N_Null
897 and then Is_Interface (Designated_Type (R_Type))
898 and then Is_Progenitor (Designated_Type (R_Type),
899 Designated_Type (Etype (Expr)))
901 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
905 Resolve (Expr, R_Type);
906 Check_Limited_Return (N, Expr, R_Type);
908 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
909 Check_Aggregate_Accessibility (Expr);
913 -- RETURN only allowed in SPARK as the last statement in function
915 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
917 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
918 or else Present (Next (N)))
920 Check_SPARK_05_Restriction
921 ("RETURN should be the last statement in function", N);
925 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
926 Obj_Decl := Last (Return_Object_Declarations (N));
928 -- Analyze parts specific to extended_return_statement:
931 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
932 HSS : constant Node_Id := Handled_Statement_Sequence (N);
935 Expr := Expression (Obj_Decl);
937 -- Note: The check for OK_For_Limited_Init will happen in
938 -- Analyze_Object_Declaration; we treat it as a normal
939 -- object declaration.
941 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
944 Check_Return_Subtype_Indication (Obj_Decl);
946 if Present (HSS) then
949 if Present (Exception_Handlers (HSS)) then
951 -- ???Has_Nested_Block_With_Handler needs to be set.
952 -- Probably by creating an actual N_Block_Statement.
953 -- Probably in Expand.
959 -- Mark the return object as referenced, since the return is an
960 -- implicit reference of the object.
962 Set_Referenced (Defining_Identifier (Obj_Decl));
964 Check_References (Stm_Entity);
966 -- Check RM 6.5 (5.9/3)
969 if Ada_Version < Ada_2012 then
971 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
972 -- Can it really happen (extended return???)
975 ("aliased only allowed for limited return objects "
976 & "in Ada 2012??", N);
978 elsif not Is_Limited_View (R_Type) then
980 ("aliased only allowed for limited return objects", N);
986 -- Case of Expr present
990 -- Defend against previous errors
992 and then Nkind (Expr) /= N_Empty
993 and then Present (Etype (Expr))
995 -- Apply constraint check. Note that this is done before the implicit
996 -- conversion of the expression done for anonymous access types to
997 -- ensure correct generation of the null-excluding check associated
998 -- with null-excluding expressions found in return statements.
1000 Apply_Constraint_Check (Expr, R_Type);
1002 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1003 -- type, apply an implicit conversion of the expression to that type
1004 -- to force appropriate static and run-time accessibility checks.
1006 if Ada_Version >= Ada_2005
1007 and then Ekind (R_Type) = E_Anonymous_Access_Type
1009 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1010 Analyze_And_Resolve (Expr, R_Type);
1012 -- If this is a local anonymous access to subprogram, the
1013 -- accessibility check can be applied statically. The return is
1014 -- illegal if the access type of the return expression is declared
1015 -- inside of the subprogram (except if it is the subtype indication
1016 -- of an extended return statement).
1018 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1019 if not Comes_From_Source (Current_Scope)
1020 or else Ekind (Current_Scope) = E_Return_Statement
1025 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1027 Error_Msg_N ("cannot return local access to subprogram", N);
1030 -- The expression cannot be of a formal incomplete type
1032 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1033 and then Is_Generic_Type (Etype (Expr))
1036 ("cannot return expression of a formal incomplete type", N);
1039 -- If the result type is class-wide, then check that the return
1040 -- expression's type is not declared at a deeper level than the
1041 -- function (RM05-6.5(5.6/2)).
1043 if Ada_Version >= Ada_2005
1044 and then Is_Class_Wide_Type (R_Type)
1046 if Type_Access_Level (Etype (Expr)) >
1047 Subprogram_Access_Level (Scope_Id)
1050 ("level of return expression type is deeper than "
1051 & "class-wide function!", Expr);
1055 -- Check incorrect use of dynamically tagged expression
1057 if Is_Tagged_Type (R_Type) then
1058 Check_Dynamically_Tagged_Expression
1064 -- ??? A real run-time accessibility check is needed in cases
1065 -- involving dereferences of access parameters. For now we just
1066 -- check the static cases.
1068 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1069 and then Is_Limited_View (Etype (Scope_Id))
1070 and then Object_Access_Level (Expr) >
1071 Subprogram_Access_Level (Scope_Id)
1073 -- Suppress the message in a generic, where the rewriting
1076 if Inside_A_Generic then
1081 Make_Raise_Program_Error (Loc,
1082 Reason => PE_Accessibility_Check_Failed));
1085 Error_Msg_Warn := SPARK_Mode /= On;
1086 Error_Msg_N ("cannot return a local value by reference<<", N);
1087 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1091 if Known_Null (Expr)
1092 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1093 and then Null_Exclusion_Present (Parent (Scope_Id))
1095 Apply_Compile_Time_Constraint_Error
1097 Msg => "(Ada 2005) null not allowed for "
1098 & "null-excluding return??",
1099 Reason => CE_Null_Not_Allowed);
1102 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1103 -- has no initializing expression.
1105 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1106 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1107 Subprogram_Access_Level (Scope_Id)
1110 ("level of return expression type is deeper than "
1111 & "class-wide function!", Obj_Decl);
1114 end Analyze_Function_Return;
1116 -------------------------------------
1117 -- Analyze_Generic_Subprogram_Body --
1118 -------------------------------------
1120 procedure Analyze_Generic_Subprogram_Body
1124 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1125 Kind : constant Entity_Kind := Ekind (Gen_Id);
1126 Body_Id : Entity_Id;
1131 -- Copy body and disable expansion while analyzing the generic For a
1132 -- stub, do not copy the stub (which would load the proper body), this
1133 -- will be done when the proper body is analyzed.
1135 if Nkind (N) /= N_Subprogram_Body_Stub then
1136 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1139 -- Once the contents of the generic copy and the template are
1140 -- swapped, do the same for their respective aspect specifications.
1142 Exchange_Aspects (N, New_N);
1144 -- Collect all contract-related source pragmas found within the
1145 -- template and attach them to the contract of the subprogram body.
1146 -- This contract is used in the capture of global references within
1149 Create_Generic_Contract (N);
1154 Spec := Specification (N);
1156 -- Within the body of the generic, the subprogram is callable, and
1157 -- behaves like the corresponding non-generic unit.
1159 Body_Id := Defining_Entity (Spec);
1161 if Kind = E_Generic_Procedure
1162 and then Nkind (Spec) /= N_Procedure_Specification
1164 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1167 elsif Kind = E_Generic_Function
1168 and then Nkind (Spec) /= N_Function_Specification
1170 Error_Msg_N ("invalid body for generic function ", Body_Id);
1174 Set_Corresponding_Body (Gen_Decl, Body_Id);
1176 if Has_Completion (Gen_Id)
1177 and then Nkind (Parent (N)) /= N_Subunit
1179 Error_Msg_N ("duplicate generic body", N);
1182 Set_Has_Completion (Gen_Id);
1185 if Nkind (N) = N_Subprogram_Body_Stub then
1186 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1188 Set_Corresponding_Spec (N, Gen_Id);
1191 if Nkind (Parent (N)) = N_Compilation_Unit then
1192 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1195 -- Make generic parameters immediately visible in the body. They are
1196 -- needed to process the formals declarations. Then make the formals
1197 -- visible in a separate step.
1199 Push_Scope (Gen_Id);
1203 First_Ent : Entity_Id;
1206 First_Ent := First_Entity (Gen_Id);
1209 while Present (E) and then not Is_Formal (E) loop
1214 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1216 -- Now generic formals are visible, and the specification can be
1217 -- analyzed, for subsequent conformance check.
1219 Body_Id := Analyze_Subprogram_Specification (Spec);
1221 -- Make formal parameters visible
1225 -- E is the first formal parameter, we loop through the formals
1226 -- installing them so that they will be visible.
1228 Set_First_Entity (Gen_Id, E);
1229 while Present (E) loop
1235 -- Visible generic entity is callable within its own body
1237 Set_Ekind (Gen_Id, Ekind (Body_Id));
1238 Set_Ekind (Body_Id, E_Subprogram_Body);
1239 Set_Convention (Body_Id, Convention (Gen_Id));
1240 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1241 Set_Scope (Body_Id, Scope (Gen_Id));
1243 -- Inherit the "ghostness" of the generic spec. Note that this
1244 -- property is not directly inherited as the body may be subject
1245 -- to a different Ghost assertion policy.
1247 if Ghost_Mode > None or else Is_Ghost_Entity (Gen_Id) then
1248 Set_Is_Ghost_Entity (Body_Id);
1250 -- The Ghost policy in effect at the point of declaration and at
1251 -- the point of completion must match (SPARK RM 6.9(14)).
1253 Check_Ghost_Completion (Gen_Id, Body_Id);
1256 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1258 if Nkind (N) = N_Subprogram_Body_Stub then
1260 -- No body to analyze, so restore state of generic unit
1262 Set_Ekind (Gen_Id, Kind);
1263 Set_Ekind (Body_Id, Kind);
1265 if Present (First_Ent) then
1266 Set_First_Entity (Gen_Id, First_Ent);
1273 -- If this is a compilation unit, it must be made visible explicitly,
1274 -- because the compilation of the declaration, unlike other library
1275 -- unit declarations, does not. If it is not a unit, the following
1276 -- is redundant but harmless.
1278 Set_Is_Immediately_Visible (Gen_Id);
1279 Reference_Body_Formals (Gen_Id, Body_Id);
1281 if Is_Child_Unit (Gen_Id) then
1282 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1285 Set_Actual_Subtypes (N, Current_Scope);
1287 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1288 Set_SPARK_Pragma_Inherited (Body_Id);
1290 -- Analyze any aspect specifications that appear on the generic
1293 if Has_Aspects (N) then
1294 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1297 Analyze_Declarations (Declarations (N));
1300 -- Process the contract of the subprogram body after all declarations
1301 -- have been analyzed. This ensures that any contract-related pragmas
1302 -- are available through the N_Contract node of the body.
1304 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1306 Analyze (Handled_Statement_Sequence (N));
1307 Save_Global_References (Original_Node (N));
1309 -- Prior to exiting the scope, include generic formals again (if any
1310 -- are present) in the set of local entities.
1312 if Present (First_Ent) then
1313 Set_First_Entity (Gen_Id, First_Ent);
1316 Check_References (Gen_Id);
1319 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1321 Check_Subprogram_Order (N);
1323 -- Outside of its body, unit is generic again
1325 Set_Ekind (Gen_Id, Kind);
1326 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1329 Style.Check_Identifier (Body_Id, Gen_Id);
1333 end Analyze_Generic_Subprogram_Body;
1335 ----------------------------
1336 -- Analyze_Null_Procedure --
1337 ----------------------------
1339 procedure Analyze_Null_Procedure
1341 Is_Completion : out Boolean)
1343 Loc : constant Source_Ptr := Sloc (N);
1344 Spec : constant Node_Id := Specification (N);
1345 Designator : Entity_Id;
1347 Null_Body : Node_Id := Empty;
1351 -- Capture the profile of the null procedure before analysis, for
1352 -- expansion at the freeze point and at each point of call. The body is
1353 -- used if the procedure has preconditions, or if it is a completion. In
1354 -- the first case the body is analyzed at the freeze point, in the other
1355 -- it replaces the null procedure declaration.
1358 Make_Subprogram_Body (Loc,
1359 Specification => New_Copy_Tree (Spec),
1360 Declarations => New_List,
1361 Handled_Statement_Sequence =>
1362 Make_Handled_Sequence_Of_Statements (Loc,
1363 Statements => New_List (Make_Null_Statement (Loc))));
1365 -- Create new entities for body and formals
1367 Set_Defining_Unit_Name (Specification (Null_Body),
1368 Make_Defining_Identifier
1369 (Sloc (Defining_Entity (N)),
1370 Chars (Defining_Entity (N))));
1372 Form := First (Parameter_Specifications (Specification (Null_Body)));
1373 while Present (Form) loop
1374 Set_Defining_Identifier (Form,
1375 Make_Defining_Identifier
1376 (Sloc (Defining_Identifier (Form)),
1377 Chars (Defining_Identifier (Form))));
1381 -- Determine whether the null procedure may be a completion of a generic
1382 -- suprogram, in which case we use the new null body as the completion
1383 -- and set minimal semantic information on the original declaration,
1384 -- which is rewritten as a null statement.
1386 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1388 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1389 Insert_Before (N, Null_Body);
1390 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1392 Rewrite (N, Make_Null_Statement (Loc));
1393 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1394 Is_Completion := True;
1398 -- Resolve the types of the formals now, because the freeze point
1399 -- may appear in a different context, e.g. an instantiation.
1401 Form := First (Parameter_Specifications (Specification (Null_Body)));
1402 while Present (Form) loop
1403 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1404 Find_Type (Parameter_Type (Form));
1407 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1409 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1412 -- The case of a null procedure with a formal that is an
1413 -- access_to_subprogram type, and that is used as an actual
1414 -- in an instantiation is left to the enthusiastic reader.
1423 -- If there are previous overloadable entities with the same name,
1424 -- check whether any of them is completed by the null procedure.
1426 if Present (Prev) and then Is_Overloadable (Prev) then
1427 Designator := Analyze_Subprogram_Specification (Spec);
1428 Prev := Find_Corresponding_Spec (N);
1431 if No (Prev) or else not Comes_From_Source (Prev) then
1432 Designator := Analyze_Subprogram_Specification (Spec);
1433 Set_Has_Completion (Designator);
1435 -- Signal to caller that this is a procedure declaration
1437 Is_Completion := False;
1439 -- Null procedures are always inlined, but generic formal subprograms
1440 -- which appear as such in the internal instance of formal packages,
1441 -- need no completion and are not marked Inline.
1444 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1446 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1447 Set_Body_To_Inline (N, Null_Body);
1448 Set_Is_Inlined (Designator);
1452 -- The null procedure is a completion. We unconditionally rewrite
1453 -- this as a null body (even if expansion is not active), because
1454 -- there are various error checks that are applied on this body
1455 -- when it is analyzed (e.g. correct aspect placement).
1457 if Has_Completion (Prev) then
1458 Error_Msg_Sloc := Sloc (Prev);
1459 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1462 Is_Completion := True;
1463 Rewrite (N, Null_Body);
1466 end Analyze_Null_Procedure;
1468 -----------------------------
1469 -- Analyze_Operator_Symbol --
1470 -----------------------------
1472 -- An operator symbol such as "+" or "and" may appear in context where the
1473 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1474 -- is just a string, as in (conjunction = "or"). In these cases the parser
1475 -- generates this node, and the semantics does the disambiguation. Other
1476 -- such case are actuals in an instantiation, the generic unit in an
1477 -- instantiation, and pragma arguments.
1479 procedure Analyze_Operator_Symbol (N : Node_Id) is
1480 Par : constant Node_Id := Parent (N);
1483 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1484 or else Nkind (Par) = N_Function_Instantiation
1485 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1486 or else (Nkind (Par) = N_Pragma_Argument_Association
1487 and then not Is_Pragma_String_Literal (Par))
1488 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1489 or else (Nkind (Par) = N_Attribute_Reference
1490 and then Attribute_Name (Par) /= Name_Value)
1492 Find_Direct_Name (N);
1495 Change_Operator_Symbol_To_String_Literal (N);
1498 end Analyze_Operator_Symbol;
1500 -----------------------------------
1501 -- Analyze_Parameter_Association --
1502 -----------------------------------
1504 procedure Analyze_Parameter_Association (N : Node_Id) is
1506 Analyze (Explicit_Actual_Parameter (N));
1507 end Analyze_Parameter_Association;
1509 ----------------------------
1510 -- Analyze_Procedure_Call --
1511 ----------------------------
1513 procedure Analyze_Procedure_Call (N : Node_Id) is
1514 procedure Analyze_Call_And_Resolve;
1515 -- Do Analyze and Resolve calls for procedure call
1516 -- At end, check illegal order dependence.
1518 ------------------------------
1519 -- Analyze_Call_And_Resolve --
1520 ------------------------------
1522 procedure Analyze_Call_And_Resolve is
1524 if Nkind (N) = N_Procedure_Call_Statement then
1526 Resolve (N, Standard_Void_Type);
1530 end Analyze_Call_And_Resolve;
1534 Actuals : constant List_Id := Parameter_Associations (N);
1535 Loc : constant Source_Ptr := Sloc (N);
1536 P : constant Node_Id := Name (N);
1540 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1542 -- Start of processing for Analyze_Procedure_Call
1545 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1546 -- a procedure call or an entry call. The prefix may denote an access
1547 -- to subprogram type, in which case an implicit dereference applies.
1548 -- If the prefix is an indexed component (without implicit dereference)
1549 -- then the construct denotes a call to a member of an entire family.
1550 -- If the prefix is a simple name, it may still denote a call to a
1551 -- parameterless member of an entry family. Resolution of these various
1552 -- interpretations is delicate.
1554 -- Do not analyze machine code statements to avoid rejecting them in
1557 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1558 Set_Etype (P, Standard_Void_Type);
1563 -- If this is a call of the form Obj.Op, the call may have been analyzed
1564 -- and possibly rewritten into a block, in which case we are done.
1566 if Analyzed (N) then
1570 -- If there is an error analyzing the name (which may have been
1571 -- rewritten if the original call was in prefix notation) then error
1572 -- has been emitted already, mark node and return.
1574 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1575 Set_Etype (N, Any_Type);
1579 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1580 -- Set the mode now to ensure that any nodes generated during analysis
1581 -- and expansion are properly marked as Ghost.
1585 -- Otherwise analyze the parameters
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1590 while Present (Actual) loop
1592 Check_Parameterless_Call (Actual);
1597 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1599 if Nkind (P) = N_Attribute_Reference
1600 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1602 Name_Elab_Subp_Body)
1604 if Present (Actuals) then
1606 ("no parameters allowed for this call", First (Actuals));
1610 Set_Etype (N, Standard_Void_Type);
1613 elsif Is_Entity_Name (P)
1614 and then Is_Record_Type (Etype (Entity (P)))
1615 and then Remote_AST_I_Dereference (P)
1617 Ghost_Mode := Save_Ghost_Mode;
1620 elsif Is_Entity_Name (P)
1621 and then Ekind (Entity (P)) /= E_Entry_Family
1623 if Is_Access_Type (Etype (P))
1624 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1625 and then No (Actuals)
1626 and then Comes_From_Source (N)
1628 Error_Msg_N ("missing explicit dereference in call", N);
1631 Analyze_Call_And_Resolve;
1633 -- If the prefix is the simple name of an entry family, this is a
1634 -- parameterless call from within the task body itself.
1636 elsif Is_Entity_Name (P)
1637 and then Nkind (P) = N_Identifier
1638 and then Ekind (Entity (P)) = E_Entry_Family
1639 and then Present (Actuals)
1640 and then No (Next (First (Actuals)))
1642 -- Can be call to parameterless entry family. What appears to be the
1643 -- sole argument is in fact the entry index. Rewrite prefix of node
1644 -- accordingly. Source representation is unchanged by this
1648 Make_Indexed_Component (Loc,
1650 Make_Selected_Component (Loc,
1651 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1652 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1653 Expressions => Actuals);
1654 Set_Name (N, New_N);
1655 Set_Etype (New_N, Standard_Void_Type);
1656 Set_Parameter_Associations (N, No_List);
1657 Analyze_Call_And_Resolve;
1659 elsif Nkind (P) = N_Explicit_Dereference then
1660 if Ekind (Etype (P)) = E_Subprogram_Type then
1661 Analyze_Call_And_Resolve;
1663 Error_Msg_N ("expect access to procedure in call", P);
1666 -- The name can be a selected component or an indexed component that
1667 -- yields an access to subprogram. Such a prefix is legal if the call
1668 -- has parameter associations.
1670 elsif Is_Access_Type (Etype (P))
1671 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1673 if Present (Actuals) then
1674 Analyze_Call_And_Resolve;
1676 Error_Msg_N ("missing explicit dereference in call ", N);
1679 -- If not an access to subprogram, then the prefix must resolve to the
1680 -- name of an entry, entry family, or protected operation.
1682 -- For the case of a simple entry call, P is a selected component where
1683 -- the prefix is the task and the selector name is the entry. A call to
1684 -- a protected procedure will have the same syntax. If the protected
1685 -- object contains overloaded operations, the entity may appear as a
1686 -- function, the context will select the operation whose type is Void.
1688 elsif Nkind (P) = N_Selected_Component
1689 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1693 Analyze_Call_And_Resolve;
1695 elsif Nkind (P) = N_Selected_Component
1696 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1697 and then Present (Actuals)
1698 and then No (Next (First (Actuals)))
1700 -- Can be call to parameterless entry family. What appears to be the
1701 -- sole argument is in fact the entry index. Rewrite prefix of node
1702 -- accordingly. Source representation is unchanged by this
1706 Make_Indexed_Component (Loc,
1707 Prefix => New_Copy (P),
1708 Expressions => Actuals);
1709 Set_Name (N, New_N);
1710 Set_Etype (New_N, Standard_Void_Type);
1711 Set_Parameter_Associations (N, No_List);
1712 Analyze_Call_And_Resolve;
1714 -- For the case of a reference to an element of an entry family, P is
1715 -- an indexed component whose prefix is a selected component (task and
1716 -- entry family), and whose index is the entry family index.
1718 elsif Nkind (P) = N_Indexed_Component
1719 and then Nkind (Prefix (P)) = N_Selected_Component
1720 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1722 Analyze_Call_And_Resolve;
1724 -- If the prefix is the name of an entry family, it is a call from
1725 -- within the task body itself.
1727 elsif Nkind (P) = N_Indexed_Component
1728 and then Nkind (Prefix (P)) = N_Identifier
1729 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1732 Make_Selected_Component (Loc,
1733 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1734 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1735 Rewrite (Prefix (P), New_N);
1737 Analyze_Call_And_Resolve;
1739 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1740 -- procedure name, so the construct can only be a qualified expression.
1742 elsif Nkind (P) = N_Qualified_Expression
1743 and then Ada_Version >= Ada_2012
1745 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1748 -- Anything else is an error
1751 Error_Msg_N ("invalid procedure or entry call", N);
1754 Ghost_Mode := Save_Ghost_Mode;
1755 end Analyze_Procedure_Call;
1757 ------------------------------
1758 -- Analyze_Return_Statement --
1759 ------------------------------
1761 procedure Analyze_Return_Statement (N : Node_Id) is
1763 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1764 N_Extended_Return_Statement));
1766 Returns_Object : constant Boolean :=
1767 Nkind (N) = N_Extended_Return_Statement
1769 (Nkind (N) = N_Simple_Return_Statement
1770 and then Present (Expression (N)));
1771 -- True if we're returning something; that is, "return <expression>;"
1772 -- or "return Result : T [:= ...]". False for "return;". Used for error
1773 -- checking: If Returns_Object is True, N should apply to a function
1774 -- body; otherwise N should apply to a procedure body, entry body,
1775 -- accept statement, or extended return statement.
1777 function Find_What_It_Applies_To return Entity_Id;
1778 -- Find the entity representing the innermost enclosing body, accept
1779 -- statement, or extended return statement. If the result is a callable
1780 -- construct or extended return statement, then this will be the value
1781 -- of the Return_Applies_To attribute. Otherwise, the program is
1782 -- illegal. See RM-6.5(4/2).
1784 -----------------------------
1785 -- Find_What_It_Applies_To --
1786 -----------------------------
1788 function Find_What_It_Applies_To return Entity_Id is
1789 Result : Entity_Id := Empty;
1792 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1793 -- and postconditions.
1795 for J in reverse 0 .. Scope_Stack.Last loop
1796 Result := Scope_Stack.Table (J).Entity;
1797 exit when not Ekind_In (Result, E_Block, E_Loop)
1798 and then Chars (Result) /= Name_uPostconditions;
1801 pragma Assert (Present (Result));
1803 end Find_What_It_Applies_To;
1805 -- Local declarations
1807 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1808 Kind : constant Entity_Kind := Ekind (Scope_Id);
1809 Loc : constant Source_Ptr := Sloc (N);
1810 Stm_Entity : constant Entity_Id :=
1812 (E_Return_Statement, Current_Scope, Loc, 'R');
1814 -- Start of processing for Analyze_Return_Statement
1817 Set_Return_Statement_Entity (N, Stm_Entity);
1819 Set_Etype (Stm_Entity, Standard_Void_Type);
1820 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1822 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1823 -- (4/2): an inner return statement will apply to this extended return.
1825 if Nkind (N) = N_Extended_Return_Statement then
1826 Push_Scope (Stm_Entity);
1829 -- Check that pragma No_Return is obeyed. Don't complain about the
1830 -- implicitly-generated return that is placed at the end.
1832 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1833 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1836 -- Warn on any unassigned OUT parameters if in procedure
1838 if Ekind (Scope_Id) = E_Procedure then
1839 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1842 -- Check that functions return objects, and other things do not
1844 if Kind = E_Function or else Kind = E_Generic_Function then
1845 if not Returns_Object then
1846 Error_Msg_N ("missing expression in return from function", N);
1849 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1850 if Returns_Object then
1851 Error_Msg_N ("procedure cannot return value (use function)", N);
1854 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1855 if Returns_Object then
1856 if Is_Protected_Type (Scope (Scope_Id)) then
1857 Error_Msg_N ("entry body cannot return value", N);
1859 Error_Msg_N ("accept statement cannot return value", N);
1863 elsif Kind = E_Return_Statement then
1865 -- We are nested within another return statement, which must be an
1866 -- extended_return_statement.
1868 if Returns_Object then
1869 if Nkind (N) = N_Extended_Return_Statement then
1871 ("extended return statement cannot be nested (use `RETURN;`)",
1874 -- Case of a simple return statement with a value inside extended
1875 -- return statement.
1879 ("return nested in extended return statement cannot return "
1880 & "value (use `RETURN;`)", N);
1885 Error_Msg_N ("illegal context for return statement", N);
1888 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1889 Analyze_Function_Return (N);
1891 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1892 Set_Return_Present (Scope_Id);
1895 if Nkind (N) = N_Extended_Return_Statement then
1899 Kill_Current_Values (Last_Assignment_Only => True);
1900 Check_Unreachable_Code (N);
1902 Analyze_Dimension (N);
1903 end Analyze_Return_Statement;
1905 -------------------------------------
1906 -- Analyze_Simple_Return_Statement --
1907 -------------------------------------
1909 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1911 if Present (Expression (N)) then
1912 Mark_Coextensions (N, Expression (N));
1915 Analyze_Return_Statement (N);
1916 end Analyze_Simple_Return_Statement;
1918 -------------------------
1919 -- Analyze_Return_Type --
1920 -------------------------
1922 procedure Analyze_Return_Type (N : Node_Id) is
1923 Designator : constant Entity_Id := Defining_Entity (N);
1924 Typ : Entity_Id := Empty;
1927 -- Normal case where result definition does not indicate an error
1929 if Result_Definition (N) /= Error then
1930 if Nkind (Result_Definition (N)) = N_Access_Definition then
1931 Check_SPARK_05_Restriction
1932 ("access result is not allowed", Result_Definition (N));
1934 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1937 AD : constant Node_Id :=
1938 Access_To_Subprogram_Definition (Result_Definition (N));
1940 if Present (AD) and then Protected_Present (AD) then
1941 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1943 Typ := Access_Definition (N, Result_Definition (N));
1947 Set_Parent (Typ, Result_Definition (N));
1948 Set_Is_Local_Anonymous_Access (Typ);
1949 Set_Etype (Designator, Typ);
1951 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1953 Null_Exclusion_Static_Checks (N);
1955 -- Subtype_Mark case
1958 Find_Type (Result_Definition (N));
1959 Typ := Entity (Result_Definition (N));
1960 Set_Etype (Designator, Typ);
1962 -- Unconstrained array as result is not allowed in SPARK
1964 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1965 Check_SPARK_05_Restriction
1966 ("returning an unconstrained array is not allowed",
1967 Result_Definition (N));
1970 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1972 Null_Exclusion_Static_Checks (N);
1974 -- If a null exclusion is imposed on the result type, then create
1975 -- a null-excluding itype (an access subtype) and use it as the
1976 -- function's Etype. Note that the null exclusion checks are done
1977 -- right before this, because they don't get applied to types that
1978 -- do not come from source.
1980 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1981 Set_Etype (Designator,
1982 Create_Null_Excluding_Itype
1985 Scope_Id => Scope (Current_Scope)));
1987 -- The new subtype must be elaborated before use because
1988 -- it is visible outside of the function. However its base
1989 -- type may not be frozen yet, so the reference that will
1990 -- force elaboration must be attached to the freezing of
1993 -- If the return specification appears on a proper body,
1994 -- the subtype will have been created already on the spec.
1996 if Is_Frozen (Typ) then
1997 if Nkind (Parent (N)) = N_Subprogram_Body
1998 and then Nkind (Parent (Parent (N))) = N_Subunit
2002 Build_Itype_Reference (Etype (Designator), Parent (N));
2006 Ensure_Freeze_Node (Typ);
2009 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2011 Set_Itype (IR, Etype (Designator));
2012 Append_Freeze_Actions (Typ, New_List (IR));
2017 Set_Etype (Designator, Typ);
2020 if Ekind (Typ) = E_Incomplete_Type
2021 or else (Is_Class_Wide_Type (Typ)
2022 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2024 -- AI05-0151: Tagged incomplete types are allowed in all formal
2025 -- parts. Untagged incomplete types are not allowed in bodies.
2026 -- As a consequence, limited views cannot appear in a basic
2027 -- declaration that is itself within a body, because there is
2028 -- no point at which the non-limited view will become visible.
2030 if Ada_Version >= Ada_2012 then
2031 if From_Limited_With (Typ) and then In_Package_Body then
2033 ("invalid use of incomplete type&",
2034 Result_Definition (N), Typ);
2036 -- The return type of a subprogram body cannot be of a
2037 -- formal incomplete type.
2039 elsif Is_Generic_Type (Typ)
2040 and then Nkind (Parent (N)) = N_Subprogram_Body
2043 ("return type cannot be a formal incomplete type",
2044 Result_Definition (N));
2046 elsif Is_Class_Wide_Type (Typ)
2047 and then Is_Generic_Type (Root_Type (Typ))
2048 and then Nkind (Parent (N)) = N_Subprogram_Body
2051 ("return type cannot be a formal incomplete type",
2052 Result_Definition (N));
2054 elsif Is_Tagged_Type (Typ) then
2057 -- Use is legal in a thunk generated for an operation
2058 -- inherited from a progenitor.
2060 elsif Is_Thunk (Designator)
2061 and then Present (Non_Limited_View (Typ))
2065 elsif Nkind (Parent (N)) = N_Subprogram_Body
2066 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2070 ("invalid use of untagged incomplete type&",
2074 -- The type must be completed in the current package. This
2075 -- is checked at the end of the package declaration when
2076 -- Taft-amendment types are identified. If the return type
2077 -- is class-wide, there is no required check, the type can
2078 -- be a bona fide TAT.
2080 if Ekind (Scope (Current_Scope)) = E_Package
2081 and then In_Private_Part (Scope (Current_Scope))
2082 and then not Is_Class_Wide_Type (Typ)
2084 Append_Elmt (Designator, Private_Dependents (Typ));
2089 ("invalid use of incomplete type&", Designator, Typ);
2094 -- Case where result definition does indicate an error
2097 Set_Etype (Designator, Any_Type);
2099 end Analyze_Return_Type;
2101 -----------------------------
2102 -- Analyze_Subprogram_Body --
2103 -----------------------------
2105 procedure Analyze_Subprogram_Body (N : Node_Id) is
2106 Loc : constant Source_Ptr := Sloc (N);
2107 Body_Spec : constant Node_Id := Specification (N);
2108 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2111 if Debug_Flag_C then
2112 Write_Str ("==> subprogram body ");
2113 Write_Name (Chars (Body_Id));
2114 Write_Str (" from ");
2115 Write_Location (Loc);
2120 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2122 -- The real work is split out into the helper, so it can do "return;"
2123 -- without skipping the debug output:
2125 Analyze_Subprogram_Body_Helper (N);
2127 if Debug_Flag_C then
2129 Write_Str ("<== subprogram body ");
2130 Write_Name (Chars (Body_Id));
2131 Write_Str (" from ");
2132 Write_Location (Loc);
2135 end Analyze_Subprogram_Body;
2137 ------------------------------------
2138 -- Analyze_Subprogram_Body_Helper --
2139 ------------------------------------
2141 -- This procedure is called for regular subprogram bodies, generic bodies,
2142 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2143 -- specification matters, and is used to create a proper declaration for
2144 -- the subprogram, or to perform conformance checks.
2146 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2147 Loc : constant Source_Ptr := Sloc (N);
2148 Body_Spec : Node_Id := Specification (N);
2149 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2150 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2151 Exch_Views : Elist_Id := No_Elist;
2152 Conformant : Boolean;
2154 Prot_Typ : Entity_Id := Empty;
2155 Spec_Id : Entity_Id;
2156 Spec_Decl : Node_Id := Empty;
2158 Last_Real_Spec_Entity : Entity_Id := Empty;
2159 -- When we analyze a separate spec, the entity chain ends up containing
2160 -- the formals, as well as any itypes generated during analysis of the
2161 -- default expressions for parameters, or the arguments of associated
2162 -- precondition/postcondition pragmas (which are analyzed in the context
2163 -- of the spec since they have visibility on formals).
2165 -- These entities belong with the spec and not the body. However we do
2166 -- the analysis of the body in the context of the spec (again to obtain
2167 -- visibility to the formals), and all the entities generated during
2168 -- this analysis end up also chained to the entity chain of the spec.
2169 -- But they really belong to the body, and there is circuitry to move
2170 -- them from the spec to the body.
2172 -- However, when we do this move, we don't want to move the real spec
2173 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2174 -- variable points to the last real spec entity, so we only move those
2175 -- chained beyond that point. It is initialized to Empty to deal with
2176 -- the case where there is no separate spec.
2178 function Body_Has_Contract return Boolean;
2179 -- Check whether unanalyzed body has an aspect or pragma that may
2180 -- generate a SPARK contract.
2182 function Body_Has_SPARK_Mode_On return Boolean;
2183 -- Check whether SPARK_Mode On applies to the subprogram body, either
2184 -- because it is specified directly on the body, or because it is
2185 -- inherited from the enclosing subprogram or package.
2187 procedure Build_Subprogram_Declaration;
2188 -- Create a matching subprogram declaration for subprogram body N
2190 procedure Check_Anonymous_Return;
2191 -- Ada 2005: if a function returns an access type that denotes a task,
2192 -- or a type that contains tasks, we must create a master entity for
2193 -- the anonymous type, which typically will be used in an allocator
2194 -- in the body of the function.
2196 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2197 -- Look ahead to recognize a pragma that may appear after the body.
2198 -- If there is a previous spec, check that it appears in the same
2199 -- declarative part. If the pragma is Inline_Always, perform inlining
2200 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2201 -- If the body acts as a spec, and inlining is required, we create a
2202 -- subprogram declaration for it, in order to attach the body to inline.
2203 -- If pragma does not appear after the body, check whether there is
2204 -- an inline pragma before any local declarations.
2206 procedure Check_Missing_Return;
2207 -- Checks for a function with a no return statements, and also performs
2208 -- the warning checks implemented by Check_Returns. In formal mode, also
2209 -- verify that a function ends with a RETURN and that a procedure does
2210 -- not contain any RETURN.
2212 function Disambiguate_Spec return Entity_Id;
2213 -- When a primitive is declared between the private view and the full
2214 -- view of a concurrent type which implements an interface, a special
2215 -- mechanism is used to find the corresponding spec of the primitive
2218 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2219 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2220 -- incomplete types coming from a limited context and replace their
2221 -- limited views with the non-limited ones. Return the list of changes
2222 -- to be used to undo the transformation.
2224 function Is_Private_Concurrent_Primitive
2225 (Subp_Id : Entity_Id) return Boolean;
2226 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2227 -- type that implements an interface and has a private view.
2229 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2230 -- Undo the transformation done by Exchange_Limited_Views.
2232 procedure Set_Trivial_Subprogram (N : Node_Id);
2233 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2234 -- subprogram whose body is being analyzed. N is the statement node
2235 -- causing the flag to be set, if the following statement is a return
2236 -- of an entity, we mark the entity as set in source to suppress any
2237 -- warning on the stylized use of function stubs with a dummy return.
2239 procedure Verify_Overriding_Indicator;
2240 -- If there was a previous spec, the entity has been entered in the
2241 -- current scope previously. If the body itself carries an overriding
2242 -- indicator, check that it is consistent with the known status of the
2245 -----------------------
2246 -- Body_Has_Contract --
2247 -----------------------
2249 function Body_Has_Contract return Boolean is
2250 Decls : constant List_Id := Declarations (N);
2254 -- Check for aspects that may generate a contract
2256 if Present (Aspect_Specifications (N)) then
2257 Item := First (Aspect_Specifications (N));
2258 while Present (Item) loop
2259 if Is_Subprogram_Contract_Annotation (Item) then
2267 -- Check for pragmas that may generate a contract
2269 if Present (Decls) then
2270 Item := First (Decls);
2271 while Present (Item) loop
2272 if Nkind (Item) = N_Pragma
2273 and then Is_Subprogram_Contract_Annotation (Item)
2283 end Body_Has_Contract;
2285 ----------------------------
2286 -- Body_Has_SPARK_Mode_On --
2287 ----------------------------
2289 function Body_Has_SPARK_Mode_On return Boolean is
2290 Decls : constant List_Id := Declarations (N);
2294 -- Check for SPARK_Mode aspect
2296 if Present (Aspect_Specifications (N)) then
2297 Item := First (Aspect_Specifications (N));
2298 while Present (Item) loop
2299 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2300 return Get_SPARK_Mode_From_Annotation (Item) = On;
2307 -- Check for SPARK_Mode pragma
2309 if Present (Decls) then
2310 Item := First (Decls);
2311 while Present (Item) loop
2313 -- Pragmas that apply to a subprogram body are usually grouped
2314 -- together. Look for a potential pragma SPARK_Mode among them.
2316 if Nkind (Item) = N_Pragma then
2317 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2318 return Get_SPARK_Mode_From_Annotation (Item) = On;
2321 -- Otherwise the first non-pragma declarative item terminates
2322 -- the region where pragma SPARK_Mode may appear.
2332 -- Otherwise, the applicable SPARK_Mode is inherited from the
2333 -- enclosing subprogram or package.
2335 return SPARK_Mode = On;
2336 end Body_Has_SPARK_Mode_On;
2338 ----------------------------------
2339 -- Build_Subprogram_Declaration --
2340 ----------------------------------
2342 procedure Build_Subprogram_Declaration is
2343 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2344 -- Relocate certain categorization pragmas from the declarative list
2345 -- of subprogram body From and insert them after node To. The pragmas
2349 -- Volatile_Function
2355 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2357 Next_Decl : Node_Id;
2360 pragma Assert (Nkind (From) = N_Subprogram_Body);
2362 -- The destination node must be part of a list, as the pragmas are
2363 -- inserted after it.
2365 pragma Assert (Is_List_Member (To));
2367 -- Inspect the declarations of the subprogram body looking for
2368 -- specific pragmas.
2370 Decl := First (Declarations (N));
2371 while Present (Decl) loop
2372 Next_Decl := Next (Decl);
2374 if Nkind (Decl) = N_Pragma
2375 and then Nam_In (Pragma_Name (Decl), Name_Ghost,
2377 Name_Volatile_Function)
2380 Insert_After (To, Decl);
2390 Subp_Decl : Node_Id;
2392 -- Start of processing for Build_Subprogram_Declaration
2395 -- Create a matching subprogram spec using the profile of the body.
2396 -- The structure of the tree is identical, but has new entities for
2397 -- the defining unit name and formal parameters.
2400 Make_Subprogram_Declaration (Loc,
2401 Specification => Copy_Subprogram_Spec (Body_Spec));
2402 Set_Comes_From_Source (Subp_Decl, True);
2404 -- Relocate the aspects and relevant pragmas from the subprogram body
2405 -- to the generated spec because it acts as the initial declaration.
2407 Insert_Before (N, Subp_Decl);
2408 Move_Aspects (N, To => Subp_Decl);
2409 Move_Pragmas (N, To => Subp_Decl);
2411 Analyze (Subp_Decl);
2413 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2414 -- the body since the expander may generate calls using that entity.
2415 -- Required to ensure that Expand_Call rewrites calls to this
2416 -- function by calls to the built procedure.
2418 if Modify_Tree_For_C
2419 and then Nkind (Body_Spec) = N_Function_Specification
2421 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2423 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2424 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2425 Corresponding_Procedure
2426 (Defining_Entity (Specification (Subp_Decl))));
2429 -- Analyze any relocated source pragmas or pragmas created for aspect
2432 Decl := Next (Subp_Decl);
2433 while Present (Decl) loop
2435 -- Stop the search for pragmas once the body has been reached as
2436 -- this terminates the region where pragmas may appear.
2441 elsif Nkind (Decl) = N_Pragma then
2448 Spec_Id := Defining_Entity (Subp_Decl);
2449 Set_Corresponding_Spec (N, Spec_Id);
2451 -- Mark the generated spec as a source construct to ensure that all
2452 -- calls to it are properly registered in ALI files for GNATprove.
2454 Set_Comes_From_Source (Spec_Id, True);
2456 -- Ensure that the specs of the subprogram declaration and its body
2457 -- are identical, otherwise they will appear non-conformant due to
2458 -- rewritings in the default values of formal parameters.
2460 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2461 Set_Specification (N, Body_Spec);
2462 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2464 -- Ensure that the generated corresponding spec and original body
2465 -- share the same Ghost and SPARK_Mode attributes.
2467 Set_Is_Checked_Ghost_Entity
2468 (Body_Id, Is_Checked_Ghost_Entity (Spec_Id));
2469 Set_Is_Ignored_Ghost_Entity
2470 (Body_Id, Is_Ignored_Ghost_Entity (Spec_Id));
2472 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
2473 Set_SPARK_Pragma_Inherited
2474 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
2475 end Build_Subprogram_Declaration;
2477 ----------------------------
2478 -- Check_Anonymous_Return --
2479 ----------------------------
2481 procedure Check_Anonymous_Return is
2487 if Present (Spec_Id) then
2493 if Ekind (Scop) = E_Function
2494 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2495 and then not Is_Thunk (Scop)
2497 -- Skip internally built functions which handle the case of
2498 -- a null access (see Expand_Interface_Conversion)
2500 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2501 and then not Comes_From_Source (Parent (Scop)))
2503 and then (Has_Task (Designated_Type (Etype (Scop)))
2505 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2507 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2508 and then Expander_Active
2510 -- Avoid cases with no tasking support
2512 and then RTE_Available (RE_Current_Master)
2513 and then not Restriction_Active (No_Task_Hierarchy)
2516 Make_Object_Declaration (Loc,
2517 Defining_Identifier =>
2518 Make_Defining_Identifier (Loc, Name_uMaster),
2519 Constant_Present => True,
2520 Object_Definition =>
2521 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2523 Make_Explicit_Dereference (Loc,
2524 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2526 if Present (Declarations (N)) then
2527 Prepend (Decl, Declarations (N));
2529 Set_Declarations (N, New_List (Decl));
2532 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2533 Set_Has_Master_Entity (Scop);
2535 -- Now mark the containing scope as a task master
2538 while Nkind (Par) /= N_Compilation_Unit loop
2539 Par := Parent (Par);
2540 pragma Assert (Present (Par));
2542 -- If we fall off the top, we are at the outer level, and
2543 -- the environment task is our effective master, so nothing
2547 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2549 Set_Is_Task_Master (Par, True);
2554 end Check_Anonymous_Return;
2556 -------------------------
2557 -- Check_Inline_Pragma --
2558 -------------------------
2560 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2564 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2565 -- True when N is a pragma Inline or Inline_Always that applies
2566 -- to this subprogram.
2568 -----------------------
2569 -- Is_Inline_Pragma --
2570 -----------------------
2572 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2574 if Nkind (N) = N_Pragma
2576 (Pragma_Name (N) = Name_Inline_Always
2577 or else (Pragma_Name (N) = Name_Inline
2579 (Front_End_Inlining or else Optimization_Level > 0)))
2580 and then Present (Pragma_Argument_Associations (N))
2583 Pragma_Arg : Node_Id :=
2584 Expression (First (Pragma_Argument_Associations (N)));
2586 if Nkind (Pragma_Arg) = N_Selected_Component then
2587 Pragma_Arg := Selector_Name (Pragma_Arg);
2590 return Chars (Pragma_Arg) = Chars (Body_Id);
2596 end Is_Inline_Pragma;
2598 -- Start of processing for Check_Inline_Pragma
2601 if not Expander_Active then
2605 if Is_List_Member (N)
2606 and then Present (Next (N))
2607 and then Is_Inline_Pragma (Next (N))
2611 elsif Nkind (N) /= N_Subprogram_Body_Stub
2612 and then Present (Declarations (N))
2613 and then Is_Inline_Pragma (First (Declarations (N)))
2615 Prag := First (Declarations (N));
2621 if Present (Prag) then
2622 if Present (Spec_Id) then
2623 if Is_List_Member (N)
2624 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2625 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2631 -- Create a subprogram declaration, to make treatment uniform.
2632 -- Make the sloc of the subprogram name that of the entity in
2633 -- the body, so that style checks find identical strings.
2636 Subp : constant Entity_Id :=
2637 Make_Defining_Identifier
2638 (Sloc (Body_Id), Chars (Body_Id));
2639 Decl : constant Node_Id :=
2640 Make_Subprogram_Declaration (Loc,
2642 New_Copy_Tree (Specification (N)));
2645 Set_Defining_Unit_Name (Specification (Decl), Subp);
2647 -- To ensure proper coverage when body is inlined, indicate
2648 -- whether the subprogram comes from source.
2650 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2652 if Present (First_Formal (Body_Id)) then
2653 Plist := Copy_Parameter_List (Body_Id);
2654 Set_Parameter_Specifications
2655 (Specification (Decl), Plist);
2658 Insert_Before (N, Decl);
2661 Set_Has_Pragma_Inline (Subp);
2663 if Pragma_Name (Prag) = Name_Inline_Always then
2664 Set_Is_Inlined (Subp);
2665 Set_Has_Pragma_Inline_Always (Subp);
2668 -- Prior to copying the subprogram body to create a template
2669 -- for it for subsequent inlining, remove the pragma from
2670 -- the current body so that the copy that will produce the
2671 -- new body will start from a completely unanalyzed tree.
2673 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2674 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2681 end Check_Inline_Pragma;
2683 --------------------------
2684 -- Check_Missing_Return --
2685 --------------------------
2687 procedure Check_Missing_Return is
2689 Missing_Ret : Boolean;
2692 if Nkind (Body_Spec) = N_Function_Specification then
2693 if Present (Spec_Id) then
2699 if Return_Present (Id) then
2700 Check_Returns (HSS, 'F', Missing_Ret);
2703 Set_Has_Missing_Return (Id);
2706 -- Within a premature instantiation of a package with no body, we
2707 -- build completions of the functions therein, with a Raise
2708 -- statement. No point in complaining about a missing return in
2711 elsif Ekind (Id) = E_Function
2712 and then In_Instance
2713 and then Present (Statements (HSS))
2714 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2718 elsif Is_Generic_Subprogram (Id)
2719 or else not Is_Machine_Code_Subprogram (Id)
2721 Error_Msg_N ("missing RETURN statement in function body", N);
2724 -- If procedure with No_Return, check returns
2726 elsif Nkind (Body_Spec) = N_Procedure_Specification
2727 and then Present (Spec_Id)
2728 and then No_Return (Spec_Id)
2730 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2733 -- Special checks in SPARK mode
2735 if Nkind (Body_Spec) = N_Function_Specification then
2737 -- In SPARK mode, last statement of a function should be a return
2740 Stat : constant Node_Id := Last_Source_Statement (HSS);
2743 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2744 N_Extended_Return_Statement)
2746 Check_SPARK_05_Restriction
2747 ("last statement in function should be RETURN", Stat);
2751 -- In SPARK mode, verify that a procedure has no return
2753 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2754 if Present (Spec_Id) then
2760 -- Would be nice to point to return statement here, can we
2761 -- borrow the Check_Returns procedure here ???
2763 if Return_Present (Id) then
2764 Check_SPARK_05_Restriction
2765 ("procedure should not have RETURN", N);
2768 end Check_Missing_Return;
2770 -----------------------
2771 -- Disambiguate_Spec --
2772 -----------------------
2774 function Disambiguate_Spec return Entity_Id is
2775 Priv_Spec : Entity_Id;
2778 procedure Replace_Types (To_Corresponding : Boolean);
2779 -- Depending on the flag, replace the type of formal parameters of
2780 -- Body_Id if it is a concurrent type implementing interfaces with
2781 -- the corresponding record type or the other way around.
2783 procedure Replace_Types (To_Corresponding : Boolean) is
2785 Formal_Typ : Entity_Id;
2788 Formal := First_Formal (Body_Id);
2789 while Present (Formal) loop
2790 Formal_Typ := Etype (Formal);
2792 if Is_Class_Wide_Type (Formal_Typ) then
2793 Formal_Typ := Root_Type (Formal_Typ);
2796 -- From concurrent type to corresponding record
2798 if To_Corresponding then
2799 if Is_Concurrent_Type (Formal_Typ)
2800 and then Present (Corresponding_Record_Type (Formal_Typ))
2803 (Corresponding_Record_Type (Formal_Typ)))
2806 Corresponding_Record_Type (Formal_Typ));
2809 -- From corresponding record to concurrent type
2812 if Is_Concurrent_Record_Type (Formal_Typ)
2813 and then Present (Interfaces (Formal_Typ))
2816 Corresponding_Concurrent_Type (Formal_Typ));
2820 Next_Formal (Formal);
2824 -- Start of processing for Disambiguate_Spec
2827 -- Try to retrieve the specification of the body as is. All error
2828 -- messages are suppressed because the body may not have a spec in
2829 -- its current state.
2831 Spec_N := Find_Corresponding_Spec (N, False);
2833 -- It is possible that this is the body of a primitive declared
2834 -- between a private and a full view of a concurrent type. The
2835 -- controlling parameter of the spec carries the concurrent type,
2836 -- not the corresponding record type as transformed by Analyze_
2837 -- Subprogram_Specification. In such cases, we undo the change
2838 -- made by the analysis of the specification and try to find the
2841 -- Note that wrappers already have their corresponding specs and
2842 -- bodies set during their creation, so if the candidate spec is
2843 -- a wrapper, then we definitely need to swap all types to their
2844 -- original concurrent status.
2847 or else Is_Primitive_Wrapper (Spec_N)
2849 -- Restore all references of corresponding record types to the
2850 -- original concurrent types.
2852 Replace_Types (To_Corresponding => False);
2853 Priv_Spec := Find_Corresponding_Spec (N, False);
2855 -- The current body truly belongs to a primitive declared between
2856 -- a private and a full view. We leave the modified body as is,
2857 -- and return the true spec.
2859 if Present (Priv_Spec)
2860 and then Is_Private_Primitive (Priv_Spec)
2865 -- In case that this is some sort of error, restore the original
2866 -- state of the body.
2868 Replace_Types (To_Corresponding => True);
2872 end Disambiguate_Spec;
2874 ----------------------------
2875 -- Exchange_Limited_Views --
2876 ----------------------------
2878 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
2879 Result : Elist_Id := No_Elist;
2881 procedure Detect_And_Exchange (Id : Entity_Id);
2882 -- Determine whether Id's type denotes an incomplete type associated
2883 -- with a limited with clause and exchange the limited view with the
2884 -- non-limited one when available. Note that the non-limited view
2885 -- may exist because of a with_clause in another unit in the context,
2886 -- but cannot be used because the current view of the enclosing unit
2887 -- is still a limited view.
2889 -------------------------
2890 -- Detect_And_Exchange --
2891 -------------------------
2893 procedure Detect_And_Exchange (Id : Entity_Id) is
2894 Typ : constant Entity_Id := Etype (Id);
2896 if From_Limited_With (Typ)
2897 and then Has_Non_Limited_View (Typ)
2898 and then not From_Limited_With (Scope (Typ))
2901 Result := New_Elmt_List;
2904 Prepend_Elmt (Typ, Result);
2905 Prepend_Elmt (Id, Result);
2906 Set_Etype (Id, Non_Limited_View (Typ));
2908 end Detect_And_Exchange;
2914 -- Start of processing for Exchange_Limited_Views
2917 if No (Subp_Id) then
2920 -- Do not process subprogram bodies as they already use the non-
2921 -- limited view of types.
2923 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2927 -- Examine all formals and swap views when applicable
2929 Formal := First_Formal (Subp_Id);
2930 while Present (Formal) loop
2931 Detect_And_Exchange (Formal);
2933 Next_Formal (Formal);
2936 -- Process the return type of a function
2938 if Ekind (Subp_Id) = E_Function then
2939 Detect_And_Exchange (Subp_Id);
2943 end Exchange_Limited_Views;
2945 -------------------------------------
2946 -- Is_Private_Concurrent_Primitive --
2947 -------------------------------------
2949 function Is_Private_Concurrent_Primitive
2950 (Subp_Id : Entity_Id) return Boolean
2952 Formal_Typ : Entity_Id;
2955 if Present (First_Formal (Subp_Id)) then
2956 Formal_Typ := Etype (First_Formal (Subp_Id));
2958 if Is_Concurrent_Record_Type (Formal_Typ) then
2959 if Is_Class_Wide_Type (Formal_Typ) then
2960 Formal_Typ := Root_Type (Formal_Typ);
2963 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2966 -- The type of the first formal is a concurrent tagged type with
2970 Is_Concurrent_Type (Formal_Typ)
2971 and then Is_Tagged_Type (Formal_Typ)
2972 and then Has_Private_Declaration (Formal_Typ);
2976 end Is_Private_Concurrent_Primitive;
2978 ---------------------------
2979 -- Restore_Limited_Views --
2980 ---------------------------
2982 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
2983 Elmt : Elmt_Id := First_Elmt (Restore_List);
2987 while Present (Elmt) loop
2990 Set_Etype (Id, Node (Elmt));
2993 end Restore_Limited_Views;
2995 ----------------------------
2996 -- Set_Trivial_Subprogram --
2997 ----------------------------
2999 procedure Set_Trivial_Subprogram (N : Node_Id) is
3000 Nxt : constant Node_Id := Next (N);
3003 Set_Is_Trivial_Subprogram (Body_Id);
3005 if Present (Spec_Id) then
3006 Set_Is_Trivial_Subprogram (Spec_Id);
3010 and then Nkind (Nxt) = N_Simple_Return_Statement
3011 and then No (Next (Nxt))
3012 and then Present (Expression (Nxt))
3013 and then Is_Entity_Name (Expression (Nxt))
3015 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3017 end Set_Trivial_Subprogram;
3019 ---------------------------------
3020 -- Verify_Overriding_Indicator --
3021 ---------------------------------
3023 procedure Verify_Overriding_Indicator is
3025 if Must_Override (Body_Spec) then
3026 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3027 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3031 elsif not Present (Overridden_Operation (Spec_Id)) then
3033 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3035 -- Overriding indicators aren't allowed for protected subprogram
3036 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3037 -- this to a warning if -gnatd.E is enabled.
3039 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3040 Error_Msg_Warn := Error_To_Warning;
3042 ("<<overriding indicator not allowed for protected "
3043 & "subprogram body", Body_Spec);
3046 elsif Must_Not_Override (Body_Spec) then
3047 if Present (Overridden_Operation (Spec_Id)) then
3049 ("subprogram& overrides inherited operation",
3050 Body_Spec, Spec_Id);
3052 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3053 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3056 ("subprogram& overrides predefined operator ",
3057 Body_Spec, Spec_Id);
3059 -- Overriding indicators aren't allowed for protected subprogram
3060 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3061 -- this to a warning if -gnatd.E is enabled.
3063 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3064 Error_Msg_Warn := Error_To_Warning;
3067 ("<<overriding indicator not allowed "
3068 & "for protected subprogram body", Body_Spec);
3070 -- If this is not a primitive operation, then the overriding
3071 -- indicator is altogether illegal.
3073 elsif not Is_Primitive (Spec_Id) then
3075 ("overriding indicator only allowed "
3076 & "if subprogram is primitive", Body_Spec);
3079 -- If checking the style rule and the operation overrides, then
3080 -- issue a warning about a missing overriding_indicator. Protected
3081 -- subprogram bodies are excluded from this style checking, since
3082 -- they aren't primitives (even though their declarations can
3083 -- override) and aren't allowed to have an overriding_indicator.
3086 and then Present (Overridden_Operation (Spec_Id))
3087 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3089 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3090 Style.Missing_Overriding (N, Body_Id);
3093 and then Can_Override_Operator (Spec_Id)
3094 and then not Is_Predefined_File_Name
3095 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3097 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3098 Style.Missing_Overriding (N, Body_Id);
3100 end Verify_Overriding_Indicator;
3104 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
3106 -- Start of processing for Analyze_Subprogram_Body_Helper
3109 -- A [generic] subprogram body "freezes" the contract of the nearest
3110 -- enclosing package body and all other contracts encountered in the
3111 -- same declarative part up to and excluding the subprogram body:
3113 -- package body Nearest_Enclosing_Package
3114 -- with Refined_State => (State => Constit)
3118 -- procedure Freezes_Enclosing_Package_Body
3119 -- with Refined_Depends => (Input => Constit) ...
3121 -- This ensures that any annotations referenced by the contract of the
3122 -- [generic] subprogram body are available. This form of "freezing" is
3123 -- decoupled from the usual Freeze_xxx mechanism because it must also
3124 -- work in the context of generics where normal freezing is disabled.
3126 -- Only bodies coming from source should cause this type of "freezing".
3127 -- Expression functions that act as bodies and complete an initial
3128 -- declaration must be included in this category, hence the use of
3131 if Comes_From_Source (Original_Node (N)) then
3132 Analyze_Previous_Contracts (N);
3135 -- Generic subprograms are handled separately. They always have a
3136 -- generic specification. Determine whether current scope has a
3137 -- previous declaration.
3139 -- If the subprogram body is defined within an instance of the same
3140 -- name, the instance appears as a package renaming, and will be hidden
3141 -- within the subprogram.
3143 if Present (Prev_Id)
3144 and then not Is_Overloadable (Prev_Id)
3145 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3146 or else Comes_From_Source (Prev_Id))
3148 if Is_Generic_Subprogram (Prev_Id) then
3151 -- A subprogram body is Ghost when it is stand alone and subject
3152 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3153 -- the mode now to ensure that any nodes generated during analysis
3154 -- and expansion are properly marked as Ghost.
3156 Set_Ghost_Mode (N, Spec_Id);
3157 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3158 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3160 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3162 if Nkind (N) = N_Subprogram_Body then
3163 HSS := Handled_Statement_Sequence (N);
3164 Check_Missing_Return;
3167 Ghost_Mode := Save_Ghost_Mode;
3171 -- Previous entity conflicts with subprogram name. Attempting to
3172 -- enter name will post error.
3174 Enter_Name (Body_Id);
3175 Ghost_Mode := Save_Ghost_Mode;
3179 -- Non-generic case, find the subprogram declaration, if one was seen,
3180 -- or enter new overloaded entity in the current scope. If the
3181 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3182 -- part of the context of one of its subunits. No need to redo the
3185 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3186 Ghost_Mode := Save_Ghost_Mode;
3190 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3192 if Nkind (N) = N_Subprogram_Body_Stub
3193 or else No (Corresponding_Spec (N))
3195 if Is_Private_Concurrent_Primitive (Body_Id) then
3196 Spec_Id := Disambiguate_Spec;
3198 -- A subprogram body is Ghost when it is stand alone and
3199 -- subject to pragma Ghost or when the corresponding spec is
3200 -- Ghost. Set the mode now to ensure that any nodes generated
3201 -- during analysis and expansion are properly marked as Ghost.
3203 Set_Ghost_Mode (N, Spec_Id);
3206 Spec_Id := Find_Corresponding_Spec (N);
3208 -- A subprogram body is Ghost when it is stand alone and
3209 -- subject to pragma Ghost or when the corresponding spec is
3210 -- Ghost. Set the mode now to ensure that any nodes generated
3211 -- during analysis and expansion are properly marked as Ghost.
3213 Set_Ghost_Mode (N, Spec_Id);
3215 -- In GNATprove mode, if the body has no previous spec, create
3216 -- one so that the inlining machinery can operate properly.
3217 -- Transfer aspects, if any, to the new spec, so that they
3218 -- are legal and can be processed ahead of the body.
3219 -- We make two copies of the given spec, one for the new
3220 -- declaration, and one for the body.
3222 if No (Spec_Id) and then GNATprove_Mode
3224 -- Inlining does not apply during pre-analysis of code
3226 and then Full_Analysis
3228 -- Inlining only applies to full bodies, not stubs
3230 and then Nkind (N) /= N_Subprogram_Body_Stub
3232 -- Inlining only applies to bodies in the source code, not to
3233 -- those generated by the compiler. In particular, expression
3234 -- functions, whose body is generated by the compiler, are
3235 -- treated specially by GNATprove.
3237 and then Comes_From_Source (Body_Id)
3239 -- This cannot be done for a compilation unit, which is not
3240 -- in a context where we can insert a new spec.
3242 and then Is_List_Member (N)
3244 -- Inlining only applies to subprograms without contracts,
3245 -- as a contract is a sign that GNATprove should perform a
3246 -- modular analysis of the subprogram instead of a contextual
3247 -- analysis at each call site. The same test is performed in
3248 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3249 -- here in another form (because the contract has not
3250 -- been attached to the body) to avoid frontend errors in
3251 -- case pragmas are used instead of aspects, because the
3252 -- corresponding pragmas in the body would not be transferred
3253 -- to the spec, leading to legality errors.
3255 and then not Body_Has_Contract
3256 and then not Inside_A_Generic
3258 Build_Subprogram_Declaration;
3260 -- If this is a function that returns a constrained array, and
3261 -- we are generating SPARK_For_C, create subprogram declaration
3262 -- to simplify subsequent C generation.
3265 and then Modify_Tree_For_C
3266 and then Nkind (Body_Spec) = N_Function_Specification
3267 and then Is_Array_Type (Etype (Body_Id))
3268 and then Is_Constrained (Etype (Body_Id))
3270 Build_Subprogram_Declaration;
3274 -- If this is a duplicate body, no point in analyzing it
3276 if Error_Posted (N) then
3277 Ghost_Mode := Save_Ghost_Mode;
3281 -- A subprogram body should cause freezing of its own declaration,
3282 -- but if there was no previous explicit declaration, then the
3283 -- subprogram will get frozen too late (there may be code within
3284 -- the body that depends on the subprogram having been frozen,
3285 -- such as uses of extra formals), so we force it to be frozen
3286 -- here. Same holds if the body and spec are compilation units.
3287 -- Finally, if the return type is an anonymous access to protected
3288 -- subprogram, it must be frozen before the body because its
3289 -- expansion has generated an equivalent type that is used when
3290 -- elaborating the body.
3292 -- An exception in the case of Ada 2012, AI05-177: The bodies
3293 -- created for expression functions do not freeze.
3296 and then Nkind (Original_Node (N)) /= N_Expression_Function
3298 Freeze_Before (N, Body_Id);
3300 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3301 Freeze_Before (N, Spec_Id);
3303 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3304 Freeze_Before (N, Etype (Body_Id));
3308 Spec_Id := Corresponding_Spec (N);
3310 -- A subprogram body is Ghost when it is stand alone and subject
3311 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3312 -- the mode now to ensure that any nodes generated during analysis
3313 -- and expansion are properly marked as Ghost.
3315 Set_Ghost_Mode (N, Spec_Id);
3319 -- Previously we scanned the body to look for nested subprograms, and
3320 -- rejected an inline directive if nested subprograms were present,
3321 -- because the back-end would generate conflicting symbols for the
3322 -- nested bodies. This is now unnecessary.
3324 -- Look ahead to recognize a pragma Inline that appears after the body
3326 Check_Inline_Pragma (Spec_Id);
3328 -- Deal with special case of a fully private operation in the body of
3329 -- the protected type. We must create a declaration for the subprogram,
3330 -- in order to attach the protected subprogram that will be used in
3331 -- internal calls. We exclude compiler generated bodies from the
3332 -- expander since the issue does not arise for those cases.
3335 and then Comes_From_Source (N)
3336 and then Is_Protected_Type (Current_Scope)
3338 Spec_Id := Build_Private_Protected_Declaration (N);
3341 -- If we are generating C and this is a function returning a constrained
3342 -- array type for which we must create a procedure with an extra out
3343 -- parameter, build and analyze the body now. The procedure declaration
3344 -- has already been created. We reuse the source body of the function,
3345 -- because in an instance it may contain global references that cannot
3346 -- be reanalyzed. The source function itself is not used any further,
3347 -- so we mark it as having a completion. If the subprogram is a stub the
3348 -- transformation is done later, when the proper body is analyzed.
3351 and then Modify_Tree_For_C
3352 and then Present (Spec_Id)
3353 and then Ekind (Spec_Id) = E_Function
3354 and then Nkind (N) /= N_Subprogram_Body_Stub
3355 and then Rewritten_For_C (Spec_Id)
3357 Set_Has_Completion (Spec_Id);
3359 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3362 -- The entity for the created procedure must remain invisible, so it
3363 -- does not participate in resolution of subsequent references to the
3366 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3370 -- If a separate spec is present, then deal with freezing issues
3372 if Present (Spec_Id) then
3373 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3374 Verify_Overriding_Indicator;
3376 -- In general, the spec will be frozen when we start analyzing the
3377 -- body. However, for internally generated operations, such as
3378 -- wrapper functions for inherited operations with controlling
3379 -- results, the spec may not have been frozen by the time we expand
3380 -- the freeze actions that include the bodies. In particular, extra
3381 -- formals for accessibility or for return-in-place may need to be
3382 -- generated. Freeze nodes, if any, are inserted before the current
3383 -- body. These freeze actions are also needed in ASIS mode and in
3384 -- Compile_Only mode to enable the proper back-end type annotations.
3385 -- They are necessary in any case to insure order of elaboration
3388 if not Is_Frozen (Spec_Id)
3389 and then (Expander_Active
3391 or else (Operating_Mode = Check_Semantics
3392 and then Serious_Errors_Detected = 0))
3394 Set_Has_Delayed_Freeze (Spec_Id);
3395 Freeze_Before (N, Spec_Id);
3399 -- Place subprogram on scope stack, and make formals visible. If there
3400 -- is a spec, the visible entity remains that of the spec.
3402 if Present (Spec_Id) then
3403 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3405 if Is_Child_Unit (Spec_Id) then
3406 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3410 Style.Check_Identifier (Body_Id, Spec_Id);
3413 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3414 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3416 if Is_Abstract_Subprogram (Spec_Id) then
3417 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3418 Ghost_Mode := Save_Ghost_Mode;
3422 Set_Convention (Body_Id, Convention (Spec_Id));
3423 Set_Has_Completion (Spec_Id);
3425 -- Inherit the "ghostness" of the subprogram spec. Note that this
3426 -- property is not directly inherited as the body may be subject
3427 -- to a different Ghost assertion policy.
3429 if Ghost_Mode > None or else Is_Ghost_Entity (Spec_Id) then
3430 Set_Is_Ghost_Entity (Body_Id);
3432 -- The Ghost policy in effect at the point of declaration and
3433 -- at the point of completion must match (SPARK RM 6.9(14)).
3435 Check_Ghost_Completion (Spec_Id, Body_Id);
3438 if Is_Protected_Type (Scope (Spec_Id)) then
3439 Prot_Typ := Scope (Spec_Id);
3442 -- If this is a body generated for a renaming, do not check for
3443 -- full conformance. The check is redundant, because the spec of
3444 -- the body is a copy of the spec in the renaming declaration,
3445 -- and the test can lead to spurious errors on nested defaults.
3447 if Present (Spec_Decl)
3448 and then not Comes_From_Source (N)
3450 (Nkind (Original_Node (Spec_Decl)) =
3451 N_Subprogram_Renaming_Declaration
3452 or else (Present (Corresponding_Body (Spec_Decl))
3454 Nkind (Unit_Declaration_Node
3455 (Corresponding_Body (Spec_Decl))) =
3456 N_Subprogram_Renaming_Declaration))
3460 -- Conversely, the spec may have been generated for specless body
3461 -- with an inline pragma. The entity comes from source, which is
3462 -- both semantically correct and necessary for proper inlining.
3463 -- The subprogram declaration itself is not in the source.
3465 elsif Comes_From_Source (N)
3466 and then Present (Spec_Decl)
3467 and then not Comes_From_Source (Spec_Decl)
3468 and then Has_Pragma_Inline (Spec_Id)
3475 Fully_Conformant, True, Conformant, Body_Id);
3478 -- If the body is not fully conformant, we have to decide if we
3479 -- should analyze it or not. If it has a really messed up profile
3480 -- then we probably should not analyze it, since we will get too
3481 -- many bogus messages.
3483 -- Our decision is to go ahead in the non-fully conformant case
3484 -- only if it is at least mode conformant with the spec. Note
3485 -- that the call to Check_Fully_Conformant has issued the proper
3486 -- error messages to complain about the lack of conformance.
3489 and then not Mode_Conformant (Body_Id, Spec_Id)
3491 Ghost_Mode := Save_Ghost_Mode;
3496 if Spec_Id /= Body_Id then
3497 Reference_Body_Formals (Spec_Id, Body_Id);
3500 Set_Ekind (Body_Id, E_Subprogram_Body);
3502 if Nkind (N) = N_Subprogram_Body_Stub then
3503 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3508 Set_Corresponding_Spec (N, Spec_Id);
3510 -- Ada 2005 (AI-345): If the operation is a primitive operation
3511 -- of a concurrent type, the type of the first parameter has been
3512 -- replaced with the corresponding record, which is the proper
3513 -- run-time structure to use. However, within the body there may
3514 -- be uses of the formals that depend on primitive operations
3515 -- of the type (in particular calls in prefixed form) for which
3516 -- we need the original concurrent type. The operation may have
3517 -- several controlling formals, so the replacement must be done
3520 if Comes_From_Source (Spec_Id)
3521 and then Present (First_Entity (Spec_Id))
3522 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3523 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3524 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3525 and then Present (Corresponding_Concurrent_Type
3526 (Etype (First_Entity (Spec_Id))))
3529 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3533 Form := First_Formal (Spec_Id);
3534 while Present (Form) loop
3535 if Etype (Form) = Typ then
3536 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3544 -- Make the formals visible, and place subprogram on scope stack.
3545 -- This is also the point at which we set Last_Real_Spec_Entity
3546 -- to mark the entities which will not be moved to the body.
3548 Install_Formals (Spec_Id);
3549 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3551 -- Within an instance, add local renaming declarations so that
3552 -- gdb can retrieve the values of actuals more easily. This is
3553 -- only relevant if generating code (and indeed we definitely
3554 -- do not want these definitions -gnatc mode, because that would
3557 if Is_Generic_Instance (Spec_Id)
3558 and then Is_Wrapper_Package (Current_Scope)
3559 and then Expander_Active
3561 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3564 Push_Scope (Spec_Id);
3566 -- Make sure that the subprogram is immediately visible. For
3567 -- child units that have no separate spec this is indispensable.
3568 -- Otherwise it is safe albeit redundant.
3570 Set_Is_Immediately_Visible (Spec_Id);
3573 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3574 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3575 Set_Scope (Body_Id, Scope (Spec_Id));
3577 -- Case of subprogram body with no previous spec
3580 -- Check for style warning required
3584 -- Only apply check for source level subprograms for which checks
3585 -- have not been suppressed.
3587 and then Comes_From_Source (Body_Id)
3588 and then not Suppress_Style_Checks (Body_Id)
3590 -- No warnings within an instance
3592 and then not In_Instance
3594 -- No warnings for expression functions
3596 and then Nkind (Original_Node (N)) /= N_Expression_Function
3598 Style.Body_With_No_Spec (N);
3601 New_Overloaded_Entity (Body_Id);
3603 -- A subprogram body declared within a Ghost region is automatically
3604 -- Ghost (SPARK RM 6.9(2)).
3606 if Ghost_Mode > None then
3607 Set_Is_Ghost_Entity (Body_Id);
3610 if Nkind (N) /= N_Subprogram_Body_Stub then
3611 Set_Acts_As_Spec (N);
3612 Generate_Definition (Body_Id);
3614 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3615 Install_Formals (Body_Id);
3617 Push_Scope (Body_Id);
3620 -- For stubs and bodies with no previous spec, generate references to
3623 Generate_Reference_To_Formals (Body_Id);
3626 -- Entry barrier functions are generated outside the protected type and
3627 -- should not carry the SPARK_Mode of the enclosing context.
3629 if Nkind (N) = N_Subprogram_Body
3630 and then Is_Entry_Barrier_Function (N)
3634 -- The body is generated as part of expression function expansion. When
3635 -- the expression function appears in the visible declarations of a
3636 -- package, the body is added to the private declarations. Since both
3637 -- declarative lists may be subject to a different SPARK_Mode, inherit
3638 -- the mode of the spec.
3640 -- package P with SPARK_Mode is
3641 -- function Expr_Func ... is (...); -- original
3642 -- [function Expr_Func ...;] -- generated spec
3645 -- pragma SPARK_Mode (Off);
3646 -- [function Expr_Func ... is return ...;] -- generated body
3647 -- end P; -- mode is ON
3649 elsif not Comes_From_Source (N)
3650 and then Present (Prev_Id)
3651 and then Is_Expression_Function (Prev_Id)
3653 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Prev_Id));
3654 Set_SPARK_Pragma_Inherited
3655 (Body_Id, SPARK_Pragma_Inherited (Prev_Id));
3657 -- Set the SPARK_Mode from the current context (may be overwritten later
3658 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
3659 -- initially on a stand-alone subprogram body, but is then relocated to
3660 -- a generated corresponding spec. In this scenario the mode is shared
3661 -- between the spec and body.
3663 elsif No (SPARK_Pragma (Body_Id)) then
3664 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3665 Set_SPARK_Pragma_Inherited (Body_Id);
3668 -- If the return type is an anonymous access type whose designated type
3669 -- is the limited view of a class-wide type and the non-limited view is
3670 -- available, update the return type accordingly.
3672 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3678 Rtyp := Etype (Current_Scope);
3680 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3681 Etyp := Directly_Designated_Type (Rtyp);
3683 if Is_Class_Wide_Type (Etyp)
3684 and then From_Limited_With (Etyp)
3686 Set_Directly_Designated_Type
3687 (Etype (Current_Scope), Available_View (Etyp));
3693 -- If this is the proper body of a stub, we must verify that the stub
3694 -- conforms to the body, and to the previous spec if one was present.
3695 -- We know already that the body conforms to that spec. This test is
3696 -- only required for subprograms that come from source.
3698 if Nkind (Parent (N)) = N_Subunit
3699 and then Comes_From_Source (N)
3700 and then not Error_Posted (Body_Id)
3701 and then Nkind (Corresponding_Stub (Parent (N))) =
3702 N_Subprogram_Body_Stub
3705 Old_Id : constant Entity_Id :=
3707 (Specification (Corresponding_Stub (Parent (N))));
3709 Conformant : Boolean := False;
3712 if No (Spec_Id) then
3713 Check_Fully_Conformant (Body_Id, Old_Id);
3717 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3719 if not Conformant then
3721 -- The stub was taken to be a new declaration. Indicate that
3724 Set_Has_Completion (Old_Id, False);
3730 Set_Has_Completion (Body_Id);
3731 Check_Eliminated (Body_Id);
3733 -- Analyze any aspect specifications that appear on the subprogram body
3734 -- stub. Stop the analysis now as the stub does not have a declarative
3735 -- or a statement part, and it cannot be inlined.
3737 if Nkind (N) = N_Subprogram_Body_Stub then
3738 if Has_Aspects (N) then
3739 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3742 Ghost_Mode := Save_Ghost_Mode;
3746 -- Handle frontend inlining
3748 -- Note: Normally we don't do any inlining if expansion is off, since
3749 -- we won't generate code in any case. An exception arises in GNATprove
3750 -- mode where we want to expand some calls in place, even with expansion
3751 -- disabled, since the inlining eases formal verification.
3753 if not GNATprove_Mode
3754 and then Expander_Active
3755 and then Serious_Errors_Detected = 0
3756 and then Present (Spec_Id)
3757 and then Has_Pragma_Inline (Spec_Id)
3759 -- Legacy implementation (relying on frontend inlining)
3761 if not Back_End_Inlining then
3762 if (Has_Pragma_Inline_Always (Spec_Id)
3763 and then not Opt.Disable_FE_Inline_Always)
3765 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3766 and then not Opt.Disable_FE_Inline)
3768 Build_Body_To_Inline (N, Spec_Id);
3771 -- New implementation (relying on backend inlining)
3774 if Has_Pragma_Inline_Always (Spec_Id)
3775 or else Optimization_Level > 0
3777 -- Handle function returning an unconstrained type
3779 if Comes_From_Source (Body_Id)
3780 and then Ekind (Spec_Id) = E_Function
3781 and then Returns_Unconstrained_Type (Spec_Id)
3783 -- If function builds in place, i.e. returns a limited type,
3784 -- inlining cannot be done.
3786 and then not Is_Limited_Type (Etype (Spec_Id))
3788 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3792 Subp_Body : constant Node_Id :=
3793 Unit_Declaration_Node (Body_Id);
3794 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3797 -- Do not pass inlining to the backend if the subprogram
3798 -- has declarations or statements which cannot be inlined
3799 -- by the backend. This check is done here to emit an
3800 -- error instead of the generic warning message reported
3801 -- by the GCC backend (ie. "function might not be
3804 if Present (Subp_Decl)
3805 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3809 elsif Has_Excluded_Statement
3812 (Handled_Statement_Sequence (Subp_Body)))
3816 -- If the backend inlining is available then at this
3817 -- stage we only have to mark the subprogram as inlined.
3818 -- The expander will take care of registering it in the
3819 -- table of subprograms inlined by the backend a part of
3820 -- processing calls to it (cf. Expand_Call)
3823 Set_Is_Inlined (Spec_Id);
3830 -- In GNATprove mode, inline only when there is a separate subprogram
3831 -- declaration for now, as inlining of subprogram bodies acting as
3832 -- declarations, or subprogram stubs, are not supported by frontend
3833 -- inlining. This inlining should occur after analysis of the body, so
3834 -- that it is known whether the value of SPARK_Mode, which can be
3835 -- defined by a pragma inside the body, is applicable to the body.
3837 elsif GNATprove_Mode
3838 and then Full_Analysis
3839 and then not Inside_A_Generic
3840 and then Present (Spec_Id)
3842 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
3843 and then Body_Has_SPARK_Mode_On
3844 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3845 and then not Body_Has_Contract
3847 Build_Body_To_Inline (N, Spec_Id);
3850 -- When generating code, inherited pre/postconditions are handled when
3851 -- expanding the corresponding contract.
3853 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3854 -- of the specification we have to install the private withed units.
3855 -- This holds for child units as well.
3857 if Is_Compilation_Unit (Body_Id)
3858 or else Nkind (Parent (N)) = N_Compilation_Unit
3860 Install_Private_With_Clauses (Body_Id);
3863 Check_Anonymous_Return;
3865 -- Set the Protected_Formal field of each extra formal of the protected
3866 -- subprogram to reference the corresponding extra formal of the
3867 -- subprogram that implements it. For regular formals this occurs when
3868 -- the protected subprogram's declaration is expanded, but the extra
3869 -- formals don't get created until the subprogram is frozen. We need to
3870 -- do this before analyzing the protected subprogram's body so that any
3871 -- references to the original subprogram's extra formals will be changed
3872 -- refer to the implementing subprogram's formals (see Expand_Formal).
3874 if Present (Spec_Id)
3875 and then Is_Protected_Type (Scope (Spec_Id))
3876 and then Present (Protected_Body_Subprogram (Spec_Id))
3879 Impl_Subp : constant Entity_Id :=
3880 Protected_Body_Subprogram (Spec_Id);
3881 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3882 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3884 while Present (Prot_Ext_Formal) loop
3885 pragma Assert (Present (Impl_Ext_Formal));
3886 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3887 Next_Formal_With_Extras (Prot_Ext_Formal);
3888 Next_Formal_With_Extras (Impl_Ext_Formal);
3893 -- Now we can go on to analyze the body
3895 HSS := Handled_Statement_Sequence (N);
3896 Set_Actual_Subtypes (N, Current_Scope);
3898 -- Add a declaration for the Protection object, renaming declarations
3899 -- for discriminals and privals and finally a declaration for the entry
3900 -- family index (if applicable). This form of early expansion is done
3901 -- when the Expander is active because Install_Private_Data_Declarations
3902 -- references entities which were created during regular expansion. The
3903 -- subprogram entity must come from source, and not be an internally
3904 -- generated subprogram.
3907 and then Present (Prot_Typ)
3908 and then Present (Spec_Id)
3909 and then Comes_From_Source (Spec_Id)
3910 and then not Is_Eliminated (Spec_Id)
3912 Install_Private_Data_Declarations
3913 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3916 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3917 -- may now appear in parameter and result profiles. Since the analysis
3918 -- of a subprogram body may use the parameter and result profile of the
3919 -- spec, swap any limited views with their non-limited counterpart.
3921 if Ada_Version >= Ada_2012 then
3922 Exch_Views := Exchange_Limited_Views (Spec_Id);
3925 -- Analyze any aspect specifications that appear on the subprogram body
3927 if Has_Aspects (N) then
3928 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3931 Analyze_Declarations (Declarations (N));
3933 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3935 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3936 if Present (SPARK_Pragma (Spec_Id)) then
3937 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
3939 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
3941 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3942 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3943 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3945 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3948 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3952 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3953 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3954 Error_Msg_Sloc := Sloc (Spec_Id);
3956 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3960 -- A subprogram body "freezes" its own contract. Analyze the contract
3961 -- after the declarations of the body have been processed as pragmas
3962 -- are now chained on the contract of the subprogram body.
3964 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
3966 -- Check completion, and analyze the statements
3969 Inspect_Deferred_Constant_Completion (Declarations (N));
3972 -- Deal with end of scope processing for the body
3974 Process_End_Label (HSS, 't', Current_Scope);
3976 Check_Subprogram_Order (N);
3977 Set_Analyzed (Body_Id);
3979 -- If we have a separate spec, then the analysis of the declarations
3980 -- caused the entities in the body to be chained to the spec id, but
3981 -- we want them chained to the body id. Only the formal parameters
3982 -- end up chained to the spec id in this case.
3984 if Present (Spec_Id) then
3986 -- We must conform to the categorization of our spec
3988 Validate_Categorization_Dependency (N, Spec_Id);
3990 -- And if this is a child unit, the parent units must conform
3992 if Is_Child_Unit (Spec_Id) then
3993 Validate_Categorization_Dependency
3994 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3997 -- Here is where we move entities from the spec to the body
3999 -- Case where there are entities that stay with the spec
4001 if Present (Last_Real_Spec_Entity) then
4003 -- No body entities (happens when the only real spec entities come
4004 -- from precondition and postcondition pragmas).
4006 if No (Last_Entity (Body_Id)) then
4007 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4009 -- Body entities present (formals), so chain stuff past them
4013 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4016 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4017 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4018 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4020 -- Case where there are no spec entities, in this case there can be
4021 -- no body entities either, so just move everything.
4023 -- If the body is generated for an expression function, it may have
4024 -- been preanalyzed already, if 'access was applied to it.
4027 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4028 N_Expression_Function
4030 pragma Assert (No (Last_Entity (Body_Id)));
4034 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4035 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4036 Set_First_Entity (Spec_Id, Empty);
4037 Set_Last_Entity (Spec_Id, Empty);
4041 Check_Missing_Return;
4043 -- Now we are going to check for variables that are never modified in
4044 -- the body of the procedure. But first we deal with a special case
4045 -- where we want to modify this check. If the body of the subprogram
4046 -- starts with a raise statement or its equivalent, or if the body
4047 -- consists entirely of a null statement, then it is pretty obvious that
4048 -- it is OK to not reference the parameters. For example, this might be
4049 -- the following common idiom for a stubbed function: statement of the
4050 -- procedure raises an exception. In particular this deals with the
4051 -- common idiom of a stubbed function, which appears something like:
4053 -- function F (A : Integer) return Some_Type;
4056 -- raise Program_Error;
4060 -- Here the purpose of X is simply to satisfy the annoying requirement
4061 -- in Ada that there be at least one return, and we certainly do not
4062 -- want to go posting warnings on X that it is not initialized. On
4063 -- the other hand, if X is entirely unreferenced that should still
4066 -- What we do is to detect these cases, and if we find them, flag the
4067 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4068 -- suppress unwanted warnings. For the case of the function stub above
4069 -- we have a special test to set X as apparently assigned to suppress
4076 -- Skip initial labels (for one thing this occurs when we are in
4077 -- front end ZCX mode, but in any case it is irrelevant), and also
4078 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4080 Stm := First (Statements (HSS));
4081 while Nkind (Stm) = N_Label
4082 or else Nkind (Stm) in N_Push_xxx_Label
4087 -- Do the test on the original statement before expansion
4090 Ostm : constant Node_Id := Original_Node (Stm);
4093 -- If explicit raise statement, turn on flag
4095 if Nkind (Ostm) = N_Raise_Statement then
4096 Set_Trivial_Subprogram (Stm);
4098 -- If null statement, and no following statements, turn on flag
4100 elsif Nkind (Stm) = N_Null_Statement
4101 and then Comes_From_Source (Stm)
4102 and then No (Next (Stm))
4104 Set_Trivial_Subprogram (Stm);
4106 -- Check for explicit call cases which likely raise an exception
4108 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4109 if Is_Entity_Name (Name (Ostm)) then
4111 Ent : constant Entity_Id := Entity (Name (Ostm));
4114 -- If the procedure is marked No_Return, then likely it
4115 -- raises an exception, but in any case it is not coming
4116 -- back here, so turn on the flag.
4119 and then Ekind (Ent) = E_Procedure
4120 and then No_Return (Ent)
4122 Set_Trivial_Subprogram (Stm);
4130 -- Check for variables that are never modified
4136 -- If there is a separate spec, then transfer Never_Set_In_Source
4137 -- flags from out parameters to the corresponding entities in the
4138 -- body. The reason we do that is we want to post error flags on
4139 -- the body entities, not the spec entities.
4141 if Present (Spec_Id) then
4142 E1 := First_Entity (Spec_Id);
4143 while Present (E1) loop
4144 if Ekind (E1) = E_Out_Parameter then
4145 E2 := First_Entity (Body_Id);
4146 while Present (E2) loop
4147 exit when Chars (E1) = Chars (E2);
4151 if Present (E2) then
4152 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4160 -- Check references in body
4162 Check_References (Body_Id);
4165 -- Check for nested subprogram, and mark outer level subprogram if so
4171 if Present (Spec_Id) then
4178 Ent := Enclosing_Subprogram (Ent);
4179 exit when No (Ent) or else Is_Subprogram (Ent);
4182 if Present (Ent) then
4183 Set_Has_Nested_Subprogram (Ent);
4187 -- Restore the limited views in the spec, if any, to let the back end
4188 -- process it without running into circularities.
4190 if Exch_Views /= No_Elist then
4191 Restore_Limited_Views (Exch_Views);
4194 Ghost_Mode := Save_Ghost_Mode;
4195 end Analyze_Subprogram_Body_Helper;
4197 ------------------------------------
4198 -- Analyze_Subprogram_Declaration --
4199 ------------------------------------
4201 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4202 Scop : constant Entity_Id := Current_Scope;
4203 Designator : Entity_Id;
4205 Is_Completion : Boolean;
4206 -- Indicates whether a null procedure declaration is a completion
4209 -- Null procedures are not allowed in SPARK
4211 if Nkind (Specification (N)) = N_Procedure_Specification
4212 and then Null_Present (Specification (N))
4214 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4216 -- Null procedures are allowed in protected types, following the
4217 -- recent AI12-0147.
4219 if Is_Protected_Type (Current_Scope)
4220 and then Ada_Version < Ada_2012
4222 Error_Msg_N ("protected operation cannot be a null procedure", N);
4225 Analyze_Null_Procedure (N, Is_Completion);
4227 -- The null procedure acts as a body, nothing further is needed
4229 if Is_Completion then
4234 Designator := Analyze_Subprogram_Specification (Specification (N));
4236 -- A reference may already have been generated for the unit name, in
4237 -- which case the following call is redundant. However it is needed for
4238 -- declarations that are the rewriting of an expression function.
4240 Generate_Definition (Designator);
4242 -- Set the SPARK mode from the current context (may be overwritten later
4243 -- with explicit pragma). This is not done for entry barrier functions
4244 -- because they are generated outside the protected type and should not
4245 -- carry the mode of the enclosing context.
4247 if Nkind (N) = N_Subprogram_Declaration
4248 and then Is_Entry_Barrier_Function (N)
4252 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4253 Set_SPARK_Pragma_Inherited (Designator);
4256 -- A subprogram declared within a Ghost region is automatically Ghost
4257 -- (SPARK RM 6.9(2)).
4259 if Ghost_Mode > None then
4260 Set_Is_Ghost_Entity (Designator);
4263 if Debug_Flag_C then
4264 Write_Str ("==> subprogram spec ");
4265 Write_Name (Chars (Designator));
4266 Write_Str (" from ");
4267 Write_Location (Sloc (N));
4272 Validate_RCI_Subprogram_Declaration (N);
4273 New_Overloaded_Entity (Designator);
4274 Check_Delayed_Subprogram (Designator);
4276 -- If the type of the first formal of the current subprogram is a non-
4277 -- generic tagged private type, mark the subprogram as being a private
4278 -- primitive. Ditto if this is a function with controlling result, and
4279 -- the return type is currently private. In both cases, the type of the
4280 -- controlling argument or result must be in the current scope for the
4281 -- operation to be primitive.
4283 if Has_Controlling_Result (Designator)
4284 and then Is_Private_Type (Etype (Designator))
4285 and then Scope (Etype (Designator)) = Current_Scope
4286 and then not Is_Generic_Actual_Type (Etype (Designator))
4288 Set_Is_Private_Primitive (Designator);
4290 elsif Present (First_Formal (Designator)) then
4292 Formal_Typ : constant Entity_Id :=
4293 Etype (First_Formal (Designator));
4295 Set_Is_Private_Primitive (Designator,
4296 Is_Tagged_Type (Formal_Typ)
4297 and then Scope (Formal_Typ) = Current_Scope
4298 and then Is_Private_Type (Formal_Typ)
4299 and then not Is_Generic_Actual_Type (Formal_Typ));
4303 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4306 if Ada_Version >= Ada_2005
4307 and then Comes_From_Source (N)
4308 and then Is_Dispatching_Operation (Designator)
4315 if Has_Controlling_Result (Designator) then
4316 Etyp := Etype (Designator);
4319 E := First_Entity (Designator);
4321 and then Is_Formal (E)
4322 and then not Is_Controlling_Formal (E)
4330 if Is_Access_Type (Etyp) then
4331 Etyp := Directly_Designated_Type (Etyp);
4334 if Is_Interface (Etyp)
4335 and then not Is_Abstract_Subprogram (Designator)
4336 and then not (Ekind (Designator) = E_Procedure
4337 and then Null_Present (Specification (N)))
4339 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4341 -- Specialize error message based on procedures vs. functions,
4342 -- since functions can't be null subprograms.
4344 if Ekind (Designator) = E_Procedure then
4346 ("interface procedure % must be abstract or null", N);
4349 ("interface function % must be abstract", N);
4355 -- What is the following code for, it used to be
4357 -- ??? Set_Suppress_Elaboration_Checks
4358 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4360 -- The following seems equivalent, but a bit dubious
4362 if Elaboration_Checks_Suppressed (Designator) then
4363 Set_Kill_Elaboration_Checks (Designator);
4366 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4367 Set_Categorization_From_Scope (Designator, Scop);
4370 -- For a compilation unit, check for library-unit pragmas
4372 Push_Scope (Designator);
4373 Set_Categorization_From_Pragmas (N);
4374 Validate_Categorization_Dependency (N, Designator);
4378 -- For a compilation unit, set body required. This flag will only be
4379 -- reset if a valid Import or Interface pragma is processed later on.
4381 if Nkind (Parent (N)) = N_Compilation_Unit then
4382 Set_Body_Required (Parent (N), True);
4384 if Ada_Version >= Ada_2005
4385 and then Nkind (Specification (N)) = N_Procedure_Specification
4386 and then Null_Present (Specification (N))
4389 ("null procedure cannot be declared at library level", N);
4393 Generate_Reference_To_Formals (Designator);
4394 Check_Eliminated (Designator);
4396 if Debug_Flag_C then
4398 Write_Str ("<== subprogram spec ");
4399 Write_Name (Chars (Designator));
4400 Write_Str (" from ");
4401 Write_Location (Sloc (N));
4405 if Is_Protected_Type (Current_Scope) then
4407 -- Indicate that this is a protected operation, because it may be
4408 -- used in subsequent declarations within the protected type.
4410 Set_Convention (Designator, Convention_Protected);
4413 List_Inherited_Pre_Post_Aspects (Designator);
4415 if Has_Aspects (N) then
4416 Analyze_Aspect_Specifications (N, Designator);
4418 end Analyze_Subprogram_Declaration;
4420 --------------------------------------
4421 -- Analyze_Subprogram_Specification --
4422 --------------------------------------
4424 -- Reminder: N here really is a subprogram specification (not a subprogram
4425 -- declaration). This procedure is called to analyze the specification in
4426 -- both subprogram bodies and subprogram declarations (specs).
4428 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4429 Designator : constant Entity_Id := Defining_Entity (N);
4430 Formals : constant List_Id := Parameter_Specifications (N);
4432 -- Start of processing for Analyze_Subprogram_Specification
4435 -- User-defined operator is not allowed in SPARK, except as a renaming
4437 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4438 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4440 Check_SPARK_05_Restriction
4441 ("user-defined operator is not allowed", N);
4444 -- Proceed with analysis. Do not emit a cross-reference entry if the
4445 -- specification comes from an expression function, because it may be
4446 -- the completion of a previous declaration. It is not, the cross-
4447 -- reference entry will be emitted for the new subprogram declaration.
4449 if Nkind (Parent (N)) /= N_Expression_Function then
4450 Generate_Definition (Designator);
4453 if Nkind (N) = N_Function_Specification then
4454 Set_Ekind (Designator, E_Function);
4455 Set_Mechanism (Designator, Default_Mechanism);
4457 Set_Ekind (Designator, E_Procedure);
4458 Set_Etype (Designator, Standard_Void_Type);
4461 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4462 -- those subprograms which could be inlined in GNATprove mode (because
4463 -- Body_To_Inline is non-Empty) but should not be inlined.
4465 if GNATprove_Mode then
4466 Set_Is_Inlined_Always (Designator);
4469 -- Introduce new scope for analysis of the formals and the return type
4471 Set_Scope (Designator, Current_Scope);
4473 if Present (Formals) then
4474 Push_Scope (Designator);
4475 Process_Formals (Formals, N);
4477 -- Check dimensions in N for formals with default expression
4479 Analyze_Dimension_Formals (N, Formals);
4481 -- Ada 2005 (AI-345): If this is an overriding operation of an
4482 -- inherited interface operation, and the controlling type is
4483 -- a synchronized type, replace the type with its corresponding
4484 -- record, to match the proper signature of an overriding operation.
4485 -- Same processing for an access parameter whose designated type is
4486 -- derived from a synchronized interface.
4488 if Ada_Version >= Ada_2005 then
4491 Formal_Typ : Entity_Id;
4492 Rec_Typ : Entity_Id;
4493 Desig_Typ : Entity_Id;
4496 Formal := First_Formal (Designator);
4497 while Present (Formal) loop
4498 Formal_Typ := Etype (Formal);
4500 if Is_Concurrent_Type (Formal_Typ)
4501 and then Present (Corresponding_Record_Type (Formal_Typ))
4503 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4505 if Present (Interfaces (Rec_Typ)) then
4506 Set_Etype (Formal, Rec_Typ);
4509 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4510 Desig_Typ := Designated_Type (Formal_Typ);
4512 if Is_Concurrent_Type (Desig_Typ)
4513 and then Present (Corresponding_Record_Type (Desig_Typ))
4515 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4517 if Present (Interfaces (Rec_Typ)) then
4518 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4523 Next_Formal (Formal);
4530 -- The subprogram scope is pushed and popped around the processing of
4531 -- the return type for consistency with call above to Process_Formals
4532 -- (which itself can call Analyze_Return_Type), and to ensure that any
4533 -- itype created for the return type will be associated with the proper
4536 elsif Nkind (N) = N_Function_Specification then
4537 Push_Scope (Designator);
4538 Analyze_Return_Type (N);
4544 if Nkind (N) = N_Function_Specification then
4546 -- Deal with operator symbol case
4548 if Nkind (Designator) = N_Defining_Operator_Symbol then
4549 Valid_Operator_Definition (Designator);
4552 May_Need_Actuals (Designator);
4554 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4555 -- the subprogram is abstract also. This does not apply to renaming
4556 -- declarations, where abstractness is inherited, and to subprogram
4557 -- bodies generated for stream operations, which become renamings as
4560 -- In case of primitives associated with abstract interface types
4561 -- the check is applied later (see Analyze_Subprogram_Declaration).
4563 if not Nkind_In (Original_Node (Parent (N)),
4564 N_Abstract_Subprogram_Declaration,
4565 N_Formal_Abstract_Subprogram_Declaration,
4566 N_Subprogram_Renaming_Declaration)
4568 if Is_Abstract_Type (Etype (Designator))
4569 and then not Is_Interface (Etype (Designator))
4572 ("function that returns abstract type must be abstract", N);
4574 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4575 -- access result whose designated type is abstract.
4577 elsif Ada_Version >= Ada_2012
4578 and then Nkind (Result_Definition (N)) = N_Access_Definition
4580 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4581 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4584 ("function whose access result designates abstract type "
4585 & "must be abstract", N);
4591 end Analyze_Subprogram_Specification;
4593 -----------------------
4594 -- Check_Conformance --
4595 -----------------------
4597 procedure Check_Conformance
4598 (New_Id : Entity_Id;
4600 Ctype : Conformance_Type;
4602 Conforms : out Boolean;
4603 Err_Loc : Node_Id := Empty;
4604 Get_Inst : Boolean := False;
4605 Skip_Controlling_Formals : Boolean := False)
4607 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4608 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4609 -- If Errmsg is True, then processing continues to post an error message
4610 -- for conformance error on given node. Two messages are output. The
4611 -- first message points to the previous declaration with a general "no
4612 -- conformance" message. The second is the detailed reason, supplied as
4613 -- Msg. The parameter N provide information for a possible & insertion
4614 -- in the message, and also provides the location for posting the
4615 -- message in the absence of a specified Err_Loc location.
4617 -----------------------
4618 -- Conformance_Error --
4619 -----------------------
4621 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4628 if No (Err_Loc) then
4634 Error_Msg_Sloc := Sloc (Old_Id);
4637 when Type_Conformant =>
4638 Error_Msg_N -- CODEFIX
4639 ("not type conformant with declaration#!", Enode);
4641 when Mode_Conformant =>
4642 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4644 ("not mode conformant with operation inherited#!",
4648 ("not mode conformant with declaration#!", Enode);
4651 when Subtype_Conformant =>
4652 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4654 ("not subtype conformant with operation inherited#!",
4658 ("not subtype conformant with declaration#!", Enode);
4661 when Fully_Conformant =>
4662 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4663 Error_Msg_N -- CODEFIX
4664 ("not fully conformant with operation inherited#!",
4667 Error_Msg_N -- CODEFIX
4668 ("not fully conformant with declaration#!", Enode);
4672 Error_Msg_NE (Msg, Enode, N);
4674 end Conformance_Error;
4678 Old_Type : constant Entity_Id := Etype (Old_Id);
4679 New_Type : constant Entity_Id := Etype (New_Id);
4680 Old_Formal : Entity_Id;
4681 New_Formal : Entity_Id;
4682 Access_Types_Match : Boolean;
4683 Old_Formal_Base : Entity_Id;
4684 New_Formal_Base : Entity_Id;
4686 -- Start of processing for Check_Conformance
4691 -- We need a special case for operators, since they don't appear
4694 if Ctype = Type_Conformant then
4695 if Ekind (New_Id) = E_Operator
4696 and then Operator_Matches_Spec (New_Id, Old_Id)
4702 -- If both are functions/operators, check return types conform
4704 if Old_Type /= Standard_Void_Type
4706 New_Type /= Standard_Void_Type
4708 -- If we are checking interface conformance we omit controlling
4709 -- arguments and result, because we are only checking the conformance
4710 -- of the remaining parameters.
4712 if Has_Controlling_Result (Old_Id)
4713 and then Has_Controlling_Result (New_Id)
4714 and then Skip_Controlling_Formals
4718 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4719 if Ctype >= Subtype_Conformant
4720 and then not Predicates_Match (Old_Type, New_Type)
4723 ("\predicate of return type does not match!", New_Id);
4726 ("\return type does not match!", New_Id);
4732 -- Ada 2005 (AI-231): In case of anonymous access types check the
4733 -- null-exclusion and access-to-constant attributes match.
4735 if Ada_Version >= Ada_2005
4736 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4738 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4739 or else Is_Access_Constant (Etype (Old_Type)) /=
4740 Is_Access_Constant (Etype (New_Type)))
4742 Conformance_Error ("\return type does not match!", New_Id);
4746 -- If either is a function/operator and the other isn't, error
4748 elsif Old_Type /= Standard_Void_Type
4749 or else New_Type /= Standard_Void_Type
4751 Conformance_Error ("\functions can only match functions!", New_Id);
4755 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4756 -- If this is a renaming as body, refine error message to indicate that
4757 -- the conflict is with the original declaration. If the entity is not
4758 -- frozen, the conventions don't have to match, the one of the renamed
4759 -- entity is inherited.
4761 if Ctype >= Subtype_Conformant then
4762 if Convention (Old_Id) /= Convention (New_Id) then
4763 if not Is_Frozen (New_Id) then
4766 elsif Present (Err_Loc)
4767 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4768 and then Present (Corresponding_Spec (Err_Loc))
4770 Error_Msg_Name_1 := Chars (New_Id);
4772 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4773 Conformance_Error ("\prior declaration for% has convention %!");
4776 Conformance_Error ("\calling conventions do not match!");
4781 elsif Is_Formal_Subprogram (Old_Id)
4782 or else Is_Formal_Subprogram (New_Id)
4784 Conformance_Error ("\formal subprograms not allowed!");
4789 -- Deal with parameters
4791 -- Note: we use the entity information, rather than going directly
4792 -- to the specification in the tree. This is not only simpler, but
4793 -- absolutely necessary for some cases of conformance tests between
4794 -- operators, where the declaration tree simply does not exist.
4796 Old_Formal := First_Formal (Old_Id);
4797 New_Formal := First_Formal (New_Id);
4798 while Present (Old_Formal) and then Present (New_Formal) loop
4799 if Is_Controlling_Formal (Old_Formal)
4800 and then Is_Controlling_Formal (New_Formal)
4801 and then Skip_Controlling_Formals
4803 -- The controlling formals will have different types when
4804 -- comparing an interface operation with its match, but both
4805 -- or neither must be access parameters.
4807 if Is_Access_Type (Etype (Old_Formal))
4809 Is_Access_Type (Etype (New_Formal))
4811 goto Skip_Controlling_Formal;
4814 ("\access parameter does not match!", New_Formal);
4818 -- Ada 2012: Mode conformance also requires that formal parameters
4819 -- be both aliased, or neither.
4821 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4822 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4824 ("\aliased parameter mismatch!", New_Formal);
4828 if Ctype = Fully_Conformant then
4830 -- Names must match. Error message is more accurate if we do
4831 -- this before checking that the types of the formals match.
4833 if Chars (Old_Formal) /= Chars (New_Formal) then
4834 Conformance_Error ("\name& does not match!", New_Formal);
4836 -- Set error posted flag on new formal as well to stop
4837 -- junk cascaded messages in some cases.
4839 Set_Error_Posted (New_Formal);
4843 -- Null exclusion must match
4845 if Null_Exclusion_Present (Parent (Old_Formal))
4847 Null_Exclusion_Present (Parent (New_Formal))
4849 -- Only give error if both come from source. This should be
4850 -- investigated some time, since it should not be needed ???
4852 if Comes_From_Source (Old_Formal)
4854 Comes_From_Source (New_Formal)
4857 ("\null exclusion for& does not match", New_Formal);
4859 -- Mark error posted on the new formal to avoid duplicated
4860 -- complaint about types not matching.
4862 Set_Error_Posted (New_Formal);
4867 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4868 -- case occurs whenever a subprogram is being renamed and one of its
4869 -- parameters imposes a null exclusion. For example:
4871 -- type T is null record;
4872 -- type Acc_T is access T;
4873 -- subtype Acc_T_Sub is Acc_T;
4875 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4876 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4879 Old_Formal_Base := Etype (Old_Formal);
4880 New_Formal_Base := Etype (New_Formal);
4883 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4884 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4887 Access_Types_Match := Ada_Version >= Ada_2005
4889 -- Ensure that this rule is only applied when New_Id is a
4890 -- renaming of Old_Id.
4892 and then Nkind (Parent (Parent (New_Id))) =
4893 N_Subprogram_Renaming_Declaration
4894 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4895 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4896 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4898 -- Now handle the allowed access-type case
4900 and then Is_Access_Type (Old_Formal_Base)
4901 and then Is_Access_Type (New_Formal_Base)
4903 -- The type kinds must match. The only exception occurs with
4904 -- multiple generics of the form:
4907 -- type F is private; type A is private;
4908 -- type F_Ptr is access F; type A_Ptr is access A;
4909 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4910 -- package F_Pack is ... package A_Pack is
4911 -- package F_Inst is
4912 -- new F_Pack (A, A_Ptr, A_P);
4914 -- When checking for conformance between the parameters of A_P
4915 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4916 -- because the compiler has transformed A_Ptr into a subtype of
4917 -- F_Ptr. We catch this case in the code below.
4919 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4921 (Is_Generic_Type (Old_Formal_Base)
4922 and then Is_Generic_Type (New_Formal_Base)
4923 and then Is_Internal (New_Formal_Base)
4924 and then Etype (Etype (New_Formal_Base)) =
4926 and then Directly_Designated_Type (Old_Formal_Base) =
4927 Directly_Designated_Type (New_Formal_Base)
4928 and then ((Is_Itype (Old_Formal_Base)
4929 and then Can_Never_Be_Null (Old_Formal_Base))
4931 (Is_Itype (New_Formal_Base)
4932 and then Can_Never_Be_Null (New_Formal_Base)));
4934 -- Types must always match. In the visible part of an instance,
4935 -- usual overloading rules for dispatching operations apply, and
4936 -- we check base types (not the actual subtypes).
4938 if In_Instance_Visible_Part
4939 and then Is_Dispatching_Operation (New_Id)
4941 if not Conforming_Types
4942 (T1 => Base_Type (Etype (Old_Formal)),
4943 T2 => Base_Type (Etype (New_Formal)),
4945 Get_Inst => Get_Inst)
4946 and then not Access_Types_Match
4948 Conformance_Error ("\type of & does not match!", New_Formal);
4952 elsif not Conforming_Types
4953 (T1 => Old_Formal_Base,
4954 T2 => New_Formal_Base,
4956 Get_Inst => Get_Inst)
4957 and then not Access_Types_Match
4959 -- Don't give error message if old type is Any_Type. This test
4960 -- avoids some cascaded errors, e.g. in case of a bad spec.
4962 if Errmsg and then Old_Formal_Base = Any_Type then
4965 if Ctype >= Subtype_Conformant
4967 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4970 ("\predicate of & does not match!", New_Formal);
4973 ("\type of & does not match!", New_Formal);
4980 -- For mode conformance, mode must match
4982 if Ctype >= Mode_Conformant then
4983 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4984 if not Ekind_In (New_Id, E_Function, E_Procedure)
4985 or else not Is_Primitive_Wrapper (New_Id)
4987 Conformance_Error ("\mode of & does not match!", New_Formal);
4991 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4993 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
4995 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
4998 ("\mode of & does not match!", New_Formal);
5005 -- Part of mode conformance for access types is having the same
5006 -- constant modifier.
5008 elsif Access_Types_Match
5009 and then Is_Access_Constant (Old_Formal_Base) /=
5010 Is_Access_Constant (New_Formal_Base)
5013 ("\constant modifier does not match!", New_Formal);
5018 if Ctype >= Subtype_Conformant then
5020 -- Ada 2005 (AI-231): In case of anonymous access types check
5021 -- the null-exclusion and access-to-constant attributes must
5022 -- match. For null exclusion, we test the types rather than the
5023 -- formals themselves, since the attribute is only set reliably
5024 -- on the formals in the Ada 95 case, and we exclude the case
5025 -- where Old_Formal is marked as controlling, to avoid errors
5026 -- when matching completing bodies with dispatching declarations
5027 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5029 if Ada_Version >= Ada_2005
5030 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5031 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5033 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5034 Can_Never_Be_Null (Etype (New_Formal))
5036 not Is_Controlling_Formal (Old_Formal))
5038 Is_Access_Constant (Etype (Old_Formal)) /=
5039 Is_Access_Constant (Etype (New_Formal)))
5041 -- Do not complain if error already posted on New_Formal. This
5042 -- avoids some redundant error messages.
5044 and then not Error_Posted (New_Formal)
5046 -- It is allowed to omit the null-exclusion in case of stream
5047 -- attribute subprograms. We recognize stream subprograms
5048 -- through their TSS-generated suffix.
5051 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5054 if TSS_Name /= TSS_Stream_Read
5055 and then TSS_Name /= TSS_Stream_Write
5056 and then TSS_Name /= TSS_Stream_Input
5057 and then TSS_Name /= TSS_Stream_Output
5059 -- Here we have a definite conformance error. It is worth
5060 -- special casing the error message for the case of a
5061 -- controlling formal (which excludes null).
5063 if Is_Controlling_Formal (New_Formal) then
5064 Error_Msg_Node_2 := Scope (New_Formal);
5066 ("\controlling formal & of & excludes null, "
5067 & "declaration must exclude null as well",
5070 -- Normal case (couldn't we give more detail here???)
5074 ("\type of & does not match!", New_Formal);
5083 -- Full conformance checks
5085 if Ctype = Fully_Conformant then
5087 -- We have checked already that names match
5089 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5091 -- Check default expressions for in parameters
5094 NewD : constant Boolean :=
5095 Present (Default_Value (New_Formal));
5096 OldD : constant Boolean :=
5097 Present (Default_Value (Old_Formal));
5099 if NewD or OldD then
5101 -- The old default value has been analyzed because the
5102 -- current full declaration will have frozen everything
5103 -- before. The new default value has not been analyzed,
5104 -- so analyze it now before we check for conformance.
5107 Push_Scope (New_Id);
5108 Preanalyze_Spec_Expression
5109 (Default_Value (New_Formal), Etype (New_Formal));
5113 if not (NewD and OldD)
5114 or else not Fully_Conformant_Expressions
5115 (Default_Value (Old_Formal),
5116 Default_Value (New_Formal))
5119 ("\default expression for & does not match!",
5128 -- A couple of special checks for Ada 83 mode. These checks are
5129 -- skipped if either entity is an operator in package Standard,
5130 -- or if either old or new instance is not from the source program.
5132 if Ada_Version = Ada_83
5133 and then Sloc (Old_Id) > Standard_Location
5134 and then Sloc (New_Id) > Standard_Location
5135 and then Comes_From_Source (Old_Id)
5136 and then Comes_From_Source (New_Id)
5139 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5140 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5143 -- Explicit IN must be present or absent in both cases. This
5144 -- test is required only in the full conformance case.
5146 if In_Present (Old_Param) /= In_Present (New_Param)
5147 and then Ctype = Fully_Conformant
5150 ("\(Ada 83) IN must appear in both declarations",
5155 -- Grouping (use of comma in param lists) must be the same
5156 -- This is where we catch a misconformance like:
5159 -- A : Integer; B : Integer
5161 -- which are represented identically in the tree except
5162 -- for the setting of the flags More_Ids and Prev_Ids.
5164 if More_Ids (Old_Param) /= More_Ids (New_Param)
5165 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5168 ("\grouping of & does not match!", New_Formal);
5174 -- This label is required when skipping controlling formals
5176 <<Skip_Controlling_Formal>>
5178 Next_Formal (Old_Formal);
5179 Next_Formal (New_Formal);
5182 if Present (Old_Formal) then
5183 Conformance_Error ("\too few parameters!");
5186 elsif Present (New_Formal) then
5187 Conformance_Error ("\too many parameters!", New_Formal);
5190 end Check_Conformance;
5192 -----------------------
5193 -- Check_Conventions --
5194 -----------------------
5196 procedure Check_Conventions (Typ : Entity_Id) is
5197 Ifaces_List : Elist_Id;
5199 procedure Check_Convention (Op : Entity_Id);
5200 -- Verify that the convention of inherited dispatching operation Op is
5201 -- consistent among all subprograms it overrides. In order to minimize
5202 -- the search, Search_From is utilized to designate a specific point in
5203 -- the list rather than iterating over the whole list once more.
5205 ----------------------
5206 -- Check_Convention --
5207 ----------------------
5209 procedure Check_Convention (Op : Entity_Id) is
5210 Op_Conv : constant Convention_Id := Convention (Op);
5211 Iface_Conv : Convention_Id;
5212 Iface_Elmt : Elmt_Id;
5213 Iface_Prim_Elmt : Elmt_Id;
5214 Iface_Prim : Entity_Id;
5217 Iface_Elmt := First_Elmt (Ifaces_List);
5218 while Present (Iface_Elmt) loop
5220 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5221 while Present (Iface_Prim_Elmt) loop
5222 Iface_Prim := Node (Iface_Prim_Elmt);
5223 Iface_Conv := Convention (Iface_Prim);
5225 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5226 and then Iface_Conv /= Op_Conv
5229 ("inconsistent conventions in primitive operations", Typ);
5231 Error_Msg_Name_1 := Chars (Op);
5232 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5233 Error_Msg_Sloc := Sloc (Op);
5235 if Comes_From_Source (Op) or else No (Alias (Op)) then
5236 if not Present (Overridden_Operation (Op)) then
5237 Error_Msg_N ("\\primitive % defined #", Typ);
5240 ("\\overriding operation % with "
5241 & "convention % defined #", Typ);
5244 else pragma Assert (Present (Alias (Op)));
5245 Error_Msg_Sloc := Sloc (Alias (Op));
5246 Error_Msg_N ("\\inherited operation % with "
5247 & "convention % defined #", Typ);
5250 Error_Msg_Name_1 := Chars (Op);
5251 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5252 Error_Msg_Sloc := Sloc (Iface_Prim);
5253 Error_Msg_N ("\\overridden operation % with "
5254 & "convention % defined #", Typ);
5256 -- Avoid cascading errors
5261 Next_Elmt (Iface_Prim_Elmt);
5264 Next_Elmt (Iface_Elmt);
5266 end Check_Convention;
5270 Prim_Op : Entity_Id;
5271 Prim_Op_Elmt : Elmt_Id;
5273 -- Start of processing for Check_Conventions
5276 if not Has_Interfaces (Typ) then
5280 Collect_Interfaces (Typ, Ifaces_List);
5282 -- The algorithm checks every overriding dispatching operation against
5283 -- all the corresponding overridden dispatching operations, detecting
5284 -- differences in conventions.
5286 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5287 while Present (Prim_Op_Elmt) loop
5288 Prim_Op := Node (Prim_Op_Elmt);
5290 -- A small optimization: skip the predefined dispatching operations
5291 -- since they always have the same convention.
5293 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5294 Check_Convention (Prim_Op);
5297 Next_Elmt (Prim_Op_Elmt);
5299 end Check_Conventions;
5301 ------------------------------
5302 -- Check_Delayed_Subprogram --
5303 ------------------------------
5305 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5308 procedure Possible_Freeze (T : Entity_Id);
5309 -- T is the type of either a formal parameter or of the return type.
5310 -- If T is not yet frozen and needs a delayed freeze, then the
5311 -- subprogram itself must be delayed.
5313 ---------------------
5314 -- Possible_Freeze --
5315 ---------------------
5317 procedure Possible_Freeze (T : Entity_Id) is
5319 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5320 Set_Has_Delayed_Freeze (Designator);
5322 elsif Is_Access_Type (T)
5323 and then Has_Delayed_Freeze (Designated_Type (T))
5324 and then not Is_Frozen (Designated_Type (T))
5326 Set_Has_Delayed_Freeze (Designator);
5329 end Possible_Freeze;
5331 -- Start of processing for Check_Delayed_Subprogram
5334 -- All subprograms, including abstract subprograms, may need a freeze
5335 -- node if some formal type or the return type needs one.
5337 Possible_Freeze (Etype (Designator));
5338 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5340 -- Need delayed freeze if any of the formal types themselves need
5341 -- a delayed freeze and are not yet frozen.
5343 F := First_Formal (Designator);
5344 while Present (F) loop
5345 Possible_Freeze (Etype (F));
5346 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5350 -- Mark functions that return by reference. Note that it cannot be
5351 -- done for delayed_freeze subprograms because the underlying
5352 -- returned type may not be known yet (for private types)
5354 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5356 Typ : constant Entity_Id := Etype (Designator);
5357 Utyp : constant Entity_Id := Underlying_Type (Typ);
5359 if Is_Limited_View (Typ) then
5360 Set_Returns_By_Ref (Designator);
5361 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5362 Set_Returns_By_Ref (Designator);
5366 end Check_Delayed_Subprogram;
5368 ------------------------------------
5369 -- Check_Discriminant_Conformance --
5370 ------------------------------------
5372 procedure Check_Discriminant_Conformance
5377 Old_Discr : Entity_Id := First_Discriminant (Prev);
5378 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5379 New_Discr_Id : Entity_Id;
5380 New_Discr_Type : Entity_Id;
5382 procedure Conformance_Error (Msg : String; N : Node_Id);
5383 -- Post error message for conformance error on given node. Two messages
5384 -- are output. The first points to the previous declaration with a
5385 -- general "no conformance" message. The second is the detailed reason,
5386 -- supplied as Msg. The parameter N provide information for a possible
5387 -- & insertion in the message.
5389 -----------------------
5390 -- Conformance_Error --
5391 -----------------------
5393 procedure Conformance_Error (Msg : String; N : Node_Id) is
5395 Error_Msg_Sloc := Sloc (Prev_Loc);
5396 Error_Msg_N -- CODEFIX
5397 ("not fully conformant with declaration#!", N);
5398 Error_Msg_NE (Msg, N, N);
5399 end Conformance_Error;
5401 -- Start of processing for Check_Discriminant_Conformance
5404 while Present (Old_Discr) and then Present (New_Discr) loop
5405 New_Discr_Id := Defining_Identifier (New_Discr);
5407 -- The subtype mark of the discriminant on the full type has not
5408 -- been analyzed so we do it here. For an access discriminant a new
5411 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5413 Access_Definition (N, Discriminant_Type (New_Discr));
5416 Analyze (Discriminant_Type (New_Discr));
5417 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5419 -- Ada 2005: if the discriminant definition carries a null
5420 -- exclusion, create an itype to check properly for consistency
5421 -- with partial declaration.
5423 if Is_Access_Type (New_Discr_Type)
5424 and then Null_Exclusion_Present (New_Discr)
5427 Create_Null_Excluding_Itype
5428 (T => New_Discr_Type,
5429 Related_Nod => New_Discr,
5430 Scope_Id => Current_Scope);
5434 if not Conforming_Types
5435 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5437 Conformance_Error ("type of & does not match!", New_Discr_Id);
5440 -- Treat the new discriminant as an occurrence of the old one,
5441 -- for navigation purposes, and fill in some semantic
5442 -- information, for completeness.
5444 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5445 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5446 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5451 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5452 Conformance_Error ("name & does not match!", New_Discr_Id);
5456 -- Default expressions must match
5459 NewD : constant Boolean :=
5460 Present (Expression (New_Discr));
5461 OldD : constant Boolean :=
5462 Present (Expression (Parent (Old_Discr)));
5465 if NewD or OldD then
5467 -- The old default value has been analyzed and expanded,
5468 -- because the current full declaration will have frozen
5469 -- everything before. The new default values have not been
5470 -- expanded, so expand now to check conformance.
5473 Preanalyze_Spec_Expression
5474 (Expression (New_Discr), New_Discr_Type);
5477 if not (NewD and OldD)
5478 or else not Fully_Conformant_Expressions
5479 (Expression (Parent (Old_Discr)),
5480 Expression (New_Discr))
5484 ("default expression for & does not match!",
5491 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5493 if Ada_Version = Ada_83 then
5495 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5498 -- Grouping (use of comma in param lists) must be the same
5499 -- This is where we catch a misconformance like:
5502 -- A : Integer; B : Integer
5504 -- which are represented identically in the tree except
5505 -- for the setting of the flags More_Ids and Prev_Ids.
5507 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5508 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5511 ("grouping of & does not match!", New_Discr_Id);
5517 Next_Discriminant (Old_Discr);
5521 if Present (Old_Discr) then
5522 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5525 elsif Present (New_Discr) then
5527 ("too many discriminants!", Defining_Identifier (New_Discr));
5530 end Check_Discriminant_Conformance;
5532 ----------------------------
5533 -- Check_Fully_Conformant --
5534 ----------------------------
5536 procedure Check_Fully_Conformant
5537 (New_Id : Entity_Id;
5539 Err_Loc : Node_Id := Empty)
5542 pragma Warnings (Off, Result);
5545 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5546 end Check_Fully_Conformant;
5548 --------------------------
5549 -- Check_Limited_Return --
5550 --------------------------
5552 procedure Check_Limited_Return
5558 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
5559 -- replaced by anonymous access results. This is an incompatibility with
5560 -- Ada 95. Not clear whether this should be enforced yet or perhaps
5561 -- controllable with special switch. ???
5563 -- A limited interface that is not immutably limited is OK
5565 if Is_Limited_Interface (R_Type)
5567 not (Is_Task_Interface (R_Type)
5568 or else Is_Protected_Interface (R_Type)
5569 or else Is_Synchronized_Interface (R_Type))
5573 elsif Is_Limited_Type (R_Type)
5574 and then not Is_Interface (R_Type)
5575 and then Comes_From_Source (N)
5576 and then not In_Instance_Body
5577 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
5579 -- Error in Ada 2005
5581 if Ada_Version >= Ada_2005
5582 and then not Debug_Flag_Dot_L
5583 and then not GNAT_Mode
5586 ("(Ada 2005) cannot copy object of a limited type "
5587 & "(RM-2005 6.5(5.5/2))", Expr);
5589 if Is_Limited_View (R_Type) then
5591 ("\return by reference not permitted in Ada 2005", Expr);
5594 -- Warn in Ada 95 mode, to give folks a heads up about this
5597 -- In GNAT mode, this is just a warning, to allow it to be evilly
5598 -- turned off. Otherwise it is a real error.
5600 -- In a generic context, simplify the warning because it makes no
5601 -- sense to discuss pass-by-reference or copy.
5603 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
5604 if Inside_A_Generic then
5606 ("return of limited object not permitted in Ada 2005 "
5607 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5609 elsif Is_Limited_View (R_Type) then
5611 ("return by reference not permitted in Ada 2005 "
5612 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5615 ("cannot copy object of a limited type in Ada 2005 "
5616 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5619 -- Ada 95 mode, compatibility warnings disabled
5622 return; -- skip continuation messages below
5625 if not Inside_A_Generic then
5627 ("\consider switching to return of access type", Expr);
5628 Explain_Limited_Type (R_Type, Expr);
5631 end Check_Limited_Return;
5633 ---------------------------
5634 -- Check_Mode_Conformant --
5635 ---------------------------
5637 procedure Check_Mode_Conformant
5638 (New_Id : Entity_Id;
5640 Err_Loc : Node_Id := Empty;
5641 Get_Inst : Boolean := False)
5644 pragma Warnings (Off, Result);
5647 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5648 end Check_Mode_Conformant;
5650 --------------------------------
5651 -- Check_Overriding_Indicator --
5652 --------------------------------
5654 procedure Check_Overriding_Indicator
5656 Overridden_Subp : Entity_Id;
5657 Is_Primitive : Boolean)
5663 -- No overriding indicator for literals
5665 if Ekind (Subp) = E_Enumeration_Literal then
5668 elsif Ekind (Subp) = E_Entry then
5669 Decl := Parent (Subp);
5671 -- No point in analyzing a malformed operator
5673 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5674 and then Error_Posted (Subp)
5679 Decl := Unit_Declaration_Node (Subp);
5682 if Nkind_In (Decl, N_Subprogram_Body,
5683 N_Subprogram_Body_Stub,
5684 N_Subprogram_Declaration,
5685 N_Abstract_Subprogram_Declaration,
5686 N_Subprogram_Renaming_Declaration)
5688 Spec := Specification (Decl);
5690 elsif Nkind (Decl) = N_Entry_Declaration then
5697 -- The overriding operation is type conformant with the overridden one,
5698 -- but the names of the formals are not required to match. If the names
5699 -- appear permuted in the overriding operation, this is a possible
5700 -- source of confusion that is worth diagnosing. Controlling formals
5701 -- often carry names that reflect the type, and it is not worthwhile
5702 -- requiring that their names match.
5704 if Present (Overridden_Subp)
5705 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5712 Form1 := First_Formal (Subp);
5713 Form2 := First_Formal (Overridden_Subp);
5715 -- If the overriding operation is a synchronized operation, skip
5716 -- the first parameter of the overridden operation, which is
5717 -- implicit in the new one. If the operation is declared in the
5718 -- body it is not primitive and all formals must match.
5720 if Is_Concurrent_Type (Scope (Subp))
5721 and then Is_Tagged_Type (Scope (Subp))
5722 and then not Has_Completion (Scope (Subp))
5724 Form2 := Next_Formal (Form2);
5727 if Present (Form1) then
5728 Form1 := Next_Formal (Form1);
5729 Form2 := Next_Formal (Form2);
5732 while Present (Form1) loop
5733 if not Is_Controlling_Formal (Form1)
5734 and then Present (Next_Formal (Form2))
5735 and then Chars (Form1) = Chars (Next_Formal (Form2))
5737 Error_Msg_Node_2 := Alias (Overridden_Subp);
5738 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5740 ("& does not match corresponding formal of&#",
5745 Next_Formal (Form1);
5746 Next_Formal (Form2);
5751 -- If there is an overridden subprogram, then check that there is no
5752 -- "not overriding" indicator, and mark the subprogram as overriding.
5753 -- This is not done if the overridden subprogram is marked as hidden,
5754 -- which can occur for the case of inherited controlled operations
5755 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5756 -- subprogram is not itself hidden. (Note: This condition could probably
5757 -- be simplified, leaving out the testing for the specific controlled
5758 -- cases, but it seems safer and clearer this way, and echoes similar
5759 -- special-case tests of this kind in other places.)
5761 if Present (Overridden_Subp)
5762 and then (not Is_Hidden (Overridden_Subp)
5764 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5767 and then Present (Alias (Overridden_Subp))
5768 and then not Is_Hidden (Alias (Overridden_Subp))))
5770 if Must_Not_Override (Spec) then
5771 Error_Msg_Sloc := Sloc (Overridden_Subp);
5773 if Ekind (Subp) = E_Entry then
5775 ("entry & overrides inherited operation #", Spec, Subp);
5778 ("subprogram & overrides inherited operation #", Spec, Subp);
5781 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5782 -- as an extension of Root_Controlled, and thus has a useless Adjust
5783 -- operation. This operation should not be inherited by other limited
5784 -- controlled types. An explicit Adjust for them is not overriding.
5786 elsif Must_Override (Spec)
5787 and then Chars (Overridden_Subp) = Name_Adjust
5788 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5789 and then Present (Alias (Overridden_Subp))
5791 Is_Predefined_File_Name
5792 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5794 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5796 elsif Is_Subprogram (Subp) then
5797 if Is_Init_Proc (Subp) then
5800 elsif No (Overridden_Operation (Subp)) then
5802 -- For entities generated by Derive_Subprograms the overridden
5803 -- operation is the inherited primitive (which is available
5804 -- through the attribute alias)
5806 if (Is_Dispatching_Operation (Subp)
5807 or else Is_Dispatching_Operation (Overridden_Subp))
5808 and then not Comes_From_Source (Overridden_Subp)
5809 and then Find_Dispatching_Type (Overridden_Subp) =
5810 Find_Dispatching_Type (Subp)
5811 and then Present (Alias (Overridden_Subp))
5812 and then Comes_From_Source (Alias (Overridden_Subp))
5814 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5815 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5818 Set_Overridden_Operation (Subp, Overridden_Subp);
5819 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5824 -- If primitive flag is set or this is a protected operation, then
5825 -- the operation is overriding at the point of its declaration, so
5826 -- warn if necessary. Otherwise it may have been declared before the
5827 -- operation it overrides and no check is required.
5830 and then not Must_Override (Spec)
5831 and then (Is_Primitive
5832 or else Ekind (Scope (Subp)) = E_Protected_Type)
5834 Style.Missing_Overriding (Decl, Subp);
5837 -- If Subp is an operator, it may override a predefined operation, if
5838 -- it is defined in the same scope as the type to which it applies.
5839 -- In that case Overridden_Subp is empty because of our implicit
5840 -- representation for predefined operators. We have to check whether the
5841 -- signature of Subp matches that of a predefined operator. Note that
5842 -- first argument provides the name of the operator, and the second
5843 -- argument the signature that may match that of a standard operation.
5844 -- If the indicator is overriding, then the operator must match a
5845 -- predefined signature, because we know already that there is no
5846 -- explicit overridden operation.
5848 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5849 if Must_Not_Override (Spec) then
5851 -- If this is not a primitive or a protected subprogram, then
5852 -- "not overriding" is illegal.
5855 and then Ekind (Scope (Subp)) /= E_Protected_Type
5857 Error_Msg_N ("overriding indicator only allowed "
5858 & "if subprogram is primitive", Subp);
5860 elsif Can_Override_Operator (Subp) then
5862 ("subprogram& overrides predefined operator ", Spec, Subp);
5865 elsif Must_Override (Spec) then
5866 if No (Overridden_Operation (Subp))
5867 and then not Can_Override_Operator (Subp)
5869 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5872 elsif not Error_Posted (Subp)
5873 and then Style_Check
5874 and then Can_Override_Operator (Subp)
5876 not Is_Predefined_File_Name
5877 (Unit_File_Name (Get_Source_Unit (Subp)))
5879 -- If style checks are enabled, indicate that the indicator is
5880 -- missing. However, at the point of declaration, the type of
5881 -- which this is a primitive operation may be private, in which
5882 -- case the indicator would be premature.
5884 if Has_Private_Declaration (Etype (Subp))
5885 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5889 Style.Missing_Overriding (Decl, Subp);
5893 elsif Must_Override (Spec) then
5894 if Ekind (Subp) = E_Entry then
5895 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5897 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5900 -- If the operation is marked "not overriding" and it's not primitive
5901 -- then an error is issued, unless this is an operation of a task or
5902 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5903 -- has been specified have already been checked above.
5905 elsif Must_Not_Override (Spec)
5906 and then not Is_Primitive
5907 and then Ekind (Subp) /= E_Entry
5908 and then Ekind (Scope (Subp)) /= E_Protected_Type
5911 ("overriding indicator only allowed if subprogram is primitive",
5915 end Check_Overriding_Indicator;
5921 -- Note: this procedure needs to know far too much about how the expander
5922 -- messes with exceptions. The use of the flag Exception_Junk and the
5923 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5924 -- works, but is not very clean. It would be better if the expansion
5925 -- routines would leave Original_Node working nicely, and we could use
5926 -- Original_Node here to ignore all the peculiar expander messing ???
5928 procedure Check_Returns
5932 Proc : Entity_Id := Empty)
5936 procedure Check_Statement_Sequence (L : List_Id);
5937 -- Internal recursive procedure to check a list of statements for proper
5938 -- termination by a return statement (or a transfer of control or a
5939 -- compound statement that is itself internally properly terminated).
5941 ------------------------------
5942 -- Check_Statement_Sequence --
5943 ------------------------------
5945 procedure Check_Statement_Sequence (L : List_Id) is
5950 function Assert_False return Boolean;
5951 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5952 -- rewritten as a null statement when assertions are off. The assert
5953 -- is not active, but it is still enough to kill the warning.
5959 function Assert_False return Boolean is
5960 Orig : constant Node_Id := Original_Node (Last_Stm);
5963 if Nkind (Orig) = N_Pragma
5964 and then Pragma_Name (Orig) = Name_Assert
5965 and then not Error_Posted (Orig)
5968 Arg : constant Node_Id :=
5969 First (Pragma_Argument_Associations (Orig));
5970 Exp : constant Node_Id := Expression (Arg);
5972 return Nkind (Exp) = N_Identifier
5973 and then Chars (Exp) = Name_False;
5983 Raise_Exception_Call : Boolean;
5984 -- Set True if statement sequence terminated by Raise_Exception call
5985 -- or a Reraise_Occurrence call.
5987 -- Start of processing for Check_Statement_Sequence
5990 Raise_Exception_Call := False;
5992 -- Get last real statement
5994 Last_Stm := Last (L);
5996 -- Deal with digging out exception handler statement sequences that
5997 -- have been transformed by the local raise to goto optimization.
5998 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5999 -- optimization has occurred, we are looking at something like:
6002 -- original stmts in block
6006 -- goto L1; | omitted if No_Exception_Propagation
6011 -- goto L3; -- skip handler when exception not raised
6013 -- <<L1>> -- target label for local exception
6027 -- and what we have to do is to dig out the estmts1 and estmts2
6028 -- sequences (which were the original sequences of statements in
6029 -- the exception handlers) and check them.
6031 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6036 exit when Nkind (Stm) /= N_Block_Statement;
6037 exit when not Exception_Junk (Stm);
6040 exit when Nkind (Stm) /= N_Label;
6041 exit when not Exception_Junk (Stm);
6042 Check_Statement_Sequence
6043 (Statements (Handled_Statement_Sequence (Next (Stm))));
6048 exit when Nkind (Stm) /= N_Goto_Statement;
6049 exit when not Exception_Junk (Stm);
6053 -- Don't count pragmas
6055 while Nkind (Last_Stm) = N_Pragma
6057 -- Don't count call to SS_Release (can happen after Raise_Exception)
6060 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6062 Nkind (Name (Last_Stm)) = N_Identifier
6064 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6066 -- Don't count exception junk
6069 (Nkind_In (Last_Stm, N_Goto_Statement,
6071 N_Object_Declaration)
6072 and then Exception_Junk (Last_Stm))
6073 or else Nkind (Last_Stm) in N_Push_xxx_Label
6074 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6076 -- Inserted code, such as finalization calls, is irrelevant: we only
6077 -- need to check original source.
6079 or else Is_Rewrite_Insertion (Last_Stm)
6084 -- Here we have the "real" last statement
6086 Kind := Nkind (Last_Stm);
6088 -- Transfer of control, OK. Note that in the No_Return procedure
6089 -- case, we already diagnosed any explicit return statements, so
6090 -- we can treat them as OK in this context.
6092 if Is_Transfer (Last_Stm) then
6095 -- Check cases of explicit non-indirect procedure calls
6097 elsif Kind = N_Procedure_Call_Statement
6098 and then Is_Entity_Name (Name (Last_Stm))
6100 -- Check call to Raise_Exception procedure which is treated
6101 -- specially, as is a call to Reraise_Occurrence.
6103 -- We suppress the warning in these cases since it is likely that
6104 -- the programmer really does not expect to deal with the case
6105 -- of Null_Occurrence, and thus would find a warning about a
6106 -- missing return curious, and raising Program_Error does not
6107 -- seem such a bad behavior if this does occur.
6109 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6110 -- behavior will be to raise Constraint_Error (see AI-329).
6112 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6114 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6116 Raise_Exception_Call := True;
6118 -- For Raise_Exception call, test first argument, if it is
6119 -- an attribute reference for a 'Identity call, then we know
6120 -- that the call cannot possibly return.
6123 Arg : constant Node_Id :=
6124 Original_Node (First_Actual (Last_Stm));
6126 if Nkind (Arg) = N_Attribute_Reference
6127 and then Attribute_Name (Arg) = Name_Identity
6134 -- If statement, need to look inside if there is an else and check
6135 -- each constituent statement sequence for proper termination.
6137 elsif Kind = N_If_Statement
6138 and then Present (Else_Statements (Last_Stm))
6140 Check_Statement_Sequence (Then_Statements (Last_Stm));
6141 Check_Statement_Sequence (Else_Statements (Last_Stm));
6143 if Present (Elsif_Parts (Last_Stm)) then
6145 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6148 while Present (Elsif_Part) loop
6149 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6157 -- Case statement, check each case for proper termination
6159 elsif Kind = N_Case_Statement then
6163 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6164 while Present (Case_Alt) loop
6165 Check_Statement_Sequence (Statements (Case_Alt));
6166 Next_Non_Pragma (Case_Alt);
6172 -- Block statement, check its handled sequence of statements
6174 elsif Kind = N_Block_Statement then
6180 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6189 -- Loop statement. If there is an iteration scheme, we can definitely
6190 -- fall out of the loop. Similarly if there is an exit statement, we
6191 -- can fall out. In either case we need a following return.
6193 elsif Kind = N_Loop_Statement then
6194 if Present (Iteration_Scheme (Last_Stm))
6195 or else Has_Exit (Entity (Identifier (Last_Stm)))
6199 -- A loop with no exit statement or iteration scheme is either
6200 -- an infinite loop, or it has some other exit (raise/return).
6201 -- In either case, no warning is required.
6207 -- Timed entry call, check entry call and delay alternatives
6209 -- Note: in expanded code, the timed entry call has been converted
6210 -- to a set of expanded statements on which the check will work
6211 -- correctly in any case.
6213 elsif Kind = N_Timed_Entry_Call then
6215 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6216 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6219 -- If statement sequence of entry call alternative is missing,
6220 -- then we can definitely fall through, and we post the error
6221 -- message on the entry call alternative itself.
6223 if No (Statements (ECA)) then
6226 -- If statement sequence of delay alternative is missing, then
6227 -- we can definitely fall through, and we post the error
6228 -- message on the delay alternative itself.
6230 -- Note: if both ECA and DCA are missing the return, then we
6231 -- post only one message, should be enough to fix the bugs.
6232 -- If not we will get a message next time on the DCA when the
6235 elsif No (Statements (DCA)) then
6238 -- Else check both statement sequences
6241 Check_Statement_Sequence (Statements (ECA));
6242 Check_Statement_Sequence (Statements (DCA));
6247 -- Conditional entry call, check entry call and else part
6249 -- Note: in expanded code, the conditional entry call has been
6250 -- converted to a set of expanded statements on which the check
6251 -- will work correctly in any case.
6253 elsif Kind = N_Conditional_Entry_Call then
6255 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6258 -- If statement sequence of entry call alternative is missing,
6259 -- then we can definitely fall through, and we post the error
6260 -- message on the entry call alternative itself.
6262 if No (Statements (ECA)) then
6265 -- Else check statement sequence and else part
6268 Check_Statement_Sequence (Statements (ECA));
6269 Check_Statement_Sequence (Else_Statements (Last_Stm));
6275 -- If we fall through, issue appropriate message
6279 -- Kill warning if last statement is a raise exception call,
6280 -- or a pragma Assert (False). Note that with assertions enabled,
6281 -- such a pragma has been converted into a raise exception call
6282 -- already, so the Assert_False is for the assertions off case.
6284 if not Raise_Exception_Call and then not Assert_False then
6286 -- In GNATprove mode, it is an error to have a missing return
6288 Error_Msg_Warn := SPARK_Mode /= On;
6290 -- Issue error message or warning
6293 ("RETURN statement missing following this statement<<!",
6296 ("\Program_Error ]<<!", Last_Stm);
6299 -- Note: we set Err even though we have not issued a warning
6300 -- because we still have a case of a missing return. This is
6301 -- an extremely marginal case, probably will never be noticed
6302 -- but we might as well get it right.
6306 -- Otherwise we have the case of a procedure marked No_Return
6309 if not Raise_Exception_Call then
6310 if GNATprove_Mode then
6312 ("implied return after this statement "
6313 & "would have raised Program_Error", Last_Stm);
6316 ("implied return after this statement "
6317 & "will raise Program_Error??", Last_Stm);
6320 Error_Msg_Warn := SPARK_Mode /= On;
6322 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6326 RE : constant Node_Id :=
6327 Make_Raise_Program_Error (Sloc (Last_Stm),
6328 Reason => PE_Implicit_Return);
6330 Insert_After (Last_Stm, RE);
6334 end Check_Statement_Sequence;
6336 -- Start of processing for Check_Returns
6340 Check_Statement_Sequence (Statements (HSS));
6342 if Present (Exception_Handlers (HSS)) then
6343 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6344 while Present (Handler) loop
6345 Check_Statement_Sequence (Statements (Handler));
6346 Next_Non_Pragma (Handler);
6351 ----------------------------
6352 -- Check_Subprogram_Order --
6353 ----------------------------
6355 procedure Check_Subprogram_Order (N : Node_Id) is
6357 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6358 -- This is used to check if S1 > S2 in the sense required by this test,
6359 -- for example nameab < namec, but name2 < name10.
6361 -----------------------------
6362 -- Subprogram_Name_Greater --
6363 -----------------------------
6365 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6370 -- Deal with special case where names are identical except for a
6371 -- numerical suffix. These are handled specially, taking the numeric
6372 -- ordering from the suffix into account.
6375 while S1 (L1) in '0' .. '9' loop
6380 while S2 (L2) in '0' .. '9' loop
6384 -- If non-numeric parts non-equal, do straight compare
6386 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6389 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6390 -- that a missing suffix is treated as numeric zero in this test.
6394 while L1 < S1'Last loop
6396 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6400 while L2 < S2'Last loop
6402 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6407 end Subprogram_Name_Greater;
6409 -- Start of processing for Check_Subprogram_Order
6412 -- Check body in alpha order if this is option
6415 and then Style_Check_Order_Subprograms
6416 and then Nkind (N) = N_Subprogram_Body
6417 and then Comes_From_Source (N)
6418 and then In_Extended_Main_Source_Unit (N)
6422 renames Scope_Stack.Table
6423 (Scope_Stack.Last).Last_Subprogram_Name;
6425 Body_Id : constant Entity_Id :=
6426 Defining_Entity (Specification (N));
6429 Get_Decoded_Name_String (Chars (Body_Id));
6432 if Subprogram_Name_Greater
6433 (LSN.all, Name_Buffer (1 .. Name_Len))
6435 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6441 LSN := new String'(Name_Buffer (1 .. Name_Len));
6444 end Check_Subprogram_Order;
6446 ------------------------------
6447 -- Check_Subtype_Conformant --
6448 ------------------------------
6450 procedure Check_Subtype_Conformant
6451 (New_Id : Entity_Id;
6453 Err_Loc : Node_Id := Empty;
6454 Skip_Controlling_Formals : Boolean := False;
6455 Get_Inst : Boolean := False)
6458 pragma Warnings (Off, Result);
6461 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6462 Skip_Controlling_Formals => Skip_Controlling_Formals,
6463 Get_Inst => Get_Inst);
6464 end Check_Subtype_Conformant;
6466 -----------------------------------
6467 -- Check_Synchronized_Overriding --
6468 -----------------------------------
6470 procedure Check_Synchronized_Overriding
6471 (Def_Id : Entity_Id;
6472 Overridden_Subp : out Entity_Id)
6474 Ifaces_List : Elist_Id;
6478 function Matches_Prefixed_View_Profile
6479 (Prim_Params : List_Id;
6480 Iface_Params : List_Id) return Boolean;
6481 -- Determine whether a subprogram's parameter profile Prim_Params
6482 -- matches that of a potentially overridden interface subprogram
6483 -- Iface_Params. Also determine if the type of first parameter of
6484 -- Iface_Params is an implemented interface.
6486 -----------------------------------
6487 -- Matches_Prefixed_View_Profile --
6488 -----------------------------------
6490 function Matches_Prefixed_View_Profile
6491 (Prim_Params : List_Id;
6492 Iface_Params : List_Id) return Boolean
6494 Iface_Id : Entity_Id;
6495 Iface_Param : Node_Id;
6496 Iface_Typ : Entity_Id;
6497 Prim_Id : Entity_Id;
6498 Prim_Param : Node_Id;
6499 Prim_Typ : Entity_Id;
6501 function Is_Implemented
6502 (Ifaces_List : Elist_Id;
6503 Iface : Entity_Id) return Boolean;
6504 -- Determine if Iface is implemented by the current task or
6507 --------------------
6508 -- Is_Implemented --
6509 --------------------
6511 function Is_Implemented
6512 (Ifaces_List : Elist_Id;
6513 Iface : Entity_Id) return Boolean
6515 Iface_Elmt : Elmt_Id;
6518 Iface_Elmt := First_Elmt (Ifaces_List);
6519 while Present (Iface_Elmt) loop
6520 if Node (Iface_Elmt) = Iface then
6524 Next_Elmt (Iface_Elmt);
6530 -- Start of processing for Matches_Prefixed_View_Profile
6533 Iface_Param := First (Iface_Params);
6534 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6536 if Is_Access_Type (Iface_Typ) then
6537 Iface_Typ := Designated_Type (Iface_Typ);
6540 Prim_Param := First (Prim_Params);
6542 -- The first parameter of the potentially overridden subprogram
6543 -- must be an interface implemented by Prim.
6545 if not Is_Interface (Iface_Typ)
6546 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6551 -- The checks on the object parameters are done, move onto the
6552 -- rest of the parameters.
6554 if not In_Scope then
6555 Prim_Param := Next (Prim_Param);
6558 Iface_Param := Next (Iface_Param);
6559 while Present (Iface_Param) and then Present (Prim_Param) loop
6560 Iface_Id := Defining_Identifier (Iface_Param);
6561 Iface_Typ := Find_Parameter_Type (Iface_Param);
6563 Prim_Id := Defining_Identifier (Prim_Param);
6564 Prim_Typ := Find_Parameter_Type (Prim_Param);
6566 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6567 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6568 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6570 Iface_Typ := Designated_Type (Iface_Typ);
6571 Prim_Typ := Designated_Type (Prim_Typ);
6574 -- Case of multiple interface types inside a parameter profile
6576 -- (Obj_Param : in out Iface; ...; Param : Iface)
6578 -- If the interface type is implemented, then the matching type
6579 -- in the primitive should be the implementing record type.
6581 if Ekind (Iface_Typ) = E_Record_Type
6582 and then Is_Interface (Iface_Typ)
6583 and then Is_Implemented (Ifaces_List, Iface_Typ)
6585 if Prim_Typ /= Typ then
6589 -- The two parameters must be both mode and subtype conformant
6591 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6593 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6602 -- One of the two lists contains more parameters than the other
6604 if Present (Iface_Param) or else Present (Prim_Param) then
6609 end Matches_Prefixed_View_Profile;
6611 -- Start of processing for Check_Synchronized_Overriding
6614 Overridden_Subp := Empty;
6616 -- Def_Id must be an entry or a subprogram. We should skip predefined
6617 -- primitives internally generated by the frontend; however at this
6618 -- stage predefined primitives are still not fully decorated. As a
6619 -- minor optimization we skip here internally generated subprograms.
6621 if (Ekind (Def_Id) /= E_Entry
6622 and then Ekind (Def_Id) /= E_Function
6623 and then Ekind (Def_Id) /= E_Procedure)
6624 or else not Comes_From_Source (Def_Id)
6629 -- Search for the concurrent declaration since it contains the list
6630 -- of all implemented interfaces. In this case, the subprogram is
6631 -- declared within the scope of a protected or a task type.
6633 if Present (Scope (Def_Id))
6634 and then Is_Concurrent_Type (Scope (Def_Id))
6635 and then not Is_Generic_Actual_Type (Scope (Def_Id))
6637 Typ := Scope (Def_Id);
6640 -- The enclosing scope is not a synchronized type and the subprogram
6643 elsif No (First_Formal (Def_Id)) then
6646 -- The subprogram has formals and hence it may be a primitive of a
6650 Typ := Etype (First_Formal (Def_Id));
6652 if Is_Access_Type (Typ) then
6653 Typ := Directly_Designated_Type (Typ);
6656 if Is_Concurrent_Type (Typ)
6657 and then not Is_Generic_Actual_Type (Typ)
6661 -- This case occurs when the concurrent type is declared within
6662 -- a generic unit. As a result the corresponding record has been
6663 -- built and used as the type of the first formal, we just have
6664 -- to retrieve the corresponding concurrent type.
6666 elsif Is_Concurrent_Record_Type (Typ)
6667 and then not Is_Class_Wide_Type (Typ)
6668 and then Present (Corresponding_Concurrent_Type (Typ))
6670 Typ := Corresponding_Concurrent_Type (Typ);
6678 -- There is no overriding to check if is an inherited operation in a
6679 -- type derivation on for a generic actual.
6681 Collect_Interfaces (Typ, Ifaces_List);
6683 if Is_Empty_Elmt_List (Ifaces_List) then
6687 -- Determine whether entry or subprogram Def_Id overrides a primitive
6688 -- operation that belongs to one of the interfaces in Ifaces_List.
6691 Candidate : Entity_Id := Empty;
6692 Hom : Entity_Id := Empty;
6693 Subp : Entity_Id := Empty;
6696 -- Traverse the homonym chain, looking for a potentially
6697 -- overridden subprogram that belongs to an implemented
6700 Hom := Current_Entity_In_Scope (Def_Id);
6701 while Present (Hom) loop
6705 or else not Is_Overloadable (Subp)
6706 or else not Is_Primitive (Subp)
6707 or else not Is_Dispatching_Operation (Subp)
6708 or else not Present (Find_Dispatching_Type (Subp))
6709 or else not Is_Interface (Find_Dispatching_Type (Subp))
6713 -- Entries and procedures can override abstract or null
6714 -- interface procedures.
6716 elsif (Ekind (Def_Id) = E_Procedure
6717 or else Ekind (Def_Id) = E_Entry)
6718 and then Ekind (Subp) = E_Procedure
6719 and then Matches_Prefixed_View_Profile
6720 (Parameter_Specifications (Parent (Def_Id)),
6721 Parameter_Specifications (Parent (Subp)))
6725 -- For an overridden subprogram Subp, check whether the mode
6726 -- of its first parameter is correct depending on the kind
6727 -- of synchronized type.
6730 Formal : constant Node_Id := First_Formal (Candidate);
6733 -- In order for an entry or a protected procedure to
6734 -- override, the first parameter of the overridden
6735 -- routine must be of mode "out", "in out" or
6736 -- access-to-variable.
6738 if Ekind_In (Candidate, E_Entry, E_Procedure)
6739 and then Is_Protected_Type (Typ)
6740 and then Ekind (Formal) /= E_In_Out_Parameter
6741 and then Ekind (Formal) /= E_Out_Parameter
6742 and then Nkind (Parameter_Type (Parent (Formal))) /=
6747 -- All other cases are OK since a task entry or routine
6748 -- does not have a restriction on the mode of the first
6749 -- parameter of the overridden interface routine.
6752 Overridden_Subp := Candidate;
6757 -- Functions can override abstract interface functions
6759 elsif Ekind (Def_Id) = E_Function
6760 and then Ekind (Subp) = E_Function
6761 and then Matches_Prefixed_View_Profile
6762 (Parameter_Specifications (Parent (Def_Id)),
6763 Parameter_Specifications (Parent (Subp)))
6764 and then Etype (Result_Definition (Parent (Def_Id))) =
6765 Etype (Result_Definition (Parent (Subp)))
6769 -- If an inherited subprogram is implemented by a protected
6770 -- function, then the first parameter of the inherited
6771 -- subprogram shall be of mode in, but not an
6772 -- access-to-variable parameter (RM 9.4(11/9)
6774 if Present (First_Formal (Subp))
6775 and then Ekind (First_Formal (Subp)) = E_In_Parameter
6777 (not Is_Access_Type (Etype (First_Formal (Subp)))
6779 Is_Access_Constant (Etype (First_Formal (Subp))))
6781 Overridden_Subp := Subp;
6786 Hom := Homonym (Hom);
6789 -- After examining all candidates for overriding, we are left with
6790 -- the best match which is a mode incompatible interface routine.
6792 if In_Scope and then Present (Candidate) then
6793 Error_Msg_PT (Def_Id, Candidate);
6796 Overridden_Subp := Candidate;
6799 end Check_Synchronized_Overriding;
6801 ---------------------------
6802 -- Check_Type_Conformant --
6803 ---------------------------
6805 procedure Check_Type_Conformant
6806 (New_Id : Entity_Id;
6808 Err_Loc : Node_Id := Empty)
6811 pragma Warnings (Off, Result);
6814 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6815 end Check_Type_Conformant;
6817 ---------------------------
6818 -- Can_Override_Operator --
6819 ---------------------------
6821 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6825 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6829 Typ := Base_Type (Etype (First_Formal (Subp)));
6831 -- Check explicitly that the operation is a primitive of the type
6833 return Operator_Matches_Spec (Subp, Subp)
6834 and then not Is_Generic_Type (Typ)
6835 and then Scope (Subp) = Scope (Typ)
6836 and then not Is_Class_Wide_Type (Typ);
6838 end Can_Override_Operator;
6840 ----------------------
6841 -- Conforming_Types --
6842 ----------------------
6844 function Conforming_Types
6847 Ctype : Conformance_Type;
6848 Get_Inst : Boolean := False) return Boolean
6850 function Base_Types_Match
6852 Typ_2 : Entity_Id) return Boolean;
6853 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
6854 -- in different scopes (e.g. parent and child instances), then verify
6855 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
6856 -- the same subtype chain. The whole purpose of this procedure is to
6857 -- prevent spurious ambiguities in an instantiation that may arise if
6858 -- two distinct generic types are instantiated with the same actual.
6860 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
6861 -- An access parameter can designate an incomplete type. If the
6862 -- incomplete type is the limited view of a type from a limited_
6863 -- with_clause, check whether the non-limited view is available.
6864 -- If it is a (non-limited) incomplete type, get the full view.
6866 function Matches_Limited_With_View
6868 Typ_2 : Entity_Id) return Boolean;
6869 -- Returns True if and only if either Typ_1 denotes a limited view of
6870 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
6871 -- the limited with view of a type is used in a subprogram declaration
6872 -- and the subprogram body is in the scope of a regular with clause for
6873 -- the same unit. In such a case, the two type entities are considered
6874 -- identical for purposes of conformance checking.
6876 ----------------------
6877 -- Base_Types_Match --
6878 ----------------------
6880 function Base_Types_Match
6882 Typ_2 : Entity_Id) return Boolean
6884 Base_1 : constant Entity_Id := Base_Type (Typ_1);
6885 Base_2 : constant Entity_Id := Base_Type (Typ_2);
6888 if Typ_1 = Typ_2 then
6891 elsif Base_1 = Base_2 then
6893 -- The following is too permissive. A more precise test should
6894 -- check that the generic actual is an ancestor subtype of the
6897 -- See code in Find_Corresponding_Spec that applies an additional
6898 -- filter to handle accidental amiguities in instances.
6901 not Is_Generic_Actual_Type (Typ_1)
6902 or else not Is_Generic_Actual_Type (Typ_2)
6903 or else Scope (Typ_1) /= Scope (Typ_2);
6905 -- If Typ_2 is a generic actual type it is declared as the subtype of
6906 -- the actual. If that actual is itself a subtype we need to use its
6907 -- own base type to check for compatibility.
6909 elsif Ekind (Base_2) = Ekind (Typ_2)
6910 and then Base_1 = Base_Type (Base_2)
6914 elsif Ekind (Base_1) = Ekind (Typ_1)
6915 and then Base_2 = Base_Type (Base_1)
6922 end Base_Types_Match;
6924 --------------------------
6925 -- Find_Designated_Type --
6926 --------------------------
6928 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
6932 Desig := Directly_Designated_Type (Typ);
6934 if Ekind (Desig) = E_Incomplete_Type then
6936 -- If regular incomplete type, get full view if available
6938 if Present (Full_View (Desig)) then
6939 Desig := Full_View (Desig);
6941 -- If limited view of a type, get non-limited view if available,
6942 -- and check again for a regular incomplete type.
6944 elsif Present (Non_Limited_View (Desig)) then
6945 Desig := Get_Full_View (Non_Limited_View (Desig));
6950 end Find_Designated_Type;
6952 -------------------------------
6953 -- Matches_Limited_With_View --
6954 -------------------------------
6956 function Matches_Limited_With_View
6958 Typ_2 : Entity_Id) return Boolean
6960 function Is_Matching_Limited_View
6962 View : Entity_Id) return Boolean;
6963 -- Determine whether non-limited view View denotes type Typ in some
6964 -- conformant fashion.
6966 ------------------------------
6967 -- Is_Matching_Limited_View --
6968 ------------------------------
6970 function Is_Matching_Limited_View
6972 View : Entity_Id) return Boolean
6974 Root_Typ : Entity_Id;
6975 Root_View : Entity_Id;
6978 -- The non-limited view directly denotes the type
6983 -- The type is a subtype of the non-limited view
6985 elsif Is_Subtype_Of (Typ, View) then
6988 -- Both the non-limited view and the type denote class-wide types
6990 elsif Is_Class_Wide_Type (Typ)
6991 and then Is_Class_Wide_Type (View)
6993 Root_Typ := Root_Type (Typ);
6994 Root_View := Root_Type (View);
6996 if Root_Typ = Root_View then
6999 -- An incomplete tagged type and its full view may receive two
7000 -- distinct class-wide types when the related package has not
7001 -- been analyzed yet.
7004 -- type T is tagged; -- CW_1
7005 -- type T is tagged null record; -- CW_2
7008 -- This is because the package lacks any semantic information
7009 -- that may eventually link both views of T. As a consequence,
7010 -- a client of the limited view of Pack will see CW_2 while a
7011 -- client of the non-limited view of Pack will see CW_1.
7013 elsif Is_Incomplete_Type (Root_Typ)
7014 and then Present (Full_View (Root_Typ))
7015 and then Full_View (Root_Typ) = Root_View
7019 elsif Is_Incomplete_Type (Root_View)
7020 and then Present (Full_View (Root_View))
7021 and then Full_View (Root_View) = Root_Typ
7028 end Is_Matching_Limited_View;
7030 -- Start of processing for Matches_Limited_With_View
7033 -- In some cases a type imported through a limited_with clause, and
7034 -- its non-limited view are both visible, for example in an anonymous
7035 -- access-to-class-wide type in a formal, or when building the body
7036 -- for a subprogram renaming after the subprogram has been frozen.
7037 -- In these cases both entities designate the same type. In addition,
7038 -- if one of them is an actual in an instance, it may be a subtype of
7039 -- the non-limited view of the other.
7041 if From_Limited_With (Typ_1)
7042 and then From_Limited_With (Typ_2)
7043 and then Available_View (Typ_1) = Available_View (Typ_2)
7047 elsif From_Limited_With (Typ_1) then
7048 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7050 elsif From_Limited_With (Typ_2) then
7051 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7056 end Matches_Limited_With_View;
7060 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7062 Type_1 : Entity_Id := T1;
7063 Type_2 : Entity_Id := T2;
7065 -- Start of processing for Conforming_Types
7068 -- The context is an instance association for a formal access-to-
7069 -- subprogram type; the formal parameter types require mapping because
7070 -- they may denote other formal parameters of the generic unit.
7073 Type_1 := Get_Instance_Of (T1);
7074 Type_2 := Get_Instance_Of (T2);
7077 -- If one of the types is a view of the other introduced by a limited
7078 -- with clause, treat these as conforming for all purposes.
7080 if Matches_Limited_With_View (T1, T2) then
7083 elsif Base_Types_Match (Type_1, Type_2) then
7084 return Ctype <= Mode_Conformant
7085 or else Subtypes_Statically_Match (Type_1, Type_2);
7087 elsif Is_Incomplete_Or_Private_Type (Type_1)
7088 and then Present (Full_View (Type_1))
7089 and then Base_Types_Match (Full_View (Type_1), Type_2)
7091 return Ctype <= Mode_Conformant
7092 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7094 elsif Ekind (Type_2) = E_Incomplete_Type
7095 and then Present (Full_View (Type_2))
7096 and then Base_Types_Match (Type_1, Full_View (Type_2))
7098 return Ctype <= Mode_Conformant
7099 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7101 elsif Is_Private_Type (Type_2)
7102 and then In_Instance
7103 and then Present (Full_View (Type_2))
7104 and then Base_Types_Match (Type_1, Full_View (Type_2))
7106 return Ctype <= Mode_Conformant
7107 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7109 -- In Ada 2012, incomplete types (including limited views) can appear
7110 -- as actuals in instantiations.
7112 elsif Is_Incomplete_Type (Type_1)
7113 and then Is_Incomplete_Type (Type_2)
7114 and then (Used_As_Generic_Actual (Type_1)
7115 or else Used_As_Generic_Actual (Type_2))
7120 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7121 -- treated recursively because they carry a signature. As far as
7122 -- conformance is concerned, convention plays no role, and either
7123 -- or both could be access to protected subprograms.
7125 Are_Anonymous_Access_To_Subprogram_Types :=
7126 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7127 E_Anonymous_Access_Protected_Subprogram_Type)
7129 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7130 E_Anonymous_Access_Protected_Subprogram_Type);
7132 -- Test anonymous access type case. For this case, static subtype
7133 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7134 -- the base types because we may have built internal subtype entities
7135 -- to handle null-excluding types (see Process_Formals).
7137 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7139 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7141 -- Ada 2005 (AI-254)
7143 or else Are_Anonymous_Access_To_Subprogram_Types
7146 Desig_1 : Entity_Id;
7147 Desig_2 : Entity_Id;
7150 -- In Ada 2005, access constant indicators must match for
7151 -- subtype conformance.
7153 if Ada_Version >= Ada_2005
7154 and then Ctype >= Subtype_Conformant
7156 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7161 Desig_1 := Find_Designated_Type (Type_1);
7162 Desig_2 := Find_Designated_Type (Type_2);
7164 -- If the context is an instance association for a formal
7165 -- access-to-subprogram type; formal access parameter designated
7166 -- types require mapping because they may denote other formal
7167 -- parameters of the generic unit.
7170 Desig_1 := Get_Instance_Of (Desig_1);
7171 Desig_2 := Get_Instance_Of (Desig_2);
7174 -- It is possible for a Class_Wide_Type to be introduced for an
7175 -- incomplete type, in which case there is a separate class_ wide
7176 -- type for the full view. The types conform if their Etypes
7177 -- conform, i.e. one may be the full view of the other. This can
7178 -- only happen in the context of an access parameter, other uses
7179 -- of an incomplete Class_Wide_Type are illegal.
7181 if Is_Class_Wide_Type (Desig_1)
7183 Is_Class_Wide_Type (Desig_2)
7187 (Etype (Base_Type (Desig_1)),
7188 Etype (Base_Type (Desig_2)), Ctype);
7190 elsif Are_Anonymous_Access_To_Subprogram_Types then
7191 if Ada_Version < Ada_2005 then
7192 return Ctype = Type_Conformant
7194 Subtypes_Statically_Match (Desig_1, Desig_2);
7196 -- We must check the conformance of the signatures themselves
7200 Conformant : Boolean;
7203 (Desig_1, Desig_2, Ctype, False, Conformant);
7208 -- A limited view of an actual matches the corresponding
7209 -- incomplete formal.
7211 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7212 and then From_Limited_With (Desig_2)
7213 and then Used_As_Generic_Actual (Etype (Desig_2))
7218 return Base_Type (Desig_1) = Base_Type (Desig_2)
7219 and then (Ctype = Type_Conformant
7221 Subtypes_Statically_Match (Desig_1, Desig_2));
7225 -- Otherwise definitely no match
7228 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7229 and then Is_Access_Type (Type_2))
7230 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7231 and then Is_Access_Type (Type_1)))
7234 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7236 May_Hide_Profile := True;
7241 end Conforming_Types;
7243 --------------------------
7244 -- Create_Extra_Formals --
7245 --------------------------
7247 procedure Create_Extra_Formals (E : Entity_Id) is
7249 First_Extra : Entity_Id := Empty;
7250 Last_Extra : Entity_Id;
7251 Formal_Type : Entity_Id;
7252 P_Formal : Entity_Id := Empty;
7254 function Add_Extra_Formal
7255 (Assoc_Entity : Entity_Id;
7258 Suffix : String) return Entity_Id;
7259 -- Add an extra formal to the current list of formals and extra formals.
7260 -- The extra formal is added to the end of the list of extra formals,
7261 -- and also returned as the result. These formals are always of mode IN.
7262 -- The new formal has the type Typ, is declared in Scope, and its name
7263 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7264 -- The following suffixes are currently used. They should not be changed
7265 -- without coordinating with CodePeer, which makes use of these to
7266 -- provide better messages.
7268 -- O denotes the Constrained bit.
7269 -- L denotes the accessibility level.
7270 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7271 -- the full list in exp_ch6.BIP_Formal_Kind.
7273 ----------------------
7274 -- Add_Extra_Formal --
7275 ----------------------
7277 function Add_Extra_Formal
7278 (Assoc_Entity : Entity_Id;
7281 Suffix : String) return Entity_Id
7283 EF : constant Entity_Id :=
7284 Make_Defining_Identifier (Sloc (Assoc_Entity),
7285 Chars => New_External_Name (Chars (Assoc_Entity),
7289 -- A little optimization. Never generate an extra formal for the
7290 -- _init operand of an initialization procedure, since it could
7293 if Chars (Formal) = Name_uInit then
7297 Set_Ekind (EF, E_In_Parameter);
7298 Set_Actual_Subtype (EF, Typ);
7299 Set_Etype (EF, Typ);
7300 Set_Scope (EF, Scope);
7301 Set_Mechanism (EF, Default_Mechanism);
7302 Set_Formal_Validity (EF);
7304 if No (First_Extra) then
7306 Set_Extra_Formals (Scope, First_Extra);
7309 if Present (Last_Extra) then
7310 Set_Extra_Formal (Last_Extra, EF);
7316 end Add_Extra_Formal;
7318 -- Start of processing for Create_Extra_Formals
7321 -- We never generate extra formals if expansion is not active because we
7322 -- don't need them unless we are generating code.
7324 if not Expander_Active then
7328 -- No need to generate extra formals in interface thunks whose target
7329 -- primitive has no extra formals.
7331 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7335 -- If this is a derived subprogram then the subtypes of the parent
7336 -- subprogram's formal parameters will be used to determine the need
7337 -- for extra formals.
7339 if Is_Overloadable (E) and then Present (Alias (E)) then
7340 P_Formal := First_Formal (Alias (E));
7343 Last_Extra := Empty;
7344 Formal := First_Formal (E);
7345 while Present (Formal) loop
7346 Last_Extra := Formal;
7347 Next_Formal (Formal);
7350 -- If Extra_Formals were already created, don't do it again. This
7351 -- situation may arise for subprogram types created as part of
7352 -- dispatching calls (see Expand_Dispatching_Call)
7354 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7358 -- If the subprogram is a predefined dispatching subprogram then don't
7359 -- generate any extra constrained or accessibility level formals. In
7360 -- general we suppress these for internal subprograms (by not calling
7361 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7362 -- generated stream attributes do get passed through because extra
7363 -- build-in-place formals are needed in some cases (limited 'Input).
7365 if Is_Predefined_Internal_Operation (E) then
7366 goto Test_For_Func_Result_Extras;
7369 Formal := First_Formal (E);
7370 while Present (Formal) loop
7372 -- Create extra formal for supporting the attribute 'Constrained.
7373 -- The case of a private type view without discriminants also
7374 -- requires the extra formal if the underlying type has defaulted
7377 if Ekind (Formal) /= E_In_Parameter then
7378 if Present (P_Formal) then
7379 Formal_Type := Etype (P_Formal);
7381 Formal_Type := Etype (Formal);
7384 -- Do not produce extra formals for Unchecked_Union parameters.
7385 -- Jump directly to the end of the loop.
7387 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7388 goto Skip_Extra_Formal_Generation;
7391 if not Has_Discriminants (Formal_Type)
7392 and then Ekind (Formal_Type) in Private_Kind
7393 and then Present (Underlying_Type (Formal_Type))
7395 Formal_Type := Underlying_Type (Formal_Type);
7398 -- Suppress the extra formal if formal's subtype is constrained or
7399 -- indefinite, or we're compiling for Ada 2012 and the underlying
7400 -- type is tagged and limited. In Ada 2012, a limited tagged type
7401 -- can have defaulted discriminants, but 'Constrained is required
7402 -- to return True, so the formal is never needed (see AI05-0214).
7403 -- Note that this ensures consistency of calling sequences for
7404 -- dispatching operations when some types in a class have defaults
7405 -- on discriminants and others do not (and requiring the extra
7406 -- formal would introduce distributed overhead).
7408 -- If the type does not have a completion yet, treat as prior to
7409 -- Ada 2012 for consistency.
7411 if Has_Discriminants (Formal_Type)
7412 and then not Is_Constrained (Formal_Type)
7413 and then Is_Definite_Subtype (Formal_Type)
7414 and then (Ada_Version < Ada_2012
7415 or else No (Underlying_Type (Formal_Type))
7417 (Is_Limited_Type (Formal_Type)
7420 (Underlying_Type (Formal_Type)))))
7422 Set_Extra_Constrained
7423 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7427 -- Create extra formal for supporting accessibility checking. This
7428 -- is done for both anonymous access formals and formals of named
7429 -- access types that are marked as controlling formals. The latter
7430 -- case can occur when Expand_Dispatching_Call creates a subprogram
7431 -- type and substitutes the types of access-to-class-wide actuals
7432 -- for the anonymous access-to-specific-type of controlling formals.
7433 -- Base_Type is applied because in cases where there is a null
7434 -- exclusion the formal may have an access subtype.
7436 -- This is suppressed if we specifically suppress accessibility
7437 -- checks at the package level for either the subprogram, or the
7438 -- package in which it resides. However, we do not suppress it
7439 -- simply if the scope has accessibility checks suppressed, since
7440 -- this could cause trouble when clients are compiled with a
7441 -- different suppression setting. The explicit checks at the
7442 -- package level are safe from this point of view.
7444 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7445 or else (Is_Controlling_Formal (Formal)
7446 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7448 (Explicit_Suppress (E, Accessibility_Check)
7450 Explicit_Suppress (Scope (E), Accessibility_Check))
7453 or else Present (Extra_Accessibility (P_Formal)))
7455 Set_Extra_Accessibility
7456 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7459 -- This label is required when skipping extra formal generation for
7460 -- Unchecked_Union parameters.
7462 <<Skip_Extra_Formal_Generation>>
7464 if Present (P_Formal) then
7465 Next_Formal (P_Formal);
7468 Next_Formal (Formal);
7471 <<Test_For_Func_Result_Extras>>
7473 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7474 -- function call is ... determined by the point of call ...".
7476 if Needs_Result_Accessibility_Level (E) then
7477 Set_Extra_Accessibility_Of_Result
7478 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7481 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7482 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7484 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7486 Result_Subt : constant Entity_Id := Etype (E);
7487 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7488 Formal_Typ : Entity_Id;
7490 Discard : Entity_Id;
7491 pragma Warnings (Off, Discard);
7494 -- In the case of functions with unconstrained result subtypes,
7495 -- add a 4-state formal indicating whether the return object is
7496 -- allocated by the caller (1), or should be allocated by the
7497 -- callee on the secondary stack (2), in the global heap (3), or
7498 -- in a user-defined storage pool (4). For the moment we just use
7499 -- Natural for the type of this formal. Note that this formal
7500 -- isn't usually needed in the case where the result subtype is
7501 -- constrained, but it is needed when the function has a tagged
7502 -- result, because generally such functions can be called in a
7503 -- dispatching context and such calls must be handled like calls
7504 -- to a class-wide function.
7506 if Needs_BIP_Alloc_Form (E) then
7509 (E, Standard_Natural,
7510 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7512 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7513 -- use a user-defined pool. This formal is not added on
7514 -- ZFP as those targets do not support pools.
7516 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
7519 (E, RTE (RE_Root_Storage_Pool_Ptr),
7520 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7524 -- In the case of functions whose result type needs finalization,
7525 -- add an extra formal which represents the finalization master.
7527 if Needs_BIP_Finalization_Master (E) then
7530 (E, RTE (RE_Finalization_Master_Ptr),
7531 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7534 -- When the result type contains tasks, add two extra formals: the
7535 -- master of the tasks to be created, and the caller's activation
7538 if Has_Task (Full_Subt) then
7541 (E, RTE (RE_Master_Id),
7542 E, BIP_Formal_Suffix (BIP_Task_Master));
7545 (E, RTE (RE_Activation_Chain_Access),
7546 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7549 -- All build-in-place functions get an extra formal that will be
7550 -- passed the address of the return object within the caller.
7553 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7555 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7556 Set_Etype (Formal_Typ, Formal_Typ);
7557 Set_Depends_On_Private
7558 (Formal_Typ, Has_Private_Component (Formal_Typ));
7559 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7560 Set_Is_Access_Constant (Formal_Typ, False);
7562 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7563 -- the designated type comes from the limited view (for back-end
7566 Set_From_Limited_With
7567 (Formal_Typ, From_Limited_With (Result_Subt));
7569 Layout_Type (Formal_Typ);
7573 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7576 end Create_Extra_Formals;
7578 -----------------------------
7579 -- Enter_Overloaded_Entity --
7580 -----------------------------
7582 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7583 function Matches_Predefined_Op return Boolean;
7584 -- This returns an approximation of whether S matches a predefined
7585 -- operator, based on the operator symbol, and the parameter and result
7586 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
7588 ---------------------------
7589 -- Matches_Predefined_Op --
7590 ---------------------------
7592 function Matches_Predefined_Op return Boolean is
7593 Formal_1 : constant Entity_Id := First_Formal (S);
7594 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
7595 Op : constant Name_Id := Chars (S);
7596 Result_Type : constant Entity_Id := Base_Type (Etype (S));
7597 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
7602 if Present (Formal_2) then
7604 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
7607 -- All but "&" and "**" have same-types parameters
7610 when Name_Op_Concat |
7615 if Type_1 /= Type_2 then
7620 -- Check parameter and result types
7627 Is_Boolean_Type (Result_Type)
7628 and then Result_Type = Type_1;
7633 Is_Integer_Type (Result_Type)
7634 and then Result_Type = Type_1;
7641 Is_Numeric_Type (Result_Type)
7642 and then Result_Type = Type_1;
7647 Is_Boolean_Type (Result_Type)
7648 and then not Is_Limited_Type (Type_1);
7655 Is_Boolean_Type (Result_Type)
7656 and then (Is_Array_Type (Type_1)
7657 or else Is_Scalar_Type (Type_1));
7659 when Name_Op_Concat =>
7660 return Is_Array_Type (Result_Type);
7662 when Name_Op_Expon =>
7664 (Is_Integer_Type (Result_Type)
7665 or else Is_Floating_Point_Type (Result_Type))
7666 and then Result_Type = Type_1
7667 and then Type_2 = Standard_Integer;
7670 raise Program_Error;
7682 Is_Numeric_Type (Result_Type)
7683 and then Result_Type = Type_1;
7687 Is_Boolean_Type (Result_Type)
7688 and then Result_Type = Type_1;
7691 raise Program_Error;
7694 end Matches_Predefined_Op;
7698 E : Entity_Id := Current_Entity_In_Scope (S);
7699 C_E : Entity_Id := Current_Entity (S);
7701 -- Start of processing for Enter_Overloaded_Entity
7705 Set_Has_Homonym (E);
7706 Set_Has_Homonym (S);
7709 Set_Is_Immediately_Visible (S);
7710 Set_Scope (S, Current_Scope);
7712 -- Chain new entity if front of homonym in current scope, so that
7713 -- homonyms are contiguous.
7715 if Present (E) and then E /= C_E then
7716 while Homonym (C_E) /= E loop
7717 C_E := Homonym (C_E);
7720 Set_Homonym (C_E, S);
7724 Set_Current_Entity (S);
7729 if Is_Inherited_Operation (S) then
7730 Append_Inherited_Subprogram (S);
7732 Append_Entity (S, Current_Scope);
7735 Set_Public_Status (S);
7737 if Debug_Flag_E then
7738 Write_Str ("New overloaded entity chain: ");
7739 Write_Name (Chars (S));
7742 while Present (E) loop
7743 Write_Str (" "); Write_Int (Int (E));
7750 -- Generate warning for hiding
7753 and then Comes_From_Source (S)
7754 and then In_Extended_Main_Source_Unit (S)
7761 -- Warn unless genuine overloading. Do not emit warning on
7762 -- hiding predefined operators in Standard (these are either an
7763 -- (artifact of our implicit declarations, or simple noise) but
7764 -- keep warning on a operator defined on a local subtype, because
7765 -- of the real danger that different operators may be applied in
7766 -- various parts of the program.
7768 -- Note that if E and S have the same scope, there is never any
7769 -- hiding. Either the two conflict, and the program is illegal,
7770 -- or S is overriding an implicit inherited subprogram.
7772 if Scope (E) /= Scope (S)
7773 and then (not Is_Overloadable (E)
7774 or else Subtype_Conformant (E, S))
7775 and then (Is_Immediately_Visible (E)
7776 or else Is_Potentially_Use_Visible (S))
7778 if Scope (E) = Standard_Standard then
7779 if Nkind (S) = N_Defining_Operator_Symbol
7780 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
7782 and then Matches_Predefined_Op
7785 ("declaration of & hides predefined operator?h?", S);
7788 -- E not immediately within Standard
7791 Error_Msg_Sloc := Sloc (E);
7792 Error_Msg_N ("declaration of & hides one #?h?", S);
7797 end Enter_Overloaded_Entity;
7799 -----------------------------
7800 -- Check_Untagged_Equality --
7801 -----------------------------
7803 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7804 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7805 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7809 -- This check applies only if we have a subprogram declaration with an
7810 -- untagged record type.
7812 if Nkind (Decl) /= N_Subprogram_Declaration
7813 or else not Is_Record_Type (Typ)
7814 or else Is_Tagged_Type (Typ)
7819 -- In Ada 2012 case, we will output errors or warnings depending on
7820 -- the setting of debug flag -gnatd.E.
7822 if Ada_Version >= Ada_2012 then
7823 Error_Msg_Warn := Debug_Flag_Dot_EE;
7825 -- In earlier versions of Ada, nothing to do unless we are warning on
7826 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7829 if not Warn_On_Ada_2012_Compatibility then
7834 -- Cases where the type has already been frozen
7836 if Is_Frozen (Typ) then
7838 -- If the type is not declared in a package, or if we are in the body
7839 -- of the package or in some other scope, the new operation is not
7840 -- primitive, and therefore legal, though suspicious. Should we
7841 -- generate a warning in this case ???
7843 if Ekind (Scope (Typ)) /= E_Package
7844 or else Scope (Typ) /= Current_Scope
7848 -- If the type is a generic actual (sub)type, the operation is not
7849 -- primitive either because the base type is declared elsewhere.
7851 elsif Is_Generic_Actual_Type (Typ) then
7854 -- Here we have a definite error of declaration after freezing
7857 if Ada_Version >= Ada_2012 then
7859 ("equality operator must be declared before type & is "
7860 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7862 -- In Ada 2012 mode with error turned to warning, output one
7863 -- more warning to warn that the equality operation may not
7864 -- compose. This is the consequence of ignoring the error.
7866 if Error_Msg_Warn then
7867 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7872 ("equality operator must be declared before type& is "
7873 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7876 -- If we are in the package body, we could just move the
7877 -- declaration to the package spec, so add a message saying that.
7879 if In_Package_Body (Scope (Typ)) then
7880 if Ada_Version >= Ada_2012 then
7882 ("\move declaration to package spec<<", Eq_Op);
7885 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7888 -- Otherwise try to find the freezing point
7891 Obj_Decl := Next (Parent (Typ));
7892 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7893 if Nkind (Obj_Decl) = N_Object_Declaration
7894 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7896 -- Freezing point, output warnings
7898 if Ada_Version >= Ada_2012 then
7900 ("type& is frozen by declaration??", Obj_Decl, Typ);
7902 ("\an equality operator cannot be declared after "
7907 ("type& is frozen by declaration (Ada 2012)?y?",
7910 ("\an equality operator cannot be declared after "
7911 & "this point (Ada 2012)?y?",
7923 -- Here if type is not frozen yet. It is illegal to have a primitive
7924 -- equality declared in the private part if the type is visible.
7926 elsif not In_Same_List (Parent (Typ), Decl)
7927 and then not Is_Limited_Type (Typ)
7929 -- Shouldn't we give an RM reference here???
7931 if Ada_Version >= Ada_2012 then
7933 ("equality operator appears too late<<", Eq_Op);
7936 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7939 -- No error detected
7944 end Check_Untagged_Equality;
7946 -----------------------------
7947 -- Find_Corresponding_Spec --
7948 -----------------------------
7950 function Find_Corresponding_Spec
7952 Post_Error : Boolean := True) return Entity_Id
7954 Spec : constant Node_Id := Specification (N);
7955 Designator : constant Entity_Id := Defining_Entity (Spec);
7959 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7960 -- Even if fully conformant, a body may depend on a generic actual when
7961 -- the spec does not, or vice versa, in which case they were distinct
7962 -- entities in the generic.
7964 -------------------------------
7965 -- Different_Generic_Profile --
7966 -------------------------------
7968 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7971 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7972 -- Check that the types of corresponding formals have the same
7973 -- generic actual if any. We have to account for subtypes of a
7974 -- generic formal, declared between a spec and a body, which may
7975 -- appear distinct in an instance but matched in the generic, and
7976 -- the subtype may be used either in the spec or the body of the
7977 -- subprogram being checked.
7979 -------------------------
7980 -- Same_Generic_Actual --
7981 -------------------------
7983 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7985 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7986 -- Predicate to check whether S1 is a subtype of S2 in the source
7989 -------------------------
7990 -- Is_Declared_Subtype --
7991 -------------------------
7993 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7995 return Comes_From_Source (Parent (S1))
7996 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7997 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7998 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7999 end Is_Declared_Subtype;
8001 -- Start of processing for Same_Generic_Actual
8004 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8005 or else Is_Declared_Subtype (T1, T2)
8006 or else Is_Declared_Subtype (T2, T1);
8007 end Same_Generic_Actual;
8009 -- Start of processing for Different_Generic_Profile
8012 if not In_Instance then
8015 elsif Ekind (E) = E_Function
8016 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8021 F1 := First_Formal (Designator);
8022 F2 := First_Formal (E);
8023 while Present (F1) loop
8024 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8033 end Different_Generic_Profile;
8035 -- Start of processing for Find_Corresponding_Spec
8038 E := Current_Entity (Designator);
8039 while Present (E) loop
8041 -- We are looking for a matching spec. It must have the same scope,
8042 -- and the same name, and either be type conformant, or be the case
8043 -- of a library procedure spec and its body (which belong to one
8044 -- another regardless of whether they are type conformant or not).
8046 if Scope (E) = Current_Scope then
8047 if Current_Scope = Standard_Standard
8048 or else (Ekind (E) = Ekind (Designator)
8049 and then Type_Conformant (E, Designator))
8051 -- Within an instantiation, we know that spec and body are
8052 -- subtype conformant, because they were subtype conformant in
8053 -- the generic. We choose the subtype-conformant entity here as
8054 -- well, to resolve spurious ambiguities in the instance that
8055 -- were not present in the generic (i.e. when two different
8056 -- types are given the same actual). If we are looking for a
8057 -- spec to match a body, full conformance is expected.
8061 -- Inherit the convention and "ghostness" of the matching
8062 -- spec to ensure proper full and subtype conformance.
8064 Set_Convention (Designator, Convention (E));
8066 if Is_Ghost_Entity (E) then
8067 Set_Is_Ghost_Entity (Designator);
8070 -- Skip past subprogram bodies and subprogram renamings that
8071 -- may appear to have a matching spec, but that aren't fully
8072 -- conformant with it. That can occur in cases where an
8073 -- actual type causes unrelated homographs in the instance.
8075 if Nkind_In (N, N_Subprogram_Body,
8076 N_Subprogram_Renaming_Declaration)
8077 and then Present (Homonym (E))
8078 and then not Fully_Conformant (Designator, E)
8082 elsif not Subtype_Conformant (Designator, E) then
8085 elsif Different_Generic_Profile (E) then
8090 -- Ada 2012 (AI05-0165): For internally generated bodies of
8091 -- null procedures locate the internally generated spec. We
8092 -- enforce mode conformance since a tagged type may inherit
8093 -- from interfaces several null primitives which differ only
8094 -- in the mode of the formals.
8096 if not (Comes_From_Source (E))
8097 and then Is_Null_Procedure (E)
8098 and then not Mode_Conformant (Designator, E)
8102 -- For null procedures coming from source that are completions,
8103 -- analysis of the generated body will establish the link.
8105 elsif Comes_From_Source (E)
8106 and then Nkind (Spec) = N_Procedure_Specification
8107 and then Null_Present (Spec)
8111 -- Expression functions can be completions, but cannot be
8112 -- completed by an explicit body.
8114 elsif Comes_From_Source (E)
8115 and then Comes_From_Source (N)
8116 and then Nkind (N) = N_Subprogram_Body
8117 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8118 N_Expression_Function
8120 Error_Msg_Sloc := Sloc (E);
8121 Error_Msg_N ("body conflicts with expression function#", N);
8124 elsif not Has_Completion (E) then
8125 if Nkind (N) /= N_Subprogram_Body_Stub then
8126 Set_Corresponding_Spec (N, E);
8129 Set_Has_Completion (E);
8132 elsif Nkind (Parent (N)) = N_Subunit then
8134 -- If this is the proper body of a subunit, the completion
8135 -- flag is set when analyzing the stub.
8139 -- If E is an internal function with a controlling result that
8140 -- was created for an operation inherited by a null extension,
8141 -- it may be overridden by a body without a previous spec (one
8142 -- more reason why these should be shunned). In that case we
8143 -- remove the generated body if present, because the current
8144 -- one is the explicit overriding.
8146 elsif Ekind (E) = E_Function
8147 and then Ada_Version >= Ada_2005
8148 and then not Comes_From_Source (E)
8149 and then Has_Controlling_Result (E)
8150 and then Is_Null_Extension (Etype (E))
8151 and then Comes_From_Source (Spec)
8153 Set_Has_Completion (E, False);
8156 and then Nkind (Parent (E)) = N_Function_Specification
8159 (Unit_Declaration_Node
8160 (Corresponding_Body (Unit_Declaration_Node (E))));
8164 -- If expansion is disabled, or if the wrapper function has
8165 -- not been generated yet, this a late body overriding an
8166 -- inherited operation, or it is an overriding by some other
8167 -- declaration before the controlling result is frozen. In
8168 -- either case this is a declaration of a new entity.
8174 -- If the body already exists, then this is an error unless
8175 -- the previous declaration is the implicit declaration of a
8176 -- derived subprogram. It is also legal for an instance to
8177 -- contain type conformant overloadable declarations (but the
8178 -- generic declaration may not), per 8.3(26/2).
8180 elsif No (Alias (E))
8181 and then not Is_Intrinsic_Subprogram (E)
8182 and then not In_Instance
8185 Error_Msg_Sloc := Sloc (E);
8187 if Is_Imported (E) then
8189 ("body not allowed for imported subprogram & declared#",
8192 Error_Msg_NE ("duplicate body for & declared#", N, E);
8196 -- Child units cannot be overloaded, so a conformance mismatch
8197 -- between body and a previous spec is an error.
8199 elsif Is_Child_Unit (E)
8201 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8203 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8208 ("body of child unit does not match previous declaration", N);
8216 -- On exit, we know that no previous declaration of subprogram exists
8219 end Find_Corresponding_Spec;
8221 ----------------------
8222 -- Fully_Conformant --
8223 ----------------------
8225 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8228 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8230 end Fully_Conformant;
8232 ----------------------------------
8233 -- Fully_Conformant_Expressions --
8234 ----------------------------------
8236 function Fully_Conformant_Expressions
8237 (Given_E1 : Node_Id;
8238 Given_E2 : Node_Id) return Boolean
8240 E1 : constant Node_Id := Original_Node (Given_E1);
8241 E2 : constant Node_Id := Original_Node (Given_E2);
8242 -- We always test conformance on original nodes, since it is possible
8243 -- for analysis and/or expansion to make things look as though they
8244 -- conform when they do not, e.g. by converting 1+2 into 3.
8246 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8247 renames Fully_Conformant_Expressions;
8249 function FCL (L1, L2 : List_Id) return Boolean;
8250 -- Compare elements of two lists for conformance. Elements have to be
8251 -- conformant, and actuals inserted as default parameters do not match
8252 -- explicit actuals with the same value.
8254 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8255 -- Compare an operator node with a function call
8261 function FCL (L1, L2 : List_Id) return Boolean is
8265 if L1 = No_List then
8271 if L2 = No_List then
8277 -- Compare two lists, skipping rewrite insertions (we want to compare
8278 -- the original trees, not the expanded versions).
8281 if Is_Rewrite_Insertion (N1) then
8283 elsif Is_Rewrite_Insertion (N2) then
8289 elsif not FCE (N1, N2) then
8302 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8303 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8308 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8313 Act := First (Actuals);
8315 if Nkind (Op_Node) in N_Binary_Op then
8316 if not FCE (Left_Opnd (Op_Node), Act) then
8323 return Present (Act)
8324 and then FCE (Right_Opnd (Op_Node), Act)
8325 and then No (Next (Act));
8329 -- Start of processing for Fully_Conformant_Expressions
8332 -- Non-conformant if paren count does not match. Note: if some idiot
8333 -- complains that we don't do this right for more than 3 levels of
8334 -- parentheses, they will be treated with the respect they deserve.
8336 if Paren_Count (E1) /= Paren_Count (E2) then
8339 -- If same entities are referenced, then they are conformant even if
8340 -- they have different forms (RM 8.3.1(19-20)).
8342 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8343 if Present (Entity (E1)) then
8344 return Entity (E1) = Entity (E2)
8345 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8346 and then Ekind (Entity (E1)) = E_Discriminant
8347 and then Ekind (Entity (E2)) = E_In_Parameter);
8349 elsif Nkind (E1) = N_Expanded_Name
8350 and then Nkind (E2) = N_Expanded_Name
8351 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8352 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8354 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8357 -- Identifiers in component associations don't always have
8358 -- entities, but their names must conform.
8360 return Nkind (E1) = N_Identifier
8361 and then Nkind (E2) = N_Identifier
8362 and then Chars (E1) = Chars (E2);
8365 elsif Nkind (E1) = N_Character_Literal
8366 and then Nkind (E2) = N_Expanded_Name
8368 return Nkind (Selector_Name (E2)) = N_Character_Literal
8369 and then Chars (E1) = Chars (Selector_Name (E2));
8371 elsif Nkind (E2) = N_Character_Literal
8372 and then Nkind (E1) = N_Expanded_Name
8374 return Nkind (Selector_Name (E1)) = N_Character_Literal
8375 and then Chars (E2) = Chars (Selector_Name (E1));
8377 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8378 return FCO (E1, E2);
8380 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8381 return FCO (E2, E1);
8383 -- Otherwise we must have the same syntactic entity
8385 elsif Nkind (E1) /= Nkind (E2) then
8388 -- At this point, we specialize by node type
8395 FCL (Expressions (E1), Expressions (E2))
8397 FCL (Component_Associations (E1),
8398 Component_Associations (E2));
8401 if Nkind (Expression (E1)) = N_Qualified_Expression
8403 Nkind (Expression (E2)) = N_Qualified_Expression
8405 return FCE (Expression (E1), Expression (E2));
8407 -- Check that the subtype marks and any constraints
8412 Indic1 : constant Node_Id := Expression (E1);
8413 Indic2 : constant Node_Id := Expression (E2);
8418 if Nkind (Indic1) /= N_Subtype_Indication then
8420 Nkind (Indic2) /= N_Subtype_Indication
8421 and then Entity (Indic1) = Entity (Indic2);
8423 elsif Nkind (Indic2) /= N_Subtype_Indication then
8425 Nkind (Indic1) /= N_Subtype_Indication
8426 and then Entity (Indic1) = Entity (Indic2);
8429 if Entity (Subtype_Mark (Indic1)) /=
8430 Entity (Subtype_Mark (Indic2))
8435 Elt1 := First (Constraints (Constraint (Indic1)));
8436 Elt2 := First (Constraints (Constraint (Indic2)));
8437 while Present (Elt1) and then Present (Elt2) loop
8438 if not FCE (Elt1, Elt2) then
8451 when N_Attribute_Reference =>
8453 Attribute_Name (E1) = Attribute_Name (E2)
8454 and then FCL (Expressions (E1), Expressions (E2));
8458 Entity (E1) = Entity (E2)
8459 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8460 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8462 when N_Short_Circuit | N_Membership_Test =>
8464 FCE (Left_Opnd (E1), Left_Opnd (E2))
8466 FCE (Right_Opnd (E1), Right_Opnd (E2));
8468 when N_Case_Expression =>
8474 if not FCE (Expression (E1), Expression (E2)) then
8478 Alt1 := First (Alternatives (E1));
8479 Alt2 := First (Alternatives (E2));
8481 if Present (Alt1) /= Present (Alt2) then
8483 elsif No (Alt1) then
8487 if not FCE (Expression (Alt1), Expression (Alt2))
8488 or else not FCL (Discrete_Choices (Alt1),
8489 Discrete_Choices (Alt2))
8500 when N_Character_Literal =>
8502 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8504 when N_Component_Association =>
8506 FCL (Choices (E1), Choices (E2))
8508 FCE (Expression (E1), Expression (E2));
8510 when N_Explicit_Dereference =>
8512 FCE (Prefix (E1), Prefix (E2));
8514 when N_Extension_Aggregate =>
8516 FCL (Expressions (E1), Expressions (E2))
8517 and then Null_Record_Present (E1) =
8518 Null_Record_Present (E2)
8519 and then FCL (Component_Associations (E1),
8520 Component_Associations (E2));
8522 when N_Function_Call =>
8524 FCE (Name (E1), Name (E2))
8526 FCL (Parameter_Associations (E1),
8527 Parameter_Associations (E2));
8529 when N_If_Expression =>
8531 FCL (Expressions (E1), Expressions (E2));
8533 when N_Indexed_Component =>
8535 FCE (Prefix (E1), Prefix (E2))
8537 FCL (Expressions (E1), Expressions (E2));
8539 when N_Integer_Literal =>
8540 return (Intval (E1) = Intval (E2));
8545 when N_Operator_Symbol =>
8547 Chars (E1) = Chars (E2);
8549 when N_Others_Choice =>
8552 when N_Parameter_Association =>
8554 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8555 and then FCE (Explicit_Actual_Parameter (E1),
8556 Explicit_Actual_Parameter (E2));
8558 when N_Qualified_Expression =>
8560 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8562 FCE (Expression (E1), Expression (E2));
8564 when N_Quantified_Expression =>
8565 if not FCE (Condition (E1), Condition (E2)) then
8569 if Present (Loop_Parameter_Specification (E1))
8570 and then Present (Loop_Parameter_Specification (E2))
8573 L1 : constant Node_Id :=
8574 Loop_Parameter_Specification (E1);
8575 L2 : constant Node_Id :=
8576 Loop_Parameter_Specification (E2);
8580 Reverse_Present (L1) = Reverse_Present (L2)
8582 FCE (Defining_Identifier (L1),
8583 Defining_Identifier (L2))
8585 FCE (Discrete_Subtype_Definition (L1),
8586 Discrete_Subtype_Definition (L2));
8589 elsif Present (Iterator_Specification (E1))
8590 and then Present (Iterator_Specification (E2))
8593 I1 : constant Node_Id := Iterator_Specification (E1);
8594 I2 : constant Node_Id := Iterator_Specification (E2);
8598 FCE (Defining_Identifier (I1),
8599 Defining_Identifier (I2))
8601 Of_Present (I1) = Of_Present (I2)
8603 Reverse_Present (I1) = Reverse_Present (I2)
8604 and then FCE (Name (I1), Name (I2))
8605 and then FCE (Subtype_Indication (I1),
8606 Subtype_Indication (I2));
8609 -- The quantified expressions used different specifications to
8610 -- walk their respective ranges.
8618 FCE (Low_Bound (E1), Low_Bound (E2))
8620 FCE (High_Bound (E1), High_Bound (E2));
8622 when N_Real_Literal =>
8623 return (Realval (E1) = Realval (E2));
8625 when N_Selected_Component =>
8627 FCE (Prefix (E1), Prefix (E2))
8629 FCE (Selector_Name (E1), Selector_Name (E2));
8633 FCE (Prefix (E1), Prefix (E2))
8635 FCE (Discrete_Range (E1), Discrete_Range (E2));
8637 when N_String_Literal =>
8639 S1 : constant String_Id := Strval (E1);
8640 S2 : constant String_Id := Strval (E2);
8641 L1 : constant Nat := String_Length (S1);
8642 L2 : constant Nat := String_Length (S2);
8649 for J in 1 .. L1 loop
8650 if Get_String_Char (S1, J) /=
8651 Get_String_Char (S2, J)
8661 when N_Type_Conversion =>
8663 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8665 FCE (Expression (E1), Expression (E2));
8669 Entity (E1) = Entity (E2)
8671 FCE (Right_Opnd (E1), Right_Opnd (E2));
8673 when N_Unchecked_Type_Conversion =>
8675 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8677 FCE (Expression (E1), Expression (E2));
8679 -- All other node types cannot appear in this context. Strictly
8680 -- we should raise a fatal internal error. Instead we just ignore
8681 -- the nodes. This means that if anyone makes a mistake in the
8682 -- expander and mucks an expression tree irretrievably, the result
8683 -- will be a failure to detect a (probably very obscure) case
8684 -- of non-conformance, which is better than bombing on some
8685 -- case where two expressions do in fact conform.
8692 end Fully_Conformant_Expressions;
8694 ----------------------------------------
8695 -- Fully_Conformant_Discrete_Subtypes --
8696 ----------------------------------------
8698 function Fully_Conformant_Discrete_Subtypes
8699 (Given_S1 : Node_Id;
8700 Given_S2 : Node_Id) return Boolean
8702 S1 : constant Node_Id := Original_Node (Given_S1);
8703 S2 : constant Node_Id := Original_Node (Given_S2);
8705 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8706 -- Special-case for a bound given by a discriminant, which in the body
8707 -- is replaced with the discriminal of the enclosing type.
8709 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8710 -- Check both bounds
8712 -----------------------
8713 -- Conforming_Bounds --
8714 -----------------------
8716 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8718 if Is_Entity_Name (B1)
8719 and then Is_Entity_Name (B2)
8720 and then Ekind (Entity (B1)) = E_Discriminant
8722 return Chars (B1) = Chars (B2);
8725 return Fully_Conformant_Expressions (B1, B2);
8727 end Conforming_Bounds;
8729 -----------------------
8730 -- Conforming_Ranges --
8731 -----------------------
8733 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8736 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8738 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8739 end Conforming_Ranges;
8741 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8744 if Nkind (S1) /= Nkind (S2) then
8747 elsif Is_Entity_Name (S1) then
8748 return Entity (S1) = Entity (S2);
8750 elsif Nkind (S1) = N_Range then
8751 return Conforming_Ranges (S1, S2);
8753 elsif Nkind (S1) = N_Subtype_Indication then
8755 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8758 (Range_Expression (Constraint (S1)),
8759 Range_Expression (Constraint (S2)));
8763 end Fully_Conformant_Discrete_Subtypes;
8765 --------------------
8766 -- Install_Entity --
8767 --------------------
8769 procedure Install_Entity (E : Entity_Id) is
8770 Prev : constant Entity_Id := Current_Entity (E);
8772 Set_Is_Immediately_Visible (E);
8773 Set_Current_Entity (E);
8774 Set_Homonym (E, Prev);
8777 ---------------------
8778 -- Install_Formals --
8779 ---------------------
8781 procedure Install_Formals (Id : Entity_Id) is
8784 F := First_Formal (Id);
8785 while Present (F) loop
8789 end Install_Formals;
8791 -----------------------------
8792 -- Is_Interface_Conformant --
8793 -----------------------------
8795 function Is_Interface_Conformant
8796 (Tagged_Type : Entity_Id;
8797 Iface_Prim : Entity_Id;
8798 Prim : Entity_Id) return Boolean
8800 -- The operation may in fact be an inherited (implicit) operation
8801 -- rather than the original interface primitive, so retrieve the
8802 -- ultimate ancestor.
8804 Iface : constant Entity_Id :=
8805 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8806 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8808 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8809 -- Return the controlling formal of Prim
8811 ------------------------
8812 -- Controlling_Formal --
8813 ------------------------
8815 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8819 E := First_Entity (Prim);
8820 while Present (E) loop
8821 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8829 end Controlling_Formal;
8833 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8834 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8836 -- Start of processing for Is_Interface_Conformant
8839 pragma Assert (Is_Subprogram (Iface_Prim)
8840 and then Is_Subprogram (Prim)
8841 and then Is_Dispatching_Operation (Iface_Prim)
8842 and then Is_Dispatching_Operation (Prim));
8844 pragma Assert (Is_Interface (Iface)
8845 or else (Present (Alias (Iface_Prim))
8848 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8850 if Prim = Iface_Prim
8851 or else not Is_Subprogram (Prim)
8852 or else Ekind (Prim) /= Ekind (Iface_Prim)
8853 or else not Is_Dispatching_Operation (Prim)
8854 or else Scope (Prim) /= Scope (Tagged_Type)
8856 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8857 or else not Primitive_Names_Match (Iface_Prim, Prim)
8861 -- The mode of the controlling formals must match
8863 elsif Present (Iface_Ctrl_F)
8864 and then Present (Prim_Ctrl_F)
8865 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8869 -- Case of a procedure, or a function whose result type matches the
8870 -- result type of the interface primitive, or a function that has no
8871 -- controlling result (I or access I).
8873 elsif Ekind (Iface_Prim) = E_Procedure
8874 or else Etype (Prim) = Etype (Iface_Prim)
8875 or else not Has_Controlling_Result (Prim)
8877 return Type_Conformant
8878 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8880 -- Case of a function returning an interface, or an access to one. Check
8881 -- that the return types correspond.
8883 elsif Implements_Interface (Typ, Iface) then
8884 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8886 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8891 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8892 Skip_Controlling_Formals => True);
8898 end Is_Interface_Conformant;
8900 ---------------------------------
8901 -- Is_Non_Overriding_Operation --
8902 ---------------------------------
8904 function Is_Non_Overriding_Operation
8905 (Prev_E : Entity_Id;
8906 New_E : Entity_Id) return Boolean
8910 G_Typ : Entity_Id := Empty;
8912 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8913 -- If F_Type is a derived type associated with a generic actual subtype,
8914 -- then return its Generic_Parent_Type attribute, else return Empty.
8916 function Types_Correspond
8917 (P_Type : Entity_Id;
8918 N_Type : Entity_Id) return Boolean;
8919 -- Returns true if and only if the types (or designated types in the
8920 -- case of anonymous access types) are the same or N_Type is derived
8921 -- directly or indirectly from P_Type.
8923 -----------------------------
8924 -- Get_Generic_Parent_Type --
8925 -----------------------------
8927 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8933 if Is_Derived_Type (F_Typ)
8934 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8936 -- The tree must be traversed to determine the parent subtype in
8937 -- the generic unit, which unfortunately isn't always available
8938 -- via semantic attributes. ??? (Note: The use of Original_Node
8939 -- is needed for cases where a full derived type has been
8942 -- If the parent type is a scalar type, the derivation creates
8943 -- an anonymous base type for it, and the source type is its
8946 if Is_Scalar_Type (F_Typ)
8947 and then not Comes_From_Source (F_Typ)
8951 (Original_Node (Parent (First_Subtype (F_Typ))));
8953 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8955 if Nkind (Defn) = N_Derived_Type_Definition then
8956 Indic := Subtype_Indication (Defn);
8958 if Nkind (Indic) = N_Subtype_Indication then
8959 G_Typ := Entity (Subtype_Mark (Indic));
8961 G_Typ := Entity (Indic);
8964 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8965 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8967 return Generic_Parent_Type (Parent (G_Typ));
8973 end Get_Generic_Parent_Type;
8975 ----------------------
8976 -- Types_Correspond --
8977 ----------------------
8979 function Types_Correspond
8980 (P_Type : Entity_Id;
8981 N_Type : Entity_Id) return Boolean
8983 Prev_Type : Entity_Id := Base_Type (P_Type);
8984 New_Type : Entity_Id := Base_Type (N_Type);
8987 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8988 Prev_Type := Designated_Type (Prev_Type);
8991 if Ekind (New_Type) = E_Anonymous_Access_Type then
8992 New_Type := Designated_Type (New_Type);
8995 if Prev_Type = New_Type then
8998 elsif not Is_Class_Wide_Type (New_Type) then
8999 while Etype (New_Type) /= New_Type loop
9000 New_Type := Etype (New_Type);
9002 if New_Type = Prev_Type then
9008 end Types_Correspond;
9010 -- Start of processing for Is_Non_Overriding_Operation
9013 -- In the case where both operations are implicit derived subprograms
9014 -- then neither overrides the other. This can only occur in certain
9015 -- obscure cases (e.g., derivation from homographs created in a generic
9018 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9021 elsif Ekind (Current_Scope) = E_Package
9022 and then Is_Generic_Instance (Current_Scope)
9023 and then In_Private_Part (Current_Scope)
9024 and then Comes_From_Source (New_E)
9026 -- We examine the formals and result type of the inherited operation,
9027 -- to determine whether their type is derived from (the instance of)
9028 -- a generic type. The first such formal or result type is the one
9031 Formal := First_Formal (Prev_E);
9032 while Present (Formal) loop
9033 F_Typ := Base_Type (Etype (Formal));
9035 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9036 F_Typ := Designated_Type (F_Typ);
9039 G_Typ := Get_Generic_Parent_Type (F_Typ);
9040 exit when Present (G_Typ);
9042 Next_Formal (Formal);
9045 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9046 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9053 -- If the generic type is a private type, then the original operation
9054 -- was not overriding in the generic, because there was no primitive
9055 -- operation to override.
9057 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9058 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9059 N_Formal_Private_Type_Definition
9063 -- The generic parent type is the ancestor of a formal derived
9064 -- type declaration. We need to check whether it has a primitive
9065 -- operation that should be overridden by New_E in the generic.
9069 P_Formal : Entity_Id;
9070 N_Formal : Entity_Id;
9074 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9077 while Present (Prim_Elt) loop
9078 P_Prim := Node (Prim_Elt);
9080 if Chars (P_Prim) = Chars (New_E)
9081 and then Ekind (P_Prim) = Ekind (New_E)
9083 P_Formal := First_Formal (P_Prim);
9084 N_Formal := First_Formal (New_E);
9085 while Present (P_Formal) and then Present (N_Formal) loop
9086 P_Typ := Etype (P_Formal);
9087 N_Typ := Etype (N_Formal);
9089 if not Types_Correspond (P_Typ, N_Typ) then
9093 Next_Entity (P_Formal);
9094 Next_Entity (N_Formal);
9097 -- Found a matching primitive operation belonging to the
9098 -- formal ancestor type, so the new subprogram is
9102 and then No (N_Formal)
9103 and then (Ekind (New_E) /= E_Function
9106 (Etype (P_Prim), Etype (New_E)))
9112 Next_Elmt (Prim_Elt);
9115 -- If no match found, then the new subprogram does not override
9116 -- in the generic (nor in the instance).
9118 -- If the type in question is not abstract, and the subprogram
9119 -- is, this will be an error if the new operation is in the
9120 -- private part of the instance. Emit a warning now, which will
9121 -- make the subsequent error message easier to understand.
9123 if not Is_Abstract_Type (F_Typ)
9124 and then Is_Abstract_Subprogram (Prev_E)
9125 and then In_Private_Part (Current_Scope)
9127 Error_Msg_Node_2 := F_Typ;
9129 ("private operation& in generic unit does not override "
9130 & "any primitive operation of& (RM 12.3 (18))??",
9140 end Is_Non_Overriding_Operation;
9142 -------------------------------------
9143 -- List_Inherited_Pre_Post_Aspects --
9144 -------------------------------------
9146 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9148 if Opt.List_Inherited_Aspects
9149 and then Is_Subprogram_Or_Generic_Subprogram (E)
9152 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9157 for Index in Subps'Range loop
9158 Items := Contract (Subps (Index));
9160 if Present (Items) then
9161 Prag := Pre_Post_Conditions (Items);
9162 while Present (Prag) loop
9163 Error_Msg_Sloc := Sloc (Prag);
9165 if Class_Present (Prag)
9166 and then not Split_PPC (Prag)
9168 if Pragma_Name (Prag) = Name_Precondition then
9170 ("info: & inherits `Pre''Class` aspect from "
9174 ("info: & inherits `Post''Class` aspect from "
9179 Prag := Next_Pragma (Prag);
9185 end List_Inherited_Pre_Post_Aspects;
9187 ------------------------------
9188 -- Make_Inequality_Operator --
9189 ------------------------------
9191 -- S is the defining identifier of an equality operator. We build a
9192 -- subprogram declaration with the right signature. This operation is
9193 -- intrinsic, because it is always expanded as the negation of the
9194 -- call to the equality function.
9196 procedure Make_Inequality_Operator (S : Entity_Id) is
9197 Loc : constant Source_Ptr := Sloc (S);
9200 Op_Name : Entity_Id;
9202 FF : constant Entity_Id := First_Formal (S);
9203 NF : constant Entity_Id := Next_Formal (FF);
9206 -- Check that equality was properly defined, ignore call if not
9213 A : constant Entity_Id :=
9214 Make_Defining_Identifier (Sloc (FF),
9215 Chars => Chars (FF));
9217 B : constant Entity_Id :=
9218 Make_Defining_Identifier (Sloc (NF),
9219 Chars => Chars (NF));
9222 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9224 Formals := New_List (
9225 Make_Parameter_Specification (Loc,
9226 Defining_Identifier => A,
9228 New_Occurrence_Of (Etype (First_Formal (S)),
9229 Sloc (Etype (First_Formal (S))))),
9231 Make_Parameter_Specification (Loc,
9232 Defining_Identifier => B,
9234 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9235 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9238 Make_Subprogram_Declaration (Loc,
9240 Make_Function_Specification (Loc,
9241 Defining_Unit_Name => Op_Name,
9242 Parameter_Specifications => Formals,
9243 Result_Definition =>
9244 New_Occurrence_Of (Standard_Boolean, Loc)));
9246 -- Insert inequality right after equality if it is explicit or after
9247 -- the derived type when implicit. These entities are created only
9248 -- for visibility purposes, and eventually replaced in the course
9249 -- of expansion, so they do not need to be attached to the tree and
9250 -- seen by the back-end. Keeping them internal also avoids spurious
9251 -- freezing problems. The declaration is inserted in the tree for
9252 -- analysis, and removed afterwards. If the equality operator comes
9253 -- from an explicit declaration, attach the inequality immediately
9254 -- after. Else the equality is inherited from a derived type
9255 -- declaration, so insert inequality after that declaration.
9257 if No (Alias (S)) then
9258 Insert_After (Unit_Declaration_Node (S), Decl);
9259 elsif Is_List_Member (Parent (S)) then
9260 Insert_After (Parent (S), Decl);
9262 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9265 Mark_Rewrite_Insertion (Decl);
9266 Set_Is_Intrinsic_Subprogram (Op_Name);
9269 Set_Has_Completion (Op_Name);
9270 Set_Corresponding_Equality (Op_Name, S);
9271 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9273 end Make_Inequality_Operator;
9275 ----------------------
9276 -- May_Need_Actuals --
9277 ----------------------
9279 procedure May_Need_Actuals (Fun : Entity_Id) is
9284 F := First_Formal (Fun);
9286 while Present (F) loop
9287 if No (Default_Value (F)) then
9295 Set_Needs_No_Actuals (Fun, B);
9296 end May_Need_Actuals;
9298 ---------------------
9299 -- Mode_Conformant --
9300 ---------------------
9302 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9305 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9307 end Mode_Conformant;
9309 ---------------------------
9310 -- New_Overloaded_Entity --
9311 ---------------------------
9313 procedure New_Overloaded_Entity
9315 Derived_Type : Entity_Id := Empty)
9317 Overridden_Subp : Entity_Id := Empty;
9318 -- Set if the current scope has an operation that is type-conformant
9319 -- with S, and becomes hidden by S.
9321 Is_Primitive_Subp : Boolean;
9322 -- Set to True if the new subprogram is primitive
9325 -- Entity that S overrides
9327 Prev_Vis : Entity_Id := Empty;
9328 -- Predecessor of E in Homonym chain
9330 procedure Check_For_Primitive_Subprogram
9331 (Is_Primitive : out Boolean;
9332 Is_Overriding : Boolean := False);
9333 -- If the subprogram being analyzed is a primitive operation of the type
9334 -- of a formal or result, set the Has_Primitive_Operations flag on the
9335 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9336 -- corresponding flag on the entity itself for later use.
9338 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
9339 -- True if a) E is a subprogram whose first formal is a concurrent type
9340 -- defined in the scope of E that has some entry or subprogram whose
9341 -- profile matches E, or b) E is an internally built dispatching
9342 -- subprogram of a protected type and there is a matching subprogram
9343 -- defined in the enclosing scope of the protected type, or c) E is
9344 -- an entry of a synchronized type and a matching procedure has been
9345 -- previously defined in the enclosing scope of the synchronized type.
9347 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9348 -- Check that E is declared in the private part of the current package,
9349 -- or in the package body, where it may hide a previous declaration.
9350 -- We can't use In_Private_Part by itself because this flag is also
9351 -- set when freezing entities, so we must examine the place of the
9352 -- declaration in the tree, and recognize wrapper packages as well.
9354 function Is_Overriding_Alias
9356 New_E : Entity_Id) return Boolean;
9357 -- Check whether new subprogram and old subprogram are both inherited
9358 -- from subprograms that have distinct dispatch table entries. This can
9359 -- occur with derivations from instances with accidental homonyms. The
9360 -- function is conservative given that the converse is only true within
9361 -- instances that contain accidental overloadings.
9363 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
9364 -- Report conflict between entities S and E.
9366 ------------------------------------
9367 -- Check_For_Primitive_Subprogram --
9368 ------------------------------------
9370 procedure Check_For_Primitive_Subprogram
9371 (Is_Primitive : out Boolean;
9372 Is_Overriding : Boolean := False)
9378 function Visible_Part_Type (T : Entity_Id) return Boolean;
9379 -- Returns true if T is declared in the visible part of the current
9380 -- package scope; otherwise returns false. Assumes that T is declared
9383 procedure Check_Private_Overriding (T : Entity_Id);
9384 -- Checks that if a primitive abstract subprogram of a visible
9385 -- abstract type is declared in a private part, then it must override
9386 -- an abstract subprogram declared in the visible part. Also checks
9387 -- that if a primitive function with a controlling result is declared
9388 -- in a private part, then it must override a function declared in
9389 -- the visible part.
9391 ------------------------------
9392 -- Check_Private_Overriding --
9393 ------------------------------
9395 procedure Check_Private_Overriding (T : Entity_Id) is
9396 function Overrides_Private_Part_Op return Boolean;
9397 -- This detects the special case where the overriding subprogram
9398 -- is overriding a subprogram that was declared in the same
9399 -- private part. That case is illegal by 3.9.3(10).
9401 function Overrides_Visible_Function
9402 (Partial_View : Entity_Id) return Boolean;
9403 -- True if S overrides a function in the visible part. The
9404 -- overridden function could be explicitly or implicitly declared.
9406 -------------------------------
9407 -- Overrides_Private_Part_Op --
9408 -------------------------------
9410 function Overrides_Private_Part_Op return Boolean is
9411 Over_Decl : constant Node_Id :=
9412 Unit_Declaration_Node (Overridden_Operation (S));
9413 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
9416 pragma Assert (Is_Overriding);
9418 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
9420 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
9422 return In_Same_List (Over_Decl, Subp_Decl);
9423 end Overrides_Private_Part_Op;
9425 --------------------------------
9426 -- Overrides_Visible_Function --
9427 --------------------------------
9429 function Overrides_Visible_Function
9430 (Partial_View : Entity_Id) return Boolean
9433 if not Is_Overriding or else not Has_Homonym (S) then
9437 if not Present (Partial_View) then
9441 -- Search through all the homonyms H of S in the current
9442 -- package spec, and return True if we find one that matches.
9443 -- Note that Parent (H) will be the declaration of the
9444 -- partial view of T for a match.
9451 exit when not Present (H) or else Scope (H) /= Scope (S);
9455 N_Private_Extension_Declaration,
9456 N_Private_Type_Declaration)
9457 and then Defining_Identifier (Parent (H)) = Partial_View
9465 end Overrides_Visible_Function;
9467 -- Start of processing for Check_Private_Overriding
9470 if Is_Package_Or_Generic_Package (Current_Scope)
9471 and then In_Private_Part (Current_Scope)
9472 and then Visible_Part_Type (T)
9473 and then not In_Instance
9475 if Is_Abstract_Type (T)
9476 and then Is_Abstract_Subprogram (S)
9477 and then (not Is_Overriding
9478 or else not Is_Abstract_Subprogram (E)
9479 or else Overrides_Private_Part_Op)
9482 ("abstract subprograms must be visible (RM 3.9.3(10))!",
9485 elsif Ekind (S) = E_Function then
9487 Partial_View : constant Entity_Id :=
9488 Incomplete_Or_Partial_View (T);
9491 if not Overrides_Visible_Function (Partial_View) then
9493 -- Here, S is "function ... return T;" declared in
9494 -- the private part, not overriding some visible
9495 -- operation. That's illegal in the tagged case
9496 -- (but not if the private type is untagged).
9498 if ((Present (Partial_View)
9499 and then Is_Tagged_Type (Partial_View))
9500 or else (not Present (Partial_View)
9501 and then Is_Tagged_Type (T)))
9502 and then T = Base_Type (Etype (S))
9505 ("private function with tagged result must"
9506 & " override visible-part function", S);
9508 ("\move subprogram to the visible part"
9509 & " (RM 3.9.3(10))", S);
9511 -- AI05-0073: extend this test to the case of a
9512 -- function with a controlling access result.
9514 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9515 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9517 not Is_Class_Wide_Type
9518 (Designated_Type (Etype (S)))
9519 and then Ada_Version >= Ada_2012
9522 ("private function with controlling access "
9523 & "result must override visible-part function",
9526 ("\move subprogram to the visible part"
9527 & " (RM 3.9.3(10))", S);
9533 end Check_Private_Overriding;
9535 -----------------------
9536 -- Visible_Part_Type --
9537 -----------------------
9539 function Visible_Part_Type (T : Entity_Id) return Boolean is
9540 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9544 -- If the entity is a private type, then it must be declared in a
9547 if Ekind (T) in Private_Kind then
9551 -- Otherwise, we traverse the visible part looking for its
9552 -- corresponding declaration. We cannot use the declaration
9553 -- node directly because in the private part the entity of a
9554 -- private type is the one in the full view, which does not
9555 -- indicate that it is the completion of something visible.
9557 N := First (Visible_Declarations (Specification (P)));
9558 while Present (N) loop
9559 if Nkind (N) = N_Full_Type_Declaration
9560 and then Present (Defining_Identifier (N))
9561 and then T = Defining_Identifier (N)
9565 elsif Nkind_In (N, N_Private_Type_Declaration,
9566 N_Private_Extension_Declaration)
9567 and then Present (Defining_Identifier (N))
9568 and then T = Full_View (Defining_Identifier (N))
9577 end Visible_Part_Type;
9579 -- Start of processing for Check_For_Primitive_Subprogram
9582 Is_Primitive := False;
9584 if not Comes_From_Source (S) then
9587 -- If subprogram is at library level, it is not primitive operation
9589 elsif Current_Scope = Standard_Standard then
9592 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9593 and then not In_Package_Body (Current_Scope))
9594 or else Is_Overriding
9596 -- For function, check return type
9598 if Ekind (S) = E_Function then
9599 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9600 F_Typ := Designated_Type (Etype (S));
9605 B_Typ := Base_Type (F_Typ);
9607 if Scope (B_Typ) = Current_Scope
9608 and then not Is_Class_Wide_Type (B_Typ)
9609 and then not Is_Generic_Type (B_Typ)
9611 Is_Primitive := True;
9612 Set_Has_Primitive_Operations (B_Typ);
9613 Set_Is_Primitive (S);
9614 Check_Private_Overriding (B_Typ);
9616 -- The Ghost policy in effect at the point of declaration of
9617 -- a tagged type and a primitive operation must match
9618 -- (SPARK RM 6.9(16)).
9620 Check_Ghost_Primitive (S, B_Typ);
9624 -- For all subprograms, check formals
9626 Formal := First_Formal (S);
9627 while Present (Formal) loop
9628 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9629 F_Typ := Designated_Type (Etype (Formal));
9631 F_Typ := Etype (Formal);
9634 B_Typ := Base_Type (F_Typ);
9636 if Ekind (B_Typ) = E_Access_Subtype then
9637 B_Typ := Base_Type (B_Typ);
9640 if Scope (B_Typ) = Current_Scope
9641 and then not Is_Class_Wide_Type (B_Typ)
9642 and then not Is_Generic_Type (B_Typ)
9644 Is_Primitive := True;
9645 Set_Is_Primitive (S);
9646 Set_Has_Primitive_Operations (B_Typ);
9647 Check_Private_Overriding (B_Typ);
9649 -- The Ghost policy in effect at the point of declaration of
9650 -- a tagged type and a primitive operation must match
9651 -- (SPARK RM 6.9(16)).
9653 Check_Ghost_Primitive (S, B_Typ);
9656 Next_Formal (Formal);
9659 -- Special case: An equality function can be redefined for a type
9660 -- occurring in a declarative part, and won't otherwise be treated as
9661 -- a primitive because it doesn't occur in a package spec and doesn't
9662 -- override an inherited subprogram. It's important that we mark it
9663 -- primitive so it can be returned by Collect_Primitive_Operations
9664 -- and be used in composing the equality operation of later types
9665 -- that have a component of the type.
9667 elsif Chars (S) = Name_Op_Eq
9668 and then Etype (S) = Standard_Boolean
9670 B_Typ := Base_Type (Etype (First_Formal (S)));
9672 if Scope (B_Typ) = Current_Scope
9674 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9675 and then not Is_Limited_Type (B_Typ)
9677 Is_Primitive := True;
9678 Set_Is_Primitive (S);
9679 Set_Has_Primitive_Operations (B_Typ);
9680 Check_Private_Overriding (B_Typ);
9682 -- The Ghost policy in effect at the point of declaration of a
9683 -- tagged type and a primitive operation must match
9684 -- (SPARK RM 6.9(16)).
9686 Check_Ghost_Primitive (S, B_Typ);
9689 end Check_For_Primitive_Subprogram;
9691 --------------------------------------
9692 -- Has_Matching_Entry_Or_Subprogram --
9693 --------------------------------------
9695 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean
9697 function Check_Conforming_Parameters
9698 (E1_Param : Node_Id;
9699 E2_Param : Node_Id) return Boolean;
9700 -- Starting from the given parameters, check that all the parameters
9701 -- of two entries or subprograms are are subtype conformant. Used to
9702 -- skip the check on the controlling argument.
9704 function Matching_Entry_Or_Subprogram
9705 (Conc_Typ : Entity_Id;
9706 Subp : Entity_Id) return Entity_Id;
9707 -- Return the first entry or subprogram of the given concurrent type
9708 -- whose name matches the name of Subp and has a profile conformant
9709 -- with Subp; return Empty if not found.
9711 function Matching_Dispatching_Subprogram
9712 (Conc_Typ : Entity_Id;
9713 Ent : Entity_Id) return Entity_Id;
9714 -- Return the first dispatching primitive of Conc_Type defined in the
9715 -- enclosing scope of Conc_Type (ie. before the full definition of
9716 -- this concurrent type) whose name matches the entry Ent and has a
9717 -- profile conformant with the profile of the corresponding (not yet
9718 -- built) dispatching primitive of Ent; return Empty if not found.
9720 function Matching_Original_Protected_Subprogram
9721 (Prot_Typ : Entity_Id;
9722 Subp : Entity_Id) return Entity_Id;
9723 -- Return the first subprogram defined in the enclosing scope of
9724 -- Prot_Typ (before the full definition of this protected type)
9725 -- whose name matches the original name of Subp and has a profile
9726 -- conformant with the profile of Subp; return Empty if not found.
9728 ---------------------------------
9729 -- Check_Confirming_Parameters --
9730 ---------------------------------
9732 function Check_Conforming_Parameters
9733 (E1_Param : Node_Id;
9734 E2_Param : Node_Id) return Boolean
9736 Param_E1 : Node_Id := E1_Param;
9737 Param_E2 : Node_Id := E2_Param;
9740 while Present (Param_E1) and then Present (Param_E2) loop
9741 if Ekind (Defining_Identifier (Param_E1))
9742 /= Ekind (Defining_Identifier (Param_E2))
9744 Conforming_Types (Find_Parameter_Type (Param_E1),
9745 Find_Parameter_Type (Param_E2),
9755 -- The candidate is not valid if one of the two lists contains
9756 -- more parameters than the other
9758 return No (Param_E1) and then No (Param_E2);
9759 end Check_Conforming_Parameters;
9761 ----------------------------------
9762 -- Matching_Entry_Or_Subprogram --
9763 ----------------------------------
9765 function Matching_Entry_Or_Subprogram
9766 (Conc_Typ : Entity_Id;
9767 Subp : Entity_Id) return Entity_Id
9772 E := First_Entity (Conc_Typ);
9773 while Present (E) loop
9774 if Chars (Subp) = Chars (E)
9775 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
9777 Check_Conforming_Parameters
9778 (First (Parameter_Specifications (Parent (E))),
9779 Next (First (Parameter_Specifications (Parent (Subp)))))
9788 end Matching_Entry_Or_Subprogram;
9790 -------------------------------------
9791 -- Matching_Dispatching_Subprogram --
9792 -------------------------------------
9794 function Matching_Dispatching_Subprogram
9795 (Conc_Typ : Entity_Id;
9796 Ent : Entity_Id) return Entity_Id
9801 -- Search for entities in the enclosing scope of this synchonized
9804 pragma Assert (Is_Concurrent_Type (Conc_Typ));
9805 Push_Scope (Scope (Conc_Typ));
9806 E := Current_Entity_In_Scope (Ent);
9809 while Present (E) loop
9810 if Scope (E) = Scope (Conc_Typ)
9811 and then Comes_From_Source (E)
9812 and then Ekind (E) = E_Procedure
9813 and then Present (First_Entity (E))
9814 and then Is_Controlling_Formal (First_Entity (E))
9815 and then Etype (First_Entity (E)) = Conc_Typ
9817 Check_Conforming_Parameters
9818 (First (Parameter_Specifications (Parent (Ent))),
9819 Next (First (Parameter_Specifications (Parent (E)))))
9828 end Matching_Dispatching_Subprogram;
9830 --------------------------------------------
9831 -- Matching_Original_Protected_Subprogram --
9832 --------------------------------------------
9834 function Matching_Original_Protected_Subprogram
9835 (Prot_Typ : Entity_Id;
9836 Subp : Entity_Id) return Entity_Id
9838 ICF : constant Boolean :=
9839 Is_Controlling_Formal (First_Entity (Subp));
9843 -- Temporarily decorate the first parameter of Subp as controlling
9844 -- formal; required to invoke Subtype_Conformant()
9846 Set_Is_Controlling_Formal (First_Entity (Subp));
9849 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
9851 while Present (E) loop
9852 if Scope (E) = Scope (Prot_Typ)
9853 and then Comes_From_Source (E)
9854 and then Ekind (Subp) = Ekind (E)
9855 and then Present (First_Entity (E))
9856 and then Is_Controlling_Formal (First_Entity (E))
9857 and then Etype (First_Entity (E)) = Prot_Typ
9858 and then Subtype_Conformant (Subp, E,
9859 Skip_Controlling_Formals => True)
9861 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
9868 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
9870 end Matching_Original_Protected_Subprogram;
9872 -- Start of processing for Has_Matching_Entry_Or_Subprogram
9875 -- Case 1: E is a subprogram whose first formal is a concurrent type
9876 -- defined in the scope of E that has an entry or subprogram whose
9877 -- profile matches E.
9879 if Comes_From_Source (E)
9880 and then Is_Subprogram (E)
9881 and then Present (First_Entity (E))
9882 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
9885 Scope (Corresponding_Concurrent_Type (
9886 Etype (First_Entity (E))))
9889 (Matching_Entry_Or_Subprogram
9890 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
9894 Matching_Entry_Or_Subprogram
9895 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
9900 -- Case 2: E is an internally built dispatching subprogram of a
9901 -- protected type and there is a subprogram defined in the enclosing
9902 -- scope of the protected type that has the original name of E and
9903 -- its profile is conformant with the profile of E. We check the
9904 -- name of the original protected subprogram associated with E since
9905 -- the expander builds dispatching primitives of protected functions
9906 -- and procedures with other name (see Exp_Ch9.Build_Selected_Name).
9908 elsif not Comes_From_Source (E)
9909 and then Is_Subprogram (E)
9910 and then Present (First_Entity (E))
9911 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
9912 and then Present (Original_Protected_Subprogram (E))
9915 (Matching_Original_Protected_Subprogram
9916 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
9920 Matching_Original_Protected_Subprogram
9921 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
9925 -- Case : E is an entry of a synchronized type and a matching
9926 -- procedure has been previously defined in the enclosing scope
9927 -- of the synchronzed type.
9929 elsif Comes_From_Source (E)
9930 and then Ekind (E) = E_Entry
9932 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
9935 Matching_Dispatching_Subprogram (Current_Scope, E));
9940 end Has_Matching_Entry_Or_Subprogram;
9942 ----------------------------
9943 -- Is_Private_Declaration --
9944 ----------------------------
9946 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9947 Priv_Decls : List_Id;
9948 Decl : constant Node_Id := Unit_Declaration_Node (E);
9951 if Is_Package_Or_Generic_Package (Current_Scope)
9952 and then In_Private_Part (Current_Scope)
9955 Private_Declarations (Package_Specification (Current_Scope));
9957 return In_Package_Body (Current_Scope)
9959 (Is_List_Member (Decl)
9960 and then List_Containing (Decl) = Priv_Decls)
9961 or else (Nkind (Parent (Decl)) = N_Package_Specification
9964 (Defining_Entity (Parent (Decl)))
9965 and then List_Containing (Parent (Parent (Decl))) =
9970 end Is_Private_Declaration;
9972 --------------------------
9973 -- Is_Overriding_Alias --
9974 --------------------------
9976 function Is_Overriding_Alias
9978 New_E : Entity_Id) return Boolean
9980 AO : constant Entity_Id := Alias (Old_E);
9981 AN : constant Entity_Id := Alias (New_E);
9983 return Scope (AO) /= Scope (AN)
9984 or else No (DTC_Entity (AO))
9985 or else No (DTC_Entity (AN))
9986 or else DT_Position (AO) = DT_Position (AN);
9987 end Is_Overriding_Alias;
9989 ---------------------
9990 -- Report_Conflict --
9991 ---------------------
9993 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
9995 Error_Msg_Sloc := Sloc (E);
9997 -- Generate message, with useful additional warning if in generic
9999 if Is_Generic_Unit (E) then
10000 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10001 Error_Msg_N ("\& conflicts with declaration#", S);
10003 Error_Msg_N ("& conflicts with declaration#", S);
10005 end Report_Conflict;
10007 -- Start of processing for New_Overloaded_Entity
10010 -- We need to look for an entity that S may override. This must be a
10011 -- homonym in the current scope, so we look for the first homonym of
10012 -- S in the current scope as the starting point for the search.
10014 E := Current_Entity_In_Scope (S);
10016 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10017 -- They are directly added to the list of primitive operations of
10018 -- Derived_Type, unless this is a rederivation in the private part
10019 -- of an operation that was already derived in the visible part of
10020 -- the current package.
10022 if Ada_Version >= Ada_2005
10023 and then Present (Derived_Type)
10024 and then Present (Alias (S))
10025 and then Is_Dispatching_Operation (Alias (S))
10026 and then Present (Find_Dispatching_Type (Alias (S)))
10027 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10029 -- For private types, when the full-view is processed we propagate to
10030 -- the full view the non-overridden entities whose attribute "alias"
10031 -- references an interface primitive. These entities were added by
10032 -- Derive_Subprograms to ensure that interface primitives are
10035 -- Inside_Freeze_Actions is non zero when S corresponds with an
10036 -- internal entity that links an interface primitive with its
10037 -- covering primitive through attribute Interface_Alias (see
10038 -- Add_Internal_Interface_Entities).
10040 if Inside_Freezing_Actions = 0
10041 and then Is_Package_Or_Generic_Package (Current_Scope)
10042 and then In_Private_Part (Current_Scope)
10043 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10044 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10045 and then Full_View (Defining_Identifier (Parent (E)))
10046 = Defining_Identifier (Parent (S))
10047 and then Alias (E) = Alias (S)
10049 Check_Operation_From_Private_View (S, E);
10050 Set_Is_Dispatching_Operation (S);
10055 Enter_Overloaded_Entity (S);
10056 Check_Dispatching_Operation (S, Empty);
10057 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10063 -- For synchronized types check conflicts of this entity with
10064 -- previously defined entities.
10066 if Ada_Version >= Ada_2005
10067 and then Has_Matching_Entry_Or_Subprogram (S)
10072 -- If there is no homonym then this is definitely not overriding
10075 Enter_Overloaded_Entity (S);
10076 Check_Dispatching_Operation (S, Empty);
10077 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10079 -- If subprogram has an explicit declaration, check whether it has an
10080 -- overriding indicator.
10082 if Comes_From_Source (S) then
10083 Check_Synchronized_Overriding (S, Overridden_Subp);
10085 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10086 -- it may have overridden some hidden inherited primitive. Update
10087 -- Overridden_Subp to avoid spurious errors when checking the
10088 -- overriding indicator.
10090 if Ada_Version >= Ada_2012
10091 and then No (Overridden_Subp)
10092 and then Is_Dispatching_Operation (S)
10093 and then Present (Overridden_Operation (S))
10095 Overridden_Subp := Overridden_Operation (S);
10098 Check_Overriding_Indicator
10099 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10101 -- The Ghost policy in effect at the point of declaration of a
10102 -- parent subprogram and an overriding subprogram must match
10103 -- (SPARK RM 6.9(17)).
10105 Check_Ghost_Overriding (S, Overridden_Subp);
10108 -- If there is a homonym that is not overloadable, then we have an
10109 -- error, except for the special cases checked explicitly below.
10111 elsif not Is_Overloadable (E) then
10113 -- Check for spurious conflict produced by a subprogram that has the
10114 -- same name as that of the enclosing generic package. The conflict
10115 -- occurs within an instance, between the subprogram and the renaming
10116 -- declaration for the package. After the subprogram, the package
10117 -- renaming declaration becomes hidden.
10119 if Ekind (E) = E_Package
10120 and then Present (Renamed_Object (E))
10121 and then Renamed_Object (E) = Current_Scope
10122 and then Nkind (Parent (Renamed_Object (E))) =
10123 N_Package_Specification
10124 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10127 Set_Is_Immediately_Visible (E, False);
10128 Enter_Overloaded_Entity (S);
10129 Set_Homonym (S, Homonym (E));
10130 Check_Dispatching_Operation (S, Empty);
10131 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10133 -- If the subprogram is implicit it is hidden by the previous
10134 -- declaration. However if it is dispatching, it must appear in the
10135 -- dispatch table anyway, because it can be dispatched to even if it
10136 -- cannot be called directly.
10138 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10139 Set_Scope (S, Current_Scope);
10141 if Is_Dispatching_Operation (Alias (S)) then
10142 Check_Dispatching_Operation (S, Empty);
10148 Report_Conflict (S, E);
10152 -- E exists and is overloadable
10155 Check_Synchronized_Overriding (S, Overridden_Subp);
10157 -- Loop through E and its homonyms to determine if any of them is
10158 -- the candidate for overriding by S.
10160 while Present (E) loop
10162 -- Definitely not interesting if not in the current scope
10164 if Scope (E) /= Current_Scope then
10167 -- A function can overload the name of an abstract state. The
10168 -- state can be viewed as a function with a profile that cannot
10169 -- be matched by anything.
10171 elsif Ekind (S) = E_Function
10172 and then Ekind (E) = E_Abstract_State
10174 Enter_Overloaded_Entity (S);
10177 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10178 -- procedures locate the internally generated spec. We enforce
10179 -- mode conformance since a tagged type may inherit from
10180 -- interfaces several null primitives which differ only in
10181 -- the mode of the formals.
10183 elsif not Comes_From_Source (S)
10184 and then Is_Null_Procedure (S)
10185 and then not Mode_Conformant (E, S)
10189 -- Check if we have type conformance
10191 elsif Type_Conformant (E, S) then
10193 -- If the old and new entities have the same profile and one
10194 -- is not the body of the other, then this is an error, unless
10195 -- one of them is implicitly declared.
10197 -- There are some cases when both can be implicit, for example
10198 -- when both a literal and a function that overrides it are
10199 -- inherited in a derivation, or when an inherited operation
10200 -- of a tagged full type overrides the inherited operation of
10201 -- a private extension. Ada 83 had a special rule for the
10202 -- literal case. In Ada 95, the later implicit operation hides
10203 -- the former, and the literal is always the former. In the
10204 -- odd case where both are derived operations declared at the
10205 -- same point, both operations should be declared, and in that
10206 -- case we bypass the following test and proceed to the next
10207 -- part. This can only occur for certain obscure cases in
10208 -- instances, when an operation on a type derived from a formal
10209 -- private type does not override a homograph inherited from
10210 -- the actual. In subsequent derivations of such a type, the
10211 -- DT positions of these operations remain distinct, if they
10214 if Present (Alias (S))
10215 and then (No (Alias (E))
10216 or else Comes_From_Source (E)
10217 or else Is_Abstract_Subprogram (S)
10219 (Is_Dispatching_Operation (E)
10220 and then Is_Overriding_Alias (E, S)))
10221 and then Ekind (E) /= E_Enumeration_Literal
10223 -- When an derived operation is overloaded it may be due to
10224 -- the fact that the full view of a private extension
10225 -- re-inherits. It has to be dealt with.
10227 if Is_Package_Or_Generic_Package (Current_Scope)
10228 and then In_Private_Part (Current_Scope)
10230 Check_Operation_From_Private_View (S, E);
10233 -- In any case the implicit operation remains hidden by the
10234 -- existing declaration, which is overriding. Indicate that
10235 -- E overrides the operation from which S is inherited.
10237 if Present (Alias (S)) then
10238 Set_Overridden_Operation (E, Alias (S));
10239 Inherit_Subprogram_Contract (E, Alias (S));
10242 Set_Overridden_Operation (E, S);
10243 Inherit_Subprogram_Contract (E, S);
10246 if Comes_From_Source (E) then
10247 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10249 -- The Ghost policy in effect at the point of declaration
10250 -- of a parent subprogram and an overriding subprogram
10251 -- must match (SPARK RM 6.9(17)).
10253 Check_Ghost_Overriding (E, S);
10258 -- Within an instance, the renaming declarations for actual
10259 -- subprograms may become ambiguous, but they do not hide each
10262 elsif Ekind (E) /= E_Entry
10263 and then not Comes_From_Source (E)
10264 and then not Is_Generic_Instance (E)
10265 and then (Present (Alias (E))
10266 or else Is_Intrinsic_Subprogram (E))
10267 and then (not In_Instance
10268 or else No (Parent (E))
10269 or else Nkind (Unit_Declaration_Node (E)) /=
10270 N_Subprogram_Renaming_Declaration)
10272 -- A subprogram child unit is not allowed to override an
10273 -- inherited subprogram (10.1.1(20)).
10275 if Is_Child_Unit (S) then
10277 ("child unit overrides inherited subprogram in parent",
10282 if Is_Non_Overriding_Operation (E, S) then
10283 Enter_Overloaded_Entity (S);
10285 if No (Derived_Type)
10286 or else Is_Tagged_Type (Derived_Type)
10288 Check_Dispatching_Operation (S, Empty);
10294 -- E is a derived operation or an internal operator which
10295 -- is being overridden. Remove E from further visibility.
10296 -- Furthermore, if E is a dispatching operation, it must be
10297 -- replaced in the list of primitive operations of its type
10298 -- (see Override_Dispatching_Operation).
10300 Overridden_Subp := E;
10306 Prev := First_Entity (Current_Scope);
10307 while Present (Prev) and then Next_Entity (Prev) /= E loop
10308 Next_Entity (Prev);
10311 -- It is possible for E to be in the current scope and
10312 -- yet not in the entity chain. This can only occur in a
10313 -- generic context where E is an implicit concatenation
10314 -- in the formal part, because in a generic body the
10315 -- entity chain starts with the formals.
10317 -- In GNATprove mode, a wrapper for an operation with
10318 -- axiomatization may be a homonym of another declaration
10319 -- for an actual subprogram (needs refinement ???).
10323 and then GNATprove_Mode
10325 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10326 N_Subprogram_Renaming_Declaration
10330 pragma Assert (Chars (E) = Name_Op_Concat);
10335 -- E must be removed both from the entity_list of the
10336 -- current scope, and from the visibility chain.
10338 if Debug_Flag_E then
10339 Write_Str ("Override implicit operation ");
10340 Write_Int (Int (E));
10344 -- If E is a predefined concatenation, it stands for four
10345 -- different operations. As a result, a single explicit
10346 -- declaration does not hide it. In a possible ambiguous
10347 -- situation, Disambiguate chooses the user-defined op,
10348 -- so it is correct to retain the previous internal one.
10350 if Chars (E) /= Name_Op_Concat
10351 or else Ekind (E) /= E_Operator
10353 -- For nondispatching derived operations that are
10354 -- overridden by a subprogram declared in the private
10355 -- part of a package, we retain the derived subprogram
10356 -- but mark it as not immediately visible. If the
10357 -- derived operation was declared in the visible part
10358 -- then this ensures that it will still be visible
10359 -- outside the package with the proper signature
10360 -- (calls from outside must also be directed to this
10361 -- version rather than the overriding one, unlike the
10362 -- dispatching case). Calls from inside the package
10363 -- will still resolve to the overriding subprogram
10364 -- since the derived one is marked as not visible
10365 -- within the package.
10367 -- If the private operation is dispatching, we achieve
10368 -- the overriding by keeping the implicit operation
10369 -- but setting its alias to be the overriding one. In
10370 -- this fashion the proper body is executed in all
10371 -- cases, but the original signature is used outside
10374 -- If the overriding is not in the private part, we
10375 -- remove the implicit operation altogether.
10377 if Is_Private_Declaration (S) then
10378 if not Is_Dispatching_Operation (E) then
10379 Set_Is_Immediately_Visible (E, False);
10381 -- Work done in Override_Dispatching_Operation,
10382 -- so nothing else needs to be done here.
10388 -- Find predecessor of E in Homonym chain
10390 if E = Current_Entity (E) then
10393 Prev_Vis := Current_Entity (E);
10394 while Homonym (Prev_Vis) /= E loop
10395 Prev_Vis := Homonym (Prev_Vis);
10399 if Prev_Vis /= Empty then
10401 -- Skip E in the visibility chain
10403 Set_Homonym (Prev_Vis, Homonym (E));
10406 Set_Name_Entity_Id (Chars (E), Homonym (E));
10409 Set_Next_Entity (Prev, Next_Entity (E));
10411 if No (Next_Entity (Prev)) then
10412 Set_Last_Entity (Current_Scope, Prev);
10417 Enter_Overloaded_Entity (S);
10419 -- For entities generated by Derive_Subprograms the
10420 -- overridden operation is the inherited primitive
10421 -- (which is available through the attribute alias).
10423 if not (Comes_From_Source (E))
10424 and then Is_Dispatching_Operation (E)
10425 and then Find_Dispatching_Type (E) =
10426 Find_Dispatching_Type (S)
10427 and then Present (Alias (E))
10428 and then Comes_From_Source (Alias (E))
10430 Set_Overridden_Operation (S, Alias (E));
10431 Inherit_Subprogram_Contract (S, Alias (E));
10433 -- Normal case of setting entity as overridden
10435 -- Note: Static_Initialization and Overridden_Operation
10436 -- attributes use the same field in subprogram entities.
10437 -- Static_Initialization is only defined for internal
10438 -- initialization procedures, where Overridden_Operation
10439 -- is irrelevant. Therefore the setting of this attribute
10440 -- must check whether the target is an init_proc.
10442 elsif not Is_Init_Proc (S) then
10443 Set_Overridden_Operation (S, E);
10444 Inherit_Subprogram_Contract (S, E);
10447 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10449 -- The Ghost policy in effect at the point of declaration
10450 -- of a parent subprogram and an overriding subprogram
10451 -- must match (SPARK RM 6.9(17)).
10453 Check_Ghost_Overriding (S, E);
10455 -- If S is a user-defined subprogram or a null procedure
10456 -- expanded to override an inherited null procedure, or a
10457 -- predefined dispatching primitive then indicate that E
10458 -- overrides the operation from which S is inherited.
10460 if Comes_From_Source (S)
10462 (Present (Parent (S))
10464 Nkind (Parent (S)) = N_Procedure_Specification
10466 Null_Present (Parent (S)))
10468 (Present (Alias (E))
10470 Is_Predefined_Dispatching_Operation (Alias (E)))
10472 if Present (Alias (E)) then
10473 Set_Overridden_Operation (S, Alias (E));
10474 Inherit_Subprogram_Contract (S, Alias (E));
10478 if Is_Dispatching_Operation (E) then
10480 -- An overriding dispatching subprogram inherits the
10481 -- convention of the overridden subprogram (AI-117).
10483 Set_Convention (S, Convention (E));
10484 Check_Dispatching_Operation (S, E);
10486 -- In GNATprove_Mode, create the pragmas corresponding
10487 -- to inherited class-wide conditions.
10489 if GNATprove_Mode then
10490 Collect_Inherited_Class_Wide_Conditions (S);
10494 Check_Dispatching_Operation (S, Empty);
10497 Check_For_Primitive_Subprogram
10498 (Is_Primitive_Subp, Is_Overriding => True);
10499 goto Check_Inequality;
10502 -- Apparent redeclarations in instances can occur when two
10503 -- formal types get the same actual type. The subprograms in
10504 -- in the instance are legal, even if not callable from the
10505 -- outside. Calls from within are disambiguated elsewhere.
10506 -- For dispatching operations in the visible part, the usual
10507 -- rules apply, and operations with the same profile are not
10508 -- legal (B830001).
10510 elsif (In_Instance_Visible_Part
10511 and then not Is_Dispatching_Operation (E))
10512 or else In_Instance_Not_Visible
10516 -- Here we have a real error (identical profile)
10519 Error_Msg_Sloc := Sloc (E);
10521 -- Avoid cascaded errors if the entity appears in
10522 -- subsequent calls.
10524 Set_Scope (S, Current_Scope);
10526 -- Generate error, with extra useful warning for the case
10527 -- of a generic instance with no completion.
10529 if Is_Generic_Instance (S)
10530 and then not Has_Completion (E)
10533 ("instantiation cannot provide body for&", S);
10534 Error_Msg_N ("\& conflicts with declaration#", S);
10536 Error_Msg_N ("& conflicts with declaration#", S);
10543 -- If one subprogram has an access parameter and the other
10544 -- a parameter of an access type, calls to either might be
10545 -- ambiguous. Verify that parameters match except for the
10546 -- access parameter.
10548 if May_Hide_Profile then
10554 F1 := First_Formal (S);
10555 F2 := First_Formal (E);
10556 while Present (F1) and then Present (F2) loop
10557 if Is_Access_Type (Etype (F1)) then
10558 if not Is_Access_Type (Etype (F2))
10559 or else not Conforming_Types
10560 (Designated_Type (Etype (F1)),
10561 Designated_Type (Etype (F2)),
10564 May_Hide_Profile := False;
10568 not Conforming_Types
10569 (Etype (F1), Etype (F2), Type_Conformant)
10571 May_Hide_Profile := False;
10578 if May_Hide_Profile
10582 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10591 -- On exit, we know that S is a new entity
10593 Enter_Overloaded_Entity (S);
10594 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10595 Check_Overriding_Indicator
10596 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10598 -- The Ghost policy in effect at the point of declaration of a parent
10599 -- subprogram and an overriding subprogram must match
10600 -- (SPARK RM 6.9(17)).
10602 Check_Ghost_Overriding (S, Overridden_Subp);
10604 -- Overloading is not allowed in SPARK, except for operators
10606 if Nkind (S) /= N_Defining_Operator_Symbol then
10607 Error_Msg_Sloc := Sloc (Homonym (S));
10608 Check_SPARK_05_Restriction
10609 ("overloading not allowed with entity#", S);
10612 -- If S is a derived operation for an untagged type then by
10613 -- definition it's not a dispatching operation (even if the parent
10614 -- operation was dispatching), so Check_Dispatching_Operation is not
10615 -- called in that case.
10617 if No (Derived_Type)
10618 or else Is_Tagged_Type (Derived_Type)
10620 Check_Dispatching_Operation (S, Empty);
10624 -- If this is a user-defined equality operator that is not a derived
10625 -- subprogram, create the corresponding inequality. If the operation is
10626 -- dispatching, the expansion is done elsewhere, and we do not create
10627 -- an explicit inequality operation.
10629 <<Check_Inequality>>
10630 if Chars (S) = Name_Op_Eq
10631 and then Etype (S) = Standard_Boolean
10632 and then Present (Parent (S))
10633 and then not Is_Dispatching_Operation (S)
10635 Make_Inequality_Operator (S);
10636 Check_Untagged_Equality (S);
10638 end New_Overloaded_Entity;
10640 ---------------------
10641 -- Process_Formals --
10642 ---------------------
10644 procedure Process_Formals
10646 Related_Nod : Node_Id)
10648 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10649 -- Determine whether an access type designates a type coming from a
10652 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10653 -- Check whether the default has a class-wide type. After analysis the
10654 -- default has the type of the formal, so we must also check explicitly
10655 -- for an access attribute.
10657 ----------------------------------
10658 -- Designates_From_Limited_With --
10659 ----------------------------------
10661 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10662 Desig : Entity_Id := Typ;
10665 if Is_Access_Type (Desig) then
10666 Desig := Directly_Designated_Type (Desig);
10669 if Is_Class_Wide_Type (Desig) then
10670 Desig := Root_Type (Desig);
10674 Ekind (Desig) = E_Incomplete_Type
10675 and then From_Limited_With (Desig);
10676 end Designates_From_Limited_With;
10678 ---------------------------
10679 -- Is_Class_Wide_Default --
10680 ---------------------------
10682 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10684 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10685 or else (Nkind (D) = N_Attribute_Reference
10686 and then Attribute_Name (D) = Name_Access
10687 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10688 end Is_Class_Wide_Default;
10692 Context : constant Node_Id := Parent (Parent (T));
10694 Formal : Entity_Id;
10695 Formal_Type : Entity_Id;
10696 Param_Spec : Node_Id;
10699 Num_Out_Params : Nat := 0;
10700 First_Out_Param : Entity_Id := Empty;
10701 -- Used for setting Is_Only_Out_Parameter
10703 -- Start of processing for Process_Formals
10706 -- In order to prevent premature use of the formals in the same formal
10707 -- part, the Ekind is left undefined until all default expressions are
10708 -- analyzed. The Ekind is established in a separate loop at the end.
10710 Param_Spec := First (T);
10711 while Present (Param_Spec) loop
10712 Formal := Defining_Identifier (Param_Spec);
10713 Set_Never_Set_In_Source (Formal, True);
10714 Enter_Name (Formal);
10716 -- Case of ordinary parameters
10718 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10719 Find_Type (Parameter_Type (Param_Spec));
10720 Ptype := Parameter_Type (Param_Spec);
10722 if Ptype = Error then
10726 Formal_Type := Entity (Ptype);
10728 if Is_Incomplete_Type (Formal_Type)
10730 (Is_Class_Wide_Type (Formal_Type)
10731 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10733 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10734 -- primitive operations, as long as their completion is
10735 -- in the same declarative part. If in the private part
10736 -- this means that the type cannot be a Taft-amendment type.
10737 -- Check is done on package exit. For access to subprograms,
10738 -- the use is legal for Taft-amendment types.
10740 -- Ada 2012: tagged incomplete types are allowed as generic
10741 -- formal types. They do not introduce dependencies and the
10742 -- corresponding generic subprogram does not have a delayed
10743 -- freeze, because it does not need a freeze node. However,
10744 -- it is still the case that untagged incomplete types cannot
10745 -- be Taft-amendment types and must be completed in private
10746 -- part, so the subprogram must appear in the list of private
10747 -- dependents of the type.
10749 if Is_Tagged_Type (Formal_Type)
10750 or else (Ada_Version >= Ada_2012
10751 and then not From_Limited_With (Formal_Type)
10752 and then not Is_Generic_Type (Formal_Type))
10754 if Ekind (Scope (Current_Scope)) = E_Package
10755 and then not Is_Generic_Type (Formal_Type)
10756 and then not Is_Class_Wide_Type (Formal_Type)
10759 (Parent (T), N_Access_Function_Definition,
10760 N_Access_Procedure_Definition)
10762 Append_Elmt (Current_Scope,
10763 Private_Dependents (Base_Type (Formal_Type)));
10765 -- Freezing is delayed to ensure that Register_Prim
10766 -- will get called for this operation, which is needed
10767 -- in cases where static dispatch tables aren't built.
10768 -- (Note that the same is done for controlling access
10769 -- parameter cases in function Access_Definition.)
10771 if not Is_Thunk (Current_Scope) then
10772 Set_Has_Delayed_Freeze (Current_Scope);
10777 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10778 N_Access_Procedure_Definition)
10780 -- AI05-0151: Tagged incomplete types are allowed in all
10781 -- formal parts. Untagged incomplete types are not allowed
10782 -- in bodies. Limited views of either kind are not allowed
10783 -- if there is no place at which the non-limited view can
10784 -- become available.
10786 -- Incomplete formal untagged types are not allowed in
10787 -- subprogram bodies (but are legal in their declarations).
10788 -- This excludes bodies created for null procedures, which
10789 -- are basic declarations.
10791 if Is_Generic_Type (Formal_Type)
10792 and then not Is_Tagged_Type (Formal_Type)
10793 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10796 ("invalid use of formal incomplete type", Param_Spec);
10798 elsif Ada_Version >= Ada_2012 then
10799 if Is_Tagged_Type (Formal_Type)
10800 and then (not From_Limited_With (Formal_Type)
10801 or else not In_Package_Body)
10805 elsif Nkind_In (Context, N_Accept_Statement,
10806 N_Accept_Alternative,
10808 or else (Nkind (Context) = N_Subprogram_Body
10809 and then Comes_From_Source (Context))
10812 ("invalid use of untagged incomplete type &",
10813 Ptype, Formal_Type);
10818 ("invalid use of incomplete type&",
10819 Param_Spec, Formal_Type);
10821 -- Further checks on the legality of incomplete types
10822 -- in formal parts are delayed until the freeze point
10823 -- of the enclosing subprogram or access to subprogram.
10827 elsif Ekind (Formal_Type) = E_Void then
10829 ("premature use of&",
10830 Parameter_Type (Param_Spec), Formal_Type);
10833 -- Ada 2012 (AI-142): Handle aliased parameters
10835 if Ada_Version >= Ada_2012
10836 and then Aliased_Present (Param_Spec)
10838 Set_Is_Aliased (Formal);
10841 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10842 -- declaration corresponding to the null-excluding type of the
10843 -- formal in the enclosing scope. Finally, replace the parameter
10844 -- type of the formal with the internal subtype.
10846 if Ada_Version >= Ada_2005
10847 and then Null_Exclusion_Present (Param_Spec)
10849 if not Is_Access_Type (Formal_Type) then
10851 ("`NOT NULL` allowed only for an access type", Param_Spec);
10854 if Can_Never_Be_Null (Formal_Type)
10855 and then Comes_From_Source (Related_Nod)
10858 ("`NOT NULL` not allowed (& already excludes null)",
10859 Param_Spec, Formal_Type);
10863 Create_Null_Excluding_Itype
10865 Related_Nod => Related_Nod,
10866 Scope_Id => Scope (Current_Scope));
10868 -- If the designated type of the itype is an itype that is
10869 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10870 -- on the access subtype, to prevent order-of-elaboration
10871 -- issues in the backend.
10874 -- type T is access procedure;
10875 -- procedure Op (O : not null T);
10877 if Is_Itype (Directly_Designated_Type (Formal_Type))
10879 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10881 Set_Has_Delayed_Freeze (Formal_Type);
10886 -- An access formal type
10890 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10892 -- No need to continue if we already notified errors
10894 if not Present (Formal_Type) then
10898 -- Ada 2005 (AI-254)
10901 AD : constant Node_Id :=
10902 Access_To_Subprogram_Definition
10903 (Parameter_Type (Param_Spec));
10905 if Present (AD) and then Protected_Present (AD) then
10907 Replace_Anonymous_Access_To_Protected_Subprogram
10913 Set_Etype (Formal, Formal_Type);
10915 -- Deal with default expression if present
10917 Default := Expression (Param_Spec);
10919 if Present (Default) then
10920 Check_SPARK_05_Restriction
10921 ("default expression is not allowed", Default);
10923 if Out_Present (Param_Spec) then
10925 ("default initialization only allowed for IN parameters",
10929 -- Do the special preanalysis of the expression (see section on
10930 -- "Handling of Default Expressions" in the spec of package Sem).
10932 Preanalyze_Spec_Expression (Default, Formal_Type);
10934 -- An access to constant cannot be the default for
10935 -- an access parameter that is an access to variable.
10937 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10938 and then not Is_Access_Constant (Formal_Type)
10939 and then Is_Access_Type (Etype (Default))
10940 and then Is_Access_Constant (Etype (Default))
10943 ("formal that is access to variable cannot be initialized "
10944 & "with an access-to-constant expression", Default);
10947 -- Check that the designated type of an access parameter's default
10948 -- is not a class-wide type unless the parameter's designated type
10949 -- is also class-wide.
10951 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10952 and then not Designates_From_Limited_With (Formal_Type)
10953 and then Is_Class_Wide_Default (Default)
10954 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10957 ("access to class-wide expression not allowed here", Default);
10960 -- Check incorrect use of dynamically tagged expressions
10962 if Is_Tagged_Type (Formal_Type) then
10963 Check_Dynamically_Tagged_Expression
10965 Typ => Formal_Type,
10966 Related_Nod => Default);
10970 -- Ada 2005 (AI-231): Static checks
10972 if Ada_Version >= Ada_2005
10973 and then Is_Access_Type (Etype (Formal))
10974 and then Can_Never_Be_Null (Etype (Formal))
10976 Null_Exclusion_Static_Checks (Param_Spec);
10979 -- The following checks are relevant only when SPARK_Mode is on as
10980 -- these are not standard Ada legality rules.
10982 if SPARK_Mode = On then
10983 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10985 -- A function cannot have a parameter of mode IN OUT or OUT
10988 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10990 ("function cannot have parameter of mode `OUT` or "
10991 & "`IN OUT`", Formal);
10994 -- A procedure cannot have an effectively volatile formal
10995 -- parameter of mode IN because it behaves as a constant
10996 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
10998 elsif Ekind (Scope (Formal)) = E_Procedure
10999 and then Ekind (Formal) = E_In_Parameter
11000 and then Is_Effectively_Volatile (Formal)
11003 ("formal parameter of mode `IN` cannot be volatile", Formal);
11011 -- If this is the formal part of a function specification, analyze the
11012 -- subtype mark in the context where the formals are visible but not
11013 -- yet usable, and may hide outer homographs.
11015 if Nkind (Related_Nod) = N_Function_Specification then
11016 Analyze_Return_Type (Related_Nod);
11019 -- Now set the kind (mode) of each formal
11021 Param_Spec := First (T);
11022 while Present (Param_Spec) loop
11023 Formal := Defining_Identifier (Param_Spec);
11024 Set_Formal_Mode (Formal);
11026 if Ekind (Formal) = E_In_Parameter then
11027 Set_Default_Value (Formal, Expression (Param_Spec));
11029 if Present (Expression (Param_Spec)) then
11030 Default := Expression (Param_Spec);
11032 if Is_Scalar_Type (Etype (Default)) then
11033 if Nkind (Parameter_Type (Param_Spec)) /=
11034 N_Access_Definition
11036 Formal_Type := Entity (Parameter_Type (Param_Spec));
11040 (Related_Nod, Parameter_Type (Param_Spec));
11043 Apply_Scalar_Range_Check (Default, Formal_Type);
11047 elsif Ekind (Formal) = E_Out_Parameter then
11048 Num_Out_Params := Num_Out_Params + 1;
11050 if Num_Out_Params = 1 then
11051 First_Out_Param := Formal;
11054 elsif Ekind (Formal) = E_In_Out_Parameter then
11055 Num_Out_Params := Num_Out_Params + 1;
11058 -- Skip remaining processing if formal type was in error
11060 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11061 goto Next_Parameter;
11064 -- Force call by reference if aliased
11067 Conv : constant Convention_Id := Convention (Etype (Formal));
11069 if Is_Aliased (Formal) then
11070 Set_Mechanism (Formal, By_Reference);
11072 -- Warn if user asked this to be passed by copy
11074 if Conv = Convention_Ada_Pass_By_Copy then
11076 ("cannot pass aliased parameter & by copy??", Formal);
11079 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11081 elsif Conv = Convention_Ada_Pass_By_Copy then
11082 Set_Mechanism (Formal, By_Copy);
11084 elsif Conv = Convention_Ada_Pass_By_Reference then
11085 Set_Mechanism (Formal, By_Reference);
11093 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11094 Set_Is_Only_Out_Parameter (First_Out_Param);
11096 end Process_Formals;
11098 ----------------------------
11099 -- Reference_Body_Formals --
11100 ----------------------------
11102 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11107 if Error_Posted (Spec) then
11111 -- Iterate over both lists. They may be of different lengths if the two
11112 -- specs are not conformant.
11114 Fs := First_Formal (Spec);
11115 Fb := First_Formal (Bod);
11116 while Present (Fs) and then Present (Fb) loop
11117 Generate_Reference (Fs, Fb, 'b');
11119 if Style_Check then
11120 Style.Check_Identifier (Fb, Fs);
11123 Set_Spec_Entity (Fb, Fs);
11124 Set_Referenced (Fs, False);
11128 end Reference_Body_Formals;
11130 -------------------------
11131 -- Set_Actual_Subtypes --
11132 -------------------------
11134 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11136 Formal : Entity_Id;
11138 First_Stmt : Node_Id := Empty;
11139 AS_Needed : Boolean;
11142 -- If this is an empty initialization procedure, no need to create
11143 -- actual subtypes (small optimization).
11145 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11149 Formal := First_Formal (Subp);
11150 while Present (Formal) loop
11151 T := Etype (Formal);
11153 -- We never need an actual subtype for a constrained formal
11155 if Is_Constrained (T) then
11156 AS_Needed := False;
11158 -- If we have unknown discriminants, then we do not need an actual
11159 -- subtype, or more accurately we cannot figure it out. Note that
11160 -- all class-wide types have unknown discriminants.
11162 elsif Has_Unknown_Discriminants (T) then
11163 AS_Needed := False;
11165 -- At this stage we have an unconstrained type that may need an
11166 -- actual subtype. For sure the actual subtype is needed if we have
11167 -- an unconstrained array type.
11169 elsif Is_Array_Type (T) then
11172 -- The only other case needing an actual subtype is an unconstrained
11173 -- record type which is an IN parameter (we cannot generate actual
11174 -- subtypes for the OUT or IN OUT case, since an assignment can
11175 -- change the discriminant values. However we exclude the case of
11176 -- initialization procedures, since discriminants are handled very
11177 -- specially in this context, see the section entitled "Handling of
11178 -- Discriminants" in Einfo.
11180 -- We also exclude the case of Discrim_SO_Functions (functions used
11181 -- in front end layout mode for size/offset values), since in such
11182 -- functions only discriminants are referenced, and not only are such
11183 -- subtypes not needed, but they cannot always be generated, because
11184 -- of order of elaboration issues.
11186 elsif Is_Record_Type (T)
11187 and then Ekind (Formal) = E_In_Parameter
11188 and then Chars (Formal) /= Name_uInit
11189 and then not Is_Unchecked_Union (T)
11190 and then not Is_Discrim_SO_Function (Subp)
11194 -- All other cases do not need an actual subtype
11197 AS_Needed := False;
11200 -- Generate actual subtypes for unconstrained arrays and
11201 -- unconstrained discriminated records.
11204 if Nkind (N) = N_Accept_Statement then
11206 -- If expansion is active, the formal is replaced by a local
11207 -- variable that renames the corresponding entry of the
11208 -- parameter block, and it is this local variable that may
11209 -- require an actual subtype.
11211 if Expander_Active then
11212 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11214 Decl := Build_Actual_Subtype (T, Formal);
11217 if Present (Handled_Statement_Sequence (N)) then
11219 First (Statements (Handled_Statement_Sequence (N)));
11220 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11221 Mark_Rewrite_Insertion (Decl);
11223 -- If the accept statement has no body, there will be no
11224 -- reference to the actuals, so no need to compute actual
11231 Decl := Build_Actual_Subtype (T, Formal);
11232 Prepend (Decl, Declarations (N));
11233 Mark_Rewrite_Insertion (Decl);
11236 -- The declaration uses the bounds of an existing object, and
11237 -- therefore needs no constraint checks.
11239 Analyze (Decl, Suppress => All_Checks);
11241 -- We need to freeze manually the generated type when it is
11242 -- inserted anywhere else than in a declarative part.
11244 if Present (First_Stmt) then
11245 Insert_List_Before_And_Analyze (First_Stmt,
11246 Freeze_Entity (Defining_Identifier (Decl), N));
11248 -- Ditto if the type has a dynamic predicate, because the
11249 -- generated function will mention the actual subtype.
11251 elsif Has_Dynamic_Predicate_Aspect (T) then
11252 Insert_List_Before_And_Analyze (Decl,
11253 Freeze_Entity (Defining_Identifier (Decl), N));
11256 if Nkind (N) = N_Accept_Statement
11257 and then Expander_Active
11259 Set_Actual_Subtype (Renamed_Object (Formal),
11260 Defining_Identifier (Decl));
11262 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11266 Next_Formal (Formal);
11268 end Set_Actual_Subtypes;
11270 ---------------------
11271 -- Set_Formal_Mode --
11272 ---------------------
11274 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11275 Spec : constant Node_Id := Parent (Formal_Id);
11276 Id : constant Entity_Id := Scope (Formal_Id);
11279 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11280 -- since we ensure that corresponding actuals are always valid at the
11281 -- point of the call.
11283 if Out_Present (Spec) then
11284 if Ekind_In (Id, E_Entry, E_Entry_Family)
11285 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11287 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11290 if Ekind_In (Id, E_Function, E_Generic_Function) then
11292 -- [IN] OUT parameters allowed for functions in Ada 2012
11294 if Ada_Version >= Ada_2012 then
11296 -- Even in Ada 2012 operators can only have IN parameters
11298 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11299 Error_Msg_N ("operators can only have IN parameters", Spec);
11302 if In_Present (Spec) then
11303 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11305 Set_Ekind (Formal_Id, E_Out_Parameter);
11308 -- But not in earlier versions of Ada
11311 Error_Msg_N ("functions can only have IN parameters", Spec);
11312 Set_Ekind (Formal_Id, E_In_Parameter);
11315 elsif In_Present (Spec) then
11316 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11319 Set_Ekind (Formal_Id, E_Out_Parameter);
11320 Set_Never_Set_In_Source (Formal_Id, True);
11321 Set_Is_True_Constant (Formal_Id, False);
11322 Set_Current_Value (Formal_Id, Empty);
11326 Set_Ekind (Formal_Id, E_In_Parameter);
11329 -- Set Is_Known_Non_Null for access parameters since the language
11330 -- guarantees that access parameters are always non-null. We also set
11331 -- Can_Never_Be_Null, since there is no way to change the value.
11333 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11335 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11336 -- null; In Ada 2005, only if then null_exclusion is explicit.
11338 if Ada_Version < Ada_2005
11339 or else Can_Never_Be_Null (Etype (Formal_Id))
11341 Set_Is_Known_Non_Null (Formal_Id);
11342 Set_Can_Never_Be_Null (Formal_Id);
11345 -- Ada 2005 (AI-231): Null-exclusion access subtype
11347 elsif Is_Access_Type (Etype (Formal_Id))
11348 and then Can_Never_Be_Null (Etype (Formal_Id))
11350 Set_Is_Known_Non_Null (Formal_Id);
11352 -- We can also set Can_Never_Be_Null (thus preventing some junk
11353 -- access checks) for the case of an IN parameter, which cannot
11354 -- be changed, or for an IN OUT parameter, which can be changed but
11355 -- not to a null value. But for an OUT parameter, the initial value
11356 -- passed in can be null, so we can't set this flag in that case.
11358 if Ekind (Formal_Id) /= E_Out_Parameter then
11359 Set_Can_Never_Be_Null (Formal_Id);
11363 Set_Mechanism (Formal_Id, Default_Mechanism);
11364 Set_Formal_Validity (Formal_Id);
11365 end Set_Formal_Mode;
11367 -------------------------
11368 -- Set_Formal_Validity --
11369 -------------------------
11371 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11373 -- If no validity checking, then we cannot assume anything about the
11374 -- validity of parameters, since we do not know there is any checking
11375 -- of the validity on the call side.
11377 if not Validity_Checks_On then
11380 -- If validity checking for parameters is enabled, this means we are
11381 -- not supposed to make any assumptions about argument values.
11383 elsif Validity_Check_Parameters then
11386 -- If we are checking in parameters, we will assume that the caller is
11387 -- also checking parameters, so we can assume the parameter is valid.
11389 elsif Ekind (Formal_Id) = E_In_Parameter
11390 and then Validity_Check_In_Params
11392 Set_Is_Known_Valid (Formal_Id, True);
11394 -- Similar treatment for IN OUT parameters
11396 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11397 and then Validity_Check_In_Out_Params
11399 Set_Is_Known_Valid (Formal_Id, True);
11401 end Set_Formal_Validity;
11403 ------------------------
11404 -- Subtype_Conformant --
11405 ------------------------
11407 function Subtype_Conformant
11408 (New_Id : Entity_Id;
11409 Old_Id : Entity_Id;
11410 Skip_Controlling_Formals : Boolean := False) return Boolean
11414 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11415 Skip_Controlling_Formals => Skip_Controlling_Formals);
11417 end Subtype_Conformant;
11419 ---------------------
11420 -- Type_Conformant --
11421 ---------------------
11423 function Type_Conformant
11424 (New_Id : Entity_Id;
11425 Old_Id : Entity_Id;
11426 Skip_Controlling_Formals : Boolean := False) return Boolean
11430 May_Hide_Profile := False;
11432 (New_Id, Old_Id, Type_Conformant, False, Result,
11433 Skip_Controlling_Formals => Skip_Controlling_Formals);
11435 end Type_Conformant;
11437 -------------------------------
11438 -- Valid_Operator_Definition --
11439 -------------------------------
11441 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11444 Id : constant Name_Id := Chars (Designator);
11448 F := First_Formal (Designator);
11449 while Present (F) loop
11452 if Present (Default_Value (F)) then
11454 ("default values not allowed for operator parameters",
11457 -- For function instantiations that are operators, we must check
11458 -- separately that the corresponding generic only has in-parameters.
11459 -- For subprogram declarations this is done in Set_Formal_Mode. Such
11460 -- an error could not arise in earlier versions of the language.
11462 elsif Ekind (F) /= E_In_Parameter then
11463 Error_Msg_N ("operators can only have IN parameters", F);
11469 -- Verify that user-defined operators have proper number of arguments
11470 -- First case of operators which can only be unary
11472 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11475 -- Case of operators which can be unary or binary
11477 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11478 N_OK := (N in 1 .. 2);
11480 -- All other operators can only be binary
11488 ("incorrect number of arguments for operator", Designator);
11492 and then Base_Type (Etype (Designator)) = Standard_Boolean
11493 and then not Is_Intrinsic_Subprogram (Designator)
11496 ("explicit definition of inequality not allowed", Designator);
11498 end Valid_Operator_Definition;