1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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_Ch9; use Sem_Ch9;
66 with Sem_Ch10; use Sem_Ch10;
67 with Sem_Ch12; use Sem_Ch12;
68 with Sem_Ch13; use Sem_Ch13;
69 with Sem_Dim; use Sem_Dim;
70 with Sem_Disp; use Sem_Disp;
71 with Sem_Dist; use Sem_Dist;
72 with Sem_Elim; use Sem_Elim;
73 with Sem_Eval; use Sem_Eval;
74 with Sem_Mech; use Sem_Mech;
75 with Sem_Prag; use Sem_Prag;
76 with Sem_Res; use Sem_Res;
77 with Sem_Util; use Sem_Util;
78 with Sem_Type; use Sem_Type;
79 with Sem_Warn; use Sem_Warn;
80 with Sinput; use Sinput;
81 with Stand; use Stand;
82 with Sinfo; use Sinfo;
83 with Sinfo.CN; use Sinfo.CN;
84 with Snames; use Snames;
85 with Stringt; use Stringt;
87 with Stylesw; use Stylesw;
88 with Tbuild; use Tbuild;
89 with Uintp; use Uintp;
90 with Urealp; use Urealp;
91 with Validsw; use Validsw;
93 package body Sem_Ch6 is
95 May_Hide_Profile : Boolean := False;
96 -- This flag is used to indicate that two formals in two subprograms being
97 -- checked for conformance differ only in that one is an access parameter
98 -- while the other is of a general access type with the same designated
99 -- type. In this case, if the rest of the signatures match, a call to
100 -- either subprogram may be ambiguous, which is worth a warning. The flag
101 -- is set in Compatible_Types, and the warning emitted in
102 -- New_Overloaded_Entity.
104 -----------------------
105 -- Local Subprograms --
106 -----------------------
108 procedure Analyze_Function_Return (N : Node_Id);
109 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
110 -- applies to a [generic] function.
112 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
113 -- Analyze a generic subprogram body. N is the body to be analyzed, and
114 -- Gen_Id is the defining entity Id for the corresponding spec.
116 procedure Analyze_Null_Procedure
118 Is_Completion : out Boolean);
119 -- A null procedure can be a declaration or (Ada 2012) a completion
121 procedure Analyze_Return_Statement (N : Node_Id);
122 -- Common processing for simple and extended return statements
124 procedure Analyze_Return_Type (N : Node_Id);
125 -- Subsidiary to Process_Formals: analyze subtype mark in function
126 -- specification in a context where the formals are visible and hide
129 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
130 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
131 -- that we can use RETURN but not skip the debug output at the end.
133 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
134 -- Returns true if Subp can override a predefined operator.
136 procedure Check_Conformance
139 Ctype : Conformance_Type;
141 Conforms : out Boolean;
142 Err_Loc : Node_Id := Empty;
143 Get_Inst : Boolean := False;
144 Skip_Controlling_Formals : Boolean := False);
145 -- Given two entities, this procedure checks that the profiles associated
146 -- with these entities meet the conformance criterion given by the third
147 -- parameter. If they conform, Conforms is set True and control returns
148 -- to the caller. If they do not conform, Conforms is set to False, and
149 -- in addition, if Errmsg is True on the call, proper messages are output
150 -- to complain about the conformance failure. If Err_Loc is non_Empty
151 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
152 -- error messages are placed on the appropriate part of the construct
153 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
154 -- against a formal access-to-subprogram type so Get_Instance_Of must
157 procedure Check_Limited_Return
161 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
162 -- types. Used only for simple return statements. Expr is the expression
165 procedure Check_Subprogram_Order (N : Node_Id);
166 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
167 -- the alpha ordering rule for N if this ordering requirement applicable.
169 procedure Check_Returns
173 Proc : Entity_Id := Empty);
174 -- Called to check for missing return statements in a function body, or for
175 -- returns present in a procedure body which has No_Return set. HSS is the
176 -- handled statement sequence for the subprogram body. This procedure
177 -- checks all flow paths to make sure they either have return (Mode = 'F',
178 -- used for functions) or do not have a return (Mode = 'P', used for
179 -- No_Return procedures). The flag Err is set if there are any control
180 -- paths not explicitly terminated by a return in the function case, and is
181 -- True otherwise. Proc is the entity for the procedure case and is used
182 -- in posting the warning message.
184 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
185 -- In Ada 2012, a primitive equality operator on an untagged record type
186 -- must appear before the type is frozen, and have the same visibility as
187 -- that of the type. This procedure checks that this rule is met, and
188 -- otherwise emits an error on the subprogram declaration and a warning
189 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
190 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
191 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
192 -- is set, otherwise the call has no effect.
194 procedure Enter_Overloaded_Entity (S : Entity_Id);
195 -- This procedure makes S, a new overloaded entity, into the first visible
196 -- entity with that name.
198 function Is_Non_Overriding_Operation
200 New_E : Entity_Id) return Boolean;
201 -- Enforce the rule given in 12.3(18): a private operation in an instance
202 -- overrides an inherited operation only if the corresponding operation
203 -- was overriding in the generic. This needs to be checked for primitive
204 -- operations of types derived (in the generic unit) from formal private
205 -- or formal derived types.
207 procedure Make_Inequality_Operator (S : Entity_Id);
208 -- Create the declaration for an inequality operator that is implicitly
209 -- created by a user-defined equality operator that yields a boolean.
211 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
212 -- Formal_Id is an formal parameter entity. This procedure deals with
213 -- setting the proper validity status for this entity, which depends on
214 -- the kind of parameter and the validity checking mode.
216 ---------------------------------------------
217 -- Analyze_Abstract_Subprogram_Declaration --
218 ---------------------------------------------
220 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
221 Scop : constant Entity_Id := Current_Scope;
222 Subp_Id : constant Entity_Id :=
223 Analyze_Subprogram_Specification (Specification (N));
226 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
228 Generate_Definition (Subp_Id);
230 Set_Is_Abstract_Subprogram (Subp_Id);
231 New_Overloaded_Entity (Subp_Id);
232 Check_Delayed_Subprogram (Subp_Id);
234 Set_Categorization_From_Scope (Subp_Id, Scop);
236 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
237 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
239 -- Issue a warning if the abstract subprogram is neither a dispatching
240 -- operation nor an operation that overrides an inherited subprogram or
241 -- predefined operator, since this most likely indicates a mistake.
243 elsif Warn_On_Redundant_Constructs
244 and then not Is_Dispatching_Operation (Subp_Id)
245 and then not Present (Overridden_Operation (Subp_Id))
246 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
247 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
250 ("abstract subprogram is not dispatching or overriding?r?", N);
253 Generate_Reference_To_Formals (Subp_Id);
254 Check_Eliminated (Subp_Id);
256 if Has_Aspects (N) then
257 Analyze_Aspect_Specifications (N, Subp_Id);
259 end Analyze_Abstract_Subprogram_Declaration;
261 ---------------------------------
262 -- Analyze_Expression_Function --
263 ---------------------------------
265 procedure Analyze_Expression_Function (N : Node_Id) is
266 Expr : constant Node_Id := Expression (N);
267 Loc : constant Source_Ptr := Sloc (N);
268 LocX : constant Source_Ptr := Sloc (Expr);
269 Spec : constant Node_Id := Specification (N);
277 Ret_Type : Entity_Id;
280 -- If the expression is a completion, Prev is the entity whose
281 -- declaration is completed. Def_Id is needed to analyze the spec.
284 -- This is one of the occasions on which we transform the tree during
285 -- semantic analysis. If this is a completion, transform the expression
286 -- function into an equivalent subprogram body, and analyze it.
288 -- Expression functions are inlined unconditionally. The back-end will
289 -- determine whether this is possible.
291 Inline_Processing_Required := True;
293 -- Create a specification for the generated body. This must be done
294 -- prior to the analysis of the initial declaration.
296 New_Spec := Copy_Subprogram_Spec (Spec);
297 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
299 -- If there are previous overloadable entities with the same name,
300 -- check whether any of them is completed by the expression function.
301 -- In a generic context a formal subprogram has no completion.
304 and then Is_Overloadable (Prev)
305 and then not Is_Formal_Subprogram (Prev)
307 Def_Id := Analyze_Subprogram_Specification (Spec);
308 Prev := Find_Corresponding_Spec (N);
310 -- The previous entity may be an expression function as well, in
311 -- which case the redeclaration is illegal.
314 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
315 N_Expression_Function
317 Error_Msg_Sloc := Sloc (Prev);
318 Error_Msg_N ("& conflicts with declaration#", Def_Id);
323 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
326 Make_Subprogram_Body (Loc,
327 Specification => New_Spec,
328 Declarations => Empty_List,
329 Handled_Statement_Sequence =>
330 Make_Handled_Sequence_Of_Statements (LocX,
331 Statements => New_List (Ret)));
332 Set_Was_Expression_Function (New_Body);
334 -- If the expression completes a generic subprogram, we must create a
335 -- separate node for the body, because at instantiation the original
336 -- node of the generic copy must be a generic subprogram body, and
337 -- cannot be a expression function. Otherwise we just rewrite the
338 -- expression with the non-generic body.
340 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
341 Insert_After (N, New_Body);
343 -- Propagate any aspects or pragmas that apply to the expression
344 -- function to the proper body when the expression function acts
347 if Has_Aspects (N) then
348 Move_Aspects (N, To => New_Body);
351 Relocate_Pragmas_To_Body (New_Body);
353 Rewrite (N, Make_Null_Statement (Loc));
354 Set_Has_Completion (Prev, False);
357 Set_Is_Inlined (Prev);
359 -- If the expression function is a completion, the previous declaration
360 -- must come from source. We know already that it appears in the current
361 -- scope. The entity itself may be internally created if within a body
365 and then Comes_From_Source (Parent (Prev))
366 and then not Is_Formal_Subprogram (Prev)
368 Set_Has_Completion (Prev, False);
369 Set_Is_Inlined (Prev);
370 Ret_Type := Etype (Prev);
372 -- An expression function which acts as a completion freezes the
373 -- expression. This means freezing the return type, and if it is
374 -- an access type, freezing its designated type as well.
376 -- Note that we cannot defer this freezing to the analysis of the
377 -- expression itself, because a freeze node might appear in a nested
378 -- scope, leading to an elaboration order issue in gigi.
380 Freeze_Before (N, Ret_Type);
382 -- An entity can only be frozen if it is complete, so if the type
383 -- is still unfrozen it must still be incomplete in some way, e.g.
384 -- a private type without a full view, or a type derived from such
385 -- in an enclosing scope. Except in a generic context (where the
386 -- type may be a generic formal or derived from such), such use of
387 -- an incomplete type is an error. On the other hand, if this is a
388 -- limited view of a type, the type is declared in another unit and
389 -- frozen there. We must be in a context seeing the nonlimited view
390 -- of the type, which will be installed when the body is compiled.
392 if not Is_Frozen (Ret_Type)
393 and then not Is_Generic_Type (Root_Type (Ret_Type))
394 and then not Inside_A_Generic
396 if From_Limited_With (Ret_Type)
397 and then Present (Non_Limited_View (Ret_Type))
402 ("premature use of private type&",
403 Result_Definition (Specification (N)), Ret_Type);
407 -- For navigation purposes, indicate that the function is a body
409 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
410 Rewrite (N, New_Body);
412 -- Remove any existing aspects from the original node because the act
413 -- of rewriting causes the list to be shared between the two nodes.
415 Orig_N := Original_Node (N);
416 Remove_Aspects (Orig_N);
418 -- Propagate any pragmas that apply to expression function to the
419 -- proper body when the expression function acts as a completion.
420 -- Aspects are automatically transfered because of node rewriting.
422 Relocate_Pragmas_To_Body (N);
425 -- Once the aspects of the generated body have been analyzed, create
426 -- a copy for ASIS purposes and associate it with the original node.
428 if Has_Aspects (N) then
429 Set_Aspect_Specifications (Orig_N,
430 New_Copy_List_Tree (Aspect_Specifications (N)));
433 -- Prev is the previous entity with the same name, but it is can
434 -- be an unrelated spec that is not completed by the expression
435 -- function. In that case the relevant entity is the one in the body.
436 -- Not clear that the backend can inline it in this case ???
438 if Has_Completion (Prev) then
440 -- The formals of the expression function are body formals,
441 -- and do not appear in the ali file, which will only contain
442 -- references to the formals of the original subprogram spec.
449 F1 := First_Formal (Def_Id);
450 F2 := First_Formal (Prev);
452 while Present (F1) loop
453 Set_Spec_Entity (F1, F2);
460 Set_Is_Inlined (Defining_Entity (New_Body));
463 -- If this is not a completion, create both a declaration and a body, so
464 -- that the expression can be inlined whenever possible.
467 -- An expression function that is not a completion is not a
468 -- subprogram declaration, and thus cannot appear in a protected
471 if Nkind (Parent (N)) = N_Protected_Definition then
473 ("an expression function is not a legal protected operation", N);
476 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
478 -- Remove any existing aspects from the original node because the act
479 -- of rewriting causes the list to be shared between the two nodes.
481 Orig_N := Original_Node (N);
482 Remove_Aspects (Orig_N);
486 -- Once the aspects of the generated spec have been analyzed, create
487 -- a copy for ASIS purposes and associate it with the original node.
489 if Has_Aspects (N) then
490 Set_Aspect_Specifications (Orig_N,
491 New_Copy_List_Tree (Aspect_Specifications (N)));
494 -- If aspect SPARK_Mode was specified on the body, it needs to be
495 -- repeated both on the generated spec and the body.
497 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
499 if Present (Asp) then
500 Asp := New_Copy_Tree (Asp);
501 Set_Analyzed (Asp, False);
502 Set_Aspect_Specifications (New_Body, New_List (Asp));
505 Def_Id := Defining_Entity (N);
507 -- Within a generic pre-analyze the original expression for name
508 -- capture. The body is also generated but plays no role in
509 -- this because it is not part of the original source.
511 if Inside_A_Generic then
512 Set_Has_Completion (Def_Id);
514 Install_Formals (Def_Id);
515 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
519 Set_Is_Inlined (Defining_Entity (N));
521 -- Establish the linkages between the spec and the body. These are
522 -- used when the expression function acts as the prefix of attribute
523 -- 'Access in order to freeze the original expression which has been
524 -- moved to the generated body.
526 Set_Corresponding_Body (N, Defining_Entity (New_Body));
527 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
529 -- To prevent premature freeze action, insert the new body at the end
530 -- of the current declarations, or at the end of the package spec.
531 -- However, resolve usage names now, to prevent spurious visibility
532 -- on later entities. Note that the function can now be called in
533 -- the current declarative part, which will appear to be prior to
534 -- the presence of the body in the code. There are nevertheless no
535 -- order of elaboration issues because all name resolution has taken
536 -- place at the point of declaration.
539 Decls : List_Id := List_Containing (N);
540 Expr : constant Node_Id := Expression (Ret);
541 Par : constant Node_Id := Parent (Decls);
542 Typ : constant Entity_Id := Etype (Def_Id);
545 -- If this is a wrapper created for in an instance for a formal
546 -- subprogram, insert body after declaration, to be analyzed when
547 -- the enclosing instance is analyzed.
550 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
552 Insert_After (N, New_Body);
555 if Nkind (Par) = N_Package_Specification
556 and then Decls = Visible_Declarations (Par)
557 and then Present (Private_Declarations (Par))
558 and then not Is_Empty_List (Private_Declarations (Par))
560 Decls := Private_Declarations (Par);
563 Insert_After (Last (Decls), New_Body);
565 -- Preanalyze the expression for name capture, except in an
566 -- instance, where this has been done during generic analysis,
567 -- and will be redone when analyzing the body.
569 Set_Parent (Expr, Ret);
571 Install_Formals (Def_Id);
573 if not In_Instance then
574 Preanalyze_Spec_Expression (Expr, Typ);
575 Check_Limited_Return (Original_Node (N), Expr, Typ);
583 -- If the return expression is a static constant, we suppress warning
584 -- messages on unused formals, which in most cases will be noise.
586 Set_Is_Trivial_Subprogram
587 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
588 end Analyze_Expression_Function;
590 ----------------------------------------
591 -- Analyze_Extended_Return_Statement --
592 ----------------------------------------
594 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
596 Check_Compiler_Unit ("extended return statement", N);
597 Analyze_Return_Statement (N);
598 end Analyze_Extended_Return_Statement;
600 ----------------------------
601 -- Analyze_Function_Call --
602 ----------------------------
604 procedure Analyze_Function_Call (N : Node_Id) is
605 Actuals : constant List_Id := Parameter_Associations (N);
606 Func_Nam : constant Node_Id := Name (N);
612 -- A call of the form A.B (X) may be an Ada 2005 call, which is
613 -- rewritten as B (A, X). If the rewriting is successful, the call
614 -- has been analyzed and we just return.
616 if Nkind (Func_Nam) = N_Selected_Component
617 and then Name (N) /= Func_Nam
618 and then Is_Rewrite_Substitution (N)
619 and then Present (Etype (N))
624 -- If error analyzing name, then set Any_Type as result type and return
626 if Etype (Func_Nam) = Any_Type then
627 Set_Etype (N, Any_Type);
631 -- Otherwise analyze the parameters
633 if Present (Actuals) then
634 Actual := First (Actuals);
635 while Present (Actual) loop
637 Check_Parameterless_Call (Actual);
643 end Analyze_Function_Call;
645 -----------------------------
646 -- Analyze_Function_Return --
647 -----------------------------
649 procedure Analyze_Function_Return (N : Node_Id) is
650 Loc : constant Source_Ptr := Sloc (N);
651 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
652 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
654 R_Type : constant Entity_Id := Etype (Scope_Id);
655 -- Function result subtype
657 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
658 -- Apply legality rule of 6.5 (5.8) to the access discriminants of an
659 -- aggregate in a return statement.
661 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
662 -- Check that the return_subtype_indication properly matches the result
663 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
665 -----------------------------------
666 -- Check_Aggregate_Accessibility --
667 -----------------------------------
669 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
670 Typ : constant Entity_Id := Etype (Aggr);
677 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
678 Discr := First_Discriminant (Typ);
679 Assoc := First (Component_Associations (Aggr));
680 while Present (Discr) loop
681 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
682 Expr := Expression (Assoc);
684 if Nkind (Expr) = N_Attribute_Reference
685 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
687 Obj := Prefix (Expr);
688 while Nkind_In (Obj, N_Indexed_Component,
689 N_Selected_Component)
694 -- Do not check aliased formals or function calls. A
695 -- run-time check may still be needed ???
697 if Is_Entity_Name (Obj)
698 and then Comes_From_Source (Obj)
700 if Is_Formal (Entity (Obj))
701 and then Is_Aliased (Entity (Obj))
705 elsif Object_Access_Level (Obj) >
706 Scope_Depth (Scope (Scope_Id))
709 ("access discriminant in return aggregate would "
710 & "be a dangling reference", Obj);
716 Next_Discriminant (Discr);
719 end Check_Aggregate_Accessibility;
721 -------------------------------------
722 -- Check_Return_Subtype_Indication --
723 -------------------------------------
725 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
726 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
728 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
729 -- Subtype given in the extended return statement (must match R_Type)
731 Subtype_Ind : constant Node_Id :=
732 Object_Definition (Original_Node (Obj_Decl));
734 procedure Error_No_Match (N : Node_Id);
735 -- Output error messages for case where types do not statically
736 -- match. N is the location for the messages.
742 procedure Error_No_Match (N : Node_Id) is
745 ("subtype must statically match function result subtype", N);
747 if not Predicates_Match (R_Stm_Type, R_Type) then
748 Error_Msg_Node_2 := R_Type;
750 ("\predicate of& does not match predicate of&",
755 -- Start of processing for Check_Return_Subtype_Indication
758 -- First, avoid cascaded errors
760 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
764 -- "return access T" case; check that the return statement also has
765 -- "access T", and that the subtypes statically match:
766 -- if this is an access to subprogram the signatures must match.
768 if Is_Anonymous_Access_Type (R_Type) then
769 if Is_Anonymous_Access_Type (R_Stm_Type) then
770 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
772 if Base_Type (Designated_Type (R_Stm_Type)) /=
773 Base_Type (Designated_Type (R_Type))
774 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
776 Error_No_Match (Subtype_Mark (Subtype_Ind));
780 -- For two anonymous access to subprogram types, the types
781 -- themselves must be type conformant.
783 if not Conforming_Types
784 (R_Stm_Type, R_Type, Fully_Conformant)
786 Error_No_Match (Subtype_Ind);
791 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
794 -- If the return object is of an anonymous access type, then report
795 -- an error if the function's result type is not also anonymous.
797 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
798 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
800 ("anonymous access not allowed for function with named access "
801 & "result", Subtype_Ind);
803 -- Subtype indication case: check that the return object's type is
804 -- covered by the result type, and that the subtypes statically match
805 -- when the result subtype is constrained. Also handle record types
806 -- with unknown discriminants for which we have built the underlying
807 -- record view. Coverage is needed to allow specific-type return
808 -- objects when the result type is class-wide (see AI05-32).
810 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
811 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
815 Underlying_Record_View (Base_Type (R_Stm_Type))))
817 -- A null exclusion may be present on the return type, on the
818 -- function specification, on the object declaration or on the
821 if Is_Access_Type (R_Type)
823 (Can_Never_Be_Null (R_Type)
824 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
825 Can_Never_Be_Null (R_Stm_Type)
827 Error_No_Match (Subtype_Ind);
830 -- AI05-103: for elementary types, subtypes must statically match
832 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
833 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
834 Error_No_Match (Subtype_Ind);
838 -- All remaining cases are illegal
840 -- Note: previous versions of this subprogram allowed the return
841 -- value to be the ancestor of the return type if the return type
842 -- was a null extension. This was plainly incorrect.
846 ("wrong type for return_subtype_indication", Subtype_Ind);
848 end Check_Return_Subtype_Indication;
850 ---------------------
851 -- Local Variables --
852 ---------------------
857 -- Start of processing for Analyze_Function_Return
860 Set_Return_Present (Scope_Id);
862 if Nkind (N) = N_Simple_Return_Statement then
863 Expr := Expression (N);
865 -- Guard against a malformed expression. The parser may have tried to
866 -- recover but the node is not analyzable.
868 if Nkind (Expr) = N_Error then
869 Set_Etype (Expr, Any_Type);
870 Expander_Mode_Save_And_Set (False);
874 -- The resolution of a controlled [extension] aggregate associated
875 -- with a return statement creates a temporary which needs to be
876 -- finalized on function exit. Wrap the return statement inside a
877 -- block so that the finalization machinery can detect this case.
878 -- This early expansion is done only when the return statement is
879 -- not part of a handled sequence of statements.
881 if Nkind_In (Expr, N_Aggregate,
882 N_Extension_Aggregate)
883 and then Needs_Finalization (R_Type)
884 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
887 Make_Block_Statement (Loc,
888 Handled_Statement_Sequence =>
889 Make_Handled_Sequence_Of_Statements (Loc,
890 Statements => New_List (Relocate_Node (N)))));
898 -- Ada 2005 (AI-251): If the type of the returned object is
899 -- an access to an interface type then we add an implicit type
900 -- conversion to force the displacement of the "this" pointer to
901 -- reference the secondary dispatch table. We cannot delay the
902 -- generation of this implicit conversion until the expansion
903 -- because in this case the type resolution changes the decoration
904 -- of the expression node to match R_Type; by contrast, if the
905 -- returned object is a class-wide interface type then it is too
906 -- early to generate here the implicit conversion since the return
907 -- statement may be rewritten by the expander into an extended
908 -- return statement whose expansion takes care of adding the
909 -- implicit type conversion to displace the pointer to the object.
912 and then Serious_Errors_Detected = 0
913 and then Is_Access_Type (R_Type)
914 and then Nkind (Expr) /= N_Null
915 and then Is_Interface (Designated_Type (R_Type))
916 and then Is_Progenitor (Designated_Type (R_Type),
917 Designated_Type (Etype (Expr)))
919 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
923 Resolve (Expr, R_Type);
924 Check_Limited_Return (N, Expr, R_Type);
926 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
927 Check_Aggregate_Accessibility (Expr);
931 -- RETURN only allowed in SPARK as the last statement in function
933 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
935 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
936 or else Present (Next (N)))
938 Check_SPARK_05_Restriction
939 ("RETURN should be the last statement in function", N);
943 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
944 Obj_Decl := Last (Return_Object_Declarations (N));
946 -- Analyze parts specific to extended_return_statement:
949 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
950 HSS : constant Node_Id := Handled_Statement_Sequence (N);
953 Expr := Expression (Obj_Decl);
955 -- Note: The check for OK_For_Limited_Init will happen in
956 -- Analyze_Object_Declaration; we treat it as a normal
957 -- object declaration.
959 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
962 Check_Return_Subtype_Indication (Obj_Decl);
964 if Present (HSS) then
967 if Present (Exception_Handlers (HSS)) then
969 -- ???Has_Nested_Block_With_Handler needs to be set.
970 -- Probably by creating an actual N_Block_Statement.
971 -- Probably in Expand.
977 -- Mark the return object as referenced, since the return is an
978 -- implicit reference of the object.
980 Set_Referenced (Defining_Identifier (Obj_Decl));
982 Check_References (Stm_Entity);
984 -- Check RM 6.5 (5.9/3)
987 if Ada_Version < Ada_2012 then
989 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
990 -- Can it really happen (extended return???)
993 ("aliased only allowed for limited return objects "
994 & "in Ada 2012??", N);
996 elsif not Is_Limited_View (R_Type) then
998 ("aliased only allowed for limited return objects", N);
1004 -- Case of Expr present
1008 -- Defend against previous errors
1010 and then Nkind (Expr) /= N_Empty
1011 and then Present (Etype (Expr))
1013 -- Apply constraint check. Note that this is done before the implicit
1014 -- conversion of the expression done for anonymous access types to
1015 -- ensure correct generation of the null-excluding check associated
1016 -- with null-excluding expressions found in return statements.
1018 Apply_Constraint_Check (Expr, R_Type);
1020 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1021 -- type, apply an implicit conversion of the expression to that type
1022 -- to force appropriate static and run-time accessibility checks.
1024 if Ada_Version >= Ada_2005
1025 and then Ekind (R_Type) = E_Anonymous_Access_Type
1027 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1028 Analyze_And_Resolve (Expr, R_Type);
1030 -- If this is a local anonymous access to subprogram, the
1031 -- accessibility check can be applied statically. The return is
1032 -- illegal if the access type of the return expression is declared
1033 -- inside of the subprogram (except if it is the subtype indication
1034 -- of an extended return statement).
1036 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1037 if not Comes_From_Source (Current_Scope)
1038 or else Ekind (Current_Scope) = E_Return_Statement
1043 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1045 Error_Msg_N ("cannot return local access to subprogram", N);
1048 -- The expression cannot be of a formal incomplete type
1050 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1051 and then Is_Generic_Type (Etype (Expr))
1054 ("cannot return expression of a formal incomplete type", N);
1057 -- If the result type is class-wide, then check that the return
1058 -- expression's type is not declared at a deeper level than the
1059 -- function (RM05-6.5(5.6/2)).
1061 if Ada_Version >= Ada_2005
1062 and then Is_Class_Wide_Type (R_Type)
1064 if Type_Access_Level (Etype (Expr)) >
1065 Subprogram_Access_Level (Scope_Id)
1068 ("level of return expression type is deeper than "
1069 & "class-wide function!", Expr);
1073 -- Check incorrect use of dynamically tagged expression
1075 if Is_Tagged_Type (R_Type) then
1076 Check_Dynamically_Tagged_Expression
1082 -- ??? A real run-time accessibility check is needed in cases
1083 -- involving dereferences of access parameters. For now we just
1084 -- check the static cases.
1086 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1087 and then Is_Limited_View (Etype (Scope_Id))
1088 and then Object_Access_Level (Expr) >
1089 Subprogram_Access_Level (Scope_Id)
1091 -- Suppress the message in a generic, where the rewriting
1094 if Inside_A_Generic then
1099 Make_Raise_Program_Error (Loc,
1100 Reason => PE_Accessibility_Check_Failed));
1103 Error_Msg_Warn := SPARK_Mode /= On;
1104 Error_Msg_N ("cannot return a local value by reference<<", N);
1105 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1109 if Known_Null (Expr)
1110 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1111 and then Null_Exclusion_Present (Parent (Scope_Id))
1113 Apply_Compile_Time_Constraint_Error
1115 Msg => "(Ada 2005) null not allowed for "
1116 & "null-excluding return??",
1117 Reason => CE_Null_Not_Allowed);
1120 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1121 -- has no initializing expression.
1123 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1124 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1125 Subprogram_Access_Level (Scope_Id)
1128 ("level of return expression type is deeper than "
1129 & "class-wide function!", Obj_Decl);
1132 end Analyze_Function_Return;
1134 -------------------------------------
1135 -- Analyze_Generic_Subprogram_Body --
1136 -------------------------------------
1138 procedure Analyze_Generic_Subprogram_Body
1142 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1143 Kind : constant Entity_Kind := Ekind (Gen_Id);
1144 Body_Id : Entity_Id;
1149 -- Copy body and disable expansion while analyzing the generic For a
1150 -- stub, do not copy the stub (which would load the proper body), this
1151 -- will be done when the proper body is analyzed.
1153 if Nkind (N) /= N_Subprogram_Body_Stub then
1154 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1157 -- Once the contents of the generic copy and the template are
1158 -- swapped, do the same for their respective aspect specifications.
1160 Exchange_Aspects (N, New_N);
1162 -- Collect all contract-related source pragmas found within the
1163 -- template and attach them to the contract of the subprogram body.
1164 -- This contract is used in the capture of global references within
1167 Create_Generic_Contract (N);
1172 Spec := Specification (N);
1174 -- Within the body of the generic, the subprogram is callable, and
1175 -- behaves like the corresponding non-generic unit.
1177 Body_Id := Defining_Entity (Spec);
1179 if Kind = E_Generic_Procedure
1180 and then Nkind (Spec) /= N_Procedure_Specification
1182 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1185 elsif Kind = E_Generic_Function
1186 and then Nkind (Spec) /= N_Function_Specification
1188 Error_Msg_N ("invalid body for generic function ", Body_Id);
1192 Set_Corresponding_Body (Gen_Decl, Body_Id);
1194 if Has_Completion (Gen_Id)
1195 and then Nkind (Parent (N)) /= N_Subunit
1197 Error_Msg_N ("duplicate generic body", N);
1200 Set_Has_Completion (Gen_Id);
1203 if Nkind (N) = N_Subprogram_Body_Stub then
1204 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1206 Set_Corresponding_Spec (N, Gen_Id);
1209 if Nkind (Parent (N)) = N_Compilation_Unit then
1210 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1213 -- Make generic parameters immediately visible in the body. They are
1214 -- needed to process the formals declarations. Then make the formals
1215 -- visible in a separate step.
1217 Push_Scope (Gen_Id);
1221 First_Ent : Entity_Id;
1224 First_Ent := First_Entity (Gen_Id);
1227 while Present (E) and then not Is_Formal (E) loop
1232 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1234 -- Now generic formals are visible, and the specification can be
1235 -- analyzed, for subsequent conformance check.
1237 Body_Id := Analyze_Subprogram_Specification (Spec);
1239 -- Make formal parameters visible
1243 -- E is the first formal parameter, we loop through the formals
1244 -- installing them so that they will be visible.
1246 Set_First_Entity (Gen_Id, E);
1247 while Present (E) loop
1253 -- Visible generic entity is callable within its own body
1255 Set_Ekind (Gen_Id, Ekind (Body_Id));
1256 Set_Ekind (Body_Id, E_Subprogram_Body);
1257 Set_Convention (Body_Id, Convention (Gen_Id));
1258 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1259 Set_Scope (Body_Id, Scope (Gen_Id));
1261 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1263 if Nkind (N) = N_Subprogram_Body_Stub then
1265 -- No body to analyze, so restore state of generic unit
1267 Set_Ekind (Gen_Id, Kind);
1268 Set_Ekind (Body_Id, Kind);
1270 if Present (First_Ent) then
1271 Set_First_Entity (Gen_Id, First_Ent);
1278 -- If this is a compilation unit, it must be made visible explicitly,
1279 -- because the compilation of the declaration, unlike other library
1280 -- unit declarations, does not. If it is not a unit, the following
1281 -- is redundant but harmless.
1283 Set_Is_Immediately_Visible (Gen_Id);
1284 Reference_Body_Formals (Gen_Id, Body_Id);
1286 if Is_Child_Unit (Gen_Id) then
1287 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1290 Set_Actual_Subtypes (N, Current_Scope);
1292 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1293 Set_SPARK_Pragma_Inherited (Body_Id);
1295 -- Analyze any aspect specifications that appear on the generic
1298 if Has_Aspects (N) then
1299 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1302 Analyze_Declarations (Declarations (N));
1305 -- Process the contract of the subprogram body after all declarations
1306 -- have been analyzed. This ensures that any contract-related pragmas
1307 -- are available through the N_Contract node of the body.
1309 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1311 Analyze (Handled_Statement_Sequence (N));
1312 Save_Global_References (Original_Node (N));
1314 -- Prior to exiting the scope, include generic formals again (if any
1315 -- are present) in the set of local entities.
1317 if Present (First_Ent) then
1318 Set_First_Entity (Gen_Id, First_Ent);
1321 Check_References (Gen_Id);
1324 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1326 Check_Subprogram_Order (N);
1328 -- Outside of its body, unit is generic again
1330 Set_Ekind (Gen_Id, Kind);
1331 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1334 Style.Check_Identifier (Body_Id, Gen_Id);
1338 end Analyze_Generic_Subprogram_Body;
1340 ----------------------------
1341 -- Analyze_Null_Procedure --
1342 ----------------------------
1344 procedure Analyze_Null_Procedure
1346 Is_Completion : out Boolean)
1348 Loc : constant Source_Ptr := Sloc (N);
1349 Spec : constant Node_Id := Specification (N);
1350 Designator : Entity_Id;
1352 Null_Body : Node_Id := Empty;
1353 Null_Stmt : Node_Id := Null_Statement (Spec);
1357 -- Capture the profile of the null procedure before analysis, for
1358 -- expansion at the freeze point and at each point of call. The body is
1359 -- used if the procedure has preconditions, or if it is a completion. In
1360 -- the first case the body is analyzed at the freeze point, in the other
1361 -- it replaces the null procedure declaration.
1363 -- For a null procedure that comes from source, a NULL statement is
1364 -- provided by the parser, which carries the source location of the
1365 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1366 -- from expansion, create one now.
1368 if No (Null_Stmt) then
1369 Null_Stmt := Make_Null_Statement (Loc);
1373 Make_Subprogram_Body (Loc,
1374 Specification => New_Copy_Tree (Spec),
1375 Declarations => New_List,
1376 Handled_Statement_Sequence =>
1377 Make_Handled_Sequence_Of_Statements (Loc,
1378 Statements => New_List (Null_Stmt)));
1380 -- Create new entities for body and formals
1382 Set_Defining_Unit_Name (Specification (Null_Body),
1383 Make_Defining_Identifier
1384 (Sloc (Defining_Entity (N)),
1385 Chars (Defining_Entity (N))));
1387 Form := First (Parameter_Specifications (Specification (Null_Body)));
1388 while Present (Form) loop
1389 Set_Defining_Identifier (Form,
1390 Make_Defining_Identifier
1391 (Sloc (Defining_Identifier (Form)),
1392 Chars (Defining_Identifier (Form))));
1396 -- Determine whether the null procedure may be a completion of a generic
1397 -- suprogram, in which case we use the new null body as the completion
1398 -- and set minimal semantic information on the original declaration,
1399 -- which is rewritten as a null statement.
1401 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1403 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1404 Insert_Before (N, Null_Body);
1405 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1407 Rewrite (N, Make_Null_Statement (Loc));
1408 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1409 Is_Completion := True;
1413 -- Resolve the types of the formals now, because the freeze point may
1414 -- appear in a different context, e.g. an instantiation.
1416 Form := First (Parameter_Specifications (Specification (Null_Body)));
1417 while Present (Form) loop
1418 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1419 Find_Type (Parameter_Type (Form));
1421 elsif No (Access_To_Subprogram_Definition
1422 (Parameter_Type (Form)))
1424 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1426 -- The case of a null procedure with a formal that is an
1427 -- access-to-subprogram type, and that is used as an actual
1428 -- in an instantiation is left to the enthusiastic reader.
1438 -- If there are previous overloadable entities with the same name, check
1439 -- whether any of them is completed by the null procedure.
1441 if Present (Prev) and then Is_Overloadable (Prev) then
1442 Designator := Analyze_Subprogram_Specification (Spec);
1443 Prev := Find_Corresponding_Spec (N);
1446 if No (Prev) or else not Comes_From_Source (Prev) then
1447 Designator := Analyze_Subprogram_Specification (Spec);
1448 Set_Has_Completion (Designator);
1450 -- Signal to caller that this is a procedure declaration
1452 Is_Completion := False;
1454 -- Null procedures are always inlined, but generic formal subprograms
1455 -- which appear as such in the internal instance of formal packages,
1456 -- need no completion and are not marked Inline.
1459 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1461 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1462 Set_Body_To_Inline (N, Null_Body);
1463 Set_Is_Inlined (Designator);
1467 -- The null procedure is a completion. We unconditionally rewrite
1468 -- this as a null body (even if expansion is not active), because
1469 -- there are various error checks that are applied on this body
1470 -- when it is analyzed (e.g. correct aspect placement).
1472 if Has_Completion (Prev) then
1473 Error_Msg_Sloc := Sloc (Prev);
1474 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1477 Is_Completion := True;
1478 Rewrite (N, Null_Body);
1481 end Analyze_Null_Procedure;
1483 -----------------------------
1484 -- Analyze_Operator_Symbol --
1485 -----------------------------
1487 -- An operator symbol such as "+" or "and" may appear in context where the
1488 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1489 -- is just a string, as in (conjunction = "or"). In these cases the parser
1490 -- generates this node, and the semantics does the disambiguation. Other
1491 -- such case are actuals in an instantiation, the generic unit in an
1492 -- instantiation, and pragma arguments.
1494 procedure Analyze_Operator_Symbol (N : Node_Id) is
1495 Par : constant Node_Id := Parent (N);
1498 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1499 or else Nkind (Par) = N_Function_Instantiation
1500 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1501 or else (Nkind (Par) = N_Pragma_Argument_Association
1502 and then not Is_Pragma_String_Literal (Par))
1503 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1504 or else (Nkind (Par) = N_Attribute_Reference
1505 and then Attribute_Name (Par) /= Name_Value)
1507 Find_Direct_Name (N);
1510 Change_Operator_Symbol_To_String_Literal (N);
1513 end Analyze_Operator_Symbol;
1515 -----------------------------------
1516 -- Analyze_Parameter_Association --
1517 -----------------------------------
1519 procedure Analyze_Parameter_Association (N : Node_Id) is
1521 Analyze (Explicit_Actual_Parameter (N));
1522 end Analyze_Parameter_Association;
1524 ----------------------------
1525 -- Analyze_Procedure_Call --
1526 ----------------------------
1528 -- WARNING: This routine manages Ghost regions. Return statements must be
1529 -- replaced by gotos which jump to the end of the routine and restore the
1532 procedure Analyze_Procedure_Call (N : Node_Id) is
1533 procedure Analyze_Call_And_Resolve;
1534 -- Do Analyze and Resolve calls for procedure call. At the end, check
1535 -- for illegal order dependence.
1536 -- ??? where is the check for illegal order dependencies?
1538 ------------------------------
1539 -- Analyze_Call_And_Resolve --
1540 ------------------------------
1542 procedure Analyze_Call_And_Resolve is
1544 if Nkind (N) = N_Procedure_Call_Statement then
1546 Resolve (N, Standard_Void_Type);
1550 end Analyze_Call_And_Resolve;
1554 Actuals : constant List_Id := Parameter_Associations (N);
1555 Loc : constant Source_Ptr := Sloc (N);
1556 P : constant Node_Id := Name (N);
1558 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1559 -- Save the Ghost mode to restore on exit
1564 -- Start of processing for Analyze_Procedure_Call
1567 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1568 -- a procedure call or an entry call. The prefix may denote an access
1569 -- to subprogram type, in which case an implicit dereference applies.
1570 -- If the prefix is an indexed component (without implicit dereference)
1571 -- then the construct denotes a call to a member of an entire family.
1572 -- If the prefix is a simple name, it may still denote a call to a
1573 -- parameterless member of an entry family. Resolution of these various
1574 -- interpretations is delicate.
1576 -- Do not analyze machine code statements to avoid rejecting them in
1579 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1580 Set_Etype (P, Standard_Void_Type);
1585 -- If this is a call of the form Obj.Op, the call may have been analyzed
1586 -- and possibly rewritten into a block, in which case we are done.
1588 if Analyzed (N) then
1592 -- If there is an error analyzing the name (which may have been
1593 -- rewritten if the original call was in prefix notation) then error
1594 -- has been emitted already, mark node and return.
1596 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1597 Set_Etype (N, Any_Type);
1601 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1602 -- Set the mode now to ensure that any nodes generated during analysis
1603 -- and expansion are properly marked as Ghost.
1605 Mark_And_Set_Ghost_Procedure_Call (N);
1607 -- Otherwise analyze the parameters
1609 if Present (Actuals) then
1610 Actual := First (Actuals);
1612 while Present (Actual) loop
1614 Check_Parameterless_Call (Actual);
1619 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1621 if Nkind (P) = N_Attribute_Reference
1622 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1624 Name_Elab_Subp_Body)
1626 if Present (Actuals) then
1628 ("no parameters allowed for this call", First (Actuals));
1632 Set_Etype (N, Standard_Void_Type);
1635 elsif Is_Entity_Name (P)
1636 and then Is_Record_Type (Etype (Entity (P)))
1637 and then Remote_AST_I_Dereference (P)
1641 elsif Is_Entity_Name (P)
1642 and then Ekind (Entity (P)) /= E_Entry_Family
1644 if Is_Access_Type (Etype (P))
1645 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1646 and then No (Actuals)
1647 and then Comes_From_Source (N)
1649 Error_Msg_N ("missing explicit dereference in call", N);
1652 Analyze_Call_And_Resolve;
1654 -- If the prefix is the simple name of an entry family, this is a
1655 -- parameterless call from within the task body itself.
1657 elsif Is_Entity_Name (P)
1658 and then Nkind (P) = N_Identifier
1659 and then Ekind (Entity (P)) = E_Entry_Family
1660 and then Present (Actuals)
1661 and then No (Next (First (Actuals)))
1663 -- Can be call to parameterless entry family. What appears to be the
1664 -- sole argument is in fact the entry index. Rewrite prefix of node
1665 -- accordingly. Source representation is unchanged by this
1669 Make_Indexed_Component (Loc,
1671 Make_Selected_Component (Loc,
1672 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1673 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1674 Expressions => Actuals);
1675 Set_Name (N, New_N);
1676 Set_Etype (New_N, Standard_Void_Type);
1677 Set_Parameter_Associations (N, No_List);
1678 Analyze_Call_And_Resolve;
1680 elsif Nkind (P) = N_Explicit_Dereference then
1681 if Ekind (Etype (P)) = E_Subprogram_Type then
1682 Analyze_Call_And_Resolve;
1684 Error_Msg_N ("expect access to procedure in call", P);
1687 -- The name can be a selected component or an indexed component that
1688 -- yields an access to subprogram. Such a prefix is legal if the call
1689 -- has parameter associations.
1691 elsif Is_Access_Type (Etype (P))
1692 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1694 if Present (Actuals) then
1695 Analyze_Call_And_Resolve;
1697 Error_Msg_N ("missing explicit dereference in call ", N);
1700 -- If not an access to subprogram, then the prefix must resolve to the
1701 -- name of an entry, entry family, or protected operation.
1703 -- For the case of a simple entry call, P is a selected component where
1704 -- the prefix is the task and the selector name is the entry. A call to
1705 -- a protected procedure will have the same syntax. If the protected
1706 -- object contains overloaded operations, the entity may appear as a
1707 -- function, the context will select the operation whose type is Void.
1709 elsif Nkind (P) = N_Selected_Component
1710 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1714 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1715 -- in prefix notation may still be missing its controlling argument,
1716 -- so perform the transformation now.
1718 if SPARK_Mode = On and then In_Inlined_Body then
1720 Subp : constant Entity_Id := Entity (Selector_Name (P));
1721 Typ : constant Entity_Id := Etype (Prefix (P));
1724 if Is_Tagged_Type (Typ)
1725 and then Present (First_Formal (Subp))
1726 and then Etype (First_Formal (Subp)) = Typ
1727 and then Try_Object_Operation (P)
1732 Analyze_Call_And_Resolve;
1737 Analyze_Call_And_Resolve;
1740 elsif Nkind (P) = N_Selected_Component
1741 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1742 and then Present (Actuals)
1743 and then No (Next (First (Actuals)))
1745 -- Can be call to parameterless entry family. What appears to be the
1746 -- sole argument is in fact the entry index. Rewrite prefix of node
1747 -- accordingly. Source representation is unchanged by this
1751 Make_Indexed_Component (Loc,
1752 Prefix => New_Copy (P),
1753 Expressions => Actuals);
1754 Set_Name (N, New_N);
1755 Set_Etype (New_N, Standard_Void_Type);
1756 Set_Parameter_Associations (N, No_List);
1757 Analyze_Call_And_Resolve;
1759 -- For the case of a reference to an element of an entry family, P is
1760 -- an indexed component whose prefix is a selected component (task and
1761 -- entry family), and whose index is the entry family index.
1763 elsif Nkind (P) = N_Indexed_Component
1764 and then Nkind (Prefix (P)) = N_Selected_Component
1765 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1767 Analyze_Call_And_Resolve;
1769 -- If the prefix is the name of an entry family, it is a call from
1770 -- within the task body itself.
1772 elsif Nkind (P) = N_Indexed_Component
1773 and then Nkind (Prefix (P)) = N_Identifier
1774 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1777 Make_Selected_Component (Loc,
1778 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1779 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1780 Rewrite (Prefix (P), New_N);
1782 Analyze_Call_And_Resolve;
1784 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1785 -- procedure name, so the construct can only be a qualified expression.
1787 elsif Nkind (P) = N_Qualified_Expression
1788 and then Ada_Version >= Ada_2012
1790 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1793 -- Anything else is an error
1796 Error_Msg_N ("invalid procedure or entry call", N);
1800 Restore_Ghost_Mode (Saved_GM);
1801 end Analyze_Procedure_Call;
1803 ------------------------------
1804 -- Analyze_Return_Statement --
1805 ------------------------------
1807 procedure Analyze_Return_Statement (N : Node_Id) is
1808 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
1809 N_Simple_Return_Statement));
1811 Returns_Object : constant Boolean :=
1812 Nkind (N) = N_Extended_Return_Statement
1814 (Nkind (N) = N_Simple_Return_Statement
1815 and then Present (Expression (N)));
1816 -- True if we're returning something; that is, "return <expression>;"
1817 -- or "return Result : T [:= ...]". False for "return;". Used for error
1818 -- checking: If Returns_Object is True, N should apply to a function
1819 -- body; otherwise N should apply to a procedure body, entry body,
1820 -- accept statement, or extended return statement.
1822 function Find_What_It_Applies_To return Entity_Id;
1823 -- Find the entity representing the innermost enclosing body, accept
1824 -- statement, or extended return statement. If the result is a callable
1825 -- construct or extended return statement, then this will be the value
1826 -- of the Return_Applies_To attribute. Otherwise, the program is
1827 -- illegal. See RM-6.5(4/2).
1829 -----------------------------
1830 -- Find_What_It_Applies_To --
1831 -----------------------------
1833 function Find_What_It_Applies_To return Entity_Id is
1834 Result : Entity_Id := Empty;
1837 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1838 -- and postconditions.
1840 for J in reverse 0 .. Scope_Stack.Last loop
1841 Result := Scope_Stack.Table (J).Entity;
1842 exit when not Ekind_In (Result, E_Block, E_Loop)
1843 and then Chars (Result) /= Name_uPostconditions;
1846 pragma Assert (Present (Result));
1848 end Find_What_It_Applies_To;
1850 -- Local declarations
1852 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1853 Kind : constant Entity_Kind := Ekind (Scope_Id);
1854 Loc : constant Source_Ptr := Sloc (N);
1855 Stm_Entity : constant Entity_Id :=
1857 (E_Return_Statement, Current_Scope, Loc, 'R');
1859 -- Start of processing for Analyze_Return_Statement
1862 Set_Return_Statement_Entity (N, Stm_Entity);
1864 Set_Etype (Stm_Entity, Standard_Void_Type);
1865 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1867 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1868 -- (4/2): an inner return statement will apply to this extended return.
1870 if Nkind (N) = N_Extended_Return_Statement then
1871 Push_Scope (Stm_Entity);
1874 -- Check that pragma No_Return is obeyed. Don't complain about the
1875 -- implicitly-generated return that is placed at the end.
1877 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1878 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1881 -- Warn on any unassigned OUT parameters if in procedure
1883 if Ekind (Scope_Id) = E_Procedure then
1884 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1887 -- Check that functions return objects, and other things do not
1889 if Kind = E_Function or else Kind = E_Generic_Function then
1890 if not Returns_Object then
1891 Error_Msg_N ("missing expression in return from function", N);
1894 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1895 if Returns_Object then
1896 Error_Msg_N ("procedure cannot return value (use function)", N);
1899 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1900 if Returns_Object then
1901 if Is_Protected_Type (Scope (Scope_Id)) then
1902 Error_Msg_N ("entry body cannot return value", N);
1904 Error_Msg_N ("accept statement cannot return value", N);
1908 elsif Kind = E_Return_Statement then
1910 -- We are nested within another return statement, which must be an
1911 -- extended_return_statement.
1913 if Returns_Object then
1914 if Nkind (N) = N_Extended_Return_Statement then
1916 ("extended return statement cannot be nested (use `RETURN;`)",
1919 -- Case of a simple return statement with a value inside extended
1920 -- return statement.
1924 ("return nested in extended return statement cannot return "
1925 & "value (use `RETURN;`)", N);
1930 Error_Msg_N ("illegal context for return statement", N);
1933 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1934 Analyze_Function_Return (N);
1936 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1937 Set_Return_Present (Scope_Id);
1940 if Nkind (N) = N_Extended_Return_Statement then
1944 Kill_Current_Values (Last_Assignment_Only => True);
1945 Check_Unreachable_Code (N);
1947 Analyze_Dimension (N);
1948 end Analyze_Return_Statement;
1950 -------------------------------------
1951 -- Analyze_Simple_Return_Statement --
1952 -------------------------------------
1954 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1956 if Present (Expression (N)) then
1957 Mark_Coextensions (N, Expression (N));
1960 Analyze_Return_Statement (N);
1961 end Analyze_Simple_Return_Statement;
1963 -------------------------
1964 -- Analyze_Return_Type --
1965 -------------------------
1967 procedure Analyze_Return_Type (N : Node_Id) is
1968 Designator : constant Entity_Id := Defining_Entity (N);
1969 Typ : Entity_Id := Empty;
1972 -- Normal case where result definition does not indicate an error
1974 if Result_Definition (N) /= Error then
1975 if Nkind (Result_Definition (N)) = N_Access_Definition then
1976 Check_SPARK_05_Restriction
1977 ("access result is not allowed", Result_Definition (N));
1979 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1982 AD : constant Node_Id :=
1983 Access_To_Subprogram_Definition (Result_Definition (N));
1985 if Present (AD) and then Protected_Present (AD) then
1986 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1988 Typ := Access_Definition (N, Result_Definition (N));
1992 Set_Parent (Typ, Result_Definition (N));
1993 Set_Is_Local_Anonymous_Access (Typ);
1994 Set_Etype (Designator, Typ);
1996 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1998 Null_Exclusion_Static_Checks (N);
2000 -- Subtype_Mark case
2003 Find_Type (Result_Definition (N));
2004 Typ := Entity (Result_Definition (N));
2005 Set_Etype (Designator, Typ);
2007 -- Unconstrained array as result is not allowed in SPARK
2009 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2010 Check_SPARK_05_Restriction
2011 ("returning an unconstrained array is not allowed",
2012 Result_Definition (N));
2015 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2017 Null_Exclusion_Static_Checks (N);
2019 -- If a null exclusion is imposed on the result type, then create
2020 -- a null-excluding itype (an access subtype) and use it as the
2021 -- function's Etype. Note that the null exclusion checks are done
2022 -- right before this, because they don't get applied to types that
2023 -- do not come from source.
2025 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2026 Set_Etype (Designator,
2027 Create_Null_Excluding_Itype
2030 Scope_Id => Scope (Current_Scope)));
2032 -- The new subtype must be elaborated before use because
2033 -- it is visible outside of the function. However its base
2034 -- type may not be frozen yet, so the reference that will
2035 -- force elaboration must be attached to the freezing of
2038 -- If the return specification appears on a proper body,
2039 -- the subtype will have been created already on the spec.
2041 if Is_Frozen (Typ) then
2042 if Nkind (Parent (N)) = N_Subprogram_Body
2043 and then Nkind (Parent (Parent (N))) = N_Subunit
2047 Build_Itype_Reference (Etype (Designator), Parent (N));
2051 Ensure_Freeze_Node (Typ);
2054 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2056 Set_Itype (IR, Etype (Designator));
2057 Append_Freeze_Actions (Typ, New_List (IR));
2062 Set_Etype (Designator, Typ);
2065 if Ekind (Typ) = E_Incomplete_Type
2066 or else (Is_Class_Wide_Type (Typ)
2067 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2069 -- AI05-0151: Tagged incomplete types are allowed in all formal
2070 -- parts. Untagged incomplete types are not allowed in bodies.
2071 -- As a consequence, limited views cannot appear in a basic
2072 -- declaration that is itself within a body, because there is
2073 -- no point at which the non-limited view will become visible.
2075 if Ada_Version >= Ada_2012 then
2076 if From_Limited_With (Typ) and then In_Package_Body then
2078 ("invalid use of incomplete type&",
2079 Result_Definition (N), Typ);
2081 -- The return type of a subprogram body cannot be of a
2082 -- formal incomplete type.
2084 elsif Is_Generic_Type (Typ)
2085 and then Nkind (Parent (N)) = N_Subprogram_Body
2088 ("return type cannot be a formal incomplete type",
2089 Result_Definition (N));
2091 elsif Is_Class_Wide_Type (Typ)
2092 and then Is_Generic_Type (Root_Type (Typ))
2093 and then Nkind (Parent (N)) = N_Subprogram_Body
2096 ("return type cannot be a formal incomplete type",
2097 Result_Definition (N));
2099 elsif Is_Tagged_Type (Typ) then
2102 -- Use is legal in a thunk generated for an operation
2103 -- inherited from a progenitor.
2105 elsif Is_Thunk (Designator)
2106 and then Present (Non_Limited_View (Typ))
2110 elsif Nkind (Parent (N)) = N_Subprogram_Body
2111 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2115 ("invalid use of untagged incomplete type&",
2119 -- The type must be completed in the current package. This
2120 -- is checked at the end of the package declaration when
2121 -- Taft-amendment types are identified. If the return type
2122 -- is class-wide, there is no required check, the type can
2123 -- be a bona fide TAT.
2125 if Ekind (Scope (Current_Scope)) = E_Package
2126 and then In_Private_Part (Scope (Current_Scope))
2127 and then not Is_Class_Wide_Type (Typ)
2129 Append_Elmt (Designator, Private_Dependents (Typ));
2134 ("invalid use of incomplete type&", Designator, Typ);
2139 -- Case where result definition does indicate an error
2142 Set_Etype (Designator, Any_Type);
2144 end Analyze_Return_Type;
2146 -----------------------------
2147 -- Analyze_Subprogram_Body --
2148 -----------------------------
2150 procedure Analyze_Subprogram_Body (N : Node_Id) is
2151 Loc : constant Source_Ptr := Sloc (N);
2152 Body_Spec : constant Node_Id := Specification (N);
2153 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2156 if Debug_Flag_C then
2157 Write_Str ("==> subprogram body ");
2158 Write_Name (Chars (Body_Id));
2159 Write_Str (" from ");
2160 Write_Location (Loc);
2165 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2167 -- The real work is split out into the helper, so it can do "return;"
2168 -- without skipping the debug output:
2170 Analyze_Subprogram_Body_Helper (N);
2172 if Debug_Flag_C then
2174 Write_Str ("<== subprogram body ");
2175 Write_Name (Chars (Body_Id));
2176 Write_Str (" from ");
2177 Write_Location (Loc);
2180 end Analyze_Subprogram_Body;
2182 ------------------------------------
2183 -- Analyze_Subprogram_Body_Helper --
2184 ------------------------------------
2186 -- This procedure is called for regular subprogram bodies, generic bodies,
2187 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2188 -- specification matters, and is used to create a proper declaration for
2189 -- the subprogram, or to perform conformance checks.
2191 -- WARNING: This routine manages Ghost regions. Return statements must be
2192 -- replaced by gotos which jump to the end of the routine and restore the
2195 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2196 Body_Spec : Node_Id := Specification (N);
2197 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2198 Loc : constant Source_Ptr := Sloc (N);
2199 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2201 Conformant : Boolean;
2202 Desig_View : Entity_Id := Empty;
2203 Exch_Views : Elist_Id := No_Elist;
2205 Prot_Typ : Entity_Id := Empty;
2206 Spec_Decl : Node_Id := Empty;
2207 Spec_Id : Entity_Id;
2209 Last_Real_Spec_Entity : Entity_Id := Empty;
2210 -- When we analyze a separate spec, the entity chain ends up containing
2211 -- the formals, as well as any itypes generated during analysis of the
2212 -- default expressions for parameters, or the arguments of associated
2213 -- precondition/postcondition pragmas (which are analyzed in the context
2214 -- of the spec since they have visibility on formals).
2216 -- These entities belong with the spec and not the body. However we do
2217 -- the analysis of the body in the context of the spec (again to obtain
2218 -- visibility to the formals), and all the entities generated during
2219 -- this analysis end up also chained to the entity chain of the spec.
2220 -- But they really belong to the body, and there is circuitry to move
2221 -- them from the spec to the body.
2223 -- However, when we do this move, we don't want to move the real spec
2224 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2225 -- variable points to the last real spec entity, so we only move those
2226 -- chained beyond that point. It is initialized to Empty to deal with
2227 -- the case where there is no separate spec.
2229 function Body_Has_Contract return Boolean;
2230 -- Check whether unanalyzed body has an aspect or pragma that may
2231 -- generate a SPARK contract.
2233 function Body_Has_SPARK_Mode_On return Boolean;
2234 -- Check whether SPARK_Mode On applies to the subprogram body, either
2235 -- because it is specified directly on the body, or because it is
2236 -- inherited from the enclosing subprogram or package.
2238 procedure Build_Subprogram_Declaration;
2239 -- Create a matching subprogram declaration for subprogram body N
2241 procedure Check_Anonymous_Return;
2242 -- Ada 2005: if a function returns an access type that denotes a task,
2243 -- or a type that contains tasks, we must create a master entity for
2244 -- the anonymous type, which typically will be used in an allocator
2245 -- in the body of the function.
2247 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2248 -- Look ahead to recognize a pragma that may appear after the body.
2249 -- If there is a previous spec, check that it appears in the same
2250 -- declarative part. If the pragma is Inline_Always, perform inlining
2251 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2252 -- If the body acts as a spec, and inlining is required, we create a
2253 -- subprogram declaration for it, in order to attach the body to inline.
2254 -- If pragma does not appear after the body, check whether there is
2255 -- an inline pragma before any local declarations.
2257 procedure Check_Missing_Return;
2258 -- Checks for a function with a no return statements, and also performs
2259 -- the warning checks implemented by Check_Returns. In formal mode, also
2260 -- verify that a function ends with a RETURN and that a procedure does
2261 -- not contain any RETURN.
2263 function Disambiguate_Spec return Entity_Id;
2264 -- When a primitive is declared between the private view and the full
2265 -- view of a concurrent type which implements an interface, a special
2266 -- mechanism is used to find the corresponding spec of the primitive
2269 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2270 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2271 -- incomplete types coming from a limited context and replace their
2272 -- limited views with the non-limited ones. Return the list of changes
2273 -- to be used to undo the transformation.
2275 procedure Freeze_Expr_Types (Spec_Id : Entity_Id);
2276 -- AI12-0103: N is the body associated with an expression function that
2277 -- is a completion, and Spec_Id is its defining entity. Freeze before N
2278 -- all the types referenced by the expression of the function.
2280 function Is_Private_Concurrent_Primitive
2281 (Subp_Id : Entity_Id) return Boolean;
2282 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2283 -- type that implements an interface and has a private view.
2285 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2286 -- Undo the transformation done by Exchange_Limited_Views.
2288 procedure Set_Trivial_Subprogram (N : Node_Id);
2289 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2290 -- subprogram whose body is being analyzed. N is the statement node
2291 -- causing the flag to be set, if the following statement is a return
2292 -- of an entity, we mark the entity as set in source to suppress any
2293 -- warning on the stylized use of function stubs with a dummy return.
2295 procedure Verify_Overriding_Indicator;
2296 -- If there was a previous spec, the entity has been entered in the
2297 -- current scope previously. If the body itself carries an overriding
2298 -- indicator, check that it is consistent with the known status of the
2301 -----------------------
2302 -- Body_Has_Contract --
2303 -----------------------
2305 function Body_Has_Contract return Boolean is
2306 Decls : constant List_Id := Declarations (N);
2310 -- Check for aspects that may generate a contract
2312 if Present (Aspect_Specifications (N)) then
2313 Item := First (Aspect_Specifications (N));
2314 while Present (Item) loop
2315 if Is_Subprogram_Contract_Annotation (Item) then
2323 -- Check for pragmas that may generate a contract
2325 if Present (Decls) then
2326 Item := First (Decls);
2327 while Present (Item) loop
2328 if Nkind (Item) = N_Pragma
2329 and then Is_Subprogram_Contract_Annotation (Item)
2339 end Body_Has_Contract;
2341 ----------------------------
2342 -- Body_Has_SPARK_Mode_On --
2343 ----------------------------
2345 function Body_Has_SPARK_Mode_On return Boolean is
2346 Decls : constant List_Id := Declarations (N);
2350 -- Check for SPARK_Mode aspect
2352 if Present (Aspect_Specifications (N)) then
2353 Item := First (Aspect_Specifications (N));
2354 while Present (Item) loop
2355 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2356 return Get_SPARK_Mode_From_Annotation (Item) = On;
2363 -- Check for SPARK_Mode pragma
2365 if Present (Decls) then
2366 Item := First (Decls);
2367 while Present (Item) loop
2369 -- Pragmas that apply to a subprogram body are usually grouped
2370 -- together. Look for a potential pragma SPARK_Mode among them.
2372 if Nkind (Item) = N_Pragma then
2373 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2374 return Get_SPARK_Mode_From_Annotation (Item) = On;
2377 -- Otherwise the first non-pragma declarative item terminates
2378 -- the region where pragma SPARK_Mode may appear.
2388 -- Otherwise, the applicable SPARK_Mode is inherited from the
2389 -- enclosing subprogram or package.
2391 return SPARK_Mode = On;
2392 end Body_Has_SPARK_Mode_On;
2394 ----------------------------------
2395 -- Build_Subprogram_Declaration --
2396 ----------------------------------
2398 procedure Build_Subprogram_Declaration is
2399 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2400 -- Relocate certain categorization pragmas from the declarative list
2401 -- of subprogram body From and insert them after node To. The pragmas
2404 -- Volatile_Function
2405 -- Also copy pragma SPARK_Mode if present in the declarative list
2406 -- of subprogram body From and insert it after node To. This pragma
2407 -- should not be moved, as it applies to the body too.
2413 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2415 Next_Decl : Node_Id;
2418 pragma Assert (Nkind (From) = N_Subprogram_Body);
2420 -- The destination node must be part of a list, as the pragmas are
2421 -- inserted after it.
2423 pragma Assert (Is_List_Member (To));
2425 -- Inspect the declarations of the subprogram body looking for
2426 -- specific pragmas.
2428 Decl := First (Declarations (N));
2429 while Present (Decl) loop
2430 Next_Decl := Next (Decl);
2432 if Nkind (Decl) = N_Pragma then
2433 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
2434 Insert_After (To, New_Copy_Tree (Decl));
2436 elsif Nam_In (Pragma_Name_Unmapped (Decl),
2438 Name_Volatile_Function)
2441 Insert_After (To, Decl);
2452 Subp_Decl : Node_Id;
2454 -- Start of processing for Build_Subprogram_Declaration
2457 -- Create a matching subprogram spec using the profile of the body.
2458 -- The structure of the tree is identical, but has new entities for
2459 -- the defining unit name and formal parameters.
2462 Make_Subprogram_Declaration (Loc,
2463 Specification => Copy_Subprogram_Spec (Body_Spec));
2464 Set_Comes_From_Source (Subp_Decl, True);
2466 -- Relocate the aspects and relevant pragmas from the subprogram body
2467 -- to the generated spec because it acts as the initial declaration.
2469 Insert_Before (N, Subp_Decl);
2470 Move_Aspects (N, To => Subp_Decl);
2471 Move_Pragmas (N, To => Subp_Decl);
2473 -- Ensure that the generated corresponding spec and original body
2474 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2475 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2476 -- correctly set for local subprograms.
2478 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2480 Analyze (Subp_Decl);
2482 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2483 -- the body since the expander may generate calls using that entity.
2484 -- Required to ensure that Expand_Call rewrites calls to this
2485 -- function by calls to the built procedure.
2487 if Modify_Tree_For_C
2488 and then Nkind (Body_Spec) = N_Function_Specification
2490 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2492 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2493 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2494 Corresponding_Procedure
2495 (Defining_Entity (Specification (Subp_Decl))));
2498 -- Analyze any relocated source pragmas or pragmas created for aspect
2501 Decl := Next (Subp_Decl);
2502 while Present (Decl) loop
2504 -- Stop the search for pragmas once the body has been reached as
2505 -- this terminates the region where pragmas may appear.
2510 elsif Nkind (Decl) = N_Pragma then
2517 Spec_Id := Defining_Entity (Subp_Decl);
2518 Set_Corresponding_Spec (N, Spec_Id);
2520 -- Mark the generated spec as a source construct to ensure that all
2521 -- calls to it are properly registered in ALI files for GNATprove.
2523 Set_Comes_From_Source (Spec_Id, True);
2525 -- Ensure that the specs of the subprogram declaration and its body
2526 -- are identical, otherwise they will appear non-conformant due to
2527 -- rewritings in the default values of formal parameters.
2529 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2530 Set_Specification (N, Body_Spec);
2531 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2532 end Build_Subprogram_Declaration;
2534 ----------------------------
2535 -- Check_Anonymous_Return --
2536 ----------------------------
2538 procedure Check_Anonymous_Return is
2544 if Present (Spec_Id) then
2550 if Ekind (Scop) = E_Function
2551 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2552 and then not Is_Thunk (Scop)
2554 -- Skip internally built functions which handle the case of
2555 -- a null access (see Expand_Interface_Conversion)
2557 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2558 and then not Comes_From_Source (Parent (Scop)))
2560 and then (Has_Task (Designated_Type (Etype (Scop)))
2562 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2564 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2565 and then Expander_Active
2567 -- Avoid cases with no tasking support
2569 and then RTE_Available (RE_Current_Master)
2570 and then not Restriction_Active (No_Task_Hierarchy)
2573 Make_Object_Declaration (Loc,
2574 Defining_Identifier =>
2575 Make_Defining_Identifier (Loc, Name_uMaster),
2576 Constant_Present => True,
2577 Object_Definition =>
2578 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2580 Make_Explicit_Dereference (Loc,
2581 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2583 if Present (Declarations (N)) then
2584 Prepend (Decl, Declarations (N));
2586 Set_Declarations (N, New_List (Decl));
2589 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2590 Set_Has_Master_Entity (Scop);
2592 -- Now mark the containing scope as a task master
2595 while Nkind (Par) /= N_Compilation_Unit loop
2596 Par := Parent (Par);
2597 pragma Assert (Present (Par));
2599 -- If we fall off the top, we are at the outer level, and
2600 -- the environment task is our effective master, so nothing
2604 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2606 Set_Is_Task_Master (Par, True);
2611 end Check_Anonymous_Return;
2613 -------------------------
2614 -- Check_Inline_Pragma --
2615 -------------------------
2617 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2621 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2622 -- True when N is a pragma Inline or Inline_Always that applies
2623 -- to this subprogram.
2625 -----------------------
2626 -- Is_Inline_Pragma --
2627 -----------------------
2629 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2631 if Nkind (N) = N_Pragma
2633 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2634 or else (Pragma_Name_Unmapped (N) = Name_Inline
2636 (Front_End_Inlining or else Optimization_Level > 0)))
2637 and then Present (Pragma_Argument_Associations (N))
2640 Pragma_Arg : Node_Id :=
2641 Expression (First (Pragma_Argument_Associations (N)));
2643 if Nkind (Pragma_Arg) = N_Selected_Component then
2644 Pragma_Arg := Selector_Name (Pragma_Arg);
2647 return Chars (Pragma_Arg) = Chars (Body_Id);
2653 end Is_Inline_Pragma;
2655 -- Start of processing for Check_Inline_Pragma
2658 if not Expander_Active then
2662 if Is_List_Member (N)
2663 and then Present (Next (N))
2664 and then Is_Inline_Pragma (Next (N))
2668 elsif Nkind (N) /= N_Subprogram_Body_Stub
2669 and then Present (Declarations (N))
2670 and then Is_Inline_Pragma (First (Declarations (N)))
2672 Prag := First (Declarations (N));
2678 if Present (Prag) then
2679 if Present (Spec_Id) then
2680 if Is_List_Member (N)
2681 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2682 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2688 -- Create a subprogram declaration, to make treatment uniform.
2689 -- Make the sloc of the subprogram name that of the entity in
2690 -- the body, so that style checks find identical strings.
2693 Subp : constant Entity_Id :=
2694 Make_Defining_Identifier
2695 (Sloc (Body_Id), Chars (Body_Id));
2696 Decl : constant Node_Id :=
2697 Make_Subprogram_Declaration (Loc,
2699 New_Copy_Tree (Specification (N)));
2702 Set_Defining_Unit_Name (Specification (Decl), Subp);
2704 -- To ensure proper coverage when body is inlined, indicate
2705 -- whether the subprogram comes from source.
2707 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2709 if Present (First_Formal (Body_Id)) then
2710 Plist := Copy_Parameter_List (Body_Id);
2711 Set_Parameter_Specifications
2712 (Specification (Decl), Plist);
2715 -- Move aspects to the new spec
2717 if Has_Aspects (N) then
2718 Move_Aspects (N, To => Decl);
2721 Insert_Before (N, Decl);
2724 Set_Has_Pragma_Inline (Subp);
2726 if Pragma_Name (Prag) = Name_Inline_Always then
2727 Set_Is_Inlined (Subp);
2728 Set_Has_Pragma_Inline_Always (Subp);
2731 -- Prior to copying the subprogram body to create a template
2732 -- for it for subsequent inlining, remove the pragma from
2733 -- the current body so that the copy that will produce the
2734 -- new body will start from a completely unanalyzed tree.
2736 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2737 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2744 end Check_Inline_Pragma;
2746 --------------------------
2747 -- Check_Missing_Return --
2748 --------------------------
2750 procedure Check_Missing_Return is
2752 Missing_Ret : Boolean;
2755 if Nkind (Body_Spec) = N_Function_Specification then
2756 if Present (Spec_Id) then
2762 if Return_Present (Id) then
2763 Check_Returns (HSS, 'F', Missing_Ret);
2766 Set_Has_Missing_Return (Id);
2769 -- Within a premature instantiation of a package with no body, we
2770 -- build completions of the functions therein, with a Raise
2771 -- statement. No point in complaining about a missing return in
2774 elsif Ekind (Id) = E_Function
2775 and then In_Instance
2776 and then Present (Statements (HSS))
2777 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2781 elsif Is_Generic_Subprogram (Id)
2782 or else not Is_Machine_Code_Subprogram (Id)
2784 Error_Msg_N ("missing RETURN statement in function body", N);
2787 -- If procedure with No_Return, check returns
2789 elsif Nkind (Body_Spec) = N_Procedure_Specification
2790 and then Present (Spec_Id)
2791 and then No_Return (Spec_Id)
2793 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2796 -- Special checks in SPARK mode
2798 if Nkind (Body_Spec) = N_Function_Specification then
2800 -- In SPARK mode, last statement of a function should be a return
2803 Stat : constant Node_Id := Last_Source_Statement (HSS);
2806 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2807 N_Extended_Return_Statement)
2809 Check_SPARK_05_Restriction
2810 ("last statement in function should be RETURN", Stat);
2814 -- In SPARK mode, verify that a procedure has no return
2816 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2817 if Present (Spec_Id) then
2823 -- Would be nice to point to return statement here, can we
2824 -- borrow the Check_Returns procedure here ???
2826 if Return_Present (Id) then
2827 Check_SPARK_05_Restriction
2828 ("procedure should not have RETURN", N);
2831 end Check_Missing_Return;
2833 -----------------------
2834 -- Disambiguate_Spec --
2835 -----------------------
2837 function Disambiguate_Spec return Entity_Id is
2838 Priv_Spec : Entity_Id;
2841 procedure Replace_Types (To_Corresponding : Boolean);
2842 -- Depending on the flag, replace the type of formal parameters of
2843 -- Body_Id if it is a concurrent type implementing interfaces with
2844 -- the corresponding record type or the other way around.
2846 procedure Replace_Types (To_Corresponding : Boolean) is
2848 Formal_Typ : Entity_Id;
2851 Formal := First_Formal (Body_Id);
2852 while Present (Formal) loop
2853 Formal_Typ := Etype (Formal);
2855 if Is_Class_Wide_Type (Formal_Typ) then
2856 Formal_Typ := Root_Type (Formal_Typ);
2859 -- From concurrent type to corresponding record
2861 if To_Corresponding then
2862 if Is_Concurrent_Type (Formal_Typ)
2863 and then Present (Corresponding_Record_Type (Formal_Typ))
2866 (Corresponding_Record_Type (Formal_Typ)))
2869 Corresponding_Record_Type (Formal_Typ));
2872 -- From corresponding record to concurrent type
2875 if Is_Concurrent_Record_Type (Formal_Typ)
2876 and then Present (Interfaces (Formal_Typ))
2879 Corresponding_Concurrent_Type (Formal_Typ));
2883 Next_Formal (Formal);
2887 -- Start of processing for Disambiguate_Spec
2890 -- Try to retrieve the specification of the body as is. All error
2891 -- messages are suppressed because the body may not have a spec in
2892 -- its current state.
2894 Spec_N := Find_Corresponding_Spec (N, False);
2896 -- It is possible that this is the body of a primitive declared
2897 -- between a private and a full view of a concurrent type. The
2898 -- controlling parameter of the spec carries the concurrent type,
2899 -- not the corresponding record type as transformed by Analyze_
2900 -- Subprogram_Specification. In such cases, we undo the change
2901 -- made by the analysis of the specification and try to find the
2904 -- Note that wrappers already have their corresponding specs and
2905 -- bodies set during their creation, so if the candidate spec is
2906 -- a wrapper, then we definitely need to swap all types to their
2907 -- original concurrent status.
2910 or else Is_Primitive_Wrapper (Spec_N)
2912 -- Restore all references of corresponding record types to the
2913 -- original concurrent types.
2915 Replace_Types (To_Corresponding => False);
2916 Priv_Spec := Find_Corresponding_Spec (N, False);
2918 -- The current body truly belongs to a primitive declared between
2919 -- a private and a full view. We leave the modified body as is,
2920 -- and return the true spec.
2922 if Present (Priv_Spec)
2923 and then Is_Private_Primitive (Priv_Spec)
2928 -- In case that this is some sort of error, restore the original
2929 -- state of the body.
2931 Replace_Types (To_Corresponding => True);
2935 end Disambiguate_Spec;
2937 ----------------------------
2938 -- Exchange_Limited_Views --
2939 ----------------------------
2941 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
2942 Result : Elist_Id := No_Elist;
2944 procedure Detect_And_Exchange (Id : Entity_Id);
2945 -- Determine whether Id's type denotes an incomplete type associated
2946 -- with a limited with clause and exchange the limited view with the
2947 -- non-limited one when available. Note that the non-limited view
2948 -- may exist because of a with_clause in another unit in the context,
2949 -- but cannot be used because the current view of the enclosing unit
2950 -- is still a limited view.
2952 -------------------------
2953 -- Detect_And_Exchange --
2954 -------------------------
2956 procedure Detect_And_Exchange (Id : Entity_Id) is
2957 Typ : constant Entity_Id := Etype (Id);
2959 if From_Limited_With (Typ)
2960 and then Has_Non_Limited_View (Typ)
2961 and then not From_Limited_With (Scope (Typ))
2964 Result := New_Elmt_List;
2967 Prepend_Elmt (Typ, Result);
2968 Prepend_Elmt (Id, Result);
2969 Set_Etype (Id, Non_Limited_View (Typ));
2971 end Detect_And_Exchange;
2977 -- Start of processing for Exchange_Limited_Views
2980 -- Do not process subprogram bodies as they already use the non-
2981 -- limited view of types.
2983 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2987 -- Examine all formals and swap views when applicable
2989 Formal := First_Formal (Subp_Id);
2990 while Present (Formal) loop
2991 Detect_And_Exchange (Formal);
2993 Next_Formal (Formal);
2996 -- Process the return type of a function
2998 if Ekind (Subp_Id) = E_Function then
2999 Detect_And_Exchange (Subp_Id);
3003 end Exchange_Limited_Views;
3005 -----------------------
3006 -- Freeze_Expr_Types --
3007 -----------------------
3009 procedure Freeze_Expr_Types (Spec_Id : Entity_Id) is
3010 function Cloned_Expression return Node_Id;
3011 -- Build a duplicate of the expression of the return statement that
3012 -- has no defining entities shared with the original expression.
3014 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
3015 -- Freeze all types referenced in the subtree rooted at Node
3017 -----------------------
3018 -- Cloned_Expression --
3019 -----------------------
3021 function Cloned_Expression return Node_Id is
3022 function Clone_Id (Node : Node_Id) return Traverse_Result;
3023 -- Tree traversal routine that clones the defining identifier of
3024 -- iterator and loop parameter specification nodes.
3030 function Clone_Id (Node : Node_Id) return Traverse_Result is
3032 if Nkind_In (Node, N_Iterator_Specification,
3033 N_Loop_Parameter_Specification)
3035 Set_Defining_Identifier (Node,
3036 New_Copy (Defining_Identifier (Node)));
3046 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
3050 Return_Stmt : constant Node_Id :=
3052 (Statements (Handled_Statement_Sequence (N)));
3055 -- Start of processing for Cloned_Expression
3058 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3060 -- We must duplicate the expression with semantic information to
3061 -- inherit the decoration of global entities in generic instances.
3063 Dup_Expr := New_Copy_Tree (Expression (Return_Stmt));
3065 -- Replace the defining identifier of iterators and loop param
3066 -- specifications by a clone to ensure that the cloned expression
3067 -- and the original expression don't have shared identifiers;
3068 -- otherwise, as part of the preanalysis of the expression, these
3069 -- shared identifiers may be left decorated with itypes which
3070 -- will not be available in the tree passed to the backend.
3072 Clone_Def_Ids (Dup_Expr);
3075 end Cloned_Expression;
3077 ----------------------
3078 -- Freeze_Type_Refs --
3079 ----------------------
3081 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
3083 if Nkind (Node) = N_Identifier
3084 and then Present (Entity (Node))
3086 if Is_Type (Entity (Node)) then
3087 Freeze_Before (N, Entity (Node));
3089 elsif Ekind_In (Entity (Node), E_Component,
3093 Rec : constant Entity_Id := Scope (Entity (Node));
3096 -- Check that the enclosing record type can be frozen.
3097 -- This provides a better error message than generating
3098 -- primitives whose compilation fails much later. Refine
3099 -- the error message if possible.
3101 Check_Fully_Declared (Rec, Node);
3103 if Error_Posted (Node) then
3104 if Has_Private_Component (Rec) then
3106 ("\type& has private component", Node, Rec);
3110 Freeze_Before (N, Rec);
3117 end Freeze_Type_Refs;
3119 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
3123 Saved_First_Entity : constant Entity_Id := First_Entity (Spec_Id);
3124 Saved_Last_Entity : constant Entity_Id := Last_Entity (Spec_Id);
3125 Dup_Expr : constant Node_Id := Cloned_Expression;
3127 -- Start of processing for Freeze_Expr_Types
3130 -- Preanalyze a duplicate of the expression to have available the
3131 -- minimum decoration needed to locate referenced unfrozen types
3132 -- without adding any decoration to the function expression. This
3133 -- preanalysis is performed with errors disabled to avoid reporting
3134 -- spurious errors on Ghost entities (since the expression is not
3137 Push_Scope (Spec_Id);
3138 Install_Formals (Spec_Id);
3139 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
3141 Preanalyze_Spec_Expression (Dup_Expr, Etype (Spec_Id));
3143 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
3146 -- Restore certain attributes of Spec_Id since the preanalysis may
3147 -- have introduced itypes to this scope, thus modifying attributes
3148 -- First_Entity and Last_Entity.
3150 Set_First_Entity (Spec_Id, Saved_First_Entity);
3151 Set_Last_Entity (Spec_Id, Saved_Last_Entity);
3153 if Present (Last_Entity (Spec_Id)) then
3154 Set_Next_Entity (Last_Entity (Spec_Id), Empty);
3157 -- Freeze all types referenced in the expression
3159 Freeze_References (Dup_Expr);
3160 end Freeze_Expr_Types;
3162 -------------------------------------
3163 -- Is_Private_Concurrent_Primitive --
3164 -------------------------------------
3166 function Is_Private_Concurrent_Primitive
3167 (Subp_Id : Entity_Id) return Boolean
3169 Formal_Typ : Entity_Id;
3172 if Present (First_Formal (Subp_Id)) then
3173 Formal_Typ := Etype (First_Formal (Subp_Id));
3175 if Is_Concurrent_Record_Type (Formal_Typ) then
3176 if Is_Class_Wide_Type (Formal_Typ) then
3177 Formal_Typ := Root_Type (Formal_Typ);
3180 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3183 -- The type of the first formal is a concurrent tagged type with
3187 Is_Concurrent_Type (Formal_Typ)
3188 and then Is_Tagged_Type (Formal_Typ)
3189 and then Has_Private_Declaration (Formal_Typ);
3193 end Is_Private_Concurrent_Primitive;
3195 ---------------------------
3196 -- Restore_Limited_Views --
3197 ---------------------------
3199 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3200 Elmt : Elmt_Id := First_Elmt (Restore_List);
3204 while Present (Elmt) loop
3207 Set_Etype (Id, Node (Elmt));
3210 end Restore_Limited_Views;
3212 ----------------------------
3213 -- Set_Trivial_Subprogram --
3214 ----------------------------
3216 procedure Set_Trivial_Subprogram (N : Node_Id) is
3217 Nxt : constant Node_Id := Next (N);
3220 Set_Is_Trivial_Subprogram (Body_Id);
3222 if Present (Spec_Id) then
3223 Set_Is_Trivial_Subprogram (Spec_Id);
3227 and then Nkind (Nxt) = N_Simple_Return_Statement
3228 and then No (Next (Nxt))
3229 and then Present (Expression (Nxt))
3230 and then Is_Entity_Name (Expression (Nxt))
3232 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3234 end Set_Trivial_Subprogram;
3236 ---------------------------------
3237 -- Verify_Overriding_Indicator --
3238 ---------------------------------
3240 procedure Verify_Overriding_Indicator is
3242 if Must_Override (Body_Spec) then
3243 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3244 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3248 elsif not Present (Overridden_Operation (Spec_Id)) then
3250 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3252 -- Overriding indicators aren't allowed for protected subprogram
3253 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3254 -- this to a warning if -gnatd.E is enabled.
3256 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3257 Error_Msg_Warn := Error_To_Warning;
3259 ("<<overriding indicator not allowed for protected "
3260 & "subprogram body", Body_Spec);
3263 elsif Must_Not_Override (Body_Spec) then
3264 if Present (Overridden_Operation (Spec_Id)) then
3266 ("subprogram& overrides inherited operation",
3267 Body_Spec, Spec_Id);
3269 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3270 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3273 ("subprogram& overrides predefined operator ",
3274 Body_Spec, Spec_Id);
3276 -- Overriding indicators aren't allowed for protected subprogram
3277 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3278 -- this to a warning if -gnatd.E is enabled.
3280 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3281 Error_Msg_Warn := Error_To_Warning;
3284 ("<<overriding indicator not allowed "
3285 & "for protected subprogram body", Body_Spec);
3287 -- If this is not a primitive operation, then the overriding
3288 -- indicator is altogether illegal.
3290 elsif not Is_Primitive (Spec_Id) then
3292 ("overriding indicator only allowed "
3293 & "if subprogram is primitive", Body_Spec);
3296 -- If checking the style rule and the operation overrides, then
3297 -- issue a warning about a missing overriding_indicator. Protected
3298 -- subprogram bodies are excluded from this style checking, since
3299 -- they aren't primitives (even though their declarations can
3300 -- override) and aren't allowed to have an overriding_indicator.
3303 and then Present (Overridden_Operation (Spec_Id))
3304 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3306 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3307 Style.Missing_Overriding (N, Body_Id);
3310 and then Can_Override_Operator (Spec_Id)
3311 and then not Is_Predefined_File_Name
3312 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3314 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3315 Style.Missing_Overriding (N, Body_Id);
3317 end Verify_Overriding_Indicator;
3321 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3322 Saved_ISMP : constant Boolean :=
3323 Ignore_SPARK_Mode_Pragmas_In_Instance;
3324 -- Save the Ghost and SPARK mode-related data to restore on exit
3326 -- Start of processing for Analyze_Subprogram_Body_Helper
3329 -- A [generic] subprogram body "freezes" the contract of the nearest
3330 -- enclosing package body and all other contracts encountered in the
3331 -- same declarative part up to and excluding the subprogram body:
3333 -- package body Nearest_Enclosing_Package
3334 -- with Refined_State => (State => Constit)
3338 -- procedure Freezes_Enclosing_Package_Body
3339 -- with Refined_Depends => (Input => Constit) ...
3341 -- This ensures that any annotations referenced by the contract of the
3342 -- [generic] subprogram body are available. This form of "freezing" is
3343 -- decoupled from the usual Freeze_xxx mechanism because it must also
3344 -- work in the context of generics where normal freezing is disabled.
3346 -- Only bodies coming from source should cause this type of "freezing".
3347 -- Expression functions that act as bodies and complete an initial
3348 -- declaration must be included in this category, hence the use of
3351 if Comes_From_Source (Original_Node (N)) then
3352 Analyze_Previous_Contracts (N);
3355 -- Generic subprograms are handled separately. They always have a
3356 -- generic specification. Determine whether current scope has a
3357 -- previous declaration.
3359 -- If the subprogram body is defined within an instance of the same
3360 -- name, the instance appears as a package renaming, and will be hidden
3361 -- within the subprogram.
3363 if Present (Prev_Id)
3364 and then not Is_Overloadable (Prev_Id)
3365 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3366 or else Comes_From_Source (Prev_Id))
3368 if Is_Generic_Subprogram (Prev_Id) then
3371 -- A subprogram body is Ghost when it is stand alone and subject
3372 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3373 -- the mode now to ensure that any nodes generated during analysis
3374 -- and expansion are properly marked as Ghost.
3376 Mark_And_Set_Ghost_Body (N, Spec_Id);
3378 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3379 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3381 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3383 if Nkind (N) = N_Subprogram_Body then
3384 HSS := Handled_Statement_Sequence (N);
3385 Check_Missing_Return;
3390 -- Otherwise a previous entity conflicts with the subprogram name.
3391 -- Attempting to enter name will post error.
3394 Enter_Name (Body_Id);
3398 -- Non-generic case, find the subprogram declaration, if one was seen,
3399 -- or enter new overloaded entity in the current scope. If the
3400 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3401 -- part of the context of one of its subunits. No need to redo the
3404 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3408 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3410 if Nkind (N) = N_Subprogram_Body_Stub
3411 or else No (Corresponding_Spec (N))
3413 if Is_Private_Concurrent_Primitive (Body_Id) then
3414 Spec_Id := Disambiguate_Spec;
3416 -- A subprogram body is Ghost when it is stand alone and
3417 -- subject to pragma Ghost or when the corresponding spec is
3418 -- Ghost. Set the mode now to ensure that any nodes generated
3419 -- during analysis and expansion are properly marked as Ghost.
3421 Mark_And_Set_Ghost_Body (N, Spec_Id);
3424 Spec_Id := Find_Corresponding_Spec (N);
3426 -- A subprogram body is Ghost when it is stand alone and
3427 -- subject to pragma Ghost or when the corresponding spec is
3428 -- Ghost. Set the mode now to ensure that any nodes generated
3429 -- during analysis and expansion are properly marked as Ghost.
3431 Mark_And_Set_Ghost_Body (N, Spec_Id);
3433 -- In GNATprove mode, if the body has no previous spec, create
3434 -- one so that the inlining machinery can operate properly.
3435 -- Transfer aspects, if any, to the new spec, so that they
3436 -- are legal and can be processed ahead of the body.
3437 -- We make two copies of the given spec, one for the new
3438 -- declaration, and one for the body.
3440 if No (Spec_Id) and then GNATprove_Mode
3442 -- Inlining does not apply during pre-analysis of code
3444 and then Full_Analysis
3446 -- Inlining only applies to full bodies, not stubs
3448 and then Nkind (N) /= N_Subprogram_Body_Stub
3450 -- Inlining only applies to bodies in the source code, not to
3451 -- those generated by the compiler. In particular, expression
3452 -- functions, whose body is generated by the compiler, are
3453 -- treated specially by GNATprove.
3455 and then Comes_From_Source (Body_Id)
3457 -- This cannot be done for a compilation unit, which is not
3458 -- in a context where we can insert a new spec.
3460 and then Is_List_Member (N)
3462 -- Inlining only applies to subprograms without contracts,
3463 -- as a contract is a sign that GNATprove should perform a
3464 -- modular analysis of the subprogram instead of a contextual
3465 -- analysis at each call site. The same test is performed in
3466 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3467 -- here in another form (because the contract has not been
3468 -- attached to the body) to avoid front-end errors in case
3469 -- pragmas are used instead of aspects, because the
3470 -- corresponding pragmas in the body would not be transferred
3471 -- to the spec, leading to legality errors.
3473 and then not Body_Has_Contract
3474 and then not Inside_A_Generic
3476 Build_Subprogram_Declaration;
3478 -- If this is a function that returns a constrained array, and
3479 -- we are generating SPARK_For_C, create subprogram declaration
3480 -- to simplify subsequent C generation.
3483 and then Modify_Tree_For_C
3484 and then Nkind (Body_Spec) = N_Function_Specification
3485 and then Is_Array_Type (Etype (Body_Id))
3486 and then Is_Constrained (Etype (Body_Id))
3488 Build_Subprogram_Declaration;
3492 -- If this is a duplicate body, no point in analyzing it
3494 if Error_Posted (N) then
3498 -- A subprogram body should cause freezing of its own declaration,
3499 -- but if there was no previous explicit declaration, then the
3500 -- subprogram will get frozen too late (there may be code within
3501 -- the body that depends on the subprogram having been frozen,
3502 -- such as uses of extra formals), so we force it to be frozen
3503 -- here. Same holds if the body and spec are compilation units.
3504 -- Finally, if the return type is an anonymous access to protected
3505 -- subprogram, it must be frozen before the body because its
3506 -- expansion has generated an equivalent type that is used when
3507 -- elaborating the body.
3509 -- An exception in the case of Ada 2012, AI05-177: The bodies
3510 -- created for expression functions do not freeze.
3513 and then Nkind (Original_Node (N)) /= N_Expression_Function
3515 Freeze_Before (N, Body_Id);
3517 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3518 Freeze_Before (N, Spec_Id);
3520 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3521 Freeze_Before (N, Etype (Body_Id));
3525 Spec_Id := Corresponding_Spec (N);
3527 -- A subprogram body is Ghost when it is stand alone and subject
3528 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3529 -- the mode now to ensure that any nodes generated during analysis
3530 -- and expansion are properly marked as Ghost.
3532 Mark_And_Set_Ghost_Body (N, Spec_Id);
3536 -- Previously we scanned the body to look for nested subprograms, and
3537 -- rejected an inline directive if nested subprograms were present,
3538 -- because the back-end would generate conflicting symbols for the
3539 -- nested bodies. This is now unnecessary.
3541 -- Look ahead to recognize a pragma Inline that appears after the body
3543 Check_Inline_Pragma (Spec_Id);
3545 -- Deal with special case of a fully private operation in the body of
3546 -- the protected type. We must create a declaration for the subprogram,
3547 -- in order to attach the protected subprogram that will be used in
3548 -- internal calls. We exclude compiler generated bodies from the
3549 -- expander since the issue does not arise for those cases.
3552 and then Comes_From_Source (N)
3553 and then Is_Protected_Type (Current_Scope)
3555 Spec_Id := Build_Private_Protected_Declaration (N);
3558 -- If we are generating C and this is a function returning a constrained
3559 -- array type for which we must create a procedure with an extra out
3560 -- parameter, build and analyze the body now. The procedure declaration
3561 -- has already been created. We reuse the source body of the function,
3562 -- because in an instance it may contain global references that cannot
3563 -- be reanalyzed. The source function itself is not used any further,
3564 -- so we mark it as having a completion. If the subprogram is a stub the
3565 -- transformation is done later, when the proper body is analyzed.
3568 and then Modify_Tree_For_C
3569 and then Present (Spec_Id)
3570 and then Ekind (Spec_Id) = E_Function
3571 and then Nkind (N) /= N_Subprogram_Body_Stub
3572 and then Rewritten_For_C (Spec_Id)
3574 Set_Has_Completion (Spec_Id);
3576 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3579 -- The entity for the created procedure must remain invisible, so it
3580 -- does not participate in resolution of subsequent references to the
3583 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3587 -- If a separate spec is present, then deal with freezing issues
3589 if Present (Spec_Id) then
3590 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3591 Verify_Overriding_Indicator;
3593 -- In general, the spec will be frozen when we start analyzing the
3594 -- body. However, for internally generated operations, such as
3595 -- wrapper functions for inherited operations with controlling
3596 -- results, the spec may not have been frozen by the time we expand
3597 -- the freeze actions that include the bodies. In particular, extra
3598 -- formals for accessibility or for return-in-place may need to be
3599 -- generated. Freeze nodes, if any, are inserted before the current
3600 -- body. These freeze actions are also needed in ASIS mode and in
3601 -- Compile_Only mode to enable the proper back-end type annotations.
3602 -- They are necessary in any case to insure order of elaboration
3605 if not Is_Frozen (Spec_Id)
3606 and then (Expander_Active
3608 or else (Operating_Mode = Check_Semantics
3609 and then Serious_Errors_Detected = 0))
3611 Set_Has_Delayed_Freeze (Spec_Id);
3612 Freeze_Before (N, Spec_Id);
3614 -- AI12-0103: At the occurrence of an expression function
3615 -- declaration that is a completion, its expression causes
3618 if Has_Completion (Spec_Id)
3619 and then Nkind (N) = N_Subprogram_Body
3620 and then Was_Expression_Function (N)
3622 Freeze_Expr_Types (Spec_Id);
3627 -- If the subprogram has a class-wide clone, build its body as a copy
3628 -- of the original body, and rewrite body of original subprogram as a
3629 -- wrapper that calls the clone.
3631 if Present (Spec_Id)
3632 and then Present (Class_Wide_Clone (Spec_Id))
3633 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
3635 Build_Class_Wide_Clone_Body (Spec_Id, N);
3637 -- This is the new body for the existing primitive operation
3639 Rewrite (N, Build_Class_Wide_Clone_Call
3640 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
3641 Set_Has_Completion (Spec_Id, False);
3646 -- Place subprogram on scope stack, and make formals visible. If there
3647 -- is a spec, the visible entity remains that of the spec.
3649 if Present (Spec_Id) then
3650 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3652 if Is_Child_Unit (Spec_Id) then
3653 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3657 Style.Check_Identifier (Body_Id, Spec_Id);
3660 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3661 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3663 if Is_Abstract_Subprogram (Spec_Id) then
3664 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3668 Set_Convention (Body_Id, Convention (Spec_Id));
3669 Set_Has_Completion (Spec_Id);
3671 if Is_Protected_Type (Scope (Spec_Id)) then
3672 Prot_Typ := Scope (Spec_Id);
3675 -- If this is a body generated for a renaming, do not check for
3676 -- full conformance. The check is redundant, because the spec of
3677 -- the body is a copy of the spec in the renaming declaration,
3678 -- and the test can lead to spurious errors on nested defaults.
3680 if Present (Spec_Decl)
3681 and then not Comes_From_Source (N)
3683 (Nkind (Original_Node (Spec_Decl)) =
3684 N_Subprogram_Renaming_Declaration
3685 or else (Present (Corresponding_Body (Spec_Decl))
3687 Nkind (Unit_Declaration_Node
3688 (Corresponding_Body (Spec_Decl))) =
3689 N_Subprogram_Renaming_Declaration))
3693 -- Conversely, the spec may have been generated for specless body
3694 -- with an inline pragma. The entity comes from source, which is
3695 -- both semantically correct and necessary for proper inlining.
3696 -- The subprogram declaration itself is not in the source.
3698 elsif Comes_From_Source (N)
3699 and then Present (Spec_Decl)
3700 and then not Comes_From_Source (Spec_Decl)
3701 and then Has_Pragma_Inline (Spec_Id)
3708 Fully_Conformant, True, Conformant, Body_Id);
3711 -- If the body is not fully conformant, we have to decide if we
3712 -- should analyze it or not. If it has a really messed up profile
3713 -- then we probably should not analyze it, since we will get too
3714 -- many bogus messages.
3716 -- Our decision is to go ahead in the non-fully conformant case
3717 -- only if it is at least mode conformant with the spec. Note
3718 -- that the call to Check_Fully_Conformant has issued the proper
3719 -- error messages to complain about the lack of conformance.
3722 and then not Mode_Conformant (Body_Id, Spec_Id)
3728 if Spec_Id /= Body_Id then
3729 Reference_Body_Formals (Spec_Id, Body_Id);
3732 Set_Ekind (Body_Id, E_Subprogram_Body);
3734 if Nkind (N) = N_Subprogram_Body_Stub then
3735 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3740 Set_Corresponding_Spec (N, Spec_Id);
3742 -- Ada 2005 (AI-345): If the operation is a primitive operation
3743 -- of a concurrent type, the type of the first parameter has been
3744 -- replaced with the corresponding record, which is the proper
3745 -- run-time structure to use. However, within the body there may
3746 -- be uses of the formals that depend on primitive operations
3747 -- of the type (in particular calls in prefixed form) for which
3748 -- we need the original concurrent type. The operation may have
3749 -- several controlling formals, so the replacement must be done
3752 if Comes_From_Source (Spec_Id)
3753 and then Present (First_Entity (Spec_Id))
3754 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3755 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3756 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3757 and then Present (Corresponding_Concurrent_Type
3758 (Etype (First_Entity (Spec_Id))))
3761 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3765 Form := First_Formal (Spec_Id);
3766 while Present (Form) loop
3767 if Etype (Form) = Typ then
3768 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3776 -- Make the formals visible, and place subprogram on scope stack.
3777 -- This is also the point at which we set Last_Real_Spec_Entity
3778 -- to mark the entities which will not be moved to the body.
3780 Install_Formals (Spec_Id);
3781 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3783 -- Within an instance, add local renaming declarations so that
3784 -- gdb can retrieve the values of actuals more easily. This is
3785 -- only relevant if generating code (and indeed we definitely
3786 -- do not want these definitions -gnatc mode, because that would
3789 if Is_Generic_Instance (Spec_Id)
3790 and then Is_Wrapper_Package (Current_Scope)
3791 and then Expander_Active
3793 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3796 Push_Scope (Spec_Id);
3798 -- Make sure that the subprogram is immediately visible. For
3799 -- child units that have no separate spec this is indispensable.
3800 -- Otherwise it is safe albeit redundant.
3802 Set_Is_Immediately_Visible (Spec_Id);
3805 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3806 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3807 Set_Scope (Body_Id, Scope (Spec_Id));
3809 -- Case of subprogram body with no previous spec
3812 -- Check for style warning required
3816 -- Only apply check for source level subprograms for which checks
3817 -- have not been suppressed.
3819 and then Comes_From_Source (Body_Id)
3820 and then not Suppress_Style_Checks (Body_Id)
3822 -- No warnings within an instance
3824 and then not In_Instance
3826 -- No warnings for expression functions
3828 and then Nkind (Original_Node (N)) /= N_Expression_Function
3830 Style.Body_With_No_Spec (N);
3833 New_Overloaded_Entity (Body_Id);
3835 if Nkind (N) /= N_Subprogram_Body_Stub then
3836 Set_Acts_As_Spec (N);
3837 Generate_Definition (Body_Id);
3839 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3841 -- If the body is an entry wrapper created for an entry with
3842 -- preconditions, it must be compiled in the context of the
3843 -- enclosing synchronized object, because it may mention other
3844 -- operations of the type.
3846 if Is_Entry_Wrapper (Body_Id) then
3848 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
3851 Install_Declarations (Prot);
3855 Install_Formals (Body_Id);
3857 Push_Scope (Body_Id);
3860 -- For stubs and bodies with no previous spec, generate references to
3863 Generate_Reference_To_Formals (Body_Id);
3866 -- Entry barrier functions are generated outside the protected type and
3867 -- should not carry the SPARK_Mode of the enclosing context.
3869 if Nkind (N) = N_Subprogram_Body
3870 and then Is_Entry_Barrier_Function (N)
3874 -- The body is generated as part of expression function expansion. When
3875 -- the expression function appears in the visible declarations of a
3876 -- package, the body is added to the private declarations. Since both
3877 -- declarative lists may be subject to a different SPARK_Mode, inherit
3878 -- the mode of the spec.
3880 -- package P with SPARK_Mode is
3881 -- function Expr_Func ... is (...); -- original
3882 -- [function Expr_Func ...;] -- generated spec
3885 -- pragma SPARK_Mode (Off);
3886 -- [function Expr_Func ... is return ...;] -- generated body
3887 -- end P; -- mode is ON
3889 elsif not Comes_From_Source (N)
3890 and then Present (Spec_Id)
3891 and then Is_Expression_Function (Spec_Id)
3893 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
3894 Set_SPARK_Pragma_Inherited
3895 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
3897 -- Set the SPARK_Mode from the current context (may be overwritten later
3898 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
3899 -- initially on a stand-alone subprogram body, but is then relocated to
3900 -- a generated corresponding spec. In this scenario the mode is shared
3901 -- between the spec and body.
3903 elsif No (SPARK_Pragma (Body_Id)) then
3904 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3905 Set_SPARK_Pragma_Inherited (Body_Id);
3908 -- A subprogram body may be instantiated or inlined at a later pass.
3909 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
3910 -- applied to the initial declaration of the body.
3912 if Present (Spec_Id) then
3913 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
3914 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3918 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
3919 -- case the body is instantiated or inlined later and out of context.
3920 -- The body uses this attribute to restore the value of the global
3923 if Ignore_SPARK_Mode_Pragmas_In_Instance then
3924 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
3926 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
3927 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3931 -- If this is the proper body of a stub, we must verify that the stub
3932 -- conforms to the body, and to the previous spec if one was present.
3933 -- We know already that the body conforms to that spec. This test is
3934 -- only required for subprograms that come from source.
3936 if Nkind (Parent (N)) = N_Subunit
3937 and then Comes_From_Source (N)
3938 and then not Error_Posted (Body_Id)
3939 and then Nkind (Corresponding_Stub (Parent (N))) =
3940 N_Subprogram_Body_Stub
3943 Old_Id : constant Entity_Id :=
3945 (Specification (Corresponding_Stub (Parent (N))));
3947 Conformant : Boolean := False;
3950 if No (Spec_Id) then
3951 Check_Fully_Conformant (Body_Id, Old_Id);
3955 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3957 if not Conformant then
3959 -- The stub was taken to be a new declaration. Indicate that
3962 Set_Has_Completion (Old_Id, False);
3968 Set_Has_Completion (Body_Id);
3969 Check_Eliminated (Body_Id);
3971 -- Analyze any aspect specifications that appear on the subprogram body
3972 -- stub. Stop the analysis now as the stub does not have a declarative
3973 -- or a statement part, and it cannot be inlined.
3975 if Nkind (N) = N_Subprogram_Body_Stub then
3976 if Has_Aspects (N) then
3977 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3985 -- Note: Normally we don't do any inlining if expansion is off, since
3986 -- we won't generate code in any case. An exception arises in GNATprove
3987 -- mode where we want to expand some calls in place, even with expansion
3988 -- disabled, since the inlining eases formal verification.
3990 if not GNATprove_Mode
3991 and then Expander_Active
3992 and then Serious_Errors_Detected = 0
3993 and then Present (Spec_Id)
3994 and then Has_Pragma_Inline (Spec_Id)
3996 -- Legacy implementation (relying on front-end inlining)
3998 if not Back_End_Inlining then
3999 if (Has_Pragma_Inline_Always (Spec_Id)
4000 and then not Opt.Disable_FE_Inline_Always)
4001 or else (Front_End_Inlining
4002 and then not Opt.Disable_FE_Inline)
4004 Build_Body_To_Inline (N, Spec_Id);
4007 -- New implementation (relying on back-end inlining)
4010 if Has_Pragma_Inline_Always (Spec_Id)
4011 or else Optimization_Level > 0
4013 -- Handle function returning an unconstrained type
4015 if Comes_From_Source (Body_Id)
4016 and then Ekind (Spec_Id) = E_Function
4017 and then Returns_Unconstrained_Type (Spec_Id)
4019 -- If function builds in place, i.e. returns a limited type,
4020 -- inlining cannot be done.
4022 and then not Is_Limited_Type (Etype (Spec_Id))
4024 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4028 Subp_Body : constant Node_Id :=
4029 Unit_Declaration_Node (Body_Id);
4030 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4033 -- Do not pass inlining to the backend if the subprogram
4034 -- has declarations or statements which cannot be inlined
4035 -- by the backend. This check is done here to emit an
4036 -- error instead of the generic warning message reported
4037 -- by the GCC backend (ie. "function might not be
4040 if Present (Subp_Decl)
4041 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4045 elsif Has_Excluded_Statement
4048 (Handled_Statement_Sequence (Subp_Body)))
4052 -- If the backend inlining is available then at this
4053 -- stage we only have to mark the subprogram as inlined.
4054 -- The expander will take care of registering it in the
4055 -- table of subprograms inlined by the backend a part of
4056 -- processing calls to it (cf. Expand_Call)
4059 Set_Is_Inlined (Spec_Id);
4066 -- In GNATprove mode, inline only when there is a separate subprogram
4067 -- declaration for now, as inlining of subprogram bodies acting as
4068 -- declarations, or subprogram stubs, are not supported by front-end
4069 -- inlining. This inlining should occur after analysis of the body, so
4070 -- that it is known whether the value of SPARK_Mode, which can be
4071 -- defined by a pragma inside the body, is applicable to the body.
4072 -- Inlining can be disabled with switch -gnatdm
4074 elsif GNATprove_Mode
4075 and then Full_Analysis
4076 and then not Inside_A_Generic
4077 and then Present (Spec_Id)
4079 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4080 and then Body_Has_SPARK_Mode_On
4081 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4082 and then not Body_Has_Contract
4083 and then not Debug_Flag_M
4085 Build_Body_To_Inline (N, Spec_Id);
4088 -- When generating code, inherited pre/postconditions are handled when
4089 -- expanding the corresponding contract.
4091 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4092 -- of the specification we have to install the private withed units.
4093 -- This holds for child units as well.
4095 if Is_Compilation_Unit (Body_Id)
4096 or else Nkind (Parent (N)) = N_Compilation_Unit
4098 Install_Private_With_Clauses (Body_Id);
4101 Check_Anonymous_Return;
4103 -- Set the Protected_Formal field of each extra formal of the protected
4104 -- subprogram to reference the corresponding extra formal of the
4105 -- subprogram that implements it. For regular formals this occurs when
4106 -- the protected subprogram's declaration is expanded, but the extra
4107 -- formals don't get created until the subprogram is frozen. We need to
4108 -- do this before analyzing the protected subprogram's body so that any
4109 -- references to the original subprogram's extra formals will be changed
4110 -- refer to the implementing subprogram's formals (see Expand_Formal).
4112 if Present (Spec_Id)
4113 and then Is_Protected_Type (Scope (Spec_Id))
4114 and then Present (Protected_Body_Subprogram (Spec_Id))
4117 Impl_Subp : constant Entity_Id :=
4118 Protected_Body_Subprogram (Spec_Id);
4119 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4120 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4123 while Present (Prot_Ext_Formal) loop
4124 pragma Assert (Present (Impl_Ext_Formal));
4125 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4126 Next_Formal_With_Extras (Prot_Ext_Formal);
4127 Next_Formal_With_Extras (Impl_Ext_Formal);
4132 -- Now we can go on to analyze the body
4134 HSS := Handled_Statement_Sequence (N);
4135 Set_Actual_Subtypes (N, Current_Scope);
4137 -- Add a declaration for the Protection object, renaming declarations
4138 -- for discriminals and privals and finally a declaration for the entry
4139 -- family index (if applicable). This form of early expansion is done
4140 -- when the Expander is active because Install_Private_Data_Declarations
4141 -- references entities which were created during regular expansion. The
4142 -- subprogram entity must come from source, and not be an internally
4143 -- generated subprogram.
4146 and then Present (Prot_Typ)
4147 and then Present (Spec_Id)
4148 and then Comes_From_Source (Spec_Id)
4149 and then not Is_Eliminated (Spec_Id)
4151 Install_Private_Data_Declarations
4152 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4155 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4156 -- may now appear in parameter and result profiles. Since the analysis
4157 -- of a subprogram body may use the parameter and result profile of the
4158 -- spec, swap any limited views with their non-limited counterpart.
4160 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4161 Exch_Views := Exchange_Limited_Views (Spec_Id);
4164 -- If the return type is an anonymous access type whose designated type
4165 -- is the limited view of a class-wide type and the non-limited view is
4166 -- available, update the return type accordingly.
4168 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4174 Rtyp := Etype (Spec_Id);
4176 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4177 Etyp := Directly_Designated_Type (Rtyp);
4179 if Is_Class_Wide_Type (Etyp)
4180 and then From_Limited_With (Etyp)
4183 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4189 -- Analyze any aspect specifications that appear on the subprogram body
4191 if Has_Aspects (N) then
4192 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4195 Analyze_Declarations (Declarations (N));
4197 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4199 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4200 if Present (SPARK_Pragma (Spec_Id)) then
4201 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4203 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4205 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4206 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4207 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4209 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4212 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4216 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4217 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4218 Error_Msg_Sloc := Sloc (Spec_Id);
4220 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4224 -- A subprogram body "freezes" its own contract. Analyze the contract
4225 -- after the declarations of the body have been processed as pragmas
4226 -- are now chained on the contract of the subprogram body.
4228 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4230 -- Check completion, and analyze the statements
4233 Inspect_Deferred_Constant_Completion (Declarations (N));
4236 -- Deal with end of scope processing for the body
4238 Process_End_Label (HSS, 't', Current_Scope);
4241 -- If we are compiling an entry wrapper, remove the enclosing
4242 -- synchronized object from the stack.
4244 if Is_Entry_Wrapper (Body_Id) then
4248 Check_Subprogram_Order (N);
4249 Set_Analyzed (Body_Id);
4251 -- If we have a separate spec, then the analysis of the declarations
4252 -- caused the entities in the body to be chained to the spec id, but
4253 -- we want them chained to the body id. Only the formal parameters
4254 -- end up chained to the spec id in this case.
4256 if Present (Spec_Id) then
4258 -- We must conform to the categorization of our spec
4260 Validate_Categorization_Dependency (N, Spec_Id);
4262 -- And if this is a child unit, the parent units must conform
4264 if Is_Child_Unit (Spec_Id) then
4265 Validate_Categorization_Dependency
4266 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4269 -- Here is where we move entities from the spec to the body
4271 -- Case where there are entities that stay with the spec
4273 if Present (Last_Real_Spec_Entity) then
4275 -- No body entities (happens when the only real spec entities come
4276 -- from precondition and postcondition pragmas).
4278 if No (Last_Entity (Body_Id)) then
4279 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4281 -- Body entities present (formals), so chain stuff past them
4285 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4288 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4289 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4290 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4292 -- Case where there are no spec entities, in this case there can be
4293 -- no body entities either, so just move everything.
4295 -- If the body is generated for an expression function, it may have
4296 -- been preanalyzed already, if 'access was applied to it.
4299 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4300 N_Expression_Function
4302 pragma Assert (No (Last_Entity (Body_Id)));
4306 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4307 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4308 Set_First_Entity (Spec_Id, Empty);
4309 Set_Last_Entity (Spec_Id, Empty);
4313 Check_Missing_Return;
4315 -- Now we are going to check for variables that are never modified in
4316 -- the body of the procedure. But first we deal with a special case
4317 -- where we want to modify this check. If the body of the subprogram
4318 -- starts with a raise statement or its equivalent, or if the body
4319 -- consists entirely of a null statement, then it is pretty obvious that
4320 -- it is OK to not reference the parameters. For example, this might be
4321 -- the following common idiom for a stubbed function: statement of the
4322 -- procedure raises an exception. In particular this deals with the
4323 -- common idiom of a stubbed function, which appears something like:
4325 -- function F (A : Integer) return Some_Type;
4328 -- raise Program_Error;
4332 -- Here the purpose of X is simply to satisfy the annoying requirement
4333 -- in Ada that there be at least one return, and we certainly do not
4334 -- want to go posting warnings on X that it is not initialized. On
4335 -- the other hand, if X is entirely unreferenced that should still
4338 -- What we do is to detect these cases, and if we find them, flag the
4339 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4340 -- suppress unwanted warnings. For the case of the function stub above
4341 -- we have a special test to set X as apparently assigned to suppress
4348 -- Skip initial labels (for one thing this occurs when we are in
4349 -- front-end ZCX mode, but in any case it is irrelevant), and also
4350 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4352 Stm := First (Statements (HSS));
4353 while Nkind (Stm) = N_Label
4354 or else Nkind (Stm) in N_Push_xxx_Label
4359 -- Do the test on the original statement before expansion
4362 Ostm : constant Node_Id := Original_Node (Stm);
4365 -- If explicit raise statement, turn on flag
4367 if Nkind (Ostm) = N_Raise_Statement then
4368 Set_Trivial_Subprogram (Stm);
4370 -- If null statement, and no following statements, turn on flag
4372 elsif Nkind (Stm) = N_Null_Statement
4373 and then Comes_From_Source (Stm)
4374 and then No (Next (Stm))
4376 Set_Trivial_Subprogram (Stm);
4378 -- Check for explicit call cases which likely raise an exception
4380 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4381 if Is_Entity_Name (Name (Ostm)) then
4383 Ent : constant Entity_Id := Entity (Name (Ostm));
4386 -- If the procedure is marked No_Return, then likely it
4387 -- raises an exception, but in any case it is not coming
4388 -- back here, so turn on the flag.
4391 and then Ekind (Ent) = E_Procedure
4392 and then No_Return (Ent)
4394 Set_Trivial_Subprogram (Stm);
4402 -- Check for variables that are never modified
4409 -- If there is a separate spec, then transfer Never_Set_In_Source
4410 -- flags from out parameters to the corresponding entities in the
4411 -- body. The reason we do that is we want to post error flags on
4412 -- the body entities, not the spec entities.
4414 if Present (Spec_Id) then
4415 E1 := First_Entity (Spec_Id);
4416 while Present (E1) loop
4417 if Ekind (E1) = E_Out_Parameter then
4418 E2 := First_Entity (Body_Id);
4419 while Present (E2) loop
4420 exit when Chars (E1) = Chars (E2);
4424 if Present (E2) then
4425 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4433 -- Check references in body
4435 Check_References (Body_Id);
4438 -- Check for nested subprogram, and mark outer level subprogram if so
4444 if Present (Spec_Id) then
4451 Ent := Enclosing_Subprogram (Ent);
4452 exit when No (Ent) or else Is_Subprogram (Ent);
4455 if Present (Ent) then
4456 Set_Has_Nested_Subprogram (Ent);
4460 -- Restore the limited views in the spec, if any, to let the back end
4461 -- process it without running into circularities.
4463 if Exch_Views /= No_Elist then
4464 Restore_Limited_Views (Exch_Views);
4467 if Present (Desig_View) then
4468 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4472 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4473 Restore_Ghost_Mode (Saved_GM);
4474 end Analyze_Subprogram_Body_Helper;
4476 ------------------------------------
4477 -- Analyze_Subprogram_Declaration --
4478 ------------------------------------
4480 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4481 Scop : constant Entity_Id := Current_Scope;
4482 Designator : Entity_Id;
4484 Is_Completion : Boolean;
4485 -- Indicates whether a null procedure declaration is a completion
4488 -- Null procedures are not allowed in SPARK
4490 if Nkind (Specification (N)) = N_Procedure_Specification
4491 and then Null_Present (Specification (N))
4493 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4495 -- Null procedures are allowed in protected types, following the
4496 -- recent AI12-0147.
4498 if Is_Protected_Type (Current_Scope)
4499 and then Ada_Version < Ada_2012
4501 Error_Msg_N ("protected operation cannot be a null procedure", N);
4504 Analyze_Null_Procedure (N, Is_Completion);
4506 -- The null procedure acts as a body, nothing further is needed
4508 if Is_Completion then
4513 Designator := Analyze_Subprogram_Specification (Specification (N));
4515 -- A reference may already have been generated for the unit name, in
4516 -- which case the following call is redundant. However it is needed for
4517 -- declarations that are the rewriting of an expression function.
4519 Generate_Definition (Designator);
4521 -- Set the SPARK mode from the current context (may be overwritten later
4522 -- with explicit pragma). This is not done for entry barrier functions
4523 -- because they are generated outside the protected type and should not
4524 -- carry the mode of the enclosing context.
4526 if Nkind (N) = N_Subprogram_Declaration
4527 and then Is_Entry_Barrier_Function (N)
4531 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4532 Set_SPARK_Pragma_Inherited (Designator);
4535 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
4536 -- the body of this subprogram is instantiated or inlined later and out
4537 -- of context. The body uses this attribute to restore the value of the
4540 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4541 Set_Ignore_SPARK_Mode_Pragmas (Designator);
4544 if Debug_Flag_C then
4545 Write_Str ("==> subprogram spec ");
4546 Write_Name (Chars (Designator));
4547 Write_Str (" from ");
4548 Write_Location (Sloc (N));
4553 Validate_RCI_Subprogram_Declaration (N);
4554 New_Overloaded_Entity (Designator);
4555 Check_Delayed_Subprogram (Designator);
4557 -- If the type of the first formal of the current subprogram is a non-
4558 -- generic tagged private type, mark the subprogram as being a private
4559 -- primitive. Ditto if this is a function with controlling result, and
4560 -- the return type is currently private. In both cases, the type of the
4561 -- controlling argument or result must be in the current scope for the
4562 -- operation to be primitive.
4564 if Has_Controlling_Result (Designator)
4565 and then Is_Private_Type (Etype (Designator))
4566 and then Scope (Etype (Designator)) = Current_Scope
4567 and then not Is_Generic_Actual_Type (Etype (Designator))
4569 Set_Is_Private_Primitive (Designator);
4571 elsif Present (First_Formal (Designator)) then
4573 Formal_Typ : constant Entity_Id :=
4574 Etype (First_Formal (Designator));
4576 Set_Is_Private_Primitive (Designator,
4577 Is_Tagged_Type (Formal_Typ)
4578 and then Scope (Formal_Typ) = Current_Scope
4579 and then Is_Private_Type (Formal_Typ)
4580 and then not Is_Generic_Actual_Type (Formal_Typ));
4584 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4587 if Ada_Version >= Ada_2005
4588 and then Comes_From_Source (N)
4589 and then Is_Dispatching_Operation (Designator)
4596 if Has_Controlling_Result (Designator) then
4597 Etyp := Etype (Designator);
4600 E := First_Entity (Designator);
4602 and then Is_Formal (E)
4603 and then not Is_Controlling_Formal (E)
4611 if Is_Access_Type (Etyp) then
4612 Etyp := Directly_Designated_Type (Etyp);
4615 if Is_Interface (Etyp)
4616 and then not Is_Abstract_Subprogram (Designator)
4617 and then not (Ekind (Designator) = E_Procedure
4618 and then Null_Present (Specification (N)))
4620 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4622 -- Specialize error message based on procedures vs. functions,
4623 -- since functions can't be null subprograms.
4625 if Ekind (Designator) = E_Procedure then
4627 ("interface procedure % must be abstract or null", N);
4630 ("interface function % must be abstract", N);
4636 -- What is the following code for, it used to be
4638 -- ??? Set_Suppress_Elaboration_Checks
4639 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4641 -- The following seems equivalent, but a bit dubious
4643 if Elaboration_Checks_Suppressed (Designator) then
4644 Set_Kill_Elaboration_Checks (Designator);
4647 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4648 Set_Categorization_From_Scope (Designator, Scop);
4651 -- For a compilation unit, check for library-unit pragmas
4653 Push_Scope (Designator);
4654 Set_Categorization_From_Pragmas (N);
4655 Validate_Categorization_Dependency (N, Designator);
4659 -- For a compilation unit, set body required. This flag will only be
4660 -- reset if a valid Import or Interface pragma is processed later on.
4662 if Nkind (Parent (N)) = N_Compilation_Unit then
4663 Set_Body_Required (Parent (N), True);
4665 if Ada_Version >= Ada_2005
4666 and then Nkind (Specification (N)) = N_Procedure_Specification
4667 and then Null_Present (Specification (N))
4670 ("null procedure cannot be declared at library level", N);
4674 Generate_Reference_To_Formals (Designator);
4675 Check_Eliminated (Designator);
4677 if Debug_Flag_C then
4679 Write_Str ("<== subprogram spec ");
4680 Write_Name (Chars (Designator));
4681 Write_Str (" from ");
4682 Write_Location (Sloc (N));
4686 if Is_Protected_Type (Current_Scope) then
4688 -- Indicate that this is a protected operation, because it may be
4689 -- used in subsequent declarations within the protected type.
4691 Set_Convention (Designator, Convention_Protected);
4694 List_Inherited_Pre_Post_Aspects (Designator);
4696 if Has_Aspects (N) then
4697 Analyze_Aspect_Specifications (N, Designator);
4699 end Analyze_Subprogram_Declaration;
4701 --------------------------------------
4702 -- Analyze_Subprogram_Specification --
4703 --------------------------------------
4705 -- Reminder: N here really is a subprogram specification (not a subprogram
4706 -- declaration). This procedure is called to analyze the specification in
4707 -- both subprogram bodies and subprogram declarations (specs).
4709 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4710 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
4711 -- Determine whether entity E denotes the spec or body of an invariant
4714 ------------------------------------
4715 -- Is_Invariant_Procedure_Or_Body --
4716 ------------------------------------
4718 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
4719 Decl : constant Node_Id := Unit_Declaration_Node (E);
4723 if Nkind (Decl) = N_Subprogram_Body then
4724 Spec := Corresponding_Spec (Decl);
4731 and then Ekind (Spec) = E_Procedure
4732 and then (Is_Partial_Invariant_Procedure (Spec)
4733 or else Is_Invariant_Procedure (Spec));
4734 end Is_Invariant_Procedure_Or_Body;
4738 Designator : constant Entity_Id := Defining_Entity (N);
4739 Formals : constant List_Id := Parameter_Specifications (N);
4741 -- Start of processing for Analyze_Subprogram_Specification
4744 -- User-defined operator is not allowed in SPARK, except as a renaming
4746 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4747 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4749 Check_SPARK_05_Restriction
4750 ("user-defined operator is not allowed", N);
4753 -- Proceed with analysis. Do not emit a cross-reference entry if the
4754 -- specification comes from an expression function, because it may be
4755 -- the completion of a previous declaration. It is not, the cross-
4756 -- reference entry will be emitted for the new subprogram declaration.
4758 if Nkind (Parent (N)) /= N_Expression_Function then
4759 Generate_Definition (Designator);
4762 if Nkind (N) = N_Function_Specification then
4763 Set_Ekind (Designator, E_Function);
4764 Set_Mechanism (Designator, Default_Mechanism);
4766 Set_Ekind (Designator, E_Procedure);
4767 Set_Etype (Designator, Standard_Void_Type);
4770 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4771 -- those subprograms which could be inlined in GNATprove mode (because
4772 -- Body_To_Inline is non-Empty) but should not be inlined.
4774 if GNATprove_Mode then
4775 Set_Is_Inlined_Always (Designator);
4778 -- Introduce new scope for analysis of the formals and the return type
4780 Set_Scope (Designator, Current_Scope);
4782 if Present (Formals) then
4783 Push_Scope (Designator);
4784 Process_Formals (Formals, N);
4786 -- Check dimensions in N for formals with default expression
4788 Analyze_Dimension_Formals (N, Formals);
4790 -- Ada 2005 (AI-345): If this is an overriding operation of an
4791 -- inherited interface operation, and the controlling type is
4792 -- a synchronized type, replace the type with its corresponding
4793 -- record, to match the proper signature of an overriding operation.
4794 -- Same processing for an access parameter whose designated type is
4795 -- derived from a synchronized interface.
4797 -- This modification is not done for invariant procedures because
4798 -- the corresponding record may not necessarely be visible when the
4799 -- concurrent type acts as the full view of a private type.
4802 -- type Prot is private with Type_Invariant => ...;
4803 -- procedure ConcInvariant (Obj : Prot);
4805 -- protected type Prot is ...;
4806 -- type Concurrent_Record_Prot is record ...;
4807 -- procedure ConcInvariant (Obj : Prot) is
4809 -- end ConcInvariant;
4812 -- In the example above, both the spec and body of the invariant
4813 -- procedure must utilize the private type as the controlling type.
4815 if Ada_Version >= Ada_2005
4816 and then not Is_Invariant_Procedure_Or_Body (Designator)
4820 Formal_Typ : Entity_Id;
4821 Rec_Typ : Entity_Id;
4822 Desig_Typ : Entity_Id;
4825 Formal := First_Formal (Designator);
4826 while Present (Formal) loop
4827 Formal_Typ := Etype (Formal);
4829 if Is_Concurrent_Type (Formal_Typ)
4830 and then Present (Corresponding_Record_Type (Formal_Typ))
4832 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4834 if Present (Interfaces (Rec_Typ)) then
4835 Set_Etype (Formal, Rec_Typ);
4838 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4839 Desig_Typ := Designated_Type (Formal_Typ);
4841 if Is_Concurrent_Type (Desig_Typ)
4842 and then Present (Corresponding_Record_Type (Desig_Typ))
4844 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4846 if Present (Interfaces (Rec_Typ)) then
4847 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4852 Next_Formal (Formal);
4859 -- The subprogram scope is pushed and popped around the processing of
4860 -- the return type for consistency with call above to Process_Formals
4861 -- (which itself can call Analyze_Return_Type), and to ensure that any
4862 -- itype created for the return type will be associated with the proper
4865 elsif Nkind (N) = N_Function_Specification then
4866 Push_Scope (Designator);
4867 Analyze_Return_Type (N);
4873 if Nkind (N) = N_Function_Specification then
4875 -- Deal with operator symbol case
4877 if Nkind (Designator) = N_Defining_Operator_Symbol then
4878 Valid_Operator_Definition (Designator);
4881 May_Need_Actuals (Designator);
4883 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4884 -- the subprogram is abstract also. This does not apply to renaming
4885 -- declarations, where abstractness is inherited, and to subprogram
4886 -- bodies generated for stream operations, which become renamings as
4889 -- In case of primitives associated with abstract interface types
4890 -- the check is applied later (see Analyze_Subprogram_Declaration).
4892 if not Nkind_In (Original_Node (Parent (N)),
4893 N_Abstract_Subprogram_Declaration,
4894 N_Formal_Abstract_Subprogram_Declaration,
4895 N_Subprogram_Renaming_Declaration)
4897 if Is_Abstract_Type (Etype (Designator))
4898 and then not Is_Interface (Etype (Designator))
4901 ("function that returns abstract type must be abstract", N);
4903 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4904 -- access result whose designated type is abstract.
4906 elsif Ada_Version >= Ada_2012
4907 and then Nkind (Result_Definition (N)) = N_Access_Definition
4909 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4910 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4913 ("function whose access result designates abstract type "
4914 & "must be abstract", N);
4920 end Analyze_Subprogram_Specification;
4922 -----------------------
4923 -- Check_Conformance --
4924 -----------------------
4926 procedure Check_Conformance
4927 (New_Id : Entity_Id;
4929 Ctype : Conformance_Type;
4931 Conforms : out Boolean;
4932 Err_Loc : Node_Id := Empty;
4933 Get_Inst : Boolean := False;
4934 Skip_Controlling_Formals : Boolean := False)
4936 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4937 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4938 -- If Errmsg is True, then processing continues to post an error message
4939 -- for conformance error on given node. Two messages are output. The
4940 -- first message points to the previous declaration with a general "no
4941 -- conformance" message. The second is the detailed reason, supplied as
4942 -- Msg. The parameter N provide information for a possible & insertion
4943 -- in the message, and also provides the location for posting the
4944 -- message in the absence of a specified Err_Loc location.
4946 function Conventions_Match
4948 Id2 : Entity_Id) return Boolean;
4949 -- Determine whether the conventions of arbitrary entities Id1 and Id2
4952 -----------------------
4953 -- Conformance_Error --
4954 -----------------------
4956 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4963 if No (Err_Loc) then
4969 Error_Msg_Sloc := Sloc (Old_Id);
4972 when Type_Conformant =>
4973 Error_Msg_N -- CODEFIX
4974 ("not type conformant with declaration#!", Enode);
4976 when Mode_Conformant =>
4977 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4979 ("not mode conformant with operation inherited#!",
4983 ("not mode conformant with declaration#!", Enode);
4986 when Subtype_Conformant =>
4987 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4989 ("not subtype conformant with operation inherited#!",
4993 ("not subtype conformant with declaration#!", Enode);
4996 when Fully_Conformant =>
4997 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4998 Error_Msg_N -- CODEFIX
4999 ("not fully conformant with operation inherited#!",
5002 Error_Msg_N -- CODEFIX
5003 ("not fully conformant with declaration#!", Enode);
5007 Error_Msg_NE (Msg, Enode, N);
5009 end Conformance_Error;
5011 -----------------------
5012 -- Conventions_Match --
5013 -----------------------
5015 function Conventions_Match
5017 Id2 : Entity_Id) return Boolean
5020 -- Ignore the conventions of anonymous access-to-subprogram types
5021 -- and subprogram types because these are internally generated and
5022 -- the only way these may receive a convention is if they inherit
5023 -- the convention of a related subprogram.
5025 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5028 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5033 -- Otherwise compare the conventions directly
5036 return Convention (Id1) = Convention (Id2);
5038 end Conventions_Match;
5042 Old_Type : constant Entity_Id := Etype (Old_Id);
5043 New_Type : constant Entity_Id := Etype (New_Id);
5044 Old_Formal : Entity_Id;
5045 New_Formal : Entity_Id;
5046 Access_Types_Match : Boolean;
5047 Old_Formal_Base : Entity_Id;
5048 New_Formal_Base : Entity_Id;
5050 -- Start of processing for Check_Conformance
5055 -- We need a special case for operators, since they don't appear
5058 if Ctype = Type_Conformant then
5059 if Ekind (New_Id) = E_Operator
5060 and then Operator_Matches_Spec (New_Id, Old_Id)
5066 -- If both are functions/operators, check return types conform
5068 if Old_Type /= Standard_Void_Type
5070 New_Type /= Standard_Void_Type
5072 -- If we are checking interface conformance we omit controlling
5073 -- arguments and result, because we are only checking the conformance
5074 -- of the remaining parameters.
5076 if Has_Controlling_Result (Old_Id)
5077 and then Has_Controlling_Result (New_Id)
5078 and then Skip_Controlling_Formals
5082 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5083 if Ctype >= Subtype_Conformant
5084 and then not Predicates_Match (Old_Type, New_Type)
5087 ("\predicate of return type does not match!", New_Id);
5090 ("\return type does not match!", New_Id);
5096 -- Ada 2005 (AI-231): In case of anonymous access types check the
5097 -- null-exclusion and access-to-constant attributes match.
5099 if Ada_Version >= Ada_2005
5100 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5102 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5103 or else Is_Access_Constant (Etype (Old_Type)) /=
5104 Is_Access_Constant (Etype (New_Type)))
5106 Conformance_Error ("\return type does not match!", New_Id);
5110 -- If either is a function/operator and the other isn't, error
5112 elsif Old_Type /= Standard_Void_Type
5113 or else New_Type /= Standard_Void_Type
5115 Conformance_Error ("\functions can only match functions!", New_Id);
5119 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5120 -- If this is a renaming as body, refine error message to indicate that
5121 -- the conflict is with the original declaration. If the entity is not
5122 -- frozen, the conventions don't have to match, the one of the renamed
5123 -- entity is inherited.
5125 if Ctype >= Subtype_Conformant then
5126 if not Conventions_Match (Old_Id, New_Id) then
5127 if not Is_Frozen (New_Id) then
5130 elsif Present (Err_Loc)
5131 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5132 and then Present (Corresponding_Spec (Err_Loc))
5134 Error_Msg_Name_1 := Chars (New_Id);
5136 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5137 Conformance_Error ("\prior declaration for% has convention %!");
5140 Conformance_Error ("\calling conventions do not match!");
5145 elsif Is_Formal_Subprogram (Old_Id)
5146 or else Is_Formal_Subprogram (New_Id)
5148 Conformance_Error ("\formal subprograms not allowed!");
5153 -- Deal with parameters
5155 -- Note: we use the entity information, rather than going directly
5156 -- to the specification in the tree. This is not only simpler, but
5157 -- absolutely necessary for some cases of conformance tests between
5158 -- operators, where the declaration tree simply does not exist.
5160 Old_Formal := First_Formal (Old_Id);
5161 New_Formal := First_Formal (New_Id);
5162 while Present (Old_Formal) and then Present (New_Formal) loop
5163 if Is_Controlling_Formal (Old_Formal)
5164 and then Is_Controlling_Formal (New_Formal)
5165 and then Skip_Controlling_Formals
5167 -- The controlling formals will have different types when
5168 -- comparing an interface operation with its match, but both
5169 -- or neither must be access parameters.
5171 if Is_Access_Type (Etype (Old_Formal))
5173 Is_Access_Type (Etype (New_Formal))
5175 goto Skip_Controlling_Formal;
5178 ("\access parameter does not match!", New_Formal);
5182 -- Ada 2012: Mode conformance also requires that formal parameters
5183 -- be both aliased, or neither.
5185 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5186 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5188 ("\aliased parameter mismatch!", New_Formal);
5192 if Ctype = Fully_Conformant then
5194 -- Names must match. Error message is more accurate if we do
5195 -- this before checking that the types of the formals match.
5197 if Chars (Old_Formal) /= Chars (New_Formal) then
5198 Conformance_Error ("\name& does not match!", New_Formal);
5200 -- Set error posted flag on new formal as well to stop
5201 -- junk cascaded messages in some cases.
5203 Set_Error_Posted (New_Formal);
5207 -- Null exclusion must match
5209 if Null_Exclusion_Present (Parent (Old_Formal))
5211 Null_Exclusion_Present (Parent (New_Formal))
5213 -- Only give error if both come from source. This should be
5214 -- investigated some time, since it should not be needed ???
5216 if Comes_From_Source (Old_Formal)
5218 Comes_From_Source (New_Formal)
5221 ("\null exclusion for& does not match", New_Formal);
5223 -- Mark error posted on the new formal to avoid duplicated
5224 -- complaint about types not matching.
5226 Set_Error_Posted (New_Formal);
5231 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5232 -- case occurs whenever a subprogram is being renamed and one of its
5233 -- parameters imposes a null exclusion. For example:
5235 -- type T is null record;
5236 -- type Acc_T is access T;
5237 -- subtype Acc_T_Sub is Acc_T;
5239 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5240 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5243 Old_Formal_Base := Etype (Old_Formal);
5244 New_Formal_Base := Etype (New_Formal);
5247 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5248 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5251 Access_Types_Match := Ada_Version >= Ada_2005
5253 -- Ensure that this rule is only applied when New_Id is a
5254 -- renaming of Old_Id.
5256 and then Nkind (Parent (Parent (New_Id))) =
5257 N_Subprogram_Renaming_Declaration
5258 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5259 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5260 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5262 -- Now handle the allowed access-type case
5264 and then Is_Access_Type (Old_Formal_Base)
5265 and then Is_Access_Type (New_Formal_Base)
5267 -- The type kinds must match. The only exception occurs with
5268 -- multiple generics of the form:
5271 -- type F is private; type A is private;
5272 -- type F_Ptr is access F; type A_Ptr is access A;
5273 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5274 -- package F_Pack is ... package A_Pack is
5275 -- package F_Inst is
5276 -- new F_Pack (A, A_Ptr, A_P);
5278 -- When checking for conformance between the parameters of A_P
5279 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5280 -- because the compiler has transformed A_Ptr into a subtype of
5281 -- F_Ptr. We catch this case in the code below.
5283 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5285 (Is_Generic_Type (Old_Formal_Base)
5286 and then Is_Generic_Type (New_Formal_Base)
5287 and then Is_Internal (New_Formal_Base)
5288 and then Etype (Etype (New_Formal_Base)) =
5290 and then Directly_Designated_Type (Old_Formal_Base) =
5291 Directly_Designated_Type (New_Formal_Base)
5292 and then ((Is_Itype (Old_Formal_Base)
5293 and then Can_Never_Be_Null (Old_Formal_Base))
5295 (Is_Itype (New_Formal_Base)
5296 and then Can_Never_Be_Null (New_Formal_Base)));
5298 -- Types must always match. In the visible part of an instance,
5299 -- usual overloading rules for dispatching operations apply, and
5300 -- we check base types (not the actual subtypes).
5302 if In_Instance_Visible_Part
5303 and then Is_Dispatching_Operation (New_Id)
5305 if not Conforming_Types
5306 (T1 => Base_Type (Etype (Old_Formal)),
5307 T2 => Base_Type (Etype (New_Formal)),
5309 Get_Inst => Get_Inst)
5310 and then not Access_Types_Match
5312 Conformance_Error ("\type of & does not match!", New_Formal);
5316 elsif not Conforming_Types
5317 (T1 => Old_Formal_Base,
5318 T2 => New_Formal_Base,
5320 Get_Inst => Get_Inst)
5321 and then not Access_Types_Match
5323 -- Don't give error message if old type is Any_Type. This test
5324 -- avoids some cascaded errors, e.g. in case of a bad spec.
5326 if Errmsg and then Old_Formal_Base = Any_Type then
5329 if Ctype >= Subtype_Conformant
5331 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5334 ("\predicate of & does not match!", New_Formal);
5337 ("\type of & does not match!", New_Formal);
5339 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5341 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5349 -- For mode conformance, mode must match
5351 if Ctype >= Mode_Conformant then
5352 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5353 if not Ekind_In (New_Id, E_Function, E_Procedure)
5354 or else not Is_Primitive_Wrapper (New_Id)
5356 Conformance_Error ("\mode of & does not match!", New_Formal);
5360 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5362 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5364 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5367 ("\mode of & does not match!", New_Formal);
5374 -- Part of mode conformance for access types is having the same
5375 -- constant modifier.
5377 elsif Access_Types_Match
5378 and then Is_Access_Constant (Old_Formal_Base) /=
5379 Is_Access_Constant (New_Formal_Base)
5382 ("\constant modifier does not match!", New_Formal);
5387 if Ctype >= Subtype_Conformant then
5389 -- Ada 2005 (AI-231): In case of anonymous access types check
5390 -- the null-exclusion and access-to-constant attributes must
5391 -- match. For null exclusion, we test the types rather than the
5392 -- formals themselves, since the attribute is only set reliably
5393 -- on the formals in the Ada 95 case, and we exclude the case
5394 -- where Old_Formal is marked as controlling, to avoid errors
5395 -- when matching completing bodies with dispatching declarations
5396 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5398 if Ada_Version >= Ada_2005
5399 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5400 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5402 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5403 Can_Never_Be_Null (Etype (New_Formal))
5405 not Is_Controlling_Formal (Old_Formal))
5407 Is_Access_Constant (Etype (Old_Formal)) /=
5408 Is_Access_Constant (Etype (New_Formal)))
5410 -- Do not complain if error already posted on New_Formal. This
5411 -- avoids some redundant error messages.
5413 and then not Error_Posted (New_Formal)
5415 -- It is allowed to omit the null-exclusion in case of stream
5416 -- attribute subprograms. We recognize stream subprograms
5417 -- through their TSS-generated suffix.
5420 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5423 if TSS_Name /= TSS_Stream_Read
5424 and then TSS_Name /= TSS_Stream_Write
5425 and then TSS_Name /= TSS_Stream_Input
5426 and then TSS_Name /= TSS_Stream_Output
5428 -- Here we have a definite conformance error. It is worth
5429 -- special casing the error message for the case of a
5430 -- controlling formal (which excludes null).
5432 if Is_Controlling_Formal (New_Formal) then
5433 Error_Msg_Node_2 := Scope (New_Formal);
5435 ("\controlling formal & of & excludes null, "
5436 & "declaration must exclude null as well",
5439 -- Normal case (couldn't we give more detail here???)
5443 ("\type of & does not match!", New_Formal);
5452 -- Full conformance checks
5454 if Ctype = Fully_Conformant then
5456 -- We have checked already that names match
5458 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5460 -- Check default expressions for in parameters
5463 NewD : constant Boolean :=
5464 Present (Default_Value (New_Formal));
5465 OldD : constant Boolean :=
5466 Present (Default_Value (Old_Formal));
5468 if NewD or OldD then
5470 -- The old default value has been analyzed because the
5471 -- current full declaration will have frozen everything
5472 -- before. The new default value has not been analyzed,
5473 -- so analyze it now before we check for conformance.
5476 Push_Scope (New_Id);
5477 Preanalyze_Spec_Expression
5478 (Default_Value (New_Formal), Etype (New_Formal));
5482 if not (NewD and OldD)
5483 or else not Fully_Conformant_Expressions
5484 (Default_Value (Old_Formal),
5485 Default_Value (New_Formal))
5488 ("\default expression for & does not match!",
5497 -- A couple of special checks for Ada 83 mode. These checks are
5498 -- skipped if either entity is an operator in package Standard,
5499 -- or if either old or new instance is not from the source program.
5501 if Ada_Version = Ada_83
5502 and then Sloc (Old_Id) > Standard_Location
5503 and then Sloc (New_Id) > Standard_Location
5504 and then Comes_From_Source (Old_Id)
5505 and then Comes_From_Source (New_Id)
5508 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5509 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5512 -- Explicit IN must be present or absent in both cases. This
5513 -- test is required only in the full conformance case.
5515 if In_Present (Old_Param) /= In_Present (New_Param)
5516 and then Ctype = Fully_Conformant
5519 ("\(Ada 83) IN must appear in both declarations",
5524 -- Grouping (use of comma in param lists) must be the same
5525 -- This is where we catch a misconformance like:
5528 -- A : Integer; B : Integer
5530 -- which are represented identically in the tree except
5531 -- for the setting of the flags More_Ids and Prev_Ids.
5533 if More_Ids (Old_Param) /= More_Ids (New_Param)
5534 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5537 ("\grouping of & does not match!", New_Formal);
5543 -- This label is required when skipping controlling formals
5545 <<Skip_Controlling_Formal>>
5547 Next_Formal (Old_Formal);
5548 Next_Formal (New_Formal);
5551 if Present (Old_Formal) then
5552 Conformance_Error ("\too few parameters!");
5555 elsif Present (New_Formal) then
5556 Conformance_Error ("\too many parameters!", New_Formal);
5559 end Check_Conformance;
5561 -----------------------
5562 -- Check_Conventions --
5563 -----------------------
5565 procedure Check_Conventions (Typ : Entity_Id) is
5566 Ifaces_List : Elist_Id;
5568 procedure Check_Convention (Op : Entity_Id);
5569 -- Verify that the convention of inherited dispatching operation Op is
5570 -- consistent among all subprograms it overrides. In order to minimize
5571 -- the search, Search_From is utilized to designate a specific point in
5572 -- the list rather than iterating over the whole list once more.
5574 ----------------------
5575 -- Check_Convention --
5576 ----------------------
5578 procedure Check_Convention (Op : Entity_Id) is
5579 Op_Conv : constant Convention_Id := Convention (Op);
5580 Iface_Conv : Convention_Id;
5581 Iface_Elmt : Elmt_Id;
5582 Iface_Prim_Elmt : Elmt_Id;
5583 Iface_Prim : Entity_Id;
5586 Iface_Elmt := First_Elmt (Ifaces_List);
5587 while Present (Iface_Elmt) loop
5589 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5590 while Present (Iface_Prim_Elmt) loop
5591 Iface_Prim := Node (Iface_Prim_Elmt);
5592 Iface_Conv := Convention (Iface_Prim);
5594 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5595 and then Iface_Conv /= Op_Conv
5598 ("inconsistent conventions in primitive operations", Typ);
5600 Error_Msg_Name_1 := Chars (Op);
5601 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5602 Error_Msg_Sloc := Sloc (Op);
5604 if Comes_From_Source (Op) or else No (Alias (Op)) then
5605 if not Present (Overridden_Operation (Op)) then
5606 Error_Msg_N ("\\primitive % defined #", Typ);
5609 ("\\overriding operation % with "
5610 & "convention % defined #", Typ);
5613 else pragma Assert (Present (Alias (Op)));
5614 Error_Msg_Sloc := Sloc (Alias (Op));
5615 Error_Msg_N ("\\inherited operation % with "
5616 & "convention % defined #", Typ);
5619 Error_Msg_Name_1 := Chars (Op);
5620 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5621 Error_Msg_Sloc := Sloc (Iface_Prim);
5622 Error_Msg_N ("\\overridden operation % with "
5623 & "convention % defined #", Typ);
5625 -- Avoid cascading errors
5630 Next_Elmt (Iface_Prim_Elmt);
5633 Next_Elmt (Iface_Elmt);
5635 end Check_Convention;
5639 Prim_Op : Entity_Id;
5640 Prim_Op_Elmt : Elmt_Id;
5642 -- Start of processing for Check_Conventions
5645 if not Has_Interfaces (Typ) then
5649 Collect_Interfaces (Typ, Ifaces_List);
5651 -- The algorithm checks every overriding dispatching operation against
5652 -- all the corresponding overridden dispatching operations, detecting
5653 -- differences in conventions.
5655 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5656 while Present (Prim_Op_Elmt) loop
5657 Prim_Op := Node (Prim_Op_Elmt);
5659 -- A small optimization: skip the predefined dispatching operations
5660 -- since they always have the same convention.
5662 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5663 Check_Convention (Prim_Op);
5666 Next_Elmt (Prim_Op_Elmt);
5668 end Check_Conventions;
5670 ------------------------------
5671 -- Check_Delayed_Subprogram --
5672 ------------------------------
5674 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5677 procedure Possible_Freeze (T : Entity_Id);
5678 -- T is the type of either a formal parameter or of the return type.
5679 -- If T is not yet frozen and needs a delayed freeze, then the
5680 -- subprogram itself must be delayed.
5682 ---------------------
5683 -- Possible_Freeze --
5684 ---------------------
5686 procedure Possible_Freeze (T : Entity_Id) is
5688 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5689 Set_Has_Delayed_Freeze (Designator);
5691 elsif Is_Access_Type (T)
5692 and then Has_Delayed_Freeze (Designated_Type (T))
5693 and then not Is_Frozen (Designated_Type (T))
5695 Set_Has_Delayed_Freeze (Designator);
5698 end Possible_Freeze;
5700 -- Start of processing for Check_Delayed_Subprogram
5703 -- All subprograms, including abstract subprograms, may need a freeze
5704 -- node if some formal type or the return type needs one.
5706 Possible_Freeze (Etype (Designator));
5707 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5709 -- Need delayed freeze if any of the formal types themselves need
5710 -- a delayed freeze and are not yet frozen.
5712 F := First_Formal (Designator);
5713 while Present (F) loop
5714 Possible_Freeze (Etype (F));
5715 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5719 -- Mark functions that return by reference. Note that it cannot be
5720 -- done for delayed_freeze subprograms because the underlying
5721 -- returned type may not be known yet (for private types)
5723 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5725 Typ : constant Entity_Id := Etype (Designator);
5726 Utyp : constant Entity_Id := Underlying_Type (Typ);
5728 if Is_Limited_View (Typ) then
5729 Set_Returns_By_Ref (Designator);
5730 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5731 Set_Returns_By_Ref (Designator);
5735 end Check_Delayed_Subprogram;
5737 ------------------------------------
5738 -- Check_Discriminant_Conformance --
5739 ------------------------------------
5741 procedure Check_Discriminant_Conformance
5746 Old_Discr : Entity_Id := First_Discriminant (Prev);
5747 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5748 New_Discr_Id : Entity_Id;
5749 New_Discr_Type : Entity_Id;
5751 procedure Conformance_Error (Msg : String; N : Node_Id);
5752 -- Post error message for conformance error on given node. Two messages
5753 -- are output. The first points to the previous declaration with a
5754 -- general "no conformance" message. The second is the detailed reason,
5755 -- supplied as Msg. The parameter N provide information for a possible
5756 -- & insertion in the message.
5758 -----------------------
5759 -- Conformance_Error --
5760 -----------------------
5762 procedure Conformance_Error (Msg : String; N : Node_Id) is
5764 Error_Msg_Sloc := Sloc (Prev_Loc);
5765 Error_Msg_N -- CODEFIX
5766 ("not fully conformant with declaration#!", N);
5767 Error_Msg_NE (Msg, N, N);
5768 end Conformance_Error;
5770 -- Start of processing for Check_Discriminant_Conformance
5773 while Present (Old_Discr) and then Present (New_Discr) loop
5774 New_Discr_Id := Defining_Identifier (New_Discr);
5776 -- The subtype mark of the discriminant on the full type has not
5777 -- been analyzed so we do it here. For an access discriminant a new
5780 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5782 Access_Definition (N, Discriminant_Type (New_Discr));
5785 Analyze (Discriminant_Type (New_Discr));
5786 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5788 -- Ada 2005: if the discriminant definition carries a null
5789 -- exclusion, create an itype to check properly for consistency
5790 -- with partial declaration.
5792 if Is_Access_Type (New_Discr_Type)
5793 and then Null_Exclusion_Present (New_Discr)
5796 Create_Null_Excluding_Itype
5797 (T => New_Discr_Type,
5798 Related_Nod => New_Discr,
5799 Scope_Id => Current_Scope);
5803 if not Conforming_Types
5804 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5806 Conformance_Error ("type of & does not match!", New_Discr_Id);
5809 -- Treat the new discriminant as an occurrence of the old one,
5810 -- for navigation purposes, and fill in some semantic
5811 -- information, for completeness.
5813 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5814 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5815 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5820 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5821 Conformance_Error ("name & does not match!", New_Discr_Id);
5825 -- Default expressions must match
5828 NewD : constant Boolean :=
5829 Present (Expression (New_Discr));
5830 OldD : constant Boolean :=
5831 Present (Expression (Parent (Old_Discr)));
5834 if NewD or OldD then
5836 -- The old default value has been analyzed and expanded,
5837 -- because the current full declaration will have frozen
5838 -- everything before. The new default values have not been
5839 -- expanded, so expand now to check conformance.
5842 Preanalyze_Spec_Expression
5843 (Expression (New_Discr), New_Discr_Type);
5846 if not (NewD and OldD)
5847 or else not Fully_Conformant_Expressions
5848 (Expression (Parent (Old_Discr)),
5849 Expression (New_Discr))
5853 ("default expression for & does not match!",
5860 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5862 if Ada_Version = Ada_83 then
5864 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5867 -- Grouping (use of comma in param lists) must be the same
5868 -- This is where we catch a misconformance like:
5871 -- A : Integer; B : Integer
5873 -- which are represented identically in the tree except
5874 -- for the setting of the flags More_Ids and Prev_Ids.
5876 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5877 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5880 ("grouping of & does not match!", New_Discr_Id);
5886 Next_Discriminant (Old_Discr);
5890 if Present (Old_Discr) then
5891 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5894 elsif Present (New_Discr) then
5896 ("too many discriminants!", Defining_Identifier (New_Discr));
5899 end Check_Discriminant_Conformance;
5901 ----------------------------
5902 -- Check_Fully_Conformant --
5903 ----------------------------
5905 procedure Check_Fully_Conformant
5906 (New_Id : Entity_Id;
5908 Err_Loc : Node_Id := Empty)
5911 pragma Warnings (Off, Result);
5914 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5915 end Check_Fully_Conformant;
5917 --------------------------
5918 -- Check_Limited_Return --
5919 --------------------------
5921 procedure Check_Limited_Return
5927 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
5928 -- replaced by anonymous access results. This is an incompatibility with
5929 -- Ada 95. Not clear whether this should be enforced yet or perhaps
5930 -- controllable with special switch. ???
5932 -- A limited interface that is not immutably limited is OK
5934 if Is_Limited_Interface (R_Type)
5936 not (Is_Task_Interface (R_Type)
5937 or else Is_Protected_Interface (R_Type)
5938 or else Is_Synchronized_Interface (R_Type))
5942 elsif Is_Limited_Type (R_Type)
5943 and then not Is_Interface (R_Type)
5944 and then Comes_From_Source (N)
5945 and then not In_Instance_Body
5946 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
5948 -- Error in Ada 2005
5950 if Ada_Version >= Ada_2005
5951 and then not Debug_Flag_Dot_L
5952 and then not GNAT_Mode
5955 ("(Ada 2005) cannot copy object of a limited type "
5956 & "(RM-2005 6.5(5.5/2))", Expr);
5958 if Is_Limited_View (R_Type) then
5960 ("\return by reference not permitted in Ada 2005", Expr);
5963 -- Warn in Ada 95 mode, to give folks a heads up about this
5966 -- In GNAT mode, this is just a warning, to allow it to be evilly
5967 -- turned off. Otherwise it is a real error.
5969 -- In a generic context, simplify the warning because it makes no
5970 -- sense to discuss pass-by-reference or copy.
5972 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
5973 if Inside_A_Generic then
5975 ("return of limited object not permitted in Ada 2005 "
5976 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5978 elsif Is_Limited_View (R_Type) then
5980 ("return by reference not permitted in Ada 2005 "
5981 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5984 ("cannot copy object of a limited type in Ada 2005 "
5985 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5988 -- Ada 95 mode, compatibility warnings disabled
5991 return; -- skip continuation messages below
5994 if not Inside_A_Generic then
5996 ("\consider switching to return of access type", Expr);
5997 Explain_Limited_Type (R_Type, Expr);
6000 end Check_Limited_Return;
6002 ---------------------------
6003 -- Check_Mode_Conformant --
6004 ---------------------------
6006 procedure Check_Mode_Conformant
6007 (New_Id : Entity_Id;
6009 Err_Loc : Node_Id := Empty;
6010 Get_Inst : Boolean := False)
6013 pragma Warnings (Off, Result);
6016 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6017 end Check_Mode_Conformant;
6019 --------------------------------
6020 -- Check_Overriding_Indicator --
6021 --------------------------------
6023 procedure Check_Overriding_Indicator
6025 Overridden_Subp : Entity_Id;
6026 Is_Primitive : Boolean)
6032 -- No overriding indicator for literals
6034 if Ekind (Subp) = E_Enumeration_Literal then
6037 elsif Ekind (Subp) = E_Entry then
6038 Decl := Parent (Subp);
6040 -- No point in analyzing a malformed operator
6042 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6043 and then Error_Posted (Subp)
6048 Decl := Unit_Declaration_Node (Subp);
6051 if Nkind_In (Decl, N_Subprogram_Body,
6052 N_Subprogram_Body_Stub,
6053 N_Subprogram_Declaration,
6054 N_Abstract_Subprogram_Declaration,
6055 N_Subprogram_Renaming_Declaration)
6057 Spec := Specification (Decl);
6059 elsif Nkind (Decl) = N_Entry_Declaration then
6066 -- The overriding operation is type conformant with the overridden one,
6067 -- but the names of the formals are not required to match. If the names
6068 -- appear permuted in the overriding operation, this is a possible
6069 -- source of confusion that is worth diagnosing. Controlling formals
6070 -- often carry names that reflect the type, and it is not worthwhile
6071 -- requiring that their names match.
6073 if Present (Overridden_Subp)
6074 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6081 Form1 := First_Formal (Subp);
6082 Form2 := First_Formal (Overridden_Subp);
6084 -- If the overriding operation is a synchronized operation, skip
6085 -- the first parameter of the overridden operation, which is
6086 -- implicit in the new one. If the operation is declared in the
6087 -- body it is not primitive and all formals must match.
6089 if Is_Concurrent_Type (Scope (Subp))
6090 and then Is_Tagged_Type (Scope (Subp))
6091 and then not Has_Completion (Scope (Subp))
6093 Form2 := Next_Formal (Form2);
6096 if Present (Form1) then
6097 Form1 := Next_Formal (Form1);
6098 Form2 := Next_Formal (Form2);
6101 while Present (Form1) loop
6102 if not Is_Controlling_Formal (Form1)
6103 and then Present (Next_Formal (Form2))
6104 and then Chars (Form1) = Chars (Next_Formal (Form2))
6106 Error_Msg_Node_2 := Alias (Overridden_Subp);
6107 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6109 ("& does not match corresponding formal of&#",
6114 Next_Formal (Form1);
6115 Next_Formal (Form2);
6120 -- If there is an overridden subprogram, then check that there is no
6121 -- "not overriding" indicator, and mark the subprogram as overriding.
6122 -- This is not done if the overridden subprogram is marked as hidden,
6123 -- which can occur for the case of inherited controlled operations
6124 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6125 -- subprogram is not itself hidden. (Note: This condition could probably
6126 -- be simplified, leaving out the testing for the specific controlled
6127 -- cases, but it seems safer and clearer this way, and echoes similar
6128 -- special-case tests of this kind in other places.)
6130 if Present (Overridden_Subp)
6131 and then (not Is_Hidden (Overridden_Subp)
6133 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6136 and then Present (Alias (Overridden_Subp))
6137 and then not Is_Hidden (Alias (Overridden_Subp))))
6139 if Must_Not_Override (Spec) then
6140 Error_Msg_Sloc := Sloc (Overridden_Subp);
6142 if Ekind (Subp) = E_Entry then
6144 ("entry & overrides inherited operation #", Spec, Subp);
6147 ("subprogram & overrides inherited operation #", Spec, Subp);
6150 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6151 -- as an extension of Root_Controlled, and thus has a useless Adjust
6152 -- operation. This operation should not be inherited by other limited
6153 -- controlled types. An explicit Adjust for them is not overriding.
6155 elsif Must_Override (Spec)
6156 and then Chars (Overridden_Subp) = Name_Adjust
6157 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6158 and then Present (Alias (Overridden_Subp))
6160 Is_Predefined_File_Name
6161 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
6164 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6165 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6167 elsif Is_Subprogram (Subp) then
6168 if Is_Init_Proc (Subp) then
6171 elsif No (Overridden_Operation (Subp)) then
6173 -- For entities generated by Derive_Subprograms the overridden
6174 -- operation is the inherited primitive (which is available
6175 -- through the attribute alias)
6177 if (Is_Dispatching_Operation (Subp)
6178 or else Is_Dispatching_Operation (Overridden_Subp))
6179 and then not Comes_From_Source (Overridden_Subp)
6180 and then Find_Dispatching_Type (Overridden_Subp) =
6181 Find_Dispatching_Type (Subp)
6182 and then Present (Alias (Overridden_Subp))
6183 and then Comes_From_Source (Alias (Overridden_Subp))
6185 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6186 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6189 Set_Overridden_Operation (Subp, Overridden_Subp);
6190 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6195 -- If primitive flag is set or this is a protected operation, then
6196 -- the operation is overriding at the point of its declaration, so
6197 -- warn if necessary. Otherwise it may have been declared before the
6198 -- operation it overrides and no check is required.
6201 and then not Must_Override (Spec)
6202 and then (Is_Primitive
6203 or else Ekind (Scope (Subp)) = E_Protected_Type)
6205 Style.Missing_Overriding (Decl, Subp);
6208 -- If Subp is an operator, it may override a predefined operation, if
6209 -- it is defined in the same scope as the type to which it applies.
6210 -- In that case Overridden_Subp is empty because of our implicit
6211 -- representation for predefined operators. We have to check whether the
6212 -- signature of Subp matches that of a predefined operator. Note that
6213 -- first argument provides the name of the operator, and the second
6214 -- argument the signature that may match that of a standard operation.
6215 -- If the indicator is overriding, then the operator must match a
6216 -- predefined signature, because we know already that there is no
6217 -- explicit overridden operation.
6219 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6220 if Must_Not_Override (Spec) then
6222 -- If this is not a primitive or a protected subprogram, then
6223 -- "not overriding" is illegal.
6226 and then Ekind (Scope (Subp)) /= E_Protected_Type
6228 Error_Msg_N ("overriding indicator only allowed "
6229 & "if subprogram is primitive", Subp);
6231 elsif Can_Override_Operator (Subp) then
6233 ("subprogram& overrides predefined operator ", Spec, Subp);
6236 elsif Must_Override (Spec) then
6237 if No (Overridden_Operation (Subp))
6238 and then not Can_Override_Operator (Subp)
6240 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6243 elsif not Error_Posted (Subp)
6244 and then Style_Check
6245 and then Can_Override_Operator (Subp)
6247 not Is_Predefined_File_Name
6248 (Unit_File_Name (Get_Source_Unit (Subp)))
6250 -- If style checks are enabled, indicate that the indicator is
6251 -- missing. However, at the point of declaration, the type of
6252 -- which this is a primitive operation may be private, in which
6253 -- case the indicator would be premature.
6255 if Has_Private_Declaration (Etype (Subp))
6256 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6260 Style.Missing_Overriding (Decl, Subp);
6264 elsif Must_Override (Spec) then
6265 if Ekind (Subp) = E_Entry then
6266 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6268 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6271 -- If the operation is marked "not overriding" and it's not primitive
6272 -- then an error is issued, unless this is an operation of a task or
6273 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6274 -- has been specified have already been checked above.
6276 elsif Must_Not_Override (Spec)
6277 and then not Is_Primitive
6278 and then Ekind (Subp) /= E_Entry
6279 and then Ekind (Scope (Subp)) /= E_Protected_Type
6282 ("overriding indicator only allowed if subprogram is primitive",
6286 end Check_Overriding_Indicator;
6292 -- Note: this procedure needs to know far too much about how the expander
6293 -- messes with exceptions. The use of the flag Exception_Junk and the
6294 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6295 -- works, but is not very clean. It would be better if the expansion
6296 -- routines would leave Original_Node working nicely, and we could use
6297 -- Original_Node here to ignore all the peculiar expander messing ???
6299 procedure Check_Returns
6303 Proc : Entity_Id := Empty)
6307 procedure Check_Statement_Sequence (L : List_Id);
6308 -- Internal recursive procedure to check a list of statements for proper
6309 -- termination by a return statement (or a transfer of control or a
6310 -- compound statement that is itself internally properly terminated).
6312 ------------------------------
6313 -- Check_Statement_Sequence --
6314 ------------------------------
6316 procedure Check_Statement_Sequence (L : List_Id) is
6321 function Assert_False return Boolean;
6322 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6323 -- rewritten as a null statement when assertions are off. The assert
6324 -- is not active, but it is still enough to kill the warning.
6330 function Assert_False return Boolean is
6331 Orig : constant Node_Id := Original_Node (Last_Stm);
6334 if Nkind (Orig) = N_Pragma
6335 and then Pragma_Name (Orig) = Name_Assert
6336 and then not Error_Posted (Orig)
6339 Arg : constant Node_Id :=
6340 First (Pragma_Argument_Associations (Orig));
6341 Exp : constant Node_Id := Expression (Arg);
6343 return Nkind (Exp) = N_Identifier
6344 and then Chars (Exp) = Name_False;
6354 Raise_Exception_Call : Boolean;
6355 -- Set True if statement sequence terminated by Raise_Exception call
6356 -- or a Reraise_Occurrence call.
6358 -- Start of processing for Check_Statement_Sequence
6361 Raise_Exception_Call := False;
6363 -- Get last real statement
6365 Last_Stm := Last (L);
6367 -- Deal with digging out exception handler statement sequences that
6368 -- have been transformed by the local raise to goto optimization.
6369 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6370 -- optimization has occurred, we are looking at something like:
6373 -- original stmts in block
6377 -- goto L1; | omitted if No_Exception_Propagation
6382 -- goto L3; -- skip handler when exception not raised
6384 -- <<L1>> -- target label for local exception
6398 -- and what we have to do is to dig out the estmts1 and estmts2
6399 -- sequences (which were the original sequences of statements in
6400 -- the exception handlers) and check them.
6402 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6407 exit when Nkind (Stm) /= N_Block_Statement;
6408 exit when not Exception_Junk (Stm);
6411 exit when Nkind (Stm) /= N_Label;
6412 exit when not Exception_Junk (Stm);
6413 Check_Statement_Sequence
6414 (Statements (Handled_Statement_Sequence (Next (Stm))));
6419 exit when Nkind (Stm) /= N_Goto_Statement;
6420 exit when not Exception_Junk (Stm);
6424 -- Don't count pragmas
6426 while Nkind (Last_Stm) = N_Pragma
6428 -- Don't count call to SS_Release (can happen after Raise_Exception)
6431 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6433 Nkind (Name (Last_Stm)) = N_Identifier
6435 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6437 -- Don't count exception junk
6440 (Nkind_In (Last_Stm, N_Goto_Statement,
6442 N_Object_Declaration)
6443 and then Exception_Junk (Last_Stm))
6444 or else Nkind (Last_Stm) in N_Push_xxx_Label
6445 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6447 -- Inserted code, such as finalization calls, is irrelevant: we only
6448 -- need to check original source.
6450 or else Is_Rewrite_Insertion (Last_Stm)
6455 -- Here we have the "real" last statement
6457 Kind := Nkind (Last_Stm);
6459 -- Transfer of control, OK. Note that in the No_Return procedure
6460 -- case, we already diagnosed any explicit return statements, so
6461 -- we can treat them as OK in this context.
6463 if Is_Transfer (Last_Stm) then
6466 -- Check cases of explicit non-indirect procedure calls
6468 elsif Kind = N_Procedure_Call_Statement
6469 and then Is_Entity_Name (Name (Last_Stm))
6471 -- Check call to Raise_Exception procedure which is treated
6472 -- specially, as is a call to Reraise_Occurrence.
6474 -- We suppress the warning in these cases since it is likely that
6475 -- the programmer really does not expect to deal with the case
6476 -- of Null_Occurrence, and thus would find a warning about a
6477 -- missing return curious, and raising Program_Error does not
6478 -- seem such a bad behavior if this does occur.
6480 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6481 -- behavior will be to raise Constraint_Error (see AI-329).
6483 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6485 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6487 Raise_Exception_Call := True;
6489 -- For Raise_Exception call, test first argument, if it is
6490 -- an attribute reference for a 'Identity call, then we know
6491 -- that the call cannot possibly return.
6494 Arg : constant Node_Id :=
6495 Original_Node (First_Actual (Last_Stm));
6497 if Nkind (Arg) = N_Attribute_Reference
6498 and then Attribute_Name (Arg) = Name_Identity
6505 -- If statement, need to look inside if there is an else and check
6506 -- each constituent statement sequence for proper termination.
6508 elsif Kind = N_If_Statement
6509 and then Present (Else_Statements (Last_Stm))
6511 Check_Statement_Sequence (Then_Statements (Last_Stm));
6512 Check_Statement_Sequence (Else_Statements (Last_Stm));
6514 if Present (Elsif_Parts (Last_Stm)) then
6516 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6519 while Present (Elsif_Part) loop
6520 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6528 -- Case statement, check each case for proper termination
6530 elsif Kind = N_Case_Statement then
6534 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6535 while Present (Case_Alt) loop
6536 Check_Statement_Sequence (Statements (Case_Alt));
6537 Next_Non_Pragma (Case_Alt);
6543 -- Block statement, check its handled sequence of statements
6545 elsif Kind = N_Block_Statement then
6551 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6560 -- Loop statement. If there is an iteration scheme, we can definitely
6561 -- fall out of the loop. Similarly if there is an exit statement, we
6562 -- can fall out. In either case we need a following return.
6564 elsif Kind = N_Loop_Statement then
6565 if Present (Iteration_Scheme (Last_Stm))
6566 or else Has_Exit (Entity (Identifier (Last_Stm)))
6570 -- A loop with no exit statement or iteration scheme is either
6571 -- an infinite loop, or it has some other exit (raise/return).
6572 -- In either case, no warning is required.
6578 -- Timed entry call, check entry call and delay alternatives
6580 -- Note: in expanded code, the timed entry call has been converted
6581 -- to a set of expanded statements on which the check will work
6582 -- correctly in any case.
6584 elsif Kind = N_Timed_Entry_Call then
6586 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6587 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6590 -- If statement sequence of entry call alternative is missing,
6591 -- then we can definitely fall through, and we post the error
6592 -- message on the entry call alternative itself.
6594 if No (Statements (ECA)) then
6597 -- If statement sequence of delay alternative is missing, then
6598 -- we can definitely fall through, and we post the error
6599 -- message on the delay alternative itself.
6601 -- Note: if both ECA and DCA are missing the return, then we
6602 -- post only one message, should be enough to fix the bugs.
6603 -- If not we will get a message next time on the DCA when the
6606 elsif No (Statements (DCA)) then
6609 -- Else check both statement sequences
6612 Check_Statement_Sequence (Statements (ECA));
6613 Check_Statement_Sequence (Statements (DCA));
6618 -- Conditional entry call, check entry call and else part
6620 -- Note: in expanded code, the conditional entry call has been
6621 -- converted to a set of expanded statements on which the check
6622 -- will work correctly in any case.
6624 elsif Kind = N_Conditional_Entry_Call then
6626 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6629 -- If statement sequence of entry call alternative is missing,
6630 -- then we can definitely fall through, and we post the error
6631 -- message on the entry call alternative itself.
6633 if No (Statements (ECA)) then
6636 -- Else check statement sequence and else part
6639 Check_Statement_Sequence (Statements (ECA));
6640 Check_Statement_Sequence (Else_Statements (Last_Stm));
6646 -- If we fall through, issue appropriate message
6650 -- Kill warning if last statement is a raise exception call,
6651 -- or a pragma Assert (False). Note that with assertions enabled,
6652 -- such a pragma has been converted into a raise exception call
6653 -- already, so the Assert_False is for the assertions off case.
6655 if not Raise_Exception_Call and then not Assert_False then
6657 -- In GNATprove mode, it is an error to have a missing return
6659 Error_Msg_Warn := SPARK_Mode /= On;
6661 -- Issue error message or warning
6664 ("RETURN statement missing following this statement<<!",
6667 ("\Program_Error ]<<!", Last_Stm);
6670 -- Note: we set Err even though we have not issued a warning
6671 -- because we still have a case of a missing return. This is
6672 -- an extremely marginal case, probably will never be noticed
6673 -- but we might as well get it right.
6677 -- Otherwise we have the case of a procedure marked No_Return
6680 if not Raise_Exception_Call then
6681 if GNATprove_Mode then
6683 ("implied return after this statement "
6684 & "would have raised Program_Error", Last_Stm);
6687 ("implied return after this statement "
6688 & "will raise Program_Error??", Last_Stm);
6691 Error_Msg_Warn := SPARK_Mode /= On;
6693 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6697 RE : constant Node_Id :=
6698 Make_Raise_Program_Error (Sloc (Last_Stm),
6699 Reason => PE_Implicit_Return);
6701 Insert_After (Last_Stm, RE);
6705 end Check_Statement_Sequence;
6707 -- Start of processing for Check_Returns
6711 Check_Statement_Sequence (Statements (HSS));
6713 if Present (Exception_Handlers (HSS)) then
6714 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6715 while Present (Handler) loop
6716 Check_Statement_Sequence (Statements (Handler));
6717 Next_Non_Pragma (Handler);
6722 ----------------------------
6723 -- Check_Subprogram_Order --
6724 ----------------------------
6726 procedure Check_Subprogram_Order (N : Node_Id) is
6728 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6729 -- This is used to check if S1 > S2 in the sense required by this test,
6730 -- for example nameab < namec, but name2 < name10.
6732 -----------------------------
6733 -- Subprogram_Name_Greater --
6734 -----------------------------
6736 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6741 -- Deal with special case where names are identical except for a
6742 -- numerical suffix. These are handled specially, taking the numeric
6743 -- ordering from the suffix into account.
6746 while S1 (L1) in '0' .. '9' loop
6751 while S2 (L2) in '0' .. '9' loop
6755 -- If non-numeric parts non-equal, do straight compare
6757 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6760 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6761 -- that a missing suffix is treated as numeric zero in this test.
6765 while L1 < S1'Last loop
6767 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6771 while L2 < S2'Last loop
6773 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6778 end Subprogram_Name_Greater;
6780 -- Start of processing for Check_Subprogram_Order
6783 -- Check body in alpha order if this is option
6786 and then Style_Check_Order_Subprograms
6787 and then Nkind (N) = N_Subprogram_Body
6788 and then Comes_From_Source (N)
6789 and then In_Extended_Main_Source_Unit (N)
6793 renames Scope_Stack.Table
6794 (Scope_Stack.Last).Last_Subprogram_Name;
6796 Body_Id : constant Entity_Id :=
6797 Defining_Entity (Specification (N));
6800 Get_Decoded_Name_String (Chars (Body_Id));
6803 if Subprogram_Name_Greater
6804 (LSN.all, Name_Buffer (1 .. Name_Len))
6806 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6812 LSN := new String'(Name_Buffer (1 .. Name_Len));
6815 end Check_Subprogram_Order;
6817 ------------------------------
6818 -- Check_Subtype_Conformant --
6819 ------------------------------
6821 procedure Check_Subtype_Conformant
6822 (New_Id : Entity_Id;
6824 Err_Loc : Node_Id := Empty;
6825 Skip_Controlling_Formals : Boolean := False;
6826 Get_Inst : Boolean := False)
6829 pragma Warnings (Off, Result);
6832 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6833 Skip_Controlling_Formals => Skip_Controlling_Formals,
6834 Get_Inst => Get_Inst);
6835 end Check_Subtype_Conformant;
6837 -----------------------------------
6838 -- Check_Synchronized_Overriding --
6839 -----------------------------------
6841 procedure Check_Synchronized_Overriding
6842 (Def_Id : Entity_Id;
6843 Overridden_Subp : out Entity_Id)
6845 Ifaces_List : Elist_Id;
6849 function Matches_Prefixed_View_Profile
6850 (Prim_Params : List_Id;
6851 Iface_Params : List_Id) return Boolean;
6852 -- Determine whether a subprogram's parameter profile Prim_Params
6853 -- matches that of a potentially overridden interface subprogram
6854 -- Iface_Params. Also determine if the type of first parameter of
6855 -- Iface_Params is an implemented interface.
6857 -----------------------------------
6858 -- Matches_Prefixed_View_Profile --
6859 -----------------------------------
6861 function Matches_Prefixed_View_Profile
6862 (Prim_Params : List_Id;
6863 Iface_Params : List_Id) return Boolean
6865 function Is_Implemented
6866 (Ifaces_List : Elist_Id;
6867 Iface : Entity_Id) return Boolean;
6868 -- Determine if Iface is implemented by the current task or
6871 --------------------
6872 -- Is_Implemented --
6873 --------------------
6875 function Is_Implemented
6876 (Ifaces_List : Elist_Id;
6877 Iface : Entity_Id) return Boolean
6879 Iface_Elmt : Elmt_Id;
6882 Iface_Elmt := First_Elmt (Ifaces_List);
6883 while Present (Iface_Elmt) loop
6884 if Node (Iface_Elmt) = Iface then
6888 Next_Elmt (Iface_Elmt);
6896 Iface_Id : Entity_Id;
6897 Iface_Param : Node_Id;
6898 Iface_Typ : Entity_Id;
6899 Prim_Id : Entity_Id;
6900 Prim_Param : Node_Id;
6901 Prim_Typ : Entity_Id;
6903 -- Start of processing for Matches_Prefixed_View_Profile
6906 Iface_Param := First (Iface_Params);
6907 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6909 if Is_Access_Type (Iface_Typ) then
6910 Iface_Typ := Designated_Type (Iface_Typ);
6913 Prim_Param := First (Prim_Params);
6915 -- The first parameter of the potentially overridden subprogram must
6916 -- be an interface implemented by Prim.
6918 if not Is_Interface (Iface_Typ)
6919 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6924 -- The checks on the object parameters are done, so move on to the
6925 -- rest of the parameters.
6927 if not In_Scope then
6928 Prim_Param := Next (Prim_Param);
6931 Iface_Param := Next (Iface_Param);
6932 while Present (Iface_Param) and then Present (Prim_Param) loop
6933 Iface_Id := Defining_Identifier (Iface_Param);
6934 Iface_Typ := Find_Parameter_Type (Iface_Param);
6936 Prim_Id := Defining_Identifier (Prim_Param);
6937 Prim_Typ := Find_Parameter_Type (Prim_Param);
6939 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6940 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6941 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6943 Iface_Typ := Designated_Type (Iface_Typ);
6944 Prim_Typ := Designated_Type (Prim_Typ);
6947 -- Case of multiple interface types inside a parameter profile
6949 -- (Obj_Param : in out Iface; ...; Param : Iface)
6951 -- If the interface type is implemented, then the matching type in
6952 -- the primitive should be the implementing record type.
6954 if Ekind (Iface_Typ) = E_Record_Type
6955 and then Is_Interface (Iface_Typ)
6956 and then Is_Implemented (Ifaces_List, Iface_Typ)
6958 if Prim_Typ /= Typ then
6962 -- The two parameters must be both mode and subtype conformant
6964 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6966 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6975 -- One of the two lists contains more parameters than the other
6977 if Present (Iface_Param) or else Present (Prim_Param) then
6982 end Matches_Prefixed_View_Profile;
6984 -- Start of processing for Check_Synchronized_Overriding
6987 Overridden_Subp := Empty;
6989 -- Def_Id must be an entry or a subprogram. We should skip predefined
6990 -- primitives internally generated by the front end; however at this
6991 -- stage predefined primitives are still not fully decorated. As a
6992 -- minor optimization we skip here internally generated subprograms.
6994 if (Ekind (Def_Id) /= E_Entry
6995 and then Ekind (Def_Id) /= E_Function
6996 and then Ekind (Def_Id) /= E_Procedure)
6997 or else not Comes_From_Source (Def_Id)
7002 -- Search for the concurrent declaration since it contains the list of
7003 -- all implemented interfaces. In this case, the subprogram is declared
7004 -- within the scope of a protected or a task type.
7006 if Present (Scope (Def_Id))
7007 and then Is_Concurrent_Type (Scope (Def_Id))
7008 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7010 Typ := Scope (Def_Id);
7013 -- The enclosing scope is not a synchronized type and the subprogram
7016 elsif No (First_Formal (Def_Id)) then
7019 -- The subprogram has formals and hence it may be a primitive of a
7023 Typ := Etype (First_Formal (Def_Id));
7025 if Is_Access_Type (Typ) then
7026 Typ := Directly_Designated_Type (Typ);
7029 if Is_Concurrent_Type (Typ)
7030 and then not Is_Generic_Actual_Type (Typ)
7034 -- This case occurs when the concurrent type is declared within a
7035 -- generic unit. As a result the corresponding record has been built
7036 -- and used as the type of the first formal, we just have to retrieve
7037 -- the corresponding concurrent type.
7039 elsif Is_Concurrent_Record_Type (Typ)
7040 and then not Is_Class_Wide_Type (Typ)
7041 and then Present (Corresponding_Concurrent_Type (Typ))
7043 Typ := Corresponding_Concurrent_Type (Typ);
7051 -- There is no overriding to check if this is an inherited operation in
7052 -- a type derivation for a generic actual.
7054 Collect_Interfaces (Typ, Ifaces_List);
7056 if Is_Empty_Elmt_List (Ifaces_List) then
7060 -- Determine whether entry or subprogram Def_Id overrides a primitive
7061 -- operation that belongs to one of the interfaces in Ifaces_List.
7064 Candidate : Entity_Id := Empty;
7065 Hom : Entity_Id := Empty;
7066 Subp : Entity_Id := Empty;
7069 -- Traverse the homonym chain, looking for a potentially overridden
7070 -- subprogram that belongs to an implemented interface.
7072 Hom := Current_Entity_In_Scope (Def_Id);
7073 while Present (Hom) loop
7077 or else not Is_Overloadable (Subp)
7078 or else not Is_Primitive (Subp)
7079 or else not Is_Dispatching_Operation (Subp)
7080 or else not Present (Find_Dispatching_Type (Subp))
7081 or else not Is_Interface (Find_Dispatching_Type (Subp))
7085 -- Entries and procedures can override abstract or null interface
7088 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7089 and then Ekind (Subp) = E_Procedure
7090 and then Matches_Prefixed_View_Profile
7091 (Parameter_Specifications (Parent (Def_Id)),
7092 Parameter_Specifications (Parent (Subp)))
7096 -- For an overridden subprogram Subp, check whether the mode
7097 -- of its first parameter is correct depending on the kind of
7098 -- synchronized type.
7101 Formal : constant Node_Id := First_Formal (Candidate);
7104 -- In order for an entry or a protected procedure to
7105 -- override, the first parameter of the overridden routine
7106 -- must be of mode "out", "in out", or access-to-variable.
7108 if Ekind_In (Candidate, E_Entry, E_Procedure)
7109 and then Is_Protected_Type (Typ)
7110 and then Ekind (Formal) /= E_In_Out_Parameter
7111 and then Ekind (Formal) /= E_Out_Parameter
7112 and then Nkind (Parameter_Type (Parent (Formal))) /=
7117 -- All other cases are OK since a task entry or routine does
7118 -- not have a restriction on the mode of the first parameter
7119 -- of the overridden interface routine.
7122 Overridden_Subp := Candidate;
7127 -- Functions can override abstract interface functions
7129 elsif Ekind (Def_Id) = E_Function
7130 and then Ekind (Subp) = E_Function
7131 and then Matches_Prefixed_View_Profile
7132 (Parameter_Specifications (Parent (Def_Id)),
7133 Parameter_Specifications (Parent (Subp)))
7134 and then Etype (Def_Id) = Etype (Subp)
7138 -- If an inherited subprogram is implemented by a protected
7139 -- function, then the first parameter of the inherited
7140 -- subprogram shall be of mode in, but not an access-to-
7141 -- variable parameter (RM 9.4(11/9)).
7143 if Present (First_Formal (Subp))
7144 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7146 (not Is_Access_Type (Etype (First_Formal (Subp)))
7148 Is_Access_Constant (Etype (First_Formal (Subp))))
7150 Overridden_Subp := Subp;
7155 Hom := Homonym (Hom);
7158 -- After examining all candidates for overriding, we are left with
7159 -- the best match, which is a mode-incompatible interface routine.
7161 if In_Scope and then Present (Candidate) then
7162 Error_Msg_PT (Def_Id, Candidate);
7165 Overridden_Subp := Candidate;
7168 end Check_Synchronized_Overriding;
7170 ---------------------------
7171 -- Check_Type_Conformant --
7172 ---------------------------
7174 procedure Check_Type_Conformant
7175 (New_Id : Entity_Id;
7177 Err_Loc : Node_Id := Empty)
7180 pragma Warnings (Off, Result);
7183 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7184 end Check_Type_Conformant;
7186 ---------------------------
7187 -- Can_Override_Operator --
7188 ---------------------------
7190 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7194 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7198 Typ := Base_Type (Etype (First_Formal (Subp)));
7200 -- Check explicitly that the operation is a primitive of the type
7202 return Operator_Matches_Spec (Subp, Subp)
7203 and then not Is_Generic_Type (Typ)
7204 and then Scope (Subp) = Scope (Typ)
7205 and then not Is_Class_Wide_Type (Typ);
7207 end Can_Override_Operator;
7209 ----------------------
7210 -- Conforming_Types --
7211 ----------------------
7213 function Conforming_Types
7216 Ctype : Conformance_Type;
7217 Get_Inst : Boolean := False) return Boolean
7219 function Base_Types_Match
7221 Typ_2 : Entity_Id) return Boolean;
7222 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7223 -- in different scopes (e.g. parent and child instances), then verify
7224 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7225 -- the same subtype chain. The whole purpose of this procedure is to
7226 -- prevent spurious ambiguities in an instantiation that may arise if
7227 -- two distinct generic types are instantiated with the same actual.
7229 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7230 -- An access parameter can designate an incomplete type. If the
7231 -- incomplete type is the limited view of a type from a limited_
7232 -- with_clause, check whether the non-limited view is available.
7233 -- If it is a (non-limited) incomplete type, get the full view.
7235 function Matches_Limited_With_View
7237 Typ_2 : Entity_Id) return Boolean;
7238 -- Returns True if and only if either Typ_1 denotes a limited view of
7239 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7240 -- the limited with view of a type is used in a subprogram declaration
7241 -- and the subprogram body is in the scope of a regular with clause for
7242 -- the same unit. In such a case, the two type entities are considered
7243 -- identical for purposes of conformance checking.
7245 ----------------------
7246 -- Base_Types_Match --
7247 ----------------------
7249 function Base_Types_Match
7251 Typ_2 : Entity_Id) return Boolean
7253 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7254 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7257 if Typ_1 = Typ_2 then
7260 elsif Base_1 = Base_2 then
7262 -- The following is too permissive. A more precise test should
7263 -- check that the generic actual is an ancestor subtype of the
7266 -- See code in Find_Corresponding_Spec that applies an additional
7267 -- filter to handle accidental amiguities in instances.
7270 not Is_Generic_Actual_Type (Typ_1)
7271 or else not Is_Generic_Actual_Type (Typ_2)
7272 or else Scope (Typ_1) /= Scope (Typ_2);
7274 -- If Typ_2 is a generic actual type it is declared as the subtype of
7275 -- the actual. If that actual is itself a subtype we need to use its
7276 -- own base type to check for compatibility.
7278 elsif Ekind (Base_2) = Ekind (Typ_2)
7279 and then Base_1 = Base_Type (Base_2)
7283 elsif Ekind (Base_1) = Ekind (Typ_1)
7284 and then Base_2 = Base_Type (Base_1)
7291 end Base_Types_Match;
7293 --------------------------
7294 -- Find_Designated_Type --
7295 --------------------------
7297 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7301 Desig := Directly_Designated_Type (Typ);
7303 if Ekind (Desig) = E_Incomplete_Type then
7305 -- If regular incomplete type, get full view if available
7307 if Present (Full_View (Desig)) then
7308 Desig := Full_View (Desig);
7310 -- If limited view of a type, get non-limited view if available,
7311 -- and check again for a regular incomplete type.
7313 elsif Present (Non_Limited_View (Desig)) then
7314 Desig := Get_Full_View (Non_Limited_View (Desig));
7319 end Find_Designated_Type;
7321 -------------------------------
7322 -- Matches_Limited_With_View --
7323 -------------------------------
7325 function Matches_Limited_With_View
7327 Typ_2 : Entity_Id) return Boolean
7329 function Is_Matching_Limited_View
7331 View : Entity_Id) return Boolean;
7332 -- Determine whether non-limited view View denotes type Typ in some
7333 -- conformant fashion.
7335 ------------------------------
7336 -- Is_Matching_Limited_View --
7337 ------------------------------
7339 function Is_Matching_Limited_View
7341 View : Entity_Id) return Boolean
7343 Root_Typ : Entity_Id;
7344 Root_View : Entity_Id;
7347 -- The non-limited view directly denotes the type
7352 -- The type is a subtype of the non-limited view
7354 elsif Is_Subtype_Of (Typ, View) then
7357 -- Both the non-limited view and the type denote class-wide types
7359 elsif Is_Class_Wide_Type (Typ)
7360 and then Is_Class_Wide_Type (View)
7362 Root_Typ := Root_Type (Typ);
7363 Root_View := Root_Type (View);
7365 if Root_Typ = Root_View then
7368 -- An incomplete tagged type and its full view may receive two
7369 -- distinct class-wide types when the related package has not
7370 -- been analyzed yet.
7373 -- type T is tagged; -- CW_1
7374 -- type T is tagged null record; -- CW_2
7377 -- This is because the package lacks any semantic information
7378 -- that may eventually link both views of T. As a consequence,
7379 -- a client of the limited view of Pack will see CW_2 while a
7380 -- client of the non-limited view of Pack will see CW_1.
7382 elsif Is_Incomplete_Type (Root_Typ)
7383 and then Present (Full_View (Root_Typ))
7384 and then Full_View (Root_Typ) = Root_View
7388 elsif Is_Incomplete_Type (Root_View)
7389 and then Present (Full_View (Root_View))
7390 and then Full_View (Root_View) = Root_Typ
7397 end Is_Matching_Limited_View;
7399 -- Start of processing for Matches_Limited_With_View
7402 -- In some cases a type imported through a limited_with clause, and
7403 -- its non-limited view are both visible, for example in an anonymous
7404 -- access-to-class-wide type in a formal, or when building the body
7405 -- for a subprogram renaming after the subprogram has been frozen.
7406 -- In these cases both entities designate the same type. In addition,
7407 -- if one of them is an actual in an instance, it may be a subtype of
7408 -- the non-limited view of the other.
7410 if From_Limited_With (Typ_1)
7411 and then From_Limited_With (Typ_2)
7412 and then Available_View (Typ_1) = Available_View (Typ_2)
7416 elsif From_Limited_With (Typ_1) then
7417 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7419 elsif From_Limited_With (Typ_2) then
7420 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7425 end Matches_Limited_With_View;
7429 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7431 Type_1 : Entity_Id := T1;
7432 Type_2 : Entity_Id := T2;
7434 -- Start of processing for Conforming_Types
7437 -- The context is an instance association for a formal access-to-
7438 -- subprogram type; the formal parameter types require mapping because
7439 -- they may denote other formal parameters of the generic unit.
7442 Type_1 := Get_Instance_Of (T1);
7443 Type_2 := Get_Instance_Of (T2);
7446 -- If one of the types is a view of the other introduced by a limited
7447 -- with clause, treat these as conforming for all purposes.
7449 if Matches_Limited_With_View (T1, T2) then
7452 elsif Base_Types_Match (Type_1, Type_2) then
7453 if Ctype <= Mode_Conformant then
7458 Subtypes_Statically_Match (Type_1, Type_2)
7459 and then Dimensions_Match (Type_1, Type_2);
7462 elsif Is_Incomplete_Or_Private_Type (Type_1)
7463 and then Present (Full_View (Type_1))
7464 and then Base_Types_Match (Full_View (Type_1), Type_2)
7467 Ctype <= Mode_Conformant
7468 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7470 elsif Ekind (Type_2) = E_Incomplete_Type
7471 and then Present (Full_View (Type_2))
7472 and then Base_Types_Match (Type_1, Full_View (Type_2))
7475 Ctype <= Mode_Conformant
7476 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7478 elsif Is_Private_Type (Type_2)
7479 and then In_Instance
7480 and then Present (Full_View (Type_2))
7481 and then Base_Types_Match (Type_1, Full_View (Type_2))
7484 Ctype <= Mode_Conformant
7485 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7487 -- Another confusion between views in a nested instance with an
7488 -- actual private type whose full view is not in scope.
7490 elsif Ekind (Type_2) = E_Private_Subtype
7491 and then In_Instance
7492 and then Etype (Type_2) = Type_1
7496 -- In Ada 2012, incomplete types (including limited views) can appear
7497 -- as actuals in instantiations.
7499 elsif Is_Incomplete_Type (Type_1)
7500 and then Is_Incomplete_Type (Type_2)
7501 and then (Used_As_Generic_Actual (Type_1)
7502 or else Used_As_Generic_Actual (Type_2))
7507 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7508 -- treated recursively because they carry a signature. As far as
7509 -- conformance is concerned, convention plays no role, and either
7510 -- or both could be access to protected subprograms.
7512 Are_Anonymous_Access_To_Subprogram_Types :=
7513 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7514 E_Anonymous_Access_Protected_Subprogram_Type)
7516 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7517 E_Anonymous_Access_Protected_Subprogram_Type);
7519 -- Test anonymous access type case. For this case, static subtype
7520 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7521 -- the base types because we may have built internal subtype entities
7522 -- to handle null-excluding types (see Process_Formals).
7524 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7526 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7528 -- Ada 2005 (AI-254)
7530 or else Are_Anonymous_Access_To_Subprogram_Types
7533 Desig_1 : Entity_Id;
7534 Desig_2 : Entity_Id;
7537 -- In Ada 2005, access constant indicators must match for
7538 -- subtype conformance.
7540 if Ada_Version >= Ada_2005
7541 and then Ctype >= Subtype_Conformant
7543 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7548 Desig_1 := Find_Designated_Type (Type_1);
7549 Desig_2 := Find_Designated_Type (Type_2);
7551 -- If the context is an instance association for a formal
7552 -- access-to-subprogram type; formal access parameter designated
7553 -- types require mapping because they may denote other formal
7554 -- parameters of the generic unit.
7557 Desig_1 := Get_Instance_Of (Desig_1);
7558 Desig_2 := Get_Instance_Of (Desig_2);
7561 -- It is possible for a Class_Wide_Type to be introduced for an
7562 -- incomplete type, in which case there is a separate class_ wide
7563 -- type for the full view. The types conform if their Etypes
7564 -- conform, i.e. one may be the full view of the other. This can
7565 -- only happen in the context of an access parameter, other uses
7566 -- of an incomplete Class_Wide_Type are illegal.
7568 if Is_Class_Wide_Type (Desig_1)
7570 Is_Class_Wide_Type (Desig_2)
7574 (Etype (Base_Type (Desig_1)),
7575 Etype (Base_Type (Desig_2)), Ctype);
7577 elsif Are_Anonymous_Access_To_Subprogram_Types then
7578 if Ada_Version < Ada_2005 then
7580 Ctype = Type_Conformant
7581 or else Subtypes_Statically_Match (Desig_1, Desig_2);
7583 -- We must check the conformance of the signatures themselves
7587 Conformant : Boolean;
7590 (Desig_1, Desig_2, Ctype, False, Conformant);
7595 -- A limited view of an actual matches the corresponding
7596 -- incomplete formal.
7598 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7599 and then From_Limited_With (Desig_2)
7600 and then Used_As_Generic_Actual (Etype (Desig_2))
7605 return Base_Type (Desig_1) = Base_Type (Desig_2)
7606 and then (Ctype = Type_Conformant
7608 Subtypes_Statically_Match (Desig_1, Desig_2));
7612 -- Otherwise definitely no match
7615 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7616 and then Is_Access_Type (Type_2))
7617 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7618 and then Is_Access_Type (Type_1)))
7621 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7623 May_Hide_Profile := True;
7628 end Conforming_Types;
7630 --------------------------
7631 -- Create_Extra_Formals --
7632 --------------------------
7634 procedure Create_Extra_Formals (E : Entity_Id) is
7635 First_Extra : Entity_Id := Empty;
7637 Last_Extra : Entity_Id := Empty;
7639 function Add_Extra_Formal
7640 (Assoc_Entity : Entity_Id;
7643 Suffix : String) return Entity_Id;
7644 -- Add an extra formal to the current list of formals and extra formals.
7645 -- The extra formal is added to the end of the list of extra formals,
7646 -- and also returned as the result. These formals are always of mode IN.
7647 -- The new formal has the type Typ, is declared in Scope, and its name
7648 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7649 -- The following suffixes are currently used. They should not be changed
7650 -- without coordinating with CodePeer, which makes use of these to
7651 -- provide better messages.
7653 -- O denotes the Constrained bit.
7654 -- L denotes the accessibility level.
7655 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7656 -- the full list in exp_ch6.BIP_Formal_Kind.
7658 ----------------------
7659 -- Add_Extra_Formal --
7660 ----------------------
7662 function Add_Extra_Formal
7663 (Assoc_Entity : Entity_Id;
7666 Suffix : String) return Entity_Id
7668 EF : constant Entity_Id :=
7669 Make_Defining_Identifier (Sloc (Assoc_Entity),
7670 Chars => New_External_Name (Chars (Assoc_Entity),
7674 -- A little optimization. Never generate an extra formal for the
7675 -- _init operand of an initialization procedure, since it could
7678 if Chars (Formal) = Name_uInit then
7682 Set_Ekind (EF, E_In_Parameter);
7683 Set_Actual_Subtype (EF, Typ);
7684 Set_Etype (EF, Typ);
7685 Set_Scope (EF, Scope);
7686 Set_Mechanism (EF, Default_Mechanism);
7687 Set_Formal_Validity (EF);
7689 if No (First_Extra) then
7691 Set_Extra_Formals (Scope, First_Extra);
7694 if Present (Last_Extra) then
7695 Set_Extra_Formal (Last_Extra, EF);
7701 end Add_Extra_Formal;
7705 Formal_Type : Entity_Id;
7706 P_Formal : Entity_Id := Empty;
7708 -- Start of processing for Create_Extra_Formals
7711 -- We never generate extra formals if expansion is not active because we
7712 -- don't need them unless we are generating code.
7714 if not Expander_Active then
7718 -- No need to generate extra formals in interface thunks whose target
7719 -- primitive has no extra formals.
7721 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7725 -- If this is a derived subprogram then the subtypes of the parent
7726 -- subprogram's formal parameters will be used to determine the need
7727 -- for extra formals.
7729 if Is_Overloadable (E) and then Present (Alias (E)) then
7730 P_Formal := First_Formal (Alias (E));
7733 Formal := First_Formal (E);
7734 while Present (Formal) loop
7735 Last_Extra := Formal;
7736 Next_Formal (Formal);
7739 -- If Extra_Formals were already created, don't do it again. This
7740 -- situation may arise for subprogram types created as part of
7741 -- dispatching calls (see Expand_Dispatching_Call)
7743 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7747 -- If the subprogram is a predefined dispatching subprogram then don't
7748 -- generate any extra constrained or accessibility level formals. In
7749 -- general we suppress these for internal subprograms (by not calling
7750 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7751 -- generated stream attributes do get passed through because extra
7752 -- build-in-place formals are needed in some cases (limited 'Input).
7754 if Is_Predefined_Internal_Operation (E) then
7755 goto Test_For_Func_Result_Extras;
7758 Formal := First_Formal (E);
7759 while Present (Formal) loop
7761 -- Create extra formal for supporting the attribute 'Constrained.
7762 -- The case of a private type view without discriminants also
7763 -- requires the extra formal if the underlying type has defaulted
7766 if Ekind (Formal) /= E_In_Parameter then
7767 if Present (P_Formal) then
7768 Formal_Type := Etype (P_Formal);
7770 Formal_Type := Etype (Formal);
7773 -- Do not produce extra formals for Unchecked_Union parameters.
7774 -- Jump directly to the end of the loop.
7776 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7777 goto Skip_Extra_Formal_Generation;
7780 if not Has_Discriminants (Formal_Type)
7781 and then Ekind (Formal_Type) in Private_Kind
7782 and then Present (Underlying_Type (Formal_Type))
7784 Formal_Type := Underlying_Type (Formal_Type);
7787 -- Suppress the extra formal if formal's subtype is constrained or
7788 -- indefinite, or we're compiling for Ada 2012 and the underlying
7789 -- type is tagged and limited. In Ada 2012, a limited tagged type
7790 -- can have defaulted discriminants, but 'Constrained is required
7791 -- to return True, so the formal is never needed (see AI05-0214).
7792 -- Note that this ensures consistency of calling sequences for
7793 -- dispatching operations when some types in a class have defaults
7794 -- on discriminants and others do not (and requiring the extra
7795 -- formal would introduce distributed overhead).
7797 -- If the type does not have a completion yet, treat as prior to
7798 -- Ada 2012 for consistency.
7800 if Has_Discriminants (Formal_Type)
7801 and then not Is_Constrained (Formal_Type)
7802 and then Is_Definite_Subtype (Formal_Type)
7803 and then (Ada_Version < Ada_2012
7804 or else No (Underlying_Type (Formal_Type))
7806 (Is_Limited_Type (Formal_Type)
7809 (Underlying_Type (Formal_Type)))))
7811 Set_Extra_Constrained
7812 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7816 -- Create extra formal for supporting accessibility checking. This
7817 -- is done for both anonymous access formals and formals of named
7818 -- access types that are marked as controlling formals. The latter
7819 -- case can occur when Expand_Dispatching_Call creates a subprogram
7820 -- type and substitutes the types of access-to-class-wide actuals
7821 -- for the anonymous access-to-specific-type of controlling formals.
7822 -- Base_Type is applied because in cases where there is a null
7823 -- exclusion the formal may have an access subtype.
7825 -- This is suppressed if we specifically suppress accessibility
7826 -- checks at the package level for either the subprogram, or the
7827 -- package in which it resides. However, we do not suppress it
7828 -- simply if the scope has accessibility checks suppressed, since
7829 -- this could cause trouble when clients are compiled with a
7830 -- different suppression setting. The explicit checks at the
7831 -- package level are safe from this point of view.
7833 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7834 or else (Is_Controlling_Formal (Formal)
7835 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7837 (Explicit_Suppress (E, Accessibility_Check)
7839 Explicit_Suppress (Scope (E), Accessibility_Check))
7842 or else Present (Extra_Accessibility (P_Formal)))
7844 Set_Extra_Accessibility
7845 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7848 -- This label is required when skipping extra formal generation for
7849 -- Unchecked_Union parameters.
7851 <<Skip_Extra_Formal_Generation>>
7853 if Present (P_Formal) then
7854 Next_Formal (P_Formal);
7857 Next_Formal (Formal);
7860 <<Test_For_Func_Result_Extras>>
7862 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7863 -- function call is ... determined by the point of call ...".
7865 if Needs_Result_Accessibility_Level (E) then
7866 Set_Extra_Accessibility_Of_Result
7867 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7870 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7871 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7873 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7875 Result_Subt : constant Entity_Id := Etype (E);
7876 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7877 Formal_Typ : Entity_Id;
7878 Subp_Decl : Node_Id;
7880 Discard : Entity_Id;
7881 pragma Warnings (Off, Discard);
7884 -- In the case of functions with unconstrained result subtypes,
7885 -- add a 4-state formal indicating whether the return object is
7886 -- allocated by the caller (1), or should be allocated by the
7887 -- callee on the secondary stack (2), in the global heap (3), or
7888 -- in a user-defined storage pool (4). For the moment we just use
7889 -- Natural for the type of this formal. Note that this formal
7890 -- isn't usually needed in the case where the result subtype is
7891 -- constrained, but it is needed when the function has a tagged
7892 -- result, because generally such functions can be called in a
7893 -- dispatching context and such calls must be handled like calls
7894 -- to a class-wide function.
7896 if Needs_BIP_Alloc_Form (E) then
7899 (E, Standard_Natural,
7900 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7902 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7903 -- use a user-defined pool. This formal is not added on
7904 -- ZFP as those targets do not support pools.
7906 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
7909 (E, RTE (RE_Root_Storage_Pool_Ptr),
7910 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7914 -- In the case of functions whose result type needs finalization,
7915 -- add an extra formal which represents the finalization master.
7917 if Needs_BIP_Finalization_Master (E) then
7920 (E, RTE (RE_Finalization_Master_Ptr),
7921 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7924 -- When the result type contains tasks, add two extra formals: the
7925 -- master of the tasks to be created, and the caller's activation
7928 if Has_Task (Full_Subt) then
7931 (E, RTE (RE_Master_Id),
7932 E, BIP_Formal_Suffix (BIP_Task_Master));
7935 (E, RTE (RE_Activation_Chain_Access),
7936 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7939 -- All build-in-place functions get an extra formal that will be
7940 -- passed the address of the return object within the caller.
7943 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7945 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7946 Set_Etype (Formal_Typ, Formal_Typ);
7947 Set_Depends_On_Private
7948 (Formal_Typ, Has_Private_Component (Formal_Typ));
7949 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7950 Set_Is_Access_Constant (Formal_Typ, False);
7952 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7953 -- the designated type comes from the limited view (for back-end
7956 Set_From_Limited_With
7957 (Formal_Typ, From_Limited_With (Result_Subt));
7959 Layout_Type (Formal_Typ);
7961 -- Force the definition of the Itype in case of internal function
7962 -- calls within the same or nested scope.
7964 if Is_Subprogram_Or_Generic_Subprogram (E) then
7965 Subp_Decl := Parent (E);
7967 -- The insertion point for an Itype reference should be after
7968 -- the unit declaration node of the subprogram. An exception
7969 -- to this are inherited operations from a parent type in which
7970 -- case the derived type acts as their parent.
7972 if Nkind_In (Subp_Decl, N_Function_Specification,
7973 N_Procedure_Specification)
7975 Subp_Decl := Parent (Subp_Decl);
7978 Build_Itype_Reference (Formal_Typ, Subp_Decl);
7983 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7986 end Create_Extra_Formals;
7988 -----------------------------
7989 -- Enter_Overloaded_Entity --
7990 -----------------------------
7992 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7993 function Matches_Predefined_Op return Boolean;
7994 -- This returns an approximation of whether S matches a predefined
7995 -- operator, based on the operator symbol, and the parameter and result
7996 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
7998 ---------------------------
7999 -- Matches_Predefined_Op --
8000 ---------------------------
8002 function Matches_Predefined_Op return Boolean is
8003 Formal_1 : constant Entity_Id := First_Formal (S);
8004 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8005 Op : constant Name_Id := Chars (S);
8006 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8007 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8012 if Present (Formal_2) then
8014 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8017 -- All but "&" and "**" have same-types parameters
8026 if Type_1 /= Type_2 then
8031 -- Check parameter and result types
8039 Is_Boolean_Type (Result_Type)
8040 and then Result_Type = Type_1;
8046 Is_Integer_Type (Result_Type)
8047 and then Result_Type = Type_1;
8055 Is_Numeric_Type (Result_Type)
8056 and then Result_Type = Type_1;
8062 Is_Boolean_Type (Result_Type)
8063 and then not Is_Limited_Type (Type_1);
8071 Is_Boolean_Type (Result_Type)
8072 and then (Is_Array_Type (Type_1)
8073 or else Is_Scalar_Type (Type_1));
8075 when Name_Op_Concat =>
8076 return Is_Array_Type (Result_Type);
8078 when Name_Op_Expon =>
8080 (Is_Integer_Type (Result_Type)
8081 or else Is_Floating_Point_Type (Result_Type))
8082 and then Result_Type = Type_1
8083 and then Type_2 = Standard_Integer;
8086 raise Program_Error;
8099 Is_Numeric_Type (Result_Type)
8100 and then Result_Type = Type_1;
8104 Is_Boolean_Type (Result_Type)
8105 and then Result_Type = Type_1;
8108 raise Program_Error;
8111 end Matches_Predefined_Op;
8115 E : Entity_Id := Current_Entity_In_Scope (S);
8116 C_E : Entity_Id := Current_Entity (S);
8118 -- Start of processing for Enter_Overloaded_Entity
8122 Set_Has_Homonym (E);
8123 Set_Has_Homonym (S);
8126 Set_Is_Immediately_Visible (S);
8127 Set_Scope (S, Current_Scope);
8129 -- Chain new entity if front of homonym in current scope, so that
8130 -- homonyms are contiguous.
8132 if Present (E) and then E /= C_E then
8133 while Homonym (C_E) /= E loop
8134 C_E := Homonym (C_E);
8137 Set_Homonym (C_E, S);
8141 Set_Current_Entity (S);
8146 if Is_Inherited_Operation (S) then
8147 Append_Inherited_Subprogram (S);
8149 Append_Entity (S, Current_Scope);
8152 Set_Public_Status (S);
8154 if Debug_Flag_E then
8155 Write_Str ("New overloaded entity chain: ");
8156 Write_Name (Chars (S));
8159 while Present (E) loop
8160 Write_Str (" "); Write_Int (Int (E));
8167 -- Generate warning for hiding
8170 and then Comes_From_Source (S)
8171 and then In_Extended_Main_Source_Unit (S)
8178 -- Warn unless genuine overloading. Do not emit warning on
8179 -- hiding predefined operators in Standard (these are either an
8180 -- (artifact of our implicit declarations, or simple noise) but
8181 -- keep warning on a operator defined on a local subtype, because
8182 -- of the real danger that different operators may be applied in
8183 -- various parts of the program.
8185 -- Note that if E and S have the same scope, there is never any
8186 -- hiding. Either the two conflict, and the program is illegal,
8187 -- or S is overriding an implicit inherited subprogram.
8189 if Scope (E) /= Scope (S)
8190 and then (not Is_Overloadable (E)
8191 or else Subtype_Conformant (E, S))
8192 and then (Is_Immediately_Visible (E)
8193 or else Is_Potentially_Use_Visible (S))
8195 if Scope (E) = Standard_Standard then
8196 if Nkind (S) = N_Defining_Operator_Symbol
8197 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8199 and then Matches_Predefined_Op
8202 ("declaration of & hides predefined operator?h?", S);
8205 -- E not immediately within Standard
8208 Error_Msg_Sloc := Sloc (E);
8209 Error_Msg_N ("declaration of & hides one #?h?", S);
8214 end Enter_Overloaded_Entity;
8216 -----------------------------
8217 -- Check_Untagged_Equality --
8218 -----------------------------
8220 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8221 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8222 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8226 -- This check applies only if we have a subprogram declaration with an
8227 -- untagged record type.
8229 if Nkind (Decl) /= N_Subprogram_Declaration
8230 or else not Is_Record_Type (Typ)
8231 or else Is_Tagged_Type (Typ)
8236 -- In Ada 2012 case, we will output errors or warnings depending on
8237 -- the setting of debug flag -gnatd.E.
8239 if Ada_Version >= Ada_2012 then
8240 Error_Msg_Warn := Debug_Flag_Dot_EE;
8242 -- In earlier versions of Ada, nothing to do unless we are warning on
8243 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8246 if not Warn_On_Ada_2012_Compatibility then
8251 -- Cases where the type has already been frozen
8253 if Is_Frozen (Typ) then
8255 -- If the type is not declared in a package, or if we are in the body
8256 -- of the package or in some other scope, the new operation is not
8257 -- primitive, and therefore legal, though suspicious. Should we
8258 -- generate a warning in this case ???
8260 if Ekind (Scope (Typ)) /= E_Package
8261 or else Scope (Typ) /= Current_Scope
8265 -- If the type is a generic actual (sub)type, the operation is not
8266 -- primitive either because the base type is declared elsewhere.
8268 elsif Is_Generic_Actual_Type (Typ) then
8271 -- Here we have a definite error of declaration after freezing
8274 if Ada_Version >= Ada_2012 then
8276 ("equality operator must be declared before type & is "
8277 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8279 -- In Ada 2012 mode with error turned to warning, output one
8280 -- more warning to warn that the equality operation may not
8281 -- compose. This is the consequence of ignoring the error.
8283 if Error_Msg_Warn then
8284 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8289 ("equality operator must be declared before type& is "
8290 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8293 -- If we are in the package body, we could just move the
8294 -- declaration to the package spec, so add a message saying that.
8296 if In_Package_Body (Scope (Typ)) then
8297 if Ada_Version >= Ada_2012 then
8299 ("\move declaration to package spec<<", Eq_Op);
8302 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8305 -- Otherwise try to find the freezing point
8308 Obj_Decl := Next (Parent (Typ));
8309 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8310 if Nkind (Obj_Decl) = N_Object_Declaration
8311 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8313 -- Freezing point, output warnings
8315 if Ada_Version >= Ada_2012 then
8317 ("type& is frozen by declaration??", Obj_Decl, Typ);
8319 ("\an equality operator cannot be declared after "
8324 ("type& is frozen by declaration (Ada 2012)?y?",
8327 ("\an equality operator cannot be declared after "
8328 & "this point (Ada 2012)?y?",
8340 -- Here if type is not frozen yet. It is illegal to have a primitive
8341 -- equality declared in the private part if the type is visible.
8343 elsif not In_Same_List (Parent (Typ), Decl)
8344 and then not Is_Limited_Type (Typ)
8346 -- Shouldn't we give an RM reference here???
8348 if Ada_Version >= Ada_2012 then
8350 ("equality operator appears too late<<", Eq_Op);
8353 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8356 -- No error detected
8361 end Check_Untagged_Equality;
8363 -----------------------------
8364 -- Find_Corresponding_Spec --
8365 -----------------------------
8367 function Find_Corresponding_Spec
8369 Post_Error : Boolean := True) return Entity_Id
8371 Spec : constant Node_Id := Specification (N);
8372 Designator : constant Entity_Id := Defining_Entity (Spec);
8376 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8377 -- Even if fully conformant, a body may depend on a generic actual when
8378 -- the spec does not, or vice versa, in which case they were distinct
8379 -- entities in the generic.
8381 -------------------------------
8382 -- Different_Generic_Profile --
8383 -------------------------------
8385 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8388 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8389 -- Check that the types of corresponding formals have the same
8390 -- generic actual if any. We have to account for subtypes of a
8391 -- generic formal, declared between a spec and a body, which may
8392 -- appear distinct in an instance but matched in the generic, and
8393 -- the subtype may be used either in the spec or the body of the
8394 -- subprogram being checked.
8396 -------------------------
8397 -- Same_Generic_Actual --
8398 -------------------------
8400 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8402 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
8403 -- Predicate to check whether S1 is a subtype of S2 in the source
8406 -------------------------
8407 -- Is_Declared_Subtype --
8408 -------------------------
8410 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
8412 return Comes_From_Source (Parent (S1))
8413 and then Nkind (Parent (S1)) = N_Subtype_Declaration
8414 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
8415 and then Entity (Subtype_Indication (Parent (S1))) = S2;
8416 end Is_Declared_Subtype;
8418 -- Start of processing for Same_Generic_Actual
8421 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8422 or else Is_Declared_Subtype (T1, T2)
8423 or else Is_Declared_Subtype (T2, T1);
8424 end Same_Generic_Actual;
8426 -- Start of processing for Different_Generic_Profile
8429 if not In_Instance then
8432 elsif Ekind (E) = E_Function
8433 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8438 F1 := First_Formal (Designator);
8439 F2 := First_Formal (E);
8440 while Present (F1) loop
8441 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8450 end Different_Generic_Profile;
8452 -- Start of processing for Find_Corresponding_Spec
8455 E := Current_Entity (Designator);
8456 while Present (E) loop
8458 -- We are looking for a matching spec. It must have the same scope,
8459 -- and the same name, and either be type conformant, or be the case
8460 -- of a library procedure spec and its body (which belong to one
8461 -- another regardless of whether they are type conformant or not).
8463 if Scope (E) = Current_Scope then
8464 if Current_Scope = Standard_Standard
8465 or else (Ekind (E) = Ekind (Designator)
8466 and then Type_Conformant (E, Designator))
8468 -- Within an instantiation, we know that spec and body are
8469 -- subtype conformant, because they were subtype conformant in
8470 -- the generic. We choose the subtype-conformant entity here as
8471 -- well, to resolve spurious ambiguities in the instance that
8472 -- were not present in the generic (i.e. when two different
8473 -- types are given the same actual). If we are looking for a
8474 -- spec to match a body, full conformance is expected.
8478 -- Inherit the convention and "ghostness" of the matching
8479 -- spec to ensure proper full and subtype conformance.
8481 Set_Convention (Designator, Convention (E));
8483 -- Skip past subprogram bodies and subprogram renamings that
8484 -- may appear to have a matching spec, but that aren't fully
8485 -- conformant with it. That can occur in cases where an
8486 -- actual type causes unrelated homographs in the instance.
8488 if Nkind_In (N, N_Subprogram_Body,
8489 N_Subprogram_Renaming_Declaration)
8490 and then Present (Homonym (E))
8491 and then not Fully_Conformant (Designator, E)
8495 elsif not Subtype_Conformant (Designator, E) then
8498 elsif Different_Generic_Profile (E) then
8503 -- Ada 2012 (AI05-0165): For internally generated bodies of
8504 -- null procedures locate the internally generated spec. We
8505 -- enforce mode conformance since a tagged type may inherit
8506 -- from interfaces several null primitives which differ only
8507 -- in the mode of the formals.
8509 if not (Comes_From_Source (E))
8510 and then Is_Null_Procedure (E)
8511 and then not Mode_Conformant (Designator, E)
8515 -- For null procedures coming from source that are completions,
8516 -- analysis of the generated body will establish the link.
8518 elsif Comes_From_Source (E)
8519 and then Nkind (Spec) = N_Procedure_Specification
8520 and then Null_Present (Spec)
8524 -- Expression functions can be completions, but cannot be
8525 -- completed by an explicit body.
8527 elsif Comes_From_Source (E)
8528 and then Comes_From_Source (N)
8529 and then Nkind (N) = N_Subprogram_Body
8530 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8531 N_Expression_Function
8533 Error_Msg_Sloc := Sloc (E);
8534 Error_Msg_N ("body conflicts with expression function#", N);
8537 elsif not Has_Completion (E) then
8538 if Nkind (N) /= N_Subprogram_Body_Stub then
8539 Set_Corresponding_Spec (N, E);
8542 Set_Has_Completion (E);
8545 elsif Nkind (Parent (N)) = N_Subunit then
8547 -- If this is the proper body of a subunit, the completion
8548 -- flag is set when analyzing the stub.
8552 -- If E is an internal function with a controlling result that
8553 -- was created for an operation inherited by a null extension,
8554 -- it may be overridden by a body without a previous spec (one
8555 -- more reason why these should be shunned). In that case we
8556 -- remove the generated body if present, because the current
8557 -- one is the explicit overriding.
8559 elsif Ekind (E) = E_Function
8560 and then Ada_Version >= Ada_2005
8561 and then not Comes_From_Source (E)
8562 and then Has_Controlling_Result (E)
8563 and then Is_Null_Extension (Etype (E))
8564 and then Comes_From_Source (Spec)
8566 Set_Has_Completion (E, False);
8569 and then Nkind (Parent (E)) = N_Function_Specification
8572 (Unit_Declaration_Node
8573 (Corresponding_Body (Unit_Declaration_Node (E))));
8577 -- If expansion is disabled, or if the wrapper function has
8578 -- not been generated yet, this a late body overriding an
8579 -- inherited operation, or it is an overriding by some other
8580 -- declaration before the controlling result is frozen. In
8581 -- either case this is a declaration of a new entity.
8587 -- If the body already exists, then this is an error unless
8588 -- the previous declaration is the implicit declaration of a
8589 -- derived subprogram. It is also legal for an instance to
8590 -- contain type conformant overloadable declarations (but the
8591 -- generic declaration may not), per 8.3(26/2).
8593 elsif No (Alias (E))
8594 and then not Is_Intrinsic_Subprogram (E)
8595 and then not In_Instance
8598 Error_Msg_Sloc := Sloc (E);
8600 if Is_Imported (E) then
8602 ("body not allowed for imported subprogram & declared#",
8605 Error_Msg_NE ("duplicate body for & declared#", N, E);
8609 -- Child units cannot be overloaded, so a conformance mismatch
8610 -- between body and a previous spec is an error.
8612 elsif Is_Child_Unit (E)
8614 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8616 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8621 ("body of child unit does not match previous declaration", N);
8629 -- On exit, we know that no previous declaration of subprogram exists
8632 end Find_Corresponding_Spec;
8634 ----------------------
8635 -- Fully_Conformant --
8636 ----------------------
8638 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8641 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8643 end Fully_Conformant;
8645 ----------------------------------
8646 -- Fully_Conformant_Expressions --
8647 ----------------------------------
8649 function Fully_Conformant_Expressions
8650 (Given_E1 : Node_Id;
8651 Given_E2 : Node_Id) return Boolean
8653 E1 : constant Node_Id := Original_Node (Given_E1);
8654 E2 : constant Node_Id := Original_Node (Given_E2);
8655 -- We always test conformance on original nodes, since it is possible
8656 -- for analysis and/or expansion to make things look as though they
8657 -- conform when they do not, e.g. by converting 1+2 into 3.
8659 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8660 renames Fully_Conformant_Expressions;
8662 function FCL (L1, L2 : List_Id) return Boolean;
8663 -- Compare elements of two lists for conformance. Elements have to be
8664 -- conformant, and actuals inserted as default parameters do not match
8665 -- explicit actuals with the same value.
8667 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8668 -- Compare an operator node with a function call
8674 function FCL (L1, L2 : List_Id) return Boolean is
8678 if L1 = No_List then
8684 if L2 = No_List then
8690 -- Compare two lists, skipping rewrite insertions (we want to compare
8691 -- the original trees, not the expanded versions).
8694 if Is_Rewrite_Insertion (N1) then
8696 elsif Is_Rewrite_Insertion (N2) then
8702 elsif not FCE (N1, N2) then
8715 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8716 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8721 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8726 Act := First (Actuals);
8728 if Nkind (Op_Node) in N_Binary_Op then
8729 if not FCE (Left_Opnd (Op_Node), Act) then
8736 return Present (Act)
8737 and then FCE (Right_Opnd (Op_Node), Act)
8738 and then No (Next (Act));
8742 -- Start of processing for Fully_Conformant_Expressions
8745 -- Nonconformant if paren count does not match. Note: if some idiot
8746 -- complains that we don't do this right for more than 3 levels of
8747 -- parentheses, they will be treated with the respect they deserve.
8749 if Paren_Count (E1) /= Paren_Count (E2) then
8752 -- If same entities are referenced, then they are conformant even if
8753 -- they have different forms (RM 8.3.1(19-20)).
8755 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8756 if Present (Entity (E1)) then
8757 return Entity (E1) = Entity (E2)
8759 -- One may be a discriminant that has been replaced by
8760 -- the corresponding discriminal.
8762 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8763 and then Ekind (Entity (E1)) = E_Discriminant
8764 and then Ekind (Entity (E2)) = E_In_Parameter)
8766 -- AI12-050: The loop variables of quantified expressions
8767 -- match if they have the same identifier, even though they
8768 -- are different entities.
8770 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8771 and then Ekind (Entity (E1)) = E_Loop_Parameter
8772 and then Ekind (Entity (E2)) = E_Loop_Parameter);
8774 elsif Nkind (E1) = N_Expanded_Name
8775 and then Nkind (E2) = N_Expanded_Name
8776 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8777 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8779 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8782 -- Identifiers in component associations don't always have
8783 -- entities, but their names must conform.
8785 return Nkind (E1) = N_Identifier
8786 and then Nkind (E2) = N_Identifier
8787 and then Chars (E1) = Chars (E2);
8790 elsif Nkind (E1) = N_Character_Literal
8791 and then Nkind (E2) = N_Expanded_Name
8793 return Nkind (Selector_Name (E2)) = N_Character_Literal
8794 and then Chars (E1) = Chars (Selector_Name (E2));
8796 elsif Nkind (E2) = N_Character_Literal
8797 and then Nkind (E1) = N_Expanded_Name
8799 return Nkind (Selector_Name (E1)) = N_Character_Literal
8800 and then Chars (E2) = Chars (Selector_Name (E1));
8802 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8803 return FCO (E1, E2);
8805 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8806 return FCO (E2, E1);
8808 -- Otherwise we must have the same syntactic entity
8810 elsif Nkind (E1) /= Nkind (E2) then
8813 -- At this point, we specialize by node type
8819 FCL (Expressions (E1), Expressions (E2))
8821 FCL (Component_Associations (E1),
8822 Component_Associations (E2));
8825 if Nkind (Expression (E1)) = N_Qualified_Expression
8827 Nkind (Expression (E2)) = N_Qualified_Expression
8829 return FCE (Expression (E1), Expression (E2));
8831 -- Check that the subtype marks and any constraints
8836 Indic1 : constant Node_Id := Expression (E1);
8837 Indic2 : constant Node_Id := Expression (E2);
8842 if Nkind (Indic1) /= N_Subtype_Indication then
8844 Nkind (Indic2) /= N_Subtype_Indication
8845 and then Entity (Indic1) = Entity (Indic2);
8847 elsif Nkind (Indic2) /= N_Subtype_Indication then
8849 Nkind (Indic1) /= N_Subtype_Indication
8850 and then Entity (Indic1) = Entity (Indic2);
8853 if Entity (Subtype_Mark (Indic1)) /=
8854 Entity (Subtype_Mark (Indic2))
8859 Elt1 := First (Constraints (Constraint (Indic1)));
8860 Elt2 := First (Constraints (Constraint (Indic2)));
8861 while Present (Elt1) and then Present (Elt2) loop
8862 if not FCE (Elt1, Elt2) then
8875 when N_Attribute_Reference =>
8877 Attribute_Name (E1) = Attribute_Name (E2)
8878 and then FCL (Expressions (E1), Expressions (E2));
8882 Entity (E1) = Entity (E2)
8883 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8884 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8886 when N_Membership_Test
8890 FCE (Left_Opnd (E1), Left_Opnd (E2))
8892 FCE (Right_Opnd (E1), Right_Opnd (E2));
8894 when N_Case_Expression =>
8900 if not FCE (Expression (E1), Expression (E2)) then
8904 Alt1 := First (Alternatives (E1));
8905 Alt2 := First (Alternatives (E2));
8907 if Present (Alt1) /= Present (Alt2) then
8909 elsif No (Alt1) then
8913 if not FCE (Expression (Alt1), Expression (Alt2))
8914 or else not FCL (Discrete_Choices (Alt1),
8915 Discrete_Choices (Alt2))
8926 when N_Character_Literal =>
8928 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8930 when N_Component_Association =>
8932 FCL (Choices (E1), Choices (E2))
8934 FCE (Expression (E1), Expression (E2));
8936 when N_Explicit_Dereference =>
8938 FCE (Prefix (E1), Prefix (E2));
8940 when N_Extension_Aggregate =>
8942 FCL (Expressions (E1), Expressions (E2))
8943 and then Null_Record_Present (E1) =
8944 Null_Record_Present (E2)
8945 and then FCL (Component_Associations (E1),
8946 Component_Associations (E2));
8948 when N_Function_Call =>
8950 FCE (Name (E1), Name (E2))
8952 FCL (Parameter_Associations (E1),
8953 Parameter_Associations (E2));
8955 when N_If_Expression =>
8957 FCL (Expressions (E1), Expressions (E2));
8959 when N_Indexed_Component =>
8961 FCE (Prefix (E1), Prefix (E2))
8963 FCL (Expressions (E1), Expressions (E2));
8965 when N_Integer_Literal =>
8966 return (Intval (E1) = Intval (E2));
8971 when N_Operator_Symbol =>
8973 Chars (E1) = Chars (E2);
8975 when N_Others_Choice =>
8978 when N_Parameter_Association =>
8980 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8981 and then FCE (Explicit_Actual_Parameter (E1),
8982 Explicit_Actual_Parameter (E2));
8984 when N_Qualified_Expression
8986 | N_Unchecked_Type_Conversion
8989 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8991 FCE (Expression (E1), Expression (E2));
8993 when N_Quantified_Expression =>
8994 if not FCE (Condition (E1), Condition (E2)) then
8998 if Present (Loop_Parameter_Specification (E1))
8999 and then Present (Loop_Parameter_Specification (E2))
9002 L1 : constant Node_Id :=
9003 Loop_Parameter_Specification (E1);
9004 L2 : constant Node_Id :=
9005 Loop_Parameter_Specification (E2);
9009 Reverse_Present (L1) = Reverse_Present (L2)
9011 FCE (Defining_Identifier (L1),
9012 Defining_Identifier (L2))
9014 FCE (Discrete_Subtype_Definition (L1),
9015 Discrete_Subtype_Definition (L2));
9018 elsif Present (Iterator_Specification (E1))
9019 and then Present (Iterator_Specification (E2))
9022 I1 : constant Node_Id := Iterator_Specification (E1);
9023 I2 : constant Node_Id := Iterator_Specification (E2);
9027 FCE (Defining_Identifier (I1),
9028 Defining_Identifier (I2))
9030 Of_Present (I1) = Of_Present (I2)
9032 Reverse_Present (I1) = Reverse_Present (I2)
9033 and then FCE (Name (I1), Name (I2))
9034 and then FCE (Subtype_Indication (I1),
9035 Subtype_Indication (I2));
9038 -- The quantified expressions used different specifications to
9039 -- walk their respective ranges.
9047 FCE (Low_Bound (E1), Low_Bound (E2))
9049 FCE (High_Bound (E1), High_Bound (E2));
9051 when N_Real_Literal =>
9052 return (Realval (E1) = Realval (E2));
9054 when N_Selected_Component =>
9056 FCE (Prefix (E1), Prefix (E2))
9058 FCE (Selector_Name (E1), Selector_Name (E2));
9062 FCE (Prefix (E1), Prefix (E2))
9064 FCE (Discrete_Range (E1), Discrete_Range (E2));
9066 when N_String_Literal =>
9068 S1 : constant String_Id := Strval (E1);
9069 S2 : constant String_Id := Strval (E2);
9070 L1 : constant Nat := String_Length (S1);
9071 L2 : constant Nat := String_Length (S2);
9078 for J in 1 .. L1 loop
9079 if Get_String_Char (S1, J) /=
9080 Get_String_Char (S2, J)
9092 Entity (E1) = Entity (E2)
9094 FCE (Right_Opnd (E1), Right_Opnd (E2));
9096 -- All other node types cannot appear in this context. Strictly
9097 -- we should raise a fatal internal error. Instead we just ignore
9098 -- the nodes. This means that if anyone makes a mistake in the
9099 -- expander and mucks an expression tree irretrievably, the result
9100 -- will be a failure to detect a (probably very obscure) case
9101 -- of non-conformance, which is better than bombing on some
9102 -- case where two expressions do in fact conform.
9108 end Fully_Conformant_Expressions;
9110 ----------------------------------------
9111 -- Fully_Conformant_Discrete_Subtypes --
9112 ----------------------------------------
9114 function Fully_Conformant_Discrete_Subtypes
9115 (Given_S1 : Node_Id;
9116 Given_S2 : Node_Id) return Boolean
9118 S1 : constant Node_Id := Original_Node (Given_S1);
9119 S2 : constant Node_Id := Original_Node (Given_S2);
9121 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9122 -- Special-case for a bound given by a discriminant, which in the body
9123 -- is replaced with the discriminal of the enclosing type.
9125 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9126 -- Check both bounds
9128 -----------------------
9129 -- Conforming_Bounds --
9130 -----------------------
9132 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9134 if Is_Entity_Name (B1)
9135 and then Is_Entity_Name (B2)
9136 and then Ekind (Entity (B1)) = E_Discriminant
9138 return Chars (B1) = Chars (B2);
9141 return Fully_Conformant_Expressions (B1, B2);
9143 end Conforming_Bounds;
9145 -----------------------
9146 -- Conforming_Ranges --
9147 -----------------------
9149 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9152 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9154 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9155 end Conforming_Ranges;
9157 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9160 if Nkind (S1) /= Nkind (S2) then
9163 elsif Is_Entity_Name (S1) then
9164 return Entity (S1) = Entity (S2);
9166 elsif Nkind (S1) = N_Range then
9167 return Conforming_Ranges (S1, S2);
9169 elsif Nkind (S1) = N_Subtype_Indication then
9171 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9174 (Range_Expression (Constraint (S1)),
9175 Range_Expression (Constraint (S2)));
9179 end Fully_Conformant_Discrete_Subtypes;
9181 --------------------
9182 -- Install_Entity --
9183 --------------------
9185 procedure Install_Entity (E : Entity_Id) is
9186 Prev : constant Entity_Id := Current_Entity (E);
9188 Set_Is_Immediately_Visible (E);
9189 Set_Current_Entity (E);
9190 Set_Homonym (E, Prev);
9193 ---------------------
9194 -- Install_Formals --
9195 ---------------------
9197 procedure Install_Formals (Id : Entity_Id) is
9200 F := First_Formal (Id);
9201 while Present (F) loop
9205 end Install_Formals;
9207 -----------------------------
9208 -- Is_Interface_Conformant --
9209 -----------------------------
9211 function Is_Interface_Conformant
9212 (Tagged_Type : Entity_Id;
9213 Iface_Prim : Entity_Id;
9214 Prim : Entity_Id) return Boolean
9216 -- The operation may in fact be an inherited (implicit) operation
9217 -- rather than the original interface primitive, so retrieve the
9218 -- ultimate ancestor.
9220 Iface : constant Entity_Id :=
9221 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9222 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9224 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9225 -- Return the controlling formal of Prim
9227 ------------------------
9228 -- Controlling_Formal --
9229 ------------------------
9231 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9235 E := First_Entity (Prim);
9236 while Present (E) loop
9237 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9245 end Controlling_Formal;
9249 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9250 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9252 -- Start of processing for Is_Interface_Conformant
9255 pragma Assert (Is_Subprogram (Iface_Prim)
9256 and then Is_Subprogram (Prim)
9257 and then Is_Dispatching_Operation (Iface_Prim)
9258 and then Is_Dispatching_Operation (Prim));
9260 pragma Assert (Is_Interface (Iface)
9261 or else (Present (Alias (Iface_Prim))
9264 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9266 if Prim = Iface_Prim
9267 or else not Is_Subprogram (Prim)
9268 or else Ekind (Prim) /= Ekind (Iface_Prim)
9269 or else not Is_Dispatching_Operation (Prim)
9270 or else Scope (Prim) /= Scope (Tagged_Type)
9272 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9273 or else not Primitive_Names_Match (Iface_Prim, Prim)
9277 -- The mode of the controlling formals must match
9279 elsif Present (Iface_Ctrl_F)
9280 and then Present (Prim_Ctrl_F)
9281 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9285 -- Case of a procedure, or a function whose result type matches the
9286 -- result type of the interface primitive, or a function that has no
9287 -- controlling result (I or access I).
9289 elsif Ekind (Iface_Prim) = E_Procedure
9290 or else Etype (Prim) = Etype (Iface_Prim)
9291 or else not Has_Controlling_Result (Prim)
9293 return Type_Conformant
9294 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9296 -- Case of a function returning an interface, or an access to one. Check
9297 -- that the return types correspond.
9299 elsif Implements_Interface (Typ, Iface) then
9300 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9302 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9307 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9308 Skip_Controlling_Formals => True);
9314 end Is_Interface_Conformant;
9316 ---------------------------------
9317 -- Is_Non_Overriding_Operation --
9318 ---------------------------------
9320 function Is_Non_Overriding_Operation
9321 (Prev_E : Entity_Id;
9322 New_E : Entity_Id) return Boolean
9326 G_Typ : Entity_Id := Empty;
9328 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9329 -- If F_Type is a derived type associated with a generic actual subtype,
9330 -- then return its Generic_Parent_Type attribute, else return Empty.
9332 function Types_Correspond
9333 (P_Type : Entity_Id;
9334 N_Type : Entity_Id) return Boolean;
9335 -- Returns true if and only if the types (or designated types in the
9336 -- case of anonymous access types) are the same or N_Type is derived
9337 -- directly or indirectly from P_Type.
9339 -----------------------------
9340 -- Get_Generic_Parent_Type --
9341 -----------------------------
9343 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9349 if Is_Derived_Type (F_Typ)
9350 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9352 -- The tree must be traversed to determine the parent subtype in
9353 -- the generic unit, which unfortunately isn't always available
9354 -- via semantic attributes. ??? (Note: The use of Original_Node
9355 -- is needed for cases where a full derived type has been
9358 -- If the parent type is a scalar type, the derivation creates
9359 -- an anonymous base type for it, and the source type is its
9362 if Is_Scalar_Type (F_Typ)
9363 and then not Comes_From_Source (F_Typ)
9367 (Original_Node (Parent (First_Subtype (F_Typ))));
9369 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9371 if Nkind (Defn) = N_Derived_Type_Definition then
9372 Indic := Subtype_Indication (Defn);
9374 if Nkind (Indic) = N_Subtype_Indication then
9375 G_Typ := Entity (Subtype_Mark (Indic));
9377 G_Typ := Entity (Indic);
9380 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9381 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9383 return Generic_Parent_Type (Parent (G_Typ));
9389 end Get_Generic_Parent_Type;
9391 ----------------------
9392 -- Types_Correspond --
9393 ----------------------
9395 function Types_Correspond
9396 (P_Type : Entity_Id;
9397 N_Type : Entity_Id) return Boolean
9399 Prev_Type : Entity_Id := Base_Type (P_Type);
9400 New_Type : Entity_Id := Base_Type (N_Type);
9403 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9404 Prev_Type := Designated_Type (Prev_Type);
9407 if Ekind (New_Type) = E_Anonymous_Access_Type then
9408 New_Type := Designated_Type (New_Type);
9411 if Prev_Type = New_Type then
9414 elsif not Is_Class_Wide_Type (New_Type) then
9415 while Etype (New_Type) /= New_Type loop
9416 New_Type := Etype (New_Type);
9418 if New_Type = Prev_Type then
9424 end Types_Correspond;
9426 -- Start of processing for Is_Non_Overriding_Operation
9429 -- In the case where both operations are implicit derived subprograms
9430 -- then neither overrides the other. This can only occur in certain
9431 -- obscure cases (e.g., derivation from homographs created in a generic
9434 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9437 elsif Ekind (Current_Scope) = E_Package
9438 and then Is_Generic_Instance (Current_Scope)
9439 and then In_Private_Part (Current_Scope)
9440 and then Comes_From_Source (New_E)
9442 -- We examine the formals and result type of the inherited operation,
9443 -- to determine whether their type is derived from (the instance of)
9444 -- a generic type. The first such formal or result type is the one
9447 Formal := First_Formal (Prev_E);
9449 while Present (Formal) loop
9450 F_Typ := Base_Type (Etype (Formal));
9452 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9453 F_Typ := Designated_Type (F_Typ);
9456 G_Typ := Get_Generic_Parent_Type (F_Typ);
9457 exit when Present (G_Typ);
9459 Next_Formal (Formal);
9462 -- If the function dispatches on result check the result type
9464 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9465 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9472 -- If the generic type is a private type, then the original operation
9473 -- was not overriding in the generic, because there was no primitive
9474 -- operation to override.
9476 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9477 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9478 N_Formal_Private_Type_Definition
9482 -- The generic parent type is the ancestor of a formal derived
9483 -- type declaration. We need to check whether it has a primitive
9484 -- operation that should be overridden by New_E in the generic.
9488 P_Formal : Entity_Id;
9489 N_Formal : Entity_Id;
9493 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9496 while Present (Prim_Elt) loop
9497 P_Prim := Node (Prim_Elt);
9499 if Chars (P_Prim) = Chars (New_E)
9500 and then Ekind (P_Prim) = Ekind (New_E)
9502 P_Formal := First_Formal (P_Prim);
9503 N_Formal := First_Formal (New_E);
9504 while Present (P_Formal) and then Present (N_Formal) loop
9505 P_Typ := Etype (P_Formal);
9506 N_Typ := Etype (N_Formal);
9508 if not Types_Correspond (P_Typ, N_Typ) then
9512 Next_Entity (P_Formal);
9513 Next_Entity (N_Formal);
9516 -- Found a matching primitive operation belonging to the
9517 -- formal ancestor type, so the new subprogram is
9521 and then No (N_Formal)
9522 and then (Ekind (New_E) /= E_Function
9525 (Etype (P_Prim), Etype (New_E)))
9531 Next_Elmt (Prim_Elt);
9534 -- If no match found, then the new subprogram does not override
9535 -- in the generic (nor in the instance).
9537 -- If the type in question is not abstract, and the subprogram
9538 -- is, this will be an error if the new operation is in the
9539 -- private part of the instance. Emit a warning now, which will
9540 -- make the subsequent error message easier to understand.
9542 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
9543 and then Is_Abstract_Subprogram (Prev_E)
9544 and then In_Private_Part (Current_Scope)
9546 Error_Msg_Node_2 := F_Typ;
9548 ("private operation& in generic unit does not override "
9549 & "any primitive operation of& (RM 12.3 (18))??",
9559 end Is_Non_Overriding_Operation;
9561 -------------------------------------
9562 -- List_Inherited_Pre_Post_Aspects --
9563 -------------------------------------
9565 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9567 if Opt.List_Inherited_Aspects
9568 and then Is_Subprogram_Or_Generic_Subprogram (E)
9571 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9576 for Index in Subps'Range loop
9577 Items := Contract (Subps (Index));
9579 if Present (Items) then
9580 Prag := Pre_Post_Conditions (Items);
9581 while Present (Prag) loop
9582 Error_Msg_Sloc := Sloc (Prag);
9584 if Class_Present (Prag)
9585 and then not Split_PPC (Prag)
9587 if Pragma_Name (Prag) = Name_Precondition then
9589 ("info: & inherits `Pre''Class` aspect from "
9593 ("info: & inherits `Post''Class` aspect from "
9598 Prag := Next_Pragma (Prag);
9604 end List_Inherited_Pre_Post_Aspects;
9606 ------------------------------
9607 -- Make_Inequality_Operator --
9608 ------------------------------
9610 -- S is the defining identifier of an equality operator. We build a
9611 -- subprogram declaration with the right signature. This operation is
9612 -- intrinsic, because it is always expanded as the negation of the
9613 -- call to the equality function.
9615 procedure Make_Inequality_Operator (S : Entity_Id) is
9616 Loc : constant Source_Ptr := Sloc (S);
9619 Op_Name : Entity_Id;
9621 FF : constant Entity_Id := First_Formal (S);
9622 NF : constant Entity_Id := Next_Formal (FF);
9625 -- Check that equality was properly defined, ignore call if not
9632 A : constant Entity_Id :=
9633 Make_Defining_Identifier (Sloc (FF),
9634 Chars => Chars (FF));
9636 B : constant Entity_Id :=
9637 Make_Defining_Identifier (Sloc (NF),
9638 Chars => Chars (NF));
9641 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9643 Formals := New_List (
9644 Make_Parameter_Specification (Loc,
9645 Defining_Identifier => A,
9647 New_Occurrence_Of (Etype (First_Formal (S)),
9648 Sloc (Etype (First_Formal (S))))),
9650 Make_Parameter_Specification (Loc,
9651 Defining_Identifier => B,
9653 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9654 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9657 Make_Subprogram_Declaration (Loc,
9659 Make_Function_Specification (Loc,
9660 Defining_Unit_Name => Op_Name,
9661 Parameter_Specifications => Formals,
9662 Result_Definition =>
9663 New_Occurrence_Of (Standard_Boolean, Loc)));
9665 -- Insert inequality right after equality if it is explicit or after
9666 -- the derived type when implicit. These entities are created only
9667 -- for visibility purposes, and eventually replaced in the course
9668 -- of expansion, so they do not need to be attached to the tree and
9669 -- seen by the back-end. Keeping them internal also avoids spurious
9670 -- freezing problems. The declaration is inserted in the tree for
9671 -- analysis, and removed afterwards. If the equality operator comes
9672 -- from an explicit declaration, attach the inequality immediately
9673 -- after. Else the equality is inherited from a derived type
9674 -- declaration, so insert inequality after that declaration.
9676 if No (Alias (S)) then
9677 Insert_After (Unit_Declaration_Node (S), Decl);
9678 elsif Is_List_Member (Parent (S)) then
9679 Insert_After (Parent (S), Decl);
9681 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9684 Mark_Rewrite_Insertion (Decl);
9685 Set_Is_Intrinsic_Subprogram (Op_Name);
9688 Set_Has_Completion (Op_Name);
9689 Set_Corresponding_Equality (Op_Name, S);
9690 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9692 end Make_Inequality_Operator;
9694 ----------------------
9695 -- May_Need_Actuals --
9696 ----------------------
9698 procedure May_Need_Actuals (Fun : Entity_Id) is
9703 F := First_Formal (Fun);
9705 while Present (F) loop
9706 if No (Default_Value (F)) then
9714 Set_Needs_No_Actuals (Fun, B);
9715 end May_Need_Actuals;
9717 ---------------------
9718 -- Mode_Conformant --
9719 ---------------------
9721 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9724 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9726 end Mode_Conformant;
9728 ---------------------------
9729 -- New_Overloaded_Entity --
9730 ---------------------------
9732 procedure New_Overloaded_Entity
9734 Derived_Type : Entity_Id := Empty)
9736 Overridden_Subp : Entity_Id := Empty;
9737 -- Set if the current scope has an operation that is type-conformant
9738 -- with S, and becomes hidden by S.
9740 Is_Primitive_Subp : Boolean;
9741 -- Set to True if the new subprogram is primitive
9744 -- Entity that S overrides
9746 Prev_Vis : Entity_Id := Empty;
9747 -- Predecessor of E in Homonym chain
9749 procedure Check_For_Primitive_Subprogram
9750 (Is_Primitive : out Boolean;
9751 Is_Overriding : Boolean := False);
9752 -- If the subprogram being analyzed is a primitive operation of the type
9753 -- of a formal or result, set the Has_Primitive_Operations flag on the
9754 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9755 -- corresponding flag on the entity itself for later use.
9757 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
9758 -- True if a) E is a subprogram whose first formal is a concurrent type
9759 -- defined in the scope of E that has some entry or subprogram whose
9760 -- profile matches E, or b) E is an internally built dispatching
9761 -- subprogram of a protected type and there is a matching subprogram
9762 -- defined in the enclosing scope of the protected type, or c) E is
9763 -- an entry of a synchronized type and a matching procedure has been
9764 -- previously defined in the enclosing scope of the synchronized type.
9766 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9767 -- Check that E is declared in the private part of the current package,
9768 -- or in the package body, where it may hide a previous declaration.
9769 -- We can't use In_Private_Part by itself because this flag is also
9770 -- set when freezing entities, so we must examine the place of the
9771 -- declaration in the tree, and recognize wrapper packages as well.
9773 function Is_Overriding_Alias
9775 New_E : Entity_Id) return Boolean;
9776 -- Check whether new subprogram and old subprogram are both inherited
9777 -- from subprograms that have distinct dispatch table entries. This can
9778 -- occur with derivations from instances with accidental homonyms. The
9779 -- function is conservative given that the converse is only true within
9780 -- instances that contain accidental overloadings.
9782 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
9783 -- Report conflict between entities S and E
9785 ------------------------------------
9786 -- Check_For_Primitive_Subprogram --
9787 ------------------------------------
9789 procedure Check_For_Primitive_Subprogram
9790 (Is_Primitive : out Boolean;
9791 Is_Overriding : Boolean := False)
9797 function Visible_Part_Type (T : Entity_Id) return Boolean;
9798 -- Returns true if T is declared in the visible part of the current
9799 -- package scope; otherwise returns false. Assumes that T is declared
9802 procedure Check_Private_Overriding (T : Entity_Id);
9803 -- Checks that if a primitive abstract subprogram of a visible
9804 -- abstract type is declared in a private part, then it must override
9805 -- an abstract subprogram declared in the visible part. Also checks
9806 -- that if a primitive function with a controlling result is declared
9807 -- in a private part, then it must override a function declared in
9808 -- the visible part.
9810 ------------------------------
9811 -- Check_Private_Overriding --
9812 ------------------------------
9814 procedure Check_Private_Overriding (T : Entity_Id) is
9815 function Overrides_Private_Part_Op return Boolean;
9816 -- This detects the special case where the overriding subprogram
9817 -- is overriding a subprogram that was declared in the same
9818 -- private part. That case is illegal by 3.9.3(10).
9820 function Overrides_Visible_Function
9821 (Partial_View : Entity_Id) return Boolean;
9822 -- True if S overrides a function in the visible part. The
9823 -- overridden function could be explicitly or implicitly declared.
9825 -------------------------------
9826 -- Overrides_Private_Part_Op --
9827 -------------------------------
9829 function Overrides_Private_Part_Op return Boolean is
9830 Over_Decl : constant Node_Id :=
9831 Unit_Declaration_Node (Overridden_Operation (S));
9832 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
9835 pragma Assert (Is_Overriding);
9837 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
9839 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
9841 return In_Same_List (Over_Decl, Subp_Decl);
9842 end Overrides_Private_Part_Op;
9844 --------------------------------
9845 -- Overrides_Visible_Function --
9846 --------------------------------
9848 function Overrides_Visible_Function
9849 (Partial_View : Entity_Id) return Boolean
9852 if not Is_Overriding or else not Has_Homonym (S) then
9856 if not Present (Partial_View) then
9860 -- Search through all the homonyms H of S in the current
9861 -- package spec, and return True if we find one that matches.
9862 -- Note that Parent (H) will be the declaration of the
9863 -- partial view of T for a match.
9870 exit when not Present (H) or else Scope (H) /= Scope (S);
9874 N_Private_Extension_Declaration,
9875 N_Private_Type_Declaration)
9876 and then Defining_Identifier (Parent (H)) = Partial_View
9884 end Overrides_Visible_Function;
9886 -- Start of processing for Check_Private_Overriding
9889 if Is_Package_Or_Generic_Package (Current_Scope)
9890 and then In_Private_Part (Current_Scope)
9891 and then Visible_Part_Type (T)
9892 and then not In_Instance
9894 if Is_Abstract_Type (T)
9895 and then Is_Abstract_Subprogram (S)
9896 and then (not Is_Overriding
9897 or else not Is_Abstract_Subprogram (E)
9898 or else Overrides_Private_Part_Op)
9901 ("abstract subprograms must be visible (RM 3.9.3(10))!",
9904 elsif Ekind (S) = E_Function then
9906 Partial_View : constant Entity_Id :=
9907 Incomplete_Or_Partial_View (T);
9910 if not Overrides_Visible_Function (Partial_View) then
9912 -- Here, S is "function ... return T;" declared in
9913 -- the private part, not overriding some visible
9914 -- operation. That's illegal in the tagged case
9915 -- (but not if the private type is untagged).
9917 if ((Present (Partial_View)
9918 and then Is_Tagged_Type (Partial_View))
9919 or else (not Present (Partial_View)
9920 and then Is_Tagged_Type (T)))
9921 and then T = Base_Type (Etype (S))
9924 ("private function with tagged result must"
9925 & " override visible-part function", S);
9927 ("\move subprogram to the visible part"
9928 & " (RM 3.9.3(10))", S);
9930 -- AI05-0073: extend this test to the case of a
9931 -- function with a controlling access result.
9933 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9934 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9936 not Is_Class_Wide_Type
9937 (Designated_Type (Etype (S)))
9938 and then Ada_Version >= Ada_2012
9941 ("private function with controlling access "
9942 & "result must override visible-part function",
9945 ("\move subprogram to the visible part"
9946 & " (RM 3.9.3(10))", S);
9952 end Check_Private_Overriding;
9954 -----------------------
9955 -- Visible_Part_Type --
9956 -----------------------
9958 function Visible_Part_Type (T : Entity_Id) return Boolean is
9959 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9963 -- If the entity is a private type, then it must be declared in a
9966 if Ekind (T) in Private_Kind then
9970 -- Otherwise, we traverse the visible part looking for its
9971 -- corresponding declaration. We cannot use the declaration
9972 -- node directly because in the private part the entity of a
9973 -- private type is the one in the full view, which does not
9974 -- indicate that it is the completion of something visible.
9976 N := First (Visible_Declarations (Specification (P)));
9977 while Present (N) loop
9978 if Nkind (N) = N_Full_Type_Declaration
9979 and then Present (Defining_Identifier (N))
9980 and then T = Defining_Identifier (N)
9984 elsif Nkind_In (N, N_Private_Type_Declaration,
9985 N_Private_Extension_Declaration)
9986 and then Present (Defining_Identifier (N))
9987 and then T = Full_View (Defining_Identifier (N))
9996 end Visible_Part_Type;
9998 -- Start of processing for Check_For_Primitive_Subprogram
10001 Is_Primitive := False;
10003 if not Comes_From_Source (S) then
10006 -- If subprogram is at library level, it is not primitive operation
10008 elsif Current_Scope = Standard_Standard then
10011 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10012 and then not In_Package_Body (Current_Scope))
10013 or else Is_Overriding
10015 -- For function, check return type
10017 if Ekind (S) = E_Function then
10018 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10019 F_Typ := Designated_Type (Etype (S));
10021 F_Typ := Etype (S);
10024 B_Typ := Base_Type (F_Typ);
10026 if Scope (B_Typ) = Current_Scope
10027 and then not Is_Class_Wide_Type (B_Typ)
10028 and then not Is_Generic_Type (B_Typ)
10030 Is_Primitive := True;
10031 Set_Has_Primitive_Operations (B_Typ);
10032 Set_Is_Primitive (S);
10033 Check_Private_Overriding (B_Typ);
10035 -- The Ghost policy in effect at the point of declaration
10036 -- or a tagged type and a primitive operation must match
10037 -- (SPARK RM 6.9(16)).
10039 Check_Ghost_Primitive (S, B_Typ);
10043 -- For all subprograms, check formals
10045 Formal := First_Formal (S);
10046 while Present (Formal) loop
10047 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10048 F_Typ := Designated_Type (Etype (Formal));
10050 F_Typ := Etype (Formal);
10053 B_Typ := Base_Type (F_Typ);
10055 if Ekind (B_Typ) = E_Access_Subtype then
10056 B_Typ := Base_Type (B_Typ);
10059 if Scope (B_Typ) = Current_Scope
10060 and then not Is_Class_Wide_Type (B_Typ)
10061 and then not Is_Generic_Type (B_Typ)
10063 Is_Primitive := True;
10064 Set_Is_Primitive (S);
10065 Set_Has_Primitive_Operations (B_Typ);
10066 Check_Private_Overriding (B_Typ);
10068 -- The Ghost policy in effect at the point of declaration
10069 -- of a tagged type and a primitive operation must match
10070 -- (SPARK RM 6.9(16)).
10072 Check_Ghost_Primitive (S, B_Typ);
10075 Next_Formal (Formal);
10078 -- Special case: An equality function can be redefined for a type
10079 -- occurring in a declarative part, and won't otherwise be treated as
10080 -- a primitive because it doesn't occur in a package spec and doesn't
10081 -- override an inherited subprogram. It's important that we mark it
10082 -- primitive so it can be returned by Collect_Primitive_Operations
10083 -- and be used in composing the equality operation of later types
10084 -- that have a component of the type.
10086 elsif Chars (S) = Name_Op_Eq
10087 and then Etype (S) = Standard_Boolean
10089 B_Typ := Base_Type (Etype (First_Formal (S)));
10091 if Scope (B_Typ) = Current_Scope
10093 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10094 and then not Is_Limited_Type (B_Typ)
10096 Is_Primitive := True;
10097 Set_Is_Primitive (S);
10098 Set_Has_Primitive_Operations (B_Typ);
10099 Check_Private_Overriding (B_Typ);
10101 -- The Ghost policy in effect at the point of declaration of a
10102 -- tagged type and a primitive operation must match
10103 -- (SPARK RM 6.9(16)).
10105 Check_Ghost_Primitive (S, B_Typ);
10108 end Check_For_Primitive_Subprogram;
10110 --------------------------------------
10111 -- Has_Matching_Entry_Or_Subprogram --
10112 --------------------------------------
10114 function Has_Matching_Entry_Or_Subprogram
10115 (E : Entity_Id) return Boolean
10117 function Check_Conforming_Parameters
10118 (E1_Param : Node_Id;
10119 E2_Param : Node_Id) return Boolean;
10120 -- Starting from the given parameters, check that all the parameters
10121 -- of two entries or subprograms are subtype conformant. Used to skip
10122 -- the check on the controlling argument.
10124 function Matching_Entry_Or_Subprogram
10125 (Conc_Typ : Entity_Id;
10126 Subp : Entity_Id) return Entity_Id;
10127 -- Return the first entry or subprogram of the given concurrent type
10128 -- whose name matches the name of Subp and has a profile conformant
10129 -- with Subp; return Empty if not found.
10131 function Matching_Dispatching_Subprogram
10132 (Conc_Typ : Entity_Id;
10133 Ent : Entity_Id) return Entity_Id;
10134 -- Return the first dispatching primitive of Conc_Type defined in the
10135 -- enclosing scope of Conc_Type (i.e. before the full definition of
10136 -- this concurrent type) whose name matches the entry Ent and has a
10137 -- profile conformant with the profile of the corresponding (not yet
10138 -- built) dispatching primitive of Ent; return Empty if not found.
10140 function Matching_Original_Protected_Subprogram
10141 (Prot_Typ : Entity_Id;
10142 Subp : Entity_Id) return Entity_Id;
10143 -- Return the first subprogram defined in the enclosing scope of
10144 -- Prot_Typ (before the full definition of this protected type)
10145 -- whose name matches the original name of Subp and has a profile
10146 -- conformant with the profile of Subp; return Empty if not found.
10148 ---------------------------------
10149 -- Check_Confirming_Parameters --
10150 ---------------------------------
10152 function Check_Conforming_Parameters
10153 (E1_Param : Node_Id;
10154 E2_Param : Node_Id) return Boolean
10156 Param_E1 : Node_Id := E1_Param;
10157 Param_E2 : Node_Id := E2_Param;
10160 while Present (Param_E1) and then Present (Param_E2) loop
10161 if Ekind (Defining_Identifier (Param_E1)) /=
10162 Ekind (Defining_Identifier (Param_E2))
10165 (Find_Parameter_Type (Param_E1),
10166 Find_Parameter_Type (Param_E2),
10167 Subtype_Conformant)
10176 -- The candidate is not valid if one of the two lists contains
10177 -- more parameters than the other
10179 return No (Param_E1) and then No (Param_E2);
10180 end Check_Conforming_Parameters;
10182 ----------------------------------
10183 -- Matching_Entry_Or_Subprogram --
10184 ----------------------------------
10186 function Matching_Entry_Or_Subprogram
10187 (Conc_Typ : Entity_Id;
10188 Subp : Entity_Id) return Entity_Id
10193 E := First_Entity (Conc_Typ);
10194 while Present (E) loop
10195 if Chars (Subp) = Chars (E)
10196 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10198 Check_Conforming_Parameters
10199 (First (Parameter_Specifications (Parent (E))),
10200 Next (First (Parameter_Specifications (Parent (Subp)))))
10209 end Matching_Entry_Or_Subprogram;
10211 -------------------------------------
10212 -- Matching_Dispatching_Subprogram --
10213 -------------------------------------
10215 function Matching_Dispatching_Subprogram
10216 (Conc_Typ : Entity_Id;
10217 Ent : Entity_Id) return Entity_Id
10222 -- Search for entities in the enclosing scope of this synchonized
10225 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10226 Push_Scope (Scope (Conc_Typ));
10227 E := Current_Entity_In_Scope (Ent);
10230 while Present (E) loop
10231 if Scope (E) = Scope (Conc_Typ)
10232 and then Comes_From_Source (E)
10233 and then Ekind (E) = E_Procedure
10234 and then Present (First_Entity (E))
10235 and then Is_Controlling_Formal (First_Entity (E))
10236 and then Etype (First_Entity (E)) = Conc_Typ
10238 Check_Conforming_Parameters
10239 (First (Parameter_Specifications (Parent (Ent))),
10240 Next (First (Parameter_Specifications (Parent (E)))))
10249 end Matching_Dispatching_Subprogram;
10251 --------------------------------------------
10252 -- Matching_Original_Protected_Subprogram --
10253 --------------------------------------------
10255 function Matching_Original_Protected_Subprogram
10256 (Prot_Typ : Entity_Id;
10257 Subp : Entity_Id) return Entity_Id
10259 ICF : constant Boolean :=
10260 Is_Controlling_Formal (First_Entity (Subp));
10264 -- Temporarily decorate the first parameter of Subp as controlling
10265 -- formal, required to invoke Subtype_Conformant.
10267 Set_Is_Controlling_Formal (First_Entity (Subp));
10270 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10272 while Present (E) loop
10273 if Scope (E) = Scope (Prot_Typ)
10274 and then Comes_From_Source (E)
10275 and then Ekind (Subp) = Ekind (E)
10276 and then Present (First_Entity (E))
10277 and then Is_Controlling_Formal (First_Entity (E))
10278 and then Etype (First_Entity (E)) = Prot_Typ
10279 and then Subtype_Conformant (Subp, E,
10280 Skip_Controlling_Formals => True)
10282 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10289 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10292 end Matching_Original_Protected_Subprogram;
10294 -- Start of processing for Has_Matching_Entry_Or_Subprogram
10297 -- Case 1: E is a subprogram whose first formal is a concurrent type
10298 -- defined in the scope of E that has an entry or subprogram whose
10299 -- profile matches E.
10301 if Comes_From_Source (E)
10302 and then Is_Subprogram (E)
10303 and then Present (First_Entity (E))
10304 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10307 Scope (Corresponding_Concurrent_Type
10308 (Etype (First_Entity (E))))
10311 (Matching_Entry_Or_Subprogram
10312 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10315 Report_Conflict (E,
10316 Matching_Entry_Or_Subprogram
10317 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10322 -- Case 2: E is an internally built dispatching subprogram of a
10323 -- protected type and there is a subprogram defined in the enclosing
10324 -- scope of the protected type that has the original name of E and
10325 -- its profile is conformant with the profile of E. We check the
10326 -- name of the original protected subprogram associated with E since
10327 -- the expander builds dispatching primitives of protected functions
10328 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
10330 elsif not Comes_From_Source (E)
10331 and then Is_Subprogram (E)
10332 and then Present (First_Entity (E))
10333 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10334 and then Present (Original_Protected_Subprogram (E))
10337 (Matching_Original_Protected_Subprogram
10338 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10341 Report_Conflict (E,
10342 Matching_Original_Protected_Subprogram
10343 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10347 -- Case 3: E is an entry of a synchronized type and a matching
10348 -- procedure has been previously defined in the enclosing scope
10349 -- of the synchronized type.
10351 elsif Comes_From_Source (E)
10352 and then Ekind (E) = E_Entry
10354 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
10356 Report_Conflict (E,
10357 Matching_Dispatching_Subprogram (Current_Scope, E));
10362 end Has_Matching_Entry_Or_Subprogram;
10364 ----------------------------
10365 -- Is_Private_Declaration --
10366 ----------------------------
10368 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10369 Decl : constant Node_Id := Unit_Declaration_Node (E);
10370 Priv_Decls : List_Id;
10373 if Is_Package_Or_Generic_Package (Current_Scope)
10374 and then In_Private_Part (Current_Scope)
10377 Private_Declarations (Package_Specification (Current_Scope));
10379 return In_Package_Body (Current_Scope)
10381 (Is_List_Member (Decl)
10382 and then List_Containing (Decl) = Priv_Decls)
10383 or else (Nkind (Parent (Decl)) = N_Package_Specification
10385 Is_Compilation_Unit
10386 (Defining_Entity (Parent (Decl)))
10387 and then List_Containing (Parent (Parent (Decl))) =
10392 end Is_Private_Declaration;
10394 --------------------------
10395 -- Is_Overriding_Alias --
10396 --------------------------
10398 function Is_Overriding_Alias
10399 (Old_E : Entity_Id;
10400 New_E : Entity_Id) return Boolean
10402 AO : constant Entity_Id := Alias (Old_E);
10403 AN : constant Entity_Id := Alias (New_E);
10406 return Scope (AO) /= Scope (AN)
10407 or else No (DTC_Entity (AO))
10408 or else No (DTC_Entity (AN))
10409 or else DT_Position (AO) = DT_Position (AN);
10410 end Is_Overriding_Alias;
10412 ---------------------
10413 -- Report_Conflict --
10414 ---------------------
10416 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
10418 Error_Msg_Sloc := Sloc (E);
10420 -- Generate message, with useful additional warning if in generic
10422 if Is_Generic_Unit (E) then
10423 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10424 Error_Msg_N ("\& conflicts with declaration#", S);
10426 Error_Msg_N ("& conflicts with declaration#", S);
10428 end Report_Conflict;
10430 -- Start of processing for New_Overloaded_Entity
10433 -- We need to look for an entity that S may override. This must be a
10434 -- homonym in the current scope, so we look for the first homonym of
10435 -- S in the current scope as the starting point for the search.
10437 E := Current_Entity_In_Scope (S);
10439 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10440 -- They are directly added to the list of primitive operations of
10441 -- Derived_Type, unless this is a rederivation in the private part
10442 -- of an operation that was already derived in the visible part of
10443 -- the current package.
10445 if Ada_Version >= Ada_2005
10446 and then Present (Derived_Type)
10447 and then Present (Alias (S))
10448 and then Is_Dispatching_Operation (Alias (S))
10449 and then Present (Find_Dispatching_Type (Alias (S)))
10450 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10452 -- For private types, when the full-view is processed we propagate to
10453 -- the full view the non-overridden entities whose attribute "alias"
10454 -- references an interface primitive. These entities were added by
10455 -- Derive_Subprograms to ensure that interface primitives are
10458 -- Inside_Freeze_Actions is non zero when S corresponds with an
10459 -- internal entity that links an interface primitive with its
10460 -- covering primitive through attribute Interface_Alias (see
10461 -- Add_Internal_Interface_Entities).
10463 if Inside_Freezing_Actions = 0
10464 and then Is_Package_Or_Generic_Package (Current_Scope)
10465 and then In_Private_Part (Current_Scope)
10466 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10467 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10468 and then Full_View (Defining_Identifier (Parent (E)))
10469 = Defining_Identifier (Parent (S))
10470 and then Alias (E) = Alias (S)
10472 Check_Operation_From_Private_View (S, E);
10473 Set_Is_Dispatching_Operation (S);
10478 Enter_Overloaded_Entity (S);
10479 Check_Dispatching_Operation (S, Empty);
10480 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10486 -- For synchronized types check conflicts of this entity with previously
10487 -- defined entities.
10489 if Ada_Version >= Ada_2005
10490 and then Has_Matching_Entry_Or_Subprogram (S)
10495 -- If there is no homonym then this is definitely not overriding
10498 Enter_Overloaded_Entity (S);
10499 Check_Dispatching_Operation (S, Empty);
10500 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10502 -- If subprogram has an explicit declaration, check whether it has an
10503 -- overriding indicator.
10505 if Comes_From_Source (S) then
10506 Check_Synchronized_Overriding (S, Overridden_Subp);
10508 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10509 -- it may have overridden some hidden inherited primitive. Update
10510 -- Overridden_Subp to avoid spurious errors when checking the
10511 -- overriding indicator.
10513 if Ada_Version >= Ada_2012
10514 and then No (Overridden_Subp)
10515 and then Is_Dispatching_Operation (S)
10516 and then Present (Overridden_Operation (S))
10518 Overridden_Subp := Overridden_Operation (S);
10521 Check_Overriding_Indicator
10522 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10524 -- The Ghost policy in effect at the point of declaration of a
10525 -- parent subprogram and an overriding subprogram must match
10526 -- (SPARK RM 6.9(17)).
10528 Check_Ghost_Overriding (S, Overridden_Subp);
10531 -- If there is a homonym that is not overloadable, then we have an
10532 -- error, except for the special cases checked explicitly below.
10534 elsif not Is_Overloadable (E) then
10536 -- Check for spurious conflict produced by a subprogram that has the
10537 -- same name as that of the enclosing generic package. The conflict
10538 -- occurs within an instance, between the subprogram and the renaming
10539 -- declaration for the package. After the subprogram, the package
10540 -- renaming declaration becomes hidden.
10542 if Ekind (E) = E_Package
10543 and then Present (Renamed_Object (E))
10544 and then Renamed_Object (E) = Current_Scope
10545 and then Nkind (Parent (Renamed_Object (E))) =
10546 N_Package_Specification
10547 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10550 Set_Is_Immediately_Visible (E, False);
10551 Enter_Overloaded_Entity (S);
10552 Set_Homonym (S, Homonym (E));
10553 Check_Dispatching_Operation (S, Empty);
10554 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10556 -- If the subprogram is implicit it is hidden by the previous
10557 -- declaration. However if it is dispatching, it must appear in the
10558 -- dispatch table anyway, because it can be dispatched to even if it
10559 -- cannot be called directly.
10561 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10562 Set_Scope (S, Current_Scope);
10564 if Is_Dispatching_Operation (Alias (S)) then
10565 Check_Dispatching_Operation (S, Empty);
10571 Report_Conflict (S, E);
10575 -- E exists and is overloadable
10578 Check_Synchronized_Overriding (S, Overridden_Subp);
10580 -- Loop through E and its homonyms to determine if any of them is
10581 -- the candidate for overriding by S.
10583 while Present (E) loop
10585 -- Definitely not interesting if not in the current scope
10587 if Scope (E) /= Current_Scope then
10590 -- A function can overload the name of an abstract state. The
10591 -- state can be viewed as a function with a profile that cannot
10592 -- be matched by anything.
10594 elsif Ekind (S) = E_Function
10595 and then Ekind (E) = E_Abstract_State
10597 Enter_Overloaded_Entity (S);
10600 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10601 -- procedures locate the internally generated spec. We enforce
10602 -- mode conformance since a tagged type may inherit from
10603 -- interfaces several null primitives which differ only in
10604 -- the mode of the formals.
10606 elsif not Comes_From_Source (S)
10607 and then Is_Null_Procedure (S)
10608 and then not Mode_Conformant (E, S)
10612 -- Check if we have type conformance
10614 elsif Type_Conformant (E, S) then
10616 -- If the old and new entities have the same profile and one
10617 -- is not the body of the other, then this is an error, unless
10618 -- one of them is implicitly declared.
10620 -- There are some cases when both can be implicit, for example
10621 -- when both a literal and a function that overrides it are
10622 -- inherited in a derivation, or when an inherited operation
10623 -- of a tagged full type overrides the inherited operation of
10624 -- a private extension. Ada 83 had a special rule for the
10625 -- literal case. In Ada 95, the later implicit operation hides
10626 -- the former, and the literal is always the former. In the
10627 -- odd case where both are derived operations declared at the
10628 -- same point, both operations should be declared, and in that
10629 -- case we bypass the following test and proceed to the next
10630 -- part. This can only occur for certain obscure cases in
10631 -- instances, when an operation on a type derived from a formal
10632 -- private type does not override a homograph inherited from
10633 -- the actual. In subsequent derivations of such a type, the
10634 -- DT positions of these operations remain distinct, if they
10637 if Present (Alias (S))
10638 and then (No (Alias (E))
10639 or else Comes_From_Source (E)
10640 or else Is_Abstract_Subprogram (S)
10642 (Is_Dispatching_Operation (E)
10643 and then Is_Overriding_Alias (E, S)))
10644 and then Ekind (E) /= E_Enumeration_Literal
10646 -- When an derived operation is overloaded it may be due to
10647 -- the fact that the full view of a private extension
10648 -- re-inherits. It has to be dealt with.
10650 if Is_Package_Or_Generic_Package (Current_Scope)
10651 and then In_Private_Part (Current_Scope)
10653 Check_Operation_From_Private_View (S, E);
10656 -- In any case the implicit operation remains hidden by the
10657 -- existing declaration, which is overriding. Indicate that
10658 -- E overrides the operation from which S is inherited.
10660 if Present (Alias (S)) then
10661 Set_Overridden_Operation (E, Alias (S));
10662 Inherit_Subprogram_Contract (E, Alias (S));
10665 Set_Overridden_Operation (E, S);
10666 Inherit_Subprogram_Contract (E, S);
10669 if Comes_From_Source (E) then
10670 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10672 -- The Ghost policy in effect at the point of declaration
10673 -- of a parent subprogram and an overriding subprogram
10674 -- must match (SPARK RM 6.9(17)).
10676 Check_Ghost_Overriding (E, S);
10681 -- Within an instance, the renaming declarations for actual
10682 -- subprograms may become ambiguous, but they do not hide each
10685 elsif Ekind (E) /= E_Entry
10686 and then not Comes_From_Source (E)
10687 and then not Is_Generic_Instance (E)
10688 and then (Present (Alias (E))
10689 or else Is_Intrinsic_Subprogram (E))
10690 and then (not In_Instance
10691 or else No (Parent (E))
10692 or else Nkind (Unit_Declaration_Node (E)) /=
10693 N_Subprogram_Renaming_Declaration)
10695 -- A subprogram child unit is not allowed to override an
10696 -- inherited subprogram (10.1.1(20)).
10698 if Is_Child_Unit (S) then
10700 ("child unit overrides inherited subprogram in parent",
10705 if Is_Non_Overriding_Operation (E, S) then
10706 Enter_Overloaded_Entity (S);
10708 if No (Derived_Type)
10709 or else Is_Tagged_Type (Derived_Type)
10711 Check_Dispatching_Operation (S, Empty);
10717 -- E is a derived operation or an internal operator which
10718 -- is being overridden. Remove E from further visibility.
10719 -- Furthermore, if E is a dispatching operation, it must be
10720 -- replaced in the list of primitive operations of its type
10721 -- (see Override_Dispatching_Operation).
10723 Overridden_Subp := E;
10729 Prev := First_Entity (Current_Scope);
10730 while Present (Prev) and then Next_Entity (Prev) /= E loop
10731 Next_Entity (Prev);
10734 -- It is possible for E to be in the current scope and
10735 -- yet not in the entity chain. This can only occur in a
10736 -- generic context where E is an implicit concatenation
10737 -- in the formal part, because in a generic body the
10738 -- entity chain starts with the formals.
10740 -- In GNATprove mode, a wrapper for an operation with
10741 -- axiomatization may be a homonym of another declaration
10742 -- for an actual subprogram (needs refinement ???).
10746 and then GNATprove_Mode
10748 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10749 N_Subprogram_Renaming_Declaration
10753 pragma Assert (Chars (E) = Name_Op_Concat);
10758 -- E must be removed both from the entity_list of the
10759 -- current scope, and from the visibility chain.
10761 if Debug_Flag_E then
10762 Write_Str ("Override implicit operation ");
10763 Write_Int (Int (E));
10767 -- If E is a predefined concatenation, it stands for four
10768 -- different operations. As a result, a single explicit
10769 -- declaration does not hide it. In a possible ambiguous
10770 -- situation, Disambiguate chooses the user-defined op,
10771 -- so it is correct to retain the previous internal one.
10773 if Chars (E) /= Name_Op_Concat
10774 or else Ekind (E) /= E_Operator
10776 -- For nondispatching derived operations that are
10777 -- overridden by a subprogram declared in the private
10778 -- part of a package, we retain the derived subprogram
10779 -- but mark it as not immediately visible. If the
10780 -- derived operation was declared in the visible part
10781 -- then this ensures that it will still be visible
10782 -- outside the package with the proper signature
10783 -- (calls from outside must also be directed to this
10784 -- version rather than the overriding one, unlike the
10785 -- dispatching case). Calls from inside the package
10786 -- will still resolve to the overriding subprogram
10787 -- since the derived one is marked as not visible
10788 -- within the package.
10790 -- If the private operation is dispatching, we achieve
10791 -- the overriding by keeping the implicit operation
10792 -- but setting its alias to be the overriding one. In
10793 -- this fashion the proper body is executed in all
10794 -- cases, but the original signature is used outside
10797 -- If the overriding is not in the private part, we
10798 -- remove the implicit operation altogether.
10800 if Is_Private_Declaration (S) then
10801 if not Is_Dispatching_Operation (E) then
10802 Set_Is_Immediately_Visible (E, False);
10804 -- Work done in Override_Dispatching_Operation,
10805 -- so nothing else needs to be done here.
10811 -- Find predecessor of E in Homonym chain
10813 if E = Current_Entity (E) then
10816 Prev_Vis := Current_Entity (E);
10817 while Homonym (Prev_Vis) /= E loop
10818 Prev_Vis := Homonym (Prev_Vis);
10822 if Prev_Vis /= Empty then
10824 -- Skip E in the visibility chain
10826 Set_Homonym (Prev_Vis, Homonym (E));
10829 Set_Name_Entity_Id (Chars (E), Homonym (E));
10832 Set_Next_Entity (Prev, Next_Entity (E));
10834 if No (Next_Entity (Prev)) then
10835 Set_Last_Entity (Current_Scope, Prev);
10840 Enter_Overloaded_Entity (S);
10842 -- For entities generated by Derive_Subprograms the
10843 -- overridden operation is the inherited primitive
10844 -- (which is available through the attribute alias).
10846 if not (Comes_From_Source (E))
10847 and then Is_Dispatching_Operation (E)
10848 and then Find_Dispatching_Type (E) =
10849 Find_Dispatching_Type (S)
10850 and then Present (Alias (E))
10851 and then Comes_From_Source (Alias (E))
10853 Set_Overridden_Operation (S, Alias (E));
10854 Inherit_Subprogram_Contract (S, Alias (E));
10856 -- Normal case of setting entity as overridden
10858 -- Note: Static_Initialization and Overridden_Operation
10859 -- attributes use the same field in subprogram entities.
10860 -- Static_Initialization is only defined for internal
10861 -- initialization procedures, where Overridden_Operation
10862 -- is irrelevant. Therefore the setting of this attribute
10863 -- must check whether the target is an init_proc.
10865 elsif not Is_Init_Proc (S) then
10866 Set_Overridden_Operation (S, E);
10867 Inherit_Subprogram_Contract (S, E);
10870 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10872 -- The Ghost policy in effect at the point of declaration
10873 -- of a parent subprogram and an overriding subprogram
10874 -- must match (SPARK RM 6.9(17)).
10876 Check_Ghost_Overriding (S, E);
10878 -- If S is a user-defined subprogram or a null procedure
10879 -- expanded to override an inherited null procedure, or a
10880 -- predefined dispatching primitive then indicate that E
10881 -- overrides the operation from which S is inherited.
10883 if Comes_From_Source (S)
10885 (Present (Parent (S))
10887 Nkind (Parent (S)) = N_Procedure_Specification
10889 Null_Present (Parent (S)))
10891 (Present (Alias (E))
10893 Is_Predefined_Dispatching_Operation (Alias (E)))
10895 if Present (Alias (E)) then
10896 Set_Overridden_Operation (S, Alias (E));
10897 Inherit_Subprogram_Contract (S, Alias (E));
10901 if Is_Dispatching_Operation (E) then
10903 -- An overriding dispatching subprogram inherits the
10904 -- convention of the overridden subprogram (AI-117).
10906 Set_Convention (S, Convention (E));
10907 Check_Dispatching_Operation (S, E);
10910 Check_Dispatching_Operation (S, Empty);
10913 Check_For_Primitive_Subprogram
10914 (Is_Primitive_Subp, Is_Overriding => True);
10915 goto Check_Inequality;
10918 -- Apparent redeclarations in instances can occur when two
10919 -- formal types get the same actual type. The subprograms in
10920 -- in the instance are legal, even if not callable from the
10921 -- outside. Calls from within are disambiguated elsewhere.
10922 -- For dispatching operations in the visible part, the usual
10923 -- rules apply, and operations with the same profile are not
10924 -- legal (B830001).
10926 elsif (In_Instance_Visible_Part
10927 and then not Is_Dispatching_Operation (E))
10928 or else In_Instance_Not_Visible
10932 -- Here we have a real error (identical profile)
10935 Error_Msg_Sloc := Sloc (E);
10937 -- Avoid cascaded errors if the entity appears in
10938 -- subsequent calls.
10940 Set_Scope (S, Current_Scope);
10942 -- Generate error, with extra useful warning for the case
10943 -- of a generic instance with no completion.
10945 if Is_Generic_Instance (S)
10946 and then not Has_Completion (E)
10949 ("instantiation cannot provide body for&", S);
10950 Error_Msg_N ("\& conflicts with declaration#", S);
10952 Error_Msg_N ("& conflicts with declaration#", S);
10959 -- If one subprogram has an access parameter and the other
10960 -- a parameter of an access type, calls to either might be
10961 -- ambiguous. Verify that parameters match except for the
10962 -- access parameter.
10964 if May_Hide_Profile then
10970 F1 := First_Formal (S);
10971 F2 := First_Formal (E);
10972 while Present (F1) and then Present (F2) loop
10973 if Is_Access_Type (Etype (F1)) then
10974 if not Is_Access_Type (Etype (F2))
10975 or else not Conforming_Types
10976 (Designated_Type (Etype (F1)),
10977 Designated_Type (Etype (F2)),
10980 May_Hide_Profile := False;
10984 not Conforming_Types
10985 (Etype (F1), Etype (F2), Type_Conformant)
10987 May_Hide_Profile := False;
10994 if May_Hide_Profile
10998 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11007 -- On exit, we know that S is a new entity
11009 Enter_Overloaded_Entity (S);
11010 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11011 Check_Overriding_Indicator
11012 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11014 -- The Ghost policy in effect at the point of declaration of a parent
11015 -- subprogram and an overriding subprogram must match
11016 -- (SPARK RM 6.9(17)).
11018 Check_Ghost_Overriding (S, Overridden_Subp);
11020 -- Overloading is not allowed in SPARK, except for operators
11022 if Nkind (S) /= N_Defining_Operator_Symbol then
11023 Error_Msg_Sloc := Sloc (Homonym (S));
11024 Check_SPARK_05_Restriction
11025 ("overloading not allowed with entity#", S);
11028 -- If S is a derived operation for an untagged type then by
11029 -- definition it's not a dispatching operation (even if the parent
11030 -- operation was dispatching), so Check_Dispatching_Operation is not
11031 -- called in that case.
11033 if No (Derived_Type)
11034 or else Is_Tagged_Type (Derived_Type)
11036 Check_Dispatching_Operation (S, Empty);
11040 -- If this is a user-defined equality operator that is not a derived
11041 -- subprogram, create the corresponding inequality. If the operation is
11042 -- dispatching, the expansion is done elsewhere, and we do not create
11043 -- an explicit inequality operation.
11045 <<Check_Inequality>>
11046 if Chars (S) = Name_Op_Eq
11047 and then Etype (S) = Standard_Boolean
11048 and then Present (Parent (S))
11049 and then not Is_Dispatching_Operation (S)
11051 Make_Inequality_Operator (S);
11052 Check_Untagged_Equality (S);
11054 end New_Overloaded_Entity;
11056 ---------------------
11057 -- Process_Formals --
11058 ---------------------
11060 procedure Process_Formals
11062 Related_Nod : Node_Id)
11064 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11065 -- Determine whether an access type designates a type coming from a
11068 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11069 -- Check whether the default has a class-wide type. After analysis the
11070 -- default has the type of the formal, so we must also check explicitly
11071 -- for an access attribute.
11073 ----------------------------------
11074 -- Designates_From_Limited_With --
11075 ----------------------------------
11077 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11078 Desig : Entity_Id := Typ;
11081 if Is_Access_Type (Desig) then
11082 Desig := Directly_Designated_Type (Desig);
11085 if Is_Class_Wide_Type (Desig) then
11086 Desig := Root_Type (Desig);
11090 Ekind (Desig) = E_Incomplete_Type
11091 and then From_Limited_With (Desig);
11092 end Designates_From_Limited_With;
11094 ---------------------------
11095 -- Is_Class_Wide_Default --
11096 ---------------------------
11098 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11100 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11101 or else (Nkind (D) = N_Attribute_Reference
11102 and then Attribute_Name (D) = Name_Access
11103 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11104 end Is_Class_Wide_Default;
11108 Context : constant Node_Id := Parent (Parent (T));
11110 Formal : Entity_Id;
11111 Formal_Type : Entity_Id;
11112 Param_Spec : Node_Id;
11115 Num_Out_Params : Nat := 0;
11116 First_Out_Param : Entity_Id := Empty;
11117 -- Used for setting Is_Only_Out_Parameter
11119 -- Start of processing for Process_Formals
11122 -- In order to prevent premature use of the formals in the same formal
11123 -- part, the Ekind is left undefined until all default expressions are
11124 -- analyzed. The Ekind is established in a separate loop at the end.
11126 Param_Spec := First (T);
11127 while Present (Param_Spec) loop
11128 Formal := Defining_Identifier (Param_Spec);
11129 Set_Never_Set_In_Source (Formal, True);
11130 Enter_Name (Formal);
11132 -- Case of ordinary parameters
11134 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11135 Find_Type (Parameter_Type (Param_Spec));
11136 Ptype := Parameter_Type (Param_Spec);
11138 if Ptype = Error then
11142 Formal_Type := Entity (Ptype);
11144 if Is_Incomplete_Type (Formal_Type)
11146 (Is_Class_Wide_Type (Formal_Type)
11147 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11149 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11150 -- primitive operations, as long as their completion is
11151 -- in the same declarative part. If in the private part
11152 -- this means that the type cannot be a Taft-amendment type.
11153 -- Check is done on package exit. For access to subprograms,
11154 -- the use is legal for Taft-amendment types.
11156 -- Ada 2012: tagged incomplete types are allowed as generic
11157 -- formal types. They do not introduce dependencies and the
11158 -- corresponding generic subprogram does not have a delayed
11159 -- freeze, because it does not need a freeze node. However,
11160 -- it is still the case that untagged incomplete types cannot
11161 -- be Taft-amendment types and must be completed in private
11162 -- part, so the subprogram must appear in the list of private
11163 -- dependents of the type.
11165 if Is_Tagged_Type (Formal_Type)
11166 or else (Ada_Version >= Ada_2012
11167 and then not From_Limited_With (Formal_Type)
11168 and then not Is_Generic_Type (Formal_Type))
11170 if Ekind (Scope (Current_Scope)) = E_Package
11171 and then not Is_Generic_Type (Formal_Type)
11172 and then not Is_Class_Wide_Type (Formal_Type)
11175 (Parent (T), N_Access_Function_Definition,
11176 N_Access_Procedure_Definition)
11178 Append_Elmt (Current_Scope,
11179 Private_Dependents (Base_Type (Formal_Type)));
11181 -- Freezing is delayed to ensure that Register_Prim
11182 -- will get called for this operation, which is needed
11183 -- in cases where static dispatch tables aren't built.
11184 -- (Note that the same is done for controlling access
11185 -- parameter cases in function Access_Definition.)
11187 if not Is_Thunk (Current_Scope) then
11188 Set_Has_Delayed_Freeze (Current_Scope);
11193 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11194 N_Access_Procedure_Definition)
11196 -- AI05-0151: Tagged incomplete types are allowed in all
11197 -- formal parts. Untagged incomplete types are not allowed
11198 -- in bodies. Limited views of either kind are not allowed
11199 -- if there is no place at which the non-limited view can
11200 -- become available.
11202 -- Incomplete formal untagged types are not allowed in
11203 -- subprogram bodies (but are legal in their declarations).
11204 -- This excludes bodies created for null procedures, which
11205 -- are basic declarations.
11207 if Is_Generic_Type (Formal_Type)
11208 and then not Is_Tagged_Type (Formal_Type)
11209 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11212 ("invalid use of formal incomplete type", Param_Spec);
11214 elsif Ada_Version >= Ada_2012 then
11215 if Is_Tagged_Type (Formal_Type)
11216 and then (not From_Limited_With (Formal_Type)
11217 or else not In_Package_Body)
11221 elsif Nkind_In (Context, N_Accept_Statement,
11222 N_Accept_Alternative,
11224 or else (Nkind (Context) = N_Subprogram_Body
11225 and then Comes_From_Source (Context))
11228 ("invalid use of untagged incomplete type &",
11229 Ptype, Formal_Type);
11234 ("invalid use of incomplete type&",
11235 Param_Spec, Formal_Type);
11237 -- Further checks on the legality of incomplete types
11238 -- in formal parts are delayed until the freeze point
11239 -- of the enclosing subprogram or access to subprogram.
11243 elsif Ekind (Formal_Type) = E_Void then
11245 ("premature use of&",
11246 Parameter_Type (Param_Spec), Formal_Type);
11249 -- Ada 2012 (AI-142): Handle aliased parameters
11251 if Ada_Version >= Ada_2012
11252 and then Aliased_Present (Param_Spec)
11254 Set_Is_Aliased (Formal);
11257 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11258 -- declaration corresponding to the null-excluding type of the
11259 -- formal in the enclosing scope. Finally, replace the parameter
11260 -- type of the formal with the internal subtype.
11262 if Ada_Version >= Ada_2005
11263 and then Null_Exclusion_Present (Param_Spec)
11265 if not Is_Access_Type (Formal_Type) then
11267 ("`NOT NULL` allowed only for an access type", Param_Spec);
11270 if Can_Never_Be_Null (Formal_Type)
11271 and then Comes_From_Source (Related_Nod)
11274 ("`NOT NULL` not allowed (& already excludes null)",
11275 Param_Spec, Formal_Type);
11279 Create_Null_Excluding_Itype
11281 Related_Nod => Related_Nod,
11282 Scope_Id => Scope (Current_Scope));
11284 -- If the designated type of the itype is an itype that is
11285 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11286 -- on the access subtype, to prevent order-of-elaboration
11287 -- issues in the backend.
11290 -- type T is access procedure;
11291 -- procedure Op (O : not null T);
11293 if Is_Itype (Directly_Designated_Type (Formal_Type))
11295 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11297 Set_Has_Delayed_Freeze (Formal_Type);
11302 -- An access formal type
11306 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11308 -- No need to continue if we already notified errors
11310 if not Present (Formal_Type) then
11314 -- Ada 2005 (AI-254)
11317 AD : constant Node_Id :=
11318 Access_To_Subprogram_Definition
11319 (Parameter_Type (Param_Spec));
11321 if Present (AD) and then Protected_Present (AD) then
11323 Replace_Anonymous_Access_To_Protected_Subprogram
11329 Set_Etype (Formal, Formal_Type);
11331 -- Deal with default expression if present
11333 Default := Expression (Param_Spec);
11335 if Present (Default) then
11336 Check_SPARK_05_Restriction
11337 ("default expression is not allowed", Default);
11339 if Out_Present (Param_Spec) then
11341 ("default initialization only allowed for IN parameters",
11345 -- Do the special preanalysis of the expression (see section on
11346 -- "Handling of Default Expressions" in the spec of package Sem).
11348 Preanalyze_Spec_Expression (Default, Formal_Type);
11350 -- An access to constant cannot be the default for
11351 -- an access parameter that is an access to variable.
11353 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11354 and then not Is_Access_Constant (Formal_Type)
11355 and then Is_Access_Type (Etype (Default))
11356 and then Is_Access_Constant (Etype (Default))
11359 ("formal that is access to variable cannot be initialized "
11360 & "with an access-to-constant expression", Default);
11363 -- Check that the designated type of an access parameter's default
11364 -- is not a class-wide type unless the parameter's designated type
11365 -- is also class-wide.
11367 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11368 and then not Designates_From_Limited_With (Formal_Type)
11369 and then Is_Class_Wide_Default (Default)
11370 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11373 ("access to class-wide expression not allowed here", Default);
11376 -- Check incorrect use of dynamically tagged expressions
11378 if Is_Tagged_Type (Formal_Type) then
11379 Check_Dynamically_Tagged_Expression
11381 Typ => Formal_Type,
11382 Related_Nod => Default);
11386 -- Ada 2005 (AI-231): Static checks
11388 if Ada_Version >= Ada_2005
11389 and then Is_Access_Type (Etype (Formal))
11390 and then Can_Never_Be_Null (Etype (Formal))
11392 Null_Exclusion_Static_Checks (Param_Spec);
11395 -- The following checks are relevant only when SPARK_Mode is on as
11396 -- these are not standard Ada legality rules.
11398 if SPARK_Mode = On then
11399 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11401 -- A function cannot have a parameter of mode IN OUT or OUT
11404 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11406 ("function cannot have parameter of mode `OUT` or "
11407 & "`IN OUT`", Formal);
11410 -- A procedure cannot have an effectively volatile formal
11411 -- parameter of mode IN because it behaves as a constant
11412 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
11414 elsif Ekind (Scope (Formal)) = E_Procedure
11415 and then Ekind (Formal) = E_In_Parameter
11416 and then Is_Effectively_Volatile (Formal)
11419 ("formal parameter of mode `IN` cannot be volatile", Formal);
11427 -- If this is the formal part of a function specification, analyze the
11428 -- subtype mark in the context where the formals are visible but not
11429 -- yet usable, and may hide outer homographs.
11431 if Nkind (Related_Nod) = N_Function_Specification then
11432 Analyze_Return_Type (Related_Nod);
11435 -- Now set the kind (mode) of each formal
11437 Param_Spec := First (T);
11438 while Present (Param_Spec) loop
11439 Formal := Defining_Identifier (Param_Spec);
11440 Set_Formal_Mode (Formal);
11442 if Ekind (Formal) = E_In_Parameter then
11443 Set_Default_Value (Formal, Expression (Param_Spec));
11445 if Present (Expression (Param_Spec)) then
11446 Default := Expression (Param_Spec);
11448 if Is_Scalar_Type (Etype (Default)) then
11449 if Nkind (Parameter_Type (Param_Spec)) /=
11450 N_Access_Definition
11452 Formal_Type := Entity (Parameter_Type (Param_Spec));
11456 (Related_Nod, Parameter_Type (Param_Spec));
11459 Apply_Scalar_Range_Check (Default, Formal_Type);
11463 elsif Ekind (Formal) = E_Out_Parameter then
11464 Num_Out_Params := Num_Out_Params + 1;
11466 if Num_Out_Params = 1 then
11467 First_Out_Param := Formal;
11470 elsif Ekind (Formal) = E_In_Out_Parameter then
11471 Num_Out_Params := Num_Out_Params + 1;
11474 -- Skip remaining processing if formal type was in error
11476 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11477 goto Next_Parameter;
11480 -- Force call by reference if aliased
11483 Conv : constant Convention_Id := Convention (Etype (Formal));
11485 if Is_Aliased (Formal) then
11486 Set_Mechanism (Formal, By_Reference);
11488 -- Warn if user asked this to be passed by copy
11490 if Conv = Convention_Ada_Pass_By_Copy then
11492 ("cannot pass aliased parameter & by copy??", Formal);
11495 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11497 elsif Conv = Convention_Ada_Pass_By_Copy then
11498 Set_Mechanism (Formal, By_Copy);
11500 elsif Conv = Convention_Ada_Pass_By_Reference then
11501 Set_Mechanism (Formal, By_Reference);
11509 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11510 Set_Is_Only_Out_Parameter (First_Out_Param);
11512 end Process_Formals;
11514 ----------------------------
11515 -- Reference_Body_Formals --
11516 ----------------------------
11518 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11523 if Error_Posted (Spec) then
11527 -- Iterate over both lists. They may be of different lengths if the two
11528 -- specs are not conformant.
11530 Fs := First_Formal (Spec);
11531 Fb := First_Formal (Bod);
11532 while Present (Fs) and then Present (Fb) loop
11533 Generate_Reference (Fs, Fb, 'b');
11535 if Style_Check then
11536 Style.Check_Identifier (Fb, Fs);
11539 Set_Spec_Entity (Fb, Fs);
11540 Set_Referenced (Fs, False);
11544 end Reference_Body_Formals;
11546 -------------------------
11547 -- Set_Actual_Subtypes --
11548 -------------------------
11550 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11552 Formal : Entity_Id;
11554 First_Stmt : Node_Id := Empty;
11555 AS_Needed : Boolean;
11558 -- If this is an empty initialization procedure, no need to create
11559 -- actual subtypes (small optimization).
11561 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11565 -- The subtype declarations may freeze the formals. The body generated
11566 -- for an expression function is not a freeze point, so do not emit
11567 -- these declarations (small loss of efficiency in rare cases).
11569 if Nkind (N) = N_Subprogram_Body
11570 and then Was_Expression_Function (N)
11575 Formal := First_Formal (Subp);
11576 while Present (Formal) loop
11577 T := Etype (Formal);
11579 -- We never need an actual subtype for a constrained formal
11581 if Is_Constrained (T) then
11582 AS_Needed := False;
11584 -- If we have unknown discriminants, then we do not need an actual
11585 -- subtype, or more accurately we cannot figure it out. Note that
11586 -- all class-wide types have unknown discriminants.
11588 elsif Has_Unknown_Discriminants (T) then
11589 AS_Needed := False;
11591 -- At this stage we have an unconstrained type that may need an
11592 -- actual subtype. For sure the actual subtype is needed if we have
11593 -- an unconstrained array type. However, in an instance, the type
11594 -- may appear as a subtype of the full view, while the actual is
11595 -- in fact private (in which case no actual subtype is needed) so
11596 -- check the kind of the base type.
11598 elsif Is_Array_Type (Base_Type (T)) then
11601 -- The only other case needing an actual subtype is an unconstrained
11602 -- record type which is an IN parameter (we cannot generate actual
11603 -- subtypes for the OUT or IN OUT case, since an assignment can
11604 -- change the discriminant values. However we exclude the case of
11605 -- initialization procedures, since discriminants are handled very
11606 -- specially in this context, see the section entitled "Handling of
11607 -- Discriminants" in Einfo.
11609 -- We also exclude the case of Discrim_SO_Functions (functions used
11610 -- in front-end layout mode for size/offset values), since in such
11611 -- functions only discriminants are referenced, and not only are such
11612 -- subtypes not needed, but they cannot always be generated, because
11613 -- of order of elaboration issues.
11615 elsif Is_Record_Type (T)
11616 and then Ekind (Formal) = E_In_Parameter
11617 and then Chars (Formal) /= Name_uInit
11618 and then not Is_Unchecked_Union (T)
11619 and then not Is_Discrim_SO_Function (Subp)
11623 -- All other cases do not need an actual subtype
11626 AS_Needed := False;
11629 -- Generate actual subtypes for unconstrained arrays and
11630 -- unconstrained discriminated records.
11633 if Nkind (N) = N_Accept_Statement then
11635 -- If expansion is active, the formal is replaced by a local
11636 -- variable that renames the corresponding entry of the
11637 -- parameter block, and it is this local variable that may
11638 -- require an actual subtype.
11640 if Expander_Active then
11641 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11643 Decl := Build_Actual_Subtype (T, Formal);
11646 if Present (Handled_Statement_Sequence (N)) then
11648 First (Statements (Handled_Statement_Sequence (N)));
11649 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11650 Mark_Rewrite_Insertion (Decl);
11652 -- If the accept statement has no body, there will be no
11653 -- reference to the actuals, so no need to compute actual
11660 Decl := Build_Actual_Subtype (T, Formal);
11661 Prepend (Decl, Declarations (N));
11662 Mark_Rewrite_Insertion (Decl);
11665 -- The declaration uses the bounds of an existing object, and
11666 -- therefore needs no constraint checks.
11668 Analyze (Decl, Suppress => All_Checks);
11669 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
11671 -- We need to freeze manually the generated type when it is
11672 -- inserted anywhere else than in a declarative part.
11674 if Present (First_Stmt) then
11675 Insert_List_Before_And_Analyze (First_Stmt,
11676 Freeze_Entity (Defining_Identifier (Decl), N));
11678 -- Ditto if the type has a dynamic predicate, because the
11679 -- generated function will mention the actual subtype. The
11680 -- predicate may come from an explicit aspect of be inherited.
11682 elsif Has_Predicates (T) then
11683 Insert_List_Before_And_Analyze (Decl,
11684 Freeze_Entity (Defining_Identifier (Decl), N));
11687 if Nkind (N) = N_Accept_Statement
11688 and then Expander_Active
11690 Set_Actual_Subtype (Renamed_Object (Formal),
11691 Defining_Identifier (Decl));
11693 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11697 Next_Formal (Formal);
11699 end Set_Actual_Subtypes;
11701 ---------------------
11702 -- Set_Formal_Mode --
11703 ---------------------
11705 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11706 Spec : constant Node_Id := Parent (Formal_Id);
11707 Id : constant Entity_Id := Scope (Formal_Id);
11710 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11711 -- since we ensure that corresponding actuals are always valid at the
11712 -- point of the call.
11714 if Out_Present (Spec) then
11715 if Ekind_In (Id, E_Entry, E_Entry_Family)
11716 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11718 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11721 if Ekind_In (Id, E_Function, E_Generic_Function) then
11723 -- [IN] OUT parameters allowed for functions in Ada 2012
11725 if Ada_Version >= Ada_2012 then
11727 -- Even in Ada 2012 operators can only have IN parameters
11729 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11730 Error_Msg_N ("operators can only have IN parameters", Spec);
11733 if In_Present (Spec) then
11734 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11736 Set_Ekind (Formal_Id, E_Out_Parameter);
11739 -- But not in earlier versions of Ada
11742 Error_Msg_N ("functions can only have IN parameters", Spec);
11743 Set_Ekind (Formal_Id, E_In_Parameter);
11746 elsif In_Present (Spec) then
11747 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11750 Set_Ekind (Formal_Id, E_Out_Parameter);
11751 Set_Never_Set_In_Source (Formal_Id, True);
11752 Set_Is_True_Constant (Formal_Id, False);
11753 Set_Current_Value (Formal_Id, Empty);
11757 Set_Ekind (Formal_Id, E_In_Parameter);
11760 -- Set Is_Known_Non_Null for access parameters since the language
11761 -- guarantees that access parameters are always non-null. We also set
11762 -- Can_Never_Be_Null, since there is no way to change the value.
11764 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11766 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11767 -- null; In Ada 2005, only if then null_exclusion is explicit.
11769 if Ada_Version < Ada_2005
11770 or else Can_Never_Be_Null (Etype (Formal_Id))
11772 Set_Is_Known_Non_Null (Formal_Id);
11773 Set_Can_Never_Be_Null (Formal_Id);
11776 -- Ada 2005 (AI-231): Null-exclusion access subtype
11778 elsif Is_Access_Type (Etype (Formal_Id))
11779 and then Can_Never_Be_Null (Etype (Formal_Id))
11781 Set_Is_Known_Non_Null (Formal_Id);
11783 -- We can also set Can_Never_Be_Null (thus preventing some junk
11784 -- access checks) for the case of an IN parameter, which cannot
11785 -- be changed, or for an IN OUT parameter, which can be changed but
11786 -- not to a null value. But for an OUT parameter, the initial value
11787 -- passed in can be null, so we can't set this flag in that case.
11789 if Ekind (Formal_Id) /= E_Out_Parameter then
11790 Set_Can_Never_Be_Null (Formal_Id);
11794 Set_Mechanism (Formal_Id, Default_Mechanism);
11795 Set_Formal_Validity (Formal_Id);
11796 end Set_Formal_Mode;
11798 -------------------------
11799 -- Set_Formal_Validity --
11800 -------------------------
11802 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11804 -- If no validity checking, then we cannot assume anything about the
11805 -- validity of parameters, since we do not know there is any checking
11806 -- of the validity on the call side.
11808 if not Validity_Checks_On then
11811 -- If validity checking for parameters is enabled, this means we are
11812 -- not supposed to make any assumptions about argument values.
11814 elsif Validity_Check_Parameters then
11817 -- If we are checking in parameters, we will assume that the caller is
11818 -- also checking parameters, so we can assume the parameter is valid.
11820 elsif Ekind (Formal_Id) = E_In_Parameter
11821 and then Validity_Check_In_Params
11823 Set_Is_Known_Valid (Formal_Id, True);
11825 -- Similar treatment for IN OUT parameters
11827 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11828 and then Validity_Check_In_Out_Params
11830 Set_Is_Known_Valid (Formal_Id, True);
11832 end Set_Formal_Validity;
11834 ------------------------
11835 -- Subtype_Conformant --
11836 ------------------------
11838 function Subtype_Conformant
11839 (New_Id : Entity_Id;
11840 Old_Id : Entity_Id;
11841 Skip_Controlling_Formals : Boolean := False) return Boolean
11845 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11846 Skip_Controlling_Formals => Skip_Controlling_Formals);
11848 end Subtype_Conformant;
11850 ---------------------
11851 -- Type_Conformant --
11852 ---------------------
11854 function Type_Conformant
11855 (New_Id : Entity_Id;
11856 Old_Id : Entity_Id;
11857 Skip_Controlling_Formals : Boolean := False) return Boolean
11861 May_Hide_Profile := False;
11863 (New_Id, Old_Id, Type_Conformant, False, Result,
11864 Skip_Controlling_Formals => Skip_Controlling_Formals);
11866 end Type_Conformant;
11868 -------------------------------
11869 -- Valid_Operator_Definition --
11870 -------------------------------
11872 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11875 Id : constant Name_Id := Chars (Designator);
11879 F := First_Formal (Designator);
11880 while Present (F) loop
11883 if Present (Default_Value (F)) then
11885 ("default values not allowed for operator parameters",
11888 -- For function instantiations that are operators, we must check
11889 -- separately that the corresponding generic only has in-parameters.
11890 -- For subprogram declarations this is done in Set_Formal_Mode. Such
11891 -- an error could not arise in earlier versions of the language.
11893 elsif Ekind (F) /= E_In_Parameter then
11894 Error_Msg_N ("operators can only have IN parameters", F);
11900 -- Verify that user-defined operators have proper number of arguments
11901 -- First case of operators which can only be unary
11903 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11906 -- Case of operators which can be unary or binary
11908 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11909 N_OK := (N in 1 .. 2);
11911 -- All other operators can only be binary
11919 ("incorrect number of arguments for operator", Designator);
11923 and then Base_Type (Etype (Designator)) = Standard_Boolean
11924 and then not Is_Intrinsic_Subprogram (Designator)
11927 ("explicit definition of inequality not allowed", Designator);
11929 end Valid_Operator_Definition;