1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Itypes; use Itypes;
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_Ch10; use Sem_Ch10;
53 with Sem_Ch12; use Sem_Ch12;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Dist; use Sem_Dist;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sem_Type; use Sem_Type;
63 with Sem_Warn; use Sem_Warn;
64 with Sinput; use Sinput;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Sinfo.CN; use Sinfo.CN;
68 with Snames; use Snames;
69 with Stringt; use Stringt;
71 with Stylesw; use Stylesw;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75 with Validsw; use Validsw;
77 package body Sem_Ch6 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Analyze_Return_Type (N : Node_Id);
84 -- Subsidiary to Process_Formals: analyze subtype mark in function
85 -- specification, in a context where the formals are visible and hide
88 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
89 -- Analyze a generic subprogram body. N is the body to be analyzed, and
90 -- Gen_Id is the defining entity Id for the corresponding spec.
92 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
93 -- If a subprogram has pragma Inline and inlining is active, use generic
94 -- machinery to build an unexpanded body for the subprogram. This body is
95 -- subsequenty used for inline expansions at call sites. If subprogram can
96 -- be inlined (depending on size and nature of local declarations) this
97 -- function returns true. Otherwise subprogram body is treated normally.
98 -- If proper warnings are enabled and the subprogram contains a construct
99 -- that cannot be inlined, the offending construct is flagged accordingly.
101 type Conformance_Type is
102 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
103 -- Conformance type used for following call, meaning matches the
104 -- RM definitions of the corresponding terms.
106 procedure Check_Conformance
109 Ctype : Conformance_Type;
111 Conforms : out Boolean;
112 Err_Loc : Node_Id := Empty;
113 Get_Inst : Boolean := False;
114 Skip_Controlling_Formals : Boolean := False);
115 -- Given two entities, this procedure checks that the profiles associated
116 -- with these entities meet the conformance criterion given by the third
117 -- parameter. If they conform, Conforms is set True and control returns
118 -- to the caller. If they do not conform, Conforms is set to False, and
119 -- in addition, if Errmsg is True on the call, proper messages are output
120 -- to complain about the conformance failure. If Err_Loc is non_Empty
121 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
122 -- error messages are placed on the appropriate part of the construct
123 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
124 -- against a formal access-to-subprogram type so Get_Instance_Of must
127 procedure Check_Overriding_Indicator
129 Does_Override : Boolean);
130 -- Verify the consistency of an overriding_indicator given for subprogram
131 -- declaration, body, renaming, or instantiation. The flag Does_Override
132 -- is set if the scope into which we are introducing the subprogram
133 -- contains a type-conformant subprogram that becomes hidden by the new
136 procedure Check_Subprogram_Order (N : Node_Id);
137 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
138 -- the alpha ordering rule for N if this ordering requirement applicable.
140 procedure Check_Returns
144 -- Called to check for missing return statements in a function body, or
145 -- for returns present in a procedure body which has No_Return set. L is
146 -- the handled statement sequence for the subprogram body. This procedure
147 -- checks all flow paths to make sure they either have return (Mode = 'F')
148 -- or do not have a return (Mode = 'P'). The flag Err is set if there are
149 -- any control paths not explicitly terminated by a return in the function
150 -- case, and is True otherwise.
152 function Conforming_Types
155 Ctype : Conformance_Type;
156 Get_Inst : Boolean := False) return Boolean;
157 -- Check that two formal parameter types conform, checking both for
158 -- equality of base types, and where required statically matching
159 -- subtypes, depending on the setting of Ctype.
161 procedure Enter_Overloaded_Entity (S : Entity_Id);
162 -- This procedure makes S, a new overloaded entity, into the first visible
163 -- entity with that name.
165 procedure Install_Entity (E : Entity_Id);
166 -- Make single entity visible. Used for generic formals as well
168 procedure Install_Formals (Id : Entity_Id);
169 -- On entry to a subprogram body, make the formals visible. Note that
170 -- simply placing the subprogram on the scope stack is not sufficient:
171 -- the formals must become the current entities for their names.
173 function Is_Non_Overriding_Operation
175 New_E : Entity_Id) return Boolean;
176 -- Enforce the rule given in 12.3(18): a private operation in an instance
177 -- overrides an inherited operation only if the corresponding operation
178 -- was overriding in the generic. This can happen for primitive operations
179 -- of types derived (in the generic unit) from formal private or formal
182 procedure Make_Inequality_Operator (S : Entity_Id);
183 -- Create the declaration for an inequality operator that is implicitly
184 -- created by a user-defined equality operator that yields a boolean.
186 procedure May_Need_Actuals (Fun : Entity_Id);
187 -- Flag functions that can be called without parameters, i.e. those that
188 -- have no parameters, or those for which defaults exist for all parameters
190 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
191 -- If there is a separate spec for a subprogram or generic subprogram, the
192 -- formals of the body are treated as references to the corresponding
193 -- formals of the spec. This reference does not count as an actual use of
194 -- the formal, in order to diagnose formals that are unused in the body.
196 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
197 -- Formal_Id is an formal parameter entity. This procedure deals with
198 -- setting the proper validity status for this entity, which depends
199 -- on the kind of parameter and the validity checking mode.
201 ---------------------------------------------
202 -- Analyze_Abstract_Subprogram_Declaration --
203 ---------------------------------------------
205 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
206 Designator : constant Entity_Id :=
207 Analyze_Subprogram_Specification (Specification (N));
208 Scop : constant Entity_Id := Current_Scope;
211 Generate_Definition (Designator);
212 Set_Is_Abstract (Designator);
213 New_Overloaded_Entity (Designator);
214 Check_Delayed_Subprogram (Designator);
216 Set_Categorization_From_Scope (Designator, Scop);
218 if Ekind (Scope (Designator)) = E_Protected_Type then
220 ("abstract subprogram not allowed in protected type", N);
223 Generate_Reference_To_Formals (Designator);
224 end Analyze_Abstract_Subprogram_Declaration;
226 ----------------------------
227 -- Analyze_Function_Call --
228 ----------------------------
230 procedure Analyze_Function_Call (N : Node_Id) is
231 P : constant Node_Id := Name (N);
232 L : constant List_Id := Parameter_Associations (N);
238 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
239 -- as B(A, X). If the rewriting is successful, the call has been
240 -- analyzed and we just return.
242 if Nkind (P) = N_Selected_Component
243 and then Name (N) /= P
244 and then Is_Rewrite_Substitution (N)
245 and then Present (Etype (N))
250 -- If error analyzing name, then set Any_Type as result type and return
252 if Etype (P) = Any_Type then
253 Set_Etype (N, Any_Type);
257 -- Otherwise analyze the parameters
261 while Present (Actual) loop
263 Check_Parameterless_Call (Actual);
269 end Analyze_Function_Call;
271 -------------------------------------
272 -- Analyze_Generic_Subprogram_Body --
273 -------------------------------------
275 procedure Analyze_Generic_Subprogram_Body
279 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
280 Kind : constant Entity_Kind := Ekind (Gen_Id);
286 -- Copy body and disable expansion while analyzing the generic For a
287 -- stub, do not copy the stub (which would load the proper body), this
288 -- will be done when the proper body is analyzed.
290 if Nkind (N) /= N_Subprogram_Body_Stub then
291 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
296 Spec := Specification (N);
298 -- Within the body of the generic, the subprogram is callable, and
299 -- behaves like the corresponding non-generic unit.
301 Body_Id := Defining_Entity (Spec);
303 if Kind = E_Generic_Procedure
304 and then Nkind (Spec) /= N_Procedure_Specification
306 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
309 elsif Kind = E_Generic_Function
310 and then Nkind (Spec) /= N_Function_Specification
312 Error_Msg_N ("invalid body for generic function ", Body_Id);
316 Set_Corresponding_Body (Gen_Decl, Body_Id);
318 if Has_Completion (Gen_Id)
319 and then Nkind (Parent (N)) /= N_Subunit
321 Error_Msg_N ("duplicate generic body", N);
324 Set_Has_Completion (Gen_Id);
327 if Nkind (N) = N_Subprogram_Body_Stub then
328 Set_Ekind (Defining_Entity (Specification (N)), Kind);
330 Set_Corresponding_Spec (N, Gen_Id);
333 if Nkind (Parent (N)) = N_Compilation_Unit then
334 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
337 -- Make generic parameters immediately visible in the body. They are
338 -- needed to process the formals declarations. Then make the formals
339 -- visible in a separate step.
345 First_Ent : Entity_Id;
348 First_Ent := First_Entity (Gen_Id);
351 while Present (E) and then not Is_Formal (E) loop
356 Set_Use (Generic_Formal_Declarations (Gen_Decl));
358 -- Now generic formals are visible, and the specification can be
359 -- analyzed, for subsequent conformance check.
361 Body_Id := Analyze_Subprogram_Specification (Spec);
363 -- Make formal parameters visible
367 -- E is the first formal parameter, we loop through the formals
368 -- installing them so that they will be visible.
370 Set_First_Entity (Gen_Id, E);
371 while Present (E) loop
377 -- Visible generic entity is callable within its own body
379 Set_Ekind (Gen_Id, Ekind (Body_Id));
380 Set_Ekind (Body_Id, E_Subprogram_Body);
381 Set_Convention (Body_Id, Convention (Gen_Id));
382 Set_Scope (Body_Id, Scope (Gen_Id));
383 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
385 if Nkind (N) = N_Subprogram_Body_Stub then
387 -- No body to analyze, so restore state of generic unit
389 Set_Ekind (Gen_Id, Kind);
390 Set_Ekind (Body_Id, Kind);
392 if Present (First_Ent) then
393 Set_First_Entity (Gen_Id, First_Ent);
400 -- If this is a compilation unit, it must be made visible explicitly,
401 -- because the compilation of the declaration, unlike other library
402 -- unit declarations, does not. If it is not a unit, the following
403 -- is redundant but harmless.
405 Set_Is_Immediately_Visible (Gen_Id);
406 Reference_Body_Formals (Gen_Id, Body_Id);
408 Set_Actual_Subtypes (N, Current_Scope);
409 Analyze_Declarations (Declarations (N));
411 Analyze (Handled_Statement_Sequence (N));
413 Save_Global_References (Original_Node (N));
415 -- Prior to exiting the scope, include generic formals again (if any
416 -- are present) in the set of local entities.
418 if Present (First_Ent) then
419 Set_First_Entity (Gen_Id, First_Ent);
422 Check_References (Gen_Id);
425 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
427 Check_Subprogram_Order (N);
429 -- Outside of its body, unit is generic again
431 Set_Ekind (Gen_Id, Kind);
432 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
433 Style.Check_Identifier (Body_Id, Gen_Id);
435 end Analyze_Generic_Subprogram_Body;
437 -----------------------------
438 -- Analyze_Operator_Symbol --
439 -----------------------------
441 -- An operator symbol such as "+" or "and" may appear in context where the
442 -- literal denotes an entity name, such as "+"(x, y) or in context when it
443 -- is just a string, as in (conjunction = "or"). In these cases the parser
444 -- generates this node, and the semantics does the disambiguation. Other
445 -- such case are actuals in an instantiation, the generic unit in an
446 -- instantiation, and pragma arguments.
448 procedure Analyze_Operator_Symbol (N : Node_Id) is
449 Par : constant Node_Id := Parent (N);
452 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
453 or else Nkind (Par) = N_Function_Instantiation
454 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
455 or else (Nkind (Par) = N_Pragma_Argument_Association
456 and then not Is_Pragma_String_Literal (Par))
457 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
458 or else (Nkind (Par) = N_Attribute_Reference
459 and then Attribute_Name (Par) /= Name_Value)
461 Find_Direct_Name (N);
464 Change_Operator_Symbol_To_String_Literal (N);
467 end Analyze_Operator_Symbol;
469 -----------------------------------
470 -- Analyze_Parameter_Association --
471 -----------------------------------
473 procedure Analyze_Parameter_Association (N : Node_Id) is
475 Analyze (Explicit_Actual_Parameter (N));
476 end Analyze_Parameter_Association;
478 ----------------------------
479 -- Analyze_Procedure_Call --
480 ----------------------------
482 procedure Analyze_Procedure_Call (N : Node_Id) is
483 Loc : constant Source_Ptr := Sloc (N);
484 P : constant Node_Id := Name (N);
485 Actuals : constant List_Id := Parameter_Associations (N);
489 procedure Analyze_Call_And_Resolve;
490 -- Do Analyze and Resolve calls for procedure call
492 ------------------------------
493 -- Analyze_Call_And_Resolve --
494 ------------------------------
496 procedure Analyze_Call_And_Resolve is
498 if Nkind (N) = N_Procedure_Call_Statement then
500 Resolve (N, Standard_Void_Type);
504 end Analyze_Call_And_Resolve;
506 -- Start of processing for Analyze_Procedure_Call
509 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
510 -- a procedure call or an entry call. The prefix may denote an access
511 -- to subprogram type, in which case an implicit dereference applies.
512 -- If the prefix is an indexed component (without implicit defererence)
513 -- then the construct denotes a call to a member of an entire family.
514 -- If the prefix is a simple name, it may still denote a call to a
515 -- parameterless member of an entry family. Resolution of these various
516 -- interpretations is delicate.
520 -- If this is a call of the form Obj.Op, the call may have been
521 -- analyzed and possibly rewritten into a block, in which case
528 -- If error analyzing prefix, then set Any_Type as result and return
530 if Etype (P) = Any_Type then
531 Set_Etype (N, Any_Type);
535 -- Otherwise analyze the parameters
537 if Present (Actuals) then
538 Actual := First (Actuals);
540 while Present (Actual) loop
542 Check_Parameterless_Call (Actual);
547 -- Special processing for Elab_Spec and Elab_Body calls
549 if Nkind (P) = N_Attribute_Reference
550 and then (Attribute_Name (P) = Name_Elab_Spec
551 or else Attribute_Name (P) = Name_Elab_Body)
553 if Present (Actuals) then
555 ("no parameters allowed for this call", First (Actuals));
559 Set_Etype (N, Standard_Void_Type);
562 elsif Is_Entity_Name (P)
563 and then Is_Record_Type (Etype (Entity (P)))
564 and then Remote_AST_I_Dereference (P)
568 elsif Is_Entity_Name (P)
569 and then Ekind (Entity (P)) /= E_Entry_Family
571 if Is_Access_Type (Etype (P))
572 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
573 and then No (Actuals)
574 and then Comes_From_Source (N)
576 Error_Msg_N ("missing explicit dereference in call", N);
579 Analyze_Call_And_Resolve;
581 -- If the prefix is the simple name of an entry family, this is
582 -- a parameterless call from within the task body itself.
584 elsif Is_Entity_Name (P)
585 and then Nkind (P) = N_Identifier
586 and then Ekind (Entity (P)) = E_Entry_Family
587 and then Present (Actuals)
588 and then No (Next (First (Actuals)))
590 -- Can be call to parameterless entry family. What appears to be the
591 -- sole argument is in fact the entry index. Rewrite prefix of node
592 -- accordingly. Source representation is unchanged by this
596 Make_Indexed_Component (Loc,
598 Make_Selected_Component (Loc,
599 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
600 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
601 Expressions => Actuals);
603 Set_Etype (New_N, Standard_Void_Type);
604 Set_Parameter_Associations (N, No_List);
605 Analyze_Call_And_Resolve;
607 elsif Nkind (P) = N_Explicit_Dereference then
608 if Ekind (Etype (P)) = E_Subprogram_Type then
609 Analyze_Call_And_Resolve;
611 Error_Msg_N ("expect access to procedure in call", P);
614 -- The name can be a selected component or an indexed component that
615 -- yields an access to subprogram. Such a prefix is legal if the call
616 -- has parameter associations.
618 elsif Is_Access_Type (Etype (P))
619 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
621 if Present (Actuals) then
622 Analyze_Call_And_Resolve;
624 Error_Msg_N ("missing explicit dereference in call ", N);
627 -- If not an access to subprogram, then the prefix must resolve to the
628 -- name of an entry, entry family, or protected operation.
630 -- For the case of a simple entry call, P is a selected component where
631 -- the prefix is the task and the selector name is the entry. A call to
632 -- a protected procedure will have the same syntax. If the protected
633 -- object contains overloaded operations, the entity may appear as a
634 -- function, the context will select the operation whose type is Void.
636 elsif Nkind (P) = N_Selected_Component
637 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
639 Ekind (Entity (Selector_Name (P))) = E_Procedure
641 Ekind (Entity (Selector_Name (P))) = E_Function)
643 Analyze_Call_And_Resolve;
645 elsif Nkind (P) = N_Selected_Component
646 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
647 and then Present (Actuals)
648 and then No (Next (First (Actuals)))
650 -- Can be call to parameterless entry family. What appears to be the
651 -- sole argument is in fact the entry index. Rewrite prefix of node
652 -- accordingly. Source representation is unchanged by this
656 Make_Indexed_Component (Loc,
657 Prefix => New_Copy (P),
658 Expressions => Actuals);
660 Set_Etype (New_N, Standard_Void_Type);
661 Set_Parameter_Associations (N, No_List);
662 Analyze_Call_And_Resolve;
664 -- For the case of a reference to an element of an entry family, P is
665 -- an indexed component whose prefix is a selected component (task and
666 -- entry family), and whose index is the entry family index.
668 elsif Nkind (P) = N_Indexed_Component
669 and then Nkind (Prefix (P)) = N_Selected_Component
670 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
672 Analyze_Call_And_Resolve;
674 -- If the prefix is the name of an entry family, it is a call from
675 -- within the task body itself.
677 elsif Nkind (P) = N_Indexed_Component
678 and then Nkind (Prefix (P)) = N_Identifier
679 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
682 Make_Selected_Component (Loc,
683 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
684 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
685 Rewrite (Prefix (P), New_N);
687 Analyze_Call_And_Resolve;
689 -- Anything else is an error
692 Error_Msg_N ("invalid procedure or entry call", N);
694 end Analyze_Procedure_Call;
696 ------------------------------
697 -- Analyze_Return_Statement --
698 ------------------------------
700 procedure Analyze_Return_Statement (N : Node_Id) is
701 Loc : constant Source_Ptr := Sloc (N);
703 Scope_Id : Entity_Id;
708 -- Find subprogram or accept statement enclosing the return statement
711 for J in reverse 0 .. Scope_Stack.Last loop
712 Scope_Id := Scope_Stack.Table (J).Entity;
713 exit when Ekind (Scope_Id) /= E_Block and then
714 Ekind (Scope_Id) /= E_Loop;
717 pragma Assert (Present (Scope_Id));
719 Kind := Ekind (Scope_Id);
720 Expr := Expression (N);
722 if Kind /= E_Function
723 and then Kind /= E_Generic_Function
724 and then Kind /= E_Procedure
725 and then Kind /= E_Generic_Procedure
726 and then Kind /= E_Entry
727 and then Kind /= E_Entry_Family
729 Error_Msg_N ("illegal context for return statement", N);
731 elsif Present (Expr) then
732 if Kind = E_Function or else Kind = E_Generic_Function then
733 Set_Return_Present (Scope_Id);
734 R_Type := Etype (Scope_Id);
735 Set_Return_Type (N, R_Type);
736 Analyze_And_Resolve (Expr, R_Type);
738 -- Ada 2005 (AI-318-02): When the result type is an anonymous
739 -- access type, apply an implicit conversion of the expression
740 -- to that type to force appropriate static and run-time
741 -- accessibility checks.
743 if Ada_Version >= Ada_05
744 and then Ekind (R_Type) = E_Anonymous_Access_Type
746 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
747 Analyze_And_Resolve (Expr, R_Type);
750 if (Is_Class_Wide_Type (Etype (Expr))
751 or else Is_Dynamically_Tagged (Expr))
752 and then not Is_Class_Wide_Type (R_Type)
755 ("dynamically tagged expression not allowed!", Expr);
758 Apply_Constraint_Check (Expr, R_Type);
760 -- Ada 2005 (AI-318-02): Return-by-reference types have been
761 -- removed and replaced by anonymous access results. This is
762 -- an incompatibility with Ada 95. Not clear whether this
763 -- should be enforced yet or perhaps controllable with a
764 -- special switch. ???
766 -- if Ada_Version >= Ada_05
767 -- and then Is_Limited_Type (R_Type)
768 -- and then Nkind (Expr) /= N_Aggregate
769 -- and then Nkind (Expr) /= N_Extension_Aggregate
770 -- and then Nkind (Expr) /= N_Function_Call
773 -- ("(Ada 2005) illegal operand for limited return", N);
776 -- ??? A real run-time accessibility check is needed in cases
777 -- involving dereferences of access parameters. For now we just
778 -- check the static cases.
780 if Is_Return_By_Reference_Type (Etype (Scope_Id))
781 and then Object_Access_Level (Expr)
782 > Subprogram_Access_Level (Scope_Id)
785 Make_Raise_Program_Error (Loc,
786 Reason => PE_Accessibility_Check_Failed));
790 ("cannot return a local value by reference?", N);
792 ("& will be raised at run time?!",
793 N, Standard_Program_Error);
796 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
797 Error_Msg_N ("procedure cannot return value (use function)", N);
800 Error_Msg_N ("accept statement cannot return value", N);
803 -- No expression present
806 if Kind = E_Function or Kind = E_Generic_Function then
807 Error_Msg_N ("missing expression in return from function", N);
810 if (Ekind (Scope_Id) = E_Procedure
811 or else Ekind (Scope_Id) = E_Generic_Procedure)
812 and then No_Return (Scope_Id)
815 ("RETURN statement not allowed (No_Return)", N);
819 Check_Unreachable_Code (N);
820 end Analyze_Return_Statement;
822 -------------------------
823 -- Analyze_Return_Type --
824 -------------------------
826 procedure Analyze_Return_Type (N : Node_Id) is
827 Designator : constant Entity_Id := Defining_Entity (N);
828 Typ : Entity_Id := Empty;
831 if Result_Definition (N) /= Error then
832 if Nkind (Result_Definition (N)) = N_Access_Definition then
833 Typ := Access_Definition (N, Result_Definition (N));
834 Set_Parent (Typ, Result_Definition (N));
835 Set_Is_Local_Anonymous_Access (Typ);
836 Set_Etype (Designator, Typ);
838 -- Ada 2005 (AI-231): Static checks
840 -- Null_Exclusion_Static_Checks needs to be extended to handle
841 -- null exclusion checks for function specifications. ???
843 -- if Null_Exclusion_Present (N) then
844 -- Null_Exclusion_Static_Checks (Param_Spec);
850 Find_Type (Result_Definition (N));
851 Typ := Entity (Result_Definition (N));
852 Set_Etype (Designator, Typ);
854 if Ekind (Typ) = E_Incomplete_Type
855 or else (Is_Class_Wide_Type (Typ)
857 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
860 ("invalid use of incomplete type", Result_Definition (N));
865 Set_Etype (Designator, Any_Type);
867 end Analyze_Return_Type;
869 -----------------------------
870 -- Analyze_Subprogram_Body --
871 -----------------------------
873 -- This procedure is called for regular subprogram bodies, generic bodies,
874 -- and for subprogram stubs of both kinds. In the case of stubs, only the
875 -- specification matters, and is used to create a proper declaration for
876 -- the subprogram, or to perform conformance checks.
878 procedure Analyze_Subprogram_Body (N : Node_Id) is
879 Loc : constant Source_Ptr := Sloc (N);
880 Body_Spec : constant Node_Id := Specification (N);
881 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
882 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
883 Body_Deleted : constant Boolean := False;
887 Spec_Decl : Node_Id := Empty;
888 Last_Formal : Entity_Id := Empty;
889 Conformant : Boolean;
890 Missing_Ret : Boolean;
893 procedure Check_Following_Pragma;
894 -- If front-end inlining is enabled, look ahead to recognize a pragma
895 -- that may appear after the body.
897 procedure Verify_Overriding_Indicator;
898 -- If there was a previous spec, the entity has been entered in the
899 -- current scope previously. If the body itself carries an overriding
900 -- indicator, check that it is consistent with the known status of the
903 ----------------------------
904 -- Check_Following_Pragma --
905 ----------------------------
907 procedure Check_Following_Pragma is
911 if Front_End_Inlining
912 and then Is_List_Member (N)
913 and then Present (Spec_Decl)
914 and then List_Containing (N) = List_Containing (Spec_Decl)
919 and then Nkind (Prag) = N_Pragma
920 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
923 (Expression (First (Pragma_Argument_Associations (Prag))))
929 end Check_Following_Pragma;
931 ---------------------------------
932 -- Verify_Overriding_Indicator --
933 ---------------------------------
935 procedure Verify_Overriding_Indicator is
937 if Must_Override (Body_Spec)
938 and then not Is_Overriding_Operation (Spec_Id)
941 ("subprogram& is not overriding", Body_Spec, Spec_Id);
943 elsif Must_Not_Override (Body_Spec)
944 and then Is_Overriding_Operation (Spec_Id)
947 ("subprogram& overrides inherited operation",
950 end Verify_Overriding_Indicator;
952 -- Start of processing for Analyze_Subprogram_Body
956 Write_Str ("==== Compiling subprogram body ");
957 Write_Name (Chars (Body_Id));
958 Write_Str (" from ");
959 Write_Location (Loc);
963 Trace_Scope (N, Body_Id, " Analyze subprogram");
965 -- Generic subprograms are handled separately. They always have a
966 -- generic specification. Determine whether current scope has a
967 -- previous declaration.
969 -- If the subprogram body is defined within an instance of the same
970 -- name, the instance appears as a package renaming, and will be hidden
971 -- within the subprogram.
974 and then not Is_Overloadable (Prev_Id)
975 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
976 or else Comes_From_Source (Prev_Id))
978 if Is_Generic_Subprogram (Prev_Id) then
980 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
981 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
983 Analyze_Generic_Subprogram_Body (N, Spec_Id);
987 -- Previous entity conflicts with subprogram name. Attempting to
988 -- enter name will post error.
990 Enter_Name (Body_Id);
994 -- Non-generic case, find the subprogram declaration, if one was seen,
995 -- or enter new overloaded entity in the current scope. If the
996 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
997 -- part of the context of one of its subunits. No need to redo the
1000 elsif Prev_Id = Body_Id
1001 and then Has_Completion (Body_Id)
1006 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1008 if Nkind (N) = N_Subprogram_Body_Stub
1009 or else No (Corresponding_Spec (N))
1011 Spec_Id := Find_Corresponding_Spec (N);
1013 -- If this is a duplicate body, no point in analyzing it
1015 if Error_Posted (N) then
1019 -- A subprogram body should cause freezing of its own declaration,
1020 -- but if there was no previous explicit declaration, then the
1021 -- subprogram will get frozen too late (there may be code within
1022 -- the body that depends on the subprogram having been frozen,
1023 -- such as uses of extra formals), so we force it to be frozen
1024 -- here. Same holds if the body and the spec are compilation
1027 if No (Spec_Id) then
1028 Freeze_Before (N, Body_Id);
1030 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1031 Freeze_Before (N, Spec_Id);
1034 Spec_Id := Corresponding_Spec (N);
1038 -- Do not inline any subprogram that contains nested subprograms, since
1039 -- the backend inlining circuit seems to generate uninitialized
1040 -- references in this case. We know this happens in the case of front
1041 -- end ZCX support, but it also appears it can happen in other cases as
1042 -- well. The backend often rejects attempts to inline in the case of
1043 -- nested procedures anyway, so little if anything is lost by this.
1044 -- Note that this is test is for the benefit of the back-end. There is
1045 -- a separate test for front-end inlining that also rejects nested
1048 -- Do not do this test if errors have been detected, because in some
1049 -- error cases, this code blows up, and we don't need it anyway if
1050 -- there have been errors, since we won't get to the linker anyway.
1052 if Comes_From_Source (Body_Id)
1053 and then Serious_Errors_Detected = 0
1057 P_Ent := Scope (P_Ent);
1058 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1060 if Is_Subprogram (P_Ent) then
1061 Set_Is_Inlined (P_Ent, False);
1063 if Comes_From_Source (P_Ent)
1064 and then Has_Pragma_Inline (P_Ent)
1067 ("cannot inline& (nested subprogram)?",
1074 -- Case of fully private operation in the body of the protected type.
1075 -- We must create a declaration for the subprogram, in order to attach
1076 -- the protected subprogram that will be used in internal calls.
1079 and then Comes_From_Source (N)
1080 and then Is_Protected_Type (Current_Scope)
1089 Formal := First_Formal (Body_Id);
1091 -- The protected operation always has at least one formal, namely
1092 -- the object itself, but it is only placed in the parameter list
1093 -- if expansion is enabled.
1096 or else Expander_Active
1104 while Present (Formal) loop
1106 (Make_Parameter_Specification (Loc,
1107 Defining_Identifier =>
1108 Make_Defining_Identifier (Sloc (Formal),
1109 Chars => Chars (Formal)),
1110 In_Present => In_Present (Parent (Formal)),
1111 Out_Present => Out_Present (Parent (Formal)),
1113 New_Reference_To (Etype (Formal), Loc),
1115 New_Copy_Tree (Expression (Parent (Formal)))),
1118 Next_Formal (Formal);
1121 if Nkind (Body_Spec) = N_Procedure_Specification then
1123 Make_Procedure_Specification (Loc,
1124 Defining_Unit_Name =>
1125 Make_Defining_Identifier (Sloc (Body_Id),
1126 Chars => Chars (Body_Id)),
1127 Parameter_Specifications => Plist);
1130 Make_Function_Specification (Loc,
1131 Defining_Unit_Name =>
1132 Make_Defining_Identifier (Sloc (Body_Id),
1133 Chars => Chars (Body_Id)),
1134 Parameter_Specifications => Plist,
1135 Result_Definition =>
1136 New_Occurrence_Of (Etype (Body_Id), Loc));
1140 Make_Subprogram_Declaration (Loc,
1141 Specification => New_Spec);
1142 Insert_Before (N, Decl);
1143 Spec_Id := Defining_Unit_Name (New_Spec);
1145 -- Indicate that the entity comes from source, to ensure that
1146 -- cross-reference information is properly generated. The body
1147 -- itself is rewritten during expansion, and the body entity will
1148 -- not appear in calls to the operation.
1150 Set_Comes_From_Source (Spec_Id, True);
1152 Set_Has_Completion (Spec_Id);
1153 Set_Convention (Spec_Id, Convention_Protected);
1156 elsif Present (Spec_Id) then
1157 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1158 Verify_Overriding_Indicator;
1161 -- Place subprogram on scope stack, and make formals visible. If there
1162 -- is a spec, the visible entity remains that of the spec.
1164 if Present (Spec_Id) then
1165 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1167 if Is_Child_Unit (Spec_Id) then
1168 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1172 Style.Check_Identifier (Body_Id, Spec_Id);
1175 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1176 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1178 if Is_Abstract (Spec_Id) then
1179 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1182 Set_Convention (Body_Id, Convention (Spec_Id));
1183 Set_Has_Completion (Spec_Id);
1185 if Is_Protected_Type (Scope (Spec_Id)) then
1186 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1189 -- If this is a body generated for a renaming, do not check for
1190 -- full conformance. The check is redundant, because the spec of
1191 -- the body is a copy of the spec in the renaming declaration,
1192 -- and the test can lead to spurious errors on nested defaults.
1194 if Present (Spec_Decl)
1195 and then not Comes_From_Source (N)
1197 (Nkind (Original_Node (Spec_Decl)) =
1198 N_Subprogram_Renaming_Declaration
1199 or else (Present (Corresponding_Body (Spec_Decl))
1201 Nkind (Unit_Declaration_Node
1202 (Corresponding_Body (Spec_Decl))) =
1203 N_Subprogram_Renaming_Declaration))
1209 Fully_Conformant, True, Conformant, Body_Id);
1212 -- If the body is not fully conformant, we have to decide if we
1213 -- should analyze it or not. If it has a really messed up profile
1214 -- then we probably should not analyze it, since we will get too
1215 -- many bogus messages.
1217 -- Our decision is to go ahead in the non-fully conformant case
1218 -- only if it is at least mode conformant with the spec. Note
1219 -- that the call to Check_Fully_Conformant has issued the proper
1220 -- error messages to complain about the lack of conformance.
1223 and then not Mode_Conformant (Body_Id, Spec_Id)
1229 if Spec_Id /= Body_Id then
1230 Reference_Body_Formals (Spec_Id, Body_Id);
1233 if Nkind (N) /= N_Subprogram_Body_Stub then
1234 Set_Corresponding_Spec (N, Spec_Id);
1236 -- Ada 2005 (AI-345): Restore the correct Etype: here we undo the
1237 -- work done by Analyze_Subprogram_Specification to allow the
1238 -- overriding of task, protected and interface primitives.
1240 if Comes_From_Source (Spec_Id)
1241 and then Present (First_Entity (Spec_Id))
1242 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
1243 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
1244 and then Present (Abstract_Interfaces
1245 (Etype (First_Entity (Spec_Id))))
1246 and then Present (Corresponding_Concurrent_Type
1247 (Etype (First_Entity (Spec_Id))))
1249 Set_Etype (First_Entity (Spec_Id),
1250 Corresponding_Concurrent_Type
1251 (Etype (First_Entity (Spec_Id))));
1254 -- Comment needed here, since this is not Ada 2005 stuff! ???
1256 Install_Formals (Spec_Id);
1257 Last_Formal := Last_Entity (Spec_Id);
1258 New_Scope (Spec_Id);
1260 -- Make sure that the subprogram is immediately visible. For
1261 -- child units that have no separate spec this is indispensable.
1262 -- Otherwise it is safe albeit redundant.
1264 Set_Is_Immediately_Visible (Spec_Id);
1267 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1268 Set_Ekind (Body_Id, E_Subprogram_Body);
1269 Set_Scope (Body_Id, Scope (Spec_Id));
1271 -- Case of subprogram body with no previous spec
1275 and then Comes_From_Source (Body_Id)
1276 and then not Suppress_Style_Checks (Body_Id)
1277 and then not In_Instance
1279 Style.Body_With_No_Spec (N);
1282 New_Overloaded_Entity (Body_Id);
1284 if Nkind (N) /= N_Subprogram_Body_Stub then
1285 Set_Acts_As_Spec (N);
1286 Generate_Definition (Body_Id);
1288 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1289 Generate_Reference_To_Formals (Body_Id);
1290 Install_Formals (Body_Id);
1291 New_Scope (Body_Id);
1295 -- If this is the proper body of a stub, we must verify that the stub
1296 -- conforms to the body, and to the previous spec if one was present.
1297 -- we know already that the body conforms to that spec. This test is
1298 -- only required for subprograms that come from source.
1300 if Nkind (Parent (N)) = N_Subunit
1301 and then Comes_From_Source (N)
1302 and then not Error_Posted (Body_Id)
1303 and then Nkind (Corresponding_Stub (Parent (N))) =
1304 N_Subprogram_Body_Stub
1307 Old_Id : constant Entity_Id :=
1309 (Specification (Corresponding_Stub (Parent (N))));
1311 Conformant : Boolean := False;
1314 if No (Spec_Id) then
1315 Check_Fully_Conformant (Body_Id, Old_Id);
1319 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1321 if not Conformant then
1323 -- The stub was taken to be a new declaration. Indicate
1324 -- that it lacks a body.
1326 Set_Has_Completion (Old_Id, False);
1332 Set_Has_Completion (Body_Id);
1333 Check_Eliminated (Body_Id);
1335 if Nkind (N) = N_Subprogram_Body_Stub then
1338 elsif Present (Spec_Id)
1339 and then Expander_Active
1341 Check_Following_Pragma;
1343 if Is_Always_Inlined (Spec_Id)
1344 or else (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining)
1346 Build_Body_To_Inline (N, Spec_Id);
1350 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1351 -- if its specification we have to install the private withed units.
1353 if Is_Compilation_Unit (Body_Id)
1354 and then Scope (Body_Id) = Standard_Standard
1356 Install_Private_With_Clauses (Body_Id);
1359 -- Now we can go on to analyze the body
1361 HSS := Handled_Statement_Sequence (N);
1362 Set_Actual_Subtypes (N, Current_Scope);
1363 Analyze_Declarations (Declarations (N));
1366 Process_End_Label (HSS, 't', Current_Scope);
1368 Check_Subprogram_Order (N);
1369 Set_Analyzed (Body_Id);
1371 -- If we have a separate spec, then the analysis of the declarations
1372 -- caused the entities in the body to be chained to the spec id, but
1373 -- we want them chained to the body id. Only the formal parameters
1374 -- end up chained to the spec id in this case.
1376 if Present (Spec_Id) then
1378 -- If a parent unit is categorized, the context of a subunit must
1379 -- conform to the categorization. Conversely, if a child unit is
1380 -- categorized, the parents themselves must conform.
1382 if Nkind (Parent (N)) = N_Subunit then
1383 Validate_Categorization_Dependency (N, Spec_Id);
1385 elsif Is_Child_Unit (Spec_Id) then
1386 Validate_Categorization_Dependency
1387 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1390 if Present (Last_Formal) then
1392 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1393 Set_Next_Entity (Last_Formal, Empty);
1394 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1395 Set_Last_Entity (Spec_Id, Last_Formal);
1398 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1399 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1400 Set_First_Entity (Spec_Id, Empty);
1401 Set_Last_Entity (Spec_Id, Empty);
1405 -- If function, check return statements
1407 if Nkind (Body_Spec) = N_Function_Specification then
1412 if Present (Spec_Id) then
1418 if Return_Present (Id) then
1419 Check_Returns (HSS, 'F', Missing_Ret);
1422 Set_Has_Missing_Return (Id);
1425 elsif not Is_Machine_Code_Subprogram (Id)
1426 and then not Body_Deleted
1428 Error_Msg_N ("missing RETURN statement in function body", N);
1432 -- If procedure with No_Return, check returns
1434 elsif Nkind (Body_Spec) = N_Procedure_Specification
1435 and then Present (Spec_Id)
1436 and then No_Return (Spec_Id)
1438 Check_Returns (HSS, 'P', Missing_Ret);
1441 -- Now we are going to check for variables that are never modified in
1442 -- the body of the procedure. We omit these checks if the first
1443 -- statement of the procedure raises an exception. In particular this
1444 -- deals with the common idiom of a stubbed function, which might
1445 -- appear as something like
1447 -- function F (A : Integer) return Some_Type;
1450 -- raise Program_Error;
1454 -- Here the purpose of X is simply to satisfy the (annoying)
1455 -- requirement in Ada that there be at least one return, and we
1456 -- certainly do not want to go posting warnings on X that it is not
1460 Stm : Node_Id := First (Statements (HSS));
1463 -- Skip an initial label (for one thing this occurs when we are in
1464 -- front end ZCX mode, but in any case it is irrelevant).
1466 if Nkind (Stm) = N_Label then
1470 -- Do the test on the original statement before expansion
1473 Ostm : constant Node_Id := Original_Node (Stm);
1476 -- If explicit raise statement, return with no checks
1478 if Nkind (Ostm) = N_Raise_Statement then
1481 -- Check for explicit call cases which likely raise an exception
1483 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1484 if Is_Entity_Name (Name (Ostm)) then
1486 Ent : constant Entity_Id := Entity (Name (Ostm));
1489 -- If the procedure is marked No_Return, then likely it
1490 -- raises an exception, but in any case it is not coming
1491 -- back here, so no need to check beyond the call.
1493 if Ekind (Ent) = E_Procedure
1494 and then No_Return (Ent)
1498 -- If the procedure name is Raise_Exception, then also
1499 -- assume that it raises an exception. The main target
1500 -- here is Ada.Exceptions.Raise_Exception, but this name
1501 -- is pretty evocative in any context! Note that the
1502 -- procedure in Ada.Exceptions is not marked No_Return
1503 -- because of the annoying case of the null exception Id.
1505 elsif Chars (Ent) = Name_Raise_Exception then
1514 -- Check for variables that are never modified
1520 -- If there is a separate spec, then transfer Never_Set_In_Source
1521 -- flags from out parameters to the corresponding entities in the
1522 -- body. The reason we do that is we want to post error flags on
1523 -- the body entities, not the spec entities.
1525 if Present (Spec_Id) then
1526 E1 := First_Entity (Spec_Id);
1528 while Present (E1) loop
1529 if Ekind (E1) = E_Out_Parameter then
1530 E2 := First_Entity (Body_Id);
1531 while Present (E2) loop
1532 exit when Chars (E1) = Chars (E2);
1536 if Present (E2) then
1537 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1545 -- Check references in body unless it was deleted. Note that the
1546 -- check of Body_Deleted here is not just for efficiency, it is
1547 -- necessary to avoid junk warnings on formal parameters.
1549 if not Body_Deleted then
1550 Check_References (Body_Id);
1553 end Analyze_Subprogram_Body;
1555 ------------------------------------
1556 -- Analyze_Subprogram_Declaration --
1557 ------------------------------------
1559 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1560 Designator : constant Entity_Id :=
1561 Analyze_Subprogram_Specification (Specification (N));
1562 Scop : constant Entity_Id := Current_Scope;
1564 -- Start of processing for Analyze_Subprogram_Declaration
1567 Generate_Definition (Designator);
1569 -- Check for RCI unit subprogram declarations against in-lined
1570 -- subprograms and subprograms having access parameter or limited
1571 -- parameter without Read and Write (RM E.2.3(12-13)).
1573 Validate_RCI_Subprogram_Declaration (N);
1577 Defining_Entity (N),
1578 " Analyze subprogram spec. ");
1580 if Debug_Flag_C then
1581 Write_Str ("==== Compiling subprogram spec ");
1582 Write_Name (Chars (Designator));
1583 Write_Str (" from ");
1584 Write_Location (Sloc (N));
1588 New_Overloaded_Entity (Designator);
1589 Check_Delayed_Subprogram (Designator);
1591 -- What is the following code for, it used to be
1593 -- ??? Set_Suppress_Elaboration_Checks
1594 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1596 -- The following seems equivalent, but a bit dubious
1598 if Elaboration_Checks_Suppressed (Designator) then
1599 Set_Kill_Elaboration_Checks (Designator);
1602 if Scop /= Standard_Standard
1603 and then not Is_Child_Unit (Designator)
1605 Set_Categorization_From_Scope (Designator, Scop);
1607 -- For a compilation unit, check for library-unit pragmas
1609 New_Scope (Designator);
1610 Set_Categorization_From_Pragmas (N);
1611 Validate_Categorization_Dependency (N, Designator);
1615 -- For a compilation unit, set body required. This flag will only be
1616 -- reset if a valid Import or Interface pragma is processed later on.
1618 if Nkind (Parent (N)) = N_Compilation_Unit then
1619 Set_Body_Required (Parent (N), True);
1621 if Ada_Version >= Ada_05
1622 and then Nkind (Specification (N)) = N_Procedure_Specification
1623 and then Null_Present (Specification (N))
1626 ("null procedure cannot be declared at library level", N);
1630 Generate_Reference_To_Formals (Designator);
1631 Check_Eliminated (Designator);
1633 -- Ada 2005: if procedure is declared with "is null" qualifier,
1634 -- it requires no body.
1636 if Nkind (Specification (N)) = N_Procedure_Specification
1637 and then Null_Present (Specification (N))
1639 Set_Has_Completion (Designator);
1640 Set_Is_Inlined (Designator);
1642 end Analyze_Subprogram_Declaration;
1644 --------------------------------------
1645 -- Analyze_Subprogram_Specification --
1646 --------------------------------------
1648 -- Reminder: N here really is a subprogram specification (not a subprogram
1649 -- declaration). This procedure is called to analyze the specification in
1650 -- both subprogram bodies and subprogram declarations (specs).
1652 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1653 Designator : constant Entity_Id := Defining_Entity (N);
1654 Formals : constant List_Id := Parameter_Specifications (N);
1656 function Has_Interface_Formals (T : List_Id) return Boolean;
1657 -- Ada 2005 (AI-251): Returns true if some non class-wide interface
1660 ---------------------------
1661 -- Has_Interface_Formals --
1662 ---------------------------
1664 function Has_Interface_Formals (T : List_Id) return Boolean is
1665 Param_Spec : Node_Id;
1669 Param_Spec := First (T);
1671 while Present (Param_Spec) loop
1672 Formal := Defining_Identifier (Param_Spec);
1674 if Is_Class_Wide_Type (Etype (Formal)) then
1677 elsif Is_Interface (Etype (Formal)) then
1685 end Has_Interface_Formals;
1687 -- Start of processing for Analyze_Subprogram_Specification
1690 Generate_Definition (Designator);
1692 if Nkind (N) = N_Function_Specification then
1693 Set_Ekind (Designator, E_Function);
1694 Set_Mechanism (Designator, Default_Mechanism);
1697 Set_Ekind (Designator, E_Procedure);
1698 Set_Etype (Designator, Standard_Void_Type);
1701 -- Introduce new scope for analysis of the formals and of the
1704 Set_Scope (Designator, Current_Scope);
1706 if Present (Formals) then
1707 New_Scope (Designator);
1708 Process_Formals (Formals, N);
1710 -- Ada 2005 (AI-345): Allow overriding primitives of protected
1711 -- interfaces by means of normal subprograms. For this purpose
1712 -- temporarily use the corresponding record type as the etype
1713 -- of the first formal.
1715 if Ada_Version >= Ada_05
1716 and then Comes_From_Source (Designator)
1717 and then Present (First_Entity (Designator))
1718 and then (Ekind (Etype (First_Entity (Designator)))
1721 Ekind (Etype (First_Entity (Designator)))
1723 and then Present (Corresponding_Record_Type
1724 (Etype (First_Entity (Designator))))
1725 and then Present (Abstract_Interfaces
1726 (Corresponding_Record_Type
1727 (Etype (First_Entity (Designator)))))
1729 Set_Etype (First_Entity (Designator),
1730 Corresponding_Record_Type (Etype (First_Entity (Designator))));
1735 elsif Nkind (N) = N_Function_Specification then
1736 Analyze_Return_Type (N);
1739 if Nkind (N) = N_Function_Specification then
1740 if Nkind (Designator) = N_Defining_Operator_Symbol then
1741 Valid_Operator_Definition (Designator);
1744 May_Need_Actuals (Designator);
1746 if Is_Abstract (Etype (Designator))
1747 and then Nkind (Parent (N))
1748 /= N_Abstract_Subprogram_Declaration
1749 and then (Nkind (Parent (N)))
1750 /= N_Formal_Abstract_Subprogram_Declaration
1751 and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1752 or else not Is_Entity_Name (Name (Parent (N)))
1753 or else not Is_Abstract (Entity (Name (Parent (N)))))
1756 ("function that returns abstract type must be abstract", N);
1760 if Ada_Version >= Ada_05
1761 and then Comes_From_Source (N)
1762 and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1763 and then (Nkind (N) /= N_Procedure_Specification
1765 not Null_Present (N))
1766 and then Has_Interface_Formals (Formals)
1768 Error_Msg_Name_1 := Chars (Defining_Unit_Name
1769 (Specification (Parent (N))));
1771 ("(Ada 2005) interface subprogram % must be abstract or null", N);
1775 end Analyze_Subprogram_Specification;
1777 --------------------------
1778 -- Build_Body_To_Inline --
1779 --------------------------
1781 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1782 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1783 Original_Body : Node_Id;
1784 Body_To_Analyze : Node_Id;
1785 Max_Size : constant := 10;
1786 Stat_Count : Integer := 0;
1788 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1789 -- Check for declarations that make inlining not worthwhile
1791 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1792 -- Check for statements that make inlining not worthwhile: any tasking
1793 -- statement, nested at any level. Keep track of total number of
1794 -- elementary statements, as a measure of acceptable size.
1796 function Has_Pending_Instantiation return Boolean;
1797 -- If some enclosing body contains instantiations that appear before
1798 -- the corresponding generic body, the enclosing body has a freeze node
1799 -- so that it can be elaborated after the generic itself. This might
1800 -- conflict with subsequent inlinings, so that it is unsafe to try to
1801 -- inline in such a case.
1803 procedure Remove_Pragmas;
1804 -- A pragma Unreferenced that mentions a formal parameter has no
1805 -- meaning when the body is inlined and the formals are rewritten.
1806 -- Remove it from body to inline. The analysis of the non-inlined body
1807 -- will handle the pragma properly.
1809 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1810 -- If the body of the subprogram includes a call that returns an
1811 -- unconstrained type, the secondary stack is involved, and it
1812 -- is not worth inlining.
1814 ------------------------------
1815 -- Has_Excluded_Declaration --
1816 ------------------------------
1818 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1821 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1822 -- Nested subprograms make a given body ineligible for inlining, but
1823 -- we make an exception for instantiations of unchecked conversion.
1824 -- The body has not been analyzed yet, so check the name, and verify
1825 -- that the visible entity with that name is the predefined unit.
1827 -----------------------------
1828 -- Is_Unchecked_Conversion --
1829 -----------------------------
1831 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1832 Id : constant Node_Id := Name (D);
1836 if Nkind (Id) = N_Identifier
1837 and then Chars (Id) = Name_Unchecked_Conversion
1839 Conv := Current_Entity (Id);
1841 elsif (Nkind (Id) = N_Selected_Component
1842 or else Nkind (Id) = N_Expanded_Name)
1843 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1845 Conv := Current_Entity (Selector_Name (Id));
1851 return Present (Conv)
1852 and then Is_Predefined_File_Name
1853 (Unit_File_Name (Get_Source_Unit (Conv)))
1854 and then Is_Intrinsic_Subprogram (Conv);
1855 end Is_Unchecked_Conversion;
1857 -- Start of processing for Has_Excluded_Declaration
1862 while Present (D) loop
1863 if (Nkind (D) = N_Function_Instantiation
1864 and then not Is_Unchecked_Conversion (D))
1865 or else Nkind (D) = N_Protected_Type_Declaration
1866 or else Nkind (D) = N_Package_Declaration
1867 or else Nkind (D) = N_Package_Instantiation
1868 or else Nkind (D) = N_Subprogram_Body
1869 or else Nkind (D) = N_Procedure_Instantiation
1870 or else Nkind (D) = N_Task_Type_Declaration
1873 ("cannot inline & (non-allowed declaration)?", D, Subp);
1881 end Has_Excluded_Declaration;
1883 ----------------------------
1884 -- Has_Excluded_Statement --
1885 ----------------------------
1887 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1894 while Present (S) loop
1895 Stat_Count := Stat_Count + 1;
1897 if Nkind (S) = N_Abort_Statement
1898 or else Nkind (S) = N_Asynchronous_Select
1899 or else Nkind (S) = N_Conditional_Entry_Call
1900 or else Nkind (S) = N_Delay_Relative_Statement
1901 or else Nkind (S) = N_Delay_Until_Statement
1902 or else Nkind (S) = N_Selective_Accept
1903 or else Nkind (S) = N_Timed_Entry_Call
1906 ("cannot inline & (non-allowed statement)?", S, Subp);
1909 elsif Nkind (S) = N_Block_Statement then
1910 if Present (Declarations (S))
1911 and then Has_Excluded_Declaration (Declarations (S))
1915 elsif Present (Handled_Statement_Sequence (S))
1918 (Exception_Handlers (Handled_Statement_Sequence (S)))
1920 Has_Excluded_Statement
1921 (Statements (Handled_Statement_Sequence (S))))
1926 elsif Nkind (S) = N_Case_Statement then
1927 E := First (Alternatives (S));
1928 while Present (E) loop
1929 if Has_Excluded_Statement (Statements (E)) then
1936 elsif Nkind (S) = N_If_Statement then
1937 if Has_Excluded_Statement (Then_Statements (S)) then
1941 if Present (Elsif_Parts (S)) then
1942 E := First (Elsif_Parts (S));
1943 while Present (E) loop
1944 if Has_Excluded_Statement (Then_Statements (E)) then
1951 if Present (Else_Statements (S))
1952 and then Has_Excluded_Statement (Else_Statements (S))
1957 elsif Nkind (S) = N_Loop_Statement
1958 and then Has_Excluded_Statement (Statements (S))
1967 end Has_Excluded_Statement;
1969 -------------------------------
1970 -- Has_Pending_Instantiation --
1971 -------------------------------
1973 function Has_Pending_Instantiation return Boolean is
1974 S : Entity_Id := Current_Scope;
1977 while Present (S) loop
1978 if Is_Compilation_Unit (S)
1979 or else Is_Child_Unit (S)
1982 elsif Ekind (S) = E_Package
1983 and then Has_Forward_Instantiation (S)
1992 end Has_Pending_Instantiation;
1994 --------------------
1995 -- Remove_Pragmas --
1996 --------------------
1998 procedure Remove_Pragmas is
2003 Decl := First (Declarations (Body_To_Analyze));
2004 while Present (Decl) loop
2007 if Nkind (Decl) = N_Pragma
2008 and then Chars (Decl) = Name_Unreferenced
2017 --------------------------
2018 -- Uses_Secondary_Stack --
2019 --------------------------
2021 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
2022 function Check_Call (N : Node_Id) return Traverse_Result;
2023 -- Look for function calls that return an unconstrained type
2029 function Check_Call (N : Node_Id) return Traverse_Result is
2031 if Nkind (N) = N_Function_Call
2032 and then Is_Entity_Name (Name (N))
2033 and then Is_Composite_Type (Etype (Entity (Name (N))))
2034 and then not Is_Constrained (Etype (Entity (Name (N))))
2037 ("cannot inline & (call returns unconstrained type)?",
2045 function Check_Calls is new Traverse_Func (Check_Call);
2048 return Check_Calls (Bod) = Abandon;
2049 end Uses_Secondary_Stack;
2051 -- Start of processing for Build_Body_To_Inline
2054 if Nkind (Decl) = N_Subprogram_Declaration
2055 and then Present (Body_To_Inline (Decl))
2057 return; -- Done already.
2059 -- Functions that return unconstrained composite types will require
2060 -- secondary stack handling, and cannot currently be inlined.
2061 -- Ditto for functions that return controlled types, where controlled
2062 -- actions interfere in complex ways with inlining.
2064 elsif Ekind (Subp) = E_Function
2065 and then not Is_Scalar_Type (Etype (Subp))
2066 and then not Is_Access_Type (Etype (Subp))
2067 and then not Is_Constrained (Etype (Subp))
2070 ("cannot inline & (unconstrained return type)?", N, Subp);
2073 elsif Ekind (Subp) = E_Function
2074 and then Controlled_Type (Etype (Subp))
2077 ("cannot inline & (controlled return type)?", N, Subp);
2081 if Present (Declarations (N))
2082 and then Has_Excluded_Declaration (Declarations (N))
2087 if Present (Handled_Statement_Sequence (N)) then
2088 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
2090 ("cannot inline& (exception handler)?",
2091 First (Exception_Handlers (Handled_Statement_Sequence (N))),
2095 Has_Excluded_Statement
2096 (Statements (Handled_Statement_Sequence (N)))
2102 -- We do not inline a subprogram that is too large, unless it is
2103 -- marked Inline_Always. This pragma does not suppress the other
2104 -- checks on inlining (forbidden declarations, handlers, etc).
2106 if Stat_Count > Max_Size
2107 and then not Is_Always_Inlined (Subp)
2109 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
2113 if Has_Pending_Instantiation then
2115 ("cannot inline& (forward instance within enclosing body)?",
2120 -- Within an instance, the body to inline must be treated as a nested
2121 -- generic, so that the proper global references are preserved.
2124 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2125 Original_Body := Copy_Generic_Node (N, Empty, True);
2127 Original_Body := Copy_Separate_Tree (N);
2130 -- We need to capture references to the formals in order to substitute
2131 -- the actuals at the point of inlining, i.e. instantiation. To treat
2132 -- the formals as globals to the body to inline, we nest it within
2133 -- a dummy parameterless subprogram, declared within the real one.
2134 -- To avoid generating an internal name (which is never public, and
2135 -- which affects serial numbers of other generated names), we use
2136 -- an internal symbol that cannot conflict with user declarations.
2138 Set_Parameter_Specifications (Specification (Original_Body), No_List);
2139 Set_Defining_Unit_Name
2140 (Specification (Original_Body),
2141 Make_Defining_Identifier (Sloc (N), Name_uParent));
2142 Set_Corresponding_Spec (Original_Body, Empty);
2144 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
2146 -- Set return type of function, which is also global and does not need
2149 if Ekind (Subp) = E_Function then
2150 Set_Result_Definition (Specification (Body_To_Analyze),
2151 New_Occurrence_Of (Etype (Subp), Sloc (N)));
2154 if No (Declarations (N)) then
2155 Set_Declarations (N, New_List (Body_To_Analyze));
2157 Append (Body_To_Analyze, Declarations (N));
2160 Expander_Mode_Save_And_Set (False);
2163 Analyze (Body_To_Analyze);
2164 New_Scope (Defining_Entity (Body_To_Analyze));
2165 Save_Global_References (Original_Body);
2167 Remove (Body_To_Analyze);
2169 Expander_Mode_Restore;
2175 -- If secondary stk used there is no point in inlining. We have
2176 -- already issued the warning in this case, so nothing to do.
2178 if Uses_Secondary_Stack (Body_To_Analyze) then
2182 Set_Body_To_Inline (Decl, Original_Body);
2183 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
2184 Set_Is_Inlined (Subp);
2185 end Build_Body_To_Inline;
2191 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
2193 -- Do not emit warning if this is a predefined unit which is not
2194 -- the main unit. With validity checks enabled, some predefined
2195 -- subprograms may contain nested subprograms and become ineligible
2198 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
2199 and then not In_Extended_Main_Source_Unit (Subp)
2203 elsif Is_Always_Inlined (Subp) then
2205 -- Remove last character (question mark) to make this into an error,
2206 -- because the Inline_Always pragma cannot be obeyed.
2208 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2210 elsif Ineffective_Inline_Warnings then
2211 Error_Msg_NE (Msg, N, Subp);
2215 -----------------------
2216 -- Check_Conformance --
2217 -----------------------
2219 procedure Check_Conformance
2220 (New_Id : Entity_Id;
2222 Ctype : Conformance_Type;
2224 Conforms : out Boolean;
2225 Err_Loc : Node_Id := Empty;
2226 Get_Inst : Boolean := False;
2227 Skip_Controlling_Formals : Boolean := False)
2229 Old_Type : constant Entity_Id := Etype (Old_Id);
2230 New_Type : constant Entity_Id := Etype (New_Id);
2231 Old_Formal : Entity_Id;
2232 New_Formal : Entity_Id;
2234 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2235 -- Post error message for conformance error on given node. Two messages
2236 -- are output. The first points to the previous declaration with a
2237 -- general "no conformance" message. The second is the detailed reason,
2238 -- supplied as Msg. The parameter N provide information for a possible
2239 -- & insertion in the message, and also provides the location for
2240 -- posting the message in the absence of a specified Err_Loc location.
2242 -----------------------
2243 -- Conformance_Error --
2244 -----------------------
2246 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2253 if No (Err_Loc) then
2259 Error_Msg_Sloc := Sloc (Old_Id);
2262 when Type_Conformant =>
2264 ("not type conformant with declaration#!", Enode);
2266 when Mode_Conformant =>
2268 ("not mode conformant with declaration#!", Enode);
2270 when Subtype_Conformant =>
2272 ("not subtype conformant with declaration#!", Enode);
2274 when Fully_Conformant =>
2276 ("not fully conformant with declaration#!", Enode);
2279 Error_Msg_NE (Msg, Enode, N);
2281 end Conformance_Error;
2283 -- Start of processing for Check_Conformance
2288 -- We need a special case for operators, since they don't appear
2291 if Ctype = Type_Conformant then
2292 if Ekind (New_Id) = E_Operator
2293 and then Operator_Matches_Spec (New_Id, Old_Id)
2299 -- If both are functions/operators, check return types conform
2301 if Old_Type /= Standard_Void_Type
2302 and then New_Type /= Standard_Void_Type
2304 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2305 Conformance_Error ("return type does not match!", New_Id);
2309 -- Ada 2005 (AI-231): In case of anonymous access types check the
2310 -- null-exclusion and access-to-constant attributes must match.
2312 if Ada_Version >= Ada_05
2313 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
2315 (Can_Never_Be_Null (Old_Type)
2316 /= Can_Never_Be_Null (New_Type)
2317 or else Is_Access_Constant (Etype (Old_Type))
2318 /= Is_Access_Constant (Etype (New_Type)))
2320 Conformance_Error ("return type does not match!", New_Id);
2324 -- If either is a function/operator and the other isn't, error
2326 elsif Old_Type /= Standard_Void_Type
2327 or else New_Type /= Standard_Void_Type
2329 Conformance_Error ("functions can only match functions!", New_Id);
2333 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2334 -- If this is a renaming as body, refine error message to indicate that
2335 -- the conflict is with the original declaration. If the entity is not
2336 -- frozen, the conventions don't have to match, the one of the renamed
2337 -- entity is inherited.
2339 if Ctype >= Subtype_Conformant then
2340 if Convention (Old_Id) /= Convention (New_Id) then
2342 if not Is_Frozen (New_Id) then
2345 elsif Present (Err_Loc)
2346 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2347 and then Present (Corresponding_Spec (Err_Loc))
2349 Error_Msg_Name_1 := Chars (New_Id);
2351 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2353 Conformance_Error ("prior declaration for% has convention %!");
2356 Conformance_Error ("calling conventions do not match!");
2361 elsif Is_Formal_Subprogram (Old_Id)
2362 or else Is_Formal_Subprogram (New_Id)
2364 Conformance_Error ("formal subprograms not allowed!");
2369 -- Deal with parameters
2371 -- Note: we use the entity information, rather than going directly
2372 -- to the specification in the tree. This is not only simpler, but
2373 -- absolutely necessary for some cases of conformance tests between
2374 -- operators, where the declaration tree simply does not exist!
2376 Old_Formal := First_Formal (Old_Id);
2377 New_Formal := First_Formal (New_Id);
2379 while Present (Old_Formal) and then Present (New_Formal) loop
2380 if Is_Controlling_Formal (Old_Formal)
2381 and then Is_Controlling_Formal (New_Formal)
2382 and then Skip_Controlling_Formals
2384 goto Skip_Controlling_Formal;
2387 if Ctype = Fully_Conformant then
2389 -- Names must match. Error message is more accurate if we do
2390 -- this before checking that the types of the formals match.
2392 if Chars (Old_Formal) /= Chars (New_Formal) then
2393 Conformance_Error ("name & does not match!", New_Formal);
2395 -- Set error posted flag on new formal as well to stop
2396 -- junk cascaded messages in some cases.
2398 Set_Error_Posted (New_Formal);
2403 -- Types must always match. In the visible part of an instance,
2404 -- usual overloading rules for dispatching operations apply, and
2405 -- we check base types (not the actual subtypes).
2407 if In_Instance_Visible_Part
2408 and then Is_Dispatching_Operation (New_Id)
2410 if not Conforming_Types
2411 (Base_Type (Etype (Old_Formal)),
2412 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2414 Conformance_Error ("type of & does not match!", New_Formal);
2418 elsif not Conforming_Types
2419 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2421 Conformance_Error ("type of & does not match!", New_Formal);
2425 -- For mode conformance, mode must match
2427 if Ctype >= Mode_Conformant
2428 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2430 Conformance_Error ("mode of & does not match!", New_Formal);
2434 -- Full conformance checks
2436 if Ctype = Fully_Conformant then
2438 -- We have checked already that names match
2440 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2442 -- Ada 2005 (AI-231): In case of anonymous access types check
2443 -- the null-exclusion and access-to-constant attributes must
2446 if Ada_Version >= Ada_05
2447 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
2449 (Can_Never_Be_Null (Old_Formal)
2450 /= Can_Never_Be_Null (New_Formal)
2451 or else Is_Access_Constant (Etype (Old_Formal))
2452 /= Is_Access_Constant (Etype (New_Formal)))
2455 ("type of & does not match!", New_Formal);
2459 -- Check default expressions for in parameters
2462 NewD : constant Boolean :=
2463 Present (Default_Value (New_Formal));
2464 OldD : constant Boolean :=
2465 Present (Default_Value (Old_Formal));
2467 if NewD or OldD then
2469 -- The old default value has been analyzed because the
2470 -- current full declaration will have frozen everything
2471 -- before. The new default values have not been
2472 -- analyzed, so analyze them now before we check for
2477 Analyze_Per_Use_Expression
2478 (Default_Value (New_Formal), Etype (New_Formal));
2482 if not (NewD and OldD)
2483 or else not Fully_Conformant_Expressions
2484 (Default_Value (Old_Formal),
2485 Default_Value (New_Formal))
2488 ("default expression for & does not match!",
2497 -- A couple of special checks for Ada 83 mode. These checks are
2498 -- skipped if either entity is an operator in package Standard.
2499 -- or if either old or new instance is not from the source program.
2501 if Ada_Version = Ada_83
2502 and then Sloc (Old_Id) > Standard_Location
2503 and then Sloc (New_Id) > Standard_Location
2504 and then Comes_From_Source (Old_Id)
2505 and then Comes_From_Source (New_Id)
2508 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2509 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2512 -- Explicit IN must be present or absent in both cases. This
2513 -- test is required only in the full conformance case.
2515 if In_Present (Old_Param) /= In_Present (New_Param)
2516 and then Ctype = Fully_Conformant
2519 ("(Ada 83) IN must appear in both declarations",
2524 -- Grouping (use of comma in param lists) must be the same
2525 -- This is where we catch a misconformance like:
2528 -- A : Integer; B : Integer
2530 -- which are represented identically in the tree except
2531 -- for the setting of the flags More_Ids and Prev_Ids.
2533 if More_Ids (Old_Param) /= More_Ids (New_Param)
2534 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2537 ("grouping of & does not match!", New_Formal);
2543 -- This label is required when skipping controlling formals
2545 <<Skip_Controlling_Formal>>
2547 Next_Formal (Old_Formal);
2548 Next_Formal (New_Formal);
2551 if Present (Old_Formal) then
2552 Conformance_Error ("too few parameters!");
2555 elsif Present (New_Formal) then
2556 Conformance_Error ("too many parameters!", New_Formal);
2559 end Check_Conformance;
2561 ------------------------------
2562 -- Check_Delayed_Subprogram --
2563 ------------------------------
2565 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2568 procedure Possible_Freeze (T : Entity_Id);
2569 -- T is the type of either a formal parameter or of the return type.
2570 -- If T is not yet frozen and needs a delayed freeze, then the
2571 -- subprogram itself must be delayed.
2573 ---------------------
2574 -- Possible_Freeze --
2575 ---------------------
2577 procedure Possible_Freeze (T : Entity_Id) is
2579 if Has_Delayed_Freeze (T)
2580 and then not Is_Frozen (T)
2582 Set_Has_Delayed_Freeze (Designator);
2584 elsif Is_Access_Type (T)
2585 and then Has_Delayed_Freeze (Designated_Type (T))
2586 and then not Is_Frozen (Designated_Type (T))
2588 Set_Has_Delayed_Freeze (Designator);
2590 end Possible_Freeze;
2592 -- Start of processing for Check_Delayed_Subprogram
2595 -- Never need to freeze abstract subprogram
2597 if Is_Abstract (Designator) then
2600 -- Need delayed freeze if return type itself needs a delayed
2601 -- freeze and is not yet frozen.
2603 Possible_Freeze (Etype (Designator));
2604 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2606 -- Need delayed freeze if any of the formal types themselves need
2607 -- a delayed freeze and are not yet frozen.
2609 F := First_Formal (Designator);
2610 while Present (F) loop
2611 Possible_Freeze (Etype (F));
2612 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2617 -- Mark functions that return by reference. Note that it cannot be
2618 -- done for delayed_freeze subprograms because the underlying
2619 -- returned type may not be known yet (for private types)
2621 if not Has_Delayed_Freeze (Designator)
2622 and then Expander_Active
2625 Typ : constant Entity_Id := Etype (Designator);
2626 Utyp : constant Entity_Id := Underlying_Type (Typ);
2629 if Is_Return_By_Reference_Type (Typ) then
2630 Set_Returns_By_Ref (Designator);
2632 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2633 Set_Returns_By_Ref (Designator);
2637 end Check_Delayed_Subprogram;
2639 ------------------------------------
2640 -- Check_Discriminant_Conformance --
2641 ------------------------------------
2643 procedure Check_Discriminant_Conformance
2648 Old_Discr : Entity_Id := First_Discriminant (Prev);
2649 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2650 New_Discr_Id : Entity_Id;
2651 New_Discr_Type : Entity_Id;
2653 procedure Conformance_Error (Msg : String; N : Node_Id);
2654 -- Post error message for conformance error on given node. Two messages
2655 -- are output. The first points to the previous declaration with a
2656 -- general "no conformance" message. The second is the detailed reason,
2657 -- supplied as Msg. The parameter N provide information for a possible
2658 -- & insertion in the message.
2660 -----------------------
2661 -- Conformance_Error --
2662 -----------------------
2664 procedure Conformance_Error (Msg : String; N : Node_Id) is
2666 Error_Msg_Sloc := Sloc (Prev_Loc);
2667 Error_Msg_N ("not fully conformant with declaration#!", N);
2668 Error_Msg_NE (Msg, N, N);
2669 end Conformance_Error;
2671 -- Start of processing for Check_Discriminant_Conformance
2674 while Present (Old_Discr) and then Present (New_Discr) loop
2676 New_Discr_Id := Defining_Identifier (New_Discr);
2678 -- The subtype mark of the discriminant on the full type has not
2679 -- been analyzed so we do it here. For an access discriminant a new
2682 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2684 Access_Definition (N, Discriminant_Type (New_Discr));
2687 Analyze (Discriminant_Type (New_Discr));
2688 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2691 if not Conforming_Types
2692 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2694 Conformance_Error ("type of & does not match!", New_Discr_Id);
2697 -- Treat the new discriminant as an occurrence of the old one,
2698 -- for navigation purposes, and fill in some semantic
2699 -- information, for completeness.
2701 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2702 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2703 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2708 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2709 Conformance_Error ("name & does not match!", New_Discr_Id);
2713 -- Default expressions must match
2716 NewD : constant Boolean :=
2717 Present (Expression (New_Discr));
2718 OldD : constant Boolean :=
2719 Present (Expression (Parent (Old_Discr)));
2722 if NewD or OldD then
2724 -- The old default value has been analyzed and expanded,
2725 -- because the current full declaration will have frozen
2726 -- everything before. The new default values have not been
2727 -- expanded, so expand now to check conformance.
2730 Analyze_Per_Use_Expression
2731 (Expression (New_Discr), New_Discr_Type);
2734 if not (NewD and OldD)
2735 or else not Fully_Conformant_Expressions
2736 (Expression (Parent (Old_Discr)),
2737 Expression (New_Discr))
2741 ("default expression for & does not match!",
2748 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2750 if Ada_Version = Ada_83 then
2752 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2755 -- Grouping (use of comma in param lists) must be the same
2756 -- This is where we catch a misconformance like:
2759 -- A : Integer; B : Integer
2761 -- which are represented identically in the tree except
2762 -- for the setting of the flags More_Ids and Prev_Ids.
2764 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2765 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2768 ("grouping of & does not match!", New_Discr_Id);
2774 Next_Discriminant (Old_Discr);
2778 if Present (Old_Discr) then
2779 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2782 elsif Present (New_Discr) then
2784 ("too many discriminants!", Defining_Identifier (New_Discr));
2787 end Check_Discriminant_Conformance;
2789 ----------------------------
2790 -- Check_Fully_Conformant --
2791 ----------------------------
2793 procedure Check_Fully_Conformant
2794 (New_Id : Entity_Id;
2796 Err_Loc : Node_Id := Empty)
2801 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2802 end Check_Fully_Conformant;
2804 ---------------------------
2805 -- Check_Mode_Conformant --
2806 ---------------------------
2808 procedure Check_Mode_Conformant
2809 (New_Id : Entity_Id;
2811 Err_Loc : Node_Id := Empty;
2812 Get_Inst : Boolean := False)
2818 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2819 end Check_Mode_Conformant;
2821 --------------------------------
2822 -- Check_Overriding_Indicator --
2823 --------------------------------
2825 procedure Check_Overriding_Indicator
2827 Does_Override : Boolean)
2833 if Ekind (Subp) = E_Enumeration_Literal then
2835 -- No overriding indicator for literals
2840 Decl := Unit_Declaration_Node (Subp);
2843 if Nkind (Decl) = N_Subprogram_Declaration
2844 or else Nkind (Decl) = N_Subprogram_Body
2845 or else Nkind (Decl) = N_Subprogram_Renaming_Declaration
2846 or else Nkind (Decl) = N_Subprogram_Body_Stub
2848 Spec := Specification (Decl);
2853 if not Does_Override then
2854 if Must_Override (Spec) then
2855 Error_Msg_NE ("subprogram& is not overriding", Spec, Subp);
2859 if Must_Not_Override (Spec) then
2861 ("subprogram& overrides inherited operation", Spec, Subp);
2864 end Check_Overriding_Indicator;
2870 procedure Check_Returns
2877 procedure Check_Statement_Sequence (L : List_Id);
2878 -- Internal recursive procedure to check a list of statements for proper
2879 -- termination by a return statement (or a transfer of control or a
2880 -- compound statement that is itself internally properly terminated).
2882 ------------------------------
2883 -- Check_Statement_Sequence --
2884 ------------------------------
2886 procedure Check_Statement_Sequence (L : List_Id) is
2890 Raise_Exception_Call : Boolean;
2891 -- Set True if statement sequence terminated by Raise_Exception call
2892 -- or a Reraise_Occurrence call.
2895 Raise_Exception_Call := False;
2897 -- Get last real statement
2899 Last_Stm := Last (L);
2901 -- Don't count pragmas
2903 while Nkind (Last_Stm) = N_Pragma
2905 -- Don't count call to SS_Release (can happen after Raise_Exception)
2908 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2910 Nkind (Name (Last_Stm)) = N_Identifier
2912 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2914 -- Don't count exception junk
2917 ((Nkind (Last_Stm) = N_Goto_Statement
2918 or else Nkind (Last_Stm) = N_Label
2919 or else Nkind (Last_Stm) = N_Object_Declaration)
2920 and then Exception_Junk (Last_Stm))
2925 -- Here we have the "real" last statement
2927 Kind := Nkind (Last_Stm);
2929 -- Transfer of control, OK. Note that in the No_Return procedure
2930 -- case, we already diagnosed any explicit return statements, so
2931 -- we can treat them as OK in this context.
2933 if Is_Transfer (Last_Stm) then
2936 -- Check cases of explicit non-indirect procedure calls
2938 elsif Kind = N_Procedure_Call_Statement
2939 and then Is_Entity_Name (Name (Last_Stm))
2941 -- Check call to Raise_Exception procedure which is treated
2942 -- specially, as is a call to Reraise_Occurrence.
2944 -- We suppress the warning in these cases since it is likely that
2945 -- the programmer really does not expect to deal with the case
2946 -- of Null_Occurrence, and thus would find a warning about a
2947 -- missing return curious, and raising Program_Error does not
2948 -- seem such a bad behavior if this does occur.
2950 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2952 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2954 Raise_Exception_Call := True;
2956 -- For Raise_Exception call, test first argument, if it is
2957 -- an attribute reference for a 'Identity call, then we know
2958 -- that the call cannot possibly return.
2961 Arg : constant Node_Id :=
2962 Original_Node (First_Actual (Last_Stm));
2965 if Nkind (Arg) = N_Attribute_Reference
2966 and then Attribute_Name (Arg) = Name_Identity
2973 -- If statement, need to look inside if there is an else and check
2974 -- each constituent statement sequence for proper termination.
2976 elsif Kind = N_If_Statement
2977 and then Present (Else_Statements (Last_Stm))
2979 Check_Statement_Sequence (Then_Statements (Last_Stm));
2980 Check_Statement_Sequence (Else_Statements (Last_Stm));
2982 if Present (Elsif_Parts (Last_Stm)) then
2984 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2987 while Present (Elsif_Part) loop
2988 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2996 -- Case statement, check each case for proper termination
2998 elsif Kind = N_Case_Statement then
3003 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
3004 while Present (Case_Alt) loop
3005 Check_Statement_Sequence (Statements (Case_Alt));
3006 Next_Non_Pragma (Case_Alt);
3012 -- Block statement, check its handled sequence of statements
3014 elsif Kind = N_Block_Statement then
3020 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
3029 -- Loop statement. If there is an iteration scheme, we can definitely
3030 -- fall out of the loop. Similarly if there is an exit statement, we
3031 -- can fall out. In either case we need a following return.
3033 elsif Kind = N_Loop_Statement then
3034 if Present (Iteration_Scheme (Last_Stm))
3035 or else Has_Exit (Entity (Identifier (Last_Stm)))
3039 -- A loop with no exit statement or iteration scheme if either
3040 -- an inifite loop, or it has some other exit (raise/return).
3041 -- In either case, no warning is required.
3047 -- Timed entry call, check entry call and delay alternatives
3049 -- Note: in expanded code, the timed entry call has been converted
3050 -- to a set of expanded statements on which the check will work
3051 -- correctly in any case.
3053 elsif Kind = N_Timed_Entry_Call then
3055 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3056 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
3059 -- If statement sequence of entry call alternative is missing,
3060 -- then we can definitely fall through, and we post the error
3061 -- message on the entry call alternative itself.
3063 if No (Statements (ECA)) then
3066 -- If statement sequence of delay alternative is missing, then
3067 -- we can definitely fall through, and we post the error
3068 -- message on the delay alternative itself.
3070 -- Note: if both ECA and DCA are missing the return, then we
3071 -- post only one message, should be enough to fix the bugs.
3072 -- If not we will get a message next time on the DCA when the
3075 elsif No (Statements (DCA)) then
3078 -- Else check both statement sequences
3081 Check_Statement_Sequence (Statements (ECA));
3082 Check_Statement_Sequence (Statements (DCA));
3087 -- Conditional entry call, check entry call and else part
3089 -- Note: in expanded code, the conditional entry call has been
3090 -- converted to a set of expanded statements on which the check
3091 -- will work correctly in any case.
3093 elsif Kind = N_Conditional_Entry_Call then
3095 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3098 -- If statement sequence of entry call alternative is missing,
3099 -- then we can definitely fall through, and we post the error
3100 -- message on the entry call alternative itself.
3102 if No (Statements (ECA)) then
3105 -- Else check statement sequence and else part
3108 Check_Statement_Sequence (Statements (ECA));
3109 Check_Statement_Sequence (Else_Statements (Last_Stm));
3115 -- If we fall through, issue appropriate message
3119 if not Raise_Exception_Call then
3121 ("?RETURN statement missing following this statement!",
3124 ("\?Program_Error may be raised at run time",
3128 -- Note: we set Err even though we have not issued a warning
3129 -- because we still have a case of a missing return. This is
3130 -- an extremely marginal case, probably will never be noticed
3131 -- but we might as well get it right.
3137 ("implied return after this statement not allowed (No_Return)",
3140 end Check_Statement_Sequence;
3142 -- Start of processing for Check_Returns
3146 Check_Statement_Sequence (Statements (HSS));
3148 if Present (Exception_Handlers (HSS)) then
3149 Handler := First_Non_Pragma (Exception_Handlers (HSS));
3150 while Present (Handler) loop
3151 Check_Statement_Sequence (Statements (Handler));
3152 Next_Non_Pragma (Handler);
3157 ----------------------------
3158 -- Check_Subprogram_Order --
3159 ----------------------------
3161 procedure Check_Subprogram_Order (N : Node_Id) is
3163 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
3164 -- This is used to check if S1 > S2 in the sense required by this
3165 -- test, for example nameab < namec, but name2 < name10.
3167 -----------------------------
3168 -- Subprogram_Name_Greater --
3169 -----------------------------
3171 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
3176 -- Remove trailing numeric parts
3179 while S1 (L1) in '0' .. '9' loop
3184 while S2 (L2) in '0' .. '9' loop
3188 -- If non-numeric parts non-equal, that's decisive
3190 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
3193 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3196 -- If non-numeric parts equal, compare suffixed numeric parts. Note
3197 -- that a missing suffix is treated as numeric zero in this test.
3201 while L1 < S1'Last loop
3203 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3207 while L2 < S2'Last loop
3209 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3214 end Subprogram_Name_Greater;
3216 -- Start of processing for Check_Subprogram_Order
3219 -- Check body in alpha order if this is option
3222 and then Style_Check_Order_Subprograms
3223 and then Nkind (N) = N_Subprogram_Body
3224 and then Comes_From_Source (N)
3225 and then In_Extended_Main_Source_Unit (N)
3229 renames Scope_Stack.Table
3230 (Scope_Stack.Last).Last_Subprogram_Name;
3232 Body_Id : constant Entity_Id :=
3233 Defining_Entity (Specification (N));
3236 Get_Decoded_Name_String (Chars (Body_Id));
3239 if Subprogram_Name_Greater
3240 (LSN.all, Name_Buffer (1 .. Name_Len))
3242 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3248 LSN := new String'(Name_Buffer (1 .. Name_Len));
3251 end Check_Subprogram_Order;
3253 ------------------------------
3254 -- Check_Subtype_Conformant --
3255 ------------------------------
3257 procedure Check_Subtype_Conformant
3258 (New_Id : Entity_Id;
3260 Err_Loc : Node_Id := Empty)
3265 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3266 end Check_Subtype_Conformant;
3268 ---------------------------
3269 -- Check_Type_Conformant --
3270 ---------------------------
3272 procedure Check_Type_Conformant
3273 (New_Id : Entity_Id;
3275 Err_Loc : Node_Id := Empty)
3280 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3281 end Check_Type_Conformant;
3283 ----------------------
3284 -- Conforming_Types --
3285 ----------------------
3287 function Conforming_Types
3290 Ctype : Conformance_Type;
3291 Get_Inst : Boolean := False) return Boolean
3293 Type_1 : Entity_Id := T1;
3294 Type_2 : Entity_Id := T2;
3295 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3297 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3298 -- If neither T1 nor T2 are generic actual types, or if they are
3299 -- in different scopes (e.g. parent and child instances), then verify
3300 -- that the base types are equal. Otherwise T1 and T2 must be
3301 -- on the same subtype chain. The whole purpose of this procedure
3302 -- is to prevent spurious ambiguities in an instantiation that may
3303 -- arise if two distinct generic types are instantiated with the
3306 ----------------------
3307 -- Base_Types_Match --
3308 ----------------------
3310 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3315 elsif Base_Type (T1) = Base_Type (T2) then
3317 -- The following is too permissive. A more precise test must
3318 -- check that the generic actual is an ancestor subtype of the
3321 return not Is_Generic_Actual_Type (T1)
3322 or else not Is_Generic_Actual_Type (T2)
3323 or else Scope (T1) /= Scope (T2);
3325 -- In some cases a type imported through a limited_with clause,
3326 -- and its non-limited view are both visible, for example in an
3327 -- anonymous access_to_classwide type in a formal. Both entities
3328 -- designate the same type.
3330 elsif From_With_Type (T1)
3331 and then Ekind (T1) = E_Incomplete_Type
3332 and then T2 = Non_Limited_View (T1)
3336 elsif From_With_Type (T2)
3337 and then Ekind (T2) = E_Incomplete_Type
3338 and then T1 = Non_Limited_View (T2)
3345 end Base_Types_Match;
3347 -- Start of processing for Conforming_Types
3350 -- The context is an instance association for a formal
3351 -- access-to-subprogram type; the formal parameter types require
3352 -- mapping because they may denote other formal parameters of the
3356 Type_1 := Get_Instance_Of (T1);
3357 Type_2 := Get_Instance_Of (T2);
3360 -- First see if base types match
3362 if Base_Types_Match (Type_1, Type_2) then
3363 return Ctype <= Mode_Conformant
3364 or else Subtypes_Statically_Match (Type_1, Type_2);
3366 elsif Is_Incomplete_Or_Private_Type (Type_1)
3367 and then Present (Full_View (Type_1))
3368 and then Base_Types_Match (Full_View (Type_1), Type_2)
3370 return Ctype <= Mode_Conformant
3371 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3373 elsif Ekind (Type_2) = E_Incomplete_Type
3374 and then Present (Full_View (Type_2))
3375 and then Base_Types_Match (Type_1, Full_View (Type_2))
3377 return Ctype <= Mode_Conformant
3378 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3380 elsif Is_Private_Type (Type_2)
3381 and then In_Instance
3382 and then Present (Full_View (Type_2))
3383 and then Base_Types_Match (Type_1, Full_View (Type_2))
3385 return Ctype <= Mode_Conformant
3386 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3389 -- Ada 2005 (AI-254): Anonymous access to subprogram types must be
3390 -- treated recursively because they carry a signature.
3392 Are_Anonymous_Access_To_Subprogram_Types :=
3394 -- Case 1: Anonymous access to subprogram types
3396 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3397 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3399 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3400 -- case the anonymous type_declaration has been replaced by an
3401 -- occurrence of an internal access to subprogram type declaration
3402 -- available through the Original_Access_Type attribute
3405 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3406 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3407 and then not Comes_From_Source (Type_1)
3408 and then not Comes_From_Source (Type_2)
3409 and then Present (Original_Access_Type (Type_1))
3410 and then Present (Original_Access_Type (Type_2))
3411 and then Ekind (Original_Access_Type (Type_1)) =
3412 E_Anonymous_Access_Protected_Subprogram_Type
3413 and then Ekind (Original_Access_Type (Type_2)) =
3414 E_Anonymous_Access_Protected_Subprogram_Type);
3416 -- Test anonymous access type case. For this case, static subtype
3417 -- matching is required for mode conformance (RM 6.3.1(15))
3419 if (Ekind (Type_1) = E_Anonymous_Access_Type
3420 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3421 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3424 Desig_1 : Entity_Id;
3425 Desig_2 : Entity_Id;
3428 Desig_1 := Directly_Designated_Type (Type_1);
3430 -- An access parameter can designate an incomplete type
3431 -- If the incomplete type is the limited view of a type
3432 -- from a limited_with_clause, check whether the non-limited
3433 -- view is available.
3435 if Ekind (Desig_1) = E_Incomplete_Type then
3436 if Present (Full_View (Desig_1)) then
3437 Desig_1 := Full_View (Desig_1);
3439 elsif Present (Non_Limited_View (Desig_1)) then
3440 Desig_1 := Non_Limited_View (Desig_1);
3444 Desig_2 := Directly_Designated_Type (Type_2);
3446 if Ekind (Desig_2) = E_Incomplete_Type then
3447 if Present (Full_View (Desig_2)) then
3448 Desig_2 := Full_View (Desig_2);
3449 elsif Present (Non_Limited_View (Desig_2)) then
3450 Desig_2 := Non_Limited_View (Desig_2);
3454 -- The context is an instance association for a formal
3455 -- access-to-subprogram type; formal access parameter designated
3456 -- types require mapping because they may denote other formal
3457 -- parameters of the generic unit.
3460 Desig_1 := Get_Instance_Of (Desig_1);
3461 Desig_2 := Get_Instance_Of (Desig_2);
3464 -- It is possible for a Class_Wide_Type to be introduced for an
3465 -- incomplete type, in which case there is a separate class_ wide
3466 -- type for the full view. The types conform if their Etypes
3467 -- conform, i.e. one may be the full view of the other. This can
3468 -- only happen in the context of an access parameter, other uses
3469 -- of an incomplete Class_Wide_Type are illegal.
3471 if Is_Class_Wide_Type (Desig_1)
3472 and then Is_Class_Wide_Type (Desig_2)
3476 (Etype (Base_Type (Desig_1)),
3477 Etype (Base_Type (Desig_2)), Ctype);
3479 elsif Are_Anonymous_Access_To_Subprogram_Types then
3480 if Ada_Version < Ada_05 then
3481 return Ctype = Type_Conformant
3483 Subtypes_Statically_Match (Desig_1, Desig_2);
3485 -- We must check the conformance of the signatures themselves
3489 Conformant : Boolean;
3492 (Desig_1, Desig_2, Ctype, False, Conformant);
3498 return Base_Type (Desig_1) = Base_Type (Desig_2)
3499 and then (Ctype = Type_Conformant
3501 Subtypes_Statically_Match (Desig_1, Desig_2));
3505 -- Otherwise definitely no match
3510 end Conforming_Types;
3512 --------------------------
3513 -- Create_Extra_Formals --
3514 --------------------------
3516 procedure Create_Extra_Formals (E : Entity_Id) is
3518 Last_Extra : Entity_Id;
3519 Formal_Type : Entity_Id;
3520 P_Formal : Entity_Id := Empty;
3522 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3523 -- Add an extra formal, associated with the current Formal. The extra
3524 -- formal is added to the list of extra formals, and also returned as
3525 -- the result. These formals are always of mode IN.
3527 ----------------------
3528 -- Add_Extra_Formal --
3529 ----------------------
3531 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3532 EF : constant Entity_Id :=
3533 Make_Defining_Identifier (Sloc (Formal),
3534 Chars => New_External_Name (Chars (Formal), 'F'));
3537 -- We never generate extra formals if expansion is not active
3538 -- because we don't need them unless we are generating code.
3540 if not Expander_Active then
3544 -- A little optimization. Never generate an extra formal for the
3545 -- _init operand of an initialization procedure, since it could
3548 if Chars (Formal) = Name_uInit then
3552 Set_Ekind (EF, E_In_Parameter);
3553 Set_Actual_Subtype (EF, Typ);
3554 Set_Etype (EF, Typ);
3555 Set_Scope (EF, Scope (Formal));
3556 Set_Mechanism (EF, Default_Mechanism);
3557 Set_Formal_Validity (EF);
3559 Set_Extra_Formal (Last_Extra, EF);
3562 end Add_Extra_Formal;
3564 -- Start of processing for Create_Extra_Formals
3567 -- If this is a derived subprogram then the subtypes of the parent
3568 -- subprogram's formal parameters will be used to to determine the need
3569 -- for extra formals.
3571 if Is_Overloadable (E) and then Present (Alias (E)) then
3572 P_Formal := First_Formal (Alias (E));
3575 Last_Extra := Empty;
3576 Formal := First_Formal (E);
3577 while Present (Formal) loop
3578 Last_Extra := Formal;
3579 Next_Formal (Formal);
3582 -- If Extra_formals where already created, don't do it again. This
3583 -- situation may arise for subprogram types created as part of
3584 -- dispatching calls (see Expand_Dispatching_Call)
3586 if Present (Last_Extra) and then
3587 Present (Extra_Formal (Last_Extra))
3592 Formal := First_Formal (E);
3594 while Present (Formal) loop
3596 -- Create extra formal for supporting the attribute 'Constrained.
3597 -- The case of a private type view without discriminants also
3598 -- requires the extra formal if the underlying type has defaulted
3601 if Ekind (Formal) /= E_In_Parameter then
3602 if Present (P_Formal) then
3603 Formal_Type := Etype (P_Formal);
3605 Formal_Type := Etype (Formal);
3608 -- Do not produce extra formals for Unchecked_Union parameters.
3609 -- Jump directly to the end of the loop.
3611 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3612 goto Skip_Extra_Formal_Generation;
3615 if not Has_Discriminants (Formal_Type)
3616 and then Ekind (Formal_Type) in Private_Kind
3617 and then Present (Underlying_Type (Formal_Type))
3619 Formal_Type := Underlying_Type (Formal_Type);
3622 if Has_Discriminants (Formal_Type)
3624 ((not Is_Constrained (Formal_Type)
3625 and then not Is_Indefinite_Subtype (Formal_Type))
3626 or else Present (Extra_Formal (Formal)))
3628 Set_Extra_Constrained
3629 (Formal, Add_Extra_Formal (Standard_Boolean));
3633 -- Create extra formal for supporting accessibility checking
3635 -- This is suppressed if we specifically suppress accessibility
3636 -- checks at the pacage level for either the subprogram, or the
3637 -- package in which it resides. However, we do not suppress it
3638 -- simply if the scope has accessibility checks suppressed, since
3639 -- this could cause trouble when clients are compiled with a
3640 -- different suppression setting. The explicit checks at the
3641 -- package level are safe from this point of view.
3643 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3645 (Explicit_Suppress (E, Accessibility_Check)
3647 Explicit_Suppress (Scope (E), Accessibility_Check))
3649 (not Present (P_Formal)
3650 or else Present (Extra_Accessibility (P_Formal)))
3652 -- Temporary kludge: for now we avoid creating the extra formal
3653 -- for access parameters of protected operations because of
3654 -- problem with the case of internal protected calls. ???
3656 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3657 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3659 Set_Extra_Accessibility
3660 (Formal, Add_Extra_Formal (Standard_Natural));
3664 if Present (P_Formal) then
3665 Next_Formal (P_Formal);
3668 -- This label is required when skipping extra formal generation for
3669 -- Unchecked_Union parameters.
3671 <<Skip_Extra_Formal_Generation>>
3673 Next_Formal (Formal);
3675 end Create_Extra_Formals;
3677 -----------------------------
3678 -- Enter_Overloaded_Entity --
3679 -----------------------------
3681 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3682 E : Entity_Id := Current_Entity_In_Scope (S);
3683 C_E : Entity_Id := Current_Entity (S);
3687 Set_Has_Homonym (E);
3688 Set_Has_Homonym (S);
3691 Set_Is_Immediately_Visible (S);
3692 Set_Scope (S, Current_Scope);
3694 -- Chain new entity if front of homonym in current scope, so that
3695 -- homonyms are contiguous.
3700 while Homonym (C_E) /= E loop
3701 C_E := Homonym (C_E);
3704 Set_Homonym (C_E, S);
3708 Set_Current_Entity (S);
3713 Append_Entity (S, Current_Scope);
3714 Set_Public_Status (S);
3716 if Debug_Flag_E then
3717 Write_Str ("New overloaded entity chain: ");
3718 Write_Name (Chars (S));
3721 while Present (E) loop
3722 Write_Str (" "); Write_Int (Int (E));
3729 -- Generate warning for hiding
3732 and then Comes_From_Source (S)
3733 and then In_Extended_Main_Source_Unit (S)
3740 -- Warn unless genuine overloading
3742 if (not Is_Overloadable (E))
3743 or else Subtype_Conformant (E, S)
3745 Error_Msg_Sloc := Sloc (E);
3746 Error_Msg_N ("declaration of & hides one#?", S);
3750 end Enter_Overloaded_Entity;
3752 -----------------------------
3753 -- Find_Corresponding_Spec --
3754 -----------------------------
3756 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3757 Spec : constant Node_Id := Specification (N);
3758 Designator : constant Entity_Id := Defining_Entity (Spec);
3763 E := Current_Entity (Designator);
3765 while Present (E) loop
3767 -- We are looking for a matching spec. It must have the same scope,
3768 -- and the same name, and either be type conformant, or be the case
3769 -- of a library procedure spec and its body (which belong to one
3770 -- another regardless of whether they are type conformant or not).
3772 if Scope (E) = Current_Scope then
3773 if Current_Scope = Standard_Standard
3774 or else (Ekind (E) = Ekind (Designator)
3775 and then Type_Conformant (E, Designator))
3777 -- Within an instantiation, we know that spec and body are
3778 -- subtype conformant, because they were subtype conformant
3779 -- in the generic. We choose the subtype-conformant entity
3780 -- here as well, to resolve spurious ambiguities in the
3781 -- instance that were not present in the generic (i.e. when
3782 -- two different types are given the same actual). If we are
3783 -- looking for a spec to match a body, full conformance is
3787 Set_Convention (Designator, Convention (E));
3789 if Nkind (N) = N_Subprogram_Body
3790 and then Present (Homonym (E))
3791 and then not Fully_Conformant (E, Designator)
3795 elsif not Subtype_Conformant (E, Designator) then
3800 if not Has_Completion (E) then
3802 if Nkind (N) /= N_Subprogram_Body_Stub then
3803 Set_Corresponding_Spec (N, E);
3806 Set_Has_Completion (E);
3809 elsif Nkind (Parent (N)) = N_Subunit then
3811 -- If this is the proper body of a subunit, the completion
3812 -- flag is set when analyzing the stub.
3816 -- If body already exists, this is an error unless the
3817 -- previous declaration is the implicit declaration of
3818 -- a derived subprogram, or this is a spurious overloading
3821 elsif No (Alias (E))
3822 and then not Is_Intrinsic_Subprogram (E)
3823 and then not In_Instance
3825 Error_Msg_Sloc := Sloc (E);
3826 if Is_Imported (E) then
3828 ("body not allowed for imported subprogram & declared#",
3831 Error_Msg_NE ("duplicate body for & declared#", N, E);
3835 elsif Is_Child_Unit (E)
3837 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3839 Nkind (Parent (Unit_Declaration_Node (Designator)))
3840 = N_Compilation_Unit
3843 -- Child units cannot be overloaded, so a conformance mismatch
3844 -- between body and a previous spec is an error.
3847 ("body of child unit does not match previous declaration", N);
3855 -- On exit, we know that no previous declaration of subprogram exists
3858 end Find_Corresponding_Spec;
3860 ----------------------
3861 -- Fully_Conformant --
3862 ----------------------
3864 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3867 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3869 end Fully_Conformant;
3871 ----------------------------------
3872 -- Fully_Conformant_Expressions --
3873 ----------------------------------
3875 function Fully_Conformant_Expressions
3876 (Given_E1 : Node_Id;
3877 Given_E2 : Node_Id) return Boolean
3879 E1 : constant Node_Id := Original_Node (Given_E1);
3880 E2 : constant Node_Id := Original_Node (Given_E2);
3881 -- We always test conformance on original nodes, since it is possible
3882 -- for analysis and/or expansion to make things look as though they
3883 -- conform when they do not, e.g. by converting 1+2 into 3.
3885 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3886 renames Fully_Conformant_Expressions;
3888 function FCL (L1, L2 : List_Id) return Boolean;
3889 -- Compare elements of two lists for conformance. Elements have to
3890 -- be conformant, and actuals inserted as default parameters do not
3891 -- match explicit actuals with the same value.
3893 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3894 -- Compare an operator node with a function call
3900 function FCL (L1, L2 : List_Id) return Boolean is
3904 if L1 = No_List then
3910 if L2 = No_List then
3916 -- Compare two lists, skipping rewrite insertions (we want to
3917 -- compare the original trees, not the expanded versions!)
3920 if Is_Rewrite_Insertion (N1) then
3922 elsif Is_Rewrite_Insertion (N2) then
3928 elsif not FCE (N1, N2) then
3941 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3942 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3947 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3952 Act := First (Actuals);
3954 if Nkind (Op_Node) in N_Binary_Op then
3956 if not FCE (Left_Opnd (Op_Node), Act) then
3963 return Present (Act)
3964 and then FCE (Right_Opnd (Op_Node), Act)
3965 and then No (Next (Act));
3969 -- Start of processing for Fully_Conformant_Expressions
3972 -- Non-conformant if paren count does not match. Note: if some idiot
3973 -- complains that we don't do this right for more than 3 levels of
3974 -- parentheses, they will be treated with the respect they deserve :-)
3976 if Paren_Count (E1) /= Paren_Count (E2) then
3979 -- If same entities are referenced, then they are conformant even if
3980 -- they have different forms (RM 8.3.1(19-20)).
3982 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3983 if Present (Entity (E1)) then
3984 return Entity (E1) = Entity (E2)
3985 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3986 and then Ekind (Entity (E1)) = E_Discriminant
3987 and then Ekind (Entity (E2)) = E_In_Parameter);
3989 elsif Nkind (E1) = N_Expanded_Name
3990 and then Nkind (E2) = N_Expanded_Name
3991 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3992 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3994 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3997 -- Identifiers in component associations don't always have
3998 -- entities, but their names must conform.
4000 return Nkind (E1) = N_Identifier
4001 and then Nkind (E2) = N_Identifier
4002 and then Chars (E1) = Chars (E2);
4005 elsif Nkind (E1) = N_Character_Literal
4006 and then Nkind (E2) = N_Expanded_Name
4008 return Nkind (Selector_Name (E2)) = N_Character_Literal
4009 and then Chars (E1) = Chars (Selector_Name (E2));
4011 elsif Nkind (E2) = N_Character_Literal
4012 and then Nkind (E1) = N_Expanded_Name
4014 return Nkind (Selector_Name (E1)) = N_Character_Literal
4015 and then Chars (E2) = Chars (Selector_Name (E1));
4017 elsif Nkind (E1) in N_Op
4018 and then Nkind (E2) = N_Function_Call
4020 return FCO (E1, E2);
4022 elsif Nkind (E2) in N_Op
4023 and then Nkind (E1) = N_Function_Call
4025 return FCO (E2, E1);
4027 -- Otherwise we must have the same syntactic entity
4029 elsif Nkind (E1) /= Nkind (E2) then
4032 -- At this point, we specialize by node type
4039 FCL (Expressions (E1), Expressions (E2))
4040 and then FCL (Component_Associations (E1),
4041 Component_Associations (E2));
4044 if Nkind (Expression (E1)) = N_Qualified_Expression
4046 Nkind (Expression (E2)) = N_Qualified_Expression
4048 return FCE (Expression (E1), Expression (E2));
4050 -- Check that the subtype marks and any constraints
4055 Indic1 : constant Node_Id := Expression (E1);
4056 Indic2 : constant Node_Id := Expression (E2);
4061 if Nkind (Indic1) /= N_Subtype_Indication then
4063 Nkind (Indic2) /= N_Subtype_Indication
4064 and then Entity (Indic1) = Entity (Indic2);
4066 elsif Nkind (Indic2) /= N_Subtype_Indication then
4068 Nkind (Indic1) /= N_Subtype_Indication
4069 and then Entity (Indic1) = Entity (Indic2);
4072 if Entity (Subtype_Mark (Indic1)) /=
4073 Entity (Subtype_Mark (Indic2))
4078 Elt1 := First (Constraints (Constraint (Indic1)));
4079 Elt2 := First (Constraints (Constraint (Indic2)));
4081 while Present (Elt1) and then Present (Elt2) loop
4082 if not FCE (Elt1, Elt2) then
4095 when N_Attribute_Reference =>
4097 Attribute_Name (E1) = Attribute_Name (E2)
4098 and then FCL (Expressions (E1), Expressions (E2));
4102 Entity (E1) = Entity (E2)
4103 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
4104 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4106 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
4108 FCE (Left_Opnd (E1), Left_Opnd (E2))
4110 FCE (Right_Opnd (E1), Right_Opnd (E2));
4112 when N_Character_Literal =>
4114 Char_Literal_Value (E1) = Char_Literal_Value (E2);
4116 when N_Component_Association =>
4118 FCL (Choices (E1), Choices (E2))
4119 and then FCE (Expression (E1), Expression (E2));
4121 when N_Conditional_Expression =>
4123 FCL (Expressions (E1), Expressions (E2));
4125 when N_Explicit_Dereference =>
4127 FCE (Prefix (E1), Prefix (E2));
4129 when N_Extension_Aggregate =>
4131 FCL (Expressions (E1), Expressions (E2))
4132 and then Null_Record_Present (E1) =
4133 Null_Record_Present (E2)
4134 and then FCL (Component_Associations (E1),
4135 Component_Associations (E2));
4137 when N_Function_Call =>
4139 FCE (Name (E1), Name (E2))
4140 and then FCL (Parameter_Associations (E1),
4141 Parameter_Associations (E2));
4143 when N_Indexed_Component =>
4145 FCE (Prefix (E1), Prefix (E2))
4146 and then FCL (Expressions (E1), Expressions (E2));
4148 when N_Integer_Literal =>
4149 return (Intval (E1) = Intval (E2));
4154 when N_Operator_Symbol =>
4156 Chars (E1) = Chars (E2);
4158 when N_Others_Choice =>
4161 when N_Parameter_Association =>
4163 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
4164 and then FCE (Explicit_Actual_Parameter (E1),
4165 Explicit_Actual_Parameter (E2));
4167 when N_Qualified_Expression =>
4169 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4170 and then FCE (Expression (E1), Expression (E2));
4174 FCE (Low_Bound (E1), Low_Bound (E2))
4175 and then FCE (High_Bound (E1), High_Bound (E2));
4177 when N_Real_Literal =>
4178 return (Realval (E1) = Realval (E2));
4180 when N_Selected_Component =>
4182 FCE (Prefix (E1), Prefix (E2))
4183 and then FCE (Selector_Name (E1), Selector_Name (E2));
4187 FCE (Prefix (E1), Prefix (E2))
4188 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
4190 when N_String_Literal =>
4192 S1 : constant String_Id := Strval (E1);
4193 S2 : constant String_Id := Strval (E2);
4194 L1 : constant Nat := String_Length (S1);
4195 L2 : constant Nat := String_Length (S2);
4202 for J in 1 .. L1 loop
4203 if Get_String_Char (S1, J) /=
4204 Get_String_Char (S2, J)
4214 when N_Type_Conversion =>
4216 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4217 and then FCE (Expression (E1), Expression (E2));
4221 Entity (E1) = Entity (E2)
4222 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4224 when N_Unchecked_Type_Conversion =>
4226 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4227 and then FCE (Expression (E1), Expression (E2));
4229 -- All other node types cannot appear in this context. Strictly
4230 -- we should raise a fatal internal error. Instead we just ignore
4231 -- the nodes. This means that if anyone makes a mistake in the
4232 -- expander and mucks an expression tree irretrievably, the
4233 -- result will be a failure to detect a (probably very obscure)
4234 -- case of non-conformance, which is better than bombing on some
4235 -- case where two expressions do in fact conform.
4242 end Fully_Conformant_Expressions;
4244 ----------------------------------------
4245 -- Fully_Conformant_Discrete_Subtypes --
4246 ----------------------------------------
4248 function Fully_Conformant_Discrete_Subtypes
4249 (Given_S1 : Node_Id;
4250 Given_S2 : Node_Id) return Boolean
4252 S1 : constant Node_Id := Original_Node (Given_S1);
4253 S2 : constant Node_Id := Original_Node (Given_S2);
4255 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4256 -- Special-case for a bound given by a discriminant, which in the body
4257 -- is replaced with the discriminal of the enclosing type.
4259 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4260 -- Check both bounds
4262 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4264 if Is_Entity_Name (B1)
4265 and then Is_Entity_Name (B2)
4266 and then Ekind (Entity (B1)) = E_Discriminant
4268 return Chars (B1) = Chars (B2);
4271 return Fully_Conformant_Expressions (B1, B2);
4273 end Conforming_Bounds;
4275 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4278 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4280 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4281 end Conforming_Ranges;
4283 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4286 if Nkind (S1) /= Nkind (S2) then
4289 elsif Is_Entity_Name (S1) then
4290 return Entity (S1) = Entity (S2);
4292 elsif Nkind (S1) = N_Range then
4293 return Conforming_Ranges (S1, S2);
4295 elsif Nkind (S1) = N_Subtype_Indication then
4297 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4300 (Range_Expression (Constraint (S1)),
4301 Range_Expression (Constraint (S2)));
4305 end Fully_Conformant_Discrete_Subtypes;
4307 --------------------
4308 -- Install_Entity --
4309 --------------------
4311 procedure Install_Entity (E : Entity_Id) is
4312 Prev : constant Entity_Id := Current_Entity (E);
4315 Set_Is_Immediately_Visible (E);
4316 Set_Current_Entity (E);
4317 Set_Homonym (E, Prev);
4320 ---------------------
4321 -- Install_Formals --
4322 ---------------------
4324 procedure Install_Formals (Id : Entity_Id) is
4328 F := First_Formal (Id);
4330 while Present (F) loop
4334 end Install_Formals;
4336 ---------------------------------
4337 -- Is_Non_Overriding_Operation --
4338 ---------------------------------
4340 function Is_Non_Overriding_Operation
4341 (Prev_E : Entity_Id;
4342 New_E : Entity_Id) return Boolean
4346 G_Typ : Entity_Id := Empty;
4348 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4349 -- If F_Type is a derived type associated with a generic actual
4350 -- subtype, then return its Generic_Parent_Type attribute, else return
4353 function Types_Correspond
4354 (P_Type : Entity_Id;
4355 N_Type : Entity_Id) return Boolean;
4356 -- Returns true if and only if the types (or designated types in the
4357 -- case of anonymous access types) are the same or N_Type is derived
4358 -- directly or indirectly from P_Type.
4360 -----------------------------
4361 -- Get_Generic_Parent_Type --
4362 -----------------------------
4364 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4369 if Is_Derived_Type (F_Typ)
4370 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4372 -- The tree must be traversed to determine the parent subtype in
4373 -- the generic unit, which unfortunately isn't always available
4374 -- via semantic attributes. ??? (Note: The use of Original_Node
4375 -- is needed for cases where a full derived type has been
4378 Indic := Subtype_Indication
4379 (Type_Definition (Original_Node (Parent (F_Typ))));
4381 if Nkind (Indic) = N_Subtype_Indication then
4382 G_Typ := Entity (Subtype_Mark (Indic));
4384 G_Typ := Entity (Indic);
4387 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4388 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4390 return Generic_Parent_Type (Parent (G_Typ));
4395 end Get_Generic_Parent_Type;
4397 ----------------------
4398 -- Types_Correspond --
4399 ----------------------
4401 function Types_Correspond
4402 (P_Type : Entity_Id;
4403 N_Type : Entity_Id) return Boolean
4405 Prev_Type : Entity_Id := Base_Type (P_Type);
4406 New_Type : Entity_Id := Base_Type (N_Type);
4409 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4410 Prev_Type := Designated_Type (Prev_Type);
4413 if Ekind (New_Type) = E_Anonymous_Access_Type then
4414 New_Type := Designated_Type (New_Type);
4417 if Prev_Type = New_Type then
4420 elsif not Is_Class_Wide_Type (New_Type) then
4421 while Etype (New_Type) /= New_Type loop
4422 New_Type := Etype (New_Type);
4423 if New_Type = Prev_Type then
4429 end Types_Correspond;
4431 -- Start of processing for Is_Non_Overriding_Operation
4434 -- In the case where both operations are implicit derived subprograms
4435 -- then neither overrides the other. This can only occur in certain
4436 -- obscure cases (e.g., derivation from homographs created in a generic
4439 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4442 elsif Ekind (Current_Scope) = E_Package
4443 and then Is_Generic_Instance (Current_Scope)
4444 and then In_Private_Part (Current_Scope)
4445 and then Comes_From_Source (New_E)
4447 -- We examine the formals and result subtype of the inherited
4448 -- operation, to determine whether their type is derived from (the
4449 -- instance of) a generic type.
4451 Formal := First_Formal (Prev_E);
4453 while Present (Formal) loop
4454 F_Typ := Base_Type (Etype (Formal));
4456 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4457 F_Typ := Designated_Type (F_Typ);
4460 G_Typ := Get_Generic_Parent_Type (F_Typ);
4462 Next_Formal (Formal);
4465 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4466 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4473 -- If the generic type is a private type, then the original
4474 -- operation was not overriding in the generic, because there was
4475 -- no primitive operation to override.
4477 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4478 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4479 N_Formal_Private_Type_Definition
4483 -- The generic parent type is the ancestor of a formal derived
4484 -- type declaration. We need to check whether it has a primitive
4485 -- operation that should be overridden by New_E in the generic.
4489 P_Formal : Entity_Id;
4490 N_Formal : Entity_Id;
4494 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4497 while Present (Prim_Elt) loop
4498 P_Prim := Node (Prim_Elt);
4500 if Chars (P_Prim) = Chars (New_E)
4501 and then Ekind (P_Prim) = Ekind (New_E)
4503 P_Formal := First_Formal (P_Prim);
4504 N_Formal := First_Formal (New_E);
4505 while Present (P_Formal) and then Present (N_Formal) loop
4506 P_Typ := Etype (P_Formal);
4507 N_Typ := Etype (N_Formal);
4509 if not Types_Correspond (P_Typ, N_Typ) then
4513 Next_Entity (P_Formal);
4514 Next_Entity (N_Formal);
4517 -- Found a matching primitive operation belonging to the
4518 -- formal ancestor type, so the new subprogram is
4521 if not Present (P_Formal)
4522 and then not Present (N_Formal)
4523 and then (Ekind (New_E) /= E_Function
4526 (Etype (P_Prim), Etype (New_E)))
4532 Next_Elmt (Prim_Elt);
4535 -- If no match found, then the new subprogram does not
4536 -- override in the generic (nor in the instance).
4544 end Is_Non_Overriding_Operation;
4546 ------------------------------
4547 -- Make_Inequality_Operator --
4548 ------------------------------
4550 -- S is the defining identifier of an equality operator. We build a
4551 -- subprogram declaration with the right signature. This operation is
4552 -- intrinsic, because it is always expanded as the negation of the
4553 -- call to the equality function.
4555 procedure Make_Inequality_Operator (S : Entity_Id) is
4556 Loc : constant Source_Ptr := Sloc (S);
4559 Op_Name : Entity_Id;
4565 -- Check that equality was properly defined
4567 if No (Next_Formal (First_Formal (S))) then
4571 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4572 B := Make_Defining_Identifier (Loc,
4573 Chars (Next_Formal (First_Formal (S))));
4575 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4577 Formals := New_List (
4578 Make_Parameter_Specification (Loc,
4579 Defining_Identifier => A,
4581 New_Reference_To (Etype (First_Formal (S)), Loc)),
4583 Make_Parameter_Specification (Loc,
4584 Defining_Identifier => B,
4586 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4589 Make_Subprogram_Declaration (Loc,
4591 Make_Function_Specification (Loc,
4592 Defining_Unit_Name => Op_Name,
4593 Parameter_Specifications => Formals,
4594 Result_Definition => New_Reference_To (Standard_Boolean, Loc)));
4596 -- Insert inequality right after equality if it is explicit or after
4597 -- the derived type when implicit. These entities are created only for
4598 -- visibility purposes, and eventually replaced in the course of
4599 -- expansion, so they do not need to be attached to the tree and seen
4600 -- by the back-end. Keeping them internal also avoids spurious freezing
4601 -- problems. The declaration is inserted in the tree for analysis, and
4602 -- removed afterwards. If the equality operator comes from an explicit
4603 -- declaration, attach the inequality immediately after. Else the
4604 -- equality is inherited from a derived type declaration, so insert
4605 -- inequality after that declaration.
4607 if No (Alias (S)) then
4608 Insert_After (Unit_Declaration_Node (S), Decl);
4609 elsif Is_List_Member (Parent (S)) then
4610 Insert_After (Parent (S), Decl);
4612 Insert_After (Parent (Etype (First_Formal (S))), Decl);
4615 Mark_Rewrite_Insertion (Decl);
4616 Set_Is_Intrinsic_Subprogram (Op_Name);
4619 Set_Has_Completion (Op_Name);
4620 Set_Corresponding_Equality (Op_Name, S);
4621 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4622 end Make_Inequality_Operator;
4624 ----------------------
4625 -- May_Need_Actuals --
4626 ----------------------
4628 procedure May_Need_Actuals (Fun : Entity_Id) is
4633 F := First_Formal (Fun);
4636 while Present (F) loop
4637 if No (Default_Value (F)) then
4645 Set_Needs_No_Actuals (Fun, B);
4646 end May_Need_Actuals;
4648 ---------------------
4649 -- Mode_Conformant --
4650 ---------------------
4652 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4655 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4657 end Mode_Conformant;
4659 ---------------------------
4660 -- New_Overloaded_Entity --
4661 ---------------------------
4663 procedure New_Overloaded_Entity
4665 Derived_Type : Entity_Id := Empty)
4667 Does_Override : Boolean := False;
4668 -- Set if the current scope has an operation that is type-conformant
4669 -- with S, and becomes hidden by S.
4672 -- Entity that S overrides
4674 Prev_Vis : Entity_Id := Empty;
4675 -- Needs comment ???
4677 Is_Alias_Interface : Boolean := False;
4679 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4680 -- Check that E is declared in the private part of the current package,
4681 -- or in the package body, where it may hide a previous declaration.
4682 -- We can't use In_Private_Part by itself because this flag is also
4683 -- set when freezing entities, so we must examine the place of the
4684 -- declaration in the tree, and recognize wrapper packages as well.
4686 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4687 -- If the subprogram being analyzed is a primitive operation of
4688 -- the type of one of its formals, set the corresponding flag.
4690 ----------------------------
4691 -- Is_Private_Declaration --
4692 ----------------------------
4694 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4695 Priv_Decls : List_Id;
4696 Decl : constant Node_Id := Unit_Declaration_Node (E);
4699 if Is_Package (Current_Scope)
4700 and then In_Private_Part (Current_Scope)
4703 Private_Declarations (
4704 Specification (Unit_Declaration_Node (Current_Scope)));
4706 return In_Package_Body (Current_Scope)
4708 (Is_List_Member (Decl)
4709 and then List_Containing (Decl) = Priv_Decls)
4710 or else (Nkind (Parent (Decl)) = N_Package_Specification
4711 and then not Is_Compilation_Unit (
4712 Defining_Entity (Parent (Decl)))
4713 and then List_Containing (Parent (Parent (Decl)))
4718 end Is_Private_Declaration;
4720 -------------------------------
4721 -- Maybe_Primitive_Operation --
4722 -------------------------------
4724 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4729 function Visible_Part_Type (T : Entity_Id) return Boolean;
4730 -- Returns true if T is declared in the visible part of
4731 -- the current package scope; otherwise returns false.
4732 -- Assumes that T is declared in a package.
4734 procedure Check_Private_Overriding (T : Entity_Id);
4735 -- Checks that if a primitive abstract subprogram of a visible
4736 -- abstract type is declared in a private part, then it must
4737 -- override an abstract subprogram declared in the visible part.
4738 -- Also checks that if a primitive function with a controlling
4739 -- result is declared in a private part, then it must override
4740 -- a function declared in the visible part.
4742 ------------------------------
4743 -- Check_Private_Overriding --
4744 ------------------------------
4746 procedure Check_Private_Overriding (T : Entity_Id) is
4748 if Ekind (Current_Scope) = E_Package
4749 and then In_Private_Part (Current_Scope)
4750 and then Visible_Part_Type (T)
4751 and then not In_Instance
4754 and then Is_Abstract (S)
4755 and then (not Is_Overriding or else not Is_Abstract (E))
4757 if not Is_Interface (T) then
4758 Error_Msg_N ("abstract subprograms must be visible "
4759 & "('R'M 3.9.3(10))!", S);
4761 -- Ada 2005 (AI-251)
4764 Error_Msg_N ("primitive subprograms of interface types "
4765 & "declared in a visible part, must be declared in "
4766 & "the visible part ('R'M 3.9.4)!", S);
4769 elsif Ekind (S) = E_Function
4770 and then Is_Tagged_Type (T)
4771 and then T = Base_Type (Etype (S))
4772 and then not Is_Overriding
4775 ("private function with tagged result must"
4776 & " override visible-part function", S);
4778 ("\move subprogram to the visible part"
4779 & " ('R'M 3.9.3(10))", S);
4782 end Check_Private_Overriding;
4784 -----------------------
4785 -- Visible_Part_Type --
4786 -----------------------
4788 function Visible_Part_Type (T : Entity_Id) return Boolean is
4789 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4793 -- If the entity is a private type, then it must be
4794 -- declared in a visible part.
4796 if Ekind (T) in Private_Kind then
4800 -- Otherwise, we traverse the visible part looking for its
4801 -- corresponding declaration. We cannot use the declaration
4802 -- node directly because in the private part the entity of a
4803 -- private type is the one in the full view, which does not
4804 -- indicate that it is the completion of something visible.
4806 N := First (Visible_Declarations (Specification (P)));
4807 while Present (N) loop
4808 if Nkind (N) = N_Full_Type_Declaration
4809 and then Present (Defining_Identifier (N))
4810 and then T = Defining_Identifier (N)
4814 elsif (Nkind (N) = N_Private_Type_Declaration
4816 Nkind (N) = N_Private_Extension_Declaration)
4817 and then Present (Defining_Identifier (N))
4818 and then T = Full_View (Defining_Identifier (N))
4827 end Visible_Part_Type;
4829 -- Start of processing for Maybe_Primitive_Operation
4832 if not Comes_From_Source (S) then
4835 -- If the subprogram is at library level, it is not primitive
4838 elsif Current_Scope = Standard_Standard then
4841 elsif (Ekind (Current_Scope) = E_Package
4842 and then not In_Package_Body (Current_Scope))
4843 or else Is_Overriding
4845 -- For function, check return type
4847 if Ekind (S) = E_Function then
4848 B_Typ := Base_Type (Etype (S));
4850 if Scope (B_Typ) = Current_Scope then
4851 Set_Has_Primitive_Operations (B_Typ);
4852 Check_Private_Overriding (B_Typ);
4856 -- For all subprograms, check formals
4858 Formal := First_Formal (S);
4859 while Present (Formal) loop
4860 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4861 F_Typ := Designated_Type (Etype (Formal));
4863 F_Typ := Etype (Formal);
4866 B_Typ := Base_Type (F_Typ);
4868 if Scope (B_Typ) = Current_Scope then
4869 Set_Has_Primitive_Operations (B_Typ);
4870 Check_Private_Overriding (B_Typ);
4873 Next_Formal (Formal);
4876 end Maybe_Primitive_Operation;
4878 -- Start of processing for New_Overloaded_Entity
4881 -- We need to look for an entity that S may override. This must be a
4882 -- homonym in the current scope, so we look for the first homonym of
4883 -- S in the current scope as the starting point for the search.
4885 E := Current_Entity_In_Scope (S);
4887 -- If there is no homonym then this is definitely not overriding
4890 Enter_Overloaded_Entity (S);
4891 Check_Dispatching_Operation (S, Empty);
4892 Maybe_Primitive_Operation;
4894 -- Ada 2005 (AI-397): Subprograms in the context of protected
4895 -- types have their overriding indicators checked in Sem_Ch9.
4897 if Ekind (S) not in Subprogram_Kind
4898 or else Ekind (Scope (S)) /= E_Protected_Type
4900 Check_Overriding_Indicator (S, False);
4903 -- If there is a homonym that is not overloadable, then we have an
4904 -- error, except for the special cases checked explicitly below.
4906 elsif not Is_Overloadable (E) then
4908 -- Check for spurious conflict produced by a subprogram that has the
4909 -- same name as that of the enclosing generic package. The conflict
4910 -- occurs within an instance, between the subprogram and the renaming
4911 -- declaration for the package. After the subprogram, the package
4912 -- renaming declaration becomes hidden.
4914 if Ekind (E) = E_Package
4915 and then Present (Renamed_Object (E))
4916 and then Renamed_Object (E) = Current_Scope
4917 and then Nkind (Parent (Renamed_Object (E))) =
4918 N_Package_Specification
4919 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4922 Set_Is_Immediately_Visible (E, False);
4923 Enter_Overloaded_Entity (S);
4924 Set_Homonym (S, Homonym (E));
4925 Check_Dispatching_Operation (S, Empty);
4926 Check_Overriding_Indicator (S, False);
4928 -- If the subprogram is implicit it is hidden by the previous
4929 -- declaration. However if it is dispatching, it must appear in the
4930 -- dispatch table anyway, because it can be dispatched to even if it
4931 -- cannot be called directly.
4933 elsif Present (Alias (S))
4934 and then not Comes_From_Source (S)
4936 Set_Scope (S, Current_Scope);
4938 if Is_Dispatching_Operation (Alias (S)) then
4939 Check_Dispatching_Operation (S, Empty);
4945 Error_Msg_Sloc := Sloc (E);
4946 Error_Msg_N ("& conflicts with declaration#", S);
4948 -- Useful additional warning
4950 if Is_Generic_Unit (E) then
4951 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4957 -- E exists and is overloadable
4960 Is_Alias_Interface :=
4962 and then Is_Dispatching_Operation (Alias (S))
4963 and then Present (DTC_Entity (Alias (S)))
4964 and then Is_Interface (Scope (DTC_Entity (Alias (S))));
4966 -- Loop through E and its homonyms to determine if any of them is
4967 -- the candidate for overriding by S.
4969 while Present (E) loop
4971 -- Definitely not interesting if not in the current scope
4973 if Scope (E) /= Current_Scope then
4976 -- Check if we have type conformance
4978 -- Ada 2005 (AI-251): In case of overriding an interface
4979 -- subprogram it is not an error that the old and new entities
4980 -- have the same profile, and hence we skip this code.
4982 elsif not Is_Alias_Interface
4983 and then Type_Conformant (E, S)
4985 -- If the old and new entities have the same profile and one
4986 -- is not the body of the other, then this is an error, unless
4987 -- one of them is implicitly declared.
4989 -- There are some cases when both can be implicit, for example
4990 -- when both a literal and a function that overrides it are
4991 -- inherited in a derivation, or when an inhertited operation
4992 -- of a tagged full type overrides the ineherited operation of
4993 -- a private extension. Ada 83 had a special rule for the the
4994 -- literal case. In Ada95, the later implicit operation hides
4995 -- the former, and the literal is always the former. In the
4996 -- odd case where both are derived operations declared at the
4997 -- same point, both operations should be declared, and in that
4998 -- case we bypass the following test and proceed to the next
4999 -- part (this can only occur for certain obscure cases
5000 -- involving homographs in instances and can't occur for
5001 -- dispatching operations ???). Note that the following
5002 -- condition is less than clear. For example, it's not at all
5003 -- clear why there's a test for E_Entry here. ???
5005 if Present (Alias (S))
5006 and then (No (Alias (E))
5007 or else Comes_From_Source (E)
5008 or else Is_Dispatching_Operation (E))
5010 (Ekind (E) = E_Entry
5011 or else Ekind (E) /= E_Enumeration_Literal)
5013 -- When an derived operation is overloaded it may be due to
5014 -- the fact that the full view of a private extension
5015 -- re-inherits. It has to be dealt with.
5017 if Is_Package (Current_Scope)
5018 and then In_Private_Part (Current_Scope)
5020 Check_Operation_From_Private_View (S, E);
5023 -- In any case the implicit operation remains hidden by
5024 -- the existing declaration, which is overriding.
5026 Set_Is_Overriding_Operation (E);
5028 if Comes_From_Source (E) then
5029 Check_Overriding_Indicator (E, True);
5031 -- Indicate that E overrides the operation from which
5034 if Present (Alias (S)) then
5035 Set_Overridden_Operation (E, Alias (S));
5037 Set_Overridden_Operation (E, S);
5043 -- Within an instance, the renaming declarations for
5044 -- actual subprograms may become ambiguous, but they do
5045 -- not hide each other.
5047 elsif Ekind (E) /= E_Entry
5048 and then not Comes_From_Source (E)
5049 and then not Is_Generic_Instance (E)
5050 and then (Present (Alias (E))
5051 or else Is_Intrinsic_Subprogram (E))
5052 and then (not In_Instance
5053 or else No (Parent (E))
5054 or else Nkind (Unit_Declaration_Node (E)) /=
5055 N_Subprogram_Renaming_Declaration)
5057 -- A subprogram child unit is not allowed to override
5058 -- an inherited subprogram (10.1.1(20)).
5060 if Is_Child_Unit (S) then
5062 ("child unit overrides inherited subprogram in parent",
5067 if Is_Non_Overriding_Operation (E, S) then
5068 Enter_Overloaded_Entity (S);
5069 if not Present (Derived_Type)
5070 or else Is_Tagged_Type (Derived_Type)
5072 Check_Dispatching_Operation (S, Empty);
5078 -- E is a derived operation or an internal operator which
5079 -- is being overridden. Remove E from further visibility.
5080 -- Furthermore, if E is a dispatching operation, it must be
5081 -- replaced in the list of primitive operations of its type
5082 -- (see Override_Dispatching_Operation).
5084 Does_Override := True;
5090 Prev := First_Entity (Current_Scope);
5092 while Present (Prev)
5093 and then Next_Entity (Prev) /= E
5098 -- It is possible for E to be in the current scope and
5099 -- yet not in the entity chain. This can only occur in a
5100 -- generic context where E is an implicit concatenation
5101 -- in the formal part, because in a generic body the
5102 -- entity chain starts with the formals.
5105 (Present (Prev) or else Chars (E) = Name_Op_Concat);
5107 -- E must be removed both from the entity_list of the
5108 -- current scope, and from the visibility chain
5110 if Debug_Flag_E then
5111 Write_Str ("Override implicit operation ");
5112 Write_Int (Int (E));
5116 -- If E is a predefined concatenation, it stands for four
5117 -- different operations. As a result, a single explicit
5118 -- declaration does not hide it. In a possible ambiguous
5119 -- situation, Disambiguate chooses the user-defined op,
5120 -- so it is correct to retain the previous internal one.
5122 if Chars (E) /= Name_Op_Concat
5123 or else Ekind (E) /= E_Operator
5125 -- For nondispatching derived operations that are
5126 -- overridden by a subprogram declared in the private
5127 -- part of a package, we retain the derived
5128 -- subprogram but mark it as not immediately visible.
5129 -- If the derived operation was declared in the
5130 -- visible part then this ensures that it will still
5131 -- be visible outside the package with the proper
5132 -- signature (calls from outside must also be
5133 -- directed to this version rather than the
5134 -- overriding one, unlike the dispatching case).
5135 -- Calls from inside the package will still resolve
5136 -- to the overriding subprogram since the derived one
5137 -- is marked as not visible within the package.
5139 -- If the private operation is dispatching, we achieve
5140 -- the overriding by keeping the implicit operation
5141 -- but setting its alias to be the overriding one. In
5142 -- this fashion the proper body is executed in all
5143 -- cases, but the original signature is used outside
5146 -- If the overriding is not in the private part, we
5147 -- remove the implicit operation altogether.
5149 if Is_Private_Declaration (S) then
5151 if not Is_Dispatching_Operation (E) then
5152 Set_Is_Immediately_Visible (E, False);
5154 -- Work done in Override_Dispatching_Operation,
5155 -- so nothing else need to be done here.
5161 -- Find predecessor of E in Homonym chain
5163 if E = Current_Entity (E) then
5166 Prev_Vis := Current_Entity (E);
5167 while Homonym (Prev_Vis) /= E loop
5168 Prev_Vis := Homonym (Prev_Vis);
5172 if Prev_Vis /= Empty then
5174 -- Skip E in the visibility chain
5176 Set_Homonym (Prev_Vis, Homonym (E));
5179 Set_Name_Entity_Id (Chars (E), Homonym (E));
5182 Set_Next_Entity (Prev, Next_Entity (E));
5184 if No (Next_Entity (Prev)) then
5185 Set_Last_Entity (Current_Scope, Prev);
5191 Enter_Overloaded_Entity (S);
5192 Set_Is_Overriding_Operation (S);
5193 Check_Overriding_Indicator (S, True);
5195 -- Indicate that S overrides the operation from which
5198 if Comes_From_Source (S) then
5199 if Present (Alias (E)) then
5200 Set_Overridden_Operation (S, Alias (E));
5202 Set_Overridden_Operation (S, E);
5206 if Is_Dispatching_Operation (E) then
5208 -- An overriding dispatching subprogram inherits the
5209 -- convention of the overridden subprogram (by
5212 Set_Convention (S, Convention (E));
5214 -- AI-251: For an entity overriding an interface
5215 -- primitive check if the entity also covers other
5216 -- abstract subprograms in the same scope. This is
5217 -- required to handle the general case, that is,
5218 -- 1) overriding other interface primitives, and
5219 -- 2) overriding abstract subprograms inherited from
5220 -- some abstract ancestor type.
5223 and then Present (Alias (E))
5224 and then Ekind (Alias (E)) /= E_Operator
5225 and then Present (DTC_Entity (Alias (E)))
5226 and then Is_Interface (Scope (DTC_Entity
5234 while Present (E1) loop
5235 if (Is_Overloadable (E1)
5236 or else Ekind (E1) = E_Subprogram_Type)
5237 and then Present (Alias (E1))
5238 and then Ekind (Alias (E1)) /= E_Operator
5239 and then Present (DTC_Entity (Alias (E1)))
5240 and then Is_Abstract
5241 (Scope (DTC_Entity (Alias (E1))))
5242 and then Type_Conformant (E1, S)
5244 Check_Dispatching_Operation (S, E1);
5252 Check_Dispatching_Operation (S, E);
5255 Check_Dispatching_Operation (S, Empty);
5258 Maybe_Primitive_Operation (Is_Overriding => True);
5259 goto Check_Inequality;
5262 -- Apparent redeclarations in instances can occur when two
5263 -- formal types get the same actual type. The subprograms in
5264 -- in the instance are legal, even if not callable from the
5265 -- outside. Calls from within are disambiguated elsewhere.
5266 -- For dispatching operations in the visible part, the usual
5267 -- rules apply, and operations with the same profile are not
5270 elsif (In_Instance_Visible_Part
5271 and then not Is_Dispatching_Operation (E))
5272 or else In_Instance_Not_Visible
5276 -- Here we have a real error (identical profile)
5279 Error_Msg_Sloc := Sloc (E);
5281 -- Avoid cascaded errors if the entity appears in
5282 -- subsequent calls.
5284 Set_Scope (S, Current_Scope);
5286 Error_Msg_N ("& conflicts with declaration#", S);
5288 if Is_Generic_Instance (S)
5289 and then not Has_Completion (E)
5292 ("\instantiation cannot provide body for it", S);
5306 -- On exit, we know that S is a new entity
5308 Enter_Overloaded_Entity (S);
5309 Maybe_Primitive_Operation;
5310 Check_Overriding_Indicator (S, Does_Override);
5312 -- If S is a derived operation for an untagged type then by
5313 -- definition it's not a dispatching operation (even if the parent
5314 -- operation was dispatching), so we don't call
5315 -- Check_Dispatching_Operation in that case.
5317 if not Present (Derived_Type)
5318 or else Is_Tagged_Type (Derived_Type)
5320 Check_Dispatching_Operation (S, Empty);
5324 -- If this is a user-defined equality operator that is not a derived
5325 -- subprogram, create the corresponding inequality. If the operation is
5326 -- dispatching, the expansion is done elsewhere, and we do not create
5327 -- an explicit inequality operation.
5329 <<Check_Inequality>>
5330 if Chars (S) = Name_Op_Eq
5331 and then Etype (S) = Standard_Boolean
5332 and then Present (Parent (S))
5333 and then not Is_Dispatching_Operation (S)
5335 Make_Inequality_Operator (S);
5337 end New_Overloaded_Entity;
5339 ---------------------
5340 -- Process_Formals --
5341 ---------------------
5343 procedure Process_Formals
5345 Related_Nod : Node_Id)
5347 Param_Spec : Node_Id;
5349 Formal_Type : Entity_Id;
5353 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5354 -- Check whether the default has a class-wide type. After analysis the
5355 -- default has the type of the formal, so we must also check explicitly
5356 -- for an access attribute.
5358 ---------------------------
5359 -- Is_Class_Wide_Default --
5360 ---------------------------
5362 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5364 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5365 or else (Nkind (D) = N_Attribute_Reference
5366 and then Attribute_Name (D) = Name_Access
5367 and then Is_Class_Wide_Type (Etype (Prefix (D))));
5368 end Is_Class_Wide_Default;
5370 -- Start of processing for Process_Formals
5373 -- In order to prevent premature use of the formals in the same formal
5374 -- part, the Ekind is left undefined until all default expressions are
5375 -- analyzed. The Ekind is established in a separate loop at the end.
5377 Param_Spec := First (T);
5379 while Present (Param_Spec) loop
5381 Formal := Defining_Identifier (Param_Spec);
5382 Enter_Name (Formal);
5384 -- Case of ordinary parameters
5386 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5387 Find_Type (Parameter_Type (Param_Spec));
5388 Ptype := Parameter_Type (Param_Spec);
5390 if Ptype = Error then
5394 Formal_Type := Entity (Ptype);
5396 if Ekind (Formal_Type) = E_Incomplete_Type
5397 or else (Is_Class_Wide_Type (Formal_Type)
5398 and then Ekind (Root_Type (Formal_Type)) =
5401 -- Ada 2005 (AI-326): Tagged incomplete types allowed
5403 if Is_Tagged_Type (Formal_Type) then
5406 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5407 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5409 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5412 elsif Ekind (Formal_Type) = E_Void then
5413 Error_Msg_NE ("premature use of&",
5414 Parameter_Type (Param_Spec), Formal_Type);
5417 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5418 -- declaration corresponding to the null-excluding type of the
5419 -- formal in the enclosing scope. Finally, replace the parameter
5420 -- type of the formal with the internal subtype.
5422 if Ada_Version >= Ada_05
5423 and then Is_Access_Type (Formal_Type)
5424 and then Null_Exclusion_Present (Param_Spec)
5426 if Can_Never_Be_Null (Formal_Type) then
5428 ("(Ada 2005) already a null-excluding type", Related_Nod);
5432 Create_Null_Excluding_Itype
5434 Related_Nod => Related_Nod,
5435 Scope_Id => Scope (Current_Scope));
5438 -- An access formal type
5442 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5444 -- Ada 2005 (AI-254)
5447 AD : constant Node_Id :=
5448 Access_To_Subprogram_Definition
5449 (Parameter_Type (Param_Spec));
5451 if Present (AD) and then Protected_Present (AD) then
5453 Replace_Anonymous_Access_To_Protected_Subprogram
5454 (Param_Spec, Formal_Type);
5459 Set_Etype (Formal, Formal_Type);
5460 Default := Expression (Param_Spec);
5462 if Present (Default) then
5463 if Out_Present (Param_Spec) then
5465 ("default initialization only allowed for IN parameters",
5469 -- Do the special preanalysis of the expression (see section on
5470 -- "Handling of Default Expressions" in the spec of package Sem).
5472 Analyze_Per_Use_Expression (Default, Formal_Type);
5474 -- Check that the designated type of an access parameter's default
5475 -- is not a class-wide type unless the parameter's designated type
5476 -- is also class-wide.
5478 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5479 and then not From_With_Type (Formal_Type)
5480 and then Is_Class_Wide_Default (Default)
5481 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5484 ("access to class-wide expression not allowed here", Default);
5488 -- Ada 2005 (AI-231): Static checks
5490 if Ada_Version >= Ada_05
5491 and then Is_Access_Type (Etype (Formal))
5492 and then Can_Never_Be_Null (Etype (Formal))
5494 Null_Exclusion_Static_Checks (Param_Spec);
5501 -- If this is the formal part of a function specification, analyze the
5502 -- subtype mark in the context where the formals are visible but not
5503 -- yet usable, and may hide outer homographs.
5505 if Nkind (Related_Nod) = N_Function_Specification then
5506 Analyze_Return_Type (Related_Nod);
5509 -- Now set the kind (mode) of each formal
5511 Param_Spec := First (T);
5513 while Present (Param_Spec) loop
5514 Formal := Defining_Identifier (Param_Spec);
5515 Set_Formal_Mode (Formal);
5517 if Ekind (Formal) = E_In_Parameter then
5518 Set_Default_Value (Formal, Expression (Param_Spec));
5520 if Present (Expression (Param_Spec)) then
5521 Default := Expression (Param_Spec);
5523 if Is_Scalar_Type (Etype (Default)) then
5525 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5527 Formal_Type := Entity (Parameter_Type (Param_Spec));
5530 Formal_Type := Access_Definition
5531 (Related_Nod, Parameter_Type (Param_Spec));
5534 Apply_Scalar_Range_Check (Default, Formal_Type);
5542 end Process_Formals;
5544 ----------------------------
5545 -- Reference_Body_Formals --
5546 ----------------------------
5548 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5553 if Error_Posted (Spec) then
5557 Fs := First_Formal (Spec);
5558 Fb := First_Formal (Bod);
5560 while Present (Fs) loop
5561 Generate_Reference (Fs, Fb, 'b');
5564 Style.Check_Identifier (Fb, Fs);
5567 Set_Spec_Entity (Fb, Fs);
5568 Set_Referenced (Fs, False);
5572 end Reference_Body_Formals;
5574 -------------------------
5575 -- Set_Actual_Subtypes --
5576 -------------------------
5578 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5579 Loc : constant Source_Ptr := Sloc (N);
5583 First_Stmt : Node_Id := Empty;
5584 AS_Needed : Boolean;
5587 -- If this is an emtpy initialization procedure, no need to create
5588 -- actual subtypes (small optimization).
5590 if Ekind (Subp) = E_Procedure
5591 and then Is_Null_Init_Proc (Subp)
5596 Formal := First_Formal (Subp);
5597 while Present (Formal) loop
5598 T := Etype (Formal);
5600 -- We never need an actual subtype for a constrained formal
5602 if Is_Constrained (T) then
5605 -- If we have unknown discriminants, then we do not need an actual
5606 -- subtype, or more accurately we cannot figure it out! Note that
5607 -- all class-wide types have unknown discriminants.
5609 elsif Has_Unknown_Discriminants (T) then
5612 -- At this stage we have an unconstrained type that may need an
5613 -- actual subtype. For sure the actual subtype is needed if we have
5614 -- an unconstrained array type.
5616 elsif Is_Array_Type (T) then
5619 -- The only other case needing an actual subtype is an unconstrained
5620 -- record type which is an IN parameter (we cannot generate actual
5621 -- subtypes for the OUT or IN OUT case, since an assignment can
5622 -- change the discriminant values. However we exclude the case of
5623 -- initialization procedures, since discriminants are handled very
5624 -- specially in this context, see the section entitled "Handling of
5625 -- Discriminants" in Einfo.
5627 -- We also exclude the case of Discrim_SO_Functions (functions used
5628 -- in front end layout mode for size/offset values), since in such
5629 -- functions only discriminants are referenced, and not only are such
5630 -- subtypes not needed, but they cannot always be generated, because
5631 -- of order of elaboration issues.
5633 elsif Is_Record_Type (T)
5634 and then Ekind (Formal) = E_In_Parameter
5635 and then Chars (Formal) /= Name_uInit
5636 and then not Is_Unchecked_Union (T)
5637 and then not Is_Discrim_SO_Function (Subp)
5641 -- All other cases do not need an actual subtype
5647 -- Generate actual subtypes for unconstrained arrays and
5648 -- unconstrained discriminated records.
5651 if Nkind (N) = N_Accept_Statement then
5653 -- If expansion is active, The formal is replaced by a local
5654 -- variable that renames the corresponding entry of the
5655 -- parameter block, and it is this local variable that may
5656 -- require an actual subtype.
5658 if Expander_Active then
5659 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5661 Decl := Build_Actual_Subtype (T, Formal);
5664 if Present (Handled_Statement_Sequence (N)) then
5666 First (Statements (Handled_Statement_Sequence (N)));
5667 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5668 Mark_Rewrite_Insertion (Decl);
5670 -- If the accept statement has no body, there will be no
5671 -- reference to the actuals, so no need to compute actual
5678 Decl := Build_Actual_Subtype (T, Formal);
5679 Prepend (Decl, Declarations (N));
5680 Mark_Rewrite_Insertion (Decl);
5683 -- The declaration uses the bounds of an existing object, and
5684 -- therefore needs no constraint checks.
5686 Analyze (Decl, Suppress => All_Checks);
5688 -- We need to freeze manually the generated type when it is
5689 -- inserted anywhere else than in a declarative part.
5691 if Present (First_Stmt) then
5692 Insert_List_Before_And_Analyze (First_Stmt,
5693 Freeze_Entity (Defining_Identifier (Decl), Loc));
5696 if Nkind (N) = N_Accept_Statement
5697 and then Expander_Active
5699 Set_Actual_Subtype (Renamed_Object (Formal),
5700 Defining_Identifier (Decl));
5702 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5706 Next_Formal (Formal);
5708 end Set_Actual_Subtypes;
5710 ---------------------
5711 -- Set_Formal_Mode --
5712 ---------------------
5714 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5715 Spec : constant Node_Id := Parent (Formal_Id);
5718 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5719 -- since we ensure that corresponding actuals are always valid at the
5720 -- point of the call.
5722 if Out_Present (Spec) then
5723 if Ekind (Scope (Formal_Id)) = E_Function
5724 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5726 Error_Msg_N ("functions can only have IN parameters", Spec);
5727 Set_Ekind (Formal_Id, E_In_Parameter);
5729 elsif In_Present (Spec) then
5730 Set_Ekind (Formal_Id, E_In_Out_Parameter);
5733 Set_Ekind (Formal_Id, E_Out_Parameter);
5734 Set_Never_Set_In_Source (Formal_Id, True);
5735 Set_Is_True_Constant (Formal_Id, False);
5736 Set_Current_Value (Formal_Id, Empty);
5740 Set_Ekind (Formal_Id, E_In_Parameter);
5743 -- Set Is_Known_Non_Null for access parameters since the language
5744 -- guarantees that access parameters are always non-null. We also set
5745 -- Can_Never_Be_Null, since there is no way to change the value.
5747 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5749 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
5750 -- null; In Ada 2005, only if then null_exclusion is explicit.
5752 if Ada_Version < Ada_05
5753 or else Can_Never_Be_Null (Etype (Formal_Id))
5755 Set_Is_Known_Non_Null (Formal_Id);
5756 Set_Can_Never_Be_Null (Formal_Id);
5759 -- Ada 2005 (AI-231): Null-exclusion access subtype
5761 elsif Is_Access_Type (Etype (Formal_Id))
5762 and then Can_Never_Be_Null (Etype (Formal_Id))
5764 Set_Is_Known_Non_Null (Formal_Id);
5767 Set_Mechanism (Formal_Id, Default_Mechanism);
5768 Set_Formal_Validity (Formal_Id);
5769 end Set_Formal_Mode;
5771 -------------------------
5772 -- Set_Formal_Validity --
5773 -------------------------
5775 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5777 -- If no validity checking, then we cannot assume anything about the
5778 -- validity of parameters, since we do not know there is any checking
5779 -- of the validity on the call side.
5781 if not Validity_Checks_On then
5784 -- If validity checking for parameters is enabled, this means we are
5785 -- not supposed to make any assumptions about argument values.
5787 elsif Validity_Check_Parameters then
5790 -- If we are checking in parameters, we will assume that the caller is
5791 -- also checking parameters, so we can assume the parameter is valid.
5793 elsif Ekind (Formal_Id) = E_In_Parameter
5794 and then Validity_Check_In_Params
5796 Set_Is_Known_Valid (Formal_Id, True);
5798 -- Similar treatment for IN OUT parameters
5800 elsif Ekind (Formal_Id) = E_In_Out_Parameter
5801 and then Validity_Check_In_Out_Params
5803 Set_Is_Known_Valid (Formal_Id, True);
5805 end Set_Formal_Validity;
5807 ------------------------
5808 -- Subtype_Conformant --
5809 ------------------------
5811 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5814 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5816 end Subtype_Conformant;
5818 ---------------------
5819 -- Type_Conformant --
5820 ---------------------
5822 function Type_Conformant
5823 (New_Id : Entity_Id;
5825 Skip_Controlling_Formals : Boolean := False) return Boolean
5830 (New_Id, Old_Id, Type_Conformant, False, Result,
5831 Skip_Controlling_Formals => Skip_Controlling_Formals);
5833 end Type_Conformant;
5835 -------------------------------
5836 -- Valid_Operator_Definition --
5837 -------------------------------
5839 procedure Valid_Operator_Definition (Designator : Entity_Id) is
5842 Id : constant Name_Id := Chars (Designator);
5846 F := First_Formal (Designator);
5847 while Present (F) loop
5850 if Present (Default_Value (F)) then
5852 ("default values not allowed for operator parameters",
5859 -- Verify that user-defined operators have proper number of arguments
5860 -- First case of operators which can only be unary
5863 or else Id = Name_Op_Abs
5867 -- Case of operators which can be unary or binary
5869 elsif Id = Name_Op_Add
5870 or Id = Name_Op_Subtract
5872 N_OK := (N in 1 .. 2);
5874 -- All other operators can only be binary
5882 ("incorrect number of arguments for operator", Designator);
5886 and then Base_Type (Etype (Designator)) = Standard_Boolean
5887 and then not Is_Intrinsic_Subprogram (Designator)
5890 ("explicit definition of inequality not allowed", Designator);
5892 end Valid_Operator_Definition;