1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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 Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch5; use Sem_Ch5;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Prag; use Sem_Prag;
68 with Sem_Res; use Sem_Res;
69 with Sem_Util; use Sem_Util;
70 with Sem_Type; use Sem_Type;
71 with Sem_Warn; use Sem_Warn;
72 with Sinput; use Sinput;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinfo.CN; use Sinfo.CN;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
79 with Stylesw; use Stylesw;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Validsw; use Validsw;
85 package body Sem_Ch6 is
87 May_Hide_Profile : Boolean := False;
88 -- This flag is used to indicate that two formals in two subprograms being
89 -- checked for conformance differ only in that one is an access parameter
90 -- while the other is of a general access type with the same designated
91 -- type. In this case, if the rest of the signatures match, a call to
92 -- either subprogram may be ambiguous, which is worth a warning. The flag
93 -- is set in Compatible_Types, and the warning emitted in
94 -- New_Overloaded_Entity.
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Analyze_Return_Statement (N : Node_Id);
101 -- Common processing for simple_ and extended_return_statements
103 procedure Analyze_Function_Return (N : Node_Id);
104 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
105 -- applies to a [generic] function.
107 procedure Analyze_Return_Type (N : Node_Id);
108 -- Subsidiary to Process_Formals: analyze subtype mark in function
109 -- specification, in a context where the formals are visible and hide
112 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
113 -- Does all the real work of Analyze_Subprogram_Body
115 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
116 -- Analyze a generic subprogram body. N is the body to be analyzed, and
117 -- Gen_Id is the defining entity Id for the corresponding spec.
119 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
120 -- If a subprogram has pragma Inline and inlining is active, use generic
121 -- machinery to build an unexpanded body for the subprogram. This body is
122 -- subsequently used for inline expansions at call sites. If subprogram can
123 -- be inlined (depending on size and nature of local declarations) this
124 -- function returns true. Otherwise subprogram body is treated normally.
125 -- If proper warnings are enabled and the subprogram contains a construct
126 -- that cannot be inlined, the offending construct is flagged accordingly.
128 procedure Check_Conformance
131 Ctype : Conformance_Type;
133 Conforms : out Boolean;
134 Err_Loc : Node_Id := Empty;
135 Get_Inst : Boolean := False;
136 Skip_Controlling_Formals : Boolean := False);
137 -- Given two entities, this procedure checks that the profiles associated
138 -- with these entities meet the conformance criterion given by the third
139 -- parameter. If they conform, Conforms is set True and control returns
140 -- to the caller. If they do not conform, Conforms is set to False, and
141 -- in addition, if Errmsg is True on the call, proper messages are output
142 -- to complain about the conformance failure. If Err_Loc is non_Empty
143 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
144 -- error messages are placed on the appropriate part of the construct
145 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
146 -- against a formal access-to-subprogram type so Get_Instance_Of must
149 procedure Check_Subprogram_Order (N : Node_Id);
150 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
151 -- the alpha ordering rule for N if this ordering requirement applicable.
153 procedure Check_Returns
157 Proc : Entity_Id := Empty);
158 -- Called to check for missing return statements in a function body, or for
159 -- returns present in a procedure body which has No_Return set. HSS is the
160 -- handled statement sequence for the subprogram body. This procedure
161 -- checks all flow paths to make sure they either have return (Mode = 'F',
162 -- used for functions) or do not have a return (Mode = 'P', used for
163 -- No_Return procedures). The flag Err is set if there are any control
164 -- paths not explicitly terminated by a return in the function case, and is
165 -- True otherwise. Proc is the entity for the procedure case and is used
166 -- in posting the warning message.
168 procedure Enter_Overloaded_Entity (S : Entity_Id);
169 -- This procedure makes S, a new overloaded entity, into the first visible
170 -- entity with that name.
172 procedure Install_Entity (E : Entity_Id);
173 -- Make single entity visible. Used for generic formals as well
175 function Is_Non_Overriding_Operation
177 New_E : Entity_Id) return Boolean;
178 -- Enforce the rule given in 12.3(18): a private operation in an instance
179 -- overrides an inherited operation only if the corresponding operation
180 -- was overriding in the generic. This can happen for primitive operations
181 -- of types derived (in the generic unit) from formal private or formal
184 procedure Make_Inequality_Operator (S : Entity_Id);
185 -- Create the declaration for an inequality operator that is implicitly
186 -- created by a user-defined equality operator that yields a boolean.
188 procedure May_Need_Actuals (Fun : Entity_Id);
189 -- Flag functions that can be called without parameters, i.e. those that
190 -- have no parameters, or those for which defaults exist for all parameters
192 procedure Process_PPCs
195 Body_Id : Entity_Id);
196 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
197 -- conditions for the body and assembling and inserting the _postconditions
198 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
199 -- the entities for the body and separate spec (if there is no separate
200 -- spec, Spec_Id is Empty).
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ------------------------------
208 -- Analyze_Return_Statement --
209 ------------------------------
211 procedure Analyze_Return_Statement (N : Node_Id) is
213 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
214 N_Extended_Return_Statement));
216 Returns_Object : constant Boolean :=
217 Nkind (N) = N_Extended_Return_Statement
219 (Nkind (N) = N_Simple_Return_Statement
220 and then Present (Expression (N)));
221 -- True if we're returning something; that is, "return <expression>;"
222 -- or "return Result : T [:= ...]". False for "return;". Used for error
223 -- checking: If Returns_Object is True, N should apply to a function
224 -- body; otherwise N should apply to a procedure body, entry body,
225 -- accept statement, or extended return statement.
227 function Find_What_It_Applies_To return Entity_Id;
228 -- Find the entity representing the innermost enclosing body, accept
229 -- statement, or extended return statement. If the result is a callable
230 -- construct or extended return statement, then this will be the value
231 -- of the Return_Applies_To attribute. Otherwise, the program is
232 -- illegal. See RM-6.5(4/2).
234 -----------------------------
235 -- Find_What_It_Applies_To --
236 -----------------------------
238 function Find_What_It_Applies_To return Entity_Id is
239 Result : Entity_Id := Empty;
242 -- Loop outward through the Scope_Stack, skipping blocks and loops
244 for J in reverse 0 .. Scope_Stack.Last loop
245 Result := Scope_Stack.Table (J).Entity;
246 exit when Ekind (Result) /= E_Block and then
247 Ekind (Result) /= E_Loop;
250 pragma Assert (Present (Result));
252 end Find_What_It_Applies_To;
254 -- Local declarations
256 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
257 Kind : constant Entity_Kind := Ekind (Scope_Id);
258 Loc : constant Source_Ptr := Sloc (N);
259 Stm_Entity : constant Entity_Id :=
261 (E_Return_Statement, Current_Scope, Loc, 'R');
263 -- Start of processing for Analyze_Return_Statement
266 Set_Return_Statement_Entity (N, Stm_Entity);
268 Set_Etype (Stm_Entity, Standard_Void_Type);
269 Set_Return_Applies_To (Stm_Entity, Scope_Id);
271 -- Place Return entity on scope stack, to simplify enforcement of 6.5
272 -- (4/2): an inner return statement will apply to this extended return.
274 if Nkind (N) = N_Extended_Return_Statement then
275 Push_Scope (Stm_Entity);
278 -- Check that pragma No_Return is obeyed. Don't complain about the
279 -- implicitly-generated return that is placed at the end.
281 if No_Return (Scope_Id) and then Comes_From_Source (N) then
282 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
285 -- Warn on any unassigned OUT parameters if in procedure
287 if Ekind (Scope_Id) = E_Procedure then
288 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
291 -- Check that functions return objects, and other things do not
293 if Kind = E_Function or else Kind = E_Generic_Function then
294 if not Returns_Object then
295 Error_Msg_N ("missing expression in return from function", N);
298 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
299 if Returns_Object then
300 Error_Msg_N ("procedure cannot return value (use function)", N);
303 elsif Kind = E_Entry or else Kind = E_Entry_Family then
304 if Returns_Object then
305 if Is_Protected_Type (Scope (Scope_Id)) then
306 Error_Msg_N ("entry body cannot return value", N);
308 Error_Msg_N ("accept statement cannot return value", N);
312 elsif Kind = E_Return_Statement then
314 -- We are nested within another return statement, which must be an
315 -- extended_return_statement.
317 if Returns_Object then
319 ("extended_return_statement cannot return value; " &
320 "use `""RETURN;""`", N);
324 Error_Msg_N ("illegal context for return statement", N);
327 if Kind = E_Function or else Kind = E_Generic_Function then
328 Analyze_Function_Return (N);
331 if Nkind (N) = N_Extended_Return_Statement then
335 Kill_Current_Values (Last_Assignment_Only => True);
336 Check_Unreachable_Code (N);
337 end Analyze_Return_Statement;
339 ---------------------------------------------
340 -- Analyze_Abstract_Subprogram_Declaration --
341 ---------------------------------------------
343 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
344 Designator : constant Entity_Id :=
345 Analyze_Subprogram_Specification (Specification (N));
346 Scop : constant Entity_Id := Current_Scope;
349 Generate_Definition (Designator);
350 Set_Is_Abstract_Subprogram (Designator);
351 New_Overloaded_Entity (Designator);
352 Check_Delayed_Subprogram (Designator);
354 Set_Categorization_From_Scope (Designator, Scop);
356 if Ekind (Scope (Designator)) = E_Protected_Type then
358 ("abstract subprogram not allowed in protected type", N);
360 -- Issue a warning if the abstract subprogram is neither a dispatching
361 -- operation nor an operation that overrides an inherited subprogram or
362 -- predefined operator, since this most likely indicates a mistake.
364 elsif Warn_On_Redundant_Constructs
365 and then not Is_Dispatching_Operation (Designator)
366 and then not Is_Overriding_Operation (Designator)
367 and then (not Is_Operator_Symbol_Name (Chars (Designator))
368 or else Scop /= Scope (Etype (First_Formal (Designator))))
371 ("?abstract subprogram is not dispatching or overriding", N);
374 Generate_Reference_To_Formals (Designator);
375 Check_Eliminated (Designator);
376 end Analyze_Abstract_Subprogram_Declaration;
378 ----------------------------------------
379 -- Analyze_Extended_Return_Statement --
380 ----------------------------------------
382 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
384 Analyze_Return_Statement (N);
385 end Analyze_Extended_Return_Statement;
387 ----------------------------
388 -- Analyze_Function_Call --
389 ----------------------------
391 procedure Analyze_Function_Call (N : Node_Id) is
392 P : constant Node_Id := Name (N);
393 L : constant List_Id := Parameter_Associations (N);
399 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
400 -- as B (A, X). If the rewriting is successful, the call has been
401 -- analyzed and we just return.
403 if Nkind (P) = N_Selected_Component
404 and then Name (N) /= P
405 and then Is_Rewrite_Substitution (N)
406 and then Present (Etype (N))
411 -- If error analyzing name, then set Any_Type as result type and return
413 if Etype (P) = Any_Type then
414 Set_Etype (N, Any_Type);
418 -- Otherwise analyze the parameters
422 while Present (Actual) loop
424 Check_Parameterless_Call (Actual);
430 end Analyze_Function_Call;
432 -----------------------------
433 -- Analyze_Function_Return --
434 -----------------------------
436 procedure Analyze_Function_Return (N : Node_Id) is
437 Loc : constant Source_Ptr := Sloc (N);
438 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
439 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
441 R_Type : constant Entity_Id := Etype (Scope_Id);
442 -- Function result subtype
444 procedure Check_Limited_Return (Expr : Node_Id);
445 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
446 -- limited types. Used only for simple return statements.
447 -- Expr is the expression returned.
449 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
450 -- Check that the return_subtype_indication properly matches the result
451 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
453 --------------------------
454 -- Check_Limited_Return --
455 --------------------------
457 procedure Check_Limited_Return (Expr : Node_Id) is
459 -- Ada 2005 (AI-318-02): Return-by-reference types have been
460 -- removed and replaced by anonymous access results. This is an
461 -- incompatibility with Ada 95. Not clear whether this should be
462 -- enforced yet or perhaps controllable with special switch. ???
464 if Is_Limited_Type (R_Type)
465 and then Comes_From_Source (N)
466 and then not In_Instance_Body
467 and then not OK_For_Limited_Init_In_05 (Expr)
471 if Ada_Version >= Ada_05
472 and then not Debug_Flag_Dot_L
473 and then not GNAT_Mode
476 ("(Ada 2005) cannot copy object of a limited type " &
477 "(RM-2005 6.5(5.5/2))", Expr);
478 if Is_Inherently_Limited_Type (R_Type) then
480 ("\return by reference not permitted in Ada 2005", Expr);
483 -- Warn in Ada 95 mode, to give folks a heads up about this
486 -- In GNAT mode, this is just a warning, to allow it to be
487 -- evilly turned off. Otherwise it is a real error.
489 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
490 if Is_Inherently_Limited_Type (R_Type) then
492 ("return by reference not permitted in Ada 2005 " &
493 "(RM-2005 6.5(5.5/2))?", Expr);
496 ("cannot copy object of a limited type in Ada 2005 " &
497 "(RM-2005 6.5(5.5/2))?", Expr);
500 -- Ada 95 mode, compatibility warnings disabled
503 return; -- skip continuation messages below
507 ("\consider switching to return of access type", Expr);
508 Explain_Limited_Type (R_Type, Expr);
510 end Check_Limited_Return;
512 -------------------------------------
513 -- Check_Return_Subtype_Indication --
514 -------------------------------------
516 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
517 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
518 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
519 -- Subtype given in the extended return statement;
520 -- this must match R_Type.
522 Subtype_Ind : constant Node_Id :=
523 Object_Definition (Original_Node (Obj_Decl));
525 R_Type_Is_Anon_Access :
527 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
529 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
531 Ekind (R_Type) = E_Anonymous_Access_Type;
532 -- True if return type of the function is an anonymous access type
533 -- Can't we make Is_Anonymous_Access_Type in einfo ???
535 R_Stm_Type_Is_Anon_Access :
537 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
539 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
541 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
542 -- True if type of the return object is an anonymous access type
545 -- First, avoid cascade errors:
547 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
551 -- "return access T" case; check that the return statement also has
552 -- "access T", and that the subtypes statically match:
553 -- if this is an access to subprogram the signatures must match.
555 if R_Type_Is_Anon_Access then
556 if R_Stm_Type_Is_Anon_Access then
558 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
560 if Base_Type (Designated_Type (R_Stm_Type)) /=
561 Base_Type (Designated_Type (R_Type))
562 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
565 ("subtype must statically match function result subtype",
566 Subtype_Mark (Subtype_Ind));
570 -- For two anonymous access to subprogram types, the
571 -- types themselves must be type conformant.
573 if not Conforming_Types
574 (R_Stm_Type, R_Type, Fully_Conformant)
577 ("subtype must statically match function result subtype",
583 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
586 -- Subtype indication case: check that the types are the same, and
587 -- statically match if appropriate. Also handle record types with
588 -- unknown discriminants for which we have built the underlying
591 elsif Base_Type (R_Stm_Type) = Base_Type (R_Type)
592 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
593 and then Underlying_Record_View (Base_Type (R_Stm_Type))
594 = Base_Type (R_Type))
596 -- A null exclusion may be present on the return type, on the
597 -- function specification, on the object declaration or on the
600 if Is_Access_Type (R_Type)
602 (Can_Never_Be_Null (R_Type)
603 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
604 Can_Never_Be_Null (R_Stm_Type)
607 ("subtype must statically match function result subtype",
611 if Is_Constrained (R_Type) then
612 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
614 ("subtype must statically match function result subtype",
619 -- If the function's result type doesn't match the return object
620 -- entity's type, then we check for the case where the result type
621 -- is class-wide, and allow the declaration if the type of the object
622 -- definition matches the class-wide type. This prevents rejection
623 -- in the case where the object declaration is initialized by a call
624 -- to a build-in-place function with a specific result type and the
625 -- object entity had its type changed to that specific type. This is
626 -- also allowed in the case where Obj_Decl does not come from source,
627 -- which can occur for an expansion of a simple return statement of
628 -- a build-in-place class-wide function when the result expression
629 -- has a specific type, because a return object with a specific type
630 -- is created. (Note that the ARG believes that return objects should
631 -- be allowed to have a type covered by a class-wide result type in
632 -- any case, so once that relaxation is made (see AI05-32), the above
633 -- check for type compatibility should be changed to test Covers
634 -- rather than equality, and the following special test will no
635 -- longer be needed. ???)
637 elsif Is_Class_Wide_Type (R_Type)
639 (R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
640 or else not Comes_From_Source (Obj_Decl))
646 ("wrong type for return_subtype_indication", Subtype_Ind);
648 end Check_Return_Subtype_Indication;
650 ---------------------
651 -- Local Variables --
652 ---------------------
656 -- Start of processing for Analyze_Function_Return
659 Set_Return_Present (Scope_Id);
661 if Nkind (N) = N_Simple_Return_Statement then
662 Expr := Expression (N);
663 Analyze_And_Resolve (Expr, R_Type);
664 Check_Limited_Return (Expr);
667 -- Analyze parts specific to extended_return_statement:
670 Obj_Decl : constant Node_Id :=
671 Last (Return_Object_Declarations (N));
673 HSS : constant Node_Id := Handled_Statement_Sequence (N);
676 Expr := Expression (Obj_Decl);
678 -- Note: The check for OK_For_Limited_Init will happen in
679 -- Analyze_Object_Declaration; we treat it as a normal
680 -- object declaration.
682 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
685 Check_Return_Subtype_Indication (Obj_Decl);
687 if Present (HSS) then
690 if Present (Exception_Handlers (HSS)) then
692 -- ???Has_Nested_Block_With_Handler needs to be set.
693 -- Probably by creating an actual N_Block_Statement.
694 -- Probably in Expand.
700 Check_References (Stm_Entity);
704 -- Case of Expr present
708 -- Defend against previous errors
710 and then Nkind (Expr) /= N_Empty
711 and then Present (Etype (Expr))
713 -- Apply constraint check. Note that this is done before the implicit
714 -- conversion of the expression done for anonymous access types to
715 -- ensure correct generation of the null-excluding check associated
716 -- with null-excluding expressions found in return statements.
718 Apply_Constraint_Check (Expr, R_Type);
720 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
721 -- type, apply an implicit conversion of the expression to that type
722 -- to force appropriate static and run-time accessibility checks.
724 if Ada_Version >= Ada_05
725 and then Ekind (R_Type) = E_Anonymous_Access_Type
727 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
728 Analyze_And_Resolve (Expr, R_Type);
731 -- If the result type is class-wide, then check that the return
732 -- expression's type is not declared at a deeper level than the
733 -- function (RM05-6.5(5.6/2)).
735 if Ada_Version >= Ada_05
736 and then Is_Class_Wide_Type (R_Type)
738 if Type_Access_Level (Etype (Expr)) >
739 Subprogram_Access_Level (Scope_Id)
742 ("level of return expression type is deeper than " &
743 "class-wide function!", Expr);
747 if (Is_Class_Wide_Type (Etype (Expr))
748 or else Is_Dynamically_Tagged (Expr))
749 and then not Is_Class_Wide_Type (R_Type)
752 ("dynamically tagged expression not allowed!", Expr);
755 -- ??? A real run-time accessibility check is needed in cases
756 -- involving dereferences of access parameters. For now we just
757 -- check the static cases.
759 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
760 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
761 and then Object_Access_Level (Expr) >
762 Subprogram_Access_Level (Scope_Id)
765 Make_Raise_Program_Error (Loc,
766 Reason => PE_Accessibility_Check_Failed));
770 ("cannot return a local value by reference?", N);
772 ("\& will be raised at run time?",
773 N, Standard_Program_Error);
777 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
778 and then Null_Exclusion_Present (Parent (Scope_Id))
780 Apply_Compile_Time_Constraint_Error
782 Msg => "(Ada 2005) null not allowed for "
783 & "null-excluding return?",
784 Reason => CE_Null_Not_Allowed);
787 end Analyze_Function_Return;
789 -------------------------------------
790 -- Analyze_Generic_Subprogram_Body --
791 -------------------------------------
793 procedure Analyze_Generic_Subprogram_Body
797 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
798 Kind : constant Entity_Kind := Ekind (Gen_Id);
804 -- Copy body and disable expansion while analyzing the generic For a
805 -- stub, do not copy the stub (which would load the proper body), this
806 -- will be done when the proper body is analyzed.
808 if Nkind (N) /= N_Subprogram_Body_Stub then
809 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
814 Spec := Specification (N);
816 -- Within the body of the generic, the subprogram is callable, and
817 -- behaves like the corresponding non-generic unit.
819 Body_Id := Defining_Entity (Spec);
821 if Kind = E_Generic_Procedure
822 and then Nkind (Spec) /= N_Procedure_Specification
824 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
827 elsif Kind = E_Generic_Function
828 and then Nkind (Spec) /= N_Function_Specification
830 Error_Msg_N ("invalid body for generic function ", Body_Id);
834 Set_Corresponding_Body (Gen_Decl, Body_Id);
836 if Has_Completion (Gen_Id)
837 and then Nkind (Parent (N)) /= N_Subunit
839 Error_Msg_N ("duplicate generic body", N);
842 Set_Has_Completion (Gen_Id);
845 if Nkind (N) = N_Subprogram_Body_Stub then
846 Set_Ekind (Defining_Entity (Specification (N)), Kind);
848 Set_Corresponding_Spec (N, Gen_Id);
851 if Nkind (Parent (N)) = N_Compilation_Unit then
852 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
855 -- Make generic parameters immediately visible in the body. They are
856 -- needed to process the formals declarations. Then make the formals
857 -- visible in a separate step.
863 First_Ent : Entity_Id;
866 First_Ent := First_Entity (Gen_Id);
869 while Present (E) and then not Is_Formal (E) loop
874 Set_Use (Generic_Formal_Declarations (Gen_Decl));
876 -- Now generic formals are visible, and the specification can be
877 -- analyzed, for subsequent conformance check.
879 Body_Id := Analyze_Subprogram_Specification (Spec);
881 -- Make formal parameters visible
885 -- E is the first formal parameter, we loop through the formals
886 -- installing them so that they will be visible.
888 Set_First_Entity (Gen_Id, E);
889 while Present (E) loop
895 -- Visible generic entity is callable within its own body
897 Set_Ekind (Gen_Id, Ekind (Body_Id));
898 Set_Ekind (Body_Id, E_Subprogram_Body);
899 Set_Convention (Body_Id, Convention (Gen_Id));
900 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
901 Set_Scope (Body_Id, Scope (Gen_Id));
902 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
904 if Nkind (N) = N_Subprogram_Body_Stub then
906 -- No body to analyze, so restore state of generic unit
908 Set_Ekind (Gen_Id, Kind);
909 Set_Ekind (Body_Id, Kind);
911 if Present (First_Ent) then
912 Set_First_Entity (Gen_Id, First_Ent);
919 -- If this is a compilation unit, it must be made visible explicitly,
920 -- because the compilation of the declaration, unlike other library
921 -- unit declarations, does not. If it is not a unit, the following
922 -- is redundant but harmless.
924 Set_Is_Immediately_Visible (Gen_Id);
925 Reference_Body_Formals (Gen_Id, Body_Id);
927 if Is_Child_Unit (Gen_Id) then
928 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
931 Set_Actual_Subtypes (N, Current_Scope);
932 Process_PPCs (N, Gen_Id, Body_Id);
934 -- If the generic unit carries pre- or post-conditions, copy them
935 -- to the original generic tree, so that they are properly added
936 -- to any instantiation.
939 Orig : constant Node_Id := Original_Node (N);
943 Cond := First (Declarations (N));
944 while Present (Cond) loop
945 if Nkind (Cond) = N_Pragma
946 and then Pragma_Name (Cond) = Name_Check
948 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
950 elsif Nkind (Cond) = N_Pragma
951 and then Pragma_Name (Cond) = Name_Postcondition
953 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
954 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
963 Analyze_Declarations (Declarations (N));
965 Analyze (Handled_Statement_Sequence (N));
967 Save_Global_References (Original_Node (N));
969 -- Prior to exiting the scope, include generic formals again (if any
970 -- are present) in the set of local entities.
972 if Present (First_Ent) then
973 Set_First_Entity (Gen_Id, First_Ent);
976 Check_References (Gen_Id);
979 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
981 Check_Subprogram_Order (N);
983 -- Outside of its body, unit is generic again
985 Set_Ekind (Gen_Id, Kind);
986 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
989 Style.Check_Identifier (Body_Id, Gen_Id);
992 end Analyze_Generic_Subprogram_Body;
994 -----------------------------
995 -- Analyze_Operator_Symbol --
996 -----------------------------
998 -- An operator symbol such as "+" or "and" may appear in context where the
999 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1000 -- is just a string, as in (conjunction = "or"). In these cases the parser
1001 -- generates this node, and the semantics does the disambiguation. Other
1002 -- such case are actuals in an instantiation, the generic unit in an
1003 -- instantiation, and pragma arguments.
1005 procedure Analyze_Operator_Symbol (N : Node_Id) is
1006 Par : constant Node_Id := Parent (N);
1009 if (Nkind (Par) = N_Function_Call
1010 and then N = Name (Par))
1011 or else Nkind (Par) = N_Function_Instantiation
1012 or else (Nkind (Par) = N_Indexed_Component
1013 and then N = Prefix (Par))
1014 or else (Nkind (Par) = N_Pragma_Argument_Association
1015 and then not Is_Pragma_String_Literal (Par))
1016 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1017 or else (Nkind (Par) = N_Attribute_Reference
1018 and then Attribute_Name (Par) /= Name_Value)
1020 Find_Direct_Name (N);
1023 Change_Operator_Symbol_To_String_Literal (N);
1026 end Analyze_Operator_Symbol;
1028 -----------------------------------
1029 -- Analyze_Parameter_Association --
1030 -----------------------------------
1032 procedure Analyze_Parameter_Association (N : Node_Id) is
1034 Analyze (Explicit_Actual_Parameter (N));
1035 end Analyze_Parameter_Association;
1037 ----------------------------
1038 -- Analyze_Procedure_Call --
1039 ----------------------------
1041 procedure Analyze_Procedure_Call (N : Node_Id) is
1042 Loc : constant Source_Ptr := Sloc (N);
1043 P : constant Node_Id := Name (N);
1044 Actuals : constant List_Id := Parameter_Associations (N);
1048 procedure Analyze_Call_And_Resolve;
1049 -- Do Analyze and Resolve calls for procedure call
1051 ------------------------------
1052 -- Analyze_Call_And_Resolve --
1053 ------------------------------
1055 procedure Analyze_Call_And_Resolve is
1057 if Nkind (N) = N_Procedure_Call_Statement then
1059 Resolve (N, Standard_Void_Type);
1063 end Analyze_Call_And_Resolve;
1065 -- Start of processing for Analyze_Procedure_Call
1068 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1069 -- a procedure call or an entry call. The prefix may denote an access
1070 -- to subprogram type, in which case an implicit dereference applies.
1071 -- If the prefix is an indexed component (without implicit dereference)
1072 -- then the construct denotes a call to a member of an entire family.
1073 -- If the prefix is a simple name, it may still denote a call to a
1074 -- parameterless member of an entry family. Resolution of these various
1075 -- interpretations is delicate.
1079 -- If this is a call of the form Obj.Op, the call may have been
1080 -- analyzed and possibly rewritten into a block, in which case
1083 if Analyzed (N) then
1087 -- If error analyzing prefix, then set Any_Type as result and return
1089 if Etype (P) = Any_Type then
1090 Set_Etype (N, Any_Type);
1094 -- Otherwise analyze the parameters
1096 if Present (Actuals) then
1097 Actual := First (Actuals);
1099 while Present (Actual) loop
1101 Check_Parameterless_Call (Actual);
1106 -- Special processing for Elab_Spec and Elab_Body calls
1108 if Nkind (P) = N_Attribute_Reference
1109 and then (Attribute_Name (P) = Name_Elab_Spec
1110 or else Attribute_Name (P) = Name_Elab_Body)
1112 if Present (Actuals) then
1114 ("no parameters allowed for this call", First (Actuals));
1118 Set_Etype (N, Standard_Void_Type);
1121 elsif Is_Entity_Name (P)
1122 and then Is_Record_Type (Etype (Entity (P)))
1123 and then Remote_AST_I_Dereference (P)
1127 elsif Is_Entity_Name (P)
1128 and then Ekind (Entity (P)) /= E_Entry_Family
1130 if Is_Access_Type (Etype (P))
1131 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1132 and then No (Actuals)
1133 and then Comes_From_Source (N)
1135 Error_Msg_N ("missing explicit dereference in call", N);
1138 Analyze_Call_And_Resolve;
1140 -- If the prefix is the simple name of an entry family, this is
1141 -- a parameterless call from within the task body itself.
1143 elsif Is_Entity_Name (P)
1144 and then Nkind (P) = N_Identifier
1145 and then Ekind (Entity (P)) = E_Entry_Family
1146 and then Present (Actuals)
1147 and then No (Next (First (Actuals)))
1149 -- Can be call to parameterless entry family. What appears to be the
1150 -- sole argument is in fact the entry index. Rewrite prefix of node
1151 -- accordingly. Source representation is unchanged by this
1155 Make_Indexed_Component (Loc,
1157 Make_Selected_Component (Loc,
1158 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1159 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1160 Expressions => Actuals);
1161 Set_Name (N, New_N);
1162 Set_Etype (New_N, Standard_Void_Type);
1163 Set_Parameter_Associations (N, No_List);
1164 Analyze_Call_And_Resolve;
1166 elsif Nkind (P) = N_Explicit_Dereference then
1167 if Ekind (Etype (P)) = E_Subprogram_Type then
1168 Analyze_Call_And_Resolve;
1170 Error_Msg_N ("expect access to procedure in call", P);
1173 -- The name can be a selected component or an indexed component that
1174 -- yields an access to subprogram. Such a prefix is legal if the call
1175 -- has parameter associations.
1177 elsif Is_Access_Type (Etype (P))
1178 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1180 if Present (Actuals) then
1181 Analyze_Call_And_Resolve;
1183 Error_Msg_N ("missing explicit dereference in call ", N);
1186 -- If not an access to subprogram, then the prefix must resolve to the
1187 -- name of an entry, entry family, or protected operation.
1189 -- For the case of a simple entry call, P is a selected component where
1190 -- the prefix is the task and the selector name is the entry. A call to
1191 -- a protected procedure will have the same syntax. If the protected
1192 -- object contains overloaded operations, the entity may appear as a
1193 -- function, the context will select the operation whose type is Void.
1195 elsif Nkind (P) = N_Selected_Component
1196 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1198 Ekind (Entity (Selector_Name (P))) = E_Procedure
1200 Ekind (Entity (Selector_Name (P))) = E_Function)
1202 Analyze_Call_And_Resolve;
1204 elsif Nkind (P) = N_Selected_Component
1205 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1206 and then Present (Actuals)
1207 and then No (Next (First (Actuals)))
1209 -- Can be call to parameterless entry family. What appears to be the
1210 -- sole argument is in fact the entry index. Rewrite prefix of node
1211 -- accordingly. Source representation is unchanged by this
1215 Make_Indexed_Component (Loc,
1216 Prefix => New_Copy (P),
1217 Expressions => Actuals);
1218 Set_Name (N, New_N);
1219 Set_Etype (New_N, Standard_Void_Type);
1220 Set_Parameter_Associations (N, No_List);
1221 Analyze_Call_And_Resolve;
1223 -- For the case of a reference to an element of an entry family, P is
1224 -- an indexed component whose prefix is a selected component (task and
1225 -- entry family), and whose index is the entry family index.
1227 elsif Nkind (P) = N_Indexed_Component
1228 and then Nkind (Prefix (P)) = N_Selected_Component
1229 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1231 Analyze_Call_And_Resolve;
1233 -- If the prefix is the name of an entry family, it is a call from
1234 -- within the task body itself.
1236 elsif Nkind (P) = N_Indexed_Component
1237 and then Nkind (Prefix (P)) = N_Identifier
1238 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1241 Make_Selected_Component (Loc,
1242 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1243 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1244 Rewrite (Prefix (P), New_N);
1246 Analyze_Call_And_Resolve;
1248 -- Anything else is an error
1251 Error_Msg_N ("invalid procedure or entry call", N);
1253 end Analyze_Procedure_Call;
1255 -------------------------------------
1256 -- Analyze_Simple_Return_Statement --
1257 -------------------------------------
1259 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1261 if Present (Expression (N)) then
1262 Mark_Coextensions (N, Expression (N));
1265 Analyze_Return_Statement (N);
1266 end Analyze_Simple_Return_Statement;
1268 -------------------------
1269 -- Analyze_Return_Type --
1270 -------------------------
1272 procedure Analyze_Return_Type (N : Node_Id) is
1273 Designator : constant Entity_Id := Defining_Entity (N);
1274 Typ : Entity_Id := Empty;
1277 -- Normal case where result definition does not indicate an error
1279 if Result_Definition (N) /= Error then
1280 if Nkind (Result_Definition (N)) = N_Access_Definition then
1282 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1285 AD : constant Node_Id :=
1286 Access_To_Subprogram_Definition (Result_Definition (N));
1288 if Present (AD) and then Protected_Present (AD) then
1289 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1291 Typ := Access_Definition (N, Result_Definition (N));
1295 Set_Parent (Typ, Result_Definition (N));
1296 Set_Is_Local_Anonymous_Access (Typ);
1297 Set_Etype (Designator, Typ);
1299 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1301 Null_Exclusion_Static_Checks (N);
1303 -- Subtype_Mark case
1306 Find_Type (Result_Definition (N));
1307 Typ := Entity (Result_Definition (N));
1308 Set_Etype (Designator, Typ);
1310 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1312 Null_Exclusion_Static_Checks (N);
1314 -- If a null exclusion is imposed on the result type, then create
1315 -- a null-excluding itype (an access subtype) and use it as the
1316 -- function's Etype. Note that the null exclusion checks are done
1317 -- right before this, because they don't get applied to types that
1318 -- do not come from source.
1320 if Is_Access_Type (Typ)
1321 and then Null_Exclusion_Present (N)
1323 Set_Etype (Designator,
1324 Create_Null_Excluding_Itype
1327 Scope_Id => Scope (Current_Scope)));
1329 Set_Etype (Designator, Typ);
1332 if Ekind (Typ) = E_Incomplete_Type
1333 and then Is_Value_Type (Typ)
1337 elsif Ekind (Typ) = E_Incomplete_Type
1338 or else (Is_Class_Wide_Type (Typ)
1340 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1343 ("invalid use of incomplete type&", Designator, Typ);
1347 -- Case where result definition does indicate an error
1350 Set_Etype (Designator, Any_Type);
1352 end Analyze_Return_Type;
1354 -----------------------------
1355 -- Analyze_Subprogram_Body --
1356 -----------------------------
1358 procedure Analyze_Subprogram_Body (N : Node_Id) is
1359 Loc : constant Source_Ptr := Sloc (N);
1360 Body_Spec : constant Node_Id := Specification (N);
1361 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1364 if Debug_Flag_C then
1365 Write_Str ("==> subprogram body ");
1366 Write_Name (Chars (Body_Id));
1367 Write_Str (" from ");
1368 Write_Location (Loc);
1373 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1375 -- The real work is split out into the helper, so it can do "return;"
1376 -- without skipping the debug output:
1378 Analyze_Subprogram_Body_Helper (N);
1380 if Debug_Flag_C then
1382 Write_Str ("<== subprogram body ");
1383 Write_Name (Chars (Body_Id));
1384 Write_Str (" from ");
1385 Write_Location (Loc);
1388 end Analyze_Subprogram_Body;
1390 ------------------------------------
1391 -- Analyze_Subprogram_Body_Helper --
1392 ------------------------------------
1394 -- This procedure is called for regular subprogram bodies, generic bodies,
1395 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1396 -- specification matters, and is used to create a proper declaration for
1397 -- the subprogram, or to perform conformance checks.
1399 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1400 Loc : constant Source_Ptr := Sloc (N);
1401 Body_Deleted : constant Boolean := False;
1402 Body_Spec : constant Node_Id := Specification (N);
1403 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1404 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1405 Conformant : Boolean;
1407 Missing_Ret : Boolean;
1409 Prot_Typ : Entity_Id := Empty;
1410 Spec_Id : Entity_Id;
1411 Spec_Decl : Node_Id := Empty;
1413 Last_Real_Spec_Entity : Entity_Id := Empty;
1414 -- When we analyze a separate spec, the entity chain ends up containing
1415 -- the formals, as well as any itypes generated during analysis of the
1416 -- default expressions for parameters, or the arguments of associated
1417 -- precondition/postcondition pragmas (which are analyzed in the context
1418 -- of the spec since they have visibility on formals).
1420 -- These entities belong with the spec and not the body. However we do
1421 -- the analysis of the body in the context of the spec (again to obtain
1422 -- visibility to the formals), and all the entities generated during
1423 -- this analysis end up also chained to the entity chain of the spec.
1424 -- But they really belong to the body, and there is circuitry to move
1425 -- them from the spec to the body.
1427 -- However, when we do this move, we don't want to move the real spec
1428 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1429 -- variable points to the last real spec entity, so we only move those
1430 -- chained beyond that point. It is initialized to Empty to deal with
1431 -- the case where there is no separate spec.
1433 procedure Check_Anonymous_Return;
1434 -- Ada 2005: if a function returns an access type that denotes a task,
1435 -- or a type that contains tasks, we must create a master entity for
1436 -- the anonymous type, which typically will be used in an allocator
1437 -- in the body of the function.
1439 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1440 -- Look ahead to recognize a pragma that may appear after the body.
1441 -- If there is a previous spec, check that it appears in the same
1442 -- declarative part. If the pragma is Inline_Always, perform inlining
1443 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1444 -- If the body acts as a spec, and inlining is required, we create a
1445 -- subprogram declaration for it, in order to attach the body to inline.
1446 -- If pragma does not appear after the body, check whether there is
1447 -- an inline pragma before any local declarations.
1449 function Disambiguate_Spec return Entity_Id;
1450 -- When a primitive is declared between the private view and the full
1451 -- view of a concurrent type which implements an interface, a special
1452 -- mechanism is used to find the corresponding spec of the primitive
1455 function Is_Private_Concurrent_Primitive
1456 (Subp_Id : Entity_Id) return Boolean;
1457 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1458 -- type that implements an interface and has a private view.
1460 procedure Set_Trivial_Subprogram (N : Node_Id);
1461 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1462 -- subprogram whose body is being analyzed. N is the statement node
1463 -- causing the flag to be set, if the following statement is a return
1464 -- of an entity, we mark the entity as set in source to suppress any
1465 -- warning on the stylized use of function stubs with a dummy return.
1467 procedure Verify_Overriding_Indicator;
1468 -- If there was a previous spec, the entity has been entered in the
1469 -- current scope previously. If the body itself carries an overriding
1470 -- indicator, check that it is consistent with the known status of the
1473 ----------------------------
1474 -- Check_Anonymous_Return --
1475 ----------------------------
1477 procedure Check_Anonymous_Return is
1483 if Present (Spec_Id) then
1489 if Ekind (Scop) = E_Function
1490 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1491 and then not Is_Thunk (Scop)
1492 and then (Has_Task (Designated_Type (Etype (Scop)))
1494 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1496 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1497 and then Expander_Active
1499 -- Avoid cases with no tasking support
1501 and then RTE_Available (RE_Current_Master)
1502 and then not Restriction_Active (No_Task_Hierarchy)
1505 Make_Object_Declaration (Loc,
1506 Defining_Identifier =>
1507 Make_Defining_Identifier (Loc, Name_uMaster),
1508 Constant_Present => True,
1509 Object_Definition =>
1510 New_Reference_To (RTE (RE_Master_Id), Loc),
1512 Make_Explicit_Dereference (Loc,
1513 New_Reference_To (RTE (RE_Current_Master), Loc)));
1515 if Present (Declarations (N)) then
1516 Prepend (Decl, Declarations (N));
1518 Set_Declarations (N, New_List (Decl));
1521 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1522 Set_Has_Master_Entity (Scop);
1524 -- Now mark the containing scope as a task master
1527 while Nkind (Par) /= N_Compilation_Unit loop
1528 Par := Parent (Par);
1529 pragma Assert (Present (Par));
1531 -- If we fall off the top, we are at the outer level, and
1532 -- the environment task is our effective master, so nothing
1536 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1538 Set_Is_Task_Master (Par, True);
1543 end Check_Anonymous_Return;
1545 -------------------------
1546 -- Check_Inline_Pragma --
1547 -------------------------
1549 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1553 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1554 -- True when N is a pragma Inline or Inline_Always that applies
1555 -- to this subprogram.
1557 -----------------------
1558 -- Is_Inline_Pragma --
1559 -----------------------
1561 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1564 Nkind (N) = N_Pragma
1566 (Pragma_Name (N) = Name_Inline_Always
1569 and then Pragma_Name (N) = Name_Inline))
1572 (Expression (First (Pragma_Argument_Associations (N))))
1574 end Is_Inline_Pragma;
1576 -- Start of processing for Check_Inline_Pragma
1579 if not Expander_Active then
1583 if Is_List_Member (N)
1584 and then Present (Next (N))
1585 and then Is_Inline_Pragma (Next (N))
1589 elsif Nkind (N) /= N_Subprogram_Body_Stub
1590 and then Present (Declarations (N))
1591 and then Is_Inline_Pragma (First (Declarations (N)))
1593 Prag := First (Declarations (N));
1599 if Present (Prag) then
1600 if Present (Spec_Id) then
1601 if List_Containing (N) =
1602 List_Containing (Unit_Declaration_Node (Spec_Id))
1608 -- Create a subprogram declaration, to make treatment uniform
1611 Subp : constant Entity_Id :=
1612 Make_Defining_Identifier (Loc, Chars (Body_Id));
1613 Decl : constant Node_Id :=
1614 Make_Subprogram_Declaration (Loc,
1615 Specification => New_Copy_Tree (Specification (N)));
1617 Set_Defining_Unit_Name (Specification (Decl), Subp);
1619 if Present (First_Formal (Body_Id)) then
1620 Plist := Copy_Parameter_List (Body_Id);
1621 Set_Parameter_Specifications
1622 (Specification (Decl), Plist);
1625 Insert_Before (N, Decl);
1628 Set_Has_Pragma_Inline (Subp);
1630 if Pragma_Name (Prag) = Name_Inline_Always then
1631 Set_Is_Inlined (Subp);
1632 Set_Has_Pragma_Inline_Always (Subp);
1639 end Check_Inline_Pragma;
1641 -----------------------
1642 -- Disambiguate_Spec --
1643 -----------------------
1645 function Disambiguate_Spec return Entity_Id is
1646 Priv_Spec : Entity_Id;
1649 procedure Replace_Types (To_Corresponding : Boolean);
1650 -- Depending on the flag, replace the type of formal parameters of
1651 -- Body_Id if it is a concurrent type implementing interfaces with
1652 -- the corresponding record type or the other way around.
1654 procedure Replace_Types (To_Corresponding : Boolean) is
1656 Formal_Typ : Entity_Id;
1659 Formal := First_Formal (Body_Id);
1660 while Present (Formal) loop
1661 Formal_Typ := Etype (Formal);
1663 -- From concurrent type to corresponding record
1665 if To_Corresponding then
1666 if Is_Concurrent_Type (Formal_Typ)
1667 and then Present (Corresponding_Record_Type (Formal_Typ))
1668 and then Present (Interfaces (
1669 Corresponding_Record_Type (Formal_Typ)))
1672 Corresponding_Record_Type (Formal_Typ));
1675 -- From corresponding record to concurrent type
1678 if Is_Concurrent_Record_Type (Formal_Typ)
1679 and then Present (Interfaces (Formal_Typ))
1682 Corresponding_Concurrent_Type (Formal_Typ));
1686 Next_Formal (Formal);
1690 -- Start of processing for Disambiguate_Spec
1693 -- Try to retrieve the specification of the body as is. All error
1694 -- messages are suppressed because the body may not have a spec in
1695 -- its current state.
1697 Spec_N := Find_Corresponding_Spec (N, False);
1699 -- It is possible that this is the body of a primitive declared
1700 -- between a private and a full view of a concurrent type. The
1701 -- controlling parameter of the spec carries the concurrent type,
1702 -- not the corresponding record type as transformed by Analyze_
1703 -- Subprogram_Specification. In such cases, we undo the change
1704 -- made by the analysis of the specification and try to find the
1707 -- Note that wrappers already have their corresponding specs and
1708 -- bodies set during their creation, so if the candidate spec is
1709 -- a wrapper, then we definitely need to swap all types to their
1710 -- original concurrent status.
1713 or else Is_Primitive_Wrapper (Spec_N)
1715 -- Restore all references of corresponding record types to the
1716 -- original concurrent types.
1718 Replace_Types (To_Corresponding => False);
1719 Priv_Spec := Find_Corresponding_Spec (N, False);
1721 -- The current body truly belongs to a primitive declared between
1722 -- a private and a full view. We leave the modified body as is,
1723 -- and return the true spec.
1725 if Present (Priv_Spec)
1726 and then Is_Private_Primitive (Priv_Spec)
1731 -- In case that this is some sort of error, restore the original
1732 -- state of the body.
1734 Replace_Types (To_Corresponding => True);
1738 end Disambiguate_Spec;
1740 -------------------------------------
1741 -- Is_Private_Concurrent_Primitive --
1742 -------------------------------------
1744 function Is_Private_Concurrent_Primitive
1745 (Subp_Id : Entity_Id) return Boolean
1747 Formal_Typ : Entity_Id;
1750 if Present (First_Formal (Subp_Id)) then
1751 Formal_Typ := Etype (First_Formal (Subp_Id));
1753 if Is_Concurrent_Record_Type (Formal_Typ) then
1754 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1757 -- The type of the first formal is a concurrent tagged type with
1761 Is_Concurrent_Type (Formal_Typ)
1762 and then Is_Tagged_Type (Formal_Typ)
1763 and then Has_Private_Declaration (Formal_Typ);
1767 end Is_Private_Concurrent_Primitive;
1769 ----------------------------
1770 -- Set_Trivial_Subprogram --
1771 ----------------------------
1773 procedure Set_Trivial_Subprogram (N : Node_Id) is
1774 Nxt : constant Node_Id := Next (N);
1777 Set_Is_Trivial_Subprogram (Body_Id);
1779 if Present (Spec_Id) then
1780 Set_Is_Trivial_Subprogram (Spec_Id);
1784 and then Nkind (Nxt) = N_Simple_Return_Statement
1785 and then No (Next (Nxt))
1786 and then Present (Expression (Nxt))
1787 and then Is_Entity_Name (Expression (Nxt))
1789 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1791 end Set_Trivial_Subprogram;
1793 ---------------------------------
1794 -- Verify_Overriding_Indicator --
1795 ---------------------------------
1797 procedure Verify_Overriding_Indicator is
1799 if Must_Override (Body_Spec) then
1800 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1801 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1805 elsif not Is_Overriding_Operation (Spec_Id) then
1807 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1810 elsif Must_Not_Override (Body_Spec) then
1811 if Is_Overriding_Operation (Spec_Id) then
1813 ("subprogram& overrides inherited operation",
1814 Body_Spec, Spec_Id);
1816 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1817 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1820 ("subprogram & overrides predefined operator ",
1821 Body_Spec, Spec_Id);
1823 -- If this is not a primitive operation or protected subprogram,
1824 -- then the overriding indicator is altogether illegal.
1826 elsif not Is_Primitive (Spec_Id)
1827 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1829 Error_Msg_N ("overriding indicator only allowed " &
1830 "if subprogram is primitive",
1834 elsif Style_Check -- ??? incorrect use of Style_Check!
1835 and then Is_Overriding_Operation (Spec_Id)
1837 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1838 Style.Missing_Overriding (N, Body_Id);
1840 end Verify_Overriding_Indicator;
1842 -- Start of processing for Analyze_Subprogram_Body_Helper
1845 -- Generic subprograms are handled separately. They always have a
1846 -- generic specification. Determine whether current scope has a
1847 -- previous declaration.
1849 -- If the subprogram body is defined within an instance of the same
1850 -- name, the instance appears as a package renaming, and will be hidden
1851 -- within the subprogram.
1853 if Present (Prev_Id)
1854 and then not Is_Overloadable (Prev_Id)
1855 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1856 or else Comes_From_Source (Prev_Id))
1858 if Is_Generic_Subprogram (Prev_Id) then
1860 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1861 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1863 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1867 -- Previous entity conflicts with subprogram name. Attempting to
1868 -- enter name will post error.
1870 Enter_Name (Body_Id);
1874 -- Non-generic case, find the subprogram declaration, if one was seen,
1875 -- or enter new overloaded entity in the current scope. If the
1876 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1877 -- part of the context of one of its subunits. No need to redo the
1880 elsif Prev_Id = Body_Id
1881 and then Has_Completion (Body_Id)
1886 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1888 if Nkind (N) = N_Subprogram_Body_Stub
1889 or else No (Corresponding_Spec (N))
1891 if Is_Private_Concurrent_Primitive (Body_Id) then
1892 Spec_Id := Disambiguate_Spec;
1894 Spec_Id := Find_Corresponding_Spec (N);
1897 -- If this is a duplicate body, no point in analyzing it
1899 if Error_Posted (N) then
1903 -- A subprogram body should cause freezing of its own declaration,
1904 -- but if there was no previous explicit declaration, then the
1905 -- subprogram will get frozen too late (there may be code within
1906 -- the body that depends on the subprogram having been frozen,
1907 -- such as uses of extra formals), so we force it to be frozen
1908 -- here. Same holds if the body and spec are compilation units.
1909 -- Finally, if the return type is an anonymous access to protected
1910 -- subprogram, it must be frozen before the body because its
1911 -- expansion has generated an equivalent type that is used when
1912 -- elaborating the body.
1914 if No (Spec_Id) then
1915 Freeze_Before (N, Body_Id);
1917 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1918 Freeze_Before (N, Spec_Id);
1920 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
1921 Freeze_Before (N, Etype (Body_Id));
1925 Spec_Id := Corresponding_Spec (N);
1929 -- Do not inline any subprogram that contains nested subprograms, since
1930 -- the backend inlining circuit seems to generate uninitialized
1931 -- references in this case. We know this happens in the case of front
1932 -- end ZCX support, but it also appears it can happen in other cases as
1933 -- well. The backend often rejects attempts to inline in the case of
1934 -- nested procedures anyway, so little if anything is lost by this.
1935 -- Note that this is test is for the benefit of the back-end. There is
1936 -- a separate test for front-end inlining that also rejects nested
1939 -- Do not do this test if errors have been detected, because in some
1940 -- error cases, this code blows up, and we don't need it anyway if
1941 -- there have been errors, since we won't get to the linker anyway.
1943 if Comes_From_Source (Body_Id)
1944 and then Serious_Errors_Detected = 0
1948 P_Ent := Scope (P_Ent);
1949 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1951 if Is_Subprogram (P_Ent) then
1952 Set_Is_Inlined (P_Ent, False);
1954 if Comes_From_Source (P_Ent)
1955 and then Has_Pragma_Inline (P_Ent)
1958 ("cannot inline& (nested subprogram)?",
1965 Check_Inline_Pragma (Spec_Id);
1967 -- Deal with special case of a fully private operation in the body of
1968 -- the protected type. We must create a declaration for the subprogram,
1969 -- in order to attach the protected subprogram that will be used in
1970 -- internal calls. We exclude compiler generated bodies from the
1971 -- expander since the issue does not arise for those cases.
1974 and then Comes_From_Source (N)
1975 and then Is_Protected_Type (Current_Scope)
1984 Formal := First_Formal (Body_Id);
1986 -- The protected operation always has at least one formal, namely
1987 -- the object itself, but it is only placed in the parameter list
1988 -- if expansion is enabled.
1991 or else Expander_Active
1993 Plist := Copy_Parameter_List (Body_Id);
1998 if Nkind (Body_Spec) = N_Procedure_Specification then
2000 Make_Procedure_Specification (Loc,
2001 Defining_Unit_Name =>
2002 Make_Defining_Identifier (Sloc (Body_Id),
2003 Chars => Chars (Body_Id)),
2004 Parameter_Specifications => Plist);
2007 Make_Function_Specification (Loc,
2008 Defining_Unit_Name =>
2009 Make_Defining_Identifier (Sloc (Body_Id),
2010 Chars => Chars (Body_Id)),
2011 Parameter_Specifications => Plist,
2012 Result_Definition =>
2013 New_Occurrence_Of (Etype (Body_Id), Loc));
2017 Make_Subprogram_Declaration (Loc,
2018 Specification => New_Spec);
2019 Insert_Before (N, Decl);
2020 Spec_Id := Defining_Unit_Name (New_Spec);
2022 -- Indicate that the entity comes from source, to ensure that
2023 -- cross-reference information is properly generated. The body
2024 -- itself is rewritten during expansion, and the body entity will
2025 -- not appear in calls to the operation.
2027 Set_Comes_From_Source (Spec_Id, True);
2029 Set_Has_Completion (Spec_Id);
2030 Set_Convention (Spec_Id, Convention_Protected);
2034 -- If a separate spec is present, then deal with freezing issues
2036 if Present (Spec_Id) then
2037 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2038 Verify_Overriding_Indicator;
2040 -- In general, the spec will be frozen when we start analyzing the
2041 -- body. However, for internally generated operations, such as
2042 -- wrapper functions for inherited operations with controlling
2043 -- results, the spec may not have been frozen by the time we
2044 -- expand the freeze actions that include the bodies. In particular,
2045 -- extra formals for accessibility or for return-in-place may need
2046 -- to be generated. Freeze nodes, if any, are inserted before the
2049 if not Is_Frozen (Spec_Id)
2050 and then Expander_Active
2052 -- Force the generation of its freezing node to ensure proper
2053 -- management of access types in the backend.
2055 -- This is definitely needed for some cases, but it is not clear
2056 -- why, to be investigated further???
2058 Set_Has_Delayed_Freeze (Spec_Id);
2059 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
2063 -- Mark presence of postcondition proc in current scope
2065 if Chars (Body_Id) = Name_uPostconditions then
2066 Set_Has_Postconditions (Current_Scope);
2069 -- Place subprogram on scope stack, and make formals visible. If there
2070 -- is a spec, the visible entity remains that of the spec.
2072 if Present (Spec_Id) then
2073 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2075 if Is_Child_Unit (Spec_Id) then
2076 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2080 Style.Check_Identifier (Body_Id, Spec_Id);
2083 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2084 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2086 if Is_Abstract_Subprogram (Spec_Id) then
2087 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2091 Set_Convention (Body_Id, Convention (Spec_Id));
2092 Set_Has_Completion (Spec_Id);
2094 if Is_Protected_Type (Scope (Spec_Id)) then
2095 Prot_Typ := Scope (Spec_Id);
2098 -- If this is a body generated for a renaming, do not check for
2099 -- full conformance. The check is redundant, because the spec of
2100 -- the body is a copy of the spec in the renaming declaration,
2101 -- and the test can lead to spurious errors on nested defaults.
2103 if Present (Spec_Decl)
2104 and then not Comes_From_Source (N)
2106 (Nkind (Original_Node (Spec_Decl)) =
2107 N_Subprogram_Renaming_Declaration
2108 or else (Present (Corresponding_Body (Spec_Decl))
2110 Nkind (Unit_Declaration_Node
2111 (Corresponding_Body (Spec_Decl))) =
2112 N_Subprogram_Renaming_Declaration))
2119 Fully_Conformant, True, Conformant, Body_Id);
2122 -- If the body is not fully conformant, we have to decide if we
2123 -- should analyze it or not. If it has a really messed up profile
2124 -- then we probably should not analyze it, since we will get too
2125 -- many bogus messages.
2127 -- Our decision is to go ahead in the non-fully conformant case
2128 -- only if it is at least mode conformant with the spec. Note
2129 -- that the call to Check_Fully_Conformant has issued the proper
2130 -- error messages to complain about the lack of conformance.
2133 and then not Mode_Conformant (Body_Id, Spec_Id)
2139 if Spec_Id /= Body_Id then
2140 Reference_Body_Formals (Spec_Id, Body_Id);
2143 if Nkind (N) /= N_Subprogram_Body_Stub then
2144 Set_Corresponding_Spec (N, Spec_Id);
2146 -- Ada 2005 (AI-345): If the operation is a primitive operation
2147 -- of a concurrent type, the type of the first parameter has been
2148 -- replaced with the corresponding record, which is the proper
2149 -- run-time structure to use. However, within the body there may
2150 -- be uses of the formals that depend on primitive operations
2151 -- of the type (in particular calls in prefixed form) for which
2152 -- we need the original concurrent type. The operation may have
2153 -- several controlling formals, so the replacement must be done
2156 if Comes_From_Source (Spec_Id)
2157 and then Present (First_Entity (Spec_Id))
2158 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2159 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2161 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2164 (Corresponding_Concurrent_Type
2165 (Etype (First_Entity (Spec_Id))))
2168 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2172 Form := First_Formal (Spec_Id);
2173 while Present (Form) loop
2174 if Etype (Form) = Typ then
2175 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2183 -- Make the formals visible, and place subprogram on scope stack.
2184 -- This is also the point at which we set Last_Real_Spec_Entity
2185 -- to mark the entities which will not be moved to the body.
2187 Install_Formals (Spec_Id);
2188 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2189 Push_Scope (Spec_Id);
2191 -- Make sure that the subprogram is immediately visible. For
2192 -- child units that have no separate spec this is indispensable.
2193 -- Otherwise it is safe albeit redundant.
2195 Set_Is_Immediately_Visible (Spec_Id);
2198 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2199 Set_Ekind (Body_Id, E_Subprogram_Body);
2200 Set_Scope (Body_Id, Scope (Spec_Id));
2201 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2203 -- Case of subprogram body with no previous spec
2207 and then Comes_From_Source (Body_Id)
2208 and then not Suppress_Style_Checks (Body_Id)
2209 and then not In_Instance
2211 Style.Body_With_No_Spec (N);
2214 New_Overloaded_Entity (Body_Id);
2216 if Nkind (N) /= N_Subprogram_Body_Stub then
2217 Set_Acts_As_Spec (N);
2218 Generate_Definition (Body_Id);
2220 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2221 Generate_Reference_To_Formals (Body_Id);
2222 Install_Formals (Body_Id);
2223 Push_Scope (Body_Id);
2227 -- If the return type is an anonymous access type whose designated type
2228 -- is the limited view of a class-wide type and the non-limited view is
2229 -- available, update the return type accordingly.
2231 if Ada_Version >= Ada_05
2232 and then Comes_From_Source (N)
2239 Rtyp := Etype (Current_Scope);
2241 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2242 Etyp := Directly_Designated_Type (Rtyp);
2244 if Is_Class_Wide_Type (Etyp)
2245 and then From_With_Type (Etyp)
2247 Set_Directly_Designated_Type
2248 (Etype (Current_Scope), Available_View (Etyp));
2254 -- If this is the proper body of a stub, we must verify that the stub
2255 -- conforms to the body, and to the previous spec if one was present.
2256 -- we know already that the body conforms to that spec. This test is
2257 -- only required for subprograms that come from source.
2259 if Nkind (Parent (N)) = N_Subunit
2260 and then Comes_From_Source (N)
2261 and then not Error_Posted (Body_Id)
2262 and then Nkind (Corresponding_Stub (Parent (N))) =
2263 N_Subprogram_Body_Stub
2266 Old_Id : constant Entity_Id :=
2268 (Specification (Corresponding_Stub (Parent (N))));
2270 Conformant : Boolean := False;
2273 if No (Spec_Id) then
2274 Check_Fully_Conformant (Body_Id, Old_Id);
2278 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2280 if not Conformant then
2282 -- The stub was taken to be a new declaration. Indicate
2283 -- that it lacks a body.
2285 Set_Has_Completion (Old_Id, False);
2291 Set_Has_Completion (Body_Id);
2292 Check_Eliminated (Body_Id);
2294 if Nkind (N) = N_Subprogram_Body_Stub then
2297 elsif Present (Spec_Id)
2298 and then Expander_Active
2300 (Has_Pragma_Inline_Always (Spec_Id)
2301 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2303 Build_Body_To_Inline (N, Spec_Id);
2306 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2307 -- if its specification we have to install the private withed units.
2308 -- This holds for child units as well.
2310 if Is_Compilation_Unit (Body_Id)
2311 or else Nkind (Parent (N)) = N_Compilation_Unit
2313 Install_Private_With_Clauses (Body_Id);
2316 Check_Anonymous_Return;
2318 -- Set the Protected_Formal field of each extra formal of the protected
2319 -- subprogram to reference the corresponding extra formal of the
2320 -- subprogram that implements it. For regular formals this occurs when
2321 -- the protected subprogram's declaration is expanded, but the extra
2322 -- formals don't get created until the subprogram is frozen. We need to
2323 -- do this before analyzing the protected subprogram's body so that any
2324 -- references to the original subprogram's extra formals will be changed
2325 -- refer to the implementing subprogram's formals (see Expand_Formal).
2327 if Present (Spec_Id)
2328 and then Is_Protected_Type (Scope (Spec_Id))
2329 and then Present (Protected_Body_Subprogram (Spec_Id))
2332 Impl_Subp : constant Entity_Id :=
2333 Protected_Body_Subprogram (Spec_Id);
2334 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2335 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2337 while Present (Prot_Ext_Formal) loop
2338 pragma Assert (Present (Impl_Ext_Formal));
2339 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2340 Next_Formal_With_Extras (Prot_Ext_Formal);
2341 Next_Formal_With_Extras (Impl_Ext_Formal);
2346 -- Now we can go on to analyze the body
2348 HSS := Handled_Statement_Sequence (N);
2349 Set_Actual_Subtypes (N, Current_Scope);
2351 -- Deal with preconditions and postconditions
2353 Process_PPCs (N, Spec_Id, Body_Id);
2355 -- Add a declaration for the Protection object, renaming declarations
2356 -- for discriminals and privals and finally a declaration for the entry
2357 -- family index (if applicable). This form of early expansion is done
2358 -- when the Expander is active because Install_Private_Data_Declarations
2359 -- references entities which were created during regular expansion.
2362 and then Comes_From_Source (N)
2363 and then Present (Prot_Typ)
2364 and then Present (Spec_Id)
2365 and then not Is_Eliminated (Spec_Id)
2367 Install_Private_Data_Declarations
2368 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2371 -- Analyze the declarations (this call will analyze the precondition
2372 -- Check pragmas we prepended to the list, as well as the declaration
2373 -- of the _Postconditions procedure).
2375 Analyze_Declarations (Declarations (N));
2377 -- Check completion, and analyze the statements
2380 Inspect_Deferred_Constant_Completion (Declarations (N));
2383 -- Deal with end of scope processing for the body
2385 Process_End_Label (HSS, 't', Current_Scope);
2387 Check_Subprogram_Order (N);
2388 Set_Analyzed (Body_Id);
2390 -- If we have a separate spec, then the analysis of the declarations
2391 -- caused the entities in the body to be chained to the spec id, but
2392 -- we want them chained to the body id. Only the formal parameters
2393 -- end up chained to the spec id in this case.
2395 if Present (Spec_Id) then
2397 -- We must conform to the categorization of our spec
2399 Validate_Categorization_Dependency (N, Spec_Id);
2401 -- And if this is a child unit, the parent units must conform
2403 if Is_Child_Unit (Spec_Id) then
2404 Validate_Categorization_Dependency
2405 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2408 -- Here is where we move entities from the spec to the body
2410 -- Case where there are entities that stay with the spec
2412 if Present (Last_Real_Spec_Entity) then
2414 -- No body entities (happens when the only real spec entities
2415 -- come from precondition and postcondition pragmas)
2417 if No (Last_Entity (Body_Id)) then
2419 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2421 -- Body entities present (formals), so chain stuff past them
2425 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2428 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2429 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2430 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2432 -- Case where there are no spec entities, in this case there can
2433 -- be no body entities either, so just move everything.
2436 pragma Assert (No (Last_Entity (Body_Id)));
2437 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2438 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2439 Set_First_Entity (Spec_Id, Empty);
2440 Set_Last_Entity (Spec_Id, Empty);
2444 -- If function, check return statements
2446 if Nkind (Body_Spec) = N_Function_Specification then
2451 if Present (Spec_Id) then
2457 if Return_Present (Id) then
2458 Check_Returns (HSS, 'F', Missing_Ret);
2461 Set_Has_Missing_Return (Id);
2464 elsif not Is_Machine_Code_Subprogram (Id)
2465 and then not Body_Deleted
2467 Error_Msg_N ("missing RETURN statement in function body", N);
2471 -- If procedure with No_Return, check returns
2473 elsif Nkind (Body_Spec) = N_Procedure_Specification
2474 and then Present (Spec_Id)
2475 and then No_Return (Spec_Id)
2477 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2480 -- Now we are going to check for variables that are never modified in
2481 -- the body of the procedure. But first we deal with a special case
2482 -- where we want to modify this check. If the body of the subprogram
2483 -- starts with a raise statement or its equivalent, or if the body
2484 -- consists entirely of a null statement, then it is pretty obvious
2485 -- that it is OK to not reference the parameters. For example, this
2486 -- might be the following common idiom for a stubbed function:
2487 -- statement of the procedure raises an exception. In particular this
2488 -- deals with the common idiom of a stubbed function, which might
2489 -- appear as something like
2491 -- function F (A : Integer) return Some_Type;
2494 -- raise Program_Error;
2498 -- Here the purpose of X is simply to satisfy the annoying requirement
2499 -- in Ada that there be at least one return, and we certainly do not
2500 -- want to go posting warnings on X that it is not initialized! On
2501 -- the other hand, if X is entirely unreferenced that should still
2504 -- What we do is to detect these cases, and if we find them, flag the
2505 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2506 -- suppress unwanted warnings. For the case of the function stub above
2507 -- we have a special test to set X as apparently assigned to suppress
2514 -- Skip initial labels (for one thing this occurs when we are in
2515 -- front end ZCX mode, but in any case it is irrelevant), and also
2516 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2518 Stm := First (Statements (HSS));
2519 while Nkind (Stm) = N_Label
2520 or else Nkind (Stm) in N_Push_xxx_Label
2525 -- Do the test on the original statement before expansion
2528 Ostm : constant Node_Id := Original_Node (Stm);
2531 -- If explicit raise statement, turn on flag
2533 if Nkind (Ostm) = N_Raise_Statement then
2534 Set_Trivial_Subprogram (Stm);
2536 -- If null statement, and no following statements, turn on flag
2538 elsif Nkind (Stm) = N_Null_Statement
2539 and then Comes_From_Source (Stm)
2540 and then No (Next (Stm))
2542 Set_Trivial_Subprogram (Stm);
2544 -- Check for explicit call cases which likely raise an exception
2546 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2547 if Is_Entity_Name (Name (Ostm)) then
2549 Ent : constant Entity_Id := Entity (Name (Ostm));
2552 -- If the procedure is marked No_Return, then likely it
2553 -- raises an exception, but in any case it is not coming
2554 -- back here, so turn on the flag.
2556 if Ekind (Ent) = E_Procedure
2557 and then No_Return (Ent)
2559 Set_Trivial_Subprogram (Stm);
2567 -- Check for variables that are never modified
2573 -- If there is a separate spec, then transfer Never_Set_In_Source
2574 -- flags from out parameters to the corresponding entities in the
2575 -- body. The reason we do that is we want to post error flags on
2576 -- the body entities, not the spec entities.
2578 if Present (Spec_Id) then
2579 E1 := First_Entity (Spec_Id);
2580 while Present (E1) loop
2581 if Ekind (E1) = E_Out_Parameter then
2582 E2 := First_Entity (Body_Id);
2583 while Present (E2) loop
2584 exit when Chars (E1) = Chars (E2);
2588 if Present (E2) then
2589 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2597 -- Check references in body unless it was deleted. Note that the
2598 -- check of Body_Deleted here is not just for efficiency, it is
2599 -- necessary to avoid junk warnings on formal parameters.
2601 if not Body_Deleted then
2602 Check_References (Body_Id);
2605 end Analyze_Subprogram_Body_Helper;
2607 ------------------------------------
2608 -- Analyze_Subprogram_Declaration --
2609 ------------------------------------
2611 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2612 Loc : constant Source_Ptr := Sloc (N);
2613 Designator : Entity_Id;
2615 Scop : constant Entity_Id := Current_Scope;
2616 Null_Body : Node_Id := Empty;
2618 -- Start of processing for Analyze_Subprogram_Declaration
2621 -- For a null procedure, capture the profile before analysis, for
2622 -- expansion at the freeze point and at each point of call.
2623 -- The body will only be used if the procedure has preconditions.
2624 -- In that case the body is analyzed at the freeze point.
2626 if Nkind (Specification (N)) = N_Procedure_Specification
2627 and then Null_Present (Specification (N))
2628 and then Expander_Active
2631 Make_Subprogram_Body (Loc,
2633 New_Copy_Tree (Specification (N)),
2636 Handled_Statement_Sequence =>
2637 Make_Handled_Sequence_Of_Statements (Loc,
2638 Statements => New_List (Make_Null_Statement (Loc))));
2640 -- Create new entities for body and formals.
2642 Set_Defining_Unit_Name (Specification (Null_Body),
2643 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2644 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2646 Form := First (Parameter_Specifications (Specification (Null_Body)));
2647 while Present (Form) loop
2648 Set_Defining_Identifier (Form,
2649 Make_Defining_Identifier (Loc,
2650 Chars (Defining_Identifier (Form))));
2654 if Is_Protected_Type (Current_Scope) then
2656 ("protected operation cannot be a null procedure", N);
2660 Designator := Analyze_Subprogram_Specification (Specification (N));
2661 Generate_Definition (Designator);
2663 if Debug_Flag_C then
2664 Write_Str ("==> subprogram spec ");
2665 Write_Name (Chars (Designator));
2666 Write_Str (" from ");
2667 Write_Location (Sloc (N));
2672 if Nkind (Specification (N)) = N_Procedure_Specification
2673 and then Null_Present (Specification (N))
2675 Set_Has_Completion (Designator);
2677 if Present (Null_Body) then
2678 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2679 Set_Body_To_Inline (N, Null_Body);
2680 Set_Is_Inlined (Designator);
2684 Validate_RCI_Subprogram_Declaration (N);
2685 New_Overloaded_Entity (Designator);
2686 Check_Delayed_Subprogram (Designator);
2688 -- If the type of the first formal of the current subprogram is a
2689 -- nongeneric tagged private type, mark the subprogram as being a
2690 -- private primitive. Ditto if this is a function with controlling
2691 -- result, and the return type is currently private.
2693 if Has_Controlling_Result (Designator)
2694 and then Is_Private_Type (Etype (Designator))
2695 and then not Is_Generic_Actual_Type (Etype (Designator))
2697 Set_Is_Private_Primitive (Designator);
2699 elsif Present (First_Formal (Designator)) then
2701 Formal_Typ : constant Entity_Id :=
2702 Etype (First_Formal (Designator));
2704 Set_Is_Private_Primitive (Designator,
2705 Is_Tagged_Type (Formal_Typ)
2706 and then Is_Private_Type (Formal_Typ)
2707 and then not Is_Generic_Actual_Type (Formal_Typ));
2711 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2714 if Ada_Version >= Ada_05
2715 and then Comes_From_Source (N)
2716 and then Is_Dispatching_Operation (Designator)
2723 if Has_Controlling_Result (Designator) then
2724 Etyp := Etype (Designator);
2727 E := First_Entity (Designator);
2729 and then Is_Formal (E)
2730 and then not Is_Controlling_Formal (E)
2738 if Is_Access_Type (Etyp) then
2739 Etyp := Directly_Designated_Type (Etyp);
2742 if Is_Interface (Etyp)
2743 and then not Is_Abstract_Subprogram (Designator)
2744 and then not (Ekind (Designator) = E_Procedure
2745 and then Null_Present (Specification (N)))
2747 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2749 ("(Ada 2005) interface subprogram % must be abstract or null",
2755 -- What is the following code for, it used to be
2757 -- ??? Set_Suppress_Elaboration_Checks
2758 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2760 -- The following seems equivalent, but a bit dubious
2762 if Elaboration_Checks_Suppressed (Designator) then
2763 Set_Kill_Elaboration_Checks (Designator);
2766 if Scop /= Standard_Standard
2767 and then not Is_Child_Unit (Designator)
2769 Set_Categorization_From_Scope (Designator, Scop);
2771 -- For a compilation unit, check for library-unit pragmas
2773 Push_Scope (Designator);
2774 Set_Categorization_From_Pragmas (N);
2775 Validate_Categorization_Dependency (N, Designator);
2779 -- For a compilation unit, set body required. This flag will only be
2780 -- reset if a valid Import or Interface pragma is processed later on.
2782 if Nkind (Parent (N)) = N_Compilation_Unit then
2783 Set_Body_Required (Parent (N), True);
2785 if Ada_Version >= Ada_05
2786 and then Nkind (Specification (N)) = N_Procedure_Specification
2787 and then Null_Present (Specification (N))
2790 ("null procedure cannot be declared at library level", N);
2794 Generate_Reference_To_Formals (Designator);
2795 Check_Eliminated (Designator);
2797 if Debug_Flag_C then
2799 Write_Str ("<== subprogram spec ");
2800 Write_Name (Chars (Designator));
2801 Write_Str (" from ");
2802 Write_Location (Sloc (N));
2805 end Analyze_Subprogram_Declaration;
2807 --------------------------------------
2808 -- Analyze_Subprogram_Specification --
2809 --------------------------------------
2811 -- Reminder: N here really is a subprogram specification (not a subprogram
2812 -- declaration). This procedure is called to analyze the specification in
2813 -- both subprogram bodies and subprogram declarations (specs).
2815 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2816 Designator : constant Entity_Id := Defining_Entity (N);
2817 Formals : constant List_Id := Parameter_Specifications (N);
2819 -- Start of processing for Analyze_Subprogram_Specification
2822 Generate_Definition (Designator);
2824 if Nkind (N) = N_Function_Specification then
2825 Set_Ekind (Designator, E_Function);
2826 Set_Mechanism (Designator, Default_Mechanism);
2829 Set_Ekind (Designator, E_Procedure);
2830 Set_Etype (Designator, Standard_Void_Type);
2833 -- Introduce new scope for analysis of the formals and the return type
2835 Set_Scope (Designator, Current_Scope);
2837 if Present (Formals) then
2838 Push_Scope (Designator);
2839 Process_Formals (Formals, N);
2841 -- Ada 2005 (AI-345): If this is an overriding operation of an
2842 -- inherited interface operation, and the controlling type is
2843 -- a synchronized type, replace the type with its corresponding
2844 -- record, to match the proper signature of an overriding operation.
2845 -- Same processing for an access parameter whose designated type is
2846 -- derived from a synchronized interface.
2848 if Ada_Version >= Ada_05 then
2851 Formal_Typ : Entity_Id;
2852 Rec_Typ : Entity_Id;
2853 Desig_Typ : Entity_Id;
2856 Formal := First_Formal (Designator);
2857 while Present (Formal) loop
2858 Formal_Typ := Etype (Formal);
2860 if Is_Concurrent_Type (Formal_Typ)
2861 and then Present (Corresponding_Record_Type (Formal_Typ))
2863 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2865 if Present (Interfaces (Rec_Typ)) then
2866 Set_Etype (Formal, Rec_Typ);
2869 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
2870 Desig_Typ := Designated_Type (Formal_Typ);
2872 if Is_Concurrent_Type (Desig_Typ)
2873 and then Present (Corresponding_Record_Type (Desig_Typ))
2875 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
2877 if Present (Interfaces (Rec_Typ)) then
2878 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
2883 Next_Formal (Formal);
2890 -- The subprogram scope is pushed and popped around the processing of
2891 -- the return type for consistency with call above to Process_Formals
2892 -- (which itself can call Analyze_Return_Type), and to ensure that any
2893 -- itype created for the return type will be associated with the proper
2896 elsif Nkind (N) = N_Function_Specification then
2897 Push_Scope (Designator);
2899 Analyze_Return_Type (N);
2904 if Nkind (N) = N_Function_Specification then
2905 if Nkind (Designator) = N_Defining_Operator_Symbol then
2906 Valid_Operator_Definition (Designator);
2909 May_Need_Actuals (Designator);
2911 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2912 -- the subprogram is abstract also. This does not apply to renaming
2913 -- declarations, where abstractness is inherited.
2914 -- In case of primitives associated with abstract interface types
2915 -- the check is applied later (see Analyze_Subprogram_Declaration).
2917 if Is_Abstract_Type (Etype (Designator))
2918 and then not Is_Interface (Etype (Designator))
2919 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2920 and then Nkind (Parent (N)) /=
2921 N_Abstract_Subprogram_Declaration
2923 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2926 ("function that returns abstract type must be abstract", N);
2931 end Analyze_Subprogram_Specification;
2933 --------------------------
2934 -- Build_Body_To_Inline --
2935 --------------------------
2937 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2938 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2939 Original_Body : Node_Id;
2940 Body_To_Analyze : Node_Id;
2941 Max_Size : constant := 10;
2942 Stat_Count : Integer := 0;
2944 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2945 -- Check for declarations that make inlining not worthwhile
2947 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2948 -- Check for statements that make inlining not worthwhile: any tasking
2949 -- statement, nested at any level. Keep track of total number of
2950 -- elementary statements, as a measure of acceptable size.
2952 function Has_Pending_Instantiation return Boolean;
2953 -- If some enclosing body contains instantiations that appear before the
2954 -- corresponding generic body, the enclosing body has a freeze node so
2955 -- that it can be elaborated after the generic itself. This might
2956 -- conflict with subsequent inlinings, so that it is unsafe to try to
2957 -- inline in such a case.
2959 function Has_Single_Return return Boolean;
2960 -- In general we cannot inline functions that return unconstrained type.
2961 -- However, we can handle such functions if all return statements return
2962 -- a local variable that is the only declaration in the body of the
2963 -- function. In that case the call can be replaced by that local
2964 -- variable as is done for other inlined calls.
2966 procedure Remove_Pragmas;
2967 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2968 -- parameter has no meaning when the body is inlined and the formals
2969 -- are rewritten. Remove it from body to inline. The analysis of the
2970 -- non-inlined body will handle the pragma properly.
2972 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2973 -- If the body of the subprogram includes a call that returns an
2974 -- unconstrained type, the secondary stack is involved, and it
2975 -- is not worth inlining.
2977 ------------------------------
2978 -- Has_Excluded_Declaration --
2979 ------------------------------
2981 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2984 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2985 -- Nested subprograms make a given body ineligible for inlining, but
2986 -- we make an exception for instantiations of unchecked conversion.
2987 -- The body has not been analyzed yet, so check the name, and verify
2988 -- that the visible entity with that name is the predefined unit.
2990 -----------------------------
2991 -- Is_Unchecked_Conversion --
2992 -----------------------------
2994 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2995 Id : constant Node_Id := Name (D);
2999 if Nkind (Id) = N_Identifier
3000 and then Chars (Id) = Name_Unchecked_Conversion
3002 Conv := Current_Entity (Id);
3004 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3005 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3007 Conv := Current_Entity (Selector_Name (Id));
3012 return Present (Conv)
3013 and then Is_Predefined_File_Name
3014 (Unit_File_Name (Get_Source_Unit (Conv)))
3015 and then Is_Intrinsic_Subprogram (Conv);
3016 end Is_Unchecked_Conversion;
3018 -- Start of processing for Has_Excluded_Declaration
3022 while Present (D) loop
3023 if (Nkind (D) = N_Function_Instantiation
3024 and then not Is_Unchecked_Conversion (D))
3025 or else Nkind_In (D, N_Protected_Type_Declaration,
3026 N_Package_Declaration,
3027 N_Package_Instantiation,
3029 N_Procedure_Instantiation,
3030 N_Task_Type_Declaration)
3033 ("cannot inline & (non-allowed declaration)?", D, Subp);
3041 end Has_Excluded_Declaration;
3043 ----------------------------
3044 -- Has_Excluded_Statement --
3045 ----------------------------
3047 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3053 while Present (S) loop
3054 Stat_Count := Stat_Count + 1;
3056 if Nkind_In (S, N_Abort_Statement,
3057 N_Asynchronous_Select,
3058 N_Conditional_Entry_Call,
3059 N_Delay_Relative_Statement,
3060 N_Delay_Until_Statement,
3065 ("cannot inline & (non-allowed statement)?", S, Subp);
3068 elsif Nkind (S) = N_Block_Statement then
3069 if Present (Declarations (S))
3070 and then Has_Excluded_Declaration (Declarations (S))
3074 elsif Present (Handled_Statement_Sequence (S))
3077 (Exception_Handlers (Handled_Statement_Sequence (S)))
3079 Has_Excluded_Statement
3080 (Statements (Handled_Statement_Sequence (S))))
3085 elsif Nkind (S) = N_Case_Statement then
3086 E := First (Alternatives (S));
3087 while Present (E) loop
3088 if Has_Excluded_Statement (Statements (E)) then
3095 elsif Nkind (S) = N_If_Statement then
3096 if Has_Excluded_Statement (Then_Statements (S)) then
3100 if Present (Elsif_Parts (S)) then
3101 E := First (Elsif_Parts (S));
3102 while Present (E) loop
3103 if Has_Excluded_Statement (Then_Statements (E)) then
3110 if Present (Else_Statements (S))
3111 and then Has_Excluded_Statement (Else_Statements (S))
3116 elsif Nkind (S) = N_Loop_Statement
3117 and then Has_Excluded_Statement (Statements (S))
3126 end Has_Excluded_Statement;
3128 -------------------------------
3129 -- Has_Pending_Instantiation --
3130 -------------------------------
3132 function Has_Pending_Instantiation return Boolean is
3137 while Present (S) loop
3138 if Is_Compilation_Unit (S)
3139 or else Is_Child_Unit (S)
3142 elsif Ekind (S) = E_Package
3143 and then Has_Forward_Instantiation (S)
3152 end Has_Pending_Instantiation;
3154 ------------------------
3155 -- Has_Single_Return --
3156 ------------------------
3158 function Has_Single_Return return Boolean is
3159 Return_Statement : Node_Id := Empty;
3161 function Check_Return (N : Node_Id) return Traverse_Result;
3167 function Check_Return (N : Node_Id) return Traverse_Result is
3169 if Nkind (N) = N_Simple_Return_Statement then
3170 if Present (Expression (N))
3171 and then Is_Entity_Name (Expression (N))
3173 if No (Return_Statement) then
3174 Return_Statement := N;
3177 elsif Chars (Expression (N)) =
3178 Chars (Expression (Return_Statement))
3187 -- Expression has wrong form
3197 function Check_All_Returns is new Traverse_Func (Check_Return);
3199 -- Start of processing for Has_Single_Return
3202 return Check_All_Returns (N) = OK
3203 and then Present (Declarations (N))
3204 and then Present (First (Declarations (N)))
3205 and then Chars (Expression (Return_Statement)) =
3206 Chars (Defining_Identifier (First (Declarations (N))));
3207 end Has_Single_Return;
3209 --------------------
3210 -- Remove_Pragmas --
3211 --------------------
3213 procedure Remove_Pragmas is
3218 Decl := First (Declarations (Body_To_Analyze));
3219 while Present (Decl) loop
3222 if Nkind (Decl) = N_Pragma
3223 and then (Pragma_Name (Decl) = Name_Unreferenced
3225 Pragma_Name (Decl) = Name_Unmodified)
3234 --------------------------
3235 -- Uses_Secondary_Stack --
3236 --------------------------
3238 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3239 function Check_Call (N : Node_Id) return Traverse_Result;
3240 -- Look for function calls that return an unconstrained type
3246 function Check_Call (N : Node_Id) return Traverse_Result is
3248 if Nkind (N) = N_Function_Call
3249 and then Is_Entity_Name (Name (N))
3250 and then Is_Composite_Type (Etype (Entity (Name (N))))
3251 and then not Is_Constrained (Etype (Entity (Name (N))))
3254 ("cannot inline & (call returns unconstrained type)?",
3262 function Check_Calls is new Traverse_Func (Check_Call);
3265 return Check_Calls (Bod) = Abandon;
3266 end Uses_Secondary_Stack;
3268 -- Start of processing for Build_Body_To_Inline
3271 -- Return immediately if done already
3273 if Nkind (Decl) = N_Subprogram_Declaration
3274 and then Present (Body_To_Inline (Decl))
3278 -- Functions that return unconstrained composite types require
3279 -- secondary stack handling, and cannot currently be inlined, unless
3280 -- all return statements return a local variable that is the first
3281 -- local declaration in the body.
3283 elsif Ekind (Subp) = E_Function
3284 and then not Is_Scalar_Type (Etype (Subp))
3285 and then not Is_Access_Type (Etype (Subp))
3286 and then not Is_Constrained (Etype (Subp))
3288 if not Has_Single_Return then
3290 ("cannot inline & (unconstrained return type)?", N, Subp);
3294 -- Ditto for functions that return controlled types, where controlled
3295 -- actions interfere in complex ways with inlining.
3297 elsif Ekind (Subp) = E_Function
3298 and then Needs_Finalization (Etype (Subp))
3301 ("cannot inline & (controlled return type)?", N, Subp);
3305 if Present (Declarations (N))
3306 and then Has_Excluded_Declaration (Declarations (N))
3311 if Present (Handled_Statement_Sequence (N)) then
3312 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3314 ("cannot inline& (exception handler)?",
3315 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3319 Has_Excluded_Statement
3320 (Statements (Handled_Statement_Sequence (N)))
3326 -- We do not inline a subprogram that is too large, unless it is
3327 -- marked Inline_Always. This pragma does not suppress the other
3328 -- checks on inlining (forbidden declarations, handlers, etc).
3330 if Stat_Count > Max_Size
3331 and then not Has_Pragma_Inline_Always (Subp)
3333 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3337 if Has_Pending_Instantiation then
3339 ("cannot inline& (forward instance within enclosing body)?",
3344 -- Within an instance, the body to inline must be treated as a nested
3345 -- generic, so that the proper global references are preserved.
3347 -- Note that we do not do this at the library level, because it is not
3348 -- needed, and furthermore this causes trouble if front end inlining
3349 -- is activated (-gnatN).
3351 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3352 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3353 Original_Body := Copy_Generic_Node (N, Empty, True);
3355 Original_Body := Copy_Separate_Tree (N);
3358 -- We need to capture references to the formals in order to substitute
3359 -- the actuals at the point of inlining, i.e. instantiation. To treat
3360 -- the formals as globals to the body to inline, we nest it within
3361 -- a dummy parameterless subprogram, declared within the real one.
3362 -- To avoid generating an internal name (which is never public, and
3363 -- which affects serial numbers of other generated names), we use
3364 -- an internal symbol that cannot conflict with user declarations.
3366 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3367 Set_Defining_Unit_Name
3368 (Specification (Original_Body),
3369 Make_Defining_Identifier (Sloc (N), Name_uParent));
3370 Set_Corresponding_Spec (Original_Body, Empty);
3372 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3374 -- Set return type of function, which is also global and does not need
3377 if Ekind (Subp) = E_Function then
3378 Set_Result_Definition (Specification (Body_To_Analyze),
3379 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3382 if No (Declarations (N)) then
3383 Set_Declarations (N, New_List (Body_To_Analyze));
3385 Append (Body_To_Analyze, Declarations (N));
3388 Expander_Mode_Save_And_Set (False);
3391 Analyze (Body_To_Analyze);
3392 Push_Scope (Defining_Entity (Body_To_Analyze));
3393 Save_Global_References (Original_Body);
3395 Remove (Body_To_Analyze);
3397 Expander_Mode_Restore;
3399 -- Restore environment if previously saved
3401 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3405 -- If secondary stk used there is no point in inlining. We have
3406 -- already issued the warning in this case, so nothing to do.
3408 if Uses_Secondary_Stack (Body_To_Analyze) then
3412 Set_Body_To_Inline (Decl, Original_Body);
3413 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3414 Set_Is_Inlined (Subp);
3415 end Build_Body_To_Inline;
3421 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3423 -- Do not emit warning if this is a predefined unit which is not
3424 -- the main unit. With validity checks enabled, some predefined
3425 -- subprograms may contain nested subprograms and become ineligible
3428 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3429 and then not In_Extended_Main_Source_Unit (Subp)
3433 elsif Has_Pragma_Inline_Always (Subp) then
3435 -- Remove last character (question mark) to make this into an error,
3436 -- because the Inline_Always pragma cannot be obeyed.
3438 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3440 elsif Ineffective_Inline_Warnings then
3441 Error_Msg_NE (Msg, N, Subp);
3445 -----------------------
3446 -- Check_Conformance --
3447 -----------------------
3449 procedure Check_Conformance
3450 (New_Id : Entity_Id;
3452 Ctype : Conformance_Type;
3454 Conforms : out Boolean;
3455 Err_Loc : Node_Id := Empty;
3456 Get_Inst : Boolean := False;
3457 Skip_Controlling_Formals : Boolean := False)
3459 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3460 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3461 -- If Errmsg is True, then processing continues to post an error message
3462 -- for conformance error on given node. Two messages are output. The
3463 -- first message points to the previous declaration with a general "no
3464 -- conformance" message. The second is the detailed reason, supplied as
3465 -- Msg. The parameter N provide information for a possible & insertion
3466 -- in the message, and also provides the location for posting the
3467 -- message in the absence of a specified Err_Loc location.
3469 -----------------------
3470 -- Conformance_Error --
3471 -----------------------
3473 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3480 if No (Err_Loc) then
3486 Error_Msg_Sloc := Sloc (Old_Id);
3489 when Type_Conformant =>
3490 Error_Msg_N -- CODEFIX
3491 ("not type conformant with declaration#!", Enode);
3493 when Mode_Conformant =>
3494 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3495 Error_Msg_N -- CODEFIX???
3496 ("not mode conformant with operation inherited#!",
3499 Error_Msg_N -- CODEFIX???
3500 ("not mode conformant with declaration#!", Enode);
3503 when Subtype_Conformant =>
3504 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3505 Error_Msg_N -- CODEFIX???
3506 ("not subtype conformant with operation inherited#!",
3509 Error_Msg_N -- CODEFIX???
3510 ("not subtype conformant with declaration#!", Enode);
3513 when Fully_Conformant =>
3514 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3515 Error_Msg_N -- CODEFIX
3516 ("not fully conformant with operation inherited#!",
3519 Error_Msg_N -- CODEFIX
3520 ("not fully conformant with declaration#!", Enode);
3524 Error_Msg_NE (Msg, Enode, N);
3526 end Conformance_Error;
3530 Old_Type : constant Entity_Id := Etype (Old_Id);
3531 New_Type : constant Entity_Id := Etype (New_Id);
3532 Old_Formal : Entity_Id;
3533 New_Formal : Entity_Id;
3534 Access_Types_Match : Boolean;
3535 Old_Formal_Base : Entity_Id;
3536 New_Formal_Base : Entity_Id;
3538 -- Start of processing for Check_Conformance
3543 -- We need a special case for operators, since they don't appear
3546 if Ctype = Type_Conformant then
3547 if Ekind (New_Id) = E_Operator
3548 and then Operator_Matches_Spec (New_Id, Old_Id)
3554 -- If both are functions/operators, check return types conform
3556 if Old_Type /= Standard_Void_Type
3557 and then New_Type /= Standard_Void_Type
3560 -- If we are checking interface conformance we omit controlling
3561 -- arguments and result, because we are only checking the conformance
3562 -- of the remaining parameters.
3564 if Has_Controlling_Result (Old_Id)
3565 and then Has_Controlling_Result (New_Id)
3566 and then Skip_Controlling_Formals
3570 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3571 Conformance_Error ("\return type does not match!", New_Id);
3575 -- Ada 2005 (AI-231): In case of anonymous access types check the
3576 -- null-exclusion and access-to-constant attributes match.
3578 if Ada_Version >= Ada_05
3579 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3581 (Can_Never_Be_Null (Old_Type)
3582 /= Can_Never_Be_Null (New_Type)
3583 or else Is_Access_Constant (Etype (Old_Type))
3584 /= Is_Access_Constant (Etype (New_Type)))
3586 Conformance_Error ("\return type does not match!", New_Id);
3590 -- If either is a function/operator and the other isn't, error
3592 elsif Old_Type /= Standard_Void_Type
3593 or else New_Type /= Standard_Void_Type
3595 Conformance_Error ("\functions can only match functions!", New_Id);
3599 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3600 -- If this is a renaming as body, refine error message to indicate that
3601 -- the conflict is with the original declaration. If the entity is not
3602 -- frozen, the conventions don't have to match, the one of the renamed
3603 -- entity is inherited.
3605 if Ctype >= Subtype_Conformant then
3606 if Convention (Old_Id) /= Convention (New_Id) then
3608 if not Is_Frozen (New_Id) then
3611 elsif Present (Err_Loc)
3612 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3613 and then Present (Corresponding_Spec (Err_Loc))
3615 Error_Msg_Name_1 := Chars (New_Id);
3617 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3619 Conformance_Error ("\prior declaration for% has convention %!");
3622 Conformance_Error ("\calling conventions do not match!");
3627 elsif Is_Formal_Subprogram (Old_Id)
3628 or else Is_Formal_Subprogram (New_Id)
3630 Conformance_Error ("\formal subprograms not allowed!");
3635 -- Deal with parameters
3637 -- Note: we use the entity information, rather than going directly
3638 -- to the specification in the tree. This is not only simpler, but
3639 -- absolutely necessary for some cases of conformance tests between
3640 -- operators, where the declaration tree simply does not exist!
3642 Old_Formal := First_Formal (Old_Id);
3643 New_Formal := First_Formal (New_Id);
3644 while Present (Old_Formal) and then Present (New_Formal) loop
3645 if Is_Controlling_Formal (Old_Formal)
3646 and then Is_Controlling_Formal (New_Formal)
3647 and then Skip_Controlling_Formals
3649 -- The controlling formals will have different types when
3650 -- comparing an interface operation with its match, but both
3651 -- or neither must be access parameters.
3653 if Is_Access_Type (Etype (Old_Formal))
3655 Is_Access_Type (Etype (New_Formal))
3657 goto Skip_Controlling_Formal;
3660 ("\access parameter does not match!", New_Formal);
3664 if Ctype = Fully_Conformant then
3666 -- Names must match. Error message is more accurate if we do
3667 -- this before checking that the types of the formals match.
3669 if Chars (Old_Formal) /= Chars (New_Formal) then
3670 Conformance_Error ("\name & does not match!", New_Formal);
3672 -- Set error posted flag on new formal as well to stop
3673 -- junk cascaded messages in some cases.
3675 Set_Error_Posted (New_Formal);
3680 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3681 -- case occurs whenever a subprogram is being renamed and one of its
3682 -- parameters imposes a null exclusion. For example:
3684 -- type T is null record;
3685 -- type Acc_T is access T;
3686 -- subtype Acc_T_Sub is Acc_T;
3688 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3689 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3692 Old_Formal_Base := Etype (Old_Formal);
3693 New_Formal_Base := Etype (New_Formal);
3696 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3697 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3700 Access_Types_Match := Ada_Version >= Ada_05
3702 -- Ensure that this rule is only applied when New_Id is a
3703 -- renaming of Old_Id.
3705 and then Nkind (Parent (Parent (New_Id))) =
3706 N_Subprogram_Renaming_Declaration
3707 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3708 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3709 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3711 -- Now handle the allowed access-type case
3713 and then Is_Access_Type (Old_Formal_Base)
3714 and then Is_Access_Type (New_Formal_Base)
3716 -- The type kinds must match. The only exception occurs with
3717 -- multiple generics of the form:
3720 -- type F is private; type A is private;
3721 -- type F_Ptr is access F; type A_Ptr is access A;
3722 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3723 -- package F_Pack is ... package A_Pack is
3724 -- package F_Inst is
3725 -- new F_Pack (A, A_Ptr, A_P);
3727 -- When checking for conformance between the parameters of A_P
3728 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3729 -- because the compiler has transformed A_Ptr into a subtype of
3730 -- F_Ptr. We catch this case in the code below.
3732 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3734 (Is_Generic_Type (Old_Formal_Base)
3735 and then Is_Generic_Type (New_Formal_Base)
3736 and then Is_Internal (New_Formal_Base)
3737 and then Etype (Etype (New_Formal_Base)) =
3739 and then Directly_Designated_Type (Old_Formal_Base) =
3740 Directly_Designated_Type (New_Formal_Base)
3741 and then ((Is_Itype (Old_Formal_Base)
3742 and then Can_Never_Be_Null (Old_Formal_Base))
3744 (Is_Itype (New_Formal_Base)
3745 and then Can_Never_Be_Null (New_Formal_Base)));
3747 -- Types must always match. In the visible part of an instance,
3748 -- usual overloading rules for dispatching operations apply, and
3749 -- we check base types (not the actual subtypes).
3751 if In_Instance_Visible_Part
3752 and then Is_Dispatching_Operation (New_Id)
3754 if not Conforming_Types
3755 (T1 => Base_Type (Etype (Old_Formal)),
3756 T2 => Base_Type (Etype (New_Formal)),
3758 Get_Inst => Get_Inst)
3759 and then not Access_Types_Match
3761 Conformance_Error ("\type of & does not match!", New_Formal);
3765 elsif not Conforming_Types
3766 (T1 => Old_Formal_Base,
3767 T2 => New_Formal_Base,
3769 Get_Inst => Get_Inst)
3770 and then not Access_Types_Match
3772 -- Don't give error message if old type is Any_Type. This test
3773 -- avoids some cascaded errors, e.g. in case of a bad spec.
3775 if Errmsg and then Old_Formal_Base = Any_Type then
3778 Conformance_Error ("\type of & does not match!", New_Formal);
3784 -- For mode conformance, mode must match
3786 if Ctype >= Mode_Conformant then
3787 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3788 Conformance_Error ("\mode of & does not match!", New_Formal);
3791 -- Part of mode conformance for access types is having the same
3792 -- constant modifier.
3794 elsif Access_Types_Match
3795 and then Is_Access_Constant (Old_Formal_Base) /=
3796 Is_Access_Constant (New_Formal_Base)
3799 ("\constant modifier does not match!", New_Formal);
3804 if Ctype >= Subtype_Conformant then
3806 -- Ada 2005 (AI-231): In case of anonymous access types check
3807 -- the null-exclusion and access-to-constant attributes must
3810 if Ada_Version >= Ada_05
3811 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3812 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3814 (Can_Never_Be_Null (Old_Formal) /=
3815 Can_Never_Be_Null (New_Formal)
3817 Is_Access_Constant (Etype (Old_Formal)) /=
3818 Is_Access_Constant (Etype (New_Formal)))
3820 -- It is allowed to omit the null-exclusion in case of stream
3821 -- attribute subprograms. We recognize stream subprograms
3822 -- through their TSS-generated suffix.
3825 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3827 if TSS_Name /= TSS_Stream_Read
3828 and then TSS_Name /= TSS_Stream_Write
3829 and then TSS_Name /= TSS_Stream_Input
3830 and then TSS_Name /= TSS_Stream_Output
3833 ("\type of & does not match!", New_Formal);
3840 -- Full conformance checks
3842 if Ctype = Fully_Conformant then
3844 -- We have checked already that names match
3846 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3848 -- Check default expressions for in parameters
3851 NewD : constant Boolean :=
3852 Present (Default_Value (New_Formal));
3853 OldD : constant Boolean :=
3854 Present (Default_Value (Old_Formal));
3856 if NewD or OldD then
3858 -- The old default value has been analyzed because the
3859 -- current full declaration will have frozen everything
3860 -- before. The new default value has not been analyzed,
3861 -- so analyze it now before we check for conformance.
3864 Push_Scope (New_Id);
3865 Preanalyze_Spec_Expression
3866 (Default_Value (New_Formal), Etype (New_Formal));
3870 if not (NewD and OldD)
3871 or else not Fully_Conformant_Expressions
3872 (Default_Value (Old_Formal),
3873 Default_Value (New_Formal))
3876 ("\default expression for & does not match!",
3885 -- A couple of special checks for Ada 83 mode. These checks are
3886 -- skipped if either entity is an operator in package Standard,
3887 -- or if either old or new instance is not from the source program.
3889 if Ada_Version = Ada_83
3890 and then Sloc (Old_Id) > Standard_Location
3891 and then Sloc (New_Id) > Standard_Location
3892 and then Comes_From_Source (Old_Id)
3893 and then Comes_From_Source (New_Id)
3896 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3897 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3900 -- Explicit IN must be present or absent in both cases. This
3901 -- test is required only in the full conformance case.
3903 if In_Present (Old_Param) /= In_Present (New_Param)
3904 and then Ctype = Fully_Conformant
3907 ("\(Ada 83) IN must appear in both declarations",
3912 -- Grouping (use of comma in param lists) must be the same
3913 -- This is where we catch a misconformance like:
3916 -- A : Integer; B : Integer
3918 -- which are represented identically in the tree except
3919 -- for the setting of the flags More_Ids and Prev_Ids.
3921 if More_Ids (Old_Param) /= More_Ids (New_Param)
3922 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3925 ("\grouping of & does not match!", New_Formal);
3931 -- This label is required when skipping controlling formals
3933 <<Skip_Controlling_Formal>>
3935 Next_Formal (Old_Formal);
3936 Next_Formal (New_Formal);
3939 if Present (Old_Formal) then
3940 Conformance_Error ("\too few parameters!");
3943 elsif Present (New_Formal) then
3944 Conformance_Error ("\too many parameters!", New_Formal);
3947 end Check_Conformance;
3949 -----------------------
3950 -- Check_Conventions --
3951 -----------------------
3953 procedure Check_Conventions (Typ : Entity_Id) is
3954 Ifaces_List : Elist_Id;
3956 procedure Check_Convention (Op : Entity_Id);
3957 -- Verify that the convention of inherited dispatching operation Op is
3958 -- consistent among all subprograms it overrides. In order to minimize
3959 -- the search, Search_From is utilized to designate a specific point in
3960 -- the list rather than iterating over the whole list once more.
3962 ----------------------
3963 -- Check_Convention --
3964 ----------------------
3966 procedure Check_Convention (Op : Entity_Id) is
3967 Iface_Elmt : Elmt_Id;
3968 Iface_Prim_Elmt : Elmt_Id;
3969 Iface_Prim : Entity_Id;
3972 Iface_Elmt := First_Elmt (Ifaces_List);
3973 while Present (Iface_Elmt) loop
3975 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3976 while Present (Iface_Prim_Elmt) loop
3977 Iface_Prim := Node (Iface_Prim_Elmt);
3979 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3980 and then Convention (Iface_Prim) /= Convention (Op)
3983 ("inconsistent conventions in primitive operations", Typ);
3985 Error_Msg_Name_1 := Chars (Op);
3986 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3987 Error_Msg_Sloc := Sloc (Op);
3989 if Comes_From_Source (Op) then
3990 if not Is_Overriding_Operation (Op) then
3991 Error_Msg_N ("\\primitive % defined #", Typ);
3993 Error_Msg_N ("\\overriding operation % with " &
3994 "convention % defined #", Typ);
3997 else pragma Assert (Present (Alias (Op)));
3998 Error_Msg_Sloc := Sloc (Alias (Op));
3999 Error_Msg_N ("\\inherited operation % with " &
4000 "convention % defined #", Typ);
4003 Error_Msg_Name_1 := Chars (Op);
4005 Get_Convention_Name (Convention (Iface_Prim));
4006 Error_Msg_Sloc := Sloc (Iface_Prim);
4007 Error_Msg_N ("\\overridden operation % with " &
4008 "convention % defined #", Typ);
4010 -- Avoid cascading errors
4015 Next_Elmt (Iface_Prim_Elmt);
4018 Next_Elmt (Iface_Elmt);
4020 end Check_Convention;
4024 Prim_Op : Entity_Id;
4025 Prim_Op_Elmt : Elmt_Id;
4027 -- Start of processing for Check_Conventions
4030 if not Has_Interfaces (Typ) then
4034 Collect_Interfaces (Typ, Ifaces_List);
4036 -- The algorithm checks every overriding dispatching operation against
4037 -- all the corresponding overridden dispatching operations, detecting
4038 -- differences in conventions.
4040 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4041 while Present (Prim_Op_Elmt) loop
4042 Prim_Op := Node (Prim_Op_Elmt);
4044 -- A small optimization: skip the predefined dispatching operations
4045 -- since they always have the same convention.
4047 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4048 Check_Convention (Prim_Op);
4051 Next_Elmt (Prim_Op_Elmt);
4053 end Check_Conventions;
4055 ------------------------------
4056 -- Check_Delayed_Subprogram --
4057 ------------------------------
4059 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4062 procedure Possible_Freeze (T : Entity_Id);
4063 -- T is the type of either a formal parameter or of the return type.
4064 -- If T is not yet frozen and needs a delayed freeze, then the
4065 -- subprogram itself must be delayed. If T is the limited view of an
4066 -- incomplete type the subprogram must be frozen as well, because
4067 -- T may depend on local types that have not been frozen yet.
4069 ---------------------
4070 -- Possible_Freeze --
4071 ---------------------
4073 procedure Possible_Freeze (T : Entity_Id) is
4075 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4076 Set_Has_Delayed_Freeze (Designator);
4078 elsif Is_Access_Type (T)
4079 and then Has_Delayed_Freeze (Designated_Type (T))
4080 and then not Is_Frozen (Designated_Type (T))
4082 Set_Has_Delayed_Freeze (Designator);
4084 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4085 Set_Has_Delayed_Freeze (Designator);
4088 end Possible_Freeze;
4090 -- Start of processing for Check_Delayed_Subprogram
4093 -- Never need to freeze abstract subprogram
4095 if Ekind (Designator) /= E_Subprogram_Type
4096 and then Is_Abstract_Subprogram (Designator)
4100 -- Need delayed freeze if return type itself needs a delayed
4101 -- freeze and is not yet frozen.
4103 Possible_Freeze (Etype (Designator));
4104 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4106 -- Need delayed freeze if any of the formal types themselves need
4107 -- a delayed freeze and are not yet frozen.
4109 F := First_Formal (Designator);
4110 while Present (F) loop
4111 Possible_Freeze (Etype (F));
4112 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4117 -- Mark functions that return by reference. Note that it cannot be
4118 -- done for delayed_freeze subprograms because the underlying
4119 -- returned type may not be known yet (for private types)
4121 if not Has_Delayed_Freeze (Designator)
4122 and then Expander_Active
4125 Typ : constant Entity_Id := Etype (Designator);
4126 Utyp : constant Entity_Id := Underlying_Type (Typ);
4129 if Is_Inherently_Limited_Type (Typ) then
4130 Set_Returns_By_Ref (Designator);
4132 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4133 Set_Returns_By_Ref (Designator);
4137 end Check_Delayed_Subprogram;
4139 ------------------------------------
4140 -- Check_Discriminant_Conformance --
4141 ------------------------------------
4143 procedure Check_Discriminant_Conformance
4148 Old_Discr : Entity_Id := First_Discriminant (Prev);
4149 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4150 New_Discr_Id : Entity_Id;
4151 New_Discr_Type : Entity_Id;
4153 procedure Conformance_Error (Msg : String; N : Node_Id);
4154 -- Post error message for conformance error on given node. Two messages
4155 -- are output. The first points to the previous declaration with a
4156 -- general "no conformance" message. The second is the detailed reason,
4157 -- supplied as Msg. The parameter N provide information for a possible
4158 -- & insertion in the message.
4160 -----------------------
4161 -- Conformance_Error --
4162 -----------------------
4164 procedure Conformance_Error (Msg : String; N : Node_Id) is
4166 Error_Msg_Sloc := Sloc (Prev_Loc);
4167 Error_Msg_N -- CODEFIX
4168 ("not fully conformant with declaration#!", N);
4169 Error_Msg_NE (Msg, N, N);
4170 end Conformance_Error;
4172 -- Start of processing for Check_Discriminant_Conformance
4175 while Present (Old_Discr) and then Present (New_Discr) loop
4177 New_Discr_Id := Defining_Identifier (New_Discr);
4179 -- The subtype mark of the discriminant on the full type has not
4180 -- been analyzed so we do it here. For an access discriminant a new
4183 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4185 Access_Definition (N, Discriminant_Type (New_Discr));
4188 Analyze (Discriminant_Type (New_Discr));
4189 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4191 -- Ada 2005: if the discriminant definition carries a null
4192 -- exclusion, create an itype to check properly for consistency
4193 -- with partial declaration.
4195 if Is_Access_Type (New_Discr_Type)
4196 and then Null_Exclusion_Present (New_Discr)
4199 Create_Null_Excluding_Itype
4200 (T => New_Discr_Type,
4201 Related_Nod => New_Discr,
4202 Scope_Id => Current_Scope);
4206 if not Conforming_Types
4207 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4209 Conformance_Error ("type of & does not match!", New_Discr_Id);
4212 -- Treat the new discriminant as an occurrence of the old one,
4213 -- for navigation purposes, and fill in some semantic
4214 -- information, for completeness.
4216 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4217 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4218 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4223 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4224 Conformance_Error ("name & does not match!", New_Discr_Id);
4228 -- Default expressions must match
4231 NewD : constant Boolean :=
4232 Present (Expression (New_Discr));
4233 OldD : constant Boolean :=
4234 Present (Expression (Parent (Old_Discr)));
4237 if NewD or OldD then
4239 -- The old default value has been analyzed and expanded,
4240 -- because the current full declaration will have frozen
4241 -- everything before. The new default values have not been
4242 -- expanded, so expand now to check conformance.
4245 Preanalyze_Spec_Expression
4246 (Expression (New_Discr), New_Discr_Type);
4249 if not (NewD and OldD)
4250 or else not Fully_Conformant_Expressions
4251 (Expression (Parent (Old_Discr)),
4252 Expression (New_Discr))
4256 ("default expression for & does not match!",
4263 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4265 if Ada_Version = Ada_83 then
4267 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4270 -- Grouping (use of comma in param lists) must be the same
4271 -- This is where we catch a misconformance like:
4274 -- A : Integer; B : Integer
4276 -- which are represented identically in the tree except
4277 -- for the setting of the flags More_Ids and Prev_Ids.
4279 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4280 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4283 ("grouping of & does not match!", New_Discr_Id);
4289 Next_Discriminant (Old_Discr);
4293 if Present (Old_Discr) then
4294 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4297 elsif Present (New_Discr) then
4299 ("too many discriminants!", Defining_Identifier (New_Discr));
4302 end Check_Discriminant_Conformance;
4304 ----------------------------
4305 -- Check_Fully_Conformant --
4306 ----------------------------
4308 procedure Check_Fully_Conformant
4309 (New_Id : Entity_Id;
4311 Err_Loc : Node_Id := Empty)
4314 pragma Warnings (Off, Result);
4317 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4318 end Check_Fully_Conformant;
4320 ---------------------------
4321 -- Check_Mode_Conformant --
4322 ---------------------------
4324 procedure Check_Mode_Conformant
4325 (New_Id : Entity_Id;
4327 Err_Loc : Node_Id := Empty;
4328 Get_Inst : Boolean := False)
4331 pragma Warnings (Off, Result);
4334 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4335 end Check_Mode_Conformant;
4337 --------------------------------
4338 -- Check_Overriding_Indicator --
4339 --------------------------------
4341 procedure Check_Overriding_Indicator
4343 Overridden_Subp : Entity_Id;
4344 Is_Primitive : Boolean)
4350 -- No overriding indicator for literals
4352 if Ekind (Subp) = E_Enumeration_Literal then
4355 elsif Ekind (Subp) = E_Entry then
4356 Decl := Parent (Subp);
4359 Decl := Unit_Declaration_Node (Subp);
4362 if Nkind_In (Decl, N_Subprogram_Body,
4363 N_Subprogram_Body_Stub,
4364 N_Subprogram_Declaration,
4365 N_Abstract_Subprogram_Declaration,
4366 N_Subprogram_Renaming_Declaration)
4368 Spec := Specification (Decl);
4370 elsif Nkind (Decl) = N_Entry_Declaration then
4377 -- The overriding operation is type conformant with the overridden one,
4378 -- but the names of the formals are not required to match. If the names
4379 -- appear permuted in the overriding operation, this is a possible
4380 -- source of confusion that is worth diagnosing. Controlling formals
4381 -- often carry names that reflect the type, and it is not worthwhile
4382 -- requiring that their names match.
4384 if Present (Overridden_Subp)
4385 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4392 Form1 := First_Formal (Subp);
4393 Form2 := First_Formal (Overridden_Subp);
4395 -- If the overriding operation is a synchronized operation, skip
4396 -- the first parameter of the overridden operation, which is
4397 -- implicit in the new one. If the operation is declared in the
4398 -- body it is not primitive and all formals must match.
4400 if Is_Concurrent_Type (Scope (Subp))
4401 and then Is_Tagged_Type (Scope (Subp))
4402 and then not Has_Completion (Scope (Subp))
4404 Form2 := Next_Formal (Form2);
4407 if Present (Form1) then
4408 Form1 := Next_Formal (Form1);
4409 Form2 := Next_Formal (Form2);
4412 while Present (Form1) loop
4413 if not Is_Controlling_Formal (Form1)
4414 and then Present (Next_Formal (Form2))
4415 and then Chars (Form1) = Chars (Next_Formal (Form2))
4417 Error_Msg_Node_2 := Alias (Overridden_Subp);
4418 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4419 Error_Msg_NE ("& does not match corresponding formal of&#",
4424 Next_Formal (Form1);
4425 Next_Formal (Form2);
4430 if Present (Overridden_Subp) then
4431 if Must_Not_Override (Spec) then
4432 Error_Msg_Sloc := Sloc (Overridden_Subp);
4434 if Ekind (Subp) = E_Entry then
4436 ("entry & overrides inherited operation #", Spec, Subp);
4439 ("subprogram & overrides inherited operation #", Spec, Subp);
4442 elsif Is_Subprogram (Subp) then
4443 Set_Is_Overriding_Operation (Subp);
4446 -- If primitive flag is set or this is a protected operation, then
4447 -- the operation is overriding at the point of its declaration, so
4448 -- warn if necessary. Otherwise it may have been declared before the
4449 -- operation it overrides and no check is required.
4452 and then not Must_Override (Spec)
4453 and then (Is_Primitive
4454 or else Ekind (Scope (Subp)) = E_Protected_Type)
4456 Style.Missing_Overriding (Decl, Subp);
4459 -- If Subp is an operator, it may override a predefined operation.
4460 -- In that case overridden_subp is empty because of our implicit
4461 -- representation for predefined operators. We have to check whether the
4462 -- signature of Subp matches that of a predefined operator. Note that
4463 -- first argument provides the name of the operator, and the second
4464 -- argument the signature that may match that of a standard operation.
4465 -- If the indicator is overriding, then the operator must match a
4466 -- predefined signature, because we know already that there is no
4467 -- explicit overridden operation.
4469 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4471 if Must_Not_Override (Spec) then
4473 -- If this is not a primitive operation or protected subprogram,
4474 -- then "not overriding" is illegal.
4477 and then Ekind (Scope (Subp)) /= E_Protected_Type
4480 ("overriding indicator only allowed "
4481 & "if subprogram is primitive", Subp);
4483 elsif Operator_Matches_Spec (Subp, Subp) then
4485 ("subprogram & overrides predefined operator ", Spec, Subp);
4488 elsif Must_Override (Spec) then
4489 if Is_Overriding_Operation (Subp) then
4490 Set_Is_Overriding_Operation (Subp);
4492 elsif not Operator_Matches_Spec (Subp, Subp) then
4493 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4496 elsif not Error_Posted (Subp)
4497 and then Style_Check
4498 and then Operator_Matches_Spec (Subp, Subp)
4500 not Is_Predefined_File_Name
4501 (Unit_File_Name (Get_Source_Unit (Subp)))
4503 Set_Is_Overriding_Operation (Subp);
4505 -- If style checks are enabled, indicate that the indicator is
4506 -- missing. However, at the point of declaration, the type of
4507 -- which this is a primitive operation may be private, in which
4508 -- case the indicator would be premature.
4510 if Has_Private_Declaration (Etype (Subp))
4511 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4515 Style.Missing_Overriding (Decl, Subp);
4519 elsif Must_Override (Spec) then
4520 if Ekind (Subp) = E_Entry then
4521 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4523 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4526 -- If the operation is marked "not overriding" and it's not primitive
4527 -- then an error is issued, unless this is an operation of a task or
4528 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4529 -- has been specified have already been checked above.
4531 elsif Must_Not_Override (Spec)
4532 and then not Is_Primitive
4533 and then Ekind (Subp) /= E_Entry
4534 and then Ekind (Scope (Subp)) /= E_Protected_Type
4537 ("overriding indicator only allowed if subprogram is primitive",
4541 end Check_Overriding_Indicator;
4547 -- Note: this procedure needs to know far too much about how the expander
4548 -- messes with exceptions. The use of the flag Exception_Junk and the
4549 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4550 -- works, but is not very clean. It would be better if the expansion
4551 -- routines would leave Original_Node working nicely, and we could use
4552 -- Original_Node here to ignore all the peculiar expander messing ???
4554 procedure Check_Returns
4558 Proc : Entity_Id := Empty)
4562 procedure Check_Statement_Sequence (L : List_Id);
4563 -- Internal recursive procedure to check a list of statements for proper
4564 -- termination by a return statement (or a transfer of control or a
4565 -- compound statement that is itself internally properly terminated).
4567 ------------------------------
4568 -- Check_Statement_Sequence --
4569 ------------------------------
4571 procedure Check_Statement_Sequence (L : List_Id) is
4576 Raise_Exception_Call : Boolean;
4577 -- Set True if statement sequence terminated by Raise_Exception call
4578 -- or a Reraise_Occurrence call.
4581 Raise_Exception_Call := False;
4583 -- Get last real statement
4585 Last_Stm := Last (L);
4587 -- Deal with digging out exception handler statement sequences that
4588 -- have been transformed by the local raise to goto optimization.
4589 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4590 -- optimization has occurred, we are looking at something like:
4593 -- original stmts in block
4597 -- goto L1; | omitted if No_Exception_Propagation
4602 -- goto L3; -- skip handler when exception not raised
4604 -- <<L1>> -- target label for local exception
4618 -- and what we have to do is to dig out the estmts1 and estmts2
4619 -- sequences (which were the original sequences of statements in
4620 -- the exception handlers) and check them.
4622 if Nkind (Last_Stm) = N_Label
4623 and then Exception_Junk (Last_Stm)
4629 exit when Nkind (Stm) /= N_Block_Statement;
4630 exit when not Exception_Junk (Stm);
4633 exit when Nkind (Stm) /= N_Label;
4634 exit when not Exception_Junk (Stm);
4635 Check_Statement_Sequence
4636 (Statements (Handled_Statement_Sequence (Next (Stm))));
4641 exit when Nkind (Stm) /= N_Goto_Statement;
4642 exit when not Exception_Junk (Stm);
4646 -- Don't count pragmas
4648 while Nkind (Last_Stm) = N_Pragma
4650 -- Don't count call to SS_Release (can happen after Raise_Exception)
4653 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4655 Nkind (Name (Last_Stm)) = N_Identifier
4657 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4659 -- Don't count exception junk
4662 (Nkind_In (Last_Stm, N_Goto_Statement,
4664 N_Object_Declaration)
4665 and then Exception_Junk (Last_Stm))
4666 or else Nkind (Last_Stm) in N_Push_xxx_Label
4667 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4672 -- Here we have the "real" last statement
4674 Kind := Nkind (Last_Stm);
4676 -- Transfer of control, OK. Note that in the No_Return procedure
4677 -- case, we already diagnosed any explicit return statements, so
4678 -- we can treat them as OK in this context.
4680 if Is_Transfer (Last_Stm) then
4683 -- Check cases of explicit non-indirect procedure calls
4685 elsif Kind = N_Procedure_Call_Statement
4686 and then Is_Entity_Name (Name (Last_Stm))
4688 -- Check call to Raise_Exception procedure which is treated
4689 -- specially, as is a call to Reraise_Occurrence.
4691 -- We suppress the warning in these cases since it is likely that
4692 -- the programmer really does not expect to deal with the case
4693 -- of Null_Occurrence, and thus would find a warning about a
4694 -- missing return curious, and raising Program_Error does not
4695 -- seem such a bad behavior if this does occur.
4697 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4698 -- behavior will be to raise Constraint_Error (see AI-329).
4700 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4702 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4704 Raise_Exception_Call := True;
4706 -- For Raise_Exception call, test first argument, if it is
4707 -- an attribute reference for a 'Identity call, then we know
4708 -- that the call cannot possibly return.
4711 Arg : constant Node_Id :=
4712 Original_Node (First_Actual (Last_Stm));
4714 if Nkind (Arg) = N_Attribute_Reference
4715 and then Attribute_Name (Arg) = Name_Identity
4722 -- If statement, need to look inside if there is an else and check
4723 -- each constituent statement sequence for proper termination.
4725 elsif Kind = N_If_Statement
4726 and then Present (Else_Statements (Last_Stm))
4728 Check_Statement_Sequence (Then_Statements (Last_Stm));
4729 Check_Statement_Sequence (Else_Statements (Last_Stm));
4731 if Present (Elsif_Parts (Last_Stm)) then
4733 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4736 while Present (Elsif_Part) loop
4737 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4745 -- Case statement, check each case for proper termination
4747 elsif Kind = N_Case_Statement then
4751 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4752 while Present (Case_Alt) loop
4753 Check_Statement_Sequence (Statements (Case_Alt));
4754 Next_Non_Pragma (Case_Alt);
4760 -- Block statement, check its handled sequence of statements
4762 elsif Kind = N_Block_Statement then
4768 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4777 -- Loop statement. If there is an iteration scheme, we can definitely
4778 -- fall out of the loop. Similarly if there is an exit statement, we
4779 -- can fall out. In either case we need a following return.
4781 elsif Kind = N_Loop_Statement then
4782 if Present (Iteration_Scheme (Last_Stm))
4783 or else Has_Exit (Entity (Identifier (Last_Stm)))
4787 -- A loop with no exit statement or iteration scheme is either
4788 -- an infinite loop, or it has some other exit (raise/return).
4789 -- In either case, no warning is required.
4795 -- Timed entry call, check entry call and delay alternatives
4797 -- Note: in expanded code, the timed entry call has been converted
4798 -- to a set of expanded statements on which the check will work
4799 -- correctly in any case.
4801 elsif Kind = N_Timed_Entry_Call then
4803 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4804 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4807 -- If statement sequence of entry call alternative is missing,
4808 -- then we can definitely fall through, and we post the error
4809 -- message on the entry call alternative itself.
4811 if No (Statements (ECA)) then
4814 -- If statement sequence of delay alternative is missing, then
4815 -- we can definitely fall through, and we post the error
4816 -- message on the delay alternative itself.
4818 -- Note: if both ECA and DCA are missing the return, then we
4819 -- post only one message, should be enough to fix the bugs.
4820 -- If not we will get a message next time on the DCA when the
4823 elsif No (Statements (DCA)) then
4826 -- Else check both statement sequences
4829 Check_Statement_Sequence (Statements (ECA));
4830 Check_Statement_Sequence (Statements (DCA));
4835 -- Conditional entry call, check entry call and else part
4837 -- Note: in expanded code, the conditional entry call has been
4838 -- converted to a set of expanded statements on which the check
4839 -- will work correctly in any case.
4841 elsif Kind = N_Conditional_Entry_Call then
4843 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4846 -- If statement sequence of entry call alternative is missing,
4847 -- then we can definitely fall through, and we post the error
4848 -- message on the entry call alternative itself.
4850 if No (Statements (ECA)) then
4853 -- Else check statement sequence and else part
4856 Check_Statement_Sequence (Statements (ECA));
4857 Check_Statement_Sequence (Else_Statements (Last_Stm));
4863 -- If we fall through, issue appropriate message
4866 if not Raise_Exception_Call then
4868 ("?RETURN statement missing following this statement!",
4871 ("\?Program_Error may be raised at run time!",
4875 -- Note: we set Err even though we have not issued a warning
4876 -- because we still have a case of a missing return. This is
4877 -- an extremely marginal case, probably will never be noticed
4878 -- but we might as well get it right.
4882 -- Otherwise we have the case of a procedure marked No_Return
4885 if not Raise_Exception_Call then
4887 ("?implied return after this statement " &
4888 "will raise Program_Error",
4891 ("\?procedure & is marked as No_Return!",
4896 RE : constant Node_Id :=
4897 Make_Raise_Program_Error (Sloc (Last_Stm),
4898 Reason => PE_Implicit_Return);
4900 Insert_After (Last_Stm, RE);
4904 end Check_Statement_Sequence;
4906 -- Start of processing for Check_Returns
4910 Check_Statement_Sequence (Statements (HSS));
4912 if Present (Exception_Handlers (HSS)) then
4913 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4914 while Present (Handler) loop
4915 Check_Statement_Sequence (Statements (Handler));
4916 Next_Non_Pragma (Handler);
4921 ----------------------------
4922 -- Check_Subprogram_Order --
4923 ----------------------------
4925 procedure Check_Subprogram_Order (N : Node_Id) is
4927 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4928 -- This is used to check if S1 > S2 in the sense required by this
4929 -- test, for example nameab < namec, but name2 < name10.
4931 -----------------------------
4932 -- Subprogram_Name_Greater --
4933 -----------------------------
4935 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4940 -- Remove trailing numeric parts
4943 while S1 (L1) in '0' .. '9' loop
4948 while S2 (L2) in '0' .. '9' loop
4952 -- If non-numeric parts non-equal, that's decisive
4954 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4957 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4960 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4961 -- that a missing suffix is treated as numeric zero in this test.
4965 while L1 < S1'Last loop
4967 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4971 while L2 < S2'Last loop
4973 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
4978 end Subprogram_Name_Greater;
4980 -- Start of processing for Check_Subprogram_Order
4983 -- Check body in alpha order if this is option
4986 and then Style_Check_Order_Subprograms
4987 and then Nkind (N) = N_Subprogram_Body
4988 and then Comes_From_Source (N)
4989 and then In_Extended_Main_Source_Unit (N)
4993 renames Scope_Stack.Table
4994 (Scope_Stack.Last).Last_Subprogram_Name;
4996 Body_Id : constant Entity_Id :=
4997 Defining_Entity (Specification (N));
5000 Get_Decoded_Name_String (Chars (Body_Id));
5003 if Subprogram_Name_Greater
5004 (LSN.all, Name_Buffer (1 .. Name_Len))
5006 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5012 LSN := new String'(Name_Buffer (1 .. Name_Len));
5015 end Check_Subprogram_Order;
5017 ------------------------------
5018 -- Check_Subtype_Conformant --
5019 ------------------------------
5021 procedure Check_Subtype_Conformant
5022 (New_Id : Entity_Id;
5024 Err_Loc : Node_Id := Empty;
5025 Skip_Controlling_Formals : Boolean := False)
5028 pragma Warnings (Off, Result);
5031 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5032 Skip_Controlling_Formals => Skip_Controlling_Formals);
5033 end Check_Subtype_Conformant;
5035 ---------------------------
5036 -- Check_Type_Conformant --
5037 ---------------------------
5039 procedure Check_Type_Conformant
5040 (New_Id : Entity_Id;
5042 Err_Loc : Node_Id := Empty)
5045 pragma Warnings (Off, Result);
5048 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5049 end Check_Type_Conformant;
5051 ----------------------
5052 -- Conforming_Types --
5053 ----------------------
5055 function Conforming_Types
5058 Ctype : Conformance_Type;
5059 Get_Inst : Boolean := False) return Boolean
5061 Type_1 : Entity_Id := T1;
5062 Type_2 : Entity_Id := T2;
5063 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5065 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5066 -- If neither T1 nor T2 are generic actual types, or if they are in
5067 -- different scopes (e.g. parent and child instances), then verify that
5068 -- the base types are equal. Otherwise T1 and T2 must be on the same
5069 -- subtype chain. The whole purpose of this procedure is to prevent
5070 -- spurious ambiguities in an instantiation that may arise if two
5071 -- distinct generic types are instantiated with the same actual.
5073 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5074 -- An access parameter can designate an incomplete type. If the
5075 -- incomplete type is the limited view of a type from a limited_
5076 -- with_clause, check whether the non-limited view is available. If
5077 -- it is a (non-limited) incomplete type, get the full view.
5079 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5080 -- Returns True if and only if either T1 denotes a limited view of T2
5081 -- or T2 denotes a limited view of T1. This can arise when the limited
5082 -- with view of a type is used in a subprogram declaration and the
5083 -- subprogram body is in the scope of a regular with clause for the
5084 -- same unit. In such a case, the two type entities can be considered
5085 -- identical for purposes of conformance checking.
5087 ----------------------
5088 -- Base_Types_Match --
5089 ----------------------
5091 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5096 elsif Base_Type (T1) = Base_Type (T2) then
5098 -- The following is too permissive. A more precise test should
5099 -- check that the generic actual is an ancestor subtype of the
5102 return not Is_Generic_Actual_Type (T1)
5103 or else not Is_Generic_Actual_Type (T2)
5104 or else Scope (T1) /= Scope (T2);
5109 end Base_Types_Match;
5111 --------------------------
5112 -- Find_Designated_Type --
5113 --------------------------
5115 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5119 Desig := Directly_Designated_Type (T);
5121 if Ekind (Desig) = E_Incomplete_Type then
5123 -- If regular incomplete type, get full view if available
5125 if Present (Full_View (Desig)) then
5126 Desig := Full_View (Desig);
5128 -- If limited view of a type, get non-limited view if available,
5129 -- and check again for a regular incomplete type.
5131 elsif Present (Non_Limited_View (Desig)) then
5132 Desig := Get_Full_View (Non_Limited_View (Desig));
5137 end Find_Designated_Type;
5139 -------------------------------
5140 -- Matches_Limited_With_View --
5141 -------------------------------
5143 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5145 -- In some cases a type imported through a limited_with clause, and
5146 -- its nonlimited view are both visible, for example in an anonymous
5147 -- access-to-class-wide type in a formal. Both entities designate the
5150 if From_With_Type (T1)
5151 and then T2 = Available_View (T1)
5155 elsif From_With_Type (T2)
5156 and then T1 = Available_View (T2)
5163 end Matches_Limited_With_View;
5165 -- Start of processing for Conforming_Types
5168 -- The context is an instance association for a formal
5169 -- access-to-subprogram type; the formal parameter types require
5170 -- mapping because they may denote other formal parameters of the
5174 Type_1 := Get_Instance_Of (T1);
5175 Type_2 := Get_Instance_Of (T2);
5178 -- If one of the types is a view of the other introduced by a limited
5179 -- with clause, treat these as conforming for all purposes.
5181 if Matches_Limited_With_View (T1, T2) then
5184 elsif Base_Types_Match (Type_1, Type_2) then
5185 return Ctype <= Mode_Conformant
5186 or else Subtypes_Statically_Match (Type_1, Type_2);
5188 elsif Is_Incomplete_Or_Private_Type (Type_1)
5189 and then Present (Full_View (Type_1))
5190 and then Base_Types_Match (Full_View (Type_1), Type_2)
5192 return Ctype <= Mode_Conformant
5193 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5195 elsif Ekind (Type_2) = E_Incomplete_Type
5196 and then Present (Full_View (Type_2))
5197 and then Base_Types_Match (Type_1, Full_View (Type_2))
5199 return Ctype <= Mode_Conformant
5200 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5202 elsif Is_Private_Type (Type_2)
5203 and then In_Instance
5204 and then Present (Full_View (Type_2))
5205 and then Base_Types_Match (Type_1, Full_View (Type_2))
5207 return Ctype <= Mode_Conformant
5208 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5211 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5212 -- treated recursively because they carry a signature.
5214 Are_Anonymous_Access_To_Subprogram_Types :=
5215 Ekind (Type_1) = Ekind (Type_2)
5217 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5219 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5221 -- Test anonymous access type case. For this case, static subtype
5222 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5223 -- the base types because we may have built internal subtype entities
5224 -- to handle null-excluding types (see Process_Formals).
5226 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5228 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5229 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5232 Desig_1 : Entity_Id;
5233 Desig_2 : Entity_Id;
5236 -- In Ada2005, access constant indicators must match for
5237 -- subtype conformance.
5239 if Ada_Version >= Ada_05
5240 and then Ctype >= Subtype_Conformant
5242 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5247 Desig_1 := Find_Designated_Type (Type_1);
5249 Desig_2 := Find_Designated_Type (Type_2);
5251 -- If the context is an instance association for a formal
5252 -- access-to-subprogram type; formal access parameter designated
5253 -- types require mapping because they may denote other formal
5254 -- parameters of the generic unit.
5257 Desig_1 := Get_Instance_Of (Desig_1);
5258 Desig_2 := Get_Instance_Of (Desig_2);
5261 -- It is possible for a Class_Wide_Type to be introduced for an
5262 -- incomplete type, in which case there is a separate class_ wide
5263 -- type for the full view. The types conform if their Etypes
5264 -- conform, i.e. one may be the full view of the other. This can
5265 -- only happen in the context of an access parameter, other uses
5266 -- of an incomplete Class_Wide_Type are illegal.
5268 if Is_Class_Wide_Type (Desig_1)
5269 and then Is_Class_Wide_Type (Desig_2)
5273 (Etype (Base_Type (Desig_1)),
5274 Etype (Base_Type (Desig_2)), Ctype);
5276 elsif Are_Anonymous_Access_To_Subprogram_Types then
5277 if Ada_Version < Ada_05 then
5278 return Ctype = Type_Conformant
5280 Subtypes_Statically_Match (Desig_1, Desig_2);
5282 -- We must check the conformance of the signatures themselves
5286 Conformant : Boolean;
5289 (Desig_1, Desig_2, Ctype, False, Conformant);
5295 return Base_Type (Desig_1) = Base_Type (Desig_2)
5296 and then (Ctype = Type_Conformant
5298 Subtypes_Statically_Match (Desig_1, Desig_2));
5302 -- Otherwise definitely no match
5305 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5306 and then Is_Access_Type (Type_2))
5307 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5308 and then Is_Access_Type (Type_1)))
5311 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5313 May_Hide_Profile := True;
5318 end Conforming_Types;
5320 --------------------------
5321 -- Create_Extra_Formals --
5322 --------------------------
5324 procedure Create_Extra_Formals (E : Entity_Id) is
5326 First_Extra : Entity_Id := Empty;
5327 Last_Extra : Entity_Id;
5328 Formal_Type : Entity_Id;
5329 P_Formal : Entity_Id := Empty;
5331 function Add_Extra_Formal
5332 (Assoc_Entity : Entity_Id;
5335 Suffix : String) return Entity_Id;
5336 -- Add an extra formal to the current list of formals and extra formals.
5337 -- The extra formal is added to the end of the list of extra formals,
5338 -- and also returned as the result. These formals are always of mode IN.
5339 -- The new formal has the type Typ, is declared in Scope, and its name
5340 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5342 ----------------------
5343 -- Add_Extra_Formal --
5344 ----------------------
5346 function Add_Extra_Formal
5347 (Assoc_Entity : Entity_Id;
5350 Suffix : String) return Entity_Id
5352 EF : constant Entity_Id :=
5353 Make_Defining_Identifier (Sloc (Assoc_Entity),
5354 Chars => New_External_Name (Chars (Assoc_Entity),
5358 -- A little optimization. Never generate an extra formal for the
5359 -- _init operand of an initialization procedure, since it could
5362 if Chars (Formal) = Name_uInit then
5366 Set_Ekind (EF, E_In_Parameter);
5367 Set_Actual_Subtype (EF, Typ);
5368 Set_Etype (EF, Typ);
5369 Set_Scope (EF, Scope);
5370 Set_Mechanism (EF, Default_Mechanism);
5371 Set_Formal_Validity (EF);
5373 if No (First_Extra) then
5375 Set_Extra_Formals (Scope, First_Extra);
5378 if Present (Last_Extra) then
5379 Set_Extra_Formal (Last_Extra, EF);
5385 end Add_Extra_Formal;
5387 -- Start of processing for Create_Extra_Formals
5390 -- We never generate extra formals if expansion is not active
5391 -- because we don't need them unless we are generating code.
5393 if not Expander_Active then
5397 -- If this is a derived subprogram then the subtypes of the parent
5398 -- subprogram's formal parameters will be used to determine the need
5399 -- for extra formals.
5401 if Is_Overloadable (E) and then Present (Alias (E)) then
5402 P_Formal := First_Formal (Alias (E));
5405 Last_Extra := Empty;
5406 Formal := First_Formal (E);
5407 while Present (Formal) loop
5408 Last_Extra := Formal;
5409 Next_Formal (Formal);
5412 -- If Extra_formals were already created, don't do it again. This
5413 -- situation may arise for subprogram types created as part of
5414 -- dispatching calls (see Expand_Dispatching_Call)
5416 if Present (Last_Extra) and then
5417 Present (Extra_Formal (Last_Extra))
5422 -- If the subprogram is a predefined dispatching subprogram then don't
5423 -- generate any extra constrained or accessibility level formals. In
5424 -- general we suppress these for internal subprograms (by not calling
5425 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5426 -- generated stream attributes do get passed through because extra
5427 -- build-in-place formals are needed in some cases (limited 'Input).
5429 if Is_Predefined_Dispatching_Operation (E) then
5430 goto Test_For_BIP_Extras;
5433 Formal := First_Formal (E);
5434 while Present (Formal) loop
5436 -- Create extra formal for supporting the attribute 'Constrained.
5437 -- The case of a private type view without discriminants also
5438 -- requires the extra formal if the underlying type has defaulted
5441 if Ekind (Formal) /= E_In_Parameter then
5442 if Present (P_Formal) then
5443 Formal_Type := Etype (P_Formal);
5445 Formal_Type := Etype (Formal);
5448 -- Do not produce extra formals for Unchecked_Union parameters.
5449 -- Jump directly to the end of the loop.
5451 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5452 goto Skip_Extra_Formal_Generation;
5455 if not Has_Discriminants (Formal_Type)
5456 and then Ekind (Formal_Type) in Private_Kind
5457 and then Present (Underlying_Type (Formal_Type))
5459 Formal_Type := Underlying_Type (Formal_Type);
5462 if Has_Discriminants (Formal_Type)
5463 and then not Is_Constrained (Formal_Type)
5464 and then not Is_Indefinite_Subtype (Formal_Type)
5466 Set_Extra_Constrained
5467 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
5471 -- Create extra formal for supporting accessibility checking. This
5472 -- is done for both anonymous access formals and formals of named
5473 -- access types that are marked as controlling formals. The latter
5474 -- case can occur when Expand_Dispatching_Call creates a subprogram
5475 -- type and substitutes the types of access-to-class-wide actuals
5476 -- for the anonymous access-to-specific-type of controlling formals.
5477 -- Base_Type is applied because in cases where there is a null
5478 -- exclusion the formal may have an access subtype.
5480 -- This is suppressed if we specifically suppress accessibility
5481 -- checks at the package level for either the subprogram, or the
5482 -- package in which it resides. However, we do not suppress it
5483 -- simply if the scope has accessibility checks suppressed, since
5484 -- this could cause trouble when clients are compiled with a
5485 -- different suppression setting. The explicit checks at the
5486 -- package level are safe from this point of view.
5488 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5489 or else (Is_Controlling_Formal (Formal)
5490 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5492 (Explicit_Suppress (E, Accessibility_Check)
5494 Explicit_Suppress (Scope (E), Accessibility_Check))
5497 or else Present (Extra_Accessibility (P_Formal)))
5499 -- Temporary kludge: for now we avoid creating the extra formal
5500 -- for access parameters of protected operations because of
5501 -- problem with the case of internal protected calls. ???
5503 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
5504 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
5506 Set_Extra_Accessibility
5507 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
5511 -- This label is required when skipping extra formal generation for
5512 -- Unchecked_Union parameters.
5514 <<Skip_Extra_Formal_Generation>>
5516 if Present (P_Formal) then
5517 Next_Formal (P_Formal);
5520 Next_Formal (Formal);
5523 <<Test_For_BIP_Extras>>
5525 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5526 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5528 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5530 Result_Subt : constant Entity_Id := Etype (E);
5532 Discard : Entity_Id;
5533 pragma Warnings (Off, Discard);
5536 -- In the case of functions with unconstrained result subtypes,
5537 -- add a 3-state formal indicating whether the return object is
5538 -- allocated by the caller (0), or should be allocated by the
5539 -- callee on the secondary stack (1) or in the global heap (2).
5540 -- For the moment we just use Natural for the type of this formal.
5541 -- Note that this formal isn't usually needed in the case where
5542 -- the result subtype is constrained, but it is needed when the
5543 -- function has a tagged result, because generally such functions
5544 -- can be called in a dispatching context and such calls must be
5545 -- handled like calls to a class-wide function.
5547 if not Is_Constrained (Underlying_Type (Result_Subt))
5548 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5552 (E, Standard_Natural,
5553 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5556 -- In the case of functions whose result type has controlled
5557 -- parts, we have an extra formal of type
5558 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5559 -- is, we are passing a pointer to a finalization list (which is
5560 -- itself a pointer). This extra formal is then passed along to
5561 -- Move_Final_List in case of successful completion of a return
5562 -- statement. We cannot pass an 'in out' parameter, because we
5563 -- need to update the finalization list during an abort-deferred
5564 -- region, rather than using copy-back after the function
5565 -- returns. This is true even if we are able to get away with
5566 -- having 'in out' parameters, which are normally illegal for
5567 -- functions. This formal is also needed when the function has
5570 if Needs_BIP_Final_List (E) then
5573 (E, RTE (RE_Finalizable_Ptr_Ptr),
5574 E, BIP_Formal_Suffix (BIP_Final_List));
5577 -- If the result type contains tasks, we have two extra formals:
5578 -- the master of the tasks to be created, and the caller's
5579 -- activation chain.
5581 if Has_Task (Result_Subt) then
5584 (E, RTE (RE_Master_Id),
5585 E, BIP_Formal_Suffix (BIP_Master));
5588 (E, RTE (RE_Activation_Chain_Access),
5589 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5592 -- All build-in-place functions get an extra formal that will be
5593 -- passed the address of the return object within the caller.
5596 Formal_Type : constant Entity_Id :=
5598 (E_Anonymous_Access_Type, E,
5599 Scope_Id => Scope (E));
5601 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5602 Set_Etype (Formal_Type, Formal_Type);
5603 Set_Depends_On_Private
5604 (Formal_Type, Has_Private_Component (Formal_Type));
5605 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5606 Set_Is_Access_Constant (Formal_Type, False);
5608 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5609 -- the designated type comes from the limited view (for
5610 -- back-end purposes).
5612 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5614 Layout_Type (Formal_Type);
5618 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5622 end Create_Extra_Formals;
5624 -----------------------------
5625 -- Enter_Overloaded_Entity --
5626 -----------------------------
5628 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5629 E : Entity_Id := Current_Entity_In_Scope (S);
5630 C_E : Entity_Id := Current_Entity (S);
5634 Set_Has_Homonym (E);
5635 Set_Has_Homonym (S);
5638 Set_Is_Immediately_Visible (S);
5639 Set_Scope (S, Current_Scope);
5641 -- Chain new entity if front of homonym in current scope, so that
5642 -- homonyms are contiguous.
5647 while Homonym (C_E) /= E loop
5648 C_E := Homonym (C_E);
5651 Set_Homonym (C_E, S);
5655 Set_Current_Entity (S);
5660 Append_Entity (S, Current_Scope);
5661 Set_Public_Status (S);
5663 if Debug_Flag_E then
5664 Write_Str ("New overloaded entity chain: ");
5665 Write_Name (Chars (S));
5668 while Present (E) loop
5669 Write_Str (" "); Write_Int (Int (E));
5676 -- Generate warning for hiding
5679 and then Comes_From_Source (S)
5680 and then In_Extended_Main_Source_Unit (S)
5687 -- Warn unless genuine overloading
5689 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5690 and then (Is_Immediately_Visible (E)
5692 Is_Potentially_Use_Visible (S))
5694 Error_Msg_Sloc := Sloc (E);
5695 Error_Msg_N ("declaration of & hides one#?", S);
5699 end Enter_Overloaded_Entity;
5701 -----------------------------
5702 -- Find_Corresponding_Spec --
5703 -----------------------------
5705 function Find_Corresponding_Spec
5707 Post_Error : Boolean := True) return Entity_Id
5709 Spec : constant Node_Id := Specification (N);
5710 Designator : constant Entity_Id := Defining_Entity (Spec);
5715 E := Current_Entity (Designator);
5716 while Present (E) loop
5718 -- We are looking for a matching spec. It must have the same scope,
5719 -- and the same name, and either be type conformant, or be the case
5720 -- of a library procedure spec and its body (which belong to one
5721 -- another regardless of whether they are type conformant or not).
5723 if Scope (E) = Current_Scope then
5724 if Current_Scope = Standard_Standard
5725 or else (Ekind (E) = Ekind (Designator)
5726 and then Type_Conformant (E, Designator))
5728 -- Within an instantiation, we know that spec and body are
5729 -- subtype conformant, because they were subtype conformant
5730 -- in the generic. We choose the subtype-conformant entity
5731 -- here as well, to resolve spurious ambiguities in the
5732 -- instance that were not present in the generic (i.e. when
5733 -- two different types are given the same actual). If we are
5734 -- looking for a spec to match a body, full conformance is
5738 Set_Convention (Designator, Convention (E));
5740 if Nkind (N) = N_Subprogram_Body
5741 and then Present (Homonym (E))
5742 and then not Fully_Conformant (E, Designator)
5746 elsif not Subtype_Conformant (E, Designator) then
5751 if not Has_Completion (E) then
5752 if Nkind (N) /= N_Subprogram_Body_Stub then
5753 Set_Corresponding_Spec (N, E);
5756 Set_Has_Completion (E);
5759 elsif Nkind (Parent (N)) = N_Subunit then
5761 -- If this is the proper body of a subunit, the completion
5762 -- flag is set when analyzing the stub.
5766 -- If E is an internal function with a controlling result
5767 -- that was created for an operation inherited by a null
5768 -- extension, it may be overridden by a body without a previous
5769 -- spec (one more reason why these should be shunned). In that
5770 -- case remove the generated body, because the current one is
5771 -- the explicit overriding.
5773 elsif Ekind (E) = E_Function
5774 and then Ada_Version >= Ada_05
5775 and then not Comes_From_Source (E)
5776 and then Has_Controlling_Result (E)
5777 and then Is_Null_Extension (Etype (E))
5778 and then Comes_From_Source (Spec)
5780 Set_Has_Completion (E, False);
5782 if Expander_Active then
5784 (Unit_Declaration_Node
5785 (Corresponding_Body (Unit_Declaration_Node (E))));
5788 -- If expansion is disabled, the wrapper function has not
5789 -- been generated, and this is the standard case of a late
5790 -- body overriding an inherited operation.
5796 -- If the body already exists, then this is an error unless
5797 -- the previous declaration is the implicit declaration of a
5798 -- derived subprogram, or this is a spurious overloading in an
5801 elsif No (Alias (E))
5802 and then not Is_Intrinsic_Subprogram (E)
5803 and then not In_Instance
5806 Error_Msg_Sloc := Sloc (E);
5808 if Is_Imported (E) then
5810 ("body not allowed for imported subprogram & declared#",
5813 Error_Msg_NE ("duplicate body for & declared#", N, E);
5817 -- Child units cannot be overloaded, so a conformance mismatch
5818 -- between body and a previous spec is an error.
5820 elsif Is_Child_Unit (E)
5822 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5824 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5829 ("body of child unit does not match previous declaration", N);
5837 -- On exit, we know that no previous declaration of subprogram exists
5840 end Find_Corresponding_Spec;
5842 ----------------------
5843 -- Fully_Conformant --
5844 ----------------------
5846 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5849 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5851 end Fully_Conformant;
5853 ----------------------------------
5854 -- Fully_Conformant_Expressions --
5855 ----------------------------------
5857 function Fully_Conformant_Expressions
5858 (Given_E1 : Node_Id;
5859 Given_E2 : Node_Id) return Boolean
5861 E1 : constant Node_Id := Original_Node (Given_E1);
5862 E2 : constant Node_Id := Original_Node (Given_E2);
5863 -- We always test conformance on original nodes, since it is possible
5864 -- for analysis and/or expansion to make things look as though they
5865 -- conform when they do not, e.g. by converting 1+2 into 3.
5867 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5868 renames Fully_Conformant_Expressions;
5870 function FCL (L1, L2 : List_Id) return Boolean;
5871 -- Compare elements of two lists for conformance. Elements have to
5872 -- be conformant, and actuals inserted as default parameters do not
5873 -- match explicit actuals with the same value.
5875 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5876 -- Compare an operator node with a function call
5882 function FCL (L1, L2 : List_Id) return Boolean is
5886 if L1 = No_List then
5892 if L2 = No_List then
5898 -- Compare two lists, skipping rewrite insertions (we want to
5899 -- compare the original trees, not the expanded versions!)
5902 if Is_Rewrite_Insertion (N1) then
5904 elsif Is_Rewrite_Insertion (N2) then
5910 elsif not FCE (N1, N2) then
5923 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5924 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5929 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5934 Act := First (Actuals);
5936 if Nkind (Op_Node) in N_Binary_Op then
5937 if not FCE (Left_Opnd (Op_Node), Act) then
5944 return Present (Act)
5945 and then FCE (Right_Opnd (Op_Node), Act)
5946 and then No (Next (Act));
5950 -- Start of processing for Fully_Conformant_Expressions
5953 -- Non-conformant if paren count does not match. Note: if some idiot
5954 -- complains that we don't do this right for more than 3 levels of
5955 -- parentheses, they will be treated with the respect they deserve!
5957 if Paren_Count (E1) /= Paren_Count (E2) then
5960 -- If same entities are referenced, then they are conformant even if
5961 -- they have different forms (RM 8.3.1(19-20)).
5963 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5964 if Present (Entity (E1)) then
5965 return Entity (E1) = Entity (E2)
5966 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5967 and then Ekind (Entity (E1)) = E_Discriminant
5968 and then Ekind (Entity (E2)) = E_In_Parameter);
5970 elsif Nkind (E1) = N_Expanded_Name
5971 and then Nkind (E2) = N_Expanded_Name
5972 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5973 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5975 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5978 -- Identifiers in component associations don't always have
5979 -- entities, but their names must conform.
5981 return Nkind (E1) = N_Identifier
5982 and then Nkind (E2) = N_Identifier
5983 and then Chars (E1) = Chars (E2);
5986 elsif Nkind (E1) = N_Character_Literal
5987 and then Nkind (E2) = N_Expanded_Name
5989 return Nkind (Selector_Name (E2)) = N_Character_Literal
5990 and then Chars (E1) = Chars (Selector_Name (E2));
5992 elsif Nkind (E2) = N_Character_Literal
5993 and then Nkind (E1) = N_Expanded_Name
5995 return Nkind (Selector_Name (E1)) = N_Character_Literal
5996 and then Chars (E2) = Chars (Selector_Name (E1));
5998 elsif Nkind (E1) in N_Op
5999 and then Nkind (E2) = N_Function_Call
6001 return FCO (E1, E2);
6003 elsif Nkind (E2) in N_Op
6004 and then Nkind (E1) = N_Function_Call
6006 return FCO (E2, E1);
6008 -- Otherwise we must have the same syntactic entity
6010 elsif Nkind (E1) /= Nkind (E2) then
6013 -- At this point, we specialize by node type
6020 FCL (Expressions (E1), Expressions (E2))
6021 and then FCL (Component_Associations (E1),
6022 Component_Associations (E2));
6025 if Nkind (Expression (E1)) = N_Qualified_Expression
6027 Nkind (Expression (E2)) = N_Qualified_Expression
6029 return FCE (Expression (E1), Expression (E2));
6031 -- Check that the subtype marks and any constraints
6036 Indic1 : constant Node_Id := Expression (E1);
6037 Indic2 : constant Node_Id := Expression (E2);
6042 if Nkind (Indic1) /= N_Subtype_Indication then
6044 Nkind (Indic2) /= N_Subtype_Indication
6045 and then Entity (Indic1) = Entity (Indic2);
6047 elsif Nkind (Indic2) /= N_Subtype_Indication then
6049 Nkind (Indic1) /= N_Subtype_Indication
6050 and then Entity (Indic1) = Entity (Indic2);
6053 if Entity (Subtype_Mark (Indic1)) /=
6054 Entity (Subtype_Mark (Indic2))
6059 Elt1 := First (Constraints (Constraint (Indic1)));
6060 Elt2 := First (Constraints (Constraint (Indic2)));
6061 while Present (Elt1) and then Present (Elt2) loop
6062 if not FCE (Elt1, Elt2) then
6075 when N_Attribute_Reference =>
6077 Attribute_Name (E1) = Attribute_Name (E2)
6078 and then FCL (Expressions (E1), Expressions (E2));
6082 Entity (E1) = Entity (E2)
6083 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6084 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6086 when N_And_Then | N_Or_Else | N_Membership_Test =>
6088 FCE (Left_Opnd (E1), Left_Opnd (E2))
6090 FCE (Right_Opnd (E1), Right_Opnd (E2));
6092 when N_Character_Literal =>
6094 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6096 when N_Component_Association =>
6098 FCL (Choices (E1), Choices (E2))
6099 and then FCE (Expression (E1), Expression (E2));
6101 when N_Conditional_Expression =>
6103 FCL (Expressions (E1), Expressions (E2));
6105 when N_Explicit_Dereference =>
6107 FCE (Prefix (E1), Prefix (E2));
6109 when N_Extension_Aggregate =>
6111 FCL (Expressions (E1), Expressions (E2))
6112 and then Null_Record_Present (E1) =
6113 Null_Record_Present (E2)
6114 and then FCL (Component_Associations (E1),
6115 Component_Associations (E2));
6117 when N_Function_Call =>
6119 FCE (Name (E1), Name (E2))
6120 and then FCL (Parameter_Associations (E1),
6121 Parameter_Associations (E2));
6123 when N_Indexed_Component =>
6125 FCE (Prefix (E1), Prefix (E2))
6126 and then FCL (Expressions (E1), Expressions (E2));
6128 when N_Integer_Literal =>
6129 return (Intval (E1) = Intval (E2));
6134 when N_Operator_Symbol =>
6136 Chars (E1) = Chars (E2);
6138 when N_Others_Choice =>
6141 when N_Parameter_Association =>
6143 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6144 and then FCE (Explicit_Actual_Parameter (E1),
6145 Explicit_Actual_Parameter (E2));
6147 when N_Qualified_Expression =>
6149 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6150 and then FCE (Expression (E1), Expression (E2));
6154 FCE (Low_Bound (E1), Low_Bound (E2))
6155 and then FCE (High_Bound (E1), High_Bound (E2));
6157 when N_Real_Literal =>
6158 return (Realval (E1) = Realval (E2));
6160 when N_Selected_Component =>
6162 FCE (Prefix (E1), Prefix (E2))
6163 and then FCE (Selector_Name (E1), Selector_Name (E2));
6167 FCE (Prefix (E1), Prefix (E2))
6168 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
6170 when N_String_Literal =>
6172 S1 : constant String_Id := Strval (E1);
6173 S2 : constant String_Id := Strval (E2);
6174 L1 : constant Nat := String_Length (S1);
6175 L2 : constant Nat := String_Length (S2);
6182 for J in 1 .. L1 loop
6183 if Get_String_Char (S1, J) /=
6184 Get_String_Char (S2, J)
6194 when N_Type_Conversion =>
6196 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6197 and then FCE (Expression (E1), Expression (E2));
6201 Entity (E1) = Entity (E2)
6202 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6204 when N_Unchecked_Type_Conversion =>
6206 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6207 and then FCE (Expression (E1), Expression (E2));
6209 -- All other node types cannot appear in this context. Strictly
6210 -- we should raise a fatal internal error. Instead we just ignore
6211 -- the nodes. This means that if anyone makes a mistake in the
6212 -- expander and mucks an expression tree irretrievably, the
6213 -- result will be a failure to detect a (probably very obscure)
6214 -- case of non-conformance, which is better than bombing on some
6215 -- case where two expressions do in fact conform.
6222 end Fully_Conformant_Expressions;
6224 ----------------------------------------
6225 -- Fully_Conformant_Discrete_Subtypes --
6226 ----------------------------------------
6228 function Fully_Conformant_Discrete_Subtypes
6229 (Given_S1 : Node_Id;
6230 Given_S2 : Node_Id) return Boolean
6232 S1 : constant Node_Id := Original_Node (Given_S1);
6233 S2 : constant Node_Id := Original_Node (Given_S2);
6235 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6236 -- Special-case for a bound given by a discriminant, which in the body
6237 -- is replaced with the discriminal of the enclosing type.
6239 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6240 -- Check both bounds
6242 -----------------------
6243 -- Conforming_Bounds --
6244 -----------------------
6246 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6248 if Is_Entity_Name (B1)
6249 and then Is_Entity_Name (B2)
6250 and then Ekind (Entity (B1)) = E_Discriminant
6252 return Chars (B1) = Chars (B2);
6255 return Fully_Conformant_Expressions (B1, B2);
6257 end Conforming_Bounds;
6259 -----------------------
6260 -- Conforming_Ranges --
6261 -----------------------
6263 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6266 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6268 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6269 end Conforming_Ranges;
6271 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6274 if Nkind (S1) /= Nkind (S2) then
6277 elsif Is_Entity_Name (S1) then
6278 return Entity (S1) = Entity (S2);
6280 elsif Nkind (S1) = N_Range then
6281 return Conforming_Ranges (S1, S2);
6283 elsif Nkind (S1) = N_Subtype_Indication then
6285 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6288 (Range_Expression (Constraint (S1)),
6289 Range_Expression (Constraint (S2)));
6293 end Fully_Conformant_Discrete_Subtypes;
6295 --------------------
6296 -- Install_Entity --
6297 --------------------
6299 procedure Install_Entity (E : Entity_Id) is
6300 Prev : constant Entity_Id := Current_Entity (E);
6302 Set_Is_Immediately_Visible (E);
6303 Set_Current_Entity (E);
6304 Set_Homonym (E, Prev);
6307 ---------------------
6308 -- Install_Formals --
6309 ---------------------
6311 procedure Install_Formals (Id : Entity_Id) is
6314 F := First_Formal (Id);
6315 while Present (F) loop
6319 end Install_Formals;
6321 -----------------------------
6322 -- Is_Interface_Conformant --
6323 -----------------------------
6325 function Is_Interface_Conformant
6326 (Tagged_Type : Entity_Id;
6327 Iface_Prim : Entity_Id;
6328 Prim : Entity_Id) return Boolean
6330 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6331 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6334 pragma Assert (Is_Subprogram (Iface_Prim)
6335 and then Is_Subprogram (Prim)
6336 and then Is_Dispatching_Operation (Iface_Prim)
6337 and then Is_Dispatching_Operation (Prim));
6339 pragma Assert (Is_Interface (Iface)
6340 or else (Present (Alias (Iface_Prim))
6343 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6345 if Prim = Iface_Prim
6346 or else not Is_Subprogram (Prim)
6347 or else Ekind (Prim) /= Ekind (Iface_Prim)
6348 or else not Is_Dispatching_Operation (Prim)
6349 or else Scope (Prim) /= Scope (Tagged_Type)
6351 or else Base_Type (Typ) /= Tagged_Type
6352 or else not Primitive_Names_Match (Iface_Prim, Prim)
6356 -- Case of a procedure, or a function that does not have a controlling
6357 -- result (I or access I).
6359 elsif Ekind (Iface_Prim) = E_Procedure
6360 or else Etype (Prim) = Etype (Iface_Prim)
6361 or else not Has_Controlling_Result (Prim)
6363 return Type_Conformant (Prim, Iface_Prim,
6364 Skip_Controlling_Formals => True);
6366 -- Case of a function returning an interface, or an access to one.
6367 -- Check that the return types correspond.
6369 elsif Implements_Interface (Typ, Iface) then
6370 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6372 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6377 Type_Conformant (Prim, Iface_Prim,
6378 Skip_Controlling_Formals => True);
6384 end Is_Interface_Conformant;
6386 ---------------------------------
6387 -- Is_Non_Overriding_Operation --
6388 ---------------------------------
6390 function Is_Non_Overriding_Operation
6391 (Prev_E : Entity_Id;
6392 New_E : Entity_Id) return Boolean
6396 G_Typ : Entity_Id := Empty;
6398 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6399 -- If F_Type is a derived type associated with a generic actual subtype,
6400 -- then return its Generic_Parent_Type attribute, else return Empty.
6402 function Types_Correspond
6403 (P_Type : Entity_Id;
6404 N_Type : Entity_Id) return Boolean;
6405 -- Returns true if and only if the types (or designated types in the
6406 -- case of anonymous access types) are the same or N_Type is derived
6407 -- directly or indirectly from P_Type.
6409 -----------------------------
6410 -- Get_Generic_Parent_Type --
6411 -----------------------------
6413 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6418 if Is_Derived_Type (F_Typ)
6419 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6421 -- The tree must be traversed to determine the parent subtype in
6422 -- the generic unit, which unfortunately isn't always available
6423 -- via semantic attributes. ??? (Note: The use of Original_Node
6424 -- is needed for cases where a full derived type has been
6427 Indic := Subtype_Indication
6428 (Type_Definition (Original_Node (Parent (F_Typ))));
6430 if Nkind (Indic) = N_Subtype_Indication then
6431 G_Typ := Entity (Subtype_Mark (Indic));
6433 G_Typ := Entity (Indic);
6436 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6437 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6439 return Generic_Parent_Type (Parent (G_Typ));
6444 end Get_Generic_Parent_Type;
6446 ----------------------
6447 -- Types_Correspond --
6448 ----------------------
6450 function Types_Correspond
6451 (P_Type : Entity_Id;
6452 N_Type : Entity_Id) return Boolean
6454 Prev_Type : Entity_Id := Base_Type (P_Type);
6455 New_Type : Entity_Id := Base_Type (N_Type);
6458 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6459 Prev_Type := Designated_Type (Prev_Type);
6462 if Ekind (New_Type) = E_Anonymous_Access_Type then
6463 New_Type := Designated_Type (New_Type);
6466 if Prev_Type = New_Type then
6469 elsif not Is_Class_Wide_Type (New_Type) then
6470 while Etype (New_Type) /= New_Type loop
6471 New_Type := Etype (New_Type);
6472 if New_Type = Prev_Type then
6478 end Types_Correspond;
6480 -- Start of processing for Is_Non_Overriding_Operation
6483 -- In the case where both operations are implicit derived subprograms
6484 -- then neither overrides the other. This can only occur in certain
6485 -- obscure cases (e.g., derivation from homographs created in a generic
6488 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6491 elsif Ekind (Current_Scope) = E_Package
6492 and then Is_Generic_Instance (Current_Scope)
6493 and then In_Private_Part (Current_Scope)
6494 and then Comes_From_Source (New_E)
6496 -- We examine the formals and result subtype of the inherited
6497 -- operation, to determine whether their type is derived from (the
6498 -- instance of) a generic type.
6500 Formal := First_Formal (Prev_E);
6502 while Present (Formal) loop
6503 F_Typ := Base_Type (Etype (Formal));
6505 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6506 F_Typ := Designated_Type (F_Typ);
6509 G_Typ := Get_Generic_Parent_Type (F_Typ);
6511 Next_Formal (Formal);
6514 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6515 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6522 -- If the generic type is a private type, then the original operation
6523 -- was not overriding in the generic, because there was no primitive
6524 -- operation to override.
6526 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6527 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6528 N_Formal_Private_Type_Definition
6532 -- The generic parent type is the ancestor of a formal derived
6533 -- type declaration. We need to check whether it has a primitive
6534 -- operation that should be overridden by New_E in the generic.
6538 P_Formal : Entity_Id;
6539 N_Formal : Entity_Id;
6543 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6546 while Present (Prim_Elt) loop
6547 P_Prim := Node (Prim_Elt);
6549 if Chars (P_Prim) = Chars (New_E)
6550 and then Ekind (P_Prim) = Ekind (New_E)
6552 P_Formal := First_Formal (P_Prim);
6553 N_Formal := First_Formal (New_E);
6554 while Present (P_Formal) and then Present (N_Formal) loop
6555 P_Typ := Etype (P_Formal);
6556 N_Typ := Etype (N_Formal);
6558 if not Types_Correspond (P_Typ, N_Typ) then
6562 Next_Entity (P_Formal);
6563 Next_Entity (N_Formal);
6566 -- Found a matching primitive operation belonging to the
6567 -- formal ancestor type, so the new subprogram is
6571 and then No (N_Formal)
6572 and then (Ekind (New_E) /= E_Function
6575 (Etype (P_Prim), Etype (New_E)))
6581 Next_Elmt (Prim_Elt);
6584 -- If no match found, then the new subprogram does not
6585 -- override in the generic (nor in the instance).
6593 end Is_Non_Overriding_Operation;
6595 ------------------------------
6596 -- Make_Inequality_Operator --
6597 ------------------------------
6599 -- S is the defining identifier of an equality operator. We build a
6600 -- subprogram declaration with the right signature. This operation is
6601 -- intrinsic, because it is always expanded as the negation of the
6602 -- call to the equality function.
6604 procedure Make_Inequality_Operator (S : Entity_Id) is
6605 Loc : constant Source_Ptr := Sloc (S);
6608 Op_Name : Entity_Id;
6610 FF : constant Entity_Id := First_Formal (S);
6611 NF : constant Entity_Id := Next_Formal (FF);
6614 -- Check that equality was properly defined, ignore call if not
6621 A : constant Entity_Id :=
6622 Make_Defining_Identifier (Sloc (FF),
6623 Chars => Chars (FF));
6625 B : constant Entity_Id :=
6626 Make_Defining_Identifier (Sloc (NF),
6627 Chars => Chars (NF));
6630 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6632 Formals := New_List (
6633 Make_Parameter_Specification (Loc,
6634 Defining_Identifier => A,
6636 New_Reference_To (Etype (First_Formal (S)),
6637 Sloc (Etype (First_Formal (S))))),
6639 Make_Parameter_Specification (Loc,
6640 Defining_Identifier => B,
6642 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6643 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6646 Make_Subprogram_Declaration (Loc,
6648 Make_Function_Specification (Loc,
6649 Defining_Unit_Name => Op_Name,
6650 Parameter_Specifications => Formals,
6651 Result_Definition =>
6652 New_Reference_To (Standard_Boolean, Loc)));
6654 -- Insert inequality right after equality if it is explicit or after
6655 -- the derived type when implicit. These entities are created only
6656 -- for visibility purposes, and eventually replaced in the course of
6657 -- expansion, so they do not need to be attached to the tree and seen
6658 -- by the back-end. Keeping them internal also avoids spurious
6659 -- freezing problems. The declaration is inserted in the tree for
6660 -- analysis, and removed afterwards. If the equality operator comes
6661 -- from an explicit declaration, attach the inequality immediately
6662 -- after. Else the equality is inherited from a derived type
6663 -- declaration, so insert inequality after that declaration.
6665 if No (Alias (S)) then
6666 Insert_After (Unit_Declaration_Node (S), Decl);
6667 elsif Is_List_Member (Parent (S)) then
6668 Insert_After (Parent (S), Decl);
6670 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6673 Mark_Rewrite_Insertion (Decl);
6674 Set_Is_Intrinsic_Subprogram (Op_Name);
6677 Set_Has_Completion (Op_Name);
6678 Set_Corresponding_Equality (Op_Name, S);
6679 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6681 end Make_Inequality_Operator;
6683 ----------------------
6684 -- May_Need_Actuals --
6685 ----------------------
6687 procedure May_Need_Actuals (Fun : Entity_Id) is
6692 F := First_Formal (Fun);
6694 while Present (F) loop
6695 if No (Default_Value (F)) then
6703 Set_Needs_No_Actuals (Fun, B);
6704 end May_Need_Actuals;
6706 ---------------------
6707 -- Mode_Conformant --
6708 ---------------------
6710 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6713 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6715 end Mode_Conformant;
6717 ---------------------------
6718 -- New_Overloaded_Entity --
6719 ---------------------------
6721 procedure New_Overloaded_Entity
6723 Derived_Type : Entity_Id := Empty)
6725 Overridden_Subp : Entity_Id := Empty;
6726 -- Set if the current scope has an operation that is type-conformant
6727 -- with S, and becomes hidden by S.
6729 Is_Primitive_Subp : Boolean;
6730 -- Set to True if the new subprogram is primitive
6733 -- Entity that S overrides
6735 Prev_Vis : Entity_Id := Empty;
6736 -- Predecessor of E in Homonym chain
6738 procedure Check_For_Primitive_Subprogram
6739 (Is_Primitive : out Boolean;
6740 Is_Overriding : Boolean := False);
6741 -- If the subprogram being analyzed is a primitive operation of the type
6742 -- of a formal or result, set the Has_Primitive_Operations flag on the
6743 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6744 -- corresponding flag on the entity itself for later use.
6746 procedure Check_Synchronized_Overriding
6747 (Def_Id : Entity_Id;
6748 Overridden_Subp : out Entity_Id);
6749 -- First determine if Def_Id is an entry or a subprogram either defined
6750 -- in the scope of a task or protected type, or is a primitive of such
6751 -- a type. Check whether Def_Id overrides a subprogram of an interface
6752 -- implemented by the synchronized type, return the overridden entity
6755 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6756 -- Check that E is declared in the private part of the current package,
6757 -- or in the package body, where it may hide a previous declaration.
6758 -- We can't use In_Private_Part by itself because this flag is also
6759 -- set when freezing entities, so we must examine the place of the
6760 -- declaration in the tree, and recognize wrapper packages as well.
6762 function Is_Overriding_Alias
6764 New_E : Entity_Id) return Boolean;
6765 -- Check whether new subprogram and old subprogram are both inherited
6766 -- from subprograms that have distinct dispatch table entries. This can
6767 -- occur with derivations from instances with accidental homonyms.
6768 -- The function is conservative given that the converse is only true
6769 -- within instances that contain accidental overloadings.
6771 ------------------------------------
6772 -- Check_For_Primitive_Subprogram --
6773 ------------------------------------
6775 procedure Check_For_Primitive_Subprogram
6776 (Is_Primitive : out Boolean;
6777 Is_Overriding : Boolean := False)
6783 function Visible_Part_Type (T : Entity_Id) return Boolean;
6784 -- Returns true if T is declared in the visible part of the current
6785 -- package scope; otherwise returns false. Assumes that T is declared
6788 procedure Check_Private_Overriding (T : Entity_Id);
6789 -- Checks that if a primitive abstract subprogram of a visible
6790 -- abstract type is declared in a private part, then it must override
6791 -- an abstract subprogram declared in the visible part. Also checks
6792 -- that if a primitive function with a controlling result is declared
6793 -- in a private part, then it must override a function declared in
6794 -- the visible part.
6796 ------------------------------
6797 -- Check_Private_Overriding --
6798 ------------------------------
6800 procedure Check_Private_Overriding (T : Entity_Id) is
6802 if Is_Package_Or_Generic_Package (Current_Scope)
6803 and then In_Private_Part (Current_Scope)
6804 and then Visible_Part_Type (T)
6805 and then not In_Instance
6807 if Is_Abstract_Type (T)
6808 and then Is_Abstract_Subprogram (S)
6809 and then (not Is_Overriding
6810 or else not Is_Abstract_Subprogram (E))
6812 Error_Msg_N ("abstract subprograms must be visible "
6813 & "(RM 3.9.3(10))!", S);
6815 elsif Ekind (S) = E_Function
6816 and then Is_Tagged_Type (T)
6817 and then T = Base_Type (Etype (S))
6818 and then not Is_Overriding
6821 ("private function with tagged result must"
6822 & " override visible-part function", S);
6824 ("\move subprogram to the visible part"
6825 & " (RM 3.9.3(10))", S);
6828 end Check_Private_Overriding;
6830 -----------------------
6831 -- Visible_Part_Type --
6832 -----------------------
6834 function Visible_Part_Type (T : Entity_Id) return Boolean is
6835 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6839 -- If the entity is a private type, then it must be declared in a
6842 if Ekind (T) in Private_Kind then
6846 -- Otherwise, we traverse the visible part looking for its
6847 -- corresponding declaration. We cannot use the declaration
6848 -- node directly because in the private part the entity of a
6849 -- private type is the one in the full view, which does not
6850 -- indicate that it is the completion of something visible.
6852 N := First (Visible_Declarations (Specification (P)));
6853 while Present (N) loop
6854 if Nkind (N) = N_Full_Type_Declaration
6855 and then Present (Defining_Identifier (N))
6856 and then T = Defining_Identifier (N)
6860 elsif Nkind_In (N, N_Private_Type_Declaration,
6861 N_Private_Extension_Declaration)
6862 and then Present (Defining_Identifier (N))
6863 and then T = Full_View (Defining_Identifier (N))
6872 end Visible_Part_Type;
6874 -- Start of processing for Check_For_Primitive_Subprogram
6877 Is_Primitive := False;
6879 if not Comes_From_Source (S) then
6882 -- If subprogram is at library level, it is not primitive operation
6884 elsif Current_Scope = Standard_Standard then
6887 elsif (Is_Package_Or_Generic_Package (Current_Scope)
6888 and then not In_Package_Body (Current_Scope))
6889 or else Is_Overriding
6891 -- For function, check return type
6893 if Ekind (S) = E_Function then
6894 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6895 F_Typ := Designated_Type (Etype (S));
6900 B_Typ := Base_Type (F_Typ);
6902 if Scope (B_Typ) = Current_Scope
6903 and then not Is_Class_Wide_Type (B_Typ)
6904 and then not Is_Generic_Type (B_Typ)
6906 Is_Primitive := True;
6907 Set_Has_Primitive_Operations (B_Typ);
6908 Set_Is_Primitive (S);
6909 Check_Private_Overriding (B_Typ);
6913 -- For all subprograms, check formals
6915 Formal := First_Formal (S);
6916 while Present (Formal) loop
6917 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6918 F_Typ := Designated_Type (Etype (Formal));
6920 F_Typ := Etype (Formal);
6923 B_Typ := Base_Type (F_Typ);
6925 if Ekind (B_Typ) = E_Access_Subtype then
6926 B_Typ := Base_Type (B_Typ);
6929 if Scope (B_Typ) = Current_Scope
6930 and then not Is_Class_Wide_Type (B_Typ)
6931 and then not Is_Generic_Type (B_Typ)
6933 Is_Primitive := True;
6934 Set_Is_Primitive (S);
6935 Set_Has_Primitive_Operations (B_Typ);
6936 Check_Private_Overriding (B_Typ);
6939 Next_Formal (Formal);
6942 end Check_For_Primitive_Subprogram;
6944 -----------------------------------
6945 -- Check_Synchronized_Overriding --
6946 -----------------------------------
6948 procedure Check_Synchronized_Overriding
6949 (Def_Id : Entity_Id;
6950 Overridden_Subp : out Entity_Id)
6952 Ifaces_List : Elist_Id;
6956 function Matches_Prefixed_View_Profile
6957 (Prim_Params : List_Id;
6958 Iface_Params : List_Id) return Boolean;
6959 -- Determine whether a subprogram's parameter profile Prim_Params
6960 -- matches that of a potentially overridden interface subprogram
6961 -- Iface_Params. Also determine if the type of first parameter of
6962 -- Iface_Params is an implemented interface.
6964 -----------------------------------
6965 -- Matches_Prefixed_View_Profile --
6966 -----------------------------------
6968 function Matches_Prefixed_View_Profile
6969 (Prim_Params : List_Id;
6970 Iface_Params : List_Id) return Boolean
6972 Iface_Id : Entity_Id;
6973 Iface_Param : Node_Id;
6974 Iface_Typ : Entity_Id;
6975 Prim_Id : Entity_Id;
6976 Prim_Param : Node_Id;
6977 Prim_Typ : Entity_Id;
6979 function Is_Implemented
6980 (Ifaces_List : Elist_Id;
6981 Iface : Entity_Id) return Boolean;
6982 -- Determine if Iface is implemented by the current task or
6985 --------------------
6986 -- Is_Implemented --
6987 --------------------
6989 function Is_Implemented
6990 (Ifaces_List : Elist_Id;
6991 Iface : Entity_Id) return Boolean
6993 Iface_Elmt : Elmt_Id;
6996 Iface_Elmt := First_Elmt (Ifaces_List);
6997 while Present (Iface_Elmt) loop
6998 if Node (Iface_Elmt) = Iface then
7002 Next_Elmt (Iface_Elmt);
7008 -- Start of processing for Matches_Prefixed_View_Profile
7011 Iface_Param := First (Iface_Params);
7012 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7014 if Is_Access_Type (Iface_Typ) then
7015 Iface_Typ := Designated_Type (Iface_Typ);
7018 Prim_Param := First (Prim_Params);
7020 -- The first parameter of the potentially overridden subprogram
7021 -- must be an interface implemented by Prim.
7023 if not Is_Interface (Iface_Typ)
7024 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7029 -- The checks on the object parameters are done, move onto the
7030 -- rest of the parameters.
7032 if not In_Scope then
7033 Prim_Param := Next (Prim_Param);
7036 Iface_Param := Next (Iface_Param);
7037 while Present (Iface_Param) and then Present (Prim_Param) loop
7038 Iface_Id := Defining_Identifier (Iface_Param);
7039 Iface_Typ := Find_Parameter_Type (Iface_Param);
7041 Prim_Id := Defining_Identifier (Prim_Param);
7042 Prim_Typ := Find_Parameter_Type (Prim_Param);
7044 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7045 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7046 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7048 Iface_Typ := Designated_Type (Iface_Typ);
7049 Prim_Typ := Designated_Type (Prim_Typ);
7052 -- Case of multiple interface types inside a parameter profile
7054 -- (Obj_Param : in out Iface; ...; Param : Iface)
7056 -- If the interface type is implemented, then the matching type
7057 -- in the primitive should be the implementing record type.
7059 if Ekind (Iface_Typ) = E_Record_Type
7060 and then Is_Interface (Iface_Typ)
7061 and then Is_Implemented (Ifaces_List, Iface_Typ)
7063 if Prim_Typ /= Typ then
7067 -- The two parameters must be both mode and subtype conformant
7069 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7071 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7080 -- One of the two lists contains more parameters than the other
7082 if Present (Iface_Param) or else Present (Prim_Param) then
7087 end Matches_Prefixed_View_Profile;
7089 -- Start of processing for Check_Synchronized_Overriding
7092 Overridden_Subp := Empty;
7094 -- Def_Id must be an entry or a subprogram. We should skip predefined
7095 -- primitives internally generated by the frontend; however at this
7096 -- stage predefined primitives are still not fully decorated. As a
7097 -- minor optimization we skip here internally generated subprograms.
7099 if (Ekind (Def_Id) /= E_Entry
7100 and then Ekind (Def_Id) /= E_Function
7101 and then Ekind (Def_Id) /= E_Procedure)
7102 or else not Comes_From_Source (Def_Id)
7107 -- Search for the concurrent declaration since it contains the list
7108 -- of all implemented interfaces. In this case, the subprogram is
7109 -- declared within the scope of a protected or a task type.
7111 if Present (Scope (Def_Id))
7112 and then Is_Concurrent_Type (Scope (Def_Id))
7113 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7115 Typ := Scope (Def_Id);
7118 -- The enclosing scope is not a synchronized type and the subprogram
7121 elsif No (First_Formal (Def_Id)) then
7124 -- The subprogram has formals and hence it may be a primitive of a
7128 Typ := Etype (First_Formal (Def_Id));
7130 if Is_Access_Type (Typ) then
7131 Typ := Directly_Designated_Type (Typ);
7134 if Is_Concurrent_Type (Typ)
7135 and then not Is_Generic_Actual_Type (Typ)
7139 -- This case occurs when the concurrent type is declared within
7140 -- a generic unit. As a result the corresponding record has been
7141 -- built and used as the type of the first formal, we just have
7142 -- to retrieve the corresponding concurrent type.
7144 elsif Is_Concurrent_Record_Type (Typ)
7145 and then Present (Corresponding_Concurrent_Type (Typ))
7147 Typ := Corresponding_Concurrent_Type (Typ);
7155 -- There is no overriding to check if is an inherited operation in a
7156 -- type derivation on for a generic actual.
7158 Collect_Interfaces (Typ, Ifaces_List);
7160 if Is_Empty_Elmt_List (Ifaces_List) then
7164 -- Determine whether entry or subprogram Def_Id overrides a primitive
7165 -- operation that belongs to one of the interfaces in Ifaces_List.
7168 Candidate : Entity_Id := Empty;
7169 Hom : Entity_Id := Empty;
7170 Iface_Typ : Entity_Id;
7171 Subp : Entity_Id := Empty;
7174 -- Traverse the homonym chain, looking at a potentially
7175 -- overridden subprogram that belongs to an implemented
7178 Hom := Current_Entity_In_Scope (Def_Id);
7179 while Present (Hom) loop
7183 or else not Is_Overloadable (Subp)
7184 or else not Is_Primitive (Subp)
7185 or else not Is_Dispatching_Operation (Subp)
7186 or else not Is_Interface (Find_Dispatching_Type (Subp))
7190 -- Entries and procedures can override abstract or null
7191 -- interface procedures
7193 elsif (Ekind (Def_Id) = E_Procedure
7194 or else Ekind (Def_Id) = E_Entry)
7195 and then Ekind (Subp) = E_Procedure
7196 and then Matches_Prefixed_View_Profile
7197 (Parameter_Specifications (Parent (Def_Id)),
7198 Parameter_Specifications (Parent (Subp)))
7202 -- For an overridden subprogram Subp, check whether the mode
7203 -- of its first parameter is correct depending on the kind
7204 -- of synchronized type.
7207 Formal : constant Node_Id := First_Formal (Candidate);
7210 -- In order for an entry or a protected procedure to
7211 -- override, the first parameter of the overridden
7212 -- routine must be of mode "out", "in out" or
7213 -- access-to-variable.
7215 if (Ekind (Candidate) = E_Entry
7216 or else Ekind (Candidate) = E_Procedure)
7217 and then Is_Protected_Type (Typ)
7218 and then Ekind (Formal) /= E_In_Out_Parameter
7219 and then Ekind (Formal) /= E_Out_Parameter
7220 and then Nkind (Parameter_Type (Parent (Formal)))
7221 /= N_Access_Definition
7225 -- All other cases are OK since a task entry or routine
7226 -- does not have a restriction on the mode of the first
7227 -- parameter of the overridden interface routine.
7230 Overridden_Subp := Candidate;
7235 -- Functions can override abstract interface functions
7237 elsif Ekind (Def_Id) = E_Function
7238 and then Ekind (Subp) = E_Function
7239 and then Matches_Prefixed_View_Profile
7240 (Parameter_Specifications (Parent (Def_Id)),
7241 Parameter_Specifications (Parent (Subp)))
7242 and then Etype (Result_Definition (Parent (Def_Id))) =
7243 Etype (Result_Definition (Parent (Subp)))
7245 Overridden_Subp := Subp;
7249 Hom := Homonym (Hom);
7252 -- After examining all candidates for overriding, we are
7253 -- left with the best match which is a mode incompatible
7254 -- interface routine. Do not emit an error if the Expander
7255 -- is active since this error will be detected later on
7256 -- after all concurrent types are expanded and all wrappers
7257 -- are built. This check is meant for spec-only
7260 if Present (Candidate)
7261 and then not Expander_Active
7264 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7266 -- Def_Id is primitive of a protected type, declared
7267 -- inside the type, and the candidate is primitive of a
7268 -- limited or synchronized interface.
7271 and then Is_Protected_Type (Typ)
7273 (Is_Limited_Interface (Iface_Typ)
7274 or else Is_Protected_Interface (Iface_Typ)
7275 or else Is_Synchronized_Interface (Iface_Typ)
7276 or else Is_Task_Interface (Iface_Typ))
7278 -- Must reword this message, comma before to in -gnatj
7282 ("first formal of & must be of mode `OUT`, `IN OUT`"
7283 & " or access-to-variable", Typ, Candidate);
7285 ("\to be overridden by protected procedure or entry "
7286 & "(RM 9.4(11.9/2))", Typ);
7290 Overridden_Subp := Candidate;
7293 end Check_Synchronized_Overriding;
7295 ----------------------------
7296 -- Is_Private_Declaration --
7297 ----------------------------
7299 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7300 Priv_Decls : List_Id;
7301 Decl : constant Node_Id := Unit_Declaration_Node (E);
7304 if Is_Package_Or_Generic_Package (Current_Scope)
7305 and then In_Private_Part (Current_Scope)
7308 Private_Declarations (
7309 Specification (Unit_Declaration_Node (Current_Scope)));
7311 return In_Package_Body (Current_Scope)
7313 (Is_List_Member (Decl)
7314 and then List_Containing (Decl) = Priv_Decls)
7315 or else (Nkind (Parent (Decl)) = N_Package_Specification
7318 (Defining_Entity (Parent (Decl)))
7319 and then List_Containing (Parent (Parent (Decl)))
7324 end Is_Private_Declaration;
7326 --------------------------
7327 -- Is_Overriding_Alias --
7328 --------------------------
7330 function Is_Overriding_Alias
7332 New_E : Entity_Id) return Boolean
7334 AO : constant Entity_Id := Alias (Old_E);
7335 AN : constant Entity_Id := Alias (New_E);
7338 return Scope (AO) /= Scope (AN)
7339 or else No (DTC_Entity (AO))
7340 or else No (DTC_Entity (AN))
7341 or else DT_Position (AO) = DT_Position (AN);
7342 end Is_Overriding_Alias;
7344 -- Start of processing for New_Overloaded_Entity
7347 -- We need to look for an entity that S may override. This must be a
7348 -- homonym in the current scope, so we look for the first homonym of
7349 -- S in the current scope as the starting point for the search.
7351 E := Current_Entity_In_Scope (S);
7353 -- If there is no homonym then this is definitely not overriding
7356 Enter_Overloaded_Entity (S);
7357 Check_Dispatching_Operation (S, Empty);
7358 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7360 -- If subprogram has an explicit declaration, check whether it
7361 -- has an overriding indicator.
7363 if Comes_From_Source (S) then
7364 Check_Synchronized_Overriding (S, Overridden_Subp);
7365 Check_Overriding_Indicator
7366 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7369 -- If there is a homonym that is not overloadable, then we have an
7370 -- error, except for the special cases checked explicitly below.
7372 elsif not Is_Overloadable (E) then
7374 -- Check for spurious conflict produced by a subprogram that has the
7375 -- same name as that of the enclosing generic package. The conflict
7376 -- occurs within an instance, between the subprogram and the renaming
7377 -- declaration for the package. After the subprogram, the package
7378 -- renaming declaration becomes hidden.
7380 if Ekind (E) = E_Package
7381 and then Present (Renamed_Object (E))
7382 and then Renamed_Object (E) = Current_Scope
7383 and then Nkind (Parent (Renamed_Object (E))) =
7384 N_Package_Specification
7385 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7388 Set_Is_Immediately_Visible (E, False);
7389 Enter_Overloaded_Entity (S);
7390 Set_Homonym (S, Homonym (E));
7391 Check_Dispatching_Operation (S, Empty);
7392 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7394 -- If the subprogram is implicit it is hidden by the previous
7395 -- declaration. However if it is dispatching, it must appear in the
7396 -- dispatch table anyway, because it can be dispatched to even if it
7397 -- cannot be called directly.
7399 elsif Present (Alias (S))
7400 and then not Comes_From_Source (S)
7402 Set_Scope (S, Current_Scope);
7404 if Is_Dispatching_Operation (Alias (S)) then
7405 Check_Dispatching_Operation (S, Empty);
7411 Error_Msg_Sloc := Sloc (E);
7413 -- Generate message, with useful additional warning if in generic
7415 if Is_Generic_Unit (E) then
7416 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7417 Error_Msg_N ("\& conflicts with declaration#", S);
7419 Error_Msg_N ("& conflicts with declaration#", S);
7425 -- E exists and is overloadable
7428 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
7429 -- need no check against the homonym chain. They are directly added
7430 -- to the list of primitive operations of Derived_Type.
7432 if Ada_Version >= Ada_05
7433 and then Present (Derived_Type)
7434 and then Is_Dispatching_Operation (Alias (S))
7435 and then Present (Find_Dispatching_Type (Alias (S)))
7436 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7438 goto Add_New_Entity;
7441 Check_Synchronized_Overriding (S, Overridden_Subp);
7443 -- Loop through E and its homonyms to determine if any of them is
7444 -- the candidate for overriding by S.
7446 while Present (E) loop
7448 -- Definitely not interesting if not in the current scope
7450 if Scope (E) /= Current_Scope then
7453 -- Check if we have type conformance
7455 elsif Type_Conformant (E, S) then
7457 -- If the old and new entities have the same profile and one
7458 -- is not the body of the other, then this is an error, unless
7459 -- one of them is implicitly declared.
7461 -- There are some cases when both can be implicit, for example
7462 -- when both a literal and a function that overrides it are
7463 -- inherited in a derivation, or when an inherited operation
7464 -- of a tagged full type overrides the inherited operation of
7465 -- a private extension. Ada 83 had a special rule for the
7466 -- literal case. In Ada95, the later implicit operation hides
7467 -- the former, and the literal is always the former. In the
7468 -- odd case where both are derived operations declared at the
7469 -- same point, both operations should be declared, and in that
7470 -- case we bypass the following test and proceed to the next
7471 -- part. This can only occur for certain obscure cases in
7472 -- instances, when an operation on a type derived from a formal
7473 -- private type does not override a homograph inherited from
7474 -- the actual. In subsequent derivations of such a type, the
7475 -- DT positions of these operations remain distinct, if they
7478 if Present (Alias (S))
7479 and then (No (Alias (E))
7480 or else Comes_From_Source (E)
7481 or else Is_Abstract_Subprogram (S)
7483 (Is_Dispatching_Operation (E)
7484 and then Is_Overriding_Alias (E, S)))
7485 and then Ekind (E) /= E_Enumeration_Literal
7487 -- When an derived operation is overloaded it may be due to
7488 -- the fact that the full view of a private extension
7489 -- re-inherits. It has to be dealt with.
7491 if Is_Package_Or_Generic_Package (Current_Scope)
7492 and then In_Private_Part (Current_Scope)
7494 Check_Operation_From_Private_View (S, E);
7497 -- In any case the implicit operation remains hidden by
7498 -- the existing declaration, which is overriding.
7500 Set_Is_Overriding_Operation (E);
7502 if Comes_From_Source (E) then
7503 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7505 -- Indicate that E overrides the operation from which
7508 if Present (Alias (S)) then
7509 Set_Overridden_Operation (E, Alias (S));
7511 Set_Overridden_Operation (E, S);
7517 -- Within an instance, the renaming declarations for actual
7518 -- subprograms may become ambiguous, but they do not hide each
7521 elsif Ekind (E) /= E_Entry
7522 and then not Comes_From_Source (E)
7523 and then not Is_Generic_Instance (E)
7524 and then (Present (Alias (E))
7525 or else Is_Intrinsic_Subprogram (E))
7526 and then (not In_Instance
7527 or else No (Parent (E))
7528 or else Nkind (Unit_Declaration_Node (E)) /=
7529 N_Subprogram_Renaming_Declaration)
7531 -- A subprogram child unit is not allowed to override an
7532 -- inherited subprogram (10.1.1(20)).
7534 if Is_Child_Unit (S) then
7536 ("child unit overrides inherited subprogram in parent",
7541 if Is_Non_Overriding_Operation (E, S) then
7542 Enter_Overloaded_Entity (S);
7544 if No (Derived_Type)
7545 or else Is_Tagged_Type (Derived_Type)
7547 Check_Dispatching_Operation (S, Empty);
7553 -- E is a derived operation or an internal operator which
7554 -- is being overridden. Remove E from further visibility.
7555 -- Furthermore, if E is a dispatching operation, it must be
7556 -- replaced in the list of primitive operations of its type
7557 -- (see Override_Dispatching_Operation).
7559 Overridden_Subp := E;
7565 Prev := First_Entity (Current_Scope);
7566 while Present (Prev)
7567 and then Next_Entity (Prev) /= E
7572 -- It is possible for E to be in the current scope and
7573 -- yet not in the entity chain. This can only occur in a
7574 -- generic context where E is an implicit concatenation
7575 -- in the formal part, because in a generic body the
7576 -- entity chain starts with the formals.
7579 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7581 -- E must be removed both from the entity_list of the
7582 -- current scope, and from the visibility chain
7584 if Debug_Flag_E then
7585 Write_Str ("Override implicit operation ");
7586 Write_Int (Int (E));
7590 -- If E is a predefined concatenation, it stands for four
7591 -- different operations. As a result, a single explicit
7592 -- declaration does not hide it. In a possible ambiguous
7593 -- situation, Disambiguate chooses the user-defined op,
7594 -- so it is correct to retain the previous internal one.
7596 if Chars (E) /= Name_Op_Concat
7597 or else Ekind (E) /= E_Operator
7599 -- For nondispatching derived operations that are
7600 -- overridden by a subprogram declared in the private
7601 -- part of a package, we retain the derived subprogram
7602 -- but mark it as not immediately visible. If the
7603 -- derived operation was declared in the visible part
7604 -- then this ensures that it will still be visible
7605 -- outside the package with the proper signature
7606 -- (calls from outside must also be directed to this
7607 -- version rather than the overriding one, unlike the
7608 -- dispatching case). Calls from inside the package
7609 -- will still resolve to the overriding subprogram
7610 -- since the derived one is marked as not visible
7611 -- within the package.
7613 -- If the private operation is dispatching, we achieve
7614 -- the overriding by keeping the implicit operation
7615 -- but setting its alias to be the overriding one. In
7616 -- this fashion the proper body is executed in all
7617 -- cases, but the original signature is used outside
7620 -- If the overriding is not in the private part, we
7621 -- remove the implicit operation altogether.
7623 if Is_Private_Declaration (S) then
7624 if not Is_Dispatching_Operation (E) then
7625 Set_Is_Immediately_Visible (E, False);
7627 -- Work done in Override_Dispatching_Operation,
7628 -- so nothing else need to be done here.
7634 -- Find predecessor of E in Homonym chain
7636 if E = Current_Entity (E) then
7639 Prev_Vis := Current_Entity (E);
7640 while Homonym (Prev_Vis) /= E loop
7641 Prev_Vis := Homonym (Prev_Vis);
7645 if Prev_Vis /= Empty then
7647 -- Skip E in the visibility chain
7649 Set_Homonym (Prev_Vis, Homonym (E));
7652 Set_Name_Entity_Id (Chars (E), Homonym (E));
7655 Set_Next_Entity (Prev, Next_Entity (E));
7657 if No (Next_Entity (Prev)) then
7658 Set_Last_Entity (Current_Scope, Prev);
7664 Enter_Overloaded_Entity (S);
7665 Set_Is_Overriding_Operation (S);
7666 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7668 -- Indicate that S overrides the operation from which
7671 if Comes_From_Source (S) then
7672 if Present (Alias (E)) then
7673 Set_Overridden_Operation (S, Alias (E));
7675 Set_Overridden_Operation (S, E);
7679 if Is_Dispatching_Operation (E) then
7681 -- An overriding dispatching subprogram inherits the
7682 -- convention of the overridden subprogram (by
7685 Set_Convention (S, Convention (E));
7686 Check_Dispatching_Operation (S, E);
7689 Check_Dispatching_Operation (S, Empty);
7692 Check_For_Primitive_Subprogram
7693 (Is_Primitive_Subp, Is_Overriding => True);
7694 goto Check_Inequality;
7697 -- Apparent redeclarations in instances can occur when two
7698 -- formal types get the same actual type. The subprograms in
7699 -- in the instance are legal, even if not callable from the
7700 -- outside. Calls from within are disambiguated elsewhere.
7701 -- For dispatching operations in the visible part, the usual
7702 -- rules apply, and operations with the same profile are not
7705 elsif (In_Instance_Visible_Part
7706 and then not Is_Dispatching_Operation (E))
7707 or else In_Instance_Not_Visible
7711 -- Here we have a real error (identical profile)
7714 Error_Msg_Sloc := Sloc (E);
7716 -- Avoid cascaded errors if the entity appears in
7717 -- subsequent calls.
7719 Set_Scope (S, Current_Scope);
7721 -- Generate error, with extra useful warning for the case
7722 -- of a generic instance with no completion.
7724 if Is_Generic_Instance (S)
7725 and then not Has_Completion (E)
7728 ("instantiation cannot provide body for&", S);
7729 Error_Msg_N ("\& conflicts with declaration#", S);
7731 Error_Msg_N ("& conflicts with declaration#", S);
7738 -- If one subprogram has an access parameter and the other
7739 -- a parameter of an access type, calls to either might be
7740 -- ambiguous. Verify that parameters match except for the
7741 -- access parameter.
7743 if May_Hide_Profile then
7749 F1 := First_Formal (S);
7750 F2 := First_Formal (E);
7751 while Present (F1) and then Present (F2) loop
7752 if Is_Access_Type (Etype (F1)) then
7753 if not Is_Access_Type (Etype (F2))
7754 or else not Conforming_Types
7755 (Designated_Type (Etype (F1)),
7756 Designated_Type (Etype (F2)),
7759 May_Hide_Profile := False;
7763 not Conforming_Types
7764 (Etype (F1), Etype (F2), Type_Conformant)
7766 May_Hide_Profile := False;
7777 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7788 -- On exit, we know that S is a new entity
7790 Enter_Overloaded_Entity (S);
7791 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7792 Check_Overriding_Indicator
7793 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7795 -- If S is a derived operation for an untagged type then by
7796 -- definition it's not a dispatching operation (even if the parent
7797 -- operation was dispatching), so we don't call
7798 -- Check_Dispatching_Operation in that case.
7800 if No (Derived_Type)
7801 or else Is_Tagged_Type (Derived_Type)
7803 Check_Dispatching_Operation (S, Empty);
7807 -- If this is a user-defined equality operator that is not a derived
7808 -- subprogram, create the corresponding inequality. If the operation is
7809 -- dispatching, the expansion is done elsewhere, and we do not create
7810 -- an explicit inequality operation.
7812 <<Check_Inequality>>
7813 if Chars (S) = Name_Op_Eq
7814 and then Etype (S) = Standard_Boolean
7815 and then Present (Parent (S))
7816 and then not Is_Dispatching_Operation (S)
7818 Make_Inequality_Operator (S);
7820 end New_Overloaded_Entity;
7822 ---------------------
7823 -- Process_Formals --
7824 ---------------------
7826 procedure Process_Formals
7828 Related_Nod : Node_Id)
7830 Param_Spec : Node_Id;
7832 Formal_Type : Entity_Id;
7836 Num_Out_Params : Nat := 0;
7837 First_Out_Param : Entity_Id := Empty;
7838 -- Used for setting Is_Only_Out_Parameter
7840 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7841 -- Check whether the default has a class-wide type. After analysis the
7842 -- default has the type of the formal, so we must also check explicitly
7843 -- for an access attribute.
7845 ---------------------------
7846 -- Is_Class_Wide_Default --
7847 ---------------------------
7849 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7851 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7852 or else (Nkind (D) = N_Attribute_Reference
7853 and then Attribute_Name (D) = Name_Access
7854 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7855 end Is_Class_Wide_Default;
7857 -- Start of processing for Process_Formals
7860 -- In order to prevent premature use of the formals in the same formal
7861 -- part, the Ekind is left undefined until all default expressions are
7862 -- analyzed. The Ekind is established in a separate loop at the end.
7864 Param_Spec := First (T);
7865 while Present (Param_Spec) loop
7866 Formal := Defining_Identifier (Param_Spec);
7867 Set_Never_Set_In_Source (Formal, True);
7868 Enter_Name (Formal);
7870 -- Case of ordinary parameters
7872 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7873 Find_Type (Parameter_Type (Param_Spec));
7874 Ptype := Parameter_Type (Param_Spec);
7876 if Ptype = Error then
7880 Formal_Type := Entity (Ptype);
7882 if Is_Incomplete_Type (Formal_Type)
7884 (Is_Class_Wide_Type (Formal_Type)
7885 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7887 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
7888 -- primitive operations, as long as their completion is
7889 -- in the same declarative part. If in the private part
7890 -- this means that the type cannot be a Taft-amendment type.
7891 -- Check is done on package exit. For access to subprograms,
7892 -- the use is legal for Taft-amendment types.
7894 if Is_Tagged_Type (Formal_Type) then
7895 if Ekind (Scope (Current_Scope)) = E_Package
7896 and then In_Private_Part (Scope (Current_Scope))
7897 and then not From_With_Type (Formal_Type)
7898 and then not Is_Class_Wide_Type (Formal_Type)
7901 (Parent (T), N_Access_Function_Definition,
7902 N_Access_Procedure_Definition)
7906 Private_Dependents (Base_Type (Formal_Type)));
7910 -- Special handling of Value_Type for CIL case
7912 elsif Is_Value_Type (Formal_Type) then
7915 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7916 N_Access_Procedure_Definition)
7919 ("invalid use of incomplete type&",
7920 Param_Spec, Formal_Type);
7922 -- Further checks on the legality of incomplete types
7923 -- in formal parts must be delayed until the freeze point
7924 -- of the enclosing subprogram or access to subprogram.
7927 elsif Ekind (Formal_Type) = E_Void then
7928 Error_Msg_NE ("premature use of&",
7929 Parameter_Type (Param_Spec), Formal_Type);
7932 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7933 -- declaration corresponding to the null-excluding type of the
7934 -- formal in the enclosing scope. Finally, replace the parameter
7935 -- type of the formal with the internal subtype.
7937 if Ada_Version >= Ada_05
7938 and then Null_Exclusion_Present (Param_Spec)
7940 if not Is_Access_Type (Formal_Type) then
7942 ("`NOT NULL` allowed only for an access type", Param_Spec);
7945 if Can_Never_Be_Null (Formal_Type)
7946 and then Comes_From_Source (Related_Nod)
7949 ("`NOT NULL` not allowed (& already excludes null)",
7955 Create_Null_Excluding_Itype
7957 Related_Nod => Related_Nod,
7958 Scope_Id => Scope (Current_Scope));
7960 -- If the designated type of the itype is an itype we
7961 -- decorate it with the Has_Delayed_Freeze attribute to
7962 -- avoid problems with the backend.
7965 -- type T is access procedure;
7966 -- procedure Op (O : not null T);
7968 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
7969 Set_Has_Delayed_Freeze (Formal_Type);
7974 -- An access formal type
7978 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7980 -- No need to continue if we already notified errors
7982 if not Present (Formal_Type) then
7986 -- Ada 2005 (AI-254)
7989 AD : constant Node_Id :=
7990 Access_To_Subprogram_Definition
7991 (Parameter_Type (Param_Spec));
7993 if Present (AD) and then Protected_Present (AD) then
7995 Replace_Anonymous_Access_To_Protected_Subprogram
8001 Set_Etype (Formal, Formal_Type);
8002 Default := Expression (Param_Spec);
8004 if Present (Default) then
8005 if Out_Present (Param_Spec) then
8007 ("default initialization only allowed for IN parameters",
8011 -- Do the special preanalysis of the expression (see section on
8012 -- "Handling of Default Expressions" in the spec of package Sem).
8014 Preanalyze_Spec_Expression (Default, Formal_Type);
8016 -- An access to constant cannot be the default for
8017 -- an access parameter that is an access to variable.
8019 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8020 and then not Is_Access_Constant (Formal_Type)
8021 and then Is_Access_Type (Etype (Default))
8022 and then Is_Access_Constant (Etype (Default))
8025 ("formal that is access to variable cannot be initialized " &
8026 "with an access-to-constant expression", Default);
8029 -- Check that the designated type of an access parameter's default
8030 -- is not a class-wide type unless the parameter's designated type
8031 -- is also class-wide.
8033 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8034 and then not From_With_Type (Formal_Type)
8035 and then Is_Class_Wide_Default (Default)
8036 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8039 ("access to class-wide expression not allowed here", Default);
8043 -- Ada 2005 (AI-231): Static checks
8045 if Ada_Version >= Ada_05
8046 and then Is_Access_Type (Etype (Formal))
8047 and then Can_Never_Be_Null (Etype (Formal))
8049 Null_Exclusion_Static_Checks (Param_Spec);
8056 -- If this is the formal part of a function specification, analyze the
8057 -- subtype mark in the context where the formals are visible but not
8058 -- yet usable, and may hide outer homographs.
8060 if Nkind (Related_Nod) = N_Function_Specification then
8061 Analyze_Return_Type (Related_Nod);
8064 -- Now set the kind (mode) of each formal
8066 Param_Spec := First (T);
8068 while Present (Param_Spec) loop
8069 Formal := Defining_Identifier (Param_Spec);
8070 Set_Formal_Mode (Formal);
8072 if Ekind (Formal) = E_In_Parameter then
8073 Set_Default_Value (Formal, Expression (Param_Spec));
8075 if Present (Expression (Param_Spec)) then
8076 Default := Expression (Param_Spec);
8078 if Is_Scalar_Type (Etype (Default)) then
8080 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8082 Formal_Type := Entity (Parameter_Type (Param_Spec));
8085 Formal_Type := Access_Definition
8086 (Related_Nod, Parameter_Type (Param_Spec));
8089 Apply_Scalar_Range_Check (Default, Formal_Type);
8093 elsif Ekind (Formal) = E_Out_Parameter then
8094 Num_Out_Params := Num_Out_Params + 1;
8096 if Num_Out_Params = 1 then
8097 First_Out_Param := Formal;
8100 elsif Ekind (Formal) = E_In_Out_Parameter then
8101 Num_Out_Params := Num_Out_Params + 1;
8107 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8108 Set_Is_Only_Out_Parameter (First_Out_Param);
8110 end Process_Formals;
8116 procedure Process_PPCs
8118 Spec_Id : Entity_Id;
8119 Body_Id : Entity_Id)
8121 Loc : constant Source_Ptr := Sloc (N);
8123 Plist : List_Id := No_List;
8127 function Grab_PPC (Nam : Name_Id) return Node_Id;
8128 -- Prag contains an analyzed precondition or postcondition pragma.
8129 -- This function copies the pragma, changes it to the corresponding
8130 -- Check pragma and returns the Check pragma as the result. The
8131 -- argument Nam is either Name_Precondition or Name_Postcondition.
8137 function Grab_PPC (Nam : Name_Id) return Node_Id is
8138 CP : constant Node_Id := New_Copy_Tree (Prag);
8141 -- Set Analyzed to false, since we want to reanalyze the check
8142 -- procedure. Note that it is only at the outer level that we
8143 -- do this fiddling, for the spec cases, the already preanalyzed
8144 -- parameters are not affected.
8146 -- For a postcondition pragma within a generic, preserve the pragma
8147 -- for later expansion.
8149 Set_Analyzed (CP, False);
8151 if Nam = Name_Postcondition
8152 and then not Expander_Active
8157 -- Change pragma into corresponding pragma Check
8159 Prepend_To (Pragma_Argument_Associations (CP),
8160 Make_Pragma_Argument_Association (Sloc (Prag),
8162 Make_Identifier (Loc,
8164 Set_Pragma_Identifier (CP,
8165 Make_Identifier (Sloc (Prag),
8166 Chars => Name_Check));
8171 -- Start of processing for Process_PPCs
8174 -- Nothing to do if we are not generating code
8176 if Operating_Mode /= Generate_Code then
8180 -- Grab preconditions from spec
8182 if Present (Spec_Id) then
8184 -- Loop through PPC pragmas from spec. Note that preconditions from
8185 -- the body will be analyzed and converted when we scan the body
8186 -- declarations below.
8188 Prag := Spec_PPC_List (Spec_Id);
8189 while Present (Prag) loop
8190 if Pragma_Name (Prag) = Name_Precondition
8191 and then PPC_Enabled (Prag)
8193 -- Add pragma Check at the start of the declarations of N.
8194 -- Note that this processing reverses the order of the list,
8195 -- which is what we want since new entries were chained to
8196 -- the head of the list.
8198 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
8201 Prag := Next_Pragma (Prag);
8205 -- Build postconditions procedure if needed and prepend the following
8206 -- declaration to the start of the declarations for the subprogram.
8208 -- procedure _postconditions [(_Result : resulttype)] is
8210 -- pragma Check (Postcondition, condition [,message]);
8211 -- pragma Check (Postcondition, condition [,message]);
8215 -- First we deal with the postconditions in the body
8217 if Is_Non_Empty_List (Declarations (N)) then
8219 -- Loop through declarations
8221 Prag := First (Declarations (N));
8222 while Present (Prag) loop
8223 if Nkind (Prag) = N_Pragma then
8225 -- If pragma, capture if enabled postcondition, else ignore
8227 if Pragma_Name (Prag) = Name_Postcondition
8228 and then Check_Enabled (Name_Postcondition)
8230 if Plist = No_List then
8231 Plist := Empty_List;
8236 -- If expansion is disabled, as in a generic unit,
8237 -- save pragma for later expansion.
8239 if not Expander_Active then
8240 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8242 Append (Grab_PPC (Name_Postcondition), Plist);
8248 -- Not a pragma, if comes from source, then end scan
8250 elsif Comes_From_Source (Prag) then
8253 -- Skip stuff not coming from source
8261 -- Now deal with any postconditions from the spec
8263 if Present (Spec_Id) then
8265 -- Loop through PPC pragmas from spec
8267 Prag := Spec_PPC_List (Spec_Id);
8268 while Present (Prag) loop
8269 if Pragma_Name (Prag) = Name_Postcondition
8270 and then PPC_Enabled (Prag)
8272 if Plist = No_List then
8273 Plist := Empty_List;
8276 if not Expander_Active then
8277 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8279 Append (Grab_PPC (Name_Postcondition), Plist);
8283 Prag := Next_Pragma (Prag);
8287 -- If we had any postconditions and expansion is enabled, build
8288 -- the _Postconditions procedure.
8291 and then Expander_Active
8293 Subp := Defining_Entity (N);
8295 if Etype (Subp) /= Standard_Void_Type then
8297 Make_Parameter_Specification (Loc,
8298 Defining_Identifier =>
8299 Make_Defining_Identifier (Loc,
8300 Chars => Name_uResult),
8301 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8307 Post_Proc : constant Entity_Id :=
8308 Make_Defining_Identifier (Loc,
8309 Chars => Name_uPostconditions);
8310 -- The entity for the _Postconditions procedure
8312 Prepend_To (Declarations (N),
8313 Make_Subprogram_Body (Loc,
8315 Make_Procedure_Specification (Loc,
8316 Defining_Unit_Name => Post_Proc,
8317 Parameter_Specifications => Parms),
8319 Declarations => Empty_List,
8321 Handled_Statement_Sequence =>
8322 Make_Handled_Sequence_Of_Statements (Loc,
8323 Statements => Plist)));
8325 -- If this is a procedure, set the Postcondition_Proc attribute
8327 if Etype (Subp) = Standard_Void_Type then
8328 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8332 if Present (Spec_Id) then
8333 Set_Has_Postconditions (Spec_Id);
8335 Set_Has_Postconditions (Body_Id);
8340 ----------------------------
8341 -- Reference_Body_Formals --
8342 ----------------------------
8344 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8349 if Error_Posted (Spec) then
8353 -- Iterate over both lists. They may be of different lengths if the two
8354 -- specs are not conformant.
8356 Fs := First_Formal (Spec);
8357 Fb := First_Formal (Bod);
8358 while Present (Fs) and then Present (Fb) loop
8359 Generate_Reference (Fs, Fb, 'b');
8362 Style.Check_Identifier (Fb, Fs);
8365 Set_Spec_Entity (Fb, Fs);
8366 Set_Referenced (Fs, False);
8370 end Reference_Body_Formals;
8372 -------------------------
8373 -- Set_Actual_Subtypes --
8374 -------------------------
8376 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8377 Loc : constant Source_Ptr := Sloc (N);
8381 First_Stmt : Node_Id := Empty;
8382 AS_Needed : Boolean;
8385 -- If this is an empty initialization procedure, no need to create
8386 -- actual subtypes (small optimization).
8388 if Ekind (Subp) = E_Procedure
8389 and then Is_Null_Init_Proc (Subp)
8394 Formal := First_Formal (Subp);
8395 while Present (Formal) loop
8396 T := Etype (Formal);
8398 -- We never need an actual subtype for a constrained formal
8400 if Is_Constrained (T) then
8403 -- If we have unknown discriminants, then we do not need an actual
8404 -- subtype, or more accurately we cannot figure it out! Note that
8405 -- all class-wide types have unknown discriminants.
8407 elsif Has_Unknown_Discriminants (T) then
8410 -- At this stage we have an unconstrained type that may need an
8411 -- actual subtype. For sure the actual subtype is needed if we have
8412 -- an unconstrained array type.
8414 elsif Is_Array_Type (T) then
8417 -- The only other case needing an actual subtype is an unconstrained
8418 -- record type which is an IN parameter (we cannot generate actual
8419 -- subtypes for the OUT or IN OUT case, since an assignment can
8420 -- change the discriminant values. However we exclude the case of
8421 -- initialization procedures, since discriminants are handled very
8422 -- specially in this context, see the section entitled "Handling of
8423 -- Discriminants" in Einfo.
8425 -- We also exclude the case of Discrim_SO_Functions (functions used
8426 -- in front end layout mode for size/offset values), since in such
8427 -- functions only discriminants are referenced, and not only are such
8428 -- subtypes not needed, but they cannot always be generated, because
8429 -- of order of elaboration issues.
8431 elsif Is_Record_Type (T)
8432 and then Ekind (Formal) = E_In_Parameter
8433 and then Chars (Formal) /= Name_uInit
8434 and then not Is_Unchecked_Union (T)
8435 and then not Is_Discrim_SO_Function (Subp)
8439 -- All other cases do not need an actual subtype
8445 -- Generate actual subtypes for unconstrained arrays and
8446 -- unconstrained discriminated records.
8449 if Nkind (N) = N_Accept_Statement then
8451 -- If expansion is active, The formal is replaced by a local
8452 -- variable that renames the corresponding entry of the
8453 -- parameter block, and it is this local variable that may
8454 -- require an actual subtype.
8456 if Expander_Active then
8457 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8459 Decl := Build_Actual_Subtype (T, Formal);
8462 if Present (Handled_Statement_Sequence (N)) then
8464 First (Statements (Handled_Statement_Sequence (N)));
8465 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8466 Mark_Rewrite_Insertion (Decl);
8468 -- If the accept statement has no body, there will be no
8469 -- reference to the actuals, so no need to compute actual
8476 Decl := Build_Actual_Subtype (T, Formal);
8477 Prepend (Decl, Declarations (N));
8478 Mark_Rewrite_Insertion (Decl);
8481 -- The declaration uses the bounds of an existing object, and
8482 -- therefore needs no constraint checks.
8484 Analyze (Decl, Suppress => All_Checks);
8486 -- We need to freeze manually the generated type when it is
8487 -- inserted anywhere else than in a declarative part.
8489 if Present (First_Stmt) then
8490 Insert_List_Before_And_Analyze (First_Stmt,
8491 Freeze_Entity (Defining_Identifier (Decl), Loc));
8494 if Nkind (N) = N_Accept_Statement
8495 and then Expander_Active
8497 Set_Actual_Subtype (Renamed_Object (Formal),
8498 Defining_Identifier (Decl));
8500 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8504 Next_Formal (Formal);
8506 end Set_Actual_Subtypes;
8508 ---------------------
8509 -- Set_Formal_Mode --
8510 ---------------------
8512 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8513 Spec : constant Node_Id := Parent (Formal_Id);
8516 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8517 -- since we ensure that corresponding actuals are always valid at the
8518 -- point of the call.
8520 if Out_Present (Spec) then
8521 if Ekind (Scope (Formal_Id)) = E_Function
8522 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8524 Error_Msg_N ("functions can only have IN parameters", Spec);
8525 Set_Ekind (Formal_Id, E_In_Parameter);
8527 elsif In_Present (Spec) then
8528 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8531 Set_Ekind (Formal_Id, E_Out_Parameter);
8532 Set_Never_Set_In_Source (Formal_Id, True);
8533 Set_Is_True_Constant (Formal_Id, False);
8534 Set_Current_Value (Formal_Id, Empty);
8538 Set_Ekind (Formal_Id, E_In_Parameter);
8541 -- Set Is_Known_Non_Null for access parameters since the language
8542 -- guarantees that access parameters are always non-null. We also set
8543 -- Can_Never_Be_Null, since there is no way to change the value.
8545 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8547 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8548 -- null; In Ada 2005, only if then null_exclusion is explicit.
8550 if Ada_Version < Ada_05
8551 or else Can_Never_Be_Null (Etype (Formal_Id))
8553 Set_Is_Known_Non_Null (Formal_Id);
8554 Set_Can_Never_Be_Null (Formal_Id);
8557 -- Ada 2005 (AI-231): Null-exclusion access subtype
8559 elsif Is_Access_Type (Etype (Formal_Id))
8560 and then Can_Never_Be_Null (Etype (Formal_Id))
8562 Set_Is_Known_Non_Null (Formal_Id);
8565 Set_Mechanism (Formal_Id, Default_Mechanism);
8566 Set_Formal_Validity (Formal_Id);
8567 end Set_Formal_Mode;
8569 -------------------------
8570 -- Set_Formal_Validity --
8571 -------------------------
8573 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8575 -- If no validity checking, then we cannot assume anything about the
8576 -- validity of parameters, since we do not know there is any checking
8577 -- of the validity on the call side.
8579 if not Validity_Checks_On then
8582 -- If validity checking for parameters is enabled, this means we are
8583 -- not supposed to make any assumptions about argument values.
8585 elsif Validity_Check_Parameters then
8588 -- If we are checking in parameters, we will assume that the caller is
8589 -- also checking parameters, so we can assume the parameter is valid.
8591 elsif Ekind (Formal_Id) = E_In_Parameter
8592 and then Validity_Check_In_Params
8594 Set_Is_Known_Valid (Formal_Id, True);
8596 -- Similar treatment for IN OUT parameters
8598 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8599 and then Validity_Check_In_Out_Params
8601 Set_Is_Known_Valid (Formal_Id, True);
8603 end Set_Formal_Validity;
8605 ------------------------
8606 -- Subtype_Conformant --
8607 ------------------------
8609 function Subtype_Conformant
8610 (New_Id : Entity_Id;
8612 Skip_Controlling_Formals : Boolean := False) return Boolean
8616 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8617 Skip_Controlling_Formals => Skip_Controlling_Formals);
8619 end Subtype_Conformant;
8621 ---------------------
8622 -- Type_Conformant --
8623 ---------------------
8625 function Type_Conformant
8626 (New_Id : Entity_Id;
8628 Skip_Controlling_Formals : Boolean := False) return Boolean
8632 May_Hide_Profile := False;
8635 (New_Id, Old_Id, Type_Conformant, False, Result,
8636 Skip_Controlling_Formals => Skip_Controlling_Formals);
8638 end Type_Conformant;
8640 -------------------------------
8641 -- Valid_Operator_Definition --
8642 -------------------------------
8644 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8647 Id : constant Name_Id := Chars (Designator);
8651 F := First_Formal (Designator);
8652 while Present (F) loop
8655 if Present (Default_Value (F)) then
8657 ("default values not allowed for operator parameters",
8664 -- Verify that user-defined operators have proper number of arguments
8665 -- First case of operators which can only be unary
8668 or else Id = Name_Op_Abs
8672 -- Case of operators which can be unary or binary
8674 elsif Id = Name_Op_Add
8675 or Id = Name_Op_Subtract
8677 N_OK := (N in 1 .. 2);
8679 -- All other operators can only be binary
8687 ("incorrect number of arguments for operator", Designator);
8691 and then Base_Type (Etype (Designator)) = Standard_Boolean
8692 and then not Is_Intrinsic_Subprogram (Designator)
8695 ("explicit definition of inequality not allowed", Designator);
8697 end Valid_Operator_Definition;