1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch7; use Exp_Ch7;
37 with Freeze; use Freeze;
38 with Lib.Xref; use Lib.Xref;
39 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Rtsfind; use Rtsfind;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch4; use Sem_Ch4;
50 with Sem_Ch5; use Sem_Ch5;
51 with Sem_Ch8; use Sem_Ch8;
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_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
83 -- Analyze a generic subprogram body
85 function Build_Body_To_Inline
90 -- If a subprogram has pragma Inline and inlining is active, use generic
91 -- machinery to build an unexpanded body for the subprogram. This body is
92 -- subsequenty used for inline expansions at call sites. If subprogram can
93 -- be inlined (depending on size and nature of local declarations) this
94 -- function returns true. Otherwise subprogram body is treated normally.
96 type Conformance_Type is
97 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
98 -- Conformance type used for following call, meaning matches the
99 -- RM definitions of the corresponding terms.
101 procedure Check_Conformance
104 Ctype : Conformance_Type;
106 Conforms : out Boolean;
107 Err_Loc : Node_Id := Empty;
108 Get_Inst : Boolean := False);
109 -- Given two entities, this procedure checks that the profiles associated
110 -- with these entities meet the conformance criterion given by the third
111 -- parameter. If they conform, Conforms is set True and control returns
112 -- to the caller. If they do not conform, Conforms is set to False, and
113 -- in addition, if Errmsg is True on the call, proper messages are output
114 -- to complain about the conformance failure. If Err_Loc is non_Empty
115 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
116 -- error messages are placed on the appropriate part of the construct
117 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
118 -- against a formal access-to-subprogram type so Get_Instance_Of must
121 procedure Check_Subprogram_Order (N : Node_Id);
122 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
123 -- the alpha ordering rule for N if this ordering requirement applicable.
125 function Is_Non_Overriding_Operation
129 -- Enforce the rule given in 12.3(18): a private operation in an instance
130 -- overrides an inherited operation only if the corresponding operation
131 -- was overriding in the generic. This can happen for primitive operations
132 -- of types derived (in the generic unit) from formal private or formal
135 procedure Check_Returns
139 -- Called to check for missing return statements in a function body,
140 -- or for returns present in a procedure body which has No_Return set.
141 -- L is the handled statement sequence for the subprogram body. This
142 -- procedure checks all flow paths to make sure they either have a
143 -- return (Mode = 'F') or do not have a return (Mode = 'P'). The flag
144 -- Err is set if there are any control paths not explicitly terminated
145 -- by a return in the function case, and is True otherwise.
147 function Conforming_Types
150 Ctype : Conformance_Type;
151 Get_Inst : Boolean := False)
153 -- Check that two formal parameter types conform, checking both
154 -- for equality of base types, and where required statically
155 -- matching subtypes, depending on the setting of Ctype.
157 procedure Enter_Overloaded_Entity (S : Entity_Id);
158 -- This procedure makes S, a new overloaded entity, into the first
159 -- visible entity with that name.
161 procedure Install_Entity (E : Entity_Id);
162 -- Make single entity visible. Used for generic formals as well.
164 procedure Install_Formals (Id : Entity_Id);
165 -- On entry to a subprogram body, make the formals visible. Note
166 -- that simply placing the subprogram on the scope stack is not
167 -- sufficient: the formals must become the current entities for
170 procedure Make_Inequality_Operator (S : Entity_Id);
171 -- Create the declaration for an inequality operator that is implicitly
172 -- created by a user-defined equality operator that yields a boolean.
174 procedure May_Need_Actuals (Fun : Entity_Id);
175 -- Flag functions that can be called without parameters, i.e. those that
176 -- have no parameters, or those for which defaults exist for all parameters
178 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
179 -- Formal_Id is an formal parameter entity. This procedure deals with
180 -- setting the proper validity status for this entity, which depends
181 -- on the kind of parameter and the validity checking mode.
183 ---------------------------------------------
184 -- Analyze_Abstract_Subprogram_Declaration --
185 ---------------------------------------------
187 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
188 Designator : constant Entity_Id := Analyze_Spec (Specification (N));
189 Scop : constant Entity_Id := Current_Scope;
192 Generate_Definition (Designator);
193 Set_Is_Abstract (Designator);
194 New_Overloaded_Entity (Designator);
195 Check_Delayed_Subprogram (Designator);
197 Set_Is_Pure (Designator,
198 Is_Pure (Scop) and then Is_Library_Level_Entity (Designator));
199 Set_Is_Remote_Call_Interface (
200 Designator, Is_Remote_Call_Interface (Scop));
201 Set_Is_Remote_Types (Designator, Is_Remote_Types (Scop));
203 if Ekind (Scope (Designator)) = E_Protected_Type then
205 ("abstract subprogram not allowed in protected type", N);
207 end Analyze_Abstract_Subprogram_Declaration;
209 ----------------------------
210 -- Analyze_Function_Call --
211 ----------------------------
213 procedure Analyze_Function_Call (N : Node_Id) is
214 P : constant Node_Id := Name (N);
215 L : constant List_Id := Parameter_Associations (N);
221 -- If error analyzing name, then set Any_Type as result type and return
223 if Etype (P) = Any_Type then
224 Set_Etype (N, Any_Type);
228 -- Otherwise analyze the parameters
233 while Present (Actual) loop
235 Check_Parameterless_Call (Actual);
242 end Analyze_Function_Call;
244 -------------------------------------
245 -- Analyze_Generic_Subprogram_Body --
246 -------------------------------------
248 procedure Analyze_Generic_Subprogram_Body
252 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
254 Kind : constant Entity_Kind := Ekind (Gen_Id);
259 -- Copy body and disable expansion while analyzing the generic
260 -- For a stub, do not copy the stub (which would load the proper body),
261 -- this will be done when the proper body is analyzed.
263 if Nkind (N) /= N_Subprogram_Body_Stub then
264 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
269 Spec := Specification (N);
271 -- Within the body of the generic, the subprogram is callable, and
272 -- behaves like the corresponding non-generic unit.
274 Nam := Defining_Entity (Spec);
276 if Kind = E_Generic_Procedure
277 and then Nkind (Spec) /= N_Procedure_Specification
279 Error_Msg_N ("invalid body for generic procedure ", Nam);
282 elsif Kind = E_Generic_Function
283 and then Nkind (Spec) /= N_Function_Specification
285 Error_Msg_N ("invalid body for generic function ", Nam);
289 Set_Corresponding_Body (Gen_Decl, Nam);
291 if Has_Completion (Gen_Id)
292 and then Nkind (Parent (N)) /= N_Subunit
294 Error_Msg_N ("duplicate generic body", N);
297 Set_Has_Completion (Gen_Id);
300 if Nkind (N) = N_Subprogram_Body_Stub then
301 Set_Ekind (Defining_Entity (Specification (N)), Kind);
303 Set_Corresponding_Spec (N, Gen_Id);
306 if Nkind (Parent (N)) = N_Compilation_Unit then
307 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
310 -- Make generic parameters immediately visible in the body. They are
311 -- needed to process the formals declarations. Then make the formals
312 -- visible in a separate step.
318 First_Ent : Entity_Id;
321 First_Ent := First_Entity (Gen_Id);
324 while Present (E) and then not Is_Formal (E) loop
329 Set_Use (Generic_Formal_Declarations (Gen_Decl));
331 -- Now generic formals are visible, and the specification can be
332 -- analyzed, for subsequent conformance check.
334 Nam := Analyze_Spec (Spec);
336 if Nkind (N) = N_Subprogram_Body_Stub then
338 -- Nothing to do if no body to process
340 Set_Ekind (Nam, Kind);
347 -- E is the first formal parameter, which must be the first
348 -- entity in the subprogram body.
350 Set_First_Entity (Gen_Id, E);
352 -- Now make formal parameters visible
354 while Present (E) loop
360 -- Visible generic entity is callable within its own body.
362 Set_Ekind (Gen_Id, Ekind (Nam));
363 Set_Convention (Nam, Convention (Gen_Id));
364 Set_Scope (Nam, Scope (Gen_Id));
365 Check_Fully_Conformant (Nam, Gen_Id, Nam);
367 -- If this is a compilation unit, it must be made visible
368 -- explicitly, because the compilation of the declaration,
369 -- unlike other library unit declarations, does not. If it
370 -- is not a unit, the following is redundant but harmless.
372 Set_Is_Immediately_Visible (Gen_Id);
374 Set_Actual_Subtypes (N, Current_Scope);
375 Analyze_Declarations (Declarations (N));
377 Analyze (Handled_Statement_Sequence (N));
379 Save_Global_References (Original_Node (N));
381 -- Prior to exiting the scope, include generic formals again
382 -- (if any are present) in the set of local entities.
384 if Present (First_Ent) then
385 Set_First_Entity (Gen_Id, First_Ent);
391 Check_Subprogram_Order (N);
393 -- Outside of its body, unit is generic again.
395 Set_Ekind (Gen_Id, Kind);
396 Set_Ekind (Nam, E_Subprogram_Body);
397 Generate_Reference (Gen_Id, Nam, 'b');
398 Style.Check_Identifier (Nam, Gen_Id);
401 end Analyze_Generic_Subprogram_Body;
403 -----------------------------
404 -- Analyze_Operator_Symbol --
405 -----------------------------
407 -- An operator symbol such as "+" or "and" may appear in context where
408 -- the literal denotes an entity name, such as "+"(x, y) or in a
409 -- context when it is just a string, as in (conjunction = "or"). In
410 -- these cases the parser generates this node, and the semantics does
411 -- the disambiguation. Other such case are actuals in an instantiation,
412 -- the generic unit in an instantiation, and pragma arguments.
414 procedure Analyze_Operator_Symbol (N : Node_Id) is
415 Par : constant Node_Id := Parent (N);
418 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
419 or else Nkind (Par) = N_Function_Instantiation
420 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
421 or else (Nkind (Par) = N_Pragma_Argument_Association
422 and then not Is_Pragma_String_Literal (Par))
423 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
424 or else (Nkind (Par) = N_Attribute_Reference
425 and then Attribute_Name (Par) /= Name_Value)
427 Find_Direct_Name (N);
430 Change_Operator_Symbol_To_String_Literal (N);
433 end Analyze_Operator_Symbol;
435 -----------------------------------
436 -- Analyze_Parameter_Association --
437 -----------------------------------
439 procedure Analyze_Parameter_Association (N : Node_Id) is
441 Analyze (Explicit_Actual_Parameter (N));
442 end Analyze_Parameter_Association;
444 ----------------------------
445 -- Analyze_Procedure_Call --
446 ----------------------------
448 procedure Analyze_Procedure_Call (N : Node_Id) is
449 Loc : constant Source_Ptr := Sloc (N);
450 P : constant Node_Id := Name (N);
451 Actuals : constant List_Id := Parameter_Associations (N);
455 procedure Analyze_Call_And_Resolve;
456 -- Do Analyze and Resolve calls for procedure call
458 procedure Analyze_Call_And_Resolve is
460 if Nkind (N) = N_Procedure_Call_Statement then
462 Resolve (N, Standard_Void_Type);
466 end Analyze_Call_And_Resolve;
468 -- Start of processing for Analyze_Procedure_Call
471 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
472 -- a procedure call or an entry call. The prefix may denote an access
473 -- to subprogram type, in which case an implicit dereference applies.
474 -- If the prefix is an indexed component (without implicit defererence)
475 -- then the construct denotes a call to a member of an entire family.
476 -- If the prefix is a simple name, it may still denote a call to a
477 -- parameterless member of an entry family. Resolution of these various
478 -- interpretations is delicate.
482 -- If error analyzing prefix, then set Any_Type as result and return
484 if Etype (P) = Any_Type then
485 Set_Etype (N, Any_Type);
489 -- Otherwise analyze the parameters
491 if Present (Actuals) then
492 Actual := First (Actuals);
494 while Present (Actual) loop
496 Check_Parameterless_Call (Actual);
501 -- Special processing for Elab_Spec and Elab_Body calls
503 if Nkind (P) = N_Attribute_Reference
504 and then (Attribute_Name (P) = Name_Elab_Spec
505 or else Attribute_Name (P) = Name_Elab_Body)
507 if Present (Actuals) then
509 ("no parameters allowed for this call", First (Actuals));
513 Set_Etype (N, Standard_Void_Type);
516 elsif Is_Entity_Name (P)
517 and then Is_Record_Type (Etype (Entity (P)))
518 and then Remote_AST_I_Dereference (P)
522 elsif Is_Entity_Name (P)
523 and then Ekind (Entity (P)) /= E_Entry_Family
525 if Is_Access_Type (Etype (P))
526 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
527 and then No (Actuals)
528 and then Comes_From_Source (N)
530 Error_Msg_N ("missing explicit dereference in call", N);
533 Analyze_Call_And_Resolve;
535 -- If the prefix is the simple name of an entry family, this is
536 -- a parameterless call from within the task body itself.
538 elsif Is_Entity_Name (P)
539 and then Nkind (P) = N_Identifier
540 and then Ekind (Entity (P)) = E_Entry_Family
541 and then Present (Actuals)
542 and then No (Next (First (Actuals)))
544 -- Can be call to parameterless entry family. What appears to be
545 -- the sole argument is in fact the entry index. Rewrite prefix
546 -- of node accordingly. Source representation is unchanged by this
550 Make_Indexed_Component (Loc,
552 Make_Selected_Component (Loc,
553 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
554 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
555 Expressions => Actuals);
557 Set_Etype (New_N, Standard_Void_Type);
558 Set_Parameter_Associations (N, No_List);
559 Analyze_Call_And_Resolve;
561 elsif Nkind (P) = N_Explicit_Dereference then
562 if Ekind (Etype (P)) = E_Subprogram_Type then
563 Analyze_Call_And_Resolve;
565 Error_Msg_N ("expect access to procedure in call", P);
568 -- The name can be a selected component or an indexed component
569 -- that yields an access to subprogram. Such a prefix is legal if
570 -- the call has parameter associations.
572 elsif Is_Access_Type (Etype (P))
573 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
575 if Present (Actuals) then
576 Analyze_Call_And_Resolve;
578 Error_Msg_N ("missing explicit dereference in call ", N);
581 -- If not an access to subprogram, then the prefix must resolve to
582 -- the name of an entry, entry family, or protected operation.
584 -- For the case of a simple entry call, P is a selected component
585 -- where the prefix is the task and the selector name is the entry.
586 -- A call to a protected procedure will have the same syntax. If
587 -- the protected object contains overloaded operations, the entity
588 -- may appear as a function, the context will select the operation
589 -- whose type is Void.
591 elsif Nkind (P) = N_Selected_Component
592 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
594 Ekind (Entity (Selector_Name (P))) = E_Procedure
596 Ekind (Entity (Selector_Name (P))) = E_Function)
598 Analyze_Call_And_Resolve;
600 elsif Nkind (P) = N_Selected_Component
601 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
602 and then Present (Actuals)
603 and then No (Next (First (Actuals)))
605 -- Can be call to parameterless entry family. What appears to be
606 -- the sole argument is in fact the entry index. Rewrite prefix
607 -- of node accordingly. Source representation is unchanged by this
611 Make_Indexed_Component (Loc,
612 Prefix => New_Copy (P),
613 Expressions => Actuals);
615 Set_Etype (New_N, Standard_Void_Type);
616 Set_Parameter_Associations (N, No_List);
617 Analyze_Call_And_Resolve;
619 -- For the case of a reference to an element of an entry family, P is
620 -- an indexed component whose prefix is a selected component (task and
621 -- entry family), and whose index is the entry family index.
623 elsif Nkind (P) = N_Indexed_Component
624 and then Nkind (Prefix (P)) = N_Selected_Component
625 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
627 Analyze_Call_And_Resolve;
629 -- If the prefix is the name of an entry family, it is a call from
630 -- within the task body itself.
632 elsif Nkind (P) = N_Indexed_Component
633 and then Nkind (Prefix (P)) = N_Identifier
634 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
637 Make_Selected_Component (Loc,
638 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
639 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
640 Rewrite (Prefix (P), New_N);
642 Analyze_Call_And_Resolve;
644 -- Anything else is an error.
647 Error_Msg_N ("Invalid procedure or entry call", N);
649 end Analyze_Procedure_Call;
651 ------------------------------
652 -- Analyze_Return_Statement --
653 ------------------------------
655 procedure Analyze_Return_Statement (N : Node_Id) is
656 Loc : constant Source_Ptr := Sloc (N);
658 Scope_Id : Entity_Id;
663 -- Find subprogram or accept statement enclosing the return statement
666 for J in reverse 0 .. Scope_Stack.Last loop
667 Scope_Id := Scope_Stack.Table (J).Entity;
668 exit when Ekind (Scope_Id) /= E_Block and then
669 Ekind (Scope_Id) /= E_Loop;
672 pragma Assert (Present (Scope_Id));
674 Kind := Ekind (Scope_Id);
675 Expr := Expression (N);
677 if Kind /= E_Function
678 and then Kind /= E_Generic_Function
679 and then Kind /= E_Procedure
680 and then Kind /= E_Generic_Procedure
681 and then Kind /= E_Entry
682 and then Kind /= E_Entry_Family
684 Error_Msg_N ("illegal context for return statement", N);
686 elsif Present (Expr) then
687 if Kind = E_Function or else Kind = E_Generic_Function then
688 Set_Return_Present (Scope_Id);
689 R_Type := Etype (Scope_Id);
690 Set_Return_Type (N, R_Type);
691 Analyze_And_Resolve (Expr, R_Type);
693 if (Is_Class_Wide_Type (Etype (Expr))
694 or else Is_Dynamically_Tagged (Expr))
695 and then not Is_Class_Wide_Type (R_Type)
698 ("dynamically tagged expression not allowed!", Expr);
701 Apply_Constraint_Check (Expr, R_Type);
703 -- ??? A real run-time accessibility check is needed
704 -- in cases involving dereferences of access parameters.
705 -- For now we just check the static cases.
707 if Is_Return_By_Reference_Type (Etype (Scope_Id))
708 and then Object_Access_Level (Expr)
709 > Subprogram_Access_Level (Scope_Id)
712 Make_Raise_Program_Error (Loc,
713 Reason => PE_Accessibility_Check_Failed));
717 ("cannot return a local value by reference?", N);
719 ("& will be raised at run time?!",
720 N, Standard_Program_Error);
723 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
724 Error_Msg_N ("procedure cannot return value (use function)", N);
727 Error_Msg_N ("accept statement cannot return value", N);
730 -- No expression present
733 if Kind = E_Function or Kind = E_Generic_Function then
734 Error_Msg_N ("missing expression in return from function", N);
737 if (Ekind (Scope_Id) = E_Procedure
738 or else Ekind (Scope_Id) = E_Generic_Procedure)
739 and then No_Return (Scope_Id)
742 ("RETURN statement not allowed (No_Return)", N);
746 Check_Unreachable_Code (N);
747 end Analyze_Return_Statement;
753 function Analyze_Spec (N : Node_Id) return Entity_Id is
754 Designator : constant Entity_Id := Defining_Entity (N);
755 Formals : constant List_Id := Parameter_Specifications (N);
759 Generate_Definition (Designator);
761 if Nkind (N) = N_Function_Specification then
762 Set_Ekind (Designator, E_Function);
763 Set_Mechanism (Designator, Default_Mechanism);
765 if Subtype_Mark (N) /= Error then
766 Find_Type (Subtype_Mark (N));
767 Typ := Entity (Subtype_Mark (N));
768 Set_Etype (Designator, Typ);
770 if (Ekind (Typ) = E_Incomplete_Type
771 or else (Is_Class_Wide_Type (Typ)
773 Ekind (Root_Type (Typ)) = E_Incomplete_Type))
776 ("invalid use of incomplete type", Subtype_Mark (N));
780 Set_Etype (Designator, Any_Type);
784 Set_Ekind (Designator, E_Procedure);
785 Set_Etype (Designator, Standard_Void_Type);
788 if Present (Formals) then
789 Set_Scope (Designator, Current_Scope);
790 New_Scope (Designator);
791 Process_Formals (Formals, N);
795 if Nkind (N) = N_Function_Specification then
796 if Nkind (Designator) = N_Defining_Operator_Symbol then
797 Valid_Operator_Definition (Designator);
800 May_Need_Actuals (Designator);
802 if Is_Abstract (Etype (Designator))
803 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
806 ("function that returns abstract type must be abstract", N);
813 -----------------------------
814 -- Analyze_Subprogram_Body --
815 -----------------------------
817 -- This procedure is called for regular subprogram bodies, generic bodies,
818 -- and for subprogram stubs of both kinds. In the case of stubs, only the
819 -- specification matters, and is used to create a proper declaration for
820 -- the subprogram, or to perform conformance checks.
822 procedure Analyze_Subprogram_Body (N : Node_Id) is
823 Loc : constant Source_Ptr := Sloc (N);
824 Body_Spec : constant Node_Id := Specification (N);
825 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
826 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
830 Spec_Decl : Node_Id := Empty;
831 Last_Formal : Entity_Id := Empty;
832 Conformant : Boolean;
833 Missing_Ret : Boolean;
834 Body_Deleted : Boolean := False;
839 Write_Str ("==== Compiling subprogram body ");
840 Write_Name (Chars (Body_Id));
841 Write_Str (" from ");
842 Write_Location (Loc);
846 Trace_Scope (N, Body_Id, " Analyze subprogram");
848 -- Generic subprograms are handled separately. They always have
849 -- a generic specification. Determine whether current scope has
850 -- a previous declaration.
852 -- If the subprogram body is defined within an instance of the
853 -- same name, the instance appears as a package renaming, and
854 -- will be hidden within the subprogram.
857 and then not Is_Overloadable (Prev_Id)
858 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
859 or else Comes_From_Source (Prev_Id))
861 if Ekind (Prev_Id) = E_Generic_Procedure
862 or else Ekind (Prev_Id) = E_Generic_Function
865 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
866 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
868 Analyze_Generic_Subprogram_Body (N, Spec_Id);
872 -- Previous entity conflicts with subprogram name.
873 -- Attempting to enter name will post error.
875 Enter_Name (Body_Id);
879 -- Non-generic case, find the subprogram declaration, if one was
880 -- seen, or enter new overloaded entity in the current scope.
881 -- If the current_entity is the body_id itself, the unit is being
882 -- analyzed as part of the context of one of its subunits. No need
883 -- to redo the analysis.
885 elsif Prev_Id = Body_Id
886 and then Has_Completion (Body_Id)
891 Body_Id := Analyze_Spec (Body_Spec);
893 if Nkind (N) = N_Subprogram_Body_Stub
894 or else No (Corresponding_Spec (N))
896 Spec_Id := Find_Corresponding_Spec (N);
898 -- If this is a duplicate body, no point in analyzing it
900 if Error_Posted (N) then
904 -- A subprogram body should cause freezing of its own
905 -- declaration, but if there was no previous explicit
906 -- declaration, then the subprogram will get frozen too
907 -- late (there may be code within the body that depends
908 -- on the subprogram having been frozen, such as uses of
909 -- extra formals), so we force it to be frozen here.
910 -- Same holds if the body and the spec are compilation units.
913 Freeze_Before (N, Body_Id);
915 elsif Nkind (Parent (N)) = N_Compilation_Unit then
916 Freeze_Before (N, Spec_Id);
919 Spec_Id := Corresponding_Spec (N);
923 -- Do not inline any subprogram that contains nested subprograms,
924 -- since the backend inlining circuit seems to generate uninitialized
925 -- references in this case. We know this happens in the case of front
926 -- end ZCX support, but it also appears it can happen in other cases
927 -- as well. The backend often rejects attempts to inline in the case
928 -- of nested procedures anyway, so little if anything is lost by this.
930 -- Do not do this test if errors have been detected, because in some
931 -- error cases, this code blows up, and we don't need it anyway if
932 -- there have been errors, since we won't get to the linker anyway.
934 if Serious_Errors_Detected = 0 then
937 P_Ent := Scope (P_Ent);
938 exit when No (P_Ent) or else P_Ent = Standard_Standard;
940 if Is_Subprogram (P_Ent) and then Is_Inlined (P_Ent) then
941 Set_Is_Inlined (P_Ent, False);
943 if Comes_From_Source (P_Ent)
944 and then Ineffective_Inline_Warnings
945 and then Has_Pragma_Inline (P_Ent)
948 ("?pragma Inline for & ignored (has nested subprogram)",
949 Get_Rep_Pragma (P_Ent, Name_Inline), P_Ent);
955 -- Case of fully private operation in the body of the protected type.
956 -- We must create a declaration for the subprogram, in order to attach
957 -- the protected subprogram that will be used in internal calls.
960 and then Comes_From_Source (N)
961 and then Is_Protected_Type (Current_Scope)
970 Formal := First_Formal (Body_Id);
972 -- The protected operation always has at least one formal,
973 -- namely the object itself, but it is only placed in the
974 -- parameter list if expansion is enabled.
977 or else Expander_Active
985 while Present (Formal) loop
987 (Make_Parameter_Specification (Loc,
988 Defining_Identifier =>
989 Make_Defining_Identifier (Sloc (Formal),
990 Chars => Chars (Formal)),
991 In_Present => In_Present (Parent (Formal)),
992 Out_Present => Out_Present (Parent (Formal)),
994 New_Reference_To (Etype (Formal), Loc),
996 New_Copy_Tree (Expression (Parent (Formal)))),
999 Next_Formal (Formal);
1002 if Nkind (Body_Spec) = N_Procedure_Specification then
1004 Make_Procedure_Specification (Loc,
1005 Defining_Unit_Name =>
1006 Make_Defining_Identifier (Sloc (Body_Id),
1007 Chars => Chars (Body_Id)),
1008 Parameter_Specifications => Plist);
1011 Make_Function_Specification (Loc,
1012 Defining_Unit_Name =>
1013 Make_Defining_Identifier (Sloc (Body_Id),
1014 Chars => Chars (Body_Id)),
1015 Parameter_Specifications => Plist,
1016 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1020 Make_Subprogram_Declaration (Loc,
1021 Specification => New_Spec);
1022 Insert_Before (N, Decl);
1024 Spec_Id := Defining_Unit_Name (New_Spec);
1025 Set_Has_Completion (Spec_Id);
1026 Set_Convention (Spec_Id, Convention_Protected);
1029 elsif Present (Spec_Id) then
1030 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1033 -- Place subprogram on scope stack, and make formals visible. If there
1034 -- is a spec, the visible entity remains that of the spec.
1036 if Present (Spec_Id) then
1037 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1038 Style.Check_Identifier (Body_Id, Spec_Id);
1040 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1041 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1043 if Is_Abstract (Spec_Id) then
1044 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1047 Set_Convention (Body_Id, Convention (Spec_Id));
1048 Set_Has_Completion (Spec_Id);
1050 if Is_Protected_Type (Scope (Spec_Id)) then
1051 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1054 -- If this is a body generated for a renaming, do not check for
1055 -- full conformance. The check is redundant, because the spec of
1056 -- the body is a copy of the spec in the renaming declaration,
1057 -- and the test can lead to spurious errors on nested defaults.
1059 if Present (Spec_Decl)
1060 and then Nkind (Original_Node (Spec_Decl)) =
1061 N_Subprogram_Renaming_Declaration
1062 and then not Comes_From_Source (N)
1068 Fully_Conformant, True, Conformant, Body_Id);
1071 -- If the body is not fully conformant, we have to decide if we
1072 -- should analyze it or not. If it has a really messed up profile
1073 -- then we probably should not analyze it, since we will get too
1074 -- many bogus messages.
1076 -- Our decision is to go ahead in the non-fully conformant case
1077 -- only if it is at least mode conformant with the spec. Note
1078 -- that the call to Check_Fully_Conformant has issued the proper
1079 -- error messages to complain about the lack of conformance.
1082 and then not Mode_Conformant (Body_Id, Spec_Id)
1088 -- Generate references from body formals to spec formals
1089 -- and also set the Spec_Entity fields for all formals. We
1090 -- do not set this reference count as a reference for the
1091 -- purposes of identifying unreferenced formals however.
1093 if Spec_Id /= Body_Id then
1099 Fs := First_Formal (Spec_Id);
1100 Fb := First_Formal (Body_Id);
1101 while Present (Fs) loop
1102 Generate_Reference (Fs, Fb, 'b');
1103 Style.Check_Identifier (Fb, Fs);
1104 Set_Spec_Entity (Fb, Fs);
1105 Set_Referenced (Fs, False);
1112 if Nkind (N) /= N_Subprogram_Body_Stub then
1113 Set_Corresponding_Spec (N, Spec_Id);
1114 Install_Formals (Spec_Id);
1115 Last_Formal := Last_Entity (Spec_Id);
1116 New_Scope (Spec_Id);
1118 -- Make sure that the subprogram is immediately visible. For
1119 -- child units that have no separate spec this is indispensable.
1120 -- Otherwise it is safe albeit redundant.
1122 Set_Is_Immediately_Visible (Spec_Id);
1125 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1126 Set_Ekind (Body_Id, E_Subprogram_Body);
1127 Set_Scope (Body_Id, Scope (Spec_Id));
1129 -- Case of subprogram body with no previous spec
1133 and then Comes_From_Source (Body_Id)
1134 and then not Suppress_Style_Checks (Body_Id)
1135 and then not In_Instance
1137 Style.Body_With_No_Spec (N);
1140 New_Overloaded_Entity (Body_Id);
1142 if Nkind (N) /= N_Subprogram_Body_Stub then
1143 Set_Acts_As_Spec (N);
1144 Generate_Definition (Body_Id);
1145 Install_Formals (Body_Id);
1146 New_Scope (Body_Id);
1150 -- If this is the proper body of a stub, we must verify that the stub
1151 -- conforms to the body, and to the previous spec if one was present.
1152 -- we know already that the body conforms to that spec. This test is
1153 -- only required for subprograms that come from source.
1155 if Nkind (Parent (N)) = N_Subunit
1156 and then Comes_From_Source (N)
1157 and then not Error_Posted (Body_Id)
1160 Conformant : Boolean := False;
1161 Old_Id : Entity_Id :=
1163 (Specification (Corresponding_Stub (Parent (N))));
1166 if No (Spec_Id) then
1167 Check_Fully_Conformant (Body_Id, Old_Id);
1171 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1173 if not Conformant then
1175 -- The stub was taken to be a new declaration. Indicate
1176 -- that it lacks a body.
1178 Set_Has_Completion (Old_Id, False);
1184 Set_Has_Completion (Body_Id);
1185 Check_Eliminated (Body_Id);
1187 if Nkind (N) = N_Subprogram_Body_Stub then
1190 elsif Present (Spec_Id)
1191 and then Expander_Active
1192 and then (Is_Always_Inlined (Spec_Id)
1193 or else (Has_Pragma_Inline (Spec_Id)
1195 (Front_End_Inlining or else No_Run_Time)))
1197 if Build_Body_To_Inline (N, Spec_Id, Copy_Separate_Tree (N)) then
1202 -- Now we can go on to analyze the body
1204 HSS := Handled_Statement_Sequence (N);
1205 Set_Actual_Subtypes (N, Current_Scope);
1206 Analyze_Declarations (Declarations (N));
1209 Process_End_Label (HSS, 't', Current_Scope);
1211 Check_Subprogram_Order (N);
1213 -- If we have a separate spec, then the analysis of the declarations
1214 -- caused the entities in the body to be chained to the spec id, but
1215 -- we want them chained to the body id. Only the formal parameters
1216 -- end up chained to the spec id in this case.
1218 if Present (Spec_Id) then
1220 -- If a parent unit is categorized, the context of a subunit
1221 -- must conform to the categorization. Conversely, if a child
1222 -- unit is categorized, the parents themselves must conform.
1224 if Nkind (Parent (N)) = N_Subunit then
1225 Validate_Categorization_Dependency (N, Spec_Id);
1227 elsif Is_Child_Unit (Spec_Id) then
1228 Validate_Categorization_Dependency
1229 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1232 if Present (Last_Formal) then
1234 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1235 Set_Next_Entity (Last_Formal, Empty);
1236 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1237 Set_Last_Entity (Spec_Id, Last_Formal);
1240 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1241 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1242 Set_First_Entity (Spec_Id, Empty);
1243 Set_Last_Entity (Spec_Id, Empty);
1247 -- If function, check return statements
1249 if Nkind (Body_Spec) = N_Function_Specification then
1254 if Present (Spec_Id) then
1260 if Return_Present (Id) then
1261 Check_Returns (HSS, 'F', Missing_Ret);
1264 Set_Has_Missing_Return (Id);
1267 elsif not Is_Machine_Code_Subprogram (Id)
1268 and then not Body_Deleted
1270 Error_Msg_N ("missing RETURN statement in function body", N);
1274 -- If procedure with No_Return, check returns
1276 elsif Nkind (Body_Spec) = N_Procedure_Specification
1277 and then Present (Spec_Id)
1278 and then No_Return (Spec_Id)
1280 Check_Returns (HSS, 'P', Missing_Ret);
1283 -- Don't worry about checking for variables that are never modified
1284 -- if the first statement of the body is a raise statement, since
1285 -- we assume this is some kind of stub. We ignore a label generated
1286 -- by the exception stuff for the purpose of this test.
1289 Stm : Node_Id := First (Statements (HSS));
1292 if Nkind (Stm) = N_Label then
1296 if Nkind (Original_Node (Stm)) = N_Raise_Statement then
1301 -- Check for variables that are never modified
1307 -- If there is a separate spec, then transfer Not_Source_Assigned
1308 -- flags from out parameters to the corresponding entities in the
1309 -- body. The reason we do that is we want to post error flags on
1310 -- the body entities, not the spec entities.
1312 if Present (Spec_Id) then
1313 E1 := First_Entity (Spec_Id);
1315 while Present (E1) loop
1316 if Ekind (E1) = E_Out_Parameter then
1317 E2 := First_Entity (Body_Id);
1320 -- If no matching body entity, then we already had
1321 -- a detected error of some kind, so just forget
1322 -- about worrying about these warnings.
1328 exit when Chars (E1) = Chars (E2);
1332 Set_Not_Source_Assigned (E2, Not_Source_Assigned (E1));
1339 -- Check references in body unless it was deleted. Note that the
1340 -- check of Body_Deleted here is not just for efficiency, it is
1341 -- necessary to avoid junk warnings on formal parameters.
1343 if not Body_Deleted then
1344 Check_References (Body_Id);
1347 end Analyze_Subprogram_Body;
1349 ------------------------------------
1350 -- Analyze_Subprogram_Declaration --
1351 ------------------------------------
1353 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1354 Designator : constant Entity_Id := Analyze_Spec (Specification (N));
1355 Scop : constant Entity_Id := Current_Scope;
1357 -- Start of processing for Analyze_Subprogram_Declaration
1360 Generate_Definition (Designator);
1362 -- Check for RCI unit subprogram declarations against in-lined
1363 -- subprograms and subprograms having access parameter or limited
1364 -- parameter without Read and Write (RM E.2.3(12-13)).
1366 Validate_RCI_Subprogram_Declaration (N);
1370 Defining_Entity (N),
1371 " Analyze subprogram spec. ");
1373 if Debug_Flag_C then
1374 Write_Str ("==== Compiling subprogram spec ");
1375 Write_Name (Chars (Designator));
1376 Write_Str (" from ");
1377 Write_Location (Sloc (N));
1381 New_Overloaded_Entity (Designator);
1382 Check_Delayed_Subprogram (Designator);
1383 Set_Suppress_Elaboration_Checks
1384 (Designator, Elaboration_Checks_Suppressed (Designator));
1386 if Scop /= Standard_Standard
1387 and then not Is_Child_Unit (Designator)
1389 Set_Is_Pure (Designator,
1390 Is_Pure (Scop) and then Is_Library_Level_Entity (Designator));
1391 Set_Is_Remote_Call_Interface (
1392 Designator, Is_Remote_Call_Interface (Scop));
1393 Set_Is_Remote_Types (Designator, Is_Remote_Types (Scop));
1396 -- For a compilation unit, check for library-unit pragmas.
1398 New_Scope (Designator);
1399 Set_Categorization_From_Pragmas (N);
1400 Validate_Categorization_Dependency (N, Designator);
1404 -- For a compilation unit, set body required. This flag will only be
1405 -- reset if a valid Import or Interface pragma is processed later on.
1407 if Nkind (Parent (N)) = N_Compilation_Unit then
1408 Set_Body_Required (Parent (N), True);
1411 Check_Eliminated (Designator);
1412 end Analyze_Subprogram_Declaration;
1414 --------------------------
1415 -- Build_Body_To_Inline --
1416 --------------------------
1418 function Build_Body_To_Inline
1421 Orig_Body : Node_Id) return Boolean
1423 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1424 Original_Body : Node_Id;
1425 Body_To_Analyze : Node_Id;
1426 Max_Size : constant := 10;
1427 Stat_Count : Integer := 0;
1429 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1430 -- Check for declarations that make inlining not worthwhile.
1432 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1433 -- Check for statements that make inlining not worthwhile: any
1434 -- tasking statement, nested at any level. Keep track of total
1435 -- number of elementary statements, as a measure of acceptable size.
1437 function Has_Pending_Instantiation return Boolean;
1438 -- If some enclosing body contains instantiations that appear before
1439 -- the corresponding generic body, the enclosing body has a freeze node
1440 -- so that it can be elaborated after the generic itself. This might
1441 -- conflict with subsequent inlinings, so that it is unsafe to try to
1442 -- inline in such a case.
1448 procedure Cannot_Inline (Msg : String; N : Node_Id);
1449 -- If subprogram has pragma Inline_Always, it is an error if
1450 -- it cannot be inlined. Otherwise, emit a warning.
1452 procedure Cannot_Inline (Msg : String; N : Node_Id) is
1454 if Is_Always_Inlined (Subp) then
1455 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
1457 elsif Ineffective_Inline_Warnings then
1458 Error_Msg_NE (Msg, N, Subp);
1462 ------------------------------
1463 -- Has_Excluded_Declaration --
1464 ------------------------------
1466 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1472 while Present (D) loop
1473 if Nkind (D) = N_Function_Instantiation
1474 or else Nkind (D) = N_Protected_Type_Declaration
1475 or else Nkind (D) = N_Package_Declaration
1476 or else Nkind (D) = N_Package_Instantiation
1477 or else Nkind (D) = N_Subprogram_Body
1478 or else Nkind (D) = N_Procedure_Instantiation
1479 or else Nkind (D) = N_Task_Type_Declaration
1482 ("\declaration prevents front-end inlining of&?", D);
1491 end Has_Excluded_Declaration;
1493 ----------------------------
1494 -- Has_Excluded_Statement --
1495 ----------------------------
1497 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1504 while Present (S) loop
1505 Stat_Count := Stat_Count + 1;
1507 if Nkind (S) = N_Abort_Statement
1508 or else Nkind (S) = N_Asynchronous_Select
1509 or else Nkind (S) = N_Conditional_Entry_Call
1510 or else Nkind (S) = N_Delay_Relative_Statement
1511 or else Nkind (S) = N_Delay_Until_Statement
1512 or else Nkind (S) = N_Selective_Accept
1513 or else Nkind (S) = N_Timed_Entry_Call
1516 ("\statement prevents front-end inlining of&?", S);
1519 elsif Nkind (S) = N_Block_Statement then
1520 if Present (Declarations (S))
1521 and then Has_Excluded_Declaration (Declarations (S))
1525 elsif Present (Handled_Statement_Sequence (S))
1528 (Exception_Handlers (Handled_Statement_Sequence (S)))
1530 Has_Excluded_Statement
1531 (Statements (Handled_Statement_Sequence (S))))
1536 elsif Nkind (S) = N_Case_Statement then
1537 E := First (Alternatives (S));
1539 while Present (E) loop
1540 if Has_Excluded_Statement (Statements (E)) then
1547 elsif Nkind (S) = N_If_Statement then
1548 if Has_Excluded_Statement (Then_Statements (S)) then
1552 if Present (Elsif_Parts (S)) then
1553 E := First (Elsif_Parts (S));
1555 while Present (E) loop
1556 if Has_Excluded_Statement (Then_Statements (E)) then
1563 if Present (Else_Statements (S))
1564 and then Has_Excluded_Statement (Else_Statements (S))
1569 elsif Nkind (S) = N_Loop_Statement
1570 and then Has_Excluded_Statement (Statements (S))
1579 end Has_Excluded_Statement;
1581 -------------------------------
1582 -- Has_Pending_Instantiation --
1583 -------------------------------
1585 function Has_Pending_Instantiation return Boolean is
1586 S : Entity_Id := Current_Scope;
1589 while Present (S) loop
1590 if Is_Compilation_Unit (S)
1591 or else Is_Child_Unit (S)
1594 elsif Ekind (S) = E_Package
1595 and then Has_Forward_Instantiation (S)
1604 end Has_Pending_Instantiation;
1606 -- Start of processing for Build_Body_To_Inline
1609 if Nkind (Decl) = N_Subprogram_Declaration
1610 and then Present (Body_To_Inline (Decl))
1612 return True; -- Done already.
1614 -- Functions that return unconstrained composite types will require
1615 -- secondary stack handling, and cannot currently be inlined.
1617 elsif Ekind (Subp) = E_Function
1618 and then not Is_Scalar_Type (Etype (Subp))
1619 and then not Is_Access_Type (Etype (Subp))
1620 and then not Is_Constrained (Etype (Subp))
1623 ("unconstrained return type prevents front-end inlining of&?", N);
1627 -- We need to capture references to the formals in order to substitute
1628 -- the actuals at the point of inlining, i.e. instantiation. To treat
1629 -- the formals as globals to the body to inline, we nest it within
1630 -- a dummy parameterless subprogram, declared within the real one.
1632 Original_Body := Orig_Body;
1634 -- Within an instance, the current tree is already the result of
1635 -- a generic copy, and not what we need for subsequent inlining.
1636 -- We create the required body by doing an instantiating copy, to
1637 -- obtain the proper partially analyzed tree.
1640 if No (Generic_Parent (Specification (N))) then
1643 elsif Is_Child_Unit (Scope (Current_Scope)) then
1646 elsif Scope (Current_Scope) = Cunit_Entity (Main_Unit) then
1648 -- compiling an instantiation. There is no point in generating
1649 -- bodies to inline, because they will not be used.
1656 (Generic_Parent (Specification (N)), Empty,
1657 Instantiating => True);
1661 Copy_Generic_Node (Original_Body, Empty,
1662 Instantiating => False);
1665 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1666 Set_Defining_Unit_Name (Specification (Original_Body),
1667 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('S')));
1668 Set_Corresponding_Spec (Original_Body, Empty);
1670 if Ekind (Subp) = E_Function then
1671 Set_Subtype_Mark (Specification (Original_Body),
1672 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1675 if Present (Declarations (Orig_Body))
1676 and then Has_Excluded_Declaration (Declarations (Orig_Body))
1681 if Present (Handled_Statement_Sequence (N)) then
1683 (Present (Exception_Handlers (Handled_Statement_Sequence (N))))
1685 Cannot_Inline ("handler prevents front-end inlining of&?",
1686 First (Exception_Handlers (Handled_Statement_Sequence (N))));
1689 Has_Excluded_Statement
1690 (Statements (Handled_Statement_Sequence (N)))
1696 -- We do not inline a subprogram that is too large, unless it is
1697 -- marked Inline_Always. This pragma does not suppress the other
1698 -- checks on inlining (forbidden declarations, handlers, etc).
1700 if Stat_Count > Max_Size
1701 and then not Is_Always_Inlined (Subp)
1703 Cannot_Inline ("body is too large for front-end inlining of&?", N);
1707 if Has_Pending_Instantiation then
1709 ("cannot inline& because of forward instance within enclosing body",
1714 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1716 -- Set return type of function, which is also global and does not need
1719 if Ekind (Subp) = E_Function then
1720 Set_Subtype_Mark (Specification (Body_To_Analyze),
1721 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1724 if No (Declarations (N)) then
1725 Set_Declarations (N, New_List (Body_To_Analyze));
1727 Append (Body_To_Analyze, Declarations (N));
1730 Expander_Mode_Save_And_Set (False);
1732 Analyze (Body_To_Analyze);
1733 New_Scope (Defining_Entity (Body_To_Analyze));
1734 Save_Global_References (Original_Body);
1736 Remove (Body_To_Analyze);
1738 Expander_Mode_Restore;
1739 Set_Body_To_Inline (Decl, Original_Body);
1740 Set_Is_Inlined (Subp);
1743 end Build_Body_To_Inline;
1745 -----------------------
1746 -- Check_Conformance --
1747 -----------------------
1749 procedure Check_Conformance
1750 (New_Id : Entity_Id;
1752 Ctype : Conformance_Type;
1754 Conforms : out Boolean;
1755 Err_Loc : Node_Id := Empty;
1756 Get_Inst : Boolean := False)
1758 Old_Type : constant Entity_Id := Etype (Old_Id);
1759 New_Type : constant Entity_Id := Etype (New_Id);
1760 Old_Formal : Entity_Id;
1761 New_Formal : Entity_Id;
1763 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
1764 -- Post error message for conformance error on given node.
1765 -- Two messages are output. The first points to the previous
1766 -- declaration with a general "no conformance" message.
1767 -- The second is the detailed reason, supplied as Msg. The
1768 -- parameter N provide information for a possible & insertion
1769 -- in the message, and also provides the location for posting
1770 -- the message in the absence of a specified Err_Loc location.
1772 -----------------------
1773 -- Conformance_Error --
1774 -----------------------
1776 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
1783 if No (Err_Loc) then
1789 Error_Msg_Sloc := Sloc (Old_Id);
1792 when Type_Conformant =>
1794 ("not type conformant with declaration#!", Enode);
1796 when Mode_Conformant =>
1798 ("not mode conformant with declaration#!", Enode);
1800 when Subtype_Conformant =>
1802 ("not subtype conformant with declaration#!", Enode);
1804 when Fully_Conformant =>
1806 ("not fully conformant with declaration#!", Enode);
1809 Error_Msg_NE (Msg, Enode, N);
1811 end Conformance_Error;
1813 -- Start of processing for Check_Conformance
1818 -- We need a special case for operators, since they don't
1819 -- appear explicitly.
1821 if Ctype = Type_Conformant then
1822 if Ekind (New_Id) = E_Operator
1823 and then Operator_Matches_Spec (New_Id, Old_Id)
1829 -- If both are functions/operators, check return types conform
1831 if Old_Type /= Standard_Void_Type
1832 and then New_Type /= Standard_Void_Type
1834 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
1835 Conformance_Error ("return type does not match!", New_Id);
1839 -- If either is a function/operator and the other isn't, error
1841 elsif Old_Type /= Standard_Void_Type
1842 or else New_Type /= Standard_Void_Type
1844 Conformance_Error ("functions can only match functions!", New_Id);
1848 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
1849 -- If this is a renaming as body, refine error message to indicate that
1850 -- the conflict is with the original declaration. If the entity is not
1851 -- frozen, the conventions don't have to match, the one of the renamed
1852 -- entity is inherited.
1854 if Ctype >= Subtype_Conformant then
1856 if Convention (Old_Id) /= Convention (New_Id) then
1858 if not Is_Frozen (New_Id) then
1861 elsif Present (Err_Loc)
1862 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
1863 and then Present (Corresponding_Spec (Err_Loc))
1865 Error_Msg_Name_1 := Chars (New_Id);
1867 Name_Ada + Convention_Id'Pos (Convention (New_Id));
1869 Conformance_Error ("prior declaration for% has convention %!");
1872 Conformance_Error ("calling conventions do not match!");
1877 elsif Is_Formal_Subprogram (Old_Id)
1878 or else Is_Formal_Subprogram (New_Id)
1880 Conformance_Error ("formal subprograms not allowed!");
1885 -- Deal with parameters
1887 -- Note: we use the entity information, rather than going directly
1888 -- to the specification in the tree. This is not only simpler, but
1889 -- absolutely necessary for some cases of conformance tests between
1890 -- operators, where the declaration tree simply does not exist!
1892 Old_Formal := First_Formal (Old_Id);
1893 New_Formal := First_Formal (New_Id);
1895 while Present (Old_Formal) and then Present (New_Formal) loop
1897 -- Types must always match. In the visible part of an instance,
1898 -- usual overloading rules for dispatching operations apply, and
1899 -- we check base types (not the actual subtypes).
1901 if In_Instance_Visible_Part
1902 and then Is_Dispatching_Operation (New_Id)
1904 if not Conforming_Types
1905 (Base_Type (Etype (Old_Formal)),
1906 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
1908 Conformance_Error ("type of & does not match!", New_Formal);
1912 elsif not Conforming_Types
1913 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
1915 Conformance_Error ("type of & does not match!", New_Formal);
1919 -- For mode conformance, mode must match
1921 if Ctype >= Mode_Conformant
1922 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
1924 Conformance_Error ("mode of & does not match!", New_Formal);
1928 -- Full conformance checks
1930 if Ctype = Fully_Conformant then
1934 if Chars (Old_Formal) /= Chars (New_Formal) then
1935 Conformance_Error ("name & does not match!", New_Formal);
1938 -- And default expressions for in parameters
1940 elsif Parameter_Mode (Old_Formal) = E_In_Parameter then
1942 NewD : constant Boolean :=
1943 Present (Default_Value (New_Formal));
1944 OldD : constant Boolean :=
1945 Present (Default_Value (Old_Formal));
1947 if NewD or OldD then
1949 -- The old default value has been analyzed and expanded,
1950 -- because the current full declaration will have frozen
1951 -- everything before. The new default values have not
1952 -- been expanded, so expand now to check conformance.
1956 Analyze_Default_Expression
1957 (Default_Value (New_Formal), Etype (New_Formal));
1961 if not (NewD and OldD)
1962 or else not Fully_Conformant_Expressions
1963 (Default_Value (Old_Formal),
1964 Default_Value (New_Formal))
1967 ("default expression for & does not match!",
1976 -- A couple of special checks for Ada 83 mode. These checks are
1977 -- skipped if either entity is an operator in package Standard.
1978 -- or if either old or new instance is not from the source program.
1981 and then Sloc (Old_Id) > Standard_Location
1982 and then Sloc (New_Id) > Standard_Location
1983 and then Comes_From_Source (Old_Id)
1984 and then Comes_From_Source (New_Id)
1987 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
1988 New_Param : constant Node_Id := Declaration_Node (New_Formal);
1991 -- Explicit IN must be present or absent in both cases. This
1992 -- test is required only in the full conformance case.
1994 if In_Present (Old_Param) /= In_Present (New_Param)
1995 and then Ctype = Fully_Conformant
1998 ("(Ada 83) IN must appear in both declarations",
2003 -- Grouping (use of comma in param lists) must be the same
2004 -- This is where we catch a misconformance like:
2007 -- A : Integer; B : Integer
2009 -- which are represented identically in the tree except
2010 -- for the setting of the flags More_Ids and Prev_Ids.
2012 if More_Ids (Old_Param) /= More_Ids (New_Param)
2013 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2016 ("grouping of & does not match!", New_Formal);
2022 Next_Formal (Old_Formal);
2023 Next_Formal (New_Formal);
2026 if Present (Old_Formal) then
2027 Conformance_Error ("too few parameters!");
2030 elsif Present (New_Formal) then
2031 Conformance_Error ("too many parameters!", New_Formal);
2035 end Check_Conformance;
2037 ------------------------------
2038 -- Check_Delayed_Subprogram --
2039 ------------------------------
2041 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2044 procedure Possible_Freeze (T : Entity_Id);
2045 -- T is the type of either a formal parameter or of the return type.
2046 -- If T is not yet frozen and needs a delayed freeze, then the
2047 -- subprogram itself must be delayed.
2049 procedure Possible_Freeze (T : Entity_Id) is
2051 if Has_Delayed_Freeze (T)
2052 and then not Is_Frozen (T)
2054 Set_Has_Delayed_Freeze (Designator);
2056 elsif Is_Access_Type (T)
2057 and then Has_Delayed_Freeze (Designated_Type (T))
2058 and then not Is_Frozen (Designated_Type (T))
2060 Set_Has_Delayed_Freeze (Designator);
2062 end Possible_Freeze;
2064 -- Start of processing for Check_Delayed_Subprogram
2067 -- Never need to freeze abstract subprogram
2069 if Is_Abstract (Designator) then
2072 -- Need delayed freeze if return type itself needs a delayed
2073 -- freeze and is not yet frozen.
2075 Possible_Freeze (Etype (Designator));
2076 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2078 -- Need delayed freeze if any of the formal types themselves need
2079 -- a delayed freeze and are not yet frozen.
2081 F := First_Formal (Designator);
2082 while Present (F) loop
2083 Possible_Freeze (Etype (F));
2084 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2089 -- Mark functions that return by reference. Note that it cannot be
2090 -- done for delayed_freeze subprograms because the underlying
2091 -- returned type may not be known yet (for private types)
2093 if not Has_Delayed_Freeze (Designator)
2094 and then Expander_Active
2097 Typ : constant Entity_Id := Etype (Designator);
2098 Utyp : constant Entity_Id := Underlying_Type (Typ);
2101 if Is_Return_By_Reference_Type (Typ) then
2102 Set_Returns_By_Ref (Designator);
2104 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2105 Set_Returns_By_Ref (Designator);
2109 end Check_Delayed_Subprogram;
2111 ------------------------------------
2112 -- Check_Discriminant_Conformance --
2113 ------------------------------------
2115 procedure Check_Discriminant_Conformance
2120 Old_Discr : Entity_Id := First_Discriminant (Prev);
2121 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2122 New_Discr_Id : Entity_Id;
2123 New_Discr_Type : Entity_Id;
2125 procedure Conformance_Error (Msg : String; N : Node_Id);
2126 -- Post error message for conformance error on given node.
2127 -- Two messages are output. The first points to the previous
2128 -- declaration with a general "no conformance" message.
2129 -- The second is the detailed reason, supplied as Msg. The
2130 -- parameter N provide information for a possible & insertion
2133 -----------------------
2134 -- Conformance_Error --
2135 -----------------------
2137 procedure Conformance_Error (Msg : String; N : Node_Id) is
2139 Error_Msg_Sloc := Sloc (Prev_Loc);
2140 Error_Msg_N ("not fully conformant with declaration#!", N);
2141 Error_Msg_NE (Msg, N, N);
2142 end Conformance_Error;
2144 -- Start of processing for Check_Discriminant_Conformance
2147 while Present (Old_Discr) and then Present (New_Discr) loop
2149 New_Discr_Id := Defining_Identifier (New_Discr);
2151 -- The subtype mark of the discriminant on the full type
2152 -- has not been analyzed so we do it here. For an access
2153 -- discriminant a new type is created.
2155 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2157 Access_Definition (N, Discriminant_Type (New_Discr));
2160 Analyze (Discriminant_Type (New_Discr));
2161 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2164 if not Conforming_Types
2165 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2167 Conformance_Error ("type of & does not match!", New_Discr_Id);
2173 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2174 Conformance_Error ("name & does not match!", New_Discr_Id);
2178 -- Default expressions must match
2181 NewD : constant Boolean :=
2182 Present (Expression (New_Discr));
2183 OldD : constant Boolean :=
2184 Present (Expression (Parent (Old_Discr)));
2187 if NewD or OldD then
2189 -- The old default value has been analyzed and expanded,
2190 -- because the current full declaration will have frozen
2191 -- everything before. The new default values have not
2192 -- been expanded, so expand now to check conformance.
2195 Analyze_Default_Expression
2196 (Expression (New_Discr), New_Discr_Type);
2199 if not (NewD and OldD)
2200 or else not Fully_Conformant_Expressions
2201 (Expression (Parent (Old_Discr)),
2202 Expression (New_Discr))
2206 ("default expression for & does not match!",
2213 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2217 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2220 -- Grouping (use of comma in param lists) must be the same
2221 -- This is where we catch a misconformance like:
2224 -- A : Integer; B : Integer
2226 -- which are represented identically in the tree except
2227 -- for the setting of the flags More_Ids and Prev_Ids.
2229 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2230 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2233 ("grouping of & does not match!", New_Discr_Id);
2239 Next_Discriminant (Old_Discr);
2243 if Present (Old_Discr) then
2244 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2247 elsif Present (New_Discr) then
2249 ("too many discriminants!", Defining_Identifier (New_Discr));
2252 end Check_Discriminant_Conformance;
2254 ----------------------------
2255 -- Check_Fully_Conformant --
2256 ----------------------------
2258 procedure Check_Fully_Conformant
2259 (New_Id : Entity_Id;
2261 Err_Loc : Node_Id := Empty)
2267 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2268 end Check_Fully_Conformant;
2270 ---------------------------
2271 -- Check_Mode_Conformant --
2272 ---------------------------
2274 procedure Check_Mode_Conformant
2275 (New_Id : Entity_Id;
2277 Err_Loc : Node_Id := Empty;
2278 Get_Inst : Boolean := False)
2284 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2285 end Check_Mode_Conformant;
2291 procedure Check_Returns
2298 procedure Check_Statement_Sequence (L : List_Id);
2299 -- Internal recursive procedure to check a list of statements for proper
2300 -- termination by a return statement (or a transfer of control or a
2301 -- compound statement that is itself internally properly terminated).
2303 ------------------------------
2304 -- Check_Statement_Sequence --
2305 ------------------------------
2307 procedure Check_Statement_Sequence (L : List_Id) is
2311 Raise_Exception_Call : Boolean;
2312 -- Set True if statement sequence terminated by Raise_Exception call
2313 -- or a Reraise_Occurrence call.
2316 Raise_Exception_Call := False;
2318 -- Get last real statement
2320 Last_Stm := Last (L);
2322 -- Don't count pragmas
2324 while Nkind (Last_Stm) = N_Pragma
2326 -- Don't count call to SS_Release (can happen after Raise_Exception)
2329 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2331 Nkind (Name (Last_Stm)) = N_Identifier
2333 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2335 -- Don't count exception junk
2338 ((Nkind (Last_Stm) = N_Goto_Statement
2339 or else Nkind (Last_Stm) = N_Label
2340 or else Nkind (Last_Stm) = N_Object_Declaration)
2341 and then Exception_Junk (Last_Stm))
2346 -- Here we have the "real" last statement
2348 Kind := Nkind (Last_Stm);
2350 -- Transfer of control, OK. Note that in the No_Return procedure
2351 -- case, we already diagnosed any explicit return statements, so
2352 -- we can treat them as OK in this context.
2354 if Is_Transfer (Last_Stm) then
2357 -- Check cases of explicit non-indirect procedure calls
2359 elsif Kind = N_Procedure_Call_Statement
2360 and then Is_Entity_Name (Name (Last_Stm))
2362 -- Check call to Raise_Exception procedure which is treated
2363 -- specially, as is a call to Reraise_Occurrence.
2365 -- We suppress the warning in these cases since it is likely that
2366 -- the programmer really does not expect to deal with the case
2367 -- of Null_Occurrence, and thus would find a warning about a
2368 -- missing return curious, and raising Program_Error does not
2369 -- seem such a bad behavior if this does occur.
2371 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2373 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2375 Raise_Exception_Call := True;
2377 -- For Raise_Exception call, test first argument, if it is
2378 -- an attribute reference for a 'Identity call, then we know
2379 -- that the call cannot possibly return.
2382 Arg : constant Node_Id :=
2383 Original_Node (First_Actual (Last_Stm));
2386 if Nkind (Arg) = N_Attribute_Reference
2387 and then Attribute_Name (Arg) = Name_Identity
2394 -- If statement, need to look inside if there is an else and check
2395 -- each constituent statement sequence for proper termination.
2397 elsif Kind = N_If_Statement
2398 and then Present (Else_Statements (Last_Stm))
2400 Check_Statement_Sequence (Then_Statements (Last_Stm));
2401 Check_Statement_Sequence (Else_Statements (Last_Stm));
2403 if Present (Elsif_Parts (Last_Stm)) then
2405 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2408 while Present (Elsif_Part) loop
2409 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2417 -- Case statement, check each case for proper termination
2419 elsif Kind = N_Case_Statement then
2424 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2425 while Present (Case_Alt) loop
2426 Check_Statement_Sequence (Statements (Case_Alt));
2427 Next_Non_Pragma (Case_Alt);
2433 -- Block statement, check its handled sequence of statements
2435 elsif Kind = N_Block_Statement then
2441 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2450 -- Loop statement. If there is an iteration scheme, we can definitely
2451 -- fall out of the loop. Similarly if there is an exit statement, we
2452 -- can fall out. In either case we need a following return.
2454 elsif Kind = N_Loop_Statement then
2455 if Present (Iteration_Scheme (Last_Stm))
2456 or else Has_Exit (Entity (Identifier (Last_Stm)))
2460 -- A loop with no exit statement or iteration scheme if either
2461 -- an inifite loop, or it has some other exit (raise/return).
2462 -- In either case, no warning is required.
2468 -- Timed entry call, check entry call and delay alternatives
2470 -- Note: in expanded code, the timed entry call has been converted
2471 -- to a set of expanded statements on which the check will work
2472 -- correctly in any case.
2474 elsif Kind = N_Timed_Entry_Call then
2476 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2477 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2480 -- If statement sequence of entry call alternative is missing,
2481 -- then we can definitely fall through, and we post the error
2482 -- message on the entry call alternative itself.
2484 if No (Statements (ECA)) then
2487 -- If statement sequence of delay alternative is missing, then
2488 -- we can definitely fall through, and we post the error
2489 -- message on the delay alternative itself.
2491 -- Note: if both ECA and DCA are missing the return, then we
2492 -- post only one message, should be enough to fix the bugs.
2493 -- If not we will get a message next time on the DCA when the
2496 elsif No (Statements (DCA)) then
2499 -- Else check both statement sequences
2502 Check_Statement_Sequence (Statements (ECA));
2503 Check_Statement_Sequence (Statements (DCA));
2508 -- Conditional entry call, check entry call and else part
2510 -- Note: in expanded code, the conditional entry call has been
2511 -- converted to a set of expanded statements on which the check
2512 -- will work correctly in any case.
2514 elsif Kind = N_Conditional_Entry_Call then
2516 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2519 -- If statement sequence of entry call alternative is missing,
2520 -- then we can definitely fall through, and we post the error
2521 -- message on the entry call alternative itself.
2523 if No (Statements (ECA)) then
2526 -- Else check statement sequence and else part
2529 Check_Statement_Sequence (Statements (ECA));
2530 Check_Statement_Sequence (Else_Statements (Last_Stm));
2536 -- If we fall through, issue appropriate message
2540 if not Raise_Exception_Call then
2542 ("?RETURN statement missing following this statement!",
2545 ("\?Program_Error may be raised at run time",
2549 -- Note: we set Err even though we have not issued a warning
2550 -- because we still have a case of a missing return. This is
2551 -- an extremely marginal case, probably will never be noticed
2552 -- but we might as well get it right.
2558 ("implied return after this statement not allowed (No_Return)",
2561 end Check_Statement_Sequence;
2563 -- Start of processing for Check_Returns
2567 Check_Statement_Sequence (Statements (HSS));
2569 if Present (Exception_Handlers (HSS)) then
2570 Handler := First_Non_Pragma (Exception_Handlers (HSS));
2571 while Present (Handler) loop
2572 Check_Statement_Sequence (Statements (Handler));
2573 Next_Non_Pragma (Handler);
2578 ----------------------------
2579 -- Check_Subprogram_Order --
2580 ----------------------------
2582 procedure Check_Subprogram_Order (N : Node_Id) is
2584 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2585 -- This is used to check if S1 > S2 in the sense required by this
2586 -- test, for example nameab < namec, but name2 < name10.
2588 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2593 -- Remove trailing numeric parts
2596 while S1 (L1) in '0' .. '9' loop
2601 while S2 (L2) in '0' .. '9' loop
2605 -- If non-numeric parts non-equal, that's decisive
2607 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2610 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
2613 -- If non-numeric parts equal, compare suffixed numeric parts. Note
2614 -- that a missing suffix is treated as numeric zero in this test.
2618 while L1 < S1'Last loop
2620 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
2624 while L2 < S2'Last loop
2626 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
2631 end Subprogram_Name_Greater;
2633 -- Start of processing for Check_Subprogram_Order
2636 -- Check body in alpha order if this is option
2638 if Style_Check_Subprogram_Order
2639 and then Nkind (N) = N_Subprogram_Body
2640 and then Comes_From_Source (N)
2641 and then In_Extended_Main_Source_Unit (N)
2645 renames Scope_Stack.Table
2646 (Scope_Stack.Last).Last_Subprogram_Name;
2648 Body_Id : constant Entity_Id :=
2649 Defining_Entity (Specification (N));
2652 Get_Decoded_Name_String (Chars (Body_Id));
2655 if Subprogram_Name_Greater
2656 (LSN.all, Name_Buffer (1 .. Name_Len))
2658 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
2664 LSN := new String'(Name_Buffer (1 .. Name_Len));
2667 end Check_Subprogram_Order;
2669 ------------------------------
2670 -- Check_Subtype_Conformant --
2671 ------------------------------
2673 procedure Check_Subtype_Conformant
2674 (New_Id : Entity_Id;
2676 Err_Loc : Node_Id := Empty)
2682 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
2683 end Check_Subtype_Conformant;
2685 ---------------------------
2686 -- Check_Type_Conformant --
2687 ---------------------------
2689 procedure Check_Type_Conformant
2690 (New_Id : Entity_Id;
2692 Err_Loc : Node_Id := Empty)
2698 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
2699 end Check_Type_Conformant;
2701 ----------------------
2702 -- Conforming_Types --
2703 ----------------------
2705 function Conforming_Types
2708 Ctype : Conformance_Type;
2709 Get_Inst : Boolean := False)
2712 Type_1 : Entity_Id := T1;
2713 Type_2 : Entity_Id := T2;
2715 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
2716 -- If neither T1 nor T2 are generic actual types, or if they are
2717 -- in different scopes (e.g. parent and child instances), then verify
2718 -- that the base types are equal. Otherwise T1 and T2 must be
2719 -- on the same subtype chain. The whole purpose of this procedure
2720 -- is to prevent spurious ambiguities in an instantiation that may
2721 -- arise if two distinct generic types are instantiated with the
2724 ----------------------
2725 -- Base_Types_Match --
2726 ----------------------
2728 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
2733 elsif Base_Type (T1) = Base_Type (T2) then
2735 -- The following is too permissive. A more precise test must
2736 -- check that the generic actual is an ancestor subtype of the
2739 return not Is_Generic_Actual_Type (T1)
2740 or else not Is_Generic_Actual_Type (T2)
2741 or else Scope (T1) /= Scope (T2);
2746 end Base_Types_Match;
2749 -- The context is an instance association for a formal
2750 -- access-to-subprogram type; the formal parameter types
2751 -- require mapping because they may denote other formal
2752 -- parameters of the generic unit.
2755 Type_1 := Get_Instance_Of (T1);
2756 Type_2 := Get_Instance_Of (T2);
2759 -- First see if base types match
2761 if Base_Types_Match (Type_1, Type_2) then
2762 return Ctype <= Mode_Conformant
2763 or else Subtypes_Statically_Match (Type_1, Type_2);
2765 elsif Is_Incomplete_Or_Private_Type (Type_1)
2766 and then Present (Full_View (Type_1))
2767 and then Base_Types_Match (Full_View (Type_1), Type_2)
2769 return Ctype <= Mode_Conformant
2770 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
2772 elsif Ekind (Type_2) = E_Incomplete_Type
2773 and then Present (Full_View (Type_2))
2774 and then Base_Types_Match (Type_1, Full_View (Type_2))
2776 return Ctype <= Mode_Conformant
2777 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
2780 -- Test anonymous access type case. For this case, static subtype
2781 -- matching is required for mode conformance (RM 6.3.1(15))
2783 if Ekind (Type_1) = E_Anonymous_Access_Type
2784 and then Ekind (Type_2) = E_Anonymous_Access_Type
2787 Desig_1 : Entity_Id;
2788 Desig_2 : Entity_Id;
2791 Desig_1 := Directly_Designated_Type (Type_1);
2793 -- An access parameter can designate an incomplete type.
2795 if Ekind (Desig_1) = E_Incomplete_Type
2796 and then Present (Full_View (Desig_1))
2798 Desig_1 := Full_View (Desig_1);
2801 Desig_2 := Directly_Designated_Type (Type_2);
2803 if Ekind (Desig_2) = E_Incomplete_Type
2804 and then Present (Full_View (Desig_2))
2806 Desig_2 := Full_View (Desig_2);
2809 -- The context is an instance association for a formal
2810 -- access-to-subprogram type; formal access parameter
2811 -- designated types require mapping because they may
2812 -- denote other formal parameters of the generic unit.
2815 Desig_1 := Get_Instance_Of (Desig_1);
2816 Desig_2 := Get_Instance_Of (Desig_2);
2819 -- It is possible for a Class_Wide_Type to be introduced for
2820 -- an incomplete type, in which case there is a separate class_
2821 -- wide type for the full view. The types conform if their
2822 -- Etypes conform, i.e. one may be the full view of the other.
2823 -- This can only happen in the context of an access parameter,
2824 -- other uses of an incomplete Class_Wide_Type are illegal.
2826 if Ekind (Desig_1) = E_Class_Wide_Type
2827 and then Ekind (Desig_2) = E_Class_Wide_Type
2830 Conforming_Types (Etype (Desig_1), Etype (Desig_2), Ctype);
2832 return Base_Type (Desig_1) = Base_Type (Desig_2)
2833 and then (Ctype = Type_Conformant
2835 Subtypes_Statically_Match (Desig_1, Desig_2));
2839 -- Otherwise definitely no match
2845 end Conforming_Types;
2847 --------------------------
2848 -- Create_Extra_Formals --
2849 --------------------------
2851 procedure Create_Extra_Formals (E : Entity_Id) is
2853 Last_Formal : Entity_Id;
2854 Last_Extra : Entity_Id;
2855 Formal_Type : Entity_Id;
2856 P_Formal : Entity_Id := Empty;
2858 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
2859 -- Add an extra formal, associated with the current Formal. The
2860 -- extra formal is added to the list of extra formals, and also
2861 -- returned as the result. These formals are always of mode IN.
2863 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
2864 EF : constant Entity_Id :=
2865 Make_Defining_Identifier (Sloc (Formal),
2866 Chars => New_External_Name (Chars (Formal), 'F'));
2869 -- We never generate extra formals if expansion is not active
2870 -- because we don't need them unless we are generating code.
2872 if not Expander_Active then
2876 -- A little optimization. Never generate an extra formal for
2877 -- the _init operand of an initialization procedure, since it
2878 -- could never be used.
2880 if Chars (Formal) = Name_uInit then
2884 Set_Ekind (EF, E_In_Parameter);
2885 Set_Actual_Subtype (EF, Typ);
2886 Set_Etype (EF, Typ);
2887 Set_Scope (EF, Scope (Formal));
2888 Set_Mechanism (EF, Default_Mechanism);
2889 Set_Formal_Validity (EF);
2891 Set_Extra_Formal (Last_Extra, EF);
2894 end Add_Extra_Formal;
2896 -- Start of processing for Create_Extra_Formals
2899 -- If this is a derived subprogram then the subtypes of the
2900 -- parent subprogram's formal parameters will be used to
2901 -- to determine the need for extra formals.
2903 if Is_Overloadable (E) and then Present (Alias (E)) then
2904 P_Formal := First_Formal (Alias (E));
2907 Last_Extra := Empty;
2908 Formal := First_Formal (E);
2909 while Present (Formal) loop
2910 Last_Extra := Formal;
2911 Next_Formal (Formal);
2914 -- If Extra_formals where already created, don't do it again
2915 -- This situation may arise for subprogram types created as part
2916 -- of dispatching calls (see Expand_Dispatch_Call)
2918 if Present (Last_Extra) and then
2919 Present (Extra_Formal (Last_Extra))
2924 Formal := First_Formal (E);
2926 while Present (Formal) loop
2928 -- Create extra formal for supporting the attribute 'Constrained.
2929 -- The case of a private type view without discriminants also
2930 -- requires the extra formal if the underlying type has defaulted
2933 if Ekind (Formal) /= E_In_Parameter then
2934 if Present (P_Formal) then
2935 Formal_Type := Etype (P_Formal);
2937 Formal_Type := Etype (Formal);
2940 if not Has_Discriminants (Formal_Type)
2941 and then Ekind (Formal_Type) in Private_Kind
2942 and then Present (Underlying_Type (Formal_Type))
2944 Formal_Type := Underlying_Type (Formal_Type);
2947 if Has_Discriminants (Formal_Type)
2949 ((not Is_Constrained (Formal_Type)
2950 and then not Is_Indefinite_Subtype (Formal_Type))
2951 or else Present (Extra_Formal (Formal)))
2953 Set_Extra_Constrained
2954 (Formal, Add_Extra_Formal (Standard_Boolean));
2958 -- Create extra formal for supporting accessibility checking
2960 -- This is suppressed if we specifically suppress accessibility
2961 -- checks for either the subprogram, or the package in which it
2962 -- resides. However, we do not suppress it simply if the scope
2963 -- has accessibility checks suppressed, since this could cause
2964 -- trouble when clients are compiled with a different suppression
2965 -- setting. The explicit checks are safe from this point of view.
2967 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2969 (Suppress_Accessibility_Checks (E)
2971 Suppress_Accessibility_Checks (Scope (E)))
2973 (not Present (P_Formal)
2974 or else Present (Extra_Accessibility (P_Formal)))
2976 -- Temporary kludge: for now we avoid creating the extra
2977 -- formal for access parameters of protected operations
2978 -- because of problem with the case of internal protected
2981 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
2982 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
2984 Set_Extra_Accessibility
2985 (Formal, Add_Extra_Formal (Standard_Natural));
2989 if Present (P_Formal) then
2990 Next_Formal (P_Formal);
2993 Last_Formal := Formal;
2994 Next_Formal (Formal);
2996 end Create_Extra_Formals;
2998 -----------------------------
2999 -- Enter_Overloaded_Entity --
3000 -----------------------------
3002 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3003 E : Entity_Id := Current_Entity_In_Scope (S);
3004 C_E : Entity_Id := Current_Entity (S);
3008 Set_Has_Homonym (E);
3009 Set_Has_Homonym (S);
3012 Set_Is_Immediately_Visible (S);
3013 Set_Scope (S, Current_Scope);
3015 -- Chain new entity if front of homonym in current scope, so that
3016 -- homonyms are contiguous.
3021 while Homonym (C_E) /= E loop
3022 C_E := Homonym (C_E);
3025 Set_Homonym (C_E, S);
3029 Set_Current_Entity (S);
3034 Append_Entity (S, Current_Scope);
3035 Set_Public_Status (S);
3037 if Debug_Flag_E then
3038 Write_Str ("New overloaded entity chain: ");
3039 Write_Name (Chars (S));
3042 while Present (E) loop
3043 Write_Str (" "); Write_Int (Int (E));
3050 -- Generate warning for hiding
3053 and then Comes_From_Source (S)
3054 and then In_Extended_Main_Source_Unit (S)
3061 -- Warn unless genuine overloading
3063 if (not Is_Overloadable (E))
3064 or else Subtype_Conformant (E, S)
3066 Error_Msg_Sloc := Sloc (E);
3067 Error_Msg_N ("declaration of & hides one#?", S);
3071 end Enter_Overloaded_Entity;
3073 -----------------------------
3074 -- Find_Corresponding_Spec --
3075 -----------------------------
3077 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3078 Spec : constant Node_Id := Specification (N);
3079 Designator : constant Entity_Id := Defining_Entity (Spec);
3084 E := Current_Entity (Designator);
3086 while Present (E) loop
3088 -- We are looking for a matching spec. It must have the same scope,
3089 -- and the same name, and either be type conformant, or be the case
3090 -- of a library procedure spec and its body (which belong to one
3091 -- another regardless of whether they are type conformant or not).
3093 if Scope (E) = Current_Scope then
3094 if (Current_Scope = Standard_Standard
3095 or else (Ekind (E) = Ekind (Designator)
3097 Type_Conformant (E, Designator)))
3099 -- Within an instantiation, we know that spec and body are
3100 -- subtype conformant, because they were subtype conformant
3101 -- in the generic. We choose the subtype-conformant entity
3102 -- here as well, to resolve spurious ambiguities in the
3103 -- instance that were not present in the generic (i.e. when
3104 -- two different types are given the same actual). If we are
3105 -- looking for a spec to match a body, full conformance is
3109 Set_Convention (Designator, Convention (E));
3111 if Nkind (N) = N_Subprogram_Body
3112 and then Present (Homonym (E))
3113 and then not Fully_Conformant (E, Designator)
3117 elsif not Subtype_Conformant (E, Designator) then
3122 if not Has_Completion (E) then
3124 if Nkind (N) /= N_Subprogram_Body_Stub then
3125 Set_Corresponding_Spec (N, E);
3128 Set_Has_Completion (E);
3131 elsif Nkind (Parent (N)) = N_Subunit then
3133 -- If this is the proper body of a subunit, the completion
3134 -- flag is set when analyzing the stub.
3138 -- If body already exists, this is an error unless the
3139 -- previous declaration is the implicit declaration of
3140 -- a derived subprogram, or this is a spurious overloading
3143 elsif No (Alias (E))
3144 and then not Is_Intrinsic_Subprogram (E)
3145 and then not In_Instance
3147 Error_Msg_Sloc := Sloc (E);
3148 if Is_Imported (E) then
3150 ("body not allowed for imported subprogram & declared#",
3153 Error_Msg_NE ("duplicate body for & declared#", N, E);
3157 elsif Is_Child_Unit (E)
3159 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3161 Nkind (Parent (Unit_Declaration_Node (Designator)))
3162 = N_Compilation_Unit
3165 -- Child units cannot be overloaded, so a conformance mismatch
3166 -- between body and a previous spec is an error.
3169 ("body of child unit does not match previous declaration", N);
3177 -- On exit, we know that no previous declaration of subprogram exists
3180 end Find_Corresponding_Spec;
3182 ----------------------
3183 -- Fully_Conformant --
3184 ----------------------
3186 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3190 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3192 end Fully_Conformant;
3194 ----------------------------------
3195 -- Fully_Conformant_Expressions --
3196 ----------------------------------
3198 function Fully_Conformant_Expressions
3199 (Given_E1 : Node_Id;
3203 E1 : constant Node_Id := Original_Node (Given_E1);
3204 E2 : constant Node_Id := Original_Node (Given_E2);
3205 -- We always test conformance on original nodes, since it is possible
3206 -- for analysis and/or expansion to make things look as though they
3207 -- conform when they do not, e.g. by converting 1+2 into 3.
3209 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3210 renames Fully_Conformant_Expressions;
3212 function FCL (L1, L2 : List_Id) return Boolean;
3213 -- Compare elements of two lists for conformance. Elements have to
3214 -- be conformant, and actuals inserted as default parameters do not
3215 -- match explicit actuals with the same value.
3217 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3218 -- Compare an operator node with a function call.
3224 function FCL (L1, L2 : List_Id) return Boolean is
3228 if L1 = No_List then
3234 if L2 = No_List then
3240 -- Compare two lists, skipping rewrite insertions (we want to
3241 -- compare the original trees, not the expanded versions!)
3244 if Is_Rewrite_Insertion (N1) then
3246 elsif Is_Rewrite_Insertion (N2) then
3252 elsif not FCE (N1, N2) then
3265 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3266 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3271 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3276 Act := First (Actuals);
3278 if Nkind (Op_Node) in N_Binary_Op then
3280 if not FCE (Left_Opnd (Op_Node), Act) then
3287 return Present (Act)
3288 and then FCE (Right_Opnd (Op_Node), Act)
3289 and then No (Next (Act));
3293 -- Start of processing for Fully_Conformant_Expressions
3296 -- Non-conformant if paren count does not match. Note: if some idiot
3297 -- complains that we don't do this right for more than 3 levels of
3298 -- parentheses, they will be treated with the respect they deserve :-)
3300 if Paren_Count (E1) /= Paren_Count (E2) then
3303 -- If same entities are referenced, then they are conformant
3304 -- even if they have different forms (RM 8.3.1(19-20)).
3306 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3307 if Present (Entity (E1)) then
3308 return Entity (E1) = Entity (E2)
3309 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3310 and then Ekind (Entity (E1)) = E_Discriminant
3311 and then Ekind (Entity (E2)) = E_In_Parameter);
3313 elsif Nkind (E1) = N_Expanded_Name
3314 and then Nkind (E2) = N_Expanded_Name
3315 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3316 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3318 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3321 -- Identifiers in component associations don't always have
3322 -- entities, but their names must conform.
3324 return Nkind (E1) = N_Identifier
3325 and then Nkind (E2) = N_Identifier
3326 and then Chars (E1) = Chars (E2);
3329 elsif Nkind (E1) = N_Character_Literal
3330 and then Nkind (E2) = N_Expanded_Name
3332 return Nkind (Selector_Name (E2)) = N_Character_Literal
3333 and then Chars (E1) = Chars (Selector_Name (E2));
3335 elsif Nkind (E2) = N_Character_Literal
3336 and then Nkind (E1) = N_Expanded_Name
3338 return Nkind (Selector_Name (E1)) = N_Character_Literal
3339 and then Chars (E2) = Chars (Selector_Name (E1));
3341 elsif Nkind (E1) in N_Op
3342 and then Nkind (E2) = N_Function_Call
3344 return FCO (E1, E2);
3346 elsif Nkind (E2) in N_Op
3347 and then Nkind (E1) = N_Function_Call
3349 return FCO (E2, E1);
3351 -- Otherwise we must have the same syntactic entity
3353 elsif Nkind (E1) /= Nkind (E2) then
3356 -- At this point, we specialize by node type
3363 FCL (Expressions (E1), Expressions (E2))
3364 and then FCL (Component_Associations (E1),
3365 Component_Associations (E2));
3368 if Nkind (Expression (E1)) = N_Qualified_Expression
3370 Nkind (Expression (E2)) = N_Qualified_Expression
3372 return FCE (Expression (E1), Expression (E2));
3374 -- Check that the subtype marks and any constraints
3379 Indic1 : constant Node_Id := Expression (E1);
3380 Indic2 : constant Node_Id := Expression (E2);
3385 if Nkind (Indic1) /= N_Subtype_Indication then
3387 Nkind (Indic2) /= N_Subtype_Indication
3388 and then Entity (Indic1) = Entity (Indic2);
3390 elsif Nkind (Indic2) /= N_Subtype_Indication then
3392 Nkind (Indic1) /= N_Subtype_Indication
3393 and then Entity (Indic1) = Entity (Indic2);
3396 if Entity (Subtype_Mark (Indic1)) /=
3397 Entity (Subtype_Mark (Indic2))
3402 Elt1 := First (Constraints (Constraint (Indic1)));
3403 Elt2 := First (Constraints (Constraint (Indic2)));
3405 while Present (Elt1) and then Present (Elt2) loop
3406 if not FCE (Elt1, Elt2) then
3419 when N_Attribute_Reference =>
3421 Attribute_Name (E1) = Attribute_Name (E2)
3422 and then FCL (Expressions (E1), Expressions (E2));
3426 Entity (E1) = Entity (E2)
3427 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3428 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3430 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3432 FCE (Left_Opnd (E1), Left_Opnd (E2))
3434 FCE (Right_Opnd (E1), Right_Opnd (E2));
3436 when N_Character_Literal =>
3438 Char_Literal_Value (E1) = Char_Literal_Value (E2);
3440 when N_Component_Association =>
3442 FCL (Choices (E1), Choices (E2))
3443 and then FCE (Expression (E1), Expression (E2));
3445 when N_Conditional_Expression =>
3447 FCL (Expressions (E1), Expressions (E2));
3449 when N_Explicit_Dereference =>
3451 FCE (Prefix (E1), Prefix (E2));
3453 when N_Extension_Aggregate =>
3455 FCL (Expressions (E1), Expressions (E2))
3456 and then Null_Record_Present (E1) =
3457 Null_Record_Present (E2)
3458 and then FCL (Component_Associations (E1),
3459 Component_Associations (E2));
3461 when N_Function_Call =>
3463 FCE (Name (E1), Name (E2))
3464 and then FCL (Parameter_Associations (E1),
3465 Parameter_Associations (E2));
3467 when N_Indexed_Component =>
3469 FCE (Prefix (E1), Prefix (E2))
3470 and then FCL (Expressions (E1), Expressions (E2));
3472 when N_Integer_Literal =>
3473 return (Intval (E1) = Intval (E2));
3478 when N_Operator_Symbol =>
3480 Chars (E1) = Chars (E2);
3482 when N_Others_Choice =>
3485 when N_Parameter_Association =>
3488 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
3489 and then FCE (Explicit_Actual_Parameter (E1),
3490 Explicit_Actual_Parameter (E2));
3492 when N_Qualified_Expression =>
3494 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3495 and then FCE (Expression (E1), Expression (E2));
3499 FCE (Low_Bound (E1), Low_Bound (E2))
3500 and then FCE (High_Bound (E1), High_Bound (E2));
3502 when N_Real_Literal =>
3503 return (Realval (E1) = Realval (E2));
3505 when N_Selected_Component =>
3507 FCE (Prefix (E1), Prefix (E2))
3508 and then FCE (Selector_Name (E1), Selector_Name (E2));
3512 FCE (Prefix (E1), Prefix (E2))
3513 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3515 when N_String_Literal =>
3517 S1 : constant String_Id := Strval (E1);
3518 S2 : constant String_Id := Strval (E2);
3519 L1 : constant Nat := String_Length (S1);
3520 L2 : constant Nat := String_Length (S2);
3527 for J in 1 .. L1 loop
3528 if Get_String_Char (S1, J) /=
3529 Get_String_Char (S2, J)
3539 when N_Type_Conversion =>
3541 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3542 and then FCE (Expression (E1), Expression (E2));
3546 Entity (E1) = Entity (E2)
3547 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3549 when N_Unchecked_Type_Conversion =>
3551 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3552 and then FCE (Expression (E1), Expression (E2));
3554 -- All other node types cannot appear in this context. Strictly
3555 -- we should raise a fatal internal error. Instead we just ignore
3556 -- the nodes. This means that if anyone makes a mistake in the
3557 -- expander and mucks an expression tree irretrievably, the
3558 -- result will be a failure to detect a (probably very obscure)
3559 -- case of non-conformance, which is better than bombing on some
3560 -- case where two expressions do in fact conform.
3567 end Fully_Conformant_Expressions;
3569 --------------------
3570 -- Install_Entity --
3571 --------------------
3573 procedure Install_Entity (E : Entity_Id) is
3574 Prev : constant Entity_Id := Current_Entity (E);
3577 Set_Is_Immediately_Visible (E);
3578 Set_Current_Entity (E);
3579 Set_Homonym (E, Prev);
3582 ---------------------
3583 -- Install_Formals --
3584 ---------------------
3586 procedure Install_Formals (Id : Entity_Id) is
3590 F := First_Formal (Id);
3592 while Present (F) loop
3596 end Install_Formals;
3598 ---------------------------------
3599 -- Is_Non_Overriding_Operation --
3600 ---------------------------------
3602 function Is_Non_Overriding_Operation
3603 (Prev_E : Entity_Id;
3609 G_Typ : Entity_Id := Empty;
3611 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
3612 -- If F_Type is a derived type associated with a generic actual
3613 -- subtype, then return its Generic_Parent_Type attribute, else
3616 function Types_Correspond
3617 (P_Type : Entity_Id;
3620 -- Returns true if and only if the types (or designated types
3621 -- in the case of anonymous access types) are the same or N_Type
3622 -- is derived directly or indirectly from P_Type.
3624 -----------------------------
3625 -- Get_Generic_Parent_Type --
3626 -----------------------------
3628 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
3633 if Is_Derived_Type (F_Typ)
3634 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
3636 -- The tree must be traversed to determine the parent
3637 -- subtype in the generic unit, which unfortunately isn't
3638 -- always available via semantic attributes. ???
3639 -- (Note: The use of Original_Node is needed for cases
3640 -- where a full derived type has been rewritten.)
3642 Indic := Subtype_Indication
3643 (Type_Definition (Original_Node (Parent (F_Typ))));
3645 if Nkind (Indic) = N_Subtype_Indication then
3646 G_Typ := Entity (Subtype_Mark (Indic));
3648 G_Typ := Entity (Indic);
3651 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
3652 and then Present (Generic_Parent_Type (Parent (G_Typ)))
3654 return Generic_Parent_Type (Parent (G_Typ));
3659 end Get_Generic_Parent_Type;
3661 ----------------------
3662 -- Types_Correspond --
3663 ----------------------
3665 function Types_Correspond
3666 (P_Type : Entity_Id;
3670 Prev_Type : Entity_Id := Base_Type (P_Type);
3671 New_Type : Entity_Id := Base_Type (N_Type);
3674 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
3675 Prev_Type := Designated_Type (Prev_Type);
3678 if Ekind (New_Type) = E_Anonymous_Access_Type then
3679 New_Type := Designated_Type (New_Type);
3682 if Prev_Type = New_Type then
3685 elsif not Is_Class_Wide_Type (New_Type) then
3686 while Etype (New_Type) /= New_Type loop
3687 New_Type := Etype (New_Type);
3688 if New_Type = Prev_Type then
3694 end Types_Correspond;
3696 -- Start of processing for Is_Non_Overriding_Operation
3699 -- In the case where both operations are implicit derived
3700 -- subprograms then neither overrides the other. This can
3701 -- only occur in certain obscure cases (e.g., derivation
3702 -- from homographs created in a generic instantiation).
3704 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
3707 elsif Ekind (Current_Scope) = E_Package
3708 and then Is_Generic_Instance (Current_Scope)
3709 and then In_Private_Part (Current_Scope)
3710 and then Comes_From_Source (New_E)
3712 -- We examine the formals and result subtype of the inherited
3713 -- operation, to determine whether their type is derived from
3714 -- (the instance of) a generic type.
3716 Formal := First_Formal (Prev_E);
3718 while Present (Formal) loop
3719 F_Typ := Base_Type (Etype (Formal));
3721 if Ekind (F_Typ) = E_Anonymous_Access_Type then
3722 F_Typ := Designated_Type (F_Typ);
3725 G_Typ := Get_Generic_Parent_Type (F_Typ);
3727 Next_Formal (Formal);
3730 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
3731 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
3738 -- If the generic type is a private type, then the original
3739 -- operation was not overriding in the generic, because there was
3740 -- no primitive operation to override.
3742 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
3743 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
3744 N_Formal_Private_Type_Definition
3748 -- The generic parent type is the ancestor of a formal derived
3749 -- type declaration. We need to check whether it has a primitive
3750 -- operation that should be overridden by New_E in the generic.
3754 P_Formal : Entity_Id;
3755 N_Formal : Entity_Id;
3759 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
3762 while Present (Prim_Elt) loop
3763 P_Prim := Node (Prim_Elt);
3764 if Chars (P_Prim) = Chars (New_E)
3765 and then Ekind (P_Prim) = Ekind (New_E)
3767 P_Formal := First_Formal (P_Prim);
3768 N_Formal := First_Formal (New_E);
3769 while Present (P_Formal) and then Present (N_Formal) loop
3770 P_Typ := Etype (P_Formal);
3771 N_Typ := Etype (N_Formal);
3773 if not Types_Correspond (P_Typ, N_Typ) then
3777 Next_Entity (P_Formal);
3778 Next_Entity (N_Formal);
3781 -- Found a matching primitive operation belonging to
3782 -- the formal ancestor type, so the new subprogram
3785 if not Present (P_Formal)
3786 and then not Present (N_Formal)
3787 and then (Ekind (New_E) /= E_Function
3790 (Etype (P_Prim), Etype (New_E)))
3796 Next_Elmt (Prim_Elt);
3799 -- If no match found, then the new subprogram does
3800 -- not override in the generic (nor in the instance).
3808 end Is_Non_Overriding_Operation;
3810 ------------------------------
3811 -- Make_Inequality_Operator --
3812 ------------------------------
3814 -- S is the defining identifier of an equality operator. We build a
3815 -- subprogram declaration with the right signature. This operation is
3816 -- intrinsic, because it is always expanded as the negation of the
3817 -- call to the equality function.
3819 procedure Make_Inequality_Operator (S : Entity_Id) is
3820 Loc : constant Source_Ptr := Sloc (S);
3823 Op_Name : Entity_Id;
3829 -- Check that equality was properly defined.
3831 if No (Next_Formal (First_Formal (S))) then
3835 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
3836 B := Make_Defining_Identifier (Loc,
3837 Chars (Next_Formal (First_Formal (S))));
3839 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
3841 Formals := New_List (
3842 Make_Parameter_Specification (Loc,
3843 Defining_Identifier => A,
3845 New_Reference_To (Etype (First_Formal (S)), Loc)),
3847 Make_Parameter_Specification (Loc,
3848 Defining_Identifier => B,
3850 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
3853 Make_Subprogram_Declaration (Loc,
3855 Make_Function_Specification (Loc,
3856 Defining_Unit_Name => Op_Name,
3857 Parameter_Specifications => Formals,
3858 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
3860 -- Insert inequality right after equality if it is explicit or after
3861 -- the derived type when implicit. These entities are created only
3862 -- for visibility purposes, and eventually replaced in the course of
3863 -- expansion, so they do not need to be attached to the tree and seen
3864 -- by the back-end. Keeping them internal also avoids spurious freezing
3865 -- problems. The parent field is set simply to make analysis safe.
3867 if No (Alias (S)) then
3868 Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
3870 Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
3873 Mark_Rewrite_Insertion (Decl);
3874 Set_Is_Intrinsic_Subprogram (Op_Name);
3876 Set_Has_Completion (Op_Name);
3877 Set_Corresponding_Equality (Op_Name, S);
3878 Set_Is_Abstract (Op_Name, Is_Abstract (S));
3880 end Make_Inequality_Operator;
3882 ----------------------
3883 -- May_Need_Actuals --
3884 ----------------------
3886 procedure May_Need_Actuals (Fun : Entity_Id) is
3891 F := First_Formal (Fun);
3894 while Present (F) loop
3895 if No (Default_Value (F)) then
3903 Set_Needs_No_Actuals (Fun, B);
3904 end May_Need_Actuals;
3906 ---------------------
3907 -- Mode_Conformant --
3908 ---------------------
3910 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3914 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
3916 end Mode_Conformant;
3918 ---------------------------
3919 -- New_Overloaded_Entity --
3920 ---------------------------
3922 procedure New_Overloaded_Entity
3924 Derived_Type : Entity_Id := Empty)
3926 E : Entity_Id := Current_Entity_In_Scope (S);
3927 Prev_Vis : Entity_Id := Empty;
3929 function Is_Private_Declaration (E : Entity_Id) return Boolean;
3930 -- Check that E is declared in the private part of the current package,
3931 -- or in the package body, where it may hide a previous declaration.
3932 -- We can' use In_Private_Part by itself because this flag is also
3933 -- set when freezing entities, so we must examine the place of the
3934 -- declaration in the tree, and recognize wrapper packages as well.
3936 procedure Maybe_Primitive_Operation (Overriding : Boolean := False);
3937 -- If the subprogram being analyzed is a primitive operation of
3938 -- the type of one of its formals, set the corresponding flag.
3940 ----------------------------
3941 -- Is_Private_Declaration --
3942 ----------------------------
3944 function Is_Private_Declaration (E : Entity_Id) return Boolean is
3945 Priv_Decls : List_Id;
3946 Decl : constant Node_Id := Unit_Declaration_Node (E);
3949 if Is_Package (Current_Scope)
3950 and then In_Private_Part (Current_Scope)
3953 Private_Declarations (
3954 Specification (Unit_Declaration_Node (Current_Scope)));
3956 return In_Package_Body (Current_Scope)
3957 or else List_Containing (Decl) = Priv_Decls
3958 or else (Nkind (Parent (Decl)) = N_Package_Specification
3959 and then not Is_Compilation_Unit (
3960 Defining_Entity (Parent (Decl)))
3961 and then List_Containing (Parent (Parent (Decl)))
3966 end Is_Private_Declaration;
3968 -------------------------------
3969 -- Maybe_Primitive_Operation --
3970 -------------------------------
3972 procedure Maybe_Primitive_Operation (Overriding : Boolean := False) is
3977 function Visible_Part_Type (T : Entity_Id) return Boolean;
3978 -- Returns true if T is declared in the visible part of
3979 -- the current package scope; otherwise returns false.
3980 -- Assumes that T is declared in a package.
3982 procedure Check_Private_Overriding (T : Entity_Id);
3983 -- Checks that if a primitive abstract subprogram of a visible
3984 -- abstract type is declared in a private part, then it must
3985 -- override an abstract subprogram declared in the visible part.
3986 -- Also checks that if a primitive function with a controlling
3987 -- result is declared in a private part, then it must override
3988 -- a function declared in the visible part.
3990 ------------------------------
3991 -- Check_Private_Overriding --
3992 ------------------------------
3994 procedure Check_Private_Overriding (T : Entity_Id) is
3996 if Ekind (Current_Scope) = E_Package
3997 and then In_Private_Part (Current_Scope)
3998 and then Visible_Part_Type (T)
3999 and then not In_Instance
4002 and then Is_Abstract (S)
4003 and then (not Overriding or else not Is_Abstract (E))
4005 Error_Msg_N ("abstract subprograms must be visible "
4006 & "('R'M 3.9.3(10))!", S);
4008 elsif Ekind (S) = E_Function
4009 and then Is_Tagged_Type (T)
4010 and then T = Base_Type (Etype (S))
4011 and then not Overriding
4014 ("private function with tagged result must"
4015 & " override visible-part function", S);
4017 ("\move subprogram to the visible part"
4018 & " ('R'M 3.9.3(10))", S);
4021 end Check_Private_Overriding;
4023 -----------------------
4024 -- Visible_Part_Type --
4025 -----------------------
4027 function Visible_Part_Type (T : Entity_Id) return Boolean is
4028 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4032 -- If the entity is a private type, then it must be
4033 -- declared in a visible part.
4035 if Ekind (T) in Private_Kind then
4039 -- Otherwise, we traverse the visible part looking for its
4040 -- corresponding declaration. We cannot use the declaration
4041 -- node directly because in the private part the entity of a
4042 -- private type is the one in the full view, which does not
4043 -- indicate that it is the completion of something visible.
4045 N := First (Visible_Declarations (Specification (P)));
4046 while Present (N) loop
4047 if Nkind (N) = N_Full_Type_Declaration
4048 and then Present (Defining_Identifier (N))
4049 and then T = Defining_Identifier (N)
4053 elsif (Nkind (N) = N_Private_Type_Declaration
4055 Nkind (N) = N_Private_Extension_Declaration)
4056 and then Present (Defining_Identifier (N))
4057 and then T = Full_View (Defining_Identifier (N))
4066 end Visible_Part_Type;
4068 -- Start of processing for Maybe_Primitive_Operation
4071 if not Comes_From_Source (S) then
4074 elsif (Ekind (Current_Scope) = E_Package
4075 and then not In_Package_Body (Current_Scope))
4078 -- For function, check return type
4080 if Ekind (S) = E_Function then
4081 B_Typ := Base_Type (Etype (S));
4083 if Scope (B_Typ) = Current_Scope then
4084 Set_Has_Primitive_Operations (B_Typ);
4085 Check_Private_Overriding (B_Typ);
4089 -- For all subprograms, check formals
4091 Formal := First_Formal (S);
4092 while Present (Formal) loop
4093 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4094 F_Typ := Designated_Type (Etype (Formal));
4096 F_Typ := Etype (Formal);
4099 B_Typ := Base_Type (F_Typ);
4101 if Scope (B_Typ) = Current_Scope then
4102 Set_Has_Primitive_Operations (B_Typ);
4103 Check_Private_Overriding (B_Typ);
4106 Next_Formal (Formal);
4109 end Maybe_Primitive_Operation;
4111 -- Start of processing for New_Overloaded_Entity
4115 Enter_Overloaded_Entity (S);
4116 Check_Dispatching_Operation (S, Empty);
4117 Maybe_Primitive_Operation;
4119 elsif not Is_Overloadable (E) then
4121 -- Check for spurious conflict produced by a subprogram that has the
4122 -- same name as that of the enclosing generic package. The conflict
4123 -- occurs within an instance, between the subprogram and the renaming
4124 -- declaration for the package. After the subprogram, the package
4125 -- renaming declaration becomes hidden.
4127 if Ekind (E) = E_Package
4128 and then Present (Renamed_Object (E))
4129 and then Renamed_Object (E) = Current_Scope
4130 and then Nkind (Parent (Renamed_Object (E))) =
4131 N_Package_Specification
4132 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4135 Set_Is_Immediately_Visible (E, False);
4136 Enter_Overloaded_Entity (S);
4137 Set_Homonym (S, Homonym (E));
4138 Check_Dispatching_Operation (S, Empty);
4140 -- If the subprogram is implicit it is hidden by the previous
4141 -- declaration. However if it is dispatching, it must appear in
4142 -- the dispatch table anyway, because it can be dispatched to
4143 -- even if it cannot be called directly.
4145 elsif Present (Alias (S))
4146 and then not Comes_From_Source (S)
4148 Set_Scope (S, Current_Scope);
4150 if Is_Dispatching_Operation (Alias (S)) then
4151 Check_Dispatching_Operation (S, Empty);
4157 Error_Msg_Sloc := Sloc (E);
4158 Error_Msg_N ("& conflicts with declaration#", S);
4160 -- Useful additional warning.
4162 if Is_Generic_Unit (E) then
4163 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4170 -- E exists and is overloadable. Determine whether S is the body
4171 -- of E, a new overloaded entity with a different signature, or
4172 -- an error altogether.
4174 while Present (E) loop
4175 if Scope (E) /= Current_Scope then
4178 elsif Type_Conformant (E, S) then
4180 -- If the old and new entities have the same profile and
4181 -- one is not the body of the other, then this is an error,
4182 -- unless one of them is implicitly declared.
4184 -- There are some cases when both can be implicit, for example
4185 -- when both a literal and a function that overrides it are
4186 -- inherited in a derivation, or when an inhertited operation
4187 -- of a tagged full type overrides the ineherited operation of
4188 -- a private extension. Ada 83 had a special rule for the
4189 -- the literal case. In Ada95, the later implicit operation
4190 -- hides the former, and the literal is always the former.
4191 -- In the odd case where both are derived operations declared
4192 -- at the same point, both operations should be declared,
4193 -- and in that case we bypass the following test and proceed
4194 -- to the next part (this can only occur for certain obscure
4195 -- cases involving homographs in instances and can't occur for
4196 -- dispatching operations ???). Note that the following
4197 -- condition is less than clear. For example, it's not at
4198 -- all clear why there's a test for E_Entry here. ???
4200 if Present (Alias (S))
4201 and then (No (Alias (E))
4202 or else Comes_From_Source (E)
4203 or else Is_Dispatching_Operation (E))
4205 (Ekind (E) = E_Entry
4206 or else Ekind (E) /= E_Enumeration_Literal)
4208 -- When an derived operation is overloaded it may be due
4209 -- to the fact that the full view of a private extension
4210 -- re-inherits. It has to be dealt with.
4212 if Is_Package (Current_Scope)
4213 and then In_Private_Part (Current_Scope)
4215 Check_Operation_From_Private_View (S, E);
4218 -- In any case the implicit operation remains hidden by
4219 -- the existing declaration.
4223 -- Within an instance, the renaming declarations for
4224 -- actual subprograms may become ambiguous, but they do
4225 -- not hide each other.
4227 elsif Ekind (E) /= E_Entry
4228 and then not Comes_From_Source (E)
4229 and then not Is_Generic_Instance (E)
4230 and then (Present (Alias (E))
4231 or else Is_Intrinsic_Subprogram (E))
4232 and then (not In_Instance
4233 or else No (Parent (E))
4234 or else Nkind (Unit_Declaration_Node (E)) /=
4235 N_Subprogram_Renaming_Declaration)
4237 -- A subprogram child unit is not allowed to override
4238 -- an inherited subprogram (10.1.1(20)).
4240 if Is_Child_Unit (S) then
4242 ("child unit overrides inherited subprogram in parent",
4247 if Is_Non_Overriding_Operation (E, S) then
4248 Enter_Overloaded_Entity (S);
4249 if not Present (Derived_Type)
4250 or else Is_Tagged_Type (Derived_Type)
4252 Check_Dispatching_Operation (S, Empty);
4258 -- E is a derived operation or an internal operator which
4259 -- is being overridden. Remove E from further visibility.
4260 -- Furthermore, if E is a dispatching operation, it must be
4261 -- replaced in the list of primitive operations of its type
4262 -- (see Override_Dispatching_Operation).
4268 Prev := First_Entity (Current_Scope);
4270 while Present (Prev)
4271 and then Next_Entity (Prev) /= E
4276 -- It is possible for E to be in the current scope and
4277 -- yet not in the entity chain. This can only occur in a
4278 -- generic context where E is an implicit concatenation
4279 -- in the formal part, because in a generic body the
4280 -- entity chain starts with the formals.
4283 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4285 -- E must be removed both from the entity_list of the
4286 -- current scope, and from the visibility chain
4288 if Debug_Flag_E then
4289 Write_Str ("Override implicit operation ");
4290 Write_Int (Int (E));
4294 -- If E is a predefined concatenation, it stands for four
4295 -- different operations. As a result, a single explicit
4296 -- declaration does not hide it. In a possible ambiguous
4297 -- situation, Disambiguate chooses the user-defined op,
4298 -- so it is correct to retain the previous internal one.
4300 if Chars (E) /= Name_Op_Concat
4301 or else Ekind (E) /= E_Operator
4303 -- For nondispatching derived operations that are
4304 -- overridden by a subprogram declared in the private
4305 -- part of a package, we retain the derived subprogram
4306 -- but mark it as not immediately visible. If the
4307 -- derived operation was declared in the visible part
4308 -- then this ensures that it will still be visible
4309 -- outside the package with the proper signature
4310 -- (calls from outside must also be directed to this
4311 -- version rather than the overriding one, unlike the
4312 -- dispatching case). Calls from inside the package
4313 -- will still resolve to the overriding subprogram
4314 -- since the derived one is marked as not visible
4315 -- within the package.
4317 -- If the private operation is dispatching, we achieve
4318 -- the overriding by keeping the implicit operation
4319 -- but setting its alias to be the overring one. In
4320 -- this fashion the proper body is executed in all
4321 -- cases, but the original signature is used outside
4324 -- If the overriding is not in the private part, we
4325 -- remove the implicit operation altogether.
4327 if Is_Private_Declaration (S) then
4329 if not Is_Dispatching_Operation (E) then
4330 Set_Is_Immediately_Visible (E, False);
4333 -- work done in Override_Dispatching_Operation.
4339 -- Find predecessor of E in Homonym chain.
4341 if E = Current_Entity (E) then
4344 Prev_Vis := Current_Entity (E);
4345 while Homonym (Prev_Vis) /= E loop
4346 Prev_Vis := Homonym (Prev_Vis);
4350 if Prev_Vis /= Empty then
4352 -- Skip E in the visibility chain
4354 Set_Homonym (Prev_Vis, Homonym (E));
4357 Set_Name_Entity_Id (Chars (E), Homonym (E));
4360 Set_Next_Entity (Prev, Next_Entity (E));
4362 if No (Next_Entity (Prev)) then
4363 Set_Last_Entity (Current_Scope, Prev);
4369 Enter_Overloaded_Entity (S);
4371 if Is_Dispatching_Operation (E) then
4372 -- An overriding dispatching subprogram inherits
4373 -- the convention of the overridden subprogram
4376 Set_Convention (S, Convention (E));
4378 Check_Dispatching_Operation (S, E);
4380 Check_Dispatching_Operation (S, Empty);
4383 Maybe_Primitive_Operation (Overriding => True);
4384 goto Check_Inequality;
4387 -- Apparent redeclarations in instances can occur when two
4388 -- formal types get the same actual type. The subprograms in
4389 -- in the instance are legal, even if not callable from the
4390 -- outside. Calls from within are disambiguated elsewhere.
4391 -- For dispatching operations in the visible part, the usual
4392 -- rules apply, and operations with the same profile are not
4395 elsif (In_Instance_Visible_Part
4396 and then not Is_Dispatching_Operation (E))
4397 or else In_Instance_Not_Visible
4401 -- Here we have a real error (identical profile)
4404 Error_Msg_Sloc := Sloc (E);
4406 -- Avoid cascaded errors if the entity appears in
4407 -- subsequent calls.
4409 Set_Scope (S, Current_Scope);
4411 Error_Msg_N ("& conflicts with declaration#", S);
4413 if Is_Generic_Instance (S)
4414 and then not Has_Completion (E)
4417 ("\instantiation cannot provide body for it", S);
4431 -- On exit, we know that S is a new entity
4433 Enter_Overloaded_Entity (S);
4434 Maybe_Primitive_Operation;
4436 -- If S is a derived operation for an untagged type then
4437 -- by definition it's not a dispatching operation (even
4438 -- if the parent operation was dispatching), so we don't
4439 -- call Check_Dispatching_Operation in that case.
4441 if not Present (Derived_Type)
4442 or else Is_Tagged_Type (Derived_Type)
4444 Check_Dispatching_Operation (S, Empty);
4448 -- If this is a user-defined equality operator that is not
4449 -- a derived subprogram, create the corresponding inequality.
4450 -- If the operation is dispatching, the expansion is done
4451 -- elsewhere, and we do not create an explicit inequality
4454 <<Check_Inequality>>
4455 if Chars (S) = Name_Op_Eq
4456 and then Etype (S) = Standard_Boolean
4457 and then Present (Parent (S))
4458 and then not Is_Dispatching_Operation (S)
4460 Make_Inequality_Operator (S);
4463 end New_Overloaded_Entity;
4465 ---------------------
4466 -- Process_Formals --
4467 ---------------------
4469 procedure Process_Formals
4471 Related_Nod : Node_Id)
4473 Param_Spec : Node_Id;
4475 Formal_Type : Entity_Id;
4479 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
4480 -- Check whether the default has a class-wide type. After analysis
4481 -- the default has the type of the formal, so we must also check
4482 -- explicitly for an access attribute.
4484 ---------------------------
4485 -- Is_Class_Wide_Default --
4486 ---------------------------
4488 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
4490 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
4491 or else (Nkind (D) = N_Attribute_Reference
4492 and then Attribute_Name (D) = Name_Access
4493 and then Is_Class_Wide_Type (Etype (Prefix (D))));
4494 end Is_Class_Wide_Default;
4496 -- Start of processing for Process_Formals
4499 -- In order to prevent premature use of the formals in the same formal
4500 -- part, the Ekind is left undefined until all default expressions are
4501 -- analyzed. The Ekind is established in a separate loop at the end.
4503 Param_Spec := First (T);
4505 while Present (Param_Spec) loop
4507 Formal := Defining_Identifier (Param_Spec);
4508 Enter_Name (Formal);
4510 -- Case of ordinary parameters
4512 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4513 Find_Type (Parameter_Type (Param_Spec));
4514 Ptype := Parameter_Type (Param_Spec);
4516 if Ptype = Error then
4520 Formal_Type := Entity (Ptype);
4522 if Ekind (Formal_Type) = E_Incomplete_Type
4523 or else (Is_Class_Wide_Type (Formal_Type)
4524 and then Ekind (Root_Type (Formal_Type)) =
4527 if Nkind (Parent (T)) /= N_Access_Function_Definition
4528 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
4530 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
4533 elsif Ekind (Formal_Type) = E_Void then
4534 Error_Msg_NE ("premature use of&",
4535 Parameter_Type (Param_Spec), Formal_Type);
4538 -- An access formal type
4542 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
4545 Set_Etype (Formal, Formal_Type);
4547 Default := Expression (Param_Spec);
4549 if Present (Default) then
4550 if Out_Present (Param_Spec) then
4552 ("default initialization only allowed for IN parameters",
4556 -- Do the special preanalysis of the expression (see section on
4557 -- "Handling of Default Expressions" in the spec of package Sem).
4559 Analyze_Default_Expression (Default, Formal_Type);
4561 -- Check that the designated type of an access parameter's
4562 -- default is not a class-wide type unless the parameter's
4563 -- designated type is also class-wide.
4565 if Ekind (Formal_Type) = E_Anonymous_Access_Type
4566 and then Is_Class_Wide_Default (Default)
4567 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
4570 ("access to class-wide expression not allowed here", Default);
4578 -- Now set the kind (mode) of each formal
4580 Param_Spec := First (T);
4582 while Present (Param_Spec) loop
4583 Formal := Defining_Identifier (Param_Spec);
4584 Set_Formal_Mode (Formal);
4586 if Ekind (Formal) = E_In_Parameter then
4587 Set_Default_Value (Formal, Expression (Param_Spec));
4589 if Present (Expression (Param_Spec)) then
4590 Default := Expression (Param_Spec);
4592 if Is_Scalar_Type (Etype (Default)) then
4594 (Parameter_Type (Param_Spec)) /= N_Access_Definition
4596 Formal_Type := Entity (Parameter_Type (Param_Spec));
4599 Formal_Type := Access_Definition
4600 (Related_Nod, Parameter_Type (Param_Spec));
4603 Apply_Scalar_Range_Check (Default, Formal_Type);
4612 end Process_Formals;
4614 -------------------------
4615 -- Set_Actual_Subtypes --
4616 -------------------------
4618 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
4619 Loc : constant Source_Ptr := Sloc (N);
4623 First_Stmt : Node_Id := Empty;
4624 AS_Needed : Boolean;
4627 Formal := First_Formal (Subp);
4628 while Present (Formal) loop
4629 T := Etype (Formal);
4631 -- We never need an actual subtype for a constrained formal.
4633 if Is_Constrained (T) then
4636 -- If we have unknown discriminants, then we do not need an
4637 -- actual subtype, or more accurately we cannot figure it out!
4638 -- Note that all class-wide types have unknown discriminants.
4640 elsif Has_Unknown_Discriminants (T) then
4643 -- At this stage we have an unconstrained type that may need
4644 -- an actual subtype. For sure the actual subtype is needed
4645 -- if we have an unconstrained array type.
4647 elsif Is_Array_Type (T) then
4650 -- The only other case which needs an actual subtype is an
4651 -- unconstrained record type which is an IN parameter (we
4652 -- cannot generate actual subtypes for the OUT or IN OUT case,
4653 -- since an assignment can change the discriminant values.
4654 -- However we exclude the case of initialization procedures,
4655 -- since discriminants are handled very specially in this context,
4656 -- see the section entitled "Handling of Discriminants" in Einfo.
4657 -- We also exclude the case of Discrim_SO_Functions (functions
4658 -- used in front end layout mode for size/offset values), since
4659 -- in such functions only discriminants are referenced, and not
4660 -- only are such subtypes not needed, but they cannot always
4661 -- be generated, because of order of elaboration issues.
4663 elsif Is_Record_Type (T)
4664 and then Ekind (Formal) = E_In_Parameter
4665 and then Chars (Formal) /= Name_uInit
4666 and then not Is_Discrim_SO_Function (Subp)
4670 -- All other cases do not need an actual subtype
4676 -- Generate actual subtypes for unconstrained arrays and
4677 -- unconstrained discriminated records.
4680 Decl := Build_Actual_Subtype (T, Formal);
4682 if Nkind (N) = N_Accept_Statement then
4683 if Present (Handled_Statement_Sequence (N)) then
4685 First (Statements (Handled_Statement_Sequence (N)));
4686 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
4687 Mark_Rewrite_Insertion (Decl);
4689 -- If the accept statement has no body, there will be
4690 -- no reference to the actuals, so no need to compute
4697 Prepend (Decl, Declarations (N));
4698 Mark_Rewrite_Insertion (Decl);
4703 -- We need to freeze manually the generated type when it is
4704 -- inserted anywhere else than in a declarative part.
4706 if Present (First_Stmt) then
4707 Insert_List_Before_And_Analyze (First_Stmt,
4708 Freeze_Entity (Defining_Identifier (Decl), Loc));
4711 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
4714 Next_Formal (Formal);
4716 end Set_Actual_Subtypes;
4718 ---------------------
4719 -- Set_Formal_Mode --
4720 ---------------------
4722 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
4723 Spec : constant Node_Id := Parent (Formal_Id);
4726 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
4727 -- since we ensure that corresponding actuals are always valid at the
4728 -- point of the call.
4730 if Out_Present (Spec) then
4732 if Ekind (Scope (Formal_Id)) = E_Function
4733 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
4735 Error_Msg_N ("functions can only have IN parameters", Spec);
4736 Set_Ekind (Formal_Id, E_In_Parameter);
4738 elsif In_Present (Spec) then
4739 Set_Ekind (Formal_Id, E_In_Out_Parameter);
4742 Set_Ekind (Formal_Id, E_Out_Parameter);
4743 Set_Not_Source_Assigned (Formal_Id);
4747 Set_Ekind (Formal_Id, E_In_Parameter);
4750 Set_Mechanism (Formal_Id, Default_Mechanism);
4751 Set_Formal_Validity (Formal_Id);
4752 end Set_Formal_Mode;
4754 -------------------------
4755 -- Set_Formal_Validity --
4756 -------------------------
4758 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
4760 -- If in full validity checking mode, then we can assume that
4761 -- an IN or IN OUT parameter is valid (see Exp_Ch5.Expand_Call)
4763 if not Validity_Checks_On then
4766 elsif Ekind (Formal_Id) = E_In_Parameter
4767 and then Validity_Check_In_Params
4769 Set_Is_Known_Valid (Formal_Id, True);
4771 elsif Ekind (Formal_Id) = E_In_Out_Parameter
4772 and then Validity_Check_In_Out_Params
4774 Set_Is_Known_Valid (Formal_Id, True);
4776 end Set_Formal_Validity;
4778 ------------------------
4779 -- Subtype_Conformant --
4780 ------------------------
4782 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4786 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
4788 end Subtype_Conformant;
4790 ---------------------
4791 -- Type_Conformant --
4792 ---------------------
4794 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4798 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
4800 end Type_Conformant;
4802 -------------------------------
4803 -- Valid_Operator_Definition --
4804 -------------------------------
4806 procedure Valid_Operator_Definition (Designator : Entity_Id) is
4809 Id : constant Name_Id := Chars (Designator);
4813 F := First_Formal (Designator);
4815 while Present (F) loop
4818 if Present (Default_Value (F)) then
4820 ("default values not allowed for operator parameters",
4827 -- Verify that user-defined operators have proper number of arguments
4828 -- First case of operators which can only be unary
4831 or else Id = Name_Op_Abs
4835 -- Case of operators which can be unary or binary
4837 elsif Id = Name_Op_Add
4838 or Id = Name_Op_Subtract
4840 N_OK := (N in 1 .. 2);
4842 -- All other operators can only be binary
4850 ("incorrect number of arguments for operator", Designator);
4854 and then Base_Type (Etype (Designator)) = Standard_Boolean
4855 and then not Is_Intrinsic_Subprogram (Designator)
4858 ("explicit definition of inequality not allowed", Designator);
4860 end Valid_Operator_Definition;