1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Layout; use Layout;
44 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
49 with Output; use Output;
50 with Rtsfind; use Rtsfind;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch4; use Sem_Ch4;
55 with Sem_Ch5; use Sem_Ch5;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Util; use Sem_Util;
67 with Sem_Type; use Sem_Type;
68 with Sem_Warn; use Sem_Warn;
69 with Sinput; use Sinput;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
76 with Stylesw; use Stylesw;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Validsw; use Validsw;
82 package body Sem_Ch6 is
84 May_Hide_Profile : Boolean := False;
85 -- This flag is used to indicate that two formals in two subprograms being
86 -- checked for conformance differ only in that one is an access parameter
87 -- while the other is of a general access type with the same designated
88 -- type. In this case, if the rest of the signatures match, a call to
89 -- either subprogram may be ambiguous, which is worth a warning. The flag
90 -- is set in Compatible_Types, and the warning emitted in
91 -- New_Overloaded_Entity.
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Analyze_Return_Statement (N : Node_Id);
98 -- Common processing for simple_ and extended_return_statements
100 procedure Analyze_Function_Return (N : Node_Id);
101 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
102 -- applies to a [generic] function.
104 procedure Analyze_Return_Type (N : Node_Id);
105 -- Subsidiary to Process_Formals: analyze subtype mark in function
106 -- specification, in a context where the formals are visible and hide
109 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
110 -- Analyze a generic subprogram body. N is the body to be analyzed, and
111 -- Gen_Id is the defining entity Id for the corresponding spec.
113 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
114 -- If a subprogram has pragma Inline and inlining is active, use generic
115 -- machinery to build an unexpanded body for the subprogram. This body is
116 -- subsequently used for inline expansions at call sites. If subprogram can
117 -- be inlined (depending on size and nature of local declarations) this
118 -- function returns true. Otherwise subprogram body is treated normally.
119 -- If proper warnings are enabled and the subprogram contains a construct
120 -- that cannot be inlined, the offending construct is flagged accordingly.
122 procedure Check_Conformance
125 Ctype : Conformance_Type;
127 Conforms : out Boolean;
128 Err_Loc : Node_Id := Empty;
129 Get_Inst : Boolean := False;
130 Skip_Controlling_Formals : Boolean := False);
131 -- Given two entities, this procedure checks that the profiles associated
132 -- with these entities meet the conformance criterion given by the third
133 -- parameter. If they conform, Conforms is set True and control returns
134 -- to the caller. If they do not conform, Conforms is set to False, and
135 -- in addition, if Errmsg is True on the call, proper messages are output
136 -- to complain about the conformance failure. If Err_Loc is non_Empty
137 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
138 -- error messages are placed on the appropriate part of the construct
139 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
140 -- against a formal access-to-subprogram type so Get_Instance_Of must
143 procedure Check_Subprogram_Order (N : Node_Id);
144 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
145 -- the alpha ordering rule for N if this ordering requirement applicable.
147 procedure Check_Returns
151 Proc : Entity_Id := Empty);
152 -- Called to check for missing return statements in a function body, or for
153 -- returns present in a procedure body which has No_Return set. HSS is the
154 -- handled statement sequence for the subprogram body. This procedure
155 -- checks all flow paths to make sure they either have return (Mode = 'F',
156 -- used for functions) or do not have a return (Mode = 'P', used for
157 -- No_Return procedures). The flag Err is set if there are any control
158 -- paths not explicitly terminated by a return in the function case, and is
159 -- True otherwise. Proc is the entity for the procedure case and is used
160 -- in posting the warning message.
162 procedure Enter_Overloaded_Entity (S : Entity_Id);
163 -- This procedure makes S, a new overloaded entity, into the first visible
164 -- entity with that name.
166 procedure Install_Entity (E : Entity_Id);
167 -- Make single entity visible. Used for generic formals as well
169 function Is_Non_Overriding_Operation
171 New_E : Entity_Id) return Boolean;
172 -- Enforce the rule given in 12.3(18): a private operation in an instance
173 -- overrides an inherited operation only if the corresponding operation
174 -- was overriding in the generic. This can happen for primitive operations
175 -- of types derived (in the generic unit) from formal private or formal
178 procedure Make_Inequality_Operator (S : Entity_Id);
179 -- Create the declaration for an inequality operator that is implicitly
180 -- created by a user-defined equality operator that yields a boolean.
182 procedure May_Need_Actuals (Fun : Entity_Id);
183 -- Flag functions that can be called without parameters, i.e. those that
184 -- have no parameters, or those for which defaults exist for all parameters
186 procedure Process_PPCs
189 Body_Id : Entity_Id);
190 -- Called from Analyze_Body to deal with scanning post conditions for the
191 -- body and assembling and inserting the _postconditions procedure. N is
192 -- the node for the subprogram body and Body_Id/Spec_Id are the entities
193 -- for the body and separate spec (if there is no separate spec, Spec_Id
196 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
197 -- Formal_Id is an formal parameter entity. This procedure deals with
198 -- setting the proper validity status for this entity, which depends
199 -- on the kind of parameter and the validity checking mode.
201 ------------------------------
202 -- Analyze_Return_Statement --
203 ------------------------------
205 procedure Analyze_Return_Statement (N : Node_Id) is
207 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
208 N_Extended_Return_Statement));
210 Returns_Object : constant Boolean :=
211 Nkind (N) = N_Extended_Return_Statement
213 (Nkind (N) = N_Simple_Return_Statement
214 and then Present (Expression (N)));
215 -- True if we're returning something; that is, "return <expression>;"
216 -- or "return Result : T [:= ...]". False for "return;". Used for error
217 -- checking: If Returns_Object is True, N should apply to a function
218 -- body; otherwise N should apply to a procedure body, entry body,
219 -- accept statement, or extended return statement.
221 function Find_What_It_Applies_To return Entity_Id;
222 -- Find the entity representing the innermost enclosing body, accept
223 -- statement, or extended return statement. If the result is a callable
224 -- construct or extended return statement, then this will be the value
225 -- of the Return_Applies_To attribute. Otherwise, the program is
226 -- illegal. See RM-6.5(4/2).
228 -----------------------------
229 -- Find_What_It_Applies_To --
230 -----------------------------
232 function Find_What_It_Applies_To return Entity_Id is
233 Result : Entity_Id := Empty;
236 -- Loop outward through the Scope_Stack, skipping blocks and loops
238 for J in reverse 0 .. Scope_Stack.Last loop
239 Result := Scope_Stack.Table (J).Entity;
240 exit when Ekind (Result) /= E_Block and then
241 Ekind (Result) /= E_Loop;
244 pragma Assert (Present (Result));
246 end Find_What_It_Applies_To;
248 -- Local declarations
250 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
251 Kind : constant Entity_Kind := Ekind (Scope_Id);
252 Loc : constant Source_Ptr := Sloc (N);
253 Stm_Entity : constant Entity_Id :=
255 (E_Return_Statement, Current_Scope, Loc, 'R');
257 -- Start of processing for Analyze_Return_Statement
260 Set_Return_Statement_Entity (N, Stm_Entity);
262 Set_Etype (Stm_Entity, Standard_Void_Type);
263 Set_Return_Applies_To (Stm_Entity, Scope_Id);
265 -- Place Return entity on scope stack, to simplify enforcement of 6.5
266 -- (4/2): an inner return statement will apply to this extended return.
268 if Nkind (N) = N_Extended_Return_Statement then
269 Push_Scope (Stm_Entity);
272 -- Check that pragma No_Return is obeyed
274 if No_Return (Scope_Id) then
275 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
278 -- Warn on any unassigned OUT parameters if in procedure
280 if Ekind (Scope_Id) = E_Procedure then
281 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
284 -- Check that functions return objects, and other things do not
286 if Kind = E_Function or else Kind = E_Generic_Function then
287 if not Returns_Object then
288 Error_Msg_N ("missing expression in return from function", N);
291 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
292 if Returns_Object then
293 Error_Msg_N ("procedure cannot return value (use function)", N);
296 elsif Kind = E_Entry or else Kind = E_Entry_Family then
297 if Returns_Object then
298 if Is_Protected_Type (Scope (Scope_Id)) then
299 Error_Msg_N ("entry body cannot return value", N);
301 Error_Msg_N ("accept statement cannot return value", N);
305 elsif Kind = E_Return_Statement then
307 -- We are nested within another return statement, which must be an
308 -- extended_return_statement.
310 if Returns_Object then
312 ("extended_return_statement cannot return value; " &
313 "use `""RETURN;""`", N);
317 Error_Msg_N ("illegal context for return statement", N);
320 if Kind = E_Function or else Kind = E_Generic_Function then
321 Analyze_Function_Return (N);
324 if Nkind (N) = N_Extended_Return_Statement then
328 Kill_Current_Values (Last_Assignment_Only => True);
329 Check_Unreachable_Code (N);
330 end Analyze_Return_Statement;
332 ---------------------------------------------
333 -- Analyze_Abstract_Subprogram_Declaration --
334 ---------------------------------------------
336 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
337 Designator : constant Entity_Id :=
338 Analyze_Subprogram_Specification (Specification (N));
339 Scop : constant Entity_Id := Current_Scope;
342 Generate_Definition (Designator);
343 Set_Is_Abstract_Subprogram (Designator);
344 New_Overloaded_Entity (Designator);
345 Check_Delayed_Subprogram (Designator);
347 Set_Categorization_From_Scope (Designator, Scop);
349 if Ekind (Scope (Designator)) = E_Protected_Type then
351 ("abstract subprogram not allowed in protected type", N);
353 -- Issue a warning if the abstract subprogram is neither a dispatching
354 -- operation nor an operation that overrides an inherited subprogram or
355 -- predefined operator, since this most likely indicates a mistake.
357 elsif Warn_On_Redundant_Constructs
358 and then not Is_Dispatching_Operation (Designator)
359 and then not Is_Overriding_Operation (Designator)
360 and then (not Is_Operator_Symbol_Name (Chars (Designator))
361 or else Scop /= Scope (Etype (First_Formal (Designator))))
364 ("?abstract subprogram is not dispatching or overriding", N);
367 Generate_Reference_To_Formals (Designator);
368 end Analyze_Abstract_Subprogram_Declaration;
370 ----------------------------------------
371 -- Analyze_Extended_Return_Statement --
372 ----------------------------------------
374 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
376 Analyze_Return_Statement (N);
377 end Analyze_Extended_Return_Statement;
379 ----------------------------
380 -- Analyze_Function_Call --
381 ----------------------------
383 procedure Analyze_Function_Call (N : Node_Id) is
384 P : constant Node_Id := Name (N);
385 L : constant List_Id := Parameter_Associations (N);
391 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
392 -- as B (A, X). If the rewriting is successful, the call has been
393 -- analyzed and we just return.
395 if Nkind (P) = N_Selected_Component
396 and then Name (N) /= P
397 and then Is_Rewrite_Substitution (N)
398 and then Present (Etype (N))
403 -- If error analyzing name, then set Any_Type as result type and return
405 if Etype (P) = Any_Type then
406 Set_Etype (N, Any_Type);
410 -- Otherwise analyze the parameters
414 while Present (Actual) loop
416 Check_Parameterless_Call (Actual);
422 end Analyze_Function_Call;
424 -----------------------------
425 -- Analyze_Function_Return --
426 -----------------------------
428 procedure Analyze_Function_Return (N : Node_Id) is
429 Loc : constant Source_Ptr := Sloc (N);
430 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
431 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
433 R_Type : constant Entity_Id := Etype (Scope_Id);
434 -- Function result subtype
436 procedure Check_Limited_Return (Expr : Node_Id);
437 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
438 -- limited types. Used only for simple return statements.
439 -- Expr is the expression returned.
441 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
442 -- Check that the return_subtype_indication properly matches the result
443 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
445 --------------------------
446 -- Check_Limited_Return --
447 --------------------------
449 procedure Check_Limited_Return (Expr : Node_Id) is
451 -- Ada 2005 (AI-318-02): Return-by-reference types have been
452 -- removed and replaced by anonymous access results. This is an
453 -- incompatibility with Ada 95. Not clear whether this should be
454 -- enforced yet or perhaps controllable with special switch. ???
456 if Is_Limited_Type (R_Type)
457 and then Comes_From_Source (N)
458 and then not In_Instance_Body
459 and then not OK_For_Limited_Init_In_05 (Expr)
463 if Ada_Version >= Ada_05
464 and then not Debug_Flag_Dot_L
465 and then not GNAT_Mode
468 ("(Ada 2005) cannot copy object of a limited type " &
469 "(RM-2005 6.5(5.5/2))", Expr);
470 if Is_Inherently_Limited_Type (R_Type) then
472 ("\return by reference not permitted in Ada 2005", Expr);
475 -- Warn in Ada 95 mode, to give folks a heads up about this
478 -- In GNAT mode, this is just a warning, to allow it to be
479 -- evilly turned off. Otherwise it is a real error.
481 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
482 if Is_Inherently_Limited_Type (R_Type) then
484 ("return by reference not permitted in Ada 2005 " &
485 "(RM-2005 6.5(5.5/2))?", Expr);
488 ("cannot copy object of a limited type in Ada 2005 " &
489 "(RM-2005 6.5(5.5/2))?", Expr);
492 -- Ada 95 mode, compatibility warnings disabled
495 return; -- skip continuation messages below
499 ("\consider switching to return of access type", Expr);
500 Explain_Limited_Type (R_Type, Expr);
502 end Check_Limited_Return;
504 -------------------------------------
505 -- Check_Return_Subtype_Indication --
506 -------------------------------------
508 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
509 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
510 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
511 -- Subtype given in the extended return statement;
512 -- this must match R_Type.
514 Subtype_Ind : constant Node_Id :=
515 Object_Definition (Original_Node (Obj_Decl));
517 R_Type_Is_Anon_Access :
519 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
521 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
523 Ekind (R_Type) = E_Anonymous_Access_Type;
524 -- True if return type of the function is an anonymous access type
525 -- Can't we make Is_Anonymous_Access_Type in einfo ???
527 R_Stm_Type_Is_Anon_Access :
529 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
531 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
533 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
534 -- True if type of the return object is an anonymous access type
537 -- First, avoid cascade errors:
539 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
543 -- "return access T" case; check that the return statement also has
544 -- "access T", and that the subtypes statically match:
546 if R_Type_Is_Anon_Access then
547 if R_Stm_Type_Is_Anon_Access then
548 if Base_Type (Designated_Type (R_Stm_Type)) /=
549 Base_Type (Designated_Type (R_Type))
550 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
553 ("subtype must statically match function result subtype",
554 Subtype_Mark (Subtype_Ind));
558 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
561 -- Subtype_indication case; check that the types are the same, and
562 -- statically match if appropriate. A null exclusion may be present
563 -- on the return type, on the function specification, on the object
564 -- declaration or on the subtype itself.
566 elsif Base_Type (R_Stm_Type) = Base_Type (R_Type) then
567 if Is_Access_Type (R_Type)
569 (Can_Never_Be_Null (R_Type)
570 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
571 Can_Never_Be_Null (R_Stm_Type)
574 ("subtype must statically match function result subtype",
578 if Is_Constrained (R_Type) then
579 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
581 ("subtype must statically match function result subtype",
586 -- If the function's result type doesn't match the return object
587 -- entity's type, then we check for the case where the result type
588 -- is class-wide, and allow the declaration if the type of the object
589 -- definition matches the class-wide type. This prevents rejection
590 -- in the case where the object declaration is initialized by a call
591 -- to a build-in-place function with a specific result type and the
592 -- object entity had its type changed to that specific type. (Note
593 -- that the ARG believes that return objects should be allowed to
594 -- have a type covered by a class-wide result type in any case, so
595 -- once that relaxation is made (see AI05-32), the above check for
596 -- type compatibility should be changed to test Covers rather than
597 -- equality, and then the following special test will no longer be
600 elsif Is_Class_Wide_Type (R_Type)
602 R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
608 ("wrong type for return_subtype_indication", Subtype_Ind);
610 end Check_Return_Subtype_Indication;
612 ---------------------
613 -- Local Variables --
614 ---------------------
618 -- Start of processing for Analyze_Function_Return
621 Set_Return_Present (Scope_Id);
623 if Nkind (N) = N_Simple_Return_Statement then
624 Expr := Expression (N);
625 Analyze_And_Resolve (Expr, R_Type);
626 Check_Limited_Return (Expr);
629 -- Analyze parts specific to extended_return_statement:
632 Obj_Decl : constant Node_Id :=
633 Last (Return_Object_Declarations (N));
635 HSS : constant Node_Id := Handled_Statement_Sequence (N);
638 Expr := Expression (Obj_Decl);
640 -- Note: The check for OK_For_Limited_Init will happen in
641 -- Analyze_Object_Declaration; we treat it as a normal
642 -- object declaration.
646 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
647 Check_Return_Subtype_Indication (Obj_Decl);
649 if Present (HSS) then
652 if Present (Exception_Handlers (HSS)) then
654 -- ???Has_Nested_Block_With_Handler needs to be set.
655 -- Probably by creating an actual N_Block_Statement.
656 -- Probably in Expand.
662 Check_References (Stm_Entity);
666 -- Case of Expr present
670 -- Defend against previous errors
672 and then Nkind (Expr) /= N_Empty
673 and then Present (Etype (Expr))
675 -- Apply constraint check. Note that this is done before the implicit
676 -- conversion of the expression done for anonymous access types to
677 -- ensure correct generation of the null-excluding check associated
678 -- with null-excluding expressions found in return statements.
680 Apply_Constraint_Check (Expr, R_Type);
682 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
683 -- type, apply an implicit conversion of the expression to that type
684 -- to force appropriate static and run-time accessibility checks.
686 if Ada_Version >= Ada_05
687 and then Ekind (R_Type) = E_Anonymous_Access_Type
689 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
690 Analyze_And_Resolve (Expr, R_Type);
693 -- If the result type is class-wide, then check that the return
694 -- expression's type is not declared at a deeper level than the
695 -- function (RM05-6.5(5.6/2)).
697 if Ada_Version >= Ada_05
698 and then Is_Class_Wide_Type (R_Type)
700 if Type_Access_Level (Etype (Expr)) >
701 Subprogram_Access_Level (Scope_Id)
704 ("level of return expression type is deeper than " &
705 "class-wide function!", Expr);
709 if (Is_Class_Wide_Type (Etype (Expr))
710 or else Is_Dynamically_Tagged (Expr))
711 and then not Is_Class_Wide_Type (R_Type)
714 ("dynamically tagged expression not allowed!", Expr);
717 -- ??? A real run-time accessibility check is needed in cases
718 -- involving dereferences of access parameters. For now we just
719 -- check the static cases.
721 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
722 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
723 and then Object_Access_Level (Expr) >
724 Subprogram_Access_Level (Scope_Id)
727 Make_Raise_Program_Error (Loc,
728 Reason => PE_Accessibility_Check_Failed));
732 ("cannot return a local value by reference?", N);
734 ("\& will be raised at run time?",
735 N, Standard_Program_Error);
739 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
740 and then Null_Exclusion_Present (Parent (Scope_Id))
742 Apply_Compile_Time_Constraint_Error
744 Msg => "(Ada 2005) null not allowed for "
745 & "null-excluding return?",
746 Reason => CE_Null_Not_Allowed);
749 end Analyze_Function_Return;
751 -------------------------------------
752 -- Analyze_Generic_Subprogram_Body --
753 -------------------------------------
755 procedure Analyze_Generic_Subprogram_Body
759 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
760 Kind : constant Entity_Kind := Ekind (Gen_Id);
766 -- Copy body and disable expansion while analyzing the generic For a
767 -- stub, do not copy the stub (which would load the proper body), this
768 -- will be done when the proper body is analyzed.
770 if Nkind (N) /= N_Subprogram_Body_Stub then
771 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
776 Spec := Specification (N);
778 -- Within the body of the generic, the subprogram is callable, and
779 -- behaves like the corresponding non-generic unit.
781 Body_Id := Defining_Entity (Spec);
783 if Kind = E_Generic_Procedure
784 and then Nkind (Spec) /= N_Procedure_Specification
786 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
789 elsif Kind = E_Generic_Function
790 and then Nkind (Spec) /= N_Function_Specification
792 Error_Msg_N ("invalid body for generic function ", Body_Id);
796 Set_Corresponding_Body (Gen_Decl, Body_Id);
798 if Has_Completion (Gen_Id)
799 and then Nkind (Parent (N)) /= N_Subunit
801 Error_Msg_N ("duplicate generic body", N);
804 Set_Has_Completion (Gen_Id);
807 if Nkind (N) = N_Subprogram_Body_Stub then
808 Set_Ekind (Defining_Entity (Specification (N)), Kind);
810 Set_Corresponding_Spec (N, Gen_Id);
813 if Nkind (Parent (N)) = N_Compilation_Unit then
814 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
817 -- Make generic parameters immediately visible in the body. They are
818 -- needed to process the formals declarations. Then make the formals
819 -- visible in a separate step.
825 First_Ent : Entity_Id;
828 First_Ent := First_Entity (Gen_Id);
831 while Present (E) and then not Is_Formal (E) loop
836 Set_Use (Generic_Formal_Declarations (Gen_Decl));
838 -- Now generic formals are visible, and the specification can be
839 -- analyzed, for subsequent conformance check.
841 Body_Id := Analyze_Subprogram_Specification (Spec);
843 -- Make formal parameters visible
847 -- E is the first formal parameter, we loop through the formals
848 -- installing them so that they will be visible.
850 Set_First_Entity (Gen_Id, E);
851 while Present (E) loop
857 -- Visible generic entity is callable within its own body
859 Set_Ekind (Gen_Id, Ekind (Body_Id));
860 Set_Ekind (Body_Id, E_Subprogram_Body);
861 Set_Convention (Body_Id, Convention (Gen_Id));
862 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
863 Set_Scope (Body_Id, Scope (Gen_Id));
864 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
866 if Nkind (N) = N_Subprogram_Body_Stub then
868 -- No body to analyze, so restore state of generic unit
870 Set_Ekind (Gen_Id, Kind);
871 Set_Ekind (Body_Id, Kind);
873 if Present (First_Ent) then
874 Set_First_Entity (Gen_Id, First_Ent);
881 -- If this is a compilation unit, it must be made visible explicitly,
882 -- because the compilation of the declaration, unlike other library
883 -- unit declarations, does not. If it is not a unit, the following
884 -- is redundant but harmless.
886 Set_Is_Immediately_Visible (Gen_Id);
887 Reference_Body_Formals (Gen_Id, Body_Id);
889 if Is_Child_Unit (Gen_Id) then
890 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
893 Set_Actual_Subtypes (N, Current_Scope);
894 Process_PPCs (N, Gen_Id, Body_Id);
896 -- If the generic unit carries pre- or post-conditions, copy them
897 -- to the original generic tree, so that they are properly added
898 -- to any instantiation.
901 Orig : constant Node_Id := Original_Node (N);
905 Cond := First (Declarations (N));
906 while Present (Cond) loop
907 if Nkind (Cond) = N_Pragma
908 and then Pragma_Name (Cond) = Name_Check
910 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
912 elsif Nkind (Cond) = N_Pragma
913 and then Pragma_Name (Cond) = Name_Postcondition
915 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
916 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
925 Analyze_Declarations (Declarations (N));
927 Analyze (Handled_Statement_Sequence (N));
929 Save_Global_References (Original_Node (N));
931 -- Prior to exiting the scope, include generic formals again (if any
932 -- are present) in the set of local entities.
934 if Present (First_Ent) then
935 Set_First_Entity (Gen_Id, First_Ent);
938 Check_References (Gen_Id);
941 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
943 Check_Subprogram_Order (N);
945 -- Outside of its body, unit is generic again
947 Set_Ekind (Gen_Id, Kind);
948 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
951 Style.Check_Identifier (Body_Id, Gen_Id);
954 end Analyze_Generic_Subprogram_Body;
956 -----------------------------
957 -- Analyze_Operator_Symbol --
958 -----------------------------
960 -- An operator symbol such as "+" or "and" may appear in context where the
961 -- literal denotes an entity name, such as "+"(x, y) or in context when it
962 -- is just a string, as in (conjunction = "or"). In these cases the parser
963 -- generates this node, and the semantics does the disambiguation. Other
964 -- such case are actuals in an instantiation, the generic unit in an
965 -- instantiation, and pragma arguments.
967 procedure Analyze_Operator_Symbol (N : Node_Id) is
968 Par : constant Node_Id := Parent (N);
971 if (Nkind (Par) = N_Function_Call
972 and then N = Name (Par))
973 or else Nkind (Par) = N_Function_Instantiation
974 or else (Nkind (Par) = N_Indexed_Component
975 and then N = Prefix (Par))
976 or else (Nkind (Par) = N_Pragma_Argument_Association
977 and then not Is_Pragma_String_Literal (Par))
978 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
979 or else (Nkind (Par) = N_Attribute_Reference
980 and then Attribute_Name (Par) /= Name_Value)
982 Find_Direct_Name (N);
985 Change_Operator_Symbol_To_String_Literal (N);
988 end Analyze_Operator_Symbol;
990 -----------------------------------
991 -- Analyze_Parameter_Association --
992 -----------------------------------
994 procedure Analyze_Parameter_Association (N : Node_Id) is
996 Analyze (Explicit_Actual_Parameter (N));
997 end Analyze_Parameter_Association;
999 ----------------------------
1000 -- Analyze_Procedure_Call --
1001 ----------------------------
1003 procedure Analyze_Procedure_Call (N : Node_Id) is
1004 Loc : constant Source_Ptr := Sloc (N);
1005 P : constant Node_Id := Name (N);
1006 Actuals : constant List_Id := Parameter_Associations (N);
1010 procedure Analyze_Call_And_Resolve;
1011 -- Do Analyze and Resolve calls for procedure call
1013 ------------------------------
1014 -- Analyze_Call_And_Resolve --
1015 ------------------------------
1017 procedure Analyze_Call_And_Resolve is
1019 if Nkind (N) = N_Procedure_Call_Statement then
1021 Resolve (N, Standard_Void_Type);
1025 end Analyze_Call_And_Resolve;
1027 -- Start of processing for Analyze_Procedure_Call
1030 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1031 -- a procedure call or an entry call. The prefix may denote an access
1032 -- to subprogram type, in which case an implicit dereference applies.
1033 -- If the prefix is an indexed component (without implicit dereference)
1034 -- then the construct denotes a call to a member of an entire family.
1035 -- If the prefix is a simple name, it may still denote a call to a
1036 -- parameterless member of an entry family. Resolution of these various
1037 -- interpretations is delicate.
1041 -- If this is a call of the form Obj.Op, the call may have been
1042 -- analyzed and possibly rewritten into a block, in which case
1045 if Analyzed (N) then
1049 -- If error analyzing prefix, then set Any_Type as result and return
1051 if Etype (P) = Any_Type then
1052 Set_Etype (N, Any_Type);
1056 -- Otherwise analyze the parameters
1058 if Present (Actuals) then
1059 Actual := First (Actuals);
1061 while Present (Actual) loop
1063 Check_Parameterless_Call (Actual);
1068 -- Special processing for Elab_Spec and Elab_Body calls
1070 if Nkind (P) = N_Attribute_Reference
1071 and then (Attribute_Name (P) = Name_Elab_Spec
1072 or else Attribute_Name (P) = Name_Elab_Body)
1074 if Present (Actuals) then
1076 ("no parameters allowed for this call", First (Actuals));
1080 Set_Etype (N, Standard_Void_Type);
1083 elsif Is_Entity_Name (P)
1084 and then Is_Record_Type (Etype (Entity (P)))
1085 and then Remote_AST_I_Dereference (P)
1089 elsif Is_Entity_Name (P)
1090 and then Ekind (Entity (P)) /= E_Entry_Family
1092 if Is_Access_Type (Etype (P))
1093 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1094 and then No (Actuals)
1095 and then Comes_From_Source (N)
1097 Error_Msg_N ("missing explicit dereference in call", N);
1100 Analyze_Call_And_Resolve;
1102 -- If the prefix is the simple name of an entry family, this is
1103 -- a parameterless call from within the task body itself.
1105 elsif Is_Entity_Name (P)
1106 and then Nkind (P) = N_Identifier
1107 and then Ekind (Entity (P)) = E_Entry_Family
1108 and then Present (Actuals)
1109 and then No (Next (First (Actuals)))
1111 -- Can be call to parameterless entry family. What appears to be the
1112 -- sole argument is in fact the entry index. Rewrite prefix of node
1113 -- accordingly. Source representation is unchanged by this
1117 Make_Indexed_Component (Loc,
1119 Make_Selected_Component (Loc,
1120 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1121 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1122 Expressions => Actuals);
1123 Set_Name (N, New_N);
1124 Set_Etype (New_N, Standard_Void_Type);
1125 Set_Parameter_Associations (N, No_List);
1126 Analyze_Call_And_Resolve;
1128 elsif Nkind (P) = N_Explicit_Dereference then
1129 if Ekind (Etype (P)) = E_Subprogram_Type then
1130 Analyze_Call_And_Resolve;
1132 Error_Msg_N ("expect access to procedure in call", P);
1135 -- The name can be a selected component or an indexed component that
1136 -- yields an access to subprogram. Such a prefix is legal if the call
1137 -- has parameter associations.
1139 elsif Is_Access_Type (Etype (P))
1140 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1142 if Present (Actuals) then
1143 Analyze_Call_And_Resolve;
1145 Error_Msg_N ("missing explicit dereference in call ", N);
1148 -- If not an access to subprogram, then the prefix must resolve to the
1149 -- name of an entry, entry family, or protected operation.
1151 -- For the case of a simple entry call, P is a selected component where
1152 -- the prefix is the task and the selector name is the entry. A call to
1153 -- a protected procedure will have the same syntax. If the protected
1154 -- object contains overloaded operations, the entity may appear as a
1155 -- function, the context will select the operation whose type is Void.
1157 elsif Nkind (P) = N_Selected_Component
1158 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1160 Ekind (Entity (Selector_Name (P))) = E_Procedure
1162 Ekind (Entity (Selector_Name (P))) = E_Function)
1164 Analyze_Call_And_Resolve;
1166 elsif Nkind (P) = N_Selected_Component
1167 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1168 and then Present (Actuals)
1169 and then No (Next (First (Actuals)))
1171 -- Can be call to parameterless entry family. What appears to be the
1172 -- sole argument is in fact the entry index. Rewrite prefix of node
1173 -- accordingly. Source representation is unchanged by this
1177 Make_Indexed_Component (Loc,
1178 Prefix => New_Copy (P),
1179 Expressions => Actuals);
1180 Set_Name (N, New_N);
1181 Set_Etype (New_N, Standard_Void_Type);
1182 Set_Parameter_Associations (N, No_List);
1183 Analyze_Call_And_Resolve;
1185 -- For the case of a reference to an element of an entry family, P is
1186 -- an indexed component whose prefix is a selected component (task and
1187 -- entry family), and whose index is the entry family index.
1189 elsif Nkind (P) = N_Indexed_Component
1190 and then Nkind (Prefix (P)) = N_Selected_Component
1191 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1193 Analyze_Call_And_Resolve;
1195 -- If the prefix is the name of an entry family, it is a call from
1196 -- within the task body itself.
1198 elsif Nkind (P) = N_Indexed_Component
1199 and then Nkind (Prefix (P)) = N_Identifier
1200 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1203 Make_Selected_Component (Loc,
1204 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1205 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1206 Rewrite (Prefix (P), New_N);
1208 Analyze_Call_And_Resolve;
1210 -- Anything else is an error
1213 Error_Msg_N ("invalid procedure or entry call", N);
1215 end Analyze_Procedure_Call;
1217 -------------------------------------
1218 -- Analyze_Simple_Return_Statement --
1219 -------------------------------------
1221 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1223 if Present (Expression (N)) then
1224 Mark_Coextensions (N, Expression (N));
1227 Analyze_Return_Statement (N);
1228 end Analyze_Simple_Return_Statement;
1230 -------------------------
1231 -- Analyze_Return_Type --
1232 -------------------------
1234 procedure Analyze_Return_Type (N : Node_Id) is
1235 Designator : constant Entity_Id := Defining_Entity (N);
1236 Typ : Entity_Id := Empty;
1239 -- Normal case where result definition does not indicate an error
1241 if Result_Definition (N) /= Error then
1242 if Nkind (Result_Definition (N)) = N_Access_Definition then
1243 Typ := Access_Definition (N, Result_Definition (N));
1244 Set_Parent (Typ, Result_Definition (N));
1245 Set_Is_Local_Anonymous_Access (Typ);
1246 Set_Etype (Designator, Typ);
1248 -- Subtype_Mark case
1251 Find_Type (Result_Definition (N));
1252 Typ := Entity (Result_Definition (N));
1253 Set_Etype (Designator, Typ);
1255 if Ekind (Typ) = E_Incomplete_Type
1256 and then Is_Value_Type (Typ)
1260 elsif Ekind (Typ) = E_Incomplete_Type
1261 or else (Is_Class_Wide_Type (Typ)
1263 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1266 ("invalid use of incomplete type", Result_Definition (N));
1270 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1272 Null_Exclusion_Static_Checks (N);
1274 -- Case where result definition does indicate an error
1277 Set_Etype (Designator, Any_Type);
1279 end Analyze_Return_Type;
1281 -----------------------------
1282 -- Analyze_Subprogram_Body --
1283 -----------------------------
1285 -- This procedure is called for regular subprogram bodies, generic bodies,
1286 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1287 -- specification matters, and is used to create a proper declaration for
1288 -- the subprogram, or to perform conformance checks.
1290 procedure Analyze_Subprogram_Body (N : Node_Id) is
1291 Loc : constant Source_Ptr := Sloc (N);
1292 Body_Deleted : constant Boolean := False;
1293 Body_Spec : constant Node_Id := Specification (N);
1294 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1295 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1296 Conformant : Boolean;
1298 Missing_Ret : Boolean;
1300 Prot_Typ : Entity_Id := Empty;
1301 Spec_Id : Entity_Id;
1302 Spec_Decl : Node_Id := Empty;
1304 Last_Real_Spec_Entity : Entity_Id := Empty;
1305 -- When we analyze a separate spec, the entity chain ends up containing
1306 -- the formals, as well as any itypes generated during analysis of the
1307 -- default expressions for parameters, or the arguments of associated
1308 -- precondition/postcondition pragmas (which are analyzed in the context
1309 -- of the spec since they have visibility on formals).
1311 -- These entities belong with the spec and not the body. However we do
1312 -- the analysis of the body in the context of the spec (again to obtain
1313 -- visibility to the formals), and all the entities generated during
1314 -- this analysis end up also chained to the entity chain of the spec.
1315 -- But they really belong to the body, and there is circuitry to move
1316 -- them from the spec to the body.
1318 -- However, when we do this move, we don't want to move the real spec
1319 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1320 -- variable points to the last real spec entity, so we only move those
1321 -- chained beyond that point. It is initialized to Empty to deal with
1322 -- the case where there is no separate spec.
1324 procedure Check_Anonymous_Return;
1325 -- (Ada 2005): if a function returns an access type that denotes a task,
1326 -- or a type that contains tasks, we must create a master entity for
1327 -- the anonymous type, which typically will be used in an allocator
1328 -- in the body of the function.
1330 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1331 -- Look ahead to recognize a pragma that may appear after the body.
1332 -- If there is a previous spec, check that it appears in the same
1333 -- declarative part. If the pragma is Inline_Always, perform inlining
1334 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1335 -- If the body acts as a spec, and inlining is required, we create a
1336 -- subprogram declaration for it, in order to attach the body to inline.
1337 -- If pragma does not appear after the body, check whether there is
1338 -- an inline pragma before any local declarations.
1340 function Disambiguate_Spec return Entity_Id;
1341 -- When a primitive is declared between the private view and the full
1342 -- view of a concurrent type which implements an interface, a special
1343 -- mechanism is used to find the corresponding spec of the primitive
1346 function Is_Private_Concurrent_Primitive
1347 (Subp_Id : Entity_Id) return Boolean;
1348 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1349 -- type that implements an interface and has a private view.
1351 procedure Set_Trivial_Subprogram (N : Node_Id);
1352 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1353 -- subprogram whose body is being analyzed. N is the statement node
1354 -- causing the flag to be set, if the following statement is a return
1355 -- of an entity, we mark the entity as set in source to suppress any
1356 -- warning on the stylized use of function stubs with a dummy return.
1358 procedure Verify_Overriding_Indicator;
1359 -- If there was a previous spec, the entity has been entered in the
1360 -- current scope previously. If the body itself carries an overriding
1361 -- indicator, check that it is consistent with the known status of the
1364 ----------------------------
1365 -- Check_Anonymous_Return --
1366 ----------------------------
1368 procedure Check_Anonymous_Return is
1373 if Present (Spec_Id) then
1379 if Ekind (Scop) = E_Function
1380 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1381 and then Has_Task (Designated_Type (Etype (Scop)))
1382 and then Expander_Active
1385 Make_Object_Declaration (Loc,
1386 Defining_Identifier =>
1387 Make_Defining_Identifier (Loc, Name_uMaster),
1388 Constant_Present => True,
1389 Object_Definition =>
1390 New_Reference_To (RTE (RE_Master_Id), Loc),
1392 Make_Explicit_Dereference (Loc,
1393 New_Reference_To (RTE (RE_Current_Master), Loc)));
1395 if Present (Declarations (N)) then
1396 Prepend (Decl, Declarations (N));
1398 Set_Declarations (N, New_List (Decl));
1401 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1402 Set_Has_Master_Entity (Scop);
1404 end Check_Anonymous_Return;
1406 -------------------------
1407 -- Check_Inline_Pragma --
1408 -------------------------
1410 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1414 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1415 -- True when N is a pragma Inline or Inline_Always that applies
1416 -- to this subprogram.
1418 -----------------------
1419 -- Is_Inline_Pragma --
1420 -----------------------
1422 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1425 Nkind (N) = N_Pragma
1427 (Pragma_Name (N) = Name_Inline_Always
1430 and then Pragma_Name (N) = Name_Inline))
1433 (Expression (First (Pragma_Argument_Associations (N))))
1435 end Is_Inline_Pragma;
1437 -- Start of processing for Check_Inline_Pragma
1440 if not Expander_Active then
1444 if Is_List_Member (N)
1445 and then Present (Next (N))
1446 and then Is_Inline_Pragma (Next (N))
1450 elsif Nkind (N) /= N_Subprogram_Body_Stub
1451 and then Present (Declarations (N))
1452 and then Is_Inline_Pragma (First (Declarations (N)))
1454 Prag := First (Declarations (N));
1460 if Present (Prag) then
1461 if Present (Spec_Id) then
1462 if List_Containing (N) =
1463 List_Containing (Unit_Declaration_Node (Spec_Id))
1469 -- Create a subprogram declaration, to make treatment uniform
1472 Subp : constant Entity_Id :=
1473 Make_Defining_Identifier (Loc, Chars (Body_Id));
1474 Decl : constant Node_Id :=
1475 Make_Subprogram_Declaration (Loc,
1476 Specification => New_Copy_Tree (Specification (N)));
1478 Set_Defining_Unit_Name (Specification (Decl), Subp);
1480 if Present (First_Formal (Body_Id)) then
1481 Plist := Copy_Parameter_List (Body_Id);
1482 Set_Parameter_Specifications
1483 (Specification (Decl), Plist);
1486 Insert_Before (N, Decl);
1489 Set_Has_Pragma_Inline (Subp);
1491 if Pragma_Name (Prag) = Name_Inline_Always then
1492 Set_Is_Inlined (Subp);
1493 Set_Has_Pragma_Inline_Always (Subp);
1500 end Check_Inline_Pragma;
1502 -----------------------
1503 -- Disambiguate_Spec --
1504 -----------------------
1506 function Disambiguate_Spec return Entity_Id is
1507 Priv_Spec : Entity_Id;
1510 procedure Replace_Types (To_Corresponding : Boolean);
1511 -- Depending on the flag, replace the type of formal parameters of
1512 -- Body_Id if it is a concurrent type implementing interfaces with
1513 -- the corresponding record type or the other way around.
1515 procedure Replace_Types (To_Corresponding : Boolean) is
1517 Formal_Typ : Entity_Id;
1520 Formal := First_Formal (Body_Id);
1521 while Present (Formal) loop
1522 Formal_Typ := Etype (Formal);
1524 -- From concurrent type to corresponding record
1526 if To_Corresponding then
1527 if Is_Concurrent_Type (Formal_Typ)
1528 and then Present (Corresponding_Record_Type (Formal_Typ))
1529 and then Present (Interfaces (
1530 Corresponding_Record_Type (Formal_Typ)))
1533 Corresponding_Record_Type (Formal_Typ));
1536 -- From corresponding record to concurrent type
1539 if Is_Concurrent_Record_Type (Formal_Typ)
1540 and then Present (Interfaces (Formal_Typ))
1543 Corresponding_Concurrent_Type (Formal_Typ));
1547 Next_Formal (Formal);
1551 -- Start of processing for Disambiguate_Spec
1554 -- Try to retrieve the specification of the body as is. All error
1555 -- messages are suppressed because the body may not have a spec in
1556 -- its current state.
1558 Spec_N := Find_Corresponding_Spec (N, False);
1560 -- It is possible that this is the body of a primitive declared
1561 -- between a private and a full view of a concurrent type. The
1562 -- controlling parameter of the spec carries the concurrent type,
1563 -- not the corresponding record type as transformed by Analyze_
1564 -- Subprogram_Specification. In such cases, we undo the change
1565 -- made by the analysis of the specification and try to find the
1570 -- Restore all references of corresponding record types to the
1571 -- original concurrent types.
1573 Replace_Types (To_Corresponding => False);
1574 Priv_Spec := Find_Corresponding_Spec (N, False);
1576 -- The current body truly belongs to a primitive declared between
1577 -- a private and a full view. We leave the modified body as is,
1578 -- and return the true spec.
1580 if Present (Priv_Spec)
1581 and then Is_Private_Primitive (Priv_Spec)
1586 -- In case that this is some sort of error, restore the original
1587 -- state of the body.
1589 Replace_Types (To_Corresponding => True);
1593 end Disambiguate_Spec;
1595 -------------------------------------
1596 -- Is_Private_Concurrent_Primitive --
1597 -------------------------------------
1599 function Is_Private_Concurrent_Primitive
1600 (Subp_Id : Entity_Id) return Boolean
1602 Formal_Typ : Entity_Id;
1605 if Present (First_Formal (Subp_Id)) then
1606 Formal_Typ := Etype (First_Formal (Subp_Id));
1608 if Is_Concurrent_Record_Type (Formal_Typ) then
1609 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1612 -- The type of the first formal is a concurrent tagged type with
1616 Is_Concurrent_Type (Formal_Typ)
1617 and then Is_Tagged_Type (Formal_Typ)
1618 and then Has_Private_Declaration (Formal_Typ);
1622 end Is_Private_Concurrent_Primitive;
1624 ----------------------------
1625 -- Set_Trivial_Subprogram --
1626 ----------------------------
1628 procedure Set_Trivial_Subprogram (N : Node_Id) is
1629 Nxt : constant Node_Id := Next (N);
1632 Set_Is_Trivial_Subprogram (Body_Id);
1634 if Present (Spec_Id) then
1635 Set_Is_Trivial_Subprogram (Spec_Id);
1639 and then Nkind (Nxt) = N_Simple_Return_Statement
1640 and then No (Next (Nxt))
1641 and then Present (Expression (Nxt))
1642 and then Is_Entity_Name (Expression (Nxt))
1644 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1646 end Set_Trivial_Subprogram;
1648 ---------------------------------
1649 -- Verify_Overriding_Indicator --
1650 ---------------------------------
1652 procedure Verify_Overriding_Indicator is
1654 if Must_Override (Body_Spec) then
1655 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1656 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1660 elsif not Is_Overriding_Operation (Spec_Id) then
1662 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1665 elsif Must_Not_Override (Body_Spec) then
1666 if Is_Overriding_Operation (Spec_Id) then
1668 ("subprogram& overrides inherited operation",
1669 Body_Spec, Spec_Id);
1671 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1672 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1675 ("subprogram & overrides predefined operator ",
1676 Body_Spec, Spec_Id);
1678 -- If this is not a primitive operation the overriding indicator
1679 -- is altogether illegal.
1681 elsif not Is_Primitive (Spec_Id) then
1682 Error_Msg_N ("overriding indicator only allowed " &
1683 "if subprogram is primitive",
1687 end Verify_Overriding_Indicator;
1689 -- Start of processing for Analyze_Subprogram_Body
1692 if Debug_Flag_C then
1693 Write_Str ("==== Compiling subprogram body ");
1694 Write_Name (Chars (Body_Id));
1695 Write_Str (" from ");
1696 Write_Location (Loc);
1700 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1702 -- Generic subprograms are handled separately. They always have a
1703 -- generic specification. Determine whether current scope has a
1704 -- previous declaration.
1706 -- If the subprogram body is defined within an instance of the same
1707 -- name, the instance appears as a package renaming, and will be hidden
1708 -- within the subprogram.
1710 if Present (Prev_Id)
1711 and then not Is_Overloadable (Prev_Id)
1712 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1713 or else Comes_From_Source (Prev_Id))
1715 if Is_Generic_Subprogram (Prev_Id) then
1717 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1718 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1720 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1724 -- Previous entity conflicts with subprogram name. Attempting to
1725 -- enter name will post error.
1727 Enter_Name (Body_Id);
1731 -- Non-generic case, find the subprogram declaration, if one was seen,
1732 -- or enter new overloaded entity in the current scope. If the
1733 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1734 -- part of the context of one of its subunits. No need to redo the
1737 elsif Prev_Id = Body_Id
1738 and then Has_Completion (Body_Id)
1743 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1745 if Nkind (N) = N_Subprogram_Body_Stub
1746 or else No (Corresponding_Spec (N))
1748 if Is_Private_Concurrent_Primitive (Body_Id) then
1749 Spec_Id := Disambiguate_Spec;
1751 Spec_Id := Find_Corresponding_Spec (N);
1754 -- If this is a duplicate body, no point in analyzing it
1756 if Error_Posted (N) then
1760 -- A subprogram body should cause freezing of its own declaration,
1761 -- but if there was no previous explicit declaration, then the
1762 -- subprogram will get frozen too late (there may be code within
1763 -- the body that depends on the subprogram having been frozen,
1764 -- such as uses of extra formals), so we force it to be frozen
1765 -- here. Same holds if the body and spec are compilation units.
1767 if No (Spec_Id) then
1768 Freeze_Before (N, Body_Id);
1770 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1771 Freeze_Before (N, Spec_Id);
1775 Spec_Id := Corresponding_Spec (N);
1779 -- Do not inline any subprogram that contains nested subprograms, since
1780 -- the backend inlining circuit seems to generate uninitialized
1781 -- references in this case. We know this happens in the case of front
1782 -- end ZCX support, but it also appears it can happen in other cases as
1783 -- well. The backend often rejects attempts to inline in the case of
1784 -- nested procedures anyway, so little if anything is lost by this.
1785 -- Note that this is test is for the benefit of the back-end. There is
1786 -- a separate test for front-end inlining that also rejects nested
1789 -- Do not do this test if errors have been detected, because in some
1790 -- error cases, this code blows up, and we don't need it anyway if
1791 -- there have been errors, since we won't get to the linker anyway.
1793 if Comes_From_Source (Body_Id)
1794 and then Serious_Errors_Detected = 0
1798 P_Ent := Scope (P_Ent);
1799 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1801 if Is_Subprogram (P_Ent) then
1802 Set_Is_Inlined (P_Ent, False);
1804 if Comes_From_Source (P_Ent)
1805 and then Has_Pragma_Inline (P_Ent)
1808 ("cannot inline& (nested subprogram)?",
1815 Check_Inline_Pragma (Spec_Id);
1817 -- Case of fully private operation in the body of the protected type.
1818 -- We must create a declaration for the subprogram, in order to attach
1819 -- the protected subprogram that will be used in internal calls.
1822 and then Comes_From_Source (N)
1823 and then Is_Protected_Type (Current_Scope)
1832 Formal := First_Formal (Body_Id);
1834 -- The protected operation always has at least one formal, namely
1835 -- the object itself, but it is only placed in the parameter list
1836 -- if expansion is enabled.
1839 or else Expander_Active
1841 Plist := Copy_Parameter_List (Body_Id);
1846 if Nkind (Body_Spec) = N_Procedure_Specification then
1848 Make_Procedure_Specification (Loc,
1849 Defining_Unit_Name =>
1850 Make_Defining_Identifier (Sloc (Body_Id),
1851 Chars => Chars (Body_Id)),
1852 Parameter_Specifications => Plist);
1855 Make_Function_Specification (Loc,
1856 Defining_Unit_Name =>
1857 Make_Defining_Identifier (Sloc (Body_Id),
1858 Chars => Chars (Body_Id)),
1859 Parameter_Specifications => Plist,
1860 Result_Definition =>
1861 New_Occurrence_Of (Etype (Body_Id), Loc));
1865 Make_Subprogram_Declaration (Loc,
1866 Specification => New_Spec);
1867 Insert_Before (N, Decl);
1868 Spec_Id := Defining_Unit_Name (New_Spec);
1870 -- Indicate that the entity comes from source, to ensure that
1871 -- cross-reference information is properly generated. The body
1872 -- itself is rewritten during expansion, and the body entity will
1873 -- not appear in calls to the operation.
1875 Set_Comes_From_Source (Spec_Id, True);
1877 Set_Has_Completion (Spec_Id);
1878 Set_Convention (Spec_Id, Convention_Protected);
1881 elsif Present (Spec_Id) then
1882 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1883 Verify_Overriding_Indicator;
1885 -- In general, the spec will be frozen when we start analyzing the
1886 -- body. However, for internally generated operations, such as
1887 -- wrapper functions for inherited operations with controlling
1888 -- results, the spec may not have been frozen by the time we
1889 -- expand the freeze actions that include the bodies. In particular,
1890 -- extra formals for accessibility or for return-in-place may need
1891 -- to be generated. Freeze nodes, if any, are inserted before the
1894 if not Is_Frozen (Spec_Id)
1895 and then Expander_Active
1897 -- Force the generation of its freezing node to ensure proper
1898 -- management of access types in the backend.
1900 -- This is definitely needed for some cases, but it is not clear
1901 -- why, to be investigated further???
1903 Set_Has_Delayed_Freeze (Spec_Id);
1904 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
1908 if Chars (Body_Id) = Name_uPostconditions then
1909 Set_Has_Postconditions (Current_Scope);
1912 -- Place subprogram on scope stack, and make formals visible. If there
1913 -- is a spec, the visible entity remains that of the spec.
1915 if Present (Spec_Id) then
1916 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1918 if Is_Child_Unit (Spec_Id) then
1919 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1923 Style.Check_Identifier (Body_Id, Spec_Id);
1926 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1927 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1929 if Is_Abstract_Subprogram (Spec_Id) then
1930 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1934 Set_Convention (Body_Id, Convention (Spec_Id));
1935 Set_Has_Completion (Spec_Id);
1937 if Is_Protected_Type (Scope (Spec_Id)) then
1938 Prot_Typ := Scope (Spec_Id);
1941 -- If this is a body generated for a renaming, do not check for
1942 -- full conformance. The check is redundant, because the spec of
1943 -- the body is a copy of the spec in the renaming declaration,
1944 -- and the test can lead to spurious errors on nested defaults.
1946 if Present (Spec_Decl)
1947 and then not Comes_From_Source (N)
1949 (Nkind (Original_Node (Spec_Decl)) =
1950 N_Subprogram_Renaming_Declaration
1951 or else (Present (Corresponding_Body (Spec_Decl))
1953 Nkind (Unit_Declaration_Node
1954 (Corresponding_Body (Spec_Decl))) =
1955 N_Subprogram_Renaming_Declaration))
1962 Fully_Conformant, True, Conformant, Body_Id);
1965 -- If the body is not fully conformant, we have to decide if we
1966 -- should analyze it or not. If it has a really messed up profile
1967 -- then we probably should not analyze it, since we will get too
1968 -- many bogus messages.
1970 -- Our decision is to go ahead in the non-fully conformant case
1971 -- only if it is at least mode conformant with the spec. Note
1972 -- that the call to Check_Fully_Conformant has issued the proper
1973 -- error messages to complain about the lack of conformance.
1976 and then not Mode_Conformant (Body_Id, Spec_Id)
1982 if Spec_Id /= Body_Id then
1983 Reference_Body_Formals (Spec_Id, Body_Id);
1986 if Nkind (N) /= N_Subprogram_Body_Stub then
1987 Set_Corresponding_Spec (N, Spec_Id);
1989 -- Ada 2005 (AI-345): If the operation is a primitive operation
1990 -- of a concurrent type, the type of the first parameter has been
1991 -- replaced with the corresponding record, which is the proper
1992 -- run-time structure to use. However, within the body there may
1993 -- be uses of the formals that depend on primitive operations
1994 -- of the type (in particular calls in prefixed form) for which
1995 -- we need the original concurrent type. The operation may have
1996 -- several controlling formals, so the replacement must be done
1999 if Comes_From_Source (Spec_Id)
2000 and then Present (First_Entity (Spec_Id))
2001 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2002 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2004 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2007 (Corresponding_Concurrent_Type
2008 (Etype (First_Entity (Spec_Id))))
2011 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2015 Form := First_Formal (Spec_Id);
2016 while Present (Form) loop
2017 if Etype (Form) = Typ then
2018 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2026 -- Make the formals visible, and place subprogram on scope stack.
2027 -- This is also the point at which we set Last_Real_Spec_Entity
2028 -- to mark the entities which will not be moved to the body.
2030 Install_Formals (Spec_Id);
2031 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2032 Push_Scope (Spec_Id);
2034 -- Make sure that the subprogram is immediately visible. For
2035 -- child units that have no separate spec this is indispensable.
2036 -- Otherwise it is safe albeit redundant.
2038 Set_Is_Immediately_Visible (Spec_Id);
2041 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2042 Set_Ekind (Body_Id, E_Subprogram_Body);
2043 Set_Scope (Body_Id, Scope (Spec_Id));
2044 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2046 -- Case of subprogram body with no previous spec
2050 and then Comes_From_Source (Body_Id)
2051 and then not Suppress_Style_Checks (Body_Id)
2052 and then not In_Instance
2054 Style.Body_With_No_Spec (N);
2057 New_Overloaded_Entity (Body_Id);
2059 if Nkind (N) /= N_Subprogram_Body_Stub then
2060 Set_Acts_As_Spec (N);
2061 Generate_Definition (Body_Id);
2063 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2064 Generate_Reference_To_Formals (Body_Id);
2065 Install_Formals (Body_Id);
2066 Push_Scope (Body_Id);
2070 -- If the return type is an anonymous access type whose designated type
2071 -- is the limited view of a class-wide type and the non-limited view is
2072 -- available, update the return type accordingly.
2074 if Ada_Version >= Ada_05
2075 and then Comes_From_Source (N)
2082 Rtyp := Etype (Current_Scope);
2084 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2085 Etyp := Directly_Designated_Type (Rtyp);
2087 if Is_Class_Wide_Type (Etyp)
2088 and then From_With_Type (Etyp)
2090 Set_Directly_Designated_Type
2091 (Etype (Current_Scope), Available_View (Etyp));
2097 -- If this is the proper body of a stub, we must verify that the stub
2098 -- conforms to the body, and to the previous spec if one was present.
2099 -- we know already that the body conforms to that spec. This test is
2100 -- only required for subprograms that come from source.
2102 if Nkind (Parent (N)) = N_Subunit
2103 and then Comes_From_Source (N)
2104 and then not Error_Posted (Body_Id)
2105 and then Nkind (Corresponding_Stub (Parent (N))) =
2106 N_Subprogram_Body_Stub
2109 Old_Id : constant Entity_Id :=
2111 (Specification (Corresponding_Stub (Parent (N))));
2113 Conformant : Boolean := False;
2116 if No (Spec_Id) then
2117 Check_Fully_Conformant (Body_Id, Old_Id);
2121 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2123 if not Conformant then
2125 -- The stub was taken to be a new declaration. Indicate
2126 -- that it lacks a body.
2128 Set_Has_Completion (Old_Id, False);
2134 Set_Has_Completion (Body_Id);
2135 Check_Eliminated (Body_Id);
2137 if Nkind (N) = N_Subprogram_Body_Stub then
2140 elsif Present (Spec_Id)
2141 and then Expander_Active
2143 (Has_Pragma_Inline_Always (Spec_Id)
2144 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2146 Build_Body_To_Inline (N, Spec_Id);
2149 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2150 -- if its specification we have to install the private withed units.
2151 -- This holds for child units as well.
2153 if Is_Compilation_Unit (Body_Id)
2154 or else Nkind (Parent (N)) = N_Compilation_Unit
2156 Install_Private_With_Clauses (Body_Id);
2159 Check_Anonymous_Return;
2161 -- Set the Protected_Formal field of each extra formal of the protected
2162 -- subprogram to reference the corresponding extra formal of the
2163 -- subprogram that implements it. For regular formals this occurs when
2164 -- the protected subprogram's declaration is expanded, but the extra
2165 -- formals don't get created until the subprogram is frozen. We need to
2166 -- do this before analyzing the protected subprogram's body so that any
2167 -- references to the original subprogram's extra formals will be changed
2168 -- refer to the implementing subprogram's formals (see Expand_Formal).
2170 if Present (Spec_Id)
2171 and then Is_Protected_Type (Scope (Spec_Id))
2172 and then Present (Protected_Body_Subprogram (Spec_Id))
2175 Impl_Subp : constant Entity_Id :=
2176 Protected_Body_Subprogram (Spec_Id);
2177 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2178 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2180 while Present (Prot_Ext_Formal) loop
2181 pragma Assert (Present (Impl_Ext_Formal));
2182 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2183 Next_Formal_With_Extras (Prot_Ext_Formal);
2184 Next_Formal_With_Extras (Impl_Ext_Formal);
2189 -- Now we can go on to analyze the body
2191 HSS := Handled_Statement_Sequence (N);
2192 Set_Actual_Subtypes (N, Current_Scope);
2194 -- Deal with preconditions and postconditions
2196 Process_PPCs (N, Spec_Id, Body_Id);
2198 -- Add a declaration for the Protection object, renaming declarations
2199 -- for discriminals and privals and finally a declaration for the entry
2200 -- family index (if applicable). This form of early expansion is done
2201 -- when the Expander is active because Install_Private_Data_Declarations
2202 -- references entities which were created during regular expansion.
2205 and then Comes_From_Source (N)
2206 and then Present (Prot_Typ)
2207 and then Present (Spec_Id)
2208 and then not Is_Eliminated (Spec_Id)
2210 Install_Private_Data_Declarations
2211 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2214 -- Analyze the declarations (this call will analyze the precondition
2215 -- Check pragmas we prepended to the list, as well as the declaration
2216 -- of the _Postconditions procedure).
2218 Analyze_Declarations (Declarations (N));
2220 -- Check completion, and analyze the statements
2223 Inspect_Deferred_Constant_Completion (Declarations (N));
2226 -- Deal with end of scope processing for the body
2228 Process_End_Label (HSS, 't', Current_Scope);
2230 Check_Subprogram_Order (N);
2231 Set_Analyzed (Body_Id);
2233 -- If we have a separate spec, then the analysis of the declarations
2234 -- caused the entities in the body to be chained to the spec id, but
2235 -- we want them chained to the body id. Only the formal parameters
2236 -- end up chained to the spec id in this case.
2238 if Present (Spec_Id) then
2240 -- We must conform to the categorization of our spec
2242 Validate_Categorization_Dependency (N, Spec_Id);
2244 -- And if this is a child unit, the parent units must conform
2246 if Is_Child_Unit (Spec_Id) then
2247 Validate_Categorization_Dependency
2248 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2251 -- Here is where we move entities from the spec to the body
2253 -- Case where there are entities that stay with the spec
2255 if Present (Last_Real_Spec_Entity) then
2257 -- No body entities (happens when the only real spec entities
2258 -- come from precondition and postcondition pragmas)
2260 if No (Last_Entity (Body_Id)) then
2262 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2264 -- Body entities present (formals), so chain stuff past them
2268 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2271 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2272 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2273 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2275 -- Case where there are no spec entities, in this case there can
2276 -- be no body entities either, so just move everything.
2279 pragma Assert (No (Last_Entity (Body_Id)));
2280 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2281 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2282 Set_First_Entity (Spec_Id, Empty);
2283 Set_Last_Entity (Spec_Id, Empty);
2287 -- If function, check return statements
2289 if Nkind (Body_Spec) = N_Function_Specification then
2294 if Present (Spec_Id) then
2300 if Return_Present (Id) then
2301 Check_Returns (HSS, 'F', Missing_Ret);
2304 Set_Has_Missing_Return (Id);
2307 elsif not Is_Machine_Code_Subprogram (Id)
2308 and then not Body_Deleted
2310 Error_Msg_N ("missing RETURN statement in function body", N);
2314 -- If procedure with No_Return, check returns
2316 elsif Nkind (Body_Spec) = N_Procedure_Specification
2317 and then Present (Spec_Id)
2318 and then No_Return (Spec_Id)
2320 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2323 -- Now we are going to check for variables that are never modified in
2324 -- the body of the procedure. But first we deal with a special case
2325 -- where we want to modify this check. If the body of the subprogram
2326 -- starts with a raise statement or its equivalent, or if the body
2327 -- consists entirely of a null statement, then it is pretty obvious
2328 -- that it is OK to not reference the parameters. For example, this
2329 -- might be the following common idiom for a stubbed function:
2330 -- statement of the procedure raises an exception. In particular this
2331 -- deals with the common idiom of a stubbed function, which might
2332 -- appear as something like
2334 -- function F (A : Integer) return Some_Type;
2337 -- raise Program_Error;
2341 -- Here the purpose of X is simply to satisfy the annoying requirement
2342 -- in Ada that there be at least one return, and we certainly do not
2343 -- want to go posting warnings on X that it is not initialized! On
2344 -- the other hand, if X is entirely unreferenced that should still
2347 -- What we do is to detect these cases, and if we find them, flag the
2348 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2349 -- suppress unwanted warnings. For the case of the function stub above
2350 -- we have a special test to set X as apparently assigned to suppress
2357 -- Skip initial labels (for one thing this occurs when we are in
2358 -- front end ZCX mode, but in any case it is irrelevant), and also
2359 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2361 Stm := First (Statements (HSS));
2362 while Nkind (Stm) = N_Label
2363 or else Nkind (Stm) in N_Push_xxx_Label
2368 -- Do the test on the original statement before expansion
2371 Ostm : constant Node_Id := Original_Node (Stm);
2374 -- If explicit raise statement, turn on flag
2376 if Nkind (Ostm) = N_Raise_Statement then
2377 Set_Trivial_Subprogram (Stm);
2379 -- If null statement, and no following statements, turn on flag
2381 elsif Nkind (Stm) = N_Null_Statement
2382 and then Comes_From_Source (Stm)
2383 and then No (Next (Stm))
2385 Set_Trivial_Subprogram (Stm);
2387 -- Check for explicit call cases which likely raise an exception
2389 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2390 if Is_Entity_Name (Name (Ostm)) then
2392 Ent : constant Entity_Id := Entity (Name (Ostm));
2395 -- If the procedure is marked No_Return, then likely it
2396 -- raises an exception, but in any case it is not coming
2397 -- back here, so turn on the flag.
2399 if Ekind (Ent) = E_Procedure
2400 and then No_Return (Ent)
2402 Set_Trivial_Subprogram (Stm);
2404 -- If the procedure name is Raise_Exception, then also
2405 -- assume that it raises an exception. The main target
2406 -- here is Ada.Exceptions.Raise_Exception, but this name
2407 -- is pretty evocative in any context! Note that the
2408 -- procedure in Ada.Exceptions is not marked No_Return
2409 -- because of the annoying case of the null exception Id
2410 -- when operating in Ada 95 mode.
2412 elsif Chars (Ent) = Name_Raise_Exception then
2413 Set_Trivial_Subprogram (Stm);
2421 -- Check for variables that are never modified
2427 -- If there is a separate spec, then transfer Never_Set_In_Source
2428 -- flags from out parameters to the corresponding entities in the
2429 -- body. The reason we do that is we want to post error flags on
2430 -- the body entities, not the spec entities.
2432 if Present (Spec_Id) then
2433 E1 := First_Entity (Spec_Id);
2434 while Present (E1) loop
2435 if Ekind (E1) = E_Out_Parameter then
2436 E2 := First_Entity (Body_Id);
2437 while Present (E2) loop
2438 exit when Chars (E1) = Chars (E2);
2442 if Present (E2) then
2443 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2451 -- Check references in body unless it was deleted. Note that the
2452 -- check of Body_Deleted here is not just for efficiency, it is
2453 -- necessary to avoid junk warnings on formal parameters.
2455 if not Body_Deleted then
2456 Check_References (Body_Id);
2459 end Analyze_Subprogram_Body;
2461 ------------------------------------
2462 -- Analyze_Subprogram_Declaration --
2463 ------------------------------------
2465 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2466 Designator : constant Entity_Id :=
2467 Analyze_Subprogram_Specification (Specification (N));
2468 Scop : constant Entity_Id := Current_Scope;
2470 -- Start of processing for Analyze_Subprogram_Declaration
2473 Generate_Definition (Designator);
2475 -- Check for RCI unit subprogram declarations for illegal inlined
2476 -- subprograms and subprograms having access parameter or limited
2477 -- parameter without Read and Write attributes (RM E.2.3(12-13)).
2479 Validate_RCI_Subprogram_Declaration (N);
2483 Defining_Entity (N),
2484 " Analyze subprogram spec: ");
2486 if Debug_Flag_C then
2487 Write_Str ("==== Compiling subprogram spec ");
2488 Write_Name (Chars (Designator));
2489 Write_Str (" from ");
2490 Write_Location (Sloc (N));
2494 New_Overloaded_Entity (Designator);
2495 Check_Delayed_Subprogram (Designator);
2497 -- If the type of the first formal of the current subprogram is a non
2498 -- generic tagged private type , mark the subprogram as being a private
2501 if Present (First_Formal (Designator)) then
2503 Formal_Typ : constant Entity_Id :=
2504 Etype (First_Formal (Designator));
2506 Set_Is_Private_Primitive (Designator,
2507 Is_Tagged_Type (Formal_Typ)
2508 and then Is_Private_Type (Formal_Typ)
2509 and then not Is_Generic_Actual_Type (Formal_Typ));
2513 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2516 if Ada_Version >= Ada_05
2517 and then Comes_From_Source (N)
2518 and then Is_Dispatching_Operation (Designator)
2525 if Has_Controlling_Result (Designator) then
2526 Etyp := Etype (Designator);
2529 E := First_Entity (Designator);
2531 and then Is_Formal (E)
2532 and then not Is_Controlling_Formal (E)
2540 if Is_Access_Type (Etyp) then
2541 Etyp := Directly_Designated_Type (Etyp);
2544 if Is_Interface (Etyp)
2545 and then not Is_Abstract_Subprogram (Designator)
2546 and then not (Ekind (Designator) = E_Procedure
2547 and then Null_Present (Specification (N)))
2549 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2551 ("(Ada 2005) interface subprogram % must be abstract or null",
2557 -- What is the following code for, it used to be
2559 -- ??? Set_Suppress_Elaboration_Checks
2560 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2562 -- The following seems equivalent, but a bit dubious
2564 if Elaboration_Checks_Suppressed (Designator) then
2565 Set_Kill_Elaboration_Checks (Designator);
2568 if Scop /= Standard_Standard
2569 and then not Is_Child_Unit (Designator)
2571 Set_Categorization_From_Scope (Designator, Scop);
2573 -- For a compilation unit, check for library-unit pragmas
2575 Push_Scope (Designator);
2576 Set_Categorization_From_Pragmas (N);
2577 Validate_Categorization_Dependency (N, Designator);
2581 -- For a compilation unit, set body required. This flag will only be
2582 -- reset if a valid Import or Interface pragma is processed later on.
2584 if Nkind (Parent (N)) = N_Compilation_Unit then
2585 Set_Body_Required (Parent (N), True);
2587 if Ada_Version >= Ada_05
2588 and then Nkind (Specification (N)) = N_Procedure_Specification
2589 and then Null_Present (Specification (N))
2592 ("null procedure cannot be declared at library level", N);
2596 Generate_Reference_To_Formals (Designator);
2597 Check_Eliminated (Designator);
2599 -- Ada 2005: if procedure is declared with "is null" qualifier,
2600 -- it requires no body.
2602 if Nkind (Specification (N)) = N_Procedure_Specification
2603 and then Null_Present (Specification (N))
2605 Set_Has_Completion (Designator);
2606 Set_Is_Inlined (Designator);
2608 if Is_Protected_Type (Current_Scope) then
2610 ("protected operation cannot be a null procedure", N);
2613 end Analyze_Subprogram_Declaration;
2615 --------------------------------------
2616 -- Analyze_Subprogram_Specification --
2617 --------------------------------------
2619 -- Reminder: N here really is a subprogram specification (not a subprogram
2620 -- declaration). This procedure is called to analyze the specification in
2621 -- both subprogram bodies and subprogram declarations (specs).
2623 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2624 Designator : constant Entity_Id := Defining_Entity (N);
2625 Formals : constant List_Id := Parameter_Specifications (N);
2627 -- Start of processing for Analyze_Subprogram_Specification
2630 Generate_Definition (Designator);
2632 if Nkind (N) = N_Function_Specification then
2633 Set_Ekind (Designator, E_Function);
2634 Set_Mechanism (Designator, Default_Mechanism);
2637 Set_Ekind (Designator, E_Procedure);
2638 Set_Etype (Designator, Standard_Void_Type);
2641 -- Introduce new scope for analysis of the formals and the return type
2643 Set_Scope (Designator, Current_Scope);
2645 if Present (Formals) then
2646 Push_Scope (Designator);
2647 Process_Formals (Formals, N);
2649 -- Ada 2005 (AI-345): If this is an overriding operation of an
2650 -- inherited interface operation, and the controlling type is
2651 -- a synchronized type, replace the type with its corresponding
2652 -- record, to match the proper signature of an overriding operation.
2654 if Ada_Version >= Ada_05 then
2657 Formal_Typ : Entity_Id;
2658 Rec_Typ : Entity_Id;
2661 Formal := First_Formal (Designator);
2662 while Present (Formal) loop
2663 Formal_Typ := Etype (Formal);
2665 if Is_Concurrent_Type (Formal_Typ)
2666 and then Present (Corresponding_Record_Type (Formal_Typ))
2668 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2670 if Present (Interfaces (Rec_Typ)) then
2671 Set_Etype (Formal, Rec_Typ);
2675 Next_Formal (Formal);
2682 elsif Nkind (N) = N_Function_Specification then
2683 Analyze_Return_Type (N);
2686 if Nkind (N) = N_Function_Specification then
2687 if Nkind (Designator) = N_Defining_Operator_Symbol then
2688 Valid_Operator_Definition (Designator);
2691 May_Need_Actuals (Designator);
2693 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2694 -- the subprogram is abstract also. This does not apply to renaming
2695 -- declarations, where abstractness is inherited.
2696 -- In case of primitives associated with abstract interface types
2697 -- the check is applied later (see Analyze_Subprogram_Declaration).
2699 if Is_Abstract_Type (Etype (Designator))
2700 and then not Is_Interface (Etype (Designator))
2701 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2702 and then Nkind (Parent (N)) /=
2703 N_Abstract_Subprogram_Declaration
2705 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2708 ("function that returns abstract type must be abstract", N);
2713 end Analyze_Subprogram_Specification;
2715 --------------------------
2716 -- Build_Body_To_Inline --
2717 --------------------------
2719 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2720 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2721 Original_Body : Node_Id;
2722 Body_To_Analyze : Node_Id;
2723 Max_Size : constant := 10;
2724 Stat_Count : Integer := 0;
2726 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2727 -- Check for declarations that make inlining not worthwhile
2729 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2730 -- Check for statements that make inlining not worthwhile: any tasking
2731 -- statement, nested at any level. Keep track of total number of
2732 -- elementary statements, as a measure of acceptable size.
2734 function Has_Pending_Instantiation return Boolean;
2735 -- If some enclosing body contains instantiations that appear before the
2736 -- corresponding generic body, the enclosing body has a freeze node so
2737 -- that it can be elaborated after the generic itself. This might
2738 -- conflict with subsequent inlinings, so that it is unsafe to try to
2739 -- inline in such a case.
2741 function Has_Single_Return return Boolean;
2742 -- In general we cannot inline functions that return unconstrained type.
2743 -- However, we can handle such functions if all return statements return
2744 -- a local variable that is the only declaration in the body of the
2745 -- function. In that case the call can be replaced by that local
2746 -- variable as is done for other inlined calls.
2748 procedure Remove_Pragmas;
2749 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2750 -- parameter has no meaning when the body is inlined and the formals
2751 -- are rewritten. Remove it from body to inline. The analysis of the
2752 -- non-inlined body will handle the pragma properly.
2754 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2755 -- If the body of the subprogram includes a call that returns an
2756 -- unconstrained type, the secondary stack is involved, and it
2757 -- is not worth inlining.
2759 ------------------------------
2760 -- Has_Excluded_Declaration --
2761 ------------------------------
2763 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2766 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2767 -- Nested subprograms make a given body ineligible for inlining, but
2768 -- we make an exception for instantiations of unchecked conversion.
2769 -- The body has not been analyzed yet, so check the name, and verify
2770 -- that the visible entity with that name is the predefined unit.
2772 -----------------------------
2773 -- Is_Unchecked_Conversion --
2774 -----------------------------
2776 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2777 Id : constant Node_Id := Name (D);
2781 if Nkind (Id) = N_Identifier
2782 and then Chars (Id) = Name_Unchecked_Conversion
2784 Conv := Current_Entity (Id);
2786 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
2787 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
2789 Conv := Current_Entity (Selector_Name (Id));
2794 return Present (Conv)
2795 and then Is_Predefined_File_Name
2796 (Unit_File_Name (Get_Source_Unit (Conv)))
2797 and then Is_Intrinsic_Subprogram (Conv);
2798 end Is_Unchecked_Conversion;
2800 -- Start of processing for Has_Excluded_Declaration
2804 while Present (D) loop
2805 if (Nkind (D) = N_Function_Instantiation
2806 and then not Is_Unchecked_Conversion (D))
2807 or else Nkind_In (D, N_Protected_Type_Declaration,
2808 N_Package_Declaration,
2809 N_Package_Instantiation,
2811 N_Procedure_Instantiation,
2812 N_Task_Type_Declaration)
2815 ("cannot inline & (non-allowed declaration)?", D, Subp);
2823 end Has_Excluded_Declaration;
2825 ----------------------------
2826 -- Has_Excluded_Statement --
2827 ----------------------------
2829 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
2835 while Present (S) loop
2836 Stat_Count := Stat_Count + 1;
2838 if Nkind_In (S, N_Abort_Statement,
2839 N_Asynchronous_Select,
2840 N_Conditional_Entry_Call,
2841 N_Delay_Relative_Statement,
2842 N_Delay_Until_Statement,
2847 ("cannot inline & (non-allowed statement)?", S, Subp);
2850 elsif Nkind (S) = N_Block_Statement then
2851 if Present (Declarations (S))
2852 and then Has_Excluded_Declaration (Declarations (S))
2856 elsif Present (Handled_Statement_Sequence (S))
2859 (Exception_Handlers (Handled_Statement_Sequence (S)))
2861 Has_Excluded_Statement
2862 (Statements (Handled_Statement_Sequence (S))))
2867 elsif Nkind (S) = N_Case_Statement then
2868 E := First (Alternatives (S));
2869 while Present (E) loop
2870 if Has_Excluded_Statement (Statements (E)) then
2877 elsif Nkind (S) = N_If_Statement then
2878 if Has_Excluded_Statement (Then_Statements (S)) then
2882 if Present (Elsif_Parts (S)) then
2883 E := First (Elsif_Parts (S));
2884 while Present (E) loop
2885 if Has_Excluded_Statement (Then_Statements (E)) then
2892 if Present (Else_Statements (S))
2893 and then Has_Excluded_Statement (Else_Statements (S))
2898 elsif Nkind (S) = N_Loop_Statement
2899 and then Has_Excluded_Statement (Statements (S))
2908 end Has_Excluded_Statement;
2910 -------------------------------
2911 -- Has_Pending_Instantiation --
2912 -------------------------------
2914 function Has_Pending_Instantiation return Boolean is
2919 while Present (S) loop
2920 if Is_Compilation_Unit (S)
2921 or else Is_Child_Unit (S)
2924 elsif Ekind (S) = E_Package
2925 and then Has_Forward_Instantiation (S)
2934 end Has_Pending_Instantiation;
2936 ------------------------
2937 -- Has_Single_Return --
2938 ------------------------
2940 function Has_Single_Return return Boolean is
2941 Return_Statement : Node_Id := Empty;
2943 function Check_Return (N : Node_Id) return Traverse_Result;
2949 function Check_Return (N : Node_Id) return Traverse_Result is
2951 if Nkind (N) = N_Simple_Return_Statement then
2952 if Present (Expression (N))
2953 and then Is_Entity_Name (Expression (N))
2955 if No (Return_Statement) then
2956 Return_Statement := N;
2959 elsif Chars (Expression (N)) =
2960 Chars (Expression (Return_Statement))
2969 -- Expression has wrong form
2979 function Check_All_Returns is new Traverse_Func (Check_Return);
2981 -- Start of processing for Has_Single_Return
2984 return Check_All_Returns (N) = OK
2985 and then Present (Declarations (N))
2986 and then Present (First (Declarations (N)))
2987 and then Chars (Expression (Return_Statement)) =
2988 Chars (Defining_Identifier (First (Declarations (N))));
2989 end Has_Single_Return;
2991 --------------------
2992 -- Remove_Pragmas --
2993 --------------------
2995 procedure Remove_Pragmas is
3000 Decl := First (Declarations (Body_To_Analyze));
3001 while Present (Decl) loop
3004 if Nkind (Decl) = N_Pragma
3005 and then (Pragma_Name (Decl) = Name_Unreferenced
3007 Pragma_Name (Decl) = Name_Unmodified)
3016 --------------------------
3017 -- Uses_Secondary_Stack --
3018 --------------------------
3020 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3021 function Check_Call (N : Node_Id) return Traverse_Result;
3022 -- Look for function calls that return an unconstrained type
3028 function Check_Call (N : Node_Id) return Traverse_Result is
3030 if Nkind (N) = N_Function_Call
3031 and then Is_Entity_Name (Name (N))
3032 and then Is_Composite_Type (Etype (Entity (Name (N))))
3033 and then not Is_Constrained (Etype (Entity (Name (N))))
3036 ("cannot inline & (call returns unconstrained type)?",
3044 function Check_Calls is new Traverse_Func (Check_Call);
3047 return Check_Calls (Bod) = Abandon;
3048 end Uses_Secondary_Stack;
3050 -- Start of processing for Build_Body_To_Inline
3053 if Nkind (Decl) = N_Subprogram_Declaration
3054 and then Present (Body_To_Inline (Decl))
3056 return; -- Done already.
3058 -- Functions that return unconstrained composite types require
3059 -- secondary stack handling, and cannot currently be inlined, unless
3060 -- all return statements return a local variable that is the first
3061 -- local declaration in the body.
3063 elsif Ekind (Subp) = E_Function
3064 and then not Is_Scalar_Type (Etype (Subp))
3065 and then not Is_Access_Type (Etype (Subp))
3066 and then not Is_Constrained (Etype (Subp))
3068 if not Has_Single_Return then
3070 ("cannot inline & (unconstrained return type)?", N, Subp);
3074 -- Ditto for functions that return controlled types, where controlled
3075 -- actions interfere in complex ways with inlining.
3077 elsif Ekind (Subp) = E_Function
3078 and then Controlled_Type (Etype (Subp))
3081 ("cannot inline & (controlled return type)?", N, Subp);
3085 if Present (Declarations (N))
3086 and then Has_Excluded_Declaration (Declarations (N))
3091 if Present (Handled_Statement_Sequence (N)) then
3092 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3094 ("cannot inline& (exception handler)?",
3095 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3099 Has_Excluded_Statement
3100 (Statements (Handled_Statement_Sequence (N)))
3106 -- We do not inline a subprogram that is too large, unless it is
3107 -- marked Inline_Always. This pragma does not suppress the other
3108 -- checks on inlining (forbidden declarations, handlers, etc).
3110 if Stat_Count > Max_Size
3111 and then not Has_Pragma_Inline_Always (Subp)
3113 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3117 if Has_Pending_Instantiation then
3119 ("cannot inline& (forward instance within enclosing body)?",
3124 -- Within an instance, the body to inline must be treated as a nested
3125 -- generic, so that the proper global references are preserved.
3127 -- Note that we do not do this at the library level, because it is not
3128 -- needed, and furthermore this causes trouble if front end inlining
3129 -- is activated (-gnatN).
3131 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3132 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3133 Original_Body := Copy_Generic_Node (N, Empty, True);
3135 Original_Body := Copy_Separate_Tree (N);
3138 -- We need to capture references to the formals in order to substitute
3139 -- the actuals at the point of inlining, i.e. instantiation. To treat
3140 -- the formals as globals to the body to inline, we nest it within
3141 -- a dummy parameterless subprogram, declared within the real one.
3142 -- To avoid generating an internal name (which is never public, and
3143 -- which affects serial numbers of other generated names), we use
3144 -- an internal symbol that cannot conflict with user declarations.
3146 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3147 Set_Defining_Unit_Name
3148 (Specification (Original_Body),
3149 Make_Defining_Identifier (Sloc (N), Name_uParent));
3150 Set_Corresponding_Spec (Original_Body, Empty);
3152 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3154 -- Set return type of function, which is also global and does not need
3157 if Ekind (Subp) = E_Function then
3158 Set_Result_Definition (Specification (Body_To_Analyze),
3159 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3162 if No (Declarations (N)) then
3163 Set_Declarations (N, New_List (Body_To_Analyze));
3165 Append (Body_To_Analyze, Declarations (N));
3168 Expander_Mode_Save_And_Set (False);
3171 Analyze (Body_To_Analyze);
3172 Push_Scope (Defining_Entity (Body_To_Analyze));
3173 Save_Global_References (Original_Body);
3175 Remove (Body_To_Analyze);
3177 Expander_Mode_Restore;
3179 -- Restore environment if previously saved
3181 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3185 -- If secondary stk used there is no point in inlining. We have
3186 -- already issued the warning in this case, so nothing to do.
3188 if Uses_Secondary_Stack (Body_To_Analyze) then
3192 Set_Body_To_Inline (Decl, Original_Body);
3193 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3194 Set_Is_Inlined (Subp);
3195 end Build_Body_To_Inline;
3201 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3203 -- Do not emit warning if this is a predefined unit which is not
3204 -- the main unit. With validity checks enabled, some predefined
3205 -- subprograms may contain nested subprograms and become ineligible
3208 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3209 and then not In_Extended_Main_Source_Unit (Subp)
3213 elsif Has_Pragma_Inline_Always (Subp) then
3215 -- Remove last character (question mark) to make this into an error,
3216 -- because the Inline_Always pragma cannot be obeyed.
3218 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3220 elsif Ineffective_Inline_Warnings then
3221 Error_Msg_NE (Msg, N, Subp);
3225 -----------------------
3226 -- Check_Conformance --
3227 -----------------------
3229 procedure Check_Conformance
3230 (New_Id : Entity_Id;
3232 Ctype : Conformance_Type;
3234 Conforms : out Boolean;
3235 Err_Loc : Node_Id := Empty;
3236 Get_Inst : Boolean := False;
3237 Skip_Controlling_Formals : Boolean := False)
3239 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3240 -- Post error message for conformance error on given node. Two messages
3241 -- are output. The first points to the previous declaration with a
3242 -- general "no conformance" message. The second is the detailed reason,
3243 -- supplied as Msg. The parameter N provide information for a possible
3244 -- & insertion in the message, and also provides the location for
3245 -- posting the message in the absence of a specified Err_Loc location.
3247 -----------------------
3248 -- Conformance_Error --
3249 -----------------------
3251 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3258 if No (Err_Loc) then
3264 Error_Msg_Sloc := Sloc (Old_Id);
3267 when Type_Conformant =>
3269 ("not type conformant with declaration#!", Enode);
3271 when Mode_Conformant =>
3272 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3274 ("not mode conformant with operation inherited#!",
3278 ("not mode conformant with declaration#!", Enode);
3281 when Subtype_Conformant =>
3282 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3284 ("not subtype conformant with operation inherited#!",
3288 ("not subtype conformant with declaration#!", Enode);
3291 when Fully_Conformant =>
3292 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3294 ("not fully conformant with operation inherited#!",
3298 ("not fully conformant with declaration#!", Enode);
3302 Error_Msg_NE (Msg, Enode, N);
3304 end Conformance_Error;
3308 Old_Type : constant Entity_Id := Etype (Old_Id);
3309 New_Type : constant Entity_Id := Etype (New_Id);
3310 Old_Formal : Entity_Id;
3311 New_Formal : Entity_Id;
3312 Access_Types_Match : Boolean;
3313 Old_Formal_Base : Entity_Id;
3314 New_Formal_Base : Entity_Id;
3316 -- Start of processing for Check_Conformance
3321 -- We need a special case for operators, since they don't appear
3324 if Ctype = Type_Conformant then
3325 if Ekind (New_Id) = E_Operator
3326 and then Operator_Matches_Spec (New_Id, Old_Id)
3332 -- If both are functions/operators, check return types conform
3334 if Old_Type /= Standard_Void_Type
3335 and then New_Type /= Standard_Void_Type
3338 -- If we are checking interface conformance we omit controlling
3339 -- arguments and result, because we are only checking the conformance
3340 -- of the remaining parameters.
3342 if Has_Controlling_Result (Old_Id)
3343 and then Has_Controlling_Result (New_Id)
3344 and then Skip_Controlling_Formals
3348 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3349 Conformance_Error ("\return type does not match!", New_Id);
3353 -- Ada 2005 (AI-231): In case of anonymous access types check the
3354 -- null-exclusion and access-to-constant attributes match.
3356 if Ada_Version >= Ada_05
3357 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3359 (Can_Never_Be_Null (Old_Type)
3360 /= Can_Never_Be_Null (New_Type)
3361 or else Is_Access_Constant (Etype (Old_Type))
3362 /= Is_Access_Constant (Etype (New_Type)))
3364 Conformance_Error ("\return type does not match!", New_Id);
3368 -- If either is a function/operator and the other isn't, error
3370 elsif Old_Type /= Standard_Void_Type
3371 or else New_Type /= Standard_Void_Type
3373 Conformance_Error ("\functions can only match functions!", New_Id);
3377 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3378 -- If this is a renaming as body, refine error message to indicate that
3379 -- the conflict is with the original declaration. If the entity is not
3380 -- frozen, the conventions don't have to match, the one of the renamed
3381 -- entity is inherited.
3383 if Ctype >= Subtype_Conformant then
3384 if Convention (Old_Id) /= Convention (New_Id) then
3386 if not Is_Frozen (New_Id) then
3389 elsif Present (Err_Loc)
3390 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3391 and then Present (Corresponding_Spec (Err_Loc))
3393 Error_Msg_Name_1 := Chars (New_Id);
3395 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3397 Conformance_Error ("\prior declaration for% has convention %!");
3400 Conformance_Error ("\calling conventions do not match!");
3405 elsif Is_Formal_Subprogram (Old_Id)
3406 or else Is_Formal_Subprogram (New_Id)
3408 Conformance_Error ("\formal subprograms not allowed!");
3413 -- Deal with parameters
3415 -- Note: we use the entity information, rather than going directly
3416 -- to the specification in the tree. This is not only simpler, but
3417 -- absolutely necessary for some cases of conformance tests between
3418 -- operators, where the declaration tree simply does not exist!
3420 Old_Formal := First_Formal (Old_Id);
3421 New_Formal := First_Formal (New_Id);
3423 while Present (Old_Formal) and then Present (New_Formal) loop
3424 if Is_Controlling_Formal (Old_Formal)
3425 and then Is_Controlling_Formal (New_Formal)
3426 and then Skip_Controlling_Formals
3428 goto Skip_Controlling_Formal;
3431 if Ctype = Fully_Conformant then
3433 -- Names must match. Error message is more accurate if we do
3434 -- this before checking that the types of the formals match.
3436 if Chars (Old_Formal) /= Chars (New_Formal) then
3437 Conformance_Error ("\name & does not match!", New_Formal);
3439 -- Set error posted flag on new formal as well to stop
3440 -- junk cascaded messages in some cases.
3442 Set_Error_Posted (New_Formal);
3447 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3448 -- case occurs whenever a subprogram is being renamed and one of its
3449 -- parameters imposes a null exclusion. For example:
3451 -- type T is null record;
3452 -- type Acc_T is access T;
3453 -- subtype Acc_T_Sub is Acc_T;
3455 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3456 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3459 Old_Formal_Base := Etype (Old_Formal);
3460 New_Formal_Base := Etype (New_Formal);
3463 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3464 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3467 Access_Types_Match := Ada_Version >= Ada_05
3469 -- Ensure that this rule is only applied when New_Id is a
3470 -- renaming of Old_Id.
3472 and then Nkind (Parent (Parent (New_Id))) =
3473 N_Subprogram_Renaming_Declaration
3474 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3475 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3476 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3478 -- Now handle the allowed access-type case
3480 and then Is_Access_Type (Old_Formal_Base)
3481 and then Is_Access_Type (New_Formal_Base)
3483 -- The type kinds must match. The only exception occurs with
3484 -- multiple generics of the form:
3487 -- type F is private; type A is private;
3488 -- type F_Ptr is access F; type A_Ptr is access A;
3489 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3490 -- package F_Pack is ... package A_Pack is
3491 -- package F_Inst is
3492 -- new F_Pack (A, A_Ptr, A_P);
3494 -- When checking for conformance between the parameters of A_P
3495 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3496 -- because the compiler has transformed A_Ptr into a subtype of
3497 -- F_Ptr. We catch this case in the code below.
3499 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3501 (Is_Generic_Type (Old_Formal_Base)
3502 and then Is_Generic_Type (New_Formal_Base)
3503 and then Is_Internal (New_Formal_Base)
3504 and then Etype (Etype (New_Formal_Base)) =
3506 and then Directly_Designated_Type (Old_Formal_Base) =
3507 Directly_Designated_Type (New_Formal_Base)
3508 and then ((Is_Itype (Old_Formal_Base)
3509 and then Can_Never_Be_Null (Old_Formal_Base))
3511 (Is_Itype (New_Formal_Base)
3512 and then Can_Never_Be_Null (New_Formal_Base)));
3514 -- Types must always match. In the visible part of an instance,
3515 -- usual overloading rules for dispatching operations apply, and
3516 -- we check base types (not the actual subtypes).
3518 if In_Instance_Visible_Part
3519 and then Is_Dispatching_Operation (New_Id)
3521 if not Conforming_Types
3522 (T1 => Base_Type (Etype (Old_Formal)),
3523 T2 => Base_Type (Etype (New_Formal)),
3525 Get_Inst => Get_Inst)
3526 and then not Access_Types_Match
3528 Conformance_Error ("\type of & does not match!", New_Formal);
3532 elsif not Conforming_Types
3533 (T1 => Old_Formal_Base,
3534 T2 => New_Formal_Base,
3536 Get_Inst => Get_Inst)
3537 and then not Access_Types_Match
3539 Conformance_Error ("\type of & does not match!", New_Formal);
3543 -- For mode conformance, mode must match
3545 if Ctype >= Mode_Conformant then
3546 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3547 Conformance_Error ("\mode of & does not match!", New_Formal);
3550 -- Part of mode conformance for access types is having the same
3551 -- constant modifier.
3553 elsif Access_Types_Match
3554 and then Is_Access_Constant (Old_Formal_Base) /=
3555 Is_Access_Constant (New_Formal_Base)
3558 ("\constant modifier does not match!", New_Formal);
3563 if Ctype >= Subtype_Conformant then
3565 -- Ada 2005 (AI-231): In case of anonymous access types check
3566 -- the null-exclusion and access-to-constant attributes must
3569 if Ada_Version >= Ada_05
3570 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3571 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3573 (Can_Never_Be_Null (Old_Formal) /=
3574 Can_Never_Be_Null (New_Formal)
3576 Is_Access_Constant (Etype (Old_Formal)) /=
3577 Is_Access_Constant (Etype (New_Formal)))
3579 -- It is allowed to omit the null-exclusion in case of stream
3580 -- attribute subprograms. We recognize stream subprograms
3581 -- through their TSS-generated suffix.
3584 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3586 if TSS_Name /= TSS_Stream_Read
3587 and then TSS_Name /= TSS_Stream_Write
3588 and then TSS_Name /= TSS_Stream_Input
3589 and then TSS_Name /= TSS_Stream_Output
3592 ("\type of & does not match!", New_Formal);
3599 -- Full conformance checks
3601 if Ctype = Fully_Conformant then
3603 -- We have checked already that names match
3605 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3607 -- Check default expressions for in parameters
3610 NewD : constant Boolean :=
3611 Present (Default_Value (New_Formal));
3612 OldD : constant Boolean :=
3613 Present (Default_Value (Old_Formal));
3615 if NewD or OldD then
3617 -- The old default value has been analyzed because the
3618 -- current full declaration will have frozen everything
3619 -- before. The new default value has not been analyzed,
3620 -- so analyze it now before we check for conformance.
3623 Push_Scope (New_Id);
3624 Preanalyze_Spec_Expression
3625 (Default_Value (New_Formal), Etype (New_Formal));
3629 if not (NewD and OldD)
3630 or else not Fully_Conformant_Expressions
3631 (Default_Value (Old_Formal),
3632 Default_Value (New_Formal))
3635 ("\default expression for & does not match!",
3644 -- A couple of special checks for Ada 83 mode. These checks are
3645 -- skipped if either entity is an operator in package Standard,
3646 -- or if either old or new instance is not from the source program.
3648 if Ada_Version = Ada_83
3649 and then Sloc (Old_Id) > Standard_Location
3650 and then Sloc (New_Id) > Standard_Location
3651 and then Comes_From_Source (Old_Id)
3652 and then Comes_From_Source (New_Id)
3655 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3656 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3659 -- Explicit IN must be present or absent in both cases. This
3660 -- test is required only in the full conformance case.
3662 if In_Present (Old_Param) /= In_Present (New_Param)
3663 and then Ctype = Fully_Conformant
3666 ("\(Ada 83) IN must appear in both declarations",
3671 -- Grouping (use of comma in param lists) must be the same
3672 -- This is where we catch a misconformance like:
3675 -- A : Integer; B : Integer
3677 -- which are represented identically in the tree except
3678 -- for the setting of the flags More_Ids and Prev_Ids.
3680 if More_Ids (Old_Param) /= More_Ids (New_Param)
3681 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3684 ("\grouping of & does not match!", New_Formal);
3690 -- This label is required when skipping controlling formals
3692 <<Skip_Controlling_Formal>>
3694 Next_Formal (Old_Formal);
3695 Next_Formal (New_Formal);
3698 if Present (Old_Formal) then
3699 Conformance_Error ("\too few parameters!");
3702 elsif Present (New_Formal) then
3703 Conformance_Error ("\too many parameters!", New_Formal);
3706 end Check_Conformance;
3708 -----------------------
3709 -- Check_Conventions --
3710 -----------------------
3712 procedure Check_Conventions (Typ : Entity_Id) is
3713 Ifaces_List : Elist_Id;
3715 procedure Check_Convention (Op : Entity_Id);
3716 -- Verify that the convention of inherited dispatching operation Op is
3717 -- consistent among all subprograms it overrides. In order to minimize
3718 -- the search, Search_From is utilized to designate a specific point in
3719 -- the list rather than iterating over the whole list once more.
3721 ----------------------
3722 -- Check_Convention --
3723 ----------------------
3725 procedure Check_Convention (Op : Entity_Id) is
3726 Iface_Elmt : Elmt_Id;
3727 Iface_Prim_Elmt : Elmt_Id;
3728 Iface_Prim : Entity_Id;
3731 Iface_Elmt := First_Elmt (Ifaces_List);
3732 while Present (Iface_Elmt) loop
3734 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3735 while Present (Iface_Prim_Elmt) loop
3736 Iface_Prim := Node (Iface_Prim_Elmt);
3738 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3739 and then Convention (Iface_Prim) /= Convention (Op)
3742 ("inconsistent conventions in primitive operations", Typ);
3744 Error_Msg_Name_1 := Chars (Op);
3745 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3746 Error_Msg_Sloc := Sloc (Op);
3748 if Comes_From_Source (Op) then
3749 if not Is_Overriding_Operation (Op) then
3750 Error_Msg_N ("\\primitive % defined #", Typ);
3752 Error_Msg_N ("\\overriding operation % with " &
3753 "convention % defined #", Typ);
3756 else pragma Assert (Present (Alias (Op)));
3757 Error_Msg_Sloc := Sloc (Alias (Op));
3758 Error_Msg_N ("\\inherited operation % with " &
3759 "convention % defined #", Typ);
3762 Error_Msg_Name_1 := Chars (Op);
3764 Get_Convention_Name (Convention (Iface_Prim));
3765 Error_Msg_Sloc := Sloc (Iface_Prim);
3766 Error_Msg_N ("\\overridden operation % with " &
3767 "convention % defined #", Typ);
3769 -- Avoid cascading errors
3774 Next_Elmt (Iface_Prim_Elmt);
3777 Next_Elmt (Iface_Elmt);
3779 end Check_Convention;
3783 Prim_Op : Entity_Id;
3784 Prim_Op_Elmt : Elmt_Id;
3786 -- Start of processing for Check_Conventions
3789 if not Has_Interfaces (Typ) then
3793 Collect_Interfaces (Typ, Ifaces_List);
3795 -- The algorithm checks every overriding dispatching operation against
3796 -- all the corresponding overridden dispatching operations, detecting
3797 -- differences in conventions.
3799 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
3800 while Present (Prim_Op_Elmt) loop
3801 Prim_Op := Node (Prim_Op_Elmt);
3803 -- A small optimization: skip the predefined dispatching operations
3804 -- since they always have the same convention.
3806 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
3807 Check_Convention (Prim_Op);
3810 Next_Elmt (Prim_Op_Elmt);
3812 end Check_Conventions;
3814 ------------------------------
3815 -- Check_Delayed_Subprogram --
3816 ------------------------------
3818 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
3821 procedure Possible_Freeze (T : Entity_Id);
3822 -- T is the type of either a formal parameter or of the return type.
3823 -- If T is not yet frozen and needs a delayed freeze, then the
3824 -- subprogram itself must be delayed.
3826 ---------------------
3827 -- Possible_Freeze --
3828 ---------------------
3830 procedure Possible_Freeze (T : Entity_Id) is
3832 if Has_Delayed_Freeze (T)
3833 and then not Is_Frozen (T)
3835 Set_Has_Delayed_Freeze (Designator);
3837 elsif Is_Access_Type (T)
3838 and then Has_Delayed_Freeze (Designated_Type (T))
3839 and then not Is_Frozen (Designated_Type (T))
3841 Set_Has_Delayed_Freeze (Designator);
3843 end Possible_Freeze;
3845 -- Start of processing for Check_Delayed_Subprogram
3848 -- Never need to freeze abstract subprogram
3850 if Ekind (Designator) /= E_Subprogram_Type
3851 and then Is_Abstract_Subprogram (Designator)
3855 -- Need delayed freeze if return type itself needs a delayed
3856 -- freeze and is not yet frozen.
3858 Possible_Freeze (Etype (Designator));
3859 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
3861 -- Need delayed freeze if any of the formal types themselves need
3862 -- a delayed freeze and are not yet frozen.
3864 F := First_Formal (Designator);
3865 while Present (F) loop
3866 Possible_Freeze (Etype (F));
3867 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
3872 -- Mark functions that return by reference. Note that it cannot be
3873 -- done for delayed_freeze subprograms because the underlying
3874 -- returned type may not be known yet (for private types)
3876 if not Has_Delayed_Freeze (Designator)
3877 and then Expander_Active
3880 Typ : constant Entity_Id := Etype (Designator);
3881 Utyp : constant Entity_Id := Underlying_Type (Typ);
3884 if Is_Inherently_Limited_Type (Typ) then
3885 Set_Returns_By_Ref (Designator);
3887 elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
3888 Set_Returns_By_Ref (Designator);
3892 end Check_Delayed_Subprogram;
3894 ------------------------------------
3895 -- Check_Discriminant_Conformance --
3896 ------------------------------------
3898 procedure Check_Discriminant_Conformance
3903 Old_Discr : Entity_Id := First_Discriminant (Prev);
3904 New_Discr : Node_Id := First (Discriminant_Specifications (N));
3905 New_Discr_Id : Entity_Id;
3906 New_Discr_Type : Entity_Id;
3908 procedure Conformance_Error (Msg : String; N : Node_Id);
3909 -- Post error message for conformance error on given node. Two messages
3910 -- are output. The first points to the previous declaration with a
3911 -- general "no conformance" message. The second is the detailed reason,
3912 -- supplied as Msg. The parameter N provide information for a possible
3913 -- & insertion in the message.
3915 -----------------------
3916 -- Conformance_Error --
3917 -----------------------
3919 procedure Conformance_Error (Msg : String; N : Node_Id) is
3921 Error_Msg_Sloc := Sloc (Prev_Loc);
3922 Error_Msg_N ("not fully conformant with declaration#!", N);
3923 Error_Msg_NE (Msg, N, N);
3924 end Conformance_Error;
3926 -- Start of processing for Check_Discriminant_Conformance
3929 while Present (Old_Discr) and then Present (New_Discr) loop
3931 New_Discr_Id := Defining_Identifier (New_Discr);
3933 -- The subtype mark of the discriminant on the full type has not
3934 -- been analyzed so we do it here. For an access discriminant a new
3937 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
3939 Access_Definition (N, Discriminant_Type (New_Discr));
3942 Analyze (Discriminant_Type (New_Discr));
3943 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
3946 if not Conforming_Types
3947 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
3949 Conformance_Error ("type of & does not match!", New_Discr_Id);
3952 -- Treat the new discriminant as an occurrence of the old one,
3953 -- for navigation purposes, and fill in some semantic
3954 -- information, for completeness.
3956 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
3957 Set_Etype (New_Discr_Id, Etype (Old_Discr));
3958 Set_Scope (New_Discr_Id, Scope (Old_Discr));
3963 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
3964 Conformance_Error ("name & does not match!", New_Discr_Id);
3968 -- Default expressions must match
3971 NewD : constant Boolean :=
3972 Present (Expression (New_Discr));
3973 OldD : constant Boolean :=
3974 Present (Expression (Parent (Old_Discr)));
3977 if NewD or OldD then
3979 -- The old default value has been analyzed and expanded,
3980 -- because the current full declaration will have frozen
3981 -- everything before. The new default values have not been
3982 -- expanded, so expand now to check conformance.
3985 Preanalyze_Spec_Expression
3986 (Expression (New_Discr), New_Discr_Type);
3989 if not (NewD and OldD)
3990 or else not Fully_Conformant_Expressions
3991 (Expression (Parent (Old_Discr)),
3992 Expression (New_Discr))
3996 ("default expression for & does not match!",
4003 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4005 if Ada_Version = Ada_83 then
4007 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4010 -- Grouping (use of comma in param lists) must be the same
4011 -- This is where we catch a misconformance like:
4014 -- A : Integer; B : Integer
4016 -- which are represented identically in the tree except
4017 -- for the setting of the flags More_Ids and Prev_Ids.
4019 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4020 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4023 ("grouping of & does not match!", New_Discr_Id);
4029 Next_Discriminant (Old_Discr);
4033 if Present (Old_Discr) then
4034 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4037 elsif Present (New_Discr) then
4039 ("too many discriminants!", Defining_Identifier (New_Discr));
4042 end Check_Discriminant_Conformance;
4044 ----------------------------
4045 -- Check_Fully_Conformant --
4046 ----------------------------
4048 procedure Check_Fully_Conformant
4049 (New_Id : Entity_Id;
4051 Err_Loc : Node_Id := Empty)
4054 pragma Warnings (Off, Result);
4057 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4058 end Check_Fully_Conformant;
4060 ---------------------------
4061 -- Check_Mode_Conformant --
4062 ---------------------------
4064 procedure Check_Mode_Conformant
4065 (New_Id : Entity_Id;
4067 Err_Loc : Node_Id := Empty;
4068 Get_Inst : Boolean := False)
4071 pragma Warnings (Off, Result);
4074 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4075 end Check_Mode_Conformant;
4077 --------------------------------
4078 -- Check_Overriding_Indicator --
4079 --------------------------------
4081 procedure Check_Overriding_Indicator
4083 Overridden_Subp : Entity_Id;
4084 Is_Primitive : Boolean)
4090 -- No overriding indicator for literals
4092 if Ekind (Subp) = E_Enumeration_Literal then
4095 elsif Ekind (Subp) = E_Entry then
4096 Decl := Parent (Subp);
4099 Decl := Unit_Declaration_Node (Subp);
4102 if Nkind_In (Decl, N_Subprogram_Body,
4103 N_Subprogram_Body_Stub,
4104 N_Subprogram_Declaration,
4105 N_Abstract_Subprogram_Declaration,
4106 N_Subprogram_Renaming_Declaration)
4108 Spec := Specification (Decl);
4110 elsif Nkind (Decl) = N_Entry_Declaration then
4117 if Present (Overridden_Subp) then
4118 if Must_Not_Override (Spec) then
4119 Error_Msg_Sloc := Sloc (Overridden_Subp);
4121 if Ekind (Subp) = E_Entry then
4123 ("entry & overrides inherited operation #", Spec, Subp);
4126 ("subprogram & overrides inherited operation #", Spec, Subp);
4129 elsif Is_Subprogram (Subp) then
4130 Set_Is_Overriding_Operation (Subp);
4133 -- If Subp is an operator, it may override a predefined operation.
4134 -- In that case overridden_subp is empty because of our implicit
4135 -- representation for predefined operators. We have to check whether the
4136 -- signature of Subp matches that of a predefined operator. Note that
4137 -- first argument provides the name of the operator, and the second
4138 -- argument the signature that may match that of a standard operation.
4139 -- If the indicator is overriding, then the operator must match a
4140 -- predefined signature, because we know already that there is no
4141 -- explicit overridden operation.
4143 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4145 if Must_Not_Override (Spec) then
4146 if not Is_Primitive then
4148 ("overriding indicator only allowed "
4149 & "if subprogram is primitive", Subp);
4151 elsif Operator_Matches_Spec (Subp, Subp) then
4153 ("subprogram & overrides predefined operator ", Spec, Subp);
4156 elsif Is_Overriding_Operation (Subp) then
4159 elsif Must_Override (Spec) then
4160 if not Operator_Matches_Spec (Subp, Subp) then
4161 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4164 Set_Is_Overriding_Operation (Subp);
4168 elsif Must_Override (Spec) then
4169 if Ekind (Subp) = E_Entry then
4170 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4172 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4175 -- If the operation is marked "not overriding" and it's not primitive
4176 -- then an error is issued, unless this is an operation of a task or
4177 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4178 -- has been specified have already been checked above.
4180 elsif Must_Not_Override (Spec)
4181 and then not Is_Primitive
4182 and then Ekind (Subp) /= E_Entry
4183 and then Ekind (Scope (Subp)) /= E_Protected_Type
4186 ("overriding indicator only allowed if subprogram is primitive",
4190 end Check_Overriding_Indicator;
4196 -- Note: this procedure needs to know far too much about how the expander
4197 -- messes with exceptions. The use of the flag Exception_Junk and the
4198 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4199 -- works, but is not very clean. It would be better if the expansion
4200 -- routines would leave Original_Node working nicely, and we could use
4201 -- Original_Node here to ignore all the peculiar expander messing ???
4203 procedure Check_Returns
4207 Proc : Entity_Id := Empty)
4211 procedure Check_Statement_Sequence (L : List_Id);
4212 -- Internal recursive procedure to check a list of statements for proper
4213 -- termination by a return statement (or a transfer of control or a
4214 -- compound statement that is itself internally properly terminated).
4216 ------------------------------
4217 -- Check_Statement_Sequence --
4218 ------------------------------
4220 procedure Check_Statement_Sequence (L : List_Id) is
4225 Raise_Exception_Call : Boolean;
4226 -- Set True if statement sequence terminated by Raise_Exception call
4227 -- or a Reraise_Occurrence call.
4230 Raise_Exception_Call := False;
4232 -- Get last real statement
4234 Last_Stm := Last (L);
4236 -- Deal with digging out exception handler statement sequences that
4237 -- have been transformed by the local raise to goto optimization.
4238 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4239 -- optimization has occurred, we are looking at something like:
4242 -- original stmts in block
4246 -- goto L1; | omitted if No_Exception_Propagation
4251 -- goto L3; -- skip handler when exception not raised
4253 -- <<L1>> -- target label for local exception
4267 -- and what we have to do is to dig out the estmts1 and estmts2
4268 -- sequences (which were the original sequences of statements in
4269 -- the exception handlers) and check them.
4271 if Nkind (Last_Stm) = N_Label
4272 and then Exception_Junk (Last_Stm)
4278 exit when Nkind (Stm) /= N_Block_Statement;
4279 exit when not Exception_Junk (Stm);
4282 exit when Nkind (Stm) /= N_Label;
4283 exit when not Exception_Junk (Stm);
4284 Check_Statement_Sequence
4285 (Statements (Handled_Statement_Sequence (Next (Stm))));
4290 exit when Nkind (Stm) /= N_Goto_Statement;
4291 exit when not Exception_Junk (Stm);
4295 -- Don't count pragmas
4297 while Nkind (Last_Stm) = N_Pragma
4299 -- Don't count call to SS_Release (can happen after Raise_Exception)
4302 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4304 Nkind (Name (Last_Stm)) = N_Identifier
4306 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4308 -- Don't count exception junk
4311 (Nkind_In (Last_Stm, N_Goto_Statement,
4313 N_Object_Declaration)
4314 and then Exception_Junk (Last_Stm))
4315 or else Nkind (Last_Stm) in N_Push_xxx_Label
4316 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4321 -- Here we have the "real" last statement
4323 Kind := Nkind (Last_Stm);
4325 -- Transfer of control, OK. Note that in the No_Return procedure
4326 -- case, we already diagnosed any explicit return statements, so
4327 -- we can treat them as OK in this context.
4329 if Is_Transfer (Last_Stm) then
4332 -- Check cases of explicit non-indirect procedure calls
4334 elsif Kind = N_Procedure_Call_Statement
4335 and then Is_Entity_Name (Name (Last_Stm))
4337 -- Check call to Raise_Exception procedure which is treated
4338 -- specially, as is a call to Reraise_Occurrence.
4340 -- We suppress the warning in these cases since it is likely that
4341 -- the programmer really does not expect to deal with the case
4342 -- of Null_Occurrence, and thus would find a warning about a
4343 -- missing return curious, and raising Program_Error does not
4344 -- seem such a bad behavior if this does occur.
4346 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4347 -- behavior will be to raise Constraint_Error (see AI-329).
4349 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4351 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4353 Raise_Exception_Call := True;
4355 -- For Raise_Exception call, test first argument, if it is
4356 -- an attribute reference for a 'Identity call, then we know
4357 -- that the call cannot possibly return.
4360 Arg : constant Node_Id :=
4361 Original_Node (First_Actual (Last_Stm));
4363 if Nkind (Arg) = N_Attribute_Reference
4364 and then Attribute_Name (Arg) = Name_Identity
4371 -- If statement, need to look inside if there is an else and check
4372 -- each constituent statement sequence for proper termination.
4374 elsif Kind = N_If_Statement
4375 and then Present (Else_Statements (Last_Stm))
4377 Check_Statement_Sequence (Then_Statements (Last_Stm));
4378 Check_Statement_Sequence (Else_Statements (Last_Stm));
4380 if Present (Elsif_Parts (Last_Stm)) then
4382 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4385 while Present (Elsif_Part) loop
4386 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4394 -- Case statement, check each case for proper termination
4396 elsif Kind = N_Case_Statement then
4400 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4401 while Present (Case_Alt) loop
4402 Check_Statement_Sequence (Statements (Case_Alt));
4403 Next_Non_Pragma (Case_Alt);
4409 -- Block statement, check its handled sequence of statements
4411 elsif Kind = N_Block_Statement then
4417 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4426 -- Loop statement. If there is an iteration scheme, we can definitely
4427 -- fall out of the loop. Similarly if there is an exit statement, we
4428 -- can fall out. In either case we need a following return.
4430 elsif Kind = N_Loop_Statement then
4431 if Present (Iteration_Scheme (Last_Stm))
4432 or else Has_Exit (Entity (Identifier (Last_Stm)))
4436 -- A loop with no exit statement or iteration scheme is either
4437 -- an infinite loop, or it has some other exit (raise/return).
4438 -- In either case, no warning is required.
4444 -- Timed entry call, check entry call and delay alternatives
4446 -- Note: in expanded code, the timed entry call has been converted
4447 -- to a set of expanded statements on which the check will work
4448 -- correctly in any case.
4450 elsif Kind = N_Timed_Entry_Call then
4452 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4453 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4456 -- If statement sequence of entry call alternative is missing,
4457 -- then we can definitely fall through, and we post the error
4458 -- message on the entry call alternative itself.
4460 if No (Statements (ECA)) then
4463 -- If statement sequence of delay alternative is missing, then
4464 -- we can definitely fall through, and we post the error
4465 -- message on the delay alternative itself.
4467 -- Note: if both ECA and DCA are missing the return, then we
4468 -- post only one message, should be enough to fix the bugs.
4469 -- If not we will get a message next time on the DCA when the
4472 elsif No (Statements (DCA)) then
4475 -- Else check both statement sequences
4478 Check_Statement_Sequence (Statements (ECA));
4479 Check_Statement_Sequence (Statements (DCA));
4484 -- Conditional entry call, check entry call and else part
4486 -- Note: in expanded code, the conditional entry call has been
4487 -- converted to a set of expanded statements on which the check
4488 -- will work correctly in any case.
4490 elsif Kind = N_Conditional_Entry_Call then
4492 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4495 -- If statement sequence of entry call alternative is missing,
4496 -- then we can definitely fall through, and we post the error
4497 -- message on the entry call alternative itself.
4499 if No (Statements (ECA)) then
4502 -- Else check statement sequence and else part
4505 Check_Statement_Sequence (Statements (ECA));
4506 Check_Statement_Sequence (Else_Statements (Last_Stm));
4512 -- If we fall through, issue appropriate message
4515 if not Raise_Exception_Call then
4517 ("?RETURN statement missing following this statement!",
4520 ("\?Program_Error may be raised at run time!",
4524 -- Note: we set Err even though we have not issued a warning
4525 -- because we still have a case of a missing return. This is
4526 -- an extremely marginal case, probably will never be noticed
4527 -- but we might as well get it right.
4531 -- Otherwise we have the case of a procedure marked No_Return
4534 if not Raise_Exception_Call then
4536 ("?implied return after this statement " &
4537 "will raise Program_Error",
4540 ("\?procedure & is marked as No_Return!",
4545 RE : constant Node_Id :=
4546 Make_Raise_Program_Error (Sloc (Last_Stm),
4547 Reason => PE_Implicit_Return);
4549 Insert_After (Last_Stm, RE);
4553 end Check_Statement_Sequence;
4555 -- Start of processing for Check_Returns
4559 Check_Statement_Sequence (Statements (HSS));
4561 if Present (Exception_Handlers (HSS)) then
4562 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4563 while Present (Handler) loop
4564 Check_Statement_Sequence (Statements (Handler));
4565 Next_Non_Pragma (Handler);
4570 ----------------------------
4571 -- Check_Subprogram_Order --
4572 ----------------------------
4574 procedure Check_Subprogram_Order (N : Node_Id) is
4576 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4577 -- This is used to check if S1 > S2 in the sense required by this
4578 -- test, for example nameab < namec, but name2 < name10.
4580 -----------------------------
4581 -- Subprogram_Name_Greater --
4582 -----------------------------
4584 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4589 -- Remove trailing numeric parts
4592 while S1 (L1) in '0' .. '9' loop
4597 while S2 (L2) in '0' .. '9' loop
4601 -- If non-numeric parts non-equal, that's decisive
4603 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4606 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4609 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4610 -- that a missing suffix is treated as numeric zero in this test.
4614 while L1 < S1'Last loop
4616 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4620 while L2 < S2'Last loop
4622 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
4627 end Subprogram_Name_Greater;
4629 -- Start of processing for Check_Subprogram_Order
4632 -- Check body in alpha order if this is option
4635 and then Style_Check_Order_Subprograms
4636 and then Nkind (N) = N_Subprogram_Body
4637 and then Comes_From_Source (N)
4638 and then In_Extended_Main_Source_Unit (N)
4642 renames Scope_Stack.Table
4643 (Scope_Stack.Last).Last_Subprogram_Name;
4645 Body_Id : constant Entity_Id :=
4646 Defining_Entity (Specification (N));
4649 Get_Decoded_Name_String (Chars (Body_Id));
4652 if Subprogram_Name_Greater
4653 (LSN.all, Name_Buffer (1 .. Name_Len))
4655 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
4661 LSN := new String'(Name_Buffer (1 .. Name_Len));
4664 end Check_Subprogram_Order;
4666 ------------------------------
4667 -- Check_Subtype_Conformant --
4668 ------------------------------
4670 procedure Check_Subtype_Conformant
4671 (New_Id : Entity_Id;
4673 Err_Loc : Node_Id := Empty;
4674 Skip_Controlling_Formals : Boolean := False)
4677 pragma Warnings (Off, Result);
4680 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
4681 Skip_Controlling_Formals => Skip_Controlling_Formals);
4682 end Check_Subtype_Conformant;
4684 ---------------------------
4685 -- Check_Type_Conformant --
4686 ---------------------------
4688 procedure Check_Type_Conformant
4689 (New_Id : Entity_Id;
4691 Err_Loc : Node_Id := Empty)
4694 pragma Warnings (Off, Result);
4697 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
4698 end Check_Type_Conformant;
4700 ----------------------
4701 -- Conforming_Types --
4702 ----------------------
4704 function Conforming_Types
4707 Ctype : Conformance_Type;
4708 Get_Inst : Boolean := False) return Boolean
4710 Type_1 : Entity_Id := T1;
4711 Type_2 : Entity_Id := T2;
4712 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
4714 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
4715 -- If neither T1 nor T2 are generic actual types, or if they are in
4716 -- different scopes (e.g. parent and child instances), then verify that
4717 -- the base types are equal. Otherwise T1 and T2 must be on the same
4718 -- subtype chain. The whole purpose of this procedure is to prevent
4719 -- spurious ambiguities in an instantiation that may arise if two
4720 -- distinct generic types are instantiated with the same actual.
4722 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
4723 -- An access parameter can designate an incomplete type. If the
4724 -- incomplete type is the limited view of a type from a limited_
4725 -- with_clause, check whether the non-limited view is available. If
4726 -- it is a (non-limited) incomplete type, get the full view.
4728 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
4729 -- Returns True if and only if either T1 denotes a limited view of T2
4730 -- or T2 denotes a limited view of T1. This can arise when the limited
4731 -- with view of a type is used in a subprogram declaration and the
4732 -- subprogram body is in the scope of a regular with clause for the
4733 -- same unit. In such a case, the two type entities can be considered
4734 -- identical for purposes of conformance checking.
4736 ----------------------
4737 -- Base_Types_Match --
4738 ----------------------
4740 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
4745 elsif Base_Type (T1) = Base_Type (T2) then
4747 -- The following is too permissive. A more precise test should
4748 -- check that the generic actual is an ancestor subtype of the
4751 return not Is_Generic_Actual_Type (T1)
4752 or else not Is_Generic_Actual_Type (T2)
4753 or else Scope (T1) /= Scope (T2);
4758 end Base_Types_Match;
4760 --------------------------
4761 -- Find_Designated_Type --
4762 --------------------------
4764 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
4768 Desig := Directly_Designated_Type (T);
4770 if Ekind (Desig) = E_Incomplete_Type then
4772 -- If regular incomplete type, get full view if available
4774 if Present (Full_View (Desig)) then
4775 Desig := Full_View (Desig);
4777 -- If limited view of a type, get non-limited view if available,
4778 -- and check again for a regular incomplete type.
4780 elsif Present (Non_Limited_View (Desig)) then
4781 Desig := Get_Full_View (Non_Limited_View (Desig));
4786 end Find_Designated_Type;
4788 -------------------------------
4789 -- Matches_Limited_With_View --
4790 -------------------------------
4792 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
4794 -- In some cases a type imported through a limited_with clause, and
4795 -- its nonlimited view are both visible, for example in an anonymous
4796 -- access-to-class-wide type in a formal. Both entities designate the
4799 if From_With_Type (T1)
4800 and then T2 = Available_View (T1)
4804 elsif From_With_Type (T2)
4805 and then T1 = Available_View (T2)
4812 end Matches_Limited_With_View;
4814 -- Start of processing for Conforming_Types
4817 -- The context is an instance association for a formal
4818 -- access-to-subprogram type; the formal parameter types require
4819 -- mapping because they may denote other formal parameters of the
4823 Type_1 := Get_Instance_Of (T1);
4824 Type_2 := Get_Instance_Of (T2);
4827 -- If one of the types is a view of the other introduced by a limited
4828 -- with clause, treat these as conforming for all purposes.
4830 if Matches_Limited_With_View (T1, T2) then
4833 elsif Base_Types_Match (Type_1, Type_2) then
4834 return Ctype <= Mode_Conformant
4835 or else Subtypes_Statically_Match (Type_1, Type_2);
4837 elsif Is_Incomplete_Or_Private_Type (Type_1)
4838 and then Present (Full_View (Type_1))
4839 and then Base_Types_Match (Full_View (Type_1), Type_2)
4841 return Ctype <= Mode_Conformant
4842 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
4844 elsif Ekind (Type_2) = E_Incomplete_Type
4845 and then Present (Full_View (Type_2))
4846 and then Base_Types_Match (Type_1, Full_View (Type_2))
4848 return Ctype <= Mode_Conformant
4849 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4851 elsif Is_Private_Type (Type_2)
4852 and then In_Instance
4853 and then Present (Full_View (Type_2))
4854 and then Base_Types_Match (Type_1, Full_View (Type_2))
4856 return Ctype <= Mode_Conformant
4857 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4860 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
4861 -- treated recursively because they carry a signature.
4863 Are_Anonymous_Access_To_Subprogram_Types :=
4864 Ekind (Type_1) = Ekind (Type_2)
4866 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
4868 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
4870 -- Test anonymous access type case. For this case, static subtype
4871 -- matching is required for mode conformance (RM 6.3.1(15)). We check
4872 -- the base types because we may have built internal subtype entities
4873 -- to handle null-excluding types (see Process_Formals).
4875 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
4877 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
4878 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
4881 Desig_1 : Entity_Id;
4882 Desig_2 : Entity_Id;
4885 -- In Ada2005, access constant indicators must match for
4886 -- subtype conformance.
4888 if Ada_Version >= Ada_05
4889 and then Ctype >= Subtype_Conformant
4891 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
4896 Desig_1 := Find_Designated_Type (Type_1);
4898 Desig_2 := Find_Designated_Type (Type_2);
4900 -- If the context is an instance association for a formal
4901 -- access-to-subprogram type; formal access parameter designated
4902 -- types require mapping because they may denote other formal
4903 -- parameters of the generic unit.
4906 Desig_1 := Get_Instance_Of (Desig_1);
4907 Desig_2 := Get_Instance_Of (Desig_2);
4910 -- It is possible for a Class_Wide_Type to be introduced for an
4911 -- incomplete type, in which case there is a separate class_ wide
4912 -- type for the full view. The types conform if their Etypes
4913 -- conform, i.e. one may be the full view of the other. This can
4914 -- only happen in the context of an access parameter, other uses
4915 -- of an incomplete Class_Wide_Type are illegal.
4917 if Is_Class_Wide_Type (Desig_1)
4918 and then Is_Class_Wide_Type (Desig_2)
4922 (Etype (Base_Type (Desig_1)),
4923 Etype (Base_Type (Desig_2)), Ctype);
4925 elsif Are_Anonymous_Access_To_Subprogram_Types then
4926 if Ada_Version < Ada_05 then
4927 return Ctype = Type_Conformant
4929 Subtypes_Statically_Match (Desig_1, Desig_2);
4931 -- We must check the conformance of the signatures themselves
4935 Conformant : Boolean;
4938 (Desig_1, Desig_2, Ctype, False, Conformant);
4944 return Base_Type (Desig_1) = Base_Type (Desig_2)
4945 and then (Ctype = Type_Conformant
4947 Subtypes_Statically_Match (Desig_1, Desig_2));
4951 -- Otherwise definitely no match
4954 if ((Ekind (Type_1) = E_Anonymous_Access_Type
4955 and then Is_Access_Type (Type_2))
4956 or else (Ekind (Type_2) = E_Anonymous_Access_Type
4957 and then Is_Access_Type (Type_1)))
4960 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
4962 May_Hide_Profile := True;
4967 end Conforming_Types;
4969 --------------------------
4970 -- Create_Extra_Formals --
4971 --------------------------
4973 procedure Create_Extra_Formals (E : Entity_Id) is
4975 First_Extra : Entity_Id := Empty;
4976 Last_Extra : Entity_Id;
4977 Formal_Type : Entity_Id;
4978 P_Formal : Entity_Id := Empty;
4980 function Add_Extra_Formal
4981 (Assoc_Entity : Entity_Id;
4984 Suffix : String) return Entity_Id;
4985 -- Add an extra formal to the current list of formals and extra formals.
4986 -- The extra formal is added to the end of the list of extra formals,
4987 -- and also returned as the result. These formals are always of mode IN.
4988 -- The new formal has the type Typ, is declared in Scope, and its name
4989 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
4991 ----------------------
4992 -- Add_Extra_Formal --
4993 ----------------------
4995 function Add_Extra_Formal
4996 (Assoc_Entity : Entity_Id;
4999 Suffix : String) return Entity_Id
5001 EF : constant Entity_Id :=
5002 Make_Defining_Identifier (Sloc (Assoc_Entity),
5003 Chars => New_External_Name (Chars (Assoc_Entity),
5007 -- A little optimization. Never generate an extra formal for the
5008 -- _init operand of an initialization procedure, since it could
5011 if Chars (Formal) = Name_uInit then
5015 Set_Ekind (EF, E_In_Parameter);
5016 Set_Actual_Subtype (EF, Typ);
5017 Set_Etype (EF, Typ);
5018 Set_Scope (EF, Scope);
5019 Set_Mechanism (EF, Default_Mechanism);
5020 Set_Formal_Validity (EF);
5022 if No (First_Extra) then
5024 Set_Extra_Formals (Scope, First_Extra);
5027 if Present (Last_Extra) then
5028 Set_Extra_Formal (Last_Extra, EF);
5034 end Add_Extra_Formal;
5036 -- Start of processing for Create_Extra_Formals
5039 -- We never generate extra formals if expansion is not active
5040 -- because we don't need them unless we are generating code.
5042 if not Expander_Active then
5046 -- If this is a derived subprogram then the subtypes of the parent
5047 -- subprogram's formal parameters will be used to to determine the need
5048 -- for extra formals.
5050 if Is_Overloadable (E) and then Present (Alias (E)) then
5051 P_Formal := First_Formal (Alias (E));
5054 Last_Extra := Empty;
5055 Formal := First_Formal (E);
5056 while Present (Formal) loop
5057 Last_Extra := Formal;
5058 Next_Formal (Formal);
5061 -- If Extra_formals were already created, don't do it again. This
5062 -- situation may arise for subprogram types created as part of
5063 -- dispatching calls (see Expand_Dispatching_Call)
5065 if Present (Last_Extra) and then
5066 Present (Extra_Formal (Last_Extra))
5071 -- If the subprogram is a predefined dispatching subprogram then don't
5072 -- generate any extra constrained or accessibility level formals. In
5073 -- general we suppress these for internal subprograms (by not calling
5074 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5075 -- generated stream attributes do get passed through because extra
5076 -- build-in-place formals are needed in some cases (limited 'Input).
5078 if Is_Predefined_Dispatching_Operation (E) then
5079 goto Test_For_BIP_Extras;
5082 Formal := First_Formal (E);
5083 while Present (Formal) loop
5085 -- Create extra formal for supporting the attribute 'Constrained.
5086 -- The case of a private type view without discriminants also
5087 -- requires the extra formal if the underlying type has defaulted
5090 if Ekind (Formal) /= E_In_Parameter then
5091 if Present (P_Formal) then
5092 Formal_Type := Etype (P_Formal);
5094 Formal_Type := Etype (Formal);
5097 -- Do not produce extra formals for Unchecked_Union parameters.
5098 -- Jump directly to the end of the loop.
5100 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5101 goto Skip_Extra_Formal_Generation;
5104 if not Has_Discriminants (Formal_Type)
5105 and then Ekind (Formal_Type) in Private_Kind
5106 and then Present (Underlying_Type (Formal_Type))
5108 Formal_Type := Underlying_Type (Formal_Type);
5111 if Has_Discriminants (Formal_Type)
5112 and then not Is_Constrained (Formal_Type)
5113 and then not Is_Indefinite_Subtype (Formal_Type)
5115 Set_Extra_Constrained
5116 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
5120 -- Create extra formal for supporting accessibility checking. This
5121 -- is done for both anonymous access formals and formals of named
5122 -- access types that are marked as controlling formals. The latter
5123 -- case can occur when Expand_Dispatching_Call creates a subprogram
5124 -- type and substitutes the types of access-to-class-wide actuals
5125 -- for the anonymous access-to-specific-type of controlling formals.
5126 -- Base_Type is applied because in cases where there is a null
5127 -- exclusion the formal may have an access subtype.
5129 -- This is suppressed if we specifically suppress accessibility
5130 -- checks at the package level for either the subprogram, or the
5131 -- package in which it resides. However, we do not suppress it
5132 -- simply if the scope has accessibility checks suppressed, since
5133 -- this could cause trouble when clients are compiled with a
5134 -- different suppression setting. The explicit checks at the
5135 -- package level are safe from this point of view.
5137 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5138 or else (Is_Controlling_Formal (Formal)
5139 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5141 (Explicit_Suppress (E, Accessibility_Check)
5143 Explicit_Suppress (Scope (E), Accessibility_Check))
5146 or else Present (Extra_Accessibility (P_Formal)))
5148 -- Temporary kludge: for now we avoid creating the extra formal
5149 -- for access parameters of protected operations because of
5150 -- problem with the case of internal protected calls. ???
5152 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
5153 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
5155 Set_Extra_Accessibility
5156 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
5160 -- This label is required when skipping extra formal generation for
5161 -- Unchecked_Union parameters.
5163 <<Skip_Extra_Formal_Generation>>
5165 if Present (P_Formal) then
5166 Next_Formal (P_Formal);
5169 Next_Formal (Formal);
5172 <<Test_For_BIP_Extras>>
5174 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5175 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5177 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5179 Result_Subt : constant Entity_Id := Etype (E);
5181 Discard : Entity_Id;
5182 pragma Warnings (Off, Discard);
5185 -- In the case of functions with unconstrained result subtypes,
5186 -- add a 3-state formal indicating whether the return object is
5187 -- allocated by the caller (0), or should be allocated by the
5188 -- callee on the secondary stack (1) or in the global heap (2).
5189 -- For the moment we just use Natural for the type of this formal.
5190 -- Note that this formal isn't usually needed in the case where
5191 -- the result subtype is constrained, but it is needed when the
5192 -- function has a tagged result, because generally such functions
5193 -- can be called in a dispatching context and such calls must be
5194 -- handled like calls to a class-wide function.
5196 if not Is_Constrained (Underlying_Type (Result_Subt))
5197 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5201 (E, Standard_Natural,
5202 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5205 -- In the case of functions whose result type has controlled
5206 -- parts, we have an extra formal of type
5207 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5208 -- is, we are passing a pointer to a finalization list (which is
5209 -- itself a pointer). This extra formal is then passed along to
5210 -- Move_Final_List in case of successful completion of a return
5211 -- statement. We cannot pass an 'in out' parameter, because we
5212 -- need to update the finalization list during an abort-deferred
5213 -- region, rather than using copy-back after the function
5214 -- returns. This is true even if we are able to get away with
5215 -- having 'in out' parameters, which are normally illegal for
5216 -- functions. This formal is also needed when the function has
5217 -- a tagged result, because generally such functions can be called
5218 -- in a dispatching context and such calls must be handled like
5219 -- calls to class-wide functions.
5221 if Controlled_Type (Result_Subt)
5222 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5226 (E, RTE (RE_Finalizable_Ptr_Ptr),
5227 E, BIP_Formal_Suffix (BIP_Final_List));
5230 -- If the result type contains tasks, we have two extra formals:
5231 -- the master of the tasks to be created, and the caller's
5232 -- activation chain.
5234 if Has_Task (Result_Subt) then
5237 (E, RTE (RE_Master_Id),
5238 E, BIP_Formal_Suffix (BIP_Master));
5241 (E, RTE (RE_Activation_Chain_Access),
5242 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5245 -- All build-in-place functions get an extra formal that will be
5246 -- passed the address of the return object within the caller.
5249 Formal_Type : constant Entity_Id :=
5251 (E_Anonymous_Access_Type, E,
5252 Scope_Id => Scope (E));
5254 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5255 Set_Etype (Formal_Type, Formal_Type);
5256 Set_Depends_On_Private
5257 (Formal_Type, Has_Private_Component (Formal_Type));
5258 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5259 Set_Is_Access_Constant (Formal_Type, False);
5261 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5262 -- the designated type comes from the limited view (for
5263 -- back-end purposes).
5265 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5267 Layout_Type (Formal_Type);
5271 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5275 end Create_Extra_Formals;
5277 -----------------------------
5278 -- Enter_Overloaded_Entity --
5279 -----------------------------
5281 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5282 E : Entity_Id := Current_Entity_In_Scope (S);
5283 C_E : Entity_Id := Current_Entity (S);
5287 Set_Has_Homonym (E);
5288 Set_Has_Homonym (S);
5291 Set_Is_Immediately_Visible (S);
5292 Set_Scope (S, Current_Scope);
5294 -- Chain new entity if front of homonym in current scope, so that
5295 -- homonyms are contiguous.
5300 while Homonym (C_E) /= E loop
5301 C_E := Homonym (C_E);
5304 Set_Homonym (C_E, S);
5308 Set_Current_Entity (S);
5313 Append_Entity (S, Current_Scope);
5314 Set_Public_Status (S);
5316 if Debug_Flag_E then
5317 Write_Str ("New overloaded entity chain: ");
5318 Write_Name (Chars (S));
5321 while Present (E) loop
5322 Write_Str (" "); Write_Int (Int (E));
5329 -- Generate warning for hiding
5332 and then Comes_From_Source (S)
5333 and then In_Extended_Main_Source_Unit (S)
5340 -- Warn unless genuine overloading
5342 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5343 and then (Is_Immediately_Visible (E)
5345 Is_Potentially_Use_Visible (S))
5347 Error_Msg_Sloc := Sloc (E);
5348 Error_Msg_N ("declaration of & hides one#?", S);
5352 end Enter_Overloaded_Entity;
5354 -----------------------------
5355 -- Find_Corresponding_Spec --
5356 -----------------------------
5358 function Find_Corresponding_Spec
5360 Post_Error : Boolean := True) return Entity_Id
5362 Spec : constant Node_Id := Specification (N);
5363 Designator : constant Entity_Id := Defining_Entity (Spec);
5368 E := Current_Entity (Designator);
5369 while Present (E) loop
5371 -- We are looking for a matching spec. It must have the same scope,
5372 -- and the same name, and either be type conformant, or be the case
5373 -- of a library procedure spec and its body (which belong to one
5374 -- another regardless of whether they are type conformant or not).
5376 if Scope (E) = Current_Scope then
5377 if Current_Scope = Standard_Standard
5378 or else (Ekind (E) = Ekind (Designator)
5379 and then Type_Conformant (E, Designator))
5381 -- Within an instantiation, we know that spec and body are
5382 -- subtype conformant, because they were subtype conformant
5383 -- in the generic. We choose the subtype-conformant entity
5384 -- here as well, to resolve spurious ambiguities in the
5385 -- instance that were not present in the generic (i.e. when
5386 -- two different types are given the same actual). If we are
5387 -- looking for a spec to match a body, full conformance is
5391 Set_Convention (Designator, Convention (E));
5393 if Nkind (N) = N_Subprogram_Body
5394 and then Present (Homonym (E))
5395 and then not Fully_Conformant (E, Designator)
5399 elsif not Subtype_Conformant (E, Designator) then
5404 if not Has_Completion (E) then
5405 if Nkind (N) /= N_Subprogram_Body_Stub then
5406 Set_Corresponding_Spec (N, E);
5409 Set_Has_Completion (E);
5412 elsif Nkind (Parent (N)) = N_Subunit then
5414 -- If this is the proper body of a subunit, the completion
5415 -- flag is set when analyzing the stub.
5419 -- If E is an internal function with a controlling result
5420 -- that was created for an operation inherited by a null
5421 -- extension, it may be overridden by a body without a previous
5422 -- spec (one more reason why these should be shunned). In that
5423 -- case remove the generated body, because the current one is
5424 -- the explicit overriding.
5426 elsif Ekind (E) = E_Function
5427 and then Ada_Version >= Ada_05
5428 and then not Comes_From_Source (E)
5429 and then Has_Controlling_Result (E)
5430 and then Is_Null_Extension (Etype (E))
5431 and then Comes_From_Source (Spec)
5433 Set_Has_Completion (E, False);
5435 if Expander_Active then
5437 (Unit_Declaration_Node
5438 (Corresponding_Body (Unit_Declaration_Node (E))));
5441 -- If expansion is disabled, the wrapper function has not
5442 -- been generated, and this is the standard case of a late
5443 -- body overriding an inherited operation.
5449 -- If the body already exists, then this is an error unless
5450 -- the previous declaration is the implicit declaration of a
5451 -- derived subprogram, or this is a spurious overloading in an
5454 elsif No (Alias (E))
5455 and then not Is_Intrinsic_Subprogram (E)
5456 and then not In_Instance
5459 Error_Msg_Sloc := Sloc (E);
5460 if Is_Imported (E) then
5462 ("body not allowed for imported subprogram & declared#",
5465 Error_Msg_NE ("duplicate body for & declared#", N, E);
5469 -- Child units cannot be overloaded, so a conformance mismatch
5470 -- between body and a previous spec is an error.
5472 elsif Is_Child_Unit (E)
5474 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5476 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5481 ("body of child unit does not match previous declaration", N);
5489 -- On exit, we know that no previous declaration of subprogram exists
5492 end Find_Corresponding_Spec;
5494 ----------------------
5495 -- Fully_Conformant --
5496 ----------------------
5498 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5501 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5503 end Fully_Conformant;
5505 ----------------------------------
5506 -- Fully_Conformant_Expressions --
5507 ----------------------------------
5509 function Fully_Conformant_Expressions
5510 (Given_E1 : Node_Id;
5511 Given_E2 : Node_Id) return Boolean
5513 E1 : constant Node_Id := Original_Node (Given_E1);
5514 E2 : constant Node_Id := Original_Node (Given_E2);
5515 -- We always test conformance on original nodes, since it is possible
5516 -- for analysis and/or expansion to make things look as though they
5517 -- conform when they do not, e.g. by converting 1+2 into 3.
5519 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5520 renames Fully_Conformant_Expressions;
5522 function FCL (L1, L2 : List_Id) return Boolean;
5523 -- Compare elements of two lists for conformance. Elements have to
5524 -- be conformant, and actuals inserted as default parameters do not
5525 -- match explicit actuals with the same value.
5527 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5528 -- Compare an operator node with a function call
5534 function FCL (L1, L2 : List_Id) return Boolean is
5538 if L1 = No_List then
5544 if L2 = No_List then
5550 -- Compare two lists, skipping rewrite insertions (we want to
5551 -- compare the original trees, not the expanded versions!)
5554 if Is_Rewrite_Insertion (N1) then
5556 elsif Is_Rewrite_Insertion (N2) then
5562 elsif not FCE (N1, N2) then
5575 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5576 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5581 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5586 Act := First (Actuals);
5588 if Nkind (Op_Node) in N_Binary_Op then
5590 if not FCE (Left_Opnd (Op_Node), Act) then
5597 return Present (Act)
5598 and then FCE (Right_Opnd (Op_Node), Act)
5599 and then No (Next (Act));
5603 -- Start of processing for Fully_Conformant_Expressions
5606 -- Non-conformant if paren count does not match. Note: if some idiot
5607 -- complains that we don't do this right for more than 3 levels of
5608 -- parentheses, they will be treated with the respect they deserve!
5610 if Paren_Count (E1) /= Paren_Count (E2) then
5613 -- If same entities are referenced, then they are conformant even if
5614 -- they have different forms (RM 8.3.1(19-20)).
5616 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5617 if Present (Entity (E1)) then
5618 return Entity (E1) = Entity (E2)
5619 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5620 and then Ekind (Entity (E1)) = E_Discriminant
5621 and then Ekind (Entity (E2)) = E_In_Parameter);
5623 elsif Nkind (E1) = N_Expanded_Name
5624 and then Nkind (E2) = N_Expanded_Name
5625 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5626 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5628 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5631 -- Identifiers in component associations don't always have
5632 -- entities, but their names must conform.
5634 return Nkind (E1) = N_Identifier
5635 and then Nkind (E2) = N_Identifier
5636 and then Chars (E1) = Chars (E2);
5639 elsif Nkind (E1) = N_Character_Literal
5640 and then Nkind (E2) = N_Expanded_Name
5642 return Nkind (Selector_Name (E2)) = N_Character_Literal
5643 and then Chars (E1) = Chars (Selector_Name (E2));
5645 elsif Nkind (E2) = N_Character_Literal
5646 and then Nkind (E1) = N_Expanded_Name
5648 return Nkind (Selector_Name (E1)) = N_Character_Literal
5649 and then Chars (E2) = Chars (Selector_Name (E1));
5651 elsif Nkind (E1) in N_Op
5652 and then Nkind (E2) = N_Function_Call
5654 return FCO (E1, E2);
5656 elsif Nkind (E2) in N_Op
5657 and then Nkind (E1) = N_Function_Call
5659 return FCO (E2, E1);
5661 -- Otherwise we must have the same syntactic entity
5663 elsif Nkind (E1) /= Nkind (E2) then
5666 -- At this point, we specialize by node type
5673 FCL (Expressions (E1), Expressions (E2))
5674 and then FCL (Component_Associations (E1),
5675 Component_Associations (E2));
5678 if Nkind (Expression (E1)) = N_Qualified_Expression
5680 Nkind (Expression (E2)) = N_Qualified_Expression
5682 return FCE (Expression (E1), Expression (E2));
5684 -- Check that the subtype marks and any constraints
5689 Indic1 : constant Node_Id := Expression (E1);
5690 Indic2 : constant Node_Id := Expression (E2);
5695 if Nkind (Indic1) /= N_Subtype_Indication then
5697 Nkind (Indic2) /= N_Subtype_Indication
5698 and then Entity (Indic1) = Entity (Indic2);
5700 elsif Nkind (Indic2) /= N_Subtype_Indication then
5702 Nkind (Indic1) /= N_Subtype_Indication
5703 and then Entity (Indic1) = Entity (Indic2);
5706 if Entity (Subtype_Mark (Indic1)) /=
5707 Entity (Subtype_Mark (Indic2))
5712 Elt1 := First (Constraints (Constraint (Indic1)));
5713 Elt2 := First (Constraints (Constraint (Indic2)));
5715 while Present (Elt1) and then Present (Elt2) loop
5716 if not FCE (Elt1, Elt2) then
5729 when N_Attribute_Reference =>
5731 Attribute_Name (E1) = Attribute_Name (E2)
5732 and then FCL (Expressions (E1), Expressions (E2));
5736 Entity (E1) = Entity (E2)
5737 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
5738 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5740 when N_And_Then | N_Or_Else | N_Membership_Test =>
5742 FCE (Left_Opnd (E1), Left_Opnd (E2))
5744 FCE (Right_Opnd (E1), Right_Opnd (E2));
5746 when N_Character_Literal =>
5748 Char_Literal_Value (E1) = Char_Literal_Value (E2);
5750 when N_Component_Association =>
5752 FCL (Choices (E1), Choices (E2))
5753 and then FCE (Expression (E1), Expression (E2));
5755 when N_Conditional_Expression =>
5757 FCL (Expressions (E1), Expressions (E2));
5759 when N_Explicit_Dereference =>
5761 FCE (Prefix (E1), Prefix (E2));
5763 when N_Extension_Aggregate =>
5765 FCL (Expressions (E1), Expressions (E2))
5766 and then Null_Record_Present (E1) =
5767 Null_Record_Present (E2)
5768 and then FCL (Component_Associations (E1),
5769 Component_Associations (E2));
5771 when N_Function_Call =>
5773 FCE (Name (E1), Name (E2))
5774 and then FCL (Parameter_Associations (E1),
5775 Parameter_Associations (E2));
5777 when N_Indexed_Component =>
5779 FCE (Prefix (E1), Prefix (E2))
5780 and then FCL (Expressions (E1), Expressions (E2));
5782 when N_Integer_Literal =>
5783 return (Intval (E1) = Intval (E2));
5788 when N_Operator_Symbol =>
5790 Chars (E1) = Chars (E2);
5792 when N_Others_Choice =>
5795 when N_Parameter_Association =>
5797 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
5798 and then FCE (Explicit_Actual_Parameter (E1),
5799 Explicit_Actual_Parameter (E2));
5801 when N_Qualified_Expression =>
5803 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5804 and then FCE (Expression (E1), Expression (E2));
5808 FCE (Low_Bound (E1), Low_Bound (E2))
5809 and then FCE (High_Bound (E1), High_Bound (E2));
5811 when N_Real_Literal =>
5812 return (Realval (E1) = Realval (E2));
5814 when N_Selected_Component =>
5816 FCE (Prefix (E1), Prefix (E2))
5817 and then FCE (Selector_Name (E1), Selector_Name (E2));
5821 FCE (Prefix (E1), Prefix (E2))
5822 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
5824 when N_String_Literal =>
5826 S1 : constant String_Id := Strval (E1);
5827 S2 : constant String_Id := Strval (E2);
5828 L1 : constant Nat := String_Length (S1);
5829 L2 : constant Nat := String_Length (S2);
5836 for J in 1 .. L1 loop
5837 if Get_String_Char (S1, J) /=
5838 Get_String_Char (S2, J)
5848 when N_Type_Conversion =>
5850 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5851 and then FCE (Expression (E1), Expression (E2));
5855 Entity (E1) = Entity (E2)
5856 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5858 when N_Unchecked_Type_Conversion =>
5860 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5861 and then FCE (Expression (E1), Expression (E2));
5863 -- All other node types cannot appear in this context. Strictly
5864 -- we should raise a fatal internal error. Instead we just ignore
5865 -- the nodes. This means that if anyone makes a mistake in the
5866 -- expander and mucks an expression tree irretrievably, the
5867 -- result will be a failure to detect a (probably very obscure)
5868 -- case of non-conformance, which is better than bombing on some
5869 -- case where two expressions do in fact conform.
5876 end Fully_Conformant_Expressions;
5878 ----------------------------------------
5879 -- Fully_Conformant_Discrete_Subtypes --
5880 ----------------------------------------
5882 function Fully_Conformant_Discrete_Subtypes
5883 (Given_S1 : Node_Id;
5884 Given_S2 : Node_Id) return Boolean
5886 S1 : constant Node_Id := Original_Node (Given_S1);
5887 S2 : constant Node_Id := Original_Node (Given_S2);
5889 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
5890 -- Special-case for a bound given by a discriminant, which in the body
5891 -- is replaced with the discriminal of the enclosing type.
5893 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
5894 -- Check both bounds
5896 -----------------------
5897 -- Conforming_Bounds --
5898 -----------------------
5900 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
5902 if Is_Entity_Name (B1)
5903 and then Is_Entity_Name (B2)
5904 and then Ekind (Entity (B1)) = E_Discriminant
5906 return Chars (B1) = Chars (B2);
5909 return Fully_Conformant_Expressions (B1, B2);
5911 end Conforming_Bounds;
5913 -----------------------
5914 -- Conforming_Ranges --
5915 -----------------------
5917 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
5920 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
5922 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
5923 end Conforming_Ranges;
5925 -- Start of processing for Fully_Conformant_Discrete_Subtypes
5928 if Nkind (S1) /= Nkind (S2) then
5931 elsif Is_Entity_Name (S1) then
5932 return Entity (S1) = Entity (S2);
5934 elsif Nkind (S1) = N_Range then
5935 return Conforming_Ranges (S1, S2);
5937 elsif Nkind (S1) = N_Subtype_Indication then
5939 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
5942 (Range_Expression (Constraint (S1)),
5943 Range_Expression (Constraint (S2)));
5947 end Fully_Conformant_Discrete_Subtypes;
5949 --------------------
5950 -- Install_Entity --
5951 --------------------
5953 procedure Install_Entity (E : Entity_Id) is
5954 Prev : constant Entity_Id := Current_Entity (E);
5956 Set_Is_Immediately_Visible (E);
5957 Set_Current_Entity (E);
5958 Set_Homonym (E, Prev);
5961 ---------------------
5962 -- Install_Formals --
5963 ---------------------
5965 procedure Install_Formals (Id : Entity_Id) is
5968 F := First_Formal (Id);
5969 while Present (F) loop
5973 end Install_Formals;
5975 -----------------------------
5976 -- Is_Interface_Conformant --
5977 -----------------------------
5979 function Is_Interface_Conformant
5980 (Tagged_Type : Entity_Id;
5981 Iface_Prim : Entity_Id;
5982 Prim : Entity_Id) return Boolean
5984 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
5985 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
5988 pragma Assert (Is_Subprogram (Iface_Prim)
5989 and then Is_Subprogram (Prim)
5990 and then Is_Dispatching_Operation (Iface_Prim)
5991 and then Is_Dispatching_Operation (Prim));
5993 pragma Assert (Is_Interface (Iface)
5994 or else (Present (Alias (Iface_Prim))
5997 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
5999 if Prim = Iface_Prim
6000 or else not Is_Subprogram (Prim)
6001 or else Ekind (Prim) /= Ekind (Iface_Prim)
6002 or else not Is_Dispatching_Operation (Prim)
6003 or else Scope (Prim) /= Scope (Tagged_Type)
6005 or else Base_Type (Typ) /= Tagged_Type
6006 or else not Primitive_Names_Match (Iface_Prim, Prim)
6010 -- Case of a procedure, or a function that does not have a controlling
6011 -- result (I or access I).
6013 elsif Ekind (Iface_Prim) = E_Procedure
6014 or else Etype (Prim) = Etype (Iface_Prim)
6015 or else not Has_Controlling_Result (Prim)
6017 return Type_Conformant (Prim, Iface_Prim,
6018 Skip_Controlling_Formals => True);
6020 -- Case of a function returning an interface, or an access to one.
6021 -- Check that the return types correspond.
6023 elsif Implements_Interface (Typ, Iface) then
6024 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6026 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6031 Type_Conformant (Prim, Iface_Prim,
6032 Skip_Controlling_Formals => True);
6038 end Is_Interface_Conformant;
6040 ---------------------------------
6041 -- Is_Non_Overriding_Operation --
6042 ---------------------------------
6044 function Is_Non_Overriding_Operation
6045 (Prev_E : Entity_Id;
6046 New_E : Entity_Id) return Boolean
6050 G_Typ : Entity_Id := Empty;
6052 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6053 -- If F_Type is a derived type associated with a generic actual subtype,
6054 -- then return its Generic_Parent_Type attribute, else return Empty.
6056 function Types_Correspond
6057 (P_Type : Entity_Id;
6058 N_Type : Entity_Id) return Boolean;
6059 -- Returns true if and only if the types (or designated types in the
6060 -- case of anonymous access types) are the same or N_Type is derived
6061 -- directly or indirectly from P_Type.
6063 -----------------------------
6064 -- Get_Generic_Parent_Type --
6065 -----------------------------
6067 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6072 if Is_Derived_Type (F_Typ)
6073 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6075 -- The tree must be traversed to determine the parent subtype in
6076 -- the generic unit, which unfortunately isn't always available
6077 -- via semantic attributes. ??? (Note: The use of Original_Node
6078 -- is needed for cases where a full derived type has been
6081 Indic := Subtype_Indication
6082 (Type_Definition (Original_Node (Parent (F_Typ))));
6084 if Nkind (Indic) = N_Subtype_Indication then
6085 G_Typ := Entity (Subtype_Mark (Indic));
6087 G_Typ := Entity (Indic);
6090 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6091 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6093 return Generic_Parent_Type (Parent (G_Typ));
6098 end Get_Generic_Parent_Type;
6100 ----------------------
6101 -- Types_Correspond --
6102 ----------------------
6104 function Types_Correspond
6105 (P_Type : Entity_Id;
6106 N_Type : Entity_Id) return Boolean
6108 Prev_Type : Entity_Id := Base_Type (P_Type);
6109 New_Type : Entity_Id := Base_Type (N_Type);
6112 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6113 Prev_Type := Designated_Type (Prev_Type);
6116 if Ekind (New_Type) = E_Anonymous_Access_Type then
6117 New_Type := Designated_Type (New_Type);
6120 if Prev_Type = New_Type then
6123 elsif not Is_Class_Wide_Type (New_Type) then
6124 while Etype (New_Type) /= New_Type loop
6125 New_Type := Etype (New_Type);
6126 if New_Type = Prev_Type then
6132 end Types_Correspond;
6134 -- Start of processing for Is_Non_Overriding_Operation
6137 -- In the case where both operations are implicit derived subprograms
6138 -- then neither overrides the other. This can only occur in certain
6139 -- obscure cases (e.g., derivation from homographs created in a generic
6142 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6145 elsif Ekind (Current_Scope) = E_Package
6146 and then Is_Generic_Instance (Current_Scope)
6147 and then In_Private_Part (Current_Scope)
6148 and then Comes_From_Source (New_E)
6150 -- We examine the formals and result subtype of the inherited
6151 -- operation, to determine whether their type is derived from (the
6152 -- instance of) a generic type.
6154 Formal := First_Formal (Prev_E);
6156 while Present (Formal) loop
6157 F_Typ := Base_Type (Etype (Formal));
6159 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6160 F_Typ := Designated_Type (F_Typ);
6163 G_Typ := Get_Generic_Parent_Type (F_Typ);
6165 Next_Formal (Formal);
6168 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6169 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6176 -- If the generic type is a private type, then the original
6177 -- operation was not overriding in the generic, because there was
6178 -- no primitive operation to override.
6180 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6181 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6182 N_Formal_Private_Type_Definition
6186 -- The generic parent type is the ancestor of a formal derived
6187 -- type declaration. We need to check whether it has a primitive
6188 -- operation that should be overridden by New_E in the generic.
6192 P_Formal : Entity_Id;
6193 N_Formal : Entity_Id;
6197 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6200 while Present (Prim_Elt) loop
6201 P_Prim := Node (Prim_Elt);
6203 if Chars (P_Prim) = Chars (New_E)
6204 and then Ekind (P_Prim) = Ekind (New_E)
6206 P_Formal := First_Formal (P_Prim);
6207 N_Formal := First_Formal (New_E);
6208 while Present (P_Formal) and then Present (N_Formal) loop
6209 P_Typ := Etype (P_Formal);
6210 N_Typ := Etype (N_Formal);
6212 if not Types_Correspond (P_Typ, N_Typ) then
6216 Next_Entity (P_Formal);
6217 Next_Entity (N_Formal);
6220 -- Found a matching primitive operation belonging to the
6221 -- formal ancestor type, so the new subprogram is
6225 and then No (N_Formal)
6226 and then (Ekind (New_E) /= E_Function
6229 (Etype (P_Prim), Etype (New_E)))
6235 Next_Elmt (Prim_Elt);
6238 -- If no match found, then the new subprogram does not
6239 -- override in the generic (nor in the instance).
6247 end Is_Non_Overriding_Operation;
6249 ------------------------------
6250 -- Make_Inequality_Operator --
6251 ------------------------------
6253 -- S is the defining identifier of an equality operator. We build a
6254 -- subprogram declaration with the right signature. This operation is
6255 -- intrinsic, because it is always expanded as the negation of the
6256 -- call to the equality function.
6258 procedure Make_Inequality_Operator (S : Entity_Id) is
6259 Loc : constant Source_Ptr := Sloc (S);
6262 Op_Name : Entity_Id;
6264 FF : constant Entity_Id := First_Formal (S);
6265 NF : constant Entity_Id := Next_Formal (FF);
6268 -- Check that equality was properly defined, ignore call if not
6275 A : constant Entity_Id :=
6276 Make_Defining_Identifier (Sloc (FF),
6277 Chars => Chars (FF));
6279 B : constant Entity_Id :=
6280 Make_Defining_Identifier (Sloc (NF),
6281 Chars => Chars (NF));
6284 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6286 Formals := New_List (
6287 Make_Parameter_Specification (Loc,
6288 Defining_Identifier => A,
6290 New_Reference_To (Etype (First_Formal (S)),
6291 Sloc (Etype (First_Formal (S))))),
6293 Make_Parameter_Specification (Loc,
6294 Defining_Identifier => B,
6296 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6297 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6300 Make_Subprogram_Declaration (Loc,
6302 Make_Function_Specification (Loc,
6303 Defining_Unit_Name => Op_Name,
6304 Parameter_Specifications => Formals,
6305 Result_Definition =>
6306 New_Reference_To (Standard_Boolean, Loc)));
6308 -- Insert inequality right after equality if it is explicit or after
6309 -- the derived type when implicit. These entities are created only
6310 -- for visibility purposes, and eventually replaced in the course of
6311 -- expansion, so they do not need to be attached to the tree and seen
6312 -- by the back-end. Keeping them internal also avoids spurious
6313 -- freezing problems. The declaration is inserted in the tree for
6314 -- analysis, and removed afterwards. If the equality operator comes
6315 -- from an explicit declaration, attach the inequality immediately
6316 -- after. Else the equality is inherited from a derived type
6317 -- declaration, so insert inequality after that declaration.
6319 if No (Alias (S)) then
6320 Insert_After (Unit_Declaration_Node (S), Decl);
6321 elsif Is_List_Member (Parent (S)) then
6322 Insert_After (Parent (S), Decl);
6324 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6327 Mark_Rewrite_Insertion (Decl);
6328 Set_Is_Intrinsic_Subprogram (Op_Name);
6331 Set_Has_Completion (Op_Name);
6332 Set_Corresponding_Equality (Op_Name, S);
6333 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6335 end Make_Inequality_Operator;
6337 ----------------------
6338 -- May_Need_Actuals --
6339 ----------------------
6341 procedure May_Need_Actuals (Fun : Entity_Id) is
6346 F := First_Formal (Fun);
6348 while Present (F) loop
6349 if No (Default_Value (F)) then
6357 Set_Needs_No_Actuals (Fun, B);
6358 end May_Need_Actuals;
6360 ---------------------
6361 -- Mode_Conformant --
6362 ---------------------
6364 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6367 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6369 end Mode_Conformant;
6371 ---------------------------
6372 -- New_Overloaded_Entity --
6373 ---------------------------
6375 procedure New_Overloaded_Entity
6377 Derived_Type : Entity_Id := Empty)
6379 Overridden_Subp : Entity_Id := Empty;
6380 -- Set if the current scope has an operation that is type-conformant
6381 -- with S, and becomes hidden by S.
6383 Is_Primitive_Subp : Boolean;
6384 -- Set to True if the new subprogram is primitive
6387 -- Entity that S overrides
6389 Prev_Vis : Entity_Id := Empty;
6390 -- Predecessor of E in Homonym chain
6392 procedure Check_For_Primitive_Subprogram
6393 (Is_Primitive : out Boolean;
6394 Is_Overriding : Boolean := False);
6395 -- If the subprogram being analyzed is a primitive operation of the type
6396 -- of a formal or result, set the Has_Primitive_Operations flag on the
6397 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6398 -- corresponding flag on the entity itself for later use.
6400 procedure Check_Synchronized_Overriding
6401 (Def_Id : Entity_Id;
6402 Overridden_Subp : out Entity_Id);
6403 -- First determine if Def_Id is an entry or a subprogram either defined
6404 -- in the scope of a task or protected type, or is a primitive of such
6405 -- a type. Check whether Def_Id overrides a subprogram of an interface
6406 -- implemented by the synchronized type, return the overridden entity
6409 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6410 -- Check that E is declared in the private part of the current package,
6411 -- or in the package body, where it may hide a previous declaration.
6412 -- We can't use In_Private_Part by itself because this flag is also
6413 -- set when freezing entities, so we must examine the place of the
6414 -- declaration in the tree, and recognize wrapper packages as well.
6416 ------------------------------------
6417 -- Check_For_Primitive_Subprogram --
6418 ------------------------------------
6420 procedure Check_For_Primitive_Subprogram
6421 (Is_Primitive : out Boolean;
6422 Is_Overriding : Boolean := False)
6428 function Visible_Part_Type (T : Entity_Id) return Boolean;
6429 -- Returns true if T is declared in the visible part of
6430 -- the current package scope; otherwise returns false.
6431 -- Assumes that T is declared in a package.
6433 procedure Check_Private_Overriding (T : Entity_Id);
6434 -- Checks that if a primitive abstract subprogram of a visible
6435 -- abstract type is declared in a private part, then it must
6436 -- override an abstract subprogram declared in the visible part.
6437 -- Also checks that if a primitive function with a controlling
6438 -- result is declared in a private part, then it must override
6439 -- a function declared in the visible part.
6441 ------------------------------
6442 -- Check_Private_Overriding --
6443 ------------------------------
6445 procedure Check_Private_Overriding (T : Entity_Id) is
6447 if Ekind (Current_Scope) = E_Package
6448 and then In_Private_Part (Current_Scope)
6449 and then Visible_Part_Type (T)
6450 and then not In_Instance
6452 if Is_Abstract_Type (T)
6453 and then Is_Abstract_Subprogram (S)
6454 and then (not Is_Overriding
6455 or else not Is_Abstract_Subprogram (E))
6457 Error_Msg_N ("abstract subprograms must be visible "
6458 & "(RM 3.9.3(10))!", S);
6460 elsif Ekind (S) = E_Function
6461 and then Is_Tagged_Type (T)
6462 and then T = Base_Type (Etype (S))
6463 and then not Is_Overriding
6466 ("private function with tagged result must"
6467 & " override visible-part function", S);
6469 ("\move subprogram to the visible part"
6470 & " (RM 3.9.3(10))", S);
6473 end Check_Private_Overriding;
6475 -----------------------
6476 -- Visible_Part_Type --
6477 -----------------------
6479 function Visible_Part_Type (T : Entity_Id) return Boolean is
6480 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6484 -- If the entity is a private type, then it must be
6485 -- declared in a visible part.
6487 if Ekind (T) in Private_Kind then
6491 -- Otherwise, we traverse the visible part looking for its
6492 -- corresponding declaration. We cannot use the declaration
6493 -- node directly because in the private part the entity of a
6494 -- private type is the one in the full view, which does not
6495 -- indicate that it is the completion of something visible.
6497 N := First (Visible_Declarations (Specification (P)));
6498 while Present (N) loop
6499 if Nkind (N) = N_Full_Type_Declaration
6500 and then Present (Defining_Identifier (N))
6501 and then T = Defining_Identifier (N)
6505 elsif Nkind_In (N, N_Private_Type_Declaration,
6506 N_Private_Extension_Declaration)
6507 and then Present (Defining_Identifier (N))
6508 and then T = Full_View (Defining_Identifier (N))
6517 end Visible_Part_Type;
6519 -- Start of processing for Check_For_Primitive_Subprogram
6522 Is_Primitive := False;
6524 if not Comes_From_Source (S) then
6527 -- If subprogram is at library level, it is not primitive operation
6529 elsif Current_Scope = Standard_Standard then
6532 elsif ((Ekind (Current_Scope) = E_Package
6533 or else Ekind (Current_Scope) = E_Generic_Package)
6534 and then not In_Package_Body (Current_Scope))
6535 or else Is_Overriding
6537 -- For function, check return type
6539 if Ekind (S) = E_Function then
6540 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6541 F_Typ := Designated_Type (Etype (S));
6546 B_Typ := Base_Type (F_Typ);
6548 if Scope (B_Typ) = Current_Scope
6549 and then not Is_Class_Wide_Type (B_Typ)
6550 and then not Is_Generic_Type (B_Typ)
6552 Is_Primitive := True;
6553 Set_Has_Primitive_Operations (B_Typ);
6554 Set_Is_Primitive (S);
6555 Check_Private_Overriding (B_Typ);
6559 -- For all subprograms, check formals
6561 Formal := First_Formal (S);
6562 while Present (Formal) loop
6563 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6564 F_Typ := Designated_Type (Etype (Formal));
6566 F_Typ := Etype (Formal);
6569 B_Typ := Base_Type (F_Typ);
6571 if Ekind (B_Typ) = E_Access_Subtype then
6572 B_Typ := Base_Type (B_Typ);
6575 if Scope (B_Typ) = Current_Scope
6576 and then not Is_Class_Wide_Type (B_Typ)
6577 and then not Is_Generic_Type (B_Typ)
6579 Is_Primitive := True;
6580 Set_Is_Primitive (S);
6581 Set_Has_Primitive_Operations (B_Typ);
6582 Check_Private_Overriding (B_Typ);
6585 Next_Formal (Formal);
6588 end Check_For_Primitive_Subprogram;
6590 -----------------------------------
6591 -- Check_Synchronized_Overriding --
6592 -----------------------------------
6594 procedure Check_Synchronized_Overriding
6595 (Def_Id : Entity_Id;
6596 Overridden_Subp : out Entity_Id)
6598 Ifaces_List : Elist_Id;
6602 function Matches_Prefixed_View_Profile
6603 (Prim_Params : List_Id;
6604 Iface_Params : List_Id) return Boolean;
6605 -- Determine whether a subprogram's parameter profile Prim_Params
6606 -- matches that of a potentially overridden interface subprogram
6607 -- Iface_Params. Also determine if the type of first parameter of
6608 -- Iface_Params is an implemented interface.
6610 -----------------------------------
6611 -- Matches_Prefixed_View_Profile --
6612 -----------------------------------
6614 function Matches_Prefixed_View_Profile
6615 (Prim_Params : List_Id;
6616 Iface_Params : List_Id) return Boolean
6618 Iface_Id : Entity_Id;
6619 Iface_Param : Node_Id;
6620 Iface_Typ : Entity_Id;
6621 Prim_Id : Entity_Id;
6622 Prim_Param : Node_Id;
6623 Prim_Typ : Entity_Id;
6625 function Is_Implemented
6626 (Ifaces_List : Elist_Id;
6627 Iface : Entity_Id) return Boolean;
6628 -- Determine if Iface is implemented by the current task or
6631 --------------------
6632 -- Is_Implemented --
6633 --------------------
6635 function Is_Implemented
6636 (Ifaces_List : Elist_Id;
6637 Iface : Entity_Id) return Boolean
6639 Iface_Elmt : Elmt_Id;
6642 Iface_Elmt := First_Elmt (Ifaces_List);
6643 while Present (Iface_Elmt) loop
6644 if Node (Iface_Elmt) = Iface then
6648 Next_Elmt (Iface_Elmt);
6654 -- Start of processing for Matches_Prefixed_View_Profile
6657 Iface_Param := First (Iface_Params);
6658 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6660 if Is_Access_Type (Iface_Typ) then
6661 Iface_Typ := Designated_Type (Iface_Typ);
6664 Prim_Param := First (Prim_Params);
6666 -- The first parameter of the potentially overridden subprogram
6667 -- must be an interface implemented by Prim.
6669 if not Is_Interface (Iface_Typ)
6670 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6675 -- The checks on the object parameters are done, move onto the
6676 -- rest of the parameters.
6678 if not In_Scope then
6679 Prim_Param := Next (Prim_Param);
6682 Iface_Param := Next (Iface_Param);
6683 while Present (Iface_Param) and then Present (Prim_Param) loop
6684 Iface_Id := Defining_Identifier (Iface_Param);
6685 Iface_Typ := Find_Parameter_Type (Iface_Param);
6687 Prim_Id := Defining_Identifier (Prim_Param);
6688 Prim_Typ := Find_Parameter_Type (Prim_Param);
6690 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6691 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6692 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6694 Iface_Typ := Designated_Type (Iface_Typ);
6695 Prim_Typ := Designated_Type (Prim_Typ);
6698 -- Case of multiple interface types inside a parameter profile
6700 -- (Obj_Param : in out Iface; ...; Param : Iface)
6702 -- If the interface type is implemented, then the matching type
6703 -- in the primitive should be the implementing record type.
6705 if Ekind (Iface_Typ) = E_Record_Type
6706 and then Is_Interface (Iface_Typ)
6707 and then Is_Implemented (Ifaces_List, Iface_Typ)
6709 if Prim_Typ /= Typ then
6713 -- The two parameters must be both mode and subtype conformant
6715 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6717 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6726 -- One of the two lists contains more parameters than the other
6728 if Present (Iface_Param) or else Present (Prim_Param) then
6733 end Matches_Prefixed_View_Profile;
6735 -- Start of processing for Check_Synchronized_Overriding
6738 Overridden_Subp := Empty;
6740 -- Def_Id must be an entry or a subprogram. We should skip predefined
6741 -- primitives internally generated by the frontend; however at this
6742 -- stage predefined primitives are still not fully decorated. As a
6743 -- minor optimization we skip here internally generated subprograms.
6745 if (Ekind (Def_Id) /= E_Entry
6746 and then Ekind (Def_Id) /= E_Function
6747 and then Ekind (Def_Id) /= E_Procedure)
6748 or else not Comes_From_Source (Def_Id)
6753 -- Search for the concurrent declaration since it contains the list
6754 -- of all implemented interfaces. In this case, the subprogram is
6755 -- declared within the scope of a protected or a task type.
6757 if Present (Scope (Def_Id))
6758 and then Is_Concurrent_Type (Scope (Def_Id))
6759 and then not Is_Generic_Actual_Type (Scope (Def_Id))
6761 Typ := Scope (Def_Id);
6764 -- The enclosing scope is not a synchronized type and the subprogram
6767 elsif No (First_Formal (Def_Id)) then
6770 -- The subprogram has formals and hence it may be a primitive of a
6774 Typ := Etype (First_Formal (Def_Id));
6776 if Is_Access_Type (Typ) then
6777 Typ := Directly_Designated_Type (Typ);
6780 if Is_Concurrent_Type (Typ)
6781 and then not Is_Generic_Actual_Type (Typ)
6785 -- This case occurs when the concurrent type is declared within
6786 -- a generic unit. As a result the corresponding record has been
6787 -- built and used as the type of the first formal, we just have
6788 -- to retrieve the corresponding concurrent type.
6790 elsif Is_Concurrent_Record_Type (Typ)
6791 and then Present (Corresponding_Concurrent_Type (Typ))
6793 Typ := Corresponding_Concurrent_Type (Typ);
6801 -- There is no overriding to check if is an inherited operation in a
6802 -- type derivation on for a generic actual.
6804 Collect_Interfaces (Typ, Ifaces_List);
6806 if Is_Empty_Elmt_List (Ifaces_List) then
6810 -- Determine whether entry or subprogram Def_Id overrides a primitive
6811 -- operation that belongs to one of the interfaces in Ifaces_List.
6814 Candidate : Entity_Id := Empty;
6815 Hom : Entity_Id := Empty;
6816 Iface_Typ : Entity_Id;
6817 Subp : Entity_Id := Empty;
6820 -- Traverse the homonym chain, looking at a potentially
6821 -- overridden subprogram that belongs to an implemented
6824 Hom := Current_Entity_In_Scope (Def_Id);
6825 while Present (Hom) loop
6829 or else not Is_Overloadable (Subp)
6830 or else not Is_Primitive (Subp)
6831 or else not Is_Dispatching_Operation (Subp)
6832 or else not Is_Interface (Find_Dispatching_Type (Subp))
6836 -- Entries and procedures can override abstract or null
6837 -- interface procedures
6839 elsif (Ekind (Def_Id) = E_Procedure
6840 or else Ekind (Def_Id) = E_Entry)
6841 and then Ekind (Subp) = E_Procedure
6842 and then Matches_Prefixed_View_Profile
6843 (Parameter_Specifications (Parent (Def_Id)),
6844 Parameter_Specifications (Parent (Subp)))
6848 -- For an overridden subprogram Subp, check whether the mode
6849 -- of its first parameter is correct depending on the kind
6850 -- of synchronized type.
6853 Formal : constant Node_Id := First_Formal (Candidate);
6856 -- In order for an entry or a protected procedure to
6857 -- override, the first parameter of the overridden
6858 -- routine must be of mode "out", "in out" or
6859 -- access-to-variable.
6861 if (Ekind (Candidate) = E_Entry
6862 or else Ekind (Candidate) = E_Procedure)
6863 and then Is_Protected_Type (Typ)
6864 and then Ekind (Formal) /= E_In_Out_Parameter
6865 and then Ekind (Formal) /= E_Out_Parameter
6866 and then Nkind (Parameter_Type (Parent (Formal)))
6867 /= N_Access_Definition
6871 -- All other cases are OK since a task entry or routine
6872 -- does not have a restriction on the mode of the first
6873 -- parameter of the overridden interface routine.
6876 Overridden_Subp := Candidate;
6881 -- Functions can override abstract interface functions
6883 elsif Ekind (Def_Id) = E_Function
6884 and then Ekind (Subp) = E_Function
6885 and then Matches_Prefixed_View_Profile
6886 (Parameter_Specifications (Parent (Def_Id)),
6887 Parameter_Specifications (Parent (Subp)))
6888 and then Etype (Result_Definition (Parent (Def_Id))) =
6889 Etype (Result_Definition (Parent (Subp)))
6891 Overridden_Subp := Subp;
6895 Hom := Homonym (Hom);
6898 -- After examining all candidates for overriding, we are
6899 -- left with the best match which is a mode incompatible
6900 -- interface routine. Do not emit an error if the Expander
6901 -- is active since this error will be detected later on
6902 -- after all concurrent types are expanded and all wrappers
6903 -- are built. This check is meant for spec-only
6906 if Present (Candidate)
6907 and then not Expander_Active
6910 Find_Parameter_Type (Parent (First_Formal (Candidate)));
6912 -- Def_Id is primitive of a protected type, declared
6913 -- inside the type, and the candidate is primitive of a
6914 -- limited or synchronized interface.
6917 and then Is_Protected_Type (Typ)
6919 (Is_Limited_Interface (Iface_Typ)
6920 or else Is_Protected_Interface (Iface_Typ)
6921 or else Is_Synchronized_Interface (Iface_Typ)
6922 or else Is_Task_Interface (Iface_Typ))
6924 -- Must reword this message, comma before to in -gnatj
6928 ("first formal of & must be of mode `OUT`, `IN OUT`"
6929 & " or access-to-variable", Typ, Candidate);
6931 ("\to be overridden by protected procedure or entry "
6932 & "(RM 9.4(11.9/2))", Typ);
6936 Overridden_Subp := Candidate;
6939 end Check_Synchronized_Overriding;
6941 ----------------------------
6942 -- Is_Private_Declaration --
6943 ----------------------------
6945 function Is_Private_Declaration (E : Entity_Id) return Boolean is
6946 Priv_Decls : List_Id;
6947 Decl : constant Node_Id := Unit_Declaration_Node (E);
6950 if Is_Package_Or_Generic_Package (Current_Scope)
6951 and then In_Private_Part (Current_Scope)
6954 Private_Declarations (
6955 Specification (Unit_Declaration_Node (Current_Scope)));
6957 return In_Package_Body (Current_Scope)
6959 (Is_List_Member (Decl)
6960 and then List_Containing (Decl) = Priv_Decls)
6961 or else (Nkind (Parent (Decl)) = N_Package_Specification
6962 and then not Is_Compilation_Unit (
6963 Defining_Entity (Parent (Decl)))
6964 and then List_Containing (Parent (Parent (Decl)))
6969 end Is_Private_Declaration;
6971 -- Start of processing for New_Overloaded_Entity
6974 -- We need to look for an entity that S may override. This must be a
6975 -- homonym in the current scope, so we look for the first homonym of
6976 -- S in the current scope as the starting point for the search.
6978 E := Current_Entity_In_Scope (S);
6980 -- If there is no homonym then this is definitely not overriding
6983 Enter_Overloaded_Entity (S);
6984 Check_Dispatching_Operation (S, Empty);
6985 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
6987 -- If subprogram has an explicit declaration, check whether it
6988 -- has an overriding indicator.
6990 if Comes_From_Source (S) then
6991 Check_Synchronized_Overriding (S, Overridden_Subp);
6992 Check_Overriding_Indicator
6993 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
6996 -- If there is a homonym that is not overloadable, then we have an
6997 -- error, except for the special cases checked explicitly below.
6999 elsif not Is_Overloadable (E) then
7001 -- Check for spurious conflict produced by a subprogram that has the
7002 -- same name as that of the enclosing generic package. The conflict
7003 -- occurs within an instance, between the subprogram and the renaming
7004 -- declaration for the package. After the subprogram, the package
7005 -- renaming declaration becomes hidden.
7007 if Ekind (E) = E_Package
7008 and then Present (Renamed_Object (E))
7009 and then Renamed_Object (E) = Current_Scope
7010 and then Nkind (Parent (Renamed_Object (E))) =
7011 N_Package_Specification
7012 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7015 Set_Is_Immediately_Visible (E, False);
7016 Enter_Overloaded_Entity (S);
7017 Set_Homonym (S, Homonym (E));
7018 Check_Dispatching_Operation (S, Empty);
7019 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7021 -- If the subprogram is implicit it is hidden by the previous
7022 -- declaration. However if it is dispatching, it must appear in the
7023 -- dispatch table anyway, because it can be dispatched to even if it
7024 -- cannot be called directly.
7026 elsif Present (Alias (S))
7027 and then not Comes_From_Source (S)
7029 Set_Scope (S, Current_Scope);
7031 if Is_Dispatching_Operation (Alias (S)) then
7032 Check_Dispatching_Operation (S, Empty);
7038 Error_Msg_Sloc := Sloc (E);
7040 -- Generate message, with useful additional warning if in generic
7042 if Is_Generic_Unit (E) then
7043 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7044 Error_Msg_N ("\& conflicts with declaration#", S);
7046 Error_Msg_N ("& conflicts with declaration#", S);
7052 -- E exists and is overloadable
7055 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
7056 -- need no check against the homonym chain. They are directly added
7057 -- to the list of primitive operations of Derived_Type.
7059 if Ada_Version >= Ada_05
7060 and then Present (Derived_Type)
7061 and then Is_Dispatching_Operation (Alias (S))
7062 and then Present (Find_Dispatching_Type (Alias (S)))
7063 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7065 goto Add_New_Entity;
7068 Check_Synchronized_Overriding (S, Overridden_Subp);
7070 -- Loop through E and its homonyms to determine if any of them is
7071 -- the candidate for overriding by S.
7073 while Present (E) loop
7075 -- Definitely not interesting if not in the current scope
7077 if Scope (E) /= Current_Scope then
7080 -- Check if we have type conformance
7082 elsif Type_Conformant (E, S) then
7084 -- If the old and new entities have the same profile and one
7085 -- is not the body of the other, then this is an error, unless
7086 -- one of them is implicitly declared.
7088 -- There are some cases when both can be implicit, for example
7089 -- when both a literal and a function that overrides it are
7090 -- inherited in a derivation, or when an inherited operation
7091 -- of a tagged full type overrides the inherited operation of
7092 -- a private extension. Ada 83 had a special rule for the
7093 -- literal case. In Ada95, the later implicit operation hides
7094 -- the former, and the literal is always the former. In the
7095 -- odd case where both are derived operations declared at the
7096 -- same point, both operations should be declared, and in that
7097 -- case we bypass the following test and proceed to the next
7098 -- part (this can only occur for certain obscure cases
7099 -- involving homographs in instances and can't occur for
7100 -- dispatching operations ???). Note that the following
7101 -- condition is less than clear. For example, it's not at all
7102 -- clear why there's a test for E_Entry here. ???
7104 if Present (Alias (S))
7105 and then (No (Alias (E))
7106 or else Comes_From_Source (E)
7107 or else Is_Dispatching_Operation (E))
7109 (Ekind (E) = E_Entry
7110 or else Ekind (E) /= E_Enumeration_Literal)
7112 -- When an derived operation is overloaded it may be due to
7113 -- the fact that the full view of a private extension
7114 -- re-inherits. It has to be dealt with.
7116 if Is_Package_Or_Generic_Package (Current_Scope)
7117 and then In_Private_Part (Current_Scope)
7119 Check_Operation_From_Private_View (S, E);
7122 -- In any case the implicit operation remains hidden by
7123 -- the existing declaration, which is overriding.
7125 Set_Is_Overriding_Operation (E);
7127 if Comes_From_Source (E) then
7128 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7130 -- Indicate that E overrides the operation from which
7133 if Present (Alias (S)) then
7134 Set_Overridden_Operation (E, Alias (S));
7136 Set_Overridden_Operation (E, S);
7142 -- Within an instance, the renaming declarations for
7143 -- actual subprograms may become ambiguous, but they do
7144 -- not hide each other.
7146 elsif Ekind (E) /= E_Entry
7147 and then not Comes_From_Source (E)
7148 and then not Is_Generic_Instance (E)
7149 and then (Present (Alias (E))
7150 or else Is_Intrinsic_Subprogram (E))
7151 and then (not In_Instance
7152 or else No (Parent (E))
7153 or else Nkind (Unit_Declaration_Node (E)) /=
7154 N_Subprogram_Renaming_Declaration)
7156 -- A subprogram child unit is not allowed to override
7157 -- an inherited subprogram (10.1.1(20)).
7159 if Is_Child_Unit (S) then
7161 ("child unit overrides inherited subprogram in parent",
7166 if Is_Non_Overriding_Operation (E, S) then
7167 Enter_Overloaded_Entity (S);
7168 if No (Derived_Type)
7169 or else Is_Tagged_Type (Derived_Type)
7171 Check_Dispatching_Operation (S, Empty);
7177 -- E is a derived operation or an internal operator which
7178 -- is being overridden. Remove E from further visibility.
7179 -- Furthermore, if E is a dispatching operation, it must be
7180 -- replaced in the list of primitive operations of its type
7181 -- (see Override_Dispatching_Operation).
7183 Overridden_Subp := E;
7189 Prev := First_Entity (Current_Scope);
7191 while Present (Prev)
7192 and then Next_Entity (Prev) /= E
7197 -- It is possible for E to be in the current scope and
7198 -- yet not in the entity chain. This can only occur in a
7199 -- generic context where E is an implicit concatenation
7200 -- in the formal part, because in a generic body the
7201 -- entity chain starts with the formals.
7204 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7206 -- E must be removed both from the entity_list of the
7207 -- current scope, and from the visibility chain
7209 if Debug_Flag_E then
7210 Write_Str ("Override implicit operation ");
7211 Write_Int (Int (E));
7215 -- If E is a predefined concatenation, it stands for four
7216 -- different operations. As a result, a single explicit
7217 -- declaration does not hide it. In a possible ambiguous
7218 -- situation, Disambiguate chooses the user-defined op,
7219 -- so it is correct to retain the previous internal one.
7221 if Chars (E) /= Name_Op_Concat
7222 or else Ekind (E) /= E_Operator
7224 -- For nondispatching derived operations that are
7225 -- overridden by a subprogram declared in the private
7226 -- part of a package, we retain the derived
7227 -- subprogram but mark it as not immediately visible.
7228 -- If the derived operation was declared in the
7229 -- visible part then this ensures that it will still
7230 -- be visible outside the package with the proper
7231 -- signature (calls from outside must also be
7232 -- directed to this version rather than the
7233 -- overriding one, unlike the dispatching case).
7234 -- Calls from inside the package will still resolve
7235 -- to the overriding subprogram since the derived one
7236 -- is marked as not visible within the package.
7238 -- If the private operation is dispatching, we achieve
7239 -- the overriding by keeping the implicit operation
7240 -- but setting its alias to be the overriding one. In
7241 -- this fashion the proper body is executed in all
7242 -- cases, but the original signature is used outside
7245 -- If the overriding is not in the private part, we
7246 -- remove the implicit operation altogether.
7248 if Is_Private_Declaration (S) then
7250 if not Is_Dispatching_Operation (E) then
7251 Set_Is_Immediately_Visible (E, False);
7253 -- Work done in Override_Dispatching_Operation,
7254 -- so nothing else need to be done here.
7260 -- Find predecessor of E in Homonym chain
7262 if E = Current_Entity (E) then
7265 Prev_Vis := Current_Entity (E);
7266 while Homonym (Prev_Vis) /= E loop
7267 Prev_Vis := Homonym (Prev_Vis);
7271 if Prev_Vis /= Empty then
7273 -- Skip E in the visibility chain
7275 Set_Homonym (Prev_Vis, Homonym (E));
7278 Set_Name_Entity_Id (Chars (E), Homonym (E));
7281 Set_Next_Entity (Prev, Next_Entity (E));
7283 if No (Next_Entity (Prev)) then
7284 Set_Last_Entity (Current_Scope, Prev);
7290 Enter_Overloaded_Entity (S);
7291 Set_Is_Overriding_Operation (S);
7292 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7294 -- Indicate that S overrides the operation from which
7297 if Comes_From_Source (S) then
7298 if Present (Alias (E)) then
7299 Set_Overridden_Operation (S, Alias (E));
7301 Set_Overridden_Operation (S, E);
7305 if Is_Dispatching_Operation (E) then
7307 -- An overriding dispatching subprogram inherits the
7308 -- convention of the overridden subprogram (by
7311 Set_Convention (S, Convention (E));
7312 Check_Dispatching_Operation (S, E);
7315 Check_Dispatching_Operation (S, Empty);
7318 Check_For_Primitive_Subprogram
7319 (Is_Primitive_Subp, Is_Overriding => True);
7320 goto Check_Inequality;
7323 -- Apparent redeclarations in instances can occur when two
7324 -- formal types get the same actual type. The subprograms in
7325 -- in the instance are legal, even if not callable from the
7326 -- outside. Calls from within are disambiguated elsewhere.
7327 -- For dispatching operations in the visible part, the usual
7328 -- rules apply, and operations with the same profile are not
7331 elsif (In_Instance_Visible_Part
7332 and then not Is_Dispatching_Operation (E))
7333 or else In_Instance_Not_Visible
7337 -- Here we have a real error (identical profile)
7340 Error_Msg_Sloc := Sloc (E);
7342 -- Avoid cascaded errors if the entity appears in
7343 -- subsequent calls.
7345 Set_Scope (S, Current_Scope);
7347 -- Generate error, with extra useful warning for the case
7348 -- of a generic instance with no completion.
7350 if Is_Generic_Instance (S)
7351 and then not Has_Completion (E)
7354 ("instantiation cannot provide body for&", S);
7355 Error_Msg_N ("\& conflicts with declaration#", S);
7357 Error_Msg_N ("& conflicts with declaration#", S);
7364 -- If one subprogram has an access parameter and the other
7365 -- a parameter of an access type, calls to either might be
7366 -- ambiguous. Verify that parameters match except for the
7367 -- access parameter.
7369 if May_Hide_Profile then
7374 F1 := First_Formal (S);
7375 F2 := First_Formal (E);
7376 while Present (F1) and then Present (F2) loop
7377 if Is_Access_Type (Etype (F1)) then
7378 if not Is_Access_Type (Etype (F2))
7379 or else not Conforming_Types
7380 (Designated_Type (Etype (F1)),
7381 Designated_Type (Etype (F2)),
7384 May_Hide_Profile := False;
7388 not Conforming_Types
7389 (Etype (F1), Etype (F2), Type_Conformant)
7391 May_Hide_Profile := False;
7402 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7413 -- On exit, we know that S is a new entity
7415 Enter_Overloaded_Entity (S);
7416 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7417 Check_Overriding_Indicator
7418 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7420 -- If S is a derived operation for an untagged type then by
7421 -- definition it's not a dispatching operation (even if the parent
7422 -- operation was dispatching), so we don't call
7423 -- Check_Dispatching_Operation in that case.
7425 if No (Derived_Type)
7426 or else Is_Tagged_Type (Derived_Type)
7428 Check_Dispatching_Operation (S, Empty);
7432 -- If this is a user-defined equality operator that is not a derived
7433 -- subprogram, create the corresponding inequality. If the operation is
7434 -- dispatching, the expansion is done elsewhere, and we do not create
7435 -- an explicit inequality operation.
7437 <<Check_Inequality>>
7438 if Chars (S) = Name_Op_Eq
7439 and then Etype (S) = Standard_Boolean
7440 and then Present (Parent (S))
7441 and then not Is_Dispatching_Operation (S)
7443 Make_Inequality_Operator (S);
7445 end New_Overloaded_Entity;
7447 ---------------------
7448 -- Process_Formals --
7449 ---------------------
7451 procedure Process_Formals
7453 Related_Nod : Node_Id)
7455 Param_Spec : Node_Id;
7457 Formal_Type : Entity_Id;
7461 Num_Out_Params : Nat := 0;
7462 First_Out_Param : Entity_Id := Empty;
7463 -- Used for setting Is_Only_Out_Parameter
7465 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7466 -- Check whether the default has a class-wide type. After analysis the
7467 -- default has the type of the formal, so we must also check explicitly
7468 -- for an access attribute.
7470 ---------------------------
7471 -- Is_Class_Wide_Default --
7472 ---------------------------
7474 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7476 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7477 or else (Nkind (D) = N_Attribute_Reference
7478 and then Attribute_Name (D) = Name_Access
7479 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7480 end Is_Class_Wide_Default;
7482 -- Start of processing for Process_Formals
7485 -- In order to prevent premature use of the formals in the same formal
7486 -- part, the Ekind is left undefined until all default expressions are
7487 -- analyzed. The Ekind is established in a separate loop at the end.
7489 Param_Spec := First (T);
7490 while Present (Param_Spec) loop
7491 Formal := Defining_Identifier (Param_Spec);
7492 Set_Never_Set_In_Source (Formal, True);
7493 Enter_Name (Formal);
7495 -- Case of ordinary parameters
7497 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7498 Find_Type (Parameter_Type (Param_Spec));
7499 Ptype := Parameter_Type (Param_Spec);
7501 if Ptype = Error then
7505 Formal_Type := Entity (Ptype);
7507 if Is_Incomplete_Type (Formal_Type)
7509 (Is_Class_Wide_Type (Formal_Type)
7510 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7512 -- Ada 2005 (AI-326): Tagged incomplete types allowed
7514 if Is_Tagged_Type (Formal_Type) then
7517 -- Special handling of Value_Type for CIL case
7519 elsif Is_Value_Type (Formal_Type) then
7522 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7523 N_Access_Procedure_Definition)
7525 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
7527 -- An incomplete type that is not tagged is allowed in an
7528 -- access-to-subprogram type only if it is a local declaration
7529 -- with a forthcoming completion (3.10.1 (9.2/2)).
7531 elsif Scope (Formal_Type) /= Scope (Current_Scope) then
7533 ("invalid use of limited view of type", Param_Spec);
7536 elsif Ekind (Formal_Type) = E_Void then
7537 Error_Msg_NE ("premature use of&",
7538 Parameter_Type (Param_Spec), Formal_Type);
7541 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7542 -- declaration corresponding to the null-excluding type of the
7543 -- formal in the enclosing scope. Finally, replace the parameter
7544 -- type of the formal with the internal subtype.
7546 if Ada_Version >= Ada_05
7547 and then Null_Exclusion_Present (Param_Spec)
7549 if not Is_Access_Type (Formal_Type) then
7551 ("`NOT NULL` allowed only for an access type", Param_Spec);
7554 if Can_Never_Be_Null (Formal_Type)
7555 and then Comes_From_Source (Related_Nod)
7558 ("`NOT NULL` not allowed (& already excludes null)",
7564 Create_Null_Excluding_Itype
7566 Related_Nod => Related_Nod,
7567 Scope_Id => Scope (Current_Scope));
7569 -- If the designated type of the itype is an itype we
7570 -- decorate it with the Has_Delayed_Freeze attribute to
7571 -- avoid problems with the backend.
7574 -- type T is access procedure;
7575 -- procedure Op (O : not null T);
7577 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
7578 Set_Has_Delayed_Freeze (Formal_Type);
7583 -- An access formal type
7587 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7589 -- No need to continue if we already notified errors
7591 if not Present (Formal_Type) then
7595 -- Ada 2005 (AI-254)
7598 AD : constant Node_Id :=
7599 Access_To_Subprogram_Definition
7600 (Parameter_Type (Param_Spec));
7602 if Present (AD) and then Protected_Present (AD) then
7604 Replace_Anonymous_Access_To_Protected_Subprogram
7610 Set_Etype (Formal, Formal_Type);
7611 Default := Expression (Param_Spec);
7613 if Present (Default) then
7614 if Out_Present (Param_Spec) then
7616 ("default initialization only allowed for IN parameters",
7620 -- Do the special preanalysis of the expression (see section on
7621 -- "Handling of Default Expressions" in the spec of package Sem).
7623 Preanalyze_Spec_Expression (Default, Formal_Type);
7625 -- An access to constant cannot be the default for
7626 -- an access parameter that is an access to variable.
7628 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7629 and then not Is_Access_Constant (Formal_Type)
7630 and then Is_Access_Type (Etype (Default))
7631 and then Is_Access_Constant (Etype (Default))
7634 ("formal that is access to variable cannot be initialized " &
7635 "with an access-to-constant expression", Default);
7638 -- Check that the designated type of an access parameter's default
7639 -- is not a class-wide type unless the parameter's designated type
7640 -- is also class-wide.
7642 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7643 and then not From_With_Type (Formal_Type)
7644 and then Is_Class_Wide_Default (Default)
7645 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
7648 ("access to class-wide expression not allowed here", Default);
7652 -- Ada 2005 (AI-231): Static checks
7654 if Ada_Version >= Ada_05
7655 and then Is_Access_Type (Etype (Formal))
7656 and then Can_Never_Be_Null (Etype (Formal))
7658 Null_Exclusion_Static_Checks (Param_Spec);
7665 -- If this is the formal part of a function specification, analyze the
7666 -- subtype mark in the context where the formals are visible but not
7667 -- yet usable, and may hide outer homographs.
7669 if Nkind (Related_Nod) = N_Function_Specification then
7670 Analyze_Return_Type (Related_Nod);
7673 -- Now set the kind (mode) of each formal
7675 Param_Spec := First (T);
7677 while Present (Param_Spec) loop
7678 Formal := Defining_Identifier (Param_Spec);
7679 Set_Formal_Mode (Formal);
7681 if Ekind (Formal) = E_In_Parameter then
7682 Set_Default_Value (Formal, Expression (Param_Spec));
7684 if Present (Expression (Param_Spec)) then
7685 Default := Expression (Param_Spec);
7687 if Is_Scalar_Type (Etype (Default)) then
7689 (Parameter_Type (Param_Spec)) /= N_Access_Definition
7691 Formal_Type := Entity (Parameter_Type (Param_Spec));
7694 Formal_Type := Access_Definition
7695 (Related_Nod, Parameter_Type (Param_Spec));
7698 Apply_Scalar_Range_Check (Default, Formal_Type);
7702 elsif Ekind (Formal) = E_Out_Parameter then
7703 Num_Out_Params := Num_Out_Params + 1;
7705 if Num_Out_Params = 1 then
7706 First_Out_Param := Formal;
7709 elsif Ekind (Formal) = E_In_Out_Parameter then
7710 Num_Out_Params := Num_Out_Params + 1;
7716 if Present (First_Out_Param) and then Num_Out_Params = 1 then
7717 Set_Is_Only_Out_Parameter (First_Out_Param);
7719 end Process_Formals;
7725 procedure Process_PPCs
7727 Spec_Id : Entity_Id;
7728 Body_Id : Entity_Id)
7730 Loc : constant Source_Ptr := Sloc (N);
7732 Plist : List_Id := No_List;
7736 function Grab_PPC (Nam : Name_Id) return Node_Id;
7737 -- Prag contains an analyzed precondition or postcondition pragma.
7738 -- This function copies the pragma, changes it to the corresponding
7739 -- Check pragma and returns the Check pragma as the result. The
7740 -- argument Nam is either Name_Precondition or Name_Postcondition.
7746 function Grab_PPC (Nam : Name_Id) return Node_Id is
7747 CP : constant Node_Id := New_Copy_Tree (Prag);
7750 -- Set Analyzed to false, since we want to reanalyze the check
7751 -- procedure. Note that it is only at the outer level that we
7752 -- do this fiddling, for the spec cases, the already preanalyzed
7753 -- parameters are not affected.
7754 -- For a postcondition pragma within a generic, preserve the pragma
7755 -- for later expansion.
7757 Set_Analyzed (CP, False);
7759 if Nam = Name_Postcondition
7760 and then not Expander_Active
7765 -- Change pragma into corresponding pragma Check
7767 Prepend_To (Pragma_Argument_Associations (CP),
7768 Make_Pragma_Argument_Association (Sloc (Prag),
7770 Make_Identifier (Loc,
7772 Set_Pragma_Identifier (CP,
7773 Make_Identifier (Sloc (Prag),
7774 Chars => Name_Check));
7779 -- Start of processing for Process_PPCs
7782 -- Grab preconditions from spec
7784 if Present (Spec_Id) then
7786 -- Loop through PPC pragmas from spec. Note that preconditions from
7787 -- the body will be analyzed and converted when we scan the body
7788 -- declarations below.
7790 Prag := Spec_PPC_List (Spec_Id);
7791 while Present (Prag) loop
7792 if Pragma_Name (Prag) = Name_Precondition
7793 and then PPC_Enabled (Prag)
7795 -- Add pragma Check at the start of the declarations of N.
7796 -- Note that this processing reverses the order of the list,
7797 -- which is what we want since new entries were chained to
7798 -- the head of the list.
7800 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
7803 Prag := Next_Pragma (Prag);
7807 -- Build postconditions procedure if needed and prepend the following
7808 -- declaration to the start of the declarations for the subprogram.
7810 -- procedure _postconditions [(_Result : resulttype)] is
7812 -- pragma Check (Postcondition, condition [,message]);
7813 -- pragma Check (Postcondition, condition [,message]);
7817 -- First we deal with the postconditions in the body
7819 if Is_Non_Empty_List (Declarations (N)) then
7821 -- Loop through declarations
7823 Prag := First (Declarations (N));
7824 while Present (Prag) loop
7825 if Nkind (Prag) = N_Pragma then
7827 -- If pragma, capture if enabled postcondition, else ignore
7829 if Pragma_Name (Prag) = Name_Postcondition
7830 and then Check_Enabled (Name_Postcondition)
7832 if Plist = No_List then
7833 Plist := Empty_List;
7838 -- If expansion is disabled, as in a generic unit,
7839 -- save pragma for later expansion.
7841 if not Expander_Active then
7842 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
7844 Append (Grab_PPC (Name_Postcondition), Plist);
7850 -- Not a pragma, if comes from source, then end scan
7852 elsif Comes_From_Source (Prag) then
7855 -- Skip stuff not coming from source
7863 -- Now deal with any postconditions from the spec
7865 if Present (Spec_Id) then
7867 -- Loop through PPC pragmas from spec
7869 Prag := Spec_PPC_List (Spec_Id);
7870 while Present (Prag) loop
7871 if Pragma_Name (Prag) = Name_Postcondition
7872 and then PPC_Enabled (Prag)
7874 if Plist = No_List then
7875 Plist := Empty_List;
7878 if not Expander_Active then
7879 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
7881 Append (Grab_PPC (Name_Postcondition), Plist);
7885 Prag := Next_Pragma (Prag);
7889 -- If we had any postconditions and expansion is enabled,, build
7890 -- the Postconditions procedure.
7893 and then Expander_Active
7895 Subp := Defining_Entity (N);
7897 if Etype (Subp) /= Standard_Void_Type then
7899 Make_Parameter_Specification (Loc,
7900 Defining_Identifier =>
7901 Make_Defining_Identifier (Loc,
7902 Chars => Name_uResult),
7903 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
7908 Prepend_To (Declarations (N),
7909 Make_Subprogram_Body (Loc,
7911 Make_Procedure_Specification (Loc,
7912 Defining_Unit_Name =>
7913 Make_Defining_Identifier (Loc,
7914 Chars => Name_uPostconditions),
7915 Parameter_Specifications => Parms),
7917 Declarations => Empty_List,
7919 Handled_Statement_Sequence =>
7920 Make_Handled_Sequence_Of_Statements (Loc,
7921 Statements => Plist)));
7923 if Present (Spec_Id) then
7924 Set_Has_Postconditions (Spec_Id);
7926 Set_Has_Postconditions (Body_Id);
7931 ----------------------------
7932 -- Reference_Body_Formals --
7933 ----------------------------
7935 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
7940 if Error_Posted (Spec) then
7944 -- Iterate over both lists. They may be of different lengths if the two
7945 -- specs are not conformant.
7947 Fs := First_Formal (Spec);
7948 Fb := First_Formal (Bod);
7949 while Present (Fs) and then Present (Fb) loop
7950 Generate_Reference (Fs, Fb, 'b');
7953 Style.Check_Identifier (Fb, Fs);
7956 Set_Spec_Entity (Fb, Fs);
7957 Set_Referenced (Fs, False);
7961 end Reference_Body_Formals;
7963 -------------------------
7964 -- Set_Actual_Subtypes --
7965 -------------------------
7967 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
7968 Loc : constant Source_Ptr := Sloc (N);
7972 First_Stmt : Node_Id := Empty;
7973 AS_Needed : Boolean;
7976 -- If this is an empty initialization procedure, no need to create
7977 -- actual subtypes (small optimization).
7979 if Ekind (Subp) = E_Procedure
7980 and then Is_Null_Init_Proc (Subp)
7985 Formal := First_Formal (Subp);
7986 while Present (Formal) loop
7987 T := Etype (Formal);
7989 -- We never need an actual subtype for a constrained formal
7991 if Is_Constrained (T) then
7994 -- If we have unknown discriminants, then we do not need an actual
7995 -- subtype, or more accurately we cannot figure it out! Note that
7996 -- all class-wide types have unknown discriminants.
7998 elsif Has_Unknown_Discriminants (T) then
8001 -- At this stage we have an unconstrained type that may need an
8002 -- actual subtype. For sure the actual subtype is needed if we have
8003 -- an unconstrained array type.
8005 elsif Is_Array_Type (T) then
8008 -- The only other case needing an actual subtype is an unconstrained
8009 -- record type which is an IN parameter (we cannot generate actual
8010 -- subtypes for the OUT or IN OUT case, since an assignment can
8011 -- change the discriminant values. However we exclude the case of
8012 -- initialization procedures, since discriminants are handled very
8013 -- specially in this context, see the section entitled "Handling of
8014 -- Discriminants" in Einfo.
8016 -- We also exclude the case of Discrim_SO_Functions (functions used
8017 -- in front end layout mode for size/offset values), since in such
8018 -- functions only discriminants are referenced, and not only are such
8019 -- subtypes not needed, but they cannot always be generated, because
8020 -- of order of elaboration issues.
8022 elsif Is_Record_Type (T)
8023 and then Ekind (Formal) = E_In_Parameter
8024 and then Chars (Formal) /= Name_uInit
8025 and then not Is_Unchecked_Union (T)
8026 and then not Is_Discrim_SO_Function (Subp)
8030 -- All other cases do not need an actual subtype
8036 -- Generate actual subtypes for unconstrained arrays and
8037 -- unconstrained discriminated records.
8040 if Nkind (N) = N_Accept_Statement then
8042 -- If expansion is active, The formal is replaced by a local
8043 -- variable that renames the corresponding entry of the
8044 -- parameter block, and it is this local variable that may
8045 -- require an actual subtype.
8047 if Expander_Active then
8048 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8050 Decl := Build_Actual_Subtype (T, Formal);
8053 if Present (Handled_Statement_Sequence (N)) then
8055 First (Statements (Handled_Statement_Sequence (N)));
8056 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8057 Mark_Rewrite_Insertion (Decl);
8059 -- If the accept statement has no body, there will be no
8060 -- reference to the actuals, so no need to compute actual
8067 Decl := Build_Actual_Subtype (T, Formal);
8068 Prepend (Decl, Declarations (N));
8069 Mark_Rewrite_Insertion (Decl);
8072 -- The declaration uses the bounds of an existing object, and
8073 -- therefore needs no constraint checks.
8075 Analyze (Decl, Suppress => All_Checks);
8077 -- We need to freeze manually the generated type when it is
8078 -- inserted anywhere else than in a declarative part.
8080 if Present (First_Stmt) then
8081 Insert_List_Before_And_Analyze (First_Stmt,
8082 Freeze_Entity (Defining_Identifier (Decl), Loc));
8085 if Nkind (N) = N_Accept_Statement
8086 and then Expander_Active
8088 Set_Actual_Subtype (Renamed_Object (Formal),
8089 Defining_Identifier (Decl));
8091 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8095 Next_Formal (Formal);
8097 end Set_Actual_Subtypes;
8099 ---------------------
8100 -- Set_Formal_Mode --
8101 ---------------------
8103 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8104 Spec : constant Node_Id := Parent (Formal_Id);
8107 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8108 -- since we ensure that corresponding actuals are always valid at the
8109 -- point of the call.
8111 if Out_Present (Spec) then
8112 if Ekind (Scope (Formal_Id)) = E_Function
8113 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8115 Error_Msg_N ("functions can only have IN parameters", Spec);
8116 Set_Ekind (Formal_Id, E_In_Parameter);
8118 elsif In_Present (Spec) then
8119 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8122 Set_Ekind (Formal_Id, E_Out_Parameter);
8123 Set_Never_Set_In_Source (Formal_Id, True);
8124 Set_Is_True_Constant (Formal_Id, False);
8125 Set_Current_Value (Formal_Id, Empty);
8129 Set_Ekind (Formal_Id, E_In_Parameter);
8132 -- Set Is_Known_Non_Null for access parameters since the language
8133 -- guarantees that access parameters are always non-null. We also set
8134 -- Can_Never_Be_Null, since there is no way to change the value.
8136 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8138 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8139 -- null; In Ada 2005, only if then null_exclusion is explicit.
8141 if Ada_Version < Ada_05
8142 or else Can_Never_Be_Null (Etype (Formal_Id))
8144 Set_Is_Known_Non_Null (Formal_Id);
8145 Set_Can_Never_Be_Null (Formal_Id);
8148 -- Ada 2005 (AI-231): Null-exclusion access subtype
8150 elsif Is_Access_Type (Etype (Formal_Id))
8151 and then Can_Never_Be_Null (Etype (Formal_Id))
8153 Set_Is_Known_Non_Null (Formal_Id);
8156 Set_Mechanism (Formal_Id, Default_Mechanism);
8157 Set_Formal_Validity (Formal_Id);
8158 end Set_Formal_Mode;
8160 -------------------------
8161 -- Set_Formal_Validity --
8162 -------------------------
8164 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8166 -- If no validity checking, then we cannot assume anything about the
8167 -- validity of parameters, since we do not know there is any checking
8168 -- of the validity on the call side.
8170 if not Validity_Checks_On then
8173 -- If validity checking for parameters is enabled, this means we are
8174 -- not supposed to make any assumptions about argument values.
8176 elsif Validity_Check_Parameters then
8179 -- If we are checking in parameters, we will assume that the caller is
8180 -- also checking parameters, so we can assume the parameter is valid.
8182 elsif Ekind (Formal_Id) = E_In_Parameter
8183 and then Validity_Check_In_Params
8185 Set_Is_Known_Valid (Formal_Id, True);
8187 -- Similar treatment for IN OUT parameters
8189 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8190 and then Validity_Check_In_Out_Params
8192 Set_Is_Known_Valid (Formal_Id, True);
8194 end Set_Formal_Validity;
8196 ------------------------
8197 -- Subtype_Conformant --
8198 ------------------------
8200 function Subtype_Conformant
8201 (New_Id : Entity_Id;
8203 Skip_Controlling_Formals : Boolean := False) return Boolean
8207 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8208 Skip_Controlling_Formals => Skip_Controlling_Formals);
8210 end Subtype_Conformant;
8212 ---------------------
8213 -- Type_Conformant --
8214 ---------------------
8216 function Type_Conformant
8217 (New_Id : Entity_Id;
8219 Skip_Controlling_Formals : Boolean := False) return Boolean
8223 May_Hide_Profile := False;
8226 (New_Id, Old_Id, Type_Conformant, False, Result,
8227 Skip_Controlling_Formals => Skip_Controlling_Formals);
8229 end Type_Conformant;
8231 -------------------------------
8232 -- Valid_Operator_Definition --
8233 -------------------------------
8235 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8238 Id : constant Name_Id := Chars (Designator);
8242 F := First_Formal (Designator);
8243 while Present (F) loop
8246 if Present (Default_Value (F)) then
8248 ("default values not allowed for operator parameters",
8255 -- Verify that user-defined operators have proper number of arguments
8256 -- First case of operators which can only be unary
8259 or else Id = Name_Op_Abs
8263 -- Case of operators which can be unary or binary
8265 elsif Id = Name_Op_Add
8266 or Id = Name_Op_Subtract
8268 N_OK := (N in 1 .. 2);
8270 -- All other operators can only be binary
8278 ("incorrect number of arguments for operator", Designator);
8282 and then Base_Type (Etype (Designator)) = Standard_Boolean
8283 and then not Is_Intrinsic_Subprogram (Designator)
8286 ("explicit definition of inequality not allowed", Designator);
8288 end Valid_Operator_Definition;