1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib.Xref; use Lib.Xref;
46 with Layout; use Layout;
47 with Namet; use Namet;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
52 with Output; use Output;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch3; use Sem_Ch3;
60 with Sem_Ch4; use Sem_Ch4;
61 with Sem_Ch5; use Sem_Ch5;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch9; use Sem_Ch9;
64 with Sem_Ch10; use Sem_Ch10;
65 with Sem_Ch12; use Sem_Ch12;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Mech; use Sem_Mech;
73 with Sem_Prag; use Sem_Prag;
74 with Sem_Res; use Sem_Res;
75 with Sem_Util; use Sem_Util;
76 with Sem_Type; use Sem_Type;
77 with Sem_Warn; use Sem_Warn;
78 with Sinput; use Sinput;
79 with Stand; use Stand;
80 with Sinfo; use Sinfo;
81 with Sinfo.CN; use Sinfo.CN;
82 with Snames; use Snames;
83 with Stringt; use Stringt;
85 with Stylesw; use Stylesw;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Urealp; use Urealp;
89 with Validsw; use Validsw;
91 package body Sem_Ch6 is
93 May_Hide_Profile : Boolean := False;
94 -- This flag is used to indicate that two formals in two subprograms being
95 -- checked for conformance differ only in that one is an access parameter
96 -- while the other is of a general access type with the same designated
97 -- type. In this case, if the rest of the signatures match, a call to
98 -- either subprogram may be ambiguous, which is worth a warning. The flag
99 -- is set in Compatible_Types, and the warning emitted in
100 -- New_Overloaded_Entity.
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
106 procedure Analyze_Function_Return (N : Node_Id);
107 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
108 -- applies to a [generic] function.
110 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
111 -- Analyze a generic subprogram body. N is the body to be analyzed, and
112 -- Gen_Id is the defining entity Id for the corresponding spec.
114 procedure Analyze_Null_Procedure
116 Is_Completion : out Boolean);
117 -- A null procedure can be a declaration or (Ada 2012) a completion
119 procedure Analyze_Return_Statement (N : Node_Id);
120 -- Common processing for simple and extended return statements
122 procedure Analyze_Return_Type (N : Node_Id);
123 -- Subsidiary to Process_Formals: analyze subtype mark in function
124 -- specification in a context where the formals are visible and hide
127 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
128 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
129 -- that we can use RETURN but not skip the debug output at the end.
131 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
132 -- Returns true if Subp can override a predefined operator.
134 procedure Check_Conformance
137 Ctype : Conformance_Type;
139 Conforms : out Boolean;
140 Err_Loc : Node_Id := Empty;
141 Get_Inst : Boolean := False;
142 Skip_Controlling_Formals : Boolean := False);
143 -- Given two entities, this procedure checks that the profiles associated
144 -- with these entities meet the conformance criterion given by the third
145 -- parameter. If they conform, Conforms is set True and control returns
146 -- to the caller. If they do not conform, Conforms is set to False, and
147 -- in addition, if Errmsg is True on the call, proper messages are output
148 -- to complain about the conformance failure. If Err_Loc is non_Empty
149 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
150 -- error messages are placed on the appropriate part of the construct
151 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
152 -- against a formal access-to-subprogram type so Get_Instance_Of must
155 procedure Check_Limited_Return
159 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
160 -- types. Used only for simple return statements. Expr is the expression
163 procedure Check_Subprogram_Order (N : Node_Id);
164 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
165 -- the alpha ordering rule for N if this ordering requirement applicable.
167 procedure Check_Returns
171 Proc : Entity_Id := Empty);
172 -- Called to check for missing return statements in a function body, or for
173 -- returns present in a procedure body which has No_Return set. HSS is the
174 -- handled statement sequence for the subprogram body. This procedure
175 -- checks all flow paths to make sure they either have return (Mode = 'F',
176 -- used for functions) or do not have a return (Mode = 'P', used for
177 -- No_Return procedures). The flag Err is set if there are any control
178 -- paths not explicitly terminated by a return in the function case, and is
179 -- True otherwise. Proc is the entity for the procedure case and is used
180 -- in posting the warning message.
182 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
183 -- In Ada 2012, a primitive equality operator on an untagged record type
184 -- must appear before the type is frozen, and have the same visibility as
185 -- that of the type. This procedure checks that this rule is met, and
186 -- otherwise emits an error on the subprogram declaration and a warning
187 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
188 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
189 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
190 -- is set, otherwise the call has no effect.
192 procedure Enter_Overloaded_Entity (S : Entity_Id);
193 -- This procedure makes S, a new overloaded entity, into the first visible
194 -- entity with that name.
196 function Is_Non_Overriding_Operation
198 New_E : Entity_Id) return Boolean;
199 -- Enforce the rule given in 12.3(18): a private operation in an instance
200 -- overrides an inherited operation only if the corresponding operation
201 -- was overriding in the generic. This needs to be checked for primitive
202 -- operations of types derived (in the generic unit) from formal private
203 -- or formal derived types.
205 procedure Make_Inequality_Operator (S : Entity_Id);
206 -- Create the declaration for an inequality operator that is implicitly
207 -- created by a user-defined equality operator that yields a boolean.
209 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
210 -- Preanalysis of default expressions of subprogram formals. N is the
211 -- expression to be analyzed and T is the expected type.
213 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
214 -- Formal_Id is an formal parameter entity. This procedure deals with
215 -- setting the proper validity status for this entity, which depends on
216 -- the kind of parameter and the validity checking mode.
218 ---------------------------------------------
219 -- Analyze_Abstract_Subprogram_Declaration --
220 ---------------------------------------------
222 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
223 Scop : constant Entity_Id := Current_Scope;
224 Subp_Id : constant Entity_Id :=
225 Analyze_Subprogram_Specification (Specification (N));
228 Generate_Definition (Subp_Id);
230 -- Set the SPARK mode from the current context (may be overwritten later
231 -- with explicit pragma).
233 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
234 Set_SPARK_Pragma_Inherited (Subp_Id);
236 -- Preserve relevant elaboration-related attributes of the context which
237 -- are no longer available or very expensive to recompute once analysis,
238 -- resolution, and expansion are over.
240 Mark_Elaboration_Attributes
245 Set_Is_Abstract_Subprogram (Subp_Id);
246 New_Overloaded_Entity (Subp_Id);
247 Check_Delayed_Subprogram (Subp_Id);
249 Set_Categorization_From_Scope (Subp_Id, Scop);
251 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
252 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
254 -- Issue a warning if the abstract subprogram is neither a dispatching
255 -- operation nor an operation that overrides an inherited subprogram or
256 -- predefined operator, since this most likely indicates a mistake.
258 elsif Warn_On_Redundant_Constructs
259 and then not Is_Dispatching_Operation (Subp_Id)
260 and then not Present (Overridden_Operation (Subp_Id))
261 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
262 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
265 ("abstract subprogram is not dispatching or overriding?r?", N);
268 Generate_Reference_To_Formals (Subp_Id);
269 Check_Eliminated (Subp_Id);
271 if Has_Aspects (N) then
272 Analyze_Aspect_Specifications (N, Subp_Id);
274 end Analyze_Abstract_Subprogram_Declaration;
276 ---------------------------------
277 -- Analyze_Expression_Function --
278 ---------------------------------
280 procedure Analyze_Expression_Function (N : Node_Id) is
281 Expr : constant Node_Id := Expression (N);
282 Loc : constant Source_Ptr := Sloc (N);
283 LocX : constant Source_Ptr := Sloc (Expr);
284 Spec : constant Node_Id := Specification (N);
294 Def_Id : Entity_Id := Empty;
296 -- If the expression is a completion, Prev is the entity whose
297 -- declaration is completed. Def_Id is needed to analyze the spec.
299 -- Start of processing for Analyze_Expression_Function
302 -- This is one of the occasions on which we transform the tree during
303 -- semantic analysis. If this is a completion, transform the expression
304 -- function into an equivalent subprogram body, and analyze it.
306 -- Expression functions are inlined unconditionally. The back-end will
307 -- determine whether this is possible.
309 Inline_Processing_Required := True;
311 -- Create a specification for the generated body. This must be done
312 -- prior to the analysis of the initial declaration.
314 New_Spec := Copy_Subprogram_Spec (Spec);
315 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
317 -- If there are previous overloadable entities with the same name,
318 -- check whether any of them is completed by the expression function.
319 -- In a generic context a formal subprogram has no completion.
322 and then Is_Overloadable (Prev)
323 and then not Is_Formal_Subprogram (Prev)
325 Def_Id := Analyze_Subprogram_Specification (Spec);
326 Prev := Find_Corresponding_Spec (N);
328 -- The previous entity may be an expression function as well, in
329 -- which case the redeclaration is illegal.
332 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
333 N_Expression_Function
335 Error_Msg_Sloc := Sloc (Prev);
336 Error_Msg_N ("& conflicts with declaration#", Def_Id);
341 Ret := Make_Simple_Return_Statement (LocX, Expr);
344 Make_Subprogram_Body (Loc,
345 Specification => New_Spec,
346 Declarations => Empty_List,
347 Handled_Statement_Sequence =>
348 Make_Handled_Sequence_Of_Statements (LocX,
349 Statements => New_List (Ret)));
350 Set_Was_Expression_Function (New_Body);
352 -- If the expression completes a generic subprogram, we must create a
353 -- separate node for the body, because at instantiation the original
354 -- node of the generic copy must be a generic subprogram body, and
355 -- cannot be a expression function. Otherwise we just rewrite the
356 -- expression with the non-generic body.
358 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
359 Insert_After (N, New_Body);
361 -- Propagate any aspects or pragmas that apply to the expression
362 -- function to the proper body when the expression function acts
365 if Has_Aspects (N) then
366 Move_Aspects (N, To => New_Body);
369 Relocate_Pragmas_To_Body (New_Body);
371 Rewrite (N, Make_Null_Statement (Loc));
372 Set_Has_Completion (Prev, False);
375 Set_Is_Inlined (Prev);
377 -- If the expression function is a completion, the previous declaration
378 -- must come from source. We know already that it appears in the current
379 -- scope. The entity itself may be internally created if within a body
383 and then Is_Overloadable (Prev)
384 and then not Is_Formal_Subprogram (Prev)
385 and then Comes_From_Source (Parent (Prev))
387 Set_Has_Completion (Prev, False);
388 Set_Is_Inlined (Prev);
390 -- AI12-0103: Expression functions that are a completion freeze their
391 -- expression but don't freeze anything else (unlike regular bodies).
393 -- Note that we cannot defer this freezing to the analysis of the
394 -- expression itself, because a freeze node might appear in a nested
395 -- scope, leading to an elaboration order issue in gigi.
396 -- As elsewhere, we do not emit freeze nodes within a generic unit.
398 if not Inside_A_Generic then
401 Typ => Etype (Def_Id),
406 -- For navigation purposes, indicate that the function is a body
408 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
409 Rewrite (N, New_Body);
411 -- Remove any existing aspects from the original node because the act
412 -- of rewriting causes the list to be shared between the two nodes.
414 Orig_N := Original_Node (N);
415 Remove_Aspects (Orig_N);
417 -- Propagate any pragmas that apply to expression function to the
418 -- proper body when the expression function acts as a completion.
419 -- Aspects are automatically transfered because of node rewriting.
421 Relocate_Pragmas_To_Body (N);
424 -- Prev is the previous entity with the same name, but it is can
425 -- be an unrelated spec that is not completed by the expression
426 -- function. In that case the relevant entity is the one in the body.
427 -- Not clear that the backend can inline it in this case ???
429 if Has_Completion (Prev) then
431 -- The formals of the expression function are body formals,
432 -- and do not appear in the ali file, which will only contain
433 -- references to the formals of the original subprogram spec.
440 F1 := First_Formal (Def_Id);
441 F2 := First_Formal (Prev);
443 while Present (F1) loop
444 Set_Spec_Entity (F1, F2);
451 Set_Is_Inlined (Defining_Entity (New_Body));
454 -- If this is not a completion, create both a declaration and a body, so
455 -- that the expression can be inlined whenever possible.
458 -- An expression function that is not a completion is not a
459 -- subprogram declaration, and thus cannot appear in a protected
462 if Nkind (Parent (N)) = N_Protected_Definition then
464 ("an expression function is not a legal protected operation", N);
467 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
469 -- Remove any existing aspects from the original node because the act
470 -- of rewriting causes the list to be shared between the two nodes.
472 Orig_N := Original_Node (N);
473 Remove_Aspects (Orig_N);
477 -- If aspect SPARK_Mode was specified on the body, it needs to be
478 -- repeated both on the generated spec and the body.
480 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
482 if Present (Asp) then
483 Asp := New_Copy_Tree (Asp);
484 Set_Analyzed (Asp, False);
485 Set_Aspect_Specifications (New_Body, New_List (Asp));
488 Def_Id := Defining_Entity (N);
489 Set_Is_Inlined (Def_Id);
491 -- Establish the linkages between the spec and the body. These are
492 -- used when the expression function acts as the prefix of attribute
493 -- 'Access in order to freeze the original expression which has been
494 -- moved to the generated body.
496 Set_Corresponding_Body (N, Defining_Entity (New_Body));
497 Set_Corresponding_Spec (New_Body, Def_Id);
499 -- Within a generic preanalyze the original expression for name
500 -- capture. The body is also generated but plays no role in
501 -- this because it is not part of the original source.
503 if Inside_A_Generic then
504 Set_Has_Completion (Def_Id);
506 Install_Formals (Def_Id);
507 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
511 -- To prevent premature freeze action, insert the new body at the end
512 -- of the current declarations, or at the end of the package spec.
513 -- However, resolve usage names now, to prevent spurious visibility
514 -- on later entities. Note that the function can now be called in
515 -- the current declarative part, which will appear to be prior to
516 -- the presence of the body in the code. There are nevertheless no
517 -- order of elaboration issues because all name resolution has taken
518 -- place at the point of declaration.
521 Decls : List_Id := List_Containing (N);
522 Expr : constant Node_Id := Expression (Ret);
523 Par : constant Node_Id := Parent (Decls);
524 Typ : constant Entity_Id := Etype (Def_Id);
527 -- If this is a wrapper created for in an instance for a formal
528 -- subprogram, insert body after declaration, to be analyzed when
529 -- the enclosing instance is analyzed.
532 and then Is_Generic_Actual_Subprogram (Def_Id)
534 Insert_After (N, New_Body);
537 if Nkind (Par) = N_Package_Specification
538 and then Decls = Visible_Declarations (Par)
539 and then Present (Private_Declarations (Par))
540 and then not Is_Empty_List (Private_Declarations (Par))
542 Decls := Private_Declarations (Par);
545 Insert_After (Last (Decls), New_Body);
547 -- Preanalyze the expression if not already done above
549 if not Inside_A_Generic then
551 Install_Formals (Def_Id);
552 Preanalyze_Formal_Expression (Expr, Typ);
553 Check_Limited_Return (Original_Node (N), Expr, Typ);
560 -- Check incorrect use of dynamically tagged expression. This doesn't
561 -- fall out automatically when analyzing the generated function body,
562 -- because Check_Dynamically_Tagged_Expression deliberately ignores
563 -- nodes that don't come from source.
566 and then Nkind (Def_Id) in N_Has_Etype
567 and then Is_Tagged_Type (Etype (Def_Id))
569 Check_Dynamically_Tagged_Expression
571 Typ => Etype (Def_Id),
572 Related_Nod => Original_Node (N));
575 -- We must enforce checks for unreferenced formals in our newly
576 -- generated function, so we propagate the referenced flag from the
577 -- original spec to the new spec as well as setting Comes_From_Source.
579 if Present (Parameter_Specifications (New_Spec)) then
581 Form_New_Def : Entity_Id;
582 Form_New_Spec : Entity_Id;
583 Form_Old_Def : Entity_Id;
584 Form_Old_Spec : Entity_Id;
587 Form_New_Spec := First (Parameter_Specifications (New_Spec));
588 Form_Old_Spec := First (Parameter_Specifications (Spec));
590 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
591 Form_New_Def := Defining_Identifier (Form_New_Spec);
592 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
594 Set_Comes_From_Source (Form_New_Def, True);
596 -- Because of the usefulness of unreferenced controlling
597 -- formals we exempt them from unreferenced warnings by marking
598 -- them as always referenced.
600 Set_Referenced (Form_Old_Def,
601 (Is_Formal (Form_Old_Def)
602 and then Is_Controlling_Formal (Form_Old_Def))
603 or else Referenced (Form_Old_Def));
605 Next (Form_New_Spec);
606 Next (Form_Old_Spec);
610 end Analyze_Expression_Function;
612 ----------------------------------------
613 -- Analyze_Extended_Return_Statement --
614 ----------------------------------------
616 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
618 Check_Compiler_Unit ("extended return statement", N);
619 Analyze_Return_Statement (N);
620 end Analyze_Extended_Return_Statement;
622 ----------------------------
623 -- Analyze_Function_Call --
624 ----------------------------
626 procedure Analyze_Function_Call (N : Node_Id) is
627 Actuals : constant List_Id := Parameter_Associations (N);
628 Func_Nam : constant Node_Id := Name (N);
634 -- A call of the form A.B (X) may be an Ada 2005 call, which is
635 -- rewritten as B (A, X). If the rewriting is successful, the call
636 -- has been analyzed and we just return.
638 if Nkind (Func_Nam) = N_Selected_Component
639 and then Name (N) /= Func_Nam
640 and then Is_Rewrite_Substitution (N)
641 and then Present (Etype (N))
646 -- If error analyzing name, then set Any_Type as result type and return
648 if Etype (Func_Nam) = Any_Type then
649 Set_Etype (N, Any_Type);
653 -- Otherwise analyze the parameters
655 if Present (Actuals) then
656 Actual := First (Actuals);
657 while Present (Actual) loop
659 Check_Parameterless_Call (Actual);
665 end Analyze_Function_Call;
667 -----------------------------
668 -- Analyze_Function_Return --
669 -----------------------------
671 procedure Analyze_Function_Return (N : Node_Id) is
672 Loc : constant Source_Ptr := Sloc (N);
673 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
674 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
676 R_Type : constant Entity_Id := Etype (Scope_Id);
677 -- Function result subtype
679 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
680 -- Ada 2020: Check that the return expression in a No_Return function
681 -- meets the conditions specified by RM 6.5.1(5.1/5).
683 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id);
684 -- Apply legality rule of 6.5 (5.9) to the access discriminants of an
685 -- aggregate in a return statement.
687 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
688 -- Check that the return_subtype_indication properly matches the result
689 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
691 --------------------------------
692 -- Check_No_Return_Expression --
693 --------------------------------
695 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
696 Kind : constant Node_Kind := Nkind (Return_Expr);
699 if Kind = N_Raise_Expression then
702 elsif Kind = N_Function_Call
703 and then Is_Entity_Name (Name (Return_Expr))
704 and then Ekind_In (Entity (Name (Return_Expr)), E_Function,
706 and then No_Return (Entity (Name (Return_Expr)))
712 ("illegal expression in RETURN statement of No_Return function",
715 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
717 end Check_No_Return_Expression;
719 ------------------------------------------
720 -- Check_Return_Construct_Accessibility --
721 ------------------------------------------
723 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id) is
725 Agg : Node_Id := Empty;
729 Process_Exprs : Boolean := False;
730 Return_Con : Node_Id;
733 -- Only perform checks on record types with access discriminants and
734 -- non-internally generated functions.
736 if not Is_Record_Type (R_Type)
737 or else not Has_Discriminants (R_Type)
738 or else not Comes_From_Source (Return_Stmt)
743 -- We are only interested in return statements
745 if not Nkind_In (Return_Stmt, N_Extended_Return_Statement,
746 N_Simple_Return_Statement)
751 -- Fetch the object from the return statement, in the case of a
752 -- simple return statement the expression is part of the node.
754 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
755 -- Obtain the object definition from the expanded extended return
757 Return_Con := First (Return_Object_Declarations (Return_Stmt));
758 while Present (Return_Con) loop
759 -- Inspect the original node to avoid object declarations
760 -- expanded into renamings.
762 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
763 and then Comes_From_Source (Original_Node (Return_Con))
768 Nlists.Next (Return_Con);
771 pragma Assert (Present (Return_Con));
773 -- Could be dealing with a renaming
775 Return_Con := Original_Node (Return_Con);
777 Return_Con := Return_Stmt;
780 -- We may need to check an aggregate or a subtype indication
781 -- depending on how the discriminants were specified and whether
782 -- we are looking at an extended return statement.
784 if Nkind (Return_Con) = N_Object_Declaration
785 and then Nkind (Object_Definition (Return_Con))
786 = N_Subtype_Indication
788 Assoc := Original_Node
791 (Constraint (Object_Definition (Return_Con)))));
793 -- Qualified expressions may be nested
795 Agg := Original_Node (Expression (Return_Con));
796 while Nkind (Agg) = N_Qualified_Expression loop
797 Agg := Original_Node (Expression (Agg));
800 -- If we are looking at an aggregate instead of a function call we
801 -- can continue checking accessibility for the supplied
802 -- discriminant associations.
804 if Nkind (Agg) = N_Aggregate then
805 if Present (Expressions (Agg)) then
806 Assoc := First (Expressions (Agg));
807 Process_Exprs := True;
809 Assoc := First (Component_Associations (Agg));
812 -- Otherwise the expression is not of interest ???
819 -- Move through the discriminants checking the accessibility level
820 -- of each co-extension's associated expression.
822 Discr := First_Discriminant (R_Type);
823 while Present (Discr) loop
824 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
826 if Nkind (Assoc) = N_Attribute_Reference then
828 elsif Nkind_In (Assoc, N_Component_Association,
829 N_Discriminant_Association)
831 Expr := Expression (Assoc);
836 -- This anonymous access discriminant has an associated
837 -- expression which needs checking.
840 and then Nkind (Expr) = N_Attribute_Reference
841 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
843 -- Obtain the object to perform static checks on by moving
844 -- up the prefixes in the expression taking into account
845 -- named access types and renamed objects within the
848 Obj := Original_Node (Prefix (Expr));
850 while Nkind_In (Obj, N_Explicit_Dereference,
852 N_Selected_Component)
854 -- When we encounter a named access type then we can
855 -- ignore accessibility checks on the dereference.
857 if Ekind (Etype (Original_Node (Prefix (Obj))))
859 E_Access_Protected_Subprogram_Type
861 if Nkind (Obj) = N_Selected_Component then
862 Obj := Selector_Name (Obj);
864 Obj := Original_Node (Prefix (Obj));
869 Obj := Original_Node (Prefix (Obj));
872 if Nkind (Obj) = N_Selected_Component then
873 Obj := Selector_Name (Obj);
876 -- Check for renamings
878 pragma Assert (Is_Entity_Name (Obj));
880 if Present (Renamed_Object (Entity (Obj))) then
881 Obj := Renamed_Object (Entity (Obj));
887 -- Do not check aliased formals or function calls. A
888 -- run-time check may still be needed ???
890 if Is_Formal (Entity (Obj))
891 and then Is_Aliased (Entity (Obj))
895 elsif Object_Access_Level (Obj) >
896 Scope_Depth (Scope (Scope_Id))
899 ("access discriminant in return aggregate would "
900 & "be a dangling reference", Obj);
905 Next_Discriminant (Discr);
907 if not Is_List_Member (Assoc) then
913 -- After aggregate expressions, examine component associations if
918 and then Process_Exprs
919 and then Present (Component_Associations (Agg))
921 Assoc := First (Component_Associations (Agg));
922 Process_Exprs := False;
928 end Check_Return_Construct_Accessibility;
930 -------------------------------------
931 -- Check_Return_Subtype_Indication --
932 -------------------------------------
934 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
935 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
937 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
938 -- Subtype given in the extended return statement (must match R_Type)
940 Subtype_Ind : constant Node_Id :=
941 Object_Definition (Original_Node (Obj_Decl));
943 procedure Error_No_Match (N : Node_Id);
944 -- Output error messages for case where types do not statically
945 -- match. N is the location for the messages.
951 procedure Error_No_Match (N : Node_Id) is
954 ("subtype must statically match function result subtype", N);
956 if not Predicates_Match (R_Stm_Type, R_Type) then
957 Error_Msg_Node_2 := R_Type;
959 ("\predicate of& does not match predicate of&",
964 -- Start of processing for Check_Return_Subtype_Indication
967 -- First, avoid cascaded errors
969 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
973 -- "return access T" case; check that the return statement also has
974 -- "access T", and that the subtypes statically match:
975 -- if this is an access to subprogram the signatures must match.
977 if Is_Anonymous_Access_Type (R_Type) then
978 if Is_Anonymous_Access_Type (R_Stm_Type) then
979 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
981 if Base_Type (Designated_Type (R_Stm_Type)) /=
982 Base_Type (Designated_Type (R_Type))
983 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
985 Error_No_Match (Subtype_Mark (Subtype_Ind));
989 -- For two anonymous access to subprogram types, the types
990 -- themselves must be type conformant.
992 if not Conforming_Types
993 (R_Stm_Type, R_Type, Fully_Conformant)
995 Error_No_Match (Subtype_Ind);
1000 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
1003 -- If the return object is of an anonymous access type, then report
1004 -- an error if the function's result type is not also anonymous.
1006 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1007 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1009 ("anonymous access not allowed for function with named access "
1010 & "result", Subtype_Ind);
1012 -- Subtype indication case: check that the return object's type is
1013 -- covered by the result type, and that the subtypes statically match
1014 -- when the result subtype is constrained. Also handle record types
1015 -- with unknown discriminants for which we have built the underlying
1016 -- record view. Coverage is needed to allow specific-type return
1017 -- objects when the result type is class-wide (see AI05-32).
1019 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1020 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1023 (Base_Type (R_Type),
1024 Underlying_Record_View (Base_Type (R_Stm_Type))))
1026 -- A null exclusion may be present on the return type, on the
1027 -- function specification, on the object declaration or on the
1030 if Is_Access_Type (R_Type)
1032 (Can_Never_Be_Null (R_Type)
1033 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1034 Can_Never_Be_Null (R_Stm_Type)
1036 Error_No_Match (Subtype_Ind);
1039 -- AI05-103: for elementary types, subtypes must statically match
1041 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1042 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1043 Error_No_Match (Subtype_Ind);
1047 -- All remaining cases are illegal
1049 -- Note: previous versions of this subprogram allowed the return
1050 -- value to be the ancestor of the return type if the return type
1051 -- was a null extension. This was plainly incorrect.
1055 ("wrong type for return_subtype_indication", Subtype_Ind);
1057 end Check_Return_Subtype_Indication;
1059 ---------------------
1060 -- Local Variables --
1061 ---------------------
1064 Obj_Decl : Node_Id := Empty;
1066 -- Start of processing for Analyze_Function_Return
1069 Set_Return_Present (Scope_Id);
1071 if Nkind (N) = N_Simple_Return_Statement then
1072 Expr := Expression (N);
1074 -- Guard against a malformed expression. The parser may have tried to
1075 -- recover but the node is not analyzable.
1077 if Nkind (Expr) = N_Error then
1078 Set_Etype (Expr, Any_Type);
1079 Expander_Mode_Save_And_Set (False);
1083 -- The resolution of a controlled [extension] aggregate associated
1084 -- with a return statement creates a temporary which needs to be
1085 -- finalized on function exit. Wrap the return statement inside a
1086 -- block so that the finalization machinery can detect this case.
1087 -- This early expansion is done only when the return statement is
1088 -- not part of a handled sequence of statements.
1090 if Nkind_In (Expr, N_Aggregate,
1091 N_Extension_Aggregate)
1092 and then Needs_Finalization (R_Type)
1093 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1096 Make_Block_Statement (Loc,
1097 Handled_Statement_Sequence =>
1098 Make_Handled_Sequence_Of_Statements (Loc,
1099 Statements => New_List (Relocate_Node (N)))));
1107 -- Ada 2005 (AI-251): If the type of the returned object is
1108 -- an access to an interface type then we add an implicit type
1109 -- conversion to force the displacement of the "this" pointer to
1110 -- reference the secondary dispatch table. We cannot delay the
1111 -- generation of this implicit conversion until the expansion
1112 -- because in this case the type resolution changes the decoration
1113 -- of the expression node to match R_Type; by contrast, if the
1114 -- returned object is a class-wide interface type then it is too
1115 -- early to generate here the implicit conversion since the return
1116 -- statement may be rewritten by the expander into an extended
1117 -- return statement whose expansion takes care of adding the
1118 -- implicit type conversion to displace the pointer to the object.
1121 and then Serious_Errors_Detected = 0
1122 and then Is_Access_Type (R_Type)
1123 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1124 and then Is_Interface (Designated_Type (R_Type))
1125 and then Is_Progenitor (Designated_Type (R_Type),
1126 Designated_Type (Etype (Expr)))
1128 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1132 Resolve (Expr, R_Type);
1133 Check_Limited_Return (N, Expr, R_Type);
1135 Check_Return_Construct_Accessibility (N);
1137 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1138 -- nonreturning function shall be a simple_return_statement with
1139 -- an expression that is a raise_expression, or else a call on a
1140 -- nonreturning function, or else a parenthesized expression of
1143 if Ada_Version >= Ada_2020
1144 and then No_Return (Scope_Id)
1145 and then Comes_From_Source (N)
1147 Check_No_Return_Expression (Original_Node (Expr));
1151 Obj_Decl := Last (Return_Object_Declarations (N));
1153 -- Analyze parts specific to extended_return_statement:
1156 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1157 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1160 Expr := Expression (Obj_Decl);
1162 -- Note: The check for OK_For_Limited_Init will happen in
1163 -- Analyze_Object_Declaration; we treat it as a normal
1164 -- object declaration.
1166 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1169 Check_Return_Subtype_Indication (Obj_Decl);
1171 if Present (HSS) then
1174 if Present (Exception_Handlers (HSS)) then
1176 -- ???Has_Nested_Block_With_Handler needs to be set.
1177 -- Probably by creating an actual N_Block_Statement.
1178 -- Probably in Expand.
1184 -- Mark the return object as referenced, since the return is an
1185 -- implicit reference of the object.
1187 Set_Referenced (Defining_Identifier (Obj_Decl));
1189 Check_References (Stm_Entity);
1191 Check_Return_Construct_Accessibility (N);
1193 -- Check RM 6.5 (5.9/3)
1196 if Ada_Version < Ada_2012 then
1198 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1199 -- Can it really happen (extended return???)
1202 ("aliased only allowed for limited return objects "
1203 & "in Ada 2012??", N);
1205 elsif not Is_Limited_View (R_Type) then
1207 ("aliased only allowed for limited return objects", N);
1211 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1212 -- nonreturning function shall be a simple_return_statement.
1214 if Ada_Version >= Ada_2020
1215 and then No_Return (Scope_Id)
1216 and then Comes_From_Source (N)
1219 ("extended RETURN statement not allowed in No_Return "
1225 -- Case of Expr present
1227 if Present (Expr) then
1229 -- Defend against previous errors
1231 if Nkind (Expr) = N_Empty
1232 or else No (Etype (Expr))
1237 -- Apply constraint check. Note that this is done before the implicit
1238 -- conversion of the expression done for anonymous access types to
1239 -- ensure correct generation of the null-excluding check associated
1240 -- with null-excluding expressions found in return statements. We
1241 -- don't need a check if the subtype of the return object is the
1242 -- same as the result subtype of the function.
1244 if Nkind (N) /= N_Extended_Return_Statement
1245 or else Nkind (Obj_Decl) /= N_Object_Declaration
1246 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
1247 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
1249 Apply_Constraint_Check (Expr, R_Type);
1252 -- The return value is converted to the return type of the function,
1253 -- which implies a predicate check if the return type is predicated.
1254 -- We do not apply the check to a case expression because it will
1255 -- be expanded into a series of return statements, each of which
1256 -- will receive a predicate check.
1258 if Nkind (Expr) /= N_Case_Expression then
1259 Apply_Predicate_Check (Expr, R_Type);
1262 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1263 -- type, apply an implicit conversion of the expression to that type
1264 -- to force appropriate static and run-time accessibility checks.
1266 if Ada_Version >= Ada_2005
1267 and then Ekind (R_Type) = E_Anonymous_Access_Type
1269 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1270 Analyze_And_Resolve (Expr, R_Type);
1272 -- If this is a local anonymous access to subprogram, the
1273 -- accessibility check can be applied statically. The return is
1274 -- illegal if the access type of the return expression is declared
1275 -- inside of the subprogram (except if it is the subtype indication
1276 -- of an extended return statement).
1278 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1279 if not Comes_From_Source (Current_Scope)
1280 or else Ekind (Current_Scope) = E_Return_Statement
1285 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1287 Error_Msg_N ("cannot return local access to subprogram", N);
1290 -- The expression cannot be of a formal incomplete type
1292 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1293 and then Is_Generic_Type (Etype (Expr))
1296 ("cannot return expression of a formal incomplete type", N);
1299 -- If the result type is class-wide, then check that the return
1300 -- expression's type is not declared at a deeper level than the
1301 -- function (RM05-6.5(5.6/2)).
1303 if Ada_Version >= Ada_2005
1304 and then Is_Class_Wide_Type (R_Type)
1306 if Type_Access_Level (Etype (Expr)) >
1307 Subprogram_Access_Level (Scope_Id)
1310 ("level of return expression type is deeper than "
1311 & "class-wide function!", Expr);
1315 -- Check incorrect use of dynamically tagged expression
1317 if Is_Tagged_Type (R_Type) then
1318 Check_Dynamically_Tagged_Expression
1324 -- ??? A real run-time accessibility check is needed in cases
1325 -- involving dereferences of access parameters. For now we just
1326 -- check the static cases.
1328 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1329 and then Is_Limited_View (Etype (Scope_Id))
1330 and then Object_Access_Level (Expr) >
1331 Subprogram_Access_Level (Scope_Id)
1333 -- Suppress the message in a generic, where the rewriting
1336 if Inside_A_Generic then
1341 Make_Raise_Program_Error (Loc,
1342 Reason => PE_Accessibility_Check_Failed));
1345 Error_Msg_Warn := SPARK_Mode /= On;
1346 Error_Msg_N ("cannot return a local value by reference<<", N);
1347 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1351 if Known_Null (Expr)
1352 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1353 and then Null_Exclusion_Present (Parent (Scope_Id))
1355 Apply_Compile_Time_Constraint_Error
1357 Msg => "(Ada 2005) null not allowed for "
1358 & "null-excluding return??",
1359 Reason => CE_Null_Not_Allowed);
1362 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1363 -- has no initializing expression.
1365 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1366 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1367 Subprogram_Access_Level (Scope_Id)
1370 ("level of return expression type is deeper than "
1371 & "class-wide function!", Obj_Decl);
1374 end Analyze_Function_Return;
1376 -------------------------------------
1377 -- Analyze_Generic_Subprogram_Body --
1378 -------------------------------------
1380 procedure Analyze_Generic_Subprogram_Body
1384 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1385 Kind : constant Entity_Kind := Ekind (Gen_Id);
1386 Body_Id : Entity_Id;
1391 -- Copy body and disable expansion while analyzing the generic For a
1392 -- stub, do not copy the stub (which would load the proper body), this
1393 -- will be done when the proper body is analyzed.
1395 if Nkind (N) /= N_Subprogram_Body_Stub then
1396 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1399 -- Once the contents of the generic copy and the template are
1400 -- swapped, do the same for their respective aspect specifications.
1402 Exchange_Aspects (N, New_N);
1404 -- Collect all contract-related source pragmas found within the
1405 -- template and attach them to the contract of the subprogram body.
1406 -- This contract is used in the capture of global references within
1409 Create_Generic_Contract (N);
1414 Spec := Specification (N);
1416 -- Within the body of the generic, the subprogram is callable, and
1417 -- behaves like the corresponding non-generic unit.
1419 Body_Id := Defining_Entity (Spec);
1421 if Kind = E_Generic_Procedure
1422 and then Nkind (Spec) /= N_Procedure_Specification
1424 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1427 elsif Kind = E_Generic_Function
1428 and then Nkind (Spec) /= N_Function_Specification
1430 Error_Msg_N ("invalid body for generic function ", Body_Id);
1434 Set_Corresponding_Body (Gen_Decl, Body_Id);
1436 if Has_Completion (Gen_Id)
1437 and then Nkind (Parent (N)) /= N_Subunit
1439 Error_Msg_N ("duplicate generic body", N);
1442 Set_Has_Completion (Gen_Id);
1445 if Nkind (N) = N_Subprogram_Body_Stub then
1446 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1448 Set_Corresponding_Spec (N, Gen_Id);
1451 if Nkind (Parent (N)) = N_Compilation_Unit then
1452 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1455 -- Make generic parameters immediately visible in the body. They are
1456 -- needed to process the formals declarations. Then make the formals
1457 -- visible in a separate step.
1459 Push_Scope (Gen_Id);
1463 First_Ent : Entity_Id;
1466 First_Ent := First_Entity (Gen_Id);
1469 while Present (E) and then not Is_Formal (E) loop
1474 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1476 -- Now generic formals are visible, and the specification can be
1477 -- analyzed, for subsequent conformance check.
1479 Body_Id := Analyze_Subprogram_Specification (Spec);
1481 -- Make formal parameters visible
1485 -- E is the first formal parameter, we loop through the formals
1486 -- installing them so that they will be visible.
1488 Set_First_Entity (Gen_Id, E);
1489 while Present (E) loop
1495 -- Visible generic entity is callable within its own body
1497 Set_Ekind (Gen_Id, Ekind (Body_Id));
1498 Set_Ekind (Body_Id, E_Subprogram_Body);
1499 Set_Convention (Body_Id, Convention (Gen_Id));
1500 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1501 Set_Scope (Body_Id, Scope (Gen_Id));
1503 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1505 if Nkind (N) = N_Subprogram_Body_Stub then
1507 -- No body to analyze, so restore state of generic unit
1509 Set_Ekind (Gen_Id, Kind);
1510 Set_Ekind (Body_Id, Kind);
1512 if Present (First_Ent) then
1513 Set_First_Entity (Gen_Id, First_Ent);
1520 -- If this is a compilation unit, it must be made visible explicitly,
1521 -- because the compilation of the declaration, unlike other library
1522 -- unit declarations, does not. If it is not a unit, the following
1523 -- is redundant but harmless.
1525 Set_Is_Immediately_Visible (Gen_Id);
1526 Reference_Body_Formals (Gen_Id, Body_Id);
1528 if Is_Child_Unit (Gen_Id) then
1529 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1532 Set_Actual_Subtypes (N, Current_Scope);
1534 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1535 Set_SPARK_Pragma_Inherited (Body_Id);
1537 -- Analyze any aspect specifications that appear on the generic
1540 if Has_Aspects (N) then
1541 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1544 Analyze_Declarations (Declarations (N));
1547 -- Process the contract of the subprogram body after all declarations
1548 -- have been analyzed. This ensures that any contract-related pragmas
1549 -- are available through the N_Contract node of the body.
1551 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1553 Analyze (Handled_Statement_Sequence (N));
1554 Save_Global_References (Original_Node (N));
1556 -- Prior to exiting the scope, include generic formals again (if any
1557 -- are present) in the set of local entities.
1559 if Present (First_Ent) then
1560 Set_First_Entity (Gen_Id, First_Ent);
1563 Check_References (Gen_Id);
1566 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1567 Update_Use_Clause_Chain;
1568 Validate_Categorization_Dependency (N, Gen_Id);
1570 Check_Subprogram_Order (N);
1572 -- Outside of its body, unit is generic again
1574 Set_Ekind (Gen_Id, Kind);
1575 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1578 Style.Check_Identifier (Body_Id, Gen_Id);
1582 end Analyze_Generic_Subprogram_Body;
1584 ----------------------------
1585 -- Analyze_Null_Procedure --
1586 ----------------------------
1588 -- WARNING: This routine manages Ghost regions. Return statements must be
1589 -- replaced by gotos that jump to the end of the routine and restore the
1592 procedure Analyze_Null_Procedure
1594 Is_Completion : out Boolean)
1596 Loc : constant Source_Ptr := Sloc (N);
1597 Spec : constant Node_Id := Specification (N);
1599 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1600 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1601 Saved_ISMP : constant Boolean :=
1602 Ignore_SPARK_Mode_Pragmas_In_Instance;
1603 -- Save the Ghost and SPARK mode-related data to restore on exit
1605 Designator : Entity_Id;
1607 Null_Body : Node_Id := Empty;
1608 Null_Stmt : Node_Id := Null_Statement (Spec);
1612 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1614 -- A null procedure is Ghost when it is stand-alone and is subject to
1615 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1616 -- now, to ensure that any nodes generated during analysis and expansion
1617 -- are properly marked as Ghost.
1619 if Present (Prev) then
1620 Mark_And_Set_Ghost_Body (N, Prev);
1623 -- Capture the profile of the null procedure before analysis, for
1624 -- expansion at the freeze point and at each point of call. The body is
1625 -- used if the procedure has preconditions, or if it is a completion. In
1626 -- the first case the body is analyzed at the freeze point, in the other
1627 -- it replaces the null procedure declaration.
1629 -- For a null procedure that comes from source, a NULL statement is
1630 -- provided by the parser, which carries the source location of the
1631 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1632 -- from expansion, create one now.
1634 if No (Null_Stmt) then
1635 Null_Stmt := Make_Null_Statement (Loc);
1639 Make_Subprogram_Body (Loc,
1640 Specification => New_Copy_Tree (Spec),
1641 Declarations => New_List,
1642 Handled_Statement_Sequence =>
1643 Make_Handled_Sequence_Of_Statements (Loc,
1644 Statements => New_List (Null_Stmt)));
1646 -- Create new entities for body and formals
1648 Set_Defining_Unit_Name (Specification (Null_Body),
1649 Make_Defining_Identifier
1650 (Sloc (Defining_Entity (N)),
1651 Chars (Defining_Entity (N))));
1653 Form := First (Parameter_Specifications (Specification (Null_Body)));
1654 while Present (Form) loop
1655 Set_Defining_Identifier (Form,
1656 Make_Defining_Identifier
1657 (Sloc (Defining_Identifier (Form)),
1658 Chars (Defining_Identifier (Form))));
1662 -- Determine whether the null procedure may be a completion of a generic
1663 -- suprogram, in which case we use the new null body as the completion
1664 -- and set minimal semantic information on the original declaration,
1665 -- which is rewritten as a null statement.
1667 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1668 Insert_Before (N, Null_Body);
1669 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1671 Rewrite (N, Make_Null_Statement (Loc));
1672 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1673 Is_Completion := True;
1678 -- Resolve the types of the formals now, because the freeze point may
1679 -- appear in a different context, e.g. an instantiation.
1681 Form := First (Parameter_Specifications (Specification (Null_Body)));
1682 while Present (Form) loop
1683 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1684 Find_Type (Parameter_Type (Form));
1686 elsif No (Access_To_Subprogram_Definition
1687 (Parameter_Type (Form)))
1689 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1691 -- The case of a null procedure with a formal that is an
1692 -- access-to-subprogram type, and that is used as an actual
1693 -- in an instantiation is left to the enthusiastic reader.
1703 -- If there are previous overloadable entities with the same name, check
1704 -- whether any of them is completed by the null procedure.
1706 if Present (Prev) and then Is_Overloadable (Prev) then
1707 Designator := Analyze_Subprogram_Specification (Spec);
1708 Prev := Find_Corresponding_Spec (N);
1711 if No (Prev) or else not Comes_From_Source (Prev) then
1712 Designator := Analyze_Subprogram_Specification (Spec);
1713 Set_Has_Completion (Designator);
1715 -- Signal to caller that this is a procedure declaration
1717 Is_Completion := False;
1719 -- Null procedures are always inlined, but generic formal subprograms
1720 -- which appear as such in the internal instance of formal packages,
1721 -- need no completion and are not marked Inline.
1724 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1726 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1727 Set_Body_To_Inline (N, Null_Body);
1728 Set_Is_Inlined (Designator);
1732 -- The null procedure is a completion. We unconditionally rewrite
1733 -- this as a null body (even if expansion is not active), because
1734 -- there are various error checks that are applied on this body
1735 -- when it is analyzed (e.g. correct aspect placement).
1737 if Has_Completion (Prev) then
1738 Error_Msg_Sloc := Sloc (Prev);
1739 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1742 Check_Previous_Null_Procedure (N, Prev);
1744 Is_Completion := True;
1745 Rewrite (N, Null_Body);
1750 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1751 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1752 end Analyze_Null_Procedure;
1754 -----------------------------
1755 -- Analyze_Operator_Symbol --
1756 -----------------------------
1758 -- An operator symbol such as "+" or "and" may appear in context where the
1759 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1760 -- is just a string, as in (conjunction = "or"). In these cases the parser
1761 -- generates this node, and the semantics does the disambiguation. Other
1762 -- such case are actuals in an instantiation, the generic unit in an
1763 -- instantiation, and pragma arguments.
1765 procedure Analyze_Operator_Symbol (N : Node_Id) is
1766 Par : constant Node_Id := Parent (N);
1769 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1770 or else Nkind (Par) = N_Function_Instantiation
1771 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1772 or else (Nkind (Par) = N_Pragma_Argument_Association
1773 and then not Is_Pragma_String_Literal (Par))
1774 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1775 or else (Nkind (Par) = N_Attribute_Reference
1776 and then Attribute_Name (Par) /= Name_Value)
1778 Find_Direct_Name (N);
1781 Change_Operator_Symbol_To_String_Literal (N);
1784 end Analyze_Operator_Symbol;
1786 -----------------------------------
1787 -- Analyze_Parameter_Association --
1788 -----------------------------------
1790 procedure Analyze_Parameter_Association (N : Node_Id) is
1792 Analyze (Explicit_Actual_Parameter (N));
1793 end Analyze_Parameter_Association;
1795 ----------------------------
1796 -- Analyze_Procedure_Call --
1797 ----------------------------
1799 -- WARNING: This routine manages Ghost regions. Return statements must be
1800 -- replaced by gotos that jump to the end of the routine and restore the
1803 procedure Analyze_Procedure_Call (N : Node_Id) is
1804 procedure Analyze_Call_And_Resolve;
1805 -- Do Analyze and Resolve calls for procedure call. At the end, check
1806 -- for illegal order dependence.
1807 -- ??? where is the check for illegal order dependencies?
1809 ------------------------------
1810 -- Analyze_Call_And_Resolve --
1811 ------------------------------
1813 procedure Analyze_Call_And_Resolve is
1815 if Nkind (N) = N_Procedure_Call_Statement then
1817 Resolve (N, Standard_Void_Type);
1821 end Analyze_Call_And_Resolve;
1825 Actuals : constant List_Id := Parameter_Associations (N);
1826 Loc : constant Source_Ptr := Sloc (N);
1827 P : constant Node_Id := Name (N);
1829 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1830 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1831 -- Save the Ghost-related attributes to restore on exit
1836 -- Start of processing for Analyze_Procedure_Call
1839 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1840 -- a procedure call or an entry call. The prefix may denote an access
1841 -- to subprogram type, in which case an implicit dereference applies.
1842 -- If the prefix is an indexed component (without implicit dereference)
1843 -- then the construct denotes a call to a member of an entire family.
1844 -- If the prefix is a simple name, it may still denote a call to a
1845 -- parameterless member of an entry family. Resolution of these various
1846 -- interpretations is delicate.
1848 -- Do not analyze machine code statements to avoid rejecting them in
1851 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1852 Set_Etype (P, Standard_Void_Type);
1857 -- If this is a call of the form Obj.Op, the call may have been analyzed
1858 -- and possibly rewritten into a block, in which case we are done.
1860 if Analyzed (N) then
1863 -- If there is an error analyzing the name (which may have been
1864 -- rewritten if the original call was in prefix notation) then error
1865 -- has been emitted already, mark node and return.
1867 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1868 Set_Etype (N, Any_Type);
1872 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1873 -- Set the mode now to ensure that any nodes generated during analysis
1874 -- and expansion are properly marked as Ghost.
1876 Mark_And_Set_Ghost_Procedure_Call (N);
1878 -- Otherwise analyze the parameters
1880 if Present (Actuals) then
1881 Actual := First (Actuals);
1883 while Present (Actual) loop
1885 Check_Parameterless_Call (Actual);
1890 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1892 if Nkind (P) = N_Attribute_Reference
1893 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1895 Name_Elab_Subp_Body)
1897 if Present (Actuals) then
1899 ("no parameters allowed for this call", First (Actuals));
1903 Set_Etype (N, Standard_Void_Type);
1906 elsif Is_Entity_Name (P)
1907 and then Is_Record_Type (Etype (Entity (P)))
1908 and then Remote_AST_I_Dereference (P)
1912 elsif Is_Entity_Name (P)
1913 and then Ekind (Entity (P)) /= E_Entry_Family
1915 if Is_Access_Type (Etype (P))
1916 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1917 and then No (Actuals)
1918 and then Comes_From_Source (N)
1920 Error_Msg_N ("missing explicit dereference in call", N);
1923 Analyze_Call_And_Resolve;
1925 -- If the prefix is the simple name of an entry family, this is a
1926 -- parameterless call from within the task body itself.
1928 elsif Is_Entity_Name (P)
1929 and then Nkind (P) = N_Identifier
1930 and then Ekind (Entity (P)) = E_Entry_Family
1931 and then Present (Actuals)
1932 and then No (Next (First (Actuals)))
1934 -- Can be call to parameterless entry family. What appears to be the
1935 -- sole argument is in fact the entry index. Rewrite prefix of node
1936 -- accordingly. Source representation is unchanged by this
1940 Make_Indexed_Component (Loc,
1942 Make_Selected_Component (Loc,
1943 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1944 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1945 Expressions => Actuals);
1946 Set_Name (N, New_N);
1947 Set_Etype (New_N, Standard_Void_Type);
1948 Set_Parameter_Associations (N, No_List);
1949 Analyze_Call_And_Resolve;
1951 elsif Nkind (P) = N_Explicit_Dereference then
1952 if Ekind (Etype (P)) = E_Subprogram_Type then
1953 Analyze_Call_And_Resolve;
1955 Error_Msg_N ("expect access to procedure in call", P);
1958 -- The name can be a selected component or an indexed component that
1959 -- yields an access to subprogram. Such a prefix is legal if the call
1960 -- has parameter associations.
1962 elsif Is_Access_Type (Etype (P))
1963 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1965 if Present (Actuals) then
1966 Analyze_Call_And_Resolve;
1968 Error_Msg_N ("missing explicit dereference in call ", N);
1971 -- If not an access to subprogram, then the prefix must resolve to the
1972 -- name of an entry, entry family, or protected operation.
1974 -- For the case of a simple entry call, P is a selected component where
1975 -- the prefix is the task and the selector name is the entry. A call to
1976 -- a protected procedure will have the same syntax. If the protected
1977 -- object contains overloaded operations, the entity may appear as a
1978 -- function, the context will select the operation whose type is Void.
1980 elsif Nkind (P) = N_Selected_Component
1981 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1985 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1986 -- in prefix notation may still be missing its controlling argument,
1987 -- so perform the transformation now.
1989 if SPARK_Mode = On and then In_Inlined_Body then
1991 Subp : constant Entity_Id := Entity (Selector_Name (P));
1992 Typ : constant Entity_Id := Etype (Prefix (P));
1995 if Is_Tagged_Type (Typ)
1996 and then Present (First_Formal (Subp))
1997 and then (Etype (First_Formal (Subp)) = Typ
1999 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
2000 and then Try_Object_Operation (P)
2005 Analyze_Call_And_Resolve;
2010 Analyze_Call_And_Resolve;
2013 elsif Nkind (P) = N_Selected_Component
2014 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
2015 and then Present (Actuals)
2016 and then No (Next (First (Actuals)))
2018 -- Can be call to parameterless entry family. What appears to be the
2019 -- sole argument is in fact the entry index. Rewrite prefix of node
2020 -- accordingly. Source representation is unchanged by this
2024 Make_Indexed_Component (Loc,
2025 Prefix => New_Copy (P),
2026 Expressions => Actuals);
2027 Set_Name (N, New_N);
2028 Set_Etype (New_N, Standard_Void_Type);
2029 Set_Parameter_Associations (N, No_List);
2030 Analyze_Call_And_Resolve;
2032 -- For the case of a reference to an element of an entry family, P is
2033 -- an indexed component whose prefix is a selected component (task and
2034 -- entry family), and whose index is the entry family index.
2036 elsif Nkind (P) = N_Indexed_Component
2037 and then Nkind (Prefix (P)) = N_Selected_Component
2038 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
2040 Analyze_Call_And_Resolve;
2042 -- If the prefix is the name of an entry family, it is a call from
2043 -- within the task body itself.
2045 elsif Nkind (P) = N_Indexed_Component
2046 and then Nkind (Prefix (P)) = N_Identifier
2047 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
2050 Make_Selected_Component (Loc,
2052 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
2053 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
2054 Rewrite (Prefix (P), New_N);
2056 Analyze_Call_And_Resolve;
2058 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2059 -- procedure name, so the construct can only be a qualified expression.
2061 elsif Nkind (P) = N_Qualified_Expression
2062 and then Ada_Version >= Ada_2012
2064 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2067 -- Anything else is an error
2070 Error_Msg_N ("invalid procedure or entry call", N);
2074 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2075 end Analyze_Procedure_Call;
2077 ------------------------------
2078 -- Analyze_Return_Statement --
2079 ------------------------------
2081 procedure Analyze_Return_Statement (N : Node_Id) is
2082 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
2083 N_Simple_Return_Statement));
2085 Returns_Object : constant Boolean :=
2086 Nkind (N) = N_Extended_Return_Statement
2088 (Nkind (N) = N_Simple_Return_Statement
2089 and then Present (Expression (N)));
2090 -- True if we're returning something; that is, "return <expression>;"
2091 -- or "return Result : T [:= ...]". False for "return;". Used for error
2092 -- checking: If Returns_Object is True, N should apply to a function
2093 -- body; otherwise N should apply to a procedure body, entry body,
2094 -- accept statement, or extended return statement.
2096 function Find_What_It_Applies_To return Entity_Id;
2097 -- Find the entity representing the innermost enclosing body, accept
2098 -- statement, or extended return statement. If the result is a callable
2099 -- construct or extended return statement, then this will be the value
2100 -- of the Return_Applies_To attribute. Otherwise, the program is
2101 -- illegal. See RM-6.5(4/2).
2103 -----------------------------
2104 -- Find_What_It_Applies_To --
2105 -----------------------------
2107 function Find_What_It_Applies_To return Entity_Id is
2108 Result : Entity_Id := Empty;
2111 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2112 -- and postconditions.
2114 for J in reverse 0 .. Scope_Stack.Last loop
2115 Result := Scope_Stack.Table (J).Entity;
2116 exit when not Ekind_In (Result, E_Block, E_Loop)
2117 and then Chars (Result) /= Name_uPostconditions;
2120 pragma Assert (Present (Result));
2122 end Find_What_It_Applies_To;
2124 -- Local declarations
2126 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2127 Kind : constant Entity_Kind := Ekind (Scope_Id);
2128 Loc : constant Source_Ptr := Sloc (N);
2129 Stm_Entity : constant Entity_Id :=
2131 (E_Return_Statement, Current_Scope, Loc, 'R');
2133 -- Start of processing for Analyze_Return_Statement
2136 Set_Return_Statement_Entity (N, Stm_Entity);
2138 Set_Etype (Stm_Entity, Standard_Void_Type);
2139 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2141 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2142 -- (4/2): an inner return statement will apply to this extended return.
2144 if Nkind (N) = N_Extended_Return_Statement then
2145 Push_Scope (Stm_Entity);
2148 -- Check that pragma No_Return is obeyed. Don't complain about the
2149 -- implicitly-generated return that is placed at the end.
2151 if No_Return (Scope_Id)
2152 and then Ekind_In (Kind, E_Procedure, E_Generic_Procedure)
2153 and then Comes_From_Source (N)
2156 ("RETURN statement not allowed in No_Return procedure", N);
2159 -- Warn on any unassigned OUT parameters if in procedure
2161 if Ekind (Scope_Id) = E_Procedure then
2162 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2165 -- Check that functions return objects, and other things do not
2167 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2168 if not Returns_Object then
2169 Error_Msg_N ("missing expression in return from function", N);
2172 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2173 if Returns_Object then
2174 Error_Msg_N ("procedure cannot return value (use function)", N);
2177 elsif Ekind_In (Kind, E_Entry, E_Entry_Family) then
2178 if Returns_Object then
2179 if Is_Protected_Type (Scope (Scope_Id)) then
2180 Error_Msg_N ("entry body cannot return value", N);
2182 Error_Msg_N ("accept statement cannot return value", N);
2186 elsif Kind = E_Return_Statement then
2188 -- We are nested within another return statement, which must be an
2189 -- extended_return_statement.
2191 if Returns_Object then
2192 if Nkind (N) = N_Extended_Return_Statement then
2194 ("extended return statement cannot be nested (use `RETURN;`)",
2197 -- Case of a simple return statement with a value inside extended
2198 -- return statement.
2202 ("return nested in extended return statement cannot return "
2203 & "value (use `RETURN;`)", N);
2208 Error_Msg_N ("illegal context for return statement", N);
2211 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2212 Analyze_Function_Return (N);
2214 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2215 Set_Return_Present (Scope_Id);
2218 if Nkind (N) = N_Extended_Return_Statement then
2222 Kill_Current_Values (Last_Assignment_Only => True);
2223 Check_Unreachable_Code (N);
2225 Analyze_Dimension (N);
2226 end Analyze_Return_Statement;
2228 -------------------------------------
2229 -- Analyze_Simple_Return_Statement --
2230 -------------------------------------
2232 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2234 if Present (Expression (N)) then
2235 Mark_Coextensions (N, Expression (N));
2238 Analyze_Return_Statement (N);
2239 end Analyze_Simple_Return_Statement;
2241 -------------------------
2242 -- Analyze_Return_Type --
2243 -------------------------
2245 procedure Analyze_Return_Type (N : Node_Id) is
2246 Designator : constant Entity_Id := Defining_Entity (N);
2247 Typ : Entity_Id := Empty;
2250 -- Normal case where result definition does not indicate an error
2252 if Result_Definition (N) /= Error then
2253 if Nkind (Result_Definition (N)) = N_Access_Definition then
2255 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2258 AD : constant Node_Id :=
2259 Access_To_Subprogram_Definition (Result_Definition (N));
2261 if Present (AD) and then Protected_Present (AD) then
2262 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2264 Typ := Access_Definition (N, Result_Definition (N));
2268 Set_Parent (Typ, Result_Definition (N));
2269 Set_Is_Local_Anonymous_Access (Typ);
2270 Set_Etype (Designator, Typ);
2272 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2274 Null_Exclusion_Static_Checks (N);
2276 -- Subtype_Mark case
2279 Find_Type (Result_Definition (N));
2280 Typ := Entity (Result_Definition (N));
2281 Set_Etype (Designator, Typ);
2283 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2285 Null_Exclusion_Static_Checks (N);
2287 -- If a null exclusion is imposed on the result type, then create
2288 -- a null-excluding itype (an access subtype) and use it as the
2289 -- function's Etype. Note that the null exclusion checks are done
2290 -- right before this, because they don't get applied to types that
2291 -- do not come from source.
2293 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2294 Set_Etype (Designator,
2295 Create_Null_Excluding_Itype
2298 Scope_Id => Scope (Current_Scope)));
2300 -- The new subtype must be elaborated before use because
2301 -- it is visible outside of the function. However its base
2302 -- type may not be frozen yet, so the reference that will
2303 -- force elaboration must be attached to the freezing of
2306 -- If the return specification appears on a proper body,
2307 -- the subtype will have been created already on the spec.
2309 if Is_Frozen (Typ) then
2310 if Nkind (Parent (N)) = N_Subprogram_Body
2311 and then Nkind (Parent (Parent (N))) = N_Subunit
2315 Build_Itype_Reference (Etype (Designator), Parent (N));
2319 Ensure_Freeze_Node (Typ);
2322 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2324 Set_Itype (IR, Etype (Designator));
2325 Append_Freeze_Actions (Typ, New_List (IR));
2330 Set_Etype (Designator, Typ);
2333 if Ekind (Typ) = E_Incomplete_Type
2334 or else (Is_Class_Wide_Type (Typ)
2335 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2337 -- AI05-0151: Tagged incomplete types are allowed in all formal
2338 -- parts. Untagged incomplete types are not allowed in bodies.
2339 -- As a consequence, limited views cannot appear in a basic
2340 -- declaration that is itself within a body, because there is
2341 -- no point at which the non-limited view will become visible.
2343 if Ada_Version >= Ada_2012 then
2344 if From_Limited_With (Typ) and then In_Package_Body then
2346 ("invalid use of incomplete type&",
2347 Result_Definition (N), Typ);
2349 -- The return type of a subprogram body cannot be of a
2350 -- formal incomplete type.
2352 elsif Is_Generic_Type (Typ)
2353 and then Nkind (Parent (N)) = N_Subprogram_Body
2356 ("return type cannot be a formal incomplete type",
2357 Result_Definition (N));
2359 elsif Is_Class_Wide_Type (Typ)
2360 and then Is_Generic_Type (Root_Type (Typ))
2361 and then Nkind (Parent (N)) = N_Subprogram_Body
2364 ("return type cannot be a formal incomplete type",
2365 Result_Definition (N));
2367 elsif Is_Tagged_Type (Typ) then
2370 -- Use is legal in a thunk generated for an operation
2371 -- inherited from a progenitor.
2373 elsif Is_Thunk (Designator)
2374 and then Present (Non_Limited_View (Typ))
2378 elsif Nkind (Parent (N)) = N_Subprogram_Body
2379 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2383 ("invalid use of untagged incomplete type&",
2387 -- The type must be completed in the current package. This
2388 -- is checked at the end of the package declaration when
2389 -- Taft-amendment types are identified. If the return type
2390 -- is class-wide, there is no required check, the type can
2391 -- be a bona fide TAT.
2393 if Ekind (Scope (Current_Scope)) = E_Package
2394 and then In_Private_Part (Scope (Current_Scope))
2395 and then not Is_Class_Wide_Type (Typ)
2397 Append_Elmt (Designator, Private_Dependents (Typ));
2402 ("invalid use of incomplete type&", Designator, Typ);
2407 -- Case where result definition does indicate an error
2410 Set_Etype (Designator, Any_Type);
2412 end Analyze_Return_Type;
2414 -----------------------------
2415 -- Analyze_Subprogram_Body --
2416 -----------------------------
2418 procedure Analyze_Subprogram_Body (N : Node_Id) is
2419 Loc : constant Source_Ptr := Sloc (N);
2420 Body_Spec : constant Node_Id := Specification (N);
2421 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2424 if Debug_Flag_C then
2425 Write_Str ("==> subprogram body ");
2426 Write_Name (Chars (Body_Id));
2427 Write_Str (" from ");
2428 Write_Location (Loc);
2433 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2435 -- The real work is split out into the helper, so it can do "return;"
2436 -- without skipping the debug output:
2438 Analyze_Subprogram_Body_Helper (N);
2440 if Debug_Flag_C then
2442 Write_Str ("<== subprogram body ");
2443 Write_Name (Chars (Body_Id));
2444 Write_Str (" from ");
2445 Write_Location (Loc);
2448 end Analyze_Subprogram_Body;
2450 ------------------------------------
2451 -- Analyze_Subprogram_Body_Helper --
2452 ------------------------------------
2454 -- This procedure is called for regular subprogram bodies, generic bodies,
2455 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2456 -- specification matters, and is used to create a proper declaration for
2457 -- the subprogram, or to perform conformance checks.
2459 -- WARNING: This routine manages Ghost regions. Return statements must be
2460 -- replaced by gotos that jump to the end of the routine and restore the
2463 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2464 Body_Spec : Node_Id := Specification (N);
2465 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2466 Loc : constant Source_Ptr := Sloc (N);
2467 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2469 Conformant : Boolean;
2470 Desig_View : Entity_Id := Empty;
2471 Exch_Views : Elist_Id := No_Elist;
2473 Mask_Types : Elist_Id := No_Elist;
2474 Prot_Typ : Entity_Id := Empty;
2475 Spec_Decl : Node_Id := Empty;
2476 Spec_Id : Entity_Id;
2478 Last_Real_Spec_Entity : Entity_Id := Empty;
2479 -- When we analyze a separate spec, the entity chain ends up containing
2480 -- the formals, as well as any itypes generated during analysis of the
2481 -- default expressions for parameters, or the arguments of associated
2482 -- precondition/postcondition pragmas (which are analyzed in the context
2483 -- of the spec since they have visibility on formals).
2485 -- These entities belong with the spec and not the body. However we do
2486 -- the analysis of the body in the context of the spec (again to obtain
2487 -- visibility to the formals), and all the entities generated during
2488 -- this analysis end up also chained to the entity chain of the spec.
2489 -- But they really belong to the body, and there is circuitry to move
2490 -- them from the spec to the body.
2492 -- However, when we do this move, we don't want to move the real spec
2493 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2494 -- variable points to the last real spec entity, so we only move those
2495 -- chained beyond that point. It is initialized to Empty to deal with
2496 -- the case where there is no separate spec.
2498 function Body_Has_Contract return Boolean;
2499 -- Check whether unanalyzed body has an aspect or pragma that may
2500 -- generate a SPARK contract.
2502 function Body_Has_SPARK_Mode_On return Boolean;
2503 -- Check whether SPARK_Mode On applies to the subprogram body, either
2504 -- because it is specified directly on the body, or because it is
2505 -- inherited from the enclosing subprogram or package.
2507 function Build_Internal_Protected_Declaration
2508 (N : Node_Id) return Entity_Id;
2509 -- A subprogram body without a previous spec that appears in a protected
2510 -- body must be expanded separately to create a subprogram declaration
2511 -- for it, in order to resolve internal calls to it from other protected
2514 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2516 procedure Build_Subprogram_Declaration;
2517 -- Create a matching subprogram declaration for subprogram body N
2519 procedure Check_Anonymous_Return;
2520 -- Ada 2005: if a function returns an access type that denotes a task,
2521 -- or a type that contains tasks, we must create a master entity for
2522 -- the anonymous type, which typically will be used in an allocator
2523 -- in the body of the function.
2525 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2526 -- Look ahead to recognize a pragma that may appear after the body.
2527 -- If there is a previous spec, check that it appears in the same
2528 -- declarative part. If the pragma is Inline_Always, perform inlining
2529 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2530 -- If the body acts as a spec, and inlining is required, we create a
2531 -- subprogram declaration for it, in order to attach the body to inline.
2532 -- If pragma does not appear after the body, check whether there is
2533 -- an inline pragma before any local declarations.
2535 procedure Check_Missing_Return;
2536 -- Checks for a function with a no return statements, and also performs
2537 -- the warning checks implemented by Check_Returns. In formal mode, also
2538 -- verify that a function ends with a RETURN and that a procedure does
2539 -- not contain any RETURN.
2541 function Disambiguate_Spec return Entity_Id;
2542 -- When a primitive is declared between the private view and the full
2543 -- view of a concurrent type which implements an interface, a special
2544 -- mechanism is used to find the corresponding spec of the primitive
2547 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2548 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2549 -- incomplete types coming from a limited context and replace their
2550 -- limited views with the non-limited ones. Return the list of changes
2551 -- to be used to undo the transformation.
2553 function Is_Private_Concurrent_Primitive
2554 (Subp_Id : Entity_Id) return Boolean;
2555 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2556 -- type that implements an interface and has a private view.
2558 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2559 -- N is the body generated for an expression function that is not a
2560 -- completion and Spec_Id the defining entity of its spec. Mark all
2561 -- the not-yet-frozen types referenced by the simple return statement
2562 -- of the function as formally frozen.
2564 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2565 -- Find all suitable source pragmas at the top of subprogram body
2566 -- From's declarations and move them after arbitrary node To.
2567 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2568 -- as it applies to the body too.
2570 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2571 -- Undo the transformation done by Exchange_Limited_Views.
2573 procedure Set_Trivial_Subprogram (N : Node_Id);
2574 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2575 -- subprogram whose body is being analyzed. N is the statement node
2576 -- causing the flag to be set, if the following statement is a return
2577 -- of an entity, we mark the entity as set in source to suppress any
2578 -- warning on the stylized use of function stubs with a dummy return.
2580 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2581 -- Undo the transformation done by Mask_Unfrozen_Types
2583 procedure Verify_Overriding_Indicator;
2584 -- If there was a previous spec, the entity has been entered in the
2585 -- current scope previously. If the body itself carries an overriding
2586 -- indicator, check that it is consistent with the known status of the
2589 -----------------------
2590 -- Body_Has_Contract --
2591 -----------------------
2593 function Body_Has_Contract return Boolean is
2594 Decls : constant List_Id := Declarations (N);
2598 -- Check for aspects that may generate a contract
2600 if Present (Aspect_Specifications (N)) then
2601 Item := First (Aspect_Specifications (N));
2602 while Present (Item) loop
2603 if Is_Subprogram_Contract_Annotation (Item) then
2611 -- Check for pragmas that may generate a contract
2613 if Present (Decls) then
2614 Item := First (Decls);
2615 while Present (Item) loop
2616 if Nkind (Item) = N_Pragma
2617 and then Is_Subprogram_Contract_Annotation (Item)
2627 end Body_Has_Contract;
2629 ----------------------------
2630 -- Body_Has_SPARK_Mode_On --
2631 ----------------------------
2633 function Body_Has_SPARK_Mode_On return Boolean is
2634 Decls : constant List_Id := Declarations (N);
2638 -- Check for SPARK_Mode aspect
2640 if Present (Aspect_Specifications (N)) then
2641 Item := First (Aspect_Specifications (N));
2642 while Present (Item) loop
2643 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2644 return Get_SPARK_Mode_From_Annotation (Item) = On;
2651 -- Check for SPARK_Mode pragma
2653 if Present (Decls) then
2654 Item := First (Decls);
2655 while Present (Item) loop
2657 -- Pragmas that apply to a subprogram body are usually grouped
2658 -- together. Look for a potential pragma SPARK_Mode among them.
2660 if Nkind (Item) = N_Pragma then
2661 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2662 return Get_SPARK_Mode_From_Annotation (Item) = On;
2665 -- Otherwise the first non-pragma declarative item terminates
2666 -- the region where pragma SPARK_Mode may appear.
2676 -- Otherwise, the applicable SPARK_Mode is inherited from the
2677 -- enclosing subprogram or package.
2679 return SPARK_Mode = On;
2680 end Body_Has_SPARK_Mode_On;
2682 ------------------------------------------
2683 -- Build_Internal_Protected_Declaration --
2684 ------------------------------------------
2686 function Build_Internal_Protected_Declaration
2687 (N : Node_Id) return Entity_Id
2689 procedure Analyze_Pragmas (From : Node_Id);
2690 -- Analyze all pragmas which follow arbitrary node From
2692 ---------------------
2693 -- Analyze_Pragmas --
2694 ---------------------
2696 procedure Analyze_Pragmas (From : Node_Id) is
2700 Decl := Next (From);
2701 while Present (Decl) loop
2702 if Nkind (Decl) = N_Pragma then
2703 Analyze_Pragma (Decl);
2705 -- No candidate pragmas are available for analysis
2713 end Analyze_Pragmas;
2717 Body_Id : constant Entity_Id := Defining_Entity (N);
2718 Loc : constant Source_Ptr := Sloc (N);
2723 Spec_Id : Entity_Id;
2725 -- Start of processing for Build_Internal_Protected_Declaration
2728 Formal := First_Formal (Body_Id);
2730 -- The protected operation always has at least one formal, namely the
2731 -- object itself, but it is only placed in the parameter list if
2732 -- expansion is enabled.
2734 if Present (Formal) or else Expander_Active then
2735 Formals := Copy_Parameter_List (Body_Id);
2741 Make_Defining_Identifier (Sloc (Body_Id),
2742 Chars => Chars (Body_Id));
2744 -- Indicate that the entity comes from source, to ensure that cross-
2745 -- reference information is properly generated. The body itself is
2746 -- rewritten during expansion, and the body entity will not appear in
2747 -- calls to the operation.
2749 Set_Comes_From_Source (Spec_Id, True);
2751 if Nkind (Specification (N)) = N_Procedure_Specification then
2753 Make_Procedure_Specification (Loc,
2754 Defining_Unit_Name => Spec_Id,
2755 Parameter_Specifications => Formals);
2758 Make_Function_Specification (Loc,
2759 Defining_Unit_Name => Spec_Id,
2760 Parameter_Specifications => Formals,
2761 Result_Definition =>
2762 New_Occurrence_Of (Etype (Body_Id), Loc));
2765 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2766 Set_Corresponding_Body (Decl, Body_Id);
2767 Set_Corresponding_Spec (N, Spec_Id);
2769 Insert_Before (N, Decl);
2771 -- Associate all aspects and pragmas of the body with the spec. This
2772 -- ensures that these annotations apply to the initial declaration of
2773 -- the subprogram body.
2775 Move_Aspects (From => N, To => Decl);
2776 Move_Pragmas (From => N, To => Decl);
2780 -- The analysis of the spec may generate pragmas which require manual
2781 -- analysis. Since the generation of the spec and the relocation of
2782 -- the annotations is driven by the expansion of the stand-alone
2783 -- body, the pragmas will not be analyzed in a timely manner. Do this
2786 Analyze_Pragmas (Decl);
2788 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2789 -- ensuring in particular that 'Access is illegal.
2791 Set_Convention (Spec_Id, Convention_Intrinsic);
2792 Set_Has_Completion (Spec_Id);
2795 end Build_Internal_Protected_Declaration;
2797 ----------------------------------
2798 -- Build_Subprogram_Declaration --
2799 ----------------------------------
2801 procedure Build_Subprogram_Declaration is
2803 Subp_Decl : Node_Id;
2806 -- Create a matching subprogram spec using the profile of the body.
2807 -- The structure of the tree is identical, but has new entities for
2808 -- the defining unit name and formal parameters.
2811 Make_Subprogram_Declaration (Loc,
2812 Specification => Copy_Subprogram_Spec (Body_Spec));
2813 Set_Comes_From_Source (Subp_Decl, True);
2815 -- Also mark parameters as coming from source
2817 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2822 First (Parameter_Specifications (Specification (Subp_Decl)));
2824 while Present (Form) loop
2825 Set_Comes_From_Source (Defining_Identifier (Form), True);
2831 -- Relocate the aspects and relevant pragmas from the subprogram body
2832 -- to the generated spec because it acts as the initial declaration.
2834 Insert_Before (N, Subp_Decl);
2835 Move_Aspects (N, To => Subp_Decl);
2836 Move_Pragmas (N, To => Subp_Decl);
2838 -- Ensure that the generated corresponding spec and original body
2839 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2840 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2841 -- correctly set for local subprograms.
2843 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2845 Analyze (Subp_Decl);
2847 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2848 -- the body since the expander may generate calls using that entity.
2849 -- Required to ensure that Expand_Call rewrites calls to this
2850 -- function by calls to the built procedure.
2852 if Modify_Tree_For_C
2853 and then Nkind (Body_Spec) = N_Function_Specification
2855 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2857 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2858 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2859 Corresponding_Procedure
2860 (Defining_Entity (Specification (Subp_Decl))));
2863 -- Analyze any relocated source pragmas or pragmas created for aspect
2866 Decl := Next (Subp_Decl);
2867 while Present (Decl) loop
2869 -- Stop the search for pragmas once the body has been reached as
2870 -- this terminates the region where pragmas may appear.
2875 elsif Nkind (Decl) = N_Pragma then
2882 Spec_Id := Defining_Entity (Subp_Decl);
2883 Set_Corresponding_Spec (N, Spec_Id);
2885 -- Mark the generated spec as a source construct to ensure that all
2886 -- calls to it are properly registered in ALI files for GNATprove.
2888 Set_Comes_From_Source (Spec_Id, True);
2890 -- Ensure that the specs of the subprogram declaration and its body
2891 -- are identical, otherwise they will appear non-conformant due to
2892 -- rewritings in the default values of formal parameters.
2894 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2895 Set_Specification (N, Body_Spec);
2896 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2897 end Build_Subprogram_Declaration;
2899 ----------------------------
2900 -- Check_Anonymous_Return --
2901 ----------------------------
2903 procedure Check_Anonymous_Return is
2909 if Present (Spec_Id) then
2915 if Ekind (Scop) = E_Function
2916 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2917 and then not Is_Thunk (Scop)
2919 -- Skip internally built functions which handle the case of
2920 -- a null access (see Expand_Interface_Conversion)
2922 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2923 and then not Comes_From_Source (Parent (Scop)))
2925 and then (Has_Task (Designated_Type (Etype (Scop)))
2927 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2929 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2930 and then Expander_Active
2932 -- Avoid cases with no tasking support
2934 and then RTE_Available (RE_Current_Master)
2935 and then not Restriction_Active (No_Task_Hierarchy)
2938 Make_Object_Declaration (Loc,
2939 Defining_Identifier =>
2940 Make_Defining_Identifier (Loc, Name_uMaster),
2941 Constant_Present => True,
2942 Object_Definition =>
2943 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2945 Make_Explicit_Dereference (Loc,
2946 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2948 if Present (Declarations (N)) then
2949 Prepend (Decl, Declarations (N));
2951 Set_Declarations (N, New_List (Decl));
2954 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2955 Set_Has_Master_Entity (Scop);
2957 -- Now mark the containing scope as a task master
2960 while Nkind (Par) /= N_Compilation_Unit loop
2961 Par := Parent (Par);
2962 pragma Assert (Present (Par));
2964 -- If we fall off the top, we are at the outer level, and
2965 -- the environment task is our effective master, so nothing
2969 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2971 Set_Is_Task_Master (Par, True);
2976 end Check_Anonymous_Return;
2978 -------------------------
2979 -- Check_Inline_Pragma --
2980 -------------------------
2982 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2986 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2987 -- True when N is a pragma Inline or Inline_Always that applies
2988 -- to this subprogram.
2990 -----------------------
2991 -- Is_Inline_Pragma --
2992 -----------------------
2994 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2996 if Nkind (N) = N_Pragma
2998 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2999 or else (Pragma_Name_Unmapped (N) = Name_Inline
3001 (Front_End_Inlining or else Optimization_Level > 0)))
3002 and then Present (Pragma_Argument_Associations (N))
3005 Pragma_Arg : Node_Id :=
3006 Expression (First (Pragma_Argument_Associations (N)));
3008 if Nkind (Pragma_Arg) = N_Selected_Component then
3009 Pragma_Arg := Selector_Name (Pragma_Arg);
3012 return Chars (Pragma_Arg) = Chars (Body_Id);
3018 end Is_Inline_Pragma;
3020 -- Start of processing for Check_Inline_Pragma
3023 if not Expander_Active then
3027 if Is_List_Member (N)
3028 and then Present (Next (N))
3029 and then Is_Inline_Pragma (Next (N))
3033 elsif Nkind (N) /= N_Subprogram_Body_Stub
3034 and then Present (Declarations (N))
3035 and then Is_Inline_Pragma (First (Declarations (N)))
3037 Prag := First (Declarations (N));
3043 if Present (Prag) then
3044 if Present (Spec_Id) then
3045 if Is_List_Member (N)
3046 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
3047 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
3053 -- Create a subprogram declaration, to make treatment uniform.
3054 -- Make the sloc of the subprogram name that of the entity in
3055 -- the body, so that style checks find identical strings.
3058 Subp : constant Entity_Id :=
3059 Make_Defining_Identifier
3060 (Sloc (Body_Id), Chars (Body_Id));
3061 Decl : constant Node_Id :=
3062 Make_Subprogram_Declaration (Loc,
3064 New_Copy_Tree (Specification (N)));
3067 -- Link the body and the generated spec
3069 Set_Corresponding_Body (Decl, Body_Id);
3070 Set_Corresponding_Spec (N, Subp);
3072 Set_Defining_Unit_Name (Specification (Decl), Subp);
3074 -- To ensure proper coverage when body is inlined, indicate
3075 -- whether the subprogram comes from source.
3077 Set_Comes_From_Source (Subp, Comes_From_Source (N));
3079 if Present (First_Formal (Body_Id)) then
3080 Plist := Copy_Parameter_List (Body_Id);
3081 Set_Parameter_Specifications
3082 (Specification (Decl), Plist);
3085 -- Move aspects to the new spec
3087 if Has_Aspects (N) then
3088 Move_Aspects (N, To => Decl);
3091 Insert_Before (N, Decl);
3094 Set_Has_Pragma_Inline (Subp);
3096 if Pragma_Name (Prag) = Name_Inline_Always then
3097 Set_Is_Inlined (Subp);
3098 Set_Has_Pragma_Inline_Always (Subp);
3101 -- Prior to copying the subprogram body to create a template
3102 -- for it for subsequent inlining, remove the pragma from
3103 -- the current body so that the copy that will produce the
3104 -- new body will start from a completely unanalyzed tree.
3106 if Nkind (Parent (Prag)) = N_Subprogram_Body then
3107 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
3114 end Check_Inline_Pragma;
3116 --------------------------
3117 -- Check_Missing_Return --
3118 --------------------------
3120 procedure Check_Missing_Return is
3122 Missing_Ret : Boolean;
3125 if Nkind (Body_Spec) = N_Function_Specification then
3126 if Present (Spec_Id) then
3132 if Return_Present (Id) then
3133 Check_Returns (HSS, 'F', Missing_Ret);
3136 Set_Has_Missing_Return (Id);
3139 -- Within a premature instantiation of a package with no body, we
3140 -- build completions of the functions therein, with a Raise
3141 -- statement. No point in complaining about a missing return in
3144 elsif Ekind (Id) = E_Function
3145 and then In_Instance
3146 and then Present (Statements (HSS))
3147 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3151 elsif Is_Generic_Subprogram (Id)
3152 or else not Is_Machine_Code_Subprogram (Id)
3154 Error_Msg_N ("missing RETURN statement in function body", N);
3157 -- If procedure with No_Return, check returns
3159 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3160 if Present (Spec_Id) then
3166 if No_Return (Id) then
3167 Check_Returns (HSS, 'P', Missing_Ret, Id);
3170 end Check_Missing_Return;
3172 -----------------------
3173 -- Disambiguate_Spec --
3174 -----------------------
3176 function Disambiguate_Spec return Entity_Id is
3177 Priv_Spec : Entity_Id;
3180 procedure Replace_Types (To_Corresponding : Boolean);
3181 -- Depending on the flag, replace the type of formal parameters of
3182 -- Body_Id if it is a concurrent type implementing interfaces with
3183 -- the corresponding record type or the other way around.
3185 procedure Replace_Types (To_Corresponding : Boolean) is
3187 Formal_Typ : Entity_Id;
3190 Formal := First_Formal (Body_Id);
3191 while Present (Formal) loop
3192 Formal_Typ := Etype (Formal);
3194 if Is_Class_Wide_Type (Formal_Typ) then
3195 Formal_Typ := Root_Type (Formal_Typ);
3198 -- From concurrent type to corresponding record
3200 if To_Corresponding then
3201 if Is_Concurrent_Type (Formal_Typ)
3202 and then Present (Corresponding_Record_Type (Formal_Typ))
3205 (Corresponding_Record_Type (Formal_Typ)))
3208 Corresponding_Record_Type (Formal_Typ));
3211 -- From corresponding record to concurrent type
3214 if Is_Concurrent_Record_Type (Formal_Typ)
3215 and then Present (Interfaces (Formal_Typ))
3218 Corresponding_Concurrent_Type (Formal_Typ));
3222 Next_Formal (Formal);
3226 -- Start of processing for Disambiguate_Spec
3229 -- Try to retrieve the specification of the body as is. All error
3230 -- messages are suppressed because the body may not have a spec in
3231 -- its current state.
3233 Spec_N := Find_Corresponding_Spec (N, False);
3235 -- It is possible that this is the body of a primitive declared
3236 -- between a private and a full view of a concurrent type. The
3237 -- controlling parameter of the spec carries the concurrent type,
3238 -- not the corresponding record type as transformed by Analyze_
3239 -- Subprogram_Specification. In such cases, we undo the change
3240 -- made by the analysis of the specification and try to find the
3243 -- Note that wrappers already have their corresponding specs and
3244 -- bodies set during their creation, so if the candidate spec is
3245 -- a wrapper, then we definitely need to swap all types to their
3246 -- original concurrent status.
3249 or else Is_Primitive_Wrapper (Spec_N)
3251 -- Restore all references of corresponding record types to the
3252 -- original concurrent types.
3254 Replace_Types (To_Corresponding => False);
3255 Priv_Spec := Find_Corresponding_Spec (N, False);
3257 -- The current body truly belongs to a primitive declared between
3258 -- a private and a full view. We leave the modified body as is,
3259 -- and return the true spec.
3261 if Present (Priv_Spec)
3262 and then Is_Private_Primitive (Priv_Spec)
3267 -- In case that this is some sort of error, restore the original
3268 -- state of the body.
3270 Replace_Types (To_Corresponding => True);
3274 end Disambiguate_Spec;
3276 ----------------------------
3277 -- Exchange_Limited_Views --
3278 ----------------------------
3280 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3281 Result : Elist_Id := No_Elist;
3283 procedure Detect_And_Exchange (Id : Entity_Id);
3284 -- Determine whether Id's type denotes an incomplete type associated
3285 -- with a limited with clause and exchange the limited view with the
3286 -- non-limited one when available. Note that the non-limited view
3287 -- may exist because of a with_clause in another unit in the context,
3288 -- but cannot be used because the current view of the enclosing unit
3289 -- is still a limited view.
3291 -------------------------
3292 -- Detect_And_Exchange --
3293 -------------------------
3295 procedure Detect_And_Exchange (Id : Entity_Id) is
3296 Typ : constant Entity_Id := Etype (Id);
3298 if From_Limited_With (Typ)
3299 and then Has_Non_Limited_View (Typ)
3300 and then not From_Limited_With (Scope (Typ))
3303 Result := New_Elmt_List;
3306 Prepend_Elmt (Typ, Result);
3307 Prepend_Elmt (Id, Result);
3308 Set_Etype (Id, Non_Limited_View (Typ));
3310 end Detect_And_Exchange;
3316 -- Start of processing for Exchange_Limited_Views
3319 -- Do not process subprogram bodies as they already use the non-
3320 -- limited view of types.
3322 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3326 -- Examine all formals and swap views when applicable
3328 Formal := First_Formal (Subp_Id);
3329 while Present (Formal) loop
3330 Detect_And_Exchange (Formal);
3332 Next_Formal (Formal);
3335 -- Process the return type of a function
3337 if Ekind (Subp_Id) = E_Function then
3338 Detect_And_Exchange (Subp_Id);
3342 end Exchange_Limited_Views;
3344 -------------------------------------
3345 -- Is_Private_Concurrent_Primitive --
3346 -------------------------------------
3348 function Is_Private_Concurrent_Primitive
3349 (Subp_Id : Entity_Id) return Boolean
3351 Formal_Typ : Entity_Id;
3354 if Present (First_Formal (Subp_Id)) then
3355 Formal_Typ := Etype (First_Formal (Subp_Id));
3357 if Is_Concurrent_Record_Type (Formal_Typ) then
3358 if Is_Class_Wide_Type (Formal_Typ) then
3359 Formal_Typ := Root_Type (Formal_Typ);
3362 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3365 -- The type of the first formal is a concurrent tagged type with
3369 Is_Concurrent_Type (Formal_Typ)
3370 and then Is_Tagged_Type (Formal_Typ)
3371 and then Has_Private_Declaration (Formal_Typ);
3375 end Is_Private_Concurrent_Primitive;
3377 -------------------------
3378 -- Mask_Unfrozen_Types --
3379 -------------------------
3381 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3382 Result : Elist_Id := No_Elist;
3384 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3385 -- Mask all types referenced in the subtree rooted at Node
3387 --------------------
3388 -- Mask_Type_Refs --
3389 --------------------
3391 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3392 procedure Mask_Type (Typ : Entity_Id);
3393 -- ??? what does this do?
3399 procedure Mask_Type (Typ : Entity_Id) is
3401 -- Skip Itypes created by the preanalysis
3404 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3409 if not Is_Frozen (Typ) then
3410 if Scope (Typ) /= Current_Scope then
3411 Set_Is_Frozen (Typ);
3412 Append_New_Elmt (Typ, Result);
3414 Freeze_Before (N, Typ);
3419 -- Start of processing for Mask_Type_Refs
3422 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3423 Mask_Type (Etype (Entity (Node)));
3425 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3426 Mask_Type (Scope (Entity (Node)));
3429 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3430 and then Present (Etype (Node))
3432 Mask_Type (Etype (Node));
3438 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3442 Return_Stmt : constant Node_Id :=
3443 First (Statements (Handled_Statement_Sequence (N)));
3445 -- Start of processing for Mask_Unfrozen_Types
3448 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3450 Mask_References (Expression (Return_Stmt));
3453 end Mask_Unfrozen_Types;
3459 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3461 Insert_Nod : Node_Id;
3462 Next_Decl : Node_Id;
3465 pragma Assert (Nkind (From) = N_Subprogram_Body);
3467 -- The pragmas are moved in an order-preserving fashion
3471 -- Inspect the declarations of the subprogram body and relocate all
3472 -- candidate pragmas.
3474 Decl := First (Declarations (From));
3475 while Present (Decl) loop
3477 -- Preserve the following declaration for iteration purposes, due
3478 -- to possible relocation of a pragma.
3480 Next_Decl := Next (Decl);
3482 if Nkind (Decl) = N_Pragma then
3483 -- Copy pragma SPARK_Mode if present in the declarative list
3484 -- of subprogram body From and insert it after node To. This
3485 -- pragma should not be moved, as it applies to the body too.
3487 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3488 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3490 -- Move relevant pragmas to the spec
3492 elsif Nam_In (Pragma_Name_Unmapped (Decl),
3499 Name_Refined_Depends,
3500 Name_Refined_Global,
3504 Name_Volatile_Function)
3507 Insert_After (Insert_Nod, Decl);
3511 -- Skip internally generated code
3513 elsif not Comes_From_Source (Decl) then
3516 -- No candidate pragmas are available for relocation
3526 ---------------------------
3527 -- Restore_Limited_Views --
3528 ---------------------------
3530 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3531 Elmt : Elmt_Id := First_Elmt (Restore_List);
3535 while Present (Elmt) loop
3538 Set_Etype (Id, Node (Elmt));
3541 end Restore_Limited_Views;
3543 ----------------------------
3544 -- Set_Trivial_Subprogram --
3545 ----------------------------
3547 procedure Set_Trivial_Subprogram (N : Node_Id) is
3548 Nxt : constant Node_Id := Next (N);
3551 Set_Is_Trivial_Subprogram (Body_Id);
3553 if Present (Spec_Id) then
3554 Set_Is_Trivial_Subprogram (Spec_Id);
3558 and then Nkind (Nxt) = N_Simple_Return_Statement
3559 and then No (Next (Nxt))
3560 and then Present (Expression (Nxt))
3561 and then Is_Entity_Name (Expression (Nxt))
3563 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3565 end Set_Trivial_Subprogram;
3567 ---------------------------
3568 -- Unmask_Unfrozen_Types --
3569 ---------------------------
3571 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3572 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3575 while Present (Elmt) loop
3576 Set_Is_Frozen (Node (Elmt), False);
3579 end Unmask_Unfrozen_Types;
3581 ---------------------------------
3582 -- Verify_Overriding_Indicator --
3583 ---------------------------------
3585 procedure Verify_Overriding_Indicator is
3587 if Must_Override (Body_Spec) then
3588 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3589 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3593 -- Overridden controlled primitives may have had their
3594 -- Overridden_Operation field cleared according to the setting of
3595 -- the Is_Hidden flag. An issue arises, however, when analyzing
3596 -- an instance that may have manipulated the flag during
3597 -- expansion. As a result, we add an exception for this case.
3599 elsif not Present (Overridden_Operation (Spec_Id))
3600 and then not (Nam_In (Chars (Spec_Id), Name_Adjust,
3603 and then In_Instance)
3606 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3608 -- Overriding indicators aren't allowed for protected subprogram
3609 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3610 -- this to a warning if -gnatd.E is enabled.
3612 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3613 Error_Msg_Warn := Error_To_Warning;
3615 ("<<overriding indicator not allowed for protected "
3616 & "subprogram body", Body_Spec);
3619 elsif Must_Not_Override (Body_Spec) then
3620 if Present (Overridden_Operation (Spec_Id)) then
3622 ("subprogram& overrides inherited operation",
3623 Body_Spec, Spec_Id);
3625 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3626 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3629 ("subprogram& overrides predefined operator ",
3630 Body_Spec, Spec_Id);
3632 -- Overriding indicators aren't allowed for protected subprogram
3633 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3634 -- this to a warning if -gnatd.E is enabled.
3636 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3637 Error_Msg_Warn := Error_To_Warning;
3640 ("<<overriding indicator not allowed "
3641 & "for protected subprogram body", Body_Spec);
3643 -- If this is not a primitive operation, then the overriding
3644 -- indicator is altogether illegal.
3646 elsif not Is_Primitive (Spec_Id) then
3648 ("overriding indicator only allowed "
3649 & "if subprogram is primitive", Body_Spec);
3652 -- If checking the style rule and the operation overrides, then
3653 -- issue a warning about a missing overriding_indicator. Protected
3654 -- subprogram bodies are excluded from this style checking, since
3655 -- they aren't primitives (even though their declarations can
3656 -- override) and aren't allowed to have an overriding_indicator.
3659 and then Present (Overridden_Operation (Spec_Id))
3660 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3662 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3663 Style.Missing_Overriding (N, Body_Id);
3666 and then Can_Override_Operator (Spec_Id)
3667 and then not In_Predefined_Unit (Spec_Id)
3669 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3670 Style.Missing_Overriding (N, Body_Id);
3672 end Verify_Overriding_Indicator;
3676 Body_Nod : Node_Id := Empty;
3677 Minimum_Acc_Objs : List_Id := No_List;
3679 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3680 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3681 Saved_EA : constant Boolean := Expander_Active;
3682 Saved_ISMP : constant Boolean :=
3683 Ignore_SPARK_Mode_Pragmas_In_Instance;
3684 -- Save the Ghost and SPARK mode-related data to restore on exit
3686 -- Start of processing for Analyze_Subprogram_Body_Helper
3689 -- A [generic] subprogram body freezes the contract of the nearest
3690 -- enclosing package body and all other contracts encountered in the
3691 -- same declarative part up to and excluding the subprogram body:
3693 -- package body Nearest_Enclosing_Package
3694 -- with Refined_State => (State => Constit)
3698 -- procedure Freezes_Enclosing_Package_Body
3699 -- with Refined_Depends => (Input => Constit) ...
3701 -- This ensures that any annotations referenced by the contract of the
3702 -- [generic] subprogram body are available. This form of freezing is
3703 -- decoupled from the usual Freeze_xxx mechanism because it must also
3704 -- work in the context of generics where normal freezing is disabled.
3706 -- Only bodies coming from source should cause this type of freezing.
3707 -- Expression functions that act as bodies and complete an initial
3708 -- declaration must be included in this category, hence the use of
3711 if Comes_From_Source (Original_Node (N)) then
3712 Freeze_Previous_Contracts (N);
3715 -- Generic subprograms are handled separately. They always have a
3716 -- generic specification. Determine whether current scope has a
3717 -- previous declaration.
3719 -- If the subprogram body is defined within an instance of the same
3720 -- name, the instance appears as a package renaming, and will be hidden
3721 -- within the subprogram.
3723 if Present (Prev_Id)
3724 and then not Is_Overloadable (Prev_Id)
3725 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3726 or else Comes_From_Source (Prev_Id))
3728 if Is_Generic_Subprogram (Prev_Id) then
3731 -- A subprogram body is Ghost when it is stand-alone and subject
3732 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3733 -- the mode now to ensure that any nodes generated during analysis
3734 -- and expansion are properly marked as Ghost.
3736 Mark_And_Set_Ghost_Body (N, Spec_Id);
3738 -- If the body completes the initial declaration of a compilation
3739 -- unit which is subject to pragma Elaboration_Checks, set the
3740 -- model specified by the pragma because it applies to all parts
3743 Install_Elaboration_Model (Spec_Id);
3745 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3746 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3748 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3750 if Nkind (N) = N_Subprogram_Body then
3751 HSS := Handled_Statement_Sequence (N);
3752 Check_Missing_Return;
3757 -- Otherwise a previous entity conflicts with the subprogram name.
3758 -- Attempting to enter name will post error.
3761 Enter_Name (Body_Id);
3765 -- Non-generic case, find the subprogram declaration, if one was seen,
3766 -- or enter new overloaded entity in the current scope. If the
3767 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3768 -- part of the context of one of its subunits. No need to redo the
3771 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3775 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3777 if Nkind (N) = N_Subprogram_Body_Stub
3778 or else No (Corresponding_Spec (N))
3780 if Is_Private_Concurrent_Primitive (Body_Id) then
3781 Spec_Id := Disambiguate_Spec;
3783 -- A subprogram body is Ghost when it is stand-alone and
3784 -- subject to pragma Ghost or when the corresponding spec is
3785 -- Ghost. Set the mode now to ensure that any nodes generated
3786 -- during analysis and expansion are properly marked as Ghost.
3788 Mark_And_Set_Ghost_Body (N, Spec_Id);
3790 -- If the body completes a compilation unit which is subject
3791 -- to pragma Elaboration_Checks, set the model specified by
3792 -- the pragma because it applies to all parts of the unit.
3794 Install_Elaboration_Model (Spec_Id);
3797 Spec_Id := Find_Corresponding_Spec (N);
3799 -- A subprogram body is Ghost when it is stand-alone and
3800 -- subject to pragma Ghost or when the corresponding spec is
3801 -- Ghost. Set the mode now to ensure that any nodes generated
3802 -- during analysis and expansion are properly marked as Ghost.
3804 Mark_And_Set_Ghost_Body (N, Spec_Id);
3806 -- If the body completes a compilation unit which is subject
3807 -- to pragma Elaboration_Checks, set the model specified by
3808 -- the pragma because it applies to all parts of the unit.
3810 Install_Elaboration_Model (Spec_Id);
3812 -- In GNATprove mode, if the body has no previous spec, create
3813 -- one so that the inlining machinery can operate properly.
3814 -- Transfer aspects, if any, to the new spec, so that they
3815 -- are legal and can be processed ahead of the body.
3816 -- We make two copies of the given spec, one for the new
3817 -- declaration, and one for the body.
3818 -- ??? This should be conditioned on front-end inlining rather
3819 -- than GNATprove_Mode.
3821 if No (Spec_Id) and then GNATprove_Mode
3823 -- Inlining does not apply during preanalysis of code
3825 and then Full_Analysis
3827 -- Inlining only applies to full bodies, not stubs
3829 and then Nkind (N) /= N_Subprogram_Body_Stub
3831 -- Inlining only applies to bodies in the source code, not to
3832 -- those generated by the compiler. In particular, expression
3833 -- functions, whose body is generated by the compiler, are
3834 -- treated specially by GNATprove.
3836 and then Comes_From_Source (Body_Id)
3838 -- This cannot be done for a compilation unit, which is not
3839 -- in a context where we can insert a new spec.
3841 and then Is_List_Member (N)
3843 -- Inlining only applies to subprograms without contracts,
3844 -- as a contract is a sign that GNATprove should perform a
3845 -- modular analysis of the subprogram instead of a contextual
3846 -- analysis at each call site. The same test is performed in
3847 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3848 -- here in another form (because the contract has not been
3849 -- attached to the body) to avoid front-end errors in case
3850 -- pragmas are used instead of aspects, because the
3851 -- corresponding pragmas in the body would not be transferred
3852 -- to the spec, leading to legality errors.
3854 and then not Body_Has_Contract
3855 and then not Inside_A_Generic
3857 Build_Subprogram_Declaration;
3859 -- If this is a function that returns a constrained array, and
3860 -- we are generating C code, create subprogram declaration
3861 -- to simplify subsequent C generation.
3864 and then Modify_Tree_For_C
3865 and then Nkind (Body_Spec) = N_Function_Specification
3866 and then Is_Array_Type (Etype (Body_Id))
3867 and then Is_Constrained (Etype (Body_Id))
3869 Build_Subprogram_Declaration;
3873 -- If this is a duplicate body, no point in analyzing it
3875 if Error_Posted (N) then
3879 -- A subprogram body should cause freezing of its own declaration,
3880 -- but if there was no previous explicit declaration, then the
3881 -- subprogram will get frozen too late (there may be code within
3882 -- the body that depends on the subprogram having been frozen,
3883 -- such as uses of extra formals), so we force it to be frozen
3884 -- here. Same holds if the body and spec are compilation units.
3885 -- Finally, if the return type is an anonymous access to protected
3886 -- subprogram, it must be frozen before the body because its
3887 -- expansion has generated an equivalent type that is used when
3888 -- elaborating the body.
3890 -- An exception in the case of Ada 2012, AI05-177: The bodies
3891 -- created for expression functions do not freeze.
3894 and then Nkind (Original_Node (N)) /= N_Expression_Function
3896 Freeze_Before (N, Body_Id);
3898 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3899 Freeze_Before (N, Spec_Id);
3901 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3902 Freeze_Before (N, Etype (Body_Id));
3906 Spec_Id := Corresponding_Spec (N);
3908 -- A subprogram body is Ghost when it is stand-alone and subject
3909 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3910 -- the mode now to ensure that any nodes generated during analysis
3911 -- and expansion are properly marked as Ghost.
3913 Mark_And_Set_Ghost_Body (N, Spec_Id);
3915 -- If the body completes the initial declaration of a compilation
3916 -- unit which is subject to pragma Elaboration_Checks, set the
3917 -- model specified by the pragma because it applies to all parts
3920 Install_Elaboration_Model (Spec_Id);
3924 -- Deactivate expansion inside the body of ignored Ghost entities,
3925 -- as this code will ultimately be ignored. This avoids requiring the
3926 -- presence of run-time units which are not needed. Only do this for
3927 -- user entities, as internally generated entitities might still need
3928 -- to be expanded (e.g. those generated for types).
3930 if Present (Ignored_Ghost_Region)
3931 and then Comes_From_Source (Body_Id)
3933 Expander_Active := False;
3936 -- Previously we scanned the body to look for nested subprograms, and
3937 -- rejected an inline directive if nested subprograms were present,
3938 -- because the back-end would generate conflicting symbols for the
3939 -- nested bodies. This is now unnecessary.
3941 -- Look ahead to recognize a pragma Inline that appears after the body
3943 Check_Inline_Pragma (Spec_Id);
3945 -- Deal with special case of a fully private operation in the body of
3946 -- the protected type. We must create a declaration for the subprogram,
3947 -- in order to attach the subprogram that will be used in internal
3948 -- calls. We exclude compiler generated bodies from the expander since
3949 -- the issue does not arise for those cases.
3952 and then Comes_From_Source (N)
3953 and then Is_Protected_Type (Current_Scope)
3955 Spec_Id := Build_Internal_Protected_Declaration (N);
3958 -- If we are generating C and this is a function returning a constrained
3959 -- array type for which we must create a procedure with an extra out
3960 -- parameter, build and analyze the body now. The procedure declaration
3961 -- has already been created. We reuse the source body of the function,
3962 -- because in an instance it may contain global references that cannot
3963 -- be reanalyzed. The source function itself is not used any further,
3964 -- so we mark it as having a completion. If the subprogram is a stub the
3965 -- transformation is done later, when the proper body is analyzed.
3968 and then Modify_Tree_For_C
3969 and then Present (Spec_Id)
3970 and then Ekind (Spec_Id) = E_Function
3971 and then Nkind (N) /= N_Subprogram_Body_Stub
3972 and then Rewritten_For_C (Spec_Id)
3974 Set_Has_Completion (Spec_Id);
3976 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3979 -- The entity for the created procedure must remain invisible, so it
3980 -- does not participate in resolution of subsequent references to the
3983 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3987 -- If a separate spec is present, then deal with freezing issues
3989 if Present (Spec_Id) then
3990 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3991 Verify_Overriding_Indicator;
3993 -- In general, the spec will be frozen when we start analyzing the
3994 -- body. However, for internally generated operations, such as
3995 -- wrapper functions for inherited operations with controlling
3996 -- results, the spec may not have been frozen by the time we expand
3997 -- the freeze actions that include the bodies. In particular, extra
3998 -- formals for accessibility or for return-in-place may need to be
3999 -- generated. Freeze nodes, if any, are inserted before the current
4000 -- body. These freeze actions are also needed in Compile_Only mode to
4001 -- enable the proper back-end type annotations.
4002 -- They are necessary in any case to ensure proper elaboration order
4005 if Nkind (N) = N_Subprogram_Body
4006 and then Was_Expression_Function (N)
4007 and then not Has_Completion (Spec_Id)
4008 and then Serious_Errors_Detected = 0
4009 and then (Expander_Active
4010 or else Operating_Mode = Check_Semantics
4011 or else Is_Ignored_Ghost_Entity (Spec_Id))
4013 -- The body generated for an expression function that is not a
4014 -- completion is a freeze point neither for the profile nor for
4015 -- anything else. That's why, in order to prevent any freezing
4016 -- during analysis, we need to mask types declared outside the
4017 -- expression (and in an outer scope) that are not yet frozen.
4018 -- This also needs to be done in the case of an ignored Ghost
4019 -- expression function, where the expander isn't active.
4021 Set_Is_Frozen (Spec_Id);
4022 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4024 elsif not Is_Frozen (Spec_Id)
4025 and then Serious_Errors_Detected = 0
4027 Set_Has_Delayed_Freeze (Spec_Id);
4028 Freeze_Before (N, Spec_Id);
4032 -- If the subprogram has a class-wide clone, build its body as a copy
4033 -- of the original body, and rewrite body of original subprogram as a
4034 -- wrapper that calls the clone. If N is a stub, this construction will
4035 -- take place when the proper body is analyzed. No action needed if this
4036 -- subprogram has been eliminated.
4038 if Present (Spec_Id)
4039 and then Present (Class_Wide_Clone (Spec_Id))
4040 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4041 and then Nkind (N) /= N_Subprogram_Body_Stub
4042 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4044 Build_Class_Wide_Clone_Body (Spec_Id, N);
4046 -- This is the new body for the existing primitive operation
4048 Rewrite (N, Build_Class_Wide_Clone_Call
4049 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4050 Set_Has_Completion (Spec_Id, False);
4055 -- Place subprogram on scope stack, and make formals visible. If there
4056 -- is a spec, the visible entity remains that of the spec.
4058 if Present (Spec_Id) then
4059 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4061 if Is_Child_Unit (Spec_Id) then
4062 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4066 Style.Check_Identifier (Body_Id, Spec_Id);
4069 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4070 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4072 if Is_Abstract_Subprogram (Spec_Id) then
4073 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4077 Set_Convention (Body_Id, Convention (Spec_Id));
4078 Set_Has_Completion (Spec_Id);
4080 if Is_Protected_Type (Scope (Spec_Id)) then
4081 Prot_Typ := Scope (Spec_Id);
4084 -- If this is a body generated for a renaming, do not check for
4085 -- full conformance. The check is redundant, because the spec of
4086 -- the body is a copy of the spec in the renaming declaration,
4087 -- and the test can lead to spurious errors on nested defaults.
4089 if Present (Spec_Decl)
4090 and then not Comes_From_Source (N)
4092 (Nkind (Original_Node (Spec_Decl)) =
4093 N_Subprogram_Renaming_Declaration
4094 or else (Present (Corresponding_Body (Spec_Decl))
4096 Nkind (Unit_Declaration_Node
4097 (Corresponding_Body (Spec_Decl))) =
4098 N_Subprogram_Renaming_Declaration))
4102 -- Conversely, the spec may have been generated for specless body
4103 -- with an inline pragma. The entity comes from source, which is
4104 -- both semantically correct and necessary for proper inlining.
4105 -- The subprogram declaration itself is not in the source.
4107 elsif Comes_From_Source (N)
4108 and then Present (Spec_Decl)
4109 and then not Comes_From_Source (Spec_Decl)
4110 and then Has_Pragma_Inline (Spec_Id)
4117 Fully_Conformant, True, Conformant, Body_Id);
4120 -- If the body is not fully conformant, we have to decide if we
4121 -- should analyze it or not. If it has a really messed up profile
4122 -- then we probably should not analyze it, since we will get too
4123 -- many bogus messages.
4125 -- Our decision is to go ahead in the non-fully conformant case
4126 -- only if it is at least mode conformant with the spec. Note
4127 -- that the call to Check_Fully_Conformant has issued the proper
4128 -- error messages to complain about the lack of conformance.
4131 and then not Mode_Conformant (Body_Id, Spec_Id)
4137 -- In the case we are dealing with an expression function we check
4138 -- the formals attached to the spec instead of the body - so we don't
4139 -- reference body formals.
4141 if Spec_Id /= Body_Id
4142 and then not Is_Expression_Function (Spec_Id)
4144 Reference_Body_Formals (Spec_Id, Body_Id);
4147 Set_Ekind (Body_Id, E_Subprogram_Body);
4149 if Nkind (N) = N_Subprogram_Body_Stub then
4150 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4155 Set_Corresponding_Spec (N, Spec_Id);
4157 -- Ada 2005 (AI-345): If the operation is a primitive operation
4158 -- of a concurrent type, the type of the first parameter has been
4159 -- replaced with the corresponding record, which is the proper
4160 -- run-time structure to use. However, within the body there may
4161 -- be uses of the formals that depend on primitive operations
4162 -- of the type (in particular calls in prefixed form) for which
4163 -- we need the original concurrent type. The operation may have
4164 -- several controlling formals, so the replacement must be done
4167 if Comes_From_Source (Spec_Id)
4168 and then Present (First_Entity (Spec_Id))
4169 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4170 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4171 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4172 and then Present (Corresponding_Concurrent_Type
4173 (Etype (First_Entity (Spec_Id))))
4176 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4180 Form := First_Formal (Spec_Id);
4181 while Present (Form) loop
4182 if Etype (Form) = Typ then
4183 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4191 -- Make the formals visible, and place subprogram on scope stack.
4192 -- This is also the point at which we set Last_Real_Spec_Entity
4193 -- to mark the entities which will not be moved to the body.
4195 Install_Formals (Spec_Id);
4196 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4198 -- Within an instance, add local renaming declarations so that
4199 -- gdb can retrieve the values of actuals more easily. This is
4200 -- only relevant if generating code.
4202 if Is_Generic_Instance (Spec_Id)
4203 and then Is_Wrapper_Package (Current_Scope)
4204 and then Expander_Active
4206 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4209 Push_Scope (Spec_Id);
4211 -- Make sure that the subprogram is immediately visible. For
4212 -- child units that have no separate spec this is indispensable.
4213 -- Otherwise it is safe albeit redundant.
4215 Set_Is_Immediately_Visible (Spec_Id);
4218 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4219 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4220 Set_Scope (Body_Id, Scope (Spec_Id));
4222 -- Case of subprogram body with no previous spec
4225 -- Check for style warning required
4229 -- Only apply check for source level subprograms for which checks
4230 -- have not been suppressed.
4232 and then Comes_From_Source (Body_Id)
4233 and then not Suppress_Style_Checks (Body_Id)
4235 -- No warnings within an instance
4237 and then not In_Instance
4239 -- No warnings for expression functions
4241 and then Nkind (Original_Node (N)) /= N_Expression_Function
4243 Style.Body_With_No_Spec (N);
4246 New_Overloaded_Entity (Body_Id);
4248 if Nkind (N) /= N_Subprogram_Body_Stub then
4249 Set_Acts_As_Spec (N);
4250 Generate_Definition (Body_Id);
4252 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4254 -- If the body is an entry wrapper created for an entry with
4255 -- preconditions, it must be compiled in the context of the
4256 -- enclosing synchronized object, because it may mention other
4257 -- operations of the type.
4259 if Is_Entry_Wrapper (Body_Id) then
4261 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4264 Install_Declarations (Prot);
4268 Install_Formals (Body_Id);
4270 Push_Scope (Body_Id);
4273 -- For stubs and bodies with no previous spec, generate references to
4276 Generate_Reference_To_Formals (Body_Id);
4279 -- Entry barrier functions are generated outside the protected type and
4280 -- should not carry the SPARK_Mode of the enclosing context.
4282 if Nkind (N) = N_Subprogram_Body
4283 and then Is_Entry_Barrier_Function (N)
4287 -- The body is generated as part of expression function expansion. When
4288 -- the expression function appears in the visible declarations of a
4289 -- package, the body is added to the private declarations. Since both
4290 -- declarative lists may be subject to a different SPARK_Mode, inherit
4291 -- the mode of the spec.
4293 -- package P with SPARK_Mode is
4294 -- function Expr_Func ... is (...); -- original
4295 -- [function Expr_Func ...;] -- generated spec
4298 -- pragma SPARK_Mode (Off);
4299 -- [function Expr_Func ... is return ...;] -- generated body
4300 -- end P; -- mode is ON
4302 elsif not Comes_From_Source (N)
4303 and then Present (Spec_Id)
4304 and then Is_Expression_Function (Spec_Id)
4306 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4307 Set_SPARK_Pragma_Inherited
4308 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4310 -- Set the SPARK_Mode from the current context (may be overwritten later
4311 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4312 -- initially on a stand-alone subprogram body, but is then relocated to
4313 -- a generated corresponding spec. In this scenario the mode is shared
4314 -- between the spec and body.
4316 elsif No (SPARK_Pragma (Body_Id)) then
4317 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4318 Set_SPARK_Pragma_Inherited (Body_Id);
4321 -- A subprogram body may be instantiated or inlined at a later pass.
4322 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4323 -- applied to the initial declaration of the body.
4325 if Present (Spec_Id) then
4326 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4327 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4331 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4332 -- case the body is instantiated or inlined later and out of context.
4333 -- The body uses this attribute to restore the value of the global
4336 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4337 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4339 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4340 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4344 -- Preserve relevant elaboration-related attributes of the context which
4345 -- are no longer available or very expensive to recompute once analysis,
4346 -- resolution, and expansion are over.
4348 if No (Spec_Id) then
4349 Mark_Elaboration_Attributes
4355 -- If this is the proper body of a stub, we must verify that the stub
4356 -- conforms to the body, and to the previous spec if one was present.
4357 -- We know already that the body conforms to that spec. This test is
4358 -- only required for subprograms that come from source.
4360 if Nkind (Parent (N)) = N_Subunit
4361 and then Comes_From_Source (N)
4362 and then not Error_Posted (Body_Id)
4363 and then Nkind (Corresponding_Stub (Parent (N))) =
4364 N_Subprogram_Body_Stub
4367 Old_Id : constant Entity_Id :=
4369 (Specification (Corresponding_Stub (Parent (N))));
4371 Conformant : Boolean := False;
4374 if No (Spec_Id) then
4375 Check_Fully_Conformant (Body_Id, Old_Id);
4379 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4381 if not Conformant then
4383 -- The stub was taken to be a new declaration. Indicate that
4386 Set_Has_Completion (Old_Id, False);
4392 Set_Has_Completion (Body_Id);
4393 Check_Eliminated (Body_Id);
4395 -- Analyze any aspect specifications that appear on the subprogram body
4396 -- stub. Stop the analysis now as the stub does not have a declarative
4397 -- or a statement part, and it cannot be inlined.
4399 if Nkind (N) = N_Subprogram_Body_Stub then
4400 if Has_Aspects (N) then
4401 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4409 -- Note: Normally we don't do any inlining if expansion is off, since
4410 -- we won't generate code in any case. An exception arises in GNATprove
4411 -- mode where we want to expand some calls in place, even with expansion
4412 -- disabled, since the inlining eases formal verification.
4414 if not GNATprove_Mode
4415 and then Expander_Active
4416 and then Serious_Errors_Detected = 0
4417 and then Present (Spec_Id)
4418 and then Has_Pragma_Inline (Spec_Id)
4420 -- Legacy implementation (relying on front-end inlining)
4422 if not Back_End_Inlining then
4423 if Has_Pragma_Inline_Always (Spec_Id)
4424 or else (Front_End_Inlining
4425 and then not Opt.Disable_FE_Inline)
4427 Build_Body_To_Inline (N, Spec_Id);
4430 -- New implementation (relying on back-end inlining)
4433 if Has_Pragma_Inline_Always (Spec_Id)
4434 or else Optimization_Level > 0
4436 -- Handle function returning an unconstrained type
4438 if Comes_From_Source (Body_Id)
4439 and then Ekind (Spec_Id) = E_Function
4440 and then Returns_Unconstrained_Type (Spec_Id)
4442 -- If function builds in place, i.e. returns a limited type,
4443 -- inlining cannot be done.
4445 and then not Is_Limited_Type (Etype (Spec_Id))
4447 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4451 Subp_Body : constant Node_Id :=
4452 Unit_Declaration_Node (Body_Id);
4453 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4456 -- Do not pass inlining to the backend if the subprogram
4457 -- has declarations or statements which cannot be inlined
4458 -- by the backend. This check is done here to emit an
4459 -- error instead of the generic warning message reported
4460 -- by the GCC backend (ie. "function might not be
4463 if Present (Subp_Decl)
4464 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4468 elsif Has_Excluded_Statement
4471 (Handled_Statement_Sequence (Subp_Body)))
4475 -- If the backend inlining is available then at this
4476 -- stage we only have to mark the subprogram as inlined.
4477 -- The expander will take care of registering it in the
4478 -- table of subprograms inlined by the backend a part of
4479 -- processing calls to it (cf. Expand_Call)
4482 Set_Is_Inlined (Spec_Id);
4489 -- In GNATprove mode, inline only when there is a separate subprogram
4490 -- declaration for now, as inlining of subprogram bodies acting as
4491 -- declarations, or subprogram stubs, are not supported by front-end
4492 -- inlining. This inlining should occur after analysis of the body, so
4493 -- that it is known whether the value of SPARK_Mode, which can be
4494 -- defined by a pragma inside the body, is applicable to the body.
4495 -- Inlining can be disabled with switch -gnatdm
4497 elsif GNATprove_Mode
4498 and then Full_Analysis
4499 and then not Inside_A_Generic
4500 and then Present (Spec_Id)
4502 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4503 and then Body_Has_SPARK_Mode_On
4504 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4505 and then not Body_Has_Contract
4506 and then not Debug_Flag_M
4508 Build_Body_To_Inline (N, Spec_Id);
4511 -- When generating code, inherited pre/postconditions are handled when
4512 -- expanding the corresponding contract.
4514 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4515 -- of the specification we have to install the private withed units.
4516 -- This holds for child units as well.
4518 if Is_Compilation_Unit (Body_Id)
4519 or else Nkind (Parent (N)) = N_Compilation_Unit
4521 Install_Private_With_Clauses (Body_Id);
4524 Check_Anonymous_Return;
4526 -- Set the Protected_Formal field of each extra formal of the protected
4527 -- subprogram to reference the corresponding extra formal of the
4528 -- subprogram that implements it. For regular formals this occurs when
4529 -- the protected subprogram's declaration is expanded, but the extra
4530 -- formals don't get created until the subprogram is frozen. We need to
4531 -- do this before analyzing the protected subprogram's body so that any
4532 -- references to the original subprogram's extra formals will be changed
4533 -- refer to the implementing subprogram's formals (see Expand_Formal).
4535 if Present (Spec_Id)
4536 and then Is_Protected_Type (Scope (Spec_Id))
4537 and then Present (Protected_Body_Subprogram (Spec_Id))
4540 Impl_Subp : constant Entity_Id :=
4541 Protected_Body_Subprogram (Spec_Id);
4542 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4543 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4546 while Present (Prot_Ext_Formal) loop
4547 pragma Assert (Present (Impl_Ext_Formal));
4548 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4549 Next_Formal_With_Extras (Prot_Ext_Formal);
4550 Next_Formal_With_Extras (Impl_Ext_Formal);
4555 -- Generate minimum accessibility local objects to correspond with
4556 -- any extra formal added for anonymous access types. This new local
4557 -- object can then be used instead of the formal in case it is used
4558 -- in an actual to a call to a nested subprogram.
4560 -- This method is used to supplement our "small integer model" for
4561 -- accessibility-check generation (for more information see
4562 -- Dynamic_Accessibility_Level).
4564 -- Because we allow accessibility values greater than our expected value
4565 -- passing along the same extra accessibility formal as an actual
4566 -- to a nested subprogram becomes a problem because high values mean
4567 -- different things to the callee even though they are the same to the
4568 -- caller. So, as described in the first section, we create a local
4569 -- object representing the minimum of the accessibility level value that
4570 -- is passed in and the accessibility level of the callee's parameter
4571 -- and locals and use it in the case of a call to a nested subprogram.
4572 -- This generated object is refered to as a "minimum accessiblity
4575 if Present (Spec_Id) or else Present (Body_Id) then
4576 Body_Nod := Unit_Declaration_Node (Body_Id);
4581 -- Grab the appropriate formal depending on whether there exists
4582 -- an actual spec for the subprogram or whether we are dealing
4583 -- with a protected subprogram.
4585 if Present (Spec_Id) then
4586 if Present (Protected_Body_Subprogram (Spec_Id)) then
4587 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4589 Form := First_Formal (Spec_Id);
4592 Form := First_Formal (Body_Id);
4595 -- Loop through formals if the subprogram is capable of accepting
4596 -- a generated local object. If it is not then it is also not
4597 -- capable of having local subprograms meaning it would not need
4598 -- a minimum accessibility level object anyway.
4600 if Present (Body_Nod)
4601 and then Has_Declarations (Body_Nod)
4602 and then Nkind (Body_Nod) /= N_Package_Specification
4604 while Present (Form) loop
4606 if Present (Extra_Accessibility (Form))
4607 and then No (Minimum_Accessibility (Form))
4609 -- Generate the minimum accessibility level object
4611 -- A60b : integer := integer'min(2, paramL);
4614 Loc : constant Source_Ptr := Sloc (Body_Nod);
4615 Obj_Node : constant Node_Id :=
4616 Make_Object_Declaration (Loc,
4617 Defining_Identifier =>
4619 (Loc, 'A', Extra_Accessibility (Form)),
4620 Object_Definition => New_Occurrence_Of
4621 (Standard_Integer, Loc),
4623 Make_Attribute_Reference (Loc,
4624 Prefix => New_Occurrence_Of
4625 (Standard_Integer, Loc),
4626 Attribute_Name => Name_Min,
4627 Expressions => New_List (
4628 Make_Integer_Literal (Loc,
4629 Object_Access_Level (Form)),
4631 (Extra_Accessibility (Form), Loc))));
4633 -- Add the new local object to the Minimum_Acc_Obj to
4634 -- be later prepended to the subprogram's list of
4635 -- declarations after we are sure all expansion is
4638 if Present (Minimum_Acc_Objs) then
4639 Prepend (Obj_Node, Minimum_Acc_Objs);
4641 Minimum_Acc_Objs := New_List (Obj_Node);
4644 -- Register the object and analyze it
4646 Set_Minimum_Accessibility
4647 (Form, Defining_Identifier (Obj_Node));
4659 -- Now we can go on to analyze the body
4661 HSS := Handled_Statement_Sequence (N);
4662 Set_Actual_Subtypes (N, Current_Scope);
4664 -- Add a declaration for the Protection object, renaming declarations
4665 -- for discriminals and privals and finally a declaration for the entry
4666 -- family index (if applicable). This form of early expansion is done
4667 -- when the Expander is active because Install_Private_Data_Declarations
4668 -- references entities which were created during regular expansion. The
4669 -- subprogram entity must come from source, and not be an internally
4670 -- generated subprogram.
4673 and then Present (Prot_Typ)
4674 and then Present (Spec_Id)
4675 and then Comes_From_Source (Spec_Id)
4676 and then not Is_Eliminated (Spec_Id)
4678 Install_Private_Data_Declarations
4679 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4682 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4683 -- may now appear in parameter and result profiles. Since the analysis
4684 -- of a subprogram body may use the parameter and result profile of the
4685 -- spec, swap any limited views with their non-limited counterpart.
4687 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4688 Exch_Views := Exchange_Limited_Views (Spec_Id);
4691 -- If the return type is an anonymous access type whose designated type
4692 -- is the limited view of a class-wide type and the non-limited view is
4693 -- available, update the return type accordingly.
4695 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4701 Rtyp := Etype (Spec_Id);
4703 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4704 Etyp := Directly_Designated_Type (Rtyp);
4706 if Is_Class_Wide_Type (Etyp)
4707 and then From_Limited_With (Etyp)
4710 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4716 -- Analyze any aspect specifications that appear on the subprogram body
4718 if Has_Aspects (N) then
4719 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4722 Analyze_Declarations (Declarations (N));
4724 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4726 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4727 if Present (SPARK_Pragma (Spec_Id)) then
4728 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4730 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4732 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4733 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4734 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4736 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4739 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4742 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4743 -- as specified in source code, or because SPARK_Mode On is ignored
4744 -- in an instance where the context is SPARK_Mode Off/Auto.
4746 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4747 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4752 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4753 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4754 Error_Msg_Sloc := Sloc (Spec_Id);
4756 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4760 -- A subprogram body freezes its own contract. Analyze the contract
4761 -- after the declarations of the body have been processed as pragmas
4762 -- are now chained on the contract of the subprogram body.
4764 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4766 -- Check completion, and analyze the statements
4769 Inspect_Deferred_Constant_Completion (Declarations (N));
4772 -- Add the generated minimum accessibility objects to the subprogram
4773 -- body's list of declarations after analysis of the statements and
4776 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4777 if Present (Declarations (Body_Nod)) then
4778 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4781 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4785 -- Deal with end of scope processing for the body
4787 Process_End_Label (HSS, 't', Current_Scope);
4788 Update_Use_Clause_Chain;
4791 -- If we are compiling an entry wrapper, remove the enclosing
4792 -- synchronized object from the stack.
4794 if Is_Entry_Wrapper (Body_Id) then
4798 Check_Subprogram_Order (N);
4799 Set_Analyzed (Body_Id);
4801 -- If we have a separate spec, then the analysis of the declarations
4802 -- caused the entities in the body to be chained to the spec id, but
4803 -- we want them chained to the body id. Only the formal parameters
4804 -- end up chained to the spec id in this case.
4806 if Present (Spec_Id) then
4808 -- We must conform to the categorization of our spec
4810 Validate_Categorization_Dependency (N, Spec_Id);
4812 -- And if this is a child unit, the parent units must conform
4814 if Is_Child_Unit (Spec_Id) then
4815 Validate_Categorization_Dependency
4816 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4819 -- Here is where we move entities from the spec to the body
4821 -- Case where there are entities that stay with the spec
4823 if Present (Last_Real_Spec_Entity) then
4825 -- No body entities (happens when the only real spec entities come
4826 -- from precondition and postcondition pragmas).
4828 if No (Last_Entity (Body_Id)) then
4829 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4831 -- Body entities present (formals), so chain stuff past them
4835 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4838 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4839 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4840 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4842 -- Case where there are no spec entities, in this case there can be
4843 -- no body entities either, so just move everything.
4845 -- If the body is generated for an expression function, it may have
4846 -- been preanalyzed already, if 'access was applied to it.
4849 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4850 N_Expression_Function
4852 pragma Assert (No (Last_Entity (Body_Id)));
4856 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4857 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4858 Set_First_Entity (Spec_Id, Empty);
4859 Set_Last_Entity (Spec_Id, Empty);
4862 -- Otherwise the body does not complete a previous declaration. Check
4863 -- the categorization of the body against the units it withs.
4866 Validate_Categorization_Dependency (N, Body_Id);
4869 Check_Missing_Return;
4871 -- Now we are going to check for variables that are never modified in
4872 -- the body of the procedure. But first we deal with a special case
4873 -- where we want to modify this check. If the body of the subprogram
4874 -- starts with a raise statement or its equivalent, or if the body
4875 -- consists entirely of a null statement, then it is pretty obvious that
4876 -- it is OK to not reference the parameters. For example, this might be
4877 -- the following common idiom for a stubbed function: statement of the
4878 -- procedure raises an exception. In particular this deals with the
4879 -- common idiom of a stubbed function, which appears something like:
4881 -- function F (A : Integer) return Some_Type;
4884 -- raise Program_Error;
4888 -- Here the purpose of X is simply to satisfy the annoying requirement
4889 -- in Ada that there be at least one return, and we certainly do not
4890 -- want to go posting warnings on X that it is not initialized. On
4891 -- the other hand, if X is entirely unreferenced that should still
4894 -- What we do is to detect these cases, and if we find them, flag the
4895 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4896 -- suppress unwanted warnings. For the case of the function stub above
4897 -- we have a special test to set X as apparently assigned to suppress
4904 -- Skip call markers installed by the ABE mechanism, labels, and
4905 -- Push_xxx_Error_Label to find the first real statement.
4907 Stm := First (Statements (HSS));
4908 while Nkind_In (Stm, N_Call_Marker, N_Label)
4909 or else Nkind (Stm) in N_Push_xxx_Label
4914 -- Do the test on the original statement before expansion
4917 Ostm : constant Node_Id := Original_Node (Stm);
4920 -- If explicit raise statement, turn on flag
4922 if Nkind (Ostm) = N_Raise_Statement then
4923 Set_Trivial_Subprogram (Stm);
4925 -- If null statement, and no following statements, turn on flag
4927 elsif Nkind (Stm) = N_Null_Statement
4928 and then Comes_From_Source (Stm)
4929 and then No (Next (Stm))
4931 Set_Trivial_Subprogram (Stm);
4933 -- Check for explicit call cases which likely raise an exception
4935 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4936 if Is_Entity_Name (Name (Ostm)) then
4938 Ent : constant Entity_Id := Entity (Name (Ostm));
4941 -- If the procedure is marked No_Return, then likely it
4942 -- raises an exception, but in any case it is not coming
4943 -- back here, so turn on the flag.
4946 and then Ekind (Ent) = E_Procedure
4947 and then No_Return (Ent)
4949 Set_Trivial_Subprogram (Stm);
4957 -- Check for variables that are never modified
4964 -- If there is a separate spec, then transfer Never_Set_In_Source
4965 -- flags from out parameters to the corresponding entities in the
4966 -- body. The reason we do that is we want to post error flags on
4967 -- the body entities, not the spec entities.
4969 if Present (Spec_Id) then
4970 E1 := First_Entity (Spec_Id);
4971 while Present (E1) loop
4972 if Ekind (E1) = E_Out_Parameter then
4973 E2 := First_Entity (Body_Id);
4974 while Present (E2) loop
4975 exit when Chars (E1) = Chars (E2);
4979 if Present (E2) then
4980 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4988 -- Check references of the subprogram spec when we are dealing with
4989 -- an expression function due to it having a generated body.
4990 -- Otherwise, we simply check the formals of the subprogram body.
4992 if Present (Spec_Id)
4993 and then Is_Expression_Function (Spec_Id)
4995 Check_References (Spec_Id);
4997 Check_References (Body_Id);
5001 -- Check for nested subprogram, and mark outer level subprogram if so
5007 if Present (Spec_Id) then
5014 Ent := Enclosing_Subprogram (Ent);
5015 exit when No (Ent) or else Is_Subprogram (Ent);
5018 if Present (Ent) then
5019 Set_Has_Nested_Subprogram (Ent);
5023 -- Restore the limited views in the spec, if any, to let the back end
5024 -- process it without running into circularities.
5026 if Exch_Views /= No_Elist then
5027 Restore_Limited_Views (Exch_Views);
5030 if Mask_Types /= No_Elist then
5031 Unmask_Unfrozen_Types (Mask_Types);
5034 if Present (Desig_View) then
5035 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5039 if Present (Ignored_Ghost_Region) then
5040 Expander_Active := Saved_EA;
5043 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5044 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5045 end Analyze_Subprogram_Body_Helper;
5047 ------------------------------------
5048 -- Analyze_Subprogram_Declaration --
5049 ------------------------------------
5051 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5052 Scop : constant Entity_Id := Current_Scope;
5053 Designator : Entity_Id;
5055 Is_Completion : Boolean;
5056 -- Indicates whether a null procedure declaration is a completion
5059 -- Null procedures are not allowed in SPARK
5061 if Nkind (Specification (N)) = N_Procedure_Specification
5062 and then Null_Present (Specification (N))
5064 -- Null procedures are allowed in protected types, following the
5065 -- recent AI12-0147.
5067 if Is_Protected_Type (Current_Scope)
5068 and then Ada_Version < Ada_2012
5070 Error_Msg_N ("protected operation cannot be a null procedure", N);
5073 Analyze_Null_Procedure (N, Is_Completion);
5075 -- The null procedure acts as a body, nothing further is needed
5077 if Is_Completion then
5082 Designator := Analyze_Subprogram_Specification (Specification (N));
5084 -- A reference may already have been generated for the unit name, in
5085 -- which case the following call is redundant. However it is needed for
5086 -- declarations that are the rewriting of an expression function.
5088 Generate_Definition (Designator);
5090 -- Set the SPARK mode from the current context (may be overwritten later
5091 -- with explicit pragma). This is not done for entry barrier functions
5092 -- because they are generated outside the protected type and should not
5093 -- carry the mode of the enclosing context.
5095 if Nkind (N) = N_Subprogram_Declaration
5096 and then Is_Entry_Barrier_Function (N)
5101 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5102 Set_SPARK_Pragma_Inherited (Designator);
5105 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5106 -- the body of this subprogram is instantiated or inlined later and out
5107 -- of context. The body uses this attribute to restore the value of the
5110 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5111 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5114 -- Preserve relevant elaboration-related attributes of the context which
5115 -- are no longer available or very expensive to recompute once analysis,
5116 -- resolution, and expansion are over.
5118 Mark_Elaboration_Attributes
5119 (N_Id => Designator,
5123 if Debug_Flag_C then
5124 Write_Str ("==> subprogram spec ");
5125 Write_Name (Chars (Designator));
5126 Write_Str (" from ");
5127 Write_Location (Sloc (N));
5132 Validate_RCI_Subprogram_Declaration (N);
5133 New_Overloaded_Entity (Designator);
5134 Check_Delayed_Subprogram (Designator);
5136 -- If the type of the first formal of the current subprogram is a non-
5137 -- generic tagged private type, mark the subprogram as being a private
5138 -- primitive. Ditto if this is a function with controlling result, and
5139 -- the return type is currently private. In both cases, the type of the
5140 -- controlling argument or result must be in the current scope for the
5141 -- operation to be primitive.
5143 if Has_Controlling_Result (Designator)
5144 and then Is_Private_Type (Etype (Designator))
5145 and then Scope (Etype (Designator)) = Current_Scope
5146 and then not Is_Generic_Actual_Type (Etype (Designator))
5148 Set_Is_Private_Primitive (Designator);
5150 elsif Present (First_Formal (Designator)) then
5152 Formal_Typ : constant Entity_Id :=
5153 Etype (First_Formal (Designator));
5155 Set_Is_Private_Primitive (Designator,
5156 Is_Tagged_Type (Formal_Typ)
5157 and then Scope (Formal_Typ) = Current_Scope
5158 and then Is_Private_Type (Formal_Typ)
5159 and then not Is_Generic_Actual_Type (Formal_Typ));
5163 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5166 if Ada_Version >= Ada_2005
5167 and then Comes_From_Source (N)
5168 and then Is_Dispatching_Operation (Designator)
5175 if Has_Controlling_Result (Designator) then
5176 Etyp := Etype (Designator);
5179 E := First_Entity (Designator);
5181 and then Is_Formal (E)
5182 and then not Is_Controlling_Formal (E)
5190 if Is_Access_Type (Etyp) then
5191 Etyp := Directly_Designated_Type (Etyp);
5194 if Is_Interface (Etyp)
5195 and then not Is_Abstract_Subprogram (Designator)
5196 and then not (Ekind (Designator) = E_Procedure
5197 and then Null_Present (Specification (N)))
5199 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5201 -- Specialize error message based on procedures vs. functions,
5202 -- since functions can't be null subprograms.
5204 if Ekind (Designator) = E_Procedure then
5206 ("interface procedure % must be abstract or null", N);
5209 ("interface function % must be abstract", N);
5215 -- What is the following code for, it used to be
5217 -- ??? Set_Suppress_Elaboration_Checks
5218 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5220 -- The following seems equivalent, but a bit dubious
5222 if Elaboration_Checks_Suppressed (Designator) then
5223 Set_Kill_Elaboration_Checks (Designator);
5226 -- For a compilation unit, set body required. This flag will only be
5227 -- reset if a valid Import or Interface pragma is processed later on.
5229 if Nkind (Parent (N)) = N_Compilation_Unit then
5230 Set_Body_Required (Parent (N), True);
5232 if Ada_Version >= Ada_2005
5233 and then Nkind (Specification (N)) = N_Procedure_Specification
5234 and then Null_Present (Specification (N))
5237 ("null procedure cannot be declared at library level", N);
5241 Generate_Reference_To_Formals (Designator);
5242 Check_Eliminated (Designator);
5244 if Debug_Flag_C then
5246 Write_Str ("<== subprogram spec ");
5247 Write_Name (Chars (Designator));
5248 Write_Str (" from ");
5249 Write_Location (Sloc (N));
5253 -- Indicate that this is a protected operation, because it may be used
5254 -- in subsequent declarations within the protected type.
5256 if Is_Protected_Type (Current_Scope) then
5257 Set_Convention (Designator, Convention_Protected);
5260 List_Inherited_Pre_Post_Aspects (Designator);
5262 -- Process the aspects before establishing the proper categorization in
5263 -- case the subprogram is a compilation unit and one of its aspects is
5264 -- converted into a categorization pragma.
5266 if Has_Aspects (N) then
5267 Analyze_Aspect_Specifications (N, Designator);
5270 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5271 Set_Categorization_From_Scope (Designator, Scop);
5273 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5274 -- proper categorization of the unit based on its pragmas.
5277 Push_Scope (Designator);
5278 Set_Categorization_From_Pragmas (N);
5279 Validate_Categorization_Dependency (N, Designator);
5282 end Analyze_Subprogram_Declaration;
5284 --------------------------------------
5285 -- Analyze_Subprogram_Specification --
5286 --------------------------------------
5288 -- Reminder: N here really is a subprogram specification (not a subprogram
5289 -- declaration). This procedure is called to analyze the specification in
5290 -- both subprogram bodies and subprogram declarations (specs).
5292 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5293 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5294 -- Determine whether entity E denotes the spec or body of an invariant
5297 ------------------------------------
5298 -- Is_Invariant_Procedure_Or_Body --
5299 ------------------------------------
5301 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5302 Decl : constant Node_Id := Unit_Declaration_Node (E);
5306 if Nkind (Decl) = N_Subprogram_Body then
5307 Spec := Corresponding_Spec (Decl);
5314 and then Ekind (Spec) = E_Procedure
5315 and then (Is_Partial_Invariant_Procedure (Spec)
5316 or else Is_Invariant_Procedure (Spec));
5317 end Is_Invariant_Procedure_Or_Body;
5321 Designator : constant Entity_Id := Defining_Entity (N);
5322 Formals : constant List_Id := Parameter_Specifications (N);
5324 -- Start of processing for Analyze_Subprogram_Specification
5327 -- Proceed with analysis. Do not emit a cross-reference entry if the
5328 -- specification comes from an expression function, because it may be
5329 -- the completion of a previous declaration. If it is not, the cross-
5330 -- reference entry will be emitted for the new subprogram declaration.
5332 if Nkind (Parent (N)) /= N_Expression_Function then
5333 Generate_Definition (Designator);
5336 if Nkind (N) = N_Function_Specification then
5337 Set_Ekind (Designator, E_Function);
5338 Set_Mechanism (Designator, Default_Mechanism);
5340 Set_Ekind (Designator, E_Procedure);
5341 Set_Etype (Designator, Standard_Void_Type);
5344 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5345 -- those subprograms which could be inlined in GNATprove mode (because
5346 -- Body_To_Inline is non-Empty) but should not be inlined.
5348 if GNATprove_Mode then
5349 Set_Is_Inlined_Always (Designator);
5352 -- Introduce new scope for analysis of the formals and the return type
5354 Set_Scope (Designator, Current_Scope);
5356 if Present (Formals) then
5357 Push_Scope (Designator);
5358 Process_Formals (Formals, N);
5360 -- Check dimensions in N for formals with default expression
5362 Analyze_Dimension_Formals (N, Formals);
5364 -- Ada 2005 (AI-345): If this is an overriding operation of an
5365 -- inherited interface operation, and the controlling type is
5366 -- a synchronized type, replace the type with its corresponding
5367 -- record, to match the proper signature of an overriding operation.
5368 -- Same processing for an access parameter whose designated type is
5369 -- derived from a synchronized interface.
5371 -- This modification is not done for invariant procedures because
5372 -- the corresponding record may not necessarely be visible when the
5373 -- concurrent type acts as the full view of a private type.
5376 -- type Prot is private with Type_Invariant => ...;
5377 -- procedure ConcInvariant (Obj : Prot);
5379 -- protected type Prot is ...;
5380 -- type Concurrent_Record_Prot is record ...;
5381 -- procedure ConcInvariant (Obj : Prot) is
5383 -- end ConcInvariant;
5386 -- In the example above, both the spec and body of the invariant
5387 -- procedure must utilize the private type as the controlling type.
5389 if Ada_Version >= Ada_2005
5390 and then not Is_Invariant_Procedure_Or_Body (Designator)
5394 Formal_Typ : Entity_Id;
5395 Rec_Typ : Entity_Id;
5396 Desig_Typ : Entity_Id;
5399 Formal := First_Formal (Designator);
5400 while Present (Formal) loop
5401 Formal_Typ := Etype (Formal);
5403 if Is_Concurrent_Type (Formal_Typ)
5404 and then Present (Corresponding_Record_Type (Formal_Typ))
5406 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5408 if Present (Interfaces (Rec_Typ)) then
5409 Set_Etype (Formal, Rec_Typ);
5412 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5413 Desig_Typ := Designated_Type (Formal_Typ);
5415 if Is_Concurrent_Type (Desig_Typ)
5416 and then Present (Corresponding_Record_Type (Desig_Typ))
5418 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5420 if Present (Interfaces (Rec_Typ)) then
5421 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5426 Next_Formal (Formal);
5433 -- The subprogram scope is pushed and popped around the processing of
5434 -- the return type for consistency with call above to Process_Formals
5435 -- (which itself can call Analyze_Return_Type), and to ensure that any
5436 -- itype created for the return type will be associated with the proper
5439 elsif Nkind (N) = N_Function_Specification then
5440 Push_Scope (Designator);
5441 Analyze_Return_Type (N);
5447 if Nkind (N) = N_Function_Specification then
5449 -- Deal with operator symbol case
5451 if Nkind (Designator) = N_Defining_Operator_Symbol then
5452 Valid_Operator_Definition (Designator);
5455 May_Need_Actuals (Designator);
5457 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5458 -- the subprogram is abstract also. This does not apply to renaming
5459 -- declarations, where abstractness is inherited, and to subprogram
5460 -- bodies generated for stream operations, which become renamings as
5463 -- In case of primitives associated with abstract interface types
5464 -- the check is applied later (see Analyze_Subprogram_Declaration).
5466 if not Nkind_In (Original_Node (Parent (N)),
5467 N_Abstract_Subprogram_Declaration,
5468 N_Formal_Abstract_Subprogram_Declaration,
5469 N_Subprogram_Renaming_Declaration)
5471 if Is_Abstract_Type (Etype (Designator))
5472 and then not Is_Interface (Etype (Designator))
5475 ("function that returns abstract type must be abstract", N);
5477 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5478 -- access result whose designated type is abstract.
5480 elsif Ada_Version >= Ada_2012
5481 and then Nkind (Result_Definition (N)) = N_Access_Definition
5483 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5484 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5487 ("function whose access result designates abstract type "
5488 & "must be abstract", N);
5494 end Analyze_Subprogram_Specification;
5496 -----------------------
5497 -- Check_Conformance --
5498 -----------------------
5500 procedure Check_Conformance
5501 (New_Id : Entity_Id;
5503 Ctype : Conformance_Type;
5505 Conforms : out Boolean;
5506 Err_Loc : Node_Id := Empty;
5507 Get_Inst : Boolean := False;
5508 Skip_Controlling_Formals : Boolean := False)
5510 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5511 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5512 -- If Errmsg is True, then processing continues to post an error message
5513 -- for conformance error on given node. Two messages are output. The
5514 -- first message points to the previous declaration with a general "no
5515 -- conformance" message. The second is the detailed reason, supplied as
5516 -- Msg. The parameter N provide information for a possible & insertion
5517 -- in the message, and also provides the location for posting the
5518 -- message in the absence of a specified Err_Loc location.
5520 function Conventions_Match
5522 Id2 : Entity_Id) return Boolean;
5523 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5526 -----------------------
5527 -- Conformance_Error --
5528 -----------------------
5530 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5537 if No (Err_Loc) then
5543 Error_Msg_Sloc := Sloc (Old_Id);
5546 when Type_Conformant =>
5547 Error_Msg_N -- CODEFIX
5548 ("not type conformant with declaration#!", Enode);
5550 when Mode_Conformant =>
5551 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5553 ("not mode conformant with operation inherited#!",
5557 ("not mode conformant with declaration#!", Enode);
5560 when Subtype_Conformant =>
5561 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5563 ("not subtype conformant with operation inherited#!",
5567 ("not subtype conformant with declaration#!", Enode);
5570 when Fully_Conformant =>
5571 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5572 Error_Msg_N -- CODEFIX
5573 ("not fully conformant with operation inherited#!",
5576 Error_Msg_N -- CODEFIX
5577 ("not fully conformant with declaration#!", Enode);
5581 Error_Msg_NE (Msg, Enode, N);
5583 end Conformance_Error;
5585 -----------------------
5586 -- Conventions_Match --
5587 -----------------------
5589 function Conventions_Match
5591 Id2 : Entity_Id) return Boolean
5594 -- Ignore the conventions of anonymous access-to-subprogram types
5595 -- and subprogram types because these are internally generated and
5596 -- the only way these may receive a convention is if they inherit
5597 -- the convention of a related subprogram.
5599 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5602 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5607 -- Otherwise compare the conventions directly
5610 return Convention (Id1) = Convention (Id2);
5612 end Conventions_Match;
5616 Old_Type : constant Entity_Id := Etype (Old_Id);
5617 New_Type : constant Entity_Id := Etype (New_Id);
5618 Old_Formal : Entity_Id;
5619 New_Formal : Entity_Id;
5620 Access_Types_Match : Boolean;
5621 Old_Formal_Base : Entity_Id;
5622 New_Formal_Base : Entity_Id;
5624 -- Start of processing for Check_Conformance
5629 -- We need a special case for operators, since they don't appear
5632 if Ctype = Type_Conformant then
5633 if Ekind (New_Id) = E_Operator
5634 and then Operator_Matches_Spec (New_Id, Old_Id)
5640 -- If both are functions/operators, check return types conform
5642 if Old_Type /= Standard_Void_Type
5644 New_Type /= Standard_Void_Type
5646 -- If we are checking interface conformance we omit controlling
5647 -- arguments and result, because we are only checking the conformance
5648 -- of the remaining parameters.
5650 if Has_Controlling_Result (Old_Id)
5651 and then Has_Controlling_Result (New_Id)
5652 and then Skip_Controlling_Formals
5656 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5657 if Ctype >= Subtype_Conformant
5658 and then not Predicates_Match (Old_Type, New_Type)
5661 ("\predicate of return type does not match!", New_Id);
5664 ("\return type does not match!", New_Id);
5670 -- Ada 2005 (AI-231): In case of anonymous access types check the
5671 -- null-exclusion and access-to-constant attributes match.
5673 if Ada_Version >= Ada_2005
5674 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5676 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5677 or else Is_Access_Constant (Etype (Old_Type)) /=
5678 Is_Access_Constant (Etype (New_Type)))
5680 Conformance_Error ("\return type does not match!", New_Id);
5684 -- If either is a function/operator and the other isn't, error
5686 elsif Old_Type /= Standard_Void_Type
5687 or else New_Type /= Standard_Void_Type
5689 Conformance_Error ("\functions can only match functions!", New_Id);
5693 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5694 -- If this is a renaming as body, refine error message to indicate that
5695 -- the conflict is with the original declaration. If the entity is not
5696 -- frozen, the conventions don't have to match, the one of the renamed
5697 -- entity is inherited.
5699 if Ctype >= Subtype_Conformant then
5700 if not Conventions_Match (Old_Id, New_Id) then
5701 if not Is_Frozen (New_Id) then
5704 elsif Present (Err_Loc)
5705 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5706 and then Present (Corresponding_Spec (Err_Loc))
5708 Error_Msg_Name_1 := Chars (New_Id);
5710 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5711 Conformance_Error ("\prior declaration for% has convention %!");
5714 Conformance_Error ("\calling conventions do not match!");
5719 elsif Is_Formal_Subprogram (Old_Id)
5720 or else Is_Formal_Subprogram (New_Id)
5721 or else (Is_Subprogram (New_Id)
5722 and then Present (Alias (New_Id))
5723 and then Is_Formal_Subprogram (Alias (New_Id)))
5726 ("\formal subprograms are not subtype conformant "
5727 & "(RM 6.3.1 (17/3))");
5731 -- Deal with parameters
5733 -- Note: we use the entity information, rather than going directly
5734 -- to the specification in the tree. This is not only simpler, but
5735 -- absolutely necessary for some cases of conformance tests between
5736 -- operators, where the declaration tree simply does not exist.
5738 Old_Formal := First_Formal (Old_Id);
5739 New_Formal := First_Formal (New_Id);
5740 while Present (Old_Formal) and then Present (New_Formal) loop
5741 if Is_Controlling_Formal (Old_Formal)
5742 and then Is_Controlling_Formal (New_Formal)
5743 and then Skip_Controlling_Formals
5745 -- The controlling formals will have different types when
5746 -- comparing an interface operation with its match, but both
5747 -- or neither must be access parameters.
5749 if Is_Access_Type (Etype (Old_Formal))
5751 Is_Access_Type (Etype (New_Formal))
5753 goto Skip_Controlling_Formal;
5756 ("\access parameter does not match!", New_Formal);
5760 -- Ada 2012: Mode conformance also requires that formal parameters
5761 -- be both aliased, or neither.
5763 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5764 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5766 ("\aliased parameter mismatch!", New_Formal);
5770 if Ctype = Fully_Conformant then
5772 -- Names must match. Error message is more accurate if we do
5773 -- this before checking that the types of the formals match.
5775 if Chars (Old_Formal) /= Chars (New_Formal) then
5776 Conformance_Error ("\name& does not match!", New_Formal);
5778 -- Set error posted flag on new formal as well to stop
5779 -- junk cascaded messages in some cases.
5781 Set_Error_Posted (New_Formal);
5785 -- Null exclusion must match
5787 if Null_Exclusion_Present (Parent (Old_Formal))
5789 Null_Exclusion_Present (Parent (New_Formal))
5791 -- Only give error if both come from source. This should be
5792 -- investigated some time, since it should not be needed ???
5794 if Comes_From_Source (Old_Formal)
5796 Comes_From_Source (New_Formal)
5799 ("\null exclusion for& does not match", New_Formal);
5801 -- Mark error posted on the new formal to avoid duplicated
5802 -- complaint about types not matching.
5804 Set_Error_Posted (New_Formal);
5809 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5810 -- case occurs whenever a subprogram is being renamed and one of its
5811 -- parameters imposes a null exclusion. For example:
5813 -- type T is null record;
5814 -- type Acc_T is access T;
5815 -- subtype Acc_T_Sub is Acc_T;
5817 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5818 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5821 Old_Formal_Base := Etype (Old_Formal);
5822 New_Formal_Base := Etype (New_Formal);
5825 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5826 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5829 Access_Types_Match := Ada_Version >= Ada_2005
5831 -- Ensure that this rule is only applied when New_Id is a
5832 -- renaming of Old_Id.
5834 and then Nkind (Parent (Parent (New_Id))) =
5835 N_Subprogram_Renaming_Declaration
5836 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5837 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5838 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5840 -- Now handle the allowed access-type case
5842 and then Is_Access_Type (Old_Formal_Base)
5843 and then Is_Access_Type (New_Formal_Base)
5845 -- The type kinds must match. The only exception occurs with
5846 -- multiple generics of the form:
5849 -- type F is private; type A is private;
5850 -- type F_Ptr is access F; type A_Ptr is access A;
5851 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5852 -- package F_Pack is ... package A_Pack is
5853 -- package F_Inst is
5854 -- new F_Pack (A, A_Ptr, A_P);
5856 -- When checking for conformance between the parameters of A_P
5857 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5858 -- because the compiler has transformed A_Ptr into a subtype of
5859 -- F_Ptr. We catch this case in the code below.
5861 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5863 (Is_Generic_Type (Old_Formal_Base)
5864 and then Is_Generic_Type (New_Formal_Base)
5865 and then Is_Internal (New_Formal_Base)
5866 and then Etype (Etype (New_Formal_Base)) =
5868 and then Directly_Designated_Type (Old_Formal_Base) =
5869 Directly_Designated_Type (New_Formal_Base)
5870 and then ((Is_Itype (Old_Formal_Base)
5871 and then (Can_Never_Be_Null (Old_Formal_Base)
5872 or else Is_Access_Constant
5875 (Is_Itype (New_Formal_Base)
5876 and then (Can_Never_Be_Null (New_Formal_Base)
5877 or else Is_Access_Constant
5878 (New_Formal_Base))));
5880 -- Types must always match. In the visible part of an instance,
5881 -- usual overloading rules for dispatching operations apply, and
5882 -- we check base types (not the actual subtypes).
5884 if In_Instance_Visible_Part
5885 and then Is_Dispatching_Operation (New_Id)
5887 if not Conforming_Types
5888 (T1 => Base_Type (Etype (Old_Formal)),
5889 T2 => Base_Type (Etype (New_Formal)),
5891 Get_Inst => Get_Inst)
5892 and then not Access_Types_Match
5894 Conformance_Error ("\type of & does not match!", New_Formal);
5898 elsif not Conforming_Types
5899 (T1 => Old_Formal_Base,
5900 T2 => New_Formal_Base,
5902 Get_Inst => Get_Inst)
5903 and then not Access_Types_Match
5905 -- Don't give error message if old type is Any_Type. This test
5906 -- avoids some cascaded errors, e.g. in case of a bad spec.
5908 if Errmsg and then Old_Formal_Base = Any_Type then
5911 if Ctype >= Subtype_Conformant
5913 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5916 ("\predicate of & does not match!", New_Formal);
5919 ("\type of & does not match!", New_Formal);
5921 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5923 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5931 -- For mode conformance, mode must match
5933 if Ctype >= Mode_Conformant then
5934 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5935 if not Ekind_In (New_Id, E_Function, E_Procedure)
5936 or else not Is_Primitive_Wrapper (New_Id)
5938 Conformance_Error ("\mode of & does not match!", New_Formal);
5942 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5944 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5946 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5949 ("\mode of & does not match!", New_Formal);
5956 -- Part of mode conformance for access types is having the same
5957 -- constant modifier.
5959 elsif Access_Types_Match
5960 and then Is_Access_Constant (Old_Formal_Base) /=
5961 Is_Access_Constant (New_Formal_Base)
5964 ("\constant modifier does not match!", New_Formal);
5969 if Ctype >= Subtype_Conformant then
5971 -- Ada 2005 (AI-231): In case of anonymous access types check
5972 -- the null-exclusion and access-to-constant attributes must
5973 -- match. For null exclusion, we test the types rather than the
5974 -- formals themselves, since the attribute is only set reliably
5975 -- on the formals in the Ada 95 case, and we exclude the case
5976 -- where Old_Formal is marked as controlling, to avoid errors
5977 -- when matching completing bodies with dispatching declarations
5978 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5980 if Ada_Version >= Ada_2005
5981 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5982 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5984 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5985 Can_Never_Be_Null (Etype (New_Formal))
5987 not Is_Controlling_Formal (Old_Formal))
5989 Is_Access_Constant (Etype (Old_Formal)) /=
5990 Is_Access_Constant (Etype (New_Formal)))
5992 -- Do not complain if error already posted on New_Formal. This
5993 -- avoids some redundant error messages.
5995 and then not Error_Posted (New_Formal)
5997 -- It is allowed to omit the null-exclusion in case of stream
5998 -- attribute subprograms. We recognize stream subprograms
5999 -- through their TSS-generated suffix.
6002 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6005 if TSS_Name /= TSS_Stream_Read
6006 and then TSS_Name /= TSS_Stream_Write
6007 and then TSS_Name /= TSS_Stream_Input
6008 and then TSS_Name /= TSS_Stream_Output
6010 -- Here we have a definite conformance error. It is worth
6011 -- special casing the error message for the case of a
6012 -- controlling formal (which excludes null).
6014 if Is_Controlling_Formal (New_Formal) then
6015 Error_Msg_Node_2 := Scope (New_Formal);
6017 ("\controlling formal & of & excludes null, "
6018 & "declaration must exclude null as well",
6021 -- Normal case (couldn't we give more detail here???)
6025 ("\type of & does not match!", New_Formal);
6034 -- Full conformance checks
6036 if Ctype = Fully_Conformant then
6038 -- We have checked already that names match
6040 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6042 -- Check default expressions for in parameters
6045 NewD : constant Boolean :=
6046 Present (Default_Value (New_Formal));
6047 OldD : constant Boolean :=
6048 Present (Default_Value (Old_Formal));
6050 if NewD or OldD then
6052 -- The old default value has been analyzed because the
6053 -- current full declaration will have frozen everything
6054 -- before. The new default value has not been analyzed,
6055 -- so analyze it now before we check for conformance.
6058 Push_Scope (New_Id);
6059 Preanalyze_Spec_Expression
6060 (Default_Value (New_Formal), Etype (New_Formal));
6064 if not (NewD and OldD)
6065 or else not Fully_Conformant_Expressions
6066 (Default_Value (Old_Formal),
6067 Default_Value (New_Formal))
6070 ("\default expression for & does not match!",
6079 -- A couple of special checks for Ada 83 mode. These checks are
6080 -- skipped if either entity is an operator in package Standard,
6081 -- or if either old or new instance is not from the source program.
6083 if Ada_Version = Ada_83
6084 and then Sloc (Old_Id) > Standard_Location
6085 and then Sloc (New_Id) > Standard_Location
6086 and then Comes_From_Source (Old_Id)
6087 and then Comes_From_Source (New_Id)
6090 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6091 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6094 -- Explicit IN must be present or absent in both cases. This
6095 -- test is required only in the full conformance case.
6097 if In_Present (Old_Param) /= In_Present (New_Param)
6098 and then Ctype = Fully_Conformant
6101 ("\(Ada 83) IN must appear in both declarations",
6106 -- Grouping (use of comma in param lists) must be the same
6107 -- This is where we catch a misconformance like:
6110 -- A : Integer; B : Integer
6112 -- which are represented identically in the tree except
6113 -- for the setting of the flags More_Ids and Prev_Ids.
6115 if More_Ids (Old_Param) /= More_Ids (New_Param)
6116 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6119 ("\grouping of & does not match!", New_Formal);
6125 -- This label is required when skipping controlling formals
6127 <<Skip_Controlling_Formal>>
6129 Next_Formal (Old_Formal);
6130 Next_Formal (New_Formal);
6133 if Present (Old_Formal) then
6134 Conformance_Error ("\too few parameters!");
6137 elsif Present (New_Formal) then
6138 Conformance_Error ("\too many parameters!", New_Formal);
6141 end Check_Conformance;
6143 -----------------------
6144 -- Check_Conventions --
6145 -----------------------
6147 procedure Check_Conventions (Typ : Entity_Id) is
6148 Ifaces_List : Elist_Id;
6150 procedure Check_Convention (Op : Entity_Id);
6151 -- Verify that the convention of inherited dispatching operation Op is
6152 -- consistent among all subprograms it overrides. In order to minimize
6153 -- the search, Search_From is utilized to designate a specific point in
6154 -- the list rather than iterating over the whole list once more.
6156 ----------------------
6157 -- Check_Convention --
6158 ----------------------
6160 procedure Check_Convention (Op : Entity_Id) is
6161 Op_Conv : constant Convention_Id := Convention (Op);
6162 Iface_Conv : Convention_Id;
6163 Iface_Elmt : Elmt_Id;
6164 Iface_Prim_Elmt : Elmt_Id;
6165 Iface_Prim : Entity_Id;
6168 Iface_Elmt := First_Elmt (Ifaces_List);
6169 while Present (Iface_Elmt) loop
6171 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6172 while Present (Iface_Prim_Elmt) loop
6173 Iface_Prim := Node (Iface_Prim_Elmt);
6174 Iface_Conv := Convention (Iface_Prim);
6176 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6177 and then Iface_Conv /= Op_Conv
6180 ("inconsistent conventions in primitive operations", Typ);
6182 Error_Msg_Name_1 := Chars (Op);
6183 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6184 Error_Msg_Sloc := Sloc (Op);
6186 if Comes_From_Source (Op) or else No (Alias (Op)) then
6187 if not Present (Overridden_Operation (Op)) then
6188 Error_Msg_N ("\\primitive % defined #", Typ);
6191 ("\\overriding operation % with "
6192 & "convention % defined #", Typ);
6195 else pragma Assert (Present (Alias (Op)));
6196 Error_Msg_Sloc := Sloc (Alias (Op));
6197 Error_Msg_N ("\\inherited operation % with "
6198 & "convention % defined #", Typ);
6201 Error_Msg_Name_1 := Chars (Op);
6202 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6203 Error_Msg_Sloc := Sloc (Iface_Prim);
6204 Error_Msg_N ("\\overridden operation % with "
6205 & "convention % defined #", Typ);
6207 -- Avoid cascading errors
6212 Next_Elmt (Iface_Prim_Elmt);
6215 Next_Elmt (Iface_Elmt);
6217 end Check_Convention;
6221 Prim_Op : Entity_Id;
6222 Prim_Op_Elmt : Elmt_Id;
6224 -- Start of processing for Check_Conventions
6227 if not Has_Interfaces (Typ) then
6231 Collect_Interfaces (Typ, Ifaces_List);
6233 -- The algorithm checks every overriding dispatching operation against
6234 -- all the corresponding overridden dispatching operations, detecting
6235 -- differences in conventions.
6237 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6238 while Present (Prim_Op_Elmt) loop
6239 Prim_Op := Node (Prim_Op_Elmt);
6241 -- A small optimization: skip the predefined dispatching operations
6242 -- since they always have the same convention.
6244 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6245 Check_Convention (Prim_Op);
6248 Next_Elmt (Prim_Op_Elmt);
6250 end Check_Conventions;
6252 ------------------------------
6253 -- Check_Delayed_Subprogram --
6254 ------------------------------
6256 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6257 procedure Possible_Freeze (T : Entity_Id);
6258 -- T is the type of either a formal parameter or of the return type. If
6259 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6260 -- itself must be delayed.
6262 ---------------------
6263 -- Possible_Freeze --
6264 ---------------------
6266 procedure Possible_Freeze (T : Entity_Id) is
6267 Scop : constant Entity_Id := Scope (Designator);
6270 -- If the subprogram appears within a package instance (which may be
6271 -- the wrapper package of a subprogram instance) the freeze node for
6272 -- that package will freeze the subprogram at the proper place, so
6273 -- do not emit a freeze node for the subprogram, given that it may
6274 -- appear in the wrong scope.
6276 if Ekind (Scop) = E_Package
6277 and then not Comes_From_Source (Scop)
6278 and then Is_Generic_Instance (Scop)
6282 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6283 Set_Has_Delayed_Freeze (Designator);
6285 elsif Is_Access_Type (T)
6286 and then Has_Delayed_Freeze (Designated_Type (T))
6287 and then not Is_Frozen (Designated_Type (T))
6289 Set_Has_Delayed_Freeze (Designator);
6291 end Possible_Freeze;
6297 -- Start of processing for Check_Delayed_Subprogram
6300 -- All subprograms, including abstract subprograms, may need a freeze
6301 -- node if some formal type or the return type needs one.
6303 Possible_Freeze (Etype (Designator));
6304 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6306 -- Need delayed freeze if any of the formal types themselves need a
6307 -- delayed freeze and are not yet frozen.
6309 F := First_Formal (Designator);
6310 while Present (F) loop
6311 Possible_Freeze (Etype (F));
6312 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6316 -- Mark functions that return by reference. Note that it cannot be done
6317 -- for delayed_freeze subprograms because the underlying returned type
6318 -- may not be known yet (for private types).
6320 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6322 Typ : constant Entity_Id := Etype (Designator);
6323 Utyp : constant Entity_Id := Underlying_Type (Typ);
6326 if Is_Limited_View (Typ) then
6327 Set_Returns_By_Ref (Designator);
6329 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6330 Set_Returns_By_Ref (Designator);
6334 end Check_Delayed_Subprogram;
6336 ------------------------------------
6337 -- Check_Discriminant_Conformance --
6338 ------------------------------------
6340 procedure Check_Discriminant_Conformance
6345 Old_Discr : Entity_Id := First_Discriminant (Prev);
6346 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6347 New_Discr_Id : Entity_Id;
6348 New_Discr_Type : Entity_Id;
6350 procedure Conformance_Error (Msg : String; N : Node_Id);
6351 -- Post error message for conformance error on given node. Two messages
6352 -- are output. The first points to the previous declaration with a
6353 -- general "no conformance" message. The second is the detailed reason,
6354 -- supplied as Msg. The parameter N provide information for a possible
6355 -- & insertion in the message.
6357 -----------------------
6358 -- Conformance_Error --
6359 -----------------------
6361 procedure Conformance_Error (Msg : String; N : Node_Id) is
6363 Error_Msg_Sloc := Sloc (Prev_Loc);
6364 Error_Msg_N -- CODEFIX
6365 ("not fully conformant with declaration#!", N);
6366 Error_Msg_NE (Msg, N, N);
6367 end Conformance_Error;
6369 -- Start of processing for Check_Discriminant_Conformance
6372 while Present (Old_Discr) and then Present (New_Discr) loop
6373 New_Discr_Id := Defining_Identifier (New_Discr);
6375 -- The subtype mark of the discriminant on the full type has not
6376 -- been analyzed so we do it here. For an access discriminant a new
6379 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6381 Access_Definition (N, Discriminant_Type (New_Discr));
6384 Find_Type (Discriminant_Type (New_Discr));
6385 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6387 -- Ada 2005: if the discriminant definition carries a null
6388 -- exclusion, create an itype to check properly for consistency
6389 -- with partial declaration.
6391 if Is_Access_Type (New_Discr_Type)
6392 and then Null_Exclusion_Present (New_Discr)
6395 Create_Null_Excluding_Itype
6396 (T => New_Discr_Type,
6397 Related_Nod => New_Discr,
6398 Scope_Id => Current_Scope);
6402 if not Conforming_Types
6403 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6405 Conformance_Error ("type of & does not match!", New_Discr_Id);
6408 -- Treat the new discriminant as an occurrence of the old one,
6409 -- for navigation purposes, and fill in some semantic
6410 -- information, for completeness.
6412 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6413 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6414 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6419 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6420 Conformance_Error ("name & does not match!", New_Discr_Id);
6424 -- Default expressions must match
6427 NewD : constant Boolean :=
6428 Present (Expression (New_Discr));
6429 OldD : constant Boolean :=
6430 Present (Expression (Parent (Old_Discr)));
6433 if NewD or OldD then
6435 -- The old default value has been analyzed and expanded,
6436 -- because the current full declaration will have frozen
6437 -- everything before. The new default values have not been
6438 -- expanded, so expand now to check conformance.
6441 Preanalyze_Spec_Expression
6442 (Expression (New_Discr), New_Discr_Type);
6445 if not (NewD and OldD)
6446 or else not Fully_Conformant_Expressions
6447 (Expression (Parent (Old_Discr)),
6448 Expression (New_Discr))
6452 ("default expression for & does not match!",
6459 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6461 if Ada_Version = Ada_83 then
6463 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6466 -- Grouping (use of comma in param lists) must be the same
6467 -- This is where we catch a misconformance like:
6470 -- A : Integer; B : Integer
6472 -- which are represented identically in the tree except
6473 -- for the setting of the flags More_Ids and Prev_Ids.
6475 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6476 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6479 ("grouping of & does not match!", New_Discr_Id);
6485 Next_Discriminant (Old_Discr);
6489 if Present (Old_Discr) then
6490 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6493 elsif Present (New_Discr) then
6495 ("too many discriminants!", Defining_Identifier (New_Discr));
6498 end Check_Discriminant_Conformance;
6500 ----------------------------
6501 -- Check_Fully_Conformant --
6502 ----------------------------
6504 procedure Check_Fully_Conformant
6505 (New_Id : Entity_Id;
6507 Err_Loc : Node_Id := Empty)
6510 pragma Warnings (Off, Result);
6513 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6514 end Check_Fully_Conformant;
6516 --------------------------
6517 -- Check_Limited_Return --
6518 --------------------------
6520 procedure Check_Limited_Return
6526 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6527 -- replaced by anonymous access results. This is an incompatibility with
6528 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6529 -- controllable with special switch. ???
6531 -- A limited interface that is not immutably limited is OK
6533 if Is_Limited_Interface (R_Type)
6535 not (Is_Task_Interface (R_Type)
6536 or else Is_Protected_Interface (R_Type)
6537 or else Is_Synchronized_Interface (R_Type))
6541 elsif Is_Limited_Type (R_Type)
6542 and then not Is_Interface (R_Type)
6543 and then Comes_From_Source (N)
6544 and then not In_Instance_Body
6545 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6547 -- Error in Ada 2005
6549 if Ada_Version >= Ada_2005
6550 and then not Debug_Flag_Dot_L
6551 and then not GNAT_Mode
6554 ("(Ada 2005) cannot copy object of a limited type "
6555 & "(RM-2005 6.5(5.5/2))", Expr);
6557 if Is_Limited_View (R_Type) then
6559 ("\return by reference not permitted in Ada 2005", Expr);
6562 -- Warn in Ada 95 mode, to give folks a heads up about this
6565 -- In GNAT mode, this is just a warning, to allow it to be evilly
6566 -- turned off. Otherwise it is a real error.
6568 -- In a generic context, simplify the warning because it makes no
6569 -- sense to discuss pass-by-reference or copy.
6571 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6572 if Inside_A_Generic then
6574 ("return of limited object not permitted in Ada 2005 "
6575 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6577 elsif Is_Limited_View (R_Type) then
6579 ("return by reference not permitted in Ada 2005 "
6580 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6583 ("cannot copy object of a limited type in Ada 2005 "
6584 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6587 -- Ada 95 mode, and compatibility warnings disabled
6590 pragma Assert (Ada_Version <= Ada_95);
6591 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6592 return; -- skip continuation messages below
6595 if not Inside_A_Generic then
6597 ("\consider switching to return of access type", Expr);
6598 Explain_Limited_Type (R_Type, Expr);
6601 end Check_Limited_Return;
6603 ---------------------------
6604 -- Check_Mode_Conformant --
6605 ---------------------------
6607 procedure Check_Mode_Conformant
6608 (New_Id : Entity_Id;
6610 Err_Loc : Node_Id := Empty;
6611 Get_Inst : Boolean := False)
6614 pragma Warnings (Off, Result);
6617 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6618 end Check_Mode_Conformant;
6620 --------------------------------
6621 -- Check_Overriding_Indicator --
6622 --------------------------------
6624 procedure Check_Overriding_Indicator
6626 Overridden_Subp : Entity_Id;
6627 Is_Primitive : Boolean)
6633 -- No overriding indicator for literals
6635 if Ekind (Subp) = E_Enumeration_Literal then
6638 elsif Ekind (Subp) = E_Entry then
6639 Decl := Parent (Subp);
6641 -- No point in analyzing a malformed operator
6643 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6644 and then Error_Posted (Subp)
6649 Decl := Unit_Declaration_Node (Subp);
6652 if Nkind_In (Decl, N_Subprogram_Body,
6653 N_Subprogram_Body_Stub,
6654 N_Subprogram_Declaration,
6655 N_Abstract_Subprogram_Declaration,
6656 N_Subprogram_Renaming_Declaration)
6658 Spec := Specification (Decl);
6660 elsif Nkind (Decl) = N_Entry_Declaration then
6667 -- An overriding indication is illegal on a subprogram declared
6668 -- in a protected body, where there is no operation to override.
6670 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6671 and then Is_List_Member (Decl)
6672 and then Present (Parent (List_Containing (Decl)))
6673 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6676 ("illegal overriding indication in protected body", Decl);
6680 -- The overriding operation is type conformant with the overridden one,
6681 -- but the names of the formals are not required to match. If the names
6682 -- appear permuted in the overriding operation, this is a possible
6683 -- source of confusion that is worth diagnosing. Controlling formals
6684 -- often carry names that reflect the type, and it is not worthwhile
6685 -- requiring that their names match.
6687 if Present (Overridden_Subp)
6688 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6695 Form1 := First_Formal (Subp);
6696 Form2 := First_Formal (Overridden_Subp);
6698 -- If the overriding operation is a synchronized operation, skip
6699 -- the first parameter of the overridden operation, which is
6700 -- implicit in the new one. If the operation is declared in the
6701 -- body it is not primitive and all formals must match.
6703 if Is_Concurrent_Type (Scope (Subp))
6704 and then Is_Tagged_Type (Scope (Subp))
6705 and then not Has_Completion (Scope (Subp))
6707 Form2 := Next_Formal (Form2);
6710 if Present (Form1) then
6711 Form1 := Next_Formal (Form1);
6712 Form2 := Next_Formal (Form2);
6715 while Present (Form1) loop
6716 if not Is_Controlling_Formal (Form1)
6717 and then Present (Next_Formal (Form2))
6718 and then Chars (Form1) = Chars (Next_Formal (Form2))
6720 Error_Msg_Node_2 := Alias (Overridden_Subp);
6721 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6723 ("& does not match corresponding formal of&#",
6728 Next_Formal (Form1);
6729 Next_Formal (Form2);
6734 -- If there is an overridden subprogram, then check that there is no
6735 -- "not overriding" indicator, and mark the subprogram as overriding.
6737 -- This is not done if the overridden subprogram is marked as hidden,
6738 -- which can occur for the case of inherited controlled operations
6739 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6740 -- subprogram is not itself hidden or we are within a generic instance,
6741 -- in which case the hidden flag may have been modified for the
6742 -- expansion of the instance.
6744 -- (Note: This condition could probably be simplified, leaving out the
6745 -- testing for the specific controlled cases, but it seems safer and
6746 -- clearer this way, and echoes similar special-case tests of this
6747 -- kind in other places.)
6749 if Present (Overridden_Subp)
6750 and then (not Is_Hidden (Overridden_Subp)
6752 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6755 and then Present (Alias (Overridden_Subp))
6756 and then (not Is_Hidden (Alias (Overridden_Subp))
6757 or else In_Instance)))
6759 if Must_Not_Override (Spec) then
6760 Error_Msg_Sloc := Sloc (Overridden_Subp);
6762 if Ekind (Subp) = E_Entry then
6764 ("entry & overrides inherited operation #", Spec, Subp);
6767 ("subprogram & overrides inherited operation #", Spec, Subp);
6770 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6771 -- as an extension of Root_Controlled, and thus has a useless Adjust
6772 -- operation. This operation should not be inherited by other limited
6773 -- controlled types. An explicit Adjust for them is not overriding.
6775 elsif Must_Override (Spec)
6776 and then Chars (Overridden_Subp) = Name_Adjust
6777 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6778 and then Present (Alias (Overridden_Subp))
6779 and then In_Predefined_Unit (Alias (Overridden_Subp))
6782 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6783 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6785 elsif Is_Subprogram (Subp) then
6786 if Is_Init_Proc (Subp) then
6789 elsif No (Overridden_Operation (Subp)) then
6791 -- For entities generated by Derive_Subprograms the overridden
6792 -- operation is the inherited primitive (which is available
6793 -- through the attribute alias)
6795 if (Is_Dispatching_Operation (Subp)
6796 or else Is_Dispatching_Operation (Overridden_Subp))
6797 and then not Comes_From_Source (Overridden_Subp)
6798 and then Find_Dispatching_Type (Overridden_Subp) =
6799 Find_Dispatching_Type (Subp)
6800 and then Present (Alias (Overridden_Subp))
6801 and then Comes_From_Source (Alias (Overridden_Subp))
6803 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6804 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6807 Set_Overridden_Operation (Subp, Overridden_Subp);
6808 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6813 -- If primitive flag is set or this is a protected operation, then
6814 -- the operation is overriding at the point of its declaration, so
6815 -- warn if necessary. Otherwise it may have been declared before the
6816 -- operation it overrides and no check is required.
6819 and then not Must_Override (Spec)
6820 and then (Is_Primitive
6821 or else Ekind (Scope (Subp)) = E_Protected_Type)
6823 Style.Missing_Overriding (Decl, Subp);
6826 -- If Subp is an operator, it may override a predefined operation, if
6827 -- it is defined in the same scope as the type to which it applies.
6828 -- In that case Overridden_Subp is empty because of our implicit
6829 -- representation for predefined operators. We have to check whether the
6830 -- signature of Subp matches that of a predefined operator. Note that
6831 -- first argument provides the name of the operator, and the second
6832 -- argument the signature that may match that of a standard operation.
6833 -- If the indicator is overriding, then the operator must match a
6834 -- predefined signature, because we know already that there is no
6835 -- explicit overridden operation.
6837 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6838 if Must_Not_Override (Spec) then
6840 -- If this is not a primitive or a protected subprogram, then
6841 -- "not overriding" is illegal.
6844 and then Ekind (Scope (Subp)) /= E_Protected_Type
6846 Error_Msg_N ("overriding indicator only allowed "
6847 & "if subprogram is primitive", Subp);
6849 elsif Can_Override_Operator (Subp) then
6851 ("subprogram& overrides predefined operator ", Spec, Subp);
6854 elsif Must_Override (Spec) then
6855 if No (Overridden_Operation (Subp))
6856 and then not Can_Override_Operator (Subp)
6858 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6861 elsif not Error_Posted (Subp)
6862 and then Style_Check
6863 and then Can_Override_Operator (Subp)
6864 and then not In_Predefined_Unit (Subp)
6866 -- If style checks are enabled, indicate that the indicator is
6867 -- missing. However, at the point of declaration, the type of
6868 -- which this is a primitive operation may be private, in which
6869 -- case the indicator would be premature.
6871 if Has_Private_Declaration (Etype (Subp))
6872 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6876 Style.Missing_Overriding (Decl, Subp);
6880 elsif Must_Override (Spec) then
6881 if Ekind (Subp) = E_Entry then
6882 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6884 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6887 -- If the operation is marked "not overriding" and it's not primitive
6888 -- then an error is issued, unless this is an operation of a task or
6889 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6890 -- has been specified have already been checked above.
6892 elsif Must_Not_Override (Spec)
6893 and then not Is_Primitive
6894 and then Ekind (Subp) /= E_Entry
6895 and then Ekind (Scope (Subp)) /= E_Protected_Type
6898 ("overriding indicator only allowed if subprogram is primitive",
6902 end Check_Overriding_Indicator;
6908 -- Note: this procedure needs to know far too much about how the expander
6909 -- messes with exceptions. The use of the flag Exception_Junk and the
6910 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6911 -- works, but is not very clean. It would be better if the expansion
6912 -- routines would leave Original_Node working nicely, and we could use
6913 -- Original_Node here to ignore all the peculiar expander messing ???
6915 procedure Check_Returns
6919 Proc : Entity_Id := Empty)
6923 procedure Check_Statement_Sequence (L : List_Id);
6924 -- Internal recursive procedure to check a list of statements for proper
6925 -- termination by a return statement (or a transfer of control or a
6926 -- compound statement that is itself internally properly terminated).
6928 ------------------------------
6929 -- Check_Statement_Sequence --
6930 ------------------------------
6932 procedure Check_Statement_Sequence (L : List_Id) is
6937 function Assert_False return Boolean;
6938 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6939 -- rewritten as a null statement when assertions are off. The assert
6940 -- is not active, but it is still enough to kill the warning.
6946 function Assert_False return Boolean is
6947 Orig : constant Node_Id := Original_Node (Last_Stm);
6950 if Nkind (Orig) = N_Pragma
6951 and then Pragma_Name (Orig) = Name_Assert
6952 and then not Error_Posted (Orig)
6955 Arg : constant Node_Id :=
6956 First (Pragma_Argument_Associations (Orig));
6957 Exp : constant Node_Id := Expression (Arg);
6959 return Nkind (Exp) = N_Identifier
6960 and then Chars (Exp) = Name_False;
6970 Raise_Exception_Call : Boolean;
6971 -- Set True if statement sequence terminated by Raise_Exception call
6972 -- or a Reraise_Occurrence call.
6974 -- Start of processing for Check_Statement_Sequence
6977 Raise_Exception_Call := False;
6979 -- Get last real statement
6981 Last_Stm := Last (L);
6983 -- Deal with digging out exception handler statement sequences that
6984 -- have been transformed by the local raise to goto optimization.
6985 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6986 -- optimization has occurred, we are looking at something like:
6989 -- original stmts in block
6993 -- goto L1; | omitted if No_Exception_Propagation
6998 -- goto L3; -- skip handler when exception not raised
7000 -- <<L1>> -- target label for local exception
7014 -- and what we have to do is to dig out the estmts1 and estmts2
7015 -- sequences (which were the original sequences of statements in
7016 -- the exception handlers) and check them.
7018 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7023 exit when Nkind (Stm) /= N_Block_Statement;
7024 exit when not Exception_Junk (Stm);
7027 exit when Nkind (Stm) /= N_Label;
7028 exit when not Exception_Junk (Stm);
7029 Check_Statement_Sequence
7030 (Statements (Handled_Statement_Sequence (Next (Stm))));
7035 exit when Nkind (Stm) /= N_Goto_Statement;
7036 exit when not Exception_Junk (Stm);
7040 -- Don't count pragmas
7042 while Nkind (Last_Stm) = N_Pragma
7044 -- Don't count call to SS_Release (can happen after Raise_Exception)
7047 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7049 Nkind (Name (Last_Stm)) = N_Identifier
7051 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7053 -- Don't count exception junk
7056 (Nkind_In (Last_Stm, N_Goto_Statement,
7058 N_Object_Declaration)
7059 and then Exception_Junk (Last_Stm))
7060 or else Nkind (Last_Stm) in N_Push_xxx_Label
7061 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7063 -- Inserted code, such as finalization calls, is irrelevant: we only
7064 -- need to check original source.
7066 or else Is_Rewrite_Insertion (Last_Stm)
7071 -- Here we have the "real" last statement
7073 Kind := Nkind (Last_Stm);
7075 -- Transfer of control, OK. Note that in the No_Return procedure
7076 -- case, we already diagnosed any explicit return statements, so
7077 -- we can treat them as OK in this context.
7079 if Is_Transfer (Last_Stm) then
7082 -- Check cases of explicit non-indirect procedure calls
7084 elsif Kind = N_Procedure_Call_Statement
7085 and then Is_Entity_Name (Name (Last_Stm))
7087 -- Check call to Raise_Exception procedure which is treated
7088 -- specially, as is a call to Reraise_Occurrence.
7090 -- We suppress the warning in these cases since it is likely that
7091 -- the programmer really does not expect to deal with the case
7092 -- of Null_Occurrence, and thus would find a warning about a
7093 -- missing return curious, and raising Program_Error does not
7094 -- seem such a bad behavior if this does occur.
7096 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7097 -- behavior will be to raise Constraint_Error (see AI-329).
7099 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7101 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7103 Raise_Exception_Call := True;
7105 -- For Raise_Exception call, test first argument, if it is
7106 -- an attribute reference for a 'Identity call, then we know
7107 -- that the call cannot possibly return.
7110 Arg : constant Node_Id :=
7111 Original_Node (First_Actual (Last_Stm));
7113 if Nkind (Arg) = N_Attribute_Reference
7114 and then Attribute_Name (Arg) = Name_Identity
7121 -- If statement, need to look inside if there is an else and check
7122 -- each constituent statement sequence for proper termination.
7124 elsif Kind = N_If_Statement
7125 and then Present (Else_Statements (Last_Stm))
7127 Check_Statement_Sequence (Then_Statements (Last_Stm));
7128 Check_Statement_Sequence (Else_Statements (Last_Stm));
7130 if Present (Elsif_Parts (Last_Stm)) then
7132 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7135 while Present (Elsif_Part) loop
7136 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7144 -- Case statement, check each case for proper termination
7146 elsif Kind = N_Case_Statement then
7150 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7151 while Present (Case_Alt) loop
7152 Check_Statement_Sequence (Statements (Case_Alt));
7153 Next_Non_Pragma (Case_Alt);
7159 -- Block statement, check its handled sequence of statements
7161 elsif Kind = N_Block_Statement then
7167 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7176 -- Loop statement. If there is an iteration scheme, we can definitely
7177 -- fall out of the loop. Similarly if there is an exit statement, we
7178 -- can fall out. In either case we need a following return.
7180 elsif Kind = N_Loop_Statement then
7181 if Present (Iteration_Scheme (Last_Stm))
7182 or else Has_Exit (Entity (Identifier (Last_Stm)))
7186 -- A loop with no exit statement or iteration scheme is either
7187 -- an infinite loop, or it has some other exit (raise/return).
7188 -- In either case, no warning is required.
7194 -- Timed entry call, check entry call and delay alternatives
7196 -- Note: in expanded code, the timed entry call has been converted
7197 -- to a set of expanded statements on which the check will work
7198 -- correctly in any case.
7200 elsif Kind = N_Timed_Entry_Call then
7202 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7203 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7206 -- If statement sequence of entry call alternative is missing,
7207 -- then we can definitely fall through, and we post the error
7208 -- message on the entry call alternative itself.
7210 if No (Statements (ECA)) then
7213 -- If statement sequence of delay alternative is missing, then
7214 -- we can definitely fall through, and we post the error
7215 -- message on the delay alternative itself.
7217 -- Note: if both ECA and DCA are missing the return, then we
7218 -- post only one message, should be enough to fix the bugs.
7219 -- If not we will get a message next time on the DCA when the
7222 elsif No (Statements (DCA)) then
7225 -- Else check both statement sequences
7228 Check_Statement_Sequence (Statements (ECA));
7229 Check_Statement_Sequence (Statements (DCA));
7234 -- Conditional entry call, check entry call and else part
7236 -- Note: in expanded code, the conditional entry call has been
7237 -- converted to a set of expanded statements on which the check
7238 -- will work correctly in any case.
7240 elsif Kind = N_Conditional_Entry_Call then
7242 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7245 -- If statement sequence of entry call alternative is missing,
7246 -- then we can definitely fall through, and we post the error
7247 -- message on the entry call alternative itself.
7249 if No (Statements (ECA)) then
7252 -- Else check statement sequence and else part
7255 Check_Statement_Sequence (Statements (ECA));
7256 Check_Statement_Sequence (Else_Statements (Last_Stm));
7262 -- If we fall through, issue appropriate message
7266 -- Kill warning if last statement is a raise exception call,
7267 -- or a pragma Assert (False). Note that with assertions enabled,
7268 -- such a pragma has been converted into a raise exception call
7269 -- already, so the Assert_False is for the assertions off case.
7271 if not Raise_Exception_Call and then not Assert_False then
7273 -- In GNATprove mode, it is an error to have a missing return
7275 Error_Msg_Warn := SPARK_Mode /= On;
7277 -- Issue error message or warning
7280 ("RETURN statement missing following this statement<<!",
7283 ("\Program_Error ]<<!", Last_Stm);
7286 -- Note: we set Err even though we have not issued a warning
7287 -- because we still have a case of a missing return. This is
7288 -- an extremely marginal case, probably will never be noticed
7289 -- but we might as well get it right.
7293 -- Otherwise we have the case of a procedure marked No_Return
7296 if not Raise_Exception_Call then
7297 if GNATprove_Mode then
7299 ("implied return after this statement would have raised "
7300 & "Program_Error", Last_Stm);
7302 -- In normal compilation mode, do not warn on a generated call
7303 -- (e.g. in the body of a renaming as completion).
7305 elsif Comes_From_Source (Last_Stm) then
7307 ("implied return after this statement will raise "
7308 & "Program_Error??", Last_Stm);
7311 Error_Msg_Warn := SPARK_Mode /= On;
7313 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7317 RE : constant Node_Id :=
7318 Make_Raise_Program_Error (Sloc (Last_Stm),
7319 Reason => PE_Implicit_Return);
7321 Insert_After (Last_Stm, RE);
7325 end Check_Statement_Sequence;
7327 -- Start of processing for Check_Returns
7331 Check_Statement_Sequence (Statements (HSS));
7333 if Present (Exception_Handlers (HSS)) then
7334 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7335 while Present (Handler) loop
7336 Check_Statement_Sequence (Statements (Handler));
7337 Next_Non_Pragma (Handler);
7342 ----------------------------
7343 -- Check_Subprogram_Order --
7344 ----------------------------
7346 procedure Check_Subprogram_Order (N : Node_Id) is
7348 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7349 -- This is used to check if S1 > S2 in the sense required by this test,
7350 -- for example nameab < namec, but name2 < name10.
7352 -----------------------------
7353 -- Subprogram_Name_Greater --
7354 -----------------------------
7356 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7361 -- Deal with special case where names are identical except for a
7362 -- numerical suffix. These are handled specially, taking the numeric
7363 -- ordering from the suffix into account.
7366 while S1 (L1) in '0' .. '9' loop
7371 while S2 (L2) in '0' .. '9' loop
7375 -- If non-numeric parts non-equal, do straight compare
7377 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7380 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7381 -- that a missing suffix is treated as numeric zero in this test.
7385 while L1 < S1'Last loop
7387 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7391 while L2 < S2'Last loop
7393 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7398 end Subprogram_Name_Greater;
7400 -- Start of processing for Check_Subprogram_Order
7403 -- Check body in alpha order if this is option
7406 and then Style_Check_Order_Subprograms
7407 and then Nkind (N) = N_Subprogram_Body
7408 and then Comes_From_Source (N)
7409 and then In_Extended_Main_Source_Unit (N)
7413 renames Scope_Stack.Table
7414 (Scope_Stack.Last).Last_Subprogram_Name;
7416 Body_Id : constant Entity_Id :=
7417 Defining_Entity (Specification (N));
7420 Get_Decoded_Name_String (Chars (Body_Id));
7423 if Subprogram_Name_Greater
7424 (LSN.all, Name_Buffer (1 .. Name_Len))
7426 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7432 LSN := new String'(Name_Buffer (1 .. Name_Len));
7435 end Check_Subprogram_Order;
7437 ------------------------------
7438 -- Check_Subtype_Conformant --
7439 ------------------------------
7441 procedure Check_Subtype_Conformant
7442 (New_Id : Entity_Id;
7444 Err_Loc : Node_Id := Empty;
7445 Skip_Controlling_Formals : Boolean := False;
7446 Get_Inst : Boolean := False)
7449 pragma Warnings (Off, Result);
7452 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7453 Skip_Controlling_Formals => Skip_Controlling_Formals,
7454 Get_Inst => Get_Inst);
7455 end Check_Subtype_Conformant;
7457 -----------------------------------
7458 -- Check_Synchronized_Overriding --
7459 -----------------------------------
7461 procedure Check_Synchronized_Overriding
7462 (Def_Id : Entity_Id;
7463 Overridden_Subp : out Entity_Id)
7465 Ifaces_List : Elist_Id;
7469 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7470 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7471 -- subprogram is implemented by a protected procedure or entry,
7472 -- its first parameter must be out, in out, or access-to-variable.
7474 function Matches_Prefixed_View_Profile
7475 (Prim_Params : List_Id;
7476 Iface_Params : List_Id) return Boolean;
7477 -- Determine whether a subprogram's parameter profile Prim_Params
7478 -- matches that of a potentially overridden interface subprogram
7479 -- Iface_Params. Also determine if the type of first parameter of
7480 -- Iface_Params is an implemented interface.
7482 ----------------------
7483 -- Is_Valid_Formal --
7484 ----------------------
7486 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7489 Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
7491 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7492 and then not Constant_Present (Parameter_Type (Parent (F))));
7493 end Is_Valid_Formal;
7495 -----------------------------------
7496 -- Matches_Prefixed_View_Profile --
7497 -----------------------------------
7499 function Matches_Prefixed_View_Profile
7500 (Prim_Params : List_Id;
7501 Iface_Params : List_Id) return Boolean
7503 function Is_Implemented
7504 (Ifaces_List : Elist_Id;
7505 Iface : Entity_Id) return Boolean;
7506 -- Determine if Iface is implemented by the current task or
7509 --------------------
7510 -- Is_Implemented --
7511 --------------------
7513 function Is_Implemented
7514 (Ifaces_List : Elist_Id;
7515 Iface : Entity_Id) return Boolean
7517 Iface_Elmt : Elmt_Id;
7520 Iface_Elmt := First_Elmt (Ifaces_List);
7521 while Present (Iface_Elmt) loop
7522 if Node (Iface_Elmt) = Iface then
7526 Next_Elmt (Iface_Elmt);
7534 Iface_Id : Entity_Id;
7535 Iface_Param : Node_Id;
7536 Iface_Typ : Entity_Id;
7537 Prim_Id : Entity_Id;
7538 Prim_Param : Node_Id;
7539 Prim_Typ : Entity_Id;
7541 -- Start of processing for Matches_Prefixed_View_Profile
7544 Iface_Param := First (Iface_Params);
7545 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7547 if Is_Access_Type (Iface_Typ) then
7548 Iface_Typ := Designated_Type (Iface_Typ);
7551 Prim_Param := First (Prim_Params);
7553 -- The first parameter of the potentially overridden subprogram must
7554 -- be an interface implemented by Prim.
7556 if not Is_Interface (Iface_Typ)
7557 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7562 -- The checks on the object parameters are done, so move on to the
7563 -- rest of the parameters.
7565 if not In_Scope then
7570 while Present (Iface_Param) and then Present (Prim_Param) loop
7571 Iface_Id := Defining_Identifier (Iface_Param);
7572 Iface_Typ := Find_Parameter_Type (Iface_Param);
7574 Prim_Id := Defining_Identifier (Prim_Param);
7575 Prim_Typ := Find_Parameter_Type (Prim_Param);
7577 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7578 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7579 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7581 Iface_Typ := Designated_Type (Iface_Typ);
7582 Prim_Typ := Designated_Type (Prim_Typ);
7585 -- Case of multiple interface types inside a parameter profile
7587 -- (Obj_Param : in out Iface; ...; Param : Iface)
7589 -- If the interface type is implemented, then the matching type in
7590 -- the primitive should be the implementing record type.
7592 if Ekind (Iface_Typ) = E_Record_Type
7593 and then Is_Interface (Iface_Typ)
7594 and then Is_Implemented (Ifaces_List, Iface_Typ)
7596 if Prim_Typ /= Typ then
7600 -- The two parameters must be both mode and subtype conformant
7602 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7604 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7613 -- One of the two lists contains more parameters than the other
7615 if Present (Iface_Param) or else Present (Prim_Param) then
7620 end Matches_Prefixed_View_Profile;
7622 -- Start of processing for Check_Synchronized_Overriding
7625 Overridden_Subp := Empty;
7627 -- Def_Id must be an entry or a subprogram. We should skip predefined
7628 -- primitives internally generated by the front end; however at this
7629 -- stage predefined primitives are still not fully decorated. As a
7630 -- minor optimization we skip here internally generated subprograms.
7632 if (Ekind (Def_Id) /= E_Entry
7633 and then Ekind (Def_Id) /= E_Function
7634 and then Ekind (Def_Id) /= E_Procedure)
7635 or else not Comes_From_Source (Def_Id)
7640 -- Search for the concurrent declaration since it contains the list of
7641 -- all implemented interfaces. In this case, the subprogram is declared
7642 -- within the scope of a protected or a task type.
7644 if Present (Scope (Def_Id))
7645 and then Is_Concurrent_Type (Scope (Def_Id))
7646 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7648 Typ := Scope (Def_Id);
7651 -- The enclosing scope is not a synchronized type and the subprogram
7654 elsif No (First_Formal (Def_Id)) then
7657 -- The subprogram has formals and hence it may be a primitive of a
7661 Typ := Etype (First_Formal (Def_Id));
7663 if Is_Access_Type (Typ) then
7664 Typ := Directly_Designated_Type (Typ);
7667 if Is_Concurrent_Type (Typ)
7668 and then not Is_Generic_Actual_Type (Typ)
7672 -- This case occurs when the concurrent type is declared within a
7673 -- generic unit. As a result the corresponding record has been built
7674 -- and used as the type of the first formal, we just have to retrieve
7675 -- the corresponding concurrent type.
7677 elsif Is_Concurrent_Record_Type (Typ)
7678 and then not Is_Class_Wide_Type (Typ)
7679 and then Present (Corresponding_Concurrent_Type (Typ))
7681 Typ := Corresponding_Concurrent_Type (Typ);
7689 -- There is no overriding to check if this is an inherited operation in
7690 -- a type derivation for a generic actual.
7692 Collect_Interfaces (Typ, Ifaces_List);
7694 if Is_Empty_Elmt_List (Ifaces_List) then
7698 -- Determine whether entry or subprogram Def_Id overrides a primitive
7699 -- operation that belongs to one of the interfaces in Ifaces_List.
7702 Candidate : Entity_Id := Empty;
7703 Hom : Entity_Id := Empty;
7704 Subp : Entity_Id := Empty;
7707 -- Traverse the homonym chain, looking for a potentially overridden
7708 -- subprogram that belongs to an implemented interface.
7710 Hom := Current_Entity_In_Scope (Def_Id);
7711 while Present (Hom) loop
7715 or else not Is_Overloadable (Subp)
7716 or else not Is_Primitive (Subp)
7717 or else not Is_Dispatching_Operation (Subp)
7718 or else not Present (Find_Dispatching_Type (Subp))
7719 or else not Is_Interface (Find_Dispatching_Type (Subp))
7723 -- Entries and procedures can override abstract or null interface
7726 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7727 and then Ekind (Subp) = E_Procedure
7728 and then Matches_Prefixed_View_Profile
7729 (Parameter_Specifications (Parent (Def_Id)),
7730 Parameter_Specifications (Parent (Subp)))
7734 -- For an overridden subprogram Subp, check whether the mode
7735 -- of its first parameter is correct depending on the kind of
7736 -- synchronized type.
7739 Formal : constant Node_Id := First_Formal (Candidate);
7742 -- In order for an entry or a protected procedure to
7743 -- override, the first parameter of the overridden routine
7744 -- must be of mode "out", "in out", or access-to-variable.
7746 if Ekind_In (Candidate, E_Entry, E_Procedure)
7747 and then Is_Protected_Type (Typ)
7748 and then not Is_Valid_Formal (Formal)
7752 -- All other cases are OK since a task entry or routine does
7753 -- not have a restriction on the mode of the first parameter
7754 -- of the overridden interface routine.
7757 Overridden_Subp := Candidate;
7762 -- Functions can override abstract interface functions. Return
7763 -- types must be subtype conformant.
7765 elsif Ekind (Def_Id) = E_Function
7766 and then Ekind (Subp) = E_Function
7767 and then Matches_Prefixed_View_Profile
7768 (Parameter_Specifications (Parent (Def_Id)),
7769 Parameter_Specifications (Parent (Subp)))
7770 and then Conforming_Types
7771 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7775 -- If an inherited subprogram is implemented by a protected
7776 -- function, then the first parameter of the inherited
7777 -- subprogram shall be of mode in, but not an access-to-
7778 -- variable parameter (RM 9.4(11/9)).
7780 if Present (First_Formal (Subp))
7781 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7783 (not Is_Access_Type (Etype (First_Formal (Subp)))
7785 Is_Access_Constant (Etype (First_Formal (Subp))))
7787 Overridden_Subp := Subp;
7792 Hom := Homonym (Hom);
7795 -- After examining all candidates for overriding, we are left with
7796 -- the best match, which is a mode-incompatible interface routine.
7798 if In_Scope and then Present (Candidate) then
7799 Error_Msg_PT (Def_Id, Candidate);
7802 Overridden_Subp := Candidate;
7805 end Check_Synchronized_Overriding;
7807 ---------------------------
7808 -- Check_Type_Conformant --
7809 ---------------------------
7811 procedure Check_Type_Conformant
7812 (New_Id : Entity_Id;
7814 Err_Loc : Node_Id := Empty)
7817 pragma Warnings (Off, Result);
7820 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7821 end Check_Type_Conformant;
7823 ---------------------------
7824 -- Can_Override_Operator --
7825 ---------------------------
7827 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7831 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7835 Typ := Base_Type (Etype (First_Formal (Subp)));
7837 -- Check explicitly that the operation is a primitive of the type
7839 return Operator_Matches_Spec (Subp, Subp)
7840 and then not Is_Generic_Type (Typ)
7841 and then Scope (Subp) = Scope (Typ)
7842 and then not Is_Class_Wide_Type (Typ);
7844 end Can_Override_Operator;
7846 ----------------------
7847 -- Conforming_Types --
7848 ----------------------
7850 function Conforming_Types
7853 Ctype : Conformance_Type;
7854 Get_Inst : Boolean := False) return Boolean
7856 function Base_Types_Match
7858 Typ_2 : Entity_Id) return Boolean;
7859 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7860 -- in different scopes (e.g. parent and child instances), then verify
7861 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7862 -- the same subtype chain. The whole purpose of this procedure is to
7863 -- prevent spurious ambiguities in an instantiation that may arise if
7864 -- two distinct generic types are instantiated with the same actual.
7866 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7867 -- An access parameter can designate an incomplete type. If the
7868 -- incomplete type is the limited view of a type from a limited_
7869 -- with_clause, check whether the non-limited view is available.
7870 -- If it is a (non-limited) incomplete type, get the full view.
7872 function Matches_Limited_With_View
7874 Typ_2 : Entity_Id) return Boolean;
7875 -- Returns True if and only if either Typ_1 denotes a limited view of
7876 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7877 -- the limited with view of a type is used in a subprogram declaration
7878 -- and the subprogram body is in the scope of a regular with clause for
7879 -- the same unit. In such a case, the two type entities are considered
7880 -- identical for purposes of conformance checking.
7882 ----------------------
7883 -- Base_Types_Match --
7884 ----------------------
7886 function Base_Types_Match
7888 Typ_2 : Entity_Id) return Boolean
7890 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7891 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7894 if Typ_1 = Typ_2 then
7897 elsif Base_1 = Base_2 then
7899 -- The following is too permissive. A more precise test should
7900 -- check that the generic actual is an ancestor subtype of the
7903 -- See code in Find_Corresponding_Spec that applies an additional
7904 -- filter to handle accidental amiguities in instances.
7907 not Is_Generic_Actual_Type (Typ_1)
7908 or else not Is_Generic_Actual_Type (Typ_2)
7909 or else Scope (Typ_1) /= Scope (Typ_2);
7911 -- If Typ_2 is a generic actual type it is declared as the subtype of
7912 -- the actual. If that actual is itself a subtype we need to use its
7913 -- own base type to check for compatibility.
7915 elsif Ekind (Base_2) = Ekind (Typ_2)
7916 and then Base_1 = Base_Type (Base_2)
7920 elsif Ekind (Base_1) = Ekind (Typ_1)
7921 and then Base_2 = Base_Type (Base_1)
7928 end Base_Types_Match;
7930 --------------------------
7931 -- Find_Designated_Type --
7932 --------------------------
7934 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7938 Desig := Directly_Designated_Type (Typ);
7940 if Ekind (Desig) = E_Incomplete_Type then
7942 -- If regular incomplete type, get full view if available
7944 if Present (Full_View (Desig)) then
7945 Desig := Full_View (Desig);
7947 -- If limited view of a type, get non-limited view if available,
7948 -- and check again for a regular incomplete type.
7950 elsif Present (Non_Limited_View (Desig)) then
7951 Desig := Get_Full_View (Non_Limited_View (Desig));
7956 end Find_Designated_Type;
7958 -------------------------------
7959 -- Matches_Limited_With_View --
7960 -------------------------------
7962 function Matches_Limited_With_View
7964 Typ_2 : Entity_Id) return Boolean
7966 function Is_Matching_Limited_View
7968 View : Entity_Id) return Boolean;
7969 -- Determine whether non-limited view View denotes type Typ in some
7970 -- conformant fashion.
7972 ------------------------------
7973 -- Is_Matching_Limited_View --
7974 ------------------------------
7976 function Is_Matching_Limited_View
7978 View : Entity_Id) return Boolean
7980 Root_Typ : Entity_Id;
7981 Root_View : Entity_Id;
7984 -- The non-limited view directly denotes the type
7989 -- The type is a subtype of the non-limited view
7991 elsif Is_Subtype_Of (Typ, View) then
7994 -- Both the non-limited view and the type denote class-wide types
7996 elsif Is_Class_Wide_Type (Typ)
7997 and then Is_Class_Wide_Type (View)
7999 Root_Typ := Root_Type (Typ);
8000 Root_View := Root_Type (View);
8002 if Root_Typ = Root_View then
8005 -- An incomplete tagged type and its full view may receive two
8006 -- distinct class-wide types when the related package has not
8007 -- been analyzed yet.
8010 -- type T is tagged; -- CW_1
8011 -- type T is tagged null record; -- CW_2
8014 -- This is because the package lacks any semantic information
8015 -- that may eventually link both views of T. As a consequence,
8016 -- a client of the limited view of Pack will see CW_2 while a
8017 -- client of the non-limited view of Pack will see CW_1.
8019 elsif Is_Incomplete_Type (Root_Typ)
8020 and then Present (Full_View (Root_Typ))
8021 and then Full_View (Root_Typ) = Root_View
8025 elsif Is_Incomplete_Type (Root_View)
8026 and then Present (Full_View (Root_View))
8027 and then Full_View (Root_View) = Root_Typ
8034 end Is_Matching_Limited_View;
8036 -- Start of processing for Matches_Limited_With_View
8039 -- In some cases a type imported through a limited_with clause, and
8040 -- its non-limited view are both visible, for example in an anonymous
8041 -- access-to-class-wide type in a formal, or when building the body
8042 -- for a subprogram renaming after the subprogram has been frozen.
8043 -- In these cases both entities designate the same type. In addition,
8044 -- if one of them is an actual in an instance, it may be a subtype of
8045 -- the non-limited view of the other.
8047 if From_Limited_With (Typ_1)
8048 and then From_Limited_With (Typ_2)
8049 and then Available_View (Typ_1) = Available_View (Typ_2)
8053 elsif From_Limited_With (Typ_1) then
8054 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8056 elsif From_Limited_With (Typ_2) then
8057 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8062 end Matches_Limited_With_View;
8066 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8068 Type_1 : Entity_Id := T1;
8069 Type_2 : Entity_Id := T2;
8071 -- Start of processing for Conforming_Types
8074 -- The context is an instance association for a formal access-to-
8075 -- subprogram type; the formal parameter types require mapping because
8076 -- they may denote other formal parameters of the generic unit.
8079 Type_1 := Get_Instance_Of (T1);
8080 Type_2 := Get_Instance_Of (T2);
8083 -- If one of the types is a view of the other introduced by a limited
8084 -- with clause, treat these as conforming for all purposes.
8086 if Matches_Limited_With_View (T1, T2) then
8089 elsif Base_Types_Match (Type_1, Type_2) then
8090 if Ctype <= Mode_Conformant then
8095 Subtypes_Statically_Match (Type_1, Type_2)
8096 and then Dimensions_Match (Type_1, Type_2);
8099 elsif Is_Incomplete_Or_Private_Type (Type_1)
8100 and then Present (Full_View (Type_1))
8101 and then Base_Types_Match (Full_View (Type_1), Type_2)
8104 Ctype <= Mode_Conformant
8105 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8107 elsif Ekind (Type_2) = E_Incomplete_Type
8108 and then Present (Full_View (Type_2))
8109 and then Base_Types_Match (Type_1, Full_View (Type_2))
8112 Ctype <= Mode_Conformant
8113 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8115 elsif Is_Private_Type (Type_2)
8116 and then In_Instance
8117 and then Present (Full_View (Type_2))
8118 and then Base_Types_Match (Type_1, Full_View (Type_2))
8121 Ctype <= Mode_Conformant
8122 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8124 -- Another confusion between views in a nested instance with an
8125 -- actual private type whose full view is not in scope.
8127 elsif Ekind (Type_2) = E_Private_Subtype
8128 and then In_Instance
8129 and then Etype (Type_2) = Type_1
8133 -- In Ada 2012, incomplete types (including limited views) can appear
8134 -- as actuals in instantiations, where they are conformant to the
8135 -- corresponding incomplete formal.
8137 elsif Is_Incomplete_Type (Type_1)
8138 and then Is_Incomplete_Type (Type_2)
8139 and then In_Instance
8140 and then (Used_As_Generic_Actual (Type_1)
8141 or else Used_As_Generic_Actual (Type_2))
8146 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8147 -- treated recursively because they carry a signature. As far as
8148 -- conformance is concerned, convention plays no role, and either
8149 -- or both could be access to protected subprograms.
8151 Are_Anonymous_Access_To_Subprogram_Types :=
8152 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
8153 E_Anonymous_Access_Protected_Subprogram_Type)
8155 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
8156 E_Anonymous_Access_Protected_Subprogram_Type);
8158 -- Test anonymous access type case. For this case, static subtype
8159 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8160 -- the base types because we may have built internal subtype entities
8161 -- to handle null-excluding types (see Process_Formals).
8163 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8165 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8167 -- Ada 2005 (AI-254)
8169 or else Are_Anonymous_Access_To_Subprogram_Types
8172 Desig_1 : Entity_Id;
8173 Desig_2 : Entity_Id;
8176 -- In Ada 2005, access constant indicators must match for
8177 -- subtype conformance.
8179 if Ada_Version >= Ada_2005
8180 and then Ctype >= Subtype_Conformant
8182 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8187 Desig_1 := Find_Designated_Type (Type_1);
8188 Desig_2 := Find_Designated_Type (Type_2);
8190 -- If the context is an instance association for a formal
8191 -- access-to-subprogram type; formal access parameter designated
8192 -- types require mapping because they may denote other formal
8193 -- parameters of the generic unit.
8196 Desig_1 := Get_Instance_Of (Desig_1);
8197 Desig_2 := Get_Instance_Of (Desig_2);
8200 -- It is possible for a Class_Wide_Type to be introduced for an
8201 -- incomplete type, in which case there is a separate class_ wide
8202 -- type for the full view. The types conform if their Etypes
8203 -- conform, i.e. one may be the full view of the other. This can
8204 -- only happen in the context of an access parameter, other uses
8205 -- of an incomplete Class_Wide_Type are illegal.
8207 if Is_Class_Wide_Type (Desig_1)
8209 Is_Class_Wide_Type (Desig_2)
8213 (Etype (Base_Type (Desig_1)),
8214 Etype (Base_Type (Desig_2)), Ctype);
8216 elsif Are_Anonymous_Access_To_Subprogram_Types then
8217 if Ada_Version < Ada_2005 then
8219 Ctype = Type_Conformant
8220 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8222 -- We must check the conformance of the signatures themselves
8226 Conformant : Boolean;
8229 (Desig_1, Desig_2, Ctype, False, Conformant);
8234 -- A limited view of an actual matches the corresponding
8235 -- incomplete formal.
8237 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8238 and then From_Limited_With (Desig_2)
8239 and then Used_As_Generic_Actual (Etype (Desig_2))
8244 return Base_Type (Desig_1) = Base_Type (Desig_2)
8245 and then (Ctype = Type_Conformant
8247 Subtypes_Statically_Match (Desig_1, Desig_2));
8251 -- Otherwise definitely no match
8254 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8255 and then Is_Access_Type (Type_2))
8256 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8257 and then Is_Access_Type (Type_1)))
8260 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8262 May_Hide_Profile := True;
8267 end Conforming_Types;
8269 --------------------------
8270 -- Create_Extra_Formals --
8271 --------------------------
8273 procedure Create_Extra_Formals (E : Entity_Id) is
8274 First_Extra : Entity_Id := Empty;
8276 Last_Extra : Entity_Id := Empty;
8278 function Add_Extra_Formal
8279 (Assoc_Entity : Entity_Id;
8282 Suffix : String) return Entity_Id;
8283 -- Add an extra formal to the current list of formals and extra formals.
8284 -- The extra formal is added to the end of the list of extra formals,
8285 -- and also returned as the result. These formals are always of mode IN.
8286 -- The new formal has the type Typ, is declared in Scope, and its name
8287 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8288 -- The following suffixes are currently used. They should not be changed
8289 -- without coordinating with CodePeer, which makes use of these to
8290 -- provide better messages.
8292 -- O denotes the Constrained bit.
8293 -- L denotes the accessibility level.
8294 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8295 -- the full list in exp_ch6.BIP_Formal_Kind.
8297 ----------------------
8298 -- Add_Extra_Formal --
8299 ----------------------
8301 function Add_Extra_Formal
8302 (Assoc_Entity : Entity_Id;
8305 Suffix : String) return Entity_Id
8307 EF : constant Entity_Id :=
8308 Make_Defining_Identifier (Sloc (Assoc_Entity),
8309 Chars => New_External_Name (Chars (Assoc_Entity),
8313 -- A little optimization. Never generate an extra formal for the
8314 -- _init operand of an initialization procedure, since it could
8317 if Chars (Formal) = Name_uInit then
8321 Set_Ekind (EF, E_In_Parameter);
8322 Set_Actual_Subtype (EF, Typ);
8323 Set_Etype (EF, Typ);
8324 Set_Scope (EF, Scope);
8325 Set_Mechanism (EF, Default_Mechanism);
8326 Set_Formal_Validity (EF);
8328 if No (First_Extra) then
8330 Set_Extra_Formals (Scope, EF);
8333 if Present (Last_Extra) then
8334 Set_Extra_Formal (Last_Extra, EF);
8340 end Add_Extra_Formal;
8344 Formal_Type : Entity_Id;
8345 P_Formal : Entity_Id := Empty;
8347 -- Start of processing for Create_Extra_Formals
8350 -- We never generate extra formals if expansion is not active because we
8351 -- don't need them unless we are generating code.
8353 if not Expander_Active then
8357 -- No need to generate extra formals in interface thunks whose target
8358 -- primitive has no extra formals.
8360 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8364 -- If this is a derived subprogram then the subtypes of the parent
8365 -- subprogram's formal parameters will be used to determine the need
8366 -- for extra formals.
8368 if Is_Overloadable (E) and then Present (Alias (E)) then
8369 P_Formal := First_Formal (Alias (E));
8372 Formal := First_Formal (E);
8373 while Present (Formal) loop
8374 Last_Extra := Formal;
8375 Next_Formal (Formal);
8378 -- If Extra_Formals were already created, don't do it again. This
8379 -- situation may arise for subprogram types created as part of
8380 -- dispatching calls (see Expand_Dispatching_Call).
8382 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8386 -- If the subprogram is a predefined dispatching subprogram then don't
8387 -- generate any extra constrained or accessibility level formals. In
8388 -- general we suppress these for internal subprograms (by not calling
8389 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8390 -- generated stream attributes do get passed through because extra
8391 -- build-in-place formals are needed in some cases (limited 'Input).
8393 if Is_Predefined_Internal_Operation (E) then
8394 goto Test_For_Func_Result_Extras;
8397 Formal := First_Formal (E);
8398 while Present (Formal) loop
8400 -- Create extra formal for supporting the attribute 'Constrained.
8401 -- The case of a private type view without discriminants also
8402 -- requires the extra formal if the underlying type has defaulted
8405 if Ekind (Formal) /= E_In_Parameter then
8406 if Present (P_Formal) then
8407 Formal_Type := Etype (P_Formal);
8409 Formal_Type := Etype (Formal);
8412 -- Do not produce extra formals for Unchecked_Union parameters.
8413 -- Jump directly to the end of the loop.
8415 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8416 goto Skip_Extra_Formal_Generation;
8419 if not Has_Discriminants (Formal_Type)
8420 and then Ekind (Formal_Type) in Private_Kind
8421 and then Present (Underlying_Type (Formal_Type))
8423 Formal_Type := Underlying_Type (Formal_Type);
8426 -- Suppress the extra formal if formal's subtype is constrained or
8427 -- indefinite, or we're compiling for Ada 2012 and the underlying
8428 -- type is tagged and limited. In Ada 2012, a limited tagged type
8429 -- can have defaulted discriminants, but 'Constrained is required
8430 -- to return True, so the formal is never needed (see AI05-0214).
8431 -- Note that this ensures consistency of calling sequences for
8432 -- dispatching operations when some types in a class have defaults
8433 -- on discriminants and others do not (and requiring the extra
8434 -- formal would introduce distributed overhead).
8436 -- If the type does not have a completion yet, treat as prior to
8437 -- Ada 2012 for consistency.
8439 if Has_Discriminants (Formal_Type)
8440 and then not Is_Constrained (Formal_Type)
8441 and then Is_Definite_Subtype (Formal_Type)
8442 and then (Ada_Version < Ada_2012
8443 or else No (Underlying_Type (Formal_Type))
8445 (Is_Limited_Type (Formal_Type)
8448 (Underlying_Type (Formal_Type)))))
8450 Set_Extra_Constrained
8451 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8455 -- Create extra formal for supporting accessibility checking. This
8456 -- is done for both anonymous access formals and formals of named
8457 -- access types that are marked as controlling formals. The latter
8458 -- case can occur when Expand_Dispatching_Call creates a subprogram
8459 -- type and substitutes the types of access-to-class-wide actuals
8460 -- for the anonymous access-to-specific-type of controlling formals.
8461 -- Base_Type is applied because in cases where there is a null
8462 -- exclusion the formal may have an access subtype.
8464 -- This is suppressed if we specifically suppress accessibility
8465 -- checks at the package level for either the subprogram, or the
8466 -- package in which it resides. However, we do not suppress it
8467 -- simply if the scope has accessibility checks suppressed, since
8468 -- this could cause trouble when clients are compiled with a
8469 -- different suppression setting. The explicit checks at the
8470 -- package level are safe from this point of view.
8472 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8473 or else (Is_Controlling_Formal (Formal)
8474 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8476 (Explicit_Suppress (E, Accessibility_Check)
8478 Explicit_Suppress (Scope (E), Accessibility_Check))
8481 or else Present (Extra_Accessibility (P_Formal)))
8483 Set_Extra_Accessibility
8484 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8487 -- This label is required when skipping extra formal generation for
8488 -- Unchecked_Union parameters.
8490 <<Skip_Extra_Formal_Generation>>
8492 if Present (P_Formal) then
8493 Next_Formal (P_Formal);
8496 Next_Formal (Formal);
8499 <<Test_For_Func_Result_Extras>>
8501 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8502 -- function call is ... determined by the point of call ...".
8504 if Needs_Result_Accessibility_Level (E) then
8505 Set_Extra_Accessibility_Of_Result
8506 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8509 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8510 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8512 if Is_Build_In_Place_Function (E) then
8514 Result_Subt : constant Entity_Id := Etype (E);
8515 Formal_Typ : Entity_Id;
8516 Subp_Decl : Node_Id;
8517 Discard : Entity_Id;
8520 -- In the case of functions with unconstrained result subtypes,
8521 -- add a 4-state formal indicating whether the return object is
8522 -- allocated by the caller (1), or should be allocated by the
8523 -- callee on the secondary stack (2), in the global heap (3), or
8524 -- in a user-defined storage pool (4). For the moment we just use
8525 -- Natural for the type of this formal. Note that this formal
8526 -- isn't usually needed in the case where the result subtype is
8527 -- constrained, but it is needed when the function has a tagged
8528 -- result, because generally such functions can be called in a
8529 -- dispatching context and such calls must be handled like calls
8530 -- to a class-wide function.
8532 if Needs_BIP_Alloc_Form (E) then
8535 (E, Standard_Natural,
8536 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8538 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8539 -- use a user-defined pool. This formal is not added on
8540 -- ZFP as those targets do not support pools.
8542 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8545 (E, RTE (RE_Root_Storage_Pool_Ptr),
8546 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8550 -- In the case of functions whose result type needs finalization,
8551 -- add an extra formal which represents the finalization master.
8553 if Needs_BIP_Finalization_Master (E) then
8556 (E, RTE (RE_Finalization_Master_Ptr),
8557 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8560 -- When the result type contains tasks, add two extra formals: the
8561 -- master of the tasks to be created, and the caller's activation
8564 if Needs_BIP_Task_Actuals (E) then
8567 (E, RTE (RE_Master_Id),
8568 E, BIP_Formal_Suffix (BIP_Task_Master));
8571 (E, RTE (RE_Activation_Chain_Access),
8572 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8575 -- All build-in-place functions get an extra formal that will be
8576 -- passed the address of the return object within the caller.
8579 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8581 -- Incomplete_View_From_Limited_With is needed here because
8582 -- gigi gets confused if the designated type is the full view
8583 -- coming from a limited-with'ed package. In the normal case,
8584 -- (no limited with) Incomplete_View_From_Limited_With
8585 -- returns Result_Subt.
8587 Set_Directly_Designated_Type
8588 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8589 Set_Etype (Formal_Typ, Formal_Typ);
8590 Set_Depends_On_Private
8591 (Formal_Typ, Has_Private_Component (Formal_Typ));
8592 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8593 Set_Is_Access_Constant (Formal_Typ, False);
8595 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8596 -- the designated type comes from the limited view (for back-end
8599 Set_From_Limited_With
8600 (Formal_Typ, From_Limited_With (Result_Subt));
8602 Layout_Type (Formal_Typ);
8604 -- Force the definition of the Itype in case of internal function
8605 -- calls within the same or nested scope.
8607 if Is_Subprogram_Or_Generic_Subprogram (E) then
8608 Subp_Decl := Parent (E);
8610 -- The insertion point for an Itype reference should be after
8611 -- the unit declaration node of the subprogram. An exception
8612 -- to this are inherited operations from a parent type in which
8613 -- case the derived type acts as their parent.
8615 if Nkind_In (Subp_Decl, N_Function_Specification,
8616 N_Procedure_Specification)
8618 Subp_Decl := Parent (Subp_Decl);
8621 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8626 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8630 -- If this is an instance of a generic, we need to have extra formals
8633 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8634 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8636 end Create_Extra_Formals;
8638 -----------------------------
8639 -- Enter_Overloaded_Entity --
8640 -----------------------------
8642 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8643 function Matches_Predefined_Op return Boolean;
8644 -- This returns an approximation of whether S matches a predefined
8645 -- operator, based on the operator symbol, and the parameter and result
8646 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8648 ---------------------------
8649 -- Matches_Predefined_Op --
8650 ---------------------------
8652 function Matches_Predefined_Op return Boolean is
8653 Formal_1 : constant Entity_Id := First_Formal (S);
8654 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8655 Op : constant Name_Id := Chars (S);
8656 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8657 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8662 if Present (Formal_2) then
8664 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8667 -- All but "&" and "**" have same-types parameters
8676 if Type_1 /= Type_2 then
8681 -- Check parameter and result types
8689 Is_Boolean_Type (Result_Type)
8690 and then Result_Type = Type_1;
8696 Is_Integer_Type (Result_Type)
8697 and then Result_Type = Type_1;
8705 Is_Numeric_Type (Result_Type)
8706 and then Result_Type = Type_1;
8712 Is_Boolean_Type (Result_Type)
8713 and then not Is_Limited_Type (Type_1);
8721 Is_Boolean_Type (Result_Type)
8722 and then (Is_Array_Type (Type_1)
8723 or else Is_Scalar_Type (Type_1));
8725 when Name_Op_Concat =>
8726 return Is_Array_Type (Result_Type);
8728 when Name_Op_Expon =>
8730 (Is_Integer_Type (Result_Type)
8731 or else Is_Floating_Point_Type (Result_Type))
8732 and then Result_Type = Type_1
8733 and then Type_2 = Standard_Integer;
8736 raise Program_Error;
8749 Is_Numeric_Type (Result_Type)
8750 and then Result_Type = Type_1;
8754 Is_Boolean_Type (Result_Type)
8755 and then Result_Type = Type_1;
8758 raise Program_Error;
8761 end Matches_Predefined_Op;
8765 E : Entity_Id := Current_Entity_In_Scope (S);
8766 C_E : Entity_Id := Current_Entity (S);
8768 -- Start of processing for Enter_Overloaded_Entity
8772 Set_Has_Homonym (E);
8773 Set_Has_Homonym (S);
8776 Set_Is_Immediately_Visible (S);
8777 Set_Scope (S, Current_Scope);
8779 -- Chain new entity if front of homonym in current scope, so that
8780 -- homonyms are contiguous.
8782 if Present (E) and then E /= C_E then
8783 while Homonym (C_E) /= E loop
8784 C_E := Homonym (C_E);
8787 Set_Homonym (C_E, S);
8791 Set_Current_Entity (S);
8796 if Is_Inherited_Operation (S) then
8797 Append_Inherited_Subprogram (S);
8799 Append_Entity (S, Current_Scope);
8802 Set_Public_Status (S);
8804 if Debug_Flag_E then
8805 Write_Str ("New overloaded entity chain: ");
8806 Write_Name (Chars (S));
8809 while Present (E) loop
8810 Write_Str (" "); Write_Int (Int (E));
8817 -- Generate warning for hiding
8820 and then Comes_From_Source (S)
8821 and then In_Extended_Main_Source_Unit (S)
8828 -- Warn unless genuine overloading. Do not emit warning on
8829 -- hiding predefined operators in Standard (these are either an
8830 -- (artifact of our implicit declarations, or simple noise) but
8831 -- keep warning on a operator defined on a local subtype, because
8832 -- of the real danger that different operators may be applied in
8833 -- various parts of the program.
8835 -- Note that if E and S have the same scope, there is never any
8836 -- hiding. Either the two conflict, and the program is illegal,
8837 -- or S is overriding an implicit inherited subprogram.
8839 if Scope (E) /= Scope (S)
8840 and then (not Is_Overloadable (E)
8841 or else Subtype_Conformant (E, S))
8842 and then (Is_Immediately_Visible (E)
8843 or else Is_Potentially_Use_Visible (S))
8845 if Scope (E) = Standard_Standard then
8846 if Nkind (S) = N_Defining_Operator_Symbol
8847 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8849 and then Matches_Predefined_Op
8852 ("declaration of & hides predefined operator?h?", S);
8855 -- E not immediately within Standard
8858 Error_Msg_Sloc := Sloc (E);
8859 Error_Msg_N ("declaration of & hides one #?h?", S);
8864 end Enter_Overloaded_Entity;
8866 -----------------------------
8867 -- Check_Untagged_Equality --
8868 -----------------------------
8870 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8871 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8872 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8876 -- This check applies only if we have a subprogram declaration with an
8877 -- untagged record type that is conformant to the predefined op.
8879 if Nkind (Decl) /= N_Subprogram_Declaration
8880 or else not Is_Record_Type (Typ)
8881 or else Is_Tagged_Type (Typ)
8882 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
8887 -- In Ada 2012 case, we will output errors or warnings depending on
8888 -- the setting of debug flag -gnatd.E.
8890 if Ada_Version >= Ada_2012 then
8891 Error_Msg_Warn := Debug_Flag_Dot_EE;
8893 -- In earlier versions of Ada, nothing to do unless we are warning on
8894 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8897 if not Warn_On_Ada_2012_Compatibility then
8902 -- Cases where the type has already been frozen
8904 if Is_Frozen (Typ) then
8906 -- The check applies to a primitive operation, so check that type
8907 -- and equality operation are in the same scope.
8909 if Scope (Typ) /= Current_Scope then
8912 -- If the type is a generic actual (sub)type, the operation is not
8913 -- primitive either because the base type is declared elsewhere.
8915 elsif Is_Generic_Actual_Type (Typ) then
8918 -- Here we have a definite error of declaration after freezing
8921 if Ada_Version >= Ada_2012 then
8923 ("equality operator must be declared before type & is "
8924 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8926 -- In Ada 2012 mode with error turned to warning, output one
8927 -- more warning to warn that the equality operation may not
8928 -- compose. This is the consequence of ignoring the error.
8930 if Error_Msg_Warn then
8931 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8936 ("equality operator must be declared before type& is "
8937 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8940 -- If we are in the package body, we could just move the
8941 -- declaration to the package spec, so add a message saying that.
8943 if In_Package_Body (Scope (Typ)) then
8944 if Ada_Version >= Ada_2012 then
8946 ("\move declaration to package spec<<", Eq_Op);
8949 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8952 -- Otherwise try to find the freezing point for better message.
8955 Obj_Decl := Next (Parent (Typ));
8956 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8957 if Nkind (Obj_Decl) = N_Object_Declaration
8958 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8960 -- Freezing point, output warnings
8962 if Ada_Version >= Ada_2012 then
8964 ("type& is frozen by declaration??", Obj_Decl, Typ);
8966 ("\an equality operator cannot be declared after "
8971 ("type& is frozen by declaration (Ada 2012)?y?",
8974 ("\an equality operator cannot be declared after "
8975 & "this point (Ada 2012)?y?",
8981 -- If we reach generated code for subprogram declaration
8982 -- or body, it is the body that froze the type and the
8983 -- declaration is legal.
8985 elsif Sloc (Obj_Decl) = Sloc (Decl) then
8994 -- Here if type is not frozen yet. It is illegal to have a primitive
8995 -- equality declared in the private part if the type is visible.
8997 elsif not In_Same_List (Parent (Typ), Decl)
8998 and then not Is_Limited_Type (Typ)
9000 -- Shouldn't we give an RM reference here???
9002 if Ada_Version >= Ada_2012 then
9004 ("equality operator appears too late<<", Eq_Op);
9007 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9010 -- No error detected
9015 end Check_Untagged_Equality;
9017 -----------------------------
9018 -- Find_Corresponding_Spec --
9019 -----------------------------
9021 function Find_Corresponding_Spec
9023 Post_Error : Boolean := True) return Entity_Id
9025 Spec : constant Node_Id := Specification (N);
9026 Designator : constant Entity_Id := Defining_Entity (Spec);
9030 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9031 -- Even if fully conformant, a body may depend on a generic actual when
9032 -- the spec does not, or vice versa, in which case they were distinct
9033 -- entities in the generic.
9035 -------------------------------
9036 -- Different_Generic_Profile --
9037 -------------------------------
9039 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9042 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9043 -- Check that the types of corresponding formals have the same
9044 -- generic actual if any. We have to account for subtypes of a
9045 -- generic formal, declared between a spec and a body, which may
9046 -- appear distinct in an instance but matched in the generic, and
9047 -- the subtype may be used either in the spec or the body of the
9048 -- subprogram being checked.
9050 -------------------------
9051 -- Same_Generic_Actual --
9052 -------------------------
9054 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9056 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9057 -- Predicate to check whether S1 is a subtype of S2 in the source
9060 -------------------------
9061 -- Is_Declared_Subtype --
9062 -------------------------
9064 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9066 return Comes_From_Source (Parent (S1))
9067 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9068 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9069 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9070 end Is_Declared_Subtype;
9072 -- Start of processing for Same_Generic_Actual
9075 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9076 or else Is_Declared_Subtype (T1, T2)
9077 or else Is_Declared_Subtype (T2, T1);
9078 end Same_Generic_Actual;
9080 -- Start of processing for Different_Generic_Profile
9083 if not In_Instance then
9086 elsif Ekind (E) = E_Function
9087 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9092 F1 := First_Formal (Designator);
9093 F2 := First_Formal (E);
9094 while Present (F1) loop
9095 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9104 end Different_Generic_Profile;
9106 -- Start of processing for Find_Corresponding_Spec
9109 E := Current_Entity (Designator);
9110 while Present (E) loop
9112 -- We are looking for a matching spec. It must have the same scope,
9113 -- and the same name, and either be type conformant, or be the case
9114 -- of a library procedure spec and its body (which belong to one
9115 -- another regardless of whether they are type conformant or not).
9117 if Scope (E) = Current_Scope then
9118 if Current_Scope = Standard_Standard
9119 or else (Ekind (E) = Ekind (Designator)
9120 and then Type_Conformant (E, Designator))
9122 -- Within an instantiation, we know that spec and body are
9123 -- subtype conformant, because they were subtype conformant in
9124 -- the generic. We choose the subtype-conformant entity here as
9125 -- well, to resolve spurious ambiguities in the instance that
9126 -- were not present in the generic (i.e. when two different
9127 -- types are given the same actual). If we are looking for a
9128 -- spec to match a body, full conformance is expected.
9132 -- Inherit the convention and "ghostness" of the matching
9133 -- spec to ensure proper full and subtype conformance.
9135 Set_Convention (Designator, Convention (E));
9137 -- Skip past subprogram bodies and subprogram renamings that
9138 -- may appear to have a matching spec, but that aren't fully
9139 -- conformant with it. That can occur in cases where an
9140 -- actual type causes unrelated homographs in the instance.
9142 if Nkind_In (N, N_Subprogram_Body,
9143 N_Subprogram_Renaming_Declaration)
9144 and then Present (Homonym (E))
9145 and then not Fully_Conformant (Designator, E)
9149 elsif not Subtype_Conformant (Designator, E) then
9152 elsif Different_Generic_Profile (E) then
9157 -- Ada 2012 (AI05-0165): For internally generated bodies of
9158 -- null procedures locate the internally generated spec. We
9159 -- enforce mode conformance since a tagged type may inherit
9160 -- from interfaces several null primitives which differ only
9161 -- in the mode of the formals.
9163 if not (Comes_From_Source (E))
9164 and then Is_Null_Procedure (E)
9165 and then not Mode_Conformant (Designator, E)
9169 -- For null procedures coming from source that are completions,
9170 -- analysis of the generated body will establish the link.
9172 elsif Comes_From_Source (E)
9173 and then Nkind (Spec) = N_Procedure_Specification
9174 and then Null_Present (Spec)
9178 -- Expression functions can be completions, but cannot be
9179 -- completed by an explicit body.
9181 elsif Comes_From_Source (E)
9182 and then Comes_From_Source (N)
9183 and then Nkind (N) = N_Subprogram_Body
9184 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9185 N_Expression_Function
9187 Error_Msg_Sloc := Sloc (E);
9188 Error_Msg_N ("body conflicts with expression function#", N);
9191 elsif not Has_Completion (E) then
9192 if Nkind (N) /= N_Subprogram_Body_Stub then
9193 Set_Corresponding_Spec (N, E);
9196 Set_Has_Completion (E);
9199 elsif Nkind (Parent (N)) = N_Subunit then
9201 -- If this is the proper body of a subunit, the completion
9202 -- flag is set when analyzing the stub.
9206 -- If E is an internal function with a controlling result that
9207 -- was created for an operation inherited by a null extension,
9208 -- it may be overridden by a body without a previous spec (one
9209 -- more reason why these should be shunned). In that case we
9210 -- remove the generated body if present, because the current
9211 -- one is the explicit overriding.
9213 elsif Ekind (E) = E_Function
9214 and then Ada_Version >= Ada_2005
9215 and then not Comes_From_Source (E)
9216 and then Has_Controlling_Result (E)
9217 and then Is_Null_Extension (Etype (E))
9218 and then Comes_From_Source (Spec)
9220 Set_Has_Completion (E, False);
9223 and then Nkind (Parent (E)) = N_Function_Specification
9226 (Unit_Declaration_Node
9227 (Corresponding_Body (Unit_Declaration_Node (E))));
9231 -- If expansion is disabled, or if the wrapper function has
9232 -- not been generated yet, this a late body overriding an
9233 -- inherited operation, or it is an overriding by some other
9234 -- declaration before the controlling result is frozen. In
9235 -- either case this is a declaration of a new entity.
9241 -- If the body already exists, then this is an error unless
9242 -- the previous declaration is the implicit declaration of a
9243 -- derived subprogram. It is also legal for an instance to
9244 -- contain type conformant overloadable declarations (but the
9245 -- generic declaration may not), per 8.3(26/2).
9247 elsif No (Alias (E))
9248 and then not Is_Intrinsic_Subprogram (E)
9249 and then not In_Instance
9252 Error_Msg_Sloc := Sloc (E);
9254 if Is_Imported (E) then
9256 ("body not allowed for imported subprogram & declared#",
9259 Error_Msg_NE ("duplicate body for & declared#", N, E);
9263 -- Child units cannot be overloaded, so a conformance mismatch
9264 -- between body and a previous spec is an error.
9266 elsif Is_Child_Unit (E)
9268 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9270 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9275 ("body of child unit does not match previous declaration", N);
9283 -- On exit, we know that no previous declaration of subprogram exists
9286 end Find_Corresponding_Spec;
9288 ----------------------
9289 -- Fully_Conformant --
9290 ----------------------
9292 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9295 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9297 end Fully_Conformant;
9299 ----------------------------------
9300 -- Fully_Conformant_Expressions --
9301 ----------------------------------
9303 function Fully_Conformant_Expressions
9304 (Given_E1 : Node_Id;
9306 Report : Boolean := False) return Boolean
9308 E1 : constant Node_Id := Original_Node (Given_E1);
9309 E2 : constant Node_Id := Original_Node (Given_E2);
9310 -- We always test conformance on original nodes, since it is possible
9311 -- for analysis and/or expansion to make things look as though they
9312 -- conform when they do not, e.g. by converting 1+2 into 3.
9314 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9317 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9318 -- Compare elements of two lists for conformance. Elements have to be
9319 -- conformant, and actuals inserted as default parameters do not match
9320 -- explicit actuals with the same value.
9322 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9323 -- Compare an operator node with a function call
9329 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9331 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9338 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9343 if L1 = No_List then
9349 if L2 = No_List then
9355 -- Compare two lists, skipping rewrite insertions (we want to compare
9356 -- the original trees, not the expanded versions).
9359 if Is_Rewrite_Insertion (N1) then
9361 elsif Is_Rewrite_Insertion (N2) then
9367 elsif not FCE (N1, N2) then
9380 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9381 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9386 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9391 Act := First (Actuals);
9393 if Nkind (Op_Node) in N_Binary_Op then
9394 if not FCE (Left_Opnd (Op_Node), Act) then
9401 return Present (Act)
9402 and then FCE (Right_Opnd (Op_Node), Act)
9403 and then No (Next (Act));
9411 -- Start of processing for Fully_Conformant_Expressions
9416 -- Nonconformant if paren count does not match. Note: if some idiot
9417 -- complains that we don't do this right for more than 3 levels of
9418 -- parentheses, they will be treated with the respect they deserve.
9420 if Paren_Count (E1) /= Paren_Count (E2) then
9423 -- If same entities are referenced, then they are conformant even if
9424 -- they have different forms (RM 8.3.1(19-20)).
9426 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9427 if Present (Entity (E1)) then
9428 Result := Entity (E1) = Entity (E2)
9430 -- One may be a discriminant that has been replaced by the
9431 -- corresponding discriminal.
9434 (Chars (Entity (E1)) = Chars (Entity (E2))
9435 and then Ekind (Entity (E1)) = E_Discriminant
9436 and then Ekind (Entity (E2)) = E_In_Parameter)
9438 -- The discriminant of a protected type is transformed into
9439 -- a local constant and then into a parameter of a protected
9443 (Ekind (Entity (E1)) = E_Constant
9444 and then Ekind (Entity (E2)) = E_In_Parameter
9445 and then Present (Discriminal_Link (Entity (E1)))
9446 and then Discriminal_Link (Entity (E1)) =
9447 Discriminal_Link (Entity (E2)))
9449 -- AI12-050: The loop variables of quantified expressions match
9450 -- if they have the same identifier, even though they may have
9451 -- different entities.
9454 (Chars (Entity (E1)) = Chars (Entity (E2))
9455 and then Ekind (Entity (E1)) = E_Loop_Parameter
9456 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9458 -- A call to an instantiation of Unchecked_Conversion is
9459 -- rewritten with the name of the generated function created for
9460 -- the instance, and this must be special-cased.
9463 (Ekind (Entity (E1)) = E_Function
9464 and then Is_Intrinsic_Subprogram (Entity (E1))
9465 and then Is_Generic_Instance (Entity (E1))
9466 and then Entity (E2) = Alias (Entity (E1)));
9467 if Report and not Result then
9469 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9471 ("Meaning of& differs because of declaration#", E1, E2);
9476 elsif Nkind (E1) = N_Expanded_Name
9477 and then Nkind (E2) = N_Expanded_Name
9478 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9479 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9481 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9484 -- Identifiers in component associations don't always have
9485 -- entities, but their names must conform.
9487 return Nkind (E1) = N_Identifier
9488 and then Nkind (E2) = N_Identifier
9489 and then Chars (E1) = Chars (E2);
9492 elsif Nkind (E1) = N_Character_Literal
9493 and then Nkind (E2) = N_Expanded_Name
9495 return Nkind (Selector_Name (E2)) = N_Character_Literal
9496 and then Chars (E1) = Chars (Selector_Name (E2));
9498 elsif Nkind (E2) = N_Character_Literal
9499 and then Nkind (E1) = N_Expanded_Name
9501 return Nkind (Selector_Name (E1)) = N_Character_Literal
9502 and then Chars (E2) = Chars (Selector_Name (E1));
9504 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9505 return FCO (E1, E2);
9507 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9508 return FCO (E2, E1);
9510 -- Otherwise we must have the same syntactic entity
9512 elsif Nkind (E1) /= Nkind (E2) then
9515 -- At this point, we specialize by node type
9521 FCL (Expressions (E1), Expressions (E2))
9523 FCL (Component_Associations (E1),
9524 Component_Associations (E2));
9527 if Nkind (Expression (E1)) = N_Qualified_Expression
9529 Nkind (Expression (E2)) = N_Qualified_Expression
9531 return FCE (Expression (E1), Expression (E2));
9533 -- Check that the subtype marks and any constraints
9538 Indic1 : constant Node_Id := Expression (E1);
9539 Indic2 : constant Node_Id := Expression (E2);
9544 if Nkind (Indic1) /= N_Subtype_Indication then
9546 Nkind (Indic2) /= N_Subtype_Indication
9547 and then Entity (Indic1) = Entity (Indic2);
9549 elsif Nkind (Indic2) /= N_Subtype_Indication then
9551 Nkind (Indic1) /= N_Subtype_Indication
9552 and then Entity (Indic1) = Entity (Indic2);
9555 if Entity (Subtype_Mark (Indic1)) /=
9556 Entity (Subtype_Mark (Indic2))
9561 Elt1 := First (Constraints (Constraint (Indic1)));
9562 Elt2 := First (Constraints (Constraint (Indic2)));
9563 while Present (Elt1) and then Present (Elt2) loop
9564 if not FCE (Elt1, Elt2) then
9577 when N_Attribute_Reference =>
9579 Attribute_Name (E1) = Attribute_Name (E2)
9580 and then FCL (Expressions (E1), Expressions (E2));
9584 Entity (E1) = Entity (E2)
9585 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9586 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9588 when N_Membership_Test
9592 FCE (Left_Opnd (E1), Left_Opnd (E2))
9594 FCE (Right_Opnd (E1), Right_Opnd (E2));
9596 when N_Case_Expression =>
9602 if not FCE (Expression (E1), Expression (E2)) then
9606 Alt1 := First (Alternatives (E1));
9607 Alt2 := First (Alternatives (E2));
9609 if Present (Alt1) /= Present (Alt2) then
9611 elsif No (Alt1) then
9615 if not FCE (Expression (Alt1), Expression (Alt2))
9616 or else not FCL (Discrete_Choices (Alt1),
9617 Discrete_Choices (Alt2))
9628 when N_Character_Literal =>
9630 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9632 when N_Component_Association =>
9634 FCL (Choices (E1), Choices (E2))
9636 FCE (Expression (E1), Expression (E2));
9638 when N_Explicit_Dereference =>
9640 FCE (Prefix (E1), Prefix (E2));
9642 when N_Extension_Aggregate =>
9644 FCL (Expressions (E1), Expressions (E2))
9645 and then Null_Record_Present (E1) =
9646 Null_Record_Present (E2)
9647 and then FCL (Component_Associations (E1),
9648 Component_Associations (E2));
9650 when N_Function_Call =>
9652 FCE (Name (E1), Name (E2))
9654 FCL (Parameter_Associations (E1),
9655 Parameter_Associations (E2));
9657 when N_If_Expression =>
9659 FCL (Expressions (E1), Expressions (E2));
9661 when N_Indexed_Component =>
9663 FCE (Prefix (E1), Prefix (E2))
9665 FCL (Expressions (E1), Expressions (E2));
9667 when N_Integer_Literal =>
9668 return (Intval (E1) = Intval (E2));
9673 when N_Operator_Symbol =>
9675 Chars (E1) = Chars (E2);
9677 when N_Others_Choice =>
9680 when N_Parameter_Association =>
9682 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9683 and then FCE (Explicit_Actual_Parameter (E1),
9684 Explicit_Actual_Parameter (E2));
9686 when N_Qualified_Expression
9688 | N_Unchecked_Type_Conversion
9691 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9693 FCE (Expression (E1), Expression (E2));
9695 when N_Quantified_Expression =>
9696 if not FCE (Condition (E1), Condition (E2)) then
9700 if Present (Loop_Parameter_Specification (E1))
9701 and then Present (Loop_Parameter_Specification (E2))
9704 L1 : constant Node_Id :=
9705 Loop_Parameter_Specification (E1);
9706 L2 : constant Node_Id :=
9707 Loop_Parameter_Specification (E2);
9711 Reverse_Present (L1) = Reverse_Present (L2)
9713 FCE (Defining_Identifier (L1),
9714 Defining_Identifier (L2))
9716 FCE (Discrete_Subtype_Definition (L1),
9717 Discrete_Subtype_Definition (L2));
9720 elsif Present (Iterator_Specification (E1))
9721 and then Present (Iterator_Specification (E2))
9724 I1 : constant Node_Id := Iterator_Specification (E1);
9725 I2 : constant Node_Id := Iterator_Specification (E2);
9729 FCE (Defining_Identifier (I1),
9730 Defining_Identifier (I2))
9732 Of_Present (I1) = Of_Present (I2)
9734 Reverse_Present (I1) = Reverse_Present (I2)
9735 and then FCE (Name (I1), Name (I2))
9736 and then FCE (Subtype_Indication (I1),
9737 Subtype_Indication (I2));
9740 -- The quantified expressions used different specifications to
9741 -- walk their respective ranges.
9749 FCE (Low_Bound (E1), Low_Bound (E2))
9751 FCE (High_Bound (E1), High_Bound (E2));
9753 when N_Real_Literal =>
9754 return (Realval (E1) = Realval (E2));
9756 when N_Selected_Component =>
9758 FCE (Prefix (E1), Prefix (E2))
9760 FCE (Selector_Name (E1), Selector_Name (E2));
9764 FCE (Prefix (E1), Prefix (E2))
9766 FCE (Discrete_Range (E1), Discrete_Range (E2));
9768 when N_String_Literal =>
9770 S1 : constant String_Id := Strval (E1);
9771 S2 : constant String_Id := Strval (E2);
9772 L1 : constant Nat := String_Length (S1);
9773 L2 : constant Nat := String_Length (S2);
9780 for J in 1 .. L1 loop
9781 if Get_String_Char (S1, J) /=
9782 Get_String_Char (S2, J)
9794 Entity (E1) = Entity (E2)
9796 FCE (Right_Opnd (E1), Right_Opnd (E2));
9798 -- All other node types cannot appear in this context. Strictly
9799 -- we should raise a fatal internal error. Instead we just ignore
9800 -- the nodes. This means that if anyone makes a mistake in the
9801 -- expander and mucks an expression tree irretrievably, the result
9802 -- will be a failure to detect a (probably very obscure) case
9803 -- of non-conformance, which is better than bombing on some
9804 -- case where two expressions do in fact conform.
9810 end Fully_Conformant_Expressions;
9812 ----------------------------------------
9813 -- Fully_Conformant_Discrete_Subtypes --
9814 ----------------------------------------
9816 function Fully_Conformant_Discrete_Subtypes
9817 (Given_S1 : Node_Id;
9818 Given_S2 : Node_Id) return Boolean
9820 S1 : constant Node_Id := Original_Node (Given_S1);
9821 S2 : constant Node_Id := Original_Node (Given_S2);
9823 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9824 -- Special-case for a bound given by a discriminant, which in the body
9825 -- is replaced with the discriminal of the enclosing type.
9827 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9828 -- Check both bounds
9830 -----------------------
9831 -- Conforming_Bounds --
9832 -----------------------
9834 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9836 if Is_Entity_Name (B1)
9837 and then Is_Entity_Name (B2)
9838 and then Ekind (Entity (B1)) = E_Discriminant
9840 return Chars (B1) = Chars (B2);
9843 return Fully_Conformant_Expressions (B1, B2);
9845 end Conforming_Bounds;
9847 -----------------------
9848 -- Conforming_Ranges --
9849 -----------------------
9851 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9854 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9856 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9857 end Conforming_Ranges;
9859 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9862 if Nkind (S1) /= Nkind (S2) then
9865 elsif Is_Entity_Name (S1) then
9866 return Entity (S1) = Entity (S2);
9868 elsif Nkind (S1) = N_Range then
9869 return Conforming_Ranges (S1, S2);
9871 elsif Nkind (S1) = N_Subtype_Indication then
9873 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9876 (Range_Expression (Constraint (S1)),
9877 Range_Expression (Constraint (S2)));
9881 end Fully_Conformant_Discrete_Subtypes;
9883 --------------------
9884 -- Install_Entity --
9885 --------------------
9887 procedure Install_Entity (E : Entity_Id) is
9888 Prev : constant Entity_Id := Current_Entity (E);
9890 Set_Is_Immediately_Visible (E);
9891 Set_Current_Entity (E);
9892 Set_Homonym (E, Prev);
9895 ---------------------
9896 -- Install_Formals --
9897 ---------------------
9899 procedure Install_Formals (Id : Entity_Id) is
9902 F := First_Formal (Id);
9903 while Present (F) loop
9907 end Install_Formals;
9909 -----------------------------
9910 -- Is_Interface_Conformant --
9911 -----------------------------
9913 function Is_Interface_Conformant
9914 (Tagged_Type : Entity_Id;
9915 Iface_Prim : Entity_Id;
9916 Prim : Entity_Id) return Boolean
9918 -- The operation may in fact be an inherited (implicit) operation
9919 -- rather than the original interface primitive, so retrieve the
9920 -- ultimate ancestor.
9922 Iface : constant Entity_Id :=
9923 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9924 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9926 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9927 -- Return the controlling formal of Prim
9929 ------------------------
9930 -- Controlling_Formal --
9931 ------------------------
9933 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9937 E := First_Entity (Prim);
9938 while Present (E) loop
9939 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9947 end Controlling_Formal;
9951 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9952 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9954 -- Start of processing for Is_Interface_Conformant
9957 pragma Assert (Is_Subprogram (Iface_Prim)
9958 and then Is_Subprogram (Prim)
9959 and then Is_Dispatching_Operation (Iface_Prim)
9960 and then Is_Dispatching_Operation (Prim));
9962 pragma Assert (Is_Interface (Iface)
9963 or else (Present (Alias (Iface_Prim))
9966 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9968 if Prim = Iface_Prim
9969 or else not Is_Subprogram (Prim)
9970 or else Ekind (Prim) /= Ekind (Iface_Prim)
9971 or else not Is_Dispatching_Operation (Prim)
9972 or else Scope (Prim) /= Scope (Tagged_Type)
9974 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9975 or else not Primitive_Names_Match (Iface_Prim, Prim)
9979 -- The mode of the controlling formals must match
9981 elsif Present (Iface_Ctrl_F)
9982 and then Present (Prim_Ctrl_F)
9983 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9987 -- Case of a procedure, or a function whose result type matches the
9988 -- result type of the interface primitive, or a function that has no
9989 -- controlling result (I or access I).
9991 elsif Ekind (Iface_Prim) = E_Procedure
9992 or else Etype (Prim) = Etype (Iface_Prim)
9993 or else not Has_Controlling_Result (Prim)
9995 return Type_Conformant
9996 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9998 -- Case of a function returning an interface, or an access to one. Check
9999 -- that the return types correspond.
10001 elsif Implements_Interface (Typ, Iface) then
10002 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10004 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10009 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10010 Skip_Controlling_Formals => True);
10016 end Is_Interface_Conformant;
10018 ---------------------------------
10019 -- Is_Non_Overriding_Operation --
10020 ---------------------------------
10022 function Is_Non_Overriding_Operation
10023 (Prev_E : Entity_Id;
10024 New_E : Entity_Id) return Boolean
10026 Formal : Entity_Id;
10028 G_Typ : Entity_Id := Empty;
10030 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10031 -- If F_Type is a derived type associated with a generic actual subtype,
10032 -- then return its Generic_Parent_Type attribute, else return Empty.
10034 function Types_Correspond
10035 (P_Type : Entity_Id;
10036 N_Type : Entity_Id) return Boolean;
10037 -- Returns true if and only if the types (or designated types in the
10038 -- case of anonymous access types) are the same or N_Type is derived
10039 -- directly or indirectly from P_Type.
10041 -----------------------------
10042 -- Get_Generic_Parent_Type --
10043 -----------------------------
10045 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10051 if Is_Derived_Type (F_Typ)
10052 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10054 -- The tree must be traversed to determine the parent subtype in
10055 -- the generic unit, which unfortunately isn't always available
10056 -- via semantic attributes. ??? (Note: The use of Original_Node
10057 -- is needed for cases where a full derived type has been
10060 -- If the parent type is a scalar type, the derivation creates
10061 -- an anonymous base type for it, and the source type is its
10064 if Is_Scalar_Type (F_Typ)
10065 and then not Comes_From_Source (F_Typ)
10069 (Original_Node (Parent (First_Subtype (F_Typ))));
10071 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10073 if Nkind (Defn) = N_Derived_Type_Definition then
10074 Indic := Subtype_Indication (Defn);
10076 if Nkind (Indic) = N_Subtype_Indication then
10077 G_Typ := Entity (Subtype_Mark (Indic));
10079 G_Typ := Entity (Indic);
10082 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10083 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10085 return Generic_Parent_Type (Parent (G_Typ));
10091 end Get_Generic_Parent_Type;
10093 ----------------------
10094 -- Types_Correspond --
10095 ----------------------
10097 function Types_Correspond
10098 (P_Type : Entity_Id;
10099 N_Type : Entity_Id) return Boolean
10101 Prev_Type : Entity_Id := Base_Type (P_Type);
10102 New_Type : Entity_Id := Base_Type (N_Type);
10105 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10106 Prev_Type := Designated_Type (Prev_Type);
10109 if Ekind (New_Type) = E_Anonymous_Access_Type then
10110 New_Type := Designated_Type (New_Type);
10113 if Prev_Type = New_Type then
10116 elsif not Is_Class_Wide_Type (New_Type) then
10117 while Etype (New_Type) /= New_Type loop
10118 New_Type := Etype (New_Type);
10120 if New_Type = Prev_Type then
10126 end Types_Correspond;
10128 -- Start of processing for Is_Non_Overriding_Operation
10131 -- In the case where both operations are implicit derived subprograms
10132 -- then neither overrides the other. This can only occur in certain
10133 -- obscure cases (e.g., derivation from homographs created in a generic
10136 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10139 elsif Ekind (Current_Scope) = E_Package
10140 and then Is_Generic_Instance (Current_Scope)
10141 and then In_Private_Part (Current_Scope)
10142 and then Comes_From_Source (New_E)
10144 -- We examine the formals and result type of the inherited operation,
10145 -- to determine whether their type is derived from (the instance of)
10146 -- a generic type. The first such formal or result type is the one
10149 Formal := First_Formal (Prev_E);
10151 while Present (Formal) loop
10152 F_Typ := Base_Type (Etype (Formal));
10154 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10155 F_Typ := Designated_Type (F_Typ);
10158 G_Typ := Get_Generic_Parent_Type (F_Typ);
10159 exit when Present (G_Typ);
10161 Next_Formal (Formal);
10164 -- If the function dispatches on result check the result type
10166 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10167 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10174 -- If the generic type is a private type, then the original operation
10175 -- was not overriding in the generic, because there was no primitive
10176 -- operation to override.
10178 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10179 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10180 N_Formal_Private_Type_Definition
10184 -- The generic parent type is the ancestor of a formal derived
10185 -- type declaration. We need to check whether it has a primitive
10186 -- operation that should be overridden by New_E in the generic.
10190 P_Formal : Entity_Id;
10191 N_Formal : Entity_Id;
10194 P_Prim : Entity_Id;
10195 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10198 while Present (Prim_Elt) loop
10199 P_Prim := Node (Prim_Elt);
10201 if Chars (P_Prim) = Chars (New_E)
10202 and then Ekind (P_Prim) = Ekind (New_E)
10204 P_Formal := First_Formal (P_Prim);
10205 N_Formal := First_Formal (New_E);
10206 while Present (P_Formal) and then Present (N_Formal) loop
10207 P_Typ := Etype (P_Formal);
10208 N_Typ := Etype (N_Formal);
10210 if not Types_Correspond (P_Typ, N_Typ) then
10214 Next_Entity (P_Formal);
10215 Next_Entity (N_Formal);
10218 -- Found a matching primitive operation belonging to the
10219 -- formal ancestor type, so the new subprogram is
10223 and then No (N_Formal)
10224 and then (Ekind (New_E) /= E_Function
10227 (Etype (P_Prim), Etype (New_E)))
10233 Next_Elmt (Prim_Elt);
10236 -- If no match found, then the new subprogram does not override
10237 -- in the generic (nor in the instance).
10239 -- If the type in question is not abstract, and the subprogram
10240 -- is, this will be an error if the new operation is in the
10241 -- private part of the instance. Emit a warning now, which will
10242 -- make the subsequent error message easier to understand.
10244 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10245 and then Is_Abstract_Subprogram (Prev_E)
10246 and then In_Private_Part (Current_Scope)
10248 Error_Msg_Node_2 := F_Typ;
10250 ("private operation& in generic unit does not override "
10251 & "any primitive operation of& (RM 12.3 (18))??",
10261 end Is_Non_Overriding_Operation;
10263 -------------------------------------
10264 -- List_Inherited_Pre_Post_Aspects --
10265 -------------------------------------
10267 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10269 if Opt.List_Inherited_Aspects
10270 and then Is_Subprogram_Or_Generic_Subprogram (E)
10273 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10278 for Index in Subps'Range loop
10279 Items := Contract (Subps (Index));
10281 if Present (Items) then
10282 Prag := Pre_Post_Conditions (Items);
10283 while Present (Prag) loop
10284 Error_Msg_Sloc := Sloc (Prag);
10286 if Class_Present (Prag)
10287 and then not Split_PPC (Prag)
10289 if Pragma_Name (Prag) = Name_Precondition then
10291 ("info: & inherits `Pre''Class` aspect from "
10295 ("info: & inherits `Post''Class` aspect from "
10300 Prag := Next_Pragma (Prag);
10306 end List_Inherited_Pre_Post_Aspects;
10308 ------------------------------
10309 -- Make_Inequality_Operator --
10310 ------------------------------
10312 -- S is the defining identifier of an equality operator. We build a
10313 -- subprogram declaration with the right signature. This operation is
10314 -- intrinsic, because it is always expanded as the negation of the
10315 -- call to the equality function.
10317 procedure Make_Inequality_Operator (S : Entity_Id) is
10318 Loc : constant Source_Ptr := Sloc (S);
10321 Op_Name : Entity_Id;
10323 FF : constant Entity_Id := First_Formal (S);
10324 NF : constant Entity_Id := Next_Formal (FF);
10327 -- Check that equality was properly defined, ignore call if not
10334 A : constant Entity_Id :=
10335 Make_Defining_Identifier (Sloc (FF),
10336 Chars => Chars (FF));
10338 B : constant Entity_Id :=
10339 Make_Defining_Identifier (Sloc (NF),
10340 Chars => Chars (NF));
10343 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10345 Formals := New_List (
10346 Make_Parameter_Specification (Loc,
10347 Defining_Identifier => A,
10349 New_Occurrence_Of (Etype (First_Formal (S)),
10350 Sloc (Etype (First_Formal (S))))),
10352 Make_Parameter_Specification (Loc,
10353 Defining_Identifier => B,
10355 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10356 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10359 Make_Subprogram_Declaration (Loc,
10361 Make_Function_Specification (Loc,
10362 Defining_Unit_Name => Op_Name,
10363 Parameter_Specifications => Formals,
10364 Result_Definition =>
10365 New_Occurrence_Of (Standard_Boolean, Loc)));
10367 -- Insert inequality right after equality if it is explicit or after
10368 -- the derived type when implicit. These entities are created only
10369 -- for visibility purposes, and eventually replaced in the course
10370 -- of expansion, so they do not need to be attached to the tree and
10371 -- seen by the back-end. Keeping them internal also avoids spurious
10372 -- freezing problems. The declaration is inserted in the tree for
10373 -- analysis, and removed afterwards. If the equality operator comes
10374 -- from an explicit declaration, attach the inequality immediately
10375 -- after. Else the equality is inherited from a derived type
10376 -- declaration, so insert inequality after that declaration.
10378 if No (Alias (S)) then
10379 Insert_After (Unit_Declaration_Node (S), Decl);
10380 elsif Is_List_Member (Parent (S)) then
10381 Insert_After (Parent (S), Decl);
10383 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10386 Mark_Rewrite_Insertion (Decl);
10387 Set_Is_Intrinsic_Subprogram (Op_Name);
10390 Set_Has_Completion (Op_Name);
10391 Set_Corresponding_Equality (Op_Name, S);
10392 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10394 end Make_Inequality_Operator;
10396 ----------------------
10397 -- May_Need_Actuals --
10398 ----------------------
10400 procedure May_Need_Actuals (Fun : Entity_Id) is
10405 F := First_Formal (Fun);
10407 while Present (F) loop
10408 if No (Default_Value (F)) then
10416 Set_Needs_No_Actuals (Fun, B);
10417 end May_Need_Actuals;
10419 ---------------------
10420 -- Mode_Conformant --
10421 ---------------------
10423 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10426 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10428 end Mode_Conformant;
10430 ---------------------------
10431 -- New_Overloaded_Entity --
10432 ---------------------------
10434 procedure New_Overloaded_Entity
10436 Derived_Type : Entity_Id := Empty)
10438 Overridden_Subp : Entity_Id := Empty;
10439 -- Set if the current scope has an operation that is type-conformant
10440 -- with S, and becomes hidden by S.
10442 Is_Primitive_Subp : Boolean;
10443 -- Set to True if the new subprogram is primitive
10446 -- Entity that S overrides
10448 procedure Check_For_Primitive_Subprogram
10449 (Is_Primitive : out Boolean;
10450 Is_Overriding : Boolean := False);
10451 -- If the subprogram being analyzed is a primitive operation of the type
10452 -- of a formal or result, set the Has_Primitive_Operations flag on the
10453 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10454 -- corresponding flag on the entity itself for later use.
10456 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10457 -- True if a) E is a subprogram whose first formal is a concurrent type
10458 -- defined in the scope of E that has some entry or subprogram whose
10459 -- profile matches E, or b) E is an internally built dispatching
10460 -- subprogram of a protected type and there is a matching subprogram
10461 -- defined in the enclosing scope of the protected type, or c) E is
10462 -- an entry of a synchronized type and a matching procedure has been
10463 -- previously defined in the enclosing scope of the synchronized type.
10465 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10466 -- Check that E is declared in the private part of the current package,
10467 -- or in the package body, where it may hide a previous declaration.
10468 -- We can't use In_Private_Part by itself because this flag is also
10469 -- set when freezing entities, so we must examine the place of the
10470 -- declaration in the tree, and recognize wrapper packages as well.
10472 function Is_Overriding_Alias
10473 (Old_E : Entity_Id;
10474 New_E : Entity_Id) return Boolean;
10475 -- Check whether new subprogram and old subprogram are both inherited
10476 -- from subprograms that have distinct dispatch table entries. This can
10477 -- occur with derivations from instances with accidental homonyms. The
10478 -- function is conservative given that the converse is only true within
10479 -- instances that contain accidental overloadings.
10481 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10482 -- Report conflict between entities S and E
10484 ------------------------------------
10485 -- Check_For_Primitive_Subprogram --
10486 ------------------------------------
10488 procedure Check_For_Primitive_Subprogram
10489 (Is_Primitive : out Boolean;
10490 Is_Overriding : Boolean := False)
10492 Formal : Entity_Id;
10496 function Visible_Part_Type (T : Entity_Id) return Boolean;
10497 -- Returns true if T is declared in the visible part of the current
10498 -- package scope; otherwise returns false. Assumes that T is declared
10501 procedure Check_Private_Overriding (T : Entity_Id);
10502 -- Checks that if a primitive abstract subprogram of a visible
10503 -- abstract type is declared in a private part, then it must override
10504 -- an abstract subprogram declared in the visible part. Also checks
10505 -- that if a primitive function with a controlling result is declared
10506 -- in a private part, then it must override a function declared in
10507 -- the visible part.
10509 ------------------------------
10510 -- Check_Private_Overriding --
10511 ------------------------------
10513 procedure Check_Private_Overriding (T : Entity_Id) is
10514 function Overrides_Private_Part_Op return Boolean;
10515 -- This detects the special case where the overriding subprogram
10516 -- is overriding a subprogram that was declared in the same
10517 -- private part. That case is illegal by 3.9.3(10).
10519 function Overrides_Visible_Function
10520 (Partial_View : Entity_Id) return Boolean;
10521 -- True if S overrides a function in the visible part. The
10522 -- overridden function could be explicitly or implicitly declared.
10524 -------------------------------
10525 -- Overrides_Private_Part_Op --
10526 -------------------------------
10528 function Overrides_Private_Part_Op return Boolean is
10529 Over_Decl : constant Node_Id :=
10530 Unit_Declaration_Node (Overridden_Operation (S));
10531 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10534 pragma Assert (Is_Overriding);
10536 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10538 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10540 return In_Same_List (Over_Decl, Subp_Decl);
10541 end Overrides_Private_Part_Op;
10543 --------------------------------
10544 -- Overrides_Visible_Function --
10545 --------------------------------
10547 function Overrides_Visible_Function
10548 (Partial_View : Entity_Id) return Boolean
10551 if not Is_Overriding or else not Has_Homonym (S) then
10555 if not Present (Partial_View) then
10559 -- Search through all the homonyms H of S in the current
10560 -- package spec, and return True if we find one that matches.
10561 -- Note that Parent (H) will be the declaration of the
10562 -- partial view of T for a match.
10565 H : Entity_Id := S;
10569 exit when not Present (H) or else Scope (H) /= Scope (S);
10573 N_Private_Extension_Declaration,
10574 N_Private_Type_Declaration)
10575 and then Defining_Identifier (Parent (H)) = Partial_View
10583 end Overrides_Visible_Function;
10585 -- Start of processing for Check_Private_Overriding
10588 if Is_Package_Or_Generic_Package (Current_Scope)
10589 and then In_Private_Part (Current_Scope)
10590 and then Visible_Part_Type (T)
10591 and then not In_Instance
10593 if Is_Abstract_Type (T)
10594 and then Is_Abstract_Subprogram (S)
10595 and then (not Is_Overriding
10596 or else not Is_Abstract_Subprogram (E)
10597 or else Overrides_Private_Part_Op)
10600 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10603 elsif Ekind (S) = E_Function then
10605 Partial_View : constant Entity_Id :=
10606 Incomplete_Or_Partial_View (T);
10609 if not Overrides_Visible_Function (Partial_View) then
10611 -- Here, S is "function ... return T;" declared in
10612 -- the private part, not overriding some visible
10613 -- operation. That's illegal in the tagged case
10614 -- (but not if the private type is untagged).
10616 if ((Present (Partial_View)
10617 and then Is_Tagged_Type (Partial_View))
10618 or else (not Present (Partial_View)
10619 and then Is_Tagged_Type (T)))
10620 and then T = Base_Type (Etype (S))
10623 ("private function with tagged result must"
10624 & " override visible-part function", S);
10626 ("\move subprogram to the visible part"
10627 & " (RM 3.9.3(10))", S);
10629 -- AI05-0073: extend this test to the case of a
10630 -- function with a controlling access result.
10632 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10633 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10635 not Is_Class_Wide_Type
10636 (Designated_Type (Etype (S)))
10637 and then Ada_Version >= Ada_2012
10640 ("private function with controlling access "
10641 & "result must override visible-part function",
10644 ("\move subprogram to the visible part"
10645 & " (RM 3.9.3(10))", S);
10651 end Check_Private_Overriding;
10653 -----------------------
10654 -- Visible_Part_Type --
10655 -----------------------
10657 function Visible_Part_Type (T : Entity_Id) return Boolean is
10658 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10661 -- If the entity is a private type, then it must be declared in a
10664 if Ekind (T) in Private_Kind then
10667 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10670 elsif Is_List_Member (Declaration_Node (T))
10671 and then List_Containing (Declaration_Node (T)) =
10672 Visible_Declarations (Specification (P))
10679 end Visible_Part_Type;
10681 -- Start of processing for Check_For_Primitive_Subprogram
10684 Is_Primitive := False;
10686 if not Comes_From_Source (S) then
10689 -- If subprogram is at library level, it is not primitive operation
10691 elsif Current_Scope = Standard_Standard then
10694 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10695 and then not In_Package_Body (Current_Scope))
10696 or else Is_Overriding
10698 -- For function, check return type
10700 if Ekind (S) = E_Function then
10701 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10702 F_Typ := Designated_Type (Etype (S));
10704 F_Typ := Etype (S);
10707 B_Typ := Base_Type (F_Typ);
10709 if Scope (B_Typ) = Current_Scope
10710 and then not Is_Class_Wide_Type (B_Typ)
10711 and then not Is_Generic_Type (B_Typ)
10713 Is_Primitive := True;
10714 Set_Has_Primitive_Operations (B_Typ);
10715 Set_Is_Primitive (S);
10716 Check_Private_Overriding (B_Typ);
10718 -- The Ghost policy in effect at the point of declaration
10719 -- or a tagged type and a primitive operation must match
10720 -- (SPARK RM 6.9(16)).
10722 Check_Ghost_Primitive (S, B_Typ);
10726 -- For all subprograms, check formals
10728 Formal := First_Formal (S);
10729 while Present (Formal) loop
10730 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10731 F_Typ := Designated_Type (Etype (Formal));
10733 F_Typ := Etype (Formal);
10736 B_Typ := Base_Type (F_Typ);
10738 if Ekind (B_Typ) = E_Access_Subtype then
10739 B_Typ := Base_Type (B_Typ);
10742 if Scope (B_Typ) = Current_Scope
10743 and then not Is_Class_Wide_Type (B_Typ)
10744 and then not Is_Generic_Type (B_Typ)
10746 Is_Primitive := True;
10747 Set_Is_Primitive (S);
10748 Set_Has_Primitive_Operations (B_Typ);
10749 Check_Private_Overriding (B_Typ);
10751 -- The Ghost policy in effect at the point of declaration
10752 -- of a tagged type and a primitive operation must match
10753 -- (SPARK RM 6.9(16)).
10755 Check_Ghost_Primitive (S, B_Typ);
10758 Next_Formal (Formal);
10761 -- Special case: An equality function can be redefined for a type
10762 -- occurring in a declarative part, and won't otherwise be treated as
10763 -- a primitive because it doesn't occur in a package spec and doesn't
10764 -- override an inherited subprogram. It's important that we mark it
10765 -- primitive so it can be returned by Collect_Primitive_Operations
10766 -- and be used in composing the equality operation of later types
10767 -- that have a component of the type.
10769 elsif Chars (S) = Name_Op_Eq
10770 and then Etype (S) = Standard_Boolean
10772 B_Typ := Base_Type (Etype (First_Formal (S)));
10774 if Scope (B_Typ) = Current_Scope
10776 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10777 and then not Is_Limited_Type (B_Typ)
10779 Is_Primitive := True;
10780 Set_Is_Primitive (S);
10781 Set_Has_Primitive_Operations (B_Typ);
10782 Check_Private_Overriding (B_Typ);
10784 -- The Ghost policy in effect at the point of declaration of a
10785 -- tagged type and a primitive operation must match
10786 -- (SPARK RM 6.9(16)).
10788 Check_Ghost_Primitive (S, B_Typ);
10791 end Check_For_Primitive_Subprogram;
10793 --------------------------------------
10794 -- Has_Matching_Entry_Or_Subprogram --
10795 --------------------------------------
10797 function Has_Matching_Entry_Or_Subprogram
10798 (E : Entity_Id) return Boolean
10800 function Check_Conforming_Parameters
10801 (E1_Param : Node_Id;
10802 E2_Param : Node_Id;
10803 Ctype : Conformance_Type) return Boolean;
10804 -- Starting from the given parameters, check that all the parameters
10805 -- of two entries or subprograms are conformant. Used to skip
10806 -- the check on the controlling argument.
10808 function Matching_Entry_Or_Subprogram
10809 (Conc_Typ : Entity_Id;
10810 Subp : Entity_Id) return Entity_Id;
10811 -- Return the first entry or subprogram of the given concurrent type
10812 -- whose name matches the name of Subp and has a profile conformant
10813 -- with Subp; return Empty if not found.
10815 function Matching_Dispatching_Subprogram
10816 (Conc_Typ : Entity_Id;
10817 Ent : Entity_Id) return Entity_Id;
10818 -- Return the first dispatching primitive of Conc_Type defined in the
10819 -- enclosing scope of Conc_Type (i.e. before the full definition of
10820 -- this concurrent type) whose name matches the entry Ent and has a
10821 -- profile conformant with the profile of the corresponding (not yet
10822 -- built) dispatching primitive of Ent; return Empty if not found.
10824 function Matching_Original_Protected_Subprogram
10825 (Prot_Typ : Entity_Id;
10826 Subp : Entity_Id) return Entity_Id;
10827 -- Return the first subprogram defined in the enclosing scope of
10828 -- Prot_Typ (before the full definition of this protected type)
10829 -- whose name matches the original name of Subp and has a profile
10830 -- conformant with the profile of Subp; return Empty if not found.
10832 function Normalized_First_Parameter_Type
10833 (E : Entity_Id) return Entity_Id;
10834 -- Return the type of the first parameter unless that type
10835 -- is an anonymous access type, in which case return the
10836 -- designated type. Used to treat anonymous-access-to-synchronized
10837 -- the same as synchronized for purposes of checking for
10838 -- prefixed view profile conflicts.
10840 ---------------------------------
10841 -- Check_Conforming_Parameters --
10842 ---------------------------------
10844 function Check_Conforming_Parameters
10845 (E1_Param : Node_Id;
10846 E2_Param : Node_Id;
10847 Ctype : Conformance_Type) return Boolean
10849 Param_E1 : Node_Id := E1_Param;
10850 Param_E2 : Node_Id := E2_Param;
10853 while Present (Param_E1) and then Present (Param_E2) loop
10854 if (Ctype >= Mode_Conformant) and then
10855 Ekind (Defining_Identifier (Param_E1)) /=
10856 Ekind (Defining_Identifier (Param_E2))
10861 (Find_Parameter_Type (Param_E1),
10862 Find_Parameter_Type (Param_E2),
10872 -- The candidate is not valid if one of the two lists contains
10873 -- more parameters than the other
10875 return No (Param_E1) and then No (Param_E2);
10876 end Check_Conforming_Parameters;
10878 ----------------------------------
10879 -- Matching_Entry_Or_Subprogram --
10880 ----------------------------------
10882 function Matching_Entry_Or_Subprogram
10883 (Conc_Typ : Entity_Id;
10884 Subp : Entity_Id) return Entity_Id
10889 E := First_Entity (Conc_Typ);
10890 while Present (E) loop
10891 if Chars (Subp) = Chars (E)
10892 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10894 Check_Conforming_Parameters
10895 (First (Parameter_Specifications (Parent (E))),
10896 Next (First (Parameter_Specifications (Parent (Subp)))),
10906 end Matching_Entry_Or_Subprogram;
10908 -------------------------------------
10909 -- Matching_Dispatching_Subprogram --
10910 -------------------------------------
10912 function Matching_Dispatching_Subprogram
10913 (Conc_Typ : Entity_Id;
10914 Ent : Entity_Id) return Entity_Id
10919 -- Search for entities in the enclosing scope of this synchonized
10922 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10923 Push_Scope (Scope (Conc_Typ));
10924 E := Current_Entity_In_Scope (Ent);
10927 while Present (E) loop
10928 if Scope (E) = Scope (Conc_Typ)
10929 and then Comes_From_Source (E)
10930 and then Ekind (E) = E_Procedure
10931 and then Present (First_Entity (E))
10932 and then Is_Controlling_Formal (First_Entity (E))
10933 and then Etype (First_Entity (E)) = Conc_Typ
10935 Check_Conforming_Parameters
10936 (First (Parameter_Specifications (Parent (Ent))),
10937 Next (First (Parameter_Specifications (Parent (E)))),
10938 Subtype_Conformant)
10947 end Matching_Dispatching_Subprogram;
10949 --------------------------------------------
10950 -- Matching_Original_Protected_Subprogram --
10951 --------------------------------------------
10953 function Matching_Original_Protected_Subprogram
10954 (Prot_Typ : Entity_Id;
10955 Subp : Entity_Id) return Entity_Id
10957 ICF : constant Boolean :=
10958 Is_Controlling_Formal (First_Entity (Subp));
10962 -- Temporarily decorate the first parameter of Subp as controlling
10963 -- formal, required to invoke Subtype_Conformant.
10965 Set_Is_Controlling_Formal (First_Entity (Subp));
10968 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10970 while Present (E) loop
10971 if Scope (E) = Scope (Prot_Typ)
10972 and then Comes_From_Source (E)
10973 and then Ekind (Subp) = Ekind (E)
10974 and then Present (First_Entity (E))
10975 and then Is_Controlling_Formal (First_Entity (E))
10976 and then Etype (First_Entity (E)) = Prot_Typ
10977 and then Subtype_Conformant (Subp, E,
10978 Skip_Controlling_Formals => True)
10980 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10987 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10990 end Matching_Original_Protected_Subprogram;
10992 -------------------------------------
10993 -- Normalized_First_Parameter_Type --
10994 -------------------------------------
10996 function Normalized_First_Parameter_Type
10997 (E : Entity_Id) return Entity_Id
10999 Result : Entity_Id := Etype (First_Entity (E));
11001 if Ekind (Result) = E_Anonymous_Access_Type then
11002 Result := Designated_Type (Result);
11005 end Normalized_First_Parameter_Type;
11007 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11010 -- Case 1: E is a subprogram whose first formal is a concurrent type
11011 -- defined in the scope of E that has an entry or subprogram whose
11012 -- profile matches E.
11014 if Comes_From_Source (E)
11015 and then Is_Subprogram (E)
11016 and then Present (First_Entity (E))
11017 and then Is_Concurrent_Record_Type
11018 (Normalized_First_Parameter_Type (E))
11021 Scope (Corresponding_Concurrent_Type
11022 (Normalized_First_Parameter_Type (E)))
11025 (Matching_Entry_Or_Subprogram
11026 (Corresponding_Concurrent_Type
11027 (Normalized_First_Parameter_Type (E)),
11030 Report_Conflict (E,
11031 Matching_Entry_Or_Subprogram
11032 (Corresponding_Concurrent_Type
11033 (Normalized_First_Parameter_Type (E)),
11038 -- Case 2: E is an internally built dispatching subprogram of a
11039 -- protected type and there is a subprogram defined in the enclosing
11040 -- scope of the protected type that has the original name of E and
11041 -- its profile is conformant with the profile of E. We check the
11042 -- name of the original protected subprogram associated with E since
11043 -- the expander builds dispatching primitives of protected functions
11044 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11046 elsif not Comes_From_Source (E)
11047 and then Is_Subprogram (E)
11048 and then Present (First_Entity (E))
11049 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11050 and then Present (Original_Protected_Subprogram (E))
11053 (Matching_Original_Protected_Subprogram
11054 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11057 Report_Conflict (E,
11058 Matching_Original_Protected_Subprogram
11059 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11063 -- Case 3: E is an entry of a synchronized type and a matching
11064 -- procedure has been previously defined in the enclosing scope
11065 -- of the synchronized type.
11067 elsif Comes_From_Source (E)
11068 and then Ekind (E) = E_Entry
11070 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11072 Report_Conflict (E,
11073 Matching_Dispatching_Subprogram (Current_Scope, E));
11078 end Has_Matching_Entry_Or_Subprogram;
11080 ----------------------------
11081 -- Is_Private_Declaration --
11082 ----------------------------
11084 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11085 Decl : constant Node_Id := Unit_Declaration_Node (E);
11086 Priv_Decls : List_Id;
11089 if Is_Package_Or_Generic_Package (Current_Scope)
11090 and then In_Private_Part (Current_Scope)
11093 Private_Declarations (Package_Specification (Current_Scope));
11095 return In_Package_Body (Current_Scope)
11097 (Is_List_Member (Decl)
11098 and then List_Containing (Decl) = Priv_Decls)
11099 or else (Nkind (Parent (Decl)) = N_Package_Specification
11101 Is_Compilation_Unit
11102 (Defining_Entity (Parent (Decl)))
11103 and then List_Containing (Parent (Parent (Decl))) =
11108 end Is_Private_Declaration;
11110 --------------------------
11111 -- Is_Overriding_Alias --
11112 --------------------------
11114 function Is_Overriding_Alias
11115 (Old_E : Entity_Id;
11116 New_E : Entity_Id) return Boolean
11118 AO : constant Entity_Id := Alias (Old_E);
11119 AN : constant Entity_Id := Alias (New_E);
11122 return Scope (AO) /= Scope (AN)
11123 or else No (DTC_Entity (AO))
11124 or else No (DTC_Entity (AN))
11125 or else DT_Position (AO) = DT_Position (AN);
11126 end Is_Overriding_Alias;
11128 ---------------------
11129 -- Report_Conflict --
11130 ---------------------
11132 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11134 Error_Msg_Sloc := Sloc (E);
11136 -- Generate message, with useful additional warning if in generic
11138 if Is_Generic_Unit (E) then
11139 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11140 Error_Msg_N ("\& conflicts with declaration#", S);
11142 Error_Msg_N ("& conflicts with declaration#", S);
11144 end Report_Conflict;
11146 -- Start of processing for New_Overloaded_Entity
11149 -- We need to look for an entity that S may override. This must be a
11150 -- homonym in the current scope, so we look for the first homonym of
11151 -- S in the current scope as the starting point for the search.
11153 E := Current_Entity_In_Scope (S);
11155 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11156 -- They are directly added to the list of primitive operations of
11157 -- Derived_Type, unless this is a rederivation in the private part
11158 -- of an operation that was already derived in the visible part of
11159 -- the current package.
11161 if Ada_Version >= Ada_2005
11162 and then Present (Derived_Type)
11163 and then Present (Alias (S))
11164 and then Is_Dispatching_Operation (Alias (S))
11165 and then Present (Find_Dispatching_Type (Alias (S)))
11166 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11168 -- For private types, when the full-view is processed we propagate to
11169 -- the full view the non-overridden entities whose attribute "alias"
11170 -- references an interface primitive. These entities were added by
11171 -- Derive_Subprograms to ensure that interface primitives are
11174 -- Inside_Freeze_Actions is non zero when S corresponds with an
11175 -- internal entity that links an interface primitive with its
11176 -- covering primitive through attribute Interface_Alias (see
11177 -- Add_Internal_Interface_Entities).
11179 if Inside_Freezing_Actions = 0
11180 and then Is_Package_Or_Generic_Package (Current_Scope)
11181 and then In_Private_Part (Current_Scope)
11182 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11183 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11184 and then Full_View (Defining_Identifier (Parent (E)))
11185 = Defining_Identifier (Parent (S))
11186 and then Alias (E) = Alias (S)
11188 Check_Operation_From_Private_View (S, E);
11189 Set_Is_Dispatching_Operation (S);
11194 Enter_Overloaded_Entity (S);
11195 Check_Dispatching_Operation (S, Empty);
11196 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11202 -- For synchronized types check conflicts of this entity with previously
11203 -- defined entities.
11205 if Ada_Version >= Ada_2005
11206 and then Has_Matching_Entry_Or_Subprogram (S)
11211 -- If there is no homonym then this is definitely not overriding
11214 Enter_Overloaded_Entity (S);
11215 Check_Dispatching_Operation (S, Empty);
11216 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11218 -- If subprogram has an explicit declaration, check whether it has an
11219 -- overriding indicator.
11221 if Comes_From_Source (S) then
11222 Check_Synchronized_Overriding (S, Overridden_Subp);
11224 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11225 -- it may have overridden some hidden inherited primitive. Update
11226 -- Overridden_Subp to avoid spurious errors when checking the
11227 -- overriding indicator.
11229 if Ada_Version >= Ada_2012
11230 and then No (Overridden_Subp)
11231 and then Is_Dispatching_Operation (S)
11232 and then Present (Overridden_Operation (S))
11234 Overridden_Subp := Overridden_Operation (S);
11237 Check_Overriding_Indicator
11238 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11240 -- The Ghost policy in effect at the point of declaration of a
11241 -- parent subprogram and an overriding subprogram must match
11242 -- (SPARK RM 6.9(17)).
11244 Check_Ghost_Overriding (S, Overridden_Subp);
11247 -- If there is a homonym that is not overloadable, then we have an
11248 -- error, except for the special cases checked explicitly below.
11250 elsif not Is_Overloadable (E) then
11252 -- Check for spurious conflict produced by a subprogram that has the
11253 -- same name as that of the enclosing generic package. The conflict
11254 -- occurs within an instance, between the subprogram and the renaming
11255 -- declaration for the package. After the subprogram, the package
11256 -- renaming declaration becomes hidden.
11258 if Ekind (E) = E_Package
11259 and then Present (Renamed_Object (E))
11260 and then Renamed_Object (E) = Current_Scope
11261 and then Nkind (Parent (Renamed_Object (E))) =
11262 N_Package_Specification
11263 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11266 Set_Is_Immediately_Visible (E, False);
11267 Enter_Overloaded_Entity (S);
11268 Set_Homonym (S, Homonym (E));
11269 Check_Dispatching_Operation (S, Empty);
11270 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11272 -- If the subprogram is implicit it is hidden by the previous
11273 -- declaration. However if it is dispatching, it must appear in the
11274 -- dispatch table anyway, because it can be dispatched to even if it
11275 -- cannot be called directly.
11277 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11278 Set_Scope (S, Current_Scope);
11280 if Is_Dispatching_Operation (Alias (S)) then
11281 Check_Dispatching_Operation (S, Empty);
11287 Report_Conflict (S, E);
11291 -- E exists and is overloadable
11294 Check_Synchronized_Overriding (S, Overridden_Subp);
11296 -- Loop through E and its homonyms to determine if any of them is
11297 -- the candidate for overriding by S.
11299 while Present (E) loop
11301 -- Definitely not interesting if not in the current scope
11303 if Scope (E) /= Current_Scope then
11306 -- A function can overload the name of an abstract state. The
11307 -- state can be viewed as a function with a profile that cannot
11308 -- be matched by anything.
11310 elsif Ekind (S) = E_Function
11311 and then Ekind (E) = E_Abstract_State
11313 Enter_Overloaded_Entity (S);
11316 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11317 -- procedures locate the internally generated spec. We enforce
11318 -- mode conformance since a tagged type may inherit from
11319 -- interfaces several null primitives which differ only in
11320 -- the mode of the formals.
11322 elsif not Comes_From_Source (S)
11323 and then Is_Null_Procedure (S)
11324 and then not Mode_Conformant (E, S)
11328 -- Check if we have type conformance
11330 elsif Type_Conformant (E, S) then
11332 -- If the old and new entities have the same profile and one
11333 -- is not the body of the other, then this is an error, unless
11334 -- one of them is implicitly declared.
11336 -- There are some cases when both can be implicit, for example
11337 -- when both a literal and a function that overrides it are
11338 -- inherited in a derivation, or when an inherited operation
11339 -- of a tagged full type overrides the inherited operation of
11340 -- a private extension. Ada 83 had a special rule for the
11341 -- literal case. In Ada 95, the later implicit operation hides
11342 -- the former, and the literal is always the former. In the
11343 -- odd case where both are derived operations declared at the
11344 -- same point, both operations should be declared, and in that
11345 -- case we bypass the following test and proceed to the next
11346 -- part. This can only occur for certain obscure cases in
11347 -- instances, when an operation on a type derived from a formal
11348 -- private type does not override a homograph inherited from
11349 -- the actual. In subsequent derivations of such a type, the
11350 -- DT positions of these operations remain distinct, if they
11353 if Present (Alias (S))
11354 and then (No (Alias (E))
11355 or else Comes_From_Source (E)
11356 or else Is_Abstract_Subprogram (S)
11358 (Is_Dispatching_Operation (E)
11359 and then Is_Overriding_Alias (E, S)))
11360 and then Ekind (E) /= E_Enumeration_Literal
11362 -- When an derived operation is overloaded it may be due to
11363 -- the fact that the full view of a private extension
11364 -- re-inherits. It has to be dealt with.
11366 if Is_Package_Or_Generic_Package (Current_Scope)
11367 and then In_Private_Part (Current_Scope)
11369 Check_Operation_From_Private_View (S, E);
11372 -- In any case the implicit operation remains hidden by the
11373 -- existing declaration, which is overriding. Indicate that
11374 -- E overrides the operation from which S is inherited.
11376 if Present (Alias (S)) then
11377 Set_Overridden_Operation (E, Alias (S));
11378 Inherit_Subprogram_Contract (E, Alias (S));
11381 Set_Overridden_Operation (E, S);
11382 Inherit_Subprogram_Contract (E, S);
11385 -- When a dispatching operation overrides an inherited
11386 -- subprogram, it shall be subtype conformant with the
11387 -- inherited subprogram (RM 3.9.2 (10.2)).
11389 if Comes_From_Source (E)
11390 and then Is_Dispatching_Operation (E)
11391 and then Find_Dispatching_Type (S)
11392 = Find_Dispatching_Type (E)
11394 Check_Subtype_Conformant (E, S);
11397 if Comes_From_Source (E) then
11398 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11400 -- The Ghost policy in effect at the point of declaration
11401 -- of a parent subprogram and an overriding subprogram
11402 -- must match (SPARK RM 6.9(17)).
11404 Check_Ghost_Overriding (E, S);
11409 -- Within an instance, the renaming declarations for actual
11410 -- subprograms may become ambiguous, but they do not hide each
11413 elsif Ekind (E) /= E_Entry
11414 and then not Comes_From_Source (E)
11415 and then not Is_Generic_Instance (E)
11416 and then (Present (Alias (E))
11417 or else Is_Intrinsic_Subprogram (E))
11418 and then (not In_Instance
11419 or else No (Parent (E))
11420 or else Nkind (Unit_Declaration_Node (E)) /=
11421 N_Subprogram_Renaming_Declaration)
11423 -- A subprogram child unit is not allowed to override an
11424 -- inherited subprogram (10.1.1(20)).
11426 if Is_Child_Unit (S) then
11428 ("child unit overrides inherited subprogram in parent",
11433 if Is_Non_Overriding_Operation (E, S) then
11434 Enter_Overloaded_Entity (S);
11436 if No (Derived_Type)
11437 or else Is_Tagged_Type (Derived_Type)
11439 Check_Dispatching_Operation (S, Empty);
11445 -- E is a derived operation or an internal operator which
11446 -- is being overridden. Remove E from further visibility.
11447 -- Furthermore, if E is a dispatching operation, it must be
11448 -- replaced in the list of primitive operations of its type
11449 -- (see Override_Dispatching_Operation).
11451 Overridden_Subp := E;
11453 -- It is possible for E to be in the current scope and
11454 -- yet not in the entity chain. This can only occur in a
11455 -- generic context where E is an implicit concatenation
11456 -- in the formal part, because in a generic body the
11457 -- entity chain starts with the formals.
11459 -- In GNATprove mode, a wrapper for an operation with
11460 -- axiomatization may be a homonym of another declaration
11461 -- for an actual subprogram (needs refinement ???).
11463 if No (Prev_Entity (E)) then
11465 and then GNATprove_Mode
11467 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11468 N_Subprogram_Renaming_Declaration
11472 pragma Assert (Chars (E) = Name_Op_Concat);
11477 -- E must be removed both from the entity_list of the
11478 -- current scope, and from the visibility chain.
11480 if Debug_Flag_E then
11481 Write_Str ("Override implicit operation ");
11482 Write_Int (Int (E));
11486 -- If E is a predefined concatenation, it stands for four
11487 -- different operations. As a result, a single explicit
11488 -- declaration does not hide it. In a possible ambiguous
11489 -- situation, Disambiguate chooses the user-defined op,
11490 -- so it is correct to retain the previous internal one.
11492 if Chars (E) /= Name_Op_Concat
11493 or else Ekind (E) /= E_Operator
11495 -- For nondispatching derived operations that are
11496 -- overridden by a subprogram declared in the private
11497 -- part of a package, we retain the derived subprogram
11498 -- but mark it as not immediately visible. If the
11499 -- derived operation was declared in the visible part
11500 -- then this ensures that it will still be visible
11501 -- outside the package with the proper signature
11502 -- (calls from outside must also be directed to this
11503 -- version rather than the overriding one, unlike the
11504 -- dispatching case). Calls from inside the package
11505 -- will still resolve to the overriding subprogram
11506 -- since the derived one is marked as not visible
11507 -- within the package.
11509 -- If the private operation is dispatching, we achieve
11510 -- the overriding by keeping the implicit operation
11511 -- but setting its alias to be the overriding one. In
11512 -- this fashion the proper body is executed in all
11513 -- cases, but the original signature is used outside
11516 -- If the overriding is not in the private part, we
11517 -- remove the implicit operation altogether.
11519 if Is_Private_Declaration (S) then
11520 if not Is_Dispatching_Operation (E) then
11521 Set_Is_Immediately_Visible (E, False);
11523 -- Work done in Override_Dispatching_Operation, so
11524 -- nothing else needs to be done here.
11530 Remove_Entity_And_Homonym (E);
11534 Enter_Overloaded_Entity (S);
11536 -- For entities generated by Derive_Subprograms the
11537 -- overridden operation is the inherited primitive
11538 -- (which is available through the attribute alias).
11540 if not (Comes_From_Source (E))
11541 and then Is_Dispatching_Operation (E)
11542 and then Find_Dispatching_Type (E) =
11543 Find_Dispatching_Type (S)
11544 and then Present (Alias (E))
11545 and then Comes_From_Source (Alias (E))
11547 Set_Overridden_Operation (S, Alias (E));
11548 Inherit_Subprogram_Contract (S, Alias (E));
11550 -- Normal case of setting entity as overridden
11552 -- Note: Static_Initialization and Overridden_Operation
11553 -- attributes use the same field in subprogram entities.
11554 -- Static_Initialization is only defined for internal
11555 -- initialization procedures, where Overridden_Operation
11556 -- is irrelevant. Therefore the setting of this attribute
11557 -- must check whether the target is an init_proc.
11559 elsif not Is_Init_Proc (S) then
11560 Set_Overridden_Operation (S, E);
11561 Inherit_Subprogram_Contract (S, E);
11564 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11566 -- The Ghost policy in effect at the point of declaration
11567 -- of a parent subprogram and an overriding subprogram
11568 -- must match (SPARK RM 6.9(17)).
11570 Check_Ghost_Overriding (S, E);
11572 -- If S is a user-defined subprogram or a null procedure
11573 -- expanded to override an inherited null procedure, or a
11574 -- predefined dispatching primitive then indicate that E
11575 -- overrides the operation from which S is inherited.
11577 if Comes_From_Source (S)
11579 (Present (Parent (S))
11580 and then Nkind (Parent (S)) = N_Procedure_Specification
11581 and then Null_Present (Parent (S)))
11583 (Present (Alias (E))
11585 Is_Predefined_Dispatching_Operation (Alias (E)))
11587 if Present (Alias (E)) then
11588 Set_Overridden_Operation (S, Alias (E));
11589 Inherit_Subprogram_Contract (S, Alias (E));
11593 if Is_Dispatching_Operation (E) then
11595 -- An overriding dispatching subprogram inherits the
11596 -- convention of the overridden subprogram (AI-117).
11598 Set_Convention (S, Convention (E));
11599 Check_Dispatching_Operation (S, E);
11602 Check_Dispatching_Operation (S, Empty);
11605 Check_For_Primitive_Subprogram
11606 (Is_Primitive_Subp, Is_Overriding => True);
11607 goto Check_Inequality;
11609 -- Apparent redeclarations in instances can occur when two
11610 -- formal types get the same actual type. The subprograms in
11611 -- in the instance are legal, even if not callable from the
11612 -- outside. Calls from within are disambiguated elsewhere.
11613 -- For dispatching operations in the visible part, the usual
11614 -- rules apply, and operations with the same profile are not
11615 -- legal (B830001).
11617 elsif (In_Instance_Visible_Part
11618 and then not Is_Dispatching_Operation (E))
11619 or else In_Instance_Not_Visible
11623 -- Here we have a real error (identical profile)
11626 Error_Msg_Sloc := Sloc (E);
11628 -- Avoid cascaded errors if the entity appears in
11629 -- subsequent calls.
11631 Set_Scope (S, Current_Scope);
11633 -- Generate error, with extra useful warning for the case
11634 -- of a generic instance with no completion.
11636 if Is_Generic_Instance (S)
11637 and then not Has_Completion (E)
11640 ("instantiation cannot provide body for&", S);
11641 Error_Msg_N ("\& conflicts with declaration#", S);
11643 Error_Msg_N ("& conflicts with declaration#", S);
11650 -- If one subprogram has an access parameter and the other
11651 -- a parameter of an access type, calls to either might be
11652 -- ambiguous. Verify that parameters match except for the
11653 -- access parameter.
11655 if May_Hide_Profile then
11661 F1 := First_Formal (S);
11662 F2 := First_Formal (E);
11663 while Present (F1) and then Present (F2) loop
11664 if Is_Access_Type (Etype (F1)) then
11665 if not Is_Access_Type (Etype (F2))
11666 or else not Conforming_Types
11667 (Designated_Type (Etype (F1)),
11668 Designated_Type (Etype (F2)),
11671 May_Hide_Profile := False;
11675 not Conforming_Types
11676 (Etype (F1), Etype (F2), Type_Conformant)
11678 May_Hide_Profile := False;
11685 if May_Hide_Profile
11689 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11698 -- On exit, we know that S is a new entity
11700 Enter_Overloaded_Entity (S);
11701 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11702 Check_Overriding_Indicator
11703 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11705 -- The Ghost policy in effect at the point of declaration of a parent
11706 -- subprogram and an overriding subprogram must match
11707 -- (SPARK RM 6.9(17)).
11709 Check_Ghost_Overriding (S, Overridden_Subp);
11711 -- If S is a derived operation for an untagged type then by
11712 -- definition it's not a dispatching operation (even if the parent
11713 -- operation was dispatching), so Check_Dispatching_Operation is not
11714 -- called in that case.
11716 if No (Derived_Type)
11717 or else Is_Tagged_Type (Derived_Type)
11719 Check_Dispatching_Operation (S, Empty);
11723 -- If this is a user-defined equality operator that is not a derived
11724 -- subprogram, create the corresponding inequality. If the operation is
11725 -- dispatching, the expansion is done elsewhere, and we do not create
11726 -- an explicit inequality operation.
11728 <<Check_Inequality>>
11729 if Chars (S) = Name_Op_Eq
11730 and then Etype (S) = Standard_Boolean
11731 and then Present (Parent (S))
11732 and then not Is_Dispatching_Operation (S)
11734 Make_Inequality_Operator (S);
11735 Check_Untagged_Equality (S);
11737 end New_Overloaded_Entity;
11739 ----------------------------------
11740 -- Preanalyze_Formal_Expression --
11741 ----------------------------------
11743 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
11744 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
11746 In_Spec_Expression := True;
11747 Preanalyze_With_Freezing_And_Resolve (N, T);
11748 In_Spec_Expression := Save_In_Spec_Expression;
11749 end Preanalyze_Formal_Expression;
11751 ---------------------
11752 -- Process_Formals --
11753 ---------------------
11755 procedure Process_Formals
11757 Related_Nod : Node_Id)
11759 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11760 -- Determine whether an access type designates a type coming from a
11763 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11764 -- Check whether the default has a class-wide type. After analysis the
11765 -- default has the type of the formal, so we must also check explicitly
11766 -- for an access attribute.
11768 ----------------------------------
11769 -- Designates_From_Limited_With --
11770 ----------------------------------
11772 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11773 Desig : Entity_Id := Typ;
11776 if Is_Access_Type (Desig) then
11777 Desig := Directly_Designated_Type (Desig);
11780 if Is_Class_Wide_Type (Desig) then
11781 Desig := Root_Type (Desig);
11785 Ekind (Desig) = E_Incomplete_Type
11786 and then From_Limited_With (Desig);
11787 end Designates_From_Limited_With;
11789 ---------------------------
11790 -- Is_Class_Wide_Default --
11791 ---------------------------
11793 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11795 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11796 or else (Nkind (D) = N_Attribute_Reference
11797 and then Attribute_Name (D) = Name_Access
11798 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11799 end Is_Class_Wide_Default;
11803 Context : constant Node_Id := Parent (Parent (T));
11805 Formal : Entity_Id;
11806 Formal_Type : Entity_Id;
11807 Param_Spec : Node_Id;
11810 Num_Out_Params : Nat := 0;
11811 First_Out_Param : Entity_Id := Empty;
11812 -- Used for setting Is_Only_Out_Parameter
11814 -- Start of processing for Process_Formals
11817 -- In order to prevent premature use of the formals in the same formal
11818 -- part, the Ekind is left undefined until all default expressions are
11819 -- analyzed. The Ekind is established in a separate loop at the end.
11821 Param_Spec := First (T);
11822 while Present (Param_Spec) loop
11823 Formal := Defining_Identifier (Param_Spec);
11824 Set_Never_Set_In_Source (Formal, True);
11825 Enter_Name (Formal);
11827 -- Case of ordinary parameters
11829 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11830 Find_Type (Parameter_Type (Param_Spec));
11831 Ptype := Parameter_Type (Param_Spec);
11833 if Ptype = Error then
11837 -- Protect against malformed parameter types
11839 if Nkind (Ptype) not in N_Has_Entity then
11840 Formal_Type := Any_Type;
11842 Formal_Type := Entity (Ptype);
11845 if Is_Incomplete_Type (Formal_Type)
11847 (Is_Class_Wide_Type (Formal_Type)
11848 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11850 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11851 -- primitive operations, as long as their completion is
11852 -- in the same declarative part. If in the private part
11853 -- this means that the type cannot be a Taft-amendment type.
11854 -- Check is done on package exit. For access to subprograms,
11855 -- the use is legal for Taft-amendment types.
11857 -- Ada 2012: tagged incomplete types are allowed as generic
11858 -- formal types. They do not introduce dependencies and the
11859 -- corresponding generic subprogram does not have a delayed
11860 -- freeze, because it does not need a freeze node. However,
11861 -- it is still the case that untagged incomplete types cannot
11862 -- be Taft-amendment types and must be completed in private
11863 -- part, so the subprogram must appear in the list of private
11864 -- dependents of the type.
11866 if Is_Tagged_Type (Formal_Type)
11867 or else (Ada_Version >= Ada_2012
11868 and then not From_Limited_With (Formal_Type)
11869 and then not Is_Generic_Type (Formal_Type))
11871 if Ekind (Scope (Current_Scope)) = E_Package
11872 and then not Is_Generic_Type (Formal_Type)
11873 and then not Is_Class_Wide_Type (Formal_Type)
11876 (Parent (T), N_Access_Function_Definition,
11877 N_Access_Procedure_Definition)
11879 Append_Elmt (Current_Scope,
11880 Private_Dependents (Base_Type (Formal_Type)));
11882 -- Freezing is delayed to ensure that Register_Prim
11883 -- will get called for this operation, which is needed
11884 -- in cases where static dispatch tables aren't built.
11885 -- (Note that the same is done for controlling access
11886 -- parameter cases in function Access_Definition.)
11888 if not Is_Thunk (Current_Scope) then
11889 Set_Has_Delayed_Freeze (Current_Scope);
11894 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11895 N_Access_Procedure_Definition)
11897 -- AI05-0151: Tagged incomplete types are allowed in all
11898 -- formal parts. Untagged incomplete types are not allowed
11899 -- in bodies. Limited views of either kind are not allowed
11900 -- if there is no place at which the non-limited view can
11901 -- become available.
11903 -- Incomplete formal untagged types are not allowed in
11904 -- subprogram bodies (but are legal in their declarations).
11905 -- This excludes bodies created for null procedures, which
11906 -- are basic declarations.
11908 if Is_Generic_Type (Formal_Type)
11909 and then not Is_Tagged_Type (Formal_Type)
11910 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11913 ("invalid use of formal incomplete type", Param_Spec);
11915 elsif Ada_Version >= Ada_2012 then
11916 if Is_Tagged_Type (Formal_Type)
11917 and then (not From_Limited_With (Formal_Type)
11918 or else not In_Package_Body)
11922 elsif Nkind_In (Context, N_Accept_Statement,
11923 N_Accept_Alternative,
11925 or else (Nkind (Context) = N_Subprogram_Body
11926 and then Comes_From_Source (Context))
11929 ("invalid use of untagged incomplete type &",
11930 Ptype, Formal_Type);
11935 ("invalid use of incomplete type&",
11936 Param_Spec, Formal_Type);
11938 -- Further checks on the legality of incomplete types
11939 -- in formal parts are delayed until the freeze point
11940 -- of the enclosing subprogram or access to subprogram.
11944 elsif Ekind (Formal_Type) = E_Void then
11946 ("premature use of&",
11947 Parameter_Type (Param_Spec), Formal_Type);
11950 -- Ada 2012 (AI-142): Handle aliased parameters
11952 if Ada_Version >= Ada_2012
11953 and then Aliased_Present (Param_Spec)
11955 Set_Is_Aliased (Formal);
11957 -- AI12-001: All aliased objects are considered to be specified
11958 -- as independently addressable (RM C.6(8.1/4)).
11960 Set_Is_Independent (Formal);
11963 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11964 -- declaration corresponding to the null-excluding type of the
11965 -- formal in the enclosing scope. Finally, replace the parameter
11966 -- type of the formal with the internal subtype.
11968 if Ada_Version >= Ada_2005
11969 and then Null_Exclusion_Present (Param_Spec)
11971 if not Is_Access_Type (Formal_Type) then
11973 ("`NOT NULL` allowed only for an access type", Param_Spec);
11976 if Can_Never_Be_Null (Formal_Type)
11977 and then Comes_From_Source (Related_Nod)
11980 ("`NOT NULL` not allowed (& already excludes null)",
11981 Param_Spec, Formal_Type);
11985 Create_Null_Excluding_Itype
11987 Related_Nod => Related_Nod,
11988 Scope_Id => Scope (Current_Scope));
11990 -- If the designated type of the itype is an itype that is
11991 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11992 -- on the access subtype, to prevent order-of-elaboration
11993 -- issues in the backend.
11996 -- type T is access procedure;
11997 -- procedure Op (O : not null T);
11999 if Is_Itype (Directly_Designated_Type (Formal_Type))
12001 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12003 Set_Has_Delayed_Freeze (Formal_Type);
12008 -- An access formal type
12012 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12014 -- No need to continue if we already notified errors
12016 if not Present (Formal_Type) then
12020 -- Ada 2005 (AI-254)
12023 AD : constant Node_Id :=
12024 Access_To_Subprogram_Definition
12025 (Parameter_Type (Param_Spec));
12027 if Present (AD) and then Protected_Present (AD) then
12029 Replace_Anonymous_Access_To_Protected_Subprogram
12035 Set_Etype (Formal, Formal_Type);
12037 -- Deal with default expression if present
12039 Default := Expression (Param_Spec);
12041 if Present (Default) then
12042 if Out_Present (Param_Spec) then
12044 ("default initialization only allowed for IN parameters",
12048 -- Do the special preanalysis of the expression (see section on
12049 -- "Handling of Default Expressions" in the spec of package Sem).
12051 Preanalyze_Formal_Expression (Default, Formal_Type);
12053 -- An access to constant cannot be the default for
12054 -- an access parameter that is an access to variable.
12056 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12057 and then not Is_Access_Constant (Formal_Type)
12058 and then Is_Access_Type (Etype (Default))
12059 and then Is_Access_Constant (Etype (Default))
12062 ("formal that is access to variable cannot be initialized "
12063 & "with an access-to-constant expression", Default);
12066 -- Check that the designated type of an access parameter's default
12067 -- is not a class-wide type unless the parameter's designated type
12068 -- is also class-wide.
12070 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12071 and then not Designates_From_Limited_With (Formal_Type)
12072 and then Is_Class_Wide_Default (Default)
12073 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12076 ("access to class-wide expression not allowed here", Default);
12079 -- Check incorrect use of dynamically tagged expressions
12081 if Is_Tagged_Type (Formal_Type) then
12082 Check_Dynamically_Tagged_Expression
12084 Typ => Formal_Type,
12085 Related_Nod => Default);
12089 -- Ada 2005 (AI-231): Static checks
12091 if Ada_Version >= Ada_2005
12092 and then Is_Access_Type (Etype (Formal))
12093 and then Can_Never_Be_Null (Etype (Formal))
12095 Null_Exclusion_Static_Checks (Param_Spec);
12098 -- The following checks are relevant only when SPARK_Mode is on as
12099 -- these are not standard Ada legality rules.
12101 if SPARK_Mode = On then
12102 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
12104 -- A function cannot have a parameter of mode IN OUT or OUT
12107 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
12109 ("function cannot have parameter of mode `OUT` or "
12110 & "`IN OUT`", Formal);
12113 -- A procedure cannot have an effectively volatile formal
12114 -- parameter of mode IN because it behaves as a constant
12115 -- (SPARK RM 7.1.3(4)).
12117 elsif Ekind (Scope (Formal)) = E_Procedure
12118 and then Ekind (Formal) = E_In_Parameter
12119 and then Is_Effectively_Volatile (Formal)
12122 ("formal parameter of mode `IN` cannot be volatile", Formal);
12130 -- If this is the formal part of a function specification, analyze the
12131 -- subtype mark in the context where the formals are visible but not
12132 -- yet usable, and may hide outer homographs.
12134 if Nkind (Related_Nod) = N_Function_Specification then
12135 Analyze_Return_Type (Related_Nod);
12138 -- Now set the kind (mode) of each formal
12140 Param_Spec := First (T);
12141 while Present (Param_Spec) loop
12142 Formal := Defining_Identifier (Param_Spec);
12143 Set_Formal_Mode (Formal);
12145 if Ekind (Formal) = E_In_Parameter then
12146 Set_Default_Value (Formal, Expression (Param_Spec));
12148 if Present (Expression (Param_Spec)) then
12149 Default := Expression (Param_Spec);
12151 if Is_Scalar_Type (Etype (Default)) then
12152 if Nkind (Parameter_Type (Param_Spec)) /=
12153 N_Access_Definition
12155 Formal_Type := Entity (Parameter_Type (Param_Spec));
12159 (Related_Nod, Parameter_Type (Param_Spec));
12162 Apply_Scalar_Range_Check (Default, Formal_Type);
12166 elsif Ekind (Formal) = E_Out_Parameter then
12167 Num_Out_Params := Num_Out_Params + 1;
12169 if Num_Out_Params = 1 then
12170 First_Out_Param := Formal;
12173 elsif Ekind (Formal) = E_In_Out_Parameter then
12174 Num_Out_Params := Num_Out_Params + 1;
12177 -- Skip remaining processing if formal type was in error
12179 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12180 goto Next_Parameter;
12183 -- Force call by reference if aliased
12186 Conv : constant Convention_Id := Convention (Etype (Formal));
12188 if Is_Aliased (Formal) then
12189 Set_Mechanism (Formal, By_Reference);
12191 -- Warn if user asked this to be passed by copy
12193 if Conv = Convention_Ada_Pass_By_Copy then
12195 ("cannot pass aliased parameter & by copy??", Formal);
12198 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12200 elsif Conv = Convention_Ada_Pass_By_Copy then
12201 Set_Mechanism (Formal, By_Copy);
12203 elsif Conv = Convention_Ada_Pass_By_Reference then
12204 Set_Mechanism (Formal, By_Reference);
12212 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12213 Set_Is_Only_Out_Parameter (First_Out_Param);
12215 end Process_Formals;
12217 ----------------------------
12218 -- Reference_Body_Formals --
12219 ----------------------------
12221 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12226 if Error_Posted (Spec) then
12230 -- Iterate over both lists. They may be of different lengths if the two
12231 -- specs are not conformant.
12233 Fs := First_Formal (Spec);
12234 Fb := First_Formal (Bod);
12235 while Present (Fs) and then Present (Fb) loop
12236 Generate_Reference (Fs, Fb, 'b');
12238 if Style_Check then
12239 Style.Check_Identifier (Fb, Fs);
12242 Set_Spec_Entity (Fb, Fs);
12243 Set_Referenced (Fs, False);
12247 end Reference_Body_Formals;
12249 -------------------------
12250 -- Set_Actual_Subtypes --
12251 -------------------------
12253 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12255 Formal : Entity_Id;
12257 First_Stmt : Node_Id := Empty;
12258 AS_Needed : Boolean;
12261 -- If this is an empty initialization procedure, no need to create
12262 -- actual subtypes (small optimization).
12264 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12267 -- Within a predicate function we do not want to generate local
12268 -- subtypes that may generate nested predicate functions.
12270 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12274 -- The subtype declarations may freeze the formals. The body generated
12275 -- for an expression function is not a freeze point, so do not emit
12276 -- these declarations (small loss of efficiency in rare cases).
12278 if Nkind (N) = N_Subprogram_Body
12279 and then Was_Expression_Function (N)
12284 Formal := First_Formal (Subp);
12285 while Present (Formal) loop
12286 T := Etype (Formal);
12288 -- We never need an actual subtype for a constrained formal
12290 if Is_Constrained (T) then
12291 AS_Needed := False;
12293 -- If we have unknown discriminants, then we do not need an actual
12294 -- subtype, or more accurately we cannot figure it out. Note that
12295 -- all class-wide types have unknown discriminants.
12297 elsif Has_Unknown_Discriminants (T) then
12298 AS_Needed := False;
12300 -- At this stage we have an unconstrained type that may need an
12301 -- actual subtype. For sure the actual subtype is needed if we have
12302 -- an unconstrained array type. However, in an instance, the type
12303 -- may appear as a subtype of the full view, while the actual is
12304 -- in fact private (in which case no actual subtype is needed) so
12305 -- check the kind of the base type.
12307 elsif Is_Array_Type (Base_Type (T)) then
12310 -- The only other case needing an actual subtype is an unconstrained
12311 -- record type which is an IN parameter (we cannot generate actual
12312 -- subtypes for the OUT or IN OUT case, since an assignment can
12313 -- change the discriminant values. However we exclude the case of
12314 -- initialization procedures, since discriminants are handled very
12315 -- specially in this context, see the section entitled "Handling of
12316 -- Discriminants" in Einfo.
12318 -- We also exclude the case of Discrim_SO_Functions (functions used
12319 -- in front-end layout mode for size/offset values), since in such
12320 -- functions only discriminants are referenced, and not only are such
12321 -- subtypes not needed, but they cannot always be generated, because
12322 -- of order of elaboration issues.
12324 elsif Is_Record_Type (T)
12325 and then Ekind (Formal) = E_In_Parameter
12326 and then Chars (Formal) /= Name_uInit
12327 and then not Is_Unchecked_Union (T)
12328 and then not Is_Discrim_SO_Function (Subp)
12332 -- All other cases do not need an actual subtype
12335 AS_Needed := False;
12338 -- Generate actual subtypes for unconstrained arrays and
12339 -- unconstrained discriminated records.
12342 if Nkind (N) = N_Accept_Statement then
12344 -- If expansion is active, the formal is replaced by a local
12345 -- variable that renames the corresponding entry of the
12346 -- parameter block, and it is this local variable that may
12347 -- require an actual subtype.
12349 if Expander_Active then
12350 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12352 Decl := Build_Actual_Subtype (T, Formal);
12355 if Present (Handled_Statement_Sequence (N)) then
12357 First (Statements (Handled_Statement_Sequence (N)));
12358 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12359 Mark_Rewrite_Insertion (Decl);
12361 -- If the accept statement has no body, there will be no
12362 -- reference to the actuals, so no need to compute actual
12369 Decl := Build_Actual_Subtype (T, Formal);
12370 Prepend (Decl, Declarations (N));
12371 Mark_Rewrite_Insertion (Decl);
12374 -- The declaration uses the bounds of an existing object, and
12375 -- therefore needs no constraint checks.
12377 Analyze (Decl, Suppress => All_Checks);
12378 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12380 -- We need to freeze manually the generated type when it is
12381 -- inserted anywhere else than in a declarative part.
12383 if Present (First_Stmt) then
12384 Insert_List_Before_And_Analyze (First_Stmt,
12385 Freeze_Entity (Defining_Identifier (Decl), N));
12387 -- Ditto if the type has a dynamic predicate, because the
12388 -- generated function will mention the actual subtype. The
12389 -- predicate may come from an explicit aspect of be inherited.
12391 elsif Has_Predicates (T) then
12392 Insert_List_After_And_Analyze (Decl,
12393 Freeze_Entity (Defining_Identifier (Decl), N));
12396 if Nkind (N) = N_Accept_Statement
12397 and then Expander_Active
12399 Set_Actual_Subtype (Renamed_Object (Formal),
12400 Defining_Identifier (Decl));
12402 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12406 Next_Formal (Formal);
12408 end Set_Actual_Subtypes;
12410 ---------------------
12411 -- Set_Formal_Mode --
12412 ---------------------
12414 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12415 Spec : constant Node_Id := Parent (Formal_Id);
12416 Id : constant Entity_Id := Scope (Formal_Id);
12419 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12420 -- since we ensure that corresponding actuals are always valid at the
12421 -- point of the call.
12423 if Out_Present (Spec) then
12425 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12427 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12430 if Ekind_In (Id, E_Function, E_Generic_Function) then
12432 -- [IN] OUT parameters allowed for functions in Ada 2012
12434 if Ada_Version >= Ada_2012 then
12436 -- Even in Ada 2012 operators can only have IN parameters
12438 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12439 Error_Msg_N ("operators can only have IN parameters", Spec);
12442 if In_Present (Spec) then
12443 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12445 Set_Ekind (Formal_Id, E_Out_Parameter);
12448 -- But not in earlier versions of Ada
12451 Error_Msg_N ("functions can only have IN parameters", Spec);
12452 Set_Ekind (Formal_Id, E_In_Parameter);
12455 elsif In_Present (Spec) then
12456 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12459 Set_Ekind (Formal_Id, E_Out_Parameter);
12460 Set_Never_Set_In_Source (Formal_Id, True);
12461 Set_Is_True_Constant (Formal_Id, False);
12462 Set_Current_Value (Formal_Id, Empty);
12466 Set_Ekind (Formal_Id, E_In_Parameter);
12469 -- Set Is_Known_Non_Null for access parameters since the language
12470 -- guarantees that access parameters are always non-null. We also set
12471 -- Can_Never_Be_Null, since there is no way to change the value.
12473 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12475 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12476 -- null; In Ada 2005, only if then null_exclusion is explicit.
12478 if Ada_Version < Ada_2005
12479 or else Can_Never_Be_Null (Etype (Formal_Id))
12481 Set_Is_Known_Non_Null (Formal_Id);
12482 Set_Can_Never_Be_Null (Formal_Id);
12485 -- Ada 2005 (AI-231): Null-exclusion access subtype
12487 elsif Is_Access_Type (Etype (Formal_Id))
12488 and then Can_Never_Be_Null (Etype (Formal_Id))
12490 Set_Is_Known_Non_Null (Formal_Id);
12492 -- We can also set Can_Never_Be_Null (thus preventing some junk
12493 -- access checks) for the case of an IN parameter, which cannot
12494 -- be changed, or for an IN OUT parameter, which can be changed but
12495 -- not to a null value. But for an OUT parameter, the initial value
12496 -- passed in can be null, so we can't set this flag in that case.
12498 if Ekind (Formal_Id) /= E_Out_Parameter then
12499 Set_Can_Never_Be_Null (Formal_Id);
12503 Set_Mechanism (Formal_Id, Default_Mechanism);
12504 Set_Formal_Validity (Formal_Id);
12505 end Set_Formal_Mode;
12507 -------------------------
12508 -- Set_Formal_Validity --
12509 -------------------------
12511 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12513 -- If no validity checking, then we cannot assume anything about the
12514 -- validity of parameters, since we do not know there is any checking
12515 -- of the validity on the call side.
12517 if not Validity_Checks_On then
12520 -- If validity checking for parameters is enabled, this means we are
12521 -- not supposed to make any assumptions about argument values.
12523 elsif Validity_Check_Parameters then
12526 -- If we are checking in parameters, we will assume that the caller is
12527 -- also checking parameters, so we can assume the parameter is valid.
12529 elsif Ekind (Formal_Id) = E_In_Parameter
12530 and then Validity_Check_In_Params
12532 Set_Is_Known_Valid (Formal_Id, True);
12534 -- Similar treatment for IN OUT parameters
12536 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12537 and then Validity_Check_In_Out_Params
12539 Set_Is_Known_Valid (Formal_Id, True);
12541 end Set_Formal_Validity;
12543 ------------------------
12544 -- Subtype_Conformant --
12545 ------------------------
12547 function Subtype_Conformant
12548 (New_Id : Entity_Id;
12549 Old_Id : Entity_Id;
12550 Skip_Controlling_Formals : Boolean := False) return Boolean
12554 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12555 Skip_Controlling_Formals => Skip_Controlling_Formals);
12557 end Subtype_Conformant;
12559 ---------------------
12560 -- Type_Conformant --
12561 ---------------------
12563 function Type_Conformant
12564 (New_Id : Entity_Id;
12565 Old_Id : Entity_Id;
12566 Skip_Controlling_Formals : Boolean := False) return Boolean
12570 May_Hide_Profile := False;
12572 (New_Id, Old_Id, Type_Conformant, False, Result,
12573 Skip_Controlling_Formals => Skip_Controlling_Formals);
12575 end Type_Conformant;
12577 -------------------------------
12578 -- Valid_Operator_Definition --
12579 -------------------------------
12581 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12584 Id : constant Name_Id := Chars (Designator);
12588 F := First_Formal (Designator);
12589 while Present (F) loop
12592 if Present (Default_Value (F)) then
12594 ("default values not allowed for operator parameters",
12597 -- For function instantiations that are operators, we must check
12598 -- separately that the corresponding generic only has in-parameters.
12599 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12600 -- an error could not arise in earlier versions of the language.
12602 elsif Ekind (F) /= E_In_Parameter then
12603 Error_Msg_N ("operators can only have IN parameters", F);
12609 -- Verify that user-defined operators have proper number of arguments
12610 -- First case of operators which can only be unary
12612 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12615 -- Case of operators which can be unary or binary
12617 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12618 N_OK := (N in 1 .. 2);
12620 -- All other operators can only be binary
12628 ("incorrect number of arguments for operator", Designator);
12632 and then Base_Type (Etype (Designator)) = Standard_Boolean
12633 and then not Is_Intrinsic_Subprogram (Designator)
12636 ("explicit definition of inequality not allowed", Designator);
12638 end Valid_Operator_Definition;