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_05
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);
486 if Is_Inherently_Limited_Type (R_Type) then
488 ("\return by reference not permitted in Ada 2005", Expr);
491 -- Warn in Ada 95 mode, to give folks a heads up about this
494 -- In GNAT mode, this is just a warning, to allow it to be
495 -- evilly turned off. Otherwise it is a real error.
497 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
498 if Is_Inherently_Limited_Type (R_Type) then
500 ("return by reference not permitted in Ada 2005 " &
501 "(RM-2005 6.5(5.5/2))?", Expr);
504 ("cannot copy object of a limited type in Ada 2005 " &
505 "(RM-2005 6.5(5.5/2))?", Expr);
508 -- Ada 95 mode, compatibility warnings disabled
511 return; -- skip continuation messages below
515 ("\consider switching to return of access type", Expr);
516 Explain_Limited_Type (R_Type, Expr);
518 end Check_Limited_Return;
520 -------------------------------------
521 -- Check_Return_Subtype_Indication --
522 -------------------------------------
524 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
525 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
527 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
528 -- Subtype given in the extended return statement (must match R_Type)
530 Subtype_Ind : constant Node_Id :=
531 Object_Definition (Original_Node (Obj_Decl));
533 R_Type_Is_Anon_Access :
535 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
537 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
539 Ekind (R_Type) = E_Anonymous_Access_Type;
540 -- True if return type of the function is an anonymous access type
541 -- Can't we make Is_Anonymous_Access_Type in einfo ???
543 R_Stm_Type_Is_Anon_Access :
545 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
547 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
549 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
550 -- True if type of the return object is an anonymous access type
553 -- First, avoid cascaded errors
555 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
559 -- "return access T" case; check that the return statement also has
560 -- "access T", and that the subtypes statically match:
561 -- if this is an access to subprogram the signatures must match.
563 if R_Type_Is_Anon_Access then
564 if R_Stm_Type_Is_Anon_Access then
566 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
568 if Base_Type (Designated_Type (R_Stm_Type)) /=
569 Base_Type (Designated_Type (R_Type))
570 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
573 ("subtype must statically match function result subtype",
574 Subtype_Mark (Subtype_Ind));
578 -- For two anonymous access to subprogram types, the
579 -- types themselves must be type conformant.
581 if not Conforming_Types
582 (R_Stm_Type, R_Type, Fully_Conformant)
585 ("subtype must statically match function result subtype",
591 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
594 -- Subtype indication case: check that the return object's type is
595 -- covered by the result type, and that the subtypes statically match
596 -- when the result subtype is constrained. Also handle record types
597 -- with unknown discriminants for which we have built the underlying
598 -- record view. Coverage is needed to allow specific-type return
599 -- objects when the result type is class-wide (see AI05-32).
601 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
602 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
606 Underlying_Record_View (Base_Type (R_Stm_Type))))
608 -- A null exclusion may be present on the return type, on the
609 -- function specification, on the object declaration or on the
612 if Is_Access_Type (R_Type)
614 (Can_Never_Be_Null (R_Type)
615 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
616 Can_Never_Be_Null (R_Stm_Type)
619 ("subtype must statically match function result subtype",
623 if Is_Constrained (R_Type) then
624 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
626 ("subtype must statically match function result subtype",
631 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
632 and then Is_Null_Extension (Base_Type (R_Type))
638 ("wrong type for return_subtype_indication", Subtype_Ind);
640 end Check_Return_Subtype_Indication;
642 ---------------------
643 -- Local Variables --
644 ---------------------
648 -- Start of processing for Analyze_Function_Return
651 Set_Return_Present (Scope_Id);
653 if Nkind (N) = N_Simple_Return_Statement then
654 Expr := Expression (N);
655 Analyze_And_Resolve (Expr, R_Type);
656 Check_Limited_Return (Expr);
659 -- Analyze parts specific to extended_return_statement:
662 Obj_Decl : constant Node_Id :=
663 Last (Return_Object_Declarations (N));
665 HSS : constant Node_Id := Handled_Statement_Sequence (N);
668 Expr := Expression (Obj_Decl);
670 -- Note: The check for OK_For_Limited_Init will happen in
671 -- Analyze_Object_Declaration; we treat it as a normal
672 -- object declaration.
674 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
677 Check_Return_Subtype_Indication (Obj_Decl);
679 if Present (HSS) then
682 if Present (Exception_Handlers (HSS)) then
684 -- ???Has_Nested_Block_With_Handler needs to be set.
685 -- Probably by creating an actual N_Block_Statement.
686 -- Probably in Expand.
692 -- Mark the return object as referenced, since the return is an
693 -- implicit reference of the object.
695 Set_Referenced (Defining_Identifier (Obj_Decl));
697 Check_References (Stm_Entity);
701 -- Case of Expr present
705 -- Defend against previous errors
707 and then Nkind (Expr) /= N_Empty
708 and then Present (Etype (Expr))
710 -- Apply constraint check. Note that this is done before the implicit
711 -- conversion of the expression done for anonymous access types to
712 -- ensure correct generation of the null-excluding check associated
713 -- with null-excluding expressions found in return statements.
715 Apply_Constraint_Check (Expr, R_Type);
717 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
718 -- type, apply an implicit conversion of the expression to that type
719 -- to force appropriate static and run-time accessibility checks.
721 if Ada_Version >= Ada_05
722 and then Ekind (R_Type) = E_Anonymous_Access_Type
724 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
725 Analyze_And_Resolve (Expr, R_Type);
728 -- If the result type is class-wide, then check that the return
729 -- expression's type is not declared at a deeper level than the
730 -- function (RM05-6.5(5.6/2)).
732 if Ada_Version >= Ada_05
733 and then Is_Class_Wide_Type (R_Type)
735 if Type_Access_Level (Etype (Expr)) >
736 Subprogram_Access_Level (Scope_Id)
739 ("level of return expression type is deeper than " &
740 "class-wide function!", Expr);
744 -- Check incorrect use of dynamically tagged expression
746 if Is_Tagged_Type (R_Type) then
747 Check_Dynamically_Tagged_Expression
753 -- ??? A real run-time accessibility check is needed in cases
754 -- involving dereferences of access parameters. For now we just
755 -- check the static cases.
757 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
758 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
759 and then Object_Access_Level (Expr) >
760 Subprogram_Access_Level (Scope_Id)
763 Make_Raise_Program_Error (Loc,
764 Reason => PE_Accessibility_Check_Failed));
768 ("cannot return a local value by reference?", N);
770 ("\& will be raised at run time?",
771 N, Standard_Program_Error);
775 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
776 and then Null_Exclusion_Present (Parent (Scope_Id))
778 Apply_Compile_Time_Constraint_Error
780 Msg => "(Ada 2005) null not allowed for "
781 & "null-excluding return?",
782 Reason => CE_Null_Not_Allowed);
785 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
786 -- (Disabled for now)
788 -- Check_Order_Dependence;
790 end Analyze_Function_Return;
792 -------------------------------------
793 -- Analyze_Generic_Subprogram_Body --
794 -------------------------------------
796 procedure Analyze_Generic_Subprogram_Body
800 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
801 Kind : constant Entity_Kind := Ekind (Gen_Id);
807 -- Copy body and disable expansion while analyzing the generic For a
808 -- stub, do not copy the stub (which would load the proper body), this
809 -- will be done when the proper body is analyzed.
811 if Nkind (N) /= N_Subprogram_Body_Stub then
812 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
817 Spec := Specification (N);
819 -- Within the body of the generic, the subprogram is callable, and
820 -- behaves like the corresponding non-generic unit.
822 Body_Id := Defining_Entity (Spec);
824 if Kind = E_Generic_Procedure
825 and then Nkind (Spec) /= N_Procedure_Specification
827 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
830 elsif Kind = E_Generic_Function
831 and then Nkind (Spec) /= N_Function_Specification
833 Error_Msg_N ("invalid body for generic function ", Body_Id);
837 Set_Corresponding_Body (Gen_Decl, Body_Id);
839 if Has_Completion (Gen_Id)
840 and then Nkind (Parent (N)) /= N_Subunit
842 Error_Msg_N ("duplicate generic body", N);
845 Set_Has_Completion (Gen_Id);
848 if Nkind (N) = N_Subprogram_Body_Stub then
849 Set_Ekind (Defining_Entity (Specification (N)), Kind);
851 Set_Corresponding_Spec (N, Gen_Id);
854 if Nkind (Parent (N)) = N_Compilation_Unit then
855 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
858 -- Make generic parameters immediately visible in the body. They are
859 -- needed to process the formals declarations. Then make the formals
860 -- visible in a separate step.
866 First_Ent : Entity_Id;
869 First_Ent := First_Entity (Gen_Id);
872 while Present (E) and then not Is_Formal (E) loop
877 Set_Use (Generic_Formal_Declarations (Gen_Decl));
879 -- Now generic formals are visible, and the specification can be
880 -- analyzed, for subsequent conformance check.
882 Body_Id := Analyze_Subprogram_Specification (Spec);
884 -- Make formal parameters visible
888 -- E is the first formal parameter, we loop through the formals
889 -- installing them so that they will be visible.
891 Set_First_Entity (Gen_Id, E);
892 while Present (E) loop
898 -- Visible generic entity is callable within its own body
900 Set_Ekind (Gen_Id, Ekind (Body_Id));
901 Set_Ekind (Body_Id, E_Subprogram_Body);
902 Set_Convention (Body_Id, Convention (Gen_Id));
903 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
904 Set_Scope (Body_Id, Scope (Gen_Id));
905 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
907 if Nkind (N) = N_Subprogram_Body_Stub then
909 -- No body to analyze, so restore state of generic unit
911 Set_Ekind (Gen_Id, Kind);
912 Set_Ekind (Body_Id, Kind);
914 if Present (First_Ent) then
915 Set_First_Entity (Gen_Id, First_Ent);
922 -- If this is a compilation unit, it must be made visible explicitly,
923 -- because the compilation of the declaration, unlike other library
924 -- unit declarations, does not. If it is not a unit, the following
925 -- is redundant but harmless.
927 Set_Is_Immediately_Visible (Gen_Id);
928 Reference_Body_Formals (Gen_Id, Body_Id);
930 if Is_Child_Unit (Gen_Id) then
931 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
934 Set_Actual_Subtypes (N, Current_Scope);
935 Process_PPCs (N, Gen_Id, Body_Id);
937 -- If the generic unit carries pre- or post-conditions, copy them
938 -- to the original generic tree, so that they are properly added
939 -- to any instantiation.
942 Orig : constant Node_Id := Original_Node (N);
946 Cond := First (Declarations (N));
947 while Present (Cond) loop
948 if Nkind (Cond) = N_Pragma
949 and then Pragma_Name (Cond) = Name_Check
951 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
953 elsif Nkind (Cond) = N_Pragma
954 and then Pragma_Name (Cond) = Name_Postcondition
956 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
957 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
966 Analyze_Declarations (Declarations (N));
968 Analyze (Handled_Statement_Sequence (N));
970 Save_Global_References (Original_Node (N));
972 -- Prior to exiting the scope, include generic formals again (if any
973 -- are present) in the set of local entities.
975 if Present (First_Ent) then
976 Set_First_Entity (Gen_Id, First_Ent);
979 Check_References (Gen_Id);
982 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
984 Check_Subprogram_Order (N);
986 -- Outside of its body, unit is generic again
988 Set_Ekind (Gen_Id, Kind);
989 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
992 Style.Check_Identifier (Body_Id, Gen_Id);
996 end Analyze_Generic_Subprogram_Body;
998 -----------------------------
999 -- Analyze_Operator_Symbol --
1000 -----------------------------
1002 -- An operator symbol such as "+" or "and" may appear in context where the
1003 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1004 -- is just a string, as in (conjunction = "or"). In these cases the parser
1005 -- generates this node, and the semantics does the disambiguation. Other
1006 -- such case are actuals in an instantiation, the generic unit in an
1007 -- instantiation, and pragma arguments.
1009 procedure Analyze_Operator_Symbol (N : Node_Id) is
1010 Par : constant Node_Id := Parent (N);
1013 if (Nkind (Par) = N_Function_Call
1014 and then N = Name (Par))
1015 or else Nkind (Par) = N_Function_Instantiation
1016 or else (Nkind (Par) = N_Indexed_Component
1017 and then N = Prefix (Par))
1018 or else (Nkind (Par) = N_Pragma_Argument_Association
1019 and then not Is_Pragma_String_Literal (Par))
1020 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1021 or else (Nkind (Par) = N_Attribute_Reference
1022 and then Attribute_Name (Par) /= Name_Value)
1024 Find_Direct_Name (N);
1027 Change_Operator_Symbol_To_String_Literal (N);
1030 end Analyze_Operator_Symbol;
1032 -----------------------------------
1033 -- Analyze_Parameter_Association --
1034 -----------------------------------
1036 procedure Analyze_Parameter_Association (N : Node_Id) is
1038 Analyze (Explicit_Actual_Parameter (N));
1039 end Analyze_Parameter_Association;
1041 ----------------------------
1042 -- Analyze_Procedure_Call --
1043 ----------------------------
1045 procedure Analyze_Procedure_Call (N : Node_Id) is
1046 Loc : constant Source_Ptr := Sloc (N);
1047 P : constant Node_Id := Name (N);
1048 Actuals : constant List_Id := Parameter_Associations (N);
1052 procedure Analyze_Call_And_Resolve;
1053 -- Do Analyze and Resolve calls for procedure call
1054 -- At end, check illegal order dependence.
1056 ------------------------------
1057 -- Analyze_Call_And_Resolve --
1058 ------------------------------
1060 procedure Analyze_Call_And_Resolve is
1062 if Nkind (N) = N_Procedure_Call_Statement then
1064 Resolve (N, Standard_Void_Type);
1066 -- Apply checks suggested by AI05-0144 (Disabled for now)
1068 -- Check_Order_Dependence;
1073 end Analyze_Call_And_Resolve;
1075 -- Start of processing for Analyze_Procedure_Call
1078 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1079 -- a procedure call or an entry call. The prefix may denote an access
1080 -- to subprogram type, in which case an implicit dereference applies.
1081 -- If the prefix is an indexed component (without implicit dereference)
1082 -- then the construct denotes a call to a member of an entire family.
1083 -- If the prefix is a simple name, it may still denote a call to a
1084 -- parameterless member of an entry family. Resolution of these various
1085 -- interpretations is delicate.
1089 -- If this is a call of the form Obj.Op, the call may have been
1090 -- analyzed and possibly rewritten into a block, in which case
1093 if Analyzed (N) then
1097 -- If there is an error analyzing the name (which may have been
1098 -- rewritten if the original call was in prefix notation) then error
1099 -- has been emitted already, mark node and return.
1102 or else Etype (Name (N)) = Any_Type
1104 Set_Etype (N, Any_Type);
1108 -- Otherwise analyze the parameters
1110 if Present (Actuals) then
1111 Actual := First (Actuals);
1113 while Present (Actual) loop
1115 Check_Parameterless_Call (Actual);
1120 -- Special processing for Elab_Spec and Elab_Body calls
1122 if Nkind (P) = N_Attribute_Reference
1123 and then (Attribute_Name (P) = Name_Elab_Spec
1124 or else Attribute_Name (P) = Name_Elab_Body)
1126 if Present (Actuals) then
1128 ("no parameters allowed for this call", First (Actuals));
1132 Set_Etype (N, Standard_Void_Type);
1135 elsif Is_Entity_Name (P)
1136 and then Is_Record_Type (Etype (Entity (P)))
1137 and then Remote_AST_I_Dereference (P)
1141 elsif Is_Entity_Name (P)
1142 and then Ekind (Entity (P)) /= E_Entry_Family
1144 if Is_Access_Type (Etype (P))
1145 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1146 and then No (Actuals)
1147 and then Comes_From_Source (N)
1149 Error_Msg_N ("missing explicit dereference in call", N);
1152 Analyze_Call_And_Resolve;
1154 -- If the prefix is the simple name of an entry family, this is
1155 -- a parameterless call from within the task body itself.
1157 elsif Is_Entity_Name (P)
1158 and then Nkind (P) = N_Identifier
1159 and then Ekind (Entity (P)) = E_Entry_Family
1160 and then Present (Actuals)
1161 and then No (Next (First (Actuals)))
1163 -- Can be call to parameterless entry family. What appears to be the
1164 -- sole argument is in fact the entry index. Rewrite prefix of node
1165 -- accordingly. Source representation is unchanged by this
1169 Make_Indexed_Component (Loc,
1171 Make_Selected_Component (Loc,
1172 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1173 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1174 Expressions => Actuals);
1175 Set_Name (N, New_N);
1176 Set_Etype (New_N, Standard_Void_Type);
1177 Set_Parameter_Associations (N, No_List);
1178 Analyze_Call_And_Resolve;
1180 elsif Nkind (P) = N_Explicit_Dereference then
1181 if Ekind (Etype (P)) = E_Subprogram_Type then
1182 Analyze_Call_And_Resolve;
1184 Error_Msg_N ("expect access to procedure in call", P);
1187 -- The name can be a selected component or an indexed component that
1188 -- yields an access to subprogram. Such a prefix is legal if the call
1189 -- has parameter associations.
1191 elsif Is_Access_Type (Etype (P))
1192 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1194 if Present (Actuals) then
1195 Analyze_Call_And_Resolve;
1197 Error_Msg_N ("missing explicit dereference in call ", N);
1200 -- If not an access to subprogram, then the prefix must resolve to the
1201 -- name of an entry, entry family, or protected operation.
1203 -- For the case of a simple entry call, P is a selected component where
1204 -- the prefix is the task and the selector name is the entry. A call to
1205 -- a protected procedure will have the same syntax. If the protected
1206 -- object contains overloaded operations, the entity may appear as a
1207 -- function, the context will select the operation whose type is Void.
1209 elsif Nkind (P) = N_Selected_Component
1210 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1212 Ekind (Entity (Selector_Name (P))) = E_Procedure
1214 Ekind (Entity (Selector_Name (P))) = E_Function)
1216 Analyze_Call_And_Resolve;
1218 elsif Nkind (P) = N_Selected_Component
1219 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1220 and then Present (Actuals)
1221 and then No (Next (First (Actuals)))
1223 -- Can be call to parameterless entry family. What appears to be the
1224 -- sole argument is in fact the entry index. Rewrite prefix of node
1225 -- accordingly. Source representation is unchanged by this
1229 Make_Indexed_Component (Loc,
1230 Prefix => New_Copy (P),
1231 Expressions => Actuals);
1232 Set_Name (N, New_N);
1233 Set_Etype (New_N, Standard_Void_Type);
1234 Set_Parameter_Associations (N, No_List);
1235 Analyze_Call_And_Resolve;
1237 -- For the case of a reference to an element of an entry family, P is
1238 -- an indexed component whose prefix is a selected component (task and
1239 -- entry family), and whose index is the entry family index.
1241 elsif Nkind (P) = N_Indexed_Component
1242 and then Nkind (Prefix (P)) = N_Selected_Component
1243 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1245 Analyze_Call_And_Resolve;
1247 -- If the prefix is the name of an entry family, it is a call from
1248 -- within the task body itself.
1250 elsif Nkind (P) = N_Indexed_Component
1251 and then Nkind (Prefix (P)) = N_Identifier
1252 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1255 Make_Selected_Component (Loc,
1256 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1257 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1258 Rewrite (Prefix (P), New_N);
1260 Analyze_Call_And_Resolve;
1262 -- Anything else is an error
1265 Error_Msg_N ("invalid procedure or entry call", N);
1267 end Analyze_Procedure_Call;
1269 -------------------------------------
1270 -- Analyze_Simple_Return_Statement --
1271 -------------------------------------
1273 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1275 if Present (Expression (N)) then
1276 Mark_Coextensions (N, Expression (N));
1279 Analyze_Return_Statement (N);
1280 end Analyze_Simple_Return_Statement;
1282 -------------------------
1283 -- Analyze_Return_Type --
1284 -------------------------
1286 procedure Analyze_Return_Type (N : Node_Id) is
1287 Designator : constant Entity_Id := Defining_Entity (N);
1288 Typ : Entity_Id := Empty;
1291 -- Normal case where result definition does not indicate an error
1293 if Result_Definition (N) /= Error then
1294 if Nkind (Result_Definition (N)) = N_Access_Definition then
1296 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1299 AD : constant Node_Id :=
1300 Access_To_Subprogram_Definition (Result_Definition (N));
1302 if Present (AD) and then Protected_Present (AD) then
1303 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1305 Typ := Access_Definition (N, Result_Definition (N));
1309 Set_Parent (Typ, Result_Definition (N));
1310 Set_Is_Local_Anonymous_Access (Typ);
1311 Set_Etype (Designator, Typ);
1313 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1315 Null_Exclusion_Static_Checks (N);
1317 -- Subtype_Mark case
1320 Find_Type (Result_Definition (N));
1321 Typ := Entity (Result_Definition (N));
1322 Set_Etype (Designator, Typ);
1324 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1326 Null_Exclusion_Static_Checks (N);
1328 -- If a null exclusion is imposed on the result type, then create
1329 -- a null-excluding itype (an access subtype) and use it as the
1330 -- function's Etype. Note that the null exclusion checks are done
1331 -- right before this, because they don't get applied to types that
1332 -- do not come from source.
1334 if Is_Access_Type (Typ)
1335 and then Null_Exclusion_Present (N)
1337 Set_Etype (Designator,
1338 Create_Null_Excluding_Itype
1341 Scope_Id => Scope (Current_Scope)));
1343 -- The new subtype must be elaborated before use because
1344 -- it is visible outside of the function. However its base
1345 -- type may not be frozen yet, so the reference that will
1346 -- force elaboration must be attached to the freezing of
1349 -- If the return specification appears on a proper body,
1350 -- the subtype will have been created already on the spec.
1352 if Is_Frozen (Typ) then
1353 if Nkind (Parent (N)) = N_Subprogram_Body
1354 and then Nkind (Parent (Parent (N))) = N_Subunit
1358 Build_Itype_Reference (Etype (Designator), Parent (N));
1362 Ensure_Freeze_Node (Typ);
1365 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1367 Set_Itype (IR, Etype (Designator));
1368 Append_Freeze_Actions (Typ, New_List (IR));
1373 Set_Etype (Designator, Typ);
1376 if Ekind (Typ) = E_Incomplete_Type
1377 and then Is_Value_Type (Typ)
1381 elsif Ekind (Typ) = E_Incomplete_Type
1382 or else (Is_Class_Wide_Type (Typ)
1384 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1387 ("invalid use of incomplete type&", Designator, Typ);
1391 -- Case where result definition does indicate an error
1394 Set_Etype (Designator, Any_Type);
1396 end Analyze_Return_Type;
1398 -----------------------------
1399 -- Analyze_Subprogram_Body --
1400 -----------------------------
1402 procedure Analyze_Subprogram_Body (N : Node_Id) is
1403 Loc : constant Source_Ptr := Sloc (N);
1404 Body_Spec : constant Node_Id := Specification (N);
1405 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1408 if Debug_Flag_C then
1409 Write_Str ("==> subprogram body ");
1410 Write_Name (Chars (Body_Id));
1411 Write_Str (" from ");
1412 Write_Location (Loc);
1417 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1419 -- The real work is split out into the helper, so it can do "return;"
1420 -- without skipping the debug output:
1422 Analyze_Subprogram_Body_Helper (N);
1424 if Debug_Flag_C then
1426 Write_Str ("<== subprogram body ");
1427 Write_Name (Chars (Body_Id));
1428 Write_Str (" from ");
1429 Write_Location (Loc);
1432 end Analyze_Subprogram_Body;
1434 ------------------------------------
1435 -- Analyze_Subprogram_Body_Helper --
1436 ------------------------------------
1438 -- This procedure is called for regular subprogram bodies, generic bodies,
1439 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1440 -- specification matters, and is used to create a proper declaration for
1441 -- the subprogram, or to perform conformance checks.
1443 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1444 Loc : constant Source_Ptr := Sloc (N);
1445 Body_Deleted : constant Boolean := False;
1446 Body_Spec : constant Node_Id := Specification (N);
1447 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1448 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1449 Conformant : Boolean;
1452 Prot_Typ : Entity_Id := Empty;
1453 Spec_Id : Entity_Id;
1454 Spec_Decl : Node_Id := Empty;
1456 Last_Real_Spec_Entity : Entity_Id := Empty;
1457 -- When we analyze a separate spec, the entity chain ends up containing
1458 -- the formals, as well as any itypes generated during analysis of the
1459 -- default expressions for parameters, or the arguments of associated
1460 -- precondition/postcondition pragmas (which are analyzed in the context
1461 -- of the spec since they have visibility on formals).
1463 -- These entities belong with the spec and not the body. However we do
1464 -- the analysis of the body in the context of the spec (again to obtain
1465 -- visibility to the formals), and all the entities generated during
1466 -- this analysis end up also chained to the entity chain of the spec.
1467 -- But they really belong to the body, and there is circuitry to move
1468 -- them from the spec to the body.
1470 -- However, when we do this move, we don't want to move the real spec
1471 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1472 -- variable points to the last real spec entity, so we only move those
1473 -- chained beyond that point. It is initialized to Empty to deal with
1474 -- the case where there is no separate spec.
1476 procedure Check_Anonymous_Return;
1477 -- Ada 2005: if a function returns an access type that denotes a task,
1478 -- or a type that contains tasks, we must create a master entity for
1479 -- the anonymous type, which typically will be used in an allocator
1480 -- in the body of the function.
1482 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1483 -- Look ahead to recognize a pragma that may appear after the body.
1484 -- If there is a previous spec, check that it appears in the same
1485 -- declarative part. If the pragma is Inline_Always, perform inlining
1486 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1487 -- If the body acts as a spec, and inlining is required, we create a
1488 -- subprogram declaration for it, in order to attach the body to inline.
1489 -- If pragma does not appear after the body, check whether there is
1490 -- an inline pragma before any local declarations.
1492 procedure Check_Missing_Return;
1493 -- Checks for a function with a no return statements, and also performs
1494 -- the warning checks implemented by Check_Returns.
1496 function Disambiguate_Spec return Entity_Id;
1497 -- When a primitive is declared between the private view and the full
1498 -- view of a concurrent type which implements an interface, a special
1499 -- mechanism is used to find the corresponding spec of the primitive
1502 function Is_Private_Concurrent_Primitive
1503 (Subp_Id : Entity_Id) return Boolean;
1504 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1505 -- type that implements an interface and has a private view.
1507 procedure Set_Trivial_Subprogram (N : Node_Id);
1508 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1509 -- subprogram whose body is being analyzed. N is the statement node
1510 -- causing the flag to be set, if the following statement is a return
1511 -- of an entity, we mark the entity as set in source to suppress any
1512 -- warning on the stylized use of function stubs with a dummy return.
1514 procedure Verify_Overriding_Indicator;
1515 -- If there was a previous spec, the entity has been entered in the
1516 -- current scope previously. If the body itself carries an overriding
1517 -- indicator, check that it is consistent with the known status of the
1520 ----------------------------
1521 -- Check_Anonymous_Return --
1522 ----------------------------
1524 procedure Check_Anonymous_Return is
1530 if Present (Spec_Id) then
1536 if Ekind (Scop) = E_Function
1537 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1538 and then not Is_Thunk (Scop)
1539 and then (Has_Task (Designated_Type (Etype (Scop)))
1541 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1543 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1544 and then Expander_Active
1546 -- Avoid cases with no tasking support
1548 and then RTE_Available (RE_Current_Master)
1549 and then not Restriction_Active (No_Task_Hierarchy)
1552 Make_Object_Declaration (Loc,
1553 Defining_Identifier =>
1554 Make_Defining_Identifier (Loc, Name_uMaster),
1555 Constant_Present => True,
1556 Object_Definition =>
1557 New_Reference_To (RTE (RE_Master_Id), Loc),
1559 Make_Explicit_Dereference (Loc,
1560 New_Reference_To (RTE (RE_Current_Master), Loc)));
1562 if Present (Declarations (N)) then
1563 Prepend (Decl, Declarations (N));
1565 Set_Declarations (N, New_List (Decl));
1568 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1569 Set_Has_Master_Entity (Scop);
1571 -- Now mark the containing scope as a task master
1574 while Nkind (Par) /= N_Compilation_Unit loop
1575 Par := Parent (Par);
1576 pragma Assert (Present (Par));
1578 -- If we fall off the top, we are at the outer level, and
1579 -- the environment task is our effective master, so nothing
1583 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1585 Set_Is_Task_Master (Par, True);
1590 end Check_Anonymous_Return;
1592 -------------------------
1593 -- Check_Inline_Pragma --
1594 -------------------------
1596 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1600 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1601 -- True when N is a pragma Inline or Inline_Always that applies
1602 -- to this subprogram.
1604 -----------------------
1605 -- Is_Inline_Pragma --
1606 -----------------------
1608 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1611 Nkind (N) = N_Pragma
1613 (Pragma_Name (N) = Name_Inline_Always
1616 and then Pragma_Name (N) = Name_Inline))
1619 (Expression (First (Pragma_Argument_Associations (N))))
1621 end Is_Inline_Pragma;
1623 -- Start of processing for Check_Inline_Pragma
1626 if not Expander_Active then
1630 if Is_List_Member (N)
1631 and then Present (Next (N))
1632 and then Is_Inline_Pragma (Next (N))
1636 elsif Nkind (N) /= N_Subprogram_Body_Stub
1637 and then Present (Declarations (N))
1638 and then Is_Inline_Pragma (First (Declarations (N)))
1640 Prag := First (Declarations (N));
1646 if Present (Prag) then
1647 if Present (Spec_Id) then
1648 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1653 -- Create a subprogram declaration, to make treatment uniform
1656 Subp : constant Entity_Id :=
1657 Make_Defining_Identifier (Loc, Chars (Body_Id));
1658 Decl : constant Node_Id :=
1659 Make_Subprogram_Declaration (Loc,
1661 New_Copy_Tree (Specification (N)));
1664 Set_Defining_Unit_Name (Specification (Decl), Subp);
1666 if Present (First_Formal (Body_Id)) then
1667 Plist := Copy_Parameter_List (Body_Id);
1668 Set_Parameter_Specifications
1669 (Specification (Decl), Plist);
1672 Insert_Before (N, Decl);
1675 Set_Has_Pragma_Inline (Subp);
1677 if Pragma_Name (Prag) = Name_Inline_Always then
1678 Set_Is_Inlined (Subp);
1679 Set_Has_Pragma_Inline_Always (Subp);
1686 end Check_Inline_Pragma;
1688 --------------------------
1689 -- Check_Missing_Return --
1690 --------------------------
1692 procedure Check_Missing_Return is
1694 Missing_Ret : Boolean;
1697 if Nkind (Body_Spec) = N_Function_Specification then
1698 if Present (Spec_Id) then
1704 if Return_Present (Id) then
1705 Check_Returns (HSS, 'F', Missing_Ret);
1708 Set_Has_Missing_Return (Id);
1711 elsif (Is_Generic_Subprogram (Id)
1712 or else not Is_Machine_Code_Subprogram (Id))
1713 and then not Body_Deleted
1715 Error_Msg_N ("missing RETURN statement in function body", N);
1718 -- If procedure with No_Return, check returns
1720 elsif Nkind (Body_Spec) = N_Procedure_Specification
1721 and then Present (Spec_Id)
1722 and then No_Return (Spec_Id)
1724 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1726 end Check_Missing_Return;
1728 -----------------------
1729 -- Disambiguate_Spec --
1730 -----------------------
1732 function Disambiguate_Spec return Entity_Id is
1733 Priv_Spec : Entity_Id;
1736 procedure Replace_Types (To_Corresponding : Boolean);
1737 -- Depending on the flag, replace the type of formal parameters of
1738 -- Body_Id if it is a concurrent type implementing interfaces with
1739 -- the corresponding record type or the other way around.
1741 procedure Replace_Types (To_Corresponding : Boolean) is
1743 Formal_Typ : Entity_Id;
1746 Formal := First_Formal (Body_Id);
1747 while Present (Formal) loop
1748 Formal_Typ := Etype (Formal);
1750 -- From concurrent type to corresponding record
1752 if To_Corresponding then
1753 if Is_Concurrent_Type (Formal_Typ)
1754 and then Present (Corresponding_Record_Type (Formal_Typ))
1755 and then Present (Interfaces (
1756 Corresponding_Record_Type (Formal_Typ)))
1759 Corresponding_Record_Type (Formal_Typ));
1762 -- From corresponding record to concurrent type
1765 if Is_Concurrent_Record_Type (Formal_Typ)
1766 and then Present (Interfaces (Formal_Typ))
1769 Corresponding_Concurrent_Type (Formal_Typ));
1773 Next_Formal (Formal);
1777 -- Start of processing for Disambiguate_Spec
1780 -- Try to retrieve the specification of the body as is. All error
1781 -- messages are suppressed because the body may not have a spec in
1782 -- its current state.
1784 Spec_N := Find_Corresponding_Spec (N, False);
1786 -- It is possible that this is the body of a primitive declared
1787 -- between a private and a full view of a concurrent type. The
1788 -- controlling parameter of the spec carries the concurrent type,
1789 -- not the corresponding record type as transformed by Analyze_
1790 -- Subprogram_Specification. In such cases, we undo the change
1791 -- made by the analysis of the specification and try to find the
1794 -- Note that wrappers already have their corresponding specs and
1795 -- bodies set during their creation, so if the candidate spec is
1796 -- a wrapper, then we definitely need to swap all types to their
1797 -- original concurrent status.
1800 or else Is_Primitive_Wrapper (Spec_N)
1802 -- Restore all references of corresponding record types to the
1803 -- original concurrent types.
1805 Replace_Types (To_Corresponding => False);
1806 Priv_Spec := Find_Corresponding_Spec (N, False);
1808 -- The current body truly belongs to a primitive declared between
1809 -- a private and a full view. We leave the modified body as is,
1810 -- and return the true spec.
1812 if Present (Priv_Spec)
1813 and then Is_Private_Primitive (Priv_Spec)
1818 -- In case that this is some sort of error, restore the original
1819 -- state of the body.
1821 Replace_Types (To_Corresponding => True);
1825 end Disambiguate_Spec;
1827 -------------------------------------
1828 -- Is_Private_Concurrent_Primitive --
1829 -------------------------------------
1831 function Is_Private_Concurrent_Primitive
1832 (Subp_Id : Entity_Id) return Boolean
1834 Formal_Typ : Entity_Id;
1837 if Present (First_Formal (Subp_Id)) then
1838 Formal_Typ := Etype (First_Formal (Subp_Id));
1840 if Is_Concurrent_Record_Type (Formal_Typ) then
1841 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1844 -- The type of the first formal is a concurrent tagged type with
1848 Is_Concurrent_Type (Formal_Typ)
1849 and then Is_Tagged_Type (Formal_Typ)
1850 and then Has_Private_Declaration (Formal_Typ);
1854 end Is_Private_Concurrent_Primitive;
1856 ----------------------------
1857 -- Set_Trivial_Subprogram --
1858 ----------------------------
1860 procedure Set_Trivial_Subprogram (N : Node_Id) is
1861 Nxt : constant Node_Id := Next (N);
1864 Set_Is_Trivial_Subprogram (Body_Id);
1866 if Present (Spec_Id) then
1867 Set_Is_Trivial_Subprogram (Spec_Id);
1871 and then Nkind (Nxt) = N_Simple_Return_Statement
1872 and then No (Next (Nxt))
1873 and then Present (Expression (Nxt))
1874 and then Is_Entity_Name (Expression (Nxt))
1876 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1878 end Set_Trivial_Subprogram;
1880 ---------------------------------
1881 -- Verify_Overriding_Indicator --
1882 ---------------------------------
1884 procedure Verify_Overriding_Indicator is
1886 if Must_Override (Body_Spec) then
1887 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1888 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1892 elsif not Is_Overriding_Operation (Spec_Id) then
1894 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1897 elsif Must_Not_Override (Body_Spec) then
1898 if Is_Overriding_Operation (Spec_Id) then
1900 ("subprogram& overrides inherited operation",
1901 Body_Spec, Spec_Id);
1903 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1904 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1907 ("subprogram & overrides predefined operator ",
1908 Body_Spec, Spec_Id);
1910 -- If this is not a primitive operation or protected subprogram,
1911 -- then the overriding indicator is altogether illegal.
1913 elsif not Is_Primitive (Spec_Id)
1914 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1917 ("overriding indicator only allowed " &
1918 "if subprogram is primitive",
1922 elsif Style_Check -- ??? incorrect use of Style_Check!
1923 and then Is_Overriding_Operation (Spec_Id)
1925 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1926 Style.Missing_Overriding (N, Body_Id);
1928 end Verify_Overriding_Indicator;
1930 -- Start of processing for Analyze_Subprogram_Body_Helper
1933 -- Generic subprograms are handled separately. They always have a
1934 -- generic specification. Determine whether current scope has a
1935 -- previous declaration.
1937 -- If the subprogram body is defined within an instance of the same
1938 -- name, the instance appears as a package renaming, and will be hidden
1939 -- within the subprogram.
1941 if Present (Prev_Id)
1942 and then not Is_Overloadable (Prev_Id)
1943 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1944 or else Comes_From_Source (Prev_Id))
1946 if Is_Generic_Subprogram (Prev_Id) then
1948 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1949 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1951 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1953 if Nkind (N) = N_Subprogram_Body then
1954 HSS := Handled_Statement_Sequence (N);
1955 Check_Missing_Return;
1961 -- Previous entity conflicts with subprogram name. Attempting to
1962 -- enter name will post error.
1964 Enter_Name (Body_Id);
1968 -- Non-generic case, find the subprogram declaration, if one was seen,
1969 -- or enter new overloaded entity in the current scope. If the
1970 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1971 -- part of the context of one of its subunits. No need to redo the
1974 elsif Prev_Id = Body_Id
1975 and then Has_Completion (Body_Id)
1980 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1982 if Nkind (N) = N_Subprogram_Body_Stub
1983 or else No (Corresponding_Spec (N))
1985 if Is_Private_Concurrent_Primitive (Body_Id) then
1986 Spec_Id := Disambiguate_Spec;
1988 Spec_Id := Find_Corresponding_Spec (N);
1991 -- If this is a duplicate body, no point in analyzing it
1993 if Error_Posted (N) then
1997 -- A subprogram body should cause freezing of its own declaration,
1998 -- but if there was no previous explicit declaration, then the
1999 -- subprogram will get frozen too late (there may be code within
2000 -- the body that depends on the subprogram having been frozen,
2001 -- such as uses of extra formals), so we force it to be frozen
2002 -- here. Same holds if the body and spec are compilation units.
2003 -- Finally, if the return type is an anonymous access to protected
2004 -- subprogram, it must be frozen before the body because its
2005 -- expansion has generated an equivalent type that is used when
2006 -- elaborating the body.
2008 if No (Spec_Id) then
2009 Freeze_Before (N, Body_Id);
2011 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2012 Freeze_Before (N, Spec_Id);
2014 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2015 Freeze_Before (N, Etype (Body_Id));
2019 Spec_Id := Corresponding_Spec (N);
2023 -- Do not inline any subprogram that contains nested subprograms, since
2024 -- the backend inlining circuit seems to generate uninitialized
2025 -- references in this case. We know this happens in the case of front
2026 -- end ZCX support, but it also appears it can happen in other cases as
2027 -- well. The backend often rejects attempts to inline in the case of
2028 -- nested procedures anyway, so little if anything is lost by this.
2029 -- Note that this is test is for the benefit of the back-end. There is
2030 -- a separate test for front-end inlining that also rejects nested
2033 -- Do not do this test if errors have been detected, because in some
2034 -- error cases, this code blows up, and we don't need it anyway if
2035 -- there have been errors, since we won't get to the linker anyway.
2037 if Comes_From_Source (Body_Id)
2038 and then Serious_Errors_Detected = 0
2042 P_Ent := Scope (P_Ent);
2043 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2045 if Is_Subprogram (P_Ent) then
2046 Set_Is_Inlined (P_Ent, False);
2048 if Comes_From_Source (P_Ent)
2049 and then Has_Pragma_Inline (P_Ent)
2052 ("cannot inline& (nested subprogram)?",
2059 Check_Inline_Pragma (Spec_Id);
2061 -- Deal with special case of a fully private operation in the body of
2062 -- the protected type. We must create a declaration for the subprogram,
2063 -- in order to attach the protected subprogram that will be used in
2064 -- internal calls. We exclude compiler generated bodies from the
2065 -- expander since the issue does not arise for those cases.
2068 and then Comes_From_Source (N)
2069 and then Is_Protected_Type (Current_Scope)
2071 Spec_Id := Build_Private_Protected_Declaration (N);
2074 -- If a separate spec is present, then deal with freezing issues
2076 if Present (Spec_Id) then
2077 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2078 Verify_Overriding_Indicator;
2080 -- In general, the spec will be frozen when we start analyzing the
2081 -- body. However, for internally generated operations, such as
2082 -- wrapper functions for inherited operations with controlling
2083 -- results, the spec may not have been frozen by the time we
2084 -- expand the freeze actions that include the bodies. In particular,
2085 -- extra formals for accessibility or for return-in-place may need
2086 -- to be generated. Freeze nodes, if any, are inserted before the
2089 if not Is_Frozen (Spec_Id)
2090 and then Expander_Active
2092 -- Force the generation of its freezing node to ensure proper
2093 -- management of access types in the backend.
2095 -- This is definitely needed for some cases, but it is not clear
2096 -- why, to be investigated further???
2098 Set_Has_Delayed_Freeze (Spec_Id);
2099 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
2103 -- Mark presence of postcondition procedure in current scope and mark
2104 -- the procedure itself as needing debug info. The latter is important
2105 -- when analyzing decision coverage (for example, for MC/DC coverage).
2107 if Chars (Body_Id) = Name_uPostconditions then
2108 Set_Has_Postconditions (Current_Scope);
2109 Set_Debug_Info_Needed (Body_Id);
2112 -- Place subprogram on scope stack, and make formals visible. If there
2113 -- is a spec, the visible entity remains that of the spec.
2115 if Present (Spec_Id) then
2116 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2118 if Is_Child_Unit (Spec_Id) then
2119 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2123 Style.Check_Identifier (Body_Id, Spec_Id);
2126 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2127 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2129 if Is_Abstract_Subprogram (Spec_Id) then
2130 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2134 Set_Convention (Body_Id, Convention (Spec_Id));
2135 Set_Has_Completion (Spec_Id);
2137 if Is_Protected_Type (Scope (Spec_Id)) then
2138 Prot_Typ := Scope (Spec_Id);
2141 -- If this is a body generated for a renaming, do not check for
2142 -- full conformance. The check is redundant, because the spec of
2143 -- the body is a copy of the spec in the renaming declaration,
2144 -- and the test can lead to spurious errors on nested defaults.
2146 if Present (Spec_Decl)
2147 and then not Comes_From_Source (N)
2149 (Nkind (Original_Node (Spec_Decl)) =
2150 N_Subprogram_Renaming_Declaration
2151 or else (Present (Corresponding_Body (Spec_Decl))
2153 Nkind (Unit_Declaration_Node
2154 (Corresponding_Body (Spec_Decl))) =
2155 N_Subprogram_Renaming_Declaration))
2159 -- Conversely, the spec may have been generated for specless body
2160 -- with an inline pragma.
2162 elsif Comes_From_Source (N)
2163 and then not Comes_From_Source (Spec_Id)
2164 and then Has_Pragma_Inline (Spec_Id)
2171 Fully_Conformant, True, Conformant, Body_Id);
2174 -- If the body is not fully conformant, we have to decide if we
2175 -- should analyze it or not. If it has a really messed up profile
2176 -- then we probably should not analyze it, since we will get too
2177 -- many bogus messages.
2179 -- Our decision is to go ahead in the non-fully conformant case
2180 -- only if it is at least mode conformant with the spec. Note
2181 -- that the call to Check_Fully_Conformant has issued the proper
2182 -- error messages to complain about the lack of conformance.
2185 and then not Mode_Conformant (Body_Id, Spec_Id)
2191 if Spec_Id /= Body_Id then
2192 Reference_Body_Formals (Spec_Id, Body_Id);
2195 if Nkind (N) /= N_Subprogram_Body_Stub then
2196 Set_Corresponding_Spec (N, Spec_Id);
2198 -- Ada 2005 (AI-345): If the operation is a primitive operation
2199 -- of a concurrent type, the type of the first parameter has been
2200 -- replaced with the corresponding record, which is the proper
2201 -- run-time structure to use. However, within the body there may
2202 -- be uses of the formals that depend on primitive operations
2203 -- of the type (in particular calls in prefixed form) for which
2204 -- we need the original concurrent type. The operation may have
2205 -- several controlling formals, so the replacement must be done
2208 if Comes_From_Source (Spec_Id)
2209 and then Present (First_Entity (Spec_Id))
2210 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2211 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2213 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2216 (Corresponding_Concurrent_Type
2217 (Etype (First_Entity (Spec_Id))))
2220 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2224 Form := First_Formal (Spec_Id);
2225 while Present (Form) loop
2226 if Etype (Form) = Typ then
2227 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2235 -- Make the formals visible, and place subprogram on scope stack.
2236 -- This is also the point at which we set Last_Real_Spec_Entity
2237 -- to mark the entities which will not be moved to the body.
2239 Install_Formals (Spec_Id);
2240 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2241 Push_Scope (Spec_Id);
2243 -- Make sure that the subprogram is immediately visible. For
2244 -- child units that have no separate spec this is indispensable.
2245 -- Otherwise it is safe albeit redundant.
2247 Set_Is_Immediately_Visible (Spec_Id);
2250 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2251 Set_Ekind (Body_Id, E_Subprogram_Body);
2252 Set_Scope (Body_Id, Scope (Spec_Id));
2253 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2255 -- Case of subprogram body with no previous spec
2259 and then Comes_From_Source (Body_Id)
2260 and then not Suppress_Style_Checks (Body_Id)
2261 and then not In_Instance
2263 Style.Body_With_No_Spec (N);
2266 New_Overloaded_Entity (Body_Id);
2268 if Nkind (N) /= N_Subprogram_Body_Stub then
2269 Set_Acts_As_Spec (N);
2270 Generate_Definition (Body_Id);
2272 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2273 Generate_Reference_To_Formals (Body_Id);
2274 Install_Formals (Body_Id);
2275 Push_Scope (Body_Id);
2279 -- If the return type is an anonymous access type whose designated type
2280 -- is the limited view of a class-wide type and the non-limited view is
2281 -- available, update the return type accordingly.
2283 if Ada_Version >= Ada_05
2284 and then Comes_From_Source (N)
2291 Rtyp := Etype (Current_Scope);
2293 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2294 Etyp := Directly_Designated_Type (Rtyp);
2296 if Is_Class_Wide_Type (Etyp)
2297 and then From_With_Type (Etyp)
2299 Set_Directly_Designated_Type
2300 (Etype (Current_Scope), Available_View (Etyp));
2306 -- If this is the proper body of a stub, we must verify that the stub
2307 -- conforms to the body, and to the previous spec if one was present.
2308 -- we know already that the body conforms to that spec. This test is
2309 -- only required for subprograms that come from source.
2311 if Nkind (Parent (N)) = N_Subunit
2312 and then Comes_From_Source (N)
2313 and then not Error_Posted (Body_Id)
2314 and then Nkind (Corresponding_Stub (Parent (N))) =
2315 N_Subprogram_Body_Stub
2318 Old_Id : constant Entity_Id :=
2320 (Specification (Corresponding_Stub (Parent (N))));
2322 Conformant : Boolean := False;
2325 if No (Spec_Id) then
2326 Check_Fully_Conformant (Body_Id, Old_Id);
2330 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2332 if not Conformant then
2334 -- The stub was taken to be a new declaration. Indicate
2335 -- that it lacks a body.
2337 Set_Has_Completion (Old_Id, False);
2343 Set_Has_Completion (Body_Id);
2344 Check_Eliminated (Body_Id);
2346 if Nkind (N) = N_Subprogram_Body_Stub then
2349 elsif Present (Spec_Id)
2350 and then Expander_Active
2352 (Has_Pragma_Inline_Always (Spec_Id)
2353 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2355 Build_Body_To_Inline (N, Spec_Id);
2358 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2359 -- if its specification we have to install the private withed units.
2360 -- This holds for child units as well.
2362 if Is_Compilation_Unit (Body_Id)
2363 or else Nkind (Parent (N)) = N_Compilation_Unit
2365 Install_Private_With_Clauses (Body_Id);
2368 Check_Anonymous_Return;
2370 -- Set the Protected_Formal field of each extra formal of the protected
2371 -- subprogram to reference the corresponding extra formal of the
2372 -- subprogram that implements it. For regular formals this occurs when
2373 -- the protected subprogram's declaration is expanded, but the extra
2374 -- formals don't get created until the subprogram is frozen. We need to
2375 -- do this before analyzing the protected subprogram's body so that any
2376 -- references to the original subprogram's extra formals will be changed
2377 -- refer to the implementing subprogram's formals (see Expand_Formal).
2379 if Present (Spec_Id)
2380 and then Is_Protected_Type (Scope (Spec_Id))
2381 and then Present (Protected_Body_Subprogram (Spec_Id))
2384 Impl_Subp : constant Entity_Id :=
2385 Protected_Body_Subprogram (Spec_Id);
2386 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2387 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2389 while Present (Prot_Ext_Formal) loop
2390 pragma Assert (Present (Impl_Ext_Formal));
2391 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2392 Next_Formal_With_Extras (Prot_Ext_Formal);
2393 Next_Formal_With_Extras (Impl_Ext_Formal);
2398 -- Now we can go on to analyze the body
2400 HSS := Handled_Statement_Sequence (N);
2401 Set_Actual_Subtypes (N, Current_Scope);
2403 -- Deal with preconditions and postconditions
2405 Process_PPCs (N, Spec_Id, Body_Id);
2407 -- Add a declaration for the Protection object, renaming declarations
2408 -- for discriminals and privals and finally a declaration for the entry
2409 -- family index (if applicable). This form of early expansion is done
2410 -- when the Expander is active because Install_Private_Data_Declarations
2411 -- references entities which were created during regular expansion.
2414 and then Comes_From_Source (N)
2415 and then Present (Prot_Typ)
2416 and then Present (Spec_Id)
2417 and then not Is_Eliminated (Spec_Id)
2419 Install_Private_Data_Declarations
2420 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2423 -- Analyze the declarations (this call will analyze the precondition
2424 -- Check pragmas we prepended to the list, as well as the declaration
2425 -- of the _Postconditions procedure).
2427 Analyze_Declarations (Declarations (N));
2429 -- Check completion, and analyze the statements
2432 Inspect_Deferred_Constant_Completion (Declarations (N));
2435 -- Deal with end of scope processing for the body
2437 Process_End_Label (HSS, 't', Current_Scope);
2439 Check_Subprogram_Order (N);
2440 Set_Analyzed (Body_Id);
2442 -- If we have a separate spec, then the analysis of the declarations
2443 -- caused the entities in the body to be chained to the spec id, but
2444 -- we want them chained to the body id. Only the formal parameters
2445 -- end up chained to the spec id in this case.
2447 if Present (Spec_Id) then
2449 -- We must conform to the categorization of our spec
2451 Validate_Categorization_Dependency (N, Spec_Id);
2453 -- And if this is a child unit, the parent units must conform
2455 if Is_Child_Unit (Spec_Id) then
2456 Validate_Categorization_Dependency
2457 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2460 -- Here is where we move entities from the spec to the body
2462 -- Case where there are entities that stay with the spec
2464 if Present (Last_Real_Spec_Entity) then
2466 -- No body entities (happens when the only real spec entities
2467 -- come from precondition and postcondition pragmas)
2469 if No (Last_Entity (Body_Id)) then
2471 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2473 -- Body entities present (formals), so chain stuff past them
2477 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2480 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2481 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2482 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2484 -- Case where there are no spec entities, in this case there can
2485 -- be no body entities either, so just move everything.
2488 pragma Assert (No (Last_Entity (Body_Id)));
2489 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2490 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2491 Set_First_Entity (Spec_Id, Empty);
2492 Set_Last_Entity (Spec_Id, Empty);
2496 Check_Missing_Return;
2498 -- Now we are going to check for variables that are never modified in
2499 -- the body of the procedure. But first we deal with a special case
2500 -- where we want to modify this check. If the body of the subprogram
2501 -- starts with a raise statement or its equivalent, or if the body
2502 -- consists entirely of a null statement, then it is pretty obvious
2503 -- that it is OK to not reference the parameters. For example, this
2504 -- might be the following common idiom for a stubbed function:
2505 -- statement of the procedure raises an exception. In particular this
2506 -- deals with the common idiom of a stubbed function, which might
2507 -- appear as something like
2509 -- function F (A : Integer) return Some_Type;
2512 -- raise Program_Error;
2516 -- Here the purpose of X is simply to satisfy the annoying requirement
2517 -- in Ada that there be at least one return, and we certainly do not
2518 -- want to go posting warnings on X that it is not initialized! On
2519 -- the other hand, if X is entirely unreferenced that should still
2522 -- What we do is to detect these cases, and if we find them, flag the
2523 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2524 -- suppress unwanted warnings. For the case of the function stub above
2525 -- we have a special test to set X as apparently assigned to suppress
2532 -- Skip initial labels (for one thing this occurs when we are in
2533 -- front end ZCX mode, but in any case it is irrelevant), and also
2534 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2536 Stm := First (Statements (HSS));
2537 while Nkind (Stm) = N_Label
2538 or else Nkind (Stm) in N_Push_xxx_Label
2543 -- Do the test on the original statement before expansion
2546 Ostm : constant Node_Id := Original_Node (Stm);
2549 -- If explicit raise statement, turn on flag
2551 if Nkind (Ostm) = N_Raise_Statement then
2552 Set_Trivial_Subprogram (Stm);
2554 -- If null statement, and no following statements, turn on flag
2556 elsif Nkind (Stm) = N_Null_Statement
2557 and then Comes_From_Source (Stm)
2558 and then No (Next (Stm))
2560 Set_Trivial_Subprogram (Stm);
2562 -- Check for explicit call cases which likely raise an exception
2564 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2565 if Is_Entity_Name (Name (Ostm)) then
2567 Ent : constant Entity_Id := Entity (Name (Ostm));
2570 -- If the procedure is marked No_Return, then likely it
2571 -- raises an exception, but in any case it is not coming
2572 -- back here, so turn on the flag.
2574 if Ekind (Ent) = E_Procedure
2575 and then No_Return (Ent)
2577 Set_Trivial_Subprogram (Stm);
2585 -- Check for variables that are never modified
2591 -- If there is a separate spec, then transfer Never_Set_In_Source
2592 -- flags from out parameters to the corresponding entities in the
2593 -- body. The reason we do that is we want to post error flags on
2594 -- the body entities, not the spec entities.
2596 if Present (Spec_Id) then
2597 E1 := First_Entity (Spec_Id);
2598 while Present (E1) loop
2599 if Ekind (E1) = E_Out_Parameter then
2600 E2 := First_Entity (Body_Id);
2601 while Present (E2) loop
2602 exit when Chars (E1) = Chars (E2);
2606 if Present (E2) then
2607 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2615 -- Check references in body unless it was deleted. Note that the
2616 -- check of Body_Deleted here is not just for efficiency, it is
2617 -- necessary to avoid junk warnings on formal parameters.
2619 if not Body_Deleted then
2620 Check_References (Body_Id);
2623 end Analyze_Subprogram_Body_Helper;
2625 ------------------------------------
2626 -- Analyze_Subprogram_Declaration --
2627 ------------------------------------
2629 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2630 Loc : constant Source_Ptr := Sloc (N);
2631 Designator : Entity_Id;
2633 Scop : constant Entity_Id := Current_Scope;
2634 Null_Body : Node_Id := Empty;
2636 -- Start of processing for Analyze_Subprogram_Declaration
2639 -- For a null procedure, capture the profile before analysis, for
2640 -- expansion at the freeze point and at each point of call.
2641 -- The body will only be used if the procedure has preconditions.
2642 -- In that case the body is analyzed at the freeze point.
2644 if Nkind (Specification (N)) = N_Procedure_Specification
2645 and then Null_Present (Specification (N))
2646 and then Expander_Active
2649 Make_Subprogram_Body (Loc,
2651 New_Copy_Tree (Specification (N)),
2654 Handled_Statement_Sequence =>
2655 Make_Handled_Sequence_Of_Statements (Loc,
2656 Statements => New_List (Make_Null_Statement (Loc))));
2658 -- Create new entities for body and formals
2660 Set_Defining_Unit_Name (Specification (Null_Body),
2661 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2662 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2664 Form := First (Parameter_Specifications (Specification (Null_Body)));
2665 while Present (Form) loop
2666 Set_Defining_Identifier (Form,
2667 Make_Defining_Identifier (Loc,
2668 Chars (Defining_Identifier (Form))));
2672 if Is_Protected_Type (Current_Scope) then
2673 Error_Msg_N ("protected operation cannot be a null procedure", N);
2677 Designator := Analyze_Subprogram_Specification (Specification (N));
2678 Generate_Definition (Designator);
2680 if Debug_Flag_C then
2681 Write_Str ("==> subprogram spec ");
2682 Write_Name (Chars (Designator));
2683 Write_Str (" from ");
2684 Write_Location (Sloc (N));
2689 if Nkind (Specification (N)) = N_Procedure_Specification
2690 and then Null_Present (Specification (N))
2692 Set_Has_Completion (Designator);
2694 if Present (Null_Body) then
2695 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2696 Set_Body_To_Inline (N, Null_Body);
2697 Set_Is_Inlined (Designator);
2701 Validate_RCI_Subprogram_Declaration (N);
2702 New_Overloaded_Entity (Designator);
2703 Check_Delayed_Subprogram (Designator);
2705 -- If the type of the first formal of the current subprogram is a
2706 -- nongeneric tagged private type, mark the subprogram as being a
2707 -- private primitive. Ditto if this is a function with controlling
2708 -- result, and the return type is currently private. In both cases,
2709 -- the type of the controlling argument or result must be in the
2710 -- current scope for the operation to be primitive.
2712 if Has_Controlling_Result (Designator)
2713 and then Is_Private_Type (Etype (Designator))
2714 and then Scope (Etype (Designator)) = Current_Scope
2715 and then not Is_Generic_Actual_Type (Etype (Designator))
2717 Set_Is_Private_Primitive (Designator);
2719 elsif Present (First_Formal (Designator)) then
2721 Formal_Typ : constant Entity_Id :=
2722 Etype (First_Formal (Designator));
2724 Set_Is_Private_Primitive (Designator,
2725 Is_Tagged_Type (Formal_Typ)
2726 and then Scope (Formal_Typ) = Current_Scope
2727 and then Is_Private_Type (Formal_Typ)
2728 and then not Is_Generic_Actual_Type (Formal_Typ));
2732 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2735 if Ada_Version >= Ada_05
2736 and then Comes_From_Source (N)
2737 and then Is_Dispatching_Operation (Designator)
2744 if Has_Controlling_Result (Designator) then
2745 Etyp := Etype (Designator);
2748 E := First_Entity (Designator);
2750 and then Is_Formal (E)
2751 and then not Is_Controlling_Formal (E)
2759 if Is_Access_Type (Etyp) then
2760 Etyp := Directly_Designated_Type (Etyp);
2763 if Is_Interface (Etyp)
2764 and then not Is_Abstract_Subprogram (Designator)
2765 and then not (Ekind (Designator) = E_Procedure
2766 and then Null_Present (Specification (N)))
2768 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2770 ("(Ada 2005) interface subprogram % must be abstract or null",
2776 -- What is the following code for, it used to be
2778 -- ??? Set_Suppress_Elaboration_Checks
2779 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2781 -- The following seems equivalent, but a bit dubious
2783 if Elaboration_Checks_Suppressed (Designator) then
2784 Set_Kill_Elaboration_Checks (Designator);
2787 if Scop /= Standard_Standard
2788 and then not Is_Child_Unit (Designator)
2790 Set_Categorization_From_Scope (Designator, Scop);
2792 -- For a compilation unit, check for library-unit pragmas
2794 Push_Scope (Designator);
2795 Set_Categorization_From_Pragmas (N);
2796 Validate_Categorization_Dependency (N, Designator);
2800 -- For a compilation unit, set body required. This flag will only be
2801 -- reset if a valid Import or Interface pragma is processed later on.
2803 if Nkind (Parent (N)) = N_Compilation_Unit then
2804 Set_Body_Required (Parent (N), True);
2806 if Ada_Version >= Ada_05
2807 and then Nkind (Specification (N)) = N_Procedure_Specification
2808 and then Null_Present (Specification (N))
2811 ("null procedure cannot be declared at library level", N);
2815 Generate_Reference_To_Formals (Designator);
2816 Check_Eliminated (Designator);
2818 if Debug_Flag_C then
2820 Write_Str ("<== subprogram spec ");
2821 Write_Name (Chars (Designator));
2822 Write_Str (" from ");
2823 Write_Location (Sloc (N));
2826 end Analyze_Subprogram_Declaration;
2828 --------------------------------------
2829 -- Analyze_Subprogram_Specification --
2830 --------------------------------------
2832 -- Reminder: N here really is a subprogram specification (not a subprogram
2833 -- declaration). This procedure is called to analyze the specification in
2834 -- both subprogram bodies and subprogram declarations (specs).
2836 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2837 Designator : constant Entity_Id := Defining_Entity (N);
2838 Formals : constant List_Id := Parameter_Specifications (N);
2840 -- Start of processing for Analyze_Subprogram_Specification
2843 Generate_Definition (Designator);
2845 if Nkind (N) = N_Function_Specification then
2846 Set_Ekind (Designator, E_Function);
2847 Set_Mechanism (Designator, Default_Mechanism);
2850 Set_Ekind (Designator, E_Procedure);
2851 Set_Etype (Designator, Standard_Void_Type);
2854 -- Introduce new scope for analysis of the formals and the return type
2856 Set_Scope (Designator, Current_Scope);
2858 if Present (Formals) then
2859 Push_Scope (Designator);
2860 Process_Formals (Formals, N);
2862 -- Ada 2005 (AI-345): If this is an overriding operation of an
2863 -- inherited interface operation, and the controlling type is
2864 -- a synchronized type, replace the type with its corresponding
2865 -- record, to match the proper signature of an overriding operation.
2866 -- Same processing for an access parameter whose designated type is
2867 -- derived from a synchronized interface.
2869 if Ada_Version >= Ada_05 then
2872 Formal_Typ : Entity_Id;
2873 Rec_Typ : Entity_Id;
2874 Desig_Typ : Entity_Id;
2877 Formal := First_Formal (Designator);
2878 while Present (Formal) loop
2879 Formal_Typ := Etype (Formal);
2881 if Is_Concurrent_Type (Formal_Typ)
2882 and then Present (Corresponding_Record_Type (Formal_Typ))
2884 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2886 if Present (Interfaces (Rec_Typ)) then
2887 Set_Etype (Formal, Rec_Typ);
2890 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
2891 Desig_Typ := Designated_Type (Formal_Typ);
2893 if Is_Concurrent_Type (Desig_Typ)
2894 and then Present (Corresponding_Record_Type (Desig_Typ))
2896 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
2898 if Present (Interfaces (Rec_Typ)) then
2899 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
2904 Next_Formal (Formal);
2911 -- The subprogram scope is pushed and popped around the processing of
2912 -- the return type for consistency with call above to Process_Formals
2913 -- (which itself can call Analyze_Return_Type), and to ensure that any
2914 -- itype created for the return type will be associated with the proper
2917 elsif Nkind (N) = N_Function_Specification then
2918 Push_Scope (Designator);
2920 Analyze_Return_Type (N);
2925 if Nkind (N) = N_Function_Specification then
2926 if Nkind (Designator) = N_Defining_Operator_Symbol then
2927 Valid_Operator_Definition (Designator);
2930 May_Need_Actuals (Designator);
2932 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2933 -- the subprogram is abstract also. This does not apply to renaming
2934 -- declarations, where abstractness is inherited.
2935 -- In case of primitives associated with abstract interface types
2936 -- the check is applied later (see Analyze_Subprogram_Declaration).
2938 if Is_Abstract_Type (Etype (Designator))
2939 and then not Is_Interface (Etype (Designator))
2940 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2941 and then Nkind (Parent (N)) /=
2942 N_Abstract_Subprogram_Declaration
2944 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2947 ("function that returns abstract type must be abstract", N);
2952 end Analyze_Subprogram_Specification;
2954 --------------------------
2955 -- Build_Body_To_Inline --
2956 --------------------------
2958 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2959 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2960 Original_Body : Node_Id;
2961 Body_To_Analyze : Node_Id;
2962 Max_Size : constant := 10;
2963 Stat_Count : Integer := 0;
2965 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2966 -- Check for declarations that make inlining not worthwhile
2968 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2969 -- Check for statements that make inlining not worthwhile: any tasking
2970 -- statement, nested at any level. Keep track of total number of
2971 -- elementary statements, as a measure of acceptable size.
2973 function Has_Pending_Instantiation return Boolean;
2974 -- If some enclosing body contains instantiations that appear before the
2975 -- corresponding generic body, the enclosing body has a freeze node so
2976 -- that it can be elaborated after the generic itself. This might
2977 -- conflict with subsequent inlinings, so that it is unsafe to try to
2978 -- inline in such a case.
2980 function Has_Single_Return return Boolean;
2981 -- In general we cannot inline functions that return unconstrained type.
2982 -- However, we can handle such functions if all return statements return
2983 -- a local variable that is the only declaration in the body of the
2984 -- function. In that case the call can be replaced by that local
2985 -- variable as is done for other inlined calls.
2987 procedure Remove_Pragmas;
2988 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2989 -- parameter has no meaning when the body is inlined and the formals
2990 -- are rewritten. Remove it from body to inline. The analysis of the
2991 -- non-inlined body will handle the pragma properly.
2993 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2994 -- If the body of the subprogram includes a call that returns an
2995 -- unconstrained type, the secondary stack is involved, and it
2996 -- is not worth inlining.
2998 ------------------------------
2999 -- Has_Excluded_Declaration --
3000 ------------------------------
3002 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3005 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3006 -- Nested subprograms make a given body ineligible for inlining, but
3007 -- we make an exception for instantiations of unchecked conversion.
3008 -- The body has not been analyzed yet, so check the name, and verify
3009 -- that the visible entity with that name is the predefined unit.
3011 -----------------------------
3012 -- Is_Unchecked_Conversion --
3013 -----------------------------
3015 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3016 Id : constant Node_Id := Name (D);
3020 if Nkind (Id) = N_Identifier
3021 and then Chars (Id) = Name_Unchecked_Conversion
3023 Conv := Current_Entity (Id);
3025 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3026 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3028 Conv := Current_Entity (Selector_Name (Id));
3033 return Present (Conv)
3034 and then Is_Predefined_File_Name
3035 (Unit_File_Name (Get_Source_Unit (Conv)))
3036 and then Is_Intrinsic_Subprogram (Conv);
3037 end Is_Unchecked_Conversion;
3039 -- Start of processing for Has_Excluded_Declaration
3043 while Present (D) loop
3044 if (Nkind (D) = N_Function_Instantiation
3045 and then not Is_Unchecked_Conversion (D))
3046 or else Nkind_In (D, N_Protected_Type_Declaration,
3047 N_Package_Declaration,
3048 N_Package_Instantiation,
3050 N_Procedure_Instantiation,
3051 N_Task_Type_Declaration)
3054 ("cannot inline & (non-allowed declaration)?", D, Subp);
3062 end Has_Excluded_Declaration;
3064 ----------------------------
3065 -- Has_Excluded_Statement --
3066 ----------------------------
3068 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3074 while Present (S) loop
3075 Stat_Count := Stat_Count + 1;
3077 if Nkind_In (S, N_Abort_Statement,
3078 N_Asynchronous_Select,
3079 N_Conditional_Entry_Call,
3080 N_Delay_Relative_Statement,
3081 N_Delay_Until_Statement,
3086 ("cannot inline & (non-allowed statement)?", S, Subp);
3089 elsif Nkind (S) = N_Block_Statement then
3090 if Present (Declarations (S))
3091 and then Has_Excluded_Declaration (Declarations (S))
3095 elsif Present (Handled_Statement_Sequence (S))
3098 (Exception_Handlers (Handled_Statement_Sequence (S)))
3100 Has_Excluded_Statement
3101 (Statements (Handled_Statement_Sequence (S))))
3106 elsif Nkind (S) = N_Case_Statement then
3107 E := First (Alternatives (S));
3108 while Present (E) loop
3109 if Has_Excluded_Statement (Statements (E)) then
3116 elsif Nkind (S) = N_If_Statement then
3117 if Has_Excluded_Statement (Then_Statements (S)) then
3121 if Present (Elsif_Parts (S)) then
3122 E := First (Elsif_Parts (S));
3123 while Present (E) loop
3124 if Has_Excluded_Statement (Then_Statements (E)) then
3131 if Present (Else_Statements (S))
3132 and then Has_Excluded_Statement (Else_Statements (S))
3137 elsif Nkind (S) = N_Loop_Statement
3138 and then Has_Excluded_Statement (Statements (S))
3142 elsif Nkind (S) = N_Extended_Return_Statement then
3143 if Has_Excluded_Statement
3144 (Statements (Handled_Statement_Sequence (S)))
3146 (Exception_Handlers (Handled_Statement_Sequence (S)))
3156 end Has_Excluded_Statement;
3158 -------------------------------
3159 -- Has_Pending_Instantiation --
3160 -------------------------------
3162 function Has_Pending_Instantiation return Boolean is
3167 while Present (S) loop
3168 if Is_Compilation_Unit (S)
3169 or else Is_Child_Unit (S)
3173 elsif Ekind (S) = E_Package
3174 and then Has_Forward_Instantiation (S)
3183 end Has_Pending_Instantiation;
3185 ------------------------
3186 -- Has_Single_Return --
3187 ------------------------
3189 function Has_Single_Return return Boolean is
3190 Return_Statement : Node_Id := Empty;
3192 function Check_Return (N : Node_Id) return Traverse_Result;
3198 function Check_Return (N : Node_Id) return Traverse_Result is
3200 if Nkind (N) = N_Simple_Return_Statement then
3201 if Present (Expression (N))
3202 and then Is_Entity_Name (Expression (N))
3204 if No (Return_Statement) then
3205 Return_Statement := N;
3208 elsif Chars (Expression (N)) =
3209 Chars (Expression (Return_Statement))
3217 -- A return statement within an extended return is a noop
3220 elsif No (Expression (N))
3221 and then Nkind (Parent (Parent (N))) =
3222 N_Extended_Return_Statement
3227 -- Expression has wrong form
3232 -- We can only inline a build-in-place function if
3233 -- it has a single extended return.
3235 elsif Nkind (N) = N_Extended_Return_Statement then
3236 if No (Return_Statement) then
3237 Return_Statement := N;
3249 function Check_All_Returns is new Traverse_Func (Check_Return);
3251 -- Start of processing for Has_Single_Return
3254 if Check_All_Returns (N) /= OK then
3257 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3261 return Present (Declarations (N))
3262 and then Present (First (Declarations (N)))
3263 and then Chars (Expression (Return_Statement)) =
3264 Chars (Defining_Identifier (First (Declarations (N))));
3266 end Has_Single_Return;
3268 --------------------
3269 -- Remove_Pragmas --
3270 --------------------
3272 procedure Remove_Pragmas is
3277 Decl := First (Declarations (Body_To_Analyze));
3278 while Present (Decl) loop
3281 if Nkind (Decl) = N_Pragma
3282 and then (Pragma_Name (Decl) = Name_Unreferenced
3284 Pragma_Name (Decl) = Name_Unmodified)
3293 --------------------------
3294 -- Uses_Secondary_Stack --
3295 --------------------------
3297 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3298 function Check_Call (N : Node_Id) return Traverse_Result;
3299 -- Look for function calls that return an unconstrained type
3305 function Check_Call (N : Node_Id) return Traverse_Result is
3307 if Nkind (N) = N_Function_Call
3308 and then Is_Entity_Name (Name (N))
3309 and then Is_Composite_Type (Etype (Entity (Name (N))))
3310 and then not Is_Constrained (Etype (Entity (Name (N))))
3313 ("cannot inline & (call returns unconstrained type)?",
3321 function Check_Calls is new Traverse_Func (Check_Call);
3324 return Check_Calls (Bod) = Abandon;
3325 end Uses_Secondary_Stack;
3327 -- Start of processing for Build_Body_To_Inline
3330 -- Return immediately if done already
3332 if Nkind (Decl) = N_Subprogram_Declaration
3333 and then Present (Body_To_Inline (Decl))
3337 -- Functions that return unconstrained composite types require
3338 -- secondary stack handling, and cannot currently be inlined, unless
3339 -- all return statements return a local variable that is the first
3340 -- local declaration in the body.
3342 elsif Ekind (Subp) = E_Function
3343 and then not Is_Scalar_Type (Etype (Subp))
3344 and then not Is_Access_Type (Etype (Subp))
3345 and then not Is_Constrained (Etype (Subp))
3347 if not Has_Single_Return then
3349 ("cannot inline & (unconstrained return type)?", N, Subp);
3353 -- Ditto for functions that return controlled types, where controlled
3354 -- actions interfere in complex ways with inlining.
3356 elsif Ekind (Subp) = E_Function
3357 and then Needs_Finalization (Etype (Subp))
3360 ("cannot inline & (controlled return type)?", N, Subp);
3364 if Present (Declarations (N))
3365 and then Has_Excluded_Declaration (Declarations (N))
3370 if Present (Handled_Statement_Sequence (N)) then
3371 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3373 ("cannot inline& (exception handler)?",
3374 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3378 Has_Excluded_Statement
3379 (Statements (Handled_Statement_Sequence (N)))
3385 -- We do not inline a subprogram that is too large, unless it is
3386 -- marked Inline_Always. This pragma does not suppress the other
3387 -- checks on inlining (forbidden declarations, handlers, etc).
3389 if Stat_Count > Max_Size
3390 and then not Has_Pragma_Inline_Always (Subp)
3392 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3396 if Has_Pending_Instantiation then
3398 ("cannot inline& (forward instance within enclosing body)?",
3403 -- Within an instance, the body to inline must be treated as a nested
3404 -- generic, so that the proper global references are preserved.
3406 -- Note that we do not do this at the library level, because it is not
3407 -- needed, and furthermore this causes trouble if front end inlining
3408 -- is activated (-gnatN).
3410 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3411 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3412 Original_Body := Copy_Generic_Node (N, Empty, True);
3414 Original_Body := Copy_Separate_Tree (N);
3417 -- We need to capture references to the formals in order to substitute
3418 -- the actuals at the point of inlining, i.e. instantiation. To treat
3419 -- the formals as globals to the body to inline, we nest it within
3420 -- a dummy parameterless subprogram, declared within the real one.
3421 -- To avoid generating an internal name (which is never public, and
3422 -- which affects serial numbers of other generated names), we use
3423 -- an internal symbol that cannot conflict with user declarations.
3425 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3426 Set_Defining_Unit_Name
3427 (Specification (Original_Body),
3428 Make_Defining_Identifier (Sloc (N), Name_uParent));
3429 Set_Corresponding_Spec (Original_Body, Empty);
3431 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3433 -- Set return type of function, which is also global and does not need
3436 if Ekind (Subp) = E_Function then
3437 Set_Result_Definition (Specification (Body_To_Analyze),
3438 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3441 if No (Declarations (N)) then
3442 Set_Declarations (N, New_List (Body_To_Analyze));
3444 Append (Body_To_Analyze, Declarations (N));
3447 Expander_Mode_Save_And_Set (False);
3450 Analyze (Body_To_Analyze);
3451 Push_Scope (Defining_Entity (Body_To_Analyze));
3452 Save_Global_References (Original_Body);
3454 Remove (Body_To_Analyze);
3456 Expander_Mode_Restore;
3458 -- Restore environment if previously saved
3460 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3464 -- If secondary stk used there is no point in inlining. We have
3465 -- already issued the warning in this case, so nothing to do.
3467 if Uses_Secondary_Stack (Body_To_Analyze) then
3471 Set_Body_To_Inline (Decl, Original_Body);
3472 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3473 Set_Is_Inlined (Subp);
3474 end Build_Body_To_Inline;
3480 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3482 -- Do not emit warning if this is a predefined unit which is not the
3483 -- main unit. With validity checks enabled, some predefined subprograms
3484 -- may contain nested subprograms and become ineligible for inlining.
3486 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3487 and then not In_Extended_Main_Source_Unit (Subp)
3491 elsif Has_Pragma_Inline_Always (Subp) then
3493 -- Remove last character (question mark) to make this into an error,
3494 -- because the Inline_Always pragma cannot be obeyed.
3496 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3498 elsif Ineffective_Inline_Warnings then
3499 Error_Msg_NE (Msg, N, Subp);
3503 -----------------------
3504 -- Check_Conformance --
3505 -----------------------
3507 procedure Check_Conformance
3508 (New_Id : Entity_Id;
3510 Ctype : Conformance_Type;
3512 Conforms : out Boolean;
3513 Err_Loc : Node_Id := Empty;
3514 Get_Inst : Boolean := False;
3515 Skip_Controlling_Formals : Boolean := False)
3517 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3518 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3519 -- If Errmsg is True, then processing continues to post an error message
3520 -- for conformance error on given node. Two messages are output. The
3521 -- first message points to the previous declaration with a general "no
3522 -- conformance" message. The second is the detailed reason, supplied as
3523 -- Msg. The parameter N provide information for a possible & insertion
3524 -- in the message, and also provides the location for posting the
3525 -- message in the absence of a specified Err_Loc location.
3527 -----------------------
3528 -- Conformance_Error --
3529 -----------------------
3531 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3538 if No (Err_Loc) then
3544 Error_Msg_Sloc := Sloc (Old_Id);
3547 when Type_Conformant =>
3548 Error_Msg_N -- CODEFIX
3549 ("not type conformant with declaration#!", Enode);
3551 when Mode_Conformant =>
3552 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3554 ("not mode conformant with operation inherited#!",
3558 ("not mode conformant with declaration#!", Enode);
3561 when Subtype_Conformant =>
3562 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3564 ("not subtype conformant with operation inherited#!",
3568 ("not subtype conformant with declaration#!", Enode);
3571 when Fully_Conformant =>
3572 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3573 Error_Msg_N -- CODEFIX
3574 ("not fully conformant with operation inherited#!",
3577 Error_Msg_N -- CODEFIX
3578 ("not fully conformant with declaration#!", Enode);
3582 Error_Msg_NE (Msg, Enode, N);
3584 end Conformance_Error;
3588 Old_Type : constant Entity_Id := Etype (Old_Id);
3589 New_Type : constant Entity_Id := Etype (New_Id);
3590 Old_Formal : Entity_Id;
3591 New_Formal : Entity_Id;
3592 Access_Types_Match : Boolean;
3593 Old_Formal_Base : Entity_Id;
3594 New_Formal_Base : Entity_Id;
3596 -- Start of processing for Check_Conformance
3601 -- We need a special case for operators, since they don't appear
3604 if Ctype = Type_Conformant then
3605 if Ekind (New_Id) = E_Operator
3606 and then Operator_Matches_Spec (New_Id, Old_Id)
3612 -- If both are functions/operators, check return types conform
3614 if Old_Type /= Standard_Void_Type
3615 and then New_Type /= Standard_Void_Type
3618 -- If we are checking interface conformance we omit controlling
3619 -- arguments and result, because we are only checking the conformance
3620 -- of the remaining parameters.
3622 if Has_Controlling_Result (Old_Id)
3623 and then Has_Controlling_Result (New_Id)
3624 and then Skip_Controlling_Formals
3628 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3629 Conformance_Error ("\return type does not match!", New_Id);
3633 -- Ada 2005 (AI-231): In case of anonymous access types check the
3634 -- null-exclusion and access-to-constant attributes match.
3636 if Ada_Version >= Ada_05
3637 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3639 (Can_Never_Be_Null (Old_Type)
3640 /= Can_Never_Be_Null (New_Type)
3641 or else Is_Access_Constant (Etype (Old_Type))
3642 /= Is_Access_Constant (Etype (New_Type)))
3644 Conformance_Error ("\return type does not match!", New_Id);
3648 -- If either is a function/operator and the other isn't, error
3650 elsif Old_Type /= Standard_Void_Type
3651 or else New_Type /= Standard_Void_Type
3653 Conformance_Error ("\functions can only match functions!", New_Id);
3657 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3658 -- If this is a renaming as body, refine error message to indicate that
3659 -- the conflict is with the original declaration. If the entity is not
3660 -- frozen, the conventions don't have to match, the one of the renamed
3661 -- entity is inherited.
3663 if Ctype >= Subtype_Conformant then
3664 if Convention (Old_Id) /= Convention (New_Id) then
3666 if not Is_Frozen (New_Id) then
3669 elsif Present (Err_Loc)
3670 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3671 and then Present (Corresponding_Spec (Err_Loc))
3673 Error_Msg_Name_1 := Chars (New_Id);
3675 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3677 Conformance_Error ("\prior declaration for% has convention %!");
3680 Conformance_Error ("\calling conventions do not match!");
3685 elsif Is_Formal_Subprogram (Old_Id)
3686 or else Is_Formal_Subprogram (New_Id)
3688 Conformance_Error ("\formal subprograms not allowed!");
3693 -- Deal with parameters
3695 -- Note: we use the entity information, rather than going directly
3696 -- to the specification in the tree. This is not only simpler, but
3697 -- absolutely necessary for some cases of conformance tests between
3698 -- operators, where the declaration tree simply does not exist!
3700 Old_Formal := First_Formal (Old_Id);
3701 New_Formal := First_Formal (New_Id);
3702 while Present (Old_Formal) and then Present (New_Formal) loop
3703 if Is_Controlling_Formal (Old_Formal)
3704 and then Is_Controlling_Formal (New_Formal)
3705 and then Skip_Controlling_Formals
3707 -- The controlling formals will have different types when
3708 -- comparing an interface operation with its match, but both
3709 -- or neither must be access parameters.
3711 if Is_Access_Type (Etype (Old_Formal))
3713 Is_Access_Type (Etype (New_Formal))
3715 goto Skip_Controlling_Formal;
3718 ("\access parameter does not match!", New_Formal);
3722 if Ctype = Fully_Conformant then
3724 -- Names must match. Error message is more accurate if we do
3725 -- this before checking that the types of the formals match.
3727 if Chars (Old_Formal) /= Chars (New_Formal) then
3728 Conformance_Error ("\name & does not match!", New_Formal);
3730 -- Set error posted flag on new formal as well to stop
3731 -- junk cascaded messages in some cases.
3733 Set_Error_Posted (New_Formal);
3738 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3739 -- case occurs whenever a subprogram is being renamed and one of its
3740 -- parameters imposes a null exclusion. For example:
3742 -- type T is null record;
3743 -- type Acc_T is access T;
3744 -- subtype Acc_T_Sub is Acc_T;
3746 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3747 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3750 Old_Formal_Base := Etype (Old_Formal);
3751 New_Formal_Base := Etype (New_Formal);
3754 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3755 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3758 Access_Types_Match := Ada_Version >= Ada_05
3760 -- Ensure that this rule is only applied when New_Id is a
3761 -- renaming of Old_Id.
3763 and then Nkind (Parent (Parent (New_Id))) =
3764 N_Subprogram_Renaming_Declaration
3765 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3766 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3767 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3769 -- Now handle the allowed access-type case
3771 and then Is_Access_Type (Old_Formal_Base)
3772 and then Is_Access_Type (New_Formal_Base)
3774 -- The type kinds must match. The only exception occurs with
3775 -- multiple generics of the form:
3778 -- type F is private; type A is private;
3779 -- type F_Ptr is access F; type A_Ptr is access A;
3780 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3781 -- package F_Pack is ... package A_Pack is
3782 -- package F_Inst is
3783 -- new F_Pack (A, A_Ptr, A_P);
3785 -- When checking for conformance between the parameters of A_P
3786 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3787 -- because the compiler has transformed A_Ptr into a subtype of
3788 -- F_Ptr. We catch this case in the code below.
3790 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3792 (Is_Generic_Type (Old_Formal_Base)
3793 and then Is_Generic_Type (New_Formal_Base)
3794 and then Is_Internal (New_Formal_Base)
3795 and then Etype (Etype (New_Formal_Base)) =
3797 and then Directly_Designated_Type (Old_Formal_Base) =
3798 Directly_Designated_Type (New_Formal_Base)
3799 and then ((Is_Itype (Old_Formal_Base)
3800 and then Can_Never_Be_Null (Old_Formal_Base))
3802 (Is_Itype (New_Formal_Base)
3803 and then Can_Never_Be_Null (New_Formal_Base)));
3805 -- Types must always match. In the visible part of an instance,
3806 -- usual overloading rules for dispatching operations apply, and
3807 -- we check base types (not the actual subtypes).
3809 if In_Instance_Visible_Part
3810 and then Is_Dispatching_Operation (New_Id)
3812 if not Conforming_Types
3813 (T1 => Base_Type (Etype (Old_Formal)),
3814 T2 => Base_Type (Etype (New_Formal)),
3816 Get_Inst => Get_Inst)
3817 and then not Access_Types_Match
3819 Conformance_Error ("\type of & does not match!", New_Formal);
3823 elsif not Conforming_Types
3824 (T1 => Old_Formal_Base,
3825 T2 => New_Formal_Base,
3827 Get_Inst => Get_Inst)
3828 and then not Access_Types_Match
3830 -- Don't give error message if old type is Any_Type. This test
3831 -- avoids some cascaded errors, e.g. in case of a bad spec.
3833 if Errmsg and then Old_Formal_Base = Any_Type then
3836 Conformance_Error ("\type of & does not match!", New_Formal);
3842 -- For mode conformance, mode must match
3844 if Ctype >= Mode_Conformant then
3845 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3846 Conformance_Error ("\mode of & does not match!", New_Formal);
3849 -- Part of mode conformance for access types is having the same
3850 -- constant modifier.
3852 elsif Access_Types_Match
3853 and then Is_Access_Constant (Old_Formal_Base) /=
3854 Is_Access_Constant (New_Formal_Base)
3857 ("\constant modifier does not match!", New_Formal);
3862 if Ctype >= Subtype_Conformant then
3864 -- Ada 2005 (AI-231): In case of anonymous access types check
3865 -- the null-exclusion and access-to-constant attributes must
3868 if Ada_Version >= Ada_05
3869 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3870 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3872 (Can_Never_Be_Null (Old_Formal) /=
3873 Can_Never_Be_Null (New_Formal)
3875 Is_Access_Constant (Etype (Old_Formal)) /=
3876 Is_Access_Constant (Etype (New_Formal)))
3878 -- It is allowed to omit the null-exclusion in case of stream
3879 -- attribute subprograms. We recognize stream subprograms
3880 -- through their TSS-generated suffix.
3883 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3885 if TSS_Name /= TSS_Stream_Read
3886 and then TSS_Name /= TSS_Stream_Write
3887 and then TSS_Name /= TSS_Stream_Input
3888 and then TSS_Name /= TSS_Stream_Output
3891 ("\type of & does not match!", New_Formal);
3898 -- Full conformance checks
3900 if Ctype = Fully_Conformant then
3902 -- We have checked already that names match
3904 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3906 -- Check default expressions for in parameters
3909 NewD : constant Boolean :=
3910 Present (Default_Value (New_Formal));
3911 OldD : constant Boolean :=
3912 Present (Default_Value (Old_Formal));
3914 if NewD or OldD then
3916 -- The old default value has been analyzed because the
3917 -- current full declaration will have frozen everything
3918 -- before. The new default value has not been analyzed,
3919 -- so analyze it now before we check for conformance.
3922 Push_Scope (New_Id);
3923 Preanalyze_Spec_Expression
3924 (Default_Value (New_Formal), Etype (New_Formal));
3928 if not (NewD and OldD)
3929 or else not Fully_Conformant_Expressions
3930 (Default_Value (Old_Formal),
3931 Default_Value (New_Formal))
3934 ("\default expression for & does not match!",
3943 -- A couple of special checks for Ada 83 mode. These checks are
3944 -- skipped if either entity is an operator in package Standard,
3945 -- or if either old or new instance is not from the source program.
3947 if Ada_Version = Ada_83
3948 and then Sloc (Old_Id) > Standard_Location
3949 and then Sloc (New_Id) > Standard_Location
3950 and then Comes_From_Source (Old_Id)
3951 and then Comes_From_Source (New_Id)
3954 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3955 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3958 -- Explicit IN must be present or absent in both cases. This
3959 -- test is required only in the full conformance case.
3961 if In_Present (Old_Param) /= In_Present (New_Param)
3962 and then Ctype = Fully_Conformant
3965 ("\(Ada 83) IN must appear in both declarations",
3970 -- Grouping (use of comma in param lists) must be the same
3971 -- This is where we catch a misconformance like:
3974 -- A : Integer; B : Integer
3976 -- which are represented identically in the tree except
3977 -- for the setting of the flags More_Ids and Prev_Ids.
3979 if More_Ids (Old_Param) /= More_Ids (New_Param)
3980 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3983 ("\grouping of & does not match!", New_Formal);
3989 -- This label is required when skipping controlling formals
3991 <<Skip_Controlling_Formal>>
3993 Next_Formal (Old_Formal);
3994 Next_Formal (New_Formal);
3997 if Present (Old_Formal) then
3998 Conformance_Error ("\too few parameters!");
4001 elsif Present (New_Formal) then
4002 Conformance_Error ("\too many parameters!", New_Formal);
4005 end Check_Conformance;
4007 -----------------------
4008 -- Check_Conventions --
4009 -----------------------
4011 procedure Check_Conventions (Typ : Entity_Id) is
4012 Ifaces_List : Elist_Id;
4014 procedure Check_Convention (Op : Entity_Id);
4015 -- Verify that the convention of inherited dispatching operation Op is
4016 -- consistent among all subprograms it overrides. In order to minimize
4017 -- the search, Search_From is utilized to designate a specific point in
4018 -- the list rather than iterating over the whole list once more.
4020 ----------------------
4021 -- Check_Convention --
4022 ----------------------
4024 procedure Check_Convention (Op : Entity_Id) is
4025 Iface_Elmt : Elmt_Id;
4026 Iface_Prim_Elmt : Elmt_Id;
4027 Iface_Prim : Entity_Id;
4030 Iface_Elmt := First_Elmt (Ifaces_List);
4031 while Present (Iface_Elmt) loop
4033 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4034 while Present (Iface_Prim_Elmt) loop
4035 Iface_Prim := Node (Iface_Prim_Elmt);
4037 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4038 and then Convention (Iface_Prim) /= Convention (Op)
4041 ("inconsistent conventions in primitive operations", Typ);
4043 Error_Msg_Name_1 := Chars (Op);
4044 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4045 Error_Msg_Sloc := Sloc (Op);
4047 if Comes_From_Source (Op) or else No (Alias (Op)) then
4048 if not Is_Overriding_Operation (Op) then
4049 Error_Msg_N ("\\primitive % defined #", Typ);
4052 ("\\overriding operation % with " &
4053 "convention % defined #", Typ);
4056 else pragma Assert (Present (Alias (Op)));
4057 Error_Msg_Sloc := Sloc (Alias (Op));
4059 ("\\inherited operation % with " &
4060 "convention % defined #", Typ);
4063 Error_Msg_Name_1 := Chars (Op);
4065 Get_Convention_Name (Convention (Iface_Prim));
4066 Error_Msg_Sloc := Sloc (Iface_Prim);
4068 ("\\overridden operation % with " &
4069 "convention % defined #", Typ);
4071 -- Avoid cascading errors
4076 Next_Elmt (Iface_Prim_Elmt);
4079 Next_Elmt (Iface_Elmt);
4081 end Check_Convention;
4085 Prim_Op : Entity_Id;
4086 Prim_Op_Elmt : Elmt_Id;
4088 -- Start of processing for Check_Conventions
4091 if not Has_Interfaces (Typ) then
4095 Collect_Interfaces (Typ, Ifaces_List);
4097 -- The algorithm checks every overriding dispatching operation against
4098 -- all the corresponding overridden dispatching operations, detecting
4099 -- differences in conventions.
4101 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4102 while Present (Prim_Op_Elmt) loop
4103 Prim_Op := Node (Prim_Op_Elmt);
4105 -- A small optimization: skip the predefined dispatching operations
4106 -- since they always have the same convention.
4108 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4109 Check_Convention (Prim_Op);
4112 Next_Elmt (Prim_Op_Elmt);
4114 end Check_Conventions;
4116 ------------------------------
4117 -- Check_Delayed_Subprogram --
4118 ------------------------------
4120 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4123 procedure Possible_Freeze (T : Entity_Id);
4124 -- T is the type of either a formal parameter or of the return type.
4125 -- If T is not yet frozen and needs a delayed freeze, then the
4126 -- subprogram itself must be delayed. If T is the limited view of an
4127 -- incomplete type the subprogram must be frozen as well, because
4128 -- T may depend on local types that have not been frozen yet.
4130 ---------------------
4131 -- Possible_Freeze --
4132 ---------------------
4134 procedure Possible_Freeze (T : Entity_Id) is
4136 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4137 Set_Has_Delayed_Freeze (Designator);
4139 elsif Is_Access_Type (T)
4140 and then Has_Delayed_Freeze (Designated_Type (T))
4141 and then not Is_Frozen (Designated_Type (T))
4143 Set_Has_Delayed_Freeze (Designator);
4145 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4146 Set_Has_Delayed_Freeze (Designator);
4149 end Possible_Freeze;
4151 -- Start of processing for Check_Delayed_Subprogram
4154 -- Never need to freeze abstract subprogram
4156 if Ekind (Designator) /= E_Subprogram_Type
4157 and then Is_Abstract_Subprogram (Designator)
4161 -- Need delayed freeze if return type itself needs a delayed
4162 -- freeze and is not yet frozen.
4164 Possible_Freeze (Etype (Designator));
4165 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4167 -- Need delayed freeze if any of the formal types themselves need
4168 -- a delayed freeze and are not yet frozen.
4170 F := First_Formal (Designator);
4171 while Present (F) loop
4172 Possible_Freeze (Etype (F));
4173 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4178 -- Mark functions that return by reference. Note that it cannot be
4179 -- done for delayed_freeze subprograms because the underlying
4180 -- returned type may not be known yet (for private types)
4182 if not Has_Delayed_Freeze (Designator)
4183 and then Expander_Active
4186 Typ : constant Entity_Id := Etype (Designator);
4187 Utyp : constant Entity_Id := Underlying_Type (Typ);
4190 if Is_Inherently_Limited_Type (Typ) then
4191 Set_Returns_By_Ref (Designator);
4193 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4194 Set_Returns_By_Ref (Designator);
4198 end Check_Delayed_Subprogram;
4200 ------------------------------------
4201 -- Check_Discriminant_Conformance --
4202 ------------------------------------
4204 procedure Check_Discriminant_Conformance
4209 Old_Discr : Entity_Id := First_Discriminant (Prev);
4210 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4211 New_Discr_Id : Entity_Id;
4212 New_Discr_Type : Entity_Id;
4214 procedure Conformance_Error (Msg : String; N : Node_Id);
4215 -- Post error message for conformance error on given node. Two messages
4216 -- are output. The first points to the previous declaration with a
4217 -- general "no conformance" message. The second is the detailed reason,
4218 -- supplied as Msg. The parameter N provide information for a possible
4219 -- & insertion in the message.
4221 -----------------------
4222 -- Conformance_Error --
4223 -----------------------
4225 procedure Conformance_Error (Msg : String; N : Node_Id) is
4227 Error_Msg_Sloc := Sloc (Prev_Loc);
4228 Error_Msg_N -- CODEFIX
4229 ("not fully conformant with declaration#!", N);
4230 Error_Msg_NE (Msg, N, N);
4231 end Conformance_Error;
4233 -- Start of processing for Check_Discriminant_Conformance
4236 while Present (Old_Discr) and then Present (New_Discr) loop
4238 New_Discr_Id := Defining_Identifier (New_Discr);
4240 -- The subtype mark of the discriminant on the full type has not
4241 -- been analyzed so we do it here. For an access discriminant a new
4244 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4246 Access_Definition (N, Discriminant_Type (New_Discr));
4249 Analyze (Discriminant_Type (New_Discr));
4250 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4252 -- Ada 2005: if the discriminant definition carries a null
4253 -- exclusion, create an itype to check properly for consistency
4254 -- with partial declaration.
4256 if Is_Access_Type (New_Discr_Type)
4257 and then Null_Exclusion_Present (New_Discr)
4260 Create_Null_Excluding_Itype
4261 (T => New_Discr_Type,
4262 Related_Nod => New_Discr,
4263 Scope_Id => Current_Scope);
4267 if not Conforming_Types
4268 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4270 Conformance_Error ("type of & does not match!", New_Discr_Id);
4273 -- Treat the new discriminant as an occurrence of the old one,
4274 -- for navigation purposes, and fill in some semantic
4275 -- information, for completeness.
4277 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4278 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4279 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4284 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4285 Conformance_Error ("name & does not match!", New_Discr_Id);
4289 -- Default expressions must match
4292 NewD : constant Boolean :=
4293 Present (Expression (New_Discr));
4294 OldD : constant Boolean :=
4295 Present (Expression (Parent (Old_Discr)));
4298 if NewD or OldD then
4300 -- The old default value has been analyzed and expanded,
4301 -- because the current full declaration will have frozen
4302 -- everything before. The new default values have not been
4303 -- expanded, so expand now to check conformance.
4306 Preanalyze_Spec_Expression
4307 (Expression (New_Discr), New_Discr_Type);
4310 if not (NewD and OldD)
4311 or else not Fully_Conformant_Expressions
4312 (Expression (Parent (Old_Discr)),
4313 Expression (New_Discr))
4317 ("default expression for & does not match!",
4324 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4326 if Ada_Version = Ada_83 then
4328 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4331 -- Grouping (use of comma in param lists) must be the same
4332 -- This is where we catch a misconformance like:
4335 -- A : Integer; B : Integer
4337 -- which are represented identically in the tree except
4338 -- for the setting of the flags More_Ids and Prev_Ids.
4340 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4341 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4344 ("grouping of & does not match!", New_Discr_Id);
4350 Next_Discriminant (Old_Discr);
4354 if Present (Old_Discr) then
4355 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4358 elsif Present (New_Discr) then
4360 ("too many discriminants!", Defining_Identifier (New_Discr));
4363 end Check_Discriminant_Conformance;
4365 ----------------------------
4366 -- Check_Fully_Conformant --
4367 ----------------------------
4369 procedure Check_Fully_Conformant
4370 (New_Id : Entity_Id;
4372 Err_Loc : Node_Id := Empty)
4375 pragma Warnings (Off, Result);
4378 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4379 end Check_Fully_Conformant;
4381 ---------------------------
4382 -- Check_Mode_Conformant --
4383 ---------------------------
4385 procedure Check_Mode_Conformant
4386 (New_Id : Entity_Id;
4388 Err_Loc : Node_Id := Empty;
4389 Get_Inst : Boolean := False)
4392 pragma Warnings (Off, Result);
4395 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4396 end Check_Mode_Conformant;
4398 --------------------------------
4399 -- Check_Overriding_Indicator --
4400 --------------------------------
4402 procedure Check_Overriding_Indicator
4404 Overridden_Subp : Entity_Id;
4405 Is_Primitive : Boolean)
4411 -- No overriding indicator for literals
4413 if Ekind (Subp) = E_Enumeration_Literal then
4416 elsif Ekind (Subp) = E_Entry then
4417 Decl := Parent (Subp);
4419 -- No point in analyzing a malformed operator
4421 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4422 and then Error_Posted (Subp)
4427 Decl := Unit_Declaration_Node (Subp);
4430 if Nkind_In (Decl, N_Subprogram_Body,
4431 N_Subprogram_Body_Stub,
4432 N_Subprogram_Declaration,
4433 N_Abstract_Subprogram_Declaration,
4434 N_Subprogram_Renaming_Declaration)
4436 Spec := Specification (Decl);
4438 elsif Nkind (Decl) = N_Entry_Declaration then
4445 -- The overriding operation is type conformant with the overridden one,
4446 -- but the names of the formals are not required to match. If the names
4447 -- appear permuted in the overriding operation, this is a possible
4448 -- source of confusion that is worth diagnosing. Controlling formals
4449 -- often carry names that reflect the type, and it is not worthwhile
4450 -- requiring that their names match.
4452 if Present (Overridden_Subp)
4453 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4460 Form1 := First_Formal (Subp);
4461 Form2 := First_Formal (Overridden_Subp);
4463 -- If the overriding operation is a synchronized operation, skip
4464 -- the first parameter of the overridden operation, which is
4465 -- implicit in the new one. If the operation is declared in the
4466 -- body it is not primitive and all formals must match.
4468 if Is_Concurrent_Type (Scope (Subp))
4469 and then Is_Tagged_Type (Scope (Subp))
4470 and then not Has_Completion (Scope (Subp))
4472 Form2 := Next_Formal (Form2);
4475 if Present (Form1) then
4476 Form1 := Next_Formal (Form1);
4477 Form2 := Next_Formal (Form2);
4480 while Present (Form1) loop
4481 if not Is_Controlling_Formal (Form1)
4482 and then Present (Next_Formal (Form2))
4483 and then Chars (Form1) = Chars (Next_Formal (Form2))
4485 Error_Msg_Node_2 := Alias (Overridden_Subp);
4486 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4488 ("& does not match corresponding formal of&#",
4493 Next_Formal (Form1);
4494 Next_Formal (Form2);
4499 -- If there is an overridden subprogram, then check that there is no
4500 -- "not overriding" indicator, and mark the subprogram as overriding.
4501 -- This is not done if the overridden subprogram is marked as hidden,
4502 -- which can occur for the case of inherited controlled operations
4503 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4504 -- subprogram is not itself hidden. (Note: This condition could probably
4505 -- be simplified, leaving out the testing for the specific controlled
4506 -- cases, but it seems safer and clearer this way, and echoes similar
4507 -- special-case tests of this kind in other places.)
4509 if Present (Overridden_Subp)
4510 and then (not Is_Hidden (Overridden_Subp)
4512 ((Chars (Overridden_Subp) = Name_Initialize
4513 or else Chars (Overridden_Subp) = Name_Adjust
4514 or else Chars (Overridden_Subp) = Name_Finalize)
4515 and then Present (Alias (Overridden_Subp))
4516 and then not Is_Hidden (Alias (Overridden_Subp))))
4518 if Must_Not_Override (Spec) then
4519 Error_Msg_Sloc := Sloc (Overridden_Subp);
4521 if Ekind (Subp) = E_Entry then
4523 ("entry & overrides inherited operation #", Spec, Subp);
4526 ("subprogram & overrides inherited operation #", Spec, Subp);
4529 elsif Is_Subprogram (Subp) then
4530 Set_Is_Overriding_Operation (Subp);
4533 -- If primitive flag is set or this is a protected operation, then
4534 -- the operation is overriding at the point of its declaration, so
4535 -- warn if necessary. Otherwise it may have been declared before the
4536 -- operation it overrides and no check is required.
4539 and then not Must_Override (Spec)
4540 and then (Is_Primitive
4541 or else Ekind (Scope (Subp)) = E_Protected_Type)
4543 Style.Missing_Overriding (Decl, Subp);
4546 -- If Subp is an operator, it may override a predefined operation, if
4547 -- it is defined in the same scope as the type to which it applies.
4548 -- In that case Overridden_Subp is empty because of our implicit
4549 -- representation for predefined operators. We have to check whether the
4550 -- signature of Subp matches that of a predefined operator. Note that
4551 -- first argument provides the name of the operator, and the second
4552 -- argument the signature that may match that of a standard operation.
4553 -- If the indicator is overriding, then the operator must match a
4554 -- predefined signature, because we know already that there is no
4555 -- explicit overridden operation.
4557 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4559 Typ : constant Entity_Id :=
4560 Base_Type (Etype (First_Formal (Subp)));
4562 Can_Override : constant Boolean :=
4563 Operator_Matches_Spec (Subp, Subp)
4564 and then Scope (Subp) = Scope (Typ)
4565 and then not Is_Class_Wide_Type (Typ);
4568 if Must_Not_Override (Spec) then
4570 -- If this is not a primitive or a protected subprogram, then
4571 -- "not overriding" is illegal.
4574 and then Ekind (Scope (Subp)) /= E_Protected_Type
4577 ("overriding indicator only allowed "
4578 & "if subprogram is primitive", Subp);
4580 elsif Can_Override then
4582 ("subprogram& overrides predefined operator ", Spec, Subp);
4585 elsif Must_Override (Spec) then
4586 if Is_Overriding_Operation (Subp) then
4589 elsif not Can_Override then
4590 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4593 elsif not Error_Posted (Subp)
4594 and then Style_Check
4595 and then Can_Override
4597 not Is_Predefined_File_Name
4598 (Unit_File_Name (Get_Source_Unit (Subp)))
4600 Set_Is_Overriding_Operation (Subp);
4602 -- If style checks are enabled, indicate that the indicator is
4603 -- missing. However, at the point of declaration, the type of
4604 -- which this is a primitive operation may be private, in which
4605 -- case the indicator would be premature.
4607 if Has_Private_Declaration (Etype (Subp))
4608 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4612 Style.Missing_Overriding (Decl, Subp);
4617 elsif Must_Override (Spec) then
4618 if Ekind (Subp) = E_Entry then
4619 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4621 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4624 -- If the operation is marked "not overriding" and it's not primitive
4625 -- then an error is issued, unless this is an operation of a task or
4626 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4627 -- has been specified have already been checked above.
4629 elsif Must_Not_Override (Spec)
4630 and then not Is_Primitive
4631 and then Ekind (Subp) /= E_Entry
4632 and then Ekind (Scope (Subp)) /= E_Protected_Type
4635 ("overriding indicator only allowed if subprogram is primitive",
4639 end Check_Overriding_Indicator;
4645 -- Note: this procedure needs to know far too much about how the expander
4646 -- messes with exceptions. The use of the flag Exception_Junk and the
4647 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4648 -- works, but is not very clean. It would be better if the expansion
4649 -- routines would leave Original_Node working nicely, and we could use
4650 -- Original_Node here to ignore all the peculiar expander messing ???
4652 procedure Check_Returns
4656 Proc : Entity_Id := Empty)
4660 procedure Check_Statement_Sequence (L : List_Id);
4661 -- Internal recursive procedure to check a list of statements for proper
4662 -- termination by a return statement (or a transfer of control or a
4663 -- compound statement that is itself internally properly terminated).
4665 ------------------------------
4666 -- Check_Statement_Sequence --
4667 ------------------------------
4669 procedure Check_Statement_Sequence (L : List_Id) is
4674 Raise_Exception_Call : Boolean;
4675 -- Set True if statement sequence terminated by Raise_Exception call
4676 -- or a Reraise_Occurrence call.
4679 Raise_Exception_Call := False;
4681 -- Get last real statement
4683 Last_Stm := Last (L);
4685 -- Deal with digging out exception handler statement sequences that
4686 -- have been transformed by the local raise to goto optimization.
4687 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4688 -- optimization has occurred, we are looking at something like:
4691 -- original stmts in block
4695 -- goto L1; | omitted if No_Exception_Propagation
4700 -- goto L3; -- skip handler when exception not raised
4702 -- <<L1>> -- target label for local exception
4716 -- and what we have to do is to dig out the estmts1 and estmts2
4717 -- sequences (which were the original sequences of statements in
4718 -- the exception handlers) and check them.
4720 if Nkind (Last_Stm) = N_Label
4721 and then Exception_Junk (Last_Stm)
4727 exit when Nkind (Stm) /= N_Block_Statement;
4728 exit when not Exception_Junk (Stm);
4731 exit when Nkind (Stm) /= N_Label;
4732 exit when not Exception_Junk (Stm);
4733 Check_Statement_Sequence
4734 (Statements (Handled_Statement_Sequence (Next (Stm))));
4739 exit when Nkind (Stm) /= N_Goto_Statement;
4740 exit when not Exception_Junk (Stm);
4744 -- Don't count pragmas
4746 while Nkind (Last_Stm) = N_Pragma
4748 -- Don't count call to SS_Release (can happen after Raise_Exception)
4751 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4753 Nkind (Name (Last_Stm)) = N_Identifier
4755 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4757 -- Don't count exception junk
4760 (Nkind_In (Last_Stm, N_Goto_Statement,
4762 N_Object_Declaration)
4763 and then Exception_Junk (Last_Stm))
4764 or else Nkind (Last_Stm) in N_Push_xxx_Label
4765 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4770 -- Here we have the "real" last statement
4772 Kind := Nkind (Last_Stm);
4774 -- Transfer of control, OK. Note that in the No_Return procedure
4775 -- case, we already diagnosed any explicit return statements, so
4776 -- we can treat them as OK in this context.
4778 if Is_Transfer (Last_Stm) then
4781 -- Check cases of explicit non-indirect procedure calls
4783 elsif Kind = N_Procedure_Call_Statement
4784 and then Is_Entity_Name (Name (Last_Stm))
4786 -- Check call to Raise_Exception procedure which is treated
4787 -- specially, as is a call to Reraise_Occurrence.
4789 -- We suppress the warning in these cases since it is likely that
4790 -- the programmer really does not expect to deal with the case
4791 -- of Null_Occurrence, and thus would find a warning about a
4792 -- missing return curious, and raising Program_Error does not
4793 -- seem such a bad behavior if this does occur.
4795 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4796 -- behavior will be to raise Constraint_Error (see AI-329).
4798 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4800 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4802 Raise_Exception_Call := True;
4804 -- For Raise_Exception call, test first argument, if it is
4805 -- an attribute reference for a 'Identity call, then we know
4806 -- that the call cannot possibly return.
4809 Arg : constant Node_Id :=
4810 Original_Node (First_Actual (Last_Stm));
4812 if Nkind (Arg) = N_Attribute_Reference
4813 and then Attribute_Name (Arg) = Name_Identity
4820 -- If statement, need to look inside if there is an else and check
4821 -- each constituent statement sequence for proper termination.
4823 elsif Kind = N_If_Statement
4824 and then Present (Else_Statements (Last_Stm))
4826 Check_Statement_Sequence (Then_Statements (Last_Stm));
4827 Check_Statement_Sequence (Else_Statements (Last_Stm));
4829 if Present (Elsif_Parts (Last_Stm)) then
4831 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4834 while Present (Elsif_Part) loop
4835 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4843 -- Case statement, check each case for proper termination
4845 elsif Kind = N_Case_Statement then
4849 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4850 while Present (Case_Alt) loop
4851 Check_Statement_Sequence (Statements (Case_Alt));
4852 Next_Non_Pragma (Case_Alt);
4858 -- Block statement, check its handled sequence of statements
4860 elsif Kind = N_Block_Statement then
4866 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4875 -- Loop statement. If there is an iteration scheme, we can definitely
4876 -- fall out of the loop. Similarly if there is an exit statement, we
4877 -- can fall out. In either case we need a following return.
4879 elsif Kind = N_Loop_Statement then
4880 if Present (Iteration_Scheme (Last_Stm))
4881 or else Has_Exit (Entity (Identifier (Last_Stm)))
4885 -- A loop with no exit statement or iteration scheme is either
4886 -- an infinite loop, or it has some other exit (raise/return).
4887 -- In either case, no warning is required.
4893 -- Timed entry call, check entry call and delay alternatives
4895 -- Note: in expanded code, the timed entry call has been converted
4896 -- to a set of expanded statements on which the check will work
4897 -- correctly in any case.
4899 elsif Kind = N_Timed_Entry_Call then
4901 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4902 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4905 -- If statement sequence of entry call alternative is missing,
4906 -- then we can definitely fall through, and we post the error
4907 -- message on the entry call alternative itself.
4909 if No (Statements (ECA)) then
4912 -- If statement sequence of delay alternative is missing, then
4913 -- we can definitely fall through, and we post the error
4914 -- message on the delay alternative itself.
4916 -- Note: if both ECA and DCA are missing the return, then we
4917 -- post only one message, should be enough to fix the bugs.
4918 -- If not we will get a message next time on the DCA when the
4921 elsif No (Statements (DCA)) then
4924 -- Else check both statement sequences
4927 Check_Statement_Sequence (Statements (ECA));
4928 Check_Statement_Sequence (Statements (DCA));
4933 -- Conditional entry call, check entry call and else part
4935 -- Note: in expanded code, the conditional entry call has been
4936 -- converted to a set of expanded statements on which the check
4937 -- will work correctly in any case.
4939 elsif Kind = N_Conditional_Entry_Call then
4941 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4944 -- If statement sequence of entry call alternative is missing,
4945 -- then we can definitely fall through, and we post the error
4946 -- message on the entry call alternative itself.
4948 if No (Statements (ECA)) then
4951 -- Else check statement sequence and else part
4954 Check_Statement_Sequence (Statements (ECA));
4955 Check_Statement_Sequence (Else_Statements (Last_Stm));
4961 -- If we fall through, issue appropriate message
4964 if not Raise_Exception_Call then
4966 ("?RETURN statement missing following this statement!",
4969 ("\?Program_Error may be raised at run time!",
4973 -- Note: we set Err even though we have not issued a warning
4974 -- because we still have a case of a missing return. This is
4975 -- an extremely marginal case, probably will never be noticed
4976 -- but we might as well get it right.
4980 -- Otherwise we have the case of a procedure marked No_Return
4983 if not Raise_Exception_Call then
4985 ("?implied return after this statement " &
4986 "will raise Program_Error",
4989 ("\?procedure & is marked as No_Return!",
4994 RE : constant Node_Id :=
4995 Make_Raise_Program_Error (Sloc (Last_Stm),
4996 Reason => PE_Implicit_Return);
4998 Insert_After (Last_Stm, RE);
5002 end Check_Statement_Sequence;
5004 -- Start of processing for Check_Returns
5008 Check_Statement_Sequence (Statements (HSS));
5010 if Present (Exception_Handlers (HSS)) then
5011 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5012 while Present (Handler) loop
5013 Check_Statement_Sequence (Statements (Handler));
5014 Next_Non_Pragma (Handler);
5019 ----------------------------
5020 -- Check_Subprogram_Order --
5021 ----------------------------
5023 procedure Check_Subprogram_Order (N : Node_Id) is
5025 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5026 -- This is used to check if S1 > S2 in the sense required by this
5027 -- test, for example nameab < namec, but name2 < name10.
5029 -----------------------------
5030 -- Subprogram_Name_Greater --
5031 -----------------------------
5033 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5038 -- Remove trailing numeric parts
5041 while S1 (L1) in '0' .. '9' loop
5046 while S2 (L2) in '0' .. '9' loop
5050 -- If non-numeric parts non-equal, that's decisive
5052 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5055 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5058 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5059 -- that a missing suffix is treated as numeric zero in this test.
5063 while L1 < S1'Last loop
5065 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5069 while L2 < S2'Last loop
5071 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5076 end Subprogram_Name_Greater;
5078 -- Start of processing for Check_Subprogram_Order
5081 -- Check body in alpha order if this is option
5084 and then Style_Check_Order_Subprograms
5085 and then Nkind (N) = N_Subprogram_Body
5086 and then Comes_From_Source (N)
5087 and then In_Extended_Main_Source_Unit (N)
5091 renames Scope_Stack.Table
5092 (Scope_Stack.Last).Last_Subprogram_Name;
5094 Body_Id : constant Entity_Id :=
5095 Defining_Entity (Specification (N));
5098 Get_Decoded_Name_String (Chars (Body_Id));
5101 if Subprogram_Name_Greater
5102 (LSN.all, Name_Buffer (1 .. Name_Len))
5104 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5110 LSN := new String'(Name_Buffer (1 .. Name_Len));
5113 end Check_Subprogram_Order;
5115 ------------------------------
5116 -- Check_Subtype_Conformant --
5117 ------------------------------
5119 procedure Check_Subtype_Conformant
5120 (New_Id : Entity_Id;
5122 Err_Loc : Node_Id := Empty;
5123 Skip_Controlling_Formals : Boolean := False)
5126 pragma Warnings (Off, Result);
5129 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5130 Skip_Controlling_Formals => Skip_Controlling_Formals);
5131 end Check_Subtype_Conformant;
5133 ---------------------------
5134 -- Check_Type_Conformant --
5135 ---------------------------
5137 procedure Check_Type_Conformant
5138 (New_Id : Entity_Id;
5140 Err_Loc : Node_Id := Empty)
5143 pragma Warnings (Off, Result);
5146 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5147 end Check_Type_Conformant;
5149 ----------------------
5150 -- Conforming_Types --
5151 ----------------------
5153 function Conforming_Types
5156 Ctype : Conformance_Type;
5157 Get_Inst : Boolean := False) return Boolean
5159 Type_1 : Entity_Id := T1;
5160 Type_2 : Entity_Id := T2;
5161 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5163 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5164 -- If neither T1 nor T2 are generic actual types, or if they are in
5165 -- different scopes (e.g. parent and child instances), then verify that
5166 -- the base types are equal. Otherwise T1 and T2 must be on the same
5167 -- subtype chain. The whole purpose of this procedure is to prevent
5168 -- spurious ambiguities in an instantiation that may arise if two
5169 -- distinct generic types are instantiated with the same actual.
5171 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5172 -- An access parameter can designate an incomplete type. If the
5173 -- incomplete type is the limited view of a type from a limited_
5174 -- with_clause, check whether the non-limited view is available. If
5175 -- it is a (non-limited) incomplete type, get the full view.
5177 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5178 -- Returns True if and only if either T1 denotes a limited view of T2
5179 -- or T2 denotes a limited view of T1. This can arise when the limited
5180 -- with view of a type is used in a subprogram declaration and the
5181 -- subprogram body is in the scope of a regular with clause for the
5182 -- same unit. In such a case, the two type entities can be considered
5183 -- identical for purposes of conformance checking.
5185 ----------------------
5186 -- Base_Types_Match --
5187 ----------------------
5189 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5194 elsif Base_Type (T1) = Base_Type (T2) then
5196 -- The following is too permissive. A more precise test should
5197 -- check that the generic actual is an ancestor subtype of the
5200 return not Is_Generic_Actual_Type (T1)
5201 or else not Is_Generic_Actual_Type (T2)
5202 or else Scope (T1) /= Scope (T2);
5207 end Base_Types_Match;
5209 --------------------------
5210 -- Find_Designated_Type --
5211 --------------------------
5213 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5217 Desig := Directly_Designated_Type (T);
5219 if Ekind (Desig) = E_Incomplete_Type then
5221 -- If regular incomplete type, get full view if available
5223 if Present (Full_View (Desig)) then
5224 Desig := Full_View (Desig);
5226 -- If limited view of a type, get non-limited view if available,
5227 -- and check again for a regular incomplete type.
5229 elsif Present (Non_Limited_View (Desig)) then
5230 Desig := Get_Full_View (Non_Limited_View (Desig));
5235 end Find_Designated_Type;
5237 -------------------------------
5238 -- Matches_Limited_With_View --
5239 -------------------------------
5241 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5243 -- In some cases a type imported through a limited_with clause, and
5244 -- its nonlimited view are both visible, for example in an anonymous
5245 -- access-to-class-wide type in a formal. Both entities designate the
5248 if From_With_Type (T1)
5249 and then T2 = Available_View (T1)
5253 elsif From_With_Type (T2)
5254 and then T1 = Available_View (T2)
5261 end Matches_Limited_With_View;
5263 -- Start of processing for Conforming_Types
5266 -- The context is an instance association for a formal
5267 -- access-to-subprogram type; the formal parameter types require
5268 -- mapping because they may denote other formal parameters of the
5272 Type_1 := Get_Instance_Of (T1);
5273 Type_2 := Get_Instance_Of (T2);
5276 -- If one of the types is a view of the other introduced by a limited
5277 -- with clause, treat these as conforming for all purposes.
5279 if Matches_Limited_With_View (T1, T2) then
5282 elsif Base_Types_Match (Type_1, Type_2) then
5283 return Ctype <= Mode_Conformant
5284 or else Subtypes_Statically_Match (Type_1, Type_2);
5286 elsif Is_Incomplete_Or_Private_Type (Type_1)
5287 and then Present (Full_View (Type_1))
5288 and then Base_Types_Match (Full_View (Type_1), Type_2)
5290 return Ctype <= Mode_Conformant
5291 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5293 elsif Ekind (Type_2) = E_Incomplete_Type
5294 and then Present (Full_View (Type_2))
5295 and then Base_Types_Match (Type_1, Full_View (Type_2))
5297 return Ctype <= Mode_Conformant
5298 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5300 elsif Is_Private_Type (Type_2)
5301 and then In_Instance
5302 and then Present (Full_View (Type_2))
5303 and then Base_Types_Match (Type_1, Full_View (Type_2))
5305 return Ctype <= Mode_Conformant
5306 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5309 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5310 -- treated recursively because they carry a signature.
5312 Are_Anonymous_Access_To_Subprogram_Types :=
5313 Ekind (Type_1) = Ekind (Type_2)
5315 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5317 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5319 -- Test anonymous access type case. For this case, static subtype
5320 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5321 -- the base types because we may have built internal subtype entities
5322 -- to handle null-excluding types (see Process_Formals).
5324 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5326 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5327 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5330 Desig_1 : Entity_Id;
5331 Desig_2 : Entity_Id;
5334 -- In Ada2005, access constant indicators must match for
5335 -- subtype conformance.
5337 if Ada_Version >= Ada_05
5338 and then Ctype >= Subtype_Conformant
5340 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5345 Desig_1 := Find_Designated_Type (Type_1);
5347 Desig_2 := Find_Designated_Type (Type_2);
5349 -- If the context is an instance association for a formal
5350 -- access-to-subprogram type; formal access parameter designated
5351 -- types require mapping because they may denote other formal
5352 -- parameters of the generic unit.
5355 Desig_1 := Get_Instance_Of (Desig_1);
5356 Desig_2 := Get_Instance_Of (Desig_2);
5359 -- It is possible for a Class_Wide_Type to be introduced for an
5360 -- incomplete type, in which case there is a separate class_ wide
5361 -- type for the full view. The types conform if their Etypes
5362 -- conform, i.e. one may be the full view of the other. This can
5363 -- only happen in the context of an access parameter, other uses
5364 -- of an incomplete Class_Wide_Type are illegal.
5366 if Is_Class_Wide_Type (Desig_1)
5367 and then Is_Class_Wide_Type (Desig_2)
5371 (Etype (Base_Type (Desig_1)),
5372 Etype (Base_Type (Desig_2)), Ctype);
5374 elsif Are_Anonymous_Access_To_Subprogram_Types then
5375 if Ada_Version < Ada_05 then
5376 return Ctype = Type_Conformant
5378 Subtypes_Statically_Match (Desig_1, Desig_2);
5380 -- We must check the conformance of the signatures themselves
5384 Conformant : Boolean;
5387 (Desig_1, Desig_2, Ctype, False, Conformant);
5393 return Base_Type (Desig_1) = Base_Type (Desig_2)
5394 and then (Ctype = Type_Conformant
5396 Subtypes_Statically_Match (Desig_1, Desig_2));
5400 -- Otherwise definitely no match
5403 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5404 and then Is_Access_Type (Type_2))
5405 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5406 and then Is_Access_Type (Type_1)))
5409 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5411 May_Hide_Profile := True;
5416 end Conforming_Types;
5418 --------------------------
5419 -- Create_Extra_Formals --
5420 --------------------------
5422 procedure Create_Extra_Formals (E : Entity_Id) is
5424 First_Extra : Entity_Id := Empty;
5425 Last_Extra : Entity_Id;
5426 Formal_Type : Entity_Id;
5427 P_Formal : Entity_Id := Empty;
5429 function Add_Extra_Formal
5430 (Assoc_Entity : Entity_Id;
5433 Suffix : String) return Entity_Id;
5434 -- Add an extra formal to the current list of formals and extra formals.
5435 -- The extra formal is added to the end of the list of extra formals,
5436 -- and also returned as the result. These formals are always of mode IN.
5437 -- The new formal has the type Typ, is declared in Scope, and its name
5438 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5439 -- The following suffixes are currently used. They should not be changed
5440 -- without coordinating with CodePeer, which makes use of these to
5441 -- provide better messages.
5443 -- O denotes the Constrained bit.
5444 -- L denotes the accessibility level.
5445 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5446 -- the full list in exp_ch6.BIP_Formal_Kind.
5448 ----------------------
5449 -- Add_Extra_Formal --
5450 ----------------------
5452 function Add_Extra_Formal
5453 (Assoc_Entity : Entity_Id;
5456 Suffix : String) return Entity_Id
5458 EF : constant Entity_Id :=
5459 Make_Defining_Identifier (Sloc (Assoc_Entity),
5460 Chars => New_External_Name (Chars (Assoc_Entity),
5464 -- A little optimization. Never generate an extra formal for the
5465 -- _init operand of an initialization procedure, since it could
5468 if Chars (Formal) = Name_uInit then
5472 Set_Ekind (EF, E_In_Parameter);
5473 Set_Actual_Subtype (EF, Typ);
5474 Set_Etype (EF, Typ);
5475 Set_Scope (EF, Scope);
5476 Set_Mechanism (EF, Default_Mechanism);
5477 Set_Formal_Validity (EF);
5479 if No (First_Extra) then
5481 Set_Extra_Formals (Scope, First_Extra);
5484 if Present (Last_Extra) then
5485 Set_Extra_Formal (Last_Extra, EF);
5491 end Add_Extra_Formal;
5493 -- Start of processing for Create_Extra_Formals
5496 -- We never generate extra formals if expansion is not active
5497 -- because we don't need them unless we are generating code.
5499 if not Expander_Active then
5503 -- If this is a derived subprogram then the subtypes of the parent
5504 -- subprogram's formal parameters will be used to determine the need
5505 -- for extra formals.
5507 if Is_Overloadable (E) and then Present (Alias (E)) then
5508 P_Formal := First_Formal (Alias (E));
5511 Last_Extra := Empty;
5512 Formal := First_Formal (E);
5513 while Present (Formal) loop
5514 Last_Extra := Formal;
5515 Next_Formal (Formal);
5518 -- If Extra_formals were already created, don't do it again. This
5519 -- situation may arise for subprogram types created as part of
5520 -- dispatching calls (see Expand_Dispatching_Call)
5522 if Present (Last_Extra) and then
5523 Present (Extra_Formal (Last_Extra))
5528 -- If the subprogram is a predefined dispatching subprogram then don't
5529 -- generate any extra constrained or accessibility level formals. In
5530 -- general we suppress these for internal subprograms (by not calling
5531 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5532 -- generated stream attributes do get passed through because extra
5533 -- build-in-place formals are needed in some cases (limited 'Input).
5535 if Is_Predefined_Internal_Operation (E) then
5536 goto Test_For_BIP_Extras;
5539 Formal := First_Formal (E);
5540 while Present (Formal) loop
5542 -- Create extra formal for supporting the attribute 'Constrained.
5543 -- The case of a private type view without discriminants also
5544 -- requires the extra formal if the underlying type has defaulted
5547 if Ekind (Formal) /= E_In_Parameter then
5548 if Present (P_Formal) then
5549 Formal_Type := Etype (P_Formal);
5551 Formal_Type := Etype (Formal);
5554 -- Do not produce extra formals for Unchecked_Union parameters.
5555 -- Jump directly to the end of the loop.
5557 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5558 goto Skip_Extra_Formal_Generation;
5561 if not Has_Discriminants (Formal_Type)
5562 and then Ekind (Formal_Type) in Private_Kind
5563 and then Present (Underlying_Type (Formal_Type))
5565 Formal_Type := Underlying_Type (Formal_Type);
5568 if Has_Discriminants (Formal_Type)
5569 and then not Is_Constrained (Formal_Type)
5570 and then not Is_Indefinite_Subtype (Formal_Type)
5572 Set_Extra_Constrained
5573 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5577 -- Create extra formal for supporting accessibility checking. This
5578 -- is done for both anonymous access formals and formals of named
5579 -- access types that are marked as controlling formals. The latter
5580 -- case can occur when Expand_Dispatching_Call creates a subprogram
5581 -- type and substitutes the types of access-to-class-wide actuals
5582 -- for the anonymous access-to-specific-type of controlling formals.
5583 -- Base_Type is applied because in cases where there is a null
5584 -- exclusion the formal may have an access subtype.
5586 -- This is suppressed if we specifically suppress accessibility
5587 -- checks at the package level for either the subprogram, or the
5588 -- package in which it resides. However, we do not suppress it
5589 -- simply if the scope has accessibility checks suppressed, since
5590 -- this could cause trouble when clients are compiled with a
5591 -- different suppression setting. The explicit checks at the
5592 -- package level are safe from this point of view.
5594 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5595 or else (Is_Controlling_Formal (Formal)
5596 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5598 (Explicit_Suppress (E, Accessibility_Check)
5600 Explicit_Suppress (Scope (E), Accessibility_Check))
5603 or else Present (Extra_Accessibility (P_Formal)))
5605 Set_Extra_Accessibility
5606 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5609 -- This label is required when skipping extra formal generation for
5610 -- Unchecked_Union parameters.
5612 <<Skip_Extra_Formal_Generation>>
5614 if Present (P_Formal) then
5615 Next_Formal (P_Formal);
5618 Next_Formal (Formal);
5621 <<Test_For_BIP_Extras>>
5623 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5624 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5626 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5628 Result_Subt : constant Entity_Id := Etype (E);
5630 Discard : Entity_Id;
5631 pragma Warnings (Off, Discard);
5634 -- In the case of functions with unconstrained result subtypes,
5635 -- add a 4-state formal indicating whether the return object is
5636 -- allocated by the caller (1), or should be allocated by the
5637 -- callee on the secondary stack (2), in the global heap (3), or
5638 -- in a user-defined storage pool (4). For the moment we just use
5639 -- Natural for the type of this formal. Note that this formal
5640 -- isn't usually needed in the case where the result subtype is
5641 -- constrained, but it is needed when the function has a tagged
5642 -- result, because generally such functions can be called in a
5643 -- dispatching context and such calls must be handled like calls
5644 -- to a class-wide function.
5646 if not Is_Constrained (Underlying_Type (Result_Subt))
5647 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5651 (E, Standard_Natural,
5652 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5655 -- In the case of functions whose result type has controlled
5656 -- parts, we have an extra formal of type
5657 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5658 -- is, we are passing a pointer to a finalization list (which is
5659 -- itself a pointer). This extra formal is then passed along to
5660 -- Move_Final_List in case of successful completion of a return
5661 -- statement. We cannot pass an 'in out' parameter, because we
5662 -- need to update the finalization list during an abort-deferred
5663 -- region, rather than using copy-back after the function
5664 -- returns. This is true even if we are able to get away with
5665 -- having 'in out' parameters, which are normally illegal for
5666 -- functions. This formal is also needed when the function has
5669 if Needs_BIP_Final_List (E) then
5672 (E, RTE (RE_Finalizable_Ptr_Ptr),
5673 E, BIP_Formal_Suffix (BIP_Final_List));
5676 -- If the result type contains tasks, we have two extra formals:
5677 -- the master of the tasks to be created, and the caller's
5678 -- activation chain.
5680 if Has_Task (Result_Subt) then
5683 (E, RTE (RE_Master_Id),
5684 E, BIP_Formal_Suffix (BIP_Master));
5687 (E, RTE (RE_Activation_Chain_Access),
5688 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5691 -- All build-in-place functions get an extra formal that will be
5692 -- passed the address of the return object within the caller.
5695 Formal_Type : constant Entity_Id :=
5697 (E_Anonymous_Access_Type, E,
5698 Scope_Id => Scope (E));
5700 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5701 Set_Etype (Formal_Type, Formal_Type);
5702 Set_Depends_On_Private
5703 (Formal_Type, Has_Private_Component (Formal_Type));
5704 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5705 Set_Is_Access_Constant (Formal_Type, False);
5707 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5708 -- the designated type comes from the limited view (for
5709 -- back-end purposes).
5711 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5713 Layout_Type (Formal_Type);
5717 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5721 end Create_Extra_Formals;
5723 -----------------------------
5724 -- Enter_Overloaded_Entity --
5725 -----------------------------
5727 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5728 E : Entity_Id := Current_Entity_In_Scope (S);
5729 C_E : Entity_Id := Current_Entity (S);
5733 Set_Has_Homonym (E);
5734 Set_Has_Homonym (S);
5737 Set_Is_Immediately_Visible (S);
5738 Set_Scope (S, Current_Scope);
5740 -- Chain new entity if front of homonym in current scope, so that
5741 -- homonyms are contiguous.
5746 while Homonym (C_E) /= E loop
5747 C_E := Homonym (C_E);
5750 Set_Homonym (C_E, S);
5754 Set_Current_Entity (S);
5759 Append_Entity (S, Current_Scope);
5760 Set_Public_Status (S);
5762 if Debug_Flag_E then
5763 Write_Str ("New overloaded entity chain: ");
5764 Write_Name (Chars (S));
5767 while Present (E) loop
5768 Write_Str (" "); Write_Int (Int (E));
5775 -- Generate warning for hiding
5778 and then Comes_From_Source (S)
5779 and then In_Extended_Main_Source_Unit (S)
5786 -- Warn unless genuine overloading
5788 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5789 and then (Is_Immediately_Visible (E)
5791 Is_Potentially_Use_Visible (S))
5793 Error_Msg_Sloc := Sloc (E);
5794 Error_Msg_N ("declaration of & hides one#?", S);
5798 end Enter_Overloaded_Entity;
5800 -----------------------------
5801 -- Check_Untagged_Equality --
5802 -----------------------------
5804 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
5805 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
5806 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
5810 if Nkind (Decl) = N_Subprogram_Declaration
5811 and then Is_Record_Type (Typ)
5812 and then not Is_Tagged_Type (Typ)
5814 if Is_Frozen (Typ) then
5816 ("equality operator must be declared "
5817 & "before type& is frozen", Eq_Op, Typ);
5819 Obj_Decl := Next (Parent (Typ));
5820 while Present (Obj_Decl)
5821 and then Obj_Decl /= Decl
5823 if Nkind (Obj_Decl) = N_Object_Declaration
5824 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
5826 Error_Msg_NE ("type& is frozen by declaration?",
5829 ("\an equality operator cannot be declared after this "
5830 & "point ('R'M 4.5.2 (9.8)) (Ada2012))?", Obj_Decl);
5837 elsif not In_Same_List (Parent (Typ), Decl)
5838 and then not Is_Limited_Type (Typ)
5840 Error_Msg_N ("equality operator appears too late", Eq_Op);
5843 end Check_Untagged_Equality;
5845 -----------------------------
5846 -- Find_Corresponding_Spec --
5847 -----------------------------
5849 function Find_Corresponding_Spec
5851 Post_Error : Boolean := True) return Entity_Id
5853 Spec : constant Node_Id := Specification (N);
5854 Designator : constant Entity_Id := Defining_Entity (Spec);
5859 E := Current_Entity (Designator);
5860 while Present (E) loop
5862 -- We are looking for a matching spec. It must have the same scope,
5863 -- and the same name, and either be type conformant, or be the case
5864 -- of a library procedure spec and its body (which belong to one
5865 -- another regardless of whether they are type conformant or not).
5867 if Scope (E) = Current_Scope then
5868 if Current_Scope = Standard_Standard
5869 or else (Ekind (E) = Ekind (Designator)
5870 and then Type_Conformant (E, Designator))
5872 -- Within an instantiation, we know that spec and body are
5873 -- subtype conformant, because they were subtype conformant
5874 -- in the generic. We choose the subtype-conformant entity
5875 -- here as well, to resolve spurious ambiguities in the
5876 -- instance that were not present in the generic (i.e. when
5877 -- two different types are given the same actual). If we are
5878 -- looking for a spec to match a body, full conformance is
5882 Set_Convention (Designator, Convention (E));
5884 if Nkind (N) = N_Subprogram_Body
5885 and then Present (Homonym (E))
5886 and then not Fully_Conformant (E, Designator)
5890 elsif not Subtype_Conformant (E, Designator) then
5895 if not Has_Completion (E) then
5896 if Nkind (N) /= N_Subprogram_Body_Stub then
5897 Set_Corresponding_Spec (N, E);
5900 Set_Has_Completion (E);
5903 elsif Nkind (Parent (N)) = N_Subunit then
5905 -- If this is the proper body of a subunit, the completion
5906 -- flag is set when analyzing the stub.
5910 -- If E is an internal function with a controlling result
5911 -- that was created for an operation inherited by a null
5912 -- extension, it may be overridden by a body without a previous
5913 -- spec (one more reason why these should be shunned). In that
5914 -- case remove the generated body, because the current one is
5915 -- the explicit overriding.
5917 elsif Ekind (E) = E_Function
5918 and then Ada_Version >= Ada_05
5919 and then not Comes_From_Source (E)
5920 and then Has_Controlling_Result (E)
5921 and then Is_Null_Extension (Etype (E))
5922 and then Comes_From_Source (Spec)
5924 Set_Has_Completion (E, False);
5926 if Expander_Active then
5928 (Unit_Declaration_Node
5929 (Corresponding_Body (Unit_Declaration_Node (E))));
5932 -- If expansion is disabled, the wrapper function has not
5933 -- been generated, and this is the standard case of a late
5934 -- body overriding an inherited operation.
5940 -- If the body already exists, then this is an error unless
5941 -- the previous declaration is the implicit declaration of a
5942 -- derived subprogram, or this is a spurious overloading in an
5945 elsif No (Alias (E))
5946 and then not Is_Intrinsic_Subprogram (E)
5947 and then not In_Instance
5950 Error_Msg_Sloc := Sloc (E);
5952 if Is_Imported (E) then
5954 ("body not allowed for imported subprogram & declared#",
5957 Error_Msg_NE ("duplicate body for & declared#", N, E);
5961 -- Child units cannot be overloaded, so a conformance mismatch
5962 -- between body and a previous spec is an error.
5964 elsif Is_Child_Unit (E)
5966 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5968 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5973 ("body of child unit does not match previous declaration", N);
5981 -- On exit, we know that no previous declaration of subprogram exists
5984 end Find_Corresponding_Spec;
5986 ----------------------
5987 -- Fully_Conformant --
5988 ----------------------
5990 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5993 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5995 end Fully_Conformant;
5997 ----------------------------------
5998 -- Fully_Conformant_Expressions --
5999 ----------------------------------
6001 function Fully_Conformant_Expressions
6002 (Given_E1 : Node_Id;
6003 Given_E2 : Node_Id) return Boolean
6005 E1 : constant Node_Id := Original_Node (Given_E1);
6006 E2 : constant Node_Id := Original_Node (Given_E2);
6007 -- We always test conformance on original nodes, since it is possible
6008 -- for analysis and/or expansion to make things look as though they
6009 -- conform when they do not, e.g. by converting 1+2 into 3.
6011 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6012 renames Fully_Conformant_Expressions;
6014 function FCL (L1, L2 : List_Id) return Boolean;
6015 -- Compare elements of two lists for conformance. Elements have to
6016 -- be conformant, and actuals inserted as default parameters do not
6017 -- match explicit actuals with the same value.
6019 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6020 -- Compare an operator node with a function call
6026 function FCL (L1, L2 : List_Id) return Boolean is
6030 if L1 = No_List then
6036 if L2 = No_List then
6042 -- Compare two lists, skipping rewrite insertions (we want to
6043 -- compare the original trees, not the expanded versions!)
6046 if Is_Rewrite_Insertion (N1) then
6048 elsif Is_Rewrite_Insertion (N2) then
6054 elsif not FCE (N1, N2) then
6067 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6068 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6073 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6078 Act := First (Actuals);
6080 if Nkind (Op_Node) in N_Binary_Op then
6081 if not FCE (Left_Opnd (Op_Node), Act) then
6088 return Present (Act)
6089 and then FCE (Right_Opnd (Op_Node), Act)
6090 and then No (Next (Act));
6094 -- Start of processing for Fully_Conformant_Expressions
6097 -- Non-conformant if paren count does not match. Note: if some idiot
6098 -- complains that we don't do this right for more than 3 levels of
6099 -- parentheses, they will be treated with the respect they deserve!
6101 if Paren_Count (E1) /= Paren_Count (E2) then
6104 -- If same entities are referenced, then they are conformant even if
6105 -- they have different forms (RM 8.3.1(19-20)).
6107 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6108 if Present (Entity (E1)) then
6109 return Entity (E1) = Entity (E2)
6110 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6111 and then Ekind (Entity (E1)) = E_Discriminant
6112 and then Ekind (Entity (E2)) = E_In_Parameter);
6114 elsif Nkind (E1) = N_Expanded_Name
6115 and then Nkind (E2) = N_Expanded_Name
6116 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6117 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6119 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6122 -- Identifiers in component associations don't always have
6123 -- entities, but their names must conform.
6125 return Nkind (E1) = N_Identifier
6126 and then Nkind (E2) = N_Identifier
6127 and then Chars (E1) = Chars (E2);
6130 elsif Nkind (E1) = N_Character_Literal
6131 and then Nkind (E2) = N_Expanded_Name
6133 return Nkind (Selector_Name (E2)) = N_Character_Literal
6134 and then Chars (E1) = Chars (Selector_Name (E2));
6136 elsif Nkind (E2) = N_Character_Literal
6137 and then Nkind (E1) = N_Expanded_Name
6139 return Nkind (Selector_Name (E1)) = N_Character_Literal
6140 and then Chars (E2) = Chars (Selector_Name (E1));
6142 elsif Nkind (E1) in N_Op
6143 and then Nkind (E2) = N_Function_Call
6145 return FCO (E1, E2);
6147 elsif Nkind (E2) in N_Op
6148 and then Nkind (E1) = N_Function_Call
6150 return FCO (E2, E1);
6152 -- Otherwise we must have the same syntactic entity
6154 elsif Nkind (E1) /= Nkind (E2) then
6157 -- At this point, we specialize by node type
6164 FCL (Expressions (E1), Expressions (E2))
6166 FCL (Component_Associations (E1),
6167 Component_Associations (E2));
6170 if Nkind (Expression (E1)) = N_Qualified_Expression
6172 Nkind (Expression (E2)) = N_Qualified_Expression
6174 return FCE (Expression (E1), Expression (E2));
6176 -- Check that the subtype marks and any constraints
6181 Indic1 : constant Node_Id := Expression (E1);
6182 Indic2 : constant Node_Id := Expression (E2);
6187 if Nkind (Indic1) /= N_Subtype_Indication then
6189 Nkind (Indic2) /= N_Subtype_Indication
6190 and then Entity (Indic1) = Entity (Indic2);
6192 elsif Nkind (Indic2) /= N_Subtype_Indication then
6194 Nkind (Indic1) /= N_Subtype_Indication
6195 and then Entity (Indic1) = Entity (Indic2);
6198 if Entity (Subtype_Mark (Indic1)) /=
6199 Entity (Subtype_Mark (Indic2))
6204 Elt1 := First (Constraints (Constraint (Indic1)));
6205 Elt2 := First (Constraints (Constraint (Indic2)));
6206 while Present (Elt1) and then Present (Elt2) loop
6207 if not FCE (Elt1, Elt2) then
6220 when N_Attribute_Reference =>
6222 Attribute_Name (E1) = Attribute_Name (E2)
6223 and then FCL (Expressions (E1), Expressions (E2));
6227 Entity (E1) = Entity (E2)
6228 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6229 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6231 when N_Short_Circuit | N_Membership_Test =>
6233 FCE (Left_Opnd (E1), Left_Opnd (E2))
6235 FCE (Right_Opnd (E1), Right_Opnd (E2));
6237 when N_Case_Expression =>
6243 if not FCE (Expression (E1), Expression (E2)) then
6247 Alt1 := First (Alternatives (E1));
6248 Alt2 := First (Alternatives (E2));
6250 if Present (Alt1) /= Present (Alt2) then
6252 elsif No (Alt1) then
6256 if not FCE (Expression (Alt1), Expression (Alt2))
6257 or else not FCL (Discrete_Choices (Alt1),
6258 Discrete_Choices (Alt2))
6269 when N_Character_Literal =>
6271 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6273 when N_Component_Association =>
6275 FCL (Choices (E1), Choices (E2))
6277 FCE (Expression (E1), Expression (E2));
6279 when N_Conditional_Expression =>
6281 FCL (Expressions (E1), Expressions (E2));
6283 when N_Explicit_Dereference =>
6285 FCE (Prefix (E1), Prefix (E2));
6287 when N_Extension_Aggregate =>
6289 FCL (Expressions (E1), Expressions (E2))
6290 and then Null_Record_Present (E1) =
6291 Null_Record_Present (E2)
6292 and then FCL (Component_Associations (E1),
6293 Component_Associations (E2));
6295 when N_Function_Call =>
6297 FCE (Name (E1), Name (E2))
6299 FCL (Parameter_Associations (E1),
6300 Parameter_Associations (E2));
6302 when N_Indexed_Component =>
6304 FCE (Prefix (E1), Prefix (E2))
6306 FCL (Expressions (E1), Expressions (E2));
6308 when N_Integer_Literal =>
6309 return (Intval (E1) = Intval (E2));
6314 when N_Operator_Symbol =>
6316 Chars (E1) = Chars (E2);
6318 when N_Others_Choice =>
6321 when N_Parameter_Association =>
6323 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6324 and then FCE (Explicit_Actual_Parameter (E1),
6325 Explicit_Actual_Parameter (E2));
6327 when N_Qualified_Expression =>
6329 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6331 FCE (Expression (E1), Expression (E2));
6335 FCE (Low_Bound (E1), Low_Bound (E2))
6337 FCE (High_Bound (E1), High_Bound (E2));
6339 when N_Real_Literal =>
6340 return (Realval (E1) = Realval (E2));
6342 when N_Selected_Component =>
6344 FCE (Prefix (E1), Prefix (E2))
6346 FCE (Selector_Name (E1), Selector_Name (E2));
6350 FCE (Prefix (E1), Prefix (E2))
6352 FCE (Discrete_Range (E1), Discrete_Range (E2));
6354 when N_String_Literal =>
6356 S1 : constant String_Id := Strval (E1);
6357 S2 : constant String_Id := Strval (E2);
6358 L1 : constant Nat := String_Length (S1);
6359 L2 : constant Nat := String_Length (S2);
6366 for J in 1 .. L1 loop
6367 if Get_String_Char (S1, J) /=
6368 Get_String_Char (S2, J)
6378 when N_Type_Conversion =>
6380 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6382 FCE (Expression (E1), Expression (E2));
6386 Entity (E1) = Entity (E2)
6388 FCE (Right_Opnd (E1), Right_Opnd (E2));
6390 when N_Unchecked_Type_Conversion =>
6392 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6394 FCE (Expression (E1), Expression (E2));
6396 -- All other node types cannot appear in this context. Strictly
6397 -- we should raise a fatal internal error. Instead we just ignore
6398 -- the nodes. This means that if anyone makes a mistake in the
6399 -- expander and mucks an expression tree irretrievably, the
6400 -- result will be a failure to detect a (probably very obscure)
6401 -- case of non-conformance, which is better than bombing on some
6402 -- case where two expressions do in fact conform.
6409 end Fully_Conformant_Expressions;
6411 ----------------------------------------
6412 -- Fully_Conformant_Discrete_Subtypes --
6413 ----------------------------------------
6415 function Fully_Conformant_Discrete_Subtypes
6416 (Given_S1 : Node_Id;
6417 Given_S2 : Node_Id) return Boolean
6419 S1 : constant Node_Id := Original_Node (Given_S1);
6420 S2 : constant Node_Id := Original_Node (Given_S2);
6422 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6423 -- Special-case for a bound given by a discriminant, which in the body
6424 -- is replaced with the discriminal of the enclosing type.
6426 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6427 -- Check both bounds
6429 -----------------------
6430 -- Conforming_Bounds --
6431 -----------------------
6433 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6435 if Is_Entity_Name (B1)
6436 and then Is_Entity_Name (B2)
6437 and then Ekind (Entity (B1)) = E_Discriminant
6439 return Chars (B1) = Chars (B2);
6442 return Fully_Conformant_Expressions (B1, B2);
6444 end Conforming_Bounds;
6446 -----------------------
6447 -- Conforming_Ranges --
6448 -----------------------
6450 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6453 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6455 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6456 end Conforming_Ranges;
6458 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6461 if Nkind (S1) /= Nkind (S2) then
6464 elsif Is_Entity_Name (S1) then
6465 return Entity (S1) = Entity (S2);
6467 elsif Nkind (S1) = N_Range then
6468 return Conforming_Ranges (S1, S2);
6470 elsif Nkind (S1) = N_Subtype_Indication then
6472 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6475 (Range_Expression (Constraint (S1)),
6476 Range_Expression (Constraint (S2)));
6480 end Fully_Conformant_Discrete_Subtypes;
6482 --------------------
6483 -- Install_Entity --
6484 --------------------
6486 procedure Install_Entity (E : Entity_Id) is
6487 Prev : constant Entity_Id := Current_Entity (E);
6489 Set_Is_Immediately_Visible (E);
6490 Set_Current_Entity (E);
6491 Set_Homonym (E, Prev);
6494 ---------------------
6495 -- Install_Formals --
6496 ---------------------
6498 procedure Install_Formals (Id : Entity_Id) is
6501 F := First_Formal (Id);
6502 while Present (F) loop
6506 end Install_Formals;
6508 -----------------------------
6509 -- Is_Interface_Conformant --
6510 -----------------------------
6512 function Is_Interface_Conformant
6513 (Tagged_Type : Entity_Id;
6514 Iface_Prim : Entity_Id;
6515 Prim : Entity_Id) return Boolean
6517 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6518 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6521 pragma Assert (Is_Subprogram (Iface_Prim)
6522 and then Is_Subprogram (Prim)
6523 and then Is_Dispatching_Operation (Iface_Prim)
6524 and then Is_Dispatching_Operation (Prim));
6526 pragma Assert (Is_Interface (Iface)
6527 or else (Present (Alias (Iface_Prim))
6530 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6532 if Prim = Iface_Prim
6533 or else not Is_Subprogram (Prim)
6534 or else Ekind (Prim) /= Ekind (Iface_Prim)
6535 or else not Is_Dispatching_Operation (Prim)
6536 or else Scope (Prim) /= Scope (Tagged_Type)
6538 or else Base_Type (Typ) /= Tagged_Type
6539 or else not Primitive_Names_Match (Iface_Prim, Prim)
6543 -- Case of a procedure, or a function that does not have a controlling
6544 -- result (I or access I).
6546 elsif Ekind (Iface_Prim) = E_Procedure
6547 or else Etype (Prim) = Etype (Iface_Prim)
6548 or else not Has_Controlling_Result (Prim)
6550 return Type_Conformant
6551 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6553 -- Case of a function returning an interface, or an access to one.
6554 -- Check that the return types correspond.
6556 elsif Implements_Interface (Typ, Iface) then
6557 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6559 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6564 Type_Conformant (Prim, Iface_Prim,
6565 Skip_Controlling_Formals => True);
6571 end Is_Interface_Conformant;
6573 ---------------------------------
6574 -- Is_Non_Overriding_Operation --
6575 ---------------------------------
6577 function Is_Non_Overriding_Operation
6578 (Prev_E : Entity_Id;
6579 New_E : Entity_Id) return Boolean
6583 G_Typ : Entity_Id := Empty;
6585 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6586 -- If F_Type is a derived type associated with a generic actual subtype,
6587 -- then return its Generic_Parent_Type attribute, else return Empty.
6589 function Types_Correspond
6590 (P_Type : Entity_Id;
6591 N_Type : Entity_Id) return Boolean;
6592 -- Returns true if and only if the types (or designated types in the
6593 -- case of anonymous access types) are the same or N_Type is derived
6594 -- directly or indirectly from P_Type.
6596 -----------------------------
6597 -- Get_Generic_Parent_Type --
6598 -----------------------------
6600 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6605 if Is_Derived_Type (F_Typ)
6606 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6608 -- The tree must be traversed to determine the parent subtype in
6609 -- the generic unit, which unfortunately isn't always available
6610 -- via semantic attributes. ??? (Note: The use of Original_Node
6611 -- is needed for cases where a full derived type has been
6614 Indic := Subtype_Indication
6615 (Type_Definition (Original_Node (Parent (F_Typ))));
6617 if Nkind (Indic) = N_Subtype_Indication then
6618 G_Typ := Entity (Subtype_Mark (Indic));
6620 G_Typ := Entity (Indic);
6623 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6624 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6626 return Generic_Parent_Type (Parent (G_Typ));
6631 end Get_Generic_Parent_Type;
6633 ----------------------
6634 -- Types_Correspond --
6635 ----------------------
6637 function Types_Correspond
6638 (P_Type : Entity_Id;
6639 N_Type : Entity_Id) return Boolean
6641 Prev_Type : Entity_Id := Base_Type (P_Type);
6642 New_Type : Entity_Id := Base_Type (N_Type);
6645 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6646 Prev_Type := Designated_Type (Prev_Type);
6649 if Ekind (New_Type) = E_Anonymous_Access_Type then
6650 New_Type := Designated_Type (New_Type);
6653 if Prev_Type = New_Type then
6656 elsif not Is_Class_Wide_Type (New_Type) then
6657 while Etype (New_Type) /= New_Type loop
6658 New_Type := Etype (New_Type);
6659 if New_Type = Prev_Type then
6665 end Types_Correspond;
6667 -- Start of processing for Is_Non_Overriding_Operation
6670 -- In the case where both operations are implicit derived subprograms
6671 -- then neither overrides the other. This can only occur in certain
6672 -- obscure cases (e.g., derivation from homographs created in a generic
6675 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6678 elsif Ekind (Current_Scope) = E_Package
6679 and then Is_Generic_Instance (Current_Scope)
6680 and then In_Private_Part (Current_Scope)
6681 and then Comes_From_Source (New_E)
6683 -- We examine the formals and result subtype of the inherited
6684 -- operation, to determine whether their type is derived from (the
6685 -- instance of) a generic type.
6687 Formal := First_Formal (Prev_E);
6688 while Present (Formal) loop
6689 F_Typ := Base_Type (Etype (Formal));
6691 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6692 F_Typ := Designated_Type (F_Typ);
6695 G_Typ := Get_Generic_Parent_Type (F_Typ);
6697 Next_Formal (Formal);
6700 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6701 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6708 -- If the generic type is a private type, then the original operation
6709 -- was not overriding in the generic, because there was no primitive
6710 -- operation to override.
6712 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6713 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6714 N_Formal_Private_Type_Definition
6718 -- The generic parent type is the ancestor of a formal derived
6719 -- type declaration. We need to check whether it has a primitive
6720 -- operation that should be overridden by New_E in the generic.
6724 P_Formal : Entity_Id;
6725 N_Formal : Entity_Id;
6729 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6732 while Present (Prim_Elt) loop
6733 P_Prim := Node (Prim_Elt);
6735 if Chars (P_Prim) = Chars (New_E)
6736 and then Ekind (P_Prim) = Ekind (New_E)
6738 P_Formal := First_Formal (P_Prim);
6739 N_Formal := First_Formal (New_E);
6740 while Present (P_Formal) and then Present (N_Formal) loop
6741 P_Typ := Etype (P_Formal);
6742 N_Typ := Etype (N_Formal);
6744 if not Types_Correspond (P_Typ, N_Typ) then
6748 Next_Entity (P_Formal);
6749 Next_Entity (N_Formal);
6752 -- Found a matching primitive operation belonging to the
6753 -- formal ancestor type, so the new subprogram is
6757 and then No (N_Formal)
6758 and then (Ekind (New_E) /= E_Function
6761 (Etype (P_Prim), Etype (New_E)))
6767 Next_Elmt (Prim_Elt);
6770 -- If no match found, then the new subprogram does not
6771 -- override in the generic (nor in the instance).
6779 end Is_Non_Overriding_Operation;
6781 ------------------------------
6782 -- Make_Inequality_Operator --
6783 ------------------------------
6785 -- S is the defining identifier of an equality operator. We build a
6786 -- subprogram declaration with the right signature. This operation is
6787 -- intrinsic, because it is always expanded as the negation of the
6788 -- call to the equality function.
6790 procedure Make_Inequality_Operator (S : Entity_Id) is
6791 Loc : constant Source_Ptr := Sloc (S);
6794 Op_Name : Entity_Id;
6796 FF : constant Entity_Id := First_Formal (S);
6797 NF : constant Entity_Id := Next_Formal (FF);
6800 -- Check that equality was properly defined, ignore call if not
6807 A : constant Entity_Id :=
6808 Make_Defining_Identifier (Sloc (FF),
6809 Chars => Chars (FF));
6811 B : constant Entity_Id :=
6812 Make_Defining_Identifier (Sloc (NF),
6813 Chars => Chars (NF));
6816 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6818 Formals := New_List (
6819 Make_Parameter_Specification (Loc,
6820 Defining_Identifier => A,
6822 New_Reference_To (Etype (First_Formal (S)),
6823 Sloc (Etype (First_Formal (S))))),
6825 Make_Parameter_Specification (Loc,
6826 Defining_Identifier => B,
6828 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6829 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6832 Make_Subprogram_Declaration (Loc,
6834 Make_Function_Specification (Loc,
6835 Defining_Unit_Name => Op_Name,
6836 Parameter_Specifications => Formals,
6837 Result_Definition =>
6838 New_Reference_To (Standard_Boolean, Loc)));
6840 -- Insert inequality right after equality if it is explicit or after
6841 -- the derived type when implicit. These entities are created only
6842 -- for visibility purposes, and eventually replaced in the course of
6843 -- expansion, so they do not need to be attached to the tree and seen
6844 -- by the back-end. Keeping them internal also avoids spurious
6845 -- freezing problems. The declaration is inserted in the tree for
6846 -- analysis, and removed afterwards. If the equality operator comes
6847 -- from an explicit declaration, attach the inequality immediately
6848 -- after. Else the equality is inherited from a derived type
6849 -- declaration, so insert inequality after that declaration.
6851 if No (Alias (S)) then
6852 Insert_After (Unit_Declaration_Node (S), Decl);
6853 elsif Is_List_Member (Parent (S)) then
6854 Insert_After (Parent (S), Decl);
6856 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6859 Mark_Rewrite_Insertion (Decl);
6860 Set_Is_Intrinsic_Subprogram (Op_Name);
6863 Set_Has_Completion (Op_Name);
6864 Set_Corresponding_Equality (Op_Name, S);
6865 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6867 end Make_Inequality_Operator;
6869 ----------------------
6870 -- May_Need_Actuals --
6871 ----------------------
6873 procedure May_Need_Actuals (Fun : Entity_Id) is
6878 F := First_Formal (Fun);
6880 while Present (F) loop
6881 if No (Default_Value (F)) then
6889 Set_Needs_No_Actuals (Fun, B);
6890 end May_Need_Actuals;
6892 ---------------------
6893 -- Mode_Conformant --
6894 ---------------------
6896 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6899 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6901 end Mode_Conformant;
6903 ---------------------------
6904 -- New_Overloaded_Entity --
6905 ---------------------------
6907 procedure New_Overloaded_Entity
6909 Derived_Type : Entity_Id := Empty)
6911 Overridden_Subp : Entity_Id := Empty;
6912 -- Set if the current scope has an operation that is type-conformant
6913 -- with S, and becomes hidden by S.
6915 Is_Primitive_Subp : Boolean;
6916 -- Set to True if the new subprogram is primitive
6919 -- Entity that S overrides
6921 Prev_Vis : Entity_Id := Empty;
6922 -- Predecessor of E in Homonym chain
6924 procedure Check_For_Primitive_Subprogram
6925 (Is_Primitive : out Boolean;
6926 Is_Overriding : Boolean := False);
6927 -- If the subprogram being analyzed is a primitive operation of the type
6928 -- of a formal or result, set the Has_Primitive_Operations flag on the
6929 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6930 -- corresponding flag on the entity itself for later use.
6932 procedure Check_Synchronized_Overriding
6933 (Def_Id : Entity_Id;
6934 Overridden_Subp : out Entity_Id);
6935 -- First determine if Def_Id is an entry or a subprogram either defined
6936 -- in the scope of a task or protected type, or is a primitive of such
6937 -- a type. Check whether Def_Id overrides a subprogram of an interface
6938 -- implemented by the synchronized type, return the overridden entity
6941 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6942 -- Check that E is declared in the private part of the current package,
6943 -- or in the package body, where it may hide a previous declaration.
6944 -- We can't use In_Private_Part by itself because this flag is also
6945 -- set when freezing entities, so we must examine the place of the
6946 -- declaration in the tree, and recognize wrapper packages as well.
6948 function Is_Overriding_Alias
6950 New_E : Entity_Id) return Boolean;
6951 -- Check whether new subprogram and old subprogram are both inherited
6952 -- from subprograms that have distinct dispatch table entries. This can
6953 -- occur with derivations from instances with accidental homonyms.
6954 -- The function is conservative given that the converse is only true
6955 -- within instances that contain accidental overloadings.
6957 ------------------------------------
6958 -- Check_For_Primitive_Subprogram --
6959 ------------------------------------
6961 procedure Check_For_Primitive_Subprogram
6962 (Is_Primitive : out Boolean;
6963 Is_Overriding : Boolean := False)
6969 function Visible_Part_Type (T : Entity_Id) return Boolean;
6970 -- Returns true if T is declared in the visible part of the current
6971 -- package scope; otherwise returns false. Assumes that T is declared
6974 procedure Check_Private_Overriding (T : Entity_Id);
6975 -- Checks that if a primitive abstract subprogram of a visible
6976 -- abstract type is declared in a private part, then it must override
6977 -- an abstract subprogram declared in the visible part. Also checks
6978 -- that if a primitive function with a controlling result is declared
6979 -- in a private part, then it must override a function declared in
6980 -- the visible part.
6982 ------------------------------
6983 -- Check_Private_Overriding --
6984 ------------------------------
6986 procedure Check_Private_Overriding (T : Entity_Id) is
6988 if Is_Package_Or_Generic_Package (Current_Scope)
6989 and then In_Private_Part (Current_Scope)
6990 and then Visible_Part_Type (T)
6991 and then not In_Instance
6993 if Is_Abstract_Type (T)
6994 and then Is_Abstract_Subprogram (S)
6995 and then (not Is_Overriding
6996 or else not Is_Abstract_Subprogram (E))
6999 ("abstract subprograms must be visible "
7000 & "(RM 3.9.3(10))!", S);
7002 elsif Ekind (S) = E_Function
7003 and then Is_Tagged_Type (T)
7004 and then T = Base_Type (Etype (S))
7005 and then not Is_Overriding
7008 ("private function with tagged result must"
7009 & " override visible-part function", S);
7011 ("\move subprogram to the visible part"
7012 & " (RM 3.9.3(10))", S);
7015 end Check_Private_Overriding;
7017 -----------------------
7018 -- Visible_Part_Type --
7019 -----------------------
7021 function Visible_Part_Type (T : Entity_Id) return Boolean is
7022 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7026 -- If the entity is a private type, then it must be declared in a
7029 if Ekind (T) in Private_Kind then
7033 -- Otherwise, we traverse the visible part looking for its
7034 -- corresponding declaration. We cannot use the declaration
7035 -- node directly because in the private part the entity of a
7036 -- private type is the one in the full view, which does not
7037 -- indicate that it is the completion of something visible.
7039 N := First (Visible_Declarations (Specification (P)));
7040 while Present (N) loop
7041 if Nkind (N) = N_Full_Type_Declaration
7042 and then Present (Defining_Identifier (N))
7043 and then T = Defining_Identifier (N)
7047 elsif Nkind_In (N, N_Private_Type_Declaration,
7048 N_Private_Extension_Declaration)
7049 and then Present (Defining_Identifier (N))
7050 and then T = Full_View (Defining_Identifier (N))
7059 end Visible_Part_Type;
7061 -- Start of processing for Check_For_Primitive_Subprogram
7064 Is_Primitive := False;
7066 if not Comes_From_Source (S) then
7069 -- If subprogram is at library level, it is not primitive operation
7071 elsif Current_Scope = Standard_Standard then
7074 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7075 and then not In_Package_Body (Current_Scope))
7076 or else Is_Overriding
7078 -- For function, check return type
7080 if Ekind (S) = E_Function then
7081 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7082 F_Typ := Designated_Type (Etype (S));
7087 B_Typ := Base_Type (F_Typ);
7089 if Scope (B_Typ) = Current_Scope
7090 and then not Is_Class_Wide_Type (B_Typ)
7091 and then not Is_Generic_Type (B_Typ)
7093 Is_Primitive := True;
7094 Set_Has_Primitive_Operations (B_Typ);
7095 Set_Is_Primitive (S);
7096 Check_Private_Overriding (B_Typ);
7100 -- For all subprograms, check formals
7102 Formal := First_Formal (S);
7103 while Present (Formal) loop
7104 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7105 F_Typ := Designated_Type (Etype (Formal));
7107 F_Typ := Etype (Formal);
7110 B_Typ := Base_Type (F_Typ);
7112 if Ekind (B_Typ) = E_Access_Subtype then
7113 B_Typ := Base_Type (B_Typ);
7116 if Scope (B_Typ) = Current_Scope
7117 and then not Is_Class_Wide_Type (B_Typ)
7118 and then not Is_Generic_Type (B_Typ)
7120 Is_Primitive := True;
7121 Set_Is_Primitive (S);
7122 Set_Has_Primitive_Operations (B_Typ);
7123 Check_Private_Overriding (B_Typ);
7126 Next_Formal (Formal);
7129 end Check_For_Primitive_Subprogram;
7131 -----------------------------------
7132 -- Check_Synchronized_Overriding --
7133 -----------------------------------
7135 procedure Check_Synchronized_Overriding
7136 (Def_Id : Entity_Id;
7137 Overridden_Subp : out Entity_Id)
7139 Ifaces_List : Elist_Id;
7143 function Matches_Prefixed_View_Profile
7144 (Prim_Params : List_Id;
7145 Iface_Params : List_Id) return Boolean;
7146 -- Determine whether a subprogram's parameter profile Prim_Params
7147 -- matches that of a potentially overridden interface subprogram
7148 -- Iface_Params. Also determine if the type of first parameter of
7149 -- Iface_Params is an implemented interface.
7151 -----------------------------------
7152 -- Matches_Prefixed_View_Profile --
7153 -----------------------------------
7155 function Matches_Prefixed_View_Profile
7156 (Prim_Params : List_Id;
7157 Iface_Params : List_Id) return Boolean
7159 Iface_Id : Entity_Id;
7160 Iface_Param : Node_Id;
7161 Iface_Typ : Entity_Id;
7162 Prim_Id : Entity_Id;
7163 Prim_Param : Node_Id;
7164 Prim_Typ : Entity_Id;
7166 function Is_Implemented
7167 (Ifaces_List : Elist_Id;
7168 Iface : Entity_Id) return Boolean;
7169 -- Determine if Iface is implemented by the current task or
7172 --------------------
7173 -- Is_Implemented --
7174 --------------------
7176 function Is_Implemented
7177 (Ifaces_List : Elist_Id;
7178 Iface : Entity_Id) return Boolean
7180 Iface_Elmt : Elmt_Id;
7183 Iface_Elmt := First_Elmt (Ifaces_List);
7184 while Present (Iface_Elmt) loop
7185 if Node (Iface_Elmt) = Iface then
7189 Next_Elmt (Iface_Elmt);
7195 -- Start of processing for Matches_Prefixed_View_Profile
7198 Iface_Param := First (Iface_Params);
7199 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7201 if Is_Access_Type (Iface_Typ) then
7202 Iface_Typ := Designated_Type (Iface_Typ);
7205 Prim_Param := First (Prim_Params);
7207 -- The first parameter of the potentially overridden subprogram
7208 -- must be an interface implemented by Prim.
7210 if not Is_Interface (Iface_Typ)
7211 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7216 -- The checks on the object parameters are done, move onto the
7217 -- rest of the parameters.
7219 if not In_Scope then
7220 Prim_Param := Next (Prim_Param);
7223 Iface_Param := Next (Iface_Param);
7224 while Present (Iface_Param) and then Present (Prim_Param) loop
7225 Iface_Id := Defining_Identifier (Iface_Param);
7226 Iface_Typ := Find_Parameter_Type (Iface_Param);
7228 Prim_Id := Defining_Identifier (Prim_Param);
7229 Prim_Typ := Find_Parameter_Type (Prim_Param);
7231 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7232 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7233 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7235 Iface_Typ := Designated_Type (Iface_Typ);
7236 Prim_Typ := Designated_Type (Prim_Typ);
7239 -- Case of multiple interface types inside a parameter profile
7241 -- (Obj_Param : in out Iface; ...; Param : Iface)
7243 -- If the interface type is implemented, then the matching type
7244 -- in the primitive should be the implementing record type.
7246 if Ekind (Iface_Typ) = E_Record_Type
7247 and then Is_Interface (Iface_Typ)
7248 and then Is_Implemented (Ifaces_List, Iface_Typ)
7250 if Prim_Typ /= Typ then
7254 -- The two parameters must be both mode and subtype conformant
7256 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7258 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7267 -- One of the two lists contains more parameters than the other
7269 if Present (Iface_Param) or else Present (Prim_Param) then
7274 end Matches_Prefixed_View_Profile;
7276 -- Start of processing for Check_Synchronized_Overriding
7279 Overridden_Subp := Empty;
7281 -- Def_Id must be an entry or a subprogram. We should skip predefined
7282 -- primitives internally generated by the frontend; however at this
7283 -- stage predefined primitives are still not fully decorated. As a
7284 -- minor optimization we skip here internally generated subprograms.
7286 if (Ekind (Def_Id) /= E_Entry
7287 and then Ekind (Def_Id) /= E_Function
7288 and then Ekind (Def_Id) /= E_Procedure)
7289 or else not Comes_From_Source (Def_Id)
7294 -- Search for the concurrent declaration since it contains the list
7295 -- of all implemented interfaces. In this case, the subprogram is
7296 -- declared within the scope of a protected or a task type.
7298 if Present (Scope (Def_Id))
7299 and then Is_Concurrent_Type (Scope (Def_Id))
7300 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7302 Typ := Scope (Def_Id);
7305 -- The enclosing scope is not a synchronized type and the subprogram
7308 elsif No (First_Formal (Def_Id)) then
7311 -- The subprogram has formals and hence it may be a primitive of a
7315 Typ := Etype (First_Formal (Def_Id));
7317 if Is_Access_Type (Typ) then
7318 Typ := Directly_Designated_Type (Typ);
7321 if Is_Concurrent_Type (Typ)
7322 and then not Is_Generic_Actual_Type (Typ)
7326 -- This case occurs when the concurrent type is declared within
7327 -- a generic unit. As a result the corresponding record has been
7328 -- built and used as the type of the first formal, we just have
7329 -- to retrieve the corresponding concurrent type.
7331 elsif Is_Concurrent_Record_Type (Typ)
7332 and then Present (Corresponding_Concurrent_Type (Typ))
7334 Typ := Corresponding_Concurrent_Type (Typ);
7342 -- There is no overriding to check if is an inherited operation in a
7343 -- type derivation on for a generic actual.
7345 Collect_Interfaces (Typ, Ifaces_List);
7347 if Is_Empty_Elmt_List (Ifaces_List) then
7351 -- Determine whether entry or subprogram Def_Id overrides a primitive
7352 -- operation that belongs to one of the interfaces in Ifaces_List.
7355 Candidate : Entity_Id := Empty;
7356 Hom : Entity_Id := Empty;
7357 Iface_Typ : Entity_Id;
7358 Subp : Entity_Id := Empty;
7361 -- Traverse the homonym chain, looking at a potentially
7362 -- overridden subprogram that belongs to an implemented
7365 Hom := Current_Entity_In_Scope (Def_Id);
7366 while Present (Hom) loop
7370 or else not Is_Overloadable (Subp)
7371 or else not Is_Primitive (Subp)
7372 or else not Is_Dispatching_Operation (Subp)
7373 or else not Present (Find_Dispatching_Type (Subp))
7374 or else not Is_Interface (Find_Dispatching_Type (Subp))
7378 -- Entries and procedures can override abstract or null
7379 -- interface procedures
7381 elsif (Ekind (Def_Id) = E_Procedure
7382 or else Ekind (Def_Id) = E_Entry)
7383 and then Ekind (Subp) = E_Procedure
7384 and then Matches_Prefixed_View_Profile
7385 (Parameter_Specifications (Parent (Def_Id)),
7386 Parameter_Specifications (Parent (Subp)))
7390 -- For an overridden subprogram Subp, check whether the mode
7391 -- of its first parameter is correct depending on the kind
7392 -- of synchronized type.
7395 Formal : constant Node_Id := First_Formal (Candidate);
7398 -- In order for an entry or a protected procedure to
7399 -- override, the first parameter of the overridden
7400 -- routine must be of mode "out", "in out" or
7401 -- access-to-variable.
7403 if (Ekind (Candidate) = E_Entry
7404 or else Ekind (Candidate) = E_Procedure)
7405 and then Is_Protected_Type (Typ)
7406 and then Ekind (Formal) /= E_In_Out_Parameter
7407 and then Ekind (Formal) /= E_Out_Parameter
7408 and then Nkind (Parameter_Type (Parent (Formal)))
7409 /= N_Access_Definition
7413 -- All other cases are OK since a task entry or routine
7414 -- does not have a restriction on the mode of the first
7415 -- parameter of the overridden interface routine.
7418 Overridden_Subp := Candidate;
7423 -- Functions can override abstract interface functions
7425 elsif Ekind (Def_Id) = E_Function
7426 and then Ekind (Subp) = E_Function
7427 and then Matches_Prefixed_View_Profile
7428 (Parameter_Specifications (Parent (Def_Id)),
7429 Parameter_Specifications (Parent (Subp)))
7430 and then Etype (Result_Definition (Parent (Def_Id))) =
7431 Etype (Result_Definition (Parent (Subp)))
7433 Overridden_Subp := Subp;
7437 Hom := Homonym (Hom);
7440 -- After examining all candidates for overriding, we are
7441 -- left with the best match which is a mode incompatible
7442 -- interface routine. Do not emit an error if the Expander
7443 -- is active since this error will be detected later on
7444 -- after all concurrent types are expanded and all wrappers
7445 -- are built. This check is meant for spec-only
7448 if Present (Candidate)
7449 and then not Expander_Active
7452 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7454 -- Def_Id is primitive of a protected type, declared
7455 -- inside the type, and the candidate is primitive of a
7456 -- limited or synchronized interface.
7459 and then Is_Protected_Type (Typ)
7461 (Is_Limited_Interface (Iface_Typ)
7462 or else Is_Protected_Interface (Iface_Typ)
7463 or else Is_Synchronized_Interface (Iface_Typ)
7464 or else Is_Task_Interface (Iface_Typ))
7466 -- Must reword this message, comma before to in -gnatj
7470 ("first formal of & must be of mode `OUT`, `IN OUT`"
7471 & " or access-to-variable", Typ, Candidate);
7473 ("\to be overridden by protected procedure or entry "
7474 & "(RM 9.4(11.9/2))", Typ);
7478 Overridden_Subp := Candidate;
7481 end Check_Synchronized_Overriding;
7483 ----------------------------
7484 -- Is_Private_Declaration --
7485 ----------------------------
7487 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7488 Priv_Decls : List_Id;
7489 Decl : constant Node_Id := Unit_Declaration_Node (E);
7492 if Is_Package_Or_Generic_Package (Current_Scope)
7493 and then In_Private_Part (Current_Scope)
7496 Private_Declarations (
7497 Specification (Unit_Declaration_Node (Current_Scope)));
7499 return In_Package_Body (Current_Scope)
7501 (Is_List_Member (Decl)
7502 and then List_Containing (Decl) = Priv_Decls)
7503 or else (Nkind (Parent (Decl)) = N_Package_Specification
7506 (Defining_Entity (Parent (Decl)))
7507 and then List_Containing (Parent (Parent (Decl)))
7512 end Is_Private_Declaration;
7514 --------------------------
7515 -- Is_Overriding_Alias --
7516 --------------------------
7518 function Is_Overriding_Alias
7520 New_E : Entity_Id) return Boolean
7522 AO : constant Entity_Id := Alias (Old_E);
7523 AN : constant Entity_Id := Alias (New_E);
7526 return Scope (AO) /= Scope (AN)
7527 or else No (DTC_Entity (AO))
7528 or else No (DTC_Entity (AN))
7529 or else DT_Position (AO) = DT_Position (AN);
7530 end Is_Overriding_Alias;
7532 -- Start of processing for New_Overloaded_Entity
7535 -- We need to look for an entity that S may override. This must be a
7536 -- homonym in the current scope, so we look for the first homonym of
7537 -- S in the current scope as the starting point for the search.
7539 E := Current_Entity_In_Scope (S);
7541 -- If there is no homonym then this is definitely not overriding
7544 Enter_Overloaded_Entity (S);
7545 Check_Dispatching_Operation (S, Empty);
7546 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7548 -- If subprogram has an explicit declaration, check whether it
7549 -- has an overriding indicator.
7551 if Comes_From_Source (S) then
7552 Check_Synchronized_Overriding (S, Overridden_Subp);
7553 Check_Overriding_Indicator
7554 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7557 -- If there is a homonym that is not overloadable, then we have an
7558 -- error, except for the special cases checked explicitly below.
7560 elsif not Is_Overloadable (E) then
7562 -- Check for spurious conflict produced by a subprogram that has the
7563 -- same name as that of the enclosing generic package. The conflict
7564 -- occurs within an instance, between the subprogram and the renaming
7565 -- declaration for the package. After the subprogram, the package
7566 -- renaming declaration becomes hidden.
7568 if Ekind (E) = E_Package
7569 and then Present (Renamed_Object (E))
7570 and then Renamed_Object (E) = Current_Scope
7571 and then Nkind (Parent (Renamed_Object (E))) =
7572 N_Package_Specification
7573 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7576 Set_Is_Immediately_Visible (E, False);
7577 Enter_Overloaded_Entity (S);
7578 Set_Homonym (S, Homonym (E));
7579 Check_Dispatching_Operation (S, Empty);
7580 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7582 -- If the subprogram is implicit it is hidden by the previous
7583 -- declaration. However if it is dispatching, it must appear in the
7584 -- dispatch table anyway, because it can be dispatched to even if it
7585 -- cannot be called directly.
7587 elsif Present (Alias (S))
7588 and then not Comes_From_Source (S)
7590 Set_Scope (S, Current_Scope);
7592 if Is_Dispatching_Operation (Alias (S)) then
7593 Check_Dispatching_Operation (S, Empty);
7599 Error_Msg_Sloc := Sloc (E);
7601 -- Generate message, with useful additional warning if in generic
7603 if Is_Generic_Unit (E) then
7604 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7605 Error_Msg_N ("\& conflicts with declaration#", S);
7607 Error_Msg_N ("& conflicts with declaration#", S);
7613 -- E exists and is overloadable
7616 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
7617 -- They are directly added to the list of primitive operations of
7618 -- Derived_Type, unless this is a rederivation in the private part
7619 -- of an operation that was already derived in the visible part of
7620 -- the current package.
7622 if Ada_Version >= Ada_05
7623 and then Present (Derived_Type)
7624 and then Is_Dispatching_Operation (Alias (S))
7625 and then Present (Find_Dispatching_Type (Alias (S)))
7626 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7628 if Type_Conformant (E, S)
7629 and then Is_Package_Or_Generic_Package (Current_Scope)
7630 and then In_Private_Part (Current_Scope)
7631 and then Parent (E) /= Parent (S)
7632 and then Alias (E) = Alias (S)
7634 Check_Operation_From_Private_View (S, E);
7636 goto Add_New_Entity;
7640 Check_Synchronized_Overriding (S, Overridden_Subp);
7642 -- Loop through E and its homonyms to determine if any of them is
7643 -- the candidate for overriding by S.
7645 while Present (E) loop
7647 -- Definitely not interesting if not in the current scope
7649 if Scope (E) /= Current_Scope then
7652 -- Check if we have type conformance
7654 elsif Type_Conformant (E, S) then
7656 -- If the old and new entities have the same profile and one
7657 -- is not the body of the other, then this is an error, unless
7658 -- one of them is implicitly declared.
7660 -- There are some cases when both can be implicit, for example
7661 -- when both a literal and a function that overrides it are
7662 -- inherited in a derivation, or when an inherited operation
7663 -- of a tagged full type overrides the inherited operation of
7664 -- a private extension. Ada 83 had a special rule for the
7665 -- literal case. In Ada95, the later implicit operation hides
7666 -- the former, and the literal is always the former. In the
7667 -- odd case where both are derived operations declared at the
7668 -- same point, both operations should be declared, and in that
7669 -- case we bypass the following test and proceed to the next
7670 -- part. This can only occur for certain obscure cases in
7671 -- instances, when an operation on a type derived from a formal
7672 -- private type does not override a homograph inherited from
7673 -- the actual. In subsequent derivations of such a type, the
7674 -- DT positions of these operations remain distinct, if they
7677 if Present (Alias (S))
7678 and then (No (Alias (E))
7679 or else Comes_From_Source (E)
7680 or else Is_Abstract_Subprogram (S)
7682 (Is_Dispatching_Operation (E)
7683 and then Is_Overriding_Alias (E, S)))
7684 and then Ekind (E) /= E_Enumeration_Literal
7686 -- When an derived operation is overloaded it may be due to
7687 -- the fact that the full view of a private extension
7688 -- re-inherits. It has to be dealt with.
7690 if Is_Package_Or_Generic_Package (Current_Scope)
7691 and then In_Private_Part (Current_Scope)
7693 Check_Operation_From_Private_View (S, E);
7696 -- In any case the implicit operation remains hidden by
7697 -- the existing declaration, which is overriding.
7699 Set_Is_Overriding_Operation (E);
7701 if Comes_From_Source (E) then
7702 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7704 -- Indicate that E overrides the operation from which
7707 if Present (Alias (S)) then
7708 Set_Overridden_Operation (E, Alias (S));
7710 Set_Overridden_Operation (E, S);
7716 -- Within an instance, the renaming declarations for actual
7717 -- subprograms may become ambiguous, but they do not hide each
7720 elsif Ekind (E) /= E_Entry
7721 and then not Comes_From_Source (E)
7722 and then not Is_Generic_Instance (E)
7723 and then (Present (Alias (E))
7724 or else Is_Intrinsic_Subprogram (E))
7725 and then (not In_Instance
7726 or else No (Parent (E))
7727 or else Nkind (Unit_Declaration_Node (E)) /=
7728 N_Subprogram_Renaming_Declaration)
7730 -- A subprogram child unit is not allowed to override an
7731 -- inherited subprogram (10.1.1(20)).
7733 if Is_Child_Unit (S) then
7735 ("child unit overrides inherited subprogram in parent",
7740 if Is_Non_Overriding_Operation (E, S) then
7741 Enter_Overloaded_Entity (S);
7743 if No (Derived_Type)
7744 or else Is_Tagged_Type (Derived_Type)
7746 Check_Dispatching_Operation (S, Empty);
7752 -- E is a derived operation or an internal operator which
7753 -- is being overridden. Remove E from further visibility.
7754 -- Furthermore, if E is a dispatching operation, it must be
7755 -- replaced in the list of primitive operations of its type
7756 -- (see Override_Dispatching_Operation).
7758 Overridden_Subp := E;
7764 Prev := First_Entity (Current_Scope);
7765 while Present (Prev)
7766 and then Next_Entity (Prev) /= E
7771 -- It is possible for E to be in the current scope and
7772 -- yet not in the entity chain. This can only occur in a
7773 -- generic context where E is an implicit concatenation
7774 -- in the formal part, because in a generic body the
7775 -- entity chain starts with the formals.
7778 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7780 -- E must be removed both from the entity_list of the
7781 -- current scope, and from the visibility chain
7783 if Debug_Flag_E then
7784 Write_Str ("Override implicit operation ");
7785 Write_Int (Int (E));
7789 -- If E is a predefined concatenation, it stands for four
7790 -- different operations. As a result, a single explicit
7791 -- declaration does not hide it. In a possible ambiguous
7792 -- situation, Disambiguate chooses the user-defined op,
7793 -- so it is correct to retain the previous internal one.
7795 if Chars (E) /= Name_Op_Concat
7796 or else Ekind (E) /= E_Operator
7798 -- For nondispatching derived operations that are
7799 -- overridden by a subprogram declared in the private
7800 -- part of a package, we retain the derived subprogram
7801 -- but mark it as not immediately visible. If the
7802 -- derived operation was declared in the visible part
7803 -- then this ensures that it will still be visible
7804 -- outside the package with the proper signature
7805 -- (calls from outside must also be directed to this
7806 -- version rather than the overriding one, unlike the
7807 -- dispatching case). Calls from inside the package
7808 -- will still resolve to the overriding subprogram
7809 -- since the derived one is marked as not visible
7810 -- within the package.
7812 -- If the private operation is dispatching, we achieve
7813 -- the overriding by keeping the implicit operation
7814 -- but setting its alias to be the overriding one. In
7815 -- this fashion the proper body is executed in all
7816 -- cases, but the original signature is used outside
7819 -- If the overriding is not in the private part, we
7820 -- remove the implicit operation altogether.
7822 if Is_Private_Declaration (S) then
7823 if not Is_Dispatching_Operation (E) then
7824 Set_Is_Immediately_Visible (E, False);
7826 -- Work done in Override_Dispatching_Operation,
7827 -- so nothing else need to be done here.
7833 -- Find predecessor of E in Homonym chain
7835 if E = Current_Entity (E) then
7838 Prev_Vis := Current_Entity (E);
7839 while Homonym (Prev_Vis) /= E loop
7840 Prev_Vis := Homonym (Prev_Vis);
7844 if Prev_Vis /= Empty then
7846 -- Skip E in the visibility chain
7848 Set_Homonym (Prev_Vis, Homonym (E));
7851 Set_Name_Entity_Id (Chars (E), Homonym (E));
7854 Set_Next_Entity (Prev, Next_Entity (E));
7856 if No (Next_Entity (Prev)) then
7857 Set_Last_Entity (Current_Scope, Prev);
7863 Enter_Overloaded_Entity (S);
7864 Set_Is_Overriding_Operation (S);
7865 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7867 -- If S is a user-defined subprogram or a null procedure
7868 -- expanded to override an inherited null procedure, then
7869 -- indicate that E overrides the operation from which S
7870 -- is inherited. It seems odd that Overridden_Operation
7871 -- isn't set in all cases where Is_Overriding_Operation
7872 -- is true, but doing so causes infinite loops in the
7873 -- compiler for implicit overriding subprograms. ???
7875 if Comes_From_Source (S)
7877 (Present (Parent (S))
7879 Nkind (Parent (S)) = N_Procedure_Specification
7881 Null_Present (Parent (S)))
7883 if Present (Alias (E)) then
7884 Set_Overridden_Operation (S, Alias (E));
7886 Set_Overridden_Operation (S, E);
7890 if Is_Dispatching_Operation (E) then
7892 -- An overriding dispatching subprogram inherits the
7893 -- convention of the overridden subprogram (by
7896 Set_Convention (S, Convention (E));
7897 Check_Dispatching_Operation (S, E);
7900 Check_Dispatching_Operation (S, Empty);
7903 Check_For_Primitive_Subprogram
7904 (Is_Primitive_Subp, Is_Overriding => True);
7905 goto Check_Inequality;
7908 -- Apparent redeclarations in instances can occur when two
7909 -- formal types get the same actual type. The subprograms in
7910 -- in the instance are legal, even if not callable from the
7911 -- outside. Calls from within are disambiguated elsewhere.
7912 -- For dispatching operations in the visible part, the usual
7913 -- rules apply, and operations with the same profile are not
7916 elsif (In_Instance_Visible_Part
7917 and then not Is_Dispatching_Operation (E))
7918 or else In_Instance_Not_Visible
7922 -- Here we have a real error (identical profile)
7925 Error_Msg_Sloc := Sloc (E);
7927 -- Avoid cascaded errors if the entity appears in
7928 -- subsequent calls.
7930 Set_Scope (S, Current_Scope);
7932 -- Generate error, with extra useful warning for the case
7933 -- of a generic instance with no completion.
7935 if Is_Generic_Instance (S)
7936 and then not Has_Completion (E)
7939 ("instantiation cannot provide body for&", S);
7940 Error_Msg_N ("\& conflicts with declaration#", S);
7942 Error_Msg_N ("& conflicts with declaration#", S);
7949 -- If one subprogram has an access parameter and the other
7950 -- a parameter of an access type, calls to either might be
7951 -- ambiguous. Verify that parameters match except for the
7952 -- access parameter.
7954 if May_Hide_Profile then
7960 F1 := First_Formal (S);
7961 F2 := First_Formal (E);
7962 while Present (F1) and then Present (F2) loop
7963 if Is_Access_Type (Etype (F1)) then
7964 if not Is_Access_Type (Etype (F2))
7965 or else not Conforming_Types
7966 (Designated_Type (Etype (F1)),
7967 Designated_Type (Etype (F2)),
7970 May_Hide_Profile := False;
7974 not Conforming_Types
7975 (Etype (F1), Etype (F2), Type_Conformant)
7977 May_Hide_Profile := False;
7988 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7999 -- On exit, we know that S is a new entity
8001 Enter_Overloaded_Entity (S);
8002 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8003 Check_Overriding_Indicator
8004 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8006 -- If S is a derived operation for an untagged type then by
8007 -- definition it's not a dispatching operation (even if the parent
8008 -- operation was dispatching), so we don't call
8009 -- Check_Dispatching_Operation in that case.
8011 if No (Derived_Type)
8012 or else Is_Tagged_Type (Derived_Type)
8014 Check_Dispatching_Operation (S, Empty);
8018 -- If this is a user-defined equality operator that is not a derived
8019 -- subprogram, create the corresponding inequality. If the operation is
8020 -- dispatching, the expansion is done elsewhere, and we do not create
8021 -- an explicit inequality operation.
8023 <<Check_Inequality>>
8024 if Chars (S) = Name_Op_Eq
8025 and then Etype (S) = Standard_Boolean
8026 and then Present (Parent (S))
8027 and then not Is_Dispatching_Operation (S)
8029 Make_Inequality_Operator (S);
8031 if Ada_Version >= Ada_12 then
8032 Check_Untagged_Equality (S);
8035 end New_Overloaded_Entity;
8037 ---------------------
8038 -- Process_Formals --
8039 ---------------------
8041 procedure Process_Formals
8043 Related_Nod : Node_Id)
8045 Param_Spec : Node_Id;
8047 Formal_Type : Entity_Id;
8051 Num_Out_Params : Nat := 0;
8052 First_Out_Param : Entity_Id := Empty;
8053 -- Used for setting Is_Only_Out_Parameter
8055 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8056 -- Determine whether an access type designates a type coming from a
8059 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8060 -- Check whether the default has a class-wide type. After analysis the
8061 -- default has the type of the formal, so we must also check explicitly
8062 -- for an access attribute.
8064 -------------------------------
8065 -- Designates_From_With_Type --
8066 -------------------------------
8068 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8069 Desig : Entity_Id := Typ;
8072 if Is_Access_Type (Desig) then
8073 Desig := Directly_Designated_Type (Desig);
8076 if Is_Class_Wide_Type (Desig) then
8077 Desig := Root_Type (Desig);
8081 Ekind (Desig) = E_Incomplete_Type
8082 and then From_With_Type (Desig);
8083 end Designates_From_With_Type;
8085 ---------------------------
8086 -- Is_Class_Wide_Default --
8087 ---------------------------
8089 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8091 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8092 or else (Nkind (D) = N_Attribute_Reference
8093 and then Attribute_Name (D) = Name_Access
8094 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8095 end Is_Class_Wide_Default;
8097 -- Start of processing for Process_Formals
8100 -- In order to prevent premature use of the formals in the same formal
8101 -- part, the Ekind is left undefined until all default expressions are
8102 -- analyzed. The Ekind is established in a separate loop at the end.
8104 Param_Spec := First (T);
8105 while Present (Param_Spec) loop
8106 Formal := Defining_Identifier (Param_Spec);
8107 Set_Never_Set_In_Source (Formal, True);
8108 Enter_Name (Formal);
8110 -- Case of ordinary parameters
8112 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8113 Find_Type (Parameter_Type (Param_Spec));
8114 Ptype := Parameter_Type (Param_Spec);
8116 if Ptype = Error then
8120 Formal_Type := Entity (Ptype);
8122 if Is_Incomplete_Type (Formal_Type)
8124 (Is_Class_Wide_Type (Formal_Type)
8125 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8127 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8128 -- primitive operations, as long as their completion is
8129 -- in the same declarative part. If in the private part
8130 -- this means that the type cannot be a Taft-amendment type.
8131 -- Check is done on package exit. For access to subprograms,
8132 -- the use is legal for Taft-amendment types.
8134 if Is_Tagged_Type (Formal_Type) then
8135 if Ekind (Scope (Current_Scope)) = E_Package
8136 and then In_Private_Part (Scope (Current_Scope))
8137 and then not From_With_Type (Formal_Type)
8138 and then not Is_Class_Wide_Type (Formal_Type)
8141 (Parent (T), N_Access_Function_Definition,
8142 N_Access_Procedure_Definition)
8146 Private_Dependents (Base_Type (Formal_Type)));
8150 -- Special handling of Value_Type for CIL case
8152 elsif Is_Value_Type (Formal_Type) then
8155 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8156 N_Access_Procedure_Definition)
8159 ("invalid use of incomplete type&",
8160 Param_Spec, Formal_Type);
8162 -- Further checks on the legality of incomplete types
8163 -- in formal parts must be delayed until the freeze point
8164 -- of the enclosing subprogram or access to subprogram.
8167 elsif Ekind (Formal_Type) = E_Void then
8168 Error_Msg_NE ("premature use of&",
8169 Parameter_Type (Param_Spec), Formal_Type);
8172 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8173 -- declaration corresponding to the null-excluding type of the
8174 -- formal in the enclosing scope. Finally, replace the parameter
8175 -- type of the formal with the internal subtype.
8177 if Ada_Version >= Ada_05
8178 and then Null_Exclusion_Present (Param_Spec)
8180 if not Is_Access_Type (Formal_Type) then
8182 ("`NOT NULL` allowed only for an access type", Param_Spec);
8185 if Can_Never_Be_Null (Formal_Type)
8186 and then Comes_From_Source (Related_Nod)
8189 ("`NOT NULL` not allowed (& already excludes null)",
8195 Create_Null_Excluding_Itype
8197 Related_Nod => Related_Nod,
8198 Scope_Id => Scope (Current_Scope));
8200 -- If the designated type of the itype is an itype we
8201 -- decorate it with the Has_Delayed_Freeze attribute to
8202 -- avoid problems with the backend.
8205 -- type T is access procedure;
8206 -- procedure Op (O : not null T);
8208 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8209 Set_Has_Delayed_Freeze (Formal_Type);
8214 -- An access formal type
8218 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8220 -- No need to continue if we already notified errors
8222 if not Present (Formal_Type) then
8226 -- Ada 2005 (AI-254)
8229 AD : constant Node_Id :=
8230 Access_To_Subprogram_Definition
8231 (Parameter_Type (Param_Spec));
8233 if Present (AD) and then Protected_Present (AD) then
8235 Replace_Anonymous_Access_To_Protected_Subprogram
8241 Set_Etype (Formal, Formal_Type);
8242 Default := Expression (Param_Spec);
8244 if Present (Default) then
8245 if Out_Present (Param_Spec) then
8247 ("default initialization only allowed for IN parameters",
8251 -- Do the special preanalysis of the expression (see section on
8252 -- "Handling of Default Expressions" in the spec of package Sem).
8254 Preanalyze_Spec_Expression (Default, Formal_Type);
8256 -- An access to constant cannot be the default for
8257 -- an access parameter that is an access to variable.
8259 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8260 and then not Is_Access_Constant (Formal_Type)
8261 and then Is_Access_Type (Etype (Default))
8262 and then Is_Access_Constant (Etype (Default))
8265 ("formal that is access to variable cannot be initialized " &
8266 "with an access-to-constant expression", Default);
8269 -- Check that the designated type of an access parameter's default
8270 -- is not a class-wide type unless the parameter's designated type
8271 -- is also class-wide.
8273 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8274 and then not Designates_From_With_Type (Formal_Type)
8275 and then Is_Class_Wide_Default (Default)
8276 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8279 ("access to class-wide expression not allowed here", Default);
8282 -- Check incorrect use of dynamically tagged expressions
8284 if Is_Tagged_Type (Formal_Type) then
8285 Check_Dynamically_Tagged_Expression
8288 Related_Nod => Default);
8292 -- Ada 2005 (AI-231): Static checks
8294 if Ada_Version >= Ada_05
8295 and then Is_Access_Type (Etype (Formal))
8296 and then Can_Never_Be_Null (Etype (Formal))
8298 Null_Exclusion_Static_Checks (Param_Spec);
8305 -- If this is the formal part of a function specification, analyze the
8306 -- subtype mark in the context where the formals are visible but not
8307 -- yet usable, and may hide outer homographs.
8309 if Nkind (Related_Nod) = N_Function_Specification then
8310 Analyze_Return_Type (Related_Nod);
8313 -- Now set the kind (mode) of each formal
8315 Param_Spec := First (T);
8317 while Present (Param_Spec) loop
8318 Formal := Defining_Identifier (Param_Spec);
8319 Set_Formal_Mode (Formal);
8321 if Ekind (Formal) = E_In_Parameter then
8322 Set_Default_Value (Formal, Expression (Param_Spec));
8324 if Present (Expression (Param_Spec)) then
8325 Default := Expression (Param_Spec);
8327 if Is_Scalar_Type (Etype (Default)) then
8329 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8331 Formal_Type := Entity (Parameter_Type (Param_Spec));
8334 Formal_Type := Access_Definition
8335 (Related_Nod, Parameter_Type (Param_Spec));
8338 Apply_Scalar_Range_Check (Default, Formal_Type);
8342 elsif Ekind (Formal) = E_Out_Parameter then
8343 Num_Out_Params := Num_Out_Params + 1;
8345 if Num_Out_Params = 1 then
8346 First_Out_Param := Formal;
8349 elsif Ekind (Formal) = E_In_Out_Parameter then
8350 Num_Out_Params := Num_Out_Params + 1;
8356 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8357 Set_Is_Only_Out_Parameter (First_Out_Param);
8359 end Process_Formals;
8365 procedure Process_PPCs
8367 Spec_Id : Entity_Id;
8368 Body_Id : Entity_Id)
8370 Loc : constant Source_Ptr := Sloc (N);
8372 Plist : List_Id := No_List;
8376 function Grab_PPC (Nam : Name_Id) return Node_Id;
8377 -- Prag contains an analyzed precondition or postcondition pragma.
8378 -- This function copies the pragma, changes it to the corresponding
8379 -- Check pragma and returns the Check pragma as the result. The
8380 -- argument Nam is either Name_Precondition or Name_Postcondition.
8386 function Grab_PPC (Nam : Name_Id) return Node_Id is
8387 CP : constant Node_Id := New_Copy_Tree (Prag);
8390 -- Set Analyzed to false, since we want to reanalyze the check
8391 -- procedure. Note that it is only at the outer level that we
8392 -- do this fiddling, for the spec cases, the already preanalyzed
8393 -- parameters are not affected.
8395 -- For a postcondition pragma within a generic, preserve the pragma
8396 -- for later expansion.
8398 Set_Analyzed (CP, False);
8400 if Nam = Name_Postcondition
8401 and then not Expander_Active
8406 -- Change pragma into corresponding pragma Check
8408 Prepend_To (Pragma_Argument_Associations (CP),
8409 Make_Pragma_Argument_Association (Sloc (Prag),
8411 Make_Identifier (Loc,
8413 Set_Pragma_Identifier (CP,
8414 Make_Identifier (Sloc (Prag),
8415 Chars => Name_Check));
8420 -- Start of processing for Process_PPCs
8423 -- Nothing to do if we are not generating code
8425 if Operating_Mode /= Generate_Code then
8429 -- Grab preconditions from spec
8431 if Present (Spec_Id) then
8433 -- Loop through PPC pragmas from spec. Note that preconditions from
8434 -- the body will be analyzed and converted when we scan the body
8435 -- declarations below.
8437 Prag := Spec_PPC_List (Spec_Id);
8438 while Present (Prag) loop
8439 if Pragma_Name (Prag) = Name_Precondition
8440 and then Pragma_Enabled (Prag)
8442 -- Add pragma Check at the start of the declarations of N.
8443 -- Note that this processing reverses the order of the list,
8444 -- which is what we want since new entries were chained to
8445 -- the head of the list.
8447 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
8450 Prag := Next_Pragma (Prag);
8454 -- Build postconditions procedure if needed and prepend the following
8455 -- declaration to the start of the declarations for the subprogram.
8457 -- procedure _postconditions [(_Result : resulttype)] is
8459 -- pragma Check (Postcondition, condition [,message]);
8460 -- pragma Check (Postcondition, condition [,message]);
8464 -- First we deal with the postconditions in the body
8466 if Is_Non_Empty_List (Declarations (N)) then
8468 -- Loop through declarations
8470 Prag := First (Declarations (N));
8471 while Present (Prag) loop
8472 if Nkind (Prag) = N_Pragma then
8474 -- If pragma, capture if enabled postcondition, else ignore
8476 if Pragma_Name (Prag) = Name_Postcondition
8477 and then Check_Enabled (Name_Postcondition)
8479 if Plist = No_List then
8480 Plist := Empty_List;
8485 -- If expansion is disabled, as in a generic unit,
8486 -- save pragma for later expansion.
8488 if not Expander_Active then
8489 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8491 Append (Grab_PPC (Name_Postcondition), Plist);
8497 -- Not a pragma, if comes from source, then end scan
8499 elsif Comes_From_Source (Prag) then
8502 -- Skip stuff not coming from source
8510 -- Now deal with any postconditions from the spec
8512 if Present (Spec_Id) then
8514 -- Loop through PPC pragmas from spec
8516 Prag := Spec_PPC_List (Spec_Id);
8517 while Present (Prag) loop
8518 if Pragma_Name (Prag) = Name_Postcondition
8519 and then Pragma_Enabled (Prag)
8521 if Plist = No_List then
8522 Plist := Empty_List;
8525 if not Expander_Active then
8526 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8528 Append (Grab_PPC (Name_Postcondition), Plist);
8532 Prag := Next_Pragma (Prag);
8536 -- If we had any postconditions and expansion is enabled, build
8537 -- the _Postconditions procedure.
8540 and then Expander_Active
8542 Subp := Defining_Entity (N);
8544 if Etype (Subp) /= Standard_Void_Type then
8546 Make_Parameter_Specification (Loc,
8547 Defining_Identifier =>
8548 Make_Defining_Identifier (Loc,
8549 Chars => Name_uResult),
8550 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8556 Post_Proc : constant Entity_Id :=
8557 Make_Defining_Identifier (Loc,
8558 Chars => Name_uPostconditions);
8559 -- The entity for the _Postconditions procedure
8561 Prepend_To (Declarations (N),
8562 Make_Subprogram_Body (Loc,
8564 Make_Procedure_Specification (Loc,
8565 Defining_Unit_Name => Post_Proc,
8566 Parameter_Specifications => Parms),
8568 Declarations => Empty_List,
8570 Handled_Statement_Sequence =>
8571 Make_Handled_Sequence_Of_Statements (Loc,
8572 Statements => Plist)));
8574 -- If this is a procedure, set the Postcondition_Proc attribute on
8575 -- the proper defining entity for the subprogram.
8577 if Etype (Subp) = Standard_Void_Type then
8578 if Present (Spec_Id) then
8579 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8581 Set_Postcondition_Proc (Body_Id, Post_Proc);
8586 if Present (Spec_Id) then
8587 Set_Has_Postconditions (Spec_Id);
8589 Set_Has_Postconditions (Body_Id);
8594 ----------------------------
8595 -- Reference_Body_Formals --
8596 ----------------------------
8598 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8603 if Error_Posted (Spec) then
8607 -- Iterate over both lists. They may be of different lengths if the two
8608 -- specs are not conformant.
8610 Fs := First_Formal (Spec);
8611 Fb := First_Formal (Bod);
8612 while Present (Fs) and then Present (Fb) loop
8613 Generate_Reference (Fs, Fb, 'b');
8616 Style.Check_Identifier (Fb, Fs);
8619 Set_Spec_Entity (Fb, Fs);
8620 Set_Referenced (Fs, False);
8624 end Reference_Body_Formals;
8626 -------------------------
8627 -- Set_Actual_Subtypes --
8628 -------------------------
8630 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8631 Loc : constant Source_Ptr := Sloc (N);
8635 First_Stmt : Node_Id := Empty;
8636 AS_Needed : Boolean;
8639 -- If this is an empty initialization procedure, no need to create
8640 -- actual subtypes (small optimization).
8642 if Ekind (Subp) = E_Procedure
8643 and then Is_Null_Init_Proc (Subp)
8648 Formal := First_Formal (Subp);
8649 while Present (Formal) loop
8650 T := Etype (Formal);
8652 -- We never need an actual subtype for a constrained formal
8654 if Is_Constrained (T) then
8657 -- If we have unknown discriminants, then we do not need an actual
8658 -- subtype, or more accurately we cannot figure it out! Note that
8659 -- all class-wide types have unknown discriminants.
8661 elsif Has_Unknown_Discriminants (T) then
8664 -- At this stage we have an unconstrained type that may need an
8665 -- actual subtype. For sure the actual subtype is needed if we have
8666 -- an unconstrained array type.
8668 elsif Is_Array_Type (T) then
8671 -- The only other case needing an actual subtype is an unconstrained
8672 -- record type which is an IN parameter (we cannot generate actual
8673 -- subtypes for the OUT or IN OUT case, since an assignment can
8674 -- change the discriminant values. However we exclude the case of
8675 -- initialization procedures, since discriminants are handled very
8676 -- specially in this context, see the section entitled "Handling of
8677 -- Discriminants" in Einfo.
8679 -- We also exclude the case of Discrim_SO_Functions (functions used
8680 -- in front end layout mode for size/offset values), since in such
8681 -- functions only discriminants are referenced, and not only are such
8682 -- subtypes not needed, but they cannot always be generated, because
8683 -- of order of elaboration issues.
8685 elsif Is_Record_Type (T)
8686 and then Ekind (Formal) = E_In_Parameter
8687 and then Chars (Formal) /= Name_uInit
8688 and then not Is_Unchecked_Union (T)
8689 and then not Is_Discrim_SO_Function (Subp)
8693 -- All other cases do not need an actual subtype
8699 -- Generate actual subtypes for unconstrained arrays and
8700 -- unconstrained discriminated records.
8703 if Nkind (N) = N_Accept_Statement then
8705 -- If expansion is active, The formal is replaced by a local
8706 -- variable that renames the corresponding entry of the
8707 -- parameter block, and it is this local variable that may
8708 -- require an actual subtype.
8710 if Expander_Active then
8711 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8713 Decl := Build_Actual_Subtype (T, Formal);
8716 if Present (Handled_Statement_Sequence (N)) then
8718 First (Statements (Handled_Statement_Sequence (N)));
8719 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8720 Mark_Rewrite_Insertion (Decl);
8722 -- If the accept statement has no body, there will be no
8723 -- reference to the actuals, so no need to compute actual
8730 Decl := Build_Actual_Subtype (T, Formal);
8731 Prepend (Decl, Declarations (N));
8732 Mark_Rewrite_Insertion (Decl);
8735 -- The declaration uses the bounds of an existing object, and
8736 -- therefore needs no constraint checks.
8738 Analyze (Decl, Suppress => All_Checks);
8740 -- We need to freeze manually the generated type when it is
8741 -- inserted anywhere else than in a declarative part.
8743 if Present (First_Stmt) then
8744 Insert_List_Before_And_Analyze (First_Stmt,
8745 Freeze_Entity (Defining_Identifier (Decl), Loc));
8748 if Nkind (N) = N_Accept_Statement
8749 and then Expander_Active
8751 Set_Actual_Subtype (Renamed_Object (Formal),
8752 Defining_Identifier (Decl));
8754 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8758 Next_Formal (Formal);
8760 end Set_Actual_Subtypes;
8762 ---------------------
8763 -- Set_Formal_Mode --
8764 ---------------------
8766 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8767 Spec : constant Node_Id := Parent (Formal_Id);
8770 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8771 -- since we ensure that corresponding actuals are always valid at the
8772 -- point of the call.
8774 if Out_Present (Spec) then
8775 if Ekind (Scope (Formal_Id)) = E_Function
8776 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8778 Error_Msg_N ("functions can only have IN parameters", Spec);
8779 Set_Ekind (Formal_Id, E_In_Parameter);
8781 elsif In_Present (Spec) then
8782 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8785 Set_Ekind (Formal_Id, E_Out_Parameter);
8786 Set_Never_Set_In_Source (Formal_Id, True);
8787 Set_Is_True_Constant (Formal_Id, False);
8788 Set_Current_Value (Formal_Id, Empty);
8792 Set_Ekind (Formal_Id, E_In_Parameter);
8795 -- Set Is_Known_Non_Null for access parameters since the language
8796 -- guarantees that access parameters are always non-null. We also set
8797 -- Can_Never_Be_Null, since there is no way to change the value.
8799 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8801 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8802 -- null; In Ada 2005, only if then null_exclusion is explicit.
8804 if Ada_Version < Ada_05
8805 or else Can_Never_Be_Null (Etype (Formal_Id))
8807 Set_Is_Known_Non_Null (Formal_Id);
8808 Set_Can_Never_Be_Null (Formal_Id);
8811 -- Ada 2005 (AI-231): Null-exclusion access subtype
8813 elsif Is_Access_Type (Etype (Formal_Id))
8814 and then Can_Never_Be_Null (Etype (Formal_Id))
8816 Set_Is_Known_Non_Null (Formal_Id);
8819 Set_Mechanism (Formal_Id, Default_Mechanism);
8820 Set_Formal_Validity (Formal_Id);
8821 end Set_Formal_Mode;
8823 -------------------------
8824 -- Set_Formal_Validity --
8825 -------------------------
8827 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8829 -- If no validity checking, then we cannot assume anything about the
8830 -- validity of parameters, since we do not know there is any checking
8831 -- of the validity on the call side.
8833 if not Validity_Checks_On then
8836 -- If validity checking for parameters is enabled, this means we are
8837 -- not supposed to make any assumptions about argument values.
8839 elsif Validity_Check_Parameters then
8842 -- If we are checking in parameters, we will assume that the caller is
8843 -- also checking parameters, so we can assume the parameter is valid.
8845 elsif Ekind (Formal_Id) = E_In_Parameter
8846 and then Validity_Check_In_Params
8848 Set_Is_Known_Valid (Formal_Id, True);
8850 -- Similar treatment for IN OUT parameters
8852 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8853 and then Validity_Check_In_Out_Params
8855 Set_Is_Known_Valid (Formal_Id, True);
8857 end Set_Formal_Validity;
8859 ------------------------
8860 -- Subtype_Conformant --
8861 ------------------------
8863 function Subtype_Conformant
8864 (New_Id : Entity_Id;
8866 Skip_Controlling_Formals : Boolean := False) return Boolean
8870 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8871 Skip_Controlling_Formals => Skip_Controlling_Formals);
8873 end Subtype_Conformant;
8875 ---------------------
8876 -- Type_Conformant --
8877 ---------------------
8879 function Type_Conformant
8880 (New_Id : Entity_Id;
8882 Skip_Controlling_Formals : Boolean := False) return Boolean
8886 May_Hide_Profile := False;
8889 (New_Id, Old_Id, Type_Conformant, False, Result,
8890 Skip_Controlling_Formals => Skip_Controlling_Formals);
8892 end Type_Conformant;
8894 -------------------------------
8895 -- Valid_Operator_Definition --
8896 -------------------------------
8898 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8901 Id : constant Name_Id := Chars (Designator);
8905 F := First_Formal (Designator);
8906 while Present (F) loop
8909 if Present (Default_Value (F)) then
8911 ("default values not allowed for operator parameters",
8918 -- Verify that user-defined operators have proper number of arguments
8919 -- First case of operators which can only be unary
8922 or else Id = Name_Op_Abs
8926 -- Case of operators which can be unary or binary
8928 elsif Id = Name_Op_Add
8929 or Id = Name_Op_Subtract
8931 N_OK := (N in 1 .. 2);
8933 -- All other operators can only be binary
8941 ("incorrect number of arguments for operator", Designator);
8945 and then Base_Type (Etype (Designator)) = Standard_Boolean
8946 and then not Is_Intrinsic_Subprogram (Designator)
8949 ("explicit definition of inequality not allowed", Designator);
8951 end Valid_Operator_Definition;