1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Output; use Output;
44 with Rtsfind; use Rtsfind;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch4; use Sem_Ch4;
49 with Sem_Ch5; use Sem_Ch5;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinput; use Sinput;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
70 with Stylesw; use Stylesw;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Sem_Ch6 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Analyze_Return_Type (N : Node_Id);
83 -- Subsidiary to Process_Formals: analyze subtype mark in function
84 -- specification, in a context where the formals are visible and hide
87 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
88 -- Analyze a generic subprogram body. N is the body to be analyzed, and
89 -- Gen_Id is the defining entity Id for the corresponding spec.
91 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
92 -- If a subprogram has pragma Inline and inlining is active, use generic
93 -- machinery to build an unexpanded body for the subprogram. This body is
94 -- subsequenty used for inline expansions at call sites. If subprogram can
95 -- be inlined (depending on size and nature of local declarations) this
96 -- function returns true. Otherwise subprogram body is treated normally.
97 -- If proper warnings are enabled and the subprogram contains a construct
98 -- that cannot be inlined, the offending construct is flagged accordingly.
100 type Conformance_Type is
101 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
102 -- Conformance type used for following call, meaning matches the
103 -- RM definitions of the corresponding terms.
105 procedure Check_Conformance
108 Ctype : Conformance_Type;
110 Conforms : out Boolean;
111 Err_Loc : Node_Id := Empty;
112 Get_Inst : Boolean := False);
113 -- Given two entities, this procedure checks that the profiles associated
114 -- with these entities meet the conformance criterion given by the third
115 -- parameter. If they conform, Conforms is set True and control returns
116 -- to the caller. If they do not conform, Conforms is set to False, and
117 -- in addition, if Errmsg is True on the call, proper messages are output
118 -- to complain about the conformance failure. If Err_Loc is non_Empty
119 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
120 -- error messages are placed on the appropriate part of the construct
121 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
122 -- against a formal access-to-subprogram type so Get_Instance_Of must
125 procedure Check_Overriding_Indicator
127 Does_Override : Boolean);
128 -- Verify the consistency of an overriding_indicator given for subprogram
129 -- declaration, body, renaming, or instantiation. The flag Does_Override
130 -- is set if the scope into which we are introducing the subprogram
131 -- contains a type-conformant subprogram that becomes hidden by the new
134 procedure Check_Subprogram_Order (N : Node_Id);
135 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
136 -- the alpha ordering rule for N if this ordering requirement applicable.
138 procedure Check_Returns
142 -- Called to check for missing return statements in a function body, or
143 -- for returns present in a procedure body which has No_Return set. L is
144 -- the handled statement sequence for the subprogram body. This procedure
145 -- checks all flow paths to make sure they either have return (Mode = 'F')
146 -- or do not have a return (Mode = 'P'). The flag Err is set if there are
147 -- any control paths not explicitly terminated by a return in the function
148 -- case, and is True otherwise.
150 function Conforming_Types
153 Ctype : Conformance_Type;
154 Get_Inst : Boolean := False) return Boolean;
155 -- Check that two formal parameter types conform, checking both for
156 -- equality of base types, and where required statically matching
157 -- subtypes, depending on the setting of Ctype.
159 procedure Enter_Overloaded_Entity (S : Entity_Id);
160 -- This procedure makes S, a new overloaded entity, into the first visible
161 -- entity with that name.
163 procedure Install_Entity (E : Entity_Id);
164 -- Make single entity visible. Used for generic formals as well
166 procedure Install_Formals (Id : Entity_Id);
167 -- On entry to a subprogram body, make the formals visible. Note that
168 -- simply placing the subprogram on the scope stack is not sufficient:
169 -- the formals must become the current entities for their names.
171 function Is_Non_Overriding_Operation
173 New_E : Entity_Id) return Boolean;
174 -- Enforce the rule given in 12.3(18): a private operation in an instance
175 -- overrides an inherited operation only if the corresponding operation
176 -- was overriding in the generic. This can happen for primitive operations
177 -- of types derived (in the generic unit) from formal private or formal
180 procedure Make_Inequality_Operator (S : Entity_Id);
181 -- Create the declaration for an inequality operator that is implicitly
182 -- created by a user-defined equality operator that yields a boolean.
184 procedure May_Need_Actuals (Fun : Entity_Id);
185 -- Flag functions that can be called without parameters, i.e. those that
186 -- have no parameters, or those for which defaults exist for all parameters
188 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
189 -- If there is a separate spec for a subprogram or generic subprogram, the
190 -- formals of the body are treated as references to the corresponding
191 -- formals of the spec. This reference does not count as an actual use of
192 -- the formal, in order to diagnose formals that are unused in the body.
194 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
195 -- Formal_Id is an formal parameter entity. This procedure deals with
196 -- setting the proper validity status for this entity, which depends
197 -- on the kind of parameter and the validity checking mode.
199 ---------------------------------------------
200 -- Analyze_Abstract_Subprogram_Declaration --
201 ---------------------------------------------
203 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
204 Designator : constant Entity_Id :=
205 Analyze_Subprogram_Specification (Specification (N));
206 Scop : constant Entity_Id := Current_Scope;
209 Generate_Definition (Designator);
210 Set_Is_Abstract (Designator);
211 New_Overloaded_Entity (Designator);
212 Check_Delayed_Subprogram (Designator);
214 Set_Categorization_From_Scope (Designator, Scop);
216 if Ekind (Scope (Designator)) = E_Protected_Type then
218 ("abstract subprogram not allowed in protected type", N);
221 Generate_Reference_To_Formals (Designator);
222 end Analyze_Abstract_Subprogram_Declaration;
224 ----------------------------
225 -- Analyze_Function_Call --
226 ----------------------------
228 procedure Analyze_Function_Call (N : Node_Id) is
229 P : constant Node_Id := Name (N);
230 L : constant List_Id := Parameter_Associations (N);
236 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
237 -- as B(A, X). If the rewriting is successful, the call has been
238 -- analyzed and we just return.
240 if Nkind (P) = N_Selected_Component
241 and then Name (N) /= P
242 and then Is_Rewrite_Substitution (N)
243 and then Present (Etype (N))
248 -- If error analyzing name, then set Any_Type as result type and return
250 if Etype (P) = Any_Type then
251 Set_Etype (N, Any_Type);
255 -- Otherwise analyze the parameters
259 while Present (Actual) loop
261 Check_Parameterless_Call (Actual);
267 end Analyze_Function_Call;
269 -------------------------------------
270 -- Analyze_Generic_Subprogram_Body --
271 -------------------------------------
273 procedure Analyze_Generic_Subprogram_Body
277 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
278 Kind : constant Entity_Kind := Ekind (Gen_Id);
284 -- Copy body and disable expansion while analyzing the generic For a
285 -- stub, do not copy the stub (which would load the proper body), this
286 -- will be done when the proper body is analyzed.
288 if Nkind (N) /= N_Subprogram_Body_Stub then
289 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
294 Spec := Specification (N);
296 -- Within the body of the generic, the subprogram is callable, and
297 -- behaves like the corresponding non-generic unit.
299 Body_Id := Defining_Entity (Spec);
301 if Kind = E_Generic_Procedure
302 and then Nkind (Spec) /= N_Procedure_Specification
304 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
307 elsif Kind = E_Generic_Function
308 and then Nkind (Spec) /= N_Function_Specification
310 Error_Msg_N ("invalid body for generic function ", Body_Id);
314 Set_Corresponding_Body (Gen_Decl, Body_Id);
316 if Has_Completion (Gen_Id)
317 and then Nkind (Parent (N)) /= N_Subunit
319 Error_Msg_N ("duplicate generic body", N);
322 Set_Has_Completion (Gen_Id);
325 if Nkind (N) = N_Subprogram_Body_Stub then
326 Set_Ekind (Defining_Entity (Specification (N)), Kind);
328 Set_Corresponding_Spec (N, Gen_Id);
331 if Nkind (Parent (N)) = N_Compilation_Unit then
332 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
335 -- Make generic parameters immediately visible in the body. They are
336 -- needed to process the formals declarations. Then make the formals
337 -- visible in a separate step.
343 First_Ent : Entity_Id;
346 First_Ent := First_Entity (Gen_Id);
349 while Present (E) and then not Is_Formal (E) loop
354 Set_Use (Generic_Formal_Declarations (Gen_Decl));
356 -- Now generic formals are visible, and the specification can be
357 -- analyzed, for subsequent conformance check.
359 Body_Id := Analyze_Subprogram_Specification (Spec);
361 -- Make formal parameters visible
365 -- E is the first formal parameter, we loop through the formals
366 -- installing them so that they will be visible.
368 Set_First_Entity (Gen_Id, E);
369 while Present (E) loop
375 -- Visible generic entity is callable within its own body
377 Set_Ekind (Gen_Id, Ekind (Body_Id));
378 Set_Ekind (Body_Id, E_Subprogram_Body);
379 Set_Convention (Body_Id, Convention (Gen_Id));
380 Set_Scope (Body_Id, Scope (Gen_Id));
381 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
383 if Nkind (N) = N_Subprogram_Body_Stub then
385 -- No body to analyze, so restore state of generic unit
387 Set_Ekind (Gen_Id, Kind);
388 Set_Ekind (Body_Id, Kind);
390 if Present (First_Ent) then
391 Set_First_Entity (Gen_Id, First_Ent);
398 -- If this is a compilation unit, it must be made visible explicitly,
399 -- because the compilation of the declaration, unlike other library
400 -- unit declarations, does not. If it is not a unit, the following
401 -- is redundant but harmless.
403 Set_Is_Immediately_Visible (Gen_Id);
404 Reference_Body_Formals (Gen_Id, Body_Id);
406 Set_Actual_Subtypes (N, Current_Scope);
407 Analyze_Declarations (Declarations (N));
409 Analyze (Handled_Statement_Sequence (N));
411 Save_Global_References (Original_Node (N));
413 -- Prior to exiting the scope, include generic formals again (if any
414 -- are present) in the set of local entities.
416 if Present (First_Ent) then
417 Set_First_Entity (Gen_Id, First_Ent);
420 Check_References (Gen_Id);
423 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
425 Check_Subprogram_Order (N);
427 -- Outside of its body, unit is generic again
429 Set_Ekind (Gen_Id, Kind);
430 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
431 Style.Check_Identifier (Body_Id, Gen_Id);
433 end Analyze_Generic_Subprogram_Body;
435 -----------------------------
436 -- Analyze_Operator_Symbol --
437 -----------------------------
439 -- An operator symbol such as "+" or "and" may appear in context where the
440 -- literal denotes an entity name, such as "+"(x, y) or in context when it
441 -- is just a string, as in (conjunction = "or"). In these cases the parser
442 -- generates this node, and the semantics does the disambiguation. Other
443 -- such case are actuals in an instantiation, the generic unit in an
444 -- instantiation, and pragma arguments.
446 procedure Analyze_Operator_Symbol (N : Node_Id) is
447 Par : constant Node_Id := Parent (N);
450 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
451 or else Nkind (Par) = N_Function_Instantiation
452 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
453 or else (Nkind (Par) = N_Pragma_Argument_Association
454 and then not Is_Pragma_String_Literal (Par))
455 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
456 or else (Nkind (Par) = N_Attribute_Reference
457 and then Attribute_Name (Par) /= Name_Value)
459 Find_Direct_Name (N);
462 Change_Operator_Symbol_To_String_Literal (N);
465 end Analyze_Operator_Symbol;
467 -----------------------------------
468 -- Analyze_Parameter_Association --
469 -----------------------------------
471 procedure Analyze_Parameter_Association (N : Node_Id) is
473 Analyze (Explicit_Actual_Parameter (N));
474 end Analyze_Parameter_Association;
476 ----------------------------
477 -- Analyze_Procedure_Call --
478 ----------------------------
480 procedure Analyze_Procedure_Call (N : Node_Id) is
481 Loc : constant Source_Ptr := Sloc (N);
482 P : constant Node_Id := Name (N);
483 Actuals : constant List_Id := Parameter_Associations (N);
487 procedure Analyze_Call_And_Resolve;
488 -- Do Analyze and Resolve calls for procedure call
490 ------------------------------
491 -- Analyze_Call_And_Resolve --
492 ------------------------------
494 procedure Analyze_Call_And_Resolve is
496 if Nkind (N) = N_Procedure_Call_Statement then
498 Resolve (N, Standard_Void_Type);
502 end Analyze_Call_And_Resolve;
504 -- Start of processing for Analyze_Procedure_Call
507 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
508 -- a procedure call or an entry call. The prefix may denote an access
509 -- to subprogram type, in which case an implicit dereference applies.
510 -- If the prefix is an indexed component (without implicit defererence)
511 -- then the construct denotes a call to a member of an entire family.
512 -- If the prefix is a simple name, it may still denote a call to a
513 -- parameterless member of an entry family. Resolution of these various
514 -- interpretations is delicate.
518 -- If this is a call of the form Obj.Op, the call may have been
519 -- analyzed and possibly rewritten into a block, in which case
526 -- If error analyzing prefix, then set Any_Type as result and return
528 if Etype (P) = Any_Type then
529 Set_Etype (N, Any_Type);
533 -- Otherwise analyze the parameters
535 if Present (Actuals) then
536 Actual := First (Actuals);
538 while Present (Actual) loop
540 Check_Parameterless_Call (Actual);
545 -- Special processing for Elab_Spec and Elab_Body calls
547 if Nkind (P) = N_Attribute_Reference
548 and then (Attribute_Name (P) = Name_Elab_Spec
549 or else Attribute_Name (P) = Name_Elab_Body)
551 if Present (Actuals) then
553 ("no parameters allowed for this call", First (Actuals));
557 Set_Etype (N, Standard_Void_Type);
560 elsif Is_Entity_Name (P)
561 and then Is_Record_Type (Etype (Entity (P)))
562 and then Remote_AST_I_Dereference (P)
566 elsif Is_Entity_Name (P)
567 and then Ekind (Entity (P)) /= E_Entry_Family
569 if Is_Access_Type (Etype (P))
570 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
571 and then No (Actuals)
572 and then Comes_From_Source (N)
574 Error_Msg_N ("missing explicit dereference in call", N);
577 Analyze_Call_And_Resolve;
579 -- If the prefix is the simple name of an entry family, this is
580 -- a parameterless call from within the task body itself.
582 elsif Is_Entity_Name (P)
583 and then Nkind (P) = N_Identifier
584 and then Ekind (Entity (P)) = E_Entry_Family
585 and then Present (Actuals)
586 and then No (Next (First (Actuals)))
588 -- Can be call to parameterless entry family. What appears to be the
589 -- sole argument is in fact the entry index. Rewrite prefix of node
590 -- accordingly. Source representation is unchanged by this
594 Make_Indexed_Component (Loc,
596 Make_Selected_Component (Loc,
597 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
598 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
599 Expressions => Actuals);
601 Set_Etype (New_N, Standard_Void_Type);
602 Set_Parameter_Associations (N, No_List);
603 Analyze_Call_And_Resolve;
605 elsif Nkind (P) = N_Explicit_Dereference then
606 if Ekind (Etype (P)) = E_Subprogram_Type then
607 Analyze_Call_And_Resolve;
609 Error_Msg_N ("expect access to procedure in call", P);
612 -- The name can be a selected component or an indexed component that
613 -- yields an access to subprogram. Such a prefix is legal if the call
614 -- has parameter associations.
616 elsif Is_Access_Type (Etype (P))
617 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
619 if Present (Actuals) then
620 Analyze_Call_And_Resolve;
622 Error_Msg_N ("missing explicit dereference in call ", N);
625 -- If not an access to subprogram, then the prefix must resolve to the
626 -- name of an entry, entry family, or protected operation.
628 -- For the case of a simple entry call, P is a selected component where
629 -- the prefix is the task and the selector name is the entry. A call to
630 -- a protected procedure will have the same syntax. If the protected
631 -- object contains overloaded operations, the entity may appear as a
632 -- function, the context will select the operation whose type is Void.
634 elsif Nkind (P) = N_Selected_Component
635 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
637 Ekind (Entity (Selector_Name (P))) = E_Procedure
639 Ekind (Entity (Selector_Name (P))) = E_Function)
641 Analyze_Call_And_Resolve;
643 elsif Nkind (P) = N_Selected_Component
644 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
645 and then Present (Actuals)
646 and then No (Next (First (Actuals)))
648 -- Can be call to parameterless entry family. What appears to be the
649 -- sole argument is in fact the entry index. Rewrite prefix of node
650 -- accordingly. Source representation is unchanged by this
654 Make_Indexed_Component (Loc,
655 Prefix => New_Copy (P),
656 Expressions => Actuals);
658 Set_Etype (New_N, Standard_Void_Type);
659 Set_Parameter_Associations (N, No_List);
660 Analyze_Call_And_Resolve;
662 -- For the case of a reference to an element of an entry family, P is
663 -- an indexed component whose prefix is a selected component (task and
664 -- entry family), and whose index is the entry family index.
666 elsif Nkind (P) = N_Indexed_Component
667 and then Nkind (Prefix (P)) = N_Selected_Component
668 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
670 Analyze_Call_And_Resolve;
672 -- If the prefix is the name of an entry family, it is a call from
673 -- within the task body itself.
675 elsif Nkind (P) = N_Indexed_Component
676 and then Nkind (Prefix (P)) = N_Identifier
677 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
680 Make_Selected_Component (Loc,
681 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
682 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
683 Rewrite (Prefix (P), New_N);
685 Analyze_Call_And_Resolve;
687 -- Anything else is an error
690 Error_Msg_N ("invalid procedure or entry call", N);
692 end Analyze_Procedure_Call;
694 ------------------------------
695 -- Analyze_Return_Statement --
696 ------------------------------
698 procedure Analyze_Return_Statement (N : Node_Id) is
699 Loc : constant Source_Ptr := Sloc (N);
701 Scope_Id : Entity_Id;
706 -- Find subprogram or accept statement enclosing the return statement
709 for J in reverse 0 .. Scope_Stack.Last loop
710 Scope_Id := Scope_Stack.Table (J).Entity;
711 exit when Ekind (Scope_Id) /= E_Block and then
712 Ekind (Scope_Id) /= E_Loop;
715 pragma Assert (Present (Scope_Id));
717 Kind := Ekind (Scope_Id);
718 Expr := Expression (N);
720 if Kind /= E_Function
721 and then Kind /= E_Generic_Function
722 and then Kind /= E_Procedure
723 and then Kind /= E_Generic_Procedure
724 and then Kind /= E_Entry
725 and then Kind /= E_Entry_Family
727 Error_Msg_N ("illegal context for return statement", N);
729 elsif Present (Expr) then
730 if Kind = E_Function or else Kind = E_Generic_Function then
731 Set_Return_Present (Scope_Id);
732 R_Type := Etype (Scope_Id);
733 Set_Return_Type (N, R_Type);
734 Analyze_And_Resolve (Expr, R_Type);
736 if (Is_Class_Wide_Type (Etype (Expr))
737 or else Is_Dynamically_Tagged (Expr))
738 and then not Is_Class_Wide_Type (R_Type)
741 ("dynamically tagged expression not allowed!", Expr);
744 Apply_Constraint_Check (Expr, R_Type);
746 -- ??? A real run-time accessibility check is needed in cases
747 -- involving dereferences of access parameters. For now we just
748 -- check the static cases.
750 if Is_Return_By_Reference_Type (Etype (Scope_Id))
751 and then Object_Access_Level (Expr)
752 > Subprogram_Access_Level (Scope_Id)
755 Make_Raise_Program_Error (Loc,
756 Reason => PE_Accessibility_Check_Failed));
760 ("cannot return a local value by reference?", N);
762 ("& will be raised at run time?!",
763 N, Standard_Program_Error);
766 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
767 Error_Msg_N ("procedure cannot return value (use function)", N);
770 Error_Msg_N ("accept statement cannot return value", N);
773 -- No expression present
776 if Kind = E_Function or Kind = E_Generic_Function then
777 Error_Msg_N ("missing expression in return from function", N);
780 if (Ekind (Scope_Id) = E_Procedure
781 or else Ekind (Scope_Id) = E_Generic_Procedure)
782 and then No_Return (Scope_Id)
785 ("RETURN statement not allowed (No_Return)", N);
789 Check_Unreachable_Code (N);
790 end Analyze_Return_Statement;
792 -------------------------
793 -- Analyze_Return_Type --
794 -------------------------
796 procedure Analyze_Return_Type (N : Node_Id) is
797 Designator : constant Entity_Id := Defining_Entity (N);
798 Typ : Entity_Id := Empty;
801 if Subtype_Mark (N) /= Error then
802 Find_Type (Subtype_Mark (N));
803 Typ := Entity (Subtype_Mark (N));
804 Set_Etype (Designator, Typ);
806 if Ekind (Typ) = E_Incomplete_Type
807 or else (Is_Class_Wide_Type (Typ)
809 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
812 ("invalid use of incomplete type", Subtype_Mark (N));
816 Set_Etype (Designator, Any_Type);
818 end Analyze_Return_Type;
820 -----------------------------
821 -- Analyze_Subprogram_Body --
822 -----------------------------
824 -- This procedure is called for regular subprogram bodies, generic bodies,
825 -- and for subprogram stubs of both kinds. In the case of stubs, only the
826 -- specification matters, and is used to create a proper declaration for
827 -- the subprogram, or to perform conformance checks.
829 procedure Analyze_Subprogram_Body (N : Node_Id) is
830 Loc : constant Source_Ptr := Sloc (N);
831 Body_Spec : constant Node_Id := Specification (N);
832 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
833 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
834 Body_Deleted : constant Boolean := False;
838 Spec_Decl : Node_Id := Empty;
839 Last_Formal : Entity_Id := Empty;
840 Conformant : Boolean;
841 Missing_Ret : Boolean;
844 procedure Check_Following_Pragma;
845 -- If front-end inlining is enabled, look ahead to recognize a pragma
846 -- that may appear after the body.
848 procedure Verify_Overriding_Indicator;
849 -- If there was a previous spec, the entity has been entered in the
850 -- current scope previously. If the body itself carries an overriding
851 -- indicator, check that it is consistent with the known status of the
854 ----------------------------
855 -- Check_Following_Pragma --
856 ----------------------------
858 procedure Check_Following_Pragma is
862 if Front_End_Inlining
863 and then Is_List_Member (N)
864 and then Present (Spec_Decl)
865 and then List_Containing (N) = List_Containing (Spec_Decl)
870 and then Nkind (Prag) = N_Pragma
871 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
874 (Expression (First (Pragma_Argument_Associations (Prag))))
880 end Check_Following_Pragma;
882 ---------------------------------
883 -- Verify_Overriding_Indicator --
884 ---------------------------------
886 procedure Verify_Overriding_Indicator is
888 if Must_Override (Body_Spec)
889 and then not Is_Overriding_Operation (Spec_Id)
892 ("subprogram& is not overriding", Body_Spec, Spec_Id);
894 elsif Must_Not_Override (Body_Spec)
895 and then Is_Overriding_Operation (Spec_Id)
898 ("subprogram& overrides inherited operation",
901 end Verify_Overriding_Indicator;
903 -- Start of processing for Analyze_Subprogram_Body
907 Write_Str ("==== Compiling subprogram body ");
908 Write_Name (Chars (Body_Id));
909 Write_Str (" from ");
910 Write_Location (Loc);
914 Trace_Scope (N, Body_Id, " Analyze subprogram");
916 -- Generic subprograms are handled separately. They always have a
917 -- generic specification. Determine whether current scope has a
918 -- previous declaration.
920 -- If the subprogram body is defined within an instance of the same
921 -- name, the instance appears as a package renaming, and will be hidden
922 -- within the subprogram.
925 and then not Is_Overloadable (Prev_Id)
926 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
927 or else Comes_From_Source (Prev_Id))
929 if Is_Generic_Subprogram (Prev_Id) then
931 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
932 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
934 Analyze_Generic_Subprogram_Body (N, Spec_Id);
938 -- Previous entity conflicts with subprogram name. Attempting to
939 -- enter name will post error.
941 Enter_Name (Body_Id);
945 -- Non-generic case, find the subprogram declaration, if one was seen,
946 -- or enter new overloaded entity in the current scope. If the
947 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
948 -- part of the context of one of its subunits. No need to redo the
951 elsif Prev_Id = Body_Id
952 and then Has_Completion (Body_Id)
957 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
959 if Nkind (N) = N_Subprogram_Body_Stub
960 or else No (Corresponding_Spec (N))
962 Spec_Id := Find_Corresponding_Spec (N);
964 -- If this is a duplicate body, no point in analyzing it
966 if Error_Posted (N) then
970 -- A subprogram body should cause freezing of its own declaration,
971 -- but if there was no previous explicit declaration, then the
972 -- subprogram will get frozen too late (there may be code within
973 -- the body that depends on the subprogram having been frozen,
974 -- such as uses of extra formals), so we force it to be frozen
975 -- here. Same holds if the body and the spec are compilation
979 Freeze_Before (N, Body_Id);
981 elsif Nkind (Parent (N)) = N_Compilation_Unit then
982 Freeze_Before (N, Spec_Id);
985 Spec_Id := Corresponding_Spec (N);
989 -- Do not inline any subprogram that contains nested subprograms, since
990 -- the backend inlining circuit seems to generate uninitialized
991 -- references in this case. We know this happens in the case of front
992 -- end ZCX support, but it also appears it can happen in other cases as
993 -- well. The backend often rejects attempts to inline in the case of
994 -- nested procedures anyway, so little if anything is lost by this.
995 -- Note that this is test is for the benefit of the back-end. There is
996 -- a separate test for front-end inlining that also rejects nested
999 -- Do not do this test if errors have been detected, because in some
1000 -- error cases, this code blows up, and we don't need it anyway if
1001 -- there have been errors, since we won't get to the linker anyway.
1003 if Comes_From_Source (Body_Id)
1004 and then Serious_Errors_Detected = 0
1008 P_Ent := Scope (P_Ent);
1009 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1011 if Is_Subprogram (P_Ent) then
1012 Set_Is_Inlined (P_Ent, False);
1014 if Comes_From_Source (P_Ent)
1015 and then Has_Pragma_Inline (P_Ent)
1018 ("cannot inline& (nested subprogram)?",
1025 -- Case of fully private operation in the body of the protected type.
1026 -- We must create a declaration for the subprogram, in order to attach
1027 -- the protected subprogram that will be used in internal calls.
1030 and then Comes_From_Source (N)
1031 and then Is_Protected_Type (Current_Scope)
1040 Formal := First_Formal (Body_Id);
1042 -- The protected operation always has at least one formal, namely
1043 -- the object itself, but it is only placed in the parameter list
1044 -- if expansion is enabled.
1047 or else Expander_Active
1055 while Present (Formal) loop
1057 (Make_Parameter_Specification (Loc,
1058 Defining_Identifier =>
1059 Make_Defining_Identifier (Sloc (Formal),
1060 Chars => Chars (Formal)),
1061 In_Present => In_Present (Parent (Formal)),
1062 Out_Present => Out_Present (Parent (Formal)),
1064 New_Reference_To (Etype (Formal), Loc),
1066 New_Copy_Tree (Expression (Parent (Formal)))),
1069 Next_Formal (Formal);
1072 if Nkind (Body_Spec) = N_Procedure_Specification then
1074 Make_Procedure_Specification (Loc,
1075 Defining_Unit_Name =>
1076 Make_Defining_Identifier (Sloc (Body_Id),
1077 Chars => Chars (Body_Id)),
1078 Parameter_Specifications => Plist);
1081 Make_Function_Specification (Loc,
1082 Defining_Unit_Name =>
1083 Make_Defining_Identifier (Sloc (Body_Id),
1084 Chars => Chars (Body_Id)),
1085 Parameter_Specifications => Plist,
1086 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1090 Make_Subprogram_Declaration (Loc,
1091 Specification => New_Spec);
1092 Insert_Before (N, Decl);
1093 Spec_Id := Defining_Unit_Name (New_Spec);
1095 -- Indicate that the entity comes from source, to ensure that
1096 -- cross-reference information is properly generated. The body
1097 -- itself is rewritten during expansion, and the body entity will
1098 -- not appear in calls to the operation.
1100 Set_Comes_From_Source (Spec_Id, True);
1102 Set_Has_Completion (Spec_Id);
1103 Set_Convention (Spec_Id, Convention_Protected);
1106 elsif Present (Spec_Id) then
1107 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1108 Verify_Overriding_Indicator;
1111 -- Place subprogram on scope stack, and make formals visible. If there
1112 -- is a spec, the visible entity remains that of the spec.
1114 if Present (Spec_Id) then
1115 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1117 if Is_Child_Unit (Spec_Id) then
1118 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1122 Style.Check_Identifier (Body_Id, Spec_Id);
1125 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1126 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1128 if Is_Abstract (Spec_Id) then
1129 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1132 Set_Convention (Body_Id, Convention (Spec_Id));
1133 Set_Has_Completion (Spec_Id);
1135 if Is_Protected_Type (Scope (Spec_Id)) then
1136 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1139 -- If this is a body generated for a renaming, do not check for
1140 -- full conformance. The check is redundant, because the spec of
1141 -- the body is a copy of the spec in the renaming declaration,
1142 -- and the test can lead to spurious errors on nested defaults.
1144 if Present (Spec_Decl)
1145 and then not Comes_From_Source (N)
1147 (Nkind (Original_Node (Spec_Decl)) =
1148 N_Subprogram_Renaming_Declaration
1149 or else (Present (Corresponding_Body (Spec_Decl))
1151 Nkind (Unit_Declaration_Node
1152 (Corresponding_Body (Spec_Decl))) =
1153 N_Subprogram_Renaming_Declaration))
1159 Fully_Conformant, True, Conformant, Body_Id);
1162 -- If the body is not fully conformant, we have to decide if we
1163 -- should analyze it or not. If it has a really messed up profile
1164 -- then we probably should not analyze it, since we will get too
1165 -- many bogus messages.
1167 -- Our decision is to go ahead in the non-fully conformant case
1168 -- only if it is at least mode conformant with the spec. Note
1169 -- that the call to Check_Fully_Conformant has issued the proper
1170 -- error messages to complain about the lack of conformance.
1173 and then not Mode_Conformant (Body_Id, Spec_Id)
1179 if Spec_Id /= Body_Id then
1180 Reference_Body_Formals (Spec_Id, Body_Id);
1183 if Nkind (N) /= N_Subprogram_Body_Stub then
1184 Set_Corresponding_Spec (N, Spec_Id);
1186 -- Ada 2005 (AI-345): Restore the correct Etype: here we undo the
1187 -- work done by Analyze_Subprogram_Specification to allow the
1188 -- overriding of task, protected and interface primitives.
1190 if Comes_From_Source (Spec_Id)
1191 and then Present (First_Entity (Spec_Id))
1192 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
1193 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
1194 and then Present (Abstract_Interfaces
1195 (Etype (First_Entity (Spec_Id))))
1196 and then Present (Corresponding_Concurrent_Type
1197 (Etype (First_Entity (Spec_Id))))
1199 Set_Etype (First_Entity (Spec_Id),
1200 Corresponding_Concurrent_Type
1201 (Etype (First_Entity (Spec_Id))));
1204 -- Comment needed here, since this is not Ada 2005 stuff! ???
1206 Install_Formals (Spec_Id);
1207 Last_Formal := Last_Entity (Spec_Id);
1208 New_Scope (Spec_Id);
1210 -- Make sure that the subprogram is immediately visible. For
1211 -- child units that have no separate spec this is indispensable.
1212 -- Otherwise it is safe albeit redundant.
1214 Set_Is_Immediately_Visible (Spec_Id);
1217 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1218 Set_Ekind (Body_Id, E_Subprogram_Body);
1219 Set_Scope (Body_Id, Scope (Spec_Id));
1221 -- Case of subprogram body with no previous spec
1225 and then Comes_From_Source (Body_Id)
1226 and then not Suppress_Style_Checks (Body_Id)
1227 and then not In_Instance
1229 Style.Body_With_No_Spec (N);
1232 New_Overloaded_Entity (Body_Id);
1234 if Nkind (N) /= N_Subprogram_Body_Stub then
1235 Set_Acts_As_Spec (N);
1236 Generate_Definition (Body_Id);
1238 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1239 Generate_Reference_To_Formals (Body_Id);
1240 Install_Formals (Body_Id);
1241 New_Scope (Body_Id);
1245 -- If this is the proper body of a stub, we must verify that the stub
1246 -- conforms to the body, and to the previous spec if one was present.
1247 -- we know already that the body conforms to that spec. This test is
1248 -- only required for subprograms that come from source.
1250 if Nkind (Parent (N)) = N_Subunit
1251 and then Comes_From_Source (N)
1252 and then not Error_Posted (Body_Id)
1253 and then Nkind (Corresponding_Stub (Parent (N))) =
1254 N_Subprogram_Body_Stub
1257 Old_Id : constant Entity_Id :=
1259 (Specification (Corresponding_Stub (Parent (N))));
1261 Conformant : Boolean := False;
1264 if No (Spec_Id) then
1265 Check_Fully_Conformant (Body_Id, Old_Id);
1269 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1271 if not Conformant then
1273 -- The stub was taken to be a new declaration. Indicate
1274 -- that it lacks a body.
1276 Set_Has_Completion (Old_Id, False);
1282 Set_Has_Completion (Body_Id);
1283 Check_Eliminated (Body_Id);
1285 if Nkind (N) = N_Subprogram_Body_Stub then
1288 elsif Present (Spec_Id)
1289 and then Expander_Active
1291 Check_Following_Pragma;
1293 if Is_Always_Inlined (Spec_Id)
1294 or else (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining)
1296 Build_Body_To_Inline (N, Spec_Id);
1300 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1301 -- if its specification we have to install the private withed units.
1303 if Is_Compilation_Unit (Body_Id)
1304 and then Scope (Body_Id) = Standard_Standard
1306 Install_Private_With_Clauses (Body_Id);
1309 -- Now we can go on to analyze the body
1311 HSS := Handled_Statement_Sequence (N);
1312 Set_Actual_Subtypes (N, Current_Scope);
1313 Analyze_Declarations (Declarations (N));
1316 Process_End_Label (HSS, 't', Current_Scope);
1318 Check_Subprogram_Order (N);
1319 Set_Analyzed (Body_Id);
1321 -- If we have a separate spec, then the analysis of the declarations
1322 -- caused the entities in the body to be chained to the spec id, but
1323 -- we want them chained to the body id. Only the formal parameters
1324 -- end up chained to the spec id in this case.
1326 if Present (Spec_Id) then
1328 -- If a parent unit is categorized, the context of a subunit must
1329 -- conform to the categorization. Conversely, if a child unit is
1330 -- categorized, the parents themselves must conform.
1332 if Nkind (Parent (N)) = N_Subunit then
1333 Validate_Categorization_Dependency (N, Spec_Id);
1335 elsif Is_Child_Unit (Spec_Id) then
1336 Validate_Categorization_Dependency
1337 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1340 if Present (Last_Formal) then
1342 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1343 Set_Next_Entity (Last_Formal, Empty);
1344 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1345 Set_Last_Entity (Spec_Id, Last_Formal);
1348 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1349 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1350 Set_First_Entity (Spec_Id, Empty);
1351 Set_Last_Entity (Spec_Id, Empty);
1355 -- If function, check return statements
1357 if Nkind (Body_Spec) = N_Function_Specification then
1362 if Present (Spec_Id) then
1368 if Return_Present (Id) then
1369 Check_Returns (HSS, 'F', Missing_Ret);
1372 Set_Has_Missing_Return (Id);
1375 elsif not Is_Machine_Code_Subprogram (Id)
1376 and then not Body_Deleted
1378 Error_Msg_N ("missing RETURN statement in function body", N);
1382 -- If procedure with No_Return, check returns
1384 elsif Nkind (Body_Spec) = N_Procedure_Specification
1385 and then Present (Spec_Id)
1386 and then No_Return (Spec_Id)
1388 Check_Returns (HSS, 'P', Missing_Ret);
1391 -- Now we are going to check for variables that are never modified in
1392 -- the body of the procedure. We omit these checks if the first
1393 -- statement of the procedure raises an exception. In particular this
1394 -- deals with the common idiom of a stubbed function, which might
1395 -- appear as something like
1397 -- function F (A : Integer) return Some_Type;
1400 -- raise Program_Error;
1404 -- Here the purpose of X is simply to satisfy the (annoying)
1405 -- requirement in Ada that there be at least one return, and we
1406 -- certainly do not want to go posting warnings on X that it is not
1410 Stm : Node_Id := First (Statements (HSS));
1413 -- Skip an initial label (for one thing this occurs when we are in
1414 -- front end ZCX mode, but in any case it is irrelevant).
1416 if Nkind (Stm) = N_Label then
1420 -- Do the test on the original statement before expansion
1423 Ostm : constant Node_Id := Original_Node (Stm);
1426 -- If explicit raise statement, return with no checks
1428 if Nkind (Ostm) = N_Raise_Statement then
1431 -- Check for explicit call cases which likely raise an exception
1433 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1434 if Is_Entity_Name (Name (Ostm)) then
1436 Ent : constant Entity_Id := Entity (Name (Ostm));
1439 -- If the procedure is marked No_Return, then likely it
1440 -- raises an exception, but in any case it is not coming
1441 -- back here, so no need to check beyond the call.
1443 if Ekind (Ent) = E_Procedure
1444 and then No_Return (Ent)
1448 -- If the procedure name is Raise_Exception, then also
1449 -- assume that it raises an exception. The main target
1450 -- here is Ada.Exceptions.Raise_Exception, but this name
1451 -- is pretty evocative in any context! Note that the
1452 -- procedure in Ada.Exceptions is not marked No_Return
1453 -- because of the annoying case of the null exception Id.
1455 elsif Chars (Ent) = Name_Raise_Exception then
1464 -- Check for variables that are never modified
1470 -- If there is a separate spec, then transfer Never_Set_In_Source
1471 -- flags from out parameters to the corresponding entities in the
1472 -- body. The reason we do that is we want to post error flags on
1473 -- the body entities, not the spec entities.
1475 if Present (Spec_Id) then
1476 E1 := First_Entity (Spec_Id);
1478 while Present (E1) loop
1479 if Ekind (E1) = E_Out_Parameter then
1480 E2 := First_Entity (Body_Id);
1481 while Present (E2) loop
1482 exit when Chars (E1) = Chars (E2);
1486 if Present (E2) then
1487 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1495 -- Check references in body unless it was deleted. Note that the
1496 -- check of Body_Deleted here is not just for efficiency, it is
1497 -- necessary to avoid junk warnings on formal parameters.
1499 if not Body_Deleted then
1500 Check_References (Body_Id);
1503 end Analyze_Subprogram_Body;
1505 ------------------------------------
1506 -- Analyze_Subprogram_Declaration --
1507 ------------------------------------
1509 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1510 Designator : constant Entity_Id :=
1511 Analyze_Subprogram_Specification (Specification (N));
1512 Scop : constant Entity_Id := Current_Scope;
1514 -- Start of processing for Analyze_Subprogram_Declaration
1517 Generate_Definition (Designator);
1519 -- Check for RCI unit subprogram declarations against in-lined
1520 -- subprograms and subprograms having access parameter or limited
1521 -- parameter without Read and Write (RM E.2.3(12-13)).
1523 Validate_RCI_Subprogram_Declaration (N);
1527 Defining_Entity (N),
1528 " Analyze subprogram spec. ");
1530 if Debug_Flag_C then
1531 Write_Str ("==== Compiling subprogram spec ");
1532 Write_Name (Chars (Designator));
1533 Write_Str (" from ");
1534 Write_Location (Sloc (N));
1538 New_Overloaded_Entity (Designator);
1539 Check_Delayed_Subprogram (Designator);
1541 -- What is the following code for, it used to be
1543 -- ??? Set_Suppress_Elaboration_Checks
1544 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1546 -- The following seems equivalent, but a bit dubious
1548 if Elaboration_Checks_Suppressed (Designator) then
1549 Set_Kill_Elaboration_Checks (Designator);
1552 if Scop /= Standard_Standard
1553 and then not Is_Child_Unit (Designator)
1555 Set_Categorization_From_Scope (Designator, Scop);
1557 -- For a compilation unit, check for library-unit pragmas
1559 New_Scope (Designator);
1560 Set_Categorization_From_Pragmas (N);
1561 Validate_Categorization_Dependency (N, Designator);
1565 -- For a compilation unit, set body required. This flag will only be
1566 -- reset if a valid Import or Interface pragma is processed later on.
1568 if Nkind (Parent (N)) = N_Compilation_Unit then
1569 Set_Body_Required (Parent (N), True);
1571 if Ada_Version >= Ada_05
1572 and then Nkind (Specification (N)) = N_Procedure_Specification
1573 and then Null_Present (Specification (N))
1576 ("null procedure cannot be declared at library level", N);
1580 Generate_Reference_To_Formals (Designator);
1581 Check_Eliminated (Designator);
1583 -- Ada 2005: if procedure is declared with "is null" qualifier,
1584 -- it requires no body.
1586 if Nkind (Specification (N)) = N_Procedure_Specification
1587 and then Null_Present (Specification (N))
1589 Set_Has_Completion (Designator);
1590 Set_Is_Inlined (Designator);
1592 end Analyze_Subprogram_Declaration;
1594 --------------------------------------
1595 -- Analyze_Subprogram_Specification --
1596 --------------------------------------
1598 -- Reminder: N here really is a subprogram specification (not a subprogram
1599 -- declaration). This procedure is called to analyze the specification in
1600 -- both subprogram bodies and subprogram declarations (specs).
1602 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1603 Designator : constant Entity_Id := Defining_Entity (N);
1604 Formals : constant List_Id := Parameter_Specifications (N);
1606 function Has_Interface_Formals (T : List_Id) return Boolean;
1607 -- Ada 2005 (AI-251): Returns true if some non class-wide interface
1610 ---------------------------
1611 -- Has_Interface_Formals --
1612 ---------------------------
1614 function Has_Interface_Formals (T : List_Id) return Boolean is
1615 Param_Spec : Node_Id;
1619 Param_Spec := First (T);
1621 while Present (Param_Spec) loop
1622 Formal := Defining_Identifier (Param_Spec);
1624 if Is_Class_Wide_Type (Etype (Formal)) then
1627 elsif Is_Interface (Etype (Formal)) then
1635 end Has_Interface_Formals;
1637 -- Start of processing for Analyze_Subprogram_Specification
1640 Generate_Definition (Designator);
1642 if Nkind (N) = N_Function_Specification then
1643 Set_Ekind (Designator, E_Function);
1644 Set_Mechanism (Designator, Default_Mechanism);
1647 Set_Ekind (Designator, E_Procedure);
1648 Set_Etype (Designator, Standard_Void_Type);
1651 -- Introduce new scope for analysis of the formals and of the
1654 Set_Scope (Designator, Current_Scope);
1656 if Present (Formals) then
1657 New_Scope (Designator);
1658 Process_Formals (Formals, N);
1660 -- Ada 2005 (AI-345): Allow overriding primitives of protected
1661 -- interfaces by means of normal subprograms. For this purpose
1662 -- temporarily use the corresponding record type as the etype
1663 -- of the first formal.
1665 if Ada_Version >= Ada_05
1666 and then Comes_From_Source (Designator)
1667 and then Present (First_Entity (Designator))
1668 and then (Ekind (Etype (First_Entity (Designator)))
1671 Ekind (Etype (First_Entity (Designator)))
1673 and then Present (Corresponding_Record_Type
1674 (Etype (First_Entity (Designator))))
1675 and then Present (Abstract_Interfaces
1676 (Corresponding_Record_Type
1677 (Etype (First_Entity (Designator)))))
1679 Set_Etype (First_Entity (Designator),
1680 Corresponding_Record_Type (Etype (First_Entity (Designator))));
1685 elsif Nkind (N) = N_Function_Specification then
1686 Analyze_Return_Type (N);
1689 if Nkind (N) = N_Function_Specification then
1690 if Nkind (Designator) = N_Defining_Operator_Symbol then
1691 Valid_Operator_Definition (Designator);
1694 May_Need_Actuals (Designator);
1696 if Is_Abstract (Etype (Designator))
1697 and then Nkind (Parent (N))
1698 /= N_Abstract_Subprogram_Declaration
1699 and then (Nkind (Parent (N)))
1700 /= N_Formal_Abstract_Subprogram_Declaration
1701 and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1702 or else not Is_Entity_Name (Name (Parent (N)))
1703 or else not Is_Abstract (Entity (Name (Parent (N)))))
1706 ("function that returns abstract type must be abstract", N);
1710 if Ada_Version >= Ada_05
1711 and then Comes_From_Source (N)
1712 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1713 and then (Nkind (N) /= N_Procedure_Specification
1715 not Null_Present (N))
1716 and then Has_Interface_Formals (Formals)
1718 Error_Msg_Name_1 := Chars (Defining_Unit_Name
1719 (Specification (Parent (N))));
1721 ("(Ada 2005) interface subprogram % must be abstract or null", N);
1725 end Analyze_Subprogram_Specification;
1727 --------------------------
1728 -- Build_Body_To_Inline --
1729 --------------------------
1731 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1732 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1733 Original_Body : Node_Id;
1734 Body_To_Analyze : Node_Id;
1735 Max_Size : constant := 10;
1736 Stat_Count : Integer := 0;
1738 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1739 -- Check for declarations that make inlining not worthwhile
1741 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1742 -- Check for statements that make inlining not worthwhile: any tasking
1743 -- statement, nested at any level. Keep track of total number of
1744 -- elementary statements, as a measure of acceptable size.
1746 function Has_Pending_Instantiation return Boolean;
1747 -- If some enclosing body contains instantiations that appear before
1748 -- the corresponding generic body, the enclosing body has a freeze node
1749 -- so that it can be elaborated after the generic itself. This might
1750 -- conflict with subsequent inlinings, so that it is unsafe to try to
1751 -- inline in such a case.
1753 procedure Remove_Pragmas;
1754 -- A pragma Unreferenced that mentions a formal parameter has no
1755 -- meaning when the body is inlined and the formals are rewritten.
1756 -- Remove it from body to inline. The analysis of the non-inlined body
1757 -- will handle the pragma properly.
1759 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1760 -- If the body of the subprogram includes a call that returns an
1761 -- unconstrained type, the secondary stack is involved, and it
1762 -- is not worth inlining.
1764 ------------------------------
1765 -- Has_Excluded_Declaration --
1766 ------------------------------
1768 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1771 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1772 -- Nested subprograms make a given body ineligible for inlining, but
1773 -- we make an exception for instantiations of unchecked conversion.
1774 -- The body has not been analyzed yet, so check the name, and verify
1775 -- that the visible entity with that name is the predefined unit.
1777 -----------------------------
1778 -- Is_Unchecked_Conversion --
1779 -----------------------------
1781 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1782 Id : constant Node_Id := Name (D);
1786 if Nkind (Id) = N_Identifier
1787 and then Chars (Id) = Name_Unchecked_Conversion
1789 Conv := Current_Entity (Id);
1791 elsif (Nkind (Id) = N_Selected_Component
1792 or else Nkind (Id) = N_Expanded_Name)
1793 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1795 Conv := Current_Entity (Selector_Name (Id));
1801 return Present (Conv)
1802 and then Is_Predefined_File_Name
1803 (Unit_File_Name (Get_Source_Unit (Conv)))
1804 and then Is_Intrinsic_Subprogram (Conv);
1805 end Is_Unchecked_Conversion;
1807 -- Start of processing for Has_Excluded_Declaration
1812 while Present (D) loop
1813 if (Nkind (D) = N_Function_Instantiation
1814 and then not Is_Unchecked_Conversion (D))
1815 or else Nkind (D) = N_Protected_Type_Declaration
1816 or else Nkind (D) = N_Package_Declaration
1817 or else Nkind (D) = N_Package_Instantiation
1818 or else Nkind (D) = N_Subprogram_Body
1819 or else Nkind (D) = N_Procedure_Instantiation
1820 or else Nkind (D) = N_Task_Type_Declaration
1823 ("cannot inline & (non-allowed declaration)?", D, Subp);
1831 end Has_Excluded_Declaration;
1833 ----------------------------
1834 -- Has_Excluded_Statement --
1835 ----------------------------
1837 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1844 while Present (S) loop
1845 Stat_Count := Stat_Count + 1;
1847 if Nkind (S) = N_Abort_Statement
1848 or else Nkind (S) = N_Asynchronous_Select
1849 or else Nkind (S) = N_Conditional_Entry_Call
1850 or else Nkind (S) = N_Delay_Relative_Statement
1851 or else Nkind (S) = N_Delay_Until_Statement
1852 or else Nkind (S) = N_Selective_Accept
1853 or else Nkind (S) = N_Timed_Entry_Call
1856 ("cannot inline & (non-allowed statement)?", S, Subp);
1859 elsif Nkind (S) = N_Block_Statement then
1860 if Present (Declarations (S))
1861 and then Has_Excluded_Declaration (Declarations (S))
1865 elsif Present (Handled_Statement_Sequence (S))
1868 (Exception_Handlers (Handled_Statement_Sequence (S)))
1870 Has_Excluded_Statement
1871 (Statements (Handled_Statement_Sequence (S))))
1876 elsif Nkind (S) = N_Case_Statement then
1877 E := First (Alternatives (S));
1878 while Present (E) loop
1879 if Has_Excluded_Statement (Statements (E)) then
1886 elsif Nkind (S) = N_If_Statement then
1887 if Has_Excluded_Statement (Then_Statements (S)) then
1891 if Present (Elsif_Parts (S)) then
1892 E := First (Elsif_Parts (S));
1893 while Present (E) loop
1894 if Has_Excluded_Statement (Then_Statements (E)) then
1901 if Present (Else_Statements (S))
1902 and then Has_Excluded_Statement (Else_Statements (S))
1907 elsif Nkind (S) = N_Loop_Statement
1908 and then Has_Excluded_Statement (Statements (S))
1917 end Has_Excluded_Statement;
1919 -------------------------------
1920 -- Has_Pending_Instantiation --
1921 -------------------------------
1923 function Has_Pending_Instantiation return Boolean is
1924 S : Entity_Id := Current_Scope;
1927 while Present (S) loop
1928 if Is_Compilation_Unit (S)
1929 or else Is_Child_Unit (S)
1932 elsif Ekind (S) = E_Package
1933 and then Has_Forward_Instantiation (S)
1942 end Has_Pending_Instantiation;
1944 --------------------
1945 -- Remove_Pragmas --
1946 --------------------
1948 procedure Remove_Pragmas is
1953 Decl := First (Declarations (Body_To_Analyze));
1954 while Present (Decl) loop
1957 if Nkind (Decl) = N_Pragma
1958 and then Chars (Decl) = Name_Unreferenced
1967 --------------------------
1968 -- Uses_Secondary_Stack --
1969 --------------------------
1971 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1972 function Check_Call (N : Node_Id) return Traverse_Result;
1973 -- Look for function calls that return an unconstrained type
1979 function Check_Call (N : Node_Id) return Traverse_Result is
1981 if Nkind (N) = N_Function_Call
1982 and then Is_Entity_Name (Name (N))
1983 and then Is_Composite_Type (Etype (Entity (Name (N))))
1984 and then not Is_Constrained (Etype (Entity (Name (N))))
1987 ("cannot inline & (call returns unconstrained type)?",
1995 function Check_Calls is new Traverse_Func (Check_Call);
1998 return Check_Calls (Bod) = Abandon;
1999 end Uses_Secondary_Stack;
2001 -- Start of processing for Build_Body_To_Inline
2004 if Nkind (Decl) = N_Subprogram_Declaration
2005 and then Present (Body_To_Inline (Decl))
2007 return; -- Done already.
2009 -- Functions that return unconstrained composite types will require
2010 -- secondary stack handling, and cannot currently be inlined.
2011 -- Ditto for functions that return controlled types, where controlled
2012 -- actions interfere in complex ways with inlining.
2014 elsif Ekind (Subp) = E_Function
2015 and then not Is_Scalar_Type (Etype (Subp))
2016 and then not Is_Access_Type (Etype (Subp))
2017 and then not Is_Constrained (Etype (Subp))
2020 ("cannot inline & (unconstrained return type)?", N, Subp);
2023 elsif Ekind (Subp) = E_Function
2024 and then Controlled_Type (Etype (Subp))
2027 ("cannot inline & (controlled return type)?", N, Subp);
2031 if Present (Declarations (N))
2032 and then Has_Excluded_Declaration (Declarations (N))
2037 if Present (Handled_Statement_Sequence (N)) then
2038 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
2040 ("cannot inline& (exception handler)?",
2041 First (Exception_Handlers (Handled_Statement_Sequence (N))),
2045 Has_Excluded_Statement
2046 (Statements (Handled_Statement_Sequence (N)))
2052 -- We do not inline a subprogram that is too large, unless it is
2053 -- marked Inline_Always. This pragma does not suppress the other
2054 -- checks on inlining (forbidden declarations, handlers, etc).
2056 if Stat_Count > Max_Size
2057 and then not Is_Always_Inlined (Subp)
2059 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
2063 if Has_Pending_Instantiation then
2065 ("cannot inline& (forward instance within enclosing body)?",
2070 -- Within an instance, the body to inline must be treated as a nested
2071 -- generic, so that the proper global references are preserved.
2074 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2075 Original_Body := Copy_Generic_Node (N, Empty, True);
2077 Original_Body := Copy_Separate_Tree (N);
2080 -- We need to capture references to the formals in order to substitute
2081 -- the actuals at the point of inlining, i.e. instantiation. To treat
2082 -- the formals as globals to the body to inline, we nest it within
2083 -- a dummy parameterless subprogram, declared within the real one.
2084 -- To avoid generating an internal name (which is never public, and
2085 -- which affects serial numbers of other generated names), we use
2086 -- an internal symbol that cannot conflict with user declarations.
2088 Set_Parameter_Specifications (Specification (Original_Body), No_List);
2089 Set_Defining_Unit_Name
2090 (Specification (Original_Body),
2091 Make_Defining_Identifier (Sloc (N), Name_uParent));
2092 Set_Corresponding_Spec (Original_Body, Empty);
2094 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
2096 -- Set return type of function, which is also global and does not need
2099 if Ekind (Subp) = E_Function then
2100 Set_Subtype_Mark (Specification (Body_To_Analyze),
2101 New_Occurrence_Of (Etype (Subp), Sloc (N)));
2104 if No (Declarations (N)) then
2105 Set_Declarations (N, New_List (Body_To_Analyze));
2107 Append (Body_To_Analyze, Declarations (N));
2110 Expander_Mode_Save_And_Set (False);
2113 Analyze (Body_To_Analyze);
2114 New_Scope (Defining_Entity (Body_To_Analyze));
2115 Save_Global_References (Original_Body);
2117 Remove (Body_To_Analyze);
2119 Expander_Mode_Restore;
2125 -- If secondary stk used there is no point in inlining. We have
2126 -- already issued the warning in this case, so nothing to do.
2128 if Uses_Secondary_Stack (Body_To_Analyze) then
2132 Set_Body_To_Inline (Decl, Original_Body);
2133 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
2134 Set_Is_Inlined (Subp);
2135 end Build_Body_To_Inline;
2141 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
2143 -- Do not emit warning if this is a predefined unit which is not
2144 -- the main unit. With validity checks enabled, some predefined
2145 -- subprograms may contain nested subprograms and become ineligible
2148 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
2149 and then not In_Extended_Main_Source_Unit (Subp)
2153 elsif Is_Always_Inlined (Subp) then
2155 -- Remove last character (question mark) to make this into an error,
2156 -- because the Inline_Always pragma cannot be obeyed.
2158 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2160 elsif Ineffective_Inline_Warnings then
2161 Error_Msg_NE (Msg, N, Subp);
2165 -----------------------
2166 -- Check_Conformance --
2167 -----------------------
2169 procedure Check_Conformance
2170 (New_Id : Entity_Id;
2172 Ctype : Conformance_Type;
2174 Conforms : out Boolean;
2175 Err_Loc : Node_Id := Empty;
2176 Get_Inst : Boolean := False)
2178 Old_Type : constant Entity_Id := Etype (Old_Id);
2179 New_Type : constant Entity_Id := Etype (New_Id);
2180 Old_Formal : Entity_Id;
2181 New_Formal : Entity_Id;
2183 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2184 -- Post error message for conformance error on given node. Two messages
2185 -- are output. The first points to the previous declaration with a
2186 -- general "no conformance" message. The second is the detailed reason,
2187 -- supplied as Msg. The parameter N provide information for a possible
2188 -- & insertion in the message, and also provides the location for
2189 -- posting the message in the absence of a specified Err_Loc location.
2191 -----------------------
2192 -- Conformance_Error --
2193 -----------------------
2195 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2202 if No (Err_Loc) then
2208 Error_Msg_Sloc := Sloc (Old_Id);
2211 when Type_Conformant =>
2213 ("not type conformant with declaration#!", Enode);
2215 when Mode_Conformant =>
2217 ("not mode conformant with declaration#!", Enode);
2219 when Subtype_Conformant =>
2221 ("not subtype conformant with declaration#!", Enode);
2223 when Fully_Conformant =>
2225 ("not fully conformant with declaration#!", Enode);
2228 Error_Msg_NE (Msg, Enode, N);
2230 end Conformance_Error;
2232 -- Start of processing for Check_Conformance
2237 -- We need a special case for operators, since they don't appear
2240 if Ctype = Type_Conformant then
2241 if Ekind (New_Id) = E_Operator
2242 and then Operator_Matches_Spec (New_Id, Old_Id)
2248 -- If both are functions/operators, check return types conform
2250 if Old_Type /= Standard_Void_Type
2251 and then New_Type /= Standard_Void_Type
2253 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2254 Conformance_Error ("return type does not match!", New_Id);
2258 -- If either is a function/operator and the other isn't, error
2260 elsif Old_Type /= Standard_Void_Type
2261 or else New_Type /= Standard_Void_Type
2263 Conformance_Error ("functions can only match functions!", New_Id);
2267 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2268 -- If this is a renaming as body, refine error message to indicate that
2269 -- the conflict is with the original declaration. If the entity is not
2270 -- frozen, the conventions don't have to match, the one of the renamed
2271 -- entity is inherited.
2273 if Ctype >= Subtype_Conformant then
2274 if Convention (Old_Id) /= Convention (New_Id) then
2276 if not Is_Frozen (New_Id) then
2279 elsif Present (Err_Loc)
2280 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2281 and then Present (Corresponding_Spec (Err_Loc))
2283 Error_Msg_Name_1 := Chars (New_Id);
2285 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2287 Conformance_Error ("prior declaration for% has convention %!");
2290 Conformance_Error ("calling conventions do not match!");
2295 elsif Is_Formal_Subprogram (Old_Id)
2296 or else Is_Formal_Subprogram (New_Id)
2298 Conformance_Error ("formal subprograms not allowed!");
2303 -- Deal with parameters
2305 -- Note: we use the entity information, rather than going directly
2306 -- to the specification in the tree. This is not only simpler, but
2307 -- absolutely necessary for some cases of conformance tests between
2308 -- operators, where the declaration tree simply does not exist!
2310 Old_Formal := First_Formal (Old_Id);
2311 New_Formal := First_Formal (New_Id);
2313 while Present (Old_Formal) and then Present (New_Formal) loop
2314 if Ctype = Fully_Conformant then
2316 -- Names must match. Error message is more accurate if we do
2317 -- this before checking that the types of the formals match.
2319 if Chars (Old_Formal) /= Chars (New_Formal) then
2320 Conformance_Error ("name & does not match!", New_Formal);
2322 -- Set error posted flag on new formal as well to stop
2323 -- junk cascaded messages in some cases.
2325 Set_Error_Posted (New_Formal);
2330 -- Types must always match. In the visible part of an instance,
2331 -- usual overloading rules for dispatching operations apply, and
2332 -- we check base types (not the actual subtypes).
2334 if In_Instance_Visible_Part
2335 and then Is_Dispatching_Operation (New_Id)
2337 if not Conforming_Types
2338 (Base_Type (Etype (Old_Formal)),
2339 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2341 Conformance_Error ("type of & does not match!", New_Formal);
2345 elsif not Conforming_Types
2346 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2348 Conformance_Error ("type of & does not match!", New_Formal);
2352 -- For mode conformance, mode must match
2354 if Ctype >= Mode_Conformant
2355 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2357 Conformance_Error ("mode of & does not match!", New_Formal);
2361 -- Full conformance checks
2363 if Ctype = Fully_Conformant then
2365 -- We have checked already that names match. Check default
2366 -- expressions for in parameters
2368 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2370 NewD : constant Boolean :=
2371 Present (Default_Value (New_Formal));
2372 OldD : constant Boolean :=
2373 Present (Default_Value (Old_Formal));
2375 if NewD or OldD then
2377 -- The old default value has been analyzed because the
2378 -- current full declaration will have frozen everything
2379 -- before. The new default values have not been
2380 -- analyzed, so analyze them now before we check for
2385 Analyze_Per_Use_Expression
2386 (Default_Value (New_Formal), Etype (New_Formal));
2390 if not (NewD and OldD)
2391 or else not Fully_Conformant_Expressions
2392 (Default_Value (Old_Formal),
2393 Default_Value (New_Formal))
2396 ("default expression for & does not match!",
2405 -- A couple of special checks for Ada 83 mode. These checks are
2406 -- skipped if either entity is an operator in package Standard.
2407 -- or if either old or new instance is not from the source program.
2409 if Ada_Version = Ada_83
2410 and then Sloc (Old_Id) > Standard_Location
2411 and then Sloc (New_Id) > Standard_Location
2412 and then Comes_From_Source (Old_Id)
2413 and then Comes_From_Source (New_Id)
2416 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2417 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2420 -- Explicit IN must be present or absent in both cases. This
2421 -- test is required only in the full conformance case.
2423 if In_Present (Old_Param) /= In_Present (New_Param)
2424 and then Ctype = Fully_Conformant
2427 ("(Ada 83) IN must appear in both declarations",
2432 -- Grouping (use of comma in param lists) must be the same
2433 -- This is where we catch a misconformance like:
2436 -- A : Integer; B : Integer
2438 -- which are represented identically in the tree except
2439 -- for the setting of the flags More_Ids and Prev_Ids.
2441 if More_Ids (Old_Param) /= More_Ids (New_Param)
2442 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2445 ("grouping of & does not match!", New_Formal);
2451 Next_Formal (Old_Formal);
2452 Next_Formal (New_Formal);
2455 if Present (Old_Formal) then
2456 Conformance_Error ("too few parameters!");
2459 elsif Present (New_Formal) then
2460 Conformance_Error ("too many parameters!", New_Formal);
2463 end Check_Conformance;
2465 ------------------------------
2466 -- Check_Delayed_Subprogram --
2467 ------------------------------
2469 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2472 procedure Possible_Freeze (T : Entity_Id);
2473 -- T is the type of either a formal parameter or of the return type.
2474 -- If T is not yet frozen and needs a delayed freeze, then the
2475 -- subprogram itself must be delayed.
2477 ---------------------
2478 -- Possible_Freeze --
2479 ---------------------
2481 procedure Possible_Freeze (T : Entity_Id) is
2483 if Has_Delayed_Freeze (T)
2484 and then not Is_Frozen (T)
2486 Set_Has_Delayed_Freeze (Designator);
2488 elsif Is_Access_Type (T)
2489 and then Has_Delayed_Freeze (Designated_Type (T))
2490 and then not Is_Frozen (Designated_Type (T))
2492 Set_Has_Delayed_Freeze (Designator);
2494 end Possible_Freeze;
2496 -- Start of processing for Check_Delayed_Subprogram
2499 -- Never need to freeze abstract subprogram
2501 if Is_Abstract (Designator) then
2504 -- Need delayed freeze if return type itself needs a delayed
2505 -- freeze and is not yet frozen.
2507 Possible_Freeze (Etype (Designator));
2508 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2510 -- Need delayed freeze if any of the formal types themselves need
2511 -- a delayed freeze and are not yet frozen.
2513 F := First_Formal (Designator);
2514 while Present (F) loop
2515 Possible_Freeze (Etype (F));
2516 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2521 -- Mark functions that return by reference. Note that it cannot be
2522 -- done for delayed_freeze subprograms because the underlying
2523 -- returned type may not be known yet (for private types)
2525 if not Has_Delayed_Freeze (Designator)
2526 and then Expander_Active
2529 Typ : constant Entity_Id := Etype (Designator);
2530 Utyp : constant Entity_Id := Underlying_Type (Typ);
2533 if Is_Return_By_Reference_Type (Typ) then
2534 Set_Returns_By_Ref (Designator);
2536 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2537 Set_Returns_By_Ref (Designator);
2541 end Check_Delayed_Subprogram;
2543 ------------------------------------
2544 -- Check_Discriminant_Conformance --
2545 ------------------------------------
2547 procedure Check_Discriminant_Conformance
2552 Old_Discr : Entity_Id := First_Discriminant (Prev);
2553 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2554 New_Discr_Id : Entity_Id;
2555 New_Discr_Type : Entity_Id;
2557 procedure Conformance_Error (Msg : String; N : Node_Id);
2558 -- Post error message for conformance error on given node. Two messages
2559 -- are output. The first points to the previous declaration with a
2560 -- general "no conformance" message. The second is the detailed reason,
2561 -- supplied as Msg. The parameter N provide information for a possible
2562 -- & insertion in the message.
2564 -----------------------
2565 -- Conformance_Error --
2566 -----------------------
2568 procedure Conformance_Error (Msg : String; N : Node_Id) is
2570 Error_Msg_Sloc := Sloc (Prev_Loc);
2571 Error_Msg_N ("not fully conformant with declaration#!", N);
2572 Error_Msg_NE (Msg, N, N);
2573 end Conformance_Error;
2575 -- Start of processing for Check_Discriminant_Conformance
2578 while Present (Old_Discr) and then Present (New_Discr) loop
2580 New_Discr_Id := Defining_Identifier (New_Discr);
2582 -- The subtype mark of the discriminant on the full type has not
2583 -- been analyzed so we do it here. For an access discriminant a new
2586 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2588 Access_Definition (N, Discriminant_Type (New_Discr));
2591 Analyze (Discriminant_Type (New_Discr));
2592 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2595 if not Conforming_Types
2596 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2598 Conformance_Error ("type of & does not match!", New_Discr_Id);
2601 -- Treat the new discriminant as an occurrence of the old one,
2602 -- for navigation purposes, and fill in some semantic
2603 -- information, for completeness.
2605 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2606 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2607 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2612 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2613 Conformance_Error ("name & does not match!", New_Discr_Id);
2617 -- Default expressions must match
2620 NewD : constant Boolean :=
2621 Present (Expression (New_Discr));
2622 OldD : constant Boolean :=
2623 Present (Expression (Parent (Old_Discr)));
2626 if NewD or OldD then
2628 -- The old default value has been analyzed and expanded,
2629 -- because the current full declaration will have frozen
2630 -- everything before. The new default values have not been
2631 -- expanded, so expand now to check conformance.
2634 Analyze_Per_Use_Expression
2635 (Expression (New_Discr), New_Discr_Type);
2638 if not (NewD and OldD)
2639 or else not Fully_Conformant_Expressions
2640 (Expression (Parent (Old_Discr)),
2641 Expression (New_Discr))
2645 ("default expression for & does not match!",
2652 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2654 if Ada_Version = Ada_83 then
2656 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2659 -- Grouping (use of comma in param lists) must be the same
2660 -- This is where we catch a misconformance like:
2663 -- A : Integer; B : Integer
2665 -- which are represented identically in the tree except
2666 -- for the setting of the flags More_Ids and Prev_Ids.
2668 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2669 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2672 ("grouping of & does not match!", New_Discr_Id);
2678 Next_Discriminant (Old_Discr);
2682 if Present (Old_Discr) then
2683 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2686 elsif Present (New_Discr) then
2688 ("too many discriminants!", Defining_Identifier (New_Discr));
2691 end Check_Discriminant_Conformance;
2693 ----------------------------
2694 -- Check_Fully_Conformant --
2695 ----------------------------
2697 procedure Check_Fully_Conformant
2698 (New_Id : Entity_Id;
2700 Err_Loc : Node_Id := Empty)
2705 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2706 end Check_Fully_Conformant;
2708 ---------------------------
2709 -- Check_Mode_Conformant --
2710 ---------------------------
2712 procedure Check_Mode_Conformant
2713 (New_Id : Entity_Id;
2715 Err_Loc : Node_Id := Empty;
2716 Get_Inst : Boolean := False)
2722 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2723 end Check_Mode_Conformant;
2725 --------------------------------
2726 -- Check_Overriding_Indicator --
2727 --------------------------------
2729 procedure Check_Overriding_Indicator
2731 Does_Override : Boolean)
2737 if Ekind (Subp) = E_Enumeration_Literal then
2739 -- No overriding indicator for literals
2744 Decl := Unit_Declaration_Node (Subp);
2747 if Nkind (Decl) = N_Subprogram_Declaration
2748 or else Nkind (Decl) = N_Subprogram_Body
2749 or else Nkind (Decl) = N_Subprogram_Renaming_Declaration
2750 or else Nkind (Decl) = N_Subprogram_Body_Stub
2752 Spec := Specification (Decl);
2757 if not Does_Override then
2758 if Must_Override (Spec) then
2759 Error_Msg_NE ("subprogram& is not overriding", Spec, Subp);
2763 if Must_Not_Override (Spec) then
2765 ("subprogram& overrides inherited operation", Spec, Subp);
2768 end Check_Overriding_Indicator;
2774 procedure Check_Returns
2781 procedure Check_Statement_Sequence (L : List_Id);
2782 -- Internal recursive procedure to check a list of statements for proper
2783 -- termination by a return statement (or a transfer of control or a
2784 -- compound statement that is itself internally properly terminated).
2786 ------------------------------
2787 -- Check_Statement_Sequence --
2788 ------------------------------
2790 procedure Check_Statement_Sequence (L : List_Id) is
2794 Raise_Exception_Call : Boolean;
2795 -- Set True if statement sequence terminated by Raise_Exception call
2796 -- or a Reraise_Occurrence call.
2799 Raise_Exception_Call := False;
2801 -- Get last real statement
2803 Last_Stm := Last (L);
2805 -- Don't count pragmas
2807 while Nkind (Last_Stm) = N_Pragma
2809 -- Don't count call to SS_Release (can happen after Raise_Exception)
2812 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2814 Nkind (Name (Last_Stm)) = N_Identifier
2816 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2818 -- Don't count exception junk
2821 ((Nkind (Last_Stm) = N_Goto_Statement
2822 or else Nkind (Last_Stm) = N_Label
2823 or else Nkind (Last_Stm) = N_Object_Declaration)
2824 and then Exception_Junk (Last_Stm))
2829 -- Here we have the "real" last statement
2831 Kind := Nkind (Last_Stm);
2833 -- Transfer of control, OK. Note that in the No_Return procedure
2834 -- case, we already diagnosed any explicit return statements, so
2835 -- we can treat them as OK in this context.
2837 if Is_Transfer (Last_Stm) then
2840 -- Check cases of explicit non-indirect procedure calls
2842 elsif Kind = N_Procedure_Call_Statement
2843 and then Is_Entity_Name (Name (Last_Stm))
2845 -- Check call to Raise_Exception procedure which is treated
2846 -- specially, as is a call to Reraise_Occurrence.
2848 -- We suppress the warning in these cases since it is likely that
2849 -- the programmer really does not expect to deal with the case
2850 -- of Null_Occurrence, and thus would find a warning about a
2851 -- missing return curious, and raising Program_Error does not
2852 -- seem such a bad behavior if this does occur.
2854 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2856 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2858 Raise_Exception_Call := True;
2860 -- For Raise_Exception call, test first argument, if it is
2861 -- an attribute reference for a 'Identity call, then we know
2862 -- that the call cannot possibly return.
2865 Arg : constant Node_Id :=
2866 Original_Node (First_Actual (Last_Stm));
2869 if Nkind (Arg) = N_Attribute_Reference
2870 and then Attribute_Name (Arg) = Name_Identity
2877 -- If statement, need to look inside if there is an else and check
2878 -- each constituent statement sequence for proper termination.
2880 elsif Kind = N_If_Statement
2881 and then Present (Else_Statements (Last_Stm))
2883 Check_Statement_Sequence (Then_Statements (Last_Stm));
2884 Check_Statement_Sequence (Else_Statements (Last_Stm));
2886 if Present (Elsif_Parts (Last_Stm)) then
2888 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2891 while Present (Elsif_Part) loop
2892 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2900 -- Case statement, check each case for proper termination
2902 elsif Kind = N_Case_Statement then
2907 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2908 while Present (Case_Alt) loop
2909 Check_Statement_Sequence (Statements (Case_Alt));
2910 Next_Non_Pragma (Case_Alt);
2916 -- Block statement, check its handled sequence of statements
2918 elsif Kind = N_Block_Statement then
2924 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2933 -- Loop statement. If there is an iteration scheme, we can definitely
2934 -- fall out of the loop. Similarly if there is an exit statement, we
2935 -- can fall out. In either case we need a following return.
2937 elsif Kind = N_Loop_Statement then
2938 if Present (Iteration_Scheme (Last_Stm))
2939 or else Has_Exit (Entity (Identifier (Last_Stm)))
2943 -- A loop with no exit statement or iteration scheme if either
2944 -- an inifite loop, or it has some other exit (raise/return).
2945 -- In either case, no warning is required.
2951 -- Timed entry call, check entry call and delay alternatives
2953 -- Note: in expanded code, the timed entry call has been converted
2954 -- to a set of expanded statements on which the check will work
2955 -- correctly in any case.
2957 elsif Kind = N_Timed_Entry_Call then
2959 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2960 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2963 -- If statement sequence of entry call alternative is missing,
2964 -- then we can definitely fall through, and we post the error
2965 -- message on the entry call alternative itself.
2967 if No (Statements (ECA)) then
2970 -- If statement sequence of delay alternative is missing, then
2971 -- we can definitely fall through, and we post the error
2972 -- message on the delay alternative itself.
2974 -- Note: if both ECA and DCA are missing the return, then we
2975 -- post only one message, should be enough to fix the bugs.
2976 -- If not we will get a message next time on the DCA when the
2979 elsif No (Statements (DCA)) then
2982 -- Else check both statement sequences
2985 Check_Statement_Sequence (Statements (ECA));
2986 Check_Statement_Sequence (Statements (DCA));
2991 -- Conditional entry call, check entry call and else part
2993 -- Note: in expanded code, the conditional entry call has been
2994 -- converted to a set of expanded statements on which the check
2995 -- will work correctly in any case.
2997 elsif Kind = N_Conditional_Entry_Call then
2999 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3002 -- If statement sequence of entry call alternative is missing,
3003 -- then we can definitely fall through, and we post the error
3004 -- message on the entry call alternative itself.
3006 if No (Statements (ECA)) then
3009 -- Else check statement sequence and else part
3012 Check_Statement_Sequence (Statements (ECA));
3013 Check_Statement_Sequence (Else_Statements (Last_Stm));
3019 -- If we fall through, issue appropriate message
3023 if not Raise_Exception_Call then
3025 ("?RETURN statement missing following this statement!",
3028 ("\?Program_Error may be raised at run time",
3032 -- Note: we set Err even though we have not issued a warning
3033 -- because we still have a case of a missing return. This is
3034 -- an extremely marginal case, probably will never be noticed
3035 -- but we might as well get it right.
3041 ("implied return after this statement not allowed (No_Return)",
3044 end Check_Statement_Sequence;
3046 -- Start of processing for Check_Returns
3050 Check_Statement_Sequence (Statements (HSS));
3052 if Present (Exception_Handlers (HSS)) then
3053 Handler := First_Non_Pragma (Exception_Handlers (HSS));
3054 while Present (Handler) loop
3055 Check_Statement_Sequence (Statements (Handler));
3056 Next_Non_Pragma (Handler);
3061 ----------------------------
3062 -- Check_Subprogram_Order --
3063 ----------------------------
3065 procedure Check_Subprogram_Order (N : Node_Id) is
3067 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
3068 -- This is used to check if S1 > S2 in the sense required by this
3069 -- test, for example nameab < namec, but name2 < name10.
3071 -----------------------------
3072 -- Subprogram_Name_Greater --
3073 -----------------------------
3075 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
3080 -- Remove trailing numeric parts
3083 while S1 (L1) in '0' .. '9' loop
3088 while S2 (L2) in '0' .. '9' loop
3092 -- If non-numeric parts non-equal, that's decisive
3094 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
3097 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3100 -- If non-numeric parts equal, compare suffixed numeric parts. Note
3101 -- that a missing suffix is treated as numeric zero in this test.
3105 while L1 < S1'Last loop
3107 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3111 while L2 < S2'Last loop
3113 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3118 end Subprogram_Name_Greater;
3120 -- Start of processing for Check_Subprogram_Order
3123 -- Check body in alpha order if this is option
3126 and then Style_Check_Order_Subprograms
3127 and then Nkind (N) = N_Subprogram_Body
3128 and then Comes_From_Source (N)
3129 and then In_Extended_Main_Source_Unit (N)
3133 renames Scope_Stack.Table
3134 (Scope_Stack.Last).Last_Subprogram_Name;
3136 Body_Id : constant Entity_Id :=
3137 Defining_Entity (Specification (N));
3140 Get_Decoded_Name_String (Chars (Body_Id));
3143 if Subprogram_Name_Greater
3144 (LSN.all, Name_Buffer (1 .. Name_Len))
3146 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3152 LSN := new String'(Name_Buffer (1 .. Name_Len));
3155 end Check_Subprogram_Order;
3157 ------------------------------
3158 -- Check_Subtype_Conformant --
3159 ------------------------------
3161 procedure Check_Subtype_Conformant
3162 (New_Id : Entity_Id;
3164 Err_Loc : Node_Id := Empty)
3169 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3170 end Check_Subtype_Conformant;
3172 ---------------------------
3173 -- Check_Type_Conformant --
3174 ---------------------------
3176 procedure Check_Type_Conformant
3177 (New_Id : Entity_Id;
3179 Err_Loc : Node_Id := Empty)
3184 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3185 end Check_Type_Conformant;
3187 ----------------------
3188 -- Conforming_Types --
3189 ----------------------
3191 function Conforming_Types
3194 Ctype : Conformance_Type;
3195 Get_Inst : Boolean := False) return Boolean
3197 Type_1 : Entity_Id := T1;
3198 Type_2 : Entity_Id := T2;
3199 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3201 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3202 -- If neither T1 nor T2 are generic actual types, or if they are
3203 -- in different scopes (e.g. parent and child instances), then verify
3204 -- that the base types are equal. Otherwise T1 and T2 must be
3205 -- on the same subtype chain. The whole purpose of this procedure
3206 -- is to prevent spurious ambiguities in an instantiation that may
3207 -- arise if two distinct generic types are instantiated with the
3210 ----------------------
3211 -- Base_Types_Match --
3212 ----------------------
3214 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3219 elsif Base_Type (T1) = Base_Type (T2) then
3221 -- The following is too permissive. A more precise test must
3222 -- check that the generic actual is an ancestor subtype of the
3225 return not Is_Generic_Actual_Type (T1)
3226 or else not Is_Generic_Actual_Type (T2)
3227 or else Scope (T1) /= Scope (T2);
3229 -- In some cases a type imported through a limited_with clause,
3230 -- and its non-limited view are both visible, for example in an
3231 -- anonymous access_to_classwide type in a formal. Both entities
3232 -- designate the same type.
3234 elsif From_With_Type (T1)
3235 and then Ekind (T1) = E_Incomplete_Type
3236 and then T2 = Non_Limited_View (T1)
3243 end Base_Types_Match;
3245 -- Start of processing for Conforming_Types
3248 -- The context is an instance association for a formal
3249 -- access-to-subprogram type; the formal parameter types require
3250 -- mapping because they may denote other formal parameters of the
3254 Type_1 := Get_Instance_Of (T1);
3255 Type_2 := Get_Instance_Of (T2);
3258 -- First see if base types match
3260 if Base_Types_Match (Type_1, Type_2) then
3261 return Ctype <= Mode_Conformant
3262 or else Subtypes_Statically_Match (Type_1, Type_2);
3264 elsif Is_Incomplete_Or_Private_Type (Type_1)
3265 and then Present (Full_View (Type_1))
3266 and then Base_Types_Match (Full_View (Type_1), Type_2)
3268 return Ctype <= Mode_Conformant
3269 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3271 elsif Ekind (Type_2) = E_Incomplete_Type
3272 and then Present (Full_View (Type_2))
3273 and then Base_Types_Match (Type_1, Full_View (Type_2))
3275 return Ctype <= Mode_Conformant
3276 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3278 elsif Is_Private_Type (Type_2)
3279 and then In_Instance
3280 and then Present (Full_View (Type_2))
3281 and then Base_Types_Match (Type_1, Full_View (Type_2))
3283 return Ctype <= Mode_Conformant
3284 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3287 -- Ada 2005 (AI-254): Anonymous access to subprogram types must be
3288 -- treated recursively because they carry a signature.
3290 Are_Anonymous_Access_To_Subprogram_Types :=
3292 -- Case 1: Anonymous access to subprogram types
3294 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3295 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3297 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3298 -- case the anonymous type_declaration has been replaced by an
3299 -- occurrence of an internal access to subprogram type declaration
3300 -- available through the Original_Access_Type attribute
3303 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3304 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3305 and then not Comes_From_Source (Type_1)
3306 and then not Comes_From_Source (Type_2)
3307 and then Present (Original_Access_Type (Type_1))
3308 and then Present (Original_Access_Type (Type_2))
3309 and then Ekind (Original_Access_Type (Type_1)) =
3310 E_Anonymous_Access_Protected_Subprogram_Type
3311 and then Ekind (Original_Access_Type (Type_2)) =
3312 E_Anonymous_Access_Protected_Subprogram_Type);
3314 -- Test anonymous access type case. For this case, static subtype
3315 -- matching is required for mode conformance (RM 6.3.1(15))
3317 if (Ekind (Type_1) = E_Anonymous_Access_Type
3318 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3319 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3322 Desig_1 : Entity_Id;
3323 Desig_2 : Entity_Id;
3326 Desig_1 := Directly_Designated_Type (Type_1);
3328 -- An access parameter can designate an incomplete type
3330 if Ekind (Desig_1) = E_Incomplete_Type
3331 and then Present (Full_View (Desig_1))
3333 Desig_1 := Full_View (Desig_1);
3336 Desig_2 := Directly_Designated_Type (Type_2);
3338 if Ekind (Desig_2) = E_Incomplete_Type
3339 and then Present (Full_View (Desig_2))
3341 Desig_2 := Full_View (Desig_2);
3344 -- The context is an instance association for a formal
3345 -- access-to-subprogram type; formal access parameter designated
3346 -- types require mapping because they may denote other formal
3347 -- parameters of the generic unit.
3350 Desig_1 := Get_Instance_Of (Desig_1);
3351 Desig_2 := Get_Instance_Of (Desig_2);
3354 -- It is possible for a Class_Wide_Type to be introduced for an
3355 -- incomplete type, in which case there is a separate class_ wide
3356 -- type for the full view. The types conform if their Etypes
3357 -- conform, i.e. one may be the full view of the other. This can
3358 -- only happen in the context of an access parameter, other uses
3359 -- of an incomplete Class_Wide_Type are illegal.
3361 if Is_Class_Wide_Type (Desig_1)
3362 and then Is_Class_Wide_Type (Desig_2)
3366 (Etype (Base_Type (Desig_1)),
3367 Etype (Base_Type (Desig_2)), Ctype);
3369 elsif Are_Anonymous_Access_To_Subprogram_Types then
3370 if Ada_Version < Ada_05 then
3371 return Ctype = Type_Conformant
3373 Subtypes_Statically_Match (Desig_1, Desig_2);
3375 -- We must check the conformance of the signatures themselves
3379 Conformant : Boolean;
3382 (Desig_1, Desig_2, Ctype, False, Conformant);
3388 return Base_Type (Desig_1) = Base_Type (Desig_2)
3389 and then (Ctype = Type_Conformant
3391 Subtypes_Statically_Match (Desig_1, Desig_2));
3395 -- Otherwise definitely no match
3400 end Conforming_Types;
3402 --------------------------
3403 -- Create_Extra_Formals --
3404 --------------------------
3406 procedure Create_Extra_Formals (E : Entity_Id) is
3408 Last_Extra : Entity_Id;
3409 Formal_Type : Entity_Id;
3410 P_Formal : Entity_Id := Empty;
3412 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3413 -- Add an extra formal, associated with the current Formal. The extra
3414 -- formal is added to the list of extra formals, and also returned as
3415 -- the result. These formals are always of mode IN.
3417 ----------------------
3418 -- Add_Extra_Formal --
3419 ----------------------
3421 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3422 EF : constant Entity_Id :=
3423 Make_Defining_Identifier (Sloc (Formal),
3424 Chars => New_External_Name (Chars (Formal), 'F'));
3427 -- We never generate extra formals if expansion is not active
3428 -- because we don't need them unless we are generating code.
3430 if not Expander_Active then
3434 -- A little optimization. Never generate an extra formal for the
3435 -- _init operand of an initialization procedure, since it could
3438 if Chars (Formal) = Name_uInit then
3442 Set_Ekind (EF, E_In_Parameter);
3443 Set_Actual_Subtype (EF, Typ);
3444 Set_Etype (EF, Typ);
3445 Set_Scope (EF, Scope (Formal));
3446 Set_Mechanism (EF, Default_Mechanism);
3447 Set_Formal_Validity (EF);
3449 Set_Extra_Formal (Last_Extra, EF);
3452 end Add_Extra_Formal;
3454 -- Start of processing for Create_Extra_Formals
3457 -- If this is a derived subprogram then the subtypes of the parent
3458 -- subprogram's formal parameters will be used to to determine the need
3459 -- for extra formals.
3461 if Is_Overloadable (E) and then Present (Alias (E)) then
3462 P_Formal := First_Formal (Alias (E));
3465 Last_Extra := Empty;
3466 Formal := First_Formal (E);
3467 while Present (Formal) loop
3468 Last_Extra := Formal;
3469 Next_Formal (Formal);
3472 -- If Extra_formals where already created, don't do it again. This
3473 -- situation may arise for subprogram types created as part of
3474 -- dispatching calls (see Expand_Dispatching_Call)
3476 if Present (Last_Extra) and then
3477 Present (Extra_Formal (Last_Extra))
3482 Formal := First_Formal (E);
3484 while Present (Formal) loop
3486 -- Create extra formal for supporting the attribute 'Constrained.
3487 -- The case of a private type view without discriminants also
3488 -- requires the extra formal if the underlying type has defaulted
3491 if Ekind (Formal) /= E_In_Parameter then
3492 if Present (P_Formal) then
3493 Formal_Type := Etype (P_Formal);
3495 Formal_Type := Etype (Formal);
3498 -- Do not produce extra formals for Unchecked_Union parameters.
3499 -- Jump directly to the end of the loop.
3501 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3502 goto Skip_Extra_Formal_Generation;
3505 if not Has_Discriminants (Formal_Type)
3506 and then Ekind (Formal_Type) in Private_Kind
3507 and then Present (Underlying_Type (Formal_Type))
3509 Formal_Type := Underlying_Type (Formal_Type);
3512 if Has_Discriminants (Formal_Type)
3514 ((not Is_Constrained (Formal_Type)
3515 and then not Is_Indefinite_Subtype (Formal_Type))
3516 or else Present (Extra_Formal (Formal)))
3518 Set_Extra_Constrained
3519 (Formal, Add_Extra_Formal (Standard_Boolean));
3523 -- Create extra formal for supporting accessibility checking
3525 -- This is suppressed if we specifically suppress accessibility
3526 -- checks at the pacage level for either the subprogram, or the
3527 -- package in which it resides. However, we do not suppress it
3528 -- simply if the scope has accessibility checks suppressed, since
3529 -- this could cause trouble when clients are compiled with a
3530 -- different suppression setting. The explicit checks at the
3531 -- package level are safe from this point of view.
3533 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3535 (Explicit_Suppress (E, Accessibility_Check)
3537 Explicit_Suppress (Scope (E), Accessibility_Check))
3539 (not Present (P_Formal)
3540 or else Present (Extra_Accessibility (P_Formal)))
3542 -- Temporary kludge: for now we avoid creating the extra formal
3543 -- for access parameters of protected operations because of
3544 -- problem with the case of internal protected calls. ???
3546 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3547 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3549 Set_Extra_Accessibility
3550 (Formal, Add_Extra_Formal (Standard_Natural));
3554 if Present (P_Formal) then
3555 Next_Formal (P_Formal);
3558 -- This label is required when skipping extra formal generation for
3559 -- Unchecked_Union parameters.
3561 <<Skip_Extra_Formal_Generation>>
3563 Next_Formal (Formal);
3565 end Create_Extra_Formals;
3567 -----------------------------
3568 -- Enter_Overloaded_Entity --
3569 -----------------------------
3571 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3572 E : Entity_Id := Current_Entity_In_Scope (S);
3573 C_E : Entity_Id := Current_Entity (S);
3577 Set_Has_Homonym (E);
3578 Set_Has_Homonym (S);
3581 Set_Is_Immediately_Visible (S);
3582 Set_Scope (S, Current_Scope);
3584 -- Chain new entity if front of homonym in current scope, so that
3585 -- homonyms are contiguous.
3590 while Homonym (C_E) /= E loop
3591 C_E := Homonym (C_E);
3594 Set_Homonym (C_E, S);
3598 Set_Current_Entity (S);
3603 Append_Entity (S, Current_Scope);
3604 Set_Public_Status (S);
3606 if Debug_Flag_E then
3607 Write_Str ("New overloaded entity chain: ");
3608 Write_Name (Chars (S));
3611 while Present (E) loop
3612 Write_Str (" "); Write_Int (Int (E));
3619 -- Generate warning for hiding
3622 and then Comes_From_Source (S)
3623 and then In_Extended_Main_Source_Unit (S)
3630 -- Warn unless genuine overloading
3632 if (not Is_Overloadable (E))
3633 or else Subtype_Conformant (E, S)
3635 Error_Msg_Sloc := Sloc (E);
3636 Error_Msg_N ("declaration of & hides one#?", S);
3640 end Enter_Overloaded_Entity;
3642 -----------------------------
3643 -- Find_Corresponding_Spec --
3644 -----------------------------
3646 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3647 Spec : constant Node_Id := Specification (N);
3648 Designator : constant Entity_Id := Defining_Entity (Spec);
3653 E := Current_Entity (Designator);
3655 while Present (E) loop
3657 -- We are looking for a matching spec. It must have the same scope,
3658 -- and the same name, and either be type conformant, or be the case
3659 -- of a library procedure spec and its body (which belong to one
3660 -- another regardless of whether they are type conformant or not).
3662 if Scope (E) = Current_Scope then
3663 if Current_Scope = Standard_Standard
3664 or else (Ekind (E) = Ekind (Designator)
3665 and then Type_Conformant (E, Designator))
3667 -- Within an instantiation, we know that spec and body are
3668 -- subtype conformant, because they were subtype conformant
3669 -- in the generic. We choose the subtype-conformant entity
3670 -- here as well, to resolve spurious ambiguities in the
3671 -- instance that were not present in the generic (i.e. when
3672 -- two different types are given the same actual). If we are
3673 -- looking for a spec to match a body, full conformance is
3677 Set_Convention (Designator, Convention (E));
3679 if Nkind (N) = N_Subprogram_Body
3680 and then Present (Homonym (E))
3681 and then not Fully_Conformant (E, Designator)
3685 elsif not Subtype_Conformant (E, Designator) then
3690 if not Has_Completion (E) then
3692 if Nkind (N) /= N_Subprogram_Body_Stub then
3693 Set_Corresponding_Spec (N, E);
3696 Set_Has_Completion (E);
3699 elsif Nkind (Parent (N)) = N_Subunit then
3701 -- If this is the proper body of a subunit, the completion
3702 -- flag is set when analyzing the stub.
3706 -- If body already exists, this is an error unless the
3707 -- previous declaration is the implicit declaration of
3708 -- a derived subprogram, or this is a spurious overloading
3711 elsif No (Alias (E))
3712 and then not Is_Intrinsic_Subprogram (E)
3713 and then not In_Instance
3715 Error_Msg_Sloc := Sloc (E);
3716 if Is_Imported (E) then
3718 ("body not allowed for imported subprogram & declared#",
3721 Error_Msg_NE ("duplicate body for & declared#", N, E);
3725 elsif Is_Child_Unit (E)
3727 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3729 Nkind (Parent (Unit_Declaration_Node (Designator)))
3730 = N_Compilation_Unit
3733 -- Child units cannot be overloaded, so a conformance mismatch
3734 -- between body and a previous spec is an error.
3737 ("body of child unit does not match previous declaration", N);
3745 -- On exit, we know that no previous declaration of subprogram exists
3748 end Find_Corresponding_Spec;
3750 ----------------------
3751 -- Fully_Conformant --
3752 ----------------------
3754 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3757 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3759 end Fully_Conformant;
3761 ----------------------------------
3762 -- Fully_Conformant_Expressions --
3763 ----------------------------------
3765 function Fully_Conformant_Expressions
3766 (Given_E1 : Node_Id;
3767 Given_E2 : Node_Id) return Boolean
3769 E1 : constant Node_Id := Original_Node (Given_E1);
3770 E2 : constant Node_Id := Original_Node (Given_E2);
3771 -- We always test conformance on original nodes, since it is possible
3772 -- for analysis and/or expansion to make things look as though they
3773 -- conform when they do not, e.g. by converting 1+2 into 3.
3775 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3776 renames Fully_Conformant_Expressions;
3778 function FCL (L1, L2 : List_Id) return Boolean;
3779 -- Compare elements of two lists for conformance. Elements have to
3780 -- be conformant, and actuals inserted as default parameters do not
3781 -- match explicit actuals with the same value.
3783 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3784 -- Compare an operator node with a function call
3790 function FCL (L1, L2 : List_Id) return Boolean is
3794 if L1 = No_List then
3800 if L2 = No_List then
3806 -- Compare two lists, skipping rewrite insertions (we want to
3807 -- compare the original trees, not the expanded versions!)
3810 if Is_Rewrite_Insertion (N1) then
3812 elsif Is_Rewrite_Insertion (N2) then
3818 elsif not FCE (N1, N2) then
3831 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3832 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3837 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3842 Act := First (Actuals);
3844 if Nkind (Op_Node) in N_Binary_Op then
3846 if not FCE (Left_Opnd (Op_Node), Act) then
3853 return Present (Act)
3854 and then FCE (Right_Opnd (Op_Node), Act)
3855 and then No (Next (Act));
3859 -- Start of processing for Fully_Conformant_Expressions
3862 -- Non-conformant if paren count does not match. Note: if some idiot
3863 -- complains that we don't do this right for more than 3 levels of
3864 -- parentheses, they will be treated with the respect they deserve :-)
3866 if Paren_Count (E1) /= Paren_Count (E2) then
3869 -- If same entities are referenced, then they are conformant even if
3870 -- they have different forms (RM 8.3.1(19-20)).
3872 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3873 if Present (Entity (E1)) then
3874 return Entity (E1) = Entity (E2)
3875 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3876 and then Ekind (Entity (E1)) = E_Discriminant
3877 and then Ekind (Entity (E2)) = E_In_Parameter);
3879 elsif Nkind (E1) = N_Expanded_Name
3880 and then Nkind (E2) = N_Expanded_Name
3881 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3882 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3884 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3887 -- Identifiers in component associations don't always have
3888 -- entities, but their names must conform.
3890 return Nkind (E1) = N_Identifier
3891 and then Nkind (E2) = N_Identifier
3892 and then Chars (E1) = Chars (E2);
3895 elsif Nkind (E1) = N_Character_Literal
3896 and then Nkind (E2) = N_Expanded_Name
3898 return Nkind (Selector_Name (E2)) = N_Character_Literal
3899 and then Chars (E1) = Chars (Selector_Name (E2));
3901 elsif Nkind (E2) = N_Character_Literal
3902 and then Nkind (E1) = N_Expanded_Name
3904 return Nkind (Selector_Name (E1)) = N_Character_Literal
3905 and then Chars (E2) = Chars (Selector_Name (E1));
3907 elsif Nkind (E1) in N_Op
3908 and then Nkind (E2) = N_Function_Call
3910 return FCO (E1, E2);
3912 elsif Nkind (E2) in N_Op
3913 and then Nkind (E1) = N_Function_Call
3915 return FCO (E2, E1);
3917 -- Otherwise we must have the same syntactic entity
3919 elsif Nkind (E1) /= Nkind (E2) then
3922 -- At this point, we specialize by node type
3929 FCL (Expressions (E1), Expressions (E2))
3930 and then FCL (Component_Associations (E1),
3931 Component_Associations (E2));
3934 if Nkind (Expression (E1)) = N_Qualified_Expression
3936 Nkind (Expression (E2)) = N_Qualified_Expression
3938 return FCE (Expression (E1), Expression (E2));
3940 -- Check that the subtype marks and any constraints
3945 Indic1 : constant Node_Id := Expression (E1);
3946 Indic2 : constant Node_Id := Expression (E2);
3951 if Nkind (Indic1) /= N_Subtype_Indication then
3953 Nkind (Indic2) /= N_Subtype_Indication
3954 and then Entity (Indic1) = Entity (Indic2);
3956 elsif Nkind (Indic2) /= N_Subtype_Indication then
3958 Nkind (Indic1) /= N_Subtype_Indication
3959 and then Entity (Indic1) = Entity (Indic2);
3962 if Entity (Subtype_Mark (Indic1)) /=
3963 Entity (Subtype_Mark (Indic2))
3968 Elt1 := First (Constraints (Constraint (Indic1)));
3969 Elt2 := First (Constraints (Constraint (Indic2)));
3971 while Present (Elt1) and then Present (Elt2) loop
3972 if not FCE (Elt1, Elt2) then
3985 when N_Attribute_Reference =>
3987 Attribute_Name (E1) = Attribute_Name (E2)
3988 and then FCL (Expressions (E1), Expressions (E2));
3992 Entity (E1) = Entity (E2)
3993 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3994 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3996 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3998 FCE (Left_Opnd (E1), Left_Opnd (E2))
4000 FCE (Right_Opnd (E1), Right_Opnd (E2));
4002 when N_Character_Literal =>
4004 Char_Literal_Value (E1) = Char_Literal_Value (E2);
4006 when N_Component_Association =>
4008 FCL (Choices (E1), Choices (E2))
4009 and then FCE (Expression (E1), Expression (E2));
4011 when N_Conditional_Expression =>
4013 FCL (Expressions (E1), Expressions (E2));
4015 when N_Explicit_Dereference =>
4017 FCE (Prefix (E1), Prefix (E2));
4019 when N_Extension_Aggregate =>
4021 FCL (Expressions (E1), Expressions (E2))
4022 and then Null_Record_Present (E1) =
4023 Null_Record_Present (E2)
4024 and then FCL (Component_Associations (E1),
4025 Component_Associations (E2));
4027 when N_Function_Call =>
4029 FCE (Name (E1), Name (E2))
4030 and then FCL (Parameter_Associations (E1),
4031 Parameter_Associations (E2));
4033 when N_Indexed_Component =>
4035 FCE (Prefix (E1), Prefix (E2))
4036 and then FCL (Expressions (E1), Expressions (E2));
4038 when N_Integer_Literal =>
4039 return (Intval (E1) = Intval (E2));
4044 when N_Operator_Symbol =>
4046 Chars (E1) = Chars (E2);
4048 when N_Others_Choice =>
4051 when N_Parameter_Association =>
4053 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
4054 and then FCE (Explicit_Actual_Parameter (E1),
4055 Explicit_Actual_Parameter (E2));
4057 when N_Qualified_Expression =>
4059 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4060 and then FCE (Expression (E1), Expression (E2));
4064 FCE (Low_Bound (E1), Low_Bound (E2))
4065 and then FCE (High_Bound (E1), High_Bound (E2));
4067 when N_Real_Literal =>
4068 return (Realval (E1) = Realval (E2));
4070 when N_Selected_Component =>
4072 FCE (Prefix (E1), Prefix (E2))
4073 and then FCE (Selector_Name (E1), Selector_Name (E2));
4077 FCE (Prefix (E1), Prefix (E2))
4078 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
4080 when N_String_Literal =>
4082 S1 : constant String_Id := Strval (E1);
4083 S2 : constant String_Id := Strval (E2);
4084 L1 : constant Nat := String_Length (S1);
4085 L2 : constant Nat := String_Length (S2);
4092 for J in 1 .. L1 loop
4093 if Get_String_Char (S1, J) /=
4094 Get_String_Char (S2, J)
4104 when N_Type_Conversion =>
4106 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4107 and then FCE (Expression (E1), Expression (E2));
4111 Entity (E1) = Entity (E2)
4112 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4114 when N_Unchecked_Type_Conversion =>
4116 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4117 and then FCE (Expression (E1), Expression (E2));
4119 -- All other node types cannot appear in this context. Strictly
4120 -- we should raise a fatal internal error. Instead we just ignore
4121 -- the nodes. This means that if anyone makes a mistake in the
4122 -- expander and mucks an expression tree irretrievably, the
4123 -- result will be a failure to detect a (probably very obscure)
4124 -- case of non-conformance, which is better than bombing on some
4125 -- case where two expressions do in fact conform.
4132 end Fully_Conformant_Expressions;
4134 ----------------------------------------
4135 -- Fully_Conformant_Discrete_Subtypes --
4136 ----------------------------------------
4138 function Fully_Conformant_Discrete_Subtypes
4139 (Given_S1 : Node_Id;
4140 Given_S2 : Node_Id) return Boolean
4142 S1 : constant Node_Id := Original_Node (Given_S1);
4143 S2 : constant Node_Id := Original_Node (Given_S2);
4145 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4146 -- Special-case for a bound given by a discriminant, which in the body
4147 -- is replaced with the discriminal of the enclosing type.
4149 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4150 -- Check both bounds
4152 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4154 if Is_Entity_Name (B1)
4155 and then Is_Entity_Name (B2)
4156 and then Ekind (Entity (B1)) = E_Discriminant
4158 return Chars (B1) = Chars (B2);
4161 return Fully_Conformant_Expressions (B1, B2);
4163 end Conforming_Bounds;
4165 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4168 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4170 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4171 end Conforming_Ranges;
4173 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4176 if Nkind (S1) /= Nkind (S2) then
4179 elsif Is_Entity_Name (S1) then
4180 return Entity (S1) = Entity (S2);
4182 elsif Nkind (S1) = N_Range then
4183 return Conforming_Ranges (S1, S2);
4185 elsif Nkind (S1) = N_Subtype_Indication then
4187 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4190 (Range_Expression (Constraint (S1)),
4191 Range_Expression (Constraint (S2)));
4195 end Fully_Conformant_Discrete_Subtypes;
4197 --------------------
4198 -- Install_Entity --
4199 --------------------
4201 procedure Install_Entity (E : Entity_Id) is
4202 Prev : constant Entity_Id := Current_Entity (E);
4205 Set_Is_Immediately_Visible (E);
4206 Set_Current_Entity (E);
4207 Set_Homonym (E, Prev);
4210 ---------------------
4211 -- Install_Formals --
4212 ---------------------
4214 procedure Install_Formals (Id : Entity_Id) is
4218 F := First_Formal (Id);
4220 while Present (F) loop
4224 end Install_Formals;
4226 ---------------------------------
4227 -- Is_Non_Overriding_Operation --
4228 ---------------------------------
4230 function Is_Non_Overriding_Operation
4231 (Prev_E : Entity_Id;
4232 New_E : Entity_Id) return Boolean
4236 G_Typ : Entity_Id := Empty;
4238 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4239 -- If F_Type is a derived type associated with a generic actual
4240 -- subtype, then return its Generic_Parent_Type attribute, else return
4243 function Types_Correspond
4244 (P_Type : Entity_Id;
4245 N_Type : Entity_Id) return Boolean;
4246 -- Returns true if and only if the types (or designated types in the
4247 -- case of anonymous access types) are the same or N_Type is derived
4248 -- directly or indirectly from P_Type.
4250 -----------------------------
4251 -- Get_Generic_Parent_Type --
4252 -----------------------------
4254 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4259 if Is_Derived_Type (F_Typ)
4260 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4262 -- The tree must be traversed to determine the parent subtype in
4263 -- the generic unit, which unfortunately isn't always available
4264 -- via semantic attributes. ??? (Note: The use of Original_Node
4265 -- is needed for cases where a full derived type has been
4268 Indic := Subtype_Indication
4269 (Type_Definition (Original_Node (Parent (F_Typ))));
4271 if Nkind (Indic) = N_Subtype_Indication then
4272 G_Typ := Entity (Subtype_Mark (Indic));
4274 G_Typ := Entity (Indic);
4277 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4278 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4280 return Generic_Parent_Type (Parent (G_Typ));
4285 end Get_Generic_Parent_Type;
4287 ----------------------
4288 -- Types_Correspond --
4289 ----------------------
4291 function Types_Correspond
4292 (P_Type : Entity_Id;
4293 N_Type : Entity_Id) return Boolean
4295 Prev_Type : Entity_Id := Base_Type (P_Type);
4296 New_Type : Entity_Id := Base_Type (N_Type);
4299 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4300 Prev_Type := Designated_Type (Prev_Type);
4303 if Ekind (New_Type) = E_Anonymous_Access_Type then
4304 New_Type := Designated_Type (New_Type);
4307 if Prev_Type = New_Type then
4310 elsif not Is_Class_Wide_Type (New_Type) then
4311 while Etype (New_Type) /= New_Type loop
4312 New_Type := Etype (New_Type);
4313 if New_Type = Prev_Type then
4319 end Types_Correspond;
4321 -- Start of processing for Is_Non_Overriding_Operation
4324 -- In the case where both operations are implicit derived subprograms
4325 -- then neither overrides the other. This can only occur in certain
4326 -- obscure cases (e.g., derivation from homographs created in a generic
4329 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4332 elsif Ekind (Current_Scope) = E_Package
4333 and then Is_Generic_Instance (Current_Scope)
4334 and then In_Private_Part (Current_Scope)
4335 and then Comes_From_Source (New_E)
4337 -- We examine the formals and result subtype of the inherited
4338 -- operation, to determine whether their type is derived from (the
4339 -- instance of) a generic type.
4341 Formal := First_Formal (Prev_E);
4343 while Present (Formal) loop
4344 F_Typ := Base_Type (Etype (Formal));
4346 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4347 F_Typ := Designated_Type (F_Typ);
4350 G_Typ := Get_Generic_Parent_Type (F_Typ);
4352 Next_Formal (Formal);
4355 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4356 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4363 -- If the generic type is a private type, then the original
4364 -- operation was not overriding in the generic, because there was
4365 -- no primitive operation to override.
4367 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4368 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4369 N_Formal_Private_Type_Definition
4373 -- The generic parent type is the ancestor of a formal derived
4374 -- type declaration. We need to check whether it has a primitive
4375 -- operation that should be overridden by New_E in the generic.
4379 P_Formal : Entity_Id;
4380 N_Formal : Entity_Id;
4384 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4387 while Present (Prim_Elt) loop
4388 P_Prim := Node (Prim_Elt);
4390 if Chars (P_Prim) = Chars (New_E)
4391 and then Ekind (P_Prim) = Ekind (New_E)
4393 P_Formal := First_Formal (P_Prim);
4394 N_Formal := First_Formal (New_E);
4395 while Present (P_Formal) and then Present (N_Formal) loop
4396 P_Typ := Etype (P_Formal);
4397 N_Typ := Etype (N_Formal);
4399 if not Types_Correspond (P_Typ, N_Typ) then
4403 Next_Entity (P_Formal);
4404 Next_Entity (N_Formal);
4407 -- Found a matching primitive operation belonging to the
4408 -- formal ancestor type, so the new subprogram is
4411 if not Present (P_Formal)
4412 and then not Present (N_Formal)
4413 and then (Ekind (New_E) /= E_Function
4416 (Etype (P_Prim), Etype (New_E)))
4422 Next_Elmt (Prim_Elt);
4425 -- If no match found, then the new subprogram does not
4426 -- override in the generic (nor in the instance).
4434 end Is_Non_Overriding_Operation;
4436 ------------------------------
4437 -- Make_Inequality_Operator --
4438 ------------------------------
4440 -- S is the defining identifier of an equality operator. We build a
4441 -- subprogram declaration with the right signature. This operation is
4442 -- intrinsic, because it is always expanded as the negation of the
4443 -- call to the equality function.
4445 procedure Make_Inequality_Operator (S : Entity_Id) is
4446 Loc : constant Source_Ptr := Sloc (S);
4449 Op_Name : Entity_Id;
4455 -- Check that equality was properly defined
4457 if No (Next_Formal (First_Formal (S))) then
4461 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4462 B := Make_Defining_Identifier (Loc,
4463 Chars (Next_Formal (First_Formal (S))));
4465 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4467 Formals := New_List (
4468 Make_Parameter_Specification (Loc,
4469 Defining_Identifier => A,
4471 New_Reference_To (Etype (First_Formal (S)), Loc)),
4473 Make_Parameter_Specification (Loc,
4474 Defining_Identifier => B,
4476 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4479 Make_Subprogram_Declaration (Loc,
4481 Make_Function_Specification (Loc,
4482 Defining_Unit_Name => Op_Name,
4483 Parameter_Specifications => Formals,
4484 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
4486 -- Insert inequality right after equality if it is explicit or after
4487 -- the derived type when implicit. These entities are created only for
4488 -- visibility purposes, and eventually replaced in the course of
4489 -- expansion, so they do not need to be attached to the tree and seen
4490 -- by the back-end. Keeping them internal also avoids spurious freezing
4491 -- problems. The declaration is inserted in the tree for analysis, and
4492 -- removed afterwards. If the equality operator comes from an explicit
4493 -- declaration, attach the inequality immediately after. Else the
4494 -- equality is inherited from a derived type declaration, so insert
4495 -- inequality after that declaration.
4497 if No (Alias (S)) then
4498 Insert_After (Unit_Declaration_Node (S), Decl);
4499 elsif Is_List_Member (Parent (S)) then
4500 Insert_After (Parent (S), Decl);
4502 Insert_After (Parent (Etype (First_Formal (S))), Decl);
4505 Mark_Rewrite_Insertion (Decl);
4506 Set_Is_Intrinsic_Subprogram (Op_Name);
4509 Set_Has_Completion (Op_Name);
4510 Set_Corresponding_Equality (Op_Name, S);
4511 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4512 end Make_Inequality_Operator;
4514 ----------------------
4515 -- May_Need_Actuals --
4516 ----------------------
4518 procedure May_Need_Actuals (Fun : Entity_Id) is
4523 F := First_Formal (Fun);
4526 while Present (F) loop
4527 if No (Default_Value (F)) then
4535 Set_Needs_No_Actuals (Fun, B);
4536 end May_Need_Actuals;
4538 ---------------------
4539 -- Mode_Conformant --
4540 ---------------------
4542 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4545 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4547 end Mode_Conformant;
4549 ---------------------------
4550 -- New_Overloaded_Entity --
4551 ---------------------------
4553 procedure New_Overloaded_Entity
4555 Derived_Type : Entity_Id := Empty)
4557 Does_Override : Boolean := False;
4558 -- Set if the current scope has an operation that is type-conformant
4559 -- with S, and becomes hidden by S.
4562 -- Entity that S overrides
4564 Prev_Vis : Entity_Id := Empty;
4565 -- Needs comment ???
4567 Is_Alias_Interface : Boolean := False;
4569 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4570 -- Check that E is declared in the private part of the current package,
4571 -- or in the package body, where it may hide a previous declaration.
4572 -- We can't use In_Private_Part by itself because this flag is also
4573 -- set when freezing entities, so we must examine the place of the
4574 -- declaration in the tree, and recognize wrapper packages as well.
4576 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4577 -- If the subprogram being analyzed is a primitive operation of
4578 -- the type of one of its formals, set the corresponding flag.
4580 ----------------------------
4581 -- Is_Private_Declaration --
4582 ----------------------------
4584 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4585 Priv_Decls : List_Id;
4586 Decl : constant Node_Id := Unit_Declaration_Node (E);
4589 if Is_Package (Current_Scope)
4590 and then In_Private_Part (Current_Scope)
4593 Private_Declarations (
4594 Specification (Unit_Declaration_Node (Current_Scope)));
4596 return In_Package_Body (Current_Scope)
4598 (Is_List_Member (Decl)
4599 and then List_Containing (Decl) = Priv_Decls)
4600 or else (Nkind (Parent (Decl)) = N_Package_Specification
4601 and then not Is_Compilation_Unit (
4602 Defining_Entity (Parent (Decl)))
4603 and then List_Containing (Parent (Parent (Decl)))
4608 end Is_Private_Declaration;
4610 -------------------------------
4611 -- Maybe_Primitive_Operation --
4612 -------------------------------
4614 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4619 function Visible_Part_Type (T : Entity_Id) return Boolean;
4620 -- Returns true if T is declared in the visible part of
4621 -- the current package scope; otherwise returns false.
4622 -- Assumes that T is declared in a package.
4624 procedure Check_Private_Overriding (T : Entity_Id);
4625 -- Checks that if a primitive abstract subprogram of a visible
4626 -- abstract type is declared in a private part, then it must
4627 -- override an abstract subprogram declared in the visible part.
4628 -- Also checks that if a primitive function with a controlling
4629 -- result is declared in a private part, then it must override
4630 -- a function declared in the visible part.
4632 ------------------------------
4633 -- Check_Private_Overriding --
4634 ------------------------------
4636 procedure Check_Private_Overriding (T : Entity_Id) is
4638 if Ekind (Current_Scope) = E_Package
4639 and then In_Private_Part (Current_Scope)
4640 and then Visible_Part_Type (T)
4641 and then not In_Instance
4644 and then Is_Abstract (S)
4645 and then (not Is_Overriding or else not Is_Abstract (E))
4647 if not Is_Interface (T) then
4648 Error_Msg_N ("abstract subprograms must be visible "
4649 & "('R'M 3.9.3(10))!", S);
4651 -- Ada 2005 (AI-251)
4654 Error_Msg_N ("primitive subprograms of interface types "
4655 & "declared in a visible part, must be declared in "
4656 & "the visible part ('R'M 3.9.4)!", S);
4659 elsif Ekind (S) = E_Function
4660 and then Is_Tagged_Type (T)
4661 and then T = Base_Type (Etype (S))
4662 and then not Is_Overriding
4665 ("private function with tagged result must"
4666 & " override visible-part function", S);
4668 ("\move subprogram to the visible part"
4669 & " ('R'M 3.9.3(10))", S);
4672 end Check_Private_Overriding;
4674 -----------------------
4675 -- Visible_Part_Type --
4676 -----------------------
4678 function Visible_Part_Type (T : Entity_Id) return Boolean is
4679 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4683 -- If the entity is a private type, then it must be
4684 -- declared in a visible part.
4686 if Ekind (T) in Private_Kind then
4690 -- Otherwise, we traverse the visible part looking for its
4691 -- corresponding declaration. We cannot use the declaration
4692 -- node directly because in the private part the entity of a
4693 -- private type is the one in the full view, which does not
4694 -- indicate that it is the completion of something visible.
4696 N := First (Visible_Declarations (Specification (P)));
4697 while Present (N) loop
4698 if Nkind (N) = N_Full_Type_Declaration
4699 and then Present (Defining_Identifier (N))
4700 and then T = Defining_Identifier (N)
4704 elsif (Nkind (N) = N_Private_Type_Declaration
4706 Nkind (N) = N_Private_Extension_Declaration)
4707 and then Present (Defining_Identifier (N))
4708 and then T = Full_View (Defining_Identifier (N))
4717 end Visible_Part_Type;
4719 -- Start of processing for Maybe_Primitive_Operation
4722 if not Comes_From_Source (S) then
4725 -- If the subprogram is at library level, it is not primitive
4728 elsif Current_Scope = Standard_Standard then
4731 elsif (Ekind (Current_Scope) = E_Package
4732 and then not In_Package_Body (Current_Scope))
4733 or else Is_Overriding
4735 -- For function, check return type
4737 if Ekind (S) = E_Function then
4738 B_Typ := Base_Type (Etype (S));
4740 if Scope (B_Typ) = Current_Scope then
4741 Set_Has_Primitive_Operations (B_Typ);
4742 Check_Private_Overriding (B_Typ);
4746 -- For all subprograms, check formals
4748 Formal := First_Formal (S);
4749 while Present (Formal) loop
4750 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4751 F_Typ := Designated_Type (Etype (Formal));
4753 F_Typ := Etype (Formal);
4756 B_Typ := Base_Type (F_Typ);
4758 if Scope (B_Typ) = Current_Scope then
4759 Set_Has_Primitive_Operations (B_Typ);
4760 Check_Private_Overriding (B_Typ);
4763 Next_Formal (Formal);
4766 end Maybe_Primitive_Operation;
4768 -- Start of processing for New_Overloaded_Entity
4771 -- We need to look for an entity that S may override. This must be a
4772 -- homonym in the current scope, so we look for the first homonym of
4773 -- S in the current scope as the starting point for the search.
4775 E := Current_Entity_In_Scope (S);
4777 -- If there is no homonym then this is definitely not overriding
4780 Enter_Overloaded_Entity (S);
4781 Check_Dispatching_Operation (S, Empty);
4782 Maybe_Primitive_Operation;
4784 -- Ada 2005 (AI-397): Subprograms in the context of protected
4785 -- types have their overriding indicators checked in Sem_Ch9.
4787 if Ekind (S) not in Subprogram_Kind
4788 or else Ekind (Scope (S)) /= E_Protected_Type
4790 Check_Overriding_Indicator (S, False);
4793 -- If there is a homonym that is not overloadable, then we have an
4794 -- error, except for the special cases checked explicitly below.
4796 elsif not Is_Overloadable (E) then
4798 -- Check for spurious conflict produced by a subprogram that has the
4799 -- same name as that of the enclosing generic package. The conflict
4800 -- occurs within an instance, between the subprogram and the renaming
4801 -- declaration for the package. After the subprogram, the package
4802 -- renaming declaration becomes hidden.
4804 if Ekind (E) = E_Package
4805 and then Present (Renamed_Object (E))
4806 and then Renamed_Object (E) = Current_Scope
4807 and then Nkind (Parent (Renamed_Object (E))) =
4808 N_Package_Specification
4809 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4812 Set_Is_Immediately_Visible (E, False);
4813 Enter_Overloaded_Entity (S);
4814 Set_Homonym (S, Homonym (E));
4815 Check_Dispatching_Operation (S, Empty);
4816 Check_Overriding_Indicator (S, False);
4818 -- If the subprogram is implicit it is hidden by the previous
4819 -- declaration. However if it is dispatching, it must appear in the
4820 -- dispatch table anyway, because it can be dispatched to even if it
4821 -- cannot be called directly.
4823 elsif Present (Alias (S))
4824 and then not Comes_From_Source (S)
4826 Set_Scope (S, Current_Scope);
4828 if Is_Dispatching_Operation (Alias (S)) then
4829 Check_Dispatching_Operation (S, Empty);
4835 Error_Msg_Sloc := Sloc (E);
4836 Error_Msg_N ("& conflicts with declaration#", S);
4838 -- Useful additional warning
4840 if Is_Generic_Unit (E) then
4841 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4847 -- E exists and is overloadable
4850 Is_Alias_Interface :=
4852 and then Is_Dispatching_Operation (Alias (S))
4853 and then Present (DTC_Entity (Alias (S)))
4854 and then Is_Interface (Scope (DTC_Entity (Alias (S))));
4856 -- Loop through E and its homonyms to determine if any of them is
4857 -- the candidate for overriding by S.
4859 while Present (E) loop
4861 -- Definitely not interesting if not in the current scope
4863 if Scope (E) /= Current_Scope then
4866 -- Check if we have type conformance
4868 -- Ada 2005 (AI-251): In case of overriding an interface
4869 -- subprogram it is not an error that the old and new entities
4870 -- have the same profile, and hence we skip this code.
4872 elsif not Is_Alias_Interface
4873 and then Type_Conformant (E, S)
4875 -- If the old and new entities have the same profile and one
4876 -- is not the body of the other, then this is an error, unless
4877 -- one of them is implicitly declared.
4879 -- There are some cases when both can be implicit, for example
4880 -- when both a literal and a function that overrides it are
4881 -- inherited in a derivation, or when an inhertited operation
4882 -- of a tagged full type overrides the ineherited operation of
4883 -- a private extension. Ada 83 had a special rule for the the
4884 -- literal case. In Ada95, the later implicit operation hides
4885 -- the former, and the literal is always the former. In the
4886 -- odd case where both are derived operations declared at the
4887 -- same point, both operations should be declared, and in that
4888 -- case we bypass the following test and proceed to the next
4889 -- part (this can only occur for certain obscure cases
4890 -- involving homographs in instances and can't occur for
4891 -- dispatching operations ???). Note that the following
4892 -- condition is less than clear. For example, it's not at all
4893 -- clear why there's a test for E_Entry here. ???
4895 if Present (Alias (S))
4896 and then (No (Alias (E))
4897 or else Comes_From_Source (E)
4898 or else Is_Dispatching_Operation (E))
4900 (Ekind (E) = E_Entry
4901 or else Ekind (E) /= E_Enumeration_Literal)
4903 -- When an derived operation is overloaded it may be due to
4904 -- the fact that the full view of a private extension
4905 -- re-inherits. It has to be dealt with.
4907 if Is_Package (Current_Scope)
4908 and then In_Private_Part (Current_Scope)
4910 Check_Operation_From_Private_View (S, E);
4913 -- In any case the implicit operation remains hidden by
4914 -- the existing declaration, which is overriding.
4916 Set_Is_Overriding_Operation (E);
4918 if Comes_From_Source (E) then
4919 Check_Overriding_Indicator (E, True);
4924 -- Within an instance, the renaming declarations for
4925 -- actual subprograms may become ambiguous, but they do
4926 -- not hide each other.
4928 elsif Ekind (E) /= E_Entry
4929 and then not Comes_From_Source (E)
4930 and then not Is_Generic_Instance (E)
4931 and then (Present (Alias (E))
4932 or else Is_Intrinsic_Subprogram (E))
4933 and then (not In_Instance
4934 or else No (Parent (E))
4935 or else Nkind (Unit_Declaration_Node (E)) /=
4936 N_Subprogram_Renaming_Declaration)
4938 -- A subprogram child unit is not allowed to override
4939 -- an inherited subprogram (10.1.1(20)).
4941 if Is_Child_Unit (S) then
4943 ("child unit overrides inherited subprogram in parent",
4948 if Is_Non_Overriding_Operation (E, S) then
4949 Enter_Overloaded_Entity (S);
4950 if not Present (Derived_Type)
4951 or else Is_Tagged_Type (Derived_Type)
4953 Check_Dispatching_Operation (S, Empty);
4959 -- E is a derived operation or an internal operator which
4960 -- is being overridden. Remove E from further visibility.
4961 -- Furthermore, if E is a dispatching operation, it must be
4962 -- replaced in the list of primitive operations of its type
4963 -- (see Override_Dispatching_Operation).
4965 Does_Override := True;
4971 Prev := First_Entity (Current_Scope);
4973 while Present (Prev)
4974 and then Next_Entity (Prev) /= E
4979 -- It is possible for E to be in the current scope and
4980 -- yet not in the entity chain. This can only occur in a
4981 -- generic context where E is an implicit concatenation
4982 -- in the formal part, because in a generic body the
4983 -- entity chain starts with the formals.
4986 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4988 -- E must be removed both from the entity_list of the
4989 -- current scope, and from the visibility chain
4991 if Debug_Flag_E then
4992 Write_Str ("Override implicit operation ");
4993 Write_Int (Int (E));
4997 -- If E is a predefined concatenation, it stands for four
4998 -- different operations. As a result, a single explicit
4999 -- declaration does not hide it. In a possible ambiguous
5000 -- situation, Disambiguate chooses the user-defined op,
5001 -- so it is correct to retain the previous internal one.
5003 if Chars (E) /= Name_Op_Concat
5004 or else Ekind (E) /= E_Operator
5006 -- For nondispatching derived operations that are
5007 -- overridden by a subprogram declared in the private
5008 -- part of a package, we retain the derived
5009 -- subprogram but mark it as not immediately visible.
5010 -- If the derived operation was declared in the
5011 -- visible part then this ensures that it will still
5012 -- be visible outside the package with the proper
5013 -- signature (calls from outside must also be
5014 -- directed to this version rather than the
5015 -- overriding one, unlike the dispatching case).
5016 -- Calls from inside the package will still resolve
5017 -- to the overriding subprogram since the derived one
5018 -- is marked as not visible within the package.
5020 -- If the private operation is dispatching, we achieve
5021 -- the overriding by keeping the implicit operation
5022 -- but setting its alias to be the overriding one. In
5023 -- this fashion the proper body is executed in all
5024 -- cases, but the original signature is used outside
5027 -- If the overriding is not in the private part, we
5028 -- remove the implicit operation altogether.
5030 if Is_Private_Declaration (S) then
5032 if not Is_Dispatching_Operation (E) then
5033 Set_Is_Immediately_Visible (E, False);
5035 -- Work done in Override_Dispatching_Operation,
5036 -- so nothing else need to be done here.
5042 -- Find predecessor of E in Homonym chain
5044 if E = Current_Entity (E) then
5047 Prev_Vis := Current_Entity (E);
5048 while Homonym (Prev_Vis) /= E loop
5049 Prev_Vis := Homonym (Prev_Vis);
5053 if Prev_Vis /= Empty then
5055 -- Skip E in the visibility chain
5057 Set_Homonym (Prev_Vis, Homonym (E));
5060 Set_Name_Entity_Id (Chars (E), Homonym (E));
5063 Set_Next_Entity (Prev, Next_Entity (E));
5065 if No (Next_Entity (Prev)) then
5066 Set_Last_Entity (Current_Scope, Prev);
5072 Enter_Overloaded_Entity (S);
5073 Set_Is_Overriding_Operation (S);
5074 Check_Overriding_Indicator (S, True);
5076 if Is_Dispatching_Operation (E) then
5078 -- An overriding dispatching subprogram inherits the
5079 -- convention of the overridden subprogram (by
5082 Set_Convention (S, Convention (E));
5084 -- AI-251: If the subprogram implements an interface,
5085 -- check if this subprogram covers other interface
5086 -- subprograms available in the same scope.
5088 if Present (Alias (E))
5089 and then Ekind (Alias (E)) /= E_Operator
5090 and then Present (DTC_Entity (Alias (E)))
5091 and then Is_Interface (Scope (DTC_Entity
5094 Check_Dispatching_Operation (S, E);
5101 while Present (E1) loop
5102 if Present (Alias (E1))
5103 and then Ekind (Alias (E1)) /= E_Operator
5104 and then Present (DTC_Entity (Alias (E1)))
5105 and then Is_Interface
5106 (Scope (DTC_Entity (Alias (E1))))
5107 and then Type_Conformant (E1, S)
5109 Check_Dispatching_Operation (S, E1);
5116 Check_Dispatching_Operation (S, E);
5120 Check_Dispatching_Operation (S, Empty);
5123 Maybe_Primitive_Operation (Is_Overriding => True);
5124 goto Check_Inequality;
5127 -- Apparent redeclarations in instances can occur when two
5128 -- formal types get the same actual type. The subprograms in
5129 -- in the instance are legal, even if not callable from the
5130 -- outside. Calls from within are disambiguated elsewhere.
5131 -- For dispatching operations in the visible part, the usual
5132 -- rules apply, and operations with the same profile are not
5135 elsif (In_Instance_Visible_Part
5136 and then not Is_Dispatching_Operation (E))
5137 or else In_Instance_Not_Visible
5141 -- Here we have a real error (identical profile)
5144 Error_Msg_Sloc := Sloc (E);
5146 -- Avoid cascaded errors if the entity appears in
5147 -- subsequent calls.
5149 Set_Scope (S, Current_Scope);
5151 Error_Msg_N ("& conflicts with declaration#", S);
5153 if Is_Generic_Instance (S)
5154 and then not Has_Completion (E)
5157 ("\instantiation cannot provide body for it", S);
5171 -- On exit, we know that S is a new entity
5173 Enter_Overloaded_Entity (S);
5174 Maybe_Primitive_Operation;
5175 Check_Overriding_Indicator (S, Does_Override);
5177 -- If S is a derived operation for an untagged type then by
5178 -- definition it's not a dispatching operation (even if the parent
5179 -- operation was dispatching), so we don't call
5180 -- Check_Dispatching_Operation in that case.
5182 if not Present (Derived_Type)
5183 or else Is_Tagged_Type (Derived_Type)
5185 Check_Dispatching_Operation (S, Empty);
5189 -- If this is a user-defined equality operator that is not a derived
5190 -- subprogram, create the corresponding inequality. If the operation is
5191 -- dispatching, the expansion is done elsewhere, and we do not create
5192 -- an explicit inequality operation.
5194 <<Check_Inequality>>
5195 if Chars (S) = Name_Op_Eq
5196 and then Etype (S) = Standard_Boolean
5197 and then Present (Parent (S))
5198 and then not Is_Dispatching_Operation (S)
5200 Make_Inequality_Operator (S);
5202 end New_Overloaded_Entity;
5204 ---------------------
5205 -- Process_Formals --
5206 ---------------------
5208 procedure Process_Formals
5210 Related_Nod : Node_Id)
5212 Param_Spec : Node_Id;
5214 Formal_Type : Entity_Id;
5218 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5219 -- Check whether the default has a class-wide type. After analysis the
5220 -- default has the type of the formal, so we must also check explicitly
5221 -- for an access attribute.
5223 ---------------------------
5224 -- Is_Class_Wide_Default --
5225 ---------------------------
5227 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5229 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5230 or else (Nkind (D) = N_Attribute_Reference
5231 and then Attribute_Name (D) = Name_Access
5232 and then Is_Class_Wide_Type (Etype (Prefix (D))));
5233 end Is_Class_Wide_Default;
5235 -- Start of processing for Process_Formals
5238 -- In order to prevent premature use of the formals in the same formal
5239 -- part, the Ekind is left undefined until all default expressions are
5240 -- analyzed. The Ekind is established in a separate loop at the end.
5242 Param_Spec := First (T);
5244 while Present (Param_Spec) loop
5246 Formal := Defining_Identifier (Param_Spec);
5247 Enter_Name (Formal);
5249 -- Case of ordinary parameters
5251 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5252 Find_Type (Parameter_Type (Param_Spec));
5253 Ptype := Parameter_Type (Param_Spec);
5255 if Ptype = Error then
5259 Formal_Type := Entity (Ptype);
5261 if Ekind (Formal_Type) = E_Incomplete_Type
5262 or else (Is_Class_Wide_Type (Formal_Type)
5263 and then Ekind (Root_Type (Formal_Type)) =
5266 -- Ada 2005 (AI-50217): Incomplete tagged types that are made
5267 -- visible by a limited with_clause are valid formal types.
5269 if From_With_Type (Formal_Type)
5270 and then Is_Tagged_Type (Formal_Type)
5274 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5275 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5277 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5280 elsif Ekind (Formal_Type) = E_Void then
5281 Error_Msg_NE ("premature use of&",
5282 Parameter_Type (Param_Spec), Formal_Type);
5285 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5286 -- declaration corresponding to the null-excluding type of the
5287 -- formal in the enclosing scope. Finally, replace the
5288 -- parameter type of the formal with the internal subtype.
5290 if Null_Exclusion_Present (Param_Spec) then
5292 Loc : constant Source_Ptr := Sloc (Param_Spec);
5294 Anon : constant Entity_Id :=
5295 Make_Defining_Identifier (Loc,
5296 Chars => New_Internal_Name ('S'));
5298 Curr_Scope : constant Scope_Stack_Entry :=
5299 Scope_Stack.Table (Scope_Stack.Last);
5301 Ptype : constant Node_Id := Parameter_Type (Param_Spec);
5303 P : Node_Id := Parent (Related_Nod);
5306 Set_Is_Internal (Anon);
5309 Make_Subtype_Declaration (Loc,
5310 Defining_Identifier => Anon,
5311 Null_Exclusion_Present => True,
5312 Subtype_Indication =>
5313 New_Occurrence_Of (Etype (Ptype), Loc));
5315 -- Propagate the null-excluding attribute to the new entity
5317 if Null_Exclusion_Present (Param_Spec) then
5318 Set_Null_Exclusion_Present (Param_Spec, False);
5319 Set_Can_Never_Be_Null (Anon);
5322 Mark_Rewrite_Insertion (Decl);
5324 -- Insert the new declaration in the nearest enclosing scope
5325 -- in front of the subprogram or entry declaration.
5327 while not Is_List_Member (P) loop
5331 Insert_Before (P, Decl);
5333 Rewrite (Ptype, New_Occurrence_Of (Anon, Loc));
5334 Mark_Rewrite_Insertion (Ptype);
5336 -- Analyze the new declaration in the context of the
5339 Scope_Stack.Decrement_Last;
5341 Scope_Stack.Append (Curr_Scope);
5343 Formal_Type := Anon;
5347 -- Ada 2005 (AI-231): Static checks
5349 if Null_Exclusion_Present (Param_Spec)
5350 or else Can_Never_Be_Null (Entity (Ptype))
5352 Null_Exclusion_Static_Checks (Param_Spec);
5355 -- An access formal type
5359 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5361 -- Ada 2005 (AI-254)
5364 AD : constant Node_Id :=
5365 Access_To_Subprogram_Definition
5366 (Parameter_Type (Param_Spec));
5368 if Present (AD) and then Protected_Present (AD) then
5370 Replace_Anonymous_Access_To_Protected_Subprogram
5371 (Param_Spec, Formal_Type);
5376 Set_Etype (Formal, Formal_Type);
5377 Default := Expression (Param_Spec);
5379 if Present (Default) then
5380 if Out_Present (Param_Spec) then
5382 ("default initialization only allowed for IN parameters",
5386 -- Do the special preanalysis of the expression (see section on
5387 -- "Handling of Default Expressions" in the spec of package Sem).
5389 Analyze_Per_Use_Expression (Default, Formal_Type);
5391 -- Check that the designated type of an access parameter's
5392 -- default is not a class-wide type unless the parameter's
5393 -- designated type is also class-wide.
5395 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5396 and then Is_Class_Wide_Default (Default)
5397 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5400 ("access to class-wide expression not allowed here", Default);
5408 -- If this is the formal part of a function specification, analyze the
5409 -- subtype mark in the context where the formals are visible but not
5410 -- yet usable, and may hide outer homographs.
5412 if Nkind (Related_Nod) = N_Function_Specification then
5413 Analyze_Return_Type (Related_Nod);
5416 -- Now set the kind (mode) of each formal
5418 Param_Spec := First (T);
5420 while Present (Param_Spec) loop
5421 Formal := Defining_Identifier (Param_Spec);
5422 Set_Formal_Mode (Formal);
5424 if Ekind (Formal) = E_In_Parameter then
5425 Set_Default_Value (Formal, Expression (Param_Spec));
5427 if Present (Expression (Param_Spec)) then
5428 Default := Expression (Param_Spec);
5430 if Is_Scalar_Type (Etype (Default)) then
5432 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5434 Formal_Type := Entity (Parameter_Type (Param_Spec));
5437 Formal_Type := Access_Definition
5438 (Related_Nod, Parameter_Type (Param_Spec));
5441 Apply_Scalar_Range_Check (Default, Formal_Type);
5449 end Process_Formals;
5451 ----------------------------
5452 -- Reference_Body_Formals --
5453 ----------------------------
5455 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5460 if Error_Posted (Spec) then
5464 Fs := First_Formal (Spec);
5465 Fb := First_Formal (Bod);
5467 while Present (Fs) loop
5468 Generate_Reference (Fs, Fb, 'b');
5471 Style.Check_Identifier (Fb, Fs);
5474 Set_Spec_Entity (Fb, Fs);
5475 Set_Referenced (Fs, False);
5479 end Reference_Body_Formals;
5481 -------------------------
5482 -- Set_Actual_Subtypes --
5483 -------------------------
5485 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5486 Loc : constant Source_Ptr := Sloc (N);
5490 First_Stmt : Node_Id := Empty;
5491 AS_Needed : Boolean;
5494 -- If this is an emtpy initialization procedure, no need to create
5495 -- actual subtypes (small optimization).
5497 if Ekind (Subp) = E_Procedure
5498 and then Is_Null_Init_Proc (Subp)
5503 Formal := First_Formal (Subp);
5504 while Present (Formal) loop
5505 T := Etype (Formal);
5507 -- We never need an actual subtype for a constrained formal
5509 if Is_Constrained (T) then
5512 -- If we have unknown discriminants, then we do not need an actual
5513 -- subtype, or more accurately we cannot figure it out! Note that
5514 -- all class-wide types have unknown discriminants.
5516 elsif Has_Unknown_Discriminants (T) then
5519 -- At this stage we have an unconstrained type that may need an
5520 -- actual subtype. For sure the actual subtype is needed if we have
5521 -- an unconstrained array type.
5523 elsif Is_Array_Type (T) then
5526 -- The only other case which needs an actual subtype is an
5527 -- unconstrained record type which is an IN parameter (we cannot
5528 -- generate actual subtypes for the OUT or IN OUT case, since an
5529 -- assignment can change the discriminant values. However we exclude
5530 -- the case of initialization procedures, since discriminants are
5531 -- handled very specially in this context, see the section entitled
5532 -- "Handling of Discriminants" in Einfo. We also exclude the case of
5533 -- Discrim_SO_Functions (functions used in front end layout mode for
5534 -- size/offset values), since in such functions only discriminants
5535 -- are referenced, and not only are such subtypes not needed, but
5536 -- they cannot always be generated, because of order of elaboration
5539 elsif Is_Record_Type (T)
5540 and then Ekind (Formal) = E_In_Parameter
5541 and then Chars (Formal) /= Name_uInit
5542 and then not Is_Unchecked_Union (T)
5543 and then not Is_Discrim_SO_Function (Subp)
5547 -- All other cases do not need an actual subtype
5553 -- Generate actual subtypes for unconstrained arrays and
5554 -- unconstrained discriminated records.
5557 if Nkind (N) = N_Accept_Statement then
5559 -- If expansion is active, The formal is replaced by a local
5560 -- variable that renames the corresponding entry of the
5561 -- parameter block, and it is this local variable that may
5562 -- require an actual subtype.
5564 if Expander_Active then
5565 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5567 Decl := Build_Actual_Subtype (T, Formal);
5570 if Present (Handled_Statement_Sequence (N)) then
5572 First (Statements (Handled_Statement_Sequence (N)));
5573 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5574 Mark_Rewrite_Insertion (Decl);
5576 -- If the accept statement has no body, there will be no
5577 -- reference to the actuals, so no need to compute actual
5584 Decl := Build_Actual_Subtype (T, Formal);
5585 Prepend (Decl, Declarations (N));
5586 Mark_Rewrite_Insertion (Decl);
5589 -- The declaration uses the bounds of an existing object, and
5590 -- therefore needs no constraint checks.
5592 Analyze (Decl, Suppress => All_Checks);
5594 -- We need to freeze manually the generated type when it is
5595 -- inserted anywhere else than in a declarative part.
5597 if Present (First_Stmt) then
5598 Insert_List_Before_And_Analyze (First_Stmt,
5599 Freeze_Entity (Defining_Identifier (Decl), Loc));
5602 if Nkind (N) = N_Accept_Statement
5603 and then Expander_Active
5605 Set_Actual_Subtype (Renamed_Object (Formal),
5606 Defining_Identifier (Decl));
5608 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5612 Next_Formal (Formal);
5614 end Set_Actual_Subtypes;
5616 ---------------------
5617 -- Set_Formal_Mode --
5618 ---------------------
5620 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5621 Spec : constant Node_Id := Parent (Formal_Id);
5624 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5625 -- since we ensure that corresponding actuals are always valid at the
5626 -- point of the call.
5628 if Out_Present (Spec) then
5629 if Ekind (Scope (Formal_Id)) = E_Function
5630 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5632 Error_Msg_N ("functions can only have IN parameters", Spec);
5633 Set_Ekind (Formal_Id, E_In_Parameter);
5635 elsif In_Present (Spec) then
5636 Set_Ekind (Formal_Id, E_In_Out_Parameter);
5639 Set_Ekind (Formal_Id, E_Out_Parameter);
5640 Set_Never_Set_In_Source (Formal_Id, True);
5641 Set_Is_True_Constant (Formal_Id, False);
5642 Set_Current_Value (Formal_Id, Empty);
5646 Set_Ekind (Formal_Id, E_In_Parameter);
5649 -- Set Is_Known_Non_Null for access parameters since the language
5650 -- guarantees that access parameters are always non-null. We also set
5651 -- Can_Never_Be_Null, since there is no way to change the value.
5653 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5655 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
5656 -- null; In Ada 2005, only if then null_exclusion is explicit.
5658 if Ada_Version < Ada_05
5659 or else Null_Exclusion_Present (Spec)
5660 or else Can_Never_Be_Null (Etype (Formal_Id))
5662 Set_Is_Known_Non_Null (Formal_Id);
5663 Set_Can_Never_Be_Null (Formal_Id);
5666 elsif Is_Access_Type (Etype (Formal_Id))
5667 and then Can_Never_Be_Null (Etype (Formal_Id))
5669 -- Ada 2005: The access subtype may be declared with null-exclusion
5671 Set_Is_Known_Non_Null (Formal_Id);
5672 Set_Can_Never_Be_Null (Formal_Id);
5675 Set_Mechanism (Formal_Id, Default_Mechanism);
5676 Set_Formal_Validity (Formal_Id);
5677 end Set_Formal_Mode;
5679 -------------------------
5680 -- Set_Formal_Validity --
5681 -------------------------
5683 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5685 -- If no validity checking, then we cannot assume anything about the
5686 -- validity of parameters, since we do not know there is any checking
5687 -- of the validity on the call side.
5689 if not Validity_Checks_On then
5692 -- If validity checking for parameters is enabled, this means we are
5693 -- not supposed to make any assumptions about argument values.
5695 elsif Validity_Check_Parameters then
5698 -- If we are checking in parameters, we will assume that the caller is
5699 -- also checking parameters, so we can assume the parameter is valid.
5701 elsif Ekind (Formal_Id) = E_In_Parameter
5702 and then Validity_Check_In_Params
5704 Set_Is_Known_Valid (Formal_Id, True);
5706 -- Similar treatment for IN OUT parameters
5708 elsif Ekind (Formal_Id) = E_In_Out_Parameter
5709 and then Validity_Check_In_Out_Params
5711 Set_Is_Known_Valid (Formal_Id, True);
5713 end Set_Formal_Validity;
5715 ------------------------
5716 -- Subtype_Conformant --
5717 ------------------------
5719 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5722 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5724 end Subtype_Conformant;
5726 ---------------------
5727 -- Type_Conformant --
5728 ---------------------
5730 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5733 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
5735 end Type_Conformant;
5737 -------------------------------
5738 -- Valid_Operator_Definition --
5739 -------------------------------
5741 procedure Valid_Operator_Definition (Designator : Entity_Id) is
5744 Id : constant Name_Id := Chars (Designator);
5748 F := First_Formal (Designator);
5750 while Present (F) loop
5753 if Present (Default_Value (F)) then
5755 ("default values not allowed for operator parameters",
5762 -- Verify that user-defined operators have proper number of arguments
5763 -- First case of operators which can only be unary
5766 or else Id = Name_Op_Abs
5770 -- Case of operators which can be unary or binary
5772 elsif Id = Name_Op_Add
5773 or Id = Name_Op_Subtract
5775 N_OK := (N in 1 .. 2);
5777 -- All other operators can only be binary
5785 ("incorrect number of arguments for operator", Designator);
5789 and then Base_Type (Etype (Designator)) = Standard_Boolean
5790 and then not Is_Intrinsic_Subprogram (Designator)
5793 ("explicit definition of inequality not allowed", Designator);
5795 end Valid_Operator_Definition;