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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7; use Exp_Ch7;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Output; use Output;
44 with Rtsfind; use Rtsfind;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch4; use Sem_Ch4;
49 with Sem_Ch5; use Sem_Ch5;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinput; use Sinput;
64 with Stand; use Stand;
65 with Sinfo; use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
70 with Stylesw; use Stylesw;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Sem_Ch6 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Analyze_Return_Type (N : Node_Id);
83 -- Subsidiary to Process_Formals: analyze subtype mark in function
84 -- specification, in a context where the formals are visible and hide
87 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
88 -- Analyze a generic subprogram body. N is the body to be analyzed, and
89 -- Gen_Id is the defining entity Id for the corresponding spec.
91 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
92 -- If a subprogram has pragma Inline and inlining is active, use generic
93 -- machinery to build an unexpanded body for the subprogram. This body is
94 -- subsequenty used for inline expansions at call sites. If subprogram can
95 -- be inlined (depending on size and nature of local declarations) this
96 -- function returns true. Otherwise subprogram body is treated normally.
97 -- If proper warnings are enabled and the subprogram contains a construct
98 -- that cannot be inlined, the offending construct is flagged accordingly.
100 type Conformance_Type is
101 (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
102 -- Conformance type used for following call, meaning matches the
103 -- RM definitions of the corresponding terms.
105 procedure Check_Conformance
108 Ctype : Conformance_Type;
110 Conforms : out Boolean;
111 Err_Loc : Node_Id := Empty;
112 Get_Inst : Boolean := False);
113 -- Given two entities, this procedure checks that the profiles associated
114 -- with these entities meet the conformance criterion given by the third
115 -- parameter. If they conform, Conforms is set True and control returns
116 -- to the caller. If they do not conform, Conforms is set to False, and
117 -- in addition, if Errmsg is True on the call, proper messages are output
118 -- to complain about the conformance failure. If Err_Loc is non_Empty
119 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
120 -- error messages are placed on the appropriate part of the construct
121 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
122 -- against a formal access-to-subprogram type so Get_Instance_Of must
125 procedure Check_Overriding_Operation
128 -- Check that a subprogram with a pragma Overriding or Optional_Overriding
129 -- is legal. This check is performed here rather than in Sem_Prag because
130 -- the pragma must follow immediately the declaration, and can be treated
131 -- as part of the declaration itself, as described in AI-218.
133 procedure Check_Subprogram_Order (N : Node_Id);
134 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
135 -- the alpha ordering rule for N if this ordering requirement applicable.
137 procedure Check_Returns
141 -- Called to check for missing return statements in a function body, or
142 -- for returns present in a procedure body which has No_Return set. L is
143 -- the handled statement sequence for the subprogram body. This procedure
144 -- checks all flow paths to make sure they either have return (Mode = 'F')
145 -- or do not have a return (Mode = 'P'). The flag Err is set if there are
146 -- any control paths not explicitly terminated by a return in the function
147 -- case, and is True otherwise.
149 function Conforming_Types
152 Ctype : Conformance_Type;
153 Get_Inst : Boolean := False) return Boolean;
154 -- Check that two formal parameter types conform, checking both for
155 -- equality of base types, and where required statically matching
156 -- subtypes, depending on the setting of Ctype.
158 procedure Enter_Overloaded_Entity (S : Entity_Id);
159 -- This procedure makes S, a new overloaded entity, into the first visible
160 -- entity with that name.
162 procedure Install_Entity (E : Entity_Id);
163 -- Make single entity visible. Used for generic formals as well
165 procedure Install_Formals (Id : Entity_Id);
166 -- On entry to a subprogram body, make the formals visible. Note that
167 -- simply placing the subprogram on the scope stack is not sufficient:
168 -- the formals must become the current entities for their names.
170 function Is_Non_Overriding_Operation
172 New_E : Entity_Id) return Boolean;
173 -- Enforce the rule given in 12.3(18): a private operation in an instance
174 -- overrides an inherited operation only if the corresponding operation
175 -- was overriding in the generic. This can happen for primitive operations
176 -- of types derived (in the generic unit) from formal private or formal
179 procedure Make_Inequality_Operator (S : Entity_Id);
180 -- Create the declaration for an inequality operator that is implicitly
181 -- created by a user-defined equality operator that yields a boolean.
183 procedure May_Need_Actuals (Fun : Entity_Id);
184 -- Flag functions that can be called without parameters, i.e. those that
185 -- have no parameters, or those for which defaults exist for all parameters
187 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
188 -- If there is a separate spec for a subprogram or generic subprogram, the
189 -- formals of the body are treated as references to the corresponding
190 -- formals of the spec. This reference does not count as an actual use of
191 -- the formal, in order to diagnose formals that are unused in the body.
193 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
194 -- Formal_Id is an formal parameter entity. This procedure deals with
195 -- setting the proper validity status for this entity, which depends
196 -- on the kind of parameter and the validity checking mode.
198 ---------------------------------------------
199 -- Analyze_Abstract_Subprogram_Declaration --
200 ---------------------------------------------
202 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
203 Designator : constant Entity_Id :=
204 Analyze_Subprogram_Specification (Specification (N));
205 Scop : constant Entity_Id := Current_Scope;
208 Generate_Definition (Designator);
209 Set_Is_Abstract (Designator);
210 New_Overloaded_Entity (Designator);
211 Check_Delayed_Subprogram (Designator);
213 Set_Categorization_From_Scope (Designator, Scop);
215 if Ekind (Scope (Designator)) = E_Protected_Type then
217 ("abstract subprogram not allowed in protected type", N);
220 Generate_Reference_To_Formals (Designator);
221 end Analyze_Abstract_Subprogram_Declaration;
223 ----------------------------
224 -- Analyze_Function_Call --
225 ----------------------------
227 procedure Analyze_Function_Call (N : Node_Id) is
228 P : constant Node_Id := Name (N);
229 L : constant List_Id := Parameter_Associations (N);
235 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
236 -- as B(A, X). If the rewriting is successful, the call has been
237 -- analyzed and we just return.
239 if Nkind (P) = N_Selected_Component
240 and then Name (N) /= P
241 and then Is_Rewrite_Substitution (N)
242 and then Present (Etype (N))
247 -- If error analyzing name, then set Any_Type as result type and return
249 if Etype (P) = Any_Type then
250 Set_Etype (N, Any_Type);
254 -- Otherwise analyze the parameters
258 while Present (Actual) loop
260 Check_Parameterless_Call (Actual);
266 end Analyze_Function_Call;
268 -------------------------------------
269 -- Analyze_Generic_Subprogram_Body --
270 -------------------------------------
272 procedure Analyze_Generic_Subprogram_Body
276 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
277 Kind : constant Entity_Kind := Ekind (Gen_Id);
283 -- Copy body and disable expansion while analyzing the generic For a
284 -- stub, do not copy the stub (which would load the proper body), this
285 -- will be done when the proper body is analyzed.
287 if Nkind (N) /= N_Subprogram_Body_Stub then
288 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
293 Spec := Specification (N);
295 -- Within the body of the generic, the subprogram is callable, and
296 -- behaves like the corresponding non-generic unit.
298 Body_Id := Defining_Entity (Spec);
300 if Kind = E_Generic_Procedure
301 and then Nkind (Spec) /= N_Procedure_Specification
303 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
306 elsif Kind = E_Generic_Function
307 and then Nkind (Spec) /= N_Function_Specification
309 Error_Msg_N ("invalid body for generic function ", Body_Id);
313 Set_Corresponding_Body (Gen_Decl, Body_Id);
315 if Has_Completion (Gen_Id)
316 and then Nkind (Parent (N)) /= N_Subunit
318 Error_Msg_N ("duplicate generic body", N);
321 Set_Has_Completion (Gen_Id);
324 if Nkind (N) = N_Subprogram_Body_Stub then
325 Set_Ekind (Defining_Entity (Specification (N)), Kind);
327 Set_Corresponding_Spec (N, Gen_Id);
330 if Nkind (Parent (N)) = N_Compilation_Unit then
331 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
334 -- Make generic parameters immediately visible in the body. They are
335 -- needed to process the formals declarations. Then make the formals
336 -- visible in a separate step.
342 First_Ent : Entity_Id;
345 First_Ent := First_Entity (Gen_Id);
348 while Present (E) and then not Is_Formal (E) loop
353 Set_Use (Generic_Formal_Declarations (Gen_Decl));
355 -- Now generic formals are visible, and the specification can be
356 -- analyzed, for subsequent conformance check.
358 Body_Id := Analyze_Subprogram_Specification (Spec);
360 -- Make formal parameters visible
364 -- E is the first formal parameter, we loop through the formals
365 -- installing them so that they will be visible.
367 Set_First_Entity (Gen_Id, E);
368 while Present (E) loop
374 -- Visible generic entity is callable within its own body
376 Set_Ekind (Gen_Id, Ekind (Body_Id));
377 Set_Ekind (Body_Id, E_Subprogram_Body);
378 Set_Convention (Body_Id, Convention (Gen_Id));
379 Set_Scope (Body_Id, Scope (Gen_Id));
380 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
382 if Nkind (N) = N_Subprogram_Body_Stub then
384 -- No body to analyze, so restore state of generic unit
386 Set_Ekind (Gen_Id, Kind);
387 Set_Ekind (Body_Id, Kind);
389 if Present (First_Ent) then
390 Set_First_Entity (Gen_Id, First_Ent);
397 -- If this is a compilation unit, it must be made visible explicitly,
398 -- because the compilation of the declaration, unlike other library
399 -- unit declarations, does not. If it is not a unit, the following
400 -- is redundant but harmless.
402 Set_Is_Immediately_Visible (Gen_Id);
403 Reference_Body_Formals (Gen_Id, Body_Id);
405 Set_Actual_Subtypes (N, Current_Scope);
406 Analyze_Declarations (Declarations (N));
408 Analyze (Handled_Statement_Sequence (N));
410 Save_Global_References (Original_Node (N));
412 -- Prior to exiting the scope, include generic formals again (if any
413 -- are present) in the set of local entities.
415 if Present (First_Ent) then
416 Set_First_Entity (Gen_Id, First_Ent);
419 Check_References (Gen_Id);
422 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
424 Check_Subprogram_Order (N);
426 -- Outside of its body, unit is generic again
428 Set_Ekind (Gen_Id, Kind);
429 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
430 Style.Check_Identifier (Body_Id, Gen_Id);
432 end Analyze_Generic_Subprogram_Body;
434 -----------------------------
435 -- Analyze_Operator_Symbol --
436 -----------------------------
438 -- An operator symbol such as "+" or "and" may appear in context where the
439 -- literal denotes an entity name, such as "+"(x, y) or in context when it
440 -- is just a string, as in (conjunction = "or"). In these cases the parser
441 -- generates this node, and the semantics does the disambiguation. Other
442 -- such case are actuals in an instantiation, the generic unit in an
443 -- instantiation, and pragma arguments.
445 procedure Analyze_Operator_Symbol (N : Node_Id) is
446 Par : constant Node_Id := Parent (N);
449 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
450 or else Nkind (Par) = N_Function_Instantiation
451 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
452 or else (Nkind (Par) = N_Pragma_Argument_Association
453 and then not Is_Pragma_String_Literal (Par))
454 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
455 or else (Nkind (Par) = N_Attribute_Reference
456 and then Attribute_Name (Par) /= Name_Value)
458 Find_Direct_Name (N);
461 Change_Operator_Symbol_To_String_Literal (N);
464 end Analyze_Operator_Symbol;
466 -----------------------------------
467 -- Analyze_Parameter_Association --
468 -----------------------------------
470 procedure Analyze_Parameter_Association (N : Node_Id) is
472 Analyze (Explicit_Actual_Parameter (N));
473 end Analyze_Parameter_Association;
475 ----------------------------
476 -- Analyze_Procedure_Call --
477 ----------------------------
479 procedure Analyze_Procedure_Call (N : Node_Id) is
480 Loc : constant Source_Ptr := Sloc (N);
481 P : constant Node_Id := Name (N);
482 Actuals : constant List_Id := Parameter_Associations (N);
486 procedure Analyze_Call_And_Resolve;
487 -- Do Analyze and Resolve calls for procedure call
489 ------------------------------
490 -- Analyze_Call_And_Resolve --
491 ------------------------------
493 procedure Analyze_Call_And_Resolve is
495 if Nkind (N) = N_Procedure_Call_Statement then
497 Resolve (N, Standard_Void_Type);
501 end Analyze_Call_And_Resolve;
503 -- Start of processing for Analyze_Procedure_Call
506 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
507 -- a procedure call or an entry call. The prefix may denote an access
508 -- to subprogram type, in which case an implicit dereference applies.
509 -- If the prefix is an indexed component (without implicit defererence)
510 -- then the construct denotes a call to a member of an entire family.
511 -- If the prefix is a simple name, it may still denote a call to a
512 -- parameterless member of an entry family. Resolution of these various
513 -- interpretations is delicate.
517 -- If error analyzing prefix, then set Any_Type as result and return
519 if Etype (P) = Any_Type then
520 Set_Etype (N, Any_Type);
524 -- Otherwise analyze the parameters
526 if Present (Actuals) then
527 Actual := First (Actuals);
529 while Present (Actual) loop
531 Check_Parameterless_Call (Actual);
536 -- Special processing for Elab_Spec and Elab_Body calls
538 if Nkind (P) = N_Attribute_Reference
539 and then (Attribute_Name (P) = Name_Elab_Spec
540 or else Attribute_Name (P) = Name_Elab_Body)
542 if Present (Actuals) then
544 ("no parameters allowed for this call", First (Actuals));
548 Set_Etype (N, Standard_Void_Type);
551 elsif Is_Entity_Name (P)
552 and then Is_Record_Type (Etype (Entity (P)))
553 and then Remote_AST_I_Dereference (P)
557 elsif Is_Entity_Name (P)
558 and then Ekind (Entity (P)) /= E_Entry_Family
560 if Is_Access_Type (Etype (P))
561 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
562 and then No (Actuals)
563 and then Comes_From_Source (N)
565 Error_Msg_N ("missing explicit dereference in call", N);
568 Analyze_Call_And_Resolve;
570 -- If the prefix is the simple name of an entry family, this is
571 -- a parameterless call from within the task body itself.
573 elsif Is_Entity_Name (P)
574 and then Nkind (P) = N_Identifier
575 and then Ekind (Entity (P)) = E_Entry_Family
576 and then Present (Actuals)
577 and then No (Next (First (Actuals)))
579 -- Can be call to parameterless entry family. What appears to be the
580 -- sole argument is in fact the entry index. Rewrite prefix of node
581 -- accordingly. Source representation is unchanged by this
585 Make_Indexed_Component (Loc,
587 Make_Selected_Component (Loc,
588 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
589 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
590 Expressions => Actuals);
592 Set_Etype (New_N, Standard_Void_Type);
593 Set_Parameter_Associations (N, No_List);
594 Analyze_Call_And_Resolve;
596 elsif Nkind (P) = N_Explicit_Dereference then
597 if Ekind (Etype (P)) = E_Subprogram_Type then
598 Analyze_Call_And_Resolve;
600 Error_Msg_N ("expect access to procedure in call", P);
603 -- The name can be a selected component or an indexed component that
604 -- yields an access to subprogram. Such a prefix is legal if the call
605 -- has parameter associations.
607 elsif Is_Access_Type (Etype (P))
608 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
610 if Present (Actuals) then
611 Analyze_Call_And_Resolve;
613 Error_Msg_N ("missing explicit dereference in call ", N);
616 -- If not an access to subprogram, then the prefix must resolve to the
617 -- name of an entry, entry family, or protected operation.
619 -- For the case of a simple entry call, P is a selected component where
620 -- the prefix is the task and the selector name is the entry. A call to
621 -- a protected procedure will have the same syntax. If the protected
622 -- object contains overloaded operations, the entity may appear as a
623 -- function, the context will select the operation whose type is Void.
625 elsif Nkind (P) = N_Selected_Component
626 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
628 Ekind (Entity (Selector_Name (P))) = E_Procedure
630 Ekind (Entity (Selector_Name (P))) = E_Function)
632 Analyze_Call_And_Resolve;
634 elsif Nkind (P) = N_Selected_Component
635 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
636 and then Present (Actuals)
637 and then No (Next (First (Actuals)))
639 -- Can be call to parameterless entry family. What appears to be the
640 -- sole argument is in fact the entry index. Rewrite prefix of node
641 -- accordingly. Source representation is unchanged by this
645 Make_Indexed_Component (Loc,
646 Prefix => New_Copy (P),
647 Expressions => Actuals);
649 Set_Etype (New_N, Standard_Void_Type);
650 Set_Parameter_Associations (N, No_List);
651 Analyze_Call_And_Resolve;
653 -- For the case of a reference to an element of an entry family, P is
654 -- an indexed component whose prefix is a selected component (task and
655 -- entry family), and whose index is the entry family index.
657 elsif Nkind (P) = N_Indexed_Component
658 and then Nkind (Prefix (P)) = N_Selected_Component
659 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
661 Analyze_Call_And_Resolve;
663 -- If the prefix is the name of an entry family, it is a call from
664 -- within the task body itself.
666 elsif Nkind (P) = N_Indexed_Component
667 and then Nkind (Prefix (P)) = N_Identifier
668 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
671 Make_Selected_Component (Loc,
672 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
673 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
674 Rewrite (Prefix (P), New_N);
676 Analyze_Call_And_Resolve;
678 -- Anything else is an error
681 Error_Msg_N ("Invalid procedure or entry call", N);
683 end Analyze_Procedure_Call;
685 ------------------------------
686 -- Analyze_Return_Statement --
687 ------------------------------
689 procedure Analyze_Return_Statement (N : Node_Id) is
690 Loc : constant Source_Ptr := Sloc (N);
692 Scope_Id : Entity_Id;
697 -- Find subprogram or accept statement enclosing the return statement
700 for J in reverse 0 .. Scope_Stack.Last loop
701 Scope_Id := Scope_Stack.Table (J).Entity;
702 exit when Ekind (Scope_Id) /= E_Block and then
703 Ekind (Scope_Id) /= E_Loop;
706 pragma Assert (Present (Scope_Id));
708 Kind := Ekind (Scope_Id);
709 Expr := Expression (N);
711 if Kind /= E_Function
712 and then Kind /= E_Generic_Function
713 and then Kind /= E_Procedure
714 and then Kind /= E_Generic_Procedure
715 and then Kind /= E_Entry
716 and then Kind /= E_Entry_Family
718 Error_Msg_N ("illegal context for return statement", N);
720 elsif Present (Expr) then
721 if Kind = E_Function or else Kind = E_Generic_Function then
722 Set_Return_Present (Scope_Id);
723 R_Type := Etype (Scope_Id);
724 Set_Return_Type (N, R_Type);
725 Analyze_And_Resolve (Expr, R_Type);
727 if (Is_Class_Wide_Type (Etype (Expr))
728 or else Is_Dynamically_Tagged (Expr))
729 and then not Is_Class_Wide_Type (R_Type)
732 ("dynamically tagged expression not allowed!", Expr);
735 Apply_Constraint_Check (Expr, R_Type);
737 -- ??? A real run-time accessibility check is needed in cases
738 -- involving dereferences of access parameters. For now we just
739 -- check the static cases.
741 if Is_Return_By_Reference_Type (Etype (Scope_Id))
742 and then Object_Access_Level (Expr)
743 > Subprogram_Access_Level (Scope_Id)
746 Make_Raise_Program_Error (Loc,
747 Reason => PE_Accessibility_Check_Failed));
751 ("cannot return a local value by reference?", N);
753 ("& will be raised at run time?!",
754 N, Standard_Program_Error);
757 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
758 Error_Msg_N ("procedure cannot return value (use function)", N);
761 Error_Msg_N ("accept statement cannot return value", N);
764 -- No expression present
767 if Kind = E_Function or Kind = E_Generic_Function then
768 Error_Msg_N ("missing expression in return from function", N);
771 if (Ekind (Scope_Id) = E_Procedure
772 or else Ekind (Scope_Id) = E_Generic_Procedure)
773 and then No_Return (Scope_Id)
776 ("RETURN statement not allowed (No_Return)", N);
780 Check_Unreachable_Code (N);
781 end Analyze_Return_Statement;
783 -------------------------
784 -- Analyze_Return_Type --
785 -------------------------
787 procedure Analyze_Return_Type (N : Node_Id) is
788 Designator : constant Entity_Id := Defining_Entity (N);
789 Typ : Entity_Id := Empty;
792 if Subtype_Mark (N) /= Error then
793 Find_Type (Subtype_Mark (N));
794 Typ := Entity (Subtype_Mark (N));
795 Set_Etype (Designator, Typ);
797 if Ekind (Typ) = E_Incomplete_Type
798 or else (Is_Class_Wide_Type (Typ)
800 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
803 ("invalid use of incomplete type", Subtype_Mark (N));
807 Set_Etype (Designator, Any_Type);
809 end Analyze_Return_Type;
811 -----------------------------
812 -- Analyze_Subprogram_Body --
813 -----------------------------
815 -- This procedure is called for regular subprogram bodies, generic bodies,
816 -- and for subprogram stubs of both kinds. In the case of stubs, only the
817 -- specification matters, and is used to create a proper declaration for
818 -- the subprogram, or to perform conformance checks.
820 procedure Analyze_Subprogram_Body (N : Node_Id) is
821 Loc : constant Source_Ptr := Sloc (N);
822 Body_Spec : constant Node_Id := Specification (N);
823 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
824 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
825 Body_Deleted : constant Boolean := False;
829 Spec_Decl : Node_Id := Empty;
830 Last_Formal : Entity_Id := Empty;
831 Conformant : Boolean;
832 Missing_Ret : Boolean;
835 procedure Check_Following_Pragma;
836 -- If front-end inlining is enabled, look ahead to recognize a pragma
837 -- that may appear after the body.
839 procedure Check_Following_Pragma is
843 if Front_End_Inlining
844 and then Is_List_Member (N)
845 and then Present (Spec_Decl)
846 and then List_Containing (N) = List_Containing (Spec_Decl)
851 and then Nkind (Prag) = N_Pragma
852 and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
855 (Expression (First (Pragma_Argument_Associations (Prag))))
861 end Check_Following_Pragma;
863 -- Start of processing for Analyze_Subprogram_Body
867 Write_Str ("==== Compiling subprogram body ");
868 Write_Name (Chars (Body_Id));
869 Write_Str (" from ");
870 Write_Location (Loc);
874 Trace_Scope (N, Body_Id, " Analyze subprogram");
876 -- Generic subprograms are handled separately. They always have a
877 -- generic specification. Determine whether current scope has a
878 -- previous declaration.
880 -- If the subprogram body is defined within an instance of the same
881 -- name, the instance appears as a package renaming, and will be hidden
882 -- within the subprogram.
885 and then not Is_Overloadable (Prev_Id)
886 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
887 or else Comes_From_Source (Prev_Id))
889 if Is_Generic_Subprogram (Prev_Id) then
891 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
892 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
894 Analyze_Generic_Subprogram_Body (N, Spec_Id);
898 -- Previous entity conflicts with subprogram name. Attempting to
899 -- enter name will post error.
901 Enter_Name (Body_Id);
905 -- Non-generic case, find the subprogram declaration, if one was seen,
906 -- or enter new overloaded entity in the current scope. If the
907 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
908 -- part of the context of one of its subunits. No need to redo the
911 elsif Prev_Id = Body_Id
912 and then Has_Completion (Body_Id)
917 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
919 if Nkind (N) = N_Subprogram_Body_Stub
920 or else No (Corresponding_Spec (N))
922 Spec_Id := Find_Corresponding_Spec (N);
924 -- If this is a duplicate body, no point in analyzing it
926 if Error_Posted (N) then
930 -- A subprogram body should cause freezing of its own declaration,
931 -- but if there was no previous explicit declaration, then the
932 -- subprogram will get frozen too late (there may be code within
933 -- the body that depends on the subprogram having been frozen,
934 -- such as uses of extra formals), so we force it to be frozen
935 -- here. Same holds if the body and the spec are compilation
939 Freeze_Before (N, Body_Id);
941 elsif Nkind (Parent (N)) = N_Compilation_Unit then
942 Freeze_Before (N, Spec_Id);
945 Spec_Id := Corresponding_Spec (N);
949 -- Do not inline any subprogram that contains nested subprograms, since
950 -- the backend inlining circuit seems to generate uninitialized
951 -- references in this case. We know this happens in the case of front
952 -- end ZCX support, but it also appears it can happen in other cases as
953 -- well. The backend often rejects attempts to inline in the case of
954 -- nested procedures anyway, so little if anything is lost by this.
955 -- Note that this is test is for the benefit of the back-end. There is
956 -- a separate test for front-end inlining that also rejects nested
959 -- Do not do this test if errors have been detected, because in some
960 -- error cases, this code blows up, and we don't need it anyway if
961 -- there have been errors, since we won't get to the linker anyway.
963 if Comes_From_Source (Body_Id)
964 and then Serious_Errors_Detected = 0
968 P_Ent := Scope (P_Ent);
969 exit when No (P_Ent) or else P_Ent = Standard_Standard;
971 if Is_Subprogram (P_Ent) then
972 Set_Is_Inlined (P_Ent, False);
974 if Comes_From_Source (P_Ent)
975 and then Has_Pragma_Inline (P_Ent)
978 ("cannot inline& (nested subprogram)?",
985 -- Case of fully private operation in the body of the protected type.
986 -- We must create a declaration for the subprogram, in order to attach
987 -- the protected subprogram that will be used in internal calls.
990 and then Comes_From_Source (N)
991 and then Is_Protected_Type (Current_Scope)
1000 Formal := First_Formal (Body_Id);
1002 -- The protected operation always has at least one formal, namely
1003 -- the object itself, but it is only placed in the parameter list
1004 -- if expansion is enabled.
1007 or else Expander_Active
1015 while Present (Formal) loop
1017 (Make_Parameter_Specification (Loc,
1018 Defining_Identifier =>
1019 Make_Defining_Identifier (Sloc (Formal),
1020 Chars => Chars (Formal)),
1021 In_Present => In_Present (Parent (Formal)),
1022 Out_Present => Out_Present (Parent (Formal)),
1024 New_Reference_To (Etype (Formal), Loc),
1026 New_Copy_Tree (Expression (Parent (Formal)))),
1029 Next_Formal (Formal);
1032 if Nkind (Body_Spec) = N_Procedure_Specification then
1034 Make_Procedure_Specification (Loc,
1035 Defining_Unit_Name =>
1036 Make_Defining_Identifier (Sloc (Body_Id),
1037 Chars => Chars (Body_Id)),
1038 Parameter_Specifications => Plist);
1041 Make_Function_Specification (Loc,
1042 Defining_Unit_Name =>
1043 Make_Defining_Identifier (Sloc (Body_Id),
1044 Chars => Chars (Body_Id)),
1045 Parameter_Specifications => Plist,
1046 Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1050 Make_Subprogram_Declaration (Loc,
1051 Specification => New_Spec);
1052 Insert_Before (N, Decl);
1053 Spec_Id := Defining_Unit_Name (New_Spec);
1055 -- Indicate that the entity comes from source, to ensure that
1056 -- cross-reference information is properly generated. The body
1057 -- itself is rewritten during expansion, and the body entity will
1058 -- not appear in calls to the operation.
1060 Set_Comes_From_Source (Spec_Id, True);
1062 Set_Has_Completion (Spec_Id);
1063 Set_Convention (Spec_Id, Convention_Protected);
1066 elsif Present (Spec_Id) then
1067 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1070 -- Place subprogram on scope stack, and make formals visible. If there
1071 -- is a spec, the visible entity remains that of the spec.
1073 if Present (Spec_Id) then
1074 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1076 Style.Check_Identifier (Body_Id, Spec_Id);
1079 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1080 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1082 if Is_Abstract (Spec_Id) then
1083 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1086 Set_Convention (Body_Id, Convention (Spec_Id));
1087 Set_Has_Completion (Spec_Id);
1089 if Is_Protected_Type (Scope (Spec_Id)) then
1090 Set_Privals_Chain (Spec_Id, New_Elmt_List);
1093 -- If this is a body generated for a renaming, do not check for
1094 -- full conformance. The check is redundant, because the spec of
1095 -- the body is a copy of the spec in the renaming declaration,
1096 -- and the test can lead to spurious errors on nested defaults.
1098 if Present (Spec_Decl)
1099 and then not Comes_From_Source (N)
1101 (Nkind (Original_Node (Spec_Decl)) =
1102 N_Subprogram_Renaming_Declaration
1103 or else (Present (Corresponding_Body (Spec_Decl))
1105 Nkind (Unit_Declaration_Node
1106 (Corresponding_Body (Spec_Decl))) =
1107 N_Subprogram_Renaming_Declaration))
1113 Fully_Conformant, True, Conformant, Body_Id);
1116 -- If the body is not fully conformant, we have to decide if we
1117 -- should analyze it or not. If it has a really messed up profile
1118 -- then we probably should not analyze it, since we will get too
1119 -- many bogus messages.
1121 -- Our decision is to go ahead in the non-fully conformant case
1122 -- only if it is at least mode conformant with the spec. Note
1123 -- that the call to Check_Fully_Conformant has issued the proper
1124 -- error messages to complain about the lack of conformance.
1127 and then not Mode_Conformant (Body_Id, Spec_Id)
1133 if Spec_Id /= Body_Id then
1134 Reference_Body_Formals (Spec_Id, Body_Id);
1137 if Nkind (N) /= N_Subprogram_Body_Stub then
1138 Set_Corresponding_Spec (N, Spec_Id);
1139 Install_Formals (Spec_Id);
1140 Last_Formal := Last_Entity (Spec_Id);
1141 New_Scope (Spec_Id);
1143 -- Make sure that the subprogram is immediately visible. For
1144 -- child units that have no separate spec this is indispensable.
1145 -- Otherwise it is safe albeit redundant.
1147 Set_Is_Immediately_Visible (Spec_Id);
1150 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1151 Set_Ekind (Body_Id, E_Subprogram_Body);
1152 Set_Scope (Body_Id, Scope (Spec_Id));
1154 -- Case of subprogram body with no previous spec
1158 and then Comes_From_Source (Body_Id)
1159 and then not Suppress_Style_Checks (Body_Id)
1160 and then not In_Instance
1162 Style.Body_With_No_Spec (N);
1165 New_Overloaded_Entity (Body_Id);
1167 if Nkind (N) /= N_Subprogram_Body_Stub then
1168 Set_Acts_As_Spec (N);
1169 Generate_Definition (Body_Id);
1171 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1172 Generate_Reference_To_Formals (Body_Id);
1173 Install_Formals (Body_Id);
1174 New_Scope (Body_Id);
1178 -- If this is the proper body of a stub, we must verify that the stub
1179 -- conforms to the body, and to the previous spec if one was present.
1180 -- we know already that the body conforms to that spec. This test is
1181 -- only required for subprograms that come from source.
1183 if Nkind (Parent (N)) = N_Subunit
1184 and then Comes_From_Source (N)
1185 and then not Error_Posted (Body_Id)
1186 and then Nkind (Corresponding_Stub (Parent (N))) =
1187 N_Subprogram_Body_Stub
1190 Old_Id : constant Entity_Id :=
1192 (Specification (Corresponding_Stub (Parent (N))));
1194 Conformant : Boolean := False;
1197 if No (Spec_Id) then
1198 Check_Fully_Conformant (Body_Id, Old_Id);
1202 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1204 if not Conformant then
1206 -- The stub was taken to be a new declaration. Indicate
1207 -- that it lacks a body.
1209 Set_Has_Completion (Old_Id, False);
1215 Set_Has_Completion (Body_Id);
1216 Check_Eliminated (Body_Id);
1218 if Nkind (N) = N_Subprogram_Body_Stub then
1221 elsif Present (Spec_Id)
1222 and then Expander_Active
1224 Check_Following_Pragma;
1226 if Is_Always_Inlined (Spec_Id)
1227 or else (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining)
1229 Build_Body_To_Inline (N, Spec_Id);
1233 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1234 -- if its specification we have to install the private withed units.
1236 if Is_Compilation_Unit (Body_Id)
1237 and then Scope (Body_Id) = Standard_Standard
1239 Install_Private_With_Clauses (Body_Id);
1242 -- Now we can go on to analyze the body
1244 HSS := Handled_Statement_Sequence (N);
1245 Set_Actual_Subtypes (N, Current_Scope);
1246 Analyze_Declarations (Declarations (N));
1249 Process_End_Label (HSS, 't', Current_Scope);
1251 Check_Subprogram_Order (N);
1252 Set_Analyzed (Body_Id);
1254 -- If we have a separate spec, then the analysis of the declarations
1255 -- caused the entities in the body to be chained to the spec id, but
1256 -- we want them chained to the body id. Only the formal parameters
1257 -- end up chained to the spec id in this case.
1259 if Present (Spec_Id) then
1261 -- If a parent unit is categorized, the context of a subunit must
1262 -- conform to the categorization. Conversely, if a child unit is
1263 -- categorized, the parents themselves must conform.
1265 if Nkind (Parent (N)) = N_Subunit then
1266 Validate_Categorization_Dependency (N, Spec_Id);
1268 elsif Is_Child_Unit (Spec_Id) then
1269 Validate_Categorization_Dependency
1270 (Unit_Declaration_Node (Spec_Id), Spec_Id);
1273 if Present (Last_Formal) then
1275 (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1276 Set_Next_Entity (Last_Formal, Empty);
1277 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1278 Set_Last_Entity (Spec_Id, Last_Formal);
1281 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1282 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1283 Set_First_Entity (Spec_Id, Empty);
1284 Set_Last_Entity (Spec_Id, Empty);
1288 -- If function, check return statements
1290 if Nkind (Body_Spec) = N_Function_Specification then
1295 if Present (Spec_Id) then
1301 if Return_Present (Id) then
1302 Check_Returns (HSS, 'F', Missing_Ret);
1305 Set_Has_Missing_Return (Id);
1308 elsif not Is_Machine_Code_Subprogram (Id)
1309 and then not Body_Deleted
1311 Error_Msg_N ("missing RETURN statement in function body", N);
1315 -- If procedure with No_Return, check returns
1317 elsif Nkind (Body_Spec) = N_Procedure_Specification
1318 and then Present (Spec_Id)
1319 and then No_Return (Spec_Id)
1321 Check_Returns (HSS, 'P', Missing_Ret);
1324 -- Now we are going to check for variables that are never modified in
1325 -- the body of the procedure. We omit these checks if the first
1326 -- statement of the procedure raises an exception. In particular this
1327 -- deals with the common idiom of a stubbed function, which might
1328 -- appear as something like
1330 -- function F (A : Integer) return Some_Type;
1333 -- raise Program_Error;
1337 -- Here the purpose of X is simply to satisfy the (annoying)
1338 -- requirement in Ada that there be at least one return, and we
1339 -- certainly do not want to go posting warnings on X that it is not
1343 Stm : Node_Id := First (Statements (HSS));
1346 -- Skip an initial label (for one thing this occurs when we are in
1347 -- front end ZCX mode, but in any case it is irrelevant).
1349 if Nkind (Stm) = N_Label then
1353 -- Do the test on the original statement before expansion
1356 Ostm : constant Node_Id := Original_Node (Stm);
1359 -- If explicit raise statement, return with no checks
1361 if Nkind (Ostm) = N_Raise_Statement then
1364 -- Check for explicit call cases which likely raise an exception
1366 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1367 if Is_Entity_Name (Name (Ostm)) then
1369 Ent : constant Entity_Id := Entity (Name (Ostm));
1372 -- If the procedure is marked No_Return, then likely it
1373 -- raises an exception, but in any case it is not coming
1374 -- back here, so no need to check beyond the call.
1376 if Ekind (Ent) = E_Procedure
1377 and then No_Return (Ent)
1381 -- If the procedure name is Raise_Exception, then also
1382 -- assume that it raises an exception. The main target
1383 -- here is Ada.Exceptions.Raise_Exception, but this name
1384 -- is pretty evocative in any context! Note that the
1385 -- procedure in Ada.Exceptions is not marked No_Return
1386 -- because of the annoying case of the null exception Id.
1388 elsif Chars (Ent) = Name_Raise_Exception then
1397 -- Check for variables that are never modified
1403 -- If there is a separate spec, then transfer Never_Set_In_Source
1404 -- flags from out parameters to the corresponding entities in the
1405 -- body. The reason we do that is we want to post error flags on
1406 -- the body entities, not the spec entities.
1408 if Present (Spec_Id) then
1409 E1 := First_Entity (Spec_Id);
1411 while Present (E1) loop
1412 if Ekind (E1) = E_Out_Parameter then
1413 E2 := First_Entity (Body_Id);
1414 while Present (E2) loop
1415 exit when Chars (E1) = Chars (E2);
1419 if Present (E2) then
1420 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1428 -- Check references in body unless it was deleted. Note that the
1429 -- check of Body_Deleted here is not just for efficiency, it is
1430 -- necessary to avoid junk warnings on formal parameters.
1432 if not Body_Deleted then
1433 Check_References (Body_Id);
1436 end Analyze_Subprogram_Body;
1438 ------------------------------------
1439 -- Analyze_Subprogram_Declaration --
1440 ------------------------------------
1442 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1443 Designator : constant Entity_Id :=
1444 Analyze_Subprogram_Specification (Specification (N));
1445 Scop : constant Entity_Id := Current_Scope;
1447 -- Start of processing for Analyze_Subprogram_Declaration
1450 Generate_Definition (Designator);
1452 -- Check for RCI unit subprogram declarations against in-lined
1453 -- subprograms and subprograms having access parameter or limited
1454 -- parameter without Read and Write (RM E.2.3(12-13)).
1456 Validate_RCI_Subprogram_Declaration (N);
1460 Defining_Entity (N),
1461 " Analyze subprogram spec. ");
1463 if Debug_Flag_C then
1464 Write_Str ("==== Compiling subprogram spec ");
1465 Write_Name (Chars (Designator));
1466 Write_Str (" from ");
1467 Write_Location (Sloc (N));
1471 New_Overloaded_Entity (Designator);
1472 Check_Delayed_Subprogram (Designator);
1474 -- What is the following code for, it used to be
1476 -- ??? Set_Suppress_Elaboration_Checks
1477 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
1479 -- The following seems equivalent, but a bit dubious
1481 if Elaboration_Checks_Suppressed (Designator) then
1482 Set_Kill_Elaboration_Checks (Designator);
1485 if Scop /= Standard_Standard
1486 and then not Is_Child_Unit (Designator)
1488 Set_Categorization_From_Scope (Designator, Scop);
1490 -- For a compilation unit, check for library-unit pragmas
1492 New_Scope (Designator);
1493 Set_Categorization_From_Pragmas (N);
1494 Validate_Categorization_Dependency (N, Designator);
1498 -- For a compilation unit, set body required. This flag will only be
1499 -- reset if a valid Import or Interface pragma is processed later on.
1501 if Nkind (Parent (N)) = N_Compilation_Unit then
1502 Set_Body_Required (Parent (N), True);
1505 Generate_Reference_To_Formals (Designator);
1506 Check_Eliminated (Designator);
1508 if Comes_From_Source (N)
1509 and then Is_List_Member (N)
1511 Check_Overriding_Operation (N, Designator);
1513 end Analyze_Subprogram_Declaration;
1515 --------------------------------------
1516 -- Analyze_Subprogram_Specification --
1517 --------------------------------------
1519 -- Reminder: N here really is a subprogram specification (not a subprogram
1520 -- declaration). This procedure is called to analyze the specification in
1521 -- both subprogram bodies and subprogram declarations (specs).
1523 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1524 Designator : constant Entity_Id := Defining_Entity (N);
1525 Formals : constant List_Id := Parameter_Specifications (N);
1528 Generate_Definition (Designator);
1530 if Nkind (N) = N_Function_Specification then
1531 Set_Ekind (Designator, E_Function);
1532 Set_Mechanism (Designator, Default_Mechanism);
1535 Set_Ekind (Designator, E_Procedure);
1536 Set_Etype (Designator, Standard_Void_Type);
1539 -- Introduce new scope for analysis of the formals and of the
1542 Set_Scope (Designator, Current_Scope);
1544 if Present (Formals) then
1545 New_Scope (Designator);
1546 Process_Formals (Formals, N);
1549 elsif Nkind (N) = N_Function_Specification then
1550 Analyze_Return_Type (N);
1553 if Nkind (N) = N_Function_Specification then
1554 if Nkind (Designator) = N_Defining_Operator_Symbol then
1555 Valid_Operator_Definition (Designator);
1558 May_Need_Actuals (Designator);
1560 if Is_Abstract (Etype (Designator))
1561 and then Nkind (Parent (N))
1562 /= N_Abstract_Subprogram_Declaration
1563 and then (Nkind (Parent (N)))
1564 /= N_Formal_Abstract_Subprogram_Declaration
1565 and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1566 or else not Is_Entity_Name (Name (Parent (N)))
1567 or else not Is_Abstract (Entity (Name (Parent (N)))))
1570 ("function that returns abstract type must be abstract", N);
1575 end Analyze_Subprogram_Specification;
1577 --------------------------
1578 -- Build_Body_To_Inline --
1579 --------------------------
1581 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1582 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1583 Original_Body : Node_Id;
1584 Body_To_Analyze : Node_Id;
1585 Max_Size : constant := 10;
1586 Stat_Count : Integer := 0;
1588 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1589 -- Check for declarations that make inlining not worthwhile
1591 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
1592 -- Check for statements that make inlining not worthwhile: any tasking
1593 -- statement, nested at any level. Keep track of total number of
1594 -- elementary statements, as a measure of acceptable size.
1596 function Has_Pending_Instantiation return Boolean;
1597 -- If some enclosing body contains instantiations that appear before
1598 -- the corresponding generic body, the enclosing body has a freeze node
1599 -- so that it can be elaborated after the generic itself. This might
1600 -- conflict with subsequent inlinings, so that it is unsafe to try to
1601 -- inline in such a case.
1603 procedure Remove_Pragmas;
1604 -- A pragma Unreferenced that mentions a formal parameter has no
1605 -- meaning when the body is inlined and the formals are rewritten.
1606 -- Remove it from body to inline. The analysis of the non-inlined body
1607 -- will handle the pragma properly.
1609 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1610 -- If the body of the subprogram includes a call that returns an
1611 -- unconstrained type, the secondary stack is involved, and it
1612 -- is not worth inlining.
1614 ------------------------------
1615 -- Has_Excluded_Declaration --
1616 ------------------------------
1618 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1621 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1622 -- Nested subprograms make a given body ineligible for inlining, but
1623 -- we make an exception for instantiations of unchecked conversion.
1624 -- The body has not been analyzed yet, so check the name, and verify
1625 -- that the visible entity with that name is the predefined unit.
1627 -----------------------------
1628 -- Is_Unchecked_Conversion --
1629 -----------------------------
1631 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1632 Id : constant Node_Id := Name (D);
1636 if Nkind (Id) = N_Identifier
1637 and then Chars (Id) = Name_Unchecked_Conversion
1639 Conv := Current_Entity (Id);
1641 elsif Nkind (Id) = N_Selected_Component
1642 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1644 Conv := Current_Entity (Selector_Name (Id));
1652 and then Scope (Conv) = Standard_Standard
1653 and then Is_Intrinsic_Subprogram (Conv);
1654 end Is_Unchecked_Conversion;
1656 -- Start of processing for Has_Excluded_Declaration
1661 while Present (D) loop
1662 if (Nkind (D) = N_Function_Instantiation
1663 and then not Is_Unchecked_Conversion (D))
1664 or else Nkind (D) = N_Protected_Type_Declaration
1665 or else Nkind (D) = N_Package_Declaration
1666 or else Nkind (D) = N_Package_Instantiation
1667 or else Nkind (D) = N_Subprogram_Body
1668 or else Nkind (D) = N_Procedure_Instantiation
1669 or else Nkind (D) = N_Task_Type_Declaration
1672 ("cannot inline & (non-allowed declaration)?", D, Subp);
1680 end Has_Excluded_Declaration;
1682 ----------------------------
1683 -- Has_Excluded_Statement --
1684 ----------------------------
1686 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1693 while Present (S) loop
1694 Stat_Count := Stat_Count + 1;
1696 if Nkind (S) = N_Abort_Statement
1697 or else Nkind (S) = N_Asynchronous_Select
1698 or else Nkind (S) = N_Conditional_Entry_Call
1699 or else Nkind (S) = N_Delay_Relative_Statement
1700 or else Nkind (S) = N_Delay_Until_Statement
1701 or else Nkind (S) = N_Selective_Accept
1702 or else Nkind (S) = N_Timed_Entry_Call
1705 ("cannot inline & (non-allowed statement)?", S, Subp);
1708 elsif Nkind (S) = N_Block_Statement then
1709 if Present (Declarations (S))
1710 and then Has_Excluded_Declaration (Declarations (S))
1714 elsif Present (Handled_Statement_Sequence (S))
1717 (Exception_Handlers (Handled_Statement_Sequence (S)))
1719 Has_Excluded_Statement
1720 (Statements (Handled_Statement_Sequence (S))))
1725 elsif Nkind (S) = N_Case_Statement then
1726 E := First (Alternatives (S));
1727 while Present (E) loop
1728 if Has_Excluded_Statement (Statements (E)) then
1735 elsif Nkind (S) = N_If_Statement then
1736 if Has_Excluded_Statement (Then_Statements (S)) then
1740 if Present (Elsif_Parts (S)) then
1741 E := First (Elsif_Parts (S));
1742 while Present (E) loop
1743 if Has_Excluded_Statement (Then_Statements (E)) then
1750 if Present (Else_Statements (S))
1751 and then Has_Excluded_Statement (Else_Statements (S))
1756 elsif Nkind (S) = N_Loop_Statement
1757 and then Has_Excluded_Statement (Statements (S))
1766 end Has_Excluded_Statement;
1768 -------------------------------
1769 -- Has_Pending_Instantiation --
1770 -------------------------------
1772 function Has_Pending_Instantiation return Boolean is
1773 S : Entity_Id := Current_Scope;
1776 while Present (S) loop
1777 if Is_Compilation_Unit (S)
1778 or else Is_Child_Unit (S)
1781 elsif Ekind (S) = E_Package
1782 and then Has_Forward_Instantiation (S)
1791 end Has_Pending_Instantiation;
1793 --------------------
1794 -- Remove_Pragmas --
1795 --------------------
1797 procedure Remove_Pragmas is
1802 Decl := First (Declarations (Body_To_Analyze));
1803 while Present (Decl) loop
1806 if Nkind (Decl) = N_Pragma
1807 and then Chars (Decl) = Name_Unreferenced
1816 --------------------------
1817 -- Uses_Secondary_Stack --
1818 --------------------------
1820 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
1821 function Check_Call (N : Node_Id) return Traverse_Result;
1822 -- Look for function calls that return an unconstrained type
1828 function Check_Call (N : Node_Id) return Traverse_Result is
1830 if Nkind (N) = N_Function_Call
1831 and then Is_Entity_Name (Name (N))
1832 and then Is_Composite_Type (Etype (Entity (Name (N))))
1833 and then not Is_Constrained (Etype (Entity (Name (N))))
1836 ("cannot inline & (call returns unconstrained type)?",
1844 function Check_Calls is new Traverse_Func (Check_Call);
1847 return Check_Calls (Bod) = Abandon;
1848 end Uses_Secondary_Stack;
1850 -- Start of processing for Build_Body_To_Inline
1853 if Nkind (Decl) = N_Subprogram_Declaration
1854 and then Present (Body_To_Inline (Decl))
1856 return; -- Done already.
1858 -- Functions that return unconstrained composite types will require
1859 -- secondary stack handling, and cannot currently be inlined.
1860 -- Ditto for functions that return controlled types, where controlled
1861 -- actions interfere in complex ways with inlining.
1863 elsif Ekind (Subp) = E_Function
1864 and then not Is_Scalar_Type (Etype (Subp))
1865 and then not Is_Access_Type (Etype (Subp))
1866 and then not Is_Constrained (Etype (Subp))
1869 ("cannot inline & (unconstrained return type)?", N, Subp);
1872 elsif Ekind (Subp) = E_Function
1873 and then Controlled_Type (Etype (Subp))
1876 ("cannot inline & (controlled return type)?", N, Subp);
1880 if Present (Declarations (N))
1881 and then Has_Excluded_Declaration (Declarations (N))
1886 if Present (Handled_Statement_Sequence (N)) then
1887 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1889 ("cannot inline& (exception handler)?",
1890 First (Exception_Handlers (Handled_Statement_Sequence (N))),
1894 Has_Excluded_Statement
1895 (Statements (Handled_Statement_Sequence (N)))
1901 -- We do not inline a subprogram that is too large, unless it is
1902 -- marked Inline_Always. This pragma does not suppress the other
1903 -- checks on inlining (forbidden declarations, handlers, etc).
1905 if Stat_Count > Max_Size
1906 and then not Is_Always_Inlined (Subp)
1908 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
1912 if Has_Pending_Instantiation then
1914 ("cannot inline& (forward instance within enclosing body)?",
1919 -- Within an instance, the body to inline must be treated as a nested
1920 -- generic, so that the proper global references are preserved.
1923 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1924 Original_Body := Copy_Generic_Node (N, Empty, True);
1926 Original_Body := Copy_Separate_Tree (N);
1929 -- We need to capture references to the formals in order to substitute
1930 -- the actuals at the point of inlining, i.e. instantiation. To treat
1931 -- the formals as globals to the body to inline, we nest it within
1932 -- a dummy parameterless subprogram, declared within the real one.
1933 -- To avoid generating an internal name (which is never public, and
1934 -- which affects serial numbers of other generated names), we use
1935 -- an internal symbol that cannot conflict with user declarations.
1937 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1938 Set_Defining_Unit_Name
1939 (Specification (Original_Body),
1940 Make_Defining_Identifier (Sloc (N), Name_uParent));
1941 Set_Corresponding_Spec (Original_Body, Empty);
1943 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1945 -- Set return type of function, which is also global and does not need
1948 if Ekind (Subp) = E_Function then
1949 Set_Subtype_Mark (Specification (Body_To_Analyze),
1950 New_Occurrence_Of (Etype (Subp), Sloc (N)));
1953 if No (Declarations (N)) then
1954 Set_Declarations (N, New_List (Body_To_Analyze));
1956 Append (Body_To_Analyze, Declarations (N));
1959 Expander_Mode_Save_And_Set (False);
1962 Analyze (Body_To_Analyze);
1963 New_Scope (Defining_Entity (Body_To_Analyze));
1964 Save_Global_References (Original_Body);
1966 Remove (Body_To_Analyze);
1968 Expander_Mode_Restore;
1974 -- If secondary stk used there is no point in inlining. We have
1975 -- already issued the warning in this case, so nothing to do.
1977 if Uses_Secondary_Stack (Body_To_Analyze) then
1981 Set_Body_To_Inline (Decl, Original_Body);
1982 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
1983 Set_Is_Inlined (Subp);
1984 end Build_Body_To_Inline;
1990 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
1992 -- Do not emit warning if this is a predefined unit which is not
1993 -- the main unit. With validity checks enabled, some predefined
1994 -- subprograms may contain nested subprograms and become ineligible
1997 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1998 and then not In_Extended_Main_Source_Unit (Subp)
2002 elsif Is_Always_Inlined (Subp) then
2004 -- Remove last character (question mark) to make this into an error,
2005 -- because the Inline_Always pragma cannot be obeyed.
2007 Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2009 elsif Ineffective_Inline_Warnings then
2010 Error_Msg_NE (Msg, N, Subp);
2014 -----------------------
2015 -- Check_Conformance --
2016 -----------------------
2018 procedure Check_Conformance
2019 (New_Id : Entity_Id;
2021 Ctype : Conformance_Type;
2023 Conforms : out Boolean;
2024 Err_Loc : Node_Id := Empty;
2025 Get_Inst : Boolean := False)
2027 Old_Type : constant Entity_Id := Etype (Old_Id);
2028 New_Type : constant Entity_Id := Etype (New_Id);
2029 Old_Formal : Entity_Id;
2030 New_Formal : Entity_Id;
2032 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2033 -- Post error message for conformance error on given node. Two messages
2034 -- are output. The first points to the previous declaration with a
2035 -- general "no conformance" message. The second is the detailed reason,
2036 -- supplied as Msg. The parameter N provide information for a possible
2037 -- & insertion in the message, and also provides the location for
2038 -- posting the message in the absence of a specified Err_Loc location.
2040 -----------------------
2041 -- Conformance_Error --
2042 -----------------------
2044 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2051 if No (Err_Loc) then
2057 Error_Msg_Sloc := Sloc (Old_Id);
2060 when Type_Conformant =>
2062 ("not type conformant with declaration#!", Enode);
2064 when Mode_Conformant =>
2066 ("not mode conformant with declaration#!", Enode);
2068 when Subtype_Conformant =>
2070 ("not subtype conformant with declaration#!", Enode);
2072 when Fully_Conformant =>
2074 ("not fully conformant with declaration#!", Enode);
2077 Error_Msg_NE (Msg, Enode, N);
2079 end Conformance_Error;
2081 -- Start of processing for Check_Conformance
2086 -- We need a special case for operators, since they don't appear
2089 if Ctype = Type_Conformant then
2090 if Ekind (New_Id) = E_Operator
2091 and then Operator_Matches_Spec (New_Id, Old_Id)
2097 -- If both are functions/operators, check return types conform
2099 if Old_Type /= Standard_Void_Type
2100 and then New_Type /= Standard_Void_Type
2102 if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2103 Conformance_Error ("return type does not match!", New_Id);
2107 -- If either is a function/operator and the other isn't, error
2109 elsif Old_Type /= Standard_Void_Type
2110 or else New_Type /= Standard_Void_Type
2112 Conformance_Error ("functions can only match functions!", New_Id);
2116 -- In subtype conformant case, conventions must match (RM 6.3.1(16))
2117 -- If this is a renaming as body, refine error message to indicate that
2118 -- the conflict is with the original declaration. If the entity is not
2119 -- frozen, the conventions don't have to match, the one of the renamed
2120 -- entity is inherited.
2122 if Ctype >= Subtype_Conformant then
2123 if Convention (Old_Id) /= Convention (New_Id) then
2125 if not Is_Frozen (New_Id) then
2128 elsif Present (Err_Loc)
2129 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2130 and then Present (Corresponding_Spec (Err_Loc))
2132 Error_Msg_Name_1 := Chars (New_Id);
2134 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2136 Conformance_Error ("prior declaration for% has convention %!");
2139 Conformance_Error ("calling conventions do not match!");
2144 elsif Is_Formal_Subprogram (Old_Id)
2145 or else Is_Formal_Subprogram (New_Id)
2147 Conformance_Error ("formal subprograms not allowed!");
2152 -- Deal with parameters
2154 -- Note: we use the entity information, rather than going directly
2155 -- to the specification in the tree. This is not only simpler, but
2156 -- absolutely necessary for some cases of conformance tests between
2157 -- operators, where the declaration tree simply does not exist!
2159 Old_Formal := First_Formal (Old_Id);
2160 New_Formal := First_Formal (New_Id);
2162 while Present (Old_Formal) and then Present (New_Formal) loop
2163 if Ctype = Fully_Conformant then
2165 -- Names must match. Error message is more accurate if we do
2166 -- this before checking that the types of the formals match.
2168 if Chars (Old_Formal) /= Chars (New_Formal) then
2169 Conformance_Error ("name & does not match!", New_Formal);
2171 -- Set error posted flag on new formal as well to stop
2172 -- junk cascaded messages in some cases.
2174 Set_Error_Posted (New_Formal);
2179 -- Types must always match. In the visible part of an instance,
2180 -- usual overloading rules for dispatching operations apply, and
2181 -- we check base types (not the actual subtypes).
2183 if In_Instance_Visible_Part
2184 and then Is_Dispatching_Operation (New_Id)
2186 if not Conforming_Types
2187 (Base_Type (Etype (Old_Formal)),
2188 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2190 Conformance_Error ("type of & does not match!", New_Formal);
2194 elsif not Conforming_Types
2195 (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2197 Conformance_Error ("type of & does not match!", New_Formal);
2201 -- For mode conformance, mode must match
2203 if Ctype >= Mode_Conformant
2204 and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2206 Conformance_Error ("mode of & does not match!", New_Formal);
2210 -- Full conformance checks
2212 if Ctype = Fully_Conformant then
2214 -- We have checked already that names match. Check default
2215 -- expressions for in parameters
2217 if Parameter_Mode (Old_Formal) = E_In_Parameter then
2219 NewD : constant Boolean :=
2220 Present (Default_Value (New_Formal));
2221 OldD : constant Boolean :=
2222 Present (Default_Value (Old_Formal));
2224 if NewD or OldD then
2226 -- The old default value has been analyzed because the
2227 -- current full declaration will have frozen everything
2228 -- before. The new default values have not been
2229 -- analyzed, so analyze them now before we check for
2234 Analyze_Per_Use_Expression
2235 (Default_Value (New_Formal), Etype (New_Formal));
2239 if not (NewD and OldD)
2240 or else not Fully_Conformant_Expressions
2241 (Default_Value (Old_Formal),
2242 Default_Value (New_Formal))
2245 ("default expression for & does not match!",
2254 -- A couple of special checks for Ada 83 mode. These checks are
2255 -- skipped if either entity is an operator in package Standard.
2256 -- or if either old or new instance is not from the source program.
2258 if Ada_Version = Ada_83
2259 and then Sloc (Old_Id) > Standard_Location
2260 and then Sloc (New_Id) > Standard_Location
2261 and then Comes_From_Source (Old_Id)
2262 and then Comes_From_Source (New_Id)
2265 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2266 New_Param : constant Node_Id := Declaration_Node (New_Formal);
2269 -- Explicit IN must be present or absent in both cases. This
2270 -- test is required only in the full conformance case.
2272 if In_Present (Old_Param) /= In_Present (New_Param)
2273 and then Ctype = Fully_Conformant
2276 ("(Ada 83) IN must appear in both declarations",
2281 -- Grouping (use of comma in param lists) must be the same
2282 -- This is where we catch a misconformance like:
2285 -- A : Integer; B : Integer
2287 -- which are represented identically in the tree except
2288 -- for the setting of the flags More_Ids and Prev_Ids.
2290 if More_Ids (Old_Param) /= More_Ids (New_Param)
2291 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2294 ("grouping of & does not match!", New_Formal);
2300 Next_Formal (Old_Formal);
2301 Next_Formal (New_Formal);
2304 if Present (Old_Formal) then
2305 Conformance_Error ("too few parameters!");
2308 elsif Present (New_Formal) then
2309 Conformance_Error ("too many parameters!", New_Formal);
2312 end Check_Conformance;
2314 ------------------------------
2315 -- Check_Delayed_Subprogram --
2316 ------------------------------
2318 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2321 procedure Possible_Freeze (T : Entity_Id);
2322 -- T is the type of either a formal parameter or of the return type.
2323 -- If T is not yet frozen and needs a delayed freeze, then the
2324 -- subprogram itself must be delayed.
2326 ---------------------
2327 -- Possible_Freeze --
2328 ---------------------
2330 procedure Possible_Freeze (T : Entity_Id) is
2332 if Has_Delayed_Freeze (T)
2333 and then not Is_Frozen (T)
2335 Set_Has_Delayed_Freeze (Designator);
2337 elsif Is_Access_Type (T)
2338 and then Has_Delayed_Freeze (Designated_Type (T))
2339 and then not Is_Frozen (Designated_Type (T))
2341 Set_Has_Delayed_Freeze (Designator);
2343 end Possible_Freeze;
2345 -- Start of processing for Check_Delayed_Subprogram
2348 -- Never need to freeze abstract subprogram
2350 if Is_Abstract (Designator) then
2353 -- Need delayed freeze if return type itself needs a delayed
2354 -- freeze and is not yet frozen.
2356 Possible_Freeze (Etype (Designator));
2357 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2359 -- Need delayed freeze if any of the formal types themselves need
2360 -- a delayed freeze and are not yet frozen.
2362 F := First_Formal (Designator);
2363 while Present (F) loop
2364 Possible_Freeze (Etype (F));
2365 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2370 -- Mark functions that return by reference. Note that it cannot be
2371 -- done for delayed_freeze subprograms because the underlying
2372 -- returned type may not be known yet (for private types)
2374 if not Has_Delayed_Freeze (Designator)
2375 and then Expander_Active
2378 Typ : constant Entity_Id := Etype (Designator);
2379 Utyp : constant Entity_Id := Underlying_Type (Typ);
2382 if Is_Return_By_Reference_Type (Typ) then
2383 Set_Returns_By_Ref (Designator);
2385 elsif Present (Utyp) and then Controlled_Type (Utyp) then
2386 Set_Returns_By_Ref (Designator);
2390 end Check_Delayed_Subprogram;
2392 ------------------------------------
2393 -- Check_Discriminant_Conformance --
2394 ------------------------------------
2396 procedure Check_Discriminant_Conformance
2401 Old_Discr : Entity_Id := First_Discriminant (Prev);
2402 New_Discr : Node_Id := First (Discriminant_Specifications (N));
2403 New_Discr_Id : Entity_Id;
2404 New_Discr_Type : Entity_Id;
2406 procedure Conformance_Error (Msg : String; N : Node_Id);
2407 -- Post error message for conformance error on given node. Two messages
2408 -- are output. The first points to the previous declaration with a
2409 -- general "no conformance" message. The second is the detailed reason,
2410 -- supplied as Msg. The parameter N provide information for a possible
2411 -- & insertion in the message.
2413 -----------------------
2414 -- Conformance_Error --
2415 -----------------------
2417 procedure Conformance_Error (Msg : String; N : Node_Id) is
2419 Error_Msg_Sloc := Sloc (Prev_Loc);
2420 Error_Msg_N ("not fully conformant with declaration#!", N);
2421 Error_Msg_NE (Msg, N, N);
2422 end Conformance_Error;
2424 -- Start of processing for Check_Discriminant_Conformance
2427 while Present (Old_Discr) and then Present (New_Discr) loop
2429 New_Discr_Id := Defining_Identifier (New_Discr);
2431 -- The subtype mark of the discriminant on the full type has not
2432 -- been analyzed so we do it here. For an access discriminant a new
2435 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2437 Access_Definition (N, Discriminant_Type (New_Discr));
2440 Analyze (Discriminant_Type (New_Discr));
2441 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2444 if not Conforming_Types
2445 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2447 Conformance_Error ("type of & does not match!", New_Discr_Id);
2450 -- Treat the new discriminant as an occurrence of the old one,
2451 -- for navigation purposes, and fill in some semantic
2452 -- information, for completeness.
2454 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2455 Set_Etype (New_Discr_Id, Etype (Old_Discr));
2456 Set_Scope (New_Discr_Id, Scope (Old_Discr));
2461 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2462 Conformance_Error ("name & does not match!", New_Discr_Id);
2466 -- Default expressions must match
2469 NewD : constant Boolean :=
2470 Present (Expression (New_Discr));
2471 OldD : constant Boolean :=
2472 Present (Expression (Parent (Old_Discr)));
2475 if NewD or OldD then
2477 -- The old default value has been analyzed and expanded,
2478 -- because the current full declaration will have frozen
2479 -- everything before. The new default values have not been
2480 -- expanded, so expand now to check conformance.
2483 Analyze_Per_Use_Expression
2484 (Expression (New_Discr), New_Discr_Type);
2487 if not (NewD and OldD)
2488 or else not Fully_Conformant_Expressions
2489 (Expression (Parent (Old_Discr)),
2490 Expression (New_Discr))
2494 ("default expression for & does not match!",
2501 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2503 if Ada_Version = Ada_83 then
2505 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2508 -- Grouping (use of comma in param lists) must be the same
2509 -- This is where we catch a misconformance like:
2512 -- A : Integer; B : Integer
2514 -- which are represented identically in the tree except
2515 -- for the setting of the flags More_Ids and Prev_Ids.
2517 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2518 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2521 ("grouping of & does not match!", New_Discr_Id);
2527 Next_Discriminant (Old_Discr);
2531 if Present (Old_Discr) then
2532 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2535 elsif Present (New_Discr) then
2537 ("too many discriminants!", Defining_Identifier (New_Discr));
2540 end Check_Discriminant_Conformance;
2542 ----------------------------
2543 -- Check_Fully_Conformant --
2544 ----------------------------
2546 procedure Check_Fully_Conformant
2547 (New_Id : Entity_Id;
2549 Err_Loc : Node_Id := Empty)
2554 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2555 end Check_Fully_Conformant;
2557 ---------------------------
2558 -- Check_Mode_Conformant --
2559 ---------------------------
2561 procedure Check_Mode_Conformant
2562 (New_Id : Entity_Id;
2564 Err_Loc : Node_Id := Empty;
2565 Get_Inst : Boolean := False)
2571 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2572 end Check_Mode_Conformant;
2574 --------------------------------
2575 -- Check_Overriding_Operation --
2576 --------------------------------
2578 procedure Check_Overriding_Operation
2584 Has_Pragma : Boolean := False;
2587 -- See whether there is an overriding pragma immediately following
2588 -- the declaration. Intervening pragmas, such as Inline, are allowed.
2591 while Present (Decl)
2592 and then Nkind (Decl) = N_Pragma
2594 if Chars (Decl) = Name_Overriding
2595 or else Chars (Decl) = Name_Optional_Overriding
2597 -- For now disable the use of these pragmas, until the ARG
2598 -- finalizes the design of this feature.
2600 Error_Msg_N ("?unrecognized pragma", Decl);
2602 if not Is_Overriding_Operation (Subp) then
2604 -- Before emitting an error message, check whether this
2605 -- may override an operation that is not yet visible, as
2606 -- in the case of a derivation of a private operation in
2607 -- a child unit. Such an operation is introduced with a
2608 -- different name, but its alias is the parent operation.
2614 E := First_Entity (Current_Scope);
2616 while Present (E) loop
2617 if Ekind (E) = Ekind (Subp)
2618 and then not Comes_From_Source (E)
2619 and then Present (Alias (E))
2620 and then Chars (Alias (E)) = Chars (Subp)
2621 and then In_Open_Scopes (Scope (Alias (E)))
2631 ("& must override an inherited operation",
2637 -- Verify syntax of pragma
2639 Arg1 := First (Pragma_Argument_Associations (Decl));
2641 if Present (Arg1) then
2642 if not Is_Entity_Name (Expression (Arg1)) then
2643 Error_Msg_N ("pragma applies to local subprogram", Decl);
2645 elsif Chars (Expression (Arg1)) /= Chars (Subp) then
2647 ("pragma must apply to preceding subprogram", Decl);
2649 elsif Present (Next (Arg1)) then
2650 Error_Msg_N ("illegal pragma format", Decl);
2654 Set_Analyzed (Decl);
2663 and then Explicit_Overriding
2664 and then Is_Overriding_Operation (Subp)
2666 Error_Msg_NE ("Missing overriding pragma for&", Subp, Subp);
2668 end Check_Overriding_Operation;
2674 procedure Check_Returns
2681 procedure Check_Statement_Sequence (L : List_Id);
2682 -- Internal recursive procedure to check a list of statements for proper
2683 -- termination by a return statement (or a transfer of control or a
2684 -- compound statement that is itself internally properly terminated).
2686 ------------------------------
2687 -- Check_Statement_Sequence --
2688 ------------------------------
2690 procedure Check_Statement_Sequence (L : List_Id) is
2694 Raise_Exception_Call : Boolean;
2695 -- Set True if statement sequence terminated by Raise_Exception call
2696 -- or a Reraise_Occurrence call.
2699 Raise_Exception_Call := False;
2701 -- Get last real statement
2703 Last_Stm := Last (L);
2705 -- Don't count pragmas
2707 while Nkind (Last_Stm) = N_Pragma
2709 -- Don't count call to SS_Release (can happen after Raise_Exception)
2712 (Nkind (Last_Stm) = N_Procedure_Call_Statement
2714 Nkind (Name (Last_Stm)) = N_Identifier
2716 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2718 -- Don't count exception junk
2721 ((Nkind (Last_Stm) = N_Goto_Statement
2722 or else Nkind (Last_Stm) = N_Label
2723 or else Nkind (Last_Stm) = N_Object_Declaration)
2724 and then Exception_Junk (Last_Stm))
2729 -- Here we have the "real" last statement
2731 Kind := Nkind (Last_Stm);
2733 -- Transfer of control, OK. Note that in the No_Return procedure
2734 -- case, we already diagnosed any explicit return statements, so
2735 -- we can treat them as OK in this context.
2737 if Is_Transfer (Last_Stm) then
2740 -- Check cases of explicit non-indirect procedure calls
2742 elsif Kind = N_Procedure_Call_Statement
2743 and then Is_Entity_Name (Name (Last_Stm))
2745 -- Check call to Raise_Exception procedure which is treated
2746 -- specially, as is a call to Reraise_Occurrence.
2748 -- We suppress the warning in these cases since it is likely that
2749 -- the programmer really does not expect to deal with the case
2750 -- of Null_Occurrence, and thus would find a warning about a
2751 -- missing return curious, and raising Program_Error does not
2752 -- seem such a bad behavior if this does occur.
2754 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2756 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2758 Raise_Exception_Call := True;
2760 -- For Raise_Exception call, test first argument, if it is
2761 -- an attribute reference for a 'Identity call, then we know
2762 -- that the call cannot possibly return.
2765 Arg : constant Node_Id :=
2766 Original_Node (First_Actual (Last_Stm));
2769 if Nkind (Arg) = N_Attribute_Reference
2770 and then Attribute_Name (Arg) = Name_Identity
2777 -- If statement, need to look inside if there is an else and check
2778 -- each constituent statement sequence for proper termination.
2780 elsif Kind = N_If_Statement
2781 and then Present (Else_Statements (Last_Stm))
2783 Check_Statement_Sequence (Then_Statements (Last_Stm));
2784 Check_Statement_Sequence (Else_Statements (Last_Stm));
2786 if Present (Elsif_Parts (Last_Stm)) then
2788 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2791 while Present (Elsif_Part) loop
2792 Check_Statement_Sequence (Then_Statements (Elsif_Part));
2800 -- Case statement, check each case for proper termination
2802 elsif Kind = N_Case_Statement then
2807 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2808 while Present (Case_Alt) loop
2809 Check_Statement_Sequence (Statements (Case_Alt));
2810 Next_Non_Pragma (Case_Alt);
2816 -- Block statement, check its handled sequence of statements
2818 elsif Kind = N_Block_Statement then
2824 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2833 -- Loop statement. If there is an iteration scheme, we can definitely
2834 -- fall out of the loop. Similarly if there is an exit statement, we
2835 -- can fall out. In either case we need a following return.
2837 elsif Kind = N_Loop_Statement then
2838 if Present (Iteration_Scheme (Last_Stm))
2839 or else Has_Exit (Entity (Identifier (Last_Stm)))
2843 -- A loop with no exit statement or iteration scheme if either
2844 -- an inifite loop, or it has some other exit (raise/return).
2845 -- In either case, no warning is required.
2851 -- Timed entry call, check entry call and delay alternatives
2853 -- Note: in expanded code, the timed entry call has been converted
2854 -- to a set of expanded statements on which the check will work
2855 -- correctly in any case.
2857 elsif Kind = N_Timed_Entry_Call then
2859 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2860 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
2863 -- If statement sequence of entry call alternative is missing,
2864 -- then we can definitely fall through, and we post the error
2865 -- message on the entry call alternative itself.
2867 if No (Statements (ECA)) then
2870 -- If statement sequence of delay alternative is missing, then
2871 -- we can definitely fall through, and we post the error
2872 -- message on the delay alternative itself.
2874 -- Note: if both ECA and DCA are missing the return, then we
2875 -- post only one message, should be enough to fix the bugs.
2876 -- If not we will get a message next time on the DCA when the
2879 elsif No (Statements (DCA)) then
2882 -- Else check both statement sequences
2885 Check_Statement_Sequence (Statements (ECA));
2886 Check_Statement_Sequence (Statements (DCA));
2891 -- Conditional entry call, check entry call and else part
2893 -- Note: in expanded code, the conditional entry call has been
2894 -- converted to a set of expanded statements on which the check
2895 -- will work correctly in any case.
2897 elsif Kind = N_Conditional_Entry_Call then
2899 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2902 -- If statement sequence of entry call alternative is missing,
2903 -- then we can definitely fall through, and we post the error
2904 -- message on the entry call alternative itself.
2906 if No (Statements (ECA)) then
2909 -- Else check statement sequence and else part
2912 Check_Statement_Sequence (Statements (ECA));
2913 Check_Statement_Sequence (Else_Statements (Last_Stm));
2919 -- If we fall through, issue appropriate message
2923 if not Raise_Exception_Call then
2925 ("?RETURN statement missing following this statement!",
2928 ("\?Program_Error may be raised at run time",
2932 -- Note: we set Err even though we have not issued a warning
2933 -- because we still have a case of a missing return. This is
2934 -- an extremely marginal case, probably will never be noticed
2935 -- but we might as well get it right.
2941 ("implied return after this statement not allowed (No_Return)",
2944 end Check_Statement_Sequence;
2946 -- Start of processing for Check_Returns
2950 Check_Statement_Sequence (Statements (HSS));
2952 if Present (Exception_Handlers (HSS)) then
2953 Handler := First_Non_Pragma (Exception_Handlers (HSS));
2954 while Present (Handler) loop
2955 Check_Statement_Sequence (Statements (Handler));
2956 Next_Non_Pragma (Handler);
2961 ----------------------------
2962 -- Check_Subprogram_Order --
2963 ----------------------------
2965 procedure Check_Subprogram_Order (N : Node_Id) is
2967 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2968 -- This is used to check if S1 > S2 in the sense required by this
2969 -- test, for example nameab < namec, but name2 < name10.
2971 -----------------------------
2972 -- Subprogram_Name_Greater --
2973 -----------------------------
2975 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2980 -- Remove trailing numeric parts
2983 while S1 (L1) in '0' .. '9' loop
2988 while S2 (L2) in '0' .. '9' loop
2992 -- If non-numeric parts non-equal, that's decisive
2994 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2997 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3000 -- If non-numeric parts equal, compare suffixed numeric parts. Note
3001 -- that a missing suffix is treated as numeric zero in this test.
3005 while L1 < S1'Last loop
3007 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3011 while L2 < S2'Last loop
3013 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3018 end Subprogram_Name_Greater;
3020 -- Start of processing for Check_Subprogram_Order
3023 -- Check body in alpha order if this is option
3026 and then Style_Check_Order_Subprograms
3027 and then Nkind (N) = N_Subprogram_Body
3028 and then Comes_From_Source (N)
3029 and then In_Extended_Main_Source_Unit (N)
3033 renames Scope_Stack.Table
3034 (Scope_Stack.Last).Last_Subprogram_Name;
3036 Body_Id : constant Entity_Id :=
3037 Defining_Entity (Specification (N));
3040 Get_Decoded_Name_String (Chars (Body_Id));
3043 if Subprogram_Name_Greater
3044 (LSN.all, Name_Buffer (1 .. Name_Len))
3046 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3052 LSN := new String'(Name_Buffer (1 .. Name_Len));
3055 end Check_Subprogram_Order;
3057 ------------------------------
3058 -- Check_Subtype_Conformant --
3059 ------------------------------
3061 procedure Check_Subtype_Conformant
3062 (New_Id : Entity_Id;
3064 Err_Loc : Node_Id := Empty)
3069 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3070 end Check_Subtype_Conformant;
3072 ---------------------------
3073 -- Check_Type_Conformant --
3074 ---------------------------
3076 procedure Check_Type_Conformant
3077 (New_Id : Entity_Id;
3079 Err_Loc : Node_Id := Empty)
3084 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3085 end Check_Type_Conformant;
3087 ----------------------
3088 -- Conforming_Types --
3089 ----------------------
3091 function Conforming_Types
3094 Ctype : Conformance_Type;
3095 Get_Inst : Boolean := False) return Boolean
3097 Type_1 : Entity_Id := T1;
3098 Type_2 : Entity_Id := T2;
3099 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3101 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3102 -- If neither T1 nor T2 are generic actual types, or if they are
3103 -- in different scopes (e.g. parent and child instances), then verify
3104 -- that the base types are equal. Otherwise T1 and T2 must be
3105 -- on the same subtype chain. The whole purpose of this procedure
3106 -- is to prevent spurious ambiguities in an instantiation that may
3107 -- arise if two distinct generic types are instantiated with the
3110 ----------------------
3111 -- Base_Types_Match --
3112 ----------------------
3114 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3119 elsif Base_Type (T1) = Base_Type (T2) then
3121 -- The following is too permissive. A more precise test must
3122 -- check that the generic actual is an ancestor subtype of the
3125 return not Is_Generic_Actual_Type (T1)
3126 or else not Is_Generic_Actual_Type (T2)
3127 or else Scope (T1) /= Scope (T2);
3129 -- In some cases a type imported through a limited_with clause,
3130 -- and its non-limited view are both visible, for example in an
3131 -- anonymous access_to_classwide type in a formal. Both entities
3132 -- designate the same type.
3134 elsif From_With_Type (T1)
3135 and then Ekind (T1) = E_Incomplete_Type
3136 and then T2 = Non_Limited_View (T1)
3143 end Base_Types_Match;
3146 -- The context is an instance association for a formal
3147 -- access-to-subprogram type; the formal parameter types require
3148 -- mapping because they may denote other formal parameters of the
3152 Type_1 := Get_Instance_Of (T1);
3153 Type_2 := Get_Instance_Of (T2);
3156 -- First see if base types match
3158 if Base_Types_Match (Type_1, Type_2) then
3159 return Ctype <= Mode_Conformant
3160 or else Subtypes_Statically_Match (Type_1, Type_2);
3162 elsif Is_Incomplete_Or_Private_Type (Type_1)
3163 and then Present (Full_View (Type_1))
3164 and then Base_Types_Match (Full_View (Type_1), Type_2)
3166 return Ctype <= Mode_Conformant
3167 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3169 elsif Ekind (Type_2) = E_Incomplete_Type
3170 and then Present (Full_View (Type_2))
3171 and then Base_Types_Match (Type_1, Full_View (Type_2))
3173 return Ctype <= Mode_Conformant
3174 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3176 elsif Is_Private_Type (Type_2)
3177 and then In_Instance
3178 and then Present (Full_View (Type_2))
3179 and then Base_Types_Match (Type_1, Full_View (Type_2))
3181 return Ctype <= Mode_Conformant
3182 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3185 -- Ada 2005 (AI-254): Detect anonymous access to subprogram types
3187 Are_Anonymous_Access_To_Subprogram_Types :=
3189 -- Case 1: Anonymous access to subprogram types
3191 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3192 and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3194 -- Case 2: Anonymous access to PROTECTED subprogram types. In this
3195 -- case the anonymous type_declaration has been replaced by an
3196 -- occurrence of an internal access to subprogram type declaration
3197 -- available through the Original_Access_Type attribute
3200 (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3201 and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3202 and then not Comes_From_Source (Type_1)
3203 and then not Comes_From_Source (Type_2)
3204 and then Present (Original_Access_Type (Type_1))
3205 and then Present (Original_Access_Type (Type_2))
3206 and then Ekind (Original_Access_Type (Type_1)) =
3207 E_Anonymous_Access_Protected_Subprogram_Type
3208 and then Ekind (Original_Access_Type (Type_2)) =
3209 E_Anonymous_Access_Protected_Subprogram_Type);
3211 -- Test anonymous access type case. For this case, static subtype
3212 -- matching is required for mode conformance (RM 6.3.1(15))
3214 if (Ekind (Type_1) = E_Anonymous_Access_Type
3215 and then Ekind (Type_2) = E_Anonymous_Access_Type)
3216 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3219 Desig_1 : Entity_Id;
3220 Desig_2 : Entity_Id;
3223 Desig_1 := Directly_Designated_Type (Type_1);
3225 -- An access parameter can designate an incomplete type
3227 if Ekind (Desig_1) = E_Incomplete_Type
3228 and then Present (Full_View (Desig_1))
3230 Desig_1 := Full_View (Desig_1);
3233 Desig_2 := Directly_Designated_Type (Type_2);
3235 if Ekind (Desig_2) = E_Incomplete_Type
3236 and then Present (Full_View (Desig_2))
3238 Desig_2 := Full_View (Desig_2);
3241 -- The context is an instance association for a formal
3242 -- access-to-subprogram type; formal access parameter designated
3243 -- types require mapping because they may denote other formal
3244 -- parameters of the generic unit.
3247 Desig_1 := Get_Instance_Of (Desig_1);
3248 Desig_2 := Get_Instance_Of (Desig_2);
3251 -- It is possible for a Class_Wide_Type to be introduced for an
3252 -- incomplete type, in which case there is a separate class_ wide
3253 -- type for the full view. The types conform if their Etypes
3254 -- conform, i.e. one may be the full view of the other. This can
3255 -- only happen in the context of an access parameter, other uses
3256 -- of an incomplete Class_Wide_Type are illegal.
3258 if Is_Class_Wide_Type (Desig_1)
3259 and then Is_Class_Wide_Type (Desig_2)
3263 (Etype (Base_Type (Desig_1)),
3264 Etype (Base_Type (Desig_2)), Ctype);
3266 elsif Are_Anonymous_Access_To_Subprogram_Types then
3267 return Ctype = Type_Conformant
3269 Subtypes_Statically_Match (Desig_1, Desig_2);
3272 return Base_Type (Desig_1) = Base_Type (Desig_2)
3273 and then (Ctype = Type_Conformant
3275 Subtypes_Statically_Match (Desig_1, Desig_2));
3279 -- Otherwise definitely no match
3284 end Conforming_Types;
3286 --------------------------
3287 -- Create_Extra_Formals --
3288 --------------------------
3290 procedure Create_Extra_Formals (E : Entity_Id) is
3292 Last_Extra : Entity_Id;
3293 Formal_Type : Entity_Id;
3294 P_Formal : Entity_Id := Empty;
3296 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3297 -- Add an extra formal, associated with the current Formal. The extra
3298 -- formal is added to the list of extra formals, and also returned as
3299 -- the result. These formals are always of mode IN.
3301 ----------------------
3302 -- Add_Extra_Formal --
3303 ----------------------
3305 function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3306 EF : constant Entity_Id :=
3307 Make_Defining_Identifier (Sloc (Formal),
3308 Chars => New_External_Name (Chars (Formal), 'F'));
3311 -- We never generate extra formals if expansion is not active
3312 -- because we don't need them unless we are generating code.
3314 if not Expander_Active then
3318 -- A little optimization. Never generate an extra formal for the
3319 -- _init operand of an initialization procedure, since it could
3322 if Chars (Formal) = Name_uInit then
3326 Set_Ekind (EF, E_In_Parameter);
3327 Set_Actual_Subtype (EF, Typ);
3328 Set_Etype (EF, Typ);
3329 Set_Scope (EF, Scope (Formal));
3330 Set_Mechanism (EF, Default_Mechanism);
3331 Set_Formal_Validity (EF);
3333 Set_Extra_Formal (Last_Extra, EF);
3336 end Add_Extra_Formal;
3338 -- Start of processing for Create_Extra_Formals
3341 -- If this is a derived subprogram then the subtypes of the parent
3342 -- subprogram's formal parameters will be used to to determine the need
3343 -- for extra formals.
3345 if Is_Overloadable (E) and then Present (Alias (E)) then
3346 P_Formal := First_Formal (Alias (E));
3349 Last_Extra := Empty;
3350 Formal := First_Formal (E);
3351 while Present (Formal) loop
3352 Last_Extra := Formal;
3353 Next_Formal (Formal);
3356 -- If Extra_formals where already created, don't do it again. This
3357 -- situation may arise for subprogram types created as part of
3358 -- dispatching calls (see Expand_Dispatching_Call)
3360 if Present (Last_Extra) and then
3361 Present (Extra_Formal (Last_Extra))
3366 Formal := First_Formal (E);
3368 while Present (Formal) loop
3370 -- Create extra formal for supporting the attribute 'Constrained.
3371 -- The case of a private type view without discriminants also
3372 -- requires the extra formal if the underlying type has defaulted
3375 if Ekind (Formal) /= E_In_Parameter then
3376 if Present (P_Formal) then
3377 Formal_Type := Etype (P_Formal);
3379 Formal_Type := Etype (Formal);
3382 -- Do not produce extra formals for Unchecked_Union parameters.
3383 -- Jump directly to the end of the loop.
3385 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3386 goto Skip_Extra_Formal_Generation;
3389 if not Has_Discriminants (Formal_Type)
3390 and then Ekind (Formal_Type) in Private_Kind
3391 and then Present (Underlying_Type (Formal_Type))
3393 Formal_Type := Underlying_Type (Formal_Type);
3396 if Has_Discriminants (Formal_Type)
3398 ((not Is_Constrained (Formal_Type)
3399 and then not Is_Indefinite_Subtype (Formal_Type))
3400 or else Present (Extra_Formal (Formal)))
3402 Set_Extra_Constrained
3403 (Formal, Add_Extra_Formal (Standard_Boolean));
3407 -- Create extra formal for supporting accessibility checking
3409 -- This is suppressed if we specifically suppress accessibility
3410 -- checks at the pacage level for either the subprogram, or the
3411 -- package in which it resides. However, we do not suppress it
3412 -- simply if the scope has accessibility checks suppressed, since
3413 -- this could cause trouble when clients are compiled with a
3414 -- different suppression setting. The explicit checks at the
3415 -- package level are safe from this point of view.
3417 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3419 (Explicit_Suppress (E, Accessibility_Check)
3421 Explicit_Suppress (Scope (E), Accessibility_Check))
3423 (not Present (P_Formal)
3424 or else Present (Extra_Accessibility (P_Formal)))
3426 -- Temporary kludge: for now we avoid creating the extra formal
3427 -- for access parameters of protected operations because of
3428 -- problem with the case of internal protected calls. ???
3430 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3431 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3433 Set_Extra_Accessibility
3434 (Formal, Add_Extra_Formal (Standard_Natural));
3438 if Present (P_Formal) then
3439 Next_Formal (P_Formal);
3442 -- This label is required when skipping extra formal generation for
3443 -- Unchecked_Union parameters.
3445 <<Skip_Extra_Formal_Generation>>
3447 Next_Formal (Formal);
3449 end Create_Extra_Formals;
3451 -----------------------------
3452 -- Enter_Overloaded_Entity --
3453 -----------------------------
3455 procedure Enter_Overloaded_Entity (S : Entity_Id) is
3456 E : Entity_Id := Current_Entity_In_Scope (S);
3457 C_E : Entity_Id := Current_Entity (S);
3461 Set_Has_Homonym (E);
3462 Set_Has_Homonym (S);
3465 Set_Is_Immediately_Visible (S);
3466 Set_Scope (S, Current_Scope);
3468 -- Chain new entity if front of homonym in current scope, so that
3469 -- homonyms are contiguous.
3474 while Homonym (C_E) /= E loop
3475 C_E := Homonym (C_E);
3478 Set_Homonym (C_E, S);
3482 Set_Current_Entity (S);
3487 Append_Entity (S, Current_Scope);
3488 Set_Public_Status (S);
3490 if Debug_Flag_E then
3491 Write_Str ("New overloaded entity chain: ");
3492 Write_Name (Chars (S));
3495 while Present (E) loop
3496 Write_Str (" "); Write_Int (Int (E));
3503 -- Generate warning for hiding
3506 and then Comes_From_Source (S)
3507 and then In_Extended_Main_Source_Unit (S)
3514 -- Warn unless genuine overloading
3516 if (not Is_Overloadable (E))
3517 or else Subtype_Conformant (E, S)
3519 Error_Msg_Sloc := Sloc (E);
3520 Error_Msg_N ("declaration of & hides one#?", S);
3524 end Enter_Overloaded_Entity;
3526 -----------------------------
3527 -- Find_Corresponding_Spec --
3528 -----------------------------
3530 function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3531 Spec : constant Node_Id := Specification (N);
3532 Designator : constant Entity_Id := Defining_Entity (Spec);
3537 E := Current_Entity (Designator);
3539 while Present (E) loop
3541 -- We are looking for a matching spec. It must have the same scope,
3542 -- and the same name, and either be type conformant, or be the case
3543 -- of a library procedure spec and its body (which belong to one
3544 -- another regardless of whether they are type conformant or not).
3546 if Scope (E) = Current_Scope then
3547 if Current_Scope = Standard_Standard
3548 or else (Ekind (E) = Ekind (Designator)
3549 and then Type_Conformant (E, Designator))
3551 -- Within an instantiation, we know that spec and body are
3552 -- subtype conformant, because they were subtype conformant
3553 -- in the generic. We choose the subtype-conformant entity
3554 -- here as well, to resolve spurious ambiguities in the
3555 -- instance that were not present in the generic (i.e. when
3556 -- two different types are given the same actual). If we are
3557 -- looking for a spec to match a body, full conformance is
3561 Set_Convention (Designator, Convention (E));
3563 if Nkind (N) = N_Subprogram_Body
3564 and then Present (Homonym (E))
3565 and then not Fully_Conformant (E, Designator)
3569 elsif not Subtype_Conformant (E, Designator) then
3574 if not Has_Completion (E) then
3576 if Nkind (N) /= N_Subprogram_Body_Stub then
3577 Set_Corresponding_Spec (N, E);
3580 Set_Has_Completion (E);
3583 elsif Nkind (Parent (N)) = N_Subunit then
3585 -- If this is the proper body of a subunit, the completion
3586 -- flag is set when analyzing the stub.
3590 -- If body already exists, this is an error unless the
3591 -- previous declaration is the implicit declaration of
3592 -- a derived subprogram, or this is a spurious overloading
3595 elsif No (Alias (E))
3596 and then not Is_Intrinsic_Subprogram (E)
3597 and then not In_Instance
3599 Error_Msg_Sloc := Sloc (E);
3600 if Is_Imported (E) then
3602 ("body not allowed for imported subprogram & declared#",
3605 Error_Msg_NE ("duplicate body for & declared#", N, E);
3609 elsif Is_Child_Unit (E)
3611 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3613 Nkind (Parent (Unit_Declaration_Node (Designator)))
3614 = N_Compilation_Unit
3617 -- Child units cannot be overloaded, so a conformance mismatch
3618 -- between body and a previous spec is an error.
3621 ("body of child unit does not match previous declaration", N);
3629 -- On exit, we know that no previous declaration of subprogram exists
3632 end Find_Corresponding_Spec;
3634 ----------------------
3635 -- Fully_Conformant --
3636 ----------------------
3638 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3641 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3643 end Fully_Conformant;
3645 ----------------------------------
3646 -- Fully_Conformant_Expressions --
3647 ----------------------------------
3649 function Fully_Conformant_Expressions
3650 (Given_E1 : Node_Id;
3651 Given_E2 : Node_Id) return Boolean
3653 E1 : constant Node_Id := Original_Node (Given_E1);
3654 E2 : constant Node_Id := Original_Node (Given_E2);
3655 -- We always test conformance on original nodes, since it is possible
3656 -- for analysis and/or expansion to make things look as though they
3657 -- conform when they do not, e.g. by converting 1+2 into 3.
3659 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3660 renames Fully_Conformant_Expressions;
3662 function FCL (L1, L2 : List_Id) return Boolean;
3663 -- Compare elements of two lists for conformance. Elements have to
3664 -- be conformant, and actuals inserted as default parameters do not
3665 -- match explicit actuals with the same value.
3667 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3668 -- Compare an operator node with a function call
3674 function FCL (L1, L2 : List_Id) return Boolean is
3678 if L1 = No_List then
3684 if L2 = No_List then
3690 -- Compare two lists, skipping rewrite insertions (we want to
3691 -- compare the original trees, not the expanded versions!)
3694 if Is_Rewrite_Insertion (N1) then
3696 elsif Is_Rewrite_Insertion (N2) then
3702 elsif not FCE (N1, N2) then
3715 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3716 Actuals : constant List_Id := Parameter_Associations (Call_Node);
3721 or else Entity (Op_Node) /= Entity (Name (Call_Node))
3726 Act := First (Actuals);
3728 if Nkind (Op_Node) in N_Binary_Op then
3730 if not FCE (Left_Opnd (Op_Node), Act) then
3737 return Present (Act)
3738 and then FCE (Right_Opnd (Op_Node), Act)
3739 and then No (Next (Act));
3743 -- Start of processing for Fully_Conformant_Expressions
3746 -- Non-conformant if paren count does not match. Note: if some idiot
3747 -- complains that we don't do this right for more than 3 levels of
3748 -- parentheses, they will be treated with the respect they deserve :-)
3750 if Paren_Count (E1) /= Paren_Count (E2) then
3753 -- If same entities are referenced, then they are conformant even if
3754 -- they have different forms (RM 8.3.1(19-20)).
3756 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3757 if Present (Entity (E1)) then
3758 return Entity (E1) = Entity (E2)
3759 or else (Chars (Entity (E1)) = Chars (Entity (E2))
3760 and then Ekind (Entity (E1)) = E_Discriminant
3761 and then Ekind (Entity (E2)) = E_In_Parameter);
3763 elsif Nkind (E1) = N_Expanded_Name
3764 and then Nkind (E2) = N_Expanded_Name
3765 and then Nkind (Selector_Name (E1)) = N_Character_Literal
3766 and then Nkind (Selector_Name (E2)) = N_Character_Literal
3768 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3771 -- Identifiers in component associations don't always have
3772 -- entities, but their names must conform.
3774 return Nkind (E1) = N_Identifier
3775 and then Nkind (E2) = N_Identifier
3776 and then Chars (E1) = Chars (E2);
3779 elsif Nkind (E1) = N_Character_Literal
3780 and then Nkind (E2) = N_Expanded_Name
3782 return Nkind (Selector_Name (E2)) = N_Character_Literal
3783 and then Chars (E1) = Chars (Selector_Name (E2));
3785 elsif Nkind (E2) = N_Character_Literal
3786 and then Nkind (E1) = N_Expanded_Name
3788 return Nkind (Selector_Name (E1)) = N_Character_Literal
3789 and then Chars (E2) = Chars (Selector_Name (E1));
3791 elsif Nkind (E1) in N_Op
3792 and then Nkind (E2) = N_Function_Call
3794 return FCO (E1, E2);
3796 elsif Nkind (E2) in N_Op
3797 and then Nkind (E1) = N_Function_Call
3799 return FCO (E2, E1);
3801 -- Otherwise we must have the same syntactic entity
3803 elsif Nkind (E1) /= Nkind (E2) then
3806 -- At this point, we specialize by node type
3813 FCL (Expressions (E1), Expressions (E2))
3814 and then FCL (Component_Associations (E1),
3815 Component_Associations (E2));
3818 if Nkind (Expression (E1)) = N_Qualified_Expression
3820 Nkind (Expression (E2)) = N_Qualified_Expression
3822 return FCE (Expression (E1), Expression (E2));
3824 -- Check that the subtype marks and any constraints
3829 Indic1 : constant Node_Id := Expression (E1);
3830 Indic2 : constant Node_Id := Expression (E2);
3835 if Nkind (Indic1) /= N_Subtype_Indication then
3837 Nkind (Indic2) /= N_Subtype_Indication
3838 and then Entity (Indic1) = Entity (Indic2);
3840 elsif Nkind (Indic2) /= N_Subtype_Indication then
3842 Nkind (Indic1) /= N_Subtype_Indication
3843 and then Entity (Indic1) = Entity (Indic2);
3846 if Entity (Subtype_Mark (Indic1)) /=
3847 Entity (Subtype_Mark (Indic2))
3852 Elt1 := First (Constraints (Constraint (Indic1)));
3853 Elt2 := First (Constraints (Constraint (Indic2)));
3855 while Present (Elt1) and then Present (Elt2) loop
3856 if not FCE (Elt1, Elt2) then
3869 when N_Attribute_Reference =>
3871 Attribute_Name (E1) = Attribute_Name (E2)
3872 and then FCL (Expressions (E1), Expressions (E2));
3876 Entity (E1) = Entity (E2)
3877 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
3878 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3880 when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3882 FCE (Left_Opnd (E1), Left_Opnd (E2))
3884 FCE (Right_Opnd (E1), Right_Opnd (E2));
3886 when N_Character_Literal =>
3888 Char_Literal_Value (E1) = Char_Literal_Value (E2);
3890 when N_Component_Association =>
3892 FCL (Choices (E1), Choices (E2))
3893 and then FCE (Expression (E1), Expression (E2));
3895 when N_Conditional_Expression =>
3897 FCL (Expressions (E1), Expressions (E2));
3899 when N_Explicit_Dereference =>
3901 FCE (Prefix (E1), Prefix (E2));
3903 when N_Extension_Aggregate =>
3905 FCL (Expressions (E1), Expressions (E2))
3906 and then Null_Record_Present (E1) =
3907 Null_Record_Present (E2)
3908 and then FCL (Component_Associations (E1),
3909 Component_Associations (E2));
3911 when N_Function_Call =>
3913 FCE (Name (E1), Name (E2))
3914 and then FCL (Parameter_Associations (E1),
3915 Parameter_Associations (E2));
3917 when N_Indexed_Component =>
3919 FCE (Prefix (E1), Prefix (E2))
3920 and then FCL (Expressions (E1), Expressions (E2));
3922 when N_Integer_Literal =>
3923 return (Intval (E1) = Intval (E2));
3928 when N_Operator_Symbol =>
3930 Chars (E1) = Chars (E2);
3932 when N_Others_Choice =>
3935 when N_Parameter_Association =>
3937 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
3938 and then FCE (Explicit_Actual_Parameter (E1),
3939 Explicit_Actual_Parameter (E2));
3941 when N_Qualified_Expression =>
3943 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3944 and then FCE (Expression (E1), Expression (E2));
3948 FCE (Low_Bound (E1), Low_Bound (E2))
3949 and then FCE (High_Bound (E1), High_Bound (E2));
3951 when N_Real_Literal =>
3952 return (Realval (E1) = Realval (E2));
3954 when N_Selected_Component =>
3956 FCE (Prefix (E1), Prefix (E2))
3957 and then FCE (Selector_Name (E1), Selector_Name (E2));
3961 FCE (Prefix (E1), Prefix (E2))
3962 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3964 when N_String_Literal =>
3966 S1 : constant String_Id := Strval (E1);
3967 S2 : constant String_Id := Strval (E2);
3968 L1 : constant Nat := String_Length (S1);
3969 L2 : constant Nat := String_Length (S2);
3976 for J in 1 .. L1 loop
3977 if Get_String_Char (S1, J) /=
3978 Get_String_Char (S2, J)
3988 when N_Type_Conversion =>
3990 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3991 and then FCE (Expression (E1), Expression (E2));
3995 Entity (E1) = Entity (E2)
3996 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3998 when N_Unchecked_Type_Conversion =>
4000 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4001 and then FCE (Expression (E1), Expression (E2));
4003 -- All other node types cannot appear in this context. Strictly
4004 -- we should raise a fatal internal error. Instead we just ignore
4005 -- the nodes. This means that if anyone makes a mistake in the
4006 -- expander and mucks an expression tree irretrievably, the
4007 -- result will be a failure to detect a (probably very obscure)
4008 -- case of non-conformance, which is better than bombing on some
4009 -- case where two expressions do in fact conform.
4016 end Fully_Conformant_Expressions;
4018 ----------------------------------------
4019 -- Fully_Conformant_Discrete_Subtypes --
4020 ----------------------------------------
4022 function Fully_Conformant_Discrete_Subtypes
4023 (Given_S1 : Node_Id;
4024 Given_S2 : Node_Id) return Boolean
4026 S1 : constant Node_Id := Original_Node (Given_S1);
4027 S2 : constant Node_Id := Original_Node (Given_S2);
4029 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4030 -- Special-case for a bound given by a discriminant, which in the body
4031 -- is replaced with the discriminal of the enclosing type.
4033 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4034 -- Check both bounds
4036 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4038 if Is_Entity_Name (B1)
4039 and then Is_Entity_Name (B2)
4040 and then Ekind (Entity (B1)) = E_Discriminant
4042 return Chars (B1) = Chars (B2);
4045 return Fully_Conformant_Expressions (B1, B2);
4047 end Conforming_Bounds;
4049 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4052 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4054 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4055 end Conforming_Ranges;
4057 -- Start of processing for Fully_Conformant_Discrete_Subtypes
4060 if Nkind (S1) /= Nkind (S2) then
4063 elsif Is_Entity_Name (S1) then
4064 return Entity (S1) = Entity (S2);
4066 elsif Nkind (S1) = N_Range then
4067 return Conforming_Ranges (S1, S2);
4069 elsif Nkind (S1) = N_Subtype_Indication then
4071 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4074 (Range_Expression (Constraint (S1)),
4075 Range_Expression (Constraint (S2)));
4079 end Fully_Conformant_Discrete_Subtypes;
4081 --------------------
4082 -- Install_Entity --
4083 --------------------
4085 procedure Install_Entity (E : Entity_Id) is
4086 Prev : constant Entity_Id := Current_Entity (E);
4089 Set_Is_Immediately_Visible (E);
4090 Set_Current_Entity (E);
4091 Set_Homonym (E, Prev);
4094 ---------------------
4095 -- Install_Formals --
4096 ---------------------
4098 procedure Install_Formals (Id : Entity_Id) is
4102 F := First_Formal (Id);
4104 while Present (F) loop
4108 end Install_Formals;
4110 ---------------------------------
4111 -- Is_Non_Overriding_Operation --
4112 ---------------------------------
4114 function Is_Non_Overriding_Operation
4115 (Prev_E : Entity_Id;
4116 New_E : Entity_Id) return Boolean
4120 G_Typ : Entity_Id := Empty;
4122 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4123 -- If F_Type is a derived type associated with a generic actual
4124 -- subtype, then return its Generic_Parent_Type attribute, else return
4127 function Types_Correspond
4128 (P_Type : Entity_Id;
4129 N_Type : Entity_Id) return Boolean;
4130 -- Returns true if and only if the types (or designated types in the
4131 -- case of anonymous access types) are the same or N_Type is derived
4132 -- directly or indirectly from P_Type.
4134 -----------------------------
4135 -- Get_Generic_Parent_Type --
4136 -----------------------------
4138 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4143 if Is_Derived_Type (F_Typ)
4144 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4146 -- The tree must be traversed to determine the parent subtype in
4147 -- the generic unit, which unfortunately isn't always available
4148 -- via semantic attributes. ??? (Note: The use of Original_Node
4149 -- is needed for cases where a full derived type has been
4152 Indic := Subtype_Indication
4153 (Type_Definition (Original_Node (Parent (F_Typ))));
4155 if Nkind (Indic) = N_Subtype_Indication then
4156 G_Typ := Entity (Subtype_Mark (Indic));
4158 G_Typ := Entity (Indic);
4161 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4162 and then Present (Generic_Parent_Type (Parent (G_Typ)))
4164 return Generic_Parent_Type (Parent (G_Typ));
4169 end Get_Generic_Parent_Type;
4171 ----------------------
4172 -- Types_Correspond --
4173 ----------------------
4175 function Types_Correspond
4176 (P_Type : Entity_Id;
4177 N_Type : Entity_Id) return Boolean
4179 Prev_Type : Entity_Id := Base_Type (P_Type);
4180 New_Type : Entity_Id := Base_Type (N_Type);
4183 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4184 Prev_Type := Designated_Type (Prev_Type);
4187 if Ekind (New_Type) = E_Anonymous_Access_Type then
4188 New_Type := Designated_Type (New_Type);
4191 if Prev_Type = New_Type then
4194 elsif not Is_Class_Wide_Type (New_Type) then
4195 while Etype (New_Type) /= New_Type loop
4196 New_Type := Etype (New_Type);
4197 if New_Type = Prev_Type then
4203 end Types_Correspond;
4205 -- Start of processing for Is_Non_Overriding_Operation
4208 -- In the case where both operations are implicit derived subprograms
4209 -- then neither overrides the other. This can only occur in certain
4210 -- obscure cases (e.g., derivation from homographs created in a generic
4213 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4216 elsif Ekind (Current_Scope) = E_Package
4217 and then Is_Generic_Instance (Current_Scope)
4218 and then In_Private_Part (Current_Scope)
4219 and then Comes_From_Source (New_E)
4221 -- We examine the formals and result subtype of the inherited
4222 -- operation, to determine whether their type is derived from (the
4223 -- instance of) a generic type.
4225 Formal := First_Formal (Prev_E);
4227 while Present (Formal) loop
4228 F_Typ := Base_Type (Etype (Formal));
4230 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4231 F_Typ := Designated_Type (F_Typ);
4234 G_Typ := Get_Generic_Parent_Type (F_Typ);
4236 Next_Formal (Formal);
4239 if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4240 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4247 -- If the generic type is a private type, then the original
4248 -- operation was not overriding in the generic, because there was
4249 -- no primitive operation to override.
4251 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4252 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4253 N_Formal_Private_Type_Definition
4257 -- The generic parent type is the ancestor of a formal derived
4258 -- type declaration. We need to check whether it has a primitive
4259 -- operation that should be overridden by New_E in the generic.
4263 P_Formal : Entity_Id;
4264 N_Formal : Entity_Id;
4268 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4271 while Present (Prim_Elt) loop
4272 P_Prim := Node (Prim_Elt);
4274 if Chars (P_Prim) = Chars (New_E)
4275 and then Ekind (P_Prim) = Ekind (New_E)
4277 P_Formal := First_Formal (P_Prim);
4278 N_Formal := First_Formal (New_E);
4279 while Present (P_Formal) and then Present (N_Formal) loop
4280 P_Typ := Etype (P_Formal);
4281 N_Typ := Etype (N_Formal);
4283 if not Types_Correspond (P_Typ, N_Typ) then
4287 Next_Entity (P_Formal);
4288 Next_Entity (N_Formal);
4291 -- Found a matching primitive operation belonging to the
4292 -- formal ancestor type, so the new subprogram is
4295 if not Present (P_Formal)
4296 and then not Present (N_Formal)
4297 and then (Ekind (New_E) /= E_Function
4300 (Etype (P_Prim), Etype (New_E)))
4306 Next_Elmt (Prim_Elt);
4309 -- If no match found, then the new subprogram does not
4310 -- override in the generic (nor in the instance).
4318 end Is_Non_Overriding_Operation;
4320 ------------------------------
4321 -- Make_Inequality_Operator --
4322 ------------------------------
4324 -- S is the defining identifier of an equality operator. We build a
4325 -- subprogram declaration with the right signature. This operation is
4326 -- intrinsic, because it is always expanded as the negation of the
4327 -- call to the equality function.
4329 procedure Make_Inequality_Operator (S : Entity_Id) is
4330 Loc : constant Source_Ptr := Sloc (S);
4333 Op_Name : Entity_Id;
4339 -- Check that equality was properly defined
4341 if No (Next_Formal (First_Formal (S))) then
4345 A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4346 B := Make_Defining_Identifier (Loc,
4347 Chars (Next_Formal (First_Formal (S))));
4349 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4351 Formals := New_List (
4352 Make_Parameter_Specification (Loc,
4353 Defining_Identifier => A,
4355 New_Reference_To (Etype (First_Formal (S)), Loc)),
4357 Make_Parameter_Specification (Loc,
4358 Defining_Identifier => B,
4360 New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4363 Make_Subprogram_Declaration (Loc,
4365 Make_Function_Specification (Loc,
4366 Defining_Unit_Name => Op_Name,
4367 Parameter_Specifications => Formals,
4368 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
4370 -- Insert inequality right after equality if it is explicit or after
4371 -- the derived type when implicit. These entities are created only for
4372 -- visibility purposes, and eventually replaced in the course of
4373 -- expansion, so they do not need to be attached to the tree and seen
4374 -- by the back-end. Keeping them internal also avoids spurious freezing
4375 -- problems. The declaration is inserted in the tree for analysis, and
4376 -- removed afterwards. If the equality operator comes from an explicit
4377 -- declaration, attach the inequality immediately after. Else the
4378 -- equality is inherited from a derived type declaration, so insert
4379 -- inequality after that declaration.
4381 if No (Alias (S)) then
4382 Insert_After (Unit_Declaration_Node (S), Decl);
4383 elsif Is_List_Member (Parent (S)) then
4384 Insert_After (Parent (S), Decl);
4386 Insert_After (Parent (Etype (First_Formal (S))), Decl);
4389 Mark_Rewrite_Insertion (Decl);
4390 Set_Is_Intrinsic_Subprogram (Op_Name);
4393 Set_Has_Completion (Op_Name);
4394 Set_Corresponding_Equality (Op_Name, S);
4395 Set_Is_Abstract (Op_Name, Is_Abstract (S));
4396 end Make_Inequality_Operator;
4398 ----------------------
4399 -- May_Need_Actuals --
4400 ----------------------
4402 procedure May_Need_Actuals (Fun : Entity_Id) is
4407 F := First_Formal (Fun);
4410 while Present (F) loop
4411 if No (Default_Value (F)) then
4419 Set_Needs_No_Actuals (Fun, B);
4420 end May_Need_Actuals;
4422 ---------------------
4423 -- Mode_Conformant --
4424 ---------------------
4426 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4429 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4431 end Mode_Conformant;
4433 ---------------------------
4434 -- New_Overloaded_Entity --
4435 ---------------------------
4437 procedure New_Overloaded_Entity
4439 Derived_Type : Entity_Id := Empty)
4442 -- Entity that S overrides
4444 Prev_Vis : Entity_Id := Empty;
4445 -- Needs comment ???
4447 function Is_Private_Declaration (E : Entity_Id) return Boolean;
4448 -- Check that E is declared in the private part of the current package,
4449 -- or in the package body, where it may hide a previous declaration.
4450 -- We can't use In_Private_Part by itself because this flag is also
4451 -- set when freezing entities, so we must examine the place of the
4452 -- declaration in the tree, and recognize wrapper packages as well.
4454 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4455 -- If the subprogram being analyzed is a primitive operation of
4456 -- the type of one of its formals, set the corresponding flag.
4458 ----------------------------
4459 -- Is_Private_Declaration --
4460 ----------------------------
4462 function Is_Private_Declaration (E : Entity_Id) return Boolean is
4463 Priv_Decls : List_Id;
4464 Decl : constant Node_Id := Unit_Declaration_Node (E);
4467 if Is_Package (Current_Scope)
4468 and then In_Private_Part (Current_Scope)
4471 Private_Declarations (
4472 Specification (Unit_Declaration_Node (Current_Scope)));
4474 return In_Package_Body (Current_Scope)
4476 (Is_List_Member (Decl)
4477 and then List_Containing (Decl) = Priv_Decls)
4478 or else (Nkind (Parent (Decl)) = N_Package_Specification
4479 and then not Is_Compilation_Unit (
4480 Defining_Entity (Parent (Decl)))
4481 and then List_Containing (Parent (Parent (Decl)))
4486 end Is_Private_Declaration;
4488 -------------------------------
4489 -- Maybe_Primitive_Operation --
4490 -------------------------------
4492 procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4497 function Visible_Part_Type (T : Entity_Id) return Boolean;
4498 -- Returns true if T is declared in the visible part of
4499 -- the current package scope; otherwise returns false.
4500 -- Assumes that T is declared in a package.
4502 procedure Check_Private_Overriding (T : Entity_Id);
4503 -- Checks that if a primitive abstract subprogram of a visible
4504 -- abstract type is declared in a private part, then it must
4505 -- override an abstract subprogram declared in the visible part.
4506 -- Also checks that if a primitive function with a controlling
4507 -- result is declared in a private part, then it must override
4508 -- a function declared in the visible part.
4510 ------------------------------
4511 -- Check_Private_Overriding --
4512 ------------------------------
4514 procedure Check_Private_Overriding (T : Entity_Id) is
4516 if Ekind (Current_Scope) = E_Package
4517 and then In_Private_Part (Current_Scope)
4518 and then Visible_Part_Type (T)
4519 and then not In_Instance
4522 and then Is_Abstract (S)
4523 and then (not Is_Overriding or else not Is_Abstract (E))
4525 Error_Msg_N ("abstract subprograms must be visible "
4526 & "('R'M 3.9.3(10))!", S);
4528 elsif Ekind (S) = E_Function
4529 and then Is_Tagged_Type (T)
4530 and then T = Base_Type (Etype (S))
4531 and then not Is_Overriding
4534 ("private function with tagged result must"
4535 & " override visible-part function", S);
4537 ("\move subprogram to the visible part"
4538 & " ('R'M 3.9.3(10))", S);
4541 end Check_Private_Overriding;
4543 -----------------------
4544 -- Visible_Part_Type --
4545 -----------------------
4547 function Visible_Part_Type (T : Entity_Id) return Boolean is
4548 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4552 -- If the entity is a private type, then it must be
4553 -- declared in a visible part.
4555 if Ekind (T) in Private_Kind then
4559 -- Otherwise, we traverse the visible part looking for its
4560 -- corresponding declaration. We cannot use the declaration
4561 -- node directly because in the private part the entity of a
4562 -- private type is the one in the full view, which does not
4563 -- indicate that it is the completion of something visible.
4565 N := First (Visible_Declarations (Specification (P)));
4566 while Present (N) loop
4567 if Nkind (N) = N_Full_Type_Declaration
4568 and then Present (Defining_Identifier (N))
4569 and then T = Defining_Identifier (N)
4573 elsif (Nkind (N) = N_Private_Type_Declaration
4575 Nkind (N) = N_Private_Extension_Declaration)
4576 and then Present (Defining_Identifier (N))
4577 and then T = Full_View (Defining_Identifier (N))
4586 end Visible_Part_Type;
4588 -- Start of processing for Maybe_Primitive_Operation
4591 if not Comes_From_Source (S) then
4594 -- If the subprogram is at library level, it is not primitive
4597 elsif Current_Scope = Standard_Standard then
4600 elsif (Ekind (Current_Scope) = E_Package
4601 and then not In_Package_Body (Current_Scope))
4602 or else Is_Overriding
4604 -- For function, check return type
4606 if Ekind (S) = E_Function then
4607 B_Typ := Base_Type (Etype (S));
4609 if Scope (B_Typ) = Current_Scope then
4610 Set_Has_Primitive_Operations (B_Typ);
4611 Check_Private_Overriding (B_Typ);
4615 -- For all subprograms, check formals
4617 Formal := First_Formal (S);
4618 while Present (Formal) loop
4619 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4620 F_Typ := Designated_Type (Etype (Formal));
4622 F_Typ := Etype (Formal);
4625 B_Typ := Base_Type (F_Typ);
4627 if Scope (B_Typ) = Current_Scope then
4628 Set_Has_Primitive_Operations (B_Typ);
4629 Check_Private_Overriding (B_Typ);
4632 Next_Formal (Formal);
4635 end Maybe_Primitive_Operation;
4637 -- Start of processing for New_Overloaded_Entity
4640 -- We need to look for an entity that S may override. This must be a
4641 -- homonym in the current scope, so we look for the first homonym of
4642 -- S in the current scope as the starting point for the search.
4644 E := Current_Entity_In_Scope (S);
4646 -- If there is no homonym then this is definitely not overriding
4649 Enter_Overloaded_Entity (S);
4650 Check_Dispatching_Operation (S, Empty);
4651 Maybe_Primitive_Operation;
4653 -- If there is a homonym that is not overloadable, then we have an
4654 -- error, except for the special cases checked explicitly below.
4656 elsif not Is_Overloadable (E) then
4658 -- Check for spurious conflict produced by a subprogram that has the
4659 -- same name as that of the enclosing generic package. The conflict
4660 -- occurs within an instance, between the subprogram and the renaming
4661 -- declaration for the package. After the subprogram, the package
4662 -- renaming declaration becomes hidden.
4664 if Ekind (E) = E_Package
4665 and then Present (Renamed_Object (E))
4666 and then Renamed_Object (E) = Current_Scope
4667 and then Nkind (Parent (Renamed_Object (E))) =
4668 N_Package_Specification
4669 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4672 Set_Is_Immediately_Visible (E, False);
4673 Enter_Overloaded_Entity (S);
4674 Set_Homonym (S, Homonym (E));
4675 Check_Dispatching_Operation (S, Empty);
4677 -- If the subprogram is implicit it is hidden by the previous
4678 -- declaration. However if it is dispatching, it must appear in the
4679 -- dispatch table anyway, because it can be dispatched to even if it
4680 -- cannot be called directly.
4682 elsif Present (Alias (S))
4683 and then not Comes_From_Source (S)
4685 Set_Scope (S, Current_Scope);
4687 if Is_Dispatching_Operation (Alias (S)) then
4688 Check_Dispatching_Operation (S, Empty);
4694 Error_Msg_Sloc := Sloc (E);
4695 Error_Msg_N ("& conflicts with declaration#", S);
4697 -- Useful additional warning
4699 if Is_Generic_Unit (E) then
4700 Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4706 -- E exists and is overloadable
4709 -- Loop through E and its homonyms to determine if any of them is
4710 -- the candidate for overriding by S.
4712 while Present (E) loop
4714 -- Definitely not interesting if not in the current scope
4716 if Scope (E) /= Current_Scope then
4719 -- Check if we have type conformance
4721 elsif Type_Conformant (E, S) then
4723 -- If the old and new entities have the same profile and one
4724 -- is not the body of the other, then this is an error, unless
4725 -- one of them is implicitly declared.
4727 -- There are some cases when both can be implicit, for example
4728 -- when both a literal and a function that overrides it are
4729 -- inherited in a derivation, or when an inhertited operation
4730 -- of a tagged full type overrides the ineherited operation of
4731 -- a private extension. Ada 83 had a special rule for the the
4732 -- literal case. In Ada95, the later implicit operation hides
4733 -- the former, and the literal is always the former. In the
4734 -- odd case where both are derived operations declared at the
4735 -- same point, both operations should be declared, and in that
4736 -- case we bypass the following test and proceed to the next
4737 -- part (this can only occur for certain obscure cases
4738 -- involving homographs in instances and can't occur for
4739 -- dispatching operations ???). Note that the following
4740 -- condition is less than clear. For example, it's not at all
4741 -- clear why there's a test for E_Entry here. ???
4743 if Present (Alias (S))
4744 and then (No (Alias (E))
4745 or else Comes_From_Source (E)
4746 or else Is_Dispatching_Operation (E))
4748 (Ekind (E) = E_Entry
4749 or else Ekind (E) /= E_Enumeration_Literal)
4751 -- When an derived operation is overloaded it may be due to
4752 -- the fact that the full view of a private extension
4753 -- re-inherits. It has to be dealt with.
4755 if Is_Package (Current_Scope)
4756 and then In_Private_Part (Current_Scope)
4758 Check_Operation_From_Private_View (S, E);
4761 -- In any case the implicit operation remains hidden by
4762 -- the existing declaration, which is overriding.
4764 Set_Is_Overriding_Operation (E);
4767 -- Within an instance, the renaming declarations for
4768 -- actual subprograms may become ambiguous, but they do
4769 -- not hide each other.
4771 elsif Ekind (E) /= E_Entry
4772 and then not Comes_From_Source (E)
4773 and then not Is_Generic_Instance (E)
4774 and then (Present (Alias (E))
4775 or else Is_Intrinsic_Subprogram (E))
4776 and then (not In_Instance
4777 or else No (Parent (E))
4778 or else Nkind (Unit_Declaration_Node (E)) /=
4779 N_Subprogram_Renaming_Declaration)
4781 -- A subprogram child unit is not allowed to override
4782 -- an inherited subprogram (10.1.1(20)).
4784 if Is_Child_Unit (S) then
4786 ("child unit overrides inherited subprogram in parent",
4791 if Is_Non_Overriding_Operation (E, S) then
4792 Enter_Overloaded_Entity (S);
4793 if not Present (Derived_Type)
4794 or else Is_Tagged_Type (Derived_Type)
4796 Check_Dispatching_Operation (S, Empty);
4802 -- E is a derived operation or an internal operator which
4803 -- is being overridden. Remove E from further visibility.
4804 -- Furthermore, if E is a dispatching operation, it must be
4805 -- replaced in the list of primitive operations of its type
4806 -- (see Override_Dispatching_Operation).
4812 Prev := First_Entity (Current_Scope);
4814 while Present (Prev)
4815 and then Next_Entity (Prev) /= E
4820 -- It is possible for E to be in the current scope and
4821 -- yet not in the entity chain. This can only occur in a
4822 -- generic context where E is an implicit concatenation
4823 -- in the formal part, because in a generic body the
4824 -- entity chain starts with the formals.
4827 (Present (Prev) or else Chars (E) = Name_Op_Concat);
4829 -- E must be removed both from the entity_list of the
4830 -- current scope, and from the visibility chain
4832 if Debug_Flag_E then
4833 Write_Str ("Override implicit operation ");
4834 Write_Int (Int (E));
4838 -- If E is a predefined concatenation, it stands for four
4839 -- different operations. As a result, a single explicit
4840 -- declaration does not hide it. In a possible ambiguous
4841 -- situation, Disambiguate chooses the user-defined op,
4842 -- so it is correct to retain the previous internal one.
4844 if Chars (E) /= Name_Op_Concat
4845 or else Ekind (E) /= E_Operator
4847 -- For nondispatching derived operations that are
4848 -- overridden by a subprogram declared in the private
4849 -- part of a package, we retain the derived
4850 -- subprogram but mark it as not immediately visible.
4851 -- If the derived operation was declared in the
4852 -- visible part then this ensures that it will still
4853 -- be visible outside the package with the proper
4854 -- signature (calls from outside must also be
4855 -- directed to this version rather than the
4856 -- overriding one, unlike the dispatching case).
4857 -- Calls from inside the package will still resolve
4858 -- to the overriding subprogram since the derived one
4859 -- is marked as not visible within the package.
4861 -- If the private operation is dispatching, we achieve
4862 -- the overriding by keeping the implicit operation
4863 -- but setting its alias to be the overriding one. In
4864 -- this fashion the proper body is executed in all
4865 -- cases, but the original signature is used outside
4868 -- If the overriding is not in the private part, we
4869 -- remove the implicit operation altogether.
4871 if Is_Private_Declaration (S) then
4873 if not Is_Dispatching_Operation (E) then
4874 Set_Is_Immediately_Visible (E, False);
4876 -- Work done in Override_Dispatching_Operation,
4877 -- so nothing else need to be done here.
4883 -- Find predecessor of E in Homonym chain
4885 if E = Current_Entity (E) then
4888 Prev_Vis := Current_Entity (E);
4889 while Homonym (Prev_Vis) /= E loop
4890 Prev_Vis := Homonym (Prev_Vis);
4894 if Prev_Vis /= Empty then
4896 -- Skip E in the visibility chain
4898 Set_Homonym (Prev_Vis, Homonym (E));
4901 Set_Name_Entity_Id (Chars (E), Homonym (E));
4904 Set_Next_Entity (Prev, Next_Entity (E));
4906 if No (Next_Entity (Prev)) then
4907 Set_Last_Entity (Current_Scope, Prev);
4913 Enter_Overloaded_Entity (S);
4914 Set_Is_Overriding_Operation (S);
4916 if Is_Dispatching_Operation (E) then
4918 -- An overriding dispatching subprogram inherits the
4919 -- convention of the overridden subprogram (by
4922 Set_Convention (S, Convention (E));
4924 Check_Dispatching_Operation (S, E);
4926 Check_Dispatching_Operation (S, Empty);
4929 Maybe_Primitive_Operation (Is_Overriding => True);
4930 goto Check_Inequality;
4933 -- Apparent redeclarations in instances can occur when two
4934 -- formal types get the same actual type. The subprograms in
4935 -- in the instance are legal, even if not callable from the
4936 -- outside. Calls from within are disambiguated elsewhere.
4937 -- For dispatching operations in the visible part, the usual
4938 -- rules apply, and operations with the same profile are not
4941 elsif (In_Instance_Visible_Part
4942 and then not Is_Dispatching_Operation (E))
4943 or else In_Instance_Not_Visible
4947 -- Here we have a real error (identical profile)
4950 Error_Msg_Sloc := Sloc (E);
4952 -- Avoid cascaded errors if the entity appears in
4953 -- subsequent calls.
4955 Set_Scope (S, Current_Scope);
4957 Error_Msg_N ("& conflicts with declaration#", S);
4959 if Is_Generic_Instance (S)
4960 and then not Has_Completion (E)
4963 ("\instantiation cannot provide body for it", S);
4977 -- On exit, we know that S is a new entity
4979 Enter_Overloaded_Entity (S);
4980 Maybe_Primitive_Operation;
4982 -- If S is a derived operation for an untagged type then by
4983 -- definition it's not a dispatching operation (even if the parent
4984 -- operation was dispatching), so we don't call
4985 -- Check_Dispatching_Operation in that case.
4987 if not Present (Derived_Type)
4988 or else Is_Tagged_Type (Derived_Type)
4990 Check_Dispatching_Operation (S, Empty);
4994 -- If this is a user-defined equality operator that is not a derived
4995 -- subprogram, create the corresponding inequality. If the operation is
4996 -- dispatching, the expansion is done elsewhere, and we do not create
4997 -- an explicit inequality operation.
4999 <<Check_Inequality>>
5000 if Chars (S) = Name_Op_Eq
5001 and then Etype (S) = Standard_Boolean
5002 and then Present (Parent (S))
5003 and then not Is_Dispatching_Operation (S)
5005 Make_Inequality_Operator (S);
5007 end New_Overloaded_Entity;
5009 ---------------------
5010 -- Process_Formals --
5011 ---------------------
5013 procedure Process_Formals
5015 Related_Nod : Node_Id)
5017 Param_Spec : Node_Id;
5019 Formal_Type : Entity_Id;
5023 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5024 -- Check whether the default has a class-wide type. After analysis the
5025 -- default has the type of the formal, so we must also check explicitly
5026 -- for an access attribute.
5028 ---------------------------
5029 -- Is_Class_Wide_Default --
5030 ---------------------------
5032 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5034 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5035 or else (Nkind (D) = N_Attribute_Reference
5036 and then Attribute_Name (D) = Name_Access
5037 and then Is_Class_Wide_Type (Etype (Prefix (D))));
5038 end Is_Class_Wide_Default;
5040 -- Start of processing for Process_Formals
5043 -- In order to prevent premature use of the formals in the same formal
5044 -- part, the Ekind is left undefined until all default expressions are
5045 -- analyzed. The Ekind is established in a separate loop at the end.
5047 Param_Spec := First (T);
5049 while Present (Param_Spec) loop
5051 Formal := Defining_Identifier (Param_Spec);
5052 Enter_Name (Formal);
5054 -- Case of ordinary parameters
5056 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5057 Find_Type (Parameter_Type (Param_Spec));
5058 Ptype := Parameter_Type (Param_Spec);
5060 if Ptype = Error then
5064 Formal_Type := Entity (Ptype);
5066 if Ekind (Formal_Type) = E_Incomplete_Type
5067 or else (Is_Class_Wide_Type (Formal_Type)
5068 and then Ekind (Root_Type (Formal_Type)) =
5071 -- Ada 2005 (AI-50217): Incomplete tagged types that are made
5072 -- visible by a limited with_clause are valid formal types.
5074 if From_With_Type (Formal_Type)
5075 and then Is_Tagged_Type (Formal_Type)
5079 elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5080 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5082 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5085 elsif Ekind (Formal_Type) = E_Void then
5086 Error_Msg_NE ("premature use of&",
5087 Parameter_Type (Param_Spec), Formal_Type);
5090 -- Ada 2005 (AI-231): Create and decorate an internal subtype
5091 -- declaration corresponding to the null-excluding type of the
5092 -- formal in the enclosing scope. Finally, replace the
5093 -- parameter type of the formal with the internal subtype.
5095 if Null_Exclusion_Present (Param_Spec) then
5097 Loc : constant Source_Ptr := Sloc (Param_Spec);
5099 Anon : constant Entity_Id :=
5100 Make_Defining_Identifier (Loc,
5101 Chars => New_Internal_Name ('S'));
5103 Curr_Scope : constant Scope_Stack_Entry :=
5104 Scope_Stack.Table (Scope_Stack.Last);
5106 Ptype : constant Node_Id := Parameter_Type (Param_Spec);
5108 P : Node_Id := Parent (Related_Nod);
5111 Set_Is_Internal (Anon);
5114 Make_Subtype_Declaration (Loc,
5115 Defining_Identifier => Anon,
5116 Null_Exclusion_Present => True,
5117 Subtype_Indication =>
5118 New_Occurrence_Of (Etype (Ptype), Loc));
5120 -- Propagate the null-excluding attribute to the new entity
5122 if Null_Exclusion_Present (Param_Spec) then
5123 Set_Null_Exclusion_Present (Param_Spec, False);
5124 Set_Can_Never_Be_Null (Anon);
5127 Mark_Rewrite_Insertion (Decl);
5129 -- Insert the new declaration in the nearest enclosing scope
5130 -- in front of the subprogram or entry declaration.
5132 while not Is_List_Member (P) loop
5136 Insert_Before (P, Decl);
5138 Rewrite (Ptype, New_Occurrence_Of (Anon, Loc));
5139 Mark_Rewrite_Insertion (Ptype);
5141 -- Analyze the new declaration in the context of the
5144 Scope_Stack.Decrement_Last;
5146 Scope_Stack.Append (Curr_Scope);
5148 Formal_Type := Anon;
5152 -- Ada 2005 (AI-231): Static checks
5154 if Null_Exclusion_Present (Param_Spec)
5155 or else Can_Never_Be_Null (Entity (Ptype))
5157 Null_Exclusion_Static_Checks (Param_Spec);
5160 -- An access formal type
5164 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5166 -- Ada 2005 (AI-254)
5169 AD : constant Node_Id :=
5170 Access_To_Subprogram_Definition
5171 (Parameter_Type (Param_Spec));
5173 if Present (AD) and then Protected_Present (AD) then
5175 Replace_Anonymous_Access_To_Protected_Subprogram
5176 (Param_Spec, Formal_Type);
5181 Set_Etype (Formal, Formal_Type);
5182 Default := Expression (Param_Spec);
5184 if Present (Default) then
5185 if Out_Present (Param_Spec) then
5187 ("default initialization only allowed for IN parameters",
5191 -- Do the special preanalysis of the expression (see section on
5192 -- "Handling of Default Expressions" in the spec of package Sem).
5194 Analyze_Per_Use_Expression (Default, Formal_Type);
5196 -- Check that the designated type of an access parameter's
5197 -- default is not a class-wide type unless the parameter's
5198 -- designated type is also class-wide.
5200 if Ekind (Formal_Type) = E_Anonymous_Access_Type
5201 and then Is_Class_Wide_Default (Default)
5202 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5205 ("access to class-wide expression not allowed here", Default);
5213 -- If this is the formal part of a function specification, analyze the
5214 -- subtype mark in the context where the formals are visible but not
5215 -- yet usable, and may hide outer homographs.
5217 if Nkind (Related_Nod) = N_Function_Specification then
5218 Analyze_Return_Type (Related_Nod);
5221 -- Now set the kind (mode) of each formal
5223 Param_Spec := First (T);
5225 while Present (Param_Spec) loop
5226 Formal := Defining_Identifier (Param_Spec);
5227 Set_Formal_Mode (Formal);
5229 if Ekind (Formal) = E_In_Parameter then
5230 Set_Default_Value (Formal, Expression (Param_Spec));
5232 if Present (Expression (Param_Spec)) then
5233 Default := Expression (Param_Spec);
5235 if Is_Scalar_Type (Etype (Default)) then
5237 (Parameter_Type (Param_Spec)) /= N_Access_Definition
5239 Formal_Type := Entity (Parameter_Type (Param_Spec));
5242 Formal_Type := Access_Definition
5243 (Related_Nod, Parameter_Type (Param_Spec));
5246 Apply_Scalar_Range_Check (Default, Formal_Type);
5254 end Process_Formals;
5256 ----------------------------
5257 -- Reference_Body_Formals --
5258 ----------------------------
5260 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5265 if Error_Posted (Spec) then
5269 Fs := First_Formal (Spec);
5270 Fb := First_Formal (Bod);
5272 while Present (Fs) loop
5273 Generate_Reference (Fs, Fb, 'b');
5276 Style.Check_Identifier (Fb, Fs);
5279 Set_Spec_Entity (Fb, Fs);
5280 Set_Referenced (Fs, False);
5284 end Reference_Body_Formals;
5286 -------------------------
5287 -- Set_Actual_Subtypes --
5288 -------------------------
5290 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5291 Loc : constant Source_Ptr := Sloc (N);
5295 First_Stmt : Node_Id := Empty;
5296 AS_Needed : Boolean;
5299 -- If this is an emtpy initialization procedure, no need to create
5300 -- actual subtypes (small optimization).
5302 if Ekind (Subp) = E_Procedure
5303 and then Is_Null_Init_Proc (Subp)
5308 Formal := First_Formal (Subp);
5309 while Present (Formal) loop
5310 T := Etype (Formal);
5312 -- We never need an actual subtype for a constrained formal
5314 if Is_Constrained (T) then
5317 -- If we have unknown discriminants, then we do not need an actual
5318 -- subtype, or more accurately we cannot figure it out! Note that
5319 -- all class-wide types have unknown discriminants.
5321 elsif Has_Unknown_Discriminants (T) then
5324 -- At this stage we have an unconstrained type that may need an
5325 -- actual subtype. For sure the actual subtype is needed if we have
5326 -- an unconstrained array type.
5328 elsif Is_Array_Type (T) then
5331 -- The only other case which needs an actual subtype is an
5332 -- unconstrained record type which is an IN parameter (we cannot
5333 -- generate actual subtypes for the OUT or IN OUT case, since an
5334 -- assignment can change the discriminant values. However we exclude
5335 -- the case of initialization procedures, since discriminants are
5336 -- handled very specially in this context, see the section entitled
5337 -- "Handling of Discriminants" in Einfo. We also exclude the case of
5338 -- Discrim_SO_Functions (functions used in front end layout mode for
5339 -- size/offset values), since in such functions only discriminants
5340 -- are referenced, and not only are such subtypes not needed, but
5341 -- they cannot always be generated, because of order of elaboration
5344 elsif Is_Record_Type (T)
5345 and then Ekind (Formal) = E_In_Parameter
5346 and then Chars (Formal) /= Name_uInit
5347 and then not Is_Unchecked_Union (T)
5348 and then not Is_Discrim_SO_Function (Subp)
5352 -- All other cases do not need an actual subtype
5358 -- Generate actual subtypes for unconstrained arrays and
5359 -- unconstrained discriminated records.
5362 if Nkind (N) = N_Accept_Statement then
5364 -- If expansion is active, The formal is replaced by a local
5365 -- variable that renames the corresponding entry of the
5366 -- parameter block, and it is this local variable that may
5367 -- require an actual subtype.
5369 if Expander_Active then
5370 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5372 Decl := Build_Actual_Subtype (T, Formal);
5375 if Present (Handled_Statement_Sequence (N)) then
5377 First (Statements (Handled_Statement_Sequence (N)));
5378 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5379 Mark_Rewrite_Insertion (Decl);
5381 -- If the accept statement has no body, there will be no
5382 -- reference to the actuals, so no need to compute actual
5389 Decl := Build_Actual_Subtype (T, Formal);
5390 Prepend (Decl, Declarations (N));
5391 Mark_Rewrite_Insertion (Decl);
5394 -- The declaration uses the bounds of an existing object, and
5395 -- therefore needs no constraint checks.
5397 Analyze (Decl, Suppress => All_Checks);
5399 -- We need to freeze manually the generated type when it is
5400 -- inserted anywhere else than in a declarative part.
5402 if Present (First_Stmt) then
5403 Insert_List_Before_And_Analyze (First_Stmt,
5404 Freeze_Entity (Defining_Identifier (Decl), Loc));
5407 if Nkind (N) = N_Accept_Statement
5408 and then Expander_Active
5410 Set_Actual_Subtype (Renamed_Object (Formal),
5411 Defining_Identifier (Decl));
5413 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5417 Next_Formal (Formal);
5419 end Set_Actual_Subtypes;
5421 ---------------------
5422 -- Set_Formal_Mode --
5423 ---------------------
5425 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5426 Spec : constant Node_Id := Parent (Formal_Id);
5429 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5430 -- since we ensure that corresponding actuals are always valid at the
5431 -- point of the call.
5433 if Out_Present (Spec) then
5434 if Ekind (Scope (Formal_Id)) = E_Function
5435 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5437 Error_Msg_N ("functions can only have IN parameters", Spec);
5438 Set_Ekind (Formal_Id, E_In_Parameter);
5440 elsif In_Present (Spec) then
5441 Set_Ekind (Formal_Id, E_In_Out_Parameter);
5444 Set_Ekind (Formal_Id, E_Out_Parameter);
5445 Set_Never_Set_In_Source (Formal_Id, True);
5446 Set_Is_True_Constant (Formal_Id, False);
5447 Set_Current_Value (Formal_Id, Empty);
5451 Set_Ekind (Formal_Id, E_In_Parameter);
5454 -- Set Is_Known_Non_Null for access parameters since the language
5455 -- guarantees that access parameters are always non-null. We also set
5456 -- Can_Never_Be_Null, since there is no way to change the value.
5458 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5460 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
5461 -- null; In Ada 2005, only if then null_exclusion is explicit.
5463 if Ada_Version < Ada_05
5464 or else Null_Exclusion_Present (Spec)
5465 or else Can_Never_Be_Null (Etype (Formal_Id))
5467 Set_Is_Known_Non_Null (Formal_Id);
5468 Set_Can_Never_Be_Null (Formal_Id);
5471 elsif Is_Access_Type (Etype (Formal_Id))
5472 and then Can_Never_Be_Null (Etype (Formal_Id))
5474 -- Ada 2005: The access subtype may be declared with null-exclusion
5476 Set_Is_Known_Non_Null (Formal_Id);
5477 Set_Can_Never_Be_Null (Formal_Id);
5480 Set_Mechanism (Formal_Id, Default_Mechanism);
5481 Set_Formal_Validity (Formal_Id);
5482 end Set_Formal_Mode;
5484 -------------------------
5485 -- Set_Formal_Validity --
5486 -------------------------
5488 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5490 -- If no validity checking, then we cannot assume anything about the
5491 -- validity of parameters, since we do not know there is any checking
5492 -- of the validity on the call side.
5494 if not Validity_Checks_On then
5497 -- If validity checking for parameters is enabled, this means we are
5498 -- not supposed to make any assumptions about argument values.
5500 elsif Validity_Check_Parameters then
5503 -- If we are checking in parameters, we will assume that the caller is
5504 -- also checking parameters, so we can assume the parameter is valid.
5506 elsif Ekind (Formal_Id) = E_In_Parameter
5507 and then Validity_Check_In_Params
5509 Set_Is_Known_Valid (Formal_Id, True);
5511 -- Similar treatment for IN OUT parameters
5513 elsif Ekind (Formal_Id) = E_In_Out_Parameter
5514 and then Validity_Check_In_Out_Params
5516 Set_Is_Known_Valid (Formal_Id, True);
5518 end Set_Formal_Validity;
5520 ------------------------
5521 -- Subtype_Conformant --
5522 ------------------------
5524 function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5527 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5529 end Subtype_Conformant;
5531 ---------------------
5532 -- Type_Conformant --
5533 ---------------------
5535 function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5538 Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
5540 end Type_Conformant;
5542 -------------------------------
5543 -- Valid_Operator_Definition --
5544 -------------------------------
5546 procedure Valid_Operator_Definition (Designator : Entity_Id) is
5549 Id : constant Name_Id := Chars (Designator);
5553 F := First_Formal (Designator);
5555 while Present (F) loop
5558 if Present (Default_Value (F)) then
5560 ("default values not allowed for operator parameters",
5567 -- Verify that user-defined operators have proper number of arguments
5568 -- First case of operators which can only be unary
5571 or else Id = Name_Op_Abs
5575 -- Case of operators which can be unary or binary
5577 elsif Id = Name_Op_Add
5578 or Id = Name_Op_Subtract
5580 N_OK := (N in 1 .. 2);
5582 -- All other operators can only be binary
5590 ("incorrect number of arguments for operator", Designator);
5594 and then Base_Type (Etype (Designator)) = Standard_Boolean
5595 and then not Is_Intrinsic_Subprogram (Designator)
5598 ("explicit definition of inequality not allowed", Designator);
5600 end Valid_Operator_Definition;