1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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. This is split out so
114 -- that we can use RETURN but not skip the debug output at the end.
116 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
117 -- Analyze a generic subprogram body. N is the body to be analyzed, and
118 -- Gen_Id is the defining entity Id for the corresponding spec.
120 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
121 -- If a subprogram has pragma Inline and inlining is active, use generic
122 -- machinery to build an unexpanded body for the subprogram. This body is
123 -- subsequently used for inline expansions at call sites. If subprogram can
124 -- be inlined (depending on size and nature of local declarations) this
125 -- function returns true. Otherwise subprogram body is treated normally.
126 -- If proper warnings are enabled and the subprogram contains a construct
127 -- that cannot be inlined, the offending construct is flagged accordingly.
129 procedure Check_Conformance
132 Ctype : Conformance_Type;
134 Conforms : out Boolean;
135 Err_Loc : Node_Id := Empty;
136 Get_Inst : Boolean := False;
137 Skip_Controlling_Formals : Boolean := False);
138 -- Given two entities, this procedure checks that the profiles associated
139 -- with these entities meet the conformance criterion given by the third
140 -- parameter. If they conform, Conforms is set True and control returns
141 -- to the caller. If they do not conform, Conforms is set to False, and
142 -- in addition, if Errmsg is True on the call, proper messages are output
143 -- to complain about the conformance failure. If Err_Loc is non_Empty
144 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
145 -- error messages are placed on the appropriate part of the construct
146 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
147 -- against a formal access-to-subprogram type so Get_Instance_Of must
150 procedure Check_Subprogram_Order (N : Node_Id);
151 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
152 -- the alpha ordering rule for N if this ordering requirement applicable.
154 procedure Check_Returns
158 Proc : Entity_Id := Empty);
159 -- Called to check for missing return statements in a function body, or for
160 -- returns present in a procedure body which has No_Return set. HSS is the
161 -- handled statement sequence for the subprogram body. This procedure
162 -- checks all flow paths to make sure they either have return (Mode = 'F',
163 -- used for functions) or do not have a return (Mode = 'P', used for
164 -- No_Return procedures). The flag Err is set if there are any control
165 -- paths not explicitly terminated by a return in the function case, and is
166 -- True otherwise. Proc is the entity for the procedure case and is used
167 -- in posting the warning message.
169 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
170 -- In Ada 2012, a primitive equality operator on an untagged record type
171 -- must appear before the type is frozen, and have the same visibility as
172 -- that of the type. This procedure checks that this rule is met, and
173 -- otherwise emits an error on the subprogram declaration and a warning
174 -- on the earlier freeze point if it is easy to locate.
176 procedure Enter_Overloaded_Entity (S : Entity_Id);
177 -- This procedure makes S, a new overloaded entity, into the first visible
178 -- entity with that name.
180 procedure Install_Entity (E : Entity_Id);
181 -- Make single entity visible. Used for generic formals as well
183 function Is_Non_Overriding_Operation
185 New_E : Entity_Id) return Boolean;
186 -- Enforce the rule given in 12.3(18): a private operation in an instance
187 -- overrides an inherited operation only if the corresponding operation
188 -- was overriding in the generic. This can happen for primitive operations
189 -- of types derived (in the generic unit) from formal private or formal
192 procedure Make_Inequality_Operator (S : Entity_Id);
193 -- Create the declaration for an inequality operator that is implicitly
194 -- created by a user-defined equality operator that yields a boolean.
196 procedure May_Need_Actuals (Fun : Entity_Id);
197 -- Flag functions that can be called without parameters, i.e. those that
198 -- have no parameters, or those for which defaults exist for all parameters
200 procedure Process_PPCs
203 Body_Id : Entity_Id);
204 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
205 -- conditions for the body and assembling and inserting the _postconditions
206 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
207 -- the entities for the body and separate spec (if there is no separate
208 -- spec, Spec_Id is Empty).
210 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
211 -- Formal_Id is an formal parameter entity. This procedure deals with
212 -- setting the proper validity status for this entity, which depends on
213 -- the kind of parameter and the validity checking mode.
215 ------------------------------
216 -- Analyze_Return_Statement --
217 ------------------------------
219 procedure Analyze_Return_Statement (N : Node_Id) is
221 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
222 N_Extended_Return_Statement));
224 Returns_Object : constant Boolean :=
225 Nkind (N) = N_Extended_Return_Statement
227 (Nkind (N) = N_Simple_Return_Statement
228 and then Present (Expression (N)));
229 -- True if we're returning something; that is, "return <expression>;"
230 -- or "return Result : T [:= ...]". False for "return;". Used for error
231 -- checking: If Returns_Object is True, N should apply to a function
232 -- body; otherwise N should apply to a procedure body, entry body,
233 -- accept statement, or extended return statement.
235 function Find_What_It_Applies_To return Entity_Id;
236 -- Find the entity representing the innermost enclosing body, accept
237 -- statement, or extended return statement. If the result is a callable
238 -- construct or extended return statement, then this will be the value
239 -- of the Return_Applies_To attribute. Otherwise, the program is
240 -- illegal. See RM-6.5(4/2).
242 -----------------------------
243 -- Find_What_It_Applies_To --
244 -----------------------------
246 function Find_What_It_Applies_To return Entity_Id is
247 Result : Entity_Id := Empty;
250 -- Loop outward through the Scope_Stack, skipping blocks and loops
252 for J in reverse 0 .. Scope_Stack.Last loop
253 Result := Scope_Stack.Table (J).Entity;
254 exit when Ekind (Result) /= E_Block and then
255 Ekind (Result) /= E_Loop;
258 pragma Assert (Present (Result));
260 end Find_What_It_Applies_To;
262 -- Local declarations
264 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
265 Kind : constant Entity_Kind := Ekind (Scope_Id);
266 Loc : constant Source_Ptr := Sloc (N);
267 Stm_Entity : constant Entity_Id :=
269 (E_Return_Statement, Current_Scope, Loc, 'R');
271 -- Start of processing for Analyze_Return_Statement
274 Set_Return_Statement_Entity (N, Stm_Entity);
276 Set_Etype (Stm_Entity, Standard_Void_Type);
277 Set_Return_Applies_To (Stm_Entity, Scope_Id);
279 -- Place Return entity on scope stack, to simplify enforcement of 6.5
280 -- (4/2): an inner return statement will apply to this extended return.
282 if Nkind (N) = N_Extended_Return_Statement then
283 Push_Scope (Stm_Entity);
286 -- Check that pragma No_Return is obeyed. Don't complain about the
287 -- implicitly-generated return that is placed at the end.
289 if No_Return (Scope_Id) and then Comes_From_Source (N) then
290 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
293 -- Warn on any unassigned OUT parameters if in procedure
295 if Ekind (Scope_Id) = E_Procedure then
296 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
299 -- Check that functions return objects, and other things do not
301 if Kind = E_Function or else Kind = E_Generic_Function then
302 if not Returns_Object then
303 Error_Msg_N ("missing expression in return from function", N);
306 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
307 if Returns_Object then
308 Error_Msg_N ("procedure cannot return value (use function)", N);
311 elsif Kind = E_Entry or else Kind = E_Entry_Family then
312 if Returns_Object then
313 if Is_Protected_Type (Scope (Scope_Id)) then
314 Error_Msg_N ("entry body cannot return value", N);
316 Error_Msg_N ("accept statement cannot return value", N);
320 elsif Kind = E_Return_Statement then
322 -- We are nested within another return statement, which must be an
323 -- extended_return_statement.
325 if Returns_Object then
327 ("extended_return_statement cannot return value; " &
328 "use `""RETURN;""`", N);
332 Error_Msg_N ("illegal context for return statement", N);
335 if Kind = E_Function or else Kind = E_Generic_Function then
336 Analyze_Function_Return (N);
339 if Nkind (N) = N_Extended_Return_Statement then
343 Kill_Current_Values (Last_Assignment_Only => True);
344 Check_Unreachable_Code (N);
345 end Analyze_Return_Statement;
347 ---------------------------------------------
348 -- Analyze_Abstract_Subprogram_Declaration --
349 ---------------------------------------------
351 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
352 Designator : constant Entity_Id :=
353 Analyze_Subprogram_Specification (Specification (N));
354 Scop : constant Entity_Id := Current_Scope;
357 Generate_Definition (Designator);
358 Set_Is_Abstract_Subprogram (Designator);
359 New_Overloaded_Entity (Designator);
360 Check_Delayed_Subprogram (Designator);
362 Set_Categorization_From_Scope (Designator, Scop);
364 if Ekind (Scope (Designator)) = E_Protected_Type then
366 ("abstract subprogram not allowed in protected type", N);
368 -- Issue a warning if the abstract subprogram is neither a dispatching
369 -- operation nor an operation that overrides an inherited subprogram or
370 -- predefined operator, since this most likely indicates a mistake.
372 elsif Warn_On_Redundant_Constructs
373 and then not Is_Dispatching_Operation (Designator)
374 and then not Is_Overriding_Operation (Designator)
375 and then (not Is_Operator_Symbol_Name (Chars (Designator))
376 or else Scop /= Scope (Etype (First_Formal (Designator))))
379 ("?abstract subprogram is not dispatching or overriding", N);
382 Generate_Reference_To_Formals (Designator);
383 Check_Eliminated (Designator);
384 end Analyze_Abstract_Subprogram_Declaration;
386 ----------------------------------------
387 -- Analyze_Extended_Return_Statement --
388 ----------------------------------------
390 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
392 Analyze_Return_Statement (N);
393 end Analyze_Extended_Return_Statement;
395 ----------------------------
396 -- Analyze_Function_Call --
397 ----------------------------
399 procedure Analyze_Function_Call (N : Node_Id) is
400 P : constant Node_Id := Name (N);
401 L : constant List_Id := Parameter_Associations (N);
407 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
408 -- as B (A, X). If the rewriting is successful, the call has been
409 -- analyzed and we just return.
411 if Nkind (P) = N_Selected_Component
412 and then Name (N) /= P
413 and then Is_Rewrite_Substitution (N)
414 and then Present (Etype (N))
419 -- If error analyzing name, then set Any_Type as result type and return
421 if Etype (P) = Any_Type then
422 Set_Etype (N, Any_Type);
426 -- Otherwise analyze the parameters
430 while Present (Actual) loop
432 Check_Parameterless_Call (Actual);
438 end Analyze_Function_Call;
440 -----------------------------
441 -- Analyze_Function_Return --
442 -----------------------------
444 procedure Analyze_Function_Return (N : Node_Id) is
445 Loc : constant Source_Ptr := Sloc (N);
446 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
447 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
449 R_Type : constant Entity_Id := Etype (Scope_Id);
450 -- Function result subtype
452 procedure Check_Limited_Return (Expr : Node_Id);
453 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
454 -- limited types. Used only for simple return statements.
455 -- Expr is the expression returned.
457 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
458 -- Check that the return_subtype_indication properly matches the result
459 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
461 --------------------------
462 -- Check_Limited_Return --
463 --------------------------
465 procedure Check_Limited_Return (Expr : Node_Id) is
467 -- Ada 2005 (AI-318-02): Return-by-reference types have been
468 -- removed and replaced by anonymous access results. This is an
469 -- incompatibility with Ada 95. Not clear whether this should be
470 -- enforced yet or perhaps controllable with special switch. ???
472 if Is_Limited_Type (R_Type)
473 and then Comes_From_Source (N)
474 and then not In_Instance_Body
475 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
479 if Ada_Version >= Ada_2005
480 and then not Debug_Flag_Dot_L
481 and then not GNAT_Mode
484 ("(Ada 2005) cannot copy object of a limited type " &
485 "(RM-2005 6.5(5.5/2))", Expr);
487 if Is_Immutably_Limited_Type (R_Type) then
489 ("\return by reference not permitted in Ada 2005", Expr);
492 -- Warn in Ada 95 mode, to give folks a heads up about this
495 -- In GNAT mode, this is just a warning, to allow it to be
496 -- evilly turned off. Otherwise it is a real error.
498 -- In a generic context, simplify the warning because it makes
499 -- no sense to discuss pass-by-reference or copy.
501 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
502 if Inside_A_Generic then
504 ("return of limited object not permitted in Ada2005 "
505 & "(RM-2005 6.5(5.5/2))?", Expr);
507 elsif Is_Immutably_Limited_Type (R_Type) then
509 ("return by reference not permitted in Ada 2005 "
510 & "(RM-2005 6.5(5.5/2))?", Expr);
513 ("cannot copy object of a limited type in Ada 2005 "
514 & "(RM-2005 6.5(5.5/2))?", Expr);
517 -- Ada 95 mode, compatibility warnings disabled
520 return; -- skip continuation messages below
523 if not Inside_A_Generic then
525 ("\consider switching to return of access type", Expr);
526 Explain_Limited_Type (R_Type, Expr);
529 end Check_Limited_Return;
531 -------------------------------------
532 -- Check_Return_Subtype_Indication --
533 -------------------------------------
535 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
536 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
538 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
539 -- Subtype given in the extended return statement (must match R_Type)
541 Subtype_Ind : constant Node_Id :=
542 Object_Definition (Original_Node (Obj_Decl));
544 R_Type_Is_Anon_Access :
546 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
548 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
550 Ekind (R_Type) = E_Anonymous_Access_Type;
551 -- True if return type of the function is an anonymous access type
552 -- Can't we make Is_Anonymous_Access_Type in einfo ???
554 R_Stm_Type_Is_Anon_Access :
556 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
558 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
560 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
561 -- True if type of the return object is an anonymous access type
564 -- First, avoid cascaded errors
566 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
570 -- "return access T" case; check that the return statement also has
571 -- "access T", and that the subtypes statically match:
572 -- if this is an access to subprogram the signatures must match.
574 if R_Type_Is_Anon_Access then
575 if R_Stm_Type_Is_Anon_Access then
577 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
579 if Base_Type (Designated_Type (R_Stm_Type)) /=
580 Base_Type (Designated_Type (R_Type))
581 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
584 ("subtype must statically match function result subtype",
585 Subtype_Mark (Subtype_Ind));
589 -- For two anonymous access to subprogram types, the
590 -- types themselves must be type conformant.
592 if not Conforming_Types
593 (R_Stm_Type, R_Type, Fully_Conformant)
596 ("subtype must statically match function result subtype",
602 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
605 -- Subtype indication case: check that the return object's type is
606 -- covered by the result type, and that the subtypes statically match
607 -- when the result subtype is constrained. Also handle record types
608 -- with unknown discriminants for which we have built the underlying
609 -- record view. Coverage is needed to allow specific-type return
610 -- objects when the result type is class-wide (see AI05-32).
612 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
613 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
617 Underlying_Record_View (Base_Type (R_Stm_Type))))
619 -- A null exclusion may be present on the return type, on the
620 -- function specification, on the object declaration or on the
623 if Is_Access_Type (R_Type)
625 (Can_Never_Be_Null (R_Type)
626 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
627 Can_Never_Be_Null (R_Stm_Type)
630 ("subtype must statically match function result subtype",
634 -- AI05-103: for elementary types, subtypes must statically match
636 if Is_Constrained (R_Type)
637 or else Is_Access_Type (R_Type)
639 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
641 ("subtype must statically match function result subtype",
646 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
647 and then Is_Null_Extension (Base_Type (R_Type))
653 ("wrong type for return_subtype_indication", Subtype_Ind);
655 end Check_Return_Subtype_Indication;
657 ---------------------
658 -- Local Variables --
659 ---------------------
663 -- Start of processing for Analyze_Function_Return
666 Set_Return_Present (Scope_Id);
668 if Nkind (N) = N_Simple_Return_Statement then
669 Expr := Expression (N);
670 Analyze_And_Resolve (Expr, R_Type);
671 Check_Limited_Return (Expr);
674 -- Analyze parts specific to extended_return_statement:
677 Obj_Decl : constant Node_Id :=
678 Last (Return_Object_Declarations (N));
680 HSS : constant Node_Id := Handled_Statement_Sequence (N);
683 Expr := Expression (Obj_Decl);
685 -- Note: The check for OK_For_Limited_Init will happen in
686 -- Analyze_Object_Declaration; we treat it as a normal
687 -- object declaration.
689 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
692 Check_Return_Subtype_Indication (Obj_Decl);
694 if Present (HSS) then
697 if Present (Exception_Handlers (HSS)) then
699 -- ???Has_Nested_Block_With_Handler needs to be set.
700 -- Probably by creating an actual N_Block_Statement.
701 -- Probably in Expand.
707 -- Mark the return object as referenced, since the return is an
708 -- implicit reference of the object.
710 Set_Referenced (Defining_Identifier (Obj_Decl));
712 Check_References (Stm_Entity);
716 -- Case of Expr present
720 -- Defend against previous errors
722 and then Nkind (Expr) /= N_Empty
723 and then Present (Etype (Expr))
725 -- Apply constraint check. Note that this is done before the implicit
726 -- conversion of the expression done for anonymous access types to
727 -- ensure correct generation of the null-excluding check associated
728 -- with null-excluding expressions found in return statements.
730 Apply_Constraint_Check (Expr, R_Type);
732 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
733 -- type, apply an implicit conversion of the expression to that type
734 -- to force appropriate static and run-time accessibility checks.
736 if Ada_Version >= Ada_2005
737 and then Ekind (R_Type) = E_Anonymous_Access_Type
739 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
740 Analyze_And_Resolve (Expr, R_Type);
743 -- If the result type is class-wide, then check that the return
744 -- expression's type is not declared at a deeper level than the
745 -- function (RM05-6.5(5.6/2)).
747 if Ada_Version >= Ada_2005
748 and then Is_Class_Wide_Type (R_Type)
750 if Type_Access_Level (Etype (Expr)) >
751 Subprogram_Access_Level (Scope_Id)
754 ("level of return expression type is deeper than " &
755 "class-wide function!", Expr);
759 -- Check incorrect use of dynamically tagged expression
761 if Is_Tagged_Type (R_Type) then
762 Check_Dynamically_Tagged_Expression
768 -- ??? A real run-time accessibility check is needed in cases
769 -- involving dereferences of access parameters. For now we just
770 -- check the static cases.
772 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
773 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
774 and then Object_Access_Level (Expr) >
775 Subprogram_Access_Level (Scope_Id)
778 -- Suppress the message in a generic, where the rewriting
781 if Inside_A_Generic then
786 Make_Raise_Program_Error (Loc,
787 Reason => PE_Accessibility_Check_Failed));
791 ("cannot return a local value by reference?", N);
793 ("\& will be raised at run time?",
794 N, Standard_Program_Error);
799 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
800 and then Null_Exclusion_Present (Parent (Scope_Id))
802 Apply_Compile_Time_Constraint_Error
804 Msg => "(Ada 2005) null not allowed for "
805 & "null-excluding return?",
806 Reason => CE_Null_Not_Allowed);
809 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
810 -- (Disabled for now)
812 -- Check_Order_Dependence;
814 end Analyze_Function_Return;
816 -------------------------------------
817 -- Analyze_Generic_Subprogram_Body --
818 -------------------------------------
820 procedure Analyze_Generic_Subprogram_Body
824 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
825 Kind : constant Entity_Kind := Ekind (Gen_Id);
831 -- Copy body and disable expansion while analyzing the generic For a
832 -- stub, do not copy the stub (which would load the proper body), this
833 -- will be done when the proper body is analyzed.
835 if Nkind (N) /= N_Subprogram_Body_Stub then
836 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
841 Spec := Specification (N);
843 -- Within the body of the generic, the subprogram is callable, and
844 -- behaves like the corresponding non-generic unit.
846 Body_Id := Defining_Entity (Spec);
848 if Kind = E_Generic_Procedure
849 and then Nkind (Spec) /= N_Procedure_Specification
851 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
854 elsif Kind = E_Generic_Function
855 and then Nkind (Spec) /= N_Function_Specification
857 Error_Msg_N ("invalid body for generic function ", Body_Id);
861 Set_Corresponding_Body (Gen_Decl, Body_Id);
863 if Has_Completion (Gen_Id)
864 and then Nkind (Parent (N)) /= N_Subunit
866 Error_Msg_N ("duplicate generic body", N);
869 Set_Has_Completion (Gen_Id);
872 if Nkind (N) = N_Subprogram_Body_Stub then
873 Set_Ekind (Defining_Entity (Specification (N)), Kind);
875 Set_Corresponding_Spec (N, Gen_Id);
878 if Nkind (Parent (N)) = N_Compilation_Unit then
879 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
882 -- Make generic parameters immediately visible in the body. They are
883 -- needed to process the formals declarations. Then make the formals
884 -- visible in a separate step.
890 First_Ent : Entity_Id;
893 First_Ent := First_Entity (Gen_Id);
896 while Present (E) and then not Is_Formal (E) loop
901 Set_Use (Generic_Formal_Declarations (Gen_Decl));
903 -- Now generic formals are visible, and the specification can be
904 -- analyzed, for subsequent conformance check.
906 Body_Id := Analyze_Subprogram_Specification (Spec);
908 -- Make formal parameters visible
912 -- E is the first formal parameter, we loop through the formals
913 -- installing them so that they will be visible.
915 Set_First_Entity (Gen_Id, E);
916 while Present (E) loop
922 -- Visible generic entity is callable within its own body
924 Set_Ekind (Gen_Id, Ekind (Body_Id));
925 Set_Ekind (Body_Id, E_Subprogram_Body);
926 Set_Convention (Body_Id, Convention (Gen_Id));
927 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
928 Set_Scope (Body_Id, Scope (Gen_Id));
929 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
931 if Nkind (N) = N_Subprogram_Body_Stub then
933 -- No body to analyze, so restore state of generic unit
935 Set_Ekind (Gen_Id, Kind);
936 Set_Ekind (Body_Id, Kind);
938 if Present (First_Ent) then
939 Set_First_Entity (Gen_Id, First_Ent);
946 -- If this is a compilation unit, it must be made visible explicitly,
947 -- because the compilation of the declaration, unlike other library
948 -- unit declarations, does not. If it is not a unit, the following
949 -- is redundant but harmless.
951 Set_Is_Immediately_Visible (Gen_Id);
952 Reference_Body_Formals (Gen_Id, Body_Id);
954 if Is_Child_Unit (Gen_Id) then
955 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
958 Set_Actual_Subtypes (N, Current_Scope);
959 Process_PPCs (N, Gen_Id, Body_Id);
961 -- If the generic unit carries pre- or post-conditions, copy them
962 -- to the original generic tree, so that they are properly added
963 -- to any instantiation.
966 Orig : constant Node_Id := Original_Node (N);
970 Cond := First (Declarations (N));
971 while Present (Cond) loop
972 if Nkind (Cond) = N_Pragma
973 and then Pragma_Name (Cond) = Name_Check
975 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
977 elsif Nkind (Cond) = N_Pragma
978 and then Pragma_Name (Cond) = Name_Postcondition
980 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
981 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
990 Analyze_Declarations (Declarations (N));
992 Analyze (Handled_Statement_Sequence (N));
994 Save_Global_References (Original_Node (N));
996 -- Prior to exiting the scope, include generic formals again (if any
997 -- are present) in the set of local entities.
999 if Present (First_Ent) then
1000 Set_First_Entity (Gen_Id, First_Ent);
1003 Check_References (Gen_Id);
1006 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1008 Check_Subprogram_Order (N);
1010 -- Outside of its body, unit is generic again
1012 Set_Ekind (Gen_Id, Kind);
1013 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1016 Style.Check_Identifier (Body_Id, Gen_Id);
1020 end Analyze_Generic_Subprogram_Body;
1022 -----------------------------
1023 -- Analyze_Operator_Symbol --
1024 -----------------------------
1026 -- An operator symbol such as "+" or "and" may appear in context where the
1027 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1028 -- is just a string, as in (conjunction = "or"). In these cases the parser
1029 -- generates this node, and the semantics does the disambiguation. Other
1030 -- such case are actuals in an instantiation, the generic unit in an
1031 -- instantiation, and pragma arguments.
1033 procedure Analyze_Operator_Symbol (N : Node_Id) is
1034 Par : constant Node_Id := Parent (N);
1037 if (Nkind (Par) = N_Function_Call
1038 and then N = Name (Par))
1039 or else Nkind (Par) = N_Function_Instantiation
1040 or else (Nkind (Par) = N_Indexed_Component
1041 and then N = Prefix (Par))
1042 or else (Nkind (Par) = N_Pragma_Argument_Association
1043 and then not Is_Pragma_String_Literal (Par))
1044 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1045 or else (Nkind (Par) = N_Attribute_Reference
1046 and then Attribute_Name (Par) /= Name_Value)
1048 Find_Direct_Name (N);
1051 Change_Operator_Symbol_To_String_Literal (N);
1054 end Analyze_Operator_Symbol;
1056 -----------------------------------
1057 -- Analyze_Parameter_Association --
1058 -----------------------------------
1060 procedure Analyze_Parameter_Association (N : Node_Id) is
1062 Analyze (Explicit_Actual_Parameter (N));
1063 end Analyze_Parameter_Association;
1065 --------------------------------------
1066 -- Analyze_Parameterized_Expression --
1067 --------------------------------------
1069 procedure Analyze_Parameterized_Expression (N : Node_Id) is
1070 Loc : constant Source_Ptr := Sloc (N);
1071 LocX : constant Source_Ptr := Sloc (Expression (N));
1074 -- This is one of the occasions on which we write things during semantic
1075 -- analysis. Transform the parameterized expression into an equivalent
1076 -- subprogram body, and then analyze that.
1079 Make_Subprogram_Body (Loc,
1080 Specification => Specification (N),
1081 Declarations => Empty_List,
1082 Handled_Statement_Sequence =>
1083 Make_Handled_Sequence_Of_Statements (LocX,
1084 Statements => New_List (
1085 Make_Simple_Return_Statement (LocX,
1086 Expression => Expression (N))))));
1088 end Analyze_Parameterized_Expression;
1090 ----------------------------
1091 -- Analyze_Procedure_Call --
1092 ----------------------------
1094 procedure Analyze_Procedure_Call (N : Node_Id) is
1095 Loc : constant Source_Ptr := Sloc (N);
1096 P : constant Node_Id := Name (N);
1097 Actuals : constant List_Id := Parameter_Associations (N);
1101 procedure Analyze_Call_And_Resolve;
1102 -- Do Analyze and Resolve calls for procedure call
1103 -- At end, check illegal order dependence.
1105 ------------------------------
1106 -- Analyze_Call_And_Resolve --
1107 ------------------------------
1109 procedure Analyze_Call_And_Resolve is
1111 if Nkind (N) = N_Procedure_Call_Statement then
1113 Resolve (N, Standard_Void_Type);
1115 -- Apply checks suggested by AI05-0144 (Disabled for now)
1117 -- Check_Order_Dependence;
1122 end Analyze_Call_And_Resolve;
1124 -- Start of processing for Analyze_Procedure_Call
1127 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1128 -- a procedure call or an entry call. The prefix may denote an access
1129 -- to subprogram type, in which case an implicit dereference applies.
1130 -- If the prefix is an indexed component (without implicit dereference)
1131 -- then the construct denotes a call to a member of an entire family.
1132 -- If the prefix is a simple name, it may still denote a call to a
1133 -- parameterless member of an entry family. Resolution of these various
1134 -- interpretations is delicate.
1138 -- If this is a call of the form Obj.Op, the call may have been
1139 -- analyzed and possibly rewritten into a block, in which case
1142 if Analyzed (N) then
1146 -- If there is an error analyzing the name (which may have been
1147 -- rewritten if the original call was in prefix notation) then error
1148 -- has been emitted already, mark node and return.
1151 or else Etype (Name (N)) = Any_Type
1153 Set_Etype (N, Any_Type);
1157 -- Otherwise analyze the parameters
1159 if Present (Actuals) then
1160 Actual := First (Actuals);
1162 while Present (Actual) loop
1164 Check_Parameterless_Call (Actual);
1169 -- Special processing for Elab_Spec and Elab_Body calls
1171 if Nkind (P) = N_Attribute_Reference
1172 and then (Attribute_Name (P) = Name_Elab_Spec
1173 or else Attribute_Name (P) = Name_Elab_Body)
1175 if Present (Actuals) then
1177 ("no parameters allowed for this call", First (Actuals));
1181 Set_Etype (N, Standard_Void_Type);
1184 elsif Is_Entity_Name (P)
1185 and then Is_Record_Type (Etype (Entity (P)))
1186 and then Remote_AST_I_Dereference (P)
1190 elsif Is_Entity_Name (P)
1191 and then Ekind (Entity (P)) /= E_Entry_Family
1193 if Is_Access_Type (Etype (P))
1194 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1195 and then No (Actuals)
1196 and then Comes_From_Source (N)
1198 Error_Msg_N ("missing explicit dereference in call", N);
1201 Analyze_Call_And_Resolve;
1203 -- If the prefix is the simple name of an entry family, this is
1204 -- a parameterless call from within the task body itself.
1206 elsif Is_Entity_Name (P)
1207 and then Nkind (P) = N_Identifier
1208 and then Ekind (Entity (P)) = E_Entry_Family
1209 and then Present (Actuals)
1210 and then No (Next (First (Actuals)))
1212 -- Can be call to parameterless entry family. What appears to be the
1213 -- sole argument is in fact the entry index. Rewrite prefix of node
1214 -- accordingly. Source representation is unchanged by this
1218 Make_Indexed_Component (Loc,
1220 Make_Selected_Component (Loc,
1221 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1222 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1223 Expressions => Actuals);
1224 Set_Name (N, New_N);
1225 Set_Etype (New_N, Standard_Void_Type);
1226 Set_Parameter_Associations (N, No_List);
1227 Analyze_Call_And_Resolve;
1229 elsif Nkind (P) = N_Explicit_Dereference then
1230 if Ekind (Etype (P)) = E_Subprogram_Type then
1231 Analyze_Call_And_Resolve;
1233 Error_Msg_N ("expect access to procedure in call", P);
1236 -- The name can be a selected component or an indexed component that
1237 -- yields an access to subprogram. Such a prefix is legal if the call
1238 -- has parameter associations.
1240 elsif Is_Access_Type (Etype (P))
1241 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1243 if Present (Actuals) then
1244 Analyze_Call_And_Resolve;
1246 Error_Msg_N ("missing explicit dereference in call ", N);
1249 -- If not an access to subprogram, then the prefix must resolve to the
1250 -- name of an entry, entry family, or protected operation.
1252 -- For the case of a simple entry call, P is a selected component where
1253 -- the prefix is the task and the selector name is the entry. A call to
1254 -- a protected procedure will have the same syntax. If the protected
1255 -- object contains overloaded operations, the entity may appear as a
1256 -- function, the context will select the operation whose type is Void.
1258 elsif Nkind (P) = N_Selected_Component
1259 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1261 Ekind (Entity (Selector_Name (P))) = E_Procedure
1263 Ekind (Entity (Selector_Name (P))) = E_Function)
1265 Analyze_Call_And_Resolve;
1267 elsif Nkind (P) = N_Selected_Component
1268 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1269 and then Present (Actuals)
1270 and then No (Next (First (Actuals)))
1272 -- Can be call to parameterless entry family. What appears to be the
1273 -- sole argument is in fact the entry index. Rewrite prefix of node
1274 -- accordingly. Source representation is unchanged by this
1278 Make_Indexed_Component (Loc,
1279 Prefix => New_Copy (P),
1280 Expressions => Actuals);
1281 Set_Name (N, New_N);
1282 Set_Etype (New_N, Standard_Void_Type);
1283 Set_Parameter_Associations (N, No_List);
1284 Analyze_Call_And_Resolve;
1286 -- For the case of a reference to an element of an entry family, P is
1287 -- an indexed component whose prefix is a selected component (task and
1288 -- entry family), and whose index is the entry family index.
1290 elsif Nkind (P) = N_Indexed_Component
1291 and then Nkind (Prefix (P)) = N_Selected_Component
1292 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1294 Analyze_Call_And_Resolve;
1296 -- If the prefix is the name of an entry family, it is a call from
1297 -- within the task body itself.
1299 elsif Nkind (P) = N_Indexed_Component
1300 and then Nkind (Prefix (P)) = N_Identifier
1301 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1304 Make_Selected_Component (Loc,
1305 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1306 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1307 Rewrite (Prefix (P), New_N);
1309 Analyze_Call_And_Resolve;
1311 -- Anything else is an error
1314 Error_Msg_N ("invalid procedure or entry call", N);
1316 end Analyze_Procedure_Call;
1318 -------------------------------------
1319 -- Analyze_Simple_Return_Statement --
1320 -------------------------------------
1322 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1324 if Present (Expression (N)) then
1325 Mark_Coextensions (N, Expression (N));
1328 Analyze_Return_Statement (N);
1329 end Analyze_Simple_Return_Statement;
1331 -------------------------
1332 -- Analyze_Return_Type --
1333 -------------------------
1335 procedure Analyze_Return_Type (N : Node_Id) is
1336 Designator : constant Entity_Id := Defining_Entity (N);
1337 Typ : Entity_Id := Empty;
1340 -- Normal case where result definition does not indicate an error
1342 if Result_Definition (N) /= Error then
1343 if Nkind (Result_Definition (N)) = N_Access_Definition then
1345 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1348 AD : constant Node_Id :=
1349 Access_To_Subprogram_Definition (Result_Definition (N));
1351 if Present (AD) and then Protected_Present (AD) then
1352 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1354 Typ := Access_Definition (N, Result_Definition (N));
1358 Set_Parent (Typ, Result_Definition (N));
1359 Set_Is_Local_Anonymous_Access (Typ);
1360 Set_Etype (Designator, Typ);
1362 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1364 Null_Exclusion_Static_Checks (N);
1366 -- Subtype_Mark case
1369 Find_Type (Result_Definition (N));
1370 Typ := Entity (Result_Definition (N));
1371 Set_Etype (Designator, Typ);
1373 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1375 Null_Exclusion_Static_Checks (N);
1377 -- If a null exclusion is imposed on the result type, then create
1378 -- a null-excluding itype (an access subtype) and use it as the
1379 -- function's Etype. Note that the null exclusion checks are done
1380 -- right before this, because they don't get applied to types that
1381 -- do not come from source.
1383 if Is_Access_Type (Typ)
1384 and then Null_Exclusion_Present (N)
1386 Set_Etype (Designator,
1387 Create_Null_Excluding_Itype
1390 Scope_Id => Scope (Current_Scope)));
1392 -- The new subtype must be elaborated before use because
1393 -- it is visible outside of the function. However its base
1394 -- type may not be frozen yet, so the reference that will
1395 -- force elaboration must be attached to the freezing of
1398 -- If the return specification appears on a proper body,
1399 -- the subtype will have been created already on the spec.
1401 if Is_Frozen (Typ) then
1402 if Nkind (Parent (N)) = N_Subprogram_Body
1403 and then Nkind (Parent (Parent (N))) = N_Subunit
1407 Build_Itype_Reference (Etype (Designator), Parent (N));
1411 Ensure_Freeze_Node (Typ);
1414 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1416 Set_Itype (IR, Etype (Designator));
1417 Append_Freeze_Actions (Typ, New_List (IR));
1422 Set_Etype (Designator, Typ);
1425 if Ekind (Typ) = E_Incomplete_Type
1426 and then Is_Value_Type (Typ)
1430 elsif Ekind (Typ) = E_Incomplete_Type
1431 or else (Is_Class_Wide_Type (Typ)
1433 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1436 ("invalid use of incomplete type&", Designator, Typ);
1440 -- Case where result definition does indicate an error
1443 Set_Etype (Designator, Any_Type);
1445 end Analyze_Return_Type;
1447 -----------------------------
1448 -- Analyze_Subprogram_Body --
1449 -----------------------------
1451 procedure Analyze_Subprogram_Body (N : Node_Id) is
1452 Loc : constant Source_Ptr := Sloc (N);
1453 Body_Spec : constant Node_Id := Specification (N);
1454 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1457 if Debug_Flag_C then
1458 Write_Str ("==> subprogram body ");
1459 Write_Name (Chars (Body_Id));
1460 Write_Str (" from ");
1461 Write_Location (Loc);
1466 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1468 -- The real work is split out into the helper, so it can do "return;"
1469 -- without skipping the debug output:
1471 Analyze_Subprogram_Body_Helper (N);
1473 if Debug_Flag_C then
1475 Write_Str ("<== subprogram body ");
1476 Write_Name (Chars (Body_Id));
1477 Write_Str (" from ");
1478 Write_Location (Loc);
1481 end Analyze_Subprogram_Body;
1483 ------------------------------------
1484 -- Analyze_Subprogram_Body_Helper --
1485 ------------------------------------
1487 -- This procedure is called for regular subprogram bodies, generic bodies,
1488 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1489 -- specification matters, and is used to create a proper declaration for
1490 -- the subprogram, or to perform conformance checks.
1492 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1493 Loc : constant Source_Ptr := Sloc (N);
1494 Body_Deleted : constant Boolean := False;
1495 Body_Spec : constant Node_Id := Specification (N);
1496 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1497 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1498 Conformant : Boolean;
1501 Prot_Typ : Entity_Id := Empty;
1502 Spec_Id : Entity_Id;
1503 Spec_Decl : Node_Id := Empty;
1505 Last_Real_Spec_Entity : Entity_Id := Empty;
1506 -- When we analyze a separate spec, the entity chain ends up containing
1507 -- the formals, as well as any itypes generated during analysis of the
1508 -- default expressions for parameters, or the arguments of associated
1509 -- precondition/postcondition pragmas (which are analyzed in the context
1510 -- of the spec since they have visibility on formals).
1512 -- These entities belong with the spec and not the body. However we do
1513 -- the analysis of the body in the context of the spec (again to obtain
1514 -- visibility to the formals), and all the entities generated during
1515 -- this analysis end up also chained to the entity chain of the spec.
1516 -- But they really belong to the body, and there is circuitry to move
1517 -- them from the spec to the body.
1519 -- However, when we do this move, we don't want to move the real spec
1520 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1521 -- variable points to the last real spec entity, so we only move those
1522 -- chained beyond that point. It is initialized to Empty to deal with
1523 -- the case where there is no separate spec.
1525 procedure Check_Anonymous_Return;
1526 -- Ada 2005: if a function returns an access type that denotes a task,
1527 -- or a type that contains tasks, we must create a master entity for
1528 -- the anonymous type, which typically will be used in an allocator
1529 -- in the body of the function.
1531 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1532 -- Look ahead to recognize a pragma that may appear after the body.
1533 -- If there is a previous spec, check that it appears in the same
1534 -- declarative part. If the pragma is Inline_Always, perform inlining
1535 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1536 -- If the body acts as a spec, and inlining is required, we create a
1537 -- subprogram declaration for it, in order to attach the body to inline.
1538 -- If pragma does not appear after the body, check whether there is
1539 -- an inline pragma before any local declarations.
1541 procedure Check_Missing_Return;
1542 -- Checks for a function with a no return statements, and also performs
1543 -- the warning checks implemented by Check_Returns.
1545 function Disambiguate_Spec return Entity_Id;
1546 -- When a primitive is declared between the private view and the full
1547 -- view of a concurrent type which implements an interface, a special
1548 -- mechanism is used to find the corresponding spec of the primitive
1551 function Is_Private_Concurrent_Primitive
1552 (Subp_Id : Entity_Id) return Boolean;
1553 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1554 -- type that implements an interface and has a private view.
1556 procedure Set_Trivial_Subprogram (N : Node_Id);
1557 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1558 -- subprogram whose body is being analyzed. N is the statement node
1559 -- causing the flag to be set, if the following statement is a return
1560 -- of an entity, we mark the entity as set in source to suppress any
1561 -- warning on the stylized use of function stubs with a dummy return.
1563 procedure Verify_Overriding_Indicator;
1564 -- If there was a previous spec, the entity has been entered in the
1565 -- current scope previously. If the body itself carries an overriding
1566 -- indicator, check that it is consistent with the known status of the
1569 ----------------------------
1570 -- Check_Anonymous_Return --
1571 ----------------------------
1573 procedure Check_Anonymous_Return is
1579 if Present (Spec_Id) then
1585 if Ekind (Scop) = E_Function
1586 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1587 and then not Is_Thunk (Scop)
1588 and then (Has_Task (Designated_Type (Etype (Scop)))
1590 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1592 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1593 and then Expander_Active
1595 -- Avoid cases with no tasking support
1597 and then RTE_Available (RE_Current_Master)
1598 and then not Restriction_Active (No_Task_Hierarchy)
1601 Make_Object_Declaration (Loc,
1602 Defining_Identifier =>
1603 Make_Defining_Identifier (Loc, Name_uMaster),
1604 Constant_Present => True,
1605 Object_Definition =>
1606 New_Reference_To (RTE (RE_Master_Id), Loc),
1608 Make_Explicit_Dereference (Loc,
1609 New_Reference_To (RTE (RE_Current_Master), Loc)));
1611 if Present (Declarations (N)) then
1612 Prepend (Decl, Declarations (N));
1614 Set_Declarations (N, New_List (Decl));
1617 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1618 Set_Has_Master_Entity (Scop);
1620 -- Now mark the containing scope as a task master
1623 while Nkind (Par) /= N_Compilation_Unit loop
1624 Par := Parent (Par);
1625 pragma Assert (Present (Par));
1627 -- If we fall off the top, we are at the outer level, and
1628 -- the environment task is our effective master, so nothing
1632 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1634 Set_Is_Task_Master (Par, True);
1639 end Check_Anonymous_Return;
1641 -------------------------
1642 -- Check_Inline_Pragma --
1643 -------------------------
1645 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1649 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1650 -- True when N is a pragma Inline or Inline_Always that applies
1651 -- to this subprogram.
1653 -----------------------
1654 -- Is_Inline_Pragma --
1655 -----------------------
1657 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1660 Nkind (N) = N_Pragma
1662 (Pragma_Name (N) = Name_Inline_Always
1665 and then Pragma_Name (N) = Name_Inline))
1668 (Expression (First (Pragma_Argument_Associations (N))))
1670 end Is_Inline_Pragma;
1672 -- Start of processing for Check_Inline_Pragma
1675 if not Expander_Active then
1679 if Is_List_Member (N)
1680 and then Present (Next (N))
1681 and then Is_Inline_Pragma (Next (N))
1685 elsif Nkind (N) /= N_Subprogram_Body_Stub
1686 and then Present (Declarations (N))
1687 and then Is_Inline_Pragma (First (Declarations (N)))
1689 Prag := First (Declarations (N));
1695 if Present (Prag) then
1696 if Present (Spec_Id) then
1697 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1702 -- Create a subprogram declaration, to make treatment uniform
1705 Subp : constant Entity_Id :=
1706 Make_Defining_Identifier (Loc, Chars (Body_Id));
1707 Decl : constant Node_Id :=
1708 Make_Subprogram_Declaration (Loc,
1710 New_Copy_Tree (Specification (N)));
1713 Set_Defining_Unit_Name (Specification (Decl), Subp);
1715 if Present (First_Formal (Body_Id)) then
1716 Plist := Copy_Parameter_List (Body_Id);
1717 Set_Parameter_Specifications
1718 (Specification (Decl), Plist);
1721 Insert_Before (N, Decl);
1724 Set_Has_Pragma_Inline (Subp);
1726 if Pragma_Name (Prag) = Name_Inline_Always then
1727 Set_Is_Inlined (Subp);
1728 Set_Has_Pragma_Inline_Always (Subp);
1735 end Check_Inline_Pragma;
1737 --------------------------
1738 -- Check_Missing_Return --
1739 --------------------------
1741 procedure Check_Missing_Return is
1743 Missing_Ret : Boolean;
1746 if Nkind (Body_Spec) = N_Function_Specification then
1747 if Present (Spec_Id) then
1753 if Return_Present (Id) then
1754 Check_Returns (HSS, 'F', Missing_Ret);
1757 Set_Has_Missing_Return (Id);
1760 elsif (Is_Generic_Subprogram (Id)
1761 or else not Is_Machine_Code_Subprogram (Id))
1762 and then not Body_Deleted
1764 Error_Msg_N ("missing RETURN statement in function body", N);
1767 -- If procedure with No_Return, check returns
1769 elsif Nkind (Body_Spec) = N_Procedure_Specification
1770 and then Present (Spec_Id)
1771 and then No_Return (Spec_Id)
1773 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1775 end Check_Missing_Return;
1777 -----------------------
1778 -- Disambiguate_Spec --
1779 -----------------------
1781 function Disambiguate_Spec return Entity_Id is
1782 Priv_Spec : Entity_Id;
1785 procedure Replace_Types (To_Corresponding : Boolean);
1786 -- Depending on the flag, replace the type of formal parameters of
1787 -- Body_Id if it is a concurrent type implementing interfaces with
1788 -- the corresponding record type or the other way around.
1790 procedure Replace_Types (To_Corresponding : Boolean) is
1792 Formal_Typ : Entity_Id;
1795 Formal := First_Formal (Body_Id);
1796 while Present (Formal) loop
1797 Formal_Typ := Etype (Formal);
1799 -- From concurrent type to corresponding record
1801 if To_Corresponding then
1802 if Is_Concurrent_Type (Formal_Typ)
1803 and then Present (Corresponding_Record_Type (Formal_Typ))
1804 and then Present (Interfaces (
1805 Corresponding_Record_Type (Formal_Typ)))
1808 Corresponding_Record_Type (Formal_Typ));
1811 -- From corresponding record to concurrent type
1814 if Is_Concurrent_Record_Type (Formal_Typ)
1815 and then Present (Interfaces (Formal_Typ))
1818 Corresponding_Concurrent_Type (Formal_Typ));
1822 Next_Formal (Formal);
1826 -- Start of processing for Disambiguate_Spec
1829 -- Try to retrieve the specification of the body as is. All error
1830 -- messages are suppressed because the body may not have a spec in
1831 -- its current state.
1833 Spec_N := Find_Corresponding_Spec (N, False);
1835 -- It is possible that this is the body of a primitive declared
1836 -- between a private and a full view of a concurrent type. The
1837 -- controlling parameter of the spec carries the concurrent type,
1838 -- not the corresponding record type as transformed by Analyze_
1839 -- Subprogram_Specification. In such cases, we undo the change
1840 -- made by the analysis of the specification and try to find the
1843 -- Note that wrappers already have their corresponding specs and
1844 -- bodies set during their creation, so if the candidate spec is
1845 -- a wrapper, then we definitely need to swap all types to their
1846 -- original concurrent status.
1849 or else Is_Primitive_Wrapper (Spec_N)
1851 -- Restore all references of corresponding record types to the
1852 -- original concurrent types.
1854 Replace_Types (To_Corresponding => False);
1855 Priv_Spec := Find_Corresponding_Spec (N, False);
1857 -- The current body truly belongs to a primitive declared between
1858 -- a private and a full view. We leave the modified body as is,
1859 -- and return the true spec.
1861 if Present (Priv_Spec)
1862 and then Is_Private_Primitive (Priv_Spec)
1867 -- In case that this is some sort of error, restore the original
1868 -- state of the body.
1870 Replace_Types (To_Corresponding => True);
1874 end Disambiguate_Spec;
1876 -------------------------------------
1877 -- Is_Private_Concurrent_Primitive --
1878 -------------------------------------
1880 function Is_Private_Concurrent_Primitive
1881 (Subp_Id : Entity_Id) return Boolean
1883 Formal_Typ : Entity_Id;
1886 if Present (First_Formal (Subp_Id)) then
1887 Formal_Typ := Etype (First_Formal (Subp_Id));
1889 if Is_Concurrent_Record_Type (Formal_Typ) then
1890 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1893 -- The type of the first formal is a concurrent tagged type with
1897 Is_Concurrent_Type (Formal_Typ)
1898 and then Is_Tagged_Type (Formal_Typ)
1899 and then Has_Private_Declaration (Formal_Typ);
1903 end Is_Private_Concurrent_Primitive;
1905 ----------------------------
1906 -- Set_Trivial_Subprogram --
1907 ----------------------------
1909 procedure Set_Trivial_Subprogram (N : Node_Id) is
1910 Nxt : constant Node_Id := Next (N);
1913 Set_Is_Trivial_Subprogram (Body_Id);
1915 if Present (Spec_Id) then
1916 Set_Is_Trivial_Subprogram (Spec_Id);
1920 and then Nkind (Nxt) = N_Simple_Return_Statement
1921 and then No (Next (Nxt))
1922 and then Present (Expression (Nxt))
1923 and then Is_Entity_Name (Expression (Nxt))
1925 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1927 end Set_Trivial_Subprogram;
1929 ---------------------------------
1930 -- Verify_Overriding_Indicator --
1931 ---------------------------------
1933 procedure Verify_Overriding_Indicator is
1935 if Must_Override (Body_Spec) then
1936 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1937 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1941 elsif not Is_Overriding_Operation (Spec_Id) then
1943 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1946 elsif Must_Not_Override (Body_Spec) then
1947 if Is_Overriding_Operation (Spec_Id) then
1949 ("subprogram& overrides inherited operation",
1950 Body_Spec, Spec_Id);
1952 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1953 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1956 ("subprogram & overrides predefined operator ",
1957 Body_Spec, Spec_Id);
1959 -- If this is not a primitive operation or protected subprogram,
1960 -- then the overriding indicator is altogether illegal.
1962 elsif not Is_Primitive (Spec_Id)
1963 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1966 ("overriding indicator only allowed " &
1967 "if subprogram is primitive",
1971 elsif Style_Check -- ??? incorrect use of Style_Check!
1972 and then Is_Overriding_Operation (Spec_Id)
1974 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1975 Style.Missing_Overriding (N, Body_Id);
1977 end Verify_Overriding_Indicator;
1979 -- Start of processing for Analyze_Subprogram_Body_Helper
1982 -- Generic subprograms are handled separately. They always have a
1983 -- generic specification. Determine whether current scope has a
1984 -- previous declaration.
1986 -- If the subprogram body is defined within an instance of the same
1987 -- name, the instance appears as a package renaming, and will be hidden
1988 -- within the subprogram.
1990 if Present (Prev_Id)
1991 and then not Is_Overloadable (Prev_Id)
1992 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1993 or else Comes_From_Source (Prev_Id))
1995 if Is_Generic_Subprogram (Prev_Id) then
1997 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1998 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2000 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2002 if Nkind (N) = N_Subprogram_Body then
2003 HSS := Handled_Statement_Sequence (N);
2004 Check_Missing_Return;
2010 -- Previous entity conflicts with subprogram name. Attempting to
2011 -- enter name will post error.
2013 Enter_Name (Body_Id);
2017 -- Non-generic case, find the subprogram declaration, if one was seen,
2018 -- or enter new overloaded entity in the current scope. If the
2019 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2020 -- part of the context of one of its subunits. No need to redo the
2023 elsif Prev_Id = Body_Id
2024 and then Has_Completion (Body_Id)
2029 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2031 if Nkind (N) = N_Subprogram_Body_Stub
2032 or else No (Corresponding_Spec (N))
2034 if Is_Private_Concurrent_Primitive (Body_Id) then
2035 Spec_Id := Disambiguate_Spec;
2037 Spec_Id := Find_Corresponding_Spec (N);
2040 -- If this is a duplicate body, no point in analyzing it
2042 if Error_Posted (N) then
2046 -- A subprogram body should cause freezing of its own declaration,
2047 -- but if there was no previous explicit declaration, then the
2048 -- subprogram will get frozen too late (there may be code within
2049 -- the body that depends on the subprogram having been frozen,
2050 -- such as uses of extra formals), so we force it to be frozen
2051 -- here. Same holds if the body and spec are compilation units.
2052 -- Finally, if the return type is an anonymous access to protected
2053 -- subprogram, it must be frozen before the body because its
2054 -- expansion has generated an equivalent type that is used when
2055 -- elaborating the body.
2057 if No (Spec_Id) then
2058 Freeze_Before (N, Body_Id);
2060 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2061 Freeze_Before (N, Spec_Id);
2063 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2064 Freeze_Before (N, Etype (Body_Id));
2068 Spec_Id := Corresponding_Spec (N);
2072 -- Do not inline any subprogram that contains nested subprograms, since
2073 -- the backend inlining circuit seems to generate uninitialized
2074 -- references in this case. We know this happens in the case of front
2075 -- end ZCX support, but it also appears it can happen in other cases as
2076 -- well. The backend often rejects attempts to inline in the case of
2077 -- nested procedures anyway, so little if anything is lost by this.
2078 -- Note that this is test is for the benefit of the back-end. There is
2079 -- a separate test for front-end inlining that also rejects nested
2082 -- Do not do this test if errors have been detected, because in some
2083 -- error cases, this code blows up, and we don't need it anyway if
2084 -- there have been errors, since we won't get to the linker anyway.
2086 if Comes_From_Source (Body_Id)
2087 and then Serious_Errors_Detected = 0
2091 P_Ent := Scope (P_Ent);
2092 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2094 if Is_Subprogram (P_Ent) then
2095 Set_Is_Inlined (P_Ent, False);
2097 if Comes_From_Source (P_Ent)
2098 and then Has_Pragma_Inline (P_Ent)
2101 ("cannot inline& (nested subprogram)?",
2108 Check_Inline_Pragma (Spec_Id);
2110 -- Deal with special case of a fully private operation in the body of
2111 -- the protected type. We must create a declaration for the subprogram,
2112 -- in order to attach the protected subprogram that will be used in
2113 -- internal calls. We exclude compiler generated bodies from the
2114 -- expander since the issue does not arise for those cases.
2117 and then Comes_From_Source (N)
2118 and then Is_Protected_Type (Current_Scope)
2120 Spec_Id := Build_Private_Protected_Declaration (N);
2123 -- If a separate spec is present, then deal with freezing issues
2125 if Present (Spec_Id) then
2126 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2127 Verify_Overriding_Indicator;
2129 -- In general, the spec will be frozen when we start analyzing the
2130 -- body. However, for internally generated operations, such as
2131 -- wrapper functions for inherited operations with controlling
2132 -- results, the spec may not have been frozen by the time we
2133 -- expand the freeze actions that include the bodies. In particular,
2134 -- extra formals for accessibility or for return-in-place may need
2135 -- to be generated. Freeze nodes, if any, are inserted before the
2138 if not Is_Frozen (Spec_Id)
2139 and then Expander_Active
2141 -- Force the generation of its freezing node to ensure proper
2142 -- management of access types in the backend.
2144 -- This is definitely needed for some cases, but it is not clear
2145 -- why, to be investigated further???
2147 Set_Has_Delayed_Freeze (Spec_Id);
2148 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
2152 -- Mark presence of postcondition procedure in current scope and mark
2153 -- the procedure itself as needing debug info. The latter is important
2154 -- when analyzing decision coverage (for example, for MC/DC coverage).
2156 if Chars (Body_Id) = Name_uPostconditions then
2157 Set_Has_Postconditions (Current_Scope);
2158 Set_Debug_Info_Needed (Body_Id);
2161 -- Place subprogram on scope stack, and make formals visible. If there
2162 -- is a spec, the visible entity remains that of the spec.
2164 if Present (Spec_Id) then
2165 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2167 if Is_Child_Unit (Spec_Id) then
2168 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2172 Style.Check_Identifier (Body_Id, Spec_Id);
2175 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2176 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2178 if Is_Abstract_Subprogram (Spec_Id) then
2179 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2183 Set_Convention (Body_Id, Convention (Spec_Id));
2184 Set_Has_Completion (Spec_Id);
2186 if Is_Protected_Type (Scope (Spec_Id)) then
2187 Prot_Typ := Scope (Spec_Id);
2190 -- If this is a body generated for a renaming, do not check for
2191 -- full conformance. The check is redundant, because the spec of
2192 -- the body is a copy of the spec in the renaming declaration,
2193 -- and the test can lead to spurious errors on nested defaults.
2195 if Present (Spec_Decl)
2196 and then not Comes_From_Source (N)
2198 (Nkind (Original_Node (Spec_Decl)) =
2199 N_Subprogram_Renaming_Declaration
2200 or else (Present (Corresponding_Body (Spec_Decl))
2202 Nkind (Unit_Declaration_Node
2203 (Corresponding_Body (Spec_Decl))) =
2204 N_Subprogram_Renaming_Declaration))
2208 -- Conversely, the spec may have been generated for specless body
2209 -- with an inline pragma.
2211 elsif Comes_From_Source (N)
2212 and then not Comes_From_Source (Spec_Id)
2213 and then Has_Pragma_Inline (Spec_Id)
2220 Fully_Conformant, True, Conformant, Body_Id);
2223 -- If the body is not fully conformant, we have to decide if we
2224 -- should analyze it or not. If it has a really messed up profile
2225 -- then we probably should not analyze it, since we will get too
2226 -- many bogus messages.
2228 -- Our decision is to go ahead in the non-fully conformant case
2229 -- only if it is at least mode conformant with the spec. Note
2230 -- that the call to Check_Fully_Conformant has issued the proper
2231 -- error messages to complain about the lack of conformance.
2234 and then not Mode_Conformant (Body_Id, Spec_Id)
2240 if Spec_Id /= Body_Id then
2241 Reference_Body_Formals (Spec_Id, Body_Id);
2244 if Nkind (N) /= N_Subprogram_Body_Stub then
2245 Set_Corresponding_Spec (N, Spec_Id);
2247 -- Ada 2005 (AI-345): If the operation is a primitive operation
2248 -- of a concurrent type, the type of the first parameter has been
2249 -- replaced with the corresponding record, which is the proper
2250 -- run-time structure to use. However, within the body there may
2251 -- be uses of the formals that depend on primitive operations
2252 -- of the type (in particular calls in prefixed form) for which
2253 -- we need the original concurrent type. The operation may have
2254 -- several controlling formals, so the replacement must be done
2257 if Comes_From_Source (Spec_Id)
2258 and then Present (First_Entity (Spec_Id))
2259 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2260 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2262 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2265 (Corresponding_Concurrent_Type
2266 (Etype (First_Entity (Spec_Id))))
2269 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2273 Form := First_Formal (Spec_Id);
2274 while Present (Form) loop
2275 if Etype (Form) = Typ then
2276 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2284 -- Make the formals visible, and place subprogram on scope stack.
2285 -- This is also the point at which we set Last_Real_Spec_Entity
2286 -- to mark the entities which will not be moved to the body.
2288 Install_Formals (Spec_Id);
2289 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2290 Push_Scope (Spec_Id);
2292 -- Make sure that the subprogram is immediately visible. For
2293 -- child units that have no separate spec this is indispensable.
2294 -- Otherwise it is safe albeit redundant.
2296 Set_Is_Immediately_Visible (Spec_Id);
2299 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2300 Set_Ekind (Body_Id, E_Subprogram_Body);
2301 Set_Scope (Body_Id, Scope (Spec_Id));
2302 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2304 -- Case of subprogram body with no previous spec
2308 and then Comes_From_Source (Body_Id)
2309 and then not Suppress_Style_Checks (Body_Id)
2310 and then not In_Instance
2312 Style.Body_With_No_Spec (N);
2315 New_Overloaded_Entity (Body_Id);
2317 if Nkind (N) /= N_Subprogram_Body_Stub then
2318 Set_Acts_As_Spec (N);
2319 Generate_Definition (Body_Id);
2321 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2322 Generate_Reference_To_Formals (Body_Id);
2323 Install_Formals (Body_Id);
2324 Push_Scope (Body_Id);
2328 -- If the return type is an anonymous access type whose designated type
2329 -- is the limited view of a class-wide type and the non-limited view is
2330 -- available, update the return type accordingly.
2332 if Ada_Version >= Ada_2005
2333 and then Comes_From_Source (N)
2340 Rtyp := Etype (Current_Scope);
2342 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2343 Etyp := Directly_Designated_Type (Rtyp);
2345 if Is_Class_Wide_Type (Etyp)
2346 and then From_With_Type (Etyp)
2348 Set_Directly_Designated_Type
2349 (Etype (Current_Scope), Available_View (Etyp));
2355 -- If this is the proper body of a stub, we must verify that the stub
2356 -- conforms to the body, and to the previous spec if one was present.
2357 -- we know already that the body conforms to that spec. This test is
2358 -- only required for subprograms that come from source.
2360 if Nkind (Parent (N)) = N_Subunit
2361 and then Comes_From_Source (N)
2362 and then not Error_Posted (Body_Id)
2363 and then Nkind (Corresponding_Stub (Parent (N))) =
2364 N_Subprogram_Body_Stub
2367 Old_Id : constant Entity_Id :=
2369 (Specification (Corresponding_Stub (Parent (N))));
2371 Conformant : Boolean := False;
2374 if No (Spec_Id) then
2375 Check_Fully_Conformant (Body_Id, Old_Id);
2379 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2381 if not Conformant then
2383 -- The stub was taken to be a new declaration. Indicate
2384 -- that it lacks a body.
2386 Set_Has_Completion (Old_Id, False);
2392 Set_Has_Completion (Body_Id);
2393 Check_Eliminated (Body_Id);
2395 if Nkind (N) = N_Subprogram_Body_Stub then
2398 elsif Present (Spec_Id)
2399 and then Expander_Active
2401 (Has_Pragma_Inline_Always (Spec_Id)
2402 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2404 Build_Body_To_Inline (N, Spec_Id);
2407 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2408 -- if its specification we have to install the private withed units.
2409 -- This holds for child units as well.
2411 if Is_Compilation_Unit (Body_Id)
2412 or else Nkind (Parent (N)) = N_Compilation_Unit
2414 Install_Private_With_Clauses (Body_Id);
2417 Check_Anonymous_Return;
2419 -- Set the Protected_Formal field of each extra formal of the protected
2420 -- subprogram to reference the corresponding extra formal of the
2421 -- subprogram that implements it. For regular formals this occurs when
2422 -- the protected subprogram's declaration is expanded, but the extra
2423 -- formals don't get created until the subprogram is frozen. We need to
2424 -- do this before analyzing the protected subprogram's body so that any
2425 -- references to the original subprogram's extra formals will be changed
2426 -- refer to the implementing subprogram's formals (see Expand_Formal).
2428 if Present (Spec_Id)
2429 and then Is_Protected_Type (Scope (Spec_Id))
2430 and then Present (Protected_Body_Subprogram (Spec_Id))
2433 Impl_Subp : constant Entity_Id :=
2434 Protected_Body_Subprogram (Spec_Id);
2435 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2436 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2438 while Present (Prot_Ext_Formal) loop
2439 pragma Assert (Present (Impl_Ext_Formal));
2440 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2441 Next_Formal_With_Extras (Prot_Ext_Formal);
2442 Next_Formal_With_Extras (Impl_Ext_Formal);
2447 -- Now we can go on to analyze the body
2449 HSS := Handled_Statement_Sequence (N);
2450 Set_Actual_Subtypes (N, Current_Scope);
2452 -- Deal with preconditions and postconditions
2454 Process_PPCs (N, Spec_Id, Body_Id);
2456 -- Add a declaration for the Protection object, renaming declarations
2457 -- for discriminals and privals and finally a declaration for the entry
2458 -- family index (if applicable). This form of early expansion is done
2459 -- when the Expander is active because Install_Private_Data_Declarations
2460 -- references entities which were created during regular expansion.
2463 and then Comes_From_Source (N)
2464 and then Present (Prot_Typ)
2465 and then Present (Spec_Id)
2466 and then not Is_Eliminated (Spec_Id)
2468 Install_Private_Data_Declarations
2469 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2472 -- Analyze the declarations (this call will analyze the precondition
2473 -- Check pragmas we prepended to the list, as well as the declaration
2474 -- of the _Postconditions procedure).
2476 Analyze_Declarations (Declarations (N));
2478 -- Check completion, and analyze the statements
2481 Inspect_Deferred_Constant_Completion (Declarations (N));
2484 -- Deal with end of scope processing for the body
2486 Process_End_Label (HSS, 't', Current_Scope);
2488 Check_Subprogram_Order (N);
2489 Set_Analyzed (Body_Id);
2491 -- If we have a separate spec, then the analysis of the declarations
2492 -- caused the entities in the body to be chained to the spec id, but
2493 -- we want them chained to the body id. Only the formal parameters
2494 -- end up chained to the spec id in this case.
2496 if Present (Spec_Id) then
2498 -- We must conform to the categorization of our spec
2500 Validate_Categorization_Dependency (N, Spec_Id);
2502 -- And if this is a child unit, the parent units must conform
2504 if Is_Child_Unit (Spec_Id) then
2505 Validate_Categorization_Dependency
2506 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2509 -- Here is where we move entities from the spec to the body
2511 -- Case where there are entities that stay with the spec
2513 if Present (Last_Real_Spec_Entity) then
2515 -- No body entities (happens when the only real spec entities
2516 -- come from precondition and postcondition pragmas)
2518 if No (Last_Entity (Body_Id)) then
2520 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2522 -- Body entities present (formals), so chain stuff past them
2526 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2529 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2530 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2531 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2533 -- Case where there are no spec entities, in this case there can
2534 -- be no body entities either, so just move everything.
2537 pragma Assert (No (Last_Entity (Body_Id)));
2538 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2539 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2540 Set_First_Entity (Spec_Id, Empty);
2541 Set_Last_Entity (Spec_Id, Empty);
2545 Check_Missing_Return;
2547 -- Now we are going to check for variables that are never modified in
2548 -- the body of the procedure. But first we deal with a special case
2549 -- where we want to modify this check. If the body of the subprogram
2550 -- starts with a raise statement or its equivalent, or if the body
2551 -- consists entirely of a null statement, then it is pretty obvious
2552 -- that it is OK to not reference the parameters. For example, this
2553 -- might be the following common idiom for a stubbed function:
2554 -- statement of the procedure raises an exception. In particular this
2555 -- deals with the common idiom of a stubbed function, which might
2556 -- appear as something like
2558 -- function F (A : Integer) return Some_Type;
2561 -- raise Program_Error;
2565 -- Here the purpose of X is simply to satisfy the annoying requirement
2566 -- in Ada that there be at least one return, and we certainly do not
2567 -- want to go posting warnings on X that it is not initialized! On
2568 -- the other hand, if X is entirely unreferenced that should still
2571 -- What we do is to detect these cases, and if we find them, flag the
2572 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2573 -- suppress unwanted warnings. For the case of the function stub above
2574 -- we have a special test to set X as apparently assigned to suppress
2581 -- Skip initial labels (for one thing this occurs when we are in
2582 -- front end ZCX mode, but in any case it is irrelevant), and also
2583 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2585 Stm := First (Statements (HSS));
2586 while Nkind (Stm) = N_Label
2587 or else Nkind (Stm) in N_Push_xxx_Label
2592 -- Do the test on the original statement before expansion
2595 Ostm : constant Node_Id := Original_Node (Stm);
2598 -- If explicit raise statement, turn on flag
2600 if Nkind (Ostm) = N_Raise_Statement then
2601 Set_Trivial_Subprogram (Stm);
2603 -- If null statement, and no following statements, turn on flag
2605 elsif Nkind (Stm) = N_Null_Statement
2606 and then Comes_From_Source (Stm)
2607 and then No (Next (Stm))
2609 Set_Trivial_Subprogram (Stm);
2611 -- Check for explicit call cases which likely raise an exception
2613 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2614 if Is_Entity_Name (Name (Ostm)) then
2616 Ent : constant Entity_Id := Entity (Name (Ostm));
2619 -- If the procedure is marked No_Return, then likely it
2620 -- raises an exception, but in any case it is not coming
2621 -- back here, so turn on the flag.
2623 if Ekind (Ent) = E_Procedure
2624 and then No_Return (Ent)
2626 Set_Trivial_Subprogram (Stm);
2634 -- Check for variables that are never modified
2640 -- If there is a separate spec, then transfer Never_Set_In_Source
2641 -- flags from out parameters to the corresponding entities in the
2642 -- body. The reason we do that is we want to post error flags on
2643 -- the body entities, not the spec entities.
2645 if Present (Spec_Id) then
2646 E1 := First_Entity (Spec_Id);
2647 while Present (E1) loop
2648 if Ekind (E1) = E_Out_Parameter then
2649 E2 := First_Entity (Body_Id);
2650 while Present (E2) loop
2651 exit when Chars (E1) = Chars (E2);
2655 if Present (E2) then
2656 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2664 -- Check references in body unless it was deleted. Note that the
2665 -- check of Body_Deleted here is not just for efficiency, it is
2666 -- necessary to avoid junk warnings on formal parameters.
2668 if not Body_Deleted then
2669 Check_References (Body_Id);
2672 end Analyze_Subprogram_Body_Helper;
2674 ------------------------------------
2675 -- Analyze_Subprogram_Declaration --
2676 ------------------------------------
2678 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2679 Loc : constant Source_Ptr := Sloc (N);
2680 Designator : Entity_Id;
2682 Scop : constant Entity_Id := Current_Scope;
2683 Null_Body : Node_Id := Empty;
2685 -- Start of processing for Analyze_Subprogram_Declaration
2688 -- For a null procedure, capture the profile before analysis, for
2689 -- expansion at the freeze point and at each point of call.
2690 -- The body will only be used if the procedure has preconditions.
2691 -- In that case the body is analyzed at the freeze point.
2693 if Nkind (Specification (N)) = N_Procedure_Specification
2694 and then Null_Present (Specification (N))
2695 and then Expander_Active
2698 Make_Subprogram_Body (Loc,
2700 New_Copy_Tree (Specification (N)),
2703 Handled_Statement_Sequence =>
2704 Make_Handled_Sequence_Of_Statements (Loc,
2705 Statements => New_List (Make_Null_Statement (Loc))));
2707 -- Create new entities for body and formals
2709 Set_Defining_Unit_Name (Specification (Null_Body),
2710 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2711 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2713 Form := First (Parameter_Specifications (Specification (Null_Body)));
2714 while Present (Form) loop
2715 Set_Defining_Identifier (Form,
2716 Make_Defining_Identifier (Loc,
2717 Chars (Defining_Identifier (Form))));
2721 if Is_Protected_Type (Current_Scope) then
2722 Error_Msg_N ("protected operation cannot be a null procedure", N);
2726 Designator := Analyze_Subprogram_Specification (Specification (N));
2727 Generate_Definition (Designator);
2729 if Debug_Flag_C then
2730 Write_Str ("==> subprogram spec ");
2731 Write_Name (Chars (Designator));
2732 Write_Str (" from ");
2733 Write_Location (Sloc (N));
2738 if Nkind (Specification (N)) = N_Procedure_Specification
2739 and then Null_Present (Specification (N))
2741 Set_Has_Completion (Designator);
2743 if Present (Null_Body) then
2744 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2745 Set_Body_To_Inline (N, Null_Body);
2746 Set_Is_Inlined (Designator);
2750 Validate_RCI_Subprogram_Declaration (N);
2751 New_Overloaded_Entity (Designator);
2752 Check_Delayed_Subprogram (Designator);
2754 -- If the type of the first formal of the current subprogram is a
2755 -- nongeneric tagged private type, mark the subprogram as being a
2756 -- private primitive. Ditto if this is a function with controlling
2757 -- result, and the return type is currently private. In both cases,
2758 -- the type of the controlling argument or result must be in the
2759 -- current scope for the operation to be primitive.
2761 if Has_Controlling_Result (Designator)
2762 and then Is_Private_Type (Etype (Designator))
2763 and then Scope (Etype (Designator)) = Current_Scope
2764 and then not Is_Generic_Actual_Type (Etype (Designator))
2766 Set_Is_Private_Primitive (Designator);
2768 elsif Present (First_Formal (Designator)) then
2770 Formal_Typ : constant Entity_Id :=
2771 Etype (First_Formal (Designator));
2773 Set_Is_Private_Primitive (Designator,
2774 Is_Tagged_Type (Formal_Typ)
2775 and then Scope (Formal_Typ) = Current_Scope
2776 and then Is_Private_Type (Formal_Typ)
2777 and then not Is_Generic_Actual_Type (Formal_Typ));
2781 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2784 if Ada_Version >= Ada_2005
2785 and then Comes_From_Source (N)
2786 and then Is_Dispatching_Operation (Designator)
2793 if Has_Controlling_Result (Designator) then
2794 Etyp := Etype (Designator);
2797 E := First_Entity (Designator);
2799 and then Is_Formal (E)
2800 and then not Is_Controlling_Formal (E)
2808 if Is_Access_Type (Etyp) then
2809 Etyp := Directly_Designated_Type (Etyp);
2812 if Is_Interface (Etyp)
2813 and then not Is_Abstract_Subprogram (Designator)
2814 and then not (Ekind (Designator) = E_Procedure
2815 and then Null_Present (Specification (N)))
2817 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2819 ("(Ada 2005) interface subprogram % must be abstract or null",
2825 -- What is the following code for, it used to be
2827 -- ??? Set_Suppress_Elaboration_Checks
2828 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2830 -- The following seems equivalent, but a bit dubious
2832 if Elaboration_Checks_Suppressed (Designator) then
2833 Set_Kill_Elaboration_Checks (Designator);
2836 if Scop /= Standard_Standard
2837 and then not Is_Child_Unit (Designator)
2839 Set_Categorization_From_Scope (Designator, Scop);
2841 -- For a compilation unit, check for library-unit pragmas
2843 Push_Scope (Designator);
2844 Set_Categorization_From_Pragmas (N);
2845 Validate_Categorization_Dependency (N, Designator);
2849 -- For a compilation unit, set body required. This flag will only be
2850 -- reset if a valid Import or Interface pragma is processed later on.
2852 if Nkind (Parent (N)) = N_Compilation_Unit then
2853 Set_Body_Required (Parent (N), True);
2855 if Ada_Version >= Ada_2005
2856 and then Nkind (Specification (N)) = N_Procedure_Specification
2857 and then Null_Present (Specification (N))
2860 ("null procedure cannot be declared at library level", N);
2864 Generate_Reference_To_Formals (Designator);
2865 Check_Eliminated (Designator);
2867 if Debug_Flag_C then
2869 Write_Str ("<== subprogram spec ");
2870 Write_Name (Chars (Designator));
2871 Write_Str (" from ");
2872 Write_Location (Sloc (N));
2875 end Analyze_Subprogram_Declaration;
2877 --------------------------------------
2878 -- Analyze_Subprogram_Specification --
2879 --------------------------------------
2881 -- Reminder: N here really is a subprogram specification (not a subprogram
2882 -- declaration). This procedure is called to analyze the specification in
2883 -- both subprogram bodies and subprogram declarations (specs).
2885 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2886 Designator : constant Entity_Id := Defining_Entity (N);
2887 Formals : constant List_Id := Parameter_Specifications (N);
2889 -- Start of processing for Analyze_Subprogram_Specification
2892 Generate_Definition (Designator);
2894 if Nkind (N) = N_Function_Specification then
2895 Set_Ekind (Designator, E_Function);
2896 Set_Mechanism (Designator, Default_Mechanism);
2899 Set_Ekind (Designator, E_Procedure);
2900 Set_Etype (Designator, Standard_Void_Type);
2903 -- Introduce new scope for analysis of the formals and the return type
2905 Set_Scope (Designator, Current_Scope);
2907 if Present (Formals) then
2908 Push_Scope (Designator);
2909 Process_Formals (Formals, N);
2911 -- Ada 2005 (AI-345): If this is an overriding operation of an
2912 -- inherited interface operation, and the controlling type is
2913 -- a synchronized type, replace the type with its corresponding
2914 -- record, to match the proper signature of an overriding operation.
2915 -- Same processing for an access parameter whose designated type is
2916 -- derived from a synchronized interface.
2918 if Ada_Version >= Ada_2005 then
2921 Formal_Typ : Entity_Id;
2922 Rec_Typ : Entity_Id;
2923 Desig_Typ : Entity_Id;
2926 Formal := First_Formal (Designator);
2927 while Present (Formal) loop
2928 Formal_Typ := Etype (Formal);
2930 if Is_Concurrent_Type (Formal_Typ)
2931 and then Present (Corresponding_Record_Type (Formal_Typ))
2933 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2935 if Present (Interfaces (Rec_Typ)) then
2936 Set_Etype (Formal, Rec_Typ);
2939 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
2940 Desig_Typ := Designated_Type (Formal_Typ);
2942 if Is_Concurrent_Type (Desig_Typ)
2943 and then Present (Corresponding_Record_Type (Desig_Typ))
2945 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
2947 if Present (Interfaces (Rec_Typ)) then
2948 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
2953 Next_Formal (Formal);
2960 -- The subprogram scope is pushed and popped around the processing of
2961 -- the return type for consistency with call above to Process_Formals
2962 -- (which itself can call Analyze_Return_Type), and to ensure that any
2963 -- itype created for the return type will be associated with the proper
2966 elsif Nkind (N) = N_Function_Specification then
2967 Push_Scope (Designator);
2969 Analyze_Return_Type (N);
2974 if Nkind (N) = N_Function_Specification then
2975 if Nkind (Designator) = N_Defining_Operator_Symbol then
2976 Valid_Operator_Definition (Designator);
2979 May_Need_Actuals (Designator);
2981 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2982 -- the subprogram is abstract also. This does not apply to renaming
2983 -- declarations, where abstractness is inherited.
2985 -- In case of primitives associated with abstract interface types
2986 -- the check is applied later (see Analyze_Subprogram_Declaration).
2988 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
2989 N_Abstract_Subprogram_Declaration,
2990 N_Formal_Abstract_Subprogram_Declaration)
2992 if Is_Abstract_Type (Etype (Designator))
2993 and then not Is_Interface (Etype (Designator))
2996 ("function that returns abstract type must be abstract", N);
2998 -- Ada 2012 (AI-0073): extend this test to subprograms with an
2999 -- access result whose designated type is abstract.
3001 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3003 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3004 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3005 and then Ada_Version >= Ada_2012
3007 Error_Msg_N ("function whose access result designates "
3008 & "abstract type must be abstract", N);
3014 end Analyze_Subprogram_Specification;
3016 --------------------------
3017 -- Build_Body_To_Inline --
3018 --------------------------
3020 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3021 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3022 Original_Body : Node_Id;
3023 Body_To_Analyze : Node_Id;
3024 Max_Size : constant := 10;
3025 Stat_Count : Integer := 0;
3027 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3028 -- Check for declarations that make inlining not worthwhile
3030 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3031 -- Check for statements that make inlining not worthwhile: any tasking
3032 -- statement, nested at any level. Keep track of total number of
3033 -- elementary statements, as a measure of acceptable size.
3035 function Has_Pending_Instantiation return Boolean;
3036 -- If some enclosing body contains instantiations that appear before the
3037 -- corresponding generic body, the enclosing body has a freeze node so
3038 -- that it can be elaborated after the generic itself. This might
3039 -- conflict with subsequent inlinings, so that it is unsafe to try to
3040 -- inline in such a case.
3042 function Has_Single_Return return Boolean;
3043 -- In general we cannot inline functions that return unconstrained type.
3044 -- However, we can handle such functions if all return statements return
3045 -- a local variable that is the only declaration in the body of the
3046 -- function. In that case the call can be replaced by that local
3047 -- variable as is done for other inlined calls.
3049 procedure Remove_Pragmas;
3050 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3051 -- parameter has no meaning when the body is inlined and the formals
3052 -- are rewritten. Remove it from body to inline. The analysis of the
3053 -- non-inlined body will handle the pragma properly.
3055 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3056 -- If the body of the subprogram includes a call that returns an
3057 -- unconstrained type, the secondary stack is involved, and it
3058 -- is not worth inlining.
3060 ------------------------------
3061 -- Has_Excluded_Declaration --
3062 ------------------------------
3064 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3067 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3068 -- Nested subprograms make a given body ineligible for inlining, but
3069 -- we make an exception for instantiations of unchecked conversion.
3070 -- The body has not been analyzed yet, so check the name, and verify
3071 -- that the visible entity with that name is the predefined unit.
3073 -----------------------------
3074 -- Is_Unchecked_Conversion --
3075 -----------------------------
3077 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3078 Id : constant Node_Id := Name (D);
3082 if Nkind (Id) = N_Identifier
3083 and then Chars (Id) = Name_Unchecked_Conversion
3085 Conv := Current_Entity (Id);
3087 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3088 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3090 Conv := Current_Entity (Selector_Name (Id));
3095 return Present (Conv)
3096 and then Is_Predefined_File_Name
3097 (Unit_File_Name (Get_Source_Unit (Conv)))
3098 and then Is_Intrinsic_Subprogram (Conv);
3099 end Is_Unchecked_Conversion;
3101 -- Start of processing for Has_Excluded_Declaration
3105 while Present (D) loop
3106 if (Nkind (D) = N_Function_Instantiation
3107 and then not Is_Unchecked_Conversion (D))
3108 or else Nkind_In (D, N_Protected_Type_Declaration,
3109 N_Package_Declaration,
3110 N_Package_Instantiation,
3112 N_Procedure_Instantiation,
3113 N_Task_Type_Declaration)
3116 ("cannot inline & (non-allowed declaration)?", D, Subp);
3124 end Has_Excluded_Declaration;
3126 ----------------------------
3127 -- Has_Excluded_Statement --
3128 ----------------------------
3130 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3136 while Present (S) loop
3137 Stat_Count := Stat_Count + 1;
3139 if Nkind_In (S, N_Abort_Statement,
3140 N_Asynchronous_Select,
3141 N_Conditional_Entry_Call,
3142 N_Delay_Relative_Statement,
3143 N_Delay_Until_Statement,
3148 ("cannot inline & (non-allowed statement)?", S, Subp);
3151 elsif Nkind (S) = N_Block_Statement then
3152 if Present (Declarations (S))
3153 and then Has_Excluded_Declaration (Declarations (S))
3157 elsif Present (Handled_Statement_Sequence (S))
3160 (Exception_Handlers (Handled_Statement_Sequence (S)))
3162 Has_Excluded_Statement
3163 (Statements (Handled_Statement_Sequence (S))))
3168 elsif Nkind (S) = N_Case_Statement then
3169 E := First (Alternatives (S));
3170 while Present (E) loop
3171 if Has_Excluded_Statement (Statements (E)) then
3178 elsif Nkind (S) = N_If_Statement then
3179 if Has_Excluded_Statement (Then_Statements (S)) then
3183 if Present (Elsif_Parts (S)) then
3184 E := First (Elsif_Parts (S));
3185 while Present (E) loop
3186 if Has_Excluded_Statement (Then_Statements (E)) then
3193 if Present (Else_Statements (S))
3194 and then Has_Excluded_Statement (Else_Statements (S))
3199 elsif Nkind (S) = N_Loop_Statement
3200 and then Has_Excluded_Statement (Statements (S))
3204 elsif Nkind (S) = N_Extended_Return_Statement then
3205 if Has_Excluded_Statement
3206 (Statements (Handled_Statement_Sequence (S)))
3208 (Exception_Handlers (Handled_Statement_Sequence (S)))
3218 end Has_Excluded_Statement;
3220 -------------------------------
3221 -- Has_Pending_Instantiation --
3222 -------------------------------
3224 function Has_Pending_Instantiation return Boolean is
3229 while Present (S) loop
3230 if Is_Compilation_Unit (S)
3231 or else Is_Child_Unit (S)
3235 elsif Ekind (S) = E_Package
3236 and then Has_Forward_Instantiation (S)
3245 end Has_Pending_Instantiation;
3247 ------------------------
3248 -- Has_Single_Return --
3249 ------------------------
3251 function Has_Single_Return return Boolean is
3252 Return_Statement : Node_Id := Empty;
3254 function Check_Return (N : Node_Id) return Traverse_Result;
3260 function Check_Return (N : Node_Id) return Traverse_Result is
3262 if Nkind (N) = N_Simple_Return_Statement then
3263 if Present (Expression (N))
3264 and then Is_Entity_Name (Expression (N))
3266 if No (Return_Statement) then
3267 Return_Statement := N;
3270 elsif Chars (Expression (N)) =
3271 Chars (Expression (Return_Statement))
3279 -- A return statement within an extended return is a noop
3282 elsif No (Expression (N))
3283 and then Nkind (Parent (Parent (N))) =
3284 N_Extended_Return_Statement
3289 -- Expression has wrong form
3294 -- We can only inline a build-in-place function if
3295 -- it has a single extended return.
3297 elsif Nkind (N) = N_Extended_Return_Statement then
3298 if No (Return_Statement) then
3299 Return_Statement := N;
3311 function Check_All_Returns is new Traverse_Func (Check_Return);
3313 -- Start of processing for Has_Single_Return
3316 if Check_All_Returns (N) /= OK then
3319 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3323 return Present (Declarations (N))
3324 and then Present (First (Declarations (N)))
3325 and then Chars (Expression (Return_Statement)) =
3326 Chars (Defining_Identifier (First (Declarations (N))));
3328 end Has_Single_Return;
3330 --------------------
3331 -- Remove_Pragmas --
3332 --------------------
3334 procedure Remove_Pragmas is
3339 Decl := First (Declarations (Body_To_Analyze));
3340 while Present (Decl) loop
3343 if Nkind (Decl) = N_Pragma
3344 and then (Pragma_Name (Decl) = Name_Unreferenced
3346 Pragma_Name (Decl) = Name_Unmodified)
3355 --------------------------
3356 -- Uses_Secondary_Stack --
3357 --------------------------
3359 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3360 function Check_Call (N : Node_Id) return Traverse_Result;
3361 -- Look for function calls that return an unconstrained type
3367 function Check_Call (N : Node_Id) return Traverse_Result is
3369 if Nkind (N) = N_Function_Call
3370 and then Is_Entity_Name (Name (N))
3371 and then Is_Composite_Type (Etype (Entity (Name (N))))
3372 and then not Is_Constrained (Etype (Entity (Name (N))))
3375 ("cannot inline & (call returns unconstrained type)?",
3383 function Check_Calls is new Traverse_Func (Check_Call);
3386 return Check_Calls (Bod) = Abandon;
3387 end Uses_Secondary_Stack;
3389 -- Start of processing for Build_Body_To_Inline
3392 -- Return immediately if done already
3394 if Nkind (Decl) = N_Subprogram_Declaration
3395 and then Present (Body_To_Inline (Decl))
3399 -- Functions that return unconstrained composite types require
3400 -- secondary stack handling, and cannot currently be inlined, unless
3401 -- all return statements return a local variable that is the first
3402 -- local declaration in the body.
3404 elsif Ekind (Subp) = E_Function
3405 and then not Is_Scalar_Type (Etype (Subp))
3406 and then not Is_Access_Type (Etype (Subp))
3407 and then not Is_Constrained (Etype (Subp))
3409 if not Has_Single_Return then
3411 ("cannot inline & (unconstrained return type)?", N, Subp);
3415 -- Ditto for functions that return controlled types, where controlled
3416 -- actions interfere in complex ways with inlining.
3418 elsif Ekind (Subp) = E_Function
3419 and then Needs_Finalization (Etype (Subp))
3422 ("cannot inline & (controlled return type)?", N, Subp);
3426 if Present (Declarations (N))
3427 and then Has_Excluded_Declaration (Declarations (N))
3432 if Present (Handled_Statement_Sequence (N)) then
3433 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3435 ("cannot inline& (exception handler)?",
3436 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3440 Has_Excluded_Statement
3441 (Statements (Handled_Statement_Sequence (N)))
3447 -- We do not inline a subprogram that is too large, unless it is
3448 -- marked Inline_Always. This pragma does not suppress the other
3449 -- checks on inlining (forbidden declarations, handlers, etc).
3451 if Stat_Count > Max_Size
3452 and then not Has_Pragma_Inline_Always (Subp)
3454 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3458 if Has_Pending_Instantiation then
3460 ("cannot inline& (forward instance within enclosing body)?",
3465 -- Within an instance, the body to inline must be treated as a nested
3466 -- generic, so that the proper global references are preserved.
3468 -- Note that we do not do this at the library level, because it is not
3469 -- needed, and furthermore this causes trouble if front end inlining
3470 -- is activated (-gnatN).
3472 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3473 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3474 Original_Body := Copy_Generic_Node (N, Empty, True);
3476 Original_Body := Copy_Separate_Tree (N);
3479 -- We need to capture references to the formals in order to substitute
3480 -- the actuals at the point of inlining, i.e. instantiation. To treat
3481 -- the formals as globals to the body to inline, we nest it within
3482 -- a dummy parameterless subprogram, declared within the real one.
3483 -- To avoid generating an internal name (which is never public, and
3484 -- which affects serial numbers of other generated names), we use
3485 -- an internal symbol that cannot conflict with user declarations.
3487 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3488 Set_Defining_Unit_Name
3489 (Specification (Original_Body),
3490 Make_Defining_Identifier (Sloc (N), Name_uParent));
3491 Set_Corresponding_Spec (Original_Body, Empty);
3493 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3495 -- Set return type of function, which is also global and does not need
3498 if Ekind (Subp) = E_Function then
3499 Set_Result_Definition (Specification (Body_To_Analyze),
3500 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3503 if No (Declarations (N)) then
3504 Set_Declarations (N, New_List (Body_To_Analyze));
3506 Append (Body_To_Analyze, Declarations (N));
3509 Expander_Mode_Save_And_Set (False);
3512 Analyze (Body_To_Analyze);
3513 Push_Scope (Defining_Entity (Body_To_Analyze));
3514 Save_Global_References (Original_Body);
3516 Remove (Body_To_Analyze);
3518 Expander_Mode_Restore;
3520 -- Restore environment if previously saved
3522 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3526 -- If secondary stk used there is no point in inlining. We have
3527 -- already issued the warning in this case, so nothing to do.
3529 if Uses_Secondary_Stack (Body_To_Analyze) then
3533 Set_Body_To_Inline (Decl, Original_Body);
3534 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3535 Set_Is_Inlined (Subp);
3536 end Build_Body_To_Inline;
3542 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3544 -- Do not emit warning if this is a predefined unit which is not the
3545 -- main unit. With validity checks enabled, some predefined subprograms
3546 -- may contain nested subprograms and become ineligible for inlining.
3548 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3549 and then not In_Extended_Main_Source_Unit (Subp)
3553 elsif Has_Pragma_Inline_Always (Subp) then
3555 -- Remove last character (question mark) to make this into an error,
3556 -- because the Inline_Always pragma cannot be obeyed.
3558 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3560 elsif Ineffective_Inline_Warnings then
3561 Error_Msg_NE (Msg, N, Subp);
3565 -----------------------
3566 -- Check_Conformance --
3567 -----------------------
3569 procedure Check_Conformance
3570 (New_Id : Entity_Id;
3572 Ctype : Conformance_Type;
3574 Conforms : out Boolean;
3575 Err_Loc : Node_Id := Empty;
3576 Get_Inst : Boolean := False;
3577 Skip_Controlling_Formals : Boolean := False)
3579 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3580 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3581 -- If Errmsg is True, then processing continues to post an error message
3582 -- for conformance error on given node. Two messages are output. The
3583 -- first message points to the previous declaration with a general "no
3584 -- conformance" message. The second is the detailed reason, supplied as
3585 -- Msg. The parameter N provide information for a possible & insertion
3586 -- in the message, and also provides the location for posting the
3587 -- message in the absence of a specified Err_Loc location.
3589 -----------------------
3590 -- Conformance_Error --
3591 -----------------------
3593 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3600 if No (Err_Loc) then
3606 Error_Msg_Sloc := Sloc (Old_Id);
3609 when Type_Conformant =>
3610 Error_Msg_N -- CODEFIX
3611 ("not type conformant with declaration#!", Enode);
3613 when Mode_Conformant =>
3614 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3616 ("not mode conformant with operation inherited#!",
3620 ("not mode conformant with declaration#!", Enode);
3623 when Subtype_Conformant =>
3624 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3626 ("not subtype conformant with operation inherited#!",
3630 ("not subtype conformant with declaration#!", Enode);
3633 when Fully_Conformant =>
3634 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3635 Error_Msg_N -- CODEFIX
3636 ("not fully conformant with operation inherited#!",
3639 Error_Msg_N -- CODEFIX
3640 ("not fully conformant with declaration#!", Enode);
3644 Error_Msg_NE (Msg, Enode, N);
3646 end Conformance_Error;
3650 Old_Type : constant Entity_Id := Etype (Old_Id);
3651 New_Type : constant Entity_Id := Etype (New_Id);
3652 Old_Formal : Entity_Id;
3653 New_Formal : Entity_Id;
3654 Access_Types_Match : Boolean;
3655 Old_Formal_Base : Entity_Id;
3656 New_Formal_Base : Entity_Id;
3658 -- Start of processing for Check_Conformance
3663 -- We need a special case for operators, since they don't appear
3666 if Ctype = Type_Conformant then
3667 if Ekind (New_Id) = E_Operator
3668 and then Operator_Matches_Spec (New_Id, Old_Id)
3674 -- If both are functions/operators, check return types conform
3676 if Old_Type /= Standard_Void_Type
3677 and then New_Type /= Standard_Void_Type
3680 -- If we are checking interface conformance we omit controlling
3681 -- arguments and result, because we are only checking the conformance
3682 -- of the remaining parameters.
3684 if Has_Controlling_Result (Old_Id)
3685 and then Has_Controlling_Result (New_Id)
3686 and then Skip_Controlling_Formals
3690 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3691 Conformance_Error ("\return type does not match!", New_Id);
3695 -- Ada 2005 (AI-231): In case of anonymous access types check the
3696 -- null-exclusion and access-to-constant attributes match.
3698 if Ada_Version >= Ada_2005
3699 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3701 (Can_Never_Be_Null (Old_Type)
3702 /= Can_Never_Be_Null (New_Type)
3703 or else Is_Access_Constant (Etype (Old_Type))
3704 /= Is_Access_Constant (Etype (New_Type)))
3706 Conformance_Error ("\return type does not match!", New_Id);
3710 -- If either is a function/operator and the other isn't, error
3712 elsif Old_Type /= Standard_Void_Type
3713 or else New_Type /= Standard_Void_Type
3715 Conformance_Error ("\functions can only match functions!", New_Id);
3719 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3720 -- If this is a renaming as body, refine error message to indicate that
3721 -- the conflict is with the original declaration. If the entity is not
3722 -- frozen, the conventions don't have to match, the one of the renamed
3723 -- entity is inherited.
3725 if Ctype >= Subtype_Conformant then
3726 if Convention (Old_Id) /= Convention (New_Id) then
3728 if not Is_Frozen (New_Id) then
3731 elsif Present (Err_Loc)
3732 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3733 and then Present (Corresponding_Spec (Err_Loc))
3735 Error_Msg_Name_1 := Chars (New_Id);
3737 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3738 Conformance_Error ("\prior declaration for% has convention %!");
3741 Conformance_Error ("\calling conventions do not match!");
3746 elsif Is_Formal_Subprogram (Old_Id)
3747 or else Is_Formal_Subprogram (New_Id)
3749 Conformance_Error ("\formal subprograms not allowed!");
3754 -- Deal with parameters
3756 -- Note: we use the entity information, rather than going directly
3757 -- to the specification in the tree. This is not only simpler, but
3758 -- absolutely necessary for some cases of conformance tests between
3759 -- operators, where the declaration tree simply does not exist!
3761 Old_Formal := First_Formal (Old_Id);
3762 New_Formal := First_Formal (New_Id);
3763 while Present (Old_Formal) and then Present (New_Formal) loop
3764 if Is_Controlling_Formal (Old_Formal)
3765 and then Is_Controlling_Formal (New_Formal)
3766 and then Skip_Controlling_Formals
3768 -- The controlling formals will have different types when
3769 -- comparing an interface operation with its match, but both
3770 -- or neither must be access parameters.
3772 if Is_Access_Type (Etype (Old_Formal))
3774 Is_Access_Type (Etype (New_Formal))
3776 goto Skip_Controlling_Formal;
3779 ("\access parameter does not match!", New_Formal);
3783 if Ctype = Fully_Conformant then
3785 -- Names must match. Error message is more accurate if we do
3786 -- this before checking that the types of the formals match.
3788 if Chars (Old_Formal) /= Chars (New_Formal) then
3789 Conformance_Error ("\name & does not match!", New_Formal);
3791 -- Set error posted flag on new formal as well to stop
3792 -- junk cascaded messages in some cases.
3794 Set_Error_Posted (New_Formal);
3798 -- Null exclusion must match
3800 if Null_Exclusion_Present (Parent (Old_Formal))
3802 Null_Exclusion_Present (Parent (New_Formal))
3804 -- Only give error if both come from source. This should be
3805 -- investigated some time, since it should not be needed ???
3807 if Comes_From_Source (Old_Formal)
3809 Comes_From_Source (New_Formal)
3812 ("\null exclusion for & does not match", New_Formal);
3814 -- Mark error posted on the new formal to avoid duplicated
3815 -- complaint about types not matching.
3817 Set_Error_Posted (New_Formal);
3822 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3823 -- case occurs whenever a subprogram is being renamed and one of its
3824 -- parameters imposes a null exclusion. For example:
3826 -- type T is null record;
3827 -- type Acc_T is access T;
3828 -- subtype Acc_T_Sub is Acc_T;
3830 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3831 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3834 Old_Formal_Base := Etype (Old_Formal);
3835 New_Formal_Base := Etype (New_Formal);
3838 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3839 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3842 Access_Types_Match := Ada_Version >= Ada_2005
3844 -- Ensure that this rule is only applied when New_Id is a
3845 -- renaming of Old_Id.
3847 and then Nkind (Parent (Parent (New_Id))) =
3848 N_Subprogram_Renaming_Declaration
3849 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3850 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3851 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3853 -- Now handle the allowed access-type case
3855 and then Is_Access_Type (Old_Formal_Base)
3856 and then Is_Access_Type (New_Formal_Base)
3858 -- The type kinds must match. The only exception occurs with
3859 -- multiple generics of the form:
3862 -- type F is private; type A is private;
3863 -- type F_Ptr is access F; type A_Ptr is access A;
3864 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3865 -- package F_Pack is ... package A_Pack is
3866 -- package F_Inst is
3867 -- new F_Pack (A, A_Ptr, A_P);
3869 -- When checking for conformance between the parameters of A_P
3870 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3871 -- because the compiler has transformed A_Ptr into a subtype of
3872 -- F_Ptr. We catch this case in the code below.
3874 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3876 (Is_Generic_Type (Old_Formal_Base)
3877 and then Is_Generic_Type (New_Formal_Base)
3878 and then Is_Internal (New_Formal_Base)
3879 and then Etype (Etype (New_Formal_Base)) =
3881 and then Directly_Designated_Type (Old_Formal_Base) =
3882 Directly_Designated_Type (New_Formal_Base)
3883 and then ((Is_Itype (Old_Formal_Base)
3884 and then Can_Never_Be_Null (Old_Formal_Base))
3886 (Is_Itype (New_Formal_Base)
3887 and then Can_Never_Be_Null (New_Formal_Base)));
3889 -- Types must always match. In the visible part of an instance,
3890 -- usual overloading rules for dispatching operations apply, and
3891 -- we check base types (not the actual subtypes).
3893 if In_Instance_Visible_Part
3894 and then Is_Dispatching_Operation (New_Id)
3896 if not Conforming_Types
3897 (T1 => Base_Type (Etype (Old_Formal)),
3898 T2 => Base_Type (Etype (New_Formal)),
3900 Get_Inst => Get_Inst)
3901 and then not Access_Types_Match
3903 Conformance_Error ("\type of & does not match!", New_Formal);
3907 elsif not Conforming_Types
3908 (T1 => Old_Formal_Base,
3909 T2 => New_Formal_Base,
3911 Get_Inst => Get_Inst)
3912 and then not Access_Types_Match
3914 -- Don't give error message if old type is Any_Type. This test
3915 -- avoids some cascaded errors, e.g. in case of a bad spec.
3917 if Errmsg and then Old_Formal_Base = Any_Type then
3920 Conformance_Error ("\type of & does not match!", New_Formal);
3926 -- For mode conformance, mode must match
3928 if Ctype >= Mode_Conformant then
3929 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3930 Conformance_Error ("\mode of & does not match!", New_Formal);
3933 -- Part of mode conformance for access types is having the same
3934 -- constant modifier.
3936 elsif Access_Types_Match
3937 and then Is_Access_Constant (Old_Formal_Base) /=
3938 Is_Access_Constant (New_Formal_Base)
3941 ("\constant modifier does not match!", New_Formal);
3946 if Ctype >= Subtype_Conformant then
3948 -- Ada 2005 (AI-231): In case of anonymous access types check
3949 -- the null-exclusion and access-to-constant attributes must
3952 if Ada_Version >= Ada_2005
3953 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3954 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3956 (Can_Never_Be_Null (Old_Formal) /=
3957 Can_Never_Be_Null (New_Formal)
3959 Is_Access_Constant (Etype (Old_Formal)) /=
3960 Is_Access_Constant (Etype (New_Formal)))
3962 -- Do not complain if error already posted on New_Formal. This
3963 -- avoids some redundant error messages.
3965 and then not Error_Posted (New_Formal)
3967 -- It is allowed to omit the null-exclusion in case of stream
3968 -- attribute subprograms. We recognize stream subprograms
3969 -- through their TSS-generated suffix.
3972 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3974 if TSS_Name /= TSS_Stream_Read
3975 and then TSS_Name /= TSS_Stream_Write
3976 and then TSS_Name /= TSS_Stream_Input
3977 and then TSS_Name /= TSS_Stream_Output
3980 ("\type of & does not match!", New_Formal);
3987 -- Full conformance checks
3989 if Ctype = Fully_Conformant then
3991 -- We have checked already that names match
3993 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3995 -- Check default expressions for in parameters
3998 NewD : constant Boolean :=
3999 Present (Default_Value (New_Formal));
4000 OldD : constant Boolean :=
4001 Present (Default_Value (Old_Formal));
4003 if NewD or OldD then
4005 -- The old default value has been analyzed because the
4006 -- current full declaration will have frozen everything
4007 -- before. The new default value has not been analyzed,
4008 -- so analyze it now before we check for conformance.
4011 Push_Scope (New_Id);
4012 Preanalyze_Spec_Expression
4013 (Default_Value (New_Formal), Etype (New_Formal));
4017 if not (NewD and OldD)
4018 or else not Fully_Conformant_Expressions
4019 (Default_Value (Old_Formal),
4020 Default_Value (New_Formal))
4023 ("\default expression for & does not match!",
4032 -- A couple of special checks for Ada 83 mode. These checks are
4033 -- skipped if either entity is an operator in package Standard,
4034 -- or if either old or new instance is not from the source program.
4036 if Ada_Version = Ada_83
4037 and then Sloc (Old_Id) > Standard_Location
4038 and then Sloc (New_Id) > Standard_Location
4039 and then Comes_From_Source (Old_Id)
4040 and then Comes_From_Source (New_Id)
4043 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4044 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4047 -- Explicit IN must be present or absent in both cases. This
4048 -- test is required only in the full conformance case.
4050 if In_Present (Old_Param) /= In_Present (New_Param)
4051 and then Ctype = Fully_Conformant
4054 ("\(Ada 83) IN must appear in both declarations",
4059 -- Grouping (use of comma in param lists) must be the same
4060 -- This is where we catch a misconformance like:
4063 -- A : Integer; B : Integer
4065 -- which are represented identically in the tree except
4066 -- for the setting of the flags More_Ids and Prev_Ids.
4068 if More_Ids (Old_Param) /= More_Ids (New_Param)
4069 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4072 ("\grouping of & does not match!", New_Formal);
4078 -- This label is required when skipping controlling formals
4080 <<Skip_Controlling_Formal>>
4082 Next_Formal (Old_Formal);
4083 Next_Formal (New_Formal);
4086 if Present (Old_Formal) then
4087 Conformance_Error ("\too few parameters!");
4090 elsif Present (New_Formal) then
4091 Conformance_Error ("\too many parameters!", New_Formal);
4094 end Check_Conformance;
4096 -----------------------
4097 -- Check_Conventions --
4098 -----------------------
4100 procedure Check_Conventions (Typ : Entity_Id) is
4101 Ifaces_List : Elist_Id;
4103 procedure Check_Convention (Op : Entity_Id);
4104 -- Verify that the convention of inherited dispatching operation Op is
4105 -- consistent among all subprograms it overrides. In order to minimize
4106 -- the search, Search_From is utilized to designate a specific point in
4107 -- the list rather than iterating over the whole list once more.
4109 ----------------------
4110 -- Check_Convention --
4111 ----------------------
4113 procedure Check_Convention (Op : Entity_Id) is
4114 Iface_Elmt : Elmt_Id;
4115 Iface_Prim_Elmt : Elmt_Id;
4116 Iface_Prim : Entity_Id;
4119 Iface_Elmt := First_Elmt (Ifaces_List);
4120 while Present (Iface_Elmt) loop
4122 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4123 while Present (Iface_Prim_Elmt) loop
4124 Iface_Prim := Node (Iface_Prim_Elmt);
4126 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4127 and then Convention (Iface_Prim) /= Convention (Op)
4130 ("inconsistent conventions in primitive operations", Typ);
4132 Error_Msg_Name_1 := Chars (Op);
4133 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4134 Error_Msg_Sloc := Sloc (Op);
4136 if Comes_From_Source (Op) or else No (Alias (Op)) then
4137 if not Is_Overriding_Operation (Op) then
4138 Error_Msg_N ("\\primitive % defined #", Typ);
4141 ("\\overriding operation % with " &
4142 "convention % defined #", Typ);
4145 else pragma Assert (Present (Alias (Op)));
4146 Error_Msg_Sloc := Sloc (Alias (Op));
4148 ("\\inherited operation % with " &
4149 "convention % defined #", Typ);
4152 Error_Msg_Name_1 := Chars (Op);
4154 Get_Convention_Name (Convention (Iface_Prim));
4155 Error_Msg_Sloc := Sloc (Iface_Prim);
4157 ("\\overridden operation % with " &
4158 "convention % defined #", Typ);
4160 -- Avoid cascading errors
4165 Next_Elmt (Iface_Prim_Elmt);
4168 Next_Elmt (Iface_Elmt);
4170 end Check_Convention;
4174 Prim_Op : Entity_Id;
4175 Prim_Op_Elmt : Elmt_Id;
4177 -- Start of processing for Check_Conventions
4180 if not Has_Interfaces (Typ) then
4184 Collect_Interfaces (Typ, Ifaces_List);
4186 -- The algorithm checks every overriding dispatching operation against
4187 -- all the corresponding overridden dispatching operations, detecting
4188 -- differences in conventions.
4190 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4191 while Present (Prim_Op_Elmt) loop
4192 Prim_Op := Node (Prim_Op_Elmt);
4194 -- A small optimization: skip the predefined dispatching operations
4195 -- since they always have the same convention.
4197 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4198 Check_Convention (Prim_Op);
4201 Next_Elmt (Prim_Op_Elmt);
4203 end Check_Conventions;
4205 ------------------------------
4206 -- Check_Delayed_Subprogram --
4207 ------------------------------
4209 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4212 procedure Possible_Freeze (T : Entity_Id);
4213 -- T is the type of either a formal parameter or of the return type.
4214 -- If T is not yet frozen and needs a delayed freeze, then the
4215 -- subprogram itself must be delayed. If T is the limited view of an
4216 -- incomplete type the subprogram must be frozen as well, because
4217 -- T may depend on local types that have not been frozen yet.
4219 ---------------------
4220 -- Possible_Freeze --
4221 ---------------------
4223 procedure Possible_Freeze (T : Entity_Id) is
4225 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4226 Set_Has_Delayed_Freeze (Designator);
4228 elsif Is_Access_Type (T)
4229 and then Has_Delayed_Freeze (Designated_Type (T))
4230 and then not Is_Frozen (Designated_Type (T))
4232 Set_Has_Delayed_Freeze (Designator);
4234 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4235 Set_Has_Delayed_Freeze (Designator);
4238 end Possible_Freeze;
4240 -- Start of processing for Check_Delayed_Subprogram
4243 -- Never need to freeze abstract subprogram
4245 if Ekind (Designator) /= E_Subprogram_Type
4246 and then Is_Abstract_Subprogram (Designator)
4250 -- Need delayed freeze if return type itself needs a delayed
4251 -- freeze and is not yet frozen.
4253 Possible_Freeze (Etype (Designator));
4254 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4256 -- Need delayed freeze if any of the formal types themselves need
4257 -- a delayed freeze and are not yet frozen.
4259 F := First_Formal (Designator);
4260 while Present (F) loop
4261 Possible_Freeze (Etype (F));
4262 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4267 -- Mark functions that return by reference. Note that it cannot be
4268 -- done for delayed_freeze subprograms because the underlying
4269 -- returned type may not be known yet (for private types)
4271 if not Has_Delayed_Freeze (Designator)
4272 and then Expander_Active
4275 Typ : constant Entity_Id := Etype (Designator);
4276 Utyp : constant Entity_Id := Underlying_Type (Typ);
4279 if Is_Immutably_Limited_Type (Typ) then
4280 Set_Returns_By_Ref (Designator);
4282 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4283 Set_Returns_By_Ref (Designator);
4287 end Check_Delayed_Subprogram;
4289 ------------------------------------
4290 -- Check_Discriminant_Conformance --
4291 ------------------------------------
4293 procedure Check_Discriminant_Conformance
4298 Old_Discr : Entity_Id := First_Discriminant (Prev);
4299 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4300 New_Discr_Id : Entity_Id;
4301 New_Discr_Type : Entity_Id;
4303 procedure Conformance_Error (Msg : String; N : Node_Id);
4304 -- Post error message for conformance error on given node. Two messages
4305 -- are output. The first points to the previous declaration with a
4306 -- general "no conformance" message. The second is the detailed reason,
4307 -- supplied as Msg. The parameter N provide information for a possible
4308 -- & insertion in the message.
4310 -----------------------
4311 -- Conformance_Error --
4312 -----------------------
4314 procedure Conformance_Error (Msg : String; N : Node_Id) is
4316 Error_Msg_Sloc := Sloc (Prev_Loc);
4317 Error_Msg_N -- CODEFIX
4318 ("not fully conformant with declaration#!", N);
4319 Error_Msg_NE (Msg, N, N);
4320 end Conformance_Error;
4322 -- Start of processing for Check_Discriminant_Conformance
4325 while Present (Old_Discr) and then Present (New_Discr) loop
4327 New_Discr_Id := Defining_Identifier (New_Discr);
4329 -- The subtype mark of the discriminant on the full type has not
4330 -- been analyzed so we do it here. For an access discriminant a new
4333 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4335 Access_Definition (N, Discriminant_Type (New_Discr));
4338 Analyze (Discriminant_Type (New_Discr));
4339 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4341 -- Ada 2005: if the discriminant definition carries a null
4342 -- exclusion, create an itype to check properly for consistency
4343 -- with partial declaration.
4345 if Is_Access_Type (New_Discr_Type)
4346 and then Null_Exclusion_Present (New_Discr)
4349 Create_Null_Excluding_Itype
4350 (T => New_Discr_Type,
4351 Related_Nod => New_Discr,
4352 Scope_Id => Current_Scope);
4356 if not Conforming_Types
4357 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4359 Conformance_Error ("type of & does not match!", New_Discr_Id);
4362 -- Treat the new discriminant as an occurrence of the old one,
4363 -- for navigation purposes, and fill in some semantic
4364 -- information, for completeness.
4366 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4367 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4368 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4373 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4374 Conformance_Error ("name & does not match!", New_Discr_Id);
4378 -- Default expressions must match
4381 NewD : constant Boolean :=
4382 Present (Expression (New_Discr));
4383 OldD : constant Boolean :=
4384 Present (Expression (Parent (Old_Discr)));
4387 if NewD or OldD then
4389 -- The old default value has been analyzed and expanded,
4390 -- because the current full declaration will have frozen
4391 -- everything before. The new default values have not been
4392 -- expanded, so expand now to check conformance.
4395 Preanalyze_Spec_Expression
4396 (Expression (New_Discr), New_Discr_Type);
4399 if not (NewD and OldD)
4400 or else not Fully_Conformant_Expressions
4401 (Expression (Parent (Old_Discr)),
4402 Expression (New_Discr))
4406 ("default expression for & does not match!",
4413 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4415 if Ada_Version = Ada_83 then
4417 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4420 -- Grouping (use of comma in param lists) must be the same
4421 -- This is where we catch a misconformance like:
4424 -- A : Integer; B : Integer
4426 -- which are represented identically in the tree except
4427 -- for the setting of the flags More_Ids and Prev_Ids.
4429 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4430 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4433 ("grouping of & does not match!", New_Discr_Id);
4439 Next_Discriminant (Old_Discr);
4443 if Present (Old_Discr) then
4444 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4447 elsif Present (New_Discr) then
4449 ("too many discriminants!", Defining_Identifier (New_Discr));
4452 end Check_Discriminant_Conformance;
4454 ----------------------------
4455 -- Check_Fully_Conformant --
4456 ----------------------------
4458 procedure Check_Fully_Conformant
4459 (New_Id : Entity_Id;
4461 Err_Loc : Node_Id := Empty)
4464 pragma Warnings (Off, Result);
4467 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4468 end Check_Fully_Conformant;
4470 ---------------------------
4471 -- Check_Mode_Conformant --
4472 ---------------------------
4474 procedure Check_Mode_Conformant
4475 (New_Id : Entity_Id;
4477 Err_Loc : Node_Id := Empty;
4478 Get_Inst : Boolean := False)
4481 pragma Warnings (Off, Result);
4484 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4485 end Check_Mode_Conformant;
4487 --------------------------------
4488 -- Check_Overriding_Indicator --
4489 --------------------------------
4491 procedure Check_Overriding_Indicator
4493 Overridden_Subp : Entity_Id;
4494 Is_Primitive : Boolean)
4500 -- No overriding indicator for literals
4502 if Ekind (Subp) = E_Enumeration_Literal then
4505 elsif Ekind (Subp) = E_Entry then
4506 Decl := Parent (Subp);
4508 -- No point in analyzing a malformed operator
4510 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4511 and then Error_Posted (Subp)
4516 Decl := Unit_Declaration_Node (Subp);
4519 if Nkind_In (Decl, N_Subprogram_Body,
4520 N_Subprogram_Body_Stub,
4521 N_Subprogram_Declaration,
4522 N_Abstract_Subprogram_Declaration,
4523 N_Subprogram_Renaming_Declaration)
4525 Spec := Specification (Decl);
4527 elsif Nkind (Decl) = N_Entry_Declaration then
4534 -- The overriding operation is type conformant with the overridden one,
4535 -- but the names of the formals are not required to match. If the names
4536 -- appear permuted in the overriding operation, this is a possible
4537 -- source of confusion that is worth diagnosing. Controlling formals
4538 -- often carry names that reflect the type, and it is not worthwhile
4539 -- requiring that their names match.
4541 if Present (Overridden_Subp)
4542 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4549 Form1 := First_Formal (Subp);
4550 Form2 := First_Formal (Overridden_Subp);
4552 -- If the overriding operation is a synchronized operation, skip
4553 -- the first parameter of the overridden operation, which is
4554 -- implicit in the new one. If the operation is declared in the
4555 -- body it is not primitive and all formals must match.
4557 if Is_Concurrent_Type (Scope (Subp))
4558 and then Is_Tagged_Type (Scope (Subp))
4559 and then not Has_Completion (Scope (Subp))
4561 Form2 := Next_Formal (Form2);
4564 if Present (Form1) then
4565 Form1 := Next_Formal (Form1);
4566 Form2 := Next_Formal (Form2);
4569 while Present (Form1) loop
4570 if not Is_Controlling_Formal (Form1)
4571 and then Present (Next_Formal (Form2))
4572 and then Chars (Form1) = Chars (Next_Formal (Form2))
4574 Error_Msg_Node_2 := Alias (Overridden_Subp);
4575 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4577 ("& does not match corresponding formal of&#",
4582 Next_Formal (Form1);
4583 Next_Formal (Form2);
4588 -- If there is an overridden subprogram, then check that there is no
4589 -- "not overriding" indicator, and mark the subprogram as overriding.
4590 -- This is not done if the overridden subprogram is marked as hidden,
4591 -- which can occur for the case of inherited controlled operations
4592 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4593 -- subprogram is not itself hidden. (Note: This condition could probably
4594 -- be simplified, leaving out the testing for the specific controlled
4595 -- cases, but it seems safer and clearer this way, and echoes similar
4596 -- special-case tests of this kind in other places.)
4598 if Present (Overridden_Subp)
4599 and then (not Is_Hidden (Overridden_Subp)
4601 ((Chars (Overridden_Subp) = Name_Initialize
4602 or else Chars (Overridden_Subp) = Name_Adjust
4603 or else Chars (Overridden_Subp) = Name_Finalize)
4604 and then Present (Alias (Overridden_Subp))
4605 and then not Is_Hidden (Alias (Overridden_Subp))))
4607 if Must_Not_Override (Spec) then
4608 Error_Msg_Sloc := Sloc (Overridden_Subp);
4610 if Ekind (Subp) = E_Entry then
4612 ("entry & overrides inherited operation #", Spec, Subp);
4615 ("subprogram & overrides inherited operation #", Spec, Subp);
4618 elsif Is_Subprogram (Subp) then
4619 Set_Is_Overriding_Operation (Subp);
4622 -- If primitive flag is set or this is a protected operation, then
4623 -- the operation is overriding at the point of its declaration, so
4624 -- warn if necessary. Otherwise it may have been declared before the
4625 -- operation it overrides and no check is required.
4628 and then not Must_Override (Spec)
4629 and then (Is_Primitive
4630 or else Ekind (Scope (Subp)) = E_Protected_Type)
4632 Style.Missing_Overriding (Decl, Subp);
4635 -- If Subp is an operator, it may override a predefined operation, if
4636 -- it is defined in the same scope as the type to which it applies.
4637 -- In that case Overridden_Subp is empty because of our implicit
4638 -- representation for predefined operators. We have to check whether the
4639 -- signature of Subp matches that of a predefined operator. Note that
4640 -- first argument provides the name of the operator, and the second
4641 -- argument the signature that may match that of a standard operation.
4642 -- If the indicator is overriding, then the operator must match a
4643 -- predefined signature, because we know already that there is no
4644 -- explicit overridden operation.
4646 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4648 Typ : constant Entity_Id :=
4649 Base_Type (Etype (First_Formal (Subp)));
4651 Can_Override : constant Boolean :=
4652 Operator_Matches_Spec (Subp, Subp)
4653 and then Scope (Subp) = Scope (Typ)
4654 and then not Is_Class_Wide_Type (Typ);
4657 if Must_Not_Override (Spec) then
4659 -- If this is not a primitive or a protected subprogram, then
4660 -- "not overriding" is illegal.
4663 and then Ekind (Scope (Subp)) /= E_Protected_Type
4666 ("overriding indicator only allowed "
4667 & "if subprogram is primitive", Subp);
4669 elsif Can_Override then
4671 ("subprogram& overrides predefined operator ", Spec, Subp);
4674 elsif Must_Override (Spec) then
4675 if Is_Overriding_Operation (Subp) then
4678 elsif not Can_Override then
4679 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4682 elsif not Error_Posted (Subp)
4683 and then Style_Check
4684 and then Can_Override
4686 not Is_Predefined_File_Name
4687 (Unit_File_Name (Get_Source_Unit (Subp)))
4689 Set_Is_Overriding_Operation (Subp);
4691 -- If style checks are enabled, indicate that the indicator is
4692 -- missing. However, at the point of declaration, the type of
4693 -- which this is a primitive operation may be private, in which
4694 -- case the indicator would be premature.
4696 if Has_Private_Declaration (Etype (Subp))
4697 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4701 Style.Missing_Overriding (Decl, Subp);
4706 elsif Must_Override (Spec) then
4707 if Ekind (Subp) = E_Entry then
4708 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4710 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4713 -- If the operation is marked "not overriding" and it's not primitive
4714 -- then an error is issued, unless this is an operation of a task or
4715 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4716 -- has been specified have already been checked above.
4718 elsif Must_Not_Override (Spec)
4719 and then not Is_Primitive
4720 and then Ekind (Subp) /= E_Entry
4721 and then Ekind (Scope (Subp)) /= E_Protected_Type
4724 ("overriding indicator only allowed if subprogram is primitive",
4728 end Check_Overriding_Indicator;
4734 -- Note: this procedure needs to know far too much about how the expander
4735 -- messes with exceptions. The use of the flag Exception_Junk and the
4736 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4737 -- works, but is not very clean. It would be better if the expansion
4738 -- routines would leave Original_Node working nicely, and we could use
4739 -- Original_Node here to ignore all the peculiar expander messing ???
4741 procedure Check_Returns
4745 Proc : Entity_Id := Empty)
4749 procedure Check_Statement_Sequence (L : List_Id);
4750 -- Internal recursive procedure to check a list of statements for proper
4751 -- termination by a return statement (or a transfer of control or a
4752 -- compound statement that is itself internally properly terminated).
4754 ------------------------------
4755 -- Check_Statement_Sequence --
4756 ------------------------------
4758 procedure Check_Statement_Sequence (L : List_Id) is
4763 Raise_Exception_Call : Boolean;
4764 -- Set True if statement sequence terminated by Raise_Exception call
4765 -- or a Reraise_Occurrence call.
4768 Raise_Exception_Call := False;
4770 -- Get last real statement
4772 Last_Stm := Last (L);
4774 -- Deal with digging out exception handler statement sequences that
4775 -- have been transformed by the local raise to goto optimization.
4776 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4777 -- optimization has occurred, we are looking at something like:
4780 -- original stmts in block
4784 -- goto L1; | omitted if No_Exception_Propagation
4789 -- goto L3; -- skip handler when exception not raised
4791 -- <<L1>> -- target label for local exception
4805 -- and what we have to do is to dig out the estmts1 and estmts2
4806 -- sequences (which were the original sequences of statements in
4807 -- the exception handlers) and check them.
4809 if Nkind (Last_Stm) = N_Label
4810 and then Exception_Junk (Last_Stm)
4816 exit when Nkind (Stm) /= N_Block_Statement;
4817 exit when not Exception_Junk (Stm);
4820 exit when Nkind (Stm) /= N_Label;
4821 exit when not Exception_Junk (Stm);
4822 Check_Statement_Sequence
4823 (Statements (Handled_Statement_Sequence (Next (Stm))));
4828 exit when Nkind (Stm) /= N_Goto_Statement;
4829 exit when not Exception_Junk (Stm);
4833 -- Don't count pragmas
4835 while Nkind (Last_Stm) = N_Pragma
4837 -- Don't count call to SS_Release (can happen after Raise_Exception)
4840 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4842 Nkind (Name (Last_Stm)) = N_Identifier
4844 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4846 -- Don't count exception junk
4849 (Nkind_In (Last_Stm, N_Goto_Statement,
4851 N_Object_Declaration)
4852 and then Exception_Junk (Last_Stm))
4853 or else Nkind (Last_Stm) in N_Push_xxx_Label
4854 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4859 -- Here we have the "real" last statement
4861 Kind := Nkind (Last_Stm);
4863 -- Transfer of control, OK. Note that in the No_Return procedure
4864 -- case, we already diagnosed any explicit return statements, so
4865 -- we can treat them as OK in this context.
4867 if Is_Transfer (Last_Stm) then
4870 -- Check cases of explicit non-indirect procedure calls
4872 elsif Kind = N_Procedure_Call_Statement
4873 and then Is_Entity_Name (Name (Last_Stm))
4875 -- Check call to Raise_Exception procedure which is treated
4876 -- specially, as is a call to Reraise_Occurrence.
4878 -- We suppress the warning in these cases since it is likely that
4879 -- the programmer really does not expect to deal with the case
4880 -- of Null_Occurrence, and thus would find a warning about a
4881 -- missing return curious, and raising Program_Error does not
4882 -- seem such a bad behavior if this does occur.
4884 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4885 -- behavior will be to raise Constraint_Error (see AI-329).
4887 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4889 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4891 Raise_Exception_Call := True;
4893 -- For Raise_Exception call, test first argument, if it is
4894 -- an attribute reference for a 'Identity call, then we know
4895 -- that the call cannot possibly return.
4898 Arg : constant Node_Id :=
4899 Original_Node (First_Actual (Last_Stm));
4901 if Nkind (Arg) = N_Attribute_Reference
4902 and then Attribute_Name (Arg) = Name_Identity
4909 -- If statement, need to look inside if there is an else and check
4910 -- each constituent statement sequence for proper termination.
4912 elsif Kind = N_If_Statement
4913 and then Present (Else_Statements (Last_Stm))
4915 Check_Statement_Sequence (Then_Statements (Last_Stm));
4916 Check_Statement_Sequence (Else_Statements (Last_Stm));
4918 if Present (Elsif_Parts (Last_Stm)) then
4920 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4923 while Present (Elsif_Part) loop
4924 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4932 -- Case statement, check each case for proper termination
4934 elsif Kind = N_Case_Statement then
4938 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4939 while Present (Case_Alt) loop
4940 Check_Statement_Sequence (Statements (Case_Alt));
4941 Next_Non_Pragma (Case_Alt);
4947 -- Block statement, check its handled sequence of statements
4949 elsif Kind = N_Block_Statement then
4955 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4964 -- Loop statement. If there is an iteration scheme, we can definitely
4965 -- fall out of the loop. Similarly if there is an exit statement, we
4966 -- can fall out. In either case we need a following return.
4968 elsif Kind = N_Loop_Statement then
4969 if Present (Iteration_Scheme (Last_Stm))
4970 or else Has_Exit (Entity (Identifier (Last_Stm)))
4974 -- A loop with no exit statement or iteration scheme is either
4975 -- an infinite loop, or it has some other exit (raise/return).
4976 -- In either case, no warning is required.
4982 -- Timed entry call, check entry call and delay alternatives
4984 -- Note: in expanded code, the timed entry call has been converted
4985 -- to a set of expanded statements on which the check will work
4986 -- correctly in any case.
4988 elsif Kind = N_Timed_Entry_Call then
4990 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4991 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4994 -- If statement sequence of entry call alternative is missing,
4995 -- then we can definitely fall through, and we post the error
4996 -- message on the entry call alternative itself.
4998 if No (Statements (ECA)) then
5001 -- If statement sequence of delay alternative is missing, then
5002 -- we can definitely fall through, and we post the error
5003 -- message on the delay alternative itself.
5005 -- Note: if both ECA and DCA are missing the return, then we
5006 -- post only one message, should be enough to fix the bugs.
5007 -- If not we will get a message next time on the DCA when the
5010 elsif No (Statements (DCA)) then
5013 -- Else check both statement sequences
5016 Check_Statement_Sequence (Statements (ECA));
5017 Check_Statement_Sequence (Statements (DCA));
5022 -- Conditional entry call, check entry call and else part
5024 -- Note: in expanded code, the conditional entry call has been
5025 -- converted to a set of expanded statements on which the check
5026 -- will work correctly in any case.
5028 elsif Kind = N_Conditional_Entry_Call then
5030 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5033 -- If statement sequence of entry call alternative is missing,
5034 -- then we can definitely fall through, and we post the error
5035 -- message on the entry call alternative itself.
5037 if No (Statements (ECA)) then
5040 -- Else check statement sequence and else part
5043 Check_Statement_Sequence (Statements (ECA));
5044 Check_Statement_Sequence (Else_Statements (Last_Stm));
5050 -- If we fall through, issue appropriate message
5053 if not Raise_Exception_Call then
5055 ("?RETURN statement missing following this statement!",
5058 ("\?Program_Error may be raised at run time!",
5062 -- Note: we set Err even though we have not issued a warning
5063 -- because we still have a case of a missing return. This is
5064 -- an extremely marginal case, probably will never be noticed
5065 -- but we might as well get it right.
5069 -- Otherwise we have the case of a procedure marked No_Return
5072 if not Raise_Exception_Call then
5074 ("?implied return after this statement " &
5075 "will raise Program_Error",
5078 ("\?procedure & is marked as No_Return!",
5083 RE : constant Node_Id :=
5084 Make_Raise_Program_Error (Sloc (Last_Stm),
5085 Reason => PE_Implicit_Return);
5087 Insert_After (Last_Stm, RE);
5091 end Check_Statement_Sequence;
5093 -- Start of processing for Check_Returns
5097 Check_Statement_Sequence (Statements (HSS));
5099 if Present (Exception_Handlers (HSS)) then
5100 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5101 while Present (Handler) loop
5102 Check_Statement_Sequence (Statements (Handler));
5103 Next_Non_Pragma (Handler);
5108 ----------------------------
5109 -- Check_Subprogram_Order --
5110 ----------------------------
5112 procedure Check_Subprogram_Order (N : Node_Id) is
5114 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5115 -- This is used to check if S1 > S2 in the sense required by this
5116 -- test, for example nameab < namec, but name2 < name10.
5118 -----------------------------
5119 -- Subprogram_Name_Greater --
5120 -----------------------------
5122 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5127 -- Remove trailing numeric parts
5130 while S1 (L1) in '0' .. '9' loop
5135 while S2 (L2) in '0' .. '9' loop
5139 -- If non-numeric parts non-equal, that's decisive
5141 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5144 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5147 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5148 -- that a missing suffix is treated as numeric zero in this test.
5152 while L1 < S1'Last loop
5154 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5158 while L2 < S2'Last loop
5160 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5165 end Subprogram_Name_Greater;
5167 -- Start of processing for Check_Subprogram_Order
5170 -- Check body in alpha order if this is option
5173 and then Style_Check_Order_Subprograms
5174 and then Nkind (N) = N_Subprogram_Body
5175 and then Comes_From_Source (N)
5176 and then In_Extended_Main_Source_Unit (N)
5180 renames Scope_Stack.Table
5181 (Scope_Stack.Last).Last_Subprogram_Name;
5183 Body_Id : constant Entity_Id :=
5184 Defining_Entity (Specification (N));
5187 Get_Decoded_Name_String (Chars (Body_Id));
5190 if Subprogram_Name_Greater
5191 (LSN.all, Name_Buffer (1 .. Name_Len))
5193 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5199 LSN := new String'(Name_Buffer (1 .. Name_Len));
5202 end Check_Subprogram_Order;
5204 ------------------------------
5205 -- Check_Subtype_Conformant --
5206 ------------------------------
5208 procedure Check_Subtype_Conformant
5209 (New_Id : Entity_Id;
5211 Err_Loc : Node_Id := Empty;
5212 Skip_Controlling_Formals : Boolean := False)
5215 pragma Warnings (Off, Result);
5218 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5219 Skip_Controlling_Formals => Skip_Controlling_Formals);
5220 end Check_Subtype_Conformant;
5222 ---------------------------
5223 -- Check_Type_Conformant --
5224 ---------------------------
5226 procedure Check_Type_Conformant
5227 (New_Id : Entity_Id;
5229 Err_Loc : Node_Id := Empty)
5232 pragma Warnings (Off, Result);
5235 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5236 end Check_Type_Conformant;
5238 ----------------------
5239 -- Conforming_Types --
5240 ----------------------
5242 function Conforming_Types
5245 Ctype : Conformance_Type;
5246 Get_Inst : Boolean := False) return Boolean
5248 Type_1 : Entity_Id := T1;
5249 Type_2 : Entity_Id := T2;
5250 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5252 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5253 -- If neither T1 nor T2 are generic actual types, or if they are in
5254 -- different scopes (e.g. parent and child instances), then verify that
5255 -- the base types are equal. Otherwise T1 and T2 must be on the same
5256 -- subtype chain. The whole purpose of this procedure is to prevent
5257 -- spurious ambiguities in an instantiation that may arise if two
5258 -- distinct generic types are instantiated with the same actual.
5260 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5261 -- An access parameter can designate an incomplete type. If the
5262 -- incomplete type is the limited view of a type from a limited_
5263 -- with_clause, check whether the non-limited view is available. If
5264 -- it is a (non-limited) incomplete type, get the full view.
5266 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5267 -- Returns True if and only if either T1 denotes a limited view of T2
5268 -- or T2 denotes a limited view of T1. This can arise when the limited
5269 -- with view of a type is used in a subprogram declaration and the
5270 -- subprogram body is in the scope of a regular with clause for the
5271 -- same unit. In such a case, the two type entities can be considered
5272 -- identical for purposes of conformance checking.
5274 ----------------------
5275 -- Base_Types_Match --
5276 ----------------------
5278 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5283 elsif Base_Type (T1) = Base_Type (T2) then
5285 -- The following is too permissive. A more precise test should
5286 -- check that the generic actual is an ancestor subtype of the
5289 return not Is_Generic_Actual_Type (T1)
5290 or else not Is_Generic_Actual_Type (T2)
5291 or else Scope (T1) /= Scope (T2);
5296 end Base_Types_Match;
5298 --------------------------
5299 -- Find_Designated_Type --
5300 --------------------------
5302 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5306 Desig := Directly_Designated_Type (T);
5308 if Ekind (Desig) = E_Incomplete_Type then
5310 -- If regular incomplete type, get full view if available
5312 if Present (Full_View (Desig)) then
5313 Desig := Full_View (Desig);
5315 -- If limited view of a type, get non-limited view if available,
5316 -- and check again for a regular incomplete type.
5318 elsif Present (Non_Limited_View (Desig)) then
5319 Desig := Get_Full_View (Non_Limited_View (Desig));
5324 end Find_Designated_Type;
5326 -------------------------------
5327 -- Matches_Limited_With_View --
5328 -------------------------------
5330 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5332 -- In some cases a type imported through a limited_with clause, and
5333 -- its nonlimited view are both visible, for example in an anonymous
5334 -- access-to-class-wide type in a formal. Both entities designate the
5337 if From_With_Type (T1)
5338 and then T2 = Available_View (T1)
5342 elsif From_With_Type (T2)
5343 and then T1 = Available_View (T2)
5350 end Matches_Limited_With_View;
5352 -- Start of processing for Conforming_Types
5355 -- The context is an instance association for a formal
5356 -- access-to-subprogram type; the formal parameter types require
5357 -- mapping because they may denote other formal parameters of the
5361 Type_1 := Get_Instance_Of (T1);
5362 Type_2 := Get_Instance_Of (T2);
5365 -- If one of the types is a view of the other introduced by a limited
5366 -- with clause, treat these as conforming for all purposes.
5368 if Matches_Limited_With_View (T1, T2) then
5371 elsif Base_Types_Match (Type_1, Type_2) then
5372 return Ctype <= Mode_Conformant
5373 or else Subtypes_Statically_Match (Type_1, Type_2);
5375 elsif Is_Incomplete_Or_Private_Type (Type_1)
5376 and then Present (Full_View (Type_1))
5377 and then Base_Types_Match (Full_View (Type_1), Type_2)
5379 return Ctype <= Mode_Conformant
5380 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5382 elsif Ekind (Type_2) = E_Incomplete_Type
5383 and then Present (Full_View (Type_2))
5384 and then Base_Types_Match (Type_1, Full_View (Type_2))
5386 return Ctype <= Mode_Conformant
5387 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5389 elsif Is_Private_Type (Type_2)
5390 and then In_Instance
5391 and then Present (Full_View (Type_2))
5392 and then Base_Types_Match (Type_1, Full_View (Type_2))
5394 return Ctype <= Mode_Conformant
5395 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5398 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5399 -- treated recursively because they carry a signature.
5401 Are_Anonymous_Access_To_Subprogram_Types :=
5402 Ekind (Type_1) = Ekind (Type_2)
5404 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5406 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5408 -- Test anonymous access type case. For this case, static subtype
5409 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5410 -- the base types because we may have built internal subtype entities
5411 -- to handle null-excluding types (see Process_Formals).
5413 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5415 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5416 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5419 Desig_1 : Entity_Id;
5420 Desig_2 : Entity_Id;
5423 -- In Ada2005, access constant indicators must match for
5424 -- subtype conformance.
5426 if Ada_Version >= Ada_2005
5427 and then Ctype >= Subtype_Conformant
5429 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5434 Desig_1 := Find_Designated_Type (Type_1);
5436 Desig_2 := Find_Designated_Type (Type_2);
5438 -- If the context is an instance association for a formal
5439 -- access-to-subprogram type; formal access parameter designated
5440 -- types require mapping because they may denote other formal
5441 -- parameters of the generic unit.
5444 Desig_1 := Get_Instance_Of (Desig_1);
5445 Desig_2 := Get_Instance_Of (Desig_2);
5448 -- It is possible for a Class_Wide_Type to be introduced for an
5449 -- incomplete type, in which case there is a separate class_ wide
5450 -- type for the full view. The types conform if their Etypes
5451 -- conform, i.e. one may be the full view of the other. This can
5452 -- only happen in the context of an access parameter, other uses
5453 -- of an incomplete Class_Wide_Type are illegal.
5455 if Is_Class_Wide_Type (Desig_1)
5456 and then Is_Class_Wide_Type (Desig_2)
5460 (Etype (Base_Type (Desig_1)),
5461 Etype (Base_Type (Desig_2)), Ctype);
5463 elsif Are_Anonymous_Access_To_Subprogram_Types then
5464 if Ada_Version < Ada_2005 then
5465 return Ctype = Type_Conformant
5467 Subtypes_Statically_Match (Desig_1, Desig_2);
5469 -- We must check the conformance of the signatures themselves
5473 Conformant : Boolean;
5476 (Desig_1, Desig_2, Ctype, False, Conformant);
5482 return Base_Type (Desig_1) = Base_Type (Desig_2)
5483 and then (Ctype = Type_Conformant
5485 Subtypes_Statically_Match (Desig_1, Desig_2));
5489 -- Otherwise definitely no match
5492 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5493 and then Is_Access_Type (Type_2))
5494 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5495 and then Is_Access_Type (Type_1)))
5498 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5500 May_Hide_Profile := True;
5505 end Conforming_Types;
5507 --------------------------
5508 -- Create_Extra_Formals --
5509 --------------------------
5511 procedure Create_Extra_Formals (E : Entity_Id) is
5513 First_Extra : Entity_Id := Empty;
5514 Last_Extra : Entity_Id;
5515 Formal_Type : Entity_Id;
5516 P_Formal : Entity_Id := Empty;
5518 function Add_Extra_Formal
5519 (Assoc_Entity : Entity_Id;
5522 Suffix : String) return Entity_Id;
5523 -- Add an extra formal to the current list of formals and extra formals.
5524 -- The extra formal is added to the end of the list of extra formals,
5525 -- and also returned as the result. These formals are always of mode IN.
5526 -- The new formal has the type Typ, is declared in Scope, and its name
5527 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5528 -- The following suffixes are currently used. They should not be changed
5529 -- without coordinating with CodePeer, which makes use of these to
5530 -- provide better messages.
5532 -- O denotes the Constrained bit.
5533 -- L denotes the accessibility level.
5534 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5535 -- the full list in exp_ch6.BIP_Formal_Kind.
5537 ----------------------
5538 -- Add_Extra_Formal --
5539 ----------------------
5541 function Add_Extra_Formal
5542 (Assoc_Entity : Entity_Id;
5545 Suffix : String) return Entity_Id
5547 EF : constant Entity_Id :=
5548 Make_Defining_Identifier (Sloc (Assoc_Entity),
5549 Chars => New_External_Name (Chars (Assoc_Entity),
5553 -- A little optimization. Never generate an extra formal for the
5554 -- _init operand of an initialization procedure, since it could
5557 if Chars (Formal) = Name_uInit then
5561 Set_Ekind (EF, E_In_Parameter);
5562 Set_Actual_Subtype (EF, Typ);
5563 Set_Etype (EF, Typ);
5564 Set_Scope (EF, Scope);
5565 Set_Mechanism (EF, Default_Mechanism);
5566 Set_Formal_Validity (EF);
5568 if No (First_Extra) then
5570 Set_Extra_Formals (Scope, First_Extra);
5573 if Present (Last_Extra) then
5574 Set_Extra_Formal (Last_Extra, EF);
5580 end Add_Extra_Formal;
5582 -- Start of processing for Create_Extra_Formals
5585 -- We never generate extra formals if expansion is not active
5586 -- because we don't need them unless we are generating code.
5588 if not Expander_Active then
5592 -- If this is a derived subprogram then the subtypes of the parent
5593 -- subprogram's formal parameters will be used to determine the need
5594 -- for extra formals.
5596 if Is_Overloadable (E) and then Present (Alias (E)) then
5597 P_Formal := First_Formal (Alias (E));
5600 Last_Extra := Empty;
5601 Formal := First_Formal (E);
5602 while Present (Formal) loop
5603 Last_Extra := Formal;
5604 Next_Formal (Formal);
5607 -- If Extra_formals were already created, don't do it again. This
5608 -- situation may arise for subprogram types created as part of
5609 -- dispatching calls (see Expand_Dispatching_Call)
5611 if Present (Last_Extra) and then
5612 Present (Extra_Formal (Last_Extra))
5617 -- If the subprogram is a predefined dispatching subprogram then don't
5618 -- generate any extra constrained or accessibility level formals. In
5619 -- general we suppress these for internal subprograms (by not calling
5620 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5621 -- generated stream attributes do get passed through because extra
5622 -- build-in-place formals are needed in some cases (limited 'Input).
5624 if Is_Predefined_Internal_Operation (E) then
5625 goto Test_For_BIP_Extras;
5628 Formal := First_Formal (E);
5629 while Present (Formal) loop
5631 -- Create extra formal for supporting the attribute 'Constrained.
5632 -- The case of a private type view without discriminants also
5633 -- requires the extra formal if the underlying type has defaulted
5636 if Ekind (Formal) /= E_In_Parameter then
5637 if Present (P_Formal) then
5638 Formal_Type := Etype (P_Formal);
5640 Formal_Type := Etype (Formal);
5643 -- Do not produce extra formals for Unchecked_Union parameters.
5644 -- Jump directly to the end of the loop.
5646 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5647 goto Skip_Extra_Formal_Generation;
5650 if not Has_Discriminants (Formal_Type)
5651 and then Ekind (Formal_Type) in Private_Kind
5652 and then Present (Underlying_Type (Formal_Type))
5654 Formal_Type := Underlying_Type (Formal_Type);
5657 if Has_Discriminants (Formal_Type)
5658 and then not Is_Constrained (Formal_Type)
5659 and then not Is_Indefinite_Subtype (Formal_Type)
5661 Set_Extra_Constrained
5662 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5666 -- Create extra formal for supporting accessibility checking. This
5667 -- is done for both anonymous access formals and formals of named
5668 -- access types that are marked as controlling formals. The latter
5669 -- case can occur when Expand_Dispatching_Call creates a subprogram
5670 -- type and substitutes the types of access-to-class-wide actuals
5671 -- for the anonymous access-to-specific-type of controlling formals.
5672 -- Base_Type is applied because in cases where there is a null
5673 -- exclusion the formal may have an access subtype.
5675 -- This is suppressed if we specifically suppress accessibility
5676 -- checks at the package level for either the subprogram, or the
5677 -- package in which it resides. However, we do not suppress it
5678 -- simply if the scope has accessibility checks suppressed, since
5679 -- this could cause trouble when clients are compiled with a
5680 -- different suppression setting. The explicit checks at the
5681 -- package level are safe from this point of view.
5683 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5684 or else (Is_Controlling_Formal (Formal)
5685 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5687 (Explicit_Suppress (E, Accessibility_Check)
5689 Explicit_Suppress (Scope (E), Accessibility_Check))
5692 or else Present (Extra_Accessibility (P_Formal)))
5694 Set_Extra_Accessibility
5695 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5698 -- This label is required when skipping extra formal generation for
5699 -- Unchecked_Union parameters.
5701 <<Skip_Extra_Formal_Generation>>
5703 if Present (P_Formal) then
5704 Next_Formal (P_Formal);
5707 Next_Formal (Formal);
5710 <<Test_For_BIP_Extras>>
5712 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5713 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5715 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
5717 Result_Subt : constant Entity_Id := Etype (E);
5719 Discard : Entity_Id;
5720 pragma Warnings (Off, Discard);
5723 -- In the case of functions with unconstrained result subtypes,
5724 -- add a 4-state formal indicating whether the return object is
5725 -- allocated by the caller (1), or should be allocated by the
5726 -- callee on the secondary stack (2), in the global heap (3), or
5727 -- in a user-defined storage pool (4). For the moment we just use
5728 -- Natural for the type of this formal. Note that this formal
5729 -- isn't usually needed in the case where the result subtype is
5730 -- constrained, but it is needed when the function has a tagged
5731 -- result, because generally such functions can be called in a
5732 -- dispatching context and such calls must be handled like calls
5733 -- to a class-wide function.
5735 if not Is_Constrained (Underlying_Type (Result_Subt))
5736 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5740 (E, Standard_Natural,
5741 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5744 -- For functions whose result type has controlled parts, we have
5745 -- an extra formal of type System.Finalization_Implementation.
5746 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5747 -- finalization list (which is itself a pointer). This extra
5748 -- formal is then passed along to Move_Final_List in case of
5749 -- successful completion of a return statement. We cannot pass an
5750 -- 'in out' parameter, because we need to update the finalization
5751 -- list during an abort-deferred region, rather than using
5752 -- copy-back after the function returns. This is true even if we
5753 -- are able to get away with having 'in out' parameters, which are
5754 -- normally illegal for functions. This formal is also needed when
5755 -- the function has a tagged result.
5757 if Needs_BIP_Final_List (E) then
5760 (E, RTE (RE_Finalizable_Ptr_Ptr),
5761 E, BIP_Formal_Suffix (BIP_Final_List));
5764 -- If the result type contains tasks, we have two extra formals:
5765 -- the master of the tasks to be created, and the caller's
5766 -- activation chain.
5768 if Has_Task (Result_Subt) then
5771 (E, RTE (RE_Master_Id),
5772 E, BIP_Formal_Suffix (BIP_Master));
5775 (E, RTE (RE_Activation_Chain_Access),
5776 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5779 -- All build-in-place functions get an extra formal that will be
5780 -- passed the address of the return object within the caller.
5783 Formal_Type : constant Entity_Id :=
5785 (E_Anonymous_Access_Type, E,
5786 Scope_Id => Scope (E));
5788 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5789 Set_Etype (Formal_Type, Formal_Type);
5790 Set_Depends_On_Private
5791 (Formal_Type, Has_Private_Component (Formal_Type));
5792 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5793 Set_Is_Access_Constant (Formal_Type, False);
5795 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5796 -- the designated type comes from the limited view (for
5797 -- back-end purposes).
5799 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5801 Layout_Type (Formal_Type);
5805 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5809 end Create_Extra_Formals;
5811 -----------------------------
5812 -- Enter_Overloaded_Entity --
5813 -----------------------------
5815 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5816 E : Entity_Id := Current_Entity_In_Scope (S);
5817 C_E : Entity_Id := Current_Entity (S);
5821 Set_Has_Homonym (E);
5822 Set_Has_Homonym (S);
5825 Set_Is_Immediately_Visible (S);
5826 Set_Scope (S, Current_Scope);
5828 -- Chain new entity if front of homonym in current scope, so that
5829 -- homonyms are contiguous.
5834 while Homonym (C_E) /= E loop
5835 C_E := Homonym (C_E);
5838 Set_Homonym (C_E, S);
5842 Set_Current_Entity (S);
5847 Append_Entity (S, Current_Scope);
5848 Set_Public_Status (S);
5850 if Debug_Flag_E then
5851 Write_Str ("New overloaded entity chain: ");
5852 Write_Name (Chars (S));
5855 while Present (E) loop
5856 Write_Str (" "); Write_Int (Int (E));
5863 -- Generate warning for hiding
5866 and then Comes_From_Source (S)
5867 and then In_Extended_Main_Source_Unit (S)
5874 -- Warn unless genuine overloading
5876 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5877 and then (Is_Immediately_Visible (E)
5879 Is_Potentially_Use_Visible (S))
5881 Error_Msg_Sloc := Sloc (E);
5882 Error_Msg_N ("declaration of & hides one#?", S);
5886 end Enter_Overloaded_Entity;
5888 -----------------------------
5889 -- Check_Untagged_Equality --
5890 -----------------------------
5892 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
5893 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
5894 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
5898 if Nkind (Decl) = N_Subprogram_Declaration
5899 and then Is_Record_Type (Typ)
5900 and then not Is_Tagged_Type (Typ)
5902 if Is_Frozen (Typ) then
5904 ("equality operator must be declared "
5905 & "before type& is frozen", Eq_Op, Typ);
5907 Obj_Decl := Next (Parent (Typ));
5908 while Present (Obj_Decl)
5909 and then Obj_Decl /= Decl
5911 if Nkind (Obj_Decl) = N_Object_Declaration
5912 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
5914 Error_Msg_NE ("type& is frozen by declaration?",
5917 ("\an equality operator cannot be declared after this "
5918 & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
5925 elsif not In_Same_List (Parent (Typ), Decl)
5926 and then not Is_Limited_Type (Typ)
5928 Error_Msg_N ("equality operator appears too late", Eq_Op);
5931 end Check_Untagged_Equality;
5933 -----------------------------
5934 -- Find_Corresponding_Spec --
5935 -----------------------------
5937 function Find_Corresponding_Spec
5939 Post_Error : Boolean := True) return Entity_Id
5941 Spec : constant Node_Id := Specification (N);
5942 Designator : constant Entity_Id := Defining_Entity (Spec);
5947 E := Current_Entity (Designator);
5948 while Present (E) loop
5950 -- We are looking for a matching spec. It must have the same scope,
5951 -- and the same name, and either be type conformant, or be the case
5952 -- of a library procedure spec and its body (which belong to one
5953 -- another regardless of whether they are type conformant or not).
5955 if Scope (E) = Current_Scope then
5956 if Current_Scope = Standard_Standard
5957 or else (Ekind (E) = Ekind (Designator)
5958 and then Type_Conformant (E, Designator))
5960 -- Within an instantiation, we know that spec and body are
5961 -- subtype conformant, because they were subtype conformant
5962 -- in the generic. We choose the subtype-conformant entity
5963 -- here as well, to resolve spurious ambiguities in the
5964 -- instance that were not present in the generic (i.e. when
5965 -- two different types are given the same actual). If we are
5966 -- looking for a spec to match a body, full conformance is
5970 Set_Convention (Designator, Convention (E));
5972 if Nkind (N) = N_Subprogram_Body
5973 and then Present (Homonym (E))
5974 and then not Fully_Conformant (E, Designator)
5978 elsif not Subtype_Conformant (E, Designator) then
5983 if not Has_Completion (E) then
5984 if Nkind (N) /= N_Subprogram_Body_Stub then
5985 Set_Corresponding_Spec (N, E);
5988 Set_Has_Completion (E);
5991 elsif Nkind (Parent (N)) = N_Subunit then
5993 -- If this is the proper body of a subunit, the completion
5994 -- flag is set when analyzing the stub.
5998 -- If E is an internal function with a controlling result
5999 -- that was created for an operation inherited by a null
6000 -- extension, it may be overridden by a body without a previous
6001 -- spec (one more reason why these should be shunned). In that
6002 -- case remove the generated body if present, because the
6003 -- current one is the explicit overriding.
6005 elsif Ekind (E) = E_Function
6006 and then Ada_Version >= Ada_2005
6007 and then not Comes_From_Source (E)
6008 and then Has_Controlling_Result (E)
6009 and then Is_Null_Extension (Etype (E))
6010 and then Comes_From_Source (Spec)
6012 Set_Has_Completion (E, False);
6015 and then Nkind (Parent (E)) = N_Function_Specification
6018 (Unit_Declaration_Node
6019 (Corresponding_Body (Unit_Declaration_Node (E))));
6023 -- If expansion is disabled, or if the wrapper function has
6024 -- not been generated yet, this a late body overriding an
6025 -- inherited operation, or it is an overriding by some other
6026 -- declaration before the controlling result is frozen. In
6027 -- either case this is a declaration of a new entity.
6033 -- If the body already exists, then this is an error unless
6034 -- the previous declaration is the implicit declaration of a
6035 -- derived subprogram, or this is a spurious overloading in an
6038 elsif No (Alias (E))
6039 and then not Is_Intrinsic_Subprogram (E)
6040 and then not In_Instance
6043 Error_Msg_Sloc := Sloc (E);
6045 if Is_Imported (E) then
6047 ("body not allowed for imported subprogram & declared#",
6050 Error_Msg_NE ("duplicate body for & declared#", N, E);
6054 -- Child units cannot be overloaded, so a conformance mismatch
6055 -- between body and a previous spec is an error.
6057 elsif Is_Child_Unit (E)
6059 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6061 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6066 ("body of child unit does not match previous declaration", N);
6074 -- On exit, we know that no previous declaration of subprogram exists
6077 end Find_Corresponding_Spec;
6079 ----------------------
6080 -- Fully_Conformant --
6081 ----------------------
6083 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6086 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6088 end Fully_Conformant;
6090 ----------------------------------
6091 -- Fully_Conformant_Expressions --
6092 ----------------------------------
6094 function Fully_Conformant_Expressions
6095 (Given_E1 : Node_Id;
6096 Given_E2 : Node_Id) return Boolean
6098 E1 : constant Node_Id := Original_Node (Given_E1);
6099 E2 : constant Node_Id := Original_Node (Given_E2);
6100 -- We always test conformance on original nodes, since it is possible
6101 -- for analysis and/or expansion to make things look as though they
6102 -- conform when they do not, e.g. by converting 1+2 into 3.
6104 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6105 renames Fully_Conformant_Expressions;
6107 function FCL (L1, L2 : List_Id) return Boolean;
6108 -- Compare elements of two lists for conformance. Elements have to
6109 -- be conformant, and actuals inserted as default parameters do not
6110 -- match explicit actuals with the same value.
6112 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6113 -- Compare an operator node with a function call
6119 function FCL (L1, L2 : List_Id) return Boolean is
6123 if L1 = No_List then
6129 if L2 = No_List then
6135 -- Compare two lists, skipping rewrite insertions (we want to
6136 -- compare the original trees, not the expanded versions!)
6139 if Is_Rewrite_Insertion (N1) then
6141 elsif Is_Rewrite_Insertion (N2) then
6147 elsif not FCE (N1, N2) then
6160 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6161 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6166 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6171 Act := First (Actuals);
6173 if Nkind (Op_Node) in N_Binary_Op then
6174 if not FCE (Left_Opnd (Op_Node), Act) then
6181 return Present (Act)
6182 and then FCE (Right_Opnd (Op_Node), Act)
6183 and then No (Next (Act));
6187 -- Start of processing for Fully_Conformant_Expressions
6190 -- Non-conformant if paren count does not match. Note: if some idiot
6191 -- complains that we don't do this right for more than 3 levels of
6192 -- parentheses, they will be treated with the respect they deserve!
6194 if Paren_Count (E1) /= Paren_Count (E2) then
6197 -- If same entities are referenced, then they are conformant even if
6198 -- they have different forms (RM 8.3.1(19-20)).
6200 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6201 if Present (Entity (E1)) then
6202 return Entity (E1) = Entity (E2)
6203 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6204 and then Ekind (Entity (E1)) = E_Discriminant
6205 and then Ekind (Entity (E2)) = E_In_Parameter);
6207 elsif Nkind (E1) = N_Expanded_Name
6208 and then Nkind (E2) = N_Expanded_Name
6209 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6210 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6212 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6215 -- Identifiers in component associations don't always have
6216 -- entities, but their names must conform.
6218 return Nkind (E1) = N_Identifier
6219 and then Nkind (E2) = N_Identifier
6220 and then Chars (E1) = Chars (E2);
6223 elsif Nkind (E1) = N_Character_Literal
6224 and then Nkind (E2) = N_Expanded_Name
6226 return Nkind (Selector_Name (E2)) = N_Character_Literal
6227 and then Chars (E1) = Chars (Selector_Name (E2));
6229 elsif Nkind (E2) = N_Character_Literal
6230 and then Nkind (E1) = N_Expanded_Name
6232 return Nkind (Selector_Name (E1)) = N_Character_Literal
6233 and then Chars (E2) = Chars (Selector_Name (E1));
6235 elsif Nkind (E1) in N_Op
6236 and then Nkind (E2) = N_Function_Call
6238 return FCO (E1, E2);
6240 elsif Nkind (E2) in N_Op
6241 and then Nkind (E1) = N_Function_Call
6243 return FCO (E2, E1);
6245 -- Otherwise we must have the same syntactic entity
6247 elsif Nkind (E1) /= Nkind (E2) then
6250 -- At this point, we specialize by node type
6257 FCL (Expressions (E1), Expressions (E2))
6259 FCL (Component_Associations (E1),
6260 Component_Associations (E2));
6263 if Nkind (Expression (E1)) = N_Qualified_Expression
6265 Nkind (Expression (E2)) = N_Qualified_Expression
6267 return FCE (Expression (E1), Expression (E2));
6269 -- Check that the subtype marks and any constraints
6274 Indic1 : constant Node_Id := Expression (E1);
6275 Indic2 : constant Node_Id := Expression (E2);
6280 if Nkind (Indic1) /= N_Subtype_Indication then
6282 Nkind (Indic2) /= N_Subtype_Indication
6283 and then Entity (Indic1) = Entity (Indic2);
6285 elsif Nkind (Indic2) /= N_Subtype_Indication then
6287 Nkind (Indic1) /= N_Subtype_Indication
6288 and then Entity (Indic1) = Entity (Indic2);
6291 if Entity (Subtype_Mark (Indic1)) /=
6292 Entity (Subtype_Mark (Indic2))
6297 Elt1 := First (Constraints (Constraint (Indic1)));
6298 Elt2 := First (Constraints (Constraint (Indic2)));
6299 while Present (Elt1) and then Present (Elt2) loop
6300 if not FCE (Elt1, Elt2) then
6313 when N_Attribute_Reference =>
6315 Attribute_Name (E1) = Attribute_Name (E2)
6316 and then FCL (Expressions (E1), Expressions (E2));
6320 Entity (E1) = Entity (E2)
6321 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6322 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6324 when N_Short_Circuit | N_Membership_Test =>
6326 FCE (Left_Opnd (E1), Left_Opnd (E2))
6328 FCE (Right_Opnd (E1), Right_Opnd (E2));
6330 when N_Case_Expression =>
6336 if not FCE (Expression (E1), Expression (E2)) then
6340 Alt1 := First (Alternatives (E1));
6341 Alt2 := First (Alternatives (E2));
6343 if Present (Alt1) /= Present (Alt2) then
6345 elsif No (Alt1) then
6349 if not FCE (Expression (Alt1), Expression (Alt2))
6350 or else not FCL (Discrete_Choices (Alt1),
6351 Discrete_Choices (Alt2))
6362 when N_Character_Literal =>
6364 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6366 when N_Component_Association =>
6368 FCL (Choices (E1), Choices (E2))
6370 FCE (Expression (E1), Expression (E2));
6372 when N_Conditional_Expression =>
6374 FCL (Expressions (E1), Expressions (E2));
6376 when N_Explicit_Dereference =>
6378 FCE (Prefix (E1), Prefix (E2));
6380 when N_Extension_Aggregate =>
6382 FCL (Expressions (E1), Expressions (E2))
6383 and then Null_Record_Present (E1) =
6384 Null_Record_Present (E2)
6385 and then FCL (Component_Associations (E1),
6386 Component_Associations (E2));
6388 when N_Function_Call =>
6390 FCE (Name (E1), Name (E2))
6392 FCL (Parameter_Associations (E1),
6393 Parameter_Associations (E2));
6395 when N_Indexed_Component =>
6397 FCE (Prefix (E1), Prefix (E2))
6399 FCL (Expressions (E1), Expressions (E2));
6401 when N_Integer_Literal =>
6402 return (Intval (E1) = Intval (E2));
6407 when N_Operator_Symbol =>
6409 Chars (E1) = Chars (E2);
6411 when N_Others_Choice =>
6414 when N_Parameter_Association =>
6416 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6417 and then FCE (Explicit_Actual_Parameter (E1),
6418 Explicit_Actual_Parameter (E2));
6420 when N_Qualified_Expression =>
6422 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6424 FCE (Expression (E1), Expression (E2));
6428 FCE (Low_Bound (E1), Low_Bound (E2))
6430 FCE (High_Bound (E1), High_Bound (E2));
6432 when N_Real_Literal =>
6433 return (Realval (E1) = Realval (E2));
6435 when N_Selected_Component =>
6437 FCE (Prefix (E1), Prefix (E2))
6439 FCE (Selector_Name (E1), Selector_Name (E2));
6443 FCE (Prefix (E1), Prefix (E2))
6445 FCE (Discrete_Range (E1), Discrete_Range (E2));
6447 when N_String_Literal =>
6449 S1 : constant String_Id := Strval (E1);
6450 S2 : constant String_Id := Strval (E2);
6451 L1 : constant Nat := String_Length (S1);
6452 L2 : constant Nat := String_Length (S2);
6459 for J in 1 .. L1 loop
6460 if Get_String_Char (S1, J) /=
6461 Get_String_Char (S2, J)
6471 when N_Type_Conversion =>
6473 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6475 FCE (Expression (E1), Expression (E2));
6479 Entity (E1) = Entity (E2)
6481 FCE (Right_Opnd (E1), Right_Opnd (E2));
6483 when N_Unchecked_Type_Conversion =>
6485 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6487 FCE (Expression (E1), Expression (E2));
6489 -- All other node types cannot appear in this context. Strictly
6490 -- we should raise a fatal internal error. Instead we just ignore
6491 -- the nodes. This means that if anyone makes a mistake in the
6492 -- expander and mucks an expression tree irretrievably, the
6493 -- result will be a failure to detect a (probably very obscure)
6494 -- case of non-conformance, which is better than bombing on some
6495 -- case where two expressions do in fact conform.
6502 end Fully_Conformant_Expressions;
6504 ----------------------------------------
6505 -- Fully_Conformant_Discrete_Subtypes --
6506 ----------------------------------------
6508 function Fully_Conformant_Discrete_Subtypes
6509 (Given_S1 : Node_Id;
6510 Given_S2 : Node_Id) return Boolean
6512 S1 : constant Node_Id := Original_Node (Given_S1);
6513 S2 : constant Node_Id := Original_Node (Given_S2);
6515 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6516 -- Special-case for a bound given by a discriminant, which in the body
6517 -- is replaced with the discriminal of the enclosing type.
6519 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6520 -- Check both bounds
6522 -----------------------
6523 -- Conforming_Bounds --
6524 -----------------------
6526 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6528 if Is_Entity_Name (B1)
6529 and then Is_Entity_Name (B2)
6530 and then Ekind (Entity (B1)) = E_Discriminant
6532 return Chars (B1) = Chars (B2);
6535 return Fully_Conformant_Expressions (B1, B2);
6537 end Conforming_Bounds;
6539 -----------------------
6540 -- Conforming_Ranges --
6541 -----------------------
6543 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6546 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6548 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6549 end Conforming_Ranges;
6551 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6554 if Nkind (S1) /= Nkind (S2) then
6557 elsif Is_Entity_Name (S1) then
6558 return Entity (S1) = Entity (S2);
6560 elsif Nkind (S1) = N_Range then
6561 return Conforming_Ranges (S1, S2);
6563 elsif Nkind (S1) = N_Subtype_Indication then
6565 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6568 (Range_Expression (Constraint (S1)),
6569 Range_Expression (Constraint (S2)));
6573 end Fully_Conformant_Discrete_Subtypes;
6575 --------------------
6576 -- Install_Entity --
6577 --------------------
6579 procedure Install_Entity (E : Entity_Id) is
6580 Prev : constant Entity_Id := Current_Entity (E);
6582 Set_Is_Immediately_Visible (E);
6583 Set_Current_Entity (E);
6584 Set_Homonym (E, Prev);
6587 ---------------------
6588 -- Install_Formals --
6589 ---------------------
6591 procedure Install_Formals (Id : Entity_Id) is
6594 F := First_Formal (Id);
6595 while Present (F) loop
6599 end Install_Formals;
6601 -----------------------------
6602 -- Is_Interface_Conformant --
6603 -----------------------------
6605 function Is_Interface_Conformant
6606 (Tagged_Type : Entity_Id;
6607 Iface_Prim : Entity_Id;
6608 Prim : Entity_Id) return Boolean
6610 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6611 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6614 pragma Assert (Is_Subprogram (Iface_Prim)
6615 and then Is_Subprogram (Prim)
6616 and then Is_Dispatching_Operation (Iface_Prim)
6617 and then Is_Dispatching_Operation (Prim));
6619 pragma Assert (Is_Interface (Iface)
6620 or else (Present (Alias (Iface_Prim))
6623 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6625 if Prim = Iface_Prim
6626 or else not Is_Subprogram (Prim)
6627 or else Ekind (Prim) /= Ekind (Iface_Prim)
6628 or else not Is_Dispatching_Operation (Prim)
6629 or else Scope (Prim) /= Scope (Tagged_Type)
6631 or else Base_Type (Typ) /= Tagged_Type
6632 or else not Primitive_Names_Match (Iface_Prim, Prim)
6636 -- Case of a procedure, or a function that does not have a controlling
6637 -- result (I or access I).
6639 elsif Ekind (Iface_Prim) = E_Procedure
6640 or else Etype (Prim) = Etype (Iface_Prim)
6641 or else not Has_Controlling_Result (Prim)
6643 return Type_Conformant
6644 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6646 -- Case of a function returning an interface, or an access to one.
6647 -- Check that the return types correspond.
6649 elsif Implements_Interface (Typ, Iface) then
6650 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6652 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6657 Type_Conformant (Prim, Iface_Prim,
6658 Skip_Controlling_Formals => True);
6664 end Is_Interface_Conformant;
6666 ---------------------------------
6667 -- Is_Non_Overriding_Operation --
6668 ---------------------------------
6670 function Is_Non_Overriding_Operation
6671 (Prev_E : Entity_Id;
6672 New_E : Entity_Id) return Boolean
6676 G_Typ : Entity_Id := Empty;
6678 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6679 -- If F_Type is a derived type associated with a generic actual subtype,
6680 -- then return its Generic_Parent_Type attribute, else return Empty.
6682 function Types_Correspond
6683 (P_Type : Entity_Id;
6684 N_Type : Entity_Id) return Boolean;
6685 -- Returns true if and only if the types (or designated types in the
6686 -- case of anonymous access types) are the same or N_Type is derived
6687 -- directly or indirectly from P_Type.
6689 -----------------------------
6690 -- Get_Generic_Parent_Type --
6691 -----------------------------
6693 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6698 if Is_Derived_Type (F_Typ)
6699 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6701 -- The tree must be traversed to determine the parent subtype in
6702 -- the generic unit, which unfortunately isn't always available
6703 -- via semantic attributes. ??? (Note: The use of Original_Node
6704 -- is needed for cases where a full derived type has been
6707 Indic := Subtype_Indication
6708 (Type_Definition (Original_Node (Parent (F_Typ))));
6710 if Nkind (Indic) = N_Subtype_Indication then
6711 G_Typ := Entity (Subtype_Mark (Indic));
6713 G_Typ := Entity (Indic);
6716 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6717 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6719 return Generic_Parent_Type (Parent (G_Typ));
6724 end Get_Generic_Parent_Type;
6726 ----------------------
6727 -- Types_Correspond --
6728 ----------------------
6730 function Types_Correspond
6731 (P_Type : Entity_Id;
6732 N_Type : Entity_Id) return Boolean
6734 Prev_Type : Entity_Id := Base_Type (P_Type);
6735 New_Type : Entity_Id := Base_Type (N_Type);
6738 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6739 Prev_Type := Designated_Type (Prev_Type);
6742 if Ekind (New_Type) = E_Anonymous_Access_Type then
6743 New_Type := Designated_Type (New_Type);
6746 if Prev_Type = New_Type then
6749 elsif not Is_Class_Wide_Type (New_Type) then
6750 while Etype (New_Type) /= New_Type loop
6751 New_Type := Etype (New_Type);
6752 if New_Type = Prev_Type then
6758 end Types_Correspond;
6760 -- Start of processing for Is_Non_Overriding_Operation
6763 -- In the case where both operations are implicit derived subprograms
6764 -- then neither overrides the other. This can only occur in certain
6765 -- obscure cases (e.g., derivation from homographs created in a generic
6768 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6771 elsif Ekind (Current_Scope) = E_Package
6772 and then Is_Generic_Instance (Current_Scope)
6773 and then In_Private_Part (Current_Scope)
6774 and then Comes_From_Source (New_E)
6776 -- We examine the formals and result subtype of the inherited
6777 -- operation, to determine whether their type is derived from (the
6778 -- instance of) a generic type.
6780 Formal := First_Formal (Prev_E);
6781 while Present (Formal) loop
6782 F_Typ := Base_Type (Etype (Formal));
6784 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6785 F_Typ := Designated_Type (F_Typ);
6788 G_Typ := Get_Generic_Parent_Type (F_Typ);
6790 Next_Formal (Formal);
6793 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6794 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6801 -- If the generic type is a private type, then the original operation
6802 -- was not overriding in the generic, because there was no primitive
6803 -- operation to override.
6805 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6806 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6807 N_Formal_Private_Type_Definition
6811 -- The generic parent type is the ancestor of a formal derived
6812 -- type declaration. We need to check whether it has a primitive
6813 -- operation that should be overridden by New_E in the generic.
6817 P_Formal : Entity_Id;
6818 N_Formal : Entity_Id;
6822 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6825 while Present (Prim_Elt) loop
6826 P_Prim := Node (Prim_Elt);
6828 if Chars (P_Prim) = Chars (New_E)
6829 and then Ekind (P_Prim) = Ekind (New_E)
6831 P_Formal := First_Formal (P_Prim);
6832 N_Formal := First_Formal (New_E);
6833 while Present (P_Formal) and then Present (N_Formal) loop
6834 P_Typ := Etype (P_Formal);
6835 N_Typ := Etype (N_Formal);
6837 if not Types_Correspond (P_Typ, N_Typ) then
6841 Next_Entity (P_Formal);
6842 Next_Entity (N_Formal);
6845 -- Found a matching primitive operation belonging to the
6846 -- formal ancestor type, so the new subprogram is
6850 and then No (N_Formal)
6851 and then (Ekind (New_E) /= E_Function
6854 (Etype (P_Prim), Etype (New_E)))
6860 Next_Elmt (Prim_Elt);
6863 -- If no match found, then the new subprogram does not
6864 -- override in the generic (nor in the instance).
6872 end Is_Non_Overriding_Operation;
6874 ------------------------------
6875 -- Make_Inequality_Operator --
6876 ------------------------------
6878 -- S is the defining identifier of an equality operator. We build a
6879 -- subprogram declaration with the right signature. This operation is
6880 -- intrinsic, because it is always expanded as the negation of the
6881 -- call to the equality function.
6883 procedure Make_Inequality_Operator (S : Entity_Id) is
6884 Loc : constant Source_Ptr := Sloc (S);
6887 Op_Name : Entity_Id;
6889 FF : constant Entity_Id := First_Formal (S);
6890 NF : constant Entity_Id := Next_Formal (FF);
6893 -- Check that equality was properly defined, ignore call if not
6900 A : constant Entity_Id :=
6901 Make_Defining_Identifier (Sloc (FF),
6902 Chars => Chars (FF));
6904 B : constant Entity_Id :=
6905 Make_Defining_Identifier (Sloc (NF),
6906 Chars => Chars (NF));
6909 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6911 Formals := New_List (
6912 Make_Parameter_Specification (Loc,
6913 Defining_Identifier => A,
6915 New_Reference_To (Etype (First_Formal (S)),
6916 Sloc (Etype (First_Formal (S))))),
6918 Make_Parameter_Specification (Loc,
6919 Defining_Identifier => B,
6921 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6922 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6925 Make_Subprogram_Declaration (Loc,
6927 Make_Function_Specification (Loc,
6928 Defining_Unit_Name => Op_Name,
6929 Parameter_Specifications => Formals,
6930 Result_Definition =>
6931 New_Reference_To (Standard_Boolean, Loc)));
6933 -- Insert inequality right after equality if it is explicit or after
6934 -- the derived type when implicit. These entities are created only
6935 -- for visibility purposes, and eventually replaced in the course of
6936 -- expansion, so they do not need to be attached to the tree and seen
6937 -- by the back-end. Keeping them internal also avoids spurious
6938 -- freezing problems. The declaration is inserted in the tree for
6939 -- analysis, and removed afterwards. If the equality operator comes
6940 -- from an explicit declaration, attach the inequality immediately
6941 -- after. Else the equality is inherited from a derived type
6942 -- declaration, so insert inequality after that declaration.
6944 if No (Alias (S)) then
6945 Insert_After (Unit_Declaration_Node (S), Decl);
6946 elsif Is_List_Member (Parent (S)) then
6947 Insert_After (Parent (S), Decl);
6949 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6952 Mark_Rewrite_Insertion (Decl);
6953 Set_Is_Intrinsic_Subprogram (Op_Name);
6956 Set_Has_Completion (Op_Name);
6957 Set_Corresponding_Equality (Op_Name, S);
6958 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6960 end Make_Inequality_Operator;
6962 ----------------------
6963 -- May_Need_Actuals --
6964 ----------------------
6966 procedure May_Need_Actuals (Fun : Entity_Id) is
6971 F := First_Formal (Fun);
6973 while Present (F) loop
6974 if No (Default_Value (F)) then
6982 Set_Needs_No_Actuals (Fun, B);
6983 end May_Need_Actuals;
6985 ---------------------
6986 -- Mode_Conformant --
6987 ---------------------
6989 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6992 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6994 end Mode_Conformant;
6996 ---------------------------
6997 -- New_Overloaded_Entity --
6998 ---------------------------
7000 procedure New_Overloaded_Entity
7002 Derived_Type : Entity_Id := Empty)
7004 Overridden_Subp : Entity_Id := Empty;
7005 -- Set if the current scope has an operation that is type-conformant
7006 -- with S, and becomes hidden by S.
7008 Is_Primitive_Subp : Boolean;
7009 -- Set to True if the new subprogram is primitive
7012 -- Entity that S overrides
7014 Prev_Vis : Entity_Id := Empty;
7015 -- Predecessor of E in Homonym chain
7017 procedure Check_For_Primitive_Subprogram
7018 (Is_Primitive : out Boolean;
7019 Is_Overriding : Boolean := False);
7020 -- If the subprogram being analyzed is a primitive operation of the type
7021 -- of a formal or result, set the Has_Primitive_Operations flag on the
7022 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7023 -- corresponding flag on the entity itself for later use.
7025 procedure Check_Synchronized_Overriding
7026 (Def_Id : Entity_Id;
7027 Overridden_Subp : out Entity_Id);
7028 -- First determine if Def_Id is an entry or a subprogram either defined
7029 -- in the scope of a task or protected type, or is a primitive of such
7030 -- a type. Check whether Def_Id overrides a subprogram of an interface
7031 -- implemented by the synchronized type, return the overridden entity
7034 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7035 -- Check that E is declared in the private part of the current package,
7036 -- or in the package body, where it may hide a previous declaration.
7037 -- We can't use In_Private_Part by itself because this flag is also
7038 -- set when freezing entities, so we must examine the place of the
7039 -- declaration in the tree, and recognize wrapper packages as well.
7041 function Is_Overriding_Alias
7043 New_E : Entity_Id) return Boolean;
7044 -- Check whether new subprogram and old subprogram are both inherited
7045 -- from subprograms that have distinct dispatch table entries. This can
7046 -- occur with derivations from instances with accidental homonyms.
7047 -- The function is conservative given that the converse is only true
7048 -- within instances that contain accidental overloadings.
7050 ------------------------------------
7051 -- Check_For_Primitive_Subprogram --
7052 ------------------------------------
7054 procedure Check_For_Primitive_Subprogram
7055 (Is_Primitive : out Boolean;
7056 Is_Overriding : Boolean := False)
7062 function Visible_Part_Type (T : Entity_Id) return Boolean;
7063 -- Returns true if T is declared in the visible part of the current
7064 -- package scope; otherwise returns false. Assumes that T is declared
7067 procedure Check_Private_Overriding (T : Entity_Id);
7068 -- Checks that if a primitive abstract subprogram of a visible
7069 -- abstract type is declared in a private part, then it must override
7070 -- an abstract subprogram declared in the visible part. Also checks
7071 -- that if a primitive function with a controlling result is declared
7072 -- in a private part, then it must override a function declared in
7073 -- the visible part.
7075 ------------------------------
7076 -- Check_Private_Overriding --
7077 ------------------------------
7079 procedure Check_Private_Overriding (T : Entity_Id) is
7081 if Is_Package_Or_Generic_Package (Current_Scope)
7082 and then In_Private_Part (Current_Scope)
7083 and then Visible_Part_Type (T)
7084 and then not In_Instance
7086 if Is_Abstract_Type (T)
7087 and then Is_Abstract_Subprogram (S)
7088 and then (not Is_Overriding
7089 or else not Is_Abstract_Subprogram (E))
7092 ("abstract subprograms must be visible "
7093 & "(RM 3.9.3(10))!", S);
7095 elsif Ekind (S) = E_Function
7096 and then not Is_Overriding
7098 if Is_Tagged_Type (T)
7099 and then T = Base_Type (Etype (S))
7102 ("private function with tagged result must"
7103 & " override visible-part function", S);
7105 ("\move subprogram to the visible part"
7106 & " (RM 3.9.3(10))", S);
7108 -- AI05-0073: extend this test to the case of a function
7109 -- with a controlling access result.
7111 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7112 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7114 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7115 and then Ada_Version >= Ada_2012
7118 ("private function with controlling access result "
7119 & "must override visible-part function", S);
7121 ("\move subprogram to the visible part"
7122 & " (RM 3.9.3(10))", S);
7126 end Check_Private_Overriding;
7128 -----------------------
7129 -- Visible_Part_Type --
7130 -----------------------
7132 function Visible_Part_Type (T : Entity_Id) return Boolean is
7133 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7137 -- If the entity is a private type, then it must be declared in a
7140 if Ekind (T) in Private_Kind then
7144 -- Otherwise, we traverse the visible part looking for its
7145 -- corresponding declaration. We cannot use the declaration
7146 -- node directly because in the private part the entity of a
7147 -- private type is the one in the full view, which does not
7148 -- indicate that it is the completion of something visible.
7150 N := First (Visible_Declarations (Specification (P)));
7151 while Present (N) loop
7152 if Nkind (N) = N_Full_Type_Declaration
7153 and then Present (Defining_Identifier (N))
7154 and then T = Defining_Identifier (N)
7158 elsif Nkind_In (N, N_Private_Type_Declaration,
7159 N_Private_Extension_Declaration)
7160 and then Present (Defining_Identifier (N))
7161 and then T = Full_View (Defining_Identifier (N))
7170 end Visible_Part_Type;
7172 -- Start of processing for Check_For_Primitive_Subprogram
7175 Is_Primitive := False;
7177 if not Comes_From_Source (S) then
7180 -- If subprogram is at library level, it is not primitive operation
7182 elsif Current_Scope = Standard_Standard then
7185 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7186 and then not In_Package_Body (Current_Scope))
7187 or else Is_Overriding
7189 -- For function, check return type
7191 if Ekind (S) = E_Function then
7192 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7193 F_Typ := Designated_Type (Etype (S));
7198 B_Typ := Base_Type (F_Typ);
7200 if Scope (B_Typ) = Current_Scope
7201 and then not Is_Class_Wide_Type (B_Typ)
7202 and then not Is_Generic_Type (B_Typ)
7204 Is_Primitive := True;
7205 Set_Has_Primitive_Operations (B_Typ);
7206 Set_Is_Primitive (S);
7207 Check_Private_Overriding (B_Typ);
7211 -- For all subprograms, check formals
7213 Formal := First_Formal (S);
7214 while Present (Formal) loop
7215 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7216 F_Typ := Designated_Type (Etype (Formal));
7218 F_Typ := Etype (Formal);
7221 B_Typ := Base_Type (F_Typ);
7223 if Ekind (B_Typ) = E_Access_Subtype then
7224 B_Typ := Base_Type (B_Typ);
7227 if Scope (B_Typ) = Current_Scope
7228 and then not Is_Class_Wide_Type (B_Typ)
7229 and then not Is_Generic_Type (B_Typ)
7231 Is_Primitive := True;
7232 Set_Is_Primitive (S);
7233 Set_Has_Primitive_Operations (B_Typ);
7234 Check_Private_Overriding (B_Typ);
7237 Next_Formal (Formal);
7240 end Check_For_Primitive_Subprogram;
7242 -----------------------------------
7243 -- Check_Synchronized_Overriding --
7244 -----------------------------------
7246 procedure Check_Synchronized_Overriding
7247 (Def_Id : Entity_Id;
7248 Overridden_Subp : out Entity_Id)
7250 Ifaces_List : Elist_Id;
7254 function Matches_Prefixed_View_Profile
7255 (Prim_Params : List_Id;
7256 Iface_Params : List_Id) return Boolean;
7257 -- Determine whether a subprogram's parameter profile Prim_Params
7258 -- matches that of a potentially overridden interface subprogram
7259 -- Iface_Params. Also determine if the type of first parameter of
7260 -- Iface_Params is an implemented interface.
7262 -----------------------------------
7263 -- Matches_Prefixed_View_Profile --
7264 -----------------------------------
7266 function Matches_Prefixed_View_Profile
7267 (Prim_Params : List_Id;
7268 Iface_Params : List_Id) return Boolean
7270 Iface_Id : Entity_Id;
7271 Iface_Param : Node_Id;
7272 Iface_Typ : Entity_Id;
7273 Prim_Id : Entity_Id;
7274 Prim_Param : Node_Id;
7275 Prim_Typ : Entity_Id;
7277 function Is_Implemented
7278 (Ifaces_List : Elist_Id;
7279 Iface : Entity_Id) return Boolean;
7280 -- Determine if Iface is implemented by the current task or
7283 --------------------
7284 -- Is_Implemented --
7285 --------------------
7287 function Is_Implemented
7288 (Ifaces_List : Elist_Id;
7289 Iface : Entity_Id) return Boolean
7291 Iface_Elmt : Elmt_Id;
7294 Iface_Elmt := First_Elmt (Ifaces_List);
7295 while Present (Iface_Elmt) loop
7296 if Node (Iface_Elmt) = Iface then
7300 Next_Elmt (Iface_Elmt);
7306 -- Start of processing for Matches_Prefixed_View_Profile
7309 Iface_Param := First (Iface_Params);
7310 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7312 if Is_Access_Type (Iface_Typ) then
7313 Iface_Typ := Designated_Type (Iface_Typ);
7316 Prim_Param := First (Prim_Params);
7318 -- The first parameter of the potentially overridden subprogram
7319 -- must be an interface implemented by Prim.
7321 if not Is_Interface (Iface_Typ)
7322 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7327 -- The checks on the object parameters are done, move onto the
7328 -- rest of the parameters.
7330 if not In_Scope then
7331 Prim_Param := Next (Prim_Param);
7334 Iface_Param := Next (Iface_Param);
7335 while Present (Iface_Param) and then Present (Prim_Param) loop
7336 Iface_Id := Defining_Identifier (Iface_Param);
7337 Iface_Typ := Find_Parameter_Type (Iface_Param);
7339 Prim_Id := Defining_Identifier (Prim_Param);
7340 Prim_Typ := Find_Parameter_Type (Prim_Param);
7342 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7343 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7344 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7346 Iface_Typ := Designated_Type (Iface_Typ);
7347 Prim_Typ := Designated_Type (Prim_Typ);
7350 -- Case of multiple interface types inside a parameter profile
7352 -- (Obj_Param : in out Iface; ...; Param : Iface)
7354 -- If the interface type is implemented, then the matching type
7355 -- in the primitive should be the implementing record type.
7357 if Ekind (Iface_Typ) = E_Record_Type
7358 and then Is_Interface (Iface_Typ)
7359 and then Is_Implemented (Ifaces_List, Iface_Typ)
7361 if Prim_Typ /= Typ then
7365 -- The two parameters must be both mode and subtype conformant
7367 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7369 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7378 -- One of the two lists contains more parameters than the other
7380 if Present (Iface_Param) or else Present (Prim_Param) then
7385 end Matches_Prefixed_View_Profile;
7387 -- Start of processing for Check_Synchronized_Overriding
7390 Overridden_Subp := Empty;
7392 -- Def_Id must be an entry or a subprogram. We should skip predefined
7393 -- primitives internally generated by the frontend; however at this
7394 -- stage predefined primitives are still not fully decorated. As a
7395 -- minor optimization we skip here internally generated subprograms.
7397 if (Ekind (Def_Id) /= E_Entry
7398 and then Ekind (Def_Id) /= E_Function
7399 and then Ekind (Def_Id) /= E_Procedure)
7400 or else not Comes_From_Source (Def_Id)
7405 -- Search for the concurrent declaration since it contains the list
7406 -- of all implemented interfaces. In this case, the subprogram is
7407 -- declared within the scope of a protected or a task type.
7409 if Present (Scope (Def_Id))
7410 and then Is_Concurrent_Type (Scope (Def_Id))
7411 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7413 Typ := Scope (Def_Id);
7416 -- The enclosing scope is not a synchronized type and the subprogram
7419 elsif No (First_Formal (Def_Id)) then
7422 -- The subprogram has formals and hence it may be a primitive of a
7426 Typ := Etype (First_Formal (Def_Id));
7428 if Is_Access_Type (Typ) then
7429 Typ := Directly_Designated_Type (Typ);
7432 if Is_Concurrent_Type (Typ)
7433 and then not Is_Generic_Actual_Type (Typ)
7437 -- This case occurs when the concurrent type is declared within
7438 -- a generic unit. As a result the corresponding record has been
7439 -- built and used as the type of the first formal, we just have
7440 -- to retrieve the corresponding concurrent type.
7442 elsif Is_Concurrent_Record_Type (Typ)
7443 and then Present (Corresponding_Concurrent_Type (Typ))
7445 Typ := Corresponding_Concurrent_Type (Typ);
7453 -- There is no overriding to check if is an inherited operation in a
7454 -- type derivation on for a generic actual.
7456 Collect_Interfaces (Typ, Ifaces_List);
7458 if Is_Empty_Elmt_List (Ifaces_List) then
7462 -- Determine whether entry or subprogram Def_Id overrides a primitive
7463 -- operation that belongs to one of the interfaces in Ifaces_List.
7466 Candidate : Entity_Id := Empty;
7467 Hom : Entity_Id := Empty;
7468 Iface_Typ : Entity_Id;
7469 Subp : Entity_Id := Empty;
7472 -- Traverse the homonym chain, looking at a potentially
7473 -- overridden subprogram that belongs to an implemented
7476 Hom := Current_Entity_In_Scope (Def_Id);
7477 while Present (Hom) loop
7481 or else not Is_Overloadable (Subp)
7482 or else not Is_Primitive (Subp)
7483 or else not Is_Dispatching_Operation (Subp)
7484 or else not Present (Find_Dispatching_Type (Subp))
7485 or else not Is_Interface (Find_Dispatching_Type (Subp))
7489 -- Entries and procedures can override abstract or null
7490 -- interface procedures
7492 elsif (Ekind (Def_Id) = E_Procedure
7493 or else Ekind (Def_Id) = E_Entry)
7494 and then Ekind (Subp) = E_Procedure
7495 and then Matches_Prefixed_View_Profile
7496 (Parameter_Specifications (Parent (Def_Id)),
7497 Parameter_Specifications (Parent (Subp)))
7501 -- For an overridden subprogram Subp, check whether the mode
7502 -- of its first parameter is correct depending on the kind
7503 -- of synchronized type.
7506 Formal : constant Node_Id := First_Formal (Candidate);
7509 -- In order for an entry or a protected procedure to
7510 -- override, the first parameter of the overridden
7511 -- routine must be of mode "out", "in out" or
7512 -- access-to-variable.
7514 if (Ekind (Candidate) = E_Entry
7515 or else Ekind (Candidate) = E_Procedure)
7516 and then Is_Protected_Type (Typ)
7517 and then Ekind (Formal) /= E_In_Out_Parameter
7518 and then Ekind (Formal) /= E_Out_Parameter
7519 and then Nkind (Parameter_Type (Parent (Formal)))
7520 /= N_Access_Definition
7524 -- All other cases are OK since a task entry or routine
7525 -- does not have a restriction on the mode of the first
7526 -- parameter of the overridden interface routine.
7529 Overridden_Subp := Candidate;
7534 -- Functions can override abstract interface functions
7536 elsif Ekind (Def_Id) = E_Function
7537 and then Ekind (Subp) = E_Function
7538 and then Matches_Prefixed_View_Profile
7539 (Parameter_Specifications (Parent (Def_Id)),
7540 Parameter_Specifications (Parent (Subp)))
7541 and then Etype (Result_Definition (Parent (Def_Id))) =
7542 Etype (Result_Definition (Parent (Subp)))
7544 Overridden_Subp := Subp;
7548 Hom := Homonym (Hom);
7551 -- After examining all candidates for overriding, we are
7552 -- left with the best match which is a mode incompatible
7553 -- interface routine. Do not emit an error if the Expander
7554 -- is active since this error will be detected later on
7555 -- after all concurrent types are expanded and all wrappers
7556 -- are built. This check is meant for spec-only
7559 if Present (Candidate)
7560 and then not Expander_Active
7563 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7565 -- Def_Id is primitive of a protected type, declared
7566 -- inside the type, and the candidate is primitive of a
7567 -- limited or synchronized interface.
7570 and then Is_Protected_Type (Typ)
7572 (Is_Limited_Interface (Iface_Typ)
7573 or else Is_Protected_Interface (Iface_Typ)
7574 or else Is_Synchronized_Interface (Iface_Typ)
7575 or else Is_Task_Interface (Iface_Typ))
7577 -- Must reword this message, comma before to in -gnatj
7581 ("first formal of & must be of mode `OUT`, `IN OUT`"
7582 & " or access-to-variable", Typ, Candidate);
7584 ("\to be overridden by protected procedure or entry "
7585 & "(RM 9.4(11.9/2))", Typ);
7589 Overridden_Subp := Candidate;
7592 end Check_Synchronized_Overriding;
7594 ----------------------------
7595 -- Is_Private_Declaration --
7596 ----------------------------
7598 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7599 Priv_Decls : List_Id;
7600 Decl : constant Node_Id := Unit_Declaration_Node (E);
7603 if Is_Package_Or_Generic_Package (Current_Scope)
7604 and then In_Private_Part (Current_Scope)
7607 Private_Declarations (
7608 Specification (Unit_Declaration_Node (Current_Scope)));
7610 return In_Package_Body (Current_Scope)
7612 (Is_List_Member (Decl)
7613 and then List_Containing (Decl) = Priv_Decls)
7614 or else (Nkind (Parent (Decl)) = N_Package_Specification
7617 (Defining_Entity (Parent (Decl)))
7618 and then List_Containing (Parent (Parent (Decl)))
7623 end Is_Private_Declaration;
7625 --------------------------
7626 -- Is_Overriding_Alias --
7627 --------------------------
7629 function Is_Overriding_Alias
7631 New_E : Entity_Id) return Boolean
7633 AO : constant Entity_Id := Alias (Old_E);
7634 AN : constant Entity_Id := Alias (New_E);
7637 return Scope (AO) /= Scope (AN)
7638 or else No (DTC_Entity (AO))
7639 or else No (DTC_Entity (AN))
7640 or else DT_Position (AO) = DT_Position (AN);
7641 end Is_Overriding_Alias;
7643 -- Start of processing for New_Overloaded_Entity
7646 -- We need to look for an entity that S may override. This must be a
7647 -- homonym in the current scope, so we look for the first homonym of
7648 -- S in the current scope as the starting point for the search.
7650 E := Current_Entity_In_Scope (S);
7652 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
7653 -- They are directly added to the list of primitive operations of
7654 -- Derived_Type, unless this is a rederivation in the private part
7655 -- of an operation that was already derived in the visible part of
7656 -- the current package.
7658 if Ada_Version >= Ada_2005
7659 and then Present (Derived_Type)
7660 and then Present (Alias (S))
7661 and then Is_Dispatching_Operation (Alias (S))
7662 and then Present (Find_Dispatching_Type (Alias (S)))
7663 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7665 -- For private types, when the full-view is processed we propagate to
7666 -- the full view the non-overridden entities whose attribute "alias"
7667 -- references an interface primitive. These entities were added by
7668 -- Derive_Subprograms to ensure that interface primitives are
7671 -- Inside_Freeze_Actions is non zero when S corresponds with an
7672 -- internal entity that links an interface primitive with its
7673 -- covering primitive through attribute Interface_Alias (see
7674 -- Add_Internal_Interface_Entities)
7676 if Inside_Freezing_Actions = 0
7677 and then Is_Package_Or_Generic_Package (Current_Scope)
7678 and then In_Private_Part (Current_Scope)
7679 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
7680 and then Nkind (Parent (S)) = N_Full_Type_Declaration
7681 and then Full_View (Defining_Identifier (Parent (E)))
7682 = Defining_Identifier (Parent (S))
7683 and then Alias (E) = Alias (S)
7685 Check_Operation_From_Private_View (S, E);
7686 Set_Is_Dispatching_Operation (S);
7691 Enter_Overloaded_Entity (S);
7692 Check_Dispatching_Operation (S, Empty);
7693 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7699 -- If there is no homonym then this is definitely not overriding
7702 Enter_Overloaded_Entity (S);
7703 Check_Dispatching_Operation (S, Empty);
7704 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7706 -- If subprogram has an explicit declaration, check whether it
7707 -- has an overriding indicator.
7709 if Comes_From_Source (S) then
7710 Check_Synchronized_Overriding (S, Overridden_Subp);
7711 Check_Overriding_Indicator
7712 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7715 -- If there is a homonym that is not overloadable, then we have an
7716 -- error, except for the special cases checked explicitly below.
7718 elsif not Is_Overloadable (E) then
7720 -- Check for spurious conflict produced by a subprogram that has the
7721 -- same name as that of the enclosing generic package. The conflict
7722 -- occurs within an instance, between the subprogram and the renaming
7723 -- declaration for the package. After the subprogram, the package
7724 -- renaming declaration becomes hidden.
7726 if Ekind (E) = E_Package
7727 and then Present (Renamed_Object (E))
7728 and then Renamed_Object (E) = Current_Scope
7729 and then Nkind (Parent (Renamed_Object (E))) =
7730 N_Package_Specification
7731 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7734 Set_Is_Immediately_Visible (E, False);
7735 Enter_Overloaded_Entity (S);
7736 Set_Homonym (S, Homonym (E));
7737 Check_Dispatching_Operation (S, Empty);
7738 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7740 -- If the subprogram is implicit it is hidden by the previous
7741 -- declaration. However if it is dispatching, it must appear in the
7742 -- dispatch table anyway, because it can be dispatched to even if it
7743 -- cannot be called directly.
7745 elsif Present (Alias (S))
7746 and then not Comes_From_Source (S)
7748 Set_Scope (S, Current_Scope);
7750 if Is_Dispatching_Operation (Alias (S)) then
7751 Check_Dispatching_Operation (S, Empty);
7757 Error_Msg_Sloc := Sloc (E);
7759 -- Generate message, with useful additional warning if in generic
7761 if Is_Generic_Unit (E) then
7762 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7763 Error_Msg_N ("\& conflicts with declaration#", S);
7765 Error_Msg_N ("& conflicts with declaration#", S);
7771 -- E exists and is overloadable
7774 Check_Synchronized_Overriding (S, Overridden_Subp);
7776 -- Loop through E and its homonyms to determine if any of them is
7777 -- the candidate for overriding by S.
7779 while Present (E) loop
7781 -- Definitely not interesting if not in the current scope
7783 if Scope (E) /= Current_Scope then
7786 -- Check if we have type conformance
7788 elsif Type_Conformant (E, S) then
7790 -- If the old and new entities have the same profile and one
7791 -- is not the body of the other, then this is an error, unless
7792 -- one of them is implicitly declared.
7794 -- There are some cases when both can be implicit, for example
7795 -- when both a literal and a function that overrides it are
7796 -- inherited in a derivation, or when an inherited operation
7797 -- of a tagged full type overrides the inherited operation of
7798 -- a private extension. Ada 83 had a special rule for the
7799 -- literal case. In Ada95, the later implicit operation hides
7800 -- the former, and the literal is always the former. In the
7801 -- odd case where both are derived operations declared at the
7802 -- same point, both operations should be declared, and in that
7803 -- case we bypass the following test and proceed to the next
7804 -- part. This can only occur for certain obscure cases in
7805 -- instances, when an operation on a type derived from a formal
7806 -- private type does not override a homograph inherited from
7807 -- the actual. In subsequent derivations of such a type, the
7808 -- DT positions of these operations remain distinct, if they
7811 if Present (Alias (S))
7812 and then (No (Alias (E))
7813 or else Comes_From_Source (E)
7814 or else Is_Abstract_Subprogram (S)
7816 (Is_Dispatching_Operation (E)
7817 and then Is_Overriding_Alias (E, S)))
7818 and then Ekind (E) /= E_Enumeration_Literal
7820 -- When an derived operation is overloaded it may be due to
7821 -- the fact that the full view of a private extension
7822 -- re-inherits. It has to be dealt with.
7824 if Is_Package_Or_Generic_Package (Current_Scope)
7825 and then In_Private_Part (Current_Scope)
7827 Check_Operation_From_Private_View (S, E);
7830 -- In any case the implicit operation remains hidden by
7831 -- the existing declaration, which is overriding.
7833 Set_Is_Overriding_Operation (E);
7835 if Comes_From_Source (E) then
7836 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7838 -- Indicate that E overrides the operation from which
7841 if Present (Alias (S)) then
7842 Set_Overridden_Operation (E, Alias (S));
7844 Set_Overridden_Operation (E, S);
7850 -- Within an instance, the renaming declarations for actual
7851 -- subprograms may become ambiguous, but they do not hide each
7854 elsif Ekind (E) /= E_Entry
7855 and then not Comes_From_Source (E)
7856 and then not Is_Generic_Instance (E)
7857 and then (Present (Alias (E))
7858 or else Is_Intrinsic_Subprogram (E))
7859 and then (not In_Instance
7860 or else No (Parent (E))
7861 or else Nkind (Unit_Declaration_Node (E)) /=
7862 N_Subprogram_Renaming_Declaration)
7864 -- A subprogram child unit is not allowed to override an
7865 -- inherited subprogram (10.1.1(20)).
7867 if Is_Child_Unit (S) then
7869 ("child unit overrides inherited subprogram in parent",
7874 if Is_Non_Overriding_Operation (E, S) then
7875 Enter_Overloaded_Entity (S);
7877 if No (Derived_Type)
7878 or else Is_Tagged_Type (Derived_Type)
7880 Check_Dispatching_Operation (S, Empty);
7886 -- E is a derived operation or an internal operator which
7887 -- is being overridden. Remove E from further visibility.
7888 -- Furthermore, if E is a dispatching operation, it must be
7889 -- replaced in the list of primitive operations of its type
7890 -- (see Override_Dispatching_Operation).
7892 Overridden_Subp := E;
7898 Prev := First_Entity (Current_Scope);
7899 while Present (Prev)
7900 and then Next_Entity (Prev) /= E
7905 -- It is possible for E to be in the current scope and
7906 -- yet not in the entity chain. This can only occur in a
7907 -- generic context where E is an implicit concatenation
7908 -- in the formal part, because in a generic body the
7909 -- entity chain starts with the formals.
7912 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7914 -- E must be removed both from the entity_list of the
7915 -- current scope, and from the visibility chain
7917 if Debug_Flag_E then
7918 Write_Str ("Override implicit operation ");
7919 Write_Int (Int (E));
7923 -- If E is a predefined concatenation, it stands for four
7924 -- different operations. As a result, a single explicit
7925 -- declaration does not hide it. In a possible ambiguous
7926 -- situation, Disambiguate chooses the user-defined op,
7927 -- so it is correct to retain the previous internal one.
7929 if Chars (E) /= Name_Op_Concat
7930 or else Ekind (E) /= E_Operator
7932 -- For nondispatching derived operations that are
7933 -- overridden by a subprogram declared in the private
7934 -- part of a package, we retain the derived subprogram
7935 -- but mark it as not immediately visible. If the
7936 -- derived operation was declared in the visible part
7937 -- then this ensures that it will still be visible
7938 -- outside the package with the proper signature
7939 -- (calls from outside must also be directed to this
7940 -- version rather than the overriding one, unlike the
7941 -- dispatching case). Calls from inside the package
7942 -- will still resolve to the overriding subprogram
7943 -- since the derived one is marked as not visible
7944 -- within the package.
7946 -- If the private operation is dispatching, we achieve
7947 -- the overriding by keeping the implicit operation
7948 -- but setting its alias to be the overriding one. In
7949 -- this fashion the proper body is executed in all
7950 -- cases, but the original signature is used outside
7953 -- If the overriding is not in the private part, we
7954 -- remove the implicit operation altogether.
7956 if Is_Private_Declaration (S) then
7957 if not Is_Dispatching_Operation (E) then
7958 Set_Is_Immediately_Visible (E, False);
7960 -- Work done in Override_Dispatching_Operation,
7961 -- so nothing else need to be done here.
7967 -- Find predecessor of E in Homonym chain
7969 if E = Current_Entity (E) then
7972 Prev_Vis := Current_Entity (E);
7973 while Homonym (Prev_Vis) /= E loop
7974 Prev_Vis := Homonym (Prev_Vis);
7978 if Prev_Vis /= Empty then
7980 -- Skip E in the visibility chain
7982 Set_Homonym (Prev_Vis, Homonym (E));
7985 Set_Name_Entity_Id (Chars (E), Homonym (E));
7988 Set_Next_Entity (Prev, Next_Entity (E));
7990 if No (Next_Entity (Prev)) then
7991 Set_Last_Entity (Current_Scope, Prev);
7997 Enter_Overloaded_Entity (S);
7998 Set_Is_Overriding_Operation (S);
7999 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8001 -- If S is a user-defined subprogram or a null procedure
8002 -- expanded to override an inherited null procedure, then
8003 -- indicate that E overrides the operation from which S
8004 -- is inherited. It seems odd that Overridden_Operation
8005 -- isn't set in all cases where Is_Overriding_Operation
8006 -- is true, but doing so causes infinite loops in the
8007 -- compiler for implicit overriding subprograms. ???
8009 if Comes_From_Source (S)
8011 (Present (Parent (S))
8013 Nkind (Parent (S)) = N_Procedure_Specification
8015 Null_Present (Parent (S)))
8017 if Present (Alias (E)) then
8018 Set_Overridden_Operation (S, Alias (E));
8020 Set_Overridden_Operation (S, E);
8024 if Is_Dispatching_Operation (E) then
8026 -- An overriding dispatching subprogram inherits the
8027 -- convention of the overridden subprogram (by
8030 Set_Convention (S, Convention (E));
8031 Check_Dispatching_Operation (S, E);
8034 Check_Dispatching_Operation (S, Empty);
8037 Check_For_Primitive_Subprogram
8038 (Is_Primitive_Subp, Is_Overriding => True);
8039 goto Check_Inequality;
8042 -- Apparent redeclarations in instances can occur when two
8043 -- formal types get the same actual type. The subprograms in
8044 -- in the instance are legal, even if not callable from the
8045 -- outside. Calls from within are disambiguated elsewhere.
8046 -- For dispatching operations in the visible part, the usual
8047 -- rules apply, and operations with the same profile are not
8050 elsif (In_Instance_Visible_Part
8051 and then not Is_Dispatching_Operation (E))
8052 or else In_Instance_Not_Visible
8056 -- Here we have a real error (identical profile)
8059 Error_Msg_Sloc := Sloc (E);
8061 -- Avoid cascaded errors if the entity appears in
8062 -- subsequent calls.
8064 Set_Scope (S, Current_Scope);
8066 -- Generate error, with extra useful warning for the case
8067 -- of a generic instance with no completion.
8069 if Is_Generic_Instance (S)
8070 and then not Has_Completion (E)
8073 ("instantiation cannot provide body for&", S);
8074 Error_Msg_N ("\& conflicts with declaration#", S);
8076 Error_Msg_N ("& conflicts with declaration#", S);
8083 -- If one subprogram has an access parameter and the other
8084 -- a parameter of an access type, calls to either might be
8085 -- ambiguous. Verify that parameters match except for the
8086 -- access parameter.
8088 if May_Hide_Profile then
8094 F1 := First_Formal (S);
8095 F2 := First_Formal (E);
8096 while Present (F1) and then Present (F2) loop
8097 if Is_Access_Type (Etype (F1)) then
8098 if not Is_Access_Type (Etype (F2))
8099 or else not Conforming_Types
8100 (Designated_Type (Etype (F1)),
8101 Designated_Type (Etype (F2)),
8104 May_Hide_Profile := False;
8108 not Conforming_Types
8109 (Etype (F1), Etype (F2), Type_Conformant)
8111 May_Hide_Profile := False;
8122 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8131 -- On exit, we know that S is a new entity
8133 Enter_Overloaded_Entity (S);
8134 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8135 Check_Overriding_Indicator
8136 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8138 -- If S is a derived operation for an untagged type then by
8139 -- definition it's not a dispatching operation (even if the parent
8140 -- operation was dispatching), so we don't call
8141 -- Check_Dispatching_Operation in that case.
8143 if No (Derived_Type)
8144 or else Is_Tagged_Type (Derived_Type)
8146 Check_Dispatching_Operation (S, Empty);
8150 -- If this is a user-defined equality operator that is not a derived
8151 -- subprogram, create the corresponding inequality. If the operation is
8152 -- dispatching, the expansion is done elsewhere, and we do not create
8153 -- an explicit inequality operation.
8155 <<Check_Inequality>>
8156 if Chars (S) = Name_Op_Eq
8157 and then Etype (S) = Standard_Boolean
8158 and then Present (Parent (S))
8159 and then not Is_Dispatching_Operation (S)
8161 Make_Inequality_Operator (S);
8163 if Ada_Version >= Ada_2012 then
8164 Check_Untagged_Equality (S);
8167 end New_Overloaded_Entity;
8169 ---------------------
8170 -- Process_Formals --
8171 ---------------------
8173 procedure Process_Formals
8175 Related_Nod : Node_Id)
8177 Param_Spec : Node_Id;
8179 Formal_Type : Entity_Id;
8183 Num_Out_Params : Nat := 0;
8184 First_Out_Param : Entity_Id := Empty;
8185 -- Used for setting Is_Only_Out_Parameter
8187 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8188 -- Determine whether an access type designates a type coming from a
8191 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8192 -- Check whether the default has a class-wide type. After analysis the
8193 -- default has the type of the formal, so we must also check explicitly
8194 -- for an access attribute.
8196 -------------------------------
8197 -- Designates_From_With_Type --
8198 -------------------------------
8200 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8201 Desig : Entity_Id := Typ;
8204 if Is_Access_Type (Desig) then
8205 Desig := Directly_Designated_Type (Desig);
8208 if Is_Class_Wide_Type (Desig) then
8209 Desig := Root_Type (Desig);
8213 Ekind (Desig) = E_Incomplete_Type
8214 and then From_With_Type (Desig);
8215 end Designates_From_With_Type;
8217 ---------------------------
8218 -- Is_Class_Wide_Default --
8219 ---------------------------
8221 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8223 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8224 or else (Nkind (D) = N_Attribute_Reference
8225 and then Attribute_Name (D) = Name_Access
8226 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8227 end Is_Class_Wide_Default;
8229 -- Start of processing for Process_Formals
8232 -- In order to prevent premature use of the formals in the same formal
8233 -- part, the Ekind is left undefined until all default expressions are
8234 -- analyzed. The Ekind is established in a separate loop at the end.
8236 Param_Spec := First (T);
8237 while Present (Param_Spec) loop
8238 Formal := Defining_Identifier (Param_Spec);
8239 Set_Never_Set_In_Source (Formal, True);
8240 Enter_Name (Formal);
8242 -- Case of ordinary parameters
8244 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8245 Find_Type (Parameter_Type (Param_Spec));
8246 Ptype := Parameter_Type (Param_Spec);
8248 if Ptype = Error then
8252 Formal_Type := Entity (Ptype);
8254 if Is_Incomplete_Type (Formal_Type)
8256 (Is_Class_Wide_Type (Formal_Type)
8257 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8259 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8260 -- primitive operations, as long as their completion is
8261 -- in the same declarative part. If in the private part
8262 -- this means that the type cannot be a Taft-amendment type.
8263 -- Check is done on package exit. For access to subprograms,
8264 -- the use is legal for Taft-amendment types.
8266 if Is_Tagged_Type (Formal_Type) then
8267 if Ekind (Scope (Current_Scope)) = E_Package
8268 and then In_Private_Part (Scope (Current_Scope))
8269 and then not From_With_Type (Formal_Type)
8270 and then not Is_Class_Wide_Type (Formal_Type)
8273 (Parent (T), N_Access_Function_Definition,
8274 N_Access_Procedure_Definition)
8278 Private_Dependents (Base_Type (Formal_Type)));
8282 -- Special handling of Value_Type for CIL case
8284 elsif Is_Value_Type (Formal_Type) then
8287 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8288 N_Access_Procedure_Definition)
8291 ("invalid use of incomplete type&",
8292 Param_Spec, Formal_Type);
8294 -- Further checks on the legality of incomplete types
8295 -- in formal parts must be delayed until the freeze point
8296 -- of the enclosing subprogram or access to subprogram.
8299 elsif Ekind (Formal_Type) = E_Void then
8300 Error_Msg_NE ("premature use of&",
8301 Parameter_Type (Param_Spec), Formal_Type);
8304 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8305 -- declaration corresponding to the null-excluding type of the
8306 -- formal in the enclosing scope. Finally, replace the parameter
8307 -- type of the formal with the internal subtype.
8309 if Ada_Version >= Ada_2005
8310 and then Null_Exclusion_Present (Param_Spec)
8312 if not Is_Access_Type (Formal_Type) then
8314 ("`NOT NULL` allowed only for an access type", Param_Spec);
8317 if Can_Never_Be_Null (Formal_Type)
8318 and then Comes_From_Source (Related_Nod)
8321 ("`NOT NULL` not allowed (& already excludes null)",
8327 Create_Null_Excluding_Itype
8329 Related_Nod => Related_Nod,
8330 Scope_Id => Scope (Current_Scope));
8332 -- If the designated type of the itype is an itype we
8333 -- decorate it with the Has_Delayed_Freeze attribute to
8334 -- avoid problems with the backend.
8337 -- type T is access procedure;
8338 -- procedure Op (O : not null T);
8340 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8341 Set_Has_Delayed_Freeze (Formal_Type);
8346 -- An access formal type
8350 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8352 -- No need to continue if we already notified errors
8354 if not Present (Formal_Type) then
8358 -- Ada 2005 (AI-254)
8361 AD : constant Node_Id :=
8362 Access_To_Subprogram_Definition
8363 (Parameter_Type (Param_Spec));
8365 if Present (AD) and then Protected_Present (AD) then
8367 Replace_Anonymous_Access_To_Protected_Subprogram
8373 Set_Etype (Formal, Formal_Type);
8374 Default := Expression (Param_Spec);
8376 if Present (Default) then
8377 if Out_Present (Param_Spec) then
8379 ("default initialization only allowed for IN parameters",
8383 -- Do the special preanalysis of the expression (see section on
8384 -- "Handling of Default Expressions" in the spec of package Sem).
8386 Preanalyze_Spec_Expression (Default, Formal_Type);
8388 -- An access to constant cannot be the default for
8389 -- an access parameter that is an access to variable.
8391 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8392 and then not Is_Access_Constant (Formal_Type)
8393 and then Is_Access_Type (Etype (Default))
8394 and then Is_Access_Constant (Etype (Default))
8397 ("formal that is access to variable cannot be initialized " &
8398 "with an access-to-constant expression", Default);
8401 -- Check that the designated type of an access parameter's default
8402 -- is not a class-wide type unless the parameter's designated type
8403 -- is also class-wide.
8405 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8406 and then not Designates_From_With_Type (Formal_Type)
8407 and then Is_Class_Wide_Default (Default)
8408 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8411 ("access to class-wide expression not allowed here", Default);
8414 -- Check incorrect use of dynamically tagged expressions
8416 if Is_Tagged_Type (Formal_Type) then
8417 Check_Dynamically_Tagged_Expression
8420 Related_Nod => Default);
8424 -- Ada 2005 (AI-231): Static checks
8426 if Ada_Version >= Ada_2005
8427 and then Is_Access_Type (Etype (Formal))
8428 and then Can_Never_Be_Null (Etype (Formal))
8430 Null_Exclusion_Static_Checks (Param_Spec);
8437 -- If this is the formal part of a function specification, analyze the
8438 -- subtype mark in the context where the formals are visible but not
8439 -- yet usable, and may hide outer homographs.
8441 if Nkind (Related_Nod) = N_Function_Specification then
8442 Analyze_Return_Type (Related_Nod);
8445 -- Now set the kind (mode) of each formal
8447 Param_Spec := First (T);
8449 while Present (Param_Spec) loop
8450 Formal := Defining_Identifier (Param_Spec);
8451 Set_Formal_Mode (Formal);
8453 if Ekind (Formal) = E_In_Parameter then
8454 Set_Default_Value (Formal, Expression (Param_Spec));
8456 if Present (Expression (Param_Spec)) then
8457 Default := Expression (Param_Spec);
8459 if Is_Scalar_Type (Etype (Default)) then
8461 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8463 Formal_Type := Entity (Parameter_Type (Param_Spec));
8466 Formal_Type := Access_Definition
8467 (Related_Nod, Parameter_Type (Param_Spec));
8470 Apply_Scalar_Range_Check (Default, Formal_Type);
8474 elsif Ekind (Formal) = E_Out_Parameter then
8475 Num_Out_Params := Num_Out_Params + 1;
8477 if Num_Out_Params = 1 then
8478 First_Out_Param := Formal;
8481 elsif Ekind (Formal) = E_In_Out_Parameter then
8482 Num_Out_Params := Num_Out_Params + 1;
8488 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8489 Set_Is_Only_Out_Parameter (First_Out_Param);
8491 end Process_Formals;
8497 procedure Process_PPCs
8499 Spec_Id : Entity_Id;
8500 Body_Id : Entity_Id)
8502 Loc : constant Source_Ptr := Sloc (N);
8504 Plist : List_Id := No_List;
8508 function Grab_PPC (Nam : Name_Id) return Node_Id;
8509 -- Prag contains an analyzed precondition or postcondition pragma.
8510 -- This function copies the pragma, changes it to the corresponding
8511 -- Check pragma and returns the Check pragma as the result. The
8512 -- argument Nam is either Name_Precondition or Name_Postcondition.
8518 function Grab_PPC (Nam : Name_Id) return Node_Id is
8519 CP : constant Node_Id := New_Copy_Tree (Prag);
8522 -- Set Analyzed to false, since we want to reanalyze the check
8523 -- procedure. Note that it is only at the outer level that we
8524 -- do this fiddling, for the spec cases, the already preanalyzed
8525 -- parameters are not affected.
8527 -- For a postcondition pragma within a generic, preserve the pragma
8528 -- for later expansion.
8530 Set_Analyzed (CP, False);
8532 if Nam = Name_Postcondition
8533 and then not Expander_Active
8538 -- Change pragma into corresponding pragma Check
8540 Prepend_To (Pragma_Argument_Associations (CP),
8541 Make_Pragma_Argument_Association (Sloc (Prag),
8543 Make_Identifier (Loc,
8545 Set_Pragma_Identifier (CP,
8546 Make_Identifier (Sloc (Prag),
8547 Chars => Name_Check));
8552 -- Start of processing for Process_PPCs
8555 -- Nothing to do if we are not generating code
8557 if Operating_Mode /= Generate_Code then
8561 -- Grab preconditions from spec
8563 if Present (Spec_Id) then
8565 -- Loop through PPC pragmas from spec. Note that preconditions from
8566 -- the body will be analyzed and converted when we scan the body
8567 -- declarations below.
8569 Prag := Spec_PPC_List (Spec_Id);
8570 while Present (Prag) loop
8571 if Pragma_Name (Prag) = Name_Precondition
8572 and then Pragma_Enabled (Prag)
8574 -- Add pragma Check at the start of the declarations of N.
8575 -- Note that this processing reverses the order of the list,
8576 -- which is what we want since new entries were chained to
8577 -- the head of the list.
8579 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
8582 Prag := Next_Pragma (Prag);
8586 -- Build postconditions procedure if needed and prepend the following
8587 -- declaration to the start of the declarations for the subprogram.
8589 -- procedure _postconditions [(_Result : resulttype)] is
8591 -- pragma Check (Postcondition, condition [,message]);
8592 -- pragma Check (Postcondition, condition [,message]);
8596 -- First we deal with the postconditions in the body
8598 if Is_Non_Empty_List (Declarations (N)) then
8600 -- Loop through declarations
8602 Prag := First (Declarations (N));
8603 while Present (Prag) loop
8604 if Nkind (Prag) = N_Pragma then
8606 -- If pragma, capture if enabled postcondition, else ignore
8608 if Pragma_Name (Prag) = Name_Postcondition
8609 and then Check_Enabled (Name_Postcondition)
8611 if Plist = No_List then
8612 Plist := Empty_List;
8617 -- If expansion is disabled, as in a generic unit,
8618 -- save pragma for later expansion.
8620 if not Expander_Active then
8621 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8623 Append (Grab_PPC (Name_Postcondition), Plist);
8629 -- Not a pragma, if comes from source, then end scan
8631 elsif Comes_From_Source (Prag) then
8634 -- Skip stuff not coming from source
8642 -- Now deal with any postconditions from the spec
8644 if Present (Spec_Id) then
8646 -- Loop through PPC pragmas from spec
8648 Prag := Spec_PPC_List (Spec_Id);
8649 while Present (Prag) loop
8650 if Pragma_Name (Prag) = Name_Postcondition
8651 and then Pragma_Enabled (Prag)
8653 if Plist = No_List then
8654 Plist := Empty_List;
8657 if not Expander_Active then
8658 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8660 Append (Grab_PPC (Name_Postcondition), Plist);
8664 Prag := Next_Pragma (Prag);
8668 -- If we had any postconditions and expansion is enabled, build
8669 -- the _Postconditions procedure.
8672 and then Expander_Active
8674 Subp := Defining_Entity (N);
8676 if Etype (Subp) /= Standard_Void_Type then
8678 Make_Parameter_Specification (Loc,
8679 Defining_Identifier =>
8680 Make_Defining_Identifier (Loc,
8681 Chars => Name_uResult),
8682 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8688 Post_Proc : constant Entity_Id :=
8689 Make_Defining_Identifier (Loc,
8690 Chars => Name_uPostconditions);
8691 -- The entity for the _Postconditions procedure
8693 Prepend_To (Declarations (N),
8694 Make_Subprogram_Body (Loc,
8696 Make_Procedure_Specification (Loc,
8697 Defining_Unit_Name => Post_Proc,
8698 Parameter_Specifications => Parms),
8700 Declarations => Empty_List,
8702 Handled_Statement_Sequence =>
8703 Make_Handled_Sequence_Of_Statements (Loc,
8704 Statements => Plist)));
8706 -- If this is a procedure, set the Postcondition_Proc attribute on
8707 -- the proper defining entity for the subprogram.
8709 if Etype (Subp) = Standard_Void_Type then
8710 if Present (Spec_Id) then
8711 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8713 Set_Postcondition_Proc (Body_Id, Post_Proc);
8718 if Present (Spec_Id) then
8719 Set_Has_Postconditions (Spec_Id);
8721 Set_Has_Postconditions (Body_Id);
8726 ----------------------------
8727 -- Reference_Body_Formals --
8728 ----------------------------
8730 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8735 if Error_Posted (Spec) then
8739 -- Iterate over both lists. They may be of different lengths if the two
8740 -- specs are not conformant.
8742 Fs := First_Formal (Spec);
8743 Fb := First_Formal (Bod);
8744 while Present (Fs) and then Present (Fb) loop
8745 Generate_Reference (Fs, Fb, 'b');
8748 Style.Check_Identifier (Fb, Fs);
8751 Set_Spec_Entity (Fb, Fs);
8752 Set_Referenced (Fs, False);
8756 end Reference_Body_Formals;
8758 -------------------------
8759 -- Set_Actual_Subtypes --
8760 -------------------------
8762 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8763 Loc : constant Source_Ptr := Sloc (N);
8767 First_Stmt : Node_Id := Empty;
8768 AS_Needed : Boolean;
8771 -- If this is an empty initialization procedure, no need to create
8772 -- actual subtypes (small optimization).
8774 if Ekind (Subp) = E_Procedure
8775 and then Is_Null_Init_Proc (Subp)
8780 Formal := First_Formal (Subp);
8781 while Present (Formal) loop
8782 T := Etype (Formal);
8784 -- We never need an actual subtype for a constrained formal
8786 if Is_Constrained (T) then
8789 -- If we have unknown discriminants, then we do not need an actual
8790 -- subtype, or more accurately we cannot figure it out! Note that
8791 -- all class-wide types have unknown discriminants.
8793 elsif Has_Unknown_Discriminants (T) then
8796 -- At this stage we have an unconstrained type that may need an
8797 -- actual subtype. For sure the actual subtype is needed if we have
8798 -- an unconstrained array type.
8800 elsif Is_Array_Type (T) then
8803 -- The only other case needing an actual subtype is an unconstrained
8804 -- record type which is an IN parameter (we cannot generate actual
8805 -- subtypes for the OUT or IN OUT case, since an assignment can
8806 -- change the discriminant values. However we exclude the case of
8807 -- initialization procedures, since discriminants are handled very
8808 -- specially in this context, see the section entitled "Handling of
8809 -- Discriminants" in Einfo.
8811 -- We also exclude the case of Discrim_SO_Functions (functions used
8812 -- in front end layout mode for size/offset values), since in such
8813 -- functions only discriminants are referenced, and not only are such
8814 -- subtypes not needed, but they cannot always be generated, because
8815 -- of order of elaboration issues.
8817 elsif Is_Record_Type (T)
8818 and then Ekind (Formal) = E_In_Parameter
8819 and then Chars (Formal) /= Name_uInit
8820 and then not Is_Unchecked_Union (T)
8821 and then not Is_Discrim_SO_Function (Subp)
8825 -- All other cases do not need an actual subtype
8831 -- Generate actual subtypes for unconstrained arrays and
8832 -- unconstrained discriminated records.
8835 if Nkind (N) = N_Accept_Statement then
8837 -- If expansion is active, The formal is replaced by a local
8838 -- variable that renames the corresponding entry of the
8839 -- parameter block, and it is this local variable that may
8840 -- require an actual subtype.
8842 if Expander_Active then
8843 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8845 Decl := Build_Actual_Subtype (T, Formal);
8848 if Present (Handled_Statement_Sequence (N)) then
8850 First (Statements (Handled_Statement_Sequence (N)));
8851 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8852 Mark_Rewrite_Insertion (Decl);
8854 -- If the accept statement has no body, there will be no
8855 -- reference to the actuals, so no need to compute actual
8862 Decl := Build_Actual_Subtype (T, Formal);
8863 Prepend (Decl, Declarations (N));
8864 Mark_Rewrite_Insertion (Decl);
8867 -- The declaration uses the bounds of an existing object, and
8868 -- therefore needs no constraint checks.
8870 Analyze (Decl, Suppress => All_Checks);
8872 -- We need to freeze manually the generated type when it is
8873 -- inserted anywhere else than in a declarative part.
8875 if Present (First_Stmt) then
8876 Insert_List_Before_And_Analyze (First_Stmt,
8877 Freeze_Entity (Defining_Identifier (Decl), Loc));
8880 if Nkind (N) = N_Accept_Statement
8881 and then Expander_Active
8883 Set_Actual_Subtype (Renamed_Object (Formal),
8884 Defining_Identifier (Decl));
8886 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8890 Next_Formal (Formal);
8892 end Set_Actual_Subtypes;
8894 ---------------------
8895 -- Set_Formal_Mode --
8896 ---------------------
8898 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8899 Spec : constant Node_Id := Parent (Formal_Id);
8902 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8903 -- since we ensure that corresponding actuals are always valid at the
8904 -- point of the call.
8906 if Out_Present (Spec) then
8907 if Ekind (Scope (Formal_Id)) = E_Function
8908 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8910 Error_Msg_N ("functions can only have IN parameters", Spec);
8911 Set_Ekind (Formal_Id, E_In_Parameter);
8913 elsif In_Present (Spec) then
8914 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8917 Set_Ekind (Formal_Id, E_Out_Parameter);
8918 Set_Never_Set_In_Source (Formal_Id, True);
8919 Set_Is_True_Constant (Formal_Id, False);
8920 Set_Current_Value (Formal_Id, Empty);
8924 Set_Ekind (Formal_Id, E_In_Parameter);
8927 -- Set Is_Known_Non_Null for access parameters since the language
8928 -- guarantees that access parameters are always non-null. We also set
8929 -- Can_Never_Be_Null, since there is no way to change the value.
8931 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8933 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8934 -- null; In Ada 2005, only if then null_exclusion is explicit.
8936 if Ada_Version < Ada_2005
8937 or else Can_Never_Be_Null (Etype (Formal_Id))
8939 Set_Is_Known_Non_Null (Formal_Id);
8940 Set_Can_Never_Be_Null (Formal_Id);
8943 -- Ada 2005 (AI-231): Null-exclusion access subtype
8945 elsif Is_Access_Type (Etype (Formal_Id))
8946 and then Can_Never_Be_Null (Etype (Formal_Id))
8948 Set_Is_Known_Non_Null (Formal_Id);
8951 Set_Mechanism (Formal_Id, Default_Mechanism);
8952 Set_Formal_Validity (Formal_Id);
8953 end Set_Formal_Mode;
8955 -------------------------
8956 -- Set_Formal_Validity --
8957 -------------------------
8959 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8961 -- If no validity checking, then we cannot assume anything about the
8962 -- validity of parameters, since we do not know there is any checking
8963 -- of the validity on the call side.
8965 if not Validity_Checks_On then
8968 -- If validity checking for parameters is enabled, this means we are
8969 -- not supposed to make any assumptions about argument values.
8971 elsif Validity_Check_Parameters then
8974 -- If we are checking in parameters, we will assume that the caller is
8975 -- also checking parameters, so we can assume the parameter is valid.
8977 elsif Ekind (Formal_Id) = E_In_Parameter
8978 and then Validity_Check_In_Params
8980 Set_Is_Known_Valid (Formal_Id, True);
8982 -- Similar treatment for IN OUT parameters
8984 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8985 and then Validity_Check_In_Out_Params
8987 Set_Is_Known_Valid (Formal_Id, True);
8989 end Set_Formal_Validity;
8991 ------------------------
8992 -- Subtype_Conformant --
8993 ------------------------
8995 function Subtype_Conformant
8996 (New_Id : Entity_Id;
8998 Skip_Controlling_Formals : Boolean := False) return Boolean
9002 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9003 Skip_Controlling_Formals => Skip_Controlling_Formals);
9005 end Subtype_Conformant;
9007 ---------------------
9008 -- Type_Conformant --
9009 ---------------------
9011 function Type_Conformant
9012 (New_Id : Entity_Id;
9014 Skip_Controlling_Formals : Boolean := False) return Boolean
9018 May_Hide_Profile := False;
9021 (New_Id, Old_Id, Type_Conformant, False, Result,
9022 Skip_Controlling_Formals => Skip_Controlling_Formals);
9024 end Type_Conformant;
9026 -------------------------------
9027 -- Valid_Operator_Definition --
9028 -------------------------------
9030 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9033 Id : constant Name_Id := Chars (Designator);
9037 F := First_Formal (Designator);
9038 while Present (F) loop
9041 if Present (Default_Value (F)) then
9043 ("default values not allowed for operator parameters",
9050 -- Verify that user-defined operators have proper number of arguments
9051 -- First case of operators which can only be unary
9054 or else Id = Name_Op_Abs
9058 -- Case of operators which can be unary or binary
9060 elsif Id = Name_Op_Add
9061 or Id = Name_Op_Subtract
9063 N_OK := (N in 1 .. 2);
9065 -- All other operators can only be binary
9073 ("incorrect number of arguments for operator", Designator);
9077 and then Base_Type (Etype (Designator)) = Standard_Boolean
9078 and then not Is_Intrinsic_Subprogram (Designator)
9081 ("explicit definition of inequality not allowed", Designator);
9083 end Valid_Operator_Definition;