2015-05-01 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob2f9e1f5532bc9271ea292cb2fc94be30416760f5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Inline; use Inline;
45 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Layout; use Layout;
48 with Namet; use Namet;
49 with Lib; use Lib;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Output; use Output;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
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;
84 with Style;
85 with Stylesw; use Stylesw;
86 with Targparm; use Targparm;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
116 (N : Node_Id;
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
126 -- outer homographs.
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
136 (New_Id : Entity_Id;
137 Old_Id : Entity_Id;
138 Ctype : Conformance_Type;
139 Errmsg : Boolean;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 -- be called.
156 procedure Check_Subprogram_Order (N : Node_Id);
157 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
158 -- the alpha ordering rule for N if this ordering requirement applicable.
160 procedure Check_Returns
161 (HSS : Node_Id;
162 Mode : Character;
163 Err : out Boolean;
164 Proc : Entity_Id := Empty);
165 -- Called to check for missing return statements in a function body, or for
166 -- returns present in a procedure body which has No_Return set. HSS is the
167 -- handled statement sequence for the subprogram body. This procedure
168 -- checks all flow paths to make sure they either have return (Mode = 'F',
169 -- used for functions) or do not have a return (Mode = 'P', used for
170 -- No_Return procedures). The flag Err is set if there are any control
171 -- paths not explicitly terminated by a return in the function case, and is
172 -- True otherwise. Proc is the entity for the procedure case and is used
173 -- in posting the warning message.
175 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
176 -- In Ada 2012, a primitive equality operator on an untagged record type
177 -- must appear before the type is frozen, and have the same visibility as
178 -- that of the type. This procedure checks that this rule is met, and
179 -- otherwise emits an error on the subprogram declaration and a warning
180 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
181 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
182 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
183 -- is set, otherwise the call has no effect.
185 procedure Enter_Overloaded_Entity (S : Entity_Id);
186 -- This procedure makes S, a new overloaded entity, into the first visible
187 -- entity with that name.
189 function Is_Non_Overriding_Operation
190 (Prev_E : Entity_Id;
191 New_E : Entity_Id) return Boolean;
192 -- Enforce the rule given in 12.3(18): a private operation in an instance
193 -- overrides an inherited operation only if the corresponding operation
194 -- was overriding in the generic. This needs to be checked for primitive
195 -- operations of types derived (in the generic unit) from formal private
196 -- or formal derived types.
198 procedure Make_Inequality_Operator (S : Entity_Id);
199 -- Create the declaration for an inequality operator that is implicitly
200 -- created by a user-defined equality operator that yields a boolean.
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ---------------------------------------------
208 -- Analyze_Abstract_Subprogram_Declaration --
209 ---------------------------------------------
211 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
212 Designator : constant Entity_Id :=
213 Analyze_Subprogram_Specification (Specification (N));
214 Scop : constant Entity_Id := Current_Scope;
216 begin
217 -- The abstract subprogram declaration may be subject to pragma Ghost
218 -- with policy Ignore. Set the mode now to ensure that any nodes
219 -- generated during analysis and expansion are properly flagged as
220 -- ignored Ghost.
222 Set_Ghost_Mode (N);
223 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
225 Generate_Definition (Designator);
227 Set_Is_Abstract_Subprogram (Designator);
228 New_Overloaded_Entity (Designator);
229 Check_Delayed_Subprogram (Designator);
231 Set_Categorization_From_Scope (Designator, Scop);
233 -- An abstract subprogram declared within a Ghost region is rendered
234 -- Ghost (SPARK RM 6.9(2)).
236 if Comes_From_Source (Designator) and then Ghost_Mode > None then
237 Set_Is_Ghost_Entity (Designator);
238 end if;
240 if Ekind (Scope (Designator)) = E_Protected_Type then
241 Error_Msg_N
242 ("abstract subprogram not allowed in protected type", N);
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation (Designator)
250 and then not Present (Overridden_Operation (Designator))
251 and then (not Is_Operator_Symbol_Name (Chars (Designator))
252 or else Scop /= Scope (Etype (First_Formal (Designator))))
253 then
254 Error_Msg_N
255 ("abstract subprogram is not dispatching or overriding?r?", N);
256 end if;
258 Generate_Reference_To_Formals (Designator);
259 Check_Eliminated (Designator);
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Designator);
263 end if;
264 end Analyze_Abstract_Subprogram_Declaration;
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
270 procedure Analyze_Expression_Function (N : Node_Id) is
271 Loc : constant Source_Ptr := Sloc (N);
272 LocX : constant Source_Ptr := Sloc (Expression (N));
273 Expr : constant Node_Id := Expression (N);
274 Spec : constant Node_Id := Specification (N);
276 Def_Id : Entity_Id;
278 Prev : Entity_Id;
279 -- If the expression is a completion, Prev is the entity whose
280 -- declaration is completed. Def_Id is needed to analyze the spec.
282 New_Body : Node_Id;
283 New_Spec : Node_Id;
284 Ret : Node_Id;
286 begin
287 -- This is one of the occasions on which we transform the tree during
288 -- semantic analysis. If this is a completion, transform the expression
289 -- function into an equivalent subprogram body, and analyze it.
291 -- Expression functions are inlined unconditionally. The back-end will
292 -- determine whether this is possible.
294 Inline_Processing_Required := True;
296 -- Create a specification for the generated body. Types and defauts in
297 -- the profile are copies of the spec, but new entities must be created
298 -- for the unit name and the formals.
300 New_Spec := New_Copy_Tree (Spec);
301 Set_Defining_Unit_Name (New_Spec,
302 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
303 Chars (Defining_Unit_Name (Spec))));
305 if Present (Parameter_Specifications (New_Spec)) then
306 declare
307 Formal_Spec : Node_Id;
308 Def : Entity_Id;
310 begin
311 Formal_Spec := First (Parameter_Specifications (New_Spec));
313 -- Create a new formal parameter at the same source position
315 while Present (Formal_Spec) loop
316 Def := Defining_Identifier (Formal_Spec);
317 Set_Defining_Identifier (Formal_Spec,
318 Make_Defining_Identifier (Sloc (Def),
319 Chars => Chars (Def)));
320 Next (Formal_Spec);
321 end loop;
322 end;
323 end if;
325 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
327 -- If there are previous overloadable entities with the same name,
328 -- check whether any of them is completed by the expression function.
329 -- In a generic context a formal subprogram has no completion.
331 if Present (Prev)
332 and then Is_Overloadable (Prev)
333 and then not Is_Formal_Subprogram (Prev)
334 then
335 Def_Id := Analyze_Subprogram_Specification (Spec);
336 Prev := Find_Corresponding_Spec (N);
338 -- The previous entity may be an expression function as well, in
339 -- which case the redeclaration is illegal.
341 if Present (Prev)
342 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
343 N_Expression_Function
344 then
345 Error_Msg_Sloc := Sloc (Prev);
346 Error_Msg_N ("& conflicts with declaration#", Def_Id);
347 return;
348 end if;
349 end if;
351 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
353 New_Body :=
354 Make_Subprogram_Body (Loc,
355 Specification => New_Spec,
356 Declarations => Empty_List,
357 Handled_Statement_Sequence =>
358 Make_Handled_Sequence_Of_Statements (LocX,
359 Statements => New_List (Ret)));
361 -- If the expression completes a generic subprogram, we must create a
362 -- separate node for the body, because at instantiation the original
363 -- node of the generic copy must be a generic subprogram body, and
364 -- cannot be a expression function. Otherwise we just rewrite the
365 -- expression with the non-generic body.
367 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
368 Insert_After (N, New_Body);
370 -- Propagate any aspects or pragmas that apply to the expression
371 -- function to the proper body when the expression function acts
372 -- as a completion.
374 if Has_Aspects (N) then
375 Move_Aspects (N, To => New_Body);
376 end if;
378 Relocate_Pragmas_To_Body (New_Body);
380 Rewrite (N, Make_Null_Statement (Loc));
381 Set_Has_Completion (Prev, False);
382 Analyze (N);
383 Analyze (New_Body);
384 Set_Is_Inlined (Prev);
386 -- If the expression function is a completion, the previous declaration
387 -- must come from source. We know already that appears in the current
388 -- scope. The entity itself may be internally created if within a body
389 -- to be inlined.
391 elsif Present (Prev)
392 and then Comes_From_Source (Parent (Prev))
393 and then not Is_Formal_Subprogram (Prev)
394 then
395 Set_Has_Completion (Prev, False);
397 -- An expression function that is a completion freezes the
398 -- expression. This means freezing the return type, and if it is
399 -- an access type, freezing its designated type as well.
401 -- Note that we cannot defer this freezing to the analysis of the
402 -- expression itself, because a freeze node might appear in a nested
403 -- scope, leading to an elaboration order issue in gigi.
405 Freeze_Before (N, Etype (Prev));
407 if Is_Access_Type (Etype (Prev)) then
408 Freeze_Before (N, Designated_Type (Etype (Prev)));
409 end if;
411 -- For navigation purposes, indicate that the function is a body
413 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
414 Rewrite (N, New_Body);
416 -- Correct the parent pointer of the aspect specification list to
417 -- reference the rewritten node.
419 if Has_Aspects (N) then
420 Set_Parent (Aspect_Specifications (N), N);
421 end if;
423 -- Propagate any pragmas that apply to the expression function to the
424 -- proper body when the expression function acts as a completion.
425 -- Aspects are automatically transfered because of node rewriting.
427 Relocate_Pragmas_To_Body (N);
428 Analyze (N);
430 -- Prev is the previous entity with the same name, but it is can
431 -- be an unrelated spec that is not completed by the expression
432 -- function. In that case the relevant entity is the one in the body.
433 -- Not clear that the backend can inline it in this case ???
435 if Has_Completion (Prev) then
436 Set_Is_Inlined (Prev);
438 -- The formals of the expression function are body formals,
439 -- and do not appear in the ali file, which will only contain
440 -- references to the formals of the original subprogram spec.
442 declare
443 F1 : Entity_Id;
444 F2 : Entity_Id;
446 begin
447 F1 := First_Formal (Def_Id);
448 F2 := First_Formal (Prev);
450 while Present (F1) loop
451 Set_Spec_Entity (F1, F2);
452 Next_Formal (F1);
453 Next_Formal (F2);
454 end loop;
455 end;
457 else
458 Set_Is_Inlined (Defining_Entity (New_Body));
459 end if;
461 -- If this is not a completion, create both a declaration and a body, so
462 -- that the expression can be inlined whenever possible.
464 else
465 -- An expression function that is not a completion is not a
466 -- subprogram declaration, and thus cannot appear in a protected
467 -- definition.
469 if Nkind (Parent (N)) = N_Protected_Definition then
470 Error_Msg_N
471 ("an expression function is not a legal protected operation", N);
472 end if;
474 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
476 -- Correct the parent pointer of the aspect specification list to
477 -- reference the rewritten node.
479 if Has_Aspects (N) then
480 Set_Parent (Aspect_Specifications (N), N);
481 end if;
483 Analyze (N);
485 -- Within a generic pre-analyze the original expression for name
486 -- capture. The body is also generated but plays no role in
487 -- this because it is not part of the original source.
489 if Inside_A_Generic then
490 declare
491 Id : constant Entity_Id := Defining_Entity (N);
493 begin
494 Set_Has_Completion (Id);
495 Push_Scope (Id);
496 Install_Formals (Id);
497 Preanalyze_Spec_Expression (Expr, Etype (Id));
498 End_Scope;
499 end;
500 end if;
502 Set_Is_Inlined (Defining_Entity (N));
504 -- Establish the linkages between the spec and the body. These are
505 -- used when the expression function acts as the prefix of attribute
506 -- 'Access in order to freeze the original expression which has been
507 -- moved to the generated body.
509 Set_Corresponding_Body (N, Defining_Entity (New_Body));
510 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
512 -- To prevent premature freeze action, insert the new body at the end
513 -- of the current declarations, or at the end of the package spec.
514 -- However, resolve usage names now, to prevent spurious visibility
515 -- on later entities. Note that the function can now be called in
516 -- the current declarative part, which will appear to be prior to
517 -- the presence of the body in the code. There are nevertheless no
518 -- order of elaboration issues because all name resolution has taken
519 -- place at the point of declaration.
521 declare
522 Decls : List_Id := List_Containing (N);
523 Par : constant Node_Id := Parent (Decls);
524 Id : constant Entity_Id := Defining_Entity (N);
526 begin
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.
531 if GNATprove_Mode
532 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
533 then
534 Insert_After (N, New_Body);
536 else
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))
541 then
542 Decls := Private_Declarations (Par);
543 end if;
545 Insert_After (Last (Decls), New_Body);
546 Push_Scope (Id);
547 Install_Formals (Id);
549 -- Preanalyze the expression for name capture, except in an
550 -- instance, where this has been done during generic analysis,
551 -- and will be redone when analyzing the body.
553 declare
554 Expr : constant Node_Id := Expression (Ret);
556 begin
557 Set_Parent (Expr, Ret);
559 if not In_Instance then
560 Preanalyze_Spec_Expression (Expr, Etype (Id));
561 end if;
562 end;
564 End_Scope;
565 end if;
566 end;
567 end if;
569 -- If the return expression is a static constant, we suppress warning
570 -- messages on unused formals, which in most cases will be noise.
572 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
573 Is_OK_Static_Expression (Expr));
574 end Analyze_Expression_Function;
576 ----------------------------------------
577 -- Analyze_Extended_Return_Statement --
578 ----------------------------------------
580 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
581 begin
582 Check_Compiler_Unit ("extended return statement", N);
583 Analyze_Return_Statement (N);
584 end Analyze_Extended_Return_Statement;
586 ----------------------------
587 -- Analyze_Function_Call --
588 ----------------------------
590 procedure Analyze_Function_Call (N : Node_Id) is
591 Actuals : constant List_Id := Parameter_Associations (N);
592 Func_Nam : constant Node_Id := Name (N);
593 Actual : Node_Id;
595 begin
596 Analyze (Func_Nam);
598 -- A call of the form A.B (X) may be an Ada 2005 call, which is
599 -- rewritten as B (A, X). If the rewriting is successful, the call
600 -- has been analyzed and we just return.
602 if Nkind (Func_Nam) = N_Selected_Component
603 and then Name (N) /= Func_Nam
604 and then Is_Rewrite_Substitution (N)
605 and then Present (Etype (N))
606 then
607 return;
608 end if;
610 -- If error analyzing name, then set Any_Type as result type and return
612 if Etype (Func_Nam) = Any_Type then
613 Set_Etype (N, Any_Type);
614 return;
615 end if;
617 -- Otherwise analyze the parameters
619 if Present (Actuals) then
620 Actual := First (Actuals);
621 while Present (Actual) loop
622 Analyze (Actual);
623 Check_Parameterless_Call (Actual);
624 Next (Actual);
625 end loop;
626 end if;
628 Analyze_Call (N);
629 end Analyze_Function_Call;
631 -----------------------------
632 -- Analyze_Function_Return --
633 -----------------------------
635 procedure Analyze_Function_Return (N : Node_Id) is
636 Loc : constant Source_Ptr := Sloc (N);
637 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
638 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
640 R_Type : constant Entity_Id := Etype (Scope_Id);
641 -- Function result subtype
643 procedure Check_Limited_Return (Expr : Node_Id);
644 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
645 -- limited types. Used only for simple return statements.
646 -- Expr is the expression returned.
648 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
649 -- Check that the return_subtype_indication properly matches the result
650 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
652 --------------------------
653 -- Check_Limited_Return --
654 --------------------------
656 procedure Check_Limited_Return (Expr : Node_Id) is
657 begin
658 -- Ada 2005 (AI-318-02): Return-by-reference types have been
659 -- removed and replaced by anonymous access results. This is an
660 -- incompatibility with Ada 95. Not clear whether this should be
661 -- enforced yet or perhaps controllable with special switch. ???
663 -- A limited interface that is not immutably limited is OK.
665 if Is_Limited_Interface (R_Type)
666 and then
667 not (Is_Task_Interface (R_Type)
668 or else Is_Protected_Interface (R_Type)
669 or else Is_Synchronized_Interface (R_Type))
670 then
671 null;
673 elsif Is_Limited_Type (R_Type)
674 and then not Is_Interface (R_Type)
675 and then Comes_From_Source (N)
676 and then not In_Instance_Body
677 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
678 then
679 -- Error in Ada 2005
681 if Ada_Version >= Ada_2005
682 and then not Debug_Flag_Dot_L
683 and then not GNAT_Mode
684 then
685 Error_Msg_N
686 ("(Ada 2005) cannot copy object of a limited type "
687 & "(RM-2005 6.5(5.5/2))", Expr);
689 if Is_Limited_View (R_Type) then
690 Error_Msg_N
691 ("\return by reference not permitted in Ada 2005", Expr);
692 end if;
694 -- Warn in Ada 95 mode, to give folks a heads up about this
695 -- incompatibility.
697 -- In GNAT mode, this is just a warning, to allow it to be
698 -- evilly turned off. Otherwise it is a real error.
700 -- In a generic context, simplify the warning because it makes
701 -- no sense to discuss pass-by-reference or copy.
703 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
704 if Inside_A_Generic then
705 Error_Msg_N
706 ("return of limited object not permitted in Ada 2005 "
707 & "(RM-2005 6.5(5.5/2))?y?", Expr);
709 elsif Is_Limited_View (R_Type) then
710 Error_Msg_N
711 ("return by reference not permitted in Ada 2005 "
712 & "(RM-2005 6.5(5.5/2))?y?", Expr);
713 else
714 Error_Msg_N
715 ("cannot copy object of a limited type in Ada 2005 "
716 & "(RM-2005 6.5(5.5/2))?y?", Expr);
717 end if;
719 -- Ada 95 mode, compatibility warnings disabled
721 else
722 return; -- skip continuation messages below
723 end if;
725 if not Inside_A_Generic then
726 Error_Msg_N
727 ("\consider switching to return of access type", Expr);
728 Explain_Limited_Type (R_Type, Expr);
729 end if;
730 end if;
731 end Check_Limited_Return;
733 -------------------------------------
734 -- Check_Return_Subtype_Indication --
735 -------------------------------------
737 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
738 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
740 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
741 -- Subtype given in the extended return statement (must match R_Type)
743 Subtype_Ind : constant Node_Id :=
744 Object_Definition (Original_Node (Obj_Decl));
746 R_Type_Is_Anon_Access : constant Boolean :=
747 Ekind_In (R_Type,
748 E_Anonymous_Access_Subprogram_Type,
749 E_Anonymous_Access_Protected_Subprogram_Type,
750 E_Anonymous_Access_Type);
751 -- True if return type of the function is an anonymous access type
752 -- Can't we make Is_Anonymous_Access_Type in einfo ???
754 R_Stm_Type_Is_Anon_Access : constant Boolean :=
755 Ekind_In (R_Stm_Type,
756 E_Anonymous_Access_Subprogram_Type,
757 E_Anonymous_Access_Protected_Subprogram_Type,
758 E_Anonymous_Access_Type);
759 -- True if type of the return object is an anonymous access type
761 procedure Error_No_Match (N : Node_Id);
762 -- Output error messages for case where types do not statically
763 -- match. N is the location for the messages.
765 --------------------
766 -- Error_No_Match --
767 --------------------
769 procedure Error_No_Match (N : Node_Id) is
770 begin
771 Error_Msg_N
772 ("subtype must statically match function result subtype", N);
774 if not Predicates_Match (R_Stm_Type, R_Type) then
775 Error_Msg_Node_2 := R_Type;
776 Error_Msg_NE
777 ("\predicate of& does not match predicate of&",
778 N, R_Stm_Type);
779 end if;
780 end Error_No_Match;
782 -- Start of processing for Check_Return_Subtype_Indication
784 begin
785 -- First, avoid cascaded errors
787 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
788 return;
789 end if;
791 -- "return access T" case; check that the return statement also has
792 -- "access T", and that the subtypes statically match:
793 -- if this is an access to subprogram the signatures must match.
795 if R_Type_Is_Anon_Access then
796 if R_Stm_Type_Is_Anon_Access then
798 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
799 then
800 if Base_Type (Designated_Type (R_Stm_Type)) /=
801 Base_Type (Designated_Type (R_Type))
802 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
803 then
804 Error_No_Match (Subtype_Mark (Subtype_Ind));
805 end if;
807 else
808 -- For two anonymous access to subprogram types, the
809 -- types themselves must be type conformant.
811 if not Conforming_Types
812 (R_Stm_Type, R_Type, Fully_Conformant)
813 then
814 Error_No_Match (Subtype_Ind);
815 end if;
816 end if;
818 else
819 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
820 end if;
822 -- If the return object is of an anonymous access type, then report
823 -- an error if the function's result type is not also anonymous.
825 elsif R_Stm_Type_Is_Anon_Access
826 and then not R_Type_Is_Anon_Access
827 then
828 Error_Msg_N ("anonymous access not allowed for function with "
829 & "named access result", Subtype_Ind);
831 -- Subtype indication case: check that the return object's type is
832 -- covered by the result type, and that the subtypes statically match
833 -- when the result subtype is constrained. Also handle record types
834 -- with unknown discriminants for which we have built the underlying
835 -- record view. Coverage is needed to allow specific-type return
836 -- objects when the result type is class-wide (see AI05-32).
838 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
839 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
840 and then
841 Covers
842 (Base_Type (R_Type),
843 Underlying_Record_View (Base_Type (R_Stm_Type))))
844 then
845 -- A null exclusion may be present on the return type, on the
846 -- function specification, on the object declaration or on the
847 -- subtype itself.
849 if Is_Access_Type (R_Type)
850 and then
851 (Can_Never_Be_Null (R_Type)
852 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
853 Can_Never_Be_Null (R_Stm_Type)
854 then
855 Error_No_Match (Subtype_Ind);
856 end if;
858 -- AI05-103: for elementary types, subtypes must statically match
860 if Is_Constrained (R_Type)
861 or else Is_Access_Type (R_Type)
862 then
863 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
864 Error_No_Match (Subtype_Ind);
865 end if;
866 end if;
868 -- All remaining cases are illegal
870 -- Note: previous versions of this subprogram allowed the return
871 -- value to be the ancestor of the return type if the return type
872 -- was a null extension. This was plainly incorrect.
874 else
875 Error_Msg_N
876 ("wrong type for return_subtype_indication", Subtype_Ind);
877 end if;
878 end Check_Return_Subtype_Indication;
880 ---------------------
881 -- Local Variables --
882 ---------------------
884 Expr : Node_Id;
885 Obj_Decl : Node_Id;
887 -- Start of processing for Analyze_Function_Return
889 begin
890 Set_Return_Present (Scope_Id);
892 if Nkind (N) = N_Simple_Return_Statement then
893 Expr := Expression (N);
895 -- Guard against a malformed expression. The parser may have tried to
896 -- recover but the node is not analyzable.
898 if Nkind (Expr) = N_Error then
899 Set_Etype (Expr, Any_Type);
900 Expander_Mode_Save_And_Set (False);
901 return;
903 else
904 -- The resolution of a controlled [extension] aggregate associated
905 -- with a return statement creates a temporary which needs to be
906 -- finalized on function exit. Wrap the return statement inside a
907 -- block so that the finalization machinery can detect this case.
908 -- This early expansion is done only when the return statement is
909 -- not part of a handled sequence of statements.
911 if Nkind_In (Expr, N_Aggregate,
912 N_Extension_Aggregate)
913 and then Needs_Finalization (R_Type)
914 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
915 then
916 Rewrite (N,
917 Make_Block_Statement (Loc,
918 Handled_Statement_Sequence =>
919 Make_Handled_Sequence_Of_Statements (Loc,
920 Statements => New_List (Relocate_Node (N)))));
922 Analyze (N);
923 return;
924 end if;
926 Analyze (Expr);
928 -- Ada 2005 (AI-251): If the type of the returned object is
929 -- an access to an interface type then we add an implicit type
930 -- conversion to force the displacement of the "this" pointer to
931 -- reference the secondary dispatch table. We cannot delay the
932 -- generation of this implicit conversion until the expansion
933 -- because in this case the type resolution changes the decoration
934 -- of the expression node to match R_Type; by contrast, if the
935 -- returned object is a class-wide interface type then it is too
936 -- early to generate here the implicit conversion since the return
937 -- statement may be rewritten by the expander into an extended
938 -- return statement whose expansion takes care of adding the
939 -- implicit type conversion to displace the pointer to the object.
941 if Expander_Active
942 and then Serious_Errors_Detected = 0
943 and then Is_Access_Type (R_Type)
944 and then Nkind (Expr) /= N_Null
945 and then Is_Interface (Designated_Type (R_Type))
946 and then Is_Progenitor (Designated_Type (R_Type),
947 Designated_Type (Etype (Expr)))
948 then
949 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
950 Analyze (Expr);
951 end if;
953 Resolve (Expr, R_Type);
954 Check_Limited_Return (Expr);
955 end if;
957 -- RETURN only allowed in SPARK as the last statement in function
959 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
960 and then
961 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
962 or else Present (Next (N)))
963 then
964 Check_SPARK_05_Restriction
965 ("RETURN should be the last statement in function", N);
966 end if;
968 else
969 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
970 Obj_Decl := Last (Return_Object_Declarations (N));
972 -- Analyze parts specific to extended_return_statement:
974 declare
975 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
976 HSS : constant Node_Id := Handled_Statement_Sequence (N);
978 begin
979 Expr := Expression (Obj_Decl);
981 -- Note: The check for OK_For_Limited_Init will happen in
982 -- Analyze_Object_Declaration; we treat it as a normal
983 -- object declaration.
985 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
986 Analyze (Obj_Decl);
988 Check_Return_Subtype_Indication (Obj_Decl);
990 if Present (HSS) then
991 Analyze (HSS);
993 if Present (Exception_Handlers (HSS)) then
995 -- ???Has_Nested_Block_With_Handler needs to be set.
996 -- Probably by creating an actual N_Block_Statement.
997 -- Probably in Expand.
999 null;
1000 end if;
1001 end if;
1003 -- Mark the return object as referenced, since the return is an
1004 -- implicit reference of the object.
1006 Set_Referenced (Defining_Identifier (Obj_Decl));
1008 Check_References (Stm_Entity);
1010 -- Check RM 6.5 (5.9/3)
1012 if Has_Aliased then
1013 if Ada_Version < Ada_2012 then
1015 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1016 -- Can it really happen (extended return???)
1018 Error_Msg_N
1019 ("aliased only allowed for limited return objects "
1020 & "in Ada 2012??", N);
1022 elsif not Is_Limited_View (R_Type) then
1023 Error_Msg_N
1024 ("aliased only allowed for limited return objects", N);
1025 end if;
1026 end if;
1027 end;
1028 end if;
1030 -- Case of Expr present
1032 if Present (Expr)
1034 -- Defend against previous errors
1036 and then Nkind (Expr) /= N_Empty
1037 and then Present (Etype (Expr))
1038 then
1039 -- Apply constraint check. Note that this is done before the implicit
1040 -- conversion of the expression done for anonymous access types to
1041 -- ensure correct generation of the null-excluding check associated
1042 -- with null-excluding expressions found in return statements.
1044 Apply_Constraint_Check (Expr, R_Type);
1046 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1047 -- type, apply an implicit conversion of the expression to that type
1048 -- to force appropriate static and run-time accessibility checks.
1050 if Ada_Version >= Ada_2005
1051 and then Ekind (R_Type) = E_Anonymous_Access_Type
1052 then
1053 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1054 Analyze_And_Resolve (Expr, R_Type);
1056 -- If this is a local anonymous access to subprogram, the
1057 -- accessibility check can be applied statically. The return is
1058 -- illegal if the access type of the return expression is declared
1059 -- inside of the subprogram (except if it is the subtype indication
1060 -- of an extended return statement).
1062 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1063 if not Comes_From_Source (Current_Scope)
1064 or else Ekind (Current_Scope) = E_Return_Statement
1065 then
1066 null;
1068 elsif
1069 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1070 then
1071 Error_Msg_N ("cannot return local access to subprogram", N);
1072 end if;
1074 -- The expression cannot be of a formal incomplete type
1076 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1077 and then Is_Generic_Type (Etype (Expr))
1078 then
1079 Error_Msg_N
1080 ("cannot return expression of a formal incomplete type", N);
1081 end if;
1083 -- If the result type is class-wide, then check that the return
1084 -- expression's type is not declared at a deeper level than the
1085 -- function (RM05-6.5(5.6/2)).
1087 if Ada_Version >= Ada_2005
1088 and then Is_Class_Wide_Type (R_Type)
1089 then
1090 if Type_Access_Level (Etype (Expr)) >
1091 Subprogram_Access_Level (Scope_Id)
1092 then
1093 Error_Msg_N
1094 ("level of return expression type is deeper than "
1095 & "class-wide function!", Expr);
1096 end if;
1097 end if;
1099 -- Check incorrect use of dynamically tagged expression
1101 if Is_Tagged_Type (R_Type) then
1102 Check_Dynamically_Tagged_Expression
1103 (Expr => Expr,
1104 Typ => R_Type,
1105 Related_Nod => N);
1106 end if;
1108 -- ??? A real run-time accessibility check is needed in cases
1109 -- involving dereferences of access parameters. For now we just
1110 -- check the static cases.
1112 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1113 and then Is_Limited_View (Etype (Scope_Id))
1114 and then Object_Access_Level (Expr) >
1115 Subprogram_Access_Level (Scope_Id)
1116 then
1117 -- Suppress the message in a generic, where the rewriting
1118 -- is irrelevant.
1120 if Inside_A_Generic then
1121 null;
1123 else
1124 Rewrite (N,
1125 Make_Raise_Program_Error (Loc,
1126 Reason => PE_Accessibility_Check_Failed));
1127 Analyze (N);
1129 Error_Msg_Warn := SPARK_Mode /= On;
1130 Error_Msg_N ("cannot return a local value by reference<<", N);
1131 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1132 end if;
1133 end if;
1135 if Known_Null (Expr)
1136 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1137 and then Null_Exclusion_Present (Parent (Scope_Id))
1138 then
1139 Apply_Compile_Time_Constraint_Error
1140 (N => Expr,
1141 Msg => "(Ada 2005) null not allowed for "
1142 & "null-excluding return??",
1143 Reason => CE_Null_Not_Allowed);
1144 end if;
1146 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1147 -- has no initializing expression.
1149 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1150 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1151 Subprogram_Access_Level (Scope_Id)
1152 then
1153 Error_Msg_N
1154 ("level of return expression type is deeper than "
1155 & "class-wide function!", Obj_Decl);
1156 end if;
1157 end if;
1158 end Analyze_Function_Return;
1160 -------------------------------------
1161 -- Analyze_Generic_Subprogram_Body --
1162 -------------------------------------
1164 procedure Analyze_Generic_Subprogram_Body
1165 (N : Node_Id;
1166 Gen_Id : Entity_Id)
1168 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1169 Kind : constant Entity_Kind := Ekind (Gen_Id);
1170 Body_Id : Entity_Id;
1171 New_N : Node_Id;
1172 Spec : Node_Id;
1174 begin
1175 -- Copy body and disable expansion while analyzing the generic For a
1176 -- stub, do not copy the stub (which would load the proper body), this
1177 -- will be done when the proper body is analyzed.
1179 if Nkind (N) /= N_Subprogram_Body_Stub then
1180 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1181 Rewrite (N, New_N);
1182 Start_Generic;
1183 end if;
1185 Spec := Specification (N);
1187 -- Within the body of the generic, the subprogram is callable, and
1188 -- behaves like the corresponding non-generic unit.
1190 Body_Id := Defining_Entity (Spec);
1192 if Kind = E_Generic_Procedure
1193 and then Nkind (Spec) /= N_Procedure_Specification
1194 then
1195 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1196 return;
1198 elsif Kind = E_Generic_Function
1199 and then Nkind (Spec) /= N_Function_Specification
1200 then
1201 Error_Msg_N ("invalid body for generic function ", Body_Id);
1202 return;
1203 end if;
1205 Set_Corresponding_Body (Gen_Decl, Body_Id);
1207 if Has_Completion (Gen_Id)
1208 and then Nkind (Parent (N)) /= N_Subunit
1209 then
1210 Error_Msg_N ("duplicate generic body", N);
1211 return;
1212 else
1213 Set_Has_Completion (Gen_Id);
1214 end if;
1216 if Nkind (N) = N_Subprogram_Body_Stub then
1217 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1218 else
1219 Set_Corresponding_Spec (N, Gen_Id);
1220 end if;
1222 if Nkind (Parent (N)) = N_Compilation_Unit then
1223 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1224 end if;
1226 -- Make generic parameters immediately visible in the body. They are
1227 -- needed to process the formals declarations. Then make the formals
1228 -- visible in a separate step.
1230 Push_Scope (Gen_Id);
1232 declare
1233 E : Entity_Id;
1234 First_Ent : Entity_Id;
1236 begin
1237 First_Ent := First_Entity (Gen_Id);
1239 E := First_Ent;
1240 while Present (E) and then not Is_Formal (E) loop
1241 Install_Entity (E);
1242 Next_Entity (E);
1243 end loop;
1245 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1247 -- Now generic formals are visible, and the specification can be
1248 -- analyzed, for subsequent conformance check.
1250 Body_Id := Analyze_Subprogram_Specification (Spec);
1252 -- Make formal parameters visible
1254 if Present (E) then
1256 -- E is the first formal parameter, we loop through the formals
1257 -- installing them so that they will be visible.
1259 Set_First_Entity (Gen_Id, E);
1260 while Present (E) loop
1261 Install_Entity (E);
1262 Next_Formal (E);
1263 end loop;
1264 end if;
1266 -- Visible generic entity is callable within its own body
1268 Set_Ekind (Gen_Id, Ekind (Body_Id));
1269 Set_Ekind (Body_Id, E_Subprogram_Body);
1270 Set_Convention (Body_Id, Convention (Gen_Id));
1271 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1272 Set_Scope (Body_Id, Scope (Gen_Id));
1274 -- Inherit the "ghostness" of the generic spec. Note that this
1275 -- property is not directly inherited as the body may be subject
1276 -- to a different Ghost assertion policy.
1278 if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then
1279 Set_Is_Ghost_Entity (Body_Id);
1281 -- The Ghost policy in effect at the point of declaration and at
1282 -- the point of completion must match (SPARK RM 6.9(15)).
1284 Check_Ghost_Completion (Gen_Id, Body_Id);
1285 end if;
1287 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1289 if Nkind (N) = N_Subprogram_Body_Stub then
1291 -- No body to analyze, so restore state of generic unit
1293 Set_Ekind (Gen_Id, Kind);
1294 Set_Ekind (Body_Id, Kind);
1296 if Present (First_Ent) then
1297 Set_First_Entity (Gen_Id, First_Ent);
1298 end if;
1300 End_Scope;
1301 return;
1302 end if;
1304 -- If this is a compilation unit, it must be made visible explicitly,
1305 -- because the compilation of the declaration, unlike other library
1306 -- unit declarations, does not. If it is not a unit, the following
1307 -- is redundant but harmless.
1309 Set_Is_Immediately_Visible (Gen_Id);
1310 Reference_Body_Formals (Gen_Id, Body_Id);
1312 if Is_Child_Unit (Gen_Id) then
1313 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1314 end if;
1316 Set_Actual_Subtypes (N, Current_Scope);
1318 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1319 Set_SPARK_Pragma_Inherited (Body_Id, True);
1321 Analyze_Declarations (Declarations (N));
1322 Check_Completion;
1323 Analyze (Handled_Statement_Sequence (N));
1325 Save_Global_References (Original_Node (N));
1327 -- Prior to exiting the scope, include generic formals again (if any
1328 -- are present) in the set of local entities.
1330 if Present (First_Ent) then
1331 Set_First_Entity (Gen_Id, First_Ent);
1332 end if;
1334 Check_References (Gen_Id);
1335 end;
1337 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1338 End_Scope;
1339 Check_Subprogram_Order (N);
1341 -- Outside of its body, unit is generic again
1343 Set_Ekind (Gen_Id, Kind);
1344 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1346 if Style_Check then
1347 Style.Check_Identifier (Body_Id, Gen_Id);
1348 end if;
1350 End_Generic;
1351 end Analyze_Generic_Subprogram_Body;
1353 ----------------------------
1354 -- Analyze_Null_Procedure --
1355 ----------------------------
1357 procedure Analyze_Null_Procedure
1358 (N : Node_Id;
1359 Is_Completion : out Boolean)
1361 Loc : constant Source_Ptr := Sloc (N);
1362 Spec : constant Node_Id := Specification (N);
1363 Designator : Entity_Id;
1364 Form : Node_Id;
1365 Null_Body : Node_Id := Empty;
1366 Prev : Entity_Id;
1368 begin
1369 -- Capture the profile of the null procedure before analysis, for
1370 -- expansion at the freeze point and at each point of call. The body is
1371 -- used if the procedure has preconditions, or if it is a completion. In
1372 -- the first case the body is analyzed at the freeze point, in the other
1373 -- it replaces the null procedure declaration.
1375 Null_Body :=
1376 Make_Subprogram_Body (Loc,
1377 Specification => New_Copy_Tree (Spec),
1378 Declarations => New_List,
1379 Handled_Statement_Sequence =>
1380 Make_Handled_Sequence_Of_Statements (Loc,
1381 Statements => New_List (Make_Null_Statement (Loc))));
1383 -- Create new entities for body and formals
1385 Set_Defining_Unit_Name (Specification (Null_Body),
1386 Make_Defining_Identifier
1387 (Sloc (Defining_Entity (N)),
1388 Chars (Defining_Entity (N))));
1390 Form := First (Parameter_Specifications (Specification (Null_Body)));
1391 while Present (Form) loop
1392 Set_Defining_Identifier (Form,
1393 Make_Defining_Identifier
1394 (Sloc (Defining_Identifier (Form)),
1395 Chars (Defining_Identifier (Form))));
1396 Next (Form);
1397 end loop;
1399 -- Determine whether the null procedure may be a completion of a generic
1400 -- suprogram, in which case we use the new null body as the completion
1401 -- and set minimal semantic information on the original declaration,
1402 -- which is rewritten as a null statement.
1404 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1406 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1407 Insert_Before (N, Null_Body);
1408 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1410 Rewrite (N, Make_Null_Statement (Loc));
1411 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1412 Is_Completion := True;
1413 return;
1415 else
1416 -- Resolve the types of the formals now, because the freeze point
1417 -- may appear in a different context, e.g. an instantiation.
1419 Form := First (Parameter_Specifications (Specification (Null_Body)));
1420 while Present (Form) loop
1421 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1422 Find_Type (Parameter_Type (Form));
1424 elsif
1425 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1426 then
1427 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1429 else
1430 -- The case of a null procedure with a formal that is an
1431 -- access_to_subprogram type, and that is used as an actual
1432 -- in an instantiation is left to the enthusiastic reader.
1434 null;
1435 end if;
1437 Next (Form);
1438 end loop;
1439 end if;
1441 -- If there are previous overloadable entities with the same name,
1442 -- check whether any of them is completed by the null procedure.
1444 if Present (Prev) and then Is_Overloadable (Prev) then
1445 Designator := Analyze_Subprogram_Specification (Spec);
1446 Prev := Find_Corresponding_Spec (N);
1447 end if;
1449 if No (Prev) or else not Comes_From_Source (Prev) then
1450 Designator := Analyze_Subprogram_Specification (Spec);
1451 Set_Has_Completion (Designator);
1453 -- Signal to caller that this is a procedure declaration
1455 Is_Completion := False;
1457 -- Null procedures are always inlined, but generic formal subprograms
1458 -- which appear as such in the internal instance of formal packages,
1459 -- need no completion and are not marked Inline.
1461 if Expander_Active
1462 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1463 then
1464 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1465 Set_Body_To_Inline (N, Null_Body);
1466 Set_Is_Inlined (Designator);
1467 end if;
1469 else
1470 -- The null procedure is a completion. We unconditionally rewrite
1471 -- this as a null body (even if expansion is not active), because
1472 -- there are various error checks that are applied on this body
1473 -- when it is analyzed (e.g. correct aspect placement).
1475 if Has_Completion (Prev) then
1476 Error_Msg_Sloc := Sloc (Prev);
1477 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1478 end if;
1480 Is_Completion := True;
1481 Rewrite (N, Null_Body);
1482 Analyze (N);
1483 end if;
1484 end Analyze_Null_Procedure;
1486 -----------------------------
1487 -- Analyze_Operator_Symbol --
1488 -----------------------------
1490 -- An operator symbol such as "+" or "and" may appear in context where the
1491 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1492 -- is just a string, as in (conjunction = "or"). In these cases the parser
1493 -- generates this node, and the semantics does the disambiguation. Other
1494 -- such case are actuals in an instantiation, the generic unit in an
1495 -- instantiation, and pragma arguments.
1497 procedure Analyze_Operator_Symbol (N : Node_Id) is
1498 Par : constant Node_Id := Parent (N);
1500 begin
1501 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1502 or else Nkind (Par) = N_Function_Instantiation
1503 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1504 or else (Nkind (Par) = N_Pragma_Argument_Association
1505 and then not Is_Pragma_String_Literal (Par))
1506 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1507 or else (Nkind (Par) = N_Attribute_Reference
1508 and then Attribute_Name (Par) /= Name_Value)
1509 then
1510 Find_Direct_Name (N);
1512 else
1513 Change_Operator_Symbol_To_String_Literal (N);
1514 Analyze (N);
1515 end if;
1516 end Analyze_Operator_Symbol;
1518 -----------------------------------
1519 -- Analyze_Parameter_Association --
1520 -----------------------------------
1522 procedure Analyze_Parameter_Association (N : Node_Id) is
1523 begin
1524 Analyze (Explicit_Actual_Parameter (N));
1525 end Analyze_Parameter_Association;
1527 ----------------------------
1528 -- Analyze_Procedure_Call --
1529 ----------------------------
1531 procedure Analyze_Procedure_Call (N : Node_Id) is
1532 Loc : constant Source_Ptr := Sloc (N);
1533 P : constant Node_Id := Name (N);
1534 Actuals : constant List_Id := Parameter_Associations (N);
1535 Actual : Node_Id;
1536 New_N : Node_Id;
1538 procedure Analyze_Call_And_Resolve;
1539 -- Do Analyze and Resolve calls for procedure call
1540 -- At end, check illegal order dependence.
1542 ------------------------------
1543 -- Analyze_Call_And_Resolve --
1544 ------------------------------
1546 procedure Analyze_Call_And_Resolve is
1547 begin
1548 if Nkind (N) = N_Procedure_Call_Statement then
1549 Analyze_Call (N);
1550 Resolve (N, Standard_Void_Type);
1551 else
1552 Analyze (N);
1553 end if;
1554 end Analyze_Call_And_Resolve;
1556 -- Start of processing for Analyze_Procedure_Call
1558 begin
1559 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1560 -- a procedure call or an entry call. The prefix may denote an access
1561 -- to subprogram type, in which case an implicit dereference applies.
1562 -- If the prefix is an indexed component (without implicit dereference)
1563 -- then the construct denotes a call to a member of an entire family.
1564 -- If the prefix is a simple name, it may still denote a call to a
1565 -- parameterless member of an entry family. Resolution of these various
1566 -- interpretations is delicate.
1568 Analyze (P);
1570 -- If this is a call of the form Obj.Op, the call may have been
1571 -- analyzed and possibly rewritten into a block, in which case
1572 -- we are done.
1574 if Analyzed (N) then
1575 return;
1576 end if;
1578 -- If there is an error analyzing the name (which may have been
1579 -- rewritten if the original call was in prefix notation) then error
1580 -- has been emitted already, mark node and return.
1582 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1583 Set_Etype (N, Any_Type);
1584 return;
1585 end if;
1587 -- The name of the procedure call may reference an entity subject to
1588 -- pragma Ghost with policy Ignore. Set the mode now to ensure that any
1589 -- nodes generated during analysis and expansion are properly flagged as
1590 -- ignored Ghost.
1592 Set_Ghost_Mode (N);
1594 -- Otherwise analyze the parameters
1596 if Present (Actuals) then
1597 Actual := First (Actuals);
1599 while Present (Actual) loop
1600 Analyze (Actual);
1601 Check_Parameterless_Call (Actual);
1602 Next (Actual);
1603 end loop;
1604 end if;
1606 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1608 if Nkind (P) = N_Attribute_Reference
1609 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1610 Name_Elab_Body,
1611 Name_Elab_Subp_Body)
1612 then
1613 if Present (Actuals) then
1614 Error_Msg_N
1615 ("no parameters allowed for this call", First (Actuals));
1616 return;
1617 end if;
1619 Set_Etype (N, Standard_Void_Type);
1620 Set_Analyzed (N);
1622 elsif Is_Entity_Name (P)
1623 and then Is_Record_Type (Etype (Entity (P)))
1624 and then Remote_AST_I_Dereference (P)
1625 then
1626 return;
1628 elsif Is_Entity_Name (P)
1629 and then Ekind (Entity (P)) /= E_Entry_Family
1630 then
1631 if Is_Access_Type (Etype (P))
1632 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1633 and then No (Actuals)
1634 and then Comes_From_Source (N)
1635 then
1636 Error_Msg_N ("missing explicit dereference in call", N);
1637 end if;
1639 Analyze_Call_And_Resolve;
1641 -- If the prefix is the simple name of an entry family, this is
1642 -- a parameterless call from within the task body itself.
1644 elsif Is_Entity_Name (P)
1645 and then Nkind (P) = N_Identifier
1646 and then Ekind (Entity (P)) = E_Entry_Family
1647 and then Present (Actuals)
1648 and then No (Next (First (Actuals)))
1649 then
1650 -- Can be call to parameterless entry family. What appears to be the
1651 -- sole argument is in fact the entry index. Rewrite prefix of node
1652 -- accordingly. Source representation is unchanged by this
1653 -- transformation.
1655 New_N :=
1656 Make_Indexed_Component (Loc,
1657 Prefix =>
1658 Make_Selected_Component (Loc,
1659 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1660 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1661 Expressions => Actuals);
1662 Set_Name (N, New_N);
1663 Set_Etype (New_N, Standard_Void_Type);
1664 Set_Parameter_Associations (N, No_List);
1665 Analyze_Call_And_Resolve;
1667 elsif Nkind (P) = N_Explicit_Dereference then
1668 if Ekind (Etype (P)) = E_Subprogram_Type then
1669 Analyze_Call_And_Resolve;
1670 else
1671 Error_Msg_N ("expect access to procedure in call", P);
1672 end if;
1674 -- The name can be a selected component or an indexed component that
1675 -- yields an access to subprogram. Such a prefix is legal if the call
1676 -- has parameter associations.
1678 elsif Is_Access_Type (Etype (P))
1679 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1680 then
1681 if Present (Actuals) then
1682 Analyze_Call_And_Resolve;
1683 else
1684 Error_Msg_N ("missing explicit dereference in call ", N);
1685 end if;
1687 -- If not an access to subprogram, then the prefix must resolve to the
1688 -- name of an entry, entry family, or protected operation.
1690 -- For the case of a simple entry call, P is a selected component where
1691 -- the prefix is the task and the selector name is the entry. A call to
1692 -- a protected procedure will have the same syntax. If the protected
1693 -- object contains overloaded operations, the entity may appear as a
1694 -- function, the context will select the operation whose type is Void.
1696 elsif Nkind (P) = N_Selected_Component
1697 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1698 E_Procedure,
1699 E_Function)
1700 then
1701 Analyze_Call_And_Resolve;
1703 elsif Nkind (P) = N_Selected_Component
1704 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1705 and then Present (Actuals)
1706 and then No (Next (First (Actuals)))
1707 then
1708 -- Can be call to parameterless entry family. What appears to be the
1709 -- sole argument is in fact the entry index. Rewrite prefix of node
1710 -- accordingly. Source representation is unchanged by this
1711 -- transformation.
1713 New_N :=
1714 Make_Indexed_Component (Loc,
1715 Prefix => New_Copy (P),
1716 Expressions => Actuals);
1717 Set_Name (N, New_N);
1718 Set_Etype (New_N, Standard_Void_Type);
1719 Set_Parameter_Associations (N, No_List);
1720 Analyze_Call_And_Resolve;
1722 -- For the case of a reference to an element of an entry family, P is
1723 -- an indexed component whose prefix is a selected component (task and
1724 -- entry family), and whose index is the entry family index.
1726 elsif Nkind (P) = N_Indexed_Component
1727 and then Nkind (Prefix (P)) = N_Selected_Component
1728 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1729 then
1730 Analyze_Call_And_Resolve;
1732 -- If the prefix is the name of an entry family, it is a call from
1733 -- within the task body itself.
1735 elsif Nkind (P) = N_Indexed_Component
1736 and then Nkind (Prefix (P)) = N_Identifier
1737 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1738 then
1739 New_N :=
1740 Make_Selected_Component (Loc,
1741 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1742 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1743 Rewrite (Prefix (P), New_N);
1744 Analyze (P);
1745 Analyze_Call_And_Resolve;
1747 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1748 -- procedure name, so the construct can only be a qualified expression.
1750 elsif Nkind (P) = N_Qualified_Expression
1751 and then Ada_Version >= Ada_2012
1752 then
1753 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1754 Analyze (N);
1756 -- Anything else is an error
1758 else
1759 Error_Msg_N ("invalid procedure or entry call", N);
1760 end if;
1761 end Analyze_Procedure_Call;
1763 ------------------------------
1764 -- Analyze_Return_Statement --
1765 ------------------------------
1767 procedure Analyze_Return_Statement (N : Node_Id) is
1769 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1770 N_Extended_Return_Statement));
1772 Returns_Object : constant Boolean :=
1773 Nkind (N) = N_Extended_Return_Statement
1774 or else
1775 (Nkind (N) = N_Simple_Return_Statement
1776 and then Present (Expression (N)));
1777 -- True if we're returning something; that is, "return <expression>;"
1778 -- or "return Result : T [:= ...]". False for "return;". Used for error
1779 -- checking: If Returns_Object is True, N should apply to a function
1780 -- body; otherwise N should apply to a procedure body, entry body,
1781 -- accept statement, or extended return statement.
1783 function Find_What_It_Applies_To return Entity_Id;
1784 -- Find the entity representing the innermost enclosing body, accept
1785 -- statement, or extended return statement. If the result is a callable
1786 -- construct or extended return statement, then this will be the value
1787 -- of the Return_Applies_To attribute. Otherwise, the program is
1788 -- illegal. See RM-6.5(4/2).
1790 -----------------------------
1791 -- Find_What_It_Applies_To --
1792 -----------------------------
1794 function Find_What_It_Applies_To return Entity_Id is
1795 Result : Entity_Id := Empty;
1797 begin
1798 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1799 -- and postconditions.
1801 for J in reverse 0 .. Scope_Stack.Last loop
1802 Result := Scope_Stack.Table (J).Entity;
1803 exit when not Ekind_In (Result, E_Block, E_Loop)
1804 and then Chars (Result) /= Name_uPostconditions;
1805 end loop;
1807 pragma Assert (Present (Result));
1808 return Result;
1809 end Find_What_It_Applies_To;
1811 -- Local declarations
1813 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1814 Kind : constant Entity_Kind := Ekind (Scope_Id);
1815 Loc : constant Source_Ptr := Sloc (N);
1816 Stm_Entity : constant Entity_Id :=
1817 New_Internal_Entity
1818 (E_Return_Statement, Current_Scope, Loc, 'R');
1820 -- Start of processing for Analyze_Return_Statement
1822 begin
1823 Set_Return_Statement_Entity (N, Stm_Entity);
1825 Set_Etype (Stm_Entity, Standard_Void_Type);
1826 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1828 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1829 -- (4/2): an inner return statement will apply to this extended return.
1831 if Nkind (N) = N_Extended_Return_Statement then
1832 Push_Scope (Stm_Entity);
1833 end if;
1835 -- Check that pragma No_Return is obeyed. Don't complain about the
1836 -- implicitly-generated return that is placed at the end.
1838 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1839 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1840 end if;
1842 -- Warn on any unassigned OUT parameters if in procedure
1844 if Ekind (Scope_Id) = E_Procedure then
1845 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1846 end if;
1848 -- Check that functions return objects, and other things do not
1850 if Kind = E_Function or else Kind = E_Generic_Function then
1851 if not Returns_Object then
1852 Error_Msg_N ("missing expression in return from function", N);
1853 end if;
1855 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1856 if Returns_Object then
1857 Error_Msg_N ("procedure cannot return value (use function)", N);
1858 end if;
1860 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1861 if Returns_Object then
1862 if Is_Protected_Type (Scope (Scope_Id)) then
1863 Error_Msg_N ("entry body cannot return value", N);
1864 else
1865 Error_Msg_N ("accept statement cannot return value", N);
1866 end if;
1867 end if;
1869 elsif Kind = E_Return_Statement then
1871 -- We are nested within another return statement, which must be an
1872 -- extended_return_statement.
1874 if Returns_Object then
1875 if Nkind (N) = N_Extended_Return_Statement then
1876 Error_Msg_N
1877 ("extended return statement cannot be nested (use `RETURN;`)",
1880 -- Case of a simple return statement with a value inside extended
1881 -- return statement.
1883 else
1884 Error_Msg_N
1885 ("return nested in extended return statement cannot return "
1886 & "value (use `RETURN;`)", N);
1887 end if;
1888 end if;
1890 else
1891 Error_Msg_N ("illegal context for return statement", N);
1892 end if;
1894 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1895 Analyze_Function_Return (N);
1897 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1898 Set_Return_Present (Scope_Id);
1899 end if;
1901 if Nkind (N) = N_Extended_Return_Statement then
1902 End_Scope;
1903 end if;
1905 Kill_Current_Values (Last_Assignment_Only => True);
1906 Check_Unreachable_Code (N);
1908 Analyze_Dimension (N);
1909 end Analyze_Return_Statement;
1911 -------------------------------------
1912 -- Analyze_Simple_Return_Statement --
1913 -------------------------------------
1915 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1916 begin
1917 if Present (Expression (N)) then
1918 Mark_Coextensions (N, Expression (N));
1919 end if;
1921 Analyze_Return_Statement (N);
1922 end Analyze_Simple_Return_Statement;
1924 -------------------------
1925 -- Analyze_Return_Type --
1926 -------------------------
1928 procedure Analyze_Return_Type (N : Node_Id) is
1929 Designator : constant Entity_Id := Defining_Entity (N);
1930 Typ : Entity_Id := Empty;
1932 begin
1933 -- Normal case where result definition does not indicate an error
1935 if Result_Definition (N) /= Error then
1936 if Nkind (Result_Definition (N)) = N_Access_Definition then
1937 Check_SPARK_05_Restriction
1938 ("access result is not allowed", Result_Definition (N));
1940 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1942 declare
1943 AD : constant Node_Id :=
1944 Access_To_Subprogram_Definition (Result_Definition (N));
1945 begin
1946 if Present (AD) and then Protected_Present (AD) then
1947 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1948 else
1949 Typ := Access_Definition (N, Result_Definition (N));
1950 end if;
1951 end;
1953 Set_Parent (Typ, Result_Definition (N));
1954 Set_Is_Local_Anonymous_Access (Typ);
1955 Set_Etype (Designator, Typ);
1957 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1959 Null_Exclusion_Static_Checks (N);
1961 -- Subtype_Mark case
1963 else
1964 Find_Type (Result_Definition (N));
1965 Typ := Entity (Result_Definition (N));
1966 Set_Etype (Designator, Typ);
1968 -- Unconstrained array as result is not allowed in SPARK
1970 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1971 Check_SPARK_05_Restriction
1972 ("returning an unconstrained array is not allowed",
1973 Result_Definition (N));
1974 end if;
1976 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1978 Null_Exclusion_Static_Checks (N);
1980 -- If a null exclusion is imposed on the result type, then create
1981 -- a null-excluding itype (an access subtype) and use it as the
1982 -- function's Etype. Note that the null exclusion checks are done
1983 -- right before this, because they don't get applied to types that
1984 -- do not come from source.
1986 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1987 Set_Etype (Designator,
1988 Create_Null_Excluding_Itype
1989 (T => Typ,
1990 Related_Nod => N,
1991 Scope_Id => Scope (Current_Scope)));
1993 -- The new subtype must be elaborated before use because
1994 -- it is visible outside of the function. However its base
1995 -- type may not be frozen yet, so the reference that will
1996 -- force elaboration must be attached to the freezing of
1997 -- the base type.
1999 -- If the return specification appears on a proper body,
2000 -- the subtype will have been created already on the spec.
2002 if Is_Frozen (Typ) then
2003 if Nkind (Parent (N)) = N_Subprogram_Body
2004 and then Nkind (Parent (Parent (N))) = N_Subunit
2005 then
2006 null;
2007 else
2008 Build_Itype_Reference (Etype (Designator), Parent (N));
2009 end if;
2011 else
2012 Ensure_Freeze_Node (Typ);
2014 declare
2015 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2016 begin
2017 Set_Itype (IR, Etype (Designator));
2018 Append_Freeze_Actions (Typ, New_List (IR));
2019 end;
2020 end if;
2022 else
2023 Set_Etype (Designator, Typ);
2024 end if;
2026 if Ekind (Typ) = E_Incomplete_Type
2027 and then Is_Value_Type (Typ)
2028 then
2029 null;
2031 elsif Ekind (Typ) = E_Incomplete_Type
2032 or else (Is_Class_Wide_Type (Typ)
2033 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2034 then
2035 -- AI05-0151: Tagged incomplete types are allowed in all formal
2036 -- parts. Untagged incomplete types are not allowed in bodies.
2037 -- As a consequence, limited views cannot appear in a basic
2038 -- declaration that is itself within a body, because there is
2039 -- no point at which the non-limited view will become visible.
2041 if Ada_Version >= Ada_2012 then
2042 if From_Limited_With (Typ) and then In_Package_Body then
2043 Error_Msg_NE
2044 ("invalid use of incomplete type&",
2045 Result_Definition (N), Typ);
2047 -- The return type of a subprogram body cannot be of a
2048 -- formal incomplete type.
2050 elsif Is_Generic_Type (Typ)
2051 and then Nkind (Parent (N)) = N_Subprogram_Body
2052 then
2053 Error_Msg_N
2054 ("return type cannot be a formal incomplete type",
2055 Result_Definition (N));
2057 elsif Is_Class_Wide_Type (Typ)
2058 and then Is_Generic_Type (Root_Type (Typ))
2059 and then Nkind (Parent (N)) = N_Subprogram_Body
2060 then
2061 Error_Msg_N
2062 ("return type cannot be a formal incomplete type",
2063 Result_Definition (N));
2065 elsif Is_Tagged_Type (Typ) then
2066 null;
2068 -- Use is legal in a thunk generated for an operation
2069 -- inherited from a progenitor.
2071 elsif Is_Thunk (Designator)
2072 and then Present (Non_Limited_View (Typ))
2073 then
2074 null;
2076 elsif Nkind (Parent (N)) = N_Subprogram_Body
2077 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2078 N_Entry_Body)
2079 then
2080 Error_Msg_NE
2081 ("invalid use of untagged incomplete type&",
2082 Designator, Typ);
2083 end if;
2085 -- The type must be completed in the current package. This
2086 -- is checked at the end of the package declaration when
2087 -- Taft-amendment types are identified. If the return type
2088 -- is class-wide, there is no required check, the type can
2089 -- be a bona fide TAT.
2091 if Ekind (Scope (Current_Scope)) = E_Package
2092 and then In_Private_Part (Scope (Current_Scope))
2093 and then not Is_Class_Wide_Type (Typ)
2094 then
2095 Append_Elmt (Designator, Private_Dependents (Typ));
2096 end if;
2098 else
2099 Error_Msg_NE
2100 ("invalid use of incomplete type&", Designator, Typ);
2101 end if;
2102 end if;
2103 end if;
2105 -- Case where result definition does indicate an error
2107 else
2108 Set_Etype (Designator, Any_Type);
2109 end if;
2110 end Analyze_Return_Type;
2112 -----------------------------
2113 -- Analyze_Subprogram_Body --
2114 -----------------------------
2116 procedure Analyze_Subprogram_Body (N : Node_Id) is
2117 Loc : constant Source_Ptr := Sloc (N);
2118 Body_Spec : constant Node_Id := Specification (N);
2119 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2121 begin
2122 if Debug_Flag_C then
2123 Write_Str ("==> subprogram body ");
2124 Write_Name (Chars (Body_Id));
2125 Write_Str (" from ");
2126 Write_Location (Loc);
2127 Write_Eol;
2128 Indent;
2129 end if;
2131 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2133 -- The real work is split out into the helper, so it can do "return;"
2134 -- without skipping the debug output:
2136 Analyze_Subprogram_Body_Helper (N);
2138 if Debug_Flag_C then
2139 Outdent;
2140 Write_Str ("<== subprogram body ");
2141 Write_Name (Chars (Body_Id));
2142 Write_Str (" from ");
2143 Write_Location (Loc);
2144 Write_Eol;
2145 end if;
2146 end Analyze_Subprogram_Body;
2148 --------------------------------------
2149 -- Analyze_Subprogram_Body_Contract --
2150 --------------------------------------
2152 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2153 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
2155 procedure Analyze_Completion_Contract (Spec_Id : Entity_Id);
2156 -- Analyze all delayed pragmas chained on the contract of subprogram
2157 -- body Body_Id as if they appeared at the end of a declarative region.
2158 -- Spec_Id denotes the corresponding spec. The aspects in question are:
2159 -- Refined_Depends
2160 -- Refined_Global
2161 -- Note that pragma Refined_Post is analyzed immediately
2163 ---------------------------------
2164 -- Analyze_Completion_Contract --
2165 ---------------------------------
2167 procedure Analyze_Completion_Contract (Spec_Id : Entity_Id) is
2168 Items : constant Node_Id := Contract (Body_Id);
2169 Prag : Node_Id;
2170 Prag_Nam : Name_Id;
2171 Ref_Depends : Node_Id := Empty;
2172 Ref_Global : Node_Id := Empty;
2174 begin
2175 -- All subprograms carry a contract, but for some it is not
2176 -- significant and should not be processed.
2178 if not Has_Significant_Contract (Spec_Id) then
2179 return;
2181 elsif Present (Items) then
2183 -- Locate and store pragmas Refined_Depends and Refined_Global
2184 -- since their order of analysis matters.
2186 Prag := Classifications (Items);
2187 while Present (Prag) loop
2188 Prag_Nam := Pragma_Name (Prag);
2190 if Prag_Nam = Name_Refined_Depends then
2191 Ref_Depends := Prag;
2193 elsif Prag_Nam = Name_Refined_Global then
2194 Ref_Global := Prag;
2195 end if;
2197 Prag := Next_Pragma (Prag);
2198 end loop;
2199 end if;
2201 -- Analyze Refined_Global first as Refined_Depends may mention items
2202 -- classified in the global refinement.
2204 if Present (Ref_Global) then
2205 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2206 end if;
2208 -- Refined_Depends must be analyzed after Refined_Global in order to
2209 -- see the modes of all global refinements.
2211 if Present (Ref_Depends) then
2212 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2213 end if;
2214 end Analyze_Completion_Contract;
2216 -- Local variables
2218 Mode : SPARK_Mode_Type;
2219 Spec_Id : Entity_Id;
2221 -- Start of processing for Analyze_Subprogram_Body_Contract
2223 begin
2224 -- When a subprogram body declaration is illegal, its defining entity is
2225 -- left unanalyzed. There is nothing left to do in this case because the
2226 -- body lacks a contract, or even a proper Ekind.
2228 if Ekind (Body_Id) = E_Void then
2229 return;
2230 end if;
2232 -- Due to the timing of contract analysis, delayed pragmas may be
2233 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2234 -- context. To remedy this, restore the original SPARK_Mode of the
2235 -- related subprogram body.
2237 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2239 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2240 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2241 else
2242 Spec_Id := Corresponding_Spec (Body_Decl);
2243 end if;
2245 -- The subprogram body is a completion, analyze all delayed pragmas that
2246 -- apply. Note that when the body is stand alone, the pragmas are always
2247 -- analyzed on the spot.
2249 if Present (Spec_Id) then
2250 Analyze_Completion_Contract (Spec_Id);
2251 end if;
2253 -- Ensure that the contract cases or postconditions mention 'Result or
2254 -- define a post-state.
2256 Check_Result_And_Post_State (Body_Id);
2258 -- Restore the SPARK_Mode of the enclosing context after all delayed
2259 -- pragmas have been analyzed.
2261 Restore_SPARK_Mode (Mode);
2262 end Analyze_Subprogram_Body_Contract;
2264 ------------------------------------
2265 -- Analyze_Subprogram_Body_Helper --
2266 ------------------------------------
2268 -- This procedure is called for regular subprogram bodies, generic bodies,
2269 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2270 -- specification matters, and is used to create a proper declaration for
2271 -- the subprogram, or to perform conformance checks.
2273 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2274 Loc : constant Source_Ptr := Sloc (N);
2275 Body_Spec : constant Node_Id := Specification (N);
2276 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2277 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2278 Conformant : Boolean;
2279 HSS : Node_Id;
2280 Prot_Typ : Entity_Id := Empty;
2281 Spec_Id : Entity_Id;
2282 Spec_Decl : Node_Id := Empty;
2284 Last_Real_Spec_Entity : Entity_Id := Empty;
2285 -- When we analyze a separate spec, the entity chain ends up containing
2286 -- the formals, as well as any itypes generated during analysis of the
2287 -- default expressions for parameters, or the arguments of associated
2288 -- precondition/postcondition pragmas (which are analyzed in the context
2289 -- of the spec since they have visibility on formals).
2291 -- These entities belong with the spec and not the body. However we do
2292 -- the analysis of the body in the context of the spec (again to obtain
2293 -- visibility to the formals), and all the entities generated during
2294 -- this analysis end up also chained to the entity chain of the spec.
2295 -- But they really belong to the body, and there is circuitry to move
2296 -- them from the spec to the body.
2298 -- However, when we do this move, we don't want to move the real spec
2299 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2300 -- variable points to the last real spec entity, so we only move those
2301 -- chained beyond that point. It is initialized to Empty to deal with
2302 -- the case where there is no separate spec.
2304 procedure Analyze_Aspects_On_Body_Or_Stub;
2305 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2306 -- assumed that N has aspects.
2308 function Body_Has_Contract return Boolean;
2309 -- Check whether unanalyzed body has an aspect or pragma that may
2310 -- generate a SPARK contract.
2312 procedure Check_Anonymous_Return;
2313 -- Ada 2005: if a function returns an access type that denotes a task,
2314 -- or a type that contains tasks, we must create a master entity for
2315 -- the anonymous type, which typically will be used in an allocator
2316 -- in the body of the function.
2318 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2319 -- Look ahead to recognize a pragma that may appear after the body.
2320 -- If there is a previous spec, check that it appears in the same
2321 -- declarative part. If the pragma is Inline_Always, perform inlining
2322 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2323 -- If the body acts as a spec, and inlining is required, we create a
2324 -- subprogram declaration for it, in order to attach the body to inline.
2325 -- If pragma does not appear after the body, check whether there is
2326 -- an inline pragma before any local declarations.
2328 procedure Check_Missing_Return;
2329 -- Checks for a function with a no return statements, and also performs
2330 -- the warning checks implemented by Check_Returns. In formal mode, also
2331 -- verify that a function ends with a RETURN and that a procedure does
2332 -- not contain any RETURN.
2334 function Disambiguate_Spec return Entity_Id;
2335 -- When a primitive is declared between the private view and the full
2336 -- view of a concurrent type which implements an interface, a special
2337 -- mechanism is used to find the corresponding spec of the primitive
2338 -- body.
2340 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2341 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2342 -- incomplete types coming from a limited context and swap their limited
2343 -- views with the non-limited ones.
2345 function Is_Private_Concurrent_Primitive
2346 (Subp_Id : Entity_Id) return Boolean;
2347 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2348 -- type that implements an interface and has a private view.
2350 procedure Set_Trivial_Subprogram (N : Node_Id);
2351 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2352 -- subprogram whose body is being analyzed. N is the statement node
2353 -- causing the flag to be set, if the following statement is a return
2354 -- of an entity, we mark the entity as set in source to suppress any
2355 -- warning on the stylized use of function stubs with a dummy return.
2357 procedure Verify_Overriding_Indicator;
2358 -- If there was a previous spec, the entity has been entered in the
2359 -- current scope previously. If the body itself carries an overriding
2360 -- indicator, check that it is consistent with the known status of the
2361 -- entity.
2363 -------------------------------------
2364 -- Analyze_Aspects_On_Body_Or_Stub --
2365 -------------------------------------
2367 procedure Analyze_Aspects_On_Body_Or_Stub is
2368 procedure Diagnose_Misplaced_Aspects;
2369 -- Subprogram body [stub] N has aspects, but they are not properly
2370 -- placed. Provide precise diagnostics depending on the aspects
2371 -- involved.
2373 --------------------------------
2374 -- Diagnose_Misplaced_Aspects --
2375 --------------------------------
2377 procedure Diagnose_Misplaced_Aspects is
2378 Asp : Node_Id;
2379 Asp_Nam : Name_Id;
2380 Asp_Id : Aspect_Id;
2381 -- The current aspect along with its name and id
2383 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2384 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2385 -- the name of the refined version of the aspect.
2387 ------------------------
2388 -- SPARK_Aspect_Error --
2389 ------------------------
2391 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2392 begin
2393 -- The corresponding spec already contains the aspect in
2394 -- question and the one appearing on the body must be the
2395 -- refined form:
2397 -- procedure P with Global ...;
2398 -- procedure P with Global ... is ... end P;
2399 -- ^
2400 -- Refined_Global
2402 if Has_Aspect (Spec_Id, Asp_Id) then
2403 Error_Msg_Name_1 := Asp_Nam;
2405 -- Subunits cannot carry aspects that apply to a subprogram
2406 -- declaration.
2408 if Nkind (Parent (N)) = N_Subunit then
2409 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2411 else
2412 Error_Msg_Name_2 := Ref_Nam;
2413 Error_Msg_N ("aspect % should be %", Asp);
2414 end if;
2416 -- Otherwise the aspect must appear in the spec, not in the
2417 -- body:
2419 -- procedure P;
2420 -- procedure P with Global ... is ... end P;
2422 else
2423 Error_Msg_N
2424 ("aspect specification must appear in subprogram "
2425 & "declaration", Asp);
2426 end if;
2427 end SPARK_Aspect_Error;
2429 -- Start of processing for Diagnose_Misplaced_Aspects
2431 begin
2432 -- Iterate over the aspect specifications and emit specific errors
2433 -- where applicable.
2435 Asp := First (Aspect_Specifications (N));
2436 while Present (Asp) loop
2437 Asp_Nam := Chars (Identifier (Asp));
2438 Asp_Id := Get_Aspect_Id (Asp_Nam);
2440 -- Do not emit errors on aspects that can appear on a
2441 -- subprogram body. This scenario occurs when the aspect
2442 -- specification list contains both misplaced and properly
2443 -- placed aspects.
2445 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2446 null;
2448 -- Special diagnostics for SPARK aspects
2450 elsif Asp_Nam = Name_Depends then
2451 SPARK_Aspect_Error (Name_Refined_Depends);
2453 elsif Asp_Nam = Name_Global then
2454 SPARK_Aspect_Error (Name_Refined_Global);
2456 elsif Asp_Nam = Name_Post then
2457 SPARK_Aspect_Error (Name_Refined_Post);
2459 else
2460 Error_Msg_N
2461 ("aspect specification must appear in subprogram "
2462 & "declaration", Asp);
2463 end if;
2465 Next (Asp);
2466 end loop;
2467 end Diagnose_Misplaced_Aspects;
2469 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2471 begin
2472 -- Language-defined aspects cannot be associated with a subprogram
2473 -- body [stub] if the subprogram has a spec. Certain implementation
2474 -- defined aspects are allowed to break this rule (for list, see
2475 -- table Aspect_On_Body_Or_Stub_OK).
2477 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2478 Diagnose_Misplaced_Aspects;
2479 else
2480 Analyze_Aspect_Specifications (N, Body_Id);
2481 end if;
2482 end Analyze_Aspects_On_Body_Or_Stub;
2484 -----------------------
2485 -- Body_Has_Contract --
2486 -----------------------
2488 function Body_Has_Contract return Boolean is
2489 Decls : constant List_Id := Declarations (N);
2490 A_Spec : Node_Id;
2491 A : Aspect_Id;
2492 Decl : Node_Id;
2493 P_Id : Pragma_Id;
2495 begin
2496 -- Check for unanalyzed aspects in the body that will
2497 -- generate a contract.
2499 if Present (Aspect_Specifications (N)) then
2500 A_Spec := First (Aspect_Specifications (N));
2501 while Present (A_Spec) loop
2502 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2504 if A = Aspect_Contract_Cases or else
2505 A = Aspect_Depends or else
2506 A = Aspect_Global or else
2507 A = Aspect_Pre or else
2508 A = Aspect_Precondition or else
2509 A = Aspect_Post or else
2510 A = Aspect_Postcondition
2511 then
2512 return True;
2513 end if;
2515 Next (A_Spec);
2516 end loop;
2517 end if;
2519 -- Check for pragmas that may generate a contract
2521 if Present (Decls) then
2522 Decl := First (Decls);
2523 while Present (Decl) loop
2524 if Nkind (Decl) = N_Pragma then
2525 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2527 if P_Id = Pragma_Contract_Cases or else
2528 P_Id = Pragma_Depends or else
2529 P_Id = Pragma_Global or else
2530 P_Id = Pragma_Pre or else
2531 P_Id = Pragma_Precondition or else
2532 P_Id = Pragma_Post or else
2533 P_Id = Pragma_Postcondition
2534 then
2535 return True;
2536 end if;
2537 end if;
2539 Next (Decl);
2540 end loop;
2541 end if;
2543 return False;
2544 end Body_Has_Contract;
2546 ----------------------------
2547 -- Check_Anonymous_Return --
2548 ----------------------------
2550 procedure Check_Anonymous_Return is
2551 Decl : Node_Id;
2552 Par : Node_Id;
2553 Scop : Entity_Id;
2555 begin
2556 if Present (Spec_Id) then
2557 Scop := Spec_Id;
2558 else
2559 Scop := Body_Id;
2560 end if;
2562 if Ekind (Scop) = E_Function
2563 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2564 and then not Is_Thunk (Scop)
2566 -- Skip internally built functions which handle the case of
2567 -- a null access (see Expand_Interface_Conversion)
2569 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2570 and then not Comes_From_Source (Parent (Scop)))
2572 and then (Has_Task (Designated_Type (Etype (Scop)))
2573 or else
2574 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2575 and then
2576 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2577 and then Expander_Active
2579 -- Avoid cases with no tasking support
2581 and then RTE_Available (RE_Current_Master)
2582 and then not Restriction_Active (No_Task_Hierarchy)
2583 then
2584 Decl :=
2585 Make_Object_Declaration (Loc,
2586 Defining_Identifier =>
2587 Make_Defining_Identifier (Loc, Name_uMaster),
2588 Constant_Present => True,
2589 Object_Definition =>
2590 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2591 Expression =>
2592 Make_Explicit_Dereference (Loc,
2593 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2595 if Present (Declarations (N)) then
2596 Prepend (Decl, Declarations (N));
2597 else
2598 Set_Declarations (N, New_List (Decl));
2599 end if;
2601 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2602 Set_Has_Master_Entity (Scop);
2604 -- Now mark the containing scope as a task master
2606 Par := N;
2607 while Nkind (Par) /= N_Compilation_Unit loop
2608 Par := Parent (Par);
2609 pragma Assert (Present (Par));
2611 -- If we fall off the top, we are at the outer level, and
2612 -- the environment task is our effective master, so nothing
2613 -- to mark.
2615 if Nkind_In
2616 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2617 then
2618 Set_Is_Task_Master (Par, True);
2619 exit;
2620 end if;
2621 end loop;
2622 end if;
2623 end Check_Anonymous_Return;
2625 -------------------------
2626 -- Check_Inline_Pragma --
2627 -------------------------
2629 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2630 Prag : Node_Id;
2631 Plist : List_Id;
2633 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2634 -- True when N is a pragma Inline or Inline_Always that applies
2635 -- to this subprogram.
2637 -----------------------
2638 -- Is_Inline_Pragma --
2639 -----------------------
2641 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2642 begin
2643 return
2644 Nkind (N) = N_Pragma
2645 and then
2646 (Pragma_Name (N) = Name_Inline_Always
2647 or else (Front_End_Inlining
2648 and then Pragma_Name (N) = Name_Inline))
2649 and then
2650 Chars
2651 (Expression (First (Pragma_Argument_Associations (N)))) =
2652 Chars (Body_Id);
2653 end Is_Inline_Pragma;
2655 -- Start of processing for Check_Inline_Pragma
2657 begin
2658 if not Expander_Active then
2659 return;
2660 end if;
2662 if Is_List_Member (N)
2663 and then Present (Next (N))
2664 and then Is_Inline_Pragma (Next (N))
2665 then
2666 Prag := Next (N);
2668 elsif Nkind (N) /= N_Subprogram_Body_Stub
2669 and then Present (Declarations (N))
2670 and then Is_Inline_Pragma (First (Declarations (N)))
2671 then
2672 Prag := First (Declarations (N));
2674 else
2675 Prag := Empty;
2676 end if;
2678 if Present (Prag) then
2679 if Present (Spec_Id) then
2680 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2681 Analyze (Prag);
2682 end if;
2684 else
2685 -- Create a subprogram declaration, to make treatment uniform
2687 declare
2688 Subp : constant Entity_Id :=
2689 Make_Defining_Identifier (Loc, Chars (Body_Id));
2690 Decl : constant Node_Id :=
2691 Make_Subprogram_Declaration (Loc,
2692 Specification =>
2693 New_Copy_Tree (Specification (N)));
2695 begin
2696 Set_Defining_Unit_Name (Specification (Decl), Subp);
2698 if Present (First_Formal (Body_Id)) then
2699 Plist := Copy_Parameter_List (Body_Id);
2700 Set_Parameter_Specifications
2701 (Specification (Decl), Plist);
2702 end if;
2704 Insert_Before (N, Decl);
2705 Analyze (Decl);
2706 Analyze (Prag);
2707 Set_Has_Pragma_Inline (Subp);
2709 if Pragma_Name (Prag) = Name_Inline_Always then
2710 Set_Is_Inlined (Subp);
2711 Set_Has_Pragma_Inline_Always (Subp);
2712 end if;
2714 -- Prior to copying the subprogram body to create a template
2715 -- for it for subsequent inlining, remove the pragma from
2716 -- the current body so that the copy that will produce the
2717 -- new body will start from a completely unanalyzed tree.
2719 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2720 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2721 end if;
2723 Spec := Subp;
2724 end;
2725 end if;
2726 end if;
2727 end Check_Inline_Pragma;
2729 --------------------------
2730 -- Check_Missing_Return --
2731 --------------------------
2733 procedure Check_Missing_Return is
2734 Id : Entity_Id;
2735 Missing_Ret : Boolean;
2737 begin
2738 if Nkind (Body_Spec) = N_Function_Specification then
2739 if Present (Spec_Id) then
2740 Id := Spec_Id;
2741 else
2742 Id := Body_Id;
2743 end if;
2745 if Return_Present (Id) then
2746 Check_Returns (HSS, 'F', Missing_Ret);
2748 if Missing_Ret then
2749 Set_Has_Missing_Return (Id);
2750 end if;
2752 elsif Is_Generic_Subprogram (Id)
2753 or else not Is_Machine_Code_Subprogram (Id)
2754 then
2755 Error_Msg_N ("missing RETURN statement in function body", N);
2756 end if;
2758 -- If procedure with No_Return, check returns
2760 elsif Nkind (Body_Spec) = N_Procedure_Specification
2761 and then Present (Spec_Id)
2762 and then No_Return (Spec_Id)
2763 then
2764 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2765 end if;
2767 -- Special checks in SPARK mode
2769 if Nkind (Body_Spec) = N_Function_Specification then
2771 -- In SPARK mode, last statement of a function should be a return
2773 declare
2774 Stat : constant Node_Id := Last_Source_Statement (HSS);
2775 begin
2776 if Present (Stat)
2777 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2778 N_Extended_Return_Statement)
2779 then
2780 Check_SPARK_05_Restriction
2781 ("last statement in function should be RETURN", Stat);
2782 end if;
2783 end;
2785 -- In SPARK mode, verify that a procedure has no return
2787 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2788 if Present (Spec_Id) then
2789 Id := Spec_Id;
2790 else
2791 Id := Body_Id;
2792 end if;
2794 -- Would be nice to point to return statement here, can we
2795 -- borrow the Check_Returns procedure here ???
2797 if Return_Present (Id) then
2798 Check_SPARK_05_Restriction
2799 ("procedure should not have RETURN", N);
2800 end if;
2801 end if;
2802 end Check_Missing_Return;
2804 -----------------------
2805 -- Disambiguate_Spec --
2806 -----------------------
2808 function Disambiguate_Spec return Entity_Id is
2809 Priv_Spec : Entity_Id;
2810 Spec_N : Entity_Id;
2812 procedure Replace_Types (To_Corresponding : Boolean);
2813 -- Depending on the flag, replace the type of formal parameters of
2814 -- Body_Id if it is a concurrent type implementing interfaces with
2815 -- the corresponding record type or the other way around.
2817 procedure Replace_Types (To_Corresponding : Boolean) is
2818 Formal : Entity_Id;
2819 Formal_Typ : Entity_Id;
2821 begin
2822 Formal := First_Formal (Body_Id);
2823 while Present (Formal) loop
2824 Formal_Typ := Etype (Formal);
2826 if Is_Class_Wide_Type (Formal_Typ) then
2827 Formal_Typ := Root_Type (Formal_Typ);
2828 end if;
2830 -- From concurrent type to corresponding record
2832 if To_Corresponding then
2833 if Is_Concurrent_Type (Formal_Typ)
2834 and then Present (Corresponding_Record_Type (Formal_Typ))
2835 and then
2836 Present (Interfaces
2837 (Corresponding_Record_Type (Formal_Typ)))
2838 then
2839 Set_Etype (Formal,
2840 Corresponding_Record_Type (Formal_Typ));
2841 end if;
2843 -- From corresponding record to concurrent type
2845 else
2846 if Is_Concurrent_Record_Type (Formal_Typ)
2847 and then Present (Interfaces (Formal_Typ))
2848 then
2849 Set_Etype (Formal,
2850 Corresponding_Concurrent_Type (Formal_Typ));
2851 end if;
2852 end if;
2854 Next_Formal (Formal);
2855 end loop;
2856 end Replace_Types;
2858 -- Start of processing for Disambiguate_Spec
2860 begin
2861 -- Try to retrieve the specification of the body as is. All error
2862 -- messages are suppressed because the body may not have a spec in
2863 -- its current state.
2865 Spec_N := Find_Corresponding_Spec (N, False);
2867 -- It is possible that this is the body of a primitive declared
2868 -- between a private and a full view of a concurrent type. The
2869 -- controlling parameter of the spec carries the concurrent type,
2870 -- not the corresponding record type as transformed by Analyze_
2871 -- Subprogram_Specification. In such cases, we undo the change
2872 -- made by the analysis of the specification and try to find the
2873 -- spec again.
2875 -- Note that wrappers already have their corresponding specs and
2876 -- bodies set during their creation, so if the candidate spec is
2877 -- a wrapper, then we definitely need to swap all types to their
2878 -- original concurrent status.
2880 if No (Spec_N)
2881 or else Is_Primitive_Wrapper (Spec_N)
2882 then
2883 -- Restore all references of corresponding record types to the
2884 -- original concurrent types.
2886 Replace_Types (To_Corresponding => False);
2887 Priv_Spec := Find_Corresponding_Spec (N, False);
2889 -- The current body truly belongs to a primitive declared between
2890 -- a private and a full view. We leave the modified body as is,
2891 -- and return the true spec.
2893 if Present (Priv_Spec)
2894 and then Is_Private_Primitive (Priv_Spec)
2895 then
2896 return Priv_Spec;
2897 end if;
2899 -- In case that this is some sort of error, restore the original
2900 -- state of the body.
2902 Replace_Types (To_Corresponding => True);
2903 end if;
2905 return Spec_N;
2906 end Disambiguate_Spec;
2908 ----------------------------
2909 -- Exchange_Limited_Views --
2910 ----------------------------
2912 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2913 procedure Detect_And_Exchange (Id : Entity_Id);
2914 -- Determine whether Id's type denotes an incomplete type associated
2915 -- with a limited with clause and exchange the limited view with the
2916 -- non-limited one.
2918 -------------------------
2919 -- Detect_And_Exchange --
2920 -------------------------
2922 procedure Detect_And_Exchange (Id : Entity_Id) is
2923 Typ : constant Entity_Id := Etype (Id);
2925 begin
2926 if Ekind (Typ) = E_Incomplete_Type
2927 and then From_Limited_With (Typ)
2928 and then Present (Non_Limited_View (Typ))
2929 then
2930 Set_Etype (Id, Non_Limited_View (Typ));
2931 end if;
2932 end Detect_And_Exchange;
2934 -- Local variables
2936 Formal : Entity_Id;
2938 -- Start of processing for Exchange_Limited_Views
2940 begin
2941 if No (Subp_Id) then
2942 return;
2944 -- Do not process subprogram bodies as they already use the non-
2945 -- limited view of types.
2947 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2948 return;
2949 end if;
2951 -- Examine all formals and swap views when applicable
2953 Formal := First_Formal (Subp_Id);
2954 while Present (Formal) loop
2955 Detect_And_Exchange (Formal);
2957 Next_Formal (Formal);
2958 end loop;
2960 -- Process the return type of a function
2962 if Ekind (Subp_Id) = E_Function then
2963 Detect_And_Exchange (Subp_Id);
2964 end if;
2965 end Exchange_Limited_Views;
2967 -------------------------------------
2968 -- Is_Private_Concurrent_Primitive --
2969 -------------------------------------
2971 function Is_Private_Concurrent_Primitive
2972 (Subp_Id : Entity_Id) return Boolean
2974 Formal_Typ : Entity_Id;
2976 begin
2977 if Present (First_Formal (Subp_Id)) then
2978 Formal_Typ := Etype (First_Formal (Subp_Id));
2980 if Is_Concurrent_Record_Type (Formal_Typ) then
2981 if Is_Class_Wide_Type (Formal_Typ) then
2982 Formal_Typ := Root_Type (Formal_Typ);
2983 end if;
2985 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2986 end if;
2988 -- The type of the first formal is a concurrent tagged type with
2989 -- a private view.
2991 return
2992 Is_Concurrent_Type (Formal_Typ)
2993 and then Is_Tagged_Type (Formal_Typ)
2994 and then Has_Private_Declaration (Formal_Typ);
2995 end if;
2997 return False;
2998 end Is_Private_Concurrent_Primitive;
3000 ----------------------------
3001 -- Set_Trivial_Subprogram --
3002 ----------------------------
3004 procedure Set_Trivial_Subprogram (N : Node_Id) is
3005 Nxt : constant Node_Id := Next (N);
3007 begin
3008 Set_Is_Trivial_Subprogram (Body_Id);
3010 if Present (Spec_Id) then
3011 Set_Is_Trivial_Subprogram (Spec_Id);
3012 end if;
3014 if Present (Nxt)
3015 and then Nkind (Nxt) = N_Simple_Return_Statement
3016 and then No (Next (Nxt))
3017 and then Present (Expression (Nxt))
3018 and then Is_Entity_Name (Expression (Nxt))
3019 then
3020 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3021 end if;
3022 end Set_Trivial_Subprogram;
3024 ---------------------------------
3025 -- Verify_Overriding_Indicator --
3026 ---------------------------------
3028 procedure Verify_Overriding_Indicator is
3029 begin
3030 if Must_Override (Body_Spec) then
3031 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3032 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3033 then
3034 null;
3036 elsif not Present (Overridden_Operation (Spec_Id)) then
3037 Error_Msg_NE
3038 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3040 -- Overriding indicators aren't allowed for protected subprogram
3041 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3042 -- this to a warning if -gnatd.E is enabled.
3044 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3045 Error_Msg_Warn := Error_To_Warning;
3046 Error_Msg_N
3047 ("<<overriding indicator not allowed for protected "
3048 & "subprogram body", Body_Spec);
3049 end if;
3051 elsif Must_Not_Override (Body_Spec) then
3052 if Present (Overridden_Operation (Spec_Id)) then
3053 Error_Msg_NE
3054 ("subprogram& overrides inherited operation",
3055 Body_Spec, Spec_Id);
3057 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3058 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3059 then
3060 Error_Msg_NE
3061 ("subprogram& overrides predefined operator ",
3062 Body_Spec, Spec_Id);
3064 -- Overriding indicators aren't allowed for protected subprogram
3065 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3066 -- this to a warning if -gnatd.E is enabled.
3068 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3069 Error_Msg_Warn := Error_To_Warning;
3071 Error_Msg_N
3072 ("<<overriding indicator not allowed "
3073 & "for protected subprogram body", Body_Spec);
3075 -- If this is not a primitive operation, then the overriding
3076 -- indicator is altogether illegal.
3078 elsif not Is_Primitive (Spec_Id) then
3079 Error_Msg_N
3080 ("overriding indicator only allowed "
3081 & "if subprogram is primitive", Body_Spec);
3082 end if;
3084 -- If checking the style rule and the operation overrides, then
3085 -- issue a warning about a missing overriding_indicator. Protected
3086 -- subprogram bodies are excluded from this style checking, since
3087 -- they aren't primitives (even though their declarations can
3088 -- override) and aren't allowed to have an overriding_indicator.
3090 elsif Style_Check
3091 and then Present (Overridden_Operation (Spec_Id))
3092 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3093 then
3094 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3095 Style.Missing_Overriding (N, Body_Id);
3097 elsif Style_Check
3098 and then Can_Override_Operator (Spec_Id)
3099 and then not Is_Predefined_File_Name
3100 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3101 then
3102 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3103 Style.Missing_Overriding (N, Body_Id);
3104 end if;
3105 end Verify_Overriding_Indicator;
3107 -- Start of processing for Analyze_Subprogram_Body_Helper
3109 begin
3110 -- Generic subprograms are handled separately. They always have a
3111 -- generic specification. Determine whether current scope has a
3112 -- previous declaration.
3114 -- If the subprogram body is defined within an instance of the same
3115 -- name, the instance appears as a package renaming, and will be hidden
3116 -- within the subprogram.
3118 if Present (Prev_Id)
3119 and then not Is_Overloadable (Prev_Id)
3120 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3121 or else Comes_From_Source (Prev_Id))
3122 then
3123 if Is_Generic_Subprogram (Prev_Id) then
3124 Spec_Id := Prev_Id;
3126 -- The corresponding spec may be subject to pragma Ghost with
3127 -- policy Ignore. Set the mode now to ensure that any nodes
3128 -- generated during analysis and expansion are properly flagged
3129 -- as ignored Ghost.
3131 Set_Ghost_Mode (N, Spec_Id);
3132 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3133 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3135 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3137 if Nkind (N) = N_Subprogram_Body then
3138 HSS := Handled_Statement_Sequence (N);
3139 Check_Missing_Return;
3140 end if;
3142 return;
3144 else
3145 -- Previous entity conflicts with subprogram name. Attempting to
3146 -- enter name will post error.
3148 Enter_Name (Body_Id);
3149 return;
3150 end if;
3152 -- Non-generic case, find the subprogram declaration, if one was seen,
3153 -- or enter new overloaded entity in the current scope. If the
3154 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3155 -- part of the context of one of its subunits. No need to redo the
3156 -- analysis.
3158 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3159 return;
3161 else
3162 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3164 if Nkind (N) = N_Subprogram_Body_Stub
3165 or else No (Corresponding_Spec (N))
3166 then
3167 if Is_Private_Concurrent_Primitive (Body_Id) then
3168 Spec_Id := Disambiguate_Spec;
3170 -- The corresponding spec may be subject to pragma Ghost with
3171 -- policy Ignore. Set the mode now to ensure that any nodes
3172 -- generated during analysis and expansion are properly flagged
3173 -- as ignored Ghost.
3175 Set_Ghost_Mode (N, Spec_Id);
3177 else
3178 Spec_Id := Find_Corresponding_Spec (N);
3180 -- The corresponding spec may be subject to pragma Ghost with
3181 -- policy Ignore. Set the mode now to ensure that any nodes
3182 -- generated during analysis and expansion are properly flagged
3183 -- as ignored Ghost.
3185 Set_Ghost_Mode (N, Spec_Id);
3187 -- In GNATprove mode, if the body has no previous spec, create
3188 -- one so that the inlining machinery can operate properly.
3189 -- Transfer aspects, if any, to the new spec, so that they
3190 -- are legal and can be processed ahead of the body.
3191 -- We make two copies of the given spec, one for the new
3192 -- declaration, and one for the body.
3194 if No (Spec_Id) and then GNATprove_Mode
3196 -- Inlining does not apply during pre-analysis of code
3198 and then Full_Analysis
3200 -- Inlining only applies to full bodies, not stubs
3202 and then Nkind (N) /= N_Subprogram_Body_Stub
3204 -- Inlining only applies to bodies in the source code, not to
3205 -- those generated by the compiler. In particular, expression
3206 -- functions, whose body is generated by the compiler, are
3207 -- treated specially by GNATprove.
3209 and then Comes_From_Source (Body_Id)
3211 -- This cannot be done for a compilation unit, which is not
3212 -- in a context where we can insert a new spec.
3214 and then Is_List_Member (N)
3216 -- Inlining only applies to subprograms without contracts,
3217 -- as a contract is a sign that GNATprove should perform a
3218 -- modular analysis of the subprogram instead of a contextual
3219 -- analysis at each call site. The same test is performed in
3220 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3221 -- here in another form (because the contract has not
3222 -- been attached to the body) to avoid frontend errors in
3223 -- case pragmas are used instead of aspects, because the
3224 -- corresponding pragmas in the body would not be transferred
3225 -- to the spec, leading to legality errors.
3227 and then not Body_Has_Contract
3228 then
3229 declare
3230 Body_Spec : constant Node_Id :=
3231 Copy_Separate_Tree (Specification (N));
3232 New_Decl : constant Node_Id :=
3233 Make_Subprogram_Declaration (Loc,
3234 Copy_Separate_Tree (Specification (N)));
3236 SPARK_Mode_Aspect : Node_Id;
3237 Aspects : List_Id;
3238 Prag, Aspect : Node_Id;
3240 begin
3241 Insert_Before (N, New_Decl);
3242 Move_Aspects (From => N, To => New_Decl);
3244 -- Mark the newly moved aspects as not analyzed, so that
3245 -- their effect on New_Decl is properly analyzed.
3247 Aspect := First (Aspect_Specifications (New_Decl));
3248 while Present (Aspect) loop
3249 Set_Analyzed (Aspect, False);
3250 Next (Aspect);
3251 end loop;
3253 Analyze (New_Decl);
3255 -- The analysis of the generated subprogram declaration
3256 -- may have introduced pragmas that need to be analyzed.
3258 Prag := Next (New_Decl);
3259 while Prag /= N loop
3260 Analyze (Prag);
3261 Next (Prag);
3262 end loop;
3264 Spec_Id := Defining_Entity (New_Decl);
3266 -- As Body_Id originally comes from source, mark the new
3267 -- Spec_Id as such, which is required so that calls to
3268 -- this subprogram are registered in the local effects
3269 -- stored in ALI files for GNATprove.
3271 Set_Comes_From_Source (Spec_Id, True);
3273 -- If aspect SPARK_Mode was specified on the body, it
3274 -- needs to be repeated on the generated decl and the
3275 -- body. Since the original aspect was moved to the
3276 -- generated decl, copy it for the body.
3278 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3279 SPARK_Mode_Aspect :=
3280 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3281 Set_Analyzed (SPARK_Mode_Aspect, False);
3282 Aspects := New_List (SPARK_Mode_Aspect);
3283 Set_Aspect_Specifications (N, Aspects);
3284 end if;
3286 Set_Specification (N, Body_Spec);
3287 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3288 Set_Corresponding_Spec (N, Spec_Id);
3289 end;
3290 end if;
3291 end if;
3293 -- If this is a duplicate body, no point in analyzing it
3295 if Error_Posted (N) then
3296 return;
3297 end if;
3299 -- A subprogram body should cause freezing of its own declaration,
3300 -- but if there was no previous explicit declaration, then the
3301 -- subprogram will get frozen too late (there may be code within
3302 -- the body that depends on the subprogram having been frozen,
3303 -- such as uses of extra formals), so we force it to be frozen
3304 -- here. Same holds if the body and spec are compilation units.
3305 -- Finally, if the return type is an anonymous access to protected
3306 -- subprogram, it must be frozen before the body because its
3307 -- expansion has generated an equivalent type that is used when
3308 -- elaborating the body.
3310 -- An exception in the case of Ada 2012, AI05-177: The bodies
3311 -- created for expression functions do not freeze.
3313 if No (Spec_Id)
3314 and then Nkind (Original_Node (N)) /= N_Expression_Function
3315 then
3316 Freeze_Before (N, Body_Id);
3318 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3319 Freeze_Before (N, Spec_Id);
3321 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3322 Freeze_Before (N, Etype (Body_Id));
3323 end if;
3325 else
3326 Spec_Id := Corresponding_Spec (N);
3328 -- The corresponding spec may be subject to pragma Ghost with
3329 -- policy Ignore. Set the mode now to ensure that any nodes
3330 -- generated during analysis and expansion are properly flagged
3331 -- as ignored Ghost.
3333 Set_Ghost_Mode (N, Spec_Id);
3334 end if;
3335 end if;
3337 -- Previously we scanned the body to look for nested subprograms, and
3338 -- rejected an inline directive if nested subprograms were present,
3339 -- because the back-end would generate conflicting symbols for the
3340 -- nested bodies. This is now unnecessary.
3342 -- Look ahead to recognize a pragma Inline that appears after the body
3344 Check_Inline_Pragma (Spec_Id);
3346 -- Deal with special case of a fully private operation in the body of
3347 -- the protected type. We must create a declaration for the subprogram,
3348 -- in order to attach the protected subprogram that will be used in
3349 -- internal calls. We exclude compiler generated bodies from the
3350 -- expander since the issue does not arise for those cases.
3352 if No (Spec_Id)
3353 and then Comes_From_Source (N)
3354 and then Is_Protected_Type (Current_Scope)
3355 then
3356 Spec_Id := Build_Private_Protected_Declaration (N);
3357 end if;
3359 -- If a separate spec is present, then deal with freezing issues
3361 if Present (Spec_Id) then
3362 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3363 Verify_Overriding_Indicator;
3365 -- In general, the spec will be frozen when we start analyzing the
3366 -- body. However, for internally generated operations, such as
3367 -- wrapper functions for inherited operations with controlling
3368 -- results, the spec may not have been frozen by the time we expand
3369 -- the freeze actions that include the bodies. In particular, extra
3370 -- formals for accessibility or for return-in-place may need to be
3371 -- generated. Freeze nodes, if any, are inserted before the current
3372 -- body. These freeze actions are also needed in ASIS mode to enable
3373 -- the proper back-annotations.
3375 if not Is_Frozen (Spec_Id)
3376 and then (Expander_Active or ASIS_Mode)
3377 then
3378 -- Force the generation of its freezing node to ensure proper
3379 -- management of access types in the backend.
3381 -- This is definitely needed for some cases, but it is not clear
3382 -- why, to be investigated further???
3384 Set_Has_Delayed_Freeze (Spec_Id);
3385 Freeze_Before (N, Spec_Id);
3386 end if;
3387 end if;
3389 -- Place subprogram on scope stack, and make formals visible. If there
3390 -- is a spec, the visible entity remains that of the spec.
3392 if Present (Spec_Id) then
3393 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3395 if Is_Child_Unit (Spec_Id) then
3396 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3397 end if;
3399 if Style_Check then
3400 Style.Check_Identifier (Body_Id, Spec_Id);
3401 end if;
3403 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3404 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3406 if Is_Abstract_Subprogram (Spec_Id) then
3407 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3408 return;
3410 else
3411 Set_Convention (Body_Id, Convention (Spec_Id));
3412 Set_Has_Completion (Spec_Id);
3414 -- Inherit the "ghostness" of the subprogram spec. Note that this
3415 -- property is not directly inherited as the body may be subject
3416 -- to a different Ghost assertion policy.
3418 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
3419 Set_Is_Ghost_Entity (Body_Id);
3421 -- The Ghost policy in effect at the point of declaration and
3422 -- at the point of completion must match (SPARK RM 6.9(15)).
3424 Check_Ghost_Completion (Spec_Id, Body_Id);
3425 end if;
3427 if Is_Protected_Type (Scope (Spec_Id)) then
3428 Prot_Typ := Scope (Spec_Id);
3429 end if;
3431 -- If this is a body generated for a renaming, do not check for
3432 -- full conformance. The check is redundant, because the spec of
3433 -- the body is a copy of the spec in the renaming declaration,
3434 -- and the test can lead to spurious errors on nested defaults.
3436 if Present (Spec_Decl)
3437 and then not Comes_From_Source (N)
3438 and then
3439 (Nkind (Original_Node (Spec_Decl)) =
3440 N_Subprogram_Renaming_Declaration
3441 or else (Present (Corresponding_Body (Spec_Decl))
3442 and then
3443 Nkind (Unit_Declaration_Node
3444 (Corresponding_Body (Spec_Decl))) =
3445 N_Subprogram_Renaming_Declaration))
3446 then
3447 Conformant := True;
3449 -- Conversely, the spec may have been generated for specless body
3450 -- with an inline pragma.
3452 elsif Comes_From_Source (N)
3453 and then not Comes_From_Source (Spec_Id)
3454 and then Has_Pragma_Inline (Spec_Id)
3455 then
3456 Conformant := True;
3458 else
3459 Check_Conformance
3460 (Body_Id, Spec_Id,
3461 Fully_Conformant, True, Conformant, Body_Id);
3462 end if;
3464 -- If the body is not fully conformant, we have to decide if we
3465 -- should analyze it or not. If it has a really messed up profile
3466 -- then we probably should not analyze it, since we will get too
3467 -- many bogus messages.
3469 -- Our decision is to go ahead in the non-fully conformant case
3470 -- only if it is at least mode conformant with the spec. Note
3471 -- that the call to Check_Fully_Conformant has issued the proper
3472 -- error messages to complain about the lack of conformance.
3474 if not Conformant
3475 and then not Mode_Conformant (Body_Id, Spec_Id)
3476 then
3477 return;
3478 end if;
3479 end if;
3481 if Spec_Id /= Body_Id then
3482 Reference_Body_Formals (Spec_Id, Body_Id);
3483 end if;
3485 Set_Ekind (Body_Id, E_Subprogram_Body);
3487 if Nkind (N) = N_Subprogram_Body_Stub then
3488 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3490 -- Regular body
3492 else
3493 Set_Corresponding_Spec (N, Spec_Id);
3495 -- Ada 2005 (AI-345): If the operation is a primitive operation
3496 -- of a concurrent type, the type of the first parameter has been
3497 -- replaced with the corresponding record, which is the proper
3498 -- run-time structure to use. However, within the body there may
3499 -- be uses of the formals that depend on primitive operations
3500 -- of the type (in particular calls in prefixed form) for which
3501 -- we need the original concurrent type. The operation may have
3502 -- several controlling formals, so the replacement must be done
3503 -- for all of them.
3505 if Comes_From_Source (Spec_Id)
3506 and then Present (First_Entity (Spec_Id))
3507 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3508 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3509 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3510 and then Present (Corresponding_Concurrent_Type
3511 (Etype (First_Entity (Spec_Id))))
3512 then
3513 declare
3514 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3515 Form : Entity_Id;
3517 begin
3518 Form := First_Formal (Spec_Id);
3519 while Present (Form) loop
3520 if Etype (Form) = Typ then
3521 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3522 end if;
3524 Next_Formal (Form);
3525 end loop;
3526 end;
3527 end if;
3529 -- Make the formals visible, and place subprogram on scope stack.
3530 -- This is also the point at which we set Last_Real_Spec_Entity
3531 -- to mark the entities which will not be moved to the body.
3533 Install_Formals (Spec_Id);
3534 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3536 -- Within an instance, add local renaming declarations so that
3537 -- gdb can retrieve the values of actuals more easily. This is
3538 -- only relevant if generating code (and indeed we definitely
3539 -- do not want these definitions -gnatc mode, because that would
3540 -- confuse ASIS).
3542 if Is_Generic_Instance (Spec_Id)
3543 and then Is_Wrapper_Package (Current_Scope)
3544 and then Expander_Active
3545 then
3546 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3547 end if;
3549 Push_Scope (Spec_Id);
3551 -- Make sure that the subprogram is immediately visible. For
3552 -- child units that have no separate spec this is indispensable.
3553 -- Otherwise it is safe albeit redundant.
3555 Set_Is_Immediately_Visible (Spec_Id);
3556 end if;
3558 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3559 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3560 Set_Scope (Body_Id, Scope (Spec_Id));
3562 -- Case of subprogram body with no previous spec
3564 else
3565 -- Check for style warning required
3567 if Style_Check
3569 -- Only apply check for source level subprograms for which checks
3570 -- have not been suppressed.
3572 and then Comes_From_Source (Body_Id)
3573 and then not Suppress_Style_Checks (Body_Id)
3575 -- No warnings within an instance
3577 and then not In_Instance
3579 -- No warnings for expression functions
3581 and then Nkind (Original_Node (N)) /= N_Expression_Function
3582 then
3583 Style.Body_With_No_Spec (N);
3584 end if;
3586 New_Overloaded_Entity (Body_Id);
3588 if Nkind (N) /= N_Subprogram_Body_Stub then
3589 Set_Acts_As_Spec (N);
3590 Generate_Definition (Body_Id);
3591 Generate_Reference
3592 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3593 Install_Formals (Body_Id);
3595 Push_Scope (Body_Id);
3596 end if;
3598 -- For stubs and bodies with no previous spec, generate references to
3599 -- formals.
3601 Generate_Reference_To_Formals (Body_Id);
3602 end if;
3604 -- Set SPARK_Mode from context
3606 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3607 Set_SPARK_Pragma_Inherited (Body_Id, True);
3609 -- If the return type is an anonymous access type whose designated type
3610 -- is the limited view of a class-wide type and the non-limited view is
3611 -- available, update the return type accordingly.
3613 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3614 declare
3615 Etyp : Entity_Id;
3616 Rtyp : Entity_Id;
3618 begin
3619 Rtyp := Etype (Current_Scope);
3621 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3622 Etyp := Directly_Designated_Type (Rtyp);
3624 if Is_Class_Wide_Type (Etyp)
3625 and then From_Limited_With (Etyp)
3626 then
3627 Set_Directly_Designated_Type
3628 (Etype (Current_Scope), Available_View (Etyp));
3629 end if;
3630 end if;
3631 end;
3632 end if;
3634 -- If this is the proper body of a stub, we must verify that the stub
3635 -- conforms to the body, and to the previous spec if one was present.
3636 -- We know already that the body conforms to that spec. This test is
3637 -- only required for subprograms that come from source.
3639 if Nkind (Parent (N)) = N_Subunit
3640 and then Comes_From_Source (N)
3641 and then not Error_Posted (Body_Id)
3642 and then Nkind (Corresponding_Stub (Parent (N))) =
3643 N_Subprogram_Body_Stub
3644 then
3645 declare
3646 Old_Id : constant Entity_Id :=
3647 Defining_Entity
3648 (Specification (Corresponding_Stub (Parent (N))));
3650 Conformant : Boolean := False;
3652 begin
3653 if No (Spec_Id) then
3654 Check_Fully_Conformant (Body_Id, Old_Id);
3656 else
3657 Check_Conformance
3658 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3660 if not Conformant then
3662 -- The stub was taken to be a new declaration. Indicate that
3663 -- it lacks a body.
3665 Set_Has_Completion (Old_Id, False);
3666 end if;
3667 end if;
3668 end;
3669 end if;
3671 Set_Has_Completion (Body_Id);
3672 Check_Eliminated (Body_Id);
3674 if Nkind (N) = N_Subprogram_Body_Stub then
3676 -- Analyze any aspect specifications that appear on the subprogram
3677 -- body stub.
3679 if Has_Aspects (N) then
3680 Analyze_Aspects_On_Body_Or_Stub;
3681 end if;
3683 -- Stop the analysis now as the stub cannot be inlined, plus it does
3684 -- not have declarative or statement lists.
3686 return;
3687 end if;
3689 -- Handle frontend inlining
3691 -- Note: Normally we don't do any inlining if expansion is off, since
3692 -- we won't generate code in any case. An exception arises in GNATprove
3693 -- mode where we want to expand some calls in place, even with expansion
3694 -- disabled, since the inlining eases formal verification.
3696 if not GNATprove_Mode
3697 and then Expander_Active
3698 and then Serious_Errors_Detected = 0
3699 and then Present (Spec_Id)
3700 and then Has_Pragma_Inline (Spec_Id)
3701 then
3702 -- Legacy implementation (relying on frontend inlining)
3704 if not Back_End_Inlining then
3705 if (Has_Pragma_Inline_Always (Spec_Id)
3706 and then not Opt.Disable_FE_Inline_Always)
3707 or else
3708 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3709 and then not Opt.Disable_FE_Inline)
3710 then
3711 Build_Body_To_Inline (N, Spec_Id);
3712 end if;
3714 -- New implementation (relying on backend inlining)
3716 else
3717 if Has_Pragma_Inline_Always (Spec_Id)
3718 or else Optimization_Level > 0
3719 then
3720 -- Handle function returning an unconstrained type
3722 if Comes_From_Source (Body_Id)
3723 and then Ekind (Spec_Id) = E_Function
3724 and then Returns_Unconstrained_Type (Spec_Id)
3726 -- If function builds in place, i.e. returns a limited type,
3727 -- inlining cannot be done.
3729 and then not Is_Limited_Type (Etype (Spec_Id))
3730 then
3731 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3733 else
3734 declare
3735 Subp_Body : constant Node_Id :=
3736 Unit_Declaration_Node (Body_Id);
3737 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3739 begin
3740 -- Do not pass inlining to the backend if the subprogram
3741 -- has declarations or statements which cannot be inlined
3742 -- by the backend. This check is done here to emit an
3743 -- error instead of the generic warning message reported
3744 -- by the GCC backend (ie. "function might not be
3745 -- inlinable").
3747 if Present (Subp_Decl)
3748 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3749 then
3750 null;
3752 elsif Has_Excluded_Statement
3753 (Spec_Id,
3754 Statements
3755 (Handled_Statement_Sequence (Subp_Body)))
3756 then
3757 null;
3759 -- If the backend inlining is available then at this
3760 -- stage we only have to mark the subprogram as inlined.
3761 -- The expander will take care of registering it in the
3762 -- table of subprograms inlined by the backend a part of
3763 -- processing calls to it (cf. Expand_Call)
3765 else
3766 Set_Is_Inlined (Spec_Id);
3767 end if;
3768 end;
3769 end if;
3770 end if;
3771 end if;
3773 -- In GNATprove mode, inline only when there is a separate subprogram
3774 -- declaration for now, as inlining of subprogram bodies acting as
3775 -- declarations, or subprogram stubs, are not supported by frontend
3776 -- inlining. This inlining should occur after analysis of the body, so
3777 -- that it is known whether the value of SPARK_Mode applicable to the
3778 -- body, which can be defined by a pragma inside the body.
3780 elsif GNATprove_Mode
3781 and then Full_Analysis
3782 and then not Inside_A_Generic
3783 and then Present (Spec_Id)
3784 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3785 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3786 and then not Body_Has_Contract
3787 then
3788 Build_Body_To_Inline (N, Spec_Id);
3789 end if;
3791 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3792 -- of the specification we have to install the private withed units.
3793 -- This holds for child units as well.
3795 if Is_Compilation_Unit (Body_Id)
3796 or else Nkind (Parent (N)) = N_Compilation_Unit
3797 then
3798 Install_Private_With_Clauses (Body_Id);
3799 end if;
3801 Check_Anonymous_Return;
3803 -- Set the Protected_Formal field of each extra formal of the protected
3804 -- subprogram to reference the corresponding extra formal of the
3805 -- subprogram that implements it. For regular formals this occurs when
3806 -- the protected subprogram's declaration is expanded, but the extra
3807 -- formals don't get created until the subprogram is frozen. We need to
3808 -- do this before analyzing the protected subprogram's body so that any
3809 -- references to the original subprogram's extra formals will be changed
3810 -- refer to the implementing subprogram's formals (see Expand_Formal).
3812 if Present (Spec_Id)
3813 and then Is_Protected_Type (Scope (Spec_Id))
3814 and then Present (Protected_Body_Subprogram (Spec_Id))
3815 then
3816 declare
3817 Impl_Subp : constant Entity_Id :=
3818 Protected_Body_Subprogram (Spec_Id);
3819 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3820 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3821 begin
3822 while Present (Prot_Ext_Formal) loop
3823 pragma Assert (Present (Impl_Ext_Formal));
3824 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3825 Next_Formal_With_Extras (Prot_Ext_Formal);
3826 Next_Formal_With_Extras (Impl_Ext_Formal);
3827 end loop;
3828 end;
3829 end if;
3831 -- Now we can go on to analyze the body
3833 HSS := Handled_Statement_Sequence (N);
3834 Set_Actual_Subtypes (N, Current_Scope);
3836 -- Add a declaration for the Protection object, renaming declarations
3837 -- for discriminals and privals and finally a declaration for the entry
3838 -- family index (if applicable). This form of early expansion is done
3839 -- when the Expander is active because Install_Private_Data_Declarations
3840 -- references entities which were created during regular expansion. The
3841 -- subprogram entity must come from source, and not be an internally
3842 -- generated subprogram.
3844 if Expander_Active
3845 and then Present (Prot_Typ)
3846 and then Present (Spec_Id)
3847 and then Comes_From_Source (Spec_Id)
3848 and then not Is_Eliminated (Spec_Id)
3849 then
3850 Install_Private_Data_Declarations
3851 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3852 end if;
3854 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3855 -- may now appear in parameter and result profiles. Since the analysis
3856 -- of a subprogram body may use the parameter and result profile of the
3857 -- spec, swap any limited views with their non-limited counterpart.
3859 if Ada_Version >= Ada_2012 then
3860 Exchange_Limited_Views (Spec_Id);
3861 end if;
3863 -- Analyze any aspect specifications that appear on the subprogram body
3865 if Has_Aspects (N) then
3866 Analyze_Aspects_On_Body_Or_Stub;
3867 end if;
3869 Analyze_Declarations (Declarations (N));
3871 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3873 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3874 if Present (SPARK_Pragma (Spec_Id)) then
3875 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3876 and then
3877 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3878 then
3879 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3880 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3881 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3882 Error_Msg_NE
3883 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3884 end if;
3886 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3887 null;
3889 else
3890 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3891 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3892 Error_Msg_Sloc := Sloc (Spec_Id);
3893 Error_Msg_NE
3894 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3895 end if;
3896 end if;
3898 -- When a subprogram body appears inside a package, its contract is
3899 -- analyzed at the end of the package body declarations. This is due
3900 -- to the delay with respect of the package contract upon which the
3901 -- body contract may depend. When the subprogram body is stand alone
3902 -- and acts as a compilation unit, this delay is not necessary.
3904 if Nkind (Parent (N)) = N_Compilation_Unit then
3905 Analyze_Subprogram_Body_Contract (Body_Id);
3906 end if;
3908 -- Deal with preconditions, [refined] postconditions, Contract_Cases,
3909 -- invariants and predicates associated with body and its spec. Since
3910 -- there is no routine Expand_Declarations which would otherwise deal
3911 -- with the contract expansion, generate all necessary mechanisms to
3912 -- verify the contract assertions now.
3914 Expand_Subprogram_Contract (N);
3916 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3917 -- subprogram in GNATprove mode, as its body should not be analyzed.
3919 if SPARK_Mode /= On
3920 and then GNATprove_Mode
3921 and then Present (Spec_Id)
3922 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3923 then
3924 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3925 Set_Is_Inlined_Always (Spec_Id, False);
3926 end if;
3928 -- Check completion, and analyze the statements
3930 Check_Completion;
3931 Inspect_Deferred_Constant_Completion (Declarations (N));
3932 Analyze (HSS);
3934 -- Deal with end of scope processing for the body
3936 Process_End_Label (HSS, 't', Current_Scope);
3937 End_Scope;
3938 Check_Subprogram_Order (N);
3939 Set_Analyzed (Body_Id);
3941 -- If we have a separate spec, then the analysis of the declarations
3942 -- caused the entities in the body to be chained to the spec id, but
3943 -- we want them chained to the body id. Only the formal parameters
3944 -- end up chained to the spec id in this case.
3946 if Present (Spec_Id) then
3948 -- We must conform to the categorization of our spec
3950 Validate_Categorization_Dependency (N, Spec_Id);
3952 -- And if this is a child unit, the parent units must conform
3954 if Is_Child_Unit (Spec_Id) then
3955 Validate_Categorization_Dependency
3956 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3957 end if;
3959 -- Here is where we move entities from the spec to the body
3961 -- Case where there are entities that stay with the spec
3963 if Present (Last_Real_Spec_Entity) then
3965 -- No body entities (happens when the only real spec entities come
3966 -- from precondition and postcondition pragmas).
3968 if No (Last_Entity (Body_Id)) then
3969 Set_First_Entity
3970 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3972 -- Body entities present (formals), so chain stuff past them
3974 else
3975 Set_Next_Entity
3976 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3977 end if;
3979 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3980 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3981 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3983 -- Case where there are no spec entities, in this case there can be
3984 -- no body entities either, so just move everything.
3986 -- If the body is generated for an expression function, it may have
3987 -- been preanalyzed already, if 'access was applied to it.
3989 else
3990 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3991 N_Expression_Function
3992 then
3993 pragma Assert (No (Last_Entity (Body_Id)));
3994 null;
3995 end if;
3997 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3998 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3999 Set_First_Entity (Spec_Id, Empty);
4000 Set_Last_Entity (Spec_Id, Empty);
4001 end if;
4002 end if;
4004 Check_Missing_Return;
4006 -- Now we are going to check for variables that are never modified in
4007 -- the body of the procedure. But first we deal with a special case
4008 -- where we want to modify this check. If the body of the subprogram
4009 -- starts with a raise statement or its equivalent, or if the body
4010 -- consists entirely of a null statement, then it is pretty obvious that
4011 -- it is OK to not reference the parameters. For example, this might be
4012 -- the following common idiom for a stubbed function: statement of the
4013 -- procedure raises an exception. In particular this deals with the
4014 -- common idiom of a stubbed function, which appears something like:
4016 -- function F (A : Integer) return Some_Type;
4017 -- X : Some_Type;
4018 -- begin
4019 -- raise Program_Error;
4020 -- return X;
4021 -- end F;
4023 -- Here the purpose of X is simply to satisfy the annoying requirement
4024 -- in Ada that there be at least one return, and we certainly do not
4025 -- want to go posting warnings on X that it is not initialized. On
4026 -- the other hand, if X is entirely unreferenced that should still
4027 -- get a warning.
4029 -- What we do is to detect these cases, and if we find them, flag the
4030 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4031 -- suppress unwanted warnings. For the case of the function stub above
4032 -- we have a special test to set X as apparently assigned to suppress
4033 -- the warning.
4035 declare
4036 Stm : Node_Id;
4038 begin
4039 -- Skip initial labels (for one thing this occurs when we are in
4040 -- front end ZCX mode, but in any case it is irrelevant), and also
4041 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4043 Stm := First (Statements (HSS));
4044 while Nkind (Stm) = N_Label
4045 or else Nkind (Stm) in N_Push_xxx_Label
4046 loop
4047 Next (Stm);
4048 end loop;
4050 -- Do the test on the original statement before expansion
4052 declare
4053 Ostm : constant Node_Id := Original_Node (Stm);
4055 begin
4056 -- If explicit raise statement, turn on flag
4058 if Nkind (Ostm) = N_Raise_Statement then
4059 Set_Trivial_Subprogram (Stm);
4061 -- If null statement, and no following statements, turn on flag
4063 elsif Nkind (Stm) = N_Null_Statement
4064 and then Comes_From_Source (Stm)
4065 and then No (Next (Stm))
4066 then
4067 Set_Trivial_Subprogram (Stm);
4069 -- Check for explicit call cases which likely raise an exception
4071 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4072 if Is_Entity_Name (Name (Ostm)) then
4073 declare
4074 Ent : constant Entity_Id := Entity (Name (Ostm));
4076 begin
4077 -- If the procedure is marked No_Return, then likely it
4078 -- raises an exception, but in any case it is not coming
4079 -- back here, so turn on the flag.
4081 if Present (Ent)
4082 and then Ekind (Ent) = E_Procedure
4083 and then No_Return (Ent)
4084 then
4085 Set_Trivial_Subprogram (Stm);
4086 end if;
4087 end;
4088 end if;
4089 end if;
4090 end;
4091 end;
4093 -- Check for variables that are never modified
4095 declare
4096 E1, E2 : Entity_Id;
4098 begin
4099 -- If there is a separate spec, then transfer Never_Set_In_Source
4100 -- flags from out parameters to the corresponding entities in the
4101 -- body. The reason we do that is we want to post error flags on
4102 -- the body entities, not the spec entities.
4104 if Present (Spec_Id) then
4105 E1 := First_Entity (Spec_Id);
4106 while Present (E1) loop
4107 if Ekind (E1) = E_Out_Parameter then
4108 E2 := First_Entity (Body_Id);
4109 while Present (E2) loop
4110 exit when Chars (E1) = Chars (E2);
4111 Next_Entity (E2);
4112 end loop;
4114 if Present (E2) then
4115 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4116 end if;
4117 end if;
4119 Next_Entity (E1);
4120 end loop;
4121 end if;
4123 -- Check references in body
4125 Check_References (Body_Id);
4126 end;
4128 -- Check for nested subprogram, and mark outer level subprogram if so
4130 declare
4131 Ent : Entity_Id;
4133 begin
4134 if Present (Spec_Id) then
4135 Ent := Spec_Id;
4136 else
4137 Ent := Body_Id;
4138 end if;
4140 loop
4141 Ent := Enclosing_Subprogram (Ent);
4142 exit when No (Ent) or else Is_Subprogram (Ent);
4143 end loop;
4145 if Present (Ent) then
4146 Set_Has_Nested_Subprogram (Ent);
4147 end if;
4148 end;
4149 end Analyze_Subprogram_Body_Helper;
4151 ---------------------------------
4152 -- Analyze_Subprogram_Contract --
4153 ---------------------------------
4155 procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is
4156 procedure Save_Global_References_In_List (First_Prag : Node_Id);
4157 -- Save all global references in contract-related source pragma found in
4158 -- the list starting from pragma First_Prag.
4160 ------------------------------------
4161 -- Save_Global_References_In_List --
4162 ------------------------------------
4164 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
4165 Prag : Node_Id;
4167 begin
4168 Prag := First_Prag;
4169 while Present (Prag) loop
4170 if Comes_From_Source (Prag)
4171 and then Nam_In (Pragma_Name (Prag), Name_Contract_Cases,
4172 Name_Depends,
4173 Name_Extensions_Visible,
4174 Name_Global,
4175 Name_Postcondition,
4176 Name_Precondition,
4177 Name_Test_Case)
4178 then
4179 Save_Global_References (Original_Node (Prag));
4180 end if;
4182 Prag := Next_Pragma (Prag);
4183 end loop;
4184 end Save_Global_References_In_List;
4186 -- Local variables
4188 Items : constant Node_Id := Contract (Subp_Id);
4189 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4190 Depends : Node_Id := Empty;
4191 Global : Node_Id := Empty;
4192 Mode : SPARK_Mode_Type;
4193 Prag : Node_Id;
4194 Prag_Nam : Name_Id;
4195 Restore_Scope : Boolean := False;
4197 -- Start of processing for Analyze_Subprogram_Contract
4199 begin
4200 -- All subprograms carry a contract, but for some it is not significant
4201 -- and should not be processed.
4203 if not Has_Significant_Contract (Subp_Id) then
4204 return;
4205 end if;
4207 -- Due to the timing of contract analysis, delayed pragmas may be
4208 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4209 -- context. To remedy this, restore the original SPARK_Mode of the
4210 -- related subprogram body.
4212 Save_SPARK_Mode_And_Set (Subp_Id, Mode);
4214 -- Ensure that the formal parameters are visible when analyzing all
4215 -- contract items.
4217 if not In_Open_Scopes (Subp_Id) then
4218 Restore_Scope := True;
4219 Push_Scope (Subp_Id);
4221 if Is_Generic_Subprogram (Subp_Id) then
4222 Install_Generic_Formals (Subp_Id);
4223 else
4224 Install_Formals (Subp_Id);
4225 end if;
4226 end if;
4228 if Present (Items) then
4230 -- Analyze pre- and postconditions
4232 Prag := Pre_Post_Conditions (Items);
4233 while Present (Prag) loop
4234 Analyze_Pre_Post_Condition_In_Decl_Part (Prag);
4235 Prag := Next_Pragma (Prag);
4236 end loop;
4238 -- Analyze contract-cases and test-cases
4240 Prag := Contract_Test_Cases (Items);
4241 while Present (Prag) loop
4242 Prag_Nam := Pragma_Name (Prag);
4244 if Prag_Nam = Name_Contract_Cases then
4245 Analyze_Contract_Cases_In_Decl_Part (Prag);
4246 else
4247 pragma Assert (Prag_Nam = Name_Test_Case);
4248 Analyze_Test_Case_In_Decl_Part (Prag);
4249 end if;
4251 Prag := Next_Pragma (Prag);
4252 end loop;
4254 -- Analyze classification pragmas
4256 Prag := Classifications (Items);
4257 while Present (Prag) loop
4258 Prag_Nam := Pragma_Name (Prag);
4260 if Prag_Nam = Name_Depends then
4261 Depends := Prag;
4263 elsif Prag_Nam = Name_Global then
4264 Global := Prag;
4266 -- Note that pragma Extensions_Visible has already been analyzed
4268 end if;
4270 Prag := Next_Pragma (Prag);
4271 end loop;
4273 -- Analyze Global first as Depends may mention items classified in
4274 -- the global categorization.
4276 if Present (Global) then
4277 Analyze_Global_In_Decl_Part (Global);
4278 end if;
4280 -- Depends must be analyzed after Global in order to see the modes of
4281 -- all global items.
4283 if Present (Depends) then
4284 Analyze_Depends_In_Decl_Part (Depends);
4285 end if;
4287 -- Ensure that the contract cases or postconditions mention 'Result
4288 -- or define a post-state.
4290 Check_Result_And_Post_State (Subp_Id);
4291 end if;
4293 -- The aspects and contract-related source pragmas associated with a
4294 -- generic subprogram are treated separately from the declaration as
4295 -- they need to be analyzed when the subprogram contract is analyzed.
4296 -- Once this is done, global references can be successfully saved.
4298 if Nkind (Subp_Decl) = N_Generic_Subprogram_Declaration then
4300 -- Save all global references found in the aspect specifications of
4301 -- the parameter profile of the generic subprogram.
4303 Save_Global_References_In_Aspects (Original_Node (Subp_Decl));
4305 -- Save all global references found in contract-related source
4306 -- pragmas. These pragmas usually appear after the declaration of
4307 -- the generic subprogram, either in the same declarative part or
4308 -- in the Pragmas_After list when the generic subprogram is a
4309 -- compilation unit.
4311 if Present (Items) then
4312 Save_Global_References_In_List (Pre_Post_Conditions (Items));
4313 Save_Global_References_In_List (Contract_Test_Cases (Items));
4314 Save_Global_References_In_List (Classifications (Items));
4315 end if;
4316 end if;
4318 if Restore_Scope then
4319 End_Scope;
4320 end if;
4322 -- Restore the SPARK_Mode of the enclosing context after all delayed
4323 -- pragmas have been analyzed.
4325 Restore_SPARK_Mode (Mode);
4326 end Analyze_Subprogram_Contract;
4328 ------------------------------------
4329 -- Analyze_Subprogram_Declaration --
4330 ------------------------------------
4332 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4333 Scop : constant Entity_Id := Current_Scope;
4334 Designator : Entity_Id;
4336 Is_Completion : Boolean;
4337 -- Indicates whether a null procedure declaration is a completion
4339 begin
4340 -- The subprogram declaration may be subject to pragma Ghost with policy
4341 -- Ignore. Set the mode now to ensure that any nodes generated during
4342 -- analysis and expansion are properly flagged as ignored Ghost.
4344 Set_Ghost_Mode (N);
4346 -- Null procedures are not allowed in SPARK
4348 if Nkind (Specification (N)) = N_Procedure_Specification
4349 and then Null_Present (Specification (N))
4350 then
4351 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4353 if Is_Protected_Type (Current_Scope) then
4354 Error_Msg_N ("protected operation cannot be a null procedure", N);
4355 end if;
4357 Analyze_Null_Procedure (N, Is_Completion);
4359 if Is_Completion then
4361 -- The null procedure acts as a body, nothing further is needed.
4363 return;
4364 end if;
4365 end if;
4367 Designator := Analyze_Subprogram_Specification (Specification (N));
4369 -- A reference may already have been generated for the unit name, in
4370 -- which case the following call is redundant. However it is needed for
4371 -- declarations that are the rewriting of an expression function.
4373 Generate_Definition (Designator);
4375 -- Set SPARK mode from current context (may be overwritten later with
4376 -- explicit pragma).
4378 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4379 Set_SPARK_Pragma_Inherited (Designator);
4381 -- A subprogram declared within a Ghost region is automatically Ghost
4382 -- (SPARK RM 6.9(2)).
4384 if Comes_From_Source (Designator) and then Ghost_Mode > None then
4385 Set_Is_Ghost_Entity (Designator);
4386 end if;
4388 if Debug_Flag_C then
4389 Write_Str ("==> subprogram spec ");
4390 Write_Name (Chars (Designator));
4391 Write_Str (" from ");
4392 Write_Location (Sloc (N));
4393 Write_Eol;
4394 Indent;
4395 end if;
4397 Validate_RCI_Subprogram_Declaration (N);
4398 New_Overloaded_Entity (Designator);
4399 Check_Delayed_Subprogram (Designator);
4401 -- If the type of the first formal of the current subprogram is a non-
4402 -- generic tagged private type, mark the subprogram as being a private
4403 -- primitive. Ditto if this is a function with controlling result, and
4404 -- the return type is currently private. In both cases, the type of the
4405 -- controlling argument or result must be in the current scope for the
4406 -- operation to be primitive.
4408 if Has_Controlling_Result (Designator)
4409 and then Is_Private_Type (Etype (Designator))
4410 and then Scope (Etype (Designator)) = Current_Scope
4411 and then not Is_Generic_Actual_Type (Etype (Designator))
4412 then
4413 Set_Is_Private_Primitive (Designator);
4415 elsif Present (First_Formal (Designator)) then
4416 declare
4417 Formal_Typ : constant Entity_Id :=
4418 Etype (First_Formal (Designator));
4419 begin
4420 Set_Is_Private_Primitive (Designator,
4421 Is_Tagged_Type (Formal_Typ)
4422 and then Scope (Formal_Typ) = Current_Scope
4423 and then Is_Private_Type (Formal_Typ)
4424 and then not Is_Generic_Actual_Type (Formal_Typ));
4425 end;
4426 end if;
4428 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4429 -- or null.
4431 if Ada_Version >= Ada_2005
4432 and then Comes_From_Source (N)
4433 and then Is_Dispatching_Operation (Designator)
4434 then
4435 declare
4436 E : Entity_Id;
4437 Etyp : Entity_Id;
4439 begin
4440 if Has_Controlling_Result (Designator) then
4441 Etyp := Etype (Designator);
4443 else
4444 E := First_Entity (Designator);
4445 while Present (E)
4446 and then Is_Formal (E)
4447 and then not Is_Controlling_Formal (E)
4448 loop
4449 Next_Entity (E);
4450 end loop;
4452 Etyp := Etype (E);
4453 end if;
4455 if Is_Access_Type (Etyp) then
4456 Etyp := Directly_Designated_Type (Etyp);
4457 end if;
4459 if Is_Interface (Etyp)
4460 and then not Is_Abstract_Subprogram (Designator)
4461 and then not (Ekind (Designator) = E_Procedure
4462 and then Null_Present (Specification (N)))
4463 then
4464 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4466 -- Specialize error message based on procedures vs. functions,
4467 -- since functions can't be null subprograms.
4469 if Ekind (Designator) = E_Procedure then
4470 Error_Msg_N
4471 ("interface procedure % must be abstract or null", N);
4472 else
4473 Error_Msg_N
4474 ("interface function % must be abstract", N);
4475 end if;
4476 end if;
4477 end;
4478 end if;
4480 -- What is the following code for, it used to be
4482 -- ??? Set_Suppress_Elaboration_Checks
4483 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4485 -- The following seems equivalent, but a bit dubious
4487 if Elaboration_Checks_Suppressed (Designator) then
4488 Set_Kill_Elaboration_Checks (Designator);
4489 end if;
4491 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4492 Set_Categorization_From_Scope (Designator, Scop);
4494 else
4495 -- For a compilation unit, check for library-unit pragmas
4497 Push_Scope (Designator);
4498 Set_Categorization_From_Pragmas (N);
4499 Validate_Categorization_Dependency (N, Designator);
4500 Pop_Scope;
4501 end if;
4503 -- For a compilation unit, set body required. This flag will only be
4504 -- reset if a valid Import or Interface pragma is processed later on.
4506 if Nkind (Parent (N)) = N_Compilation_Unit then
4507 Set_Body_Required (Parent (N), True);
4509 if Ada_Version >= Ada_2005
4510 and then Nkind (Specification (N)) = N_Procedure_Specification
4511 and then Null_Present (Specification (N))
4512 then
4513 Error_Msg_N
4514 ("null procedure cannot be declared at library level", N);
4515 end if;
4516 end if;
4518 Generate_Reference_To_Formals (Designator);
4519 Check_Eliminated (Designator);
4521 if Debug_Flag_C then
4522 Outdent;
4523 Write_Str ("<== subprogram spec ");
4524 Write_Name (Chars (Designator));
4525 Write_Str (" from ");
4526 Write_Location (Sloc (N));
4527 Write_Eol;
4528 end if;
4530 if Is_Protected_Type (Current_Scope) then
4532 -- Indicate that this is a protected operation, because it may be
4533 -- used in subsequent declarations within the protected type.
4535 Set_Convention (Designator, Convention_Protected);
4536 end if;
4538 List_Inherited_Pre_Post_Aspects (Designator);
4540 if Has_Aspects (N) then
4541 Analyze_Aspect_Specifications (N, Designator);
4542 end if;
4543 end Analyze_Subprogram_Declaration;
4545 --------------------------------------
4546 -- Analyze_Subprogram_Specification --
4547 --------------------------------------
4549 -- Reminder: N here really is a subprogram specification (not a subprogram
4550 -- declaration). This procedure is called to analyze the specification in
4551 -- both subprogram bodies and subprogram declarations (specs).
4553 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4554 Designator : constant Entity_Id := Defining_Entity (N);
4555 Formals : constant List_Id := Parameter_Specifications (N);
4557 -- Start of processing for Analyze_Subprogram_Specification
4559 begin
4560 -- User-defined operator is not allowed in SPARK, except as a renaming
4562 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4563 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4564 then
4565 Check_SPARK_05_Restriction
4566 ("user-defined operator is not allowed", N);
4567 end if;
4569 -- Proceed with analysis. Do not emit a cross-reference entry if the
4570 -- specification comes from an expression function, because it may be
4571 -- the completion of a previous declaration. It is is not, the cross-
4572 -- reference entry will be emitted for the new subprogram declaration.
4574 if Nkind (Parent (N)) /= N_Expression_Function then
4575 Generate_Definition (Designator);
4576 end if;
4578 if Nkind (N) = N_Function_Specification then
4579 Set_Ekind (Designator, E_Function);
4580 Set_Mechanism (Designator, Default_Mechanism);
4581 else
4582 Set_Ekind (Designator, E_Procedure);
4583 Set_Etype (Designator, Standard_Void_Type);
4584 end if;
4586 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4587 -- those subprograms which could be inlined in GNATprove mode (because
4588 -- Body_To_Inline is non-Empty) but cannot be inlined.
4590 if GNATprove_Mode then
4591 Set_Is_Inlined_Always (Designator);
4592 end if;
4594 -- Introduce new scope for analysis of the formals and the return type
4596 Set_Scope (Designator, Current_Scope);
4598 if Present (Formals) then
4599 Push_Scope (Designator);
4600 Process_Formals (Formals, N);
4602 -- Check dimensions in N for formals with default expression
4604 Analyze_Dimension_Formals (N, Formals);
4606 -- Ada 2005 (AI-345): If this is an overriding operation of an
4607 -- inherited interface operation, and the controlling type is
4608 -- a synchronized type, replace the type with its corresponding
4609 -- record, to match the proper signature of an overriding operation.
4610 -- Same processing for an access parameter whose designated type is
4611 -- derived from a synchronized interface.
4613 if Ada_Version >= Ada_2005 then
4614 declare
4615 Formal : Entity_Id;
4616 Formal_Typ : Entity_Id;
4617 Rec_Typ : Entity_Id;
4618 Desig_Typ : Entity_Id;
4620 begin
4621 Formal := First_Formal (Designator);
4622 while Present (Formal) loop
4623 Formal_Typ := Etype (Formal);
4625 if Is_Concurrent_Type (Formal_Typ)
4626 and then Present (Corresponding_Record_Type (Formal_Typ))
4627 then
4628 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4630 if Present (Interfaces (Rec_Typ)) then
4631 Set_Etype (Formal, Rec_Typ);
4632 end if;
4634 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4635 Desig_Typ := Designated_Type (Formal_Typ);
4637 if Is_Concurrent_Type (Desig_Typ)
4638 and then Present (Corresponding_Record_Type (Desig_Typ))
4639 then
4640 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4642 if Present (Interfaces (Rec_Typ)) then
4643 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4644 end if;
4645 end if;
4646 end if;
4648 Next_Formal (Formal);
4649 end loop;
4650 end;
4651 end if;
4653 End_Scope;
4655 -- The subprogram scope is pushed and popped around the processing of
4656 -- the return type for consistency with call above to Process_Formals
4657 -- (which itself can call Analyze_Return_Type), and to ensure that any
4658 -- itype created for the return type will be associated with the proper
4659 -- scope.
4661 elsif Nkind (N) = N_Function_Specification then
4662 Push_Scope (Designator);
4663 Analyze_Return_Type (N);
4664 End_Scope;
4665 end if;
4667 -- Function case
4669 if Nkind (N) = N_Function_Specification then
4671 -- Deal with operator symbol case
4673 if Nkind (Designator) = N_Defining_Operator_Symbol then
4674 Valid_Operator_Definition (Designator);
4675 end if;
4677 May_Need_Actuals (Designator);
4679 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4680 -- the subprogram is abstract also. This does not apply to renaming
4681 -- declarations, where abstractness is inherited, and to subprogram
4682 -- bodies generated for stream operations, which become renamings as
4683 -- bodies.
4685 -- In case of primitives associated with abstract interface types
4686 -- the check is applied later (see Analyze_Subprogram_Declaration).
4688 if not Nkind_In (Original_Node (Parent (N)),
4689 N_Subprogram_Renaming_Declaration,
4690 N_Abstract_Subprogram_Declaration,
4691 N_Formal_Abstract_Subprogram_Declaration)
4692 then
4693 if Is_Abstract_Type (Etype (Designator))
4694 and then not Is_Interface (Etype (Designator))
4695 then
4696 Error_Msg_N
4697 ("function that returns abstract type must be abstract", N);
4699 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4700 -- access result whose designated type is abstract.
4702 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4703 and then
4704 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4705 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4706 and then Ada_Version >= Ada_2012
4707 then
4708 Error_Msg_N ("function whose access result designates "
4709 & "abstract type must be abstract", N);
4710 end if;
4711 end if;
4712 end if;
4714 return Designator;
4715 end Analyze_Subprogram_Specification;
4717 -----------------------
4718 -- Check_Conformance --
4719 -----------------------
4721 procedure Check_Conformance
4722 (New_Id : Entity_Id;
4723 Old_Id : Entity_Id;
4724 Ctype : Conformance_Type;
4725 Errmsg : Boolean;
4726 Conforms : out Boolean;
4727 Err_Loc : Node_Id := Empty;
4728 Get_Inst : Boolean := False;
4729 Skip_Controlling_Formals : Boolean := False)
4731 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4732 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4733 -- If Errmsg is True, then processing continues to post an error message
4734 -- for conformance error on given node. Two messages are output. The
4735 -- first message points to the previous declaration with a general "no
4736 -- conformance" message. The second is the detailed reason, supplied as
4737 -- Msg. The parameter N provide information for a possible & insertion
4738 -- in the message, and also provides the location for posting the
4739 -- message in the absence of a specified Err_Loc location.
4741 -----------------------
4742 -- Conformance_Error --
4743 -----------------------
4745 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4746 Enode : Node_Id;
4748 begin
4749 Conforms := False;
4751 if Errmsg then
4752 if No (Err_Loc) then
4753 Enode := N;
4754 else
4755 Enode := Err_Loc;
4756 end if;
4758 Error_Msg_Sloc := Sloc (Old_Id);
4760 case Ctype is
4761 when Type_Conformant =>
4762 Error_Msg_N -- CODEFIX
4763 ("not type conformant with declaration#!", Enode);
4765 when Mode_Conformant =>
4766 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4767 Error_Msg_N
4768 ("not mode conformant with operation inherited#!",
4769 Enode);
4770 else
4771 Error_Msg_N
4772 ("not mode conformant with declaration#!", Enode);
4773 end if;
4775 when Subtype_Conformant =>
4776 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4777 Error_Msg_N
4778 ("not subtype conformant with operation inherited#!",
4779 Enode);
4780 else
4781 Error_Msg_N
4782 ("not subtype conformant with declaration#!", Enode);
4783 end if;
4785 when Fully_Conformant =>
4786 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4787 Error_Msg_N -- CODEFIX
4788 ("not fully conformant with operation inherited#!",
4789 Enode);
4790 else
4791 Error_Msg_N -- CODEFIX
4792 ("not fully conformant with declaration#!", Enode);
4793 end if;
4794 end case;
4796 Error_Msg_NE (Msg, Enode, N);
4797 end if;
4798 end Conformance_Error;
4800 -- Local Variables
4802 Old_Type : constant Entity_Id := Etype (Old_Id);
4803 New_Type : constant Entity_Id := Etype (New_Id);
4804 Old_Formal : Entity_Id;
4805 New_Formal : Entity_Id;
4806 Access_Types_Match : Boolean;
4807 Old_Formal_Base : Entity_Id;
4808 New_Formal_Base : Entity_Id;
4810 -- Start of processing for Check_Conformance
4812 begin
4813 Conforms := True;
4815 -- We need a special case for operators, since they don't appear
4816 -- explicitly.
4818 if Ctype = Type_Conformant then
4819 if Ekind (New_Id) = E_Operator
4820 and then Operator_Matches_Spec (New_Id, Old_Id)
4821 then
4822 return;
4823 end if;
4824 end if;
4826 -- If both are functions/operators, check return types conform
4828 if Old_Type /= Standard_Void_Type
4829 and then
4830 New_Type /= Standard_Void_Type
4831 then
4832 -- If we are checking interface conformance we omit controlling
4833 -- arguments and result, because we are only checking the conformance
4834 -- of the remaining parameters.
4836 if Has_Controlling_Result (Old_Id)
4837 and then Has_Controlling_Result (New_Id)
4838 and then Skip_Controlling_Formals
4839 then
4840 null;
4842 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4843 if Ctype >= Subtype_Conformant
4844 and then not Predicates_Match (Old_Type, New_Type)
4845 then
4846 Conformance_Error
4847 ("\predicate of return type does not match!", New_Id);
4848 else
4849 Conformance_Error
4850 ("\return type does not match!", New_Id);
4851 end if;
4853 return;
4854 end if;
4856 -- Ada 2005 (AI-231): In case of anonymous access types check the
4857 -- null-exclusion and access-to-constant attributes match.
4859 if Ada_Version >= Ada_2005
4860 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4861 and then
4862 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4863 or else Is_Access_Constant (Etype (Old_Type)) /=
4864 Is_Access_Constant (Etype (New_Type)))
4865 then
4866 Conformance_Error ("\return type does not match!", New_Id);
4867 return;
4868 end if;
4870 -- If either is a function/operator and the other isn't, error
4872 elsif Old_Type /= Standard_Void_Type
4873 or else New_Type /= Standard_Void_Type
4874 then
4875 Conformance_Error ("\functions can only match functions!", New_Id);
4876 return;
4877 end if;
4879 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4880 -- If this is a renaming as body, refine error message to indicate that
4881 -- the conflict is with the original declaration. If the entity is not
4882 -- frozen, the conventions don't have to match, the one of the renamed
4883 -- entity is inherited.
4885 if Ctype >= Subtype_Conformant then
4886 if Convention (Old_Id) /= Convention (New_Id) then
4887 if not Is_Frozen (New_Id) then
4888 null;
4890 elsif Present (Err_Loc)
4891 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4892 and then Present (Corresponding_Spec (Err_Loc))
4893 then
4894 Error_Msg_Name_1 := Chars (New_Id);
4895 Error_Msg_Name_2 :=
4896 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4897 Conformance_Error ("\prior declaration for% has convention %!");
4899 else
4900 Conformance_Error ("\calling conventions do not match!");
4901 end if;
4903 return;
4905 elsif Is_Formal_Subprogram (Old_Id)
4906 or else Is_Formal_Subprogram (New_Id)
4907 then
4908 Conformance_Error ("\formal subprograms not allowed!");
4909 return;
4911 -- Pragma Ghost behaves as a convention in the context of subtype
4912 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4913 -- subprograms as their spec may reside in a Ghost region and their
4914 -- body not, or vice versa.
4916 elsif Comes_From_Source (Old_Id)
4917 and then Comes_From_Source (New_Id)
4918 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4919 then
4920 Conformance_Error ("\ghost modes do not match!");
4921 return;
4922 end if;
4923 end if;
4925 -- Deal with parameters
4927 -- Note: we use the entity information, rather than going directly
4928 -- to the specification in the tree. This is not only simpler, but
4929 -- absolutely necessary for some cases of conformance tests between
4930 -- operators, where the declaration tree simply does not exist.
4932 Old_Formal := First_Formal (Old_Id);
4933 New_Formal := First_Formal (New_Id);
4934 while Present (Old_Formal) and then Present (New_Formal) loop
4935 if Is_Controlling_Formal (Old_Formal)
4936 and then Is_Controlling_Formal (New_Formal)
4937 and then Skip_Controlling_Formals
4938 then
4939 -- The controlling formals will have different types when
4940 -- comparing an interface operation with its match, but both
4941 -- or neither must be access parameters.
4943 if Is_Access_Type (Etype (Old_Formal))
4945 Is_Access_Type (Etype (New_Formal))
4946 then
4947 goto Skip_Controlling_Formal;
4948 else
4949 Conformance_Error
4950 ("\access parameter does not match!", New_Formal);
4951 end if;
4952 end if;
4954 -- Ada 2012: Mode conformance also requires that formal parameters
4955 -- be both aliased, or neither.
4957 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4958 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4959 Conformance_Error
4960 ("\aliased parameter mismatch!", New_Formal);
4961 end if;
4962 end if;
4964 if Ctype = Fully_Conformant then
4966 -- Names must match. Error message is more accurate if we do
4967 -- this before checking that the types of the formals match.
4969 if Chars (Old_Formal) /= Chars (New_Formal) then
4970 Conformance_Error ("\name& does not match!", New_Formal);
4972 -- Set error posted flag on new formal as well to stop
4973 -- junk cascaded messages in some cases.
4975 Set_Error_Posted (New_Formal);
4976 return;
4977 end if;
4979 -- Null exclusion must match
4981 if Null_Exclusion_Present (Parent (Old_Formal))
4983 Null_Exclusion_Present (Parent (New_Formal))
4984 then
4985 -- Only give error if both come from source. This should be
4986 -- investigated some time, since it should not be needed ???
4988 if Comes_From_Source (Old_Formal)
4989 and then
4990 Comes_From_Source (New_Formal)
4991 then
4992 Conformance_Error
4993 ("\null exclusion for& does not match", New_Formal);
4995 -- Mark error posted on the new formal to avoid duplicated
4996 -- complaint about types not matching.
4998 Set_Error_Posted (New_Formal);
4999 end if;
5000 end if;
5001 end if;
5003 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5004 -- case occurs whenever a subprogram is being renamed and one of its
5005 -- parameters imposes a null exclusion. For example:
5007 -- type T is null record;
5008 -- type Acc_T is access T;
5009 -- subtype Acc_T_Sub is Acc_T;
5011 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5012 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5013 -- renames P;
5015 Old_Formal_Base := Etype (Old_Formal);
5016 New_Formal_Base := Etype (New_Formal);
5018 if Get_Inst then
5019 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5020 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5021 end if;
5023 Access_Types_Match := Ada_Version >= Ada_2005
5025 -- Ensure that this rule is only applied when New_Id is a
5026 -- renaming of Old_Id.
5028 and then Nkind (Parent (Parent (New_Id))) =
5029 N_Subprogram_Renaming_Declaration
5030 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5031 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5032 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5034 -- Now handle the allowed access-type case
5036 and then Is_Access_Type (Old_Formal_Base)
5037 and then Is_Access_Type (New_Formal_Base)
5039 -- The type kinds must match. The only exception occurs with
5040 -- multiple generics of the form:
5042 -- generic generic
5043 -- type F is private; type A is private;
5044 -- type F_Ptr is access F; type A_Ptr is access A;
5045 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5046 -- package F_Pack is ... package A_Pack is
5047 -- package F_Inst is
5048 -- new F_Pack (A, A_Ptr, A_P);
5050 -- When checking for conformance between the parameters of A_P
5051 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5052 -- because the compiler has transformed A_Ptr into a subtype of
5053 -- F_Ptr. We catch this case in the code below.
5055 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5056 or else
5057 (Is_Generic_Type (Old_Formal_Base)
5058 and then Is_Generic_Type (New_Formal_Base)
5059 and then Is_Internal (New_Formal_Base)
5060 and then Etype (Etype (New_Formal_Base)) =
5061 Old_Formal_Base))
5062 and then Directly_Designated_Type (Old_Formal_Base) =
5063 Directly_Designated_Type (New_Formal_Base)
5064 and then ((Is_Itype (Old_Formal_Base)
5065 and then Can_Never_Be_Null (Old_Formal_Base))
5066 or else
5067 (Is_Itype (New_Formal_Base)
5068 and then Can_Never_Be_Null (New_Formal_Base)));
5070 -- Types must always match. In the visible part of an instance,
5071 -- usual overloading rules for dispatching operations apply, and
5072 -- we check base types (not the actual subtypes).
5074 if In_Instance_Visible_Part
5075 and then Is_Dispatching_Operation (New_Id)
5076 then
5077 if not Conforming_Types
5078 (T1 => Base_Type (Etype (Old_Formal)),
5079 T2 => Base_Type (Etype (New_Formal)),
5080 Ctype => Ctype,
5081 Get_Inst => Get_Inst)
5082 and then not Access_Types_Match
5083 then
5084 Conformance_Error ("\type of & does not match!", New_Formal);
5085 return;
5086 end if;
5088 elsif not Conforming_Types
5089 (T1 => Old_Formal_Base,
5090 T2 => New_Formal_Base,
5091 Ctype => Ctype,
5092 Get_Inst => Get_Inst)
5093 and then not Access_Types_Match
5094 then
5095 -- Don't give error message if old type is Any_Type. This test
5096 -- avoids some cascaded errors, e.g. in case of a bad spec.
5098 if Errmsg and then Old_Formal_Base = Any_Type then
5099 Conforms := False;
5100 else
5101 if Ctype >= Subtype_Conformant
5102 and then
5103 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5104 then
5105 Conformance_Error
5106 ("\predicate of & does not match!", New_Formal);
5107 else
5108 Conformance_Error
5109 ("\type of & does not match!", New_Formal);
5110 end if;
5111 end if;
5113 return;
5114 end if;
5116 -- For mode conformance, mode must match
5118 if Ctype >= Mode_Conformant then
5119 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5120 if not Ekind_In (New_Id, E_Function, E_Procedure)
5121 or else not Is_Primitive_Wrapper (New_Id)
5122 then
5123 Conformance_Error ("\mode of & does not match!", New_Formal);
5125 else
5126 declare
5127 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5128 begin
5129 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5130 then
5131 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5132 else
5133 Conformance_Error
5134 ("\mode of & does not match!", New_Formal);
5135 end if;
5136 end;
5137 end if;
5139 return;
5141 -- Part of mode conformance for access types is having the same
5142 -- constant modifier.
5144 elsif Access_Types_Match
5145 and then Is_Access_Constant (Old_Formal_Base) /=
5146 Is_Access_Constant (New_Formal_Base)
5147 then
5148 Conformance_Error
5149 ("\constant modifier does not match!", New_Formal);
5150 return;
5151 end if;
5152 end if;
5154 if Ctype >= Subtype_Conformant then
5156 -- Ada 2005 (AI-231): In case of anonymous access types check
5157 -- the null-exclusion and access-to-constant attributes must
5158 -- match. For null exclusion, we test the types rather than the
5159 -- formals themselves, since the attribute is only set reliably
5160 -- on the formals in the Ada 95 case, and we exclude the case
5161 -- where Old_Formal is marked as controlling, to avoid errors
5162 -- when matching completing bodies with dispatching declarations
5163 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5165 if Ada_Version >= Ada_2005
5166 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5167 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5168 and then
5169 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5170 Can_Never_Be_Null (Etype (New_Formal))
5171 and then
5172 not Is_Controlling_Formal (Old_Formal))
5173 or else
5174 Is_Access_Constant (Etype (Old_Formal)) /=
5175 Is_Access_Constant (Etype (New_Formal)))
5177 -- Do not complain if error already posted on New_Formal. This
5178 -- avoids some redundant error messages.
5180 and then not Error_Posted (New_Formal)
5181 then
5182 -- It is allowed to omit the null-exclusion in case of stream
5183 -- attribute subprograms. We recognize stream subprograms
5184 -- through their TSS-generated suffix.
5186 declare
5187 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5189 begin
5190 if TSS_Name /= TSS_Stream_Read
5191 and then TSS_Name /= TSS_Stream_Write
5192 and then TSS_Name /= TSS_Stream_Input
5193 and then TSS_Name /= TSS_Stream_Output
5194 then
5195 -- Here we have a definite conformance error. It is worth
5196 -- special casing the error message for the case of a
5197 -- controlling formal (which excludes null).
5199 if Is_Controlling_Formal (New_Formal) then
5200 Error_Msg_Node_2 := Scope (New_Formal);
5201 Conformance_Error
5202 ("\controlling formal & of & excludes null, "
5203 & "declaration must exclude null as well",
5204 New_Formal);
5206 -- Normal case (couldn't we give more detail here???)
5208 else
5209 Conformance_Error
5210 ("\type of & does not match!", New_Formal);
5211 end if;
5213 return;
5214 end if;
5215 end;
5216 end if;
5217 end if;
5219 -- Full conformance checks
5221 if Ctype = Fully_Conformant then
5223 -- We have checked already that names match
5225 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5227 -- Check default expressions for in parameters
5229 declare
5230 NewD : constant Boolean :=
5231 Present (Default_Value (New_Formal));
5232 OldD : constant Boolean :=
5233 Present (Default_Value (Old_Formal));
5234 begin
5235 if NewD or OldD then
5237 -- The old default value has been analyzed because the
5238 -- current full declaration will have frozen everything
5239 -- before. The new default value has not been analyzed,
5240 -- so analyze it now before we check for conformance.
5242 if NewD then
5243 Push_Scope (New_Id);
5244 Preanalyze_Spec_Expression
5245 (Default_Value (New_Formal), Etype (New_Formal));
5246 End_Scope;
5247 end if;
5249 if not (NewD and OldD)
5250 or else not Fully_Conformant_Expressions
5251 (Default_Value (Old_Formal),
5252 Default_Value (New_Formal))
5253 then
5254 Conformance_Error
5255 ("\default expression for & does not match!",
5256 New_Formal);
5257 return;
5258 end if;
5259 end if;
5260 end;
5261 end if;
5262 end if;
5264 -- A couple of special checks for Ada 83 mode. These checks are
5265 -- skipped if either entity is an operator in package Standard,
5266 -- or if either old or new instance is not from the source program.
5268 if Ada_Version = Ada_83
5269 and then Sloc (Old_Id) > Standard_Location
5270 and then Sloc (New_Id) > Standard_Location
5271 and then Comes_From_Source (Old_Id)
5272 and then Comes_From_Source (New_Id)
5273 then
5274 declare
5275 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5276 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5278 begin
5279 -- Explicit IN must be present or absent in both cases. This
5280 -- test is required only in the full conformance case.
5282 if In_Present (Old_Param) /= In_Present (New_Param)
5283 and then Ctype = Fully_Conformant
5284 then
5285 Conformance_Error
5286 ("\(Ada 83) IN must appear in both declarations",
5287 New_Formal);
5288 return;
5289 end if;
5291 -- Grouping (use of comma in param lists) must be the same
5292 -- This is where we catch a misconformance like:
5294 -- A, B : Integer
5295 -- A : Integer; B : Integer
5297 -- which are represented identically in the tree except
5298 -- for the setting of the flags More_Ids and Prev_Ids.
5300 if More_Ids (Old_Param) /= More_Ids (New_Param)
5301 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5302 then
5303 Conformance_Error
5304 ("\grouping of & does not match!", New_Formal);
5305 return;
5306 end if;
5307 end;
5308 end if;
5310 -- This label is required when skipping controlling formals
5312 <<Skip_Controlling_Formal>>
5314 Next_Formal (Old_Formal);
5315 Next_Formal (New_Formal);
5316 end loop;
5318 if Present (Old_Formal) then
5319 Conformance_Error ("\too few parameters!");
5320 return;
5322 elsif Present (New_Formal) then
5323 Conformance_Error ("\too many parameters!", New_Formal);
5324 return;
5325 end if;
5326 end Check_Conformance;
5328 -----------------------
5329 -- Check_Conventions --
5330 -----------------------
5332 procedure Check_Conventions (Typ : Entity_Id) is
5333 Ifaces_List : Elist_Id;
5335 procedure Check_Convention (Op : Entity_Id);
5336 -- Verify that the convention of inherited dispatching operation Op is
5337 -- consistent among all subprograms it overrides. In order to minimize
5338 -- the search, Search_From is utilized to designate a specific point in
5339 -- the list rather than iterating over the whole list once more.
5341 ----------------------
5342 -- Check_Convention --
5343 ----------------------
5345 procedure Check_Convention (Op : Entity_Id) is
5346 Op_Conv : constant Convention_Id := Convention (Op);
5347 Iface_Conv : Convention_Id;
5348 Iface_Elmt : Elmt_Id;
5349 Iface_Prim_Elmt : Elmt_Id;
5350 Iface_Prim : Entity_Id;
5352 begin
5353 Iface_Elmt := First_Elmt (Ifaces_List);
5354 while Present (Iface_Elmt) loop
5355 Iface_Prim_Elmt :=
5356 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5357 while Present (Iface_Prim_Elmt) loop
5358 Iface_Prim := Node (Iface_Prim_Elmt);
5359 Iface_Conv := Convention (Iface_Prim);
5361 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5362 and then Iface_Conv /= Op_Conv
5363 then
5364 Error_Msg_N
5365 ("inconsistent conventions in primitive operations", Typ);
5367 Error_Msg_Name_1 := Chars (Op);
5368 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5369 Error_Msg_Sloc := Sloc (Op);
5371 if Comes_From_Source (Op) or else No (Alias (Op)) then
5372 if not Present (Overridden_Operation (Op)) then
5373 Error_Msg_N ("\\primitive % defined #", Typ);
5374 else
5375 Error_Msg_N
5376 ("\\overriding operation % with "
5377 & "convention % defined #", Typ);
5378 end if;
5380 else pragma Assert (Present (Alias (Op)));
5381 Error_Msg_Sloc := Sloc (Alias (Op));
5382 Error_Msg_N ("\\inherited operation % with "
5383 & "convention % defined #", Typ);
5384 end if;
5386 Error_Msg_Name_1 := Chars (Op);
5387 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5388 Error_Msg_Sloc := Sloc (Iface_Prim);
5389 Error_Msg_N ("\\overridden operation % with "
5390 & "convention % defined #", Typ);
5392 -- Avoid cascading errors
5394 return;
5395 end if;
5397 Next_Elmt (Iface_Prim_Elmt);
5398 end loop;
5400 Next_Elmt (Iface_Elmt);
5401 end loop;
5402 end Check_Convention;
5404 -- Local variables
5406 Prim_Op : Entity_Id;
5407 Prim_Op_Elmt : Elmt_Id;
5409 -- Start of processing for Check_Conventions
5411 begin
5412 if not Has_Interfaces (Typ) then
5413 return;
5414 end if;
5416 Collect_Interfaces (Typ, Ifaces_List);
5418 -- The algorithm checks every overriding dispatching operation against
5419 -- all the corresponding overridden dispatching operations, detecting
5420 -- differences in conventions.
5422 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5423 while Present (Prim_Op_Elmt) loop
5424 Prim_Op := Node (Prim_Op_Elmt);
5426 -- A small optimization: skip the predefined dispatching operations
5427 -- since they always have the same convention.
5429 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5430 Check_Convention (Prim_Op);
5431 end if;
5433 Next_Elmt (Prim_Op_Elmt);
5434 end loop;
5435 end Check_Conventions;
5437 ------------------------------
5438 -- Check_Delayed_Subprogram --
5439 ------------------------------
5441 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5442 F : Entity_Id;
5444 procedure Possible_Freeze (T : Entity_Id);
5445 -- T is the type of either a formal parameter or of the return type.
5446 -- If T is not yet frozen and needs a delayed freeze, then the
5447 -- subprogram itself must be delayed. If T is the limited view of an
5448 -- incomplete type the subprogram must be frozen as well, because
5449 -- T may depend on local types that have not been frozen yet.
5451 ---------------------
5452 -- Possible_Freeze --
5453 ---------------------
5455 procedure Possible_Freeze (T : Entity_Id) is
5456 begin
5457 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5458 Set_Has_Delayed_Freeze (Designator);
5460 elsif Is_Access_Type (T)
5461 and then Has_Delayed_Freeze (Designated_Type (T))
5462 and then not Is_Frozen (Designated_Type (T))
5463 then
5464 Set_Has_Delayed_Freeze (Designator);
5466 elsif Ekind (T) = E_Incomplete_Type
5467 and then From_Limited_With (T)
5468 then
5469 Set_Has_Delayed_Freeze (Designator);
5471 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5472 -- of a subprogram or entry declaration.
5474 elsif Ekind (T) = E_Incomplete_Type
5475 and then Ada_Version >= Ada_2012
5476 then
5477 Set_Has_Delayed_Freeze (Designator);
5478 end if;
5480 end Possible_Freeze;
5482 -- Start of processing for Check_Delayed_Subprogram
5484 begin
5485 -- All subprograms, including abstract subprograms, may need a freeze
5486 -- node if some formal type or the return type needs one.
5488 Possible_Freeze (Etype (Designator));
5489 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5491 -- Need delayed freeze if any of the formal types themselves need
5492 -- a delayed freeze and are not yet frozen.
5494 F := First_Formal (Designator);
5495 while Present (F) loop
5496 Possible_Freeze (Etype (F));
5497 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5498 Next_Formal (F);
5499 end loop;
5501 -- Mark functions that return by reference. Note that it cannot be
5502 -- done for delayed_freeze subprograms because the underlying
5503 -- returned type may not be known yet (for private types)
5505 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5506 declare
5507 Typ : constant Entity_Id := Etype (Designator);
5508 Utyp : constant Entity_Id := Underlying_Type (Typ);
5509 begin
5510 if Is_Limited_View (Typ) then
5511 Set_Returns_By_Ref (Designator);
5512 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5513 Set_Returns_By_Ref (Designator);
5514 end if;
5515 end;
5516 end if;
5517 end Check_Delayed_Subprogram;
5519 ------------------------------------
5520 -- Check_Discriminant_Conformance --
5521 ------------------------------------
5523 procedure Check_Discriminant_Conformance
5524 (N : Node_Id;
5525 Prev : Entity_Id;
5526 Prev_Loc : Node_Id)
5528 Old_Discr : Entity_Id := First_Discriminant (Prev);
5529 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5530 New_Discr_Id : Entity_Id;
5531 New_Discr_Type : Entity_Id;
5533 procedure Conformance_Error (Msg : String; N : Node_Id);
5534 -- Post error message for conformance error on given node. Two messages
5535 -- are output. The first points to the previous declaration with a
5536 -- general "no conformance" message. The second is the detailed reason,
5537 -- supplied as Msg. The parameter N provide information for a possible
5538 -- & insertion in the message.
5540 -----------------------
5541 -- Conformance_Error --
5542 -----------------------
5544 procedure Conformance_Error (Msg : String; N : Node_Id) is
5545 begin
5546 Error_Msg_Sloc := Sloc (Prev_Loc);
5547 Error_Msg_N -- CODEFIX
5548 ("not fully conformant with declaration#!", N);
5549 Error_Msg_NE (Msg, N, N);
5550 end Conformance_Error;
5552 -- Start of processing for Check_Discriminant_Conformance
5554 begin
5555 while Present (Old_Discr) and then Present (New_Discr) loop
5556 New_Discr_Id := Defining_Identifier (New_Discr);
5558 -- The subtype mark of the discriminant on the full type has not
5559 -- been analyzed so we do it here. For an access discriminant a new
5560 -- type is created.
5562 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5563 New_Discr_Type :=
5564 Access_Definition (N, Discriminant_Type (New_Discr));
5566 else
5567 Analyze (Discriminant_Type (New_Discr));
5568 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5570 -- Ada 2005: if the discriminant definition carries a null
5571 -- exclusion, create an itype to check properly for consistency
5572 -- with partial declaration.
5574 if Is_Access_Type (New_Discr_Type)
5575 and then Null_Exclusion_Present (New_Discr)
5576 then
5577 New_Discr_Type :=
5578 Create_Null_Excluding_Itype
5579 (T => New_Discr_Type,
5580 Related_Nod => New_Discr,
5581 Scope_Id => Current_Scope);
5582 end if;
5583 end if;
5585 if not Conforming_Types
5586 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5587 then
5588 Conformance_Error ("type of & does not match!", New_Discr_Id);
5589 return;
5590 else
5591 -- Treat the new discriminant as an occurrence of the old one,
5592 -- for navigation purposes, and fill in some semantic
5593 -- information, for completeness.
5595 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5596 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5597 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5598 end if;
5600 -- Names must match
5602 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5603 Conformance_Error ("name & does not match!", New_Discr_Id);
5604 return;
5605 end if;
5607 -- Default expressions must match
5609 declare
5610 NewD : constant Boolean :=
5611 Present (Expression (New_Discr));
5612 OldD : constant Boolean :=
5613 Present (Expression (Parent (Old_Discr)));
5615 begin
5616 if NewD or OldD then
5618 -- The old default value has been analyzed and expanded,
5619 -- because the current full declaration will have frozen
5620 -- everything before. The new default values have not been
5621 -- expanded, so expand now to check conformance.
5623 if NewD then
5624 Preanalyze_Spec_Expression
5625 (Expression (New_Discr), New_Discr_Type);
5626 end if;
5628 if not (NewD and OldD)
5629 or else not Fully_Conformant_Expressions
5630 (Expression (Parent (Old_Discr)),
5631 Expression (New_Discr))
5633 then
5634 Conformance_Error
5635 ("default expression for & does not match!",
5636 New_Discr_Id);
5637 return;
5638 end if;
5639 end if;
5640 end;
5642 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5644 if Ada_Version = Ada_83 then
5645 declare
5646 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5648 begin
5649 -- Grouping (use of comma in param lists) must be the same
5650 -- This is where we catch a misconformance like:
5652 -- A, B : Integer
5653 -- A : Integer; B : Integer
5655 -- which are represented identically in the tree except
5656 -- for the setting of the flags More_Ids and Prev_Ids.
5658 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5659 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5660 then
5661 Conformance_Error
5662 ("grouping of & does not match!", New_Discr_Id);
5663 return;
5664 end if;
5665 end;
5666 end if;
5668 Next_Discriminant (Old_Discr);
5669 Next (New_Discr);
5670 end loop;
5672 if Present (Old_Discr) then
5673 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5674 return;
5676 elsif Present (New_Discr) then
5677 Conformance_Error
5678 ("too many discriminants!", Defining_Identifier (New_Discr));
5679 return;
5680 end if;
5681 end Check_Discriminant_Conformance;
5683 ----------------------------
5684 -- Check_Fully_Conformant --
5685 ----------------------------
5687 procedure Check_Fully_Conformant
5688 (New_Id : Entity_Id;
5689 Old_Id : Entity_Id;
5690 Err_Loc : Node_Id := Empty)
5692 Result : Boolean;
5693 pragma Warnings (Off, Result);
5694 begin
5695 Check_Conformance
5696 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5697 end Check_Fully_Conformant;
5699 ---------------------------
5700 -- Check_Mode_Conformant --
5701 ---------------------------
5703 procedure Check_Mode_Conformant
5704 (New_Id : Entity_Id;
5705 Old_Id : Entity_Id;
5706 Err_Loc : Node_Id := Empty;
5707 Get_Inst : Boolean := False)
5709 Result : Boolean;
5710 pragma Warnings (Off, Result);
5711 begin
5712 Check_Conformance
5713 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5714 end Check_Mode_Conformant;
5716 --------------------------------
5717 -- Check_Overriding_Indicator --
5718 --------------------------------
5720 procedure Check_Overriding_Indicator
5721 (Subp : Entity_Id;
5722 Overridden_Subp : Entity_Id;
5723 Is_Primitive : Boolean)
5725 Decl : Node_Id;
5726 Spec : Node_Id;
5728 begin
5729 -- No overriding indicator for literals
5731 if Ekind (Subp) = E_Enumeration_Literal then
5732 return;
5734 elsif Ekind (Subp) = E_Entry then
5735 Decl := Parent (Subp);
5737 -- No point in analyzing a malformed operator
5739 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5740 and then Error_Posted (Subp)
5741 then
5742 return;
5744 else
5745 Decl := Unit_Declaration_Node (Subp);
5746 end if;
5748 if Nkind_In (Decl, N_Subprogram_Body,
5749 N_Subprogram_Body_Stub,
5750 N_Subprogram_Declaration,
5751 N_Abstract_Subprogram_Declaration,
5752 N_Subprogram_Renaming_Declaration)
5753 then
5754 Spec := Specification (Decl);
5756 elsif Nkind (Decl) = N_Entry_Declaration then
5757 Spec := Decl;
5759 else
5760 return;
5761 end if;
5763 -- The overriding operation is type conformant with the overridden one,
5764 -- but the names of the formals are not required to match. If the names
5765 -- appear permuted in the overriding operation, this is a possible
5766 -- source of confusion that is worth diagnosing. Controlling formals
5767 -- often carry names that reflect the type, and it is not worthwhile
5768 -- requiring that their names match.
5770 if Present (Overridden_Subp)
5771 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5772 then
5773 declare
5774 Form1 : Entity_Id;
5775 Form2 : Entity_Id;
5777 begin
5778 Form1 := First_Formal (Subp);
5779 Form2 := First_Formal (Overridden_Subp);
5781 -- If the overriding operation is a synchronized operation, skip
5782 -- the first parameter of the overridden operation, which is
5783 -- implicit in the new one. If the operation is declared in the
5784 -- body it is not primitive and all formals must match.
5786 if Is_Concurrent_Type (Scope (Subp))
5787 and then Is_Tagged_Type (Scope (Subp))
5788 and then not Has_Completion (Scope (Subp))
5789 then
5790 Form2 := Next_Formal (Form2);
5791 end if;
5793 if Present (Form1) then
5794 Form1 := Next_Formal (Form1);
5795 Form2 := Next_Formal (Form2);
5796 end if;
5798 while Present (Form1) loop
5799 if not Is_Controlling_Formal (Form1)
5800 and then Present (Next_Formal (Form2))
5801 and then Chars (Form1) = Chars (Next_Formal (Form2))
5802 then
5803 Error_Msg_Node_2 := Alias (Overridden_Subp);
5804 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5805 Error_Msg_NE
5806 ("& does not match corresponding formal of&#",
5807 Form1, Form1);
5808 exit;
5809 end if;
5811 Next_Formal (Form1);
5812 Next_Formal (Form2);
5813 end loop;
5814 end;
5815 end if;
5817 -- If there is an overridden subprogram, then check that there is no
5818 -- "not overriding" indicator, and mark the subprogram as overriding.
5819 -- This is not done if the overridden subprogram is marked as hidden,
5820 -- which can occur for the case of inherited controlled operations
5821 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5822 -- subprogram is not itself hidden. (Note: This condition could probably
5823 -- be simplified, leaving out the testing for the specific controlled
5824 -- cases, but it seems safer and clearer this way, and echoes similar
5825 -- special-case tests of this kind in other places.)
5827 if Present (Overridden_Subp)
5828 and then (not Is_Hidden (Overridden_Subp)
5829 or else
5830 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5831 Name_Adjust,
5832 Name_Finalize)
5833 and then Present (Alias (Overridden_Subp))
5834 and then not Is_Hidden (Alias (Overridden_Subp))))
5835 then
5836 if Must_Not_Override (Spec) then
5837 Error_Msg_Sloc := Sloc (Overridden_Subp);
5839 if Ekind (Subp) = E_Entry then
5840 Error_Msg_NE
5841 ("entry & overrides inherited operation #", Spec, Subp);
5842 else
5843 Error_Msg_NE
5844 ("subprogram & overrides inherited operation #", Spec, Subp);
5845 end if;
5847 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5848 -- as an extension of Root_Controlled, and thus has a useless Adjust
5849 -- operation. This operation should not be inherited by other limited
5850 -- controlled types. An explicit Adjust for them is not overriding.
5852 elsif Must_Override (Spec)
5853 and then Chars (Overridden_Subp) = Name_Adjust
5854 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5855 and then Present (Alias (Overridden_Subp))
5856 and then
5857 Is_Predefined_File_Name
5858 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5859 then
5860 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5862 elsif Is_Subprogram (Subp) then
5863 if Is_Init_Proc (Subp) then
5864 null;
5866 elsif No (Overridden_Operation (Subp)) then
5868 -- For entities generated by Derive_Subprograms the overridden
5869 -- operation is the inherited primitive (which is available
5870 -- through the attribute alias)
5872 if (Is_Dispatching_Operation (Subp)
5873 or else Is_Dispatching_Operation (Overridden_Subp))
5874 and then not Comes_From_Source (Overridden_Subp)
5875 and then Find_Dispatching_Type (Overridden_Subp) =
5876 Find_Dispatching_Type (Subp)
5877 and then Present (Alias (Overridden_Subp))
5878 and then Comes_From_Source (Alias (Overridden_Subp))
5879 then
5880 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5881 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5883 else
5884 Set_Overridden_Operation (Subp, Overridden_Subp);
5885 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5886 end if;
5887 end if;
5888 end if;
5890 -- If primitive flag is set or this is a protected operation, then
5891 -- the operation is overriding at the point of its declaration, so
5892 -- warn if necessary. Otherwise it may have been declared before the
5893 -- operation it overrides and no check is required.
5895 if Style_Check
5896 and then not Must_Override (Spec)
5897 and then (Is_Primitive
5898 or else Ekind (Scope (Subp)) = E_Protected_Type)
5899 then
5900 Style.Missing_Overriding (Decl, Subp);
5901 end if;
5903 -- If Subp is an operator, it may override a predefined operation, if
5904 -- it is defined in the same scope as the type to which it applies.
5905 -- In that case Overridden_Subp is empty because of our implicit
5906 -- representation for predefined operators. We have to check whether the
5907 -- signature of Subp matches that of a predefined operator. Note that
5908 -- first argument provides the name of the operator, and the second
5909 -- argument the signature that may match that of a standard operation.
5910 -- If the indicator is overriding, then the operator must match a
5911 -- predefined signature, because we know already that there is no
5912 -- explicit overridden operation.
5914 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5915 if Must_Not_Override (Spec) then
5917 -- If this is not a primitive or a protected subprogram, then
5918 -- "not overriding" is illegal.
5920 if not Is_Primitive
5921 and then Ekind (Scope (Subp)) /= E_Protected_Type
5922 then
5923 Error_Msg_N ("overriding indicator only allowed "
5924 & "if subprogram is primitive", Subp);
5926 elsif Can_Override_Operator (Subp) then
5927 Error_Msg_NE
5928 ("subprogram& overrides predefined operator ", Spec, Subp);
5929 end if;
5931 elsif Must_Override (Spec) then
5932 if No (Overridden_Operation (Subp))
5933 and then not Can_Override_Operator (Subp)
5934 then
5935 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5936 end if;
5938 elsif not Error_Posted (Subp)
5939 and then Style_Check
5940 and then Can_Override_Operator (Subp)
5941 and then
5942 not Is_Predefined_File_Name
5943 (Unit_File_Name (Get_Source_Unit (Subp)))
5944 then
5945 -- If style checks are enabled, indicate that the indicator is
5946 -- missing. However, at the point of declaration, the type of
5947 -- which this is a primitive operation may be private, in which
5948 -- case the indicator would be premature.
5950 if Has_Private_Declaration (Etype (Subp))
5951 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5952 then
5953 null;
5954 else
5955 Style.Missing_Overriding (Decl, Subp);
5956 end if;
5957 end if;
5959 elsif Must_Override (Spec) then
5960 if Ekind (Subp) = E_Entry then
5961 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5962 else
5963 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5964 end if;
5966 -- If the operation is marked "not overriding" and it's not primitive
5967 -- then an error is issued, unless this is an operation of a task or
5968 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5969 -- has been specified have already been checked above.
5971 elsif Must_Not_Override (Spec)
5972 and then not Is_Primitive
5973 and then Ekind (Subp) /= E_Entry
5974 and then Ekind (Scope (Subp)) /= E_Protected_Type
5975 then
5976 Error_Msg_N
5977 ("overriding indicator only allowed if subprogram is primitive",
5978 Subp);
5979 return;
5980 end if;
5981 end Check_Overriding_Indicator;
5983 -------------------
5984 -- Check_Returns --
5985 -------------------
5987 -- Note: this procedure needs to know far too much about how the expander
5988 -- messes with exceptions. The use of the flag Exception_Junk and the
5989 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5990 -- works, but is not very clean. It would be better if the expansion
5991 -- routines would leave Original_Node working nicely, and we could use
5992 -- Original_Node here to ignore all the peculiar expander messing ???
5994 procedure Check_Returns
5995 (HSS : Node_Id;
5996 Mode : Character;
5997 Err : out Boolean;
5998 Proc : Entity_Id := Empty)
6000 Handler : Node_Id;
6002 procedure Check_Statement_Sequence (L : List_Id);
6003 -- Internal recursive procedure to check a list of statements for proper
6004 -- termination by a return statement (or a transfer of control or a
6005 -- compound statement that is itself internally properly terminated).
6007 ------------------------------
6008 -- Check_Statement_Sequence --
6009 ------------------------------
6011 procedure Check_Statement_Sequence (L : List_Id) is
6012 Last_Stm : Node_Id;
6013 Stm : Node_Id;
6014 Kind : Node_Kind;
6016 function Assert_False return Boolean;
6017 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6018 -- rewritten as a null statement when assertions are off. The assert
6019 -- is not active, but it is still enough to kill the warning.
6021 ------------------
6022 -- Assert_False --
6023 ------------------
6025 function Assert_False return Boolean is
6026 Orig : constant Node_Id := Original_Node (Last_Stm);
6028 begin
6029 if Nkind (Orig) = N_Pragma
6030 and then Pragma_Name (Orig) = Name_Assert
6031 and then not Error_Posted (Orig)
6032 then
6033 declare
6034 Arg : constant Node_Id :=
6035 First (Pragma_Argument_Associations (Orig));
6036 Exp : constant Node_Id := Expression (Arg);
6037 begin
6038 return Nkind (Exp) = N_Identifier
6039 and then Chars (Exp) = Name_False;
6040 end;
6042 else
6043 return False;
6044 end if;
6045 end Assert_False;
6047 -- Local variables
6049 Raise_Exception_Call : Boolean;
6050 -- Set True if statement sequence terminated by Raise_Exception call
6051 -- or a Reraise_Occurrence call.
6053 -- Start of processing for Check_Statement_Sequence
6055 begin
6056 Raise_Exception_Call := False;
6058 -- Get last real statement
6060 Last_Stm := Last (L);
6062 -- Deal with digging out exception handler statement sequences that
6063 -- have been transformed by the local raise to goto optimization.
6064 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6065 -- optimization has occurred, we are looking at something like:
6067 -- begin
6068 -- original stmts in block
6070 -- exception \
6071 -- when excep1 => |
6072 -- goto L1; | omitted if No_Exception_Propagation
6073 -- when excep2 => |
6074 -- goto L2; /
6075 -- end;
6077 -- goto L3; -- skip handler when exception not raised
6079 -- <<L1>> -- target label for local exception
6080 -- begin
6081 -- estmts1
6082 -- end;
6084 -- goto L3;
6086 -- <<L2>>
6087 -- begin
6088 -- estmts2
6089 -- end;
6091 -- <<L3>>
6093 -- and what we have to do is to dig out the estmts1 and estmts2
6094 -- sequences (which were the original sequences of statements in
6095 -- the exception handlers) and check them.
6097 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6098 Stm := Last_Stm;
6099 loop
6100 Prev (Stm);
6101 exit when No (Stm);
6102 exit when Nkind (Stm) /= N_Block_Statement;
6103 exit when not Exception_Junk (Stm);
6104 Prev (Stm);
6105 exit when No (Stm);
6106 exit when Nkind (Stm) /= N_Label;
6107 exit when not Exception_Junk (Stm);
6108 Check_Statement_Sequence
6109 (Statements (Handled_Statement_Sequence (Next (Stm))));
6111 Prev (Stm);
6112 Last_Stm := Stm;
6113 exit when No (Stm);
6114 exit when Nkind (Stm) /= N_Goto_Statement;
6115 exit when not Exception_Junk (Stm);
6116 end loop;
6117 end if;
6119 -- Don't count pragmas
6121 while Nkind (Last_Stm) = N_Pragma
6123 -- Don't count call to SS_Release (can happen after Raise_Exception)
6125 or else
6126 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6127 and then
6128 Nkind (Name (Last_Stm)) = N_Identifier
6129 and then
6130 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6132 -- Don't count exception junk
6134 or else
6135 (Nkind_In (Last_Stm, N_Goto_Statement,
6136 N_Label,
6137 N_Object_Declaration)
6138 and then Exception_Junk (Last_Stm))
6139 or else Nkind (Last_Stm) in N_Push_xxx_Label
6140 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6142 -- Inserted code, such as finalization calls, is irrelevant: we only
6143 -- need to check original source.
6145 or else Is_Rewrite_Insertion (Last_Stm)
6146 loop
6147 Prev (Last_Stm);
6148 end loop;
6150 -- Here we have the "real" last statement
6152 Kind := Nkind (Last_Stm);
6154 -- Transfer of control, OK. Note that in the No_Return procedure
6155 -- case, we already diagnosed any explicit return statements, so
6156 -- we can treat them as OK in this context.
6158 if Is_Transfer (Last_Stm) then
6159 return;
6161 -- Check cases of explicit non-indirect procedure calls
6163 elsif Kind = N_Procedure_Call_Statement
6164 and then Is_Entity_Name (Name (Last_Stm))
6165 then
6166 -- Check call to Raise_Exception procedure which is treated
6167 -- specially, as is a call to Reraise_Occurrence.
6169 -- We suppress the warning in these cases since it is likely that
6170 -- the programmer really does not expect to deal with the case
6171 -- of Null_Occurrence, and thus would find a warning about a
6172 -- missing return curious, and raising Program_Error does not
6173 -- seem such a bad behavior if this does occur.
6175 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6176 -- behavior will be to raise Constraint_Error (see AI-329).
6178 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6179 or else
6180 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6181 then
6182 Raise_Exception_Call := True;
6184 -- For Raise_Exception call, test first argument, if it is
6185 -- an attribute reference for a 'Identity call, then we know
6186 -- that the call cannot possibly return.
6188 declare
6189 Arg : constant Node_Id :=
6190 Original_Node (First_Actual (Last_Stm));
6191 begin
6192 if Nkind (Arg) = N_Attribute_Reference
6193 and then Attribute_Name (Arg) = Name_Identity
6194 then
6195 return;
6196 end if;
6197 end;
6198 end if;
6200 -- If statement, need to look inside if there is an else and check
6201 -- each constituent statement sequence for proper termination.
6203 elsif Kind = N_If_Statement
6204 and then Present (Else_Statements (Last_Stm))
6205 then
6206 Check_Statement_Sequence (Then_Statements (Last_Stm));
6207 Check_Statement_Sequence (Else_Statements (Last_Stm));
6209 if Present (Elsif_Parts (Last_Stm)) then
6210 declare
6211 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6213 begin
6214 while Present (Elsif_Part) loop
6215 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6216 Next (Elsif_Part);
6217 end loop;
6218 end;
6219 end if;
6221 return;
6223 -- Case statement, check each case for proper termination
6225 elsif Kind = N_Case_Statement then
6226 declare
6227 Case_Alt : Node_Id;
6228 begin
6229 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6230 while Present (Case_Alt) loop
6231 Check_Statement_Sequence (Statements (Case_Alt));
6232 Next_Non_Pragma (Case_Alt);
6233 end loop;
6234 end;
6236 return;
6238 -- Block statement, check its handled sequence of statements
6240 elsif Kind = N_Block_Statement then
6241 declare
6242 Err1 : Boolean;
6244 begin
6245 Check_Returns
6246 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6248 if Err1 then
6249 Err := True;
6250 end if;
6252 return;
6253 end;
6255 -- Loop statement. If there is an iteration scheme, we can definitely
6256 -- fall out of the loop. Similarly if there is an exit statement, we
6257 -- can fall out. In either case we need a following return.
6259 elsif Kind = N_Loop_Statement then
6260 if Present (Iteration_Scheme (Last_Stm))
6261 or else Has_Exit (Entity (Identifier (Last_Stm)))
6262 then
6263 null;
6265 -- A loop with no exit statement or iteration scheme is either
6266 -- an infinite loop, or it has some other exit (raise/return).
6267 -- In either case, no warning is required.
6269 else
6270 return;
6271 end if;
6273 -- Timed entry call, check entry call and delay alternatives
6275 -- Note: in expanded code, the timed entry call has been converted
6276 -- to a set of expanded statements on which the check will work
6277 -- correctly in any case.
6279 elsif Kind = N_Timed_Entry_Call then
6280 declare
6281 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6282 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6284 begin
6285 -- If statement sequence of entry call alternative is missing,
6286 -- then we can definitely fall through, and we post the error
6287 -- message on the entry call alternative itself.
6289 if No (Statements (ECA)) then
6290 Last_Stm := ECA;
6292 -- If statement sequence of delay alternative is missing, then
6293 -- we can definitely fall through, and we post the error
6294 -- message on the delay alternative itself.
6296 -- Note: if both ECA and DCA are missing the return, then we
6297 -- post only one message, should be enough to fix the bugs.
6298 -- If not we will get a message next time on the DCA when the
6299 -- ECA is fixed.
6301 elsif No (Statements (DCA)) then
6302 Last_Stm := DCA;
6304 -- Else check both statement sequences
6306 else
6307 Check_Statement_Sequence (Statements (ECA));
6308 Check_Statement_Sequence (Statements (DCA));
6309 return;
6310 end if;
6311 end;
6313 -- Conditional entry call, check entry call and else part
6315 -- Note: in expanded code, the conditional entry call has been
6316 -- converted to a set of expanded statements on which the check
6317 -- will work correctly in any case.
6319 elsif Kind = N_Conditional_Entry_Call then
6320 declare
6321 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6323 begin
6324 -- If statement sequence of entry call alternative is missing,
6325 -- then we can definitely fall through, and we post the error
6326 -- message on the entry call alternative itself.
6328 if No (Statements (ECA)) then
6329 Last_Stm := ECA;
6331 -- Else check statement sequence and else part
6333 else
6334 Check_Statement_Sequence (Statements (ECA));
6335 Check_Statement_Sequence (Else_Statements (Last_Stm));
6336 return;
6337 end if;
6338 end;
6339 end if;
6341 -- If we fall through, issue appropriate message
6343 if Mode = 'F' then
6345 -- Kill warning if last statement is a raise exception call,
6346 -- or a pragma Assert (False). Note that with assertions enabled,
6347 -- such a pragma has been converted into a raise exception call
6348 -- already, so the Assert_False is for the assertions off case.
6350 if not Raise_Exception_Call and then not Assert_False then
6352 -- In GNATprove mode, it is an error to have a missing return
6354 Error_Msg_Warn := SPARK_Mode /= On;
6356 -- Issue error message or warning
6358 Error_Msg_N
6359 ("RETURN statement missing following this statement<<!",
6360 Last_Stm);
6361 Error_Msg_N
6362 ("\Program_Error ]<<!", Last_Stm);
6363 end if;
6365 -- Note: we set Err even though we have not issued a warning
6366 -- because we still have a case of a missing return. This is
6367 -- an extremely marginal case, probably will never be noticed
6368 -- but we might as well get it right.
6370 Err := True;
6372 -- Otherwise we have the case of a procedure marked No_Return
6374 else
6375 if not Raise_Exception_Call then
6376 if GNATprove_Mode then
6377 Error_Msg_N
6378 ("implied return after this statement "
6379 & "would have raised Program_Error", Last_Stm);
6380 else
6381 Error_Msg_N
6382 ("implied return after this statement "
6383 & "will raise Program_Error??", Last_Stm);
6384 end if;
6386 Error_Msg_Warn := SPARK_Mode /= On;
6387 Error_Msg_NE
6388 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6389 end if;
6391 declare
6392 RE : constant Node_Id :=
6393 Make_Raise_Program_Error (Sloc (Last_Stm),
6394 Reason => PE_Implicit_Return);
6395 begin
6396 Insert_After (Last_Stm, RE);
6397 Analyze (RE);
6398 end;
6399 end if;
6400 end Check_Statement_Sequence;
6402 -- Start of processing for Check_Returns
6404 begin
6405 Err := False;
6406 Check_Statement_Sequence (Statements (HSS));
6408 if Present (Exception_Handlers (HSS)) then
6409 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6410 while Present (Handler) loop
6411 Check_Statement_Sequence (Statements (Handler));
6412 Next_Non_Pragma (Handler);
6413 end loop;
6414 end if;
6415 end Check_Returns;
6417 ----------------------------
6418 -- Check_Subprogram_Order --
6419 ----------------------------
6421 procedure Check_Subprogram_Order (N : Node_Id) is
6423 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6424 -- This is used to check if S1 > S2 in the sense required by this test,
6425 -- for example nameab < namec, but name2 < name10.
6427 -----------------------------
6428 -- Subprogram_Name_Greater --
6429 -----------------------------
6431 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6432 L1, L2 : Positive;
6433 N1, N2 : Natural;
6435 begin
6436 -- Deal with special case where names are identical except for a
6437 -- numerical suffix. These are handled specially, taking the numeric
6438 -- ordering from the suffix into account.
6440 L1 := S1'Last;
6441 while S1 (L1) in '0' .. '9' loop
6442 L1 := L1 - 1;
6443 end loop;
6445 L2 := S2'Last;
6446 while S2 (L2) in '0' .. '9' loop
6447 L2 := L2 - 1;
6448 end loop;
6450 -- If non-numeric parts non-equal, do straight compare
6452 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6453 return S1 > S2;
6455 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6456 -- that a missing suffix is treated as numeric zero in this test.
6458 else
6459 N1 := 0;
6460 while L1 < S1'Last loop
6461 L1 := L1 + 1;
6462 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6463 end loop;
6465 N2 := 0;
6466 while L2 < S2'Last loop
6467 L2 := L2 + 1;
6468 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6469 end loop;
6471 return N1 > N2;
6472 end if;
6473 end Subprogram_Name_Greater;
6475 -- Start of processing for Check_Subprogram_Order
6477 begin
6478 -- Check body in alpha order if this is option
6480 if Style_Check
6481 and then Style_Check_Order_Subprograms
6482 and then Nkind (N) = N_Subprogram_Body
6483 and then Comes_From_Source (N)
6484 and then In_Extended_Main_Source_Unit (N)
6485 then
6486 declare
6487 LSN : String_Ptr
6488 renames Scope_Stack.Table
6489 (Scope_Stack.Last).Last_Subprogram_Name;
6491 Body_Id : constant Entity_Id :=
6492 Defining_Entity (Specification (N));
6494 begin
6495 Get_Decoded_Name_String (Chars (Body_Id));
6497 if LSN /= null then
6498 if Subprogram_Name_Greater
6499 (LSN.all, Name_Buffer (1 .. Name_Len))
6500 then
6501 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6502 end if;
6504 Free (LSN);
6505 end if;
6507 LSN := new String'(Name_Buffer (1 .. Name_Len));
6508 end;
6509 end if;
6510 end Check_Subprogram_Order;
6512 ------------------------------
6513 -- Check_Subtype_Conformant --
6514 ------------------------------
6516 procedure Check_Subtype_Conformant
6517 (New_Id : Entity_Id;
6518 Old_Id : Entity_Id;
6519 Err_Loc : Node_Id := Empty;
6520 Skip_Controlling_Formals : Boolean := False;
6521 Get_Inst : Boolean := False)
6523 Result : Boolean;
6524 pragma Warnings (Off, Result);
6525 begin
6526 Check_Conformance
6527 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6528 Skip_Controlling_Formals => Skip_Controlling_Formals,
6529 Get_Inst => Get_Inst);
6530 end Check_Subtype_Conformant;
6532 ---------------------------
6533 -- Check_Type_Conformant --
6534 ---------------------------
6536 procedure Check_Type_Conformant
6537 (New_Id : Entity_Id;
6538 Old_Id : Entity_Id;
6539 Err_Loc : Node_Id := Empty)
6541 Result : Boolean;
6542 pragma Warnings (Off, Result);
6543 begin
6544 Check_Conformance
6545 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6546 end Check_Type_Conformant;
6548 ---------------------------
6549 -- Can_Override_Operator --
6550 ---------------------------
6552 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6553 Typ : Entity_Id;
6555 begin
6556 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6557 return False;
6559 else
6560 Typ := Base_Type (Etype (First_Formal (Subp)));
6562 -- Check explicitly that the operation is a primitive of the type
6564 return Operator_Matches_Spec (Subp, Subp)
6565 and then not Is_Generic_Type (Typ)
6566 and then Scope (Subp) = Scope (Typ)
6567 and then not Is_Class_Wide_Type (Typ);
6568 end if;
6569 end Can_Override_Operator;
6571 ----------------------
6572 -- Conforming_Types --
6573 ----------------------
6575 function Conforming_Types
6576 (T1 : Entity_Id;
6577 T2 : Entity_Id;
6578 Ctype : Conformance_Type;
6579 Get_Inst : Boolean := False) return Boolean
6581 Type_1 : Entity_Id := T1;
6582 Type_2 : Entity_Id := T2;
6583 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6585 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6586 -- If neither T1 nor T2 are generic actual types, or if they are in
6587 -- different scopes (e.g. parent and child instances), then verify that
6588 -- the base types are equal. Otherwise T1 and T2 must be on the same
6589 -- subtype chain. The whole purpose of this procedure is to prevent
6590 -- spurious ambiguities in an instantiation that may arise if two
6591 -- distinct generic types are instantiated with the same actual.
6593 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6594 -- An access parameter can designate an incomplete type. If the
6595 -- incomplete type is the limited view of a type from a limited_
6596 -- with_clause, check whether the non-limited view is available. If
6597 -- it is a (non-limited) incomplete type, get the full view.
6599 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6600 -- Returns True if and only if either T1 denotes a limited view of T2
6601 -- or T2 denotes a limited view of T1. This can arise when the limited
6602 -- with view of a type is used in a subprogram declaration and the
6603 -- subprogram body is in the scope of a regular with clause for the
6604 -- same unit. In such a case, the two type entities can be considered
6605 -- identical for purposes of conformance checking.
6607 ----------------------
6608 -- Base_Types_Match --
6609 ----------------------
6611 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6612 BT1 : constant Entity_Id := Base_Type (T1);
6613 BT2 : constant Entity_Id := Base_Type (T2);
6615 begin
6616 if T1 = T2 then
6617 return True;
6619 elsif BT1 = BT2 then
6621 -- The following is too permissive. A more precise test should
6622 -- check that the generic actual is an ancestor subtype of the
6623 -- other ???.
6625 -- See code in Find_Corresponding_Spec that applies an additional
6626 -- filter to handle accidental amiguities in instances.
6628 return not Is_Generic_Actual_Type (T1)
6629 or else not Is_Generic_Actual_Type (T2)
6630 or else Scope (T1) /= Scope (T2);
6632 -- If T2 is a generic actual type it is declared as the subtype of
6633 -- the actual. If that actual is itself a subtype we need to use its
6634 -- own base type to check for compatibility.
6636 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6637 return True;
6639 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6640 return True;
6642 else
6643 return False;
6644 end if;
6645 end Base_Types_Match;
6647 --------------------------
6648 -- Find_Designated_Type --
6649 --------------------------
6651 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6652 Desig : Entity_Id;
6654 begin
6655 Desig := Directly_Designated_Type (T);
6657 if Ekind (Desig) = E_Incomplete_Type then
6659 -- If regular incomplete type, get full view if available
6661 if Present (Full_View (Desig)) then
6662 Desig := Full_View (Desig);
6664 -- If limited view of a type, get non-limited view if available,
6665 -- and check again for a regular incomplete type.
6667 elsif Present (Non_Limited_View (Desig)) then
6668 Desig := Get_Full_View (Non_Limited_View (Desig));
6669 end if;
6670 end if;
6672 return Desig;
6673 end Find_Designated_Type;
6675 -------------------------------
6676 -- Matches_Limited_With_View --
6677 -------------------------------
6679 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6680 begin
6681 -- In some cases a type imported through a limited_with clause, and
6682 -- its nonlimited view are both visible, for example in an anonymous
6683 -- access-to-class-wide type in a formal, or when building the body
6684 -- for a subprogram renaming after the subprogram has been frozen.
6685 -- In these cases Both entities designate the same type. In addition,
6686 -- if one of them is an actual in an instance, it may be a subtype of
6687 -- the non-limited view of the other.
6689 if From_Limited_With (T1)
6690 and then (T2 = Available_View (T1)
6691 or else Is_Subtype_Of (T2, Available_View (T1)))
6692 then
6693 return True;
6695 elsif From_Limited_With (T2)
6696 and then (T1 = Available_View (T2)
6697 or else Is_Subtype_Of (T1, Available_View (T2)))
6698 then
6699 return True;
6701 elsif From_Limited_With (T1)
6702 and then From_Limited_With (T2)
6703 and then Available_View (T1) = Available_View (T2)
6704 then
6705 return True;
6707 else
6708 return False;
6709 end if;
6710 end Matches_Limited_With_View;
6712 -- Start of processing for Conforming_Types
6714 begin
6715 -- The context is an instance association for a formal access-to-
6716 -- subprogram type; the formal parameter types require mapping because
6717 -- they may denote other formal parameters of the generic unit.
6719 if Get_Inst then
6720 Type_1 := Get_Instance_Of (T1);
6721 Type_2 := Get_Instance_Of (T2);
6722 end if;
6724 -- If one of the types is a view of the other introduced by a limited
6725 -- with clause, treat these as conforming for all purposes.
6727 if Matches_Limited_With_View (T1, T2) then
6728 return True;
6730 elsif Base_Types_Match (Type_1, Type_2) then
6731 return Ctype <= Mode_Conformant
6732 or else Subtypes_Statically_Match (Type_1, Type_2);
6734 elsif Is_Incomplete_Or_Private_Type (Type_1)
6735 and then Present (Full_View (Type_1))
6736 and then Base_Types_Match (Full_View (Type_1), Type_2)
6737 then
6738 return Ctype <= Mode_Conformant
6739 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6741 elsif Ekind (Type_2) = E_Incomplete_Type
6742 and then Present (Full_View (Type_2))
6743 and then Base_Types_Match (Type_1, Full_View (Type_2))
6744 then
6745 return Ctype <= Mode_Conformant
6746 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6748 elsif Is_Private_Type (Type_2)
6749 and then In_Instance
6750 and then Present (Full_View (Type_2))
6751 and then Base_Types_Match (Type_1, Full_View (Type_2))
6752 then
6753 return Ctype <= Mode_Conformant
6754 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6755 end if;
6757 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6758 -- treated recursively because they carry a signature. As far as
6759 -- conformance is concerned, convention plays no role, and either
6760 -- or both could be access to protected subprograms.
6762 Are_Anonymous_Access_To_Subprogram_Types :=
6763 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6764 E_Anonymous_Access_Protected_Subprogram_Type)
6765 and then
6766 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6767 E_Anonymous_Access_Protected_Subprogram_Type);
6769 -- Test anonymous access type case. For this case, static subtype
6770 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6771 -- the base types because we may have built internal subtype entities
6772 -- to handle null-excluding types (see Process_Formals).
6774 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6775 and then
6776 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6778 -- Ada 2005 (AI-254)
6780 or else Are_Anonymous_Access_To_Subprogram_Types
6781 then
6782 declare
6783 Desig_1 : Entity_Id;
6784 Desig_2 : Entity_Id;
6786 begin
6787 -- In Ada 2005, access constant indicators must match for
6788 -- subtype conformance.
6790 if Ada_Version >= Ada_2005
6791 and then Ctype >= Subtype_Conformant
6792 and then
6793 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6794 then
6795 return False;
6796 end if;
6798 Desig_1 := Find_Designated_Type (Type_1);
6799 Desig_2 := Find_Designated_Type (Type_2);
6801 -- If the context is an instance association for a formal
6802 -- access-to-subprogram type; formal access parameter designated
6803 -- types require mapping because they may denote other formal
6804 -- parameters of the generic unit.
6806 if Get_Inst then
6807 Desig_1 := Get_Instance_Of (Desig_1);
6808 Desig_2 := Get_Instance_Of (Desig_2);
6809 end if;
6811 -- It is possible for a Class_Wide_Type to be introduced for an
6812 -- incomplete type, in which case there is a separate class_ wide
6813 -- type for the full view. The types conform if their Etypes
6814 -- conform, i.e. one may be the full view of the other. This can
6815 -- only happen in the context of an access parameter, other uses
6816 -- of an incomplete Class_Wide_Type are illegal.
6818 if Is_Class_Wide_Type (Desig_1)
6819 and then
6820 Is_Class_Wide_Type (Desig_2)
6821 then
6822 return
6823 Conforming_Types
6824 (Etype (Base_Type (Desig_1)),
6825 Etype (Base_Type (Desig_2)), Ctype);
6827 elsif Are_Anonymous_Access_To_Subprogram_Types then
6828 if Ada_Version < Ada_2005 then
6829 return Ctype = Type_Conformant
6830 or else
6831 Subtypes_Statically_Match (Desig_1, Desig_2);
6833 -- We must check the conformance of the signatures themselves
6835 else
6836 declare
6837 Conformant : Boolean;
6838 begin
6839 Check_Conformance
6840 (Desig_1, Desig_2, Ctype, False, Conformant);
6841 return Conformant;
6842 end;
6843 end if;
6845 else
6846 return Base_Type (Desig_1) = Base_Type (Desig_2)
6847 and then (Ctype = Type_Conformant
6848 or else
6849 Subtypes_Statically_Match (Desig_1, Desig_2));
6850 end if;
6851 end;
6853 -- Otherwise definitely no match
6855 else
6856 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6857 and then Is_Access_Type (Type_2))
6858 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6859 and then Is_Access_Type (Type_1)))
6860 and then
6861 Conforming_Types
6862 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6863 then
6864 May_Hide_Profile := True;
6865 end if;
6867 return False;
6868 end if;
6869 end Conforming_Types;
6871 --------------------------
6872 -- Create_Extra_Formals --
6873 --------------------------
6875 procedure Create_Extra_Formals (E : Entity_Id) is
6876 Formal : Entity_Id;
6877 First_Extra : Entity_Id := Empty;
6878 Last_Extra : Entity_Id;
6879 Formal_Type : Entity_Id;
6880 P_Formal : Entity_Id := Empty;
6882 function Add_Extra_Formal
6883 (Assoc_Entity : Entity_Id;
6884 Typ : Entity_Id;
6885 Scope : Entity_Id;
6886 Suffix : String) return Entity_Id;
6887 -- Add an extra formal to the current list of formals and extra formals.
6888 -- The extra formal is added to the end of the list of extra formals,
6889 -- and also returned as the result. These formals are always of mode IN.
6890 -- The new formal has the type Typ, is declared in Scope, and its name
6891 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6892 -- The following suffixes are currently used. They should not be changed
6893 -- without coordinating with CodePeer, which makes use of these to
6894 -- provide better messages.
6896 -- O denotes the Constrained bit.
6897 -- L denotes the accessibility level.
6898 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6899 -- the full list in exp_ch6.BIP_Formal_Kind.
6901 ----------------------
6902 -- Add_Extra_Formal --
6903 ----------------------
6905 function Add_Extra_Formal
6906 (Assoc_Entity : Entity_Id;
6907 Typ : Entity_Id;
6908 Scope : Entity_Id;
6909 Suffix : String) return Entity_Id
6911 EF : constant Entity_Id :=
6912 Make_Defining_Identifier (Sloc (Assoc_Entity),
6913 Chars => New_External_Name (Chars (Assoc_Entity),
6914 Suffix => Suffix));
6916 begin
6917 -- A little optimization. Never generate an extra formal for the
6918 -- _init operand of an initialization procedure, since it could
6919 -- never be used.
6921 if Chars (Formal) = Name_uInit then
6922 return Empty;
6923 end if;
6925 Set_Ekind (EF, E_In_Parameter);
6926 Set_Actual_Subtype (EF, Typ);
6927 Set_Etype (EF, Typ);
6928 Set_Scope (EF, Scope);
6929 Set_Mechanism (EF, Default_Mechanism);
6930 Set_Formal_Validity (EF);
6932 if No (First_Extra) then
6933 First_Extra := EF;
6934 Set_Extra_Formals (Scope, First_Extra);
6935 end if;
6937 if Present (Last_Extra) then
6938 Set_Extra_Formal (Last_Extra, EF);
6939 end if;
6941 Last_Extra := EF;
6943 return EF;
6944 end Add_Extra_Formal;
6946 -- Start of processing for Create_Extra_Formals
6948 begin
6949 -- We never generate extra formals if expansion is not active because we
6950 -- don't need them unless we are generating code.
6952 if not Expander_Active then
6953 return;
6954 end if;
6956 -- No need to generate extra formals in interface thunks whose target
6957 -- primitive has no extra formals.
6959 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6960 return;
6961 end if;
6963 -- If this is a derived subprogram then the subtypes of the parent
6964 -- subprogram's formal parameters will be used to determine the need
6965 -- for extra formals.
6967 if Is_Overloadable (E) and then Present (Alias (E)) then
6968 P_Formal := First_Formal (Alias (E));
6969 end if;
6971 Last_Extra := Empty;
6972 Formal := First_Formal (E);
6973 while Present (Formal) loop
6974 Last_Extra := Formal;
6975 Next_Formal (Formal);
6976 end loop;
6978 -- If Extra_formals were already created, don't do it again. This
6979 -- situation may arise for subprogram types created as part of
6980 -- dispatching calls (see Expand_Dispatching_Call)
6982 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6983 return;
6984 end if;
6986 -- If the subprogram is a predefined dispatching subprogram then don't
6987 -- generate any extra constrained or accessibility level formals. In
6988 -- general we suppress these for internal subprograms (by not calling
6989 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6990 -- generated stream attributes do get passed through because extra
6991 -- build-in-place formals are needed in some cases (limited 'Input).
6993 if Is_Predefined_Internal_Operation (E) then
6994 goto Test_For_Func_Result_Extras;
6995 end if;
6997 Formal := First_Formal (E);
6998 while Present (Formal) loop
7000 -- Create extra formal for supporting the attribute 'Constrained.
7001 -- The case of a private type view without discriminants also
7002 -- requires the extra formal if the underlying type has defaulted
7003 -- discriminants.
7005 if Ekind (Formal) /= E_In_Parameter then
7006 if Present (P_Formal) then
7007 Formal_Type := Etype (P_Formal);
7008 else
7009 Formal_Type := Etype (Formal);
7010 end if;
7012 -- Do not produce extra formals for Unchecked_Union parameters.
7013 -- Jump directly to the end of the loop.
7015 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7016 goto Skip_Extra_Formal_Generation;
7017 end if;
7019 if not Has_Discriminants (Formal_Type)
7020 and then Ekind (Formal_Type) in Private_Kind
7021 and then Present (Underlying_Type (Formal_Type))
7022 then
7023 Formal_Type := Underlying_Type (Formal_Type);
7024 end if;
7026 -- Suppress the extra formal if formal's subtype is constrained or
7027 -- indefinite, or we're compiling for Ada 2012 and the underlying
7028 -- type is tagged and limited. In Ada 2012, a limited tagged type
7029 -- can have defaulted discriminants, but 'Constrained is required
7030 -- to return True, so the formal is never needed (see AI05-0214).
7031 -- Note that this ensures consistency of calling sequences for
7032 -- dispatching operations when some types in a class have defaults
7033 -- on discriminants and others do not (and requiring the extra
7034 -- formal would introduce distributed overhead).
7036 -- If the type does not have a completion yet, treat as prior to
7037 -- Ada 2012 for consistency.
7039 if Has_Discriminants (Formal_Type)
7040 and then not Is_Constrained (Formal_Type)
7041 and then not Is_Indefinite_Subtype (Formal_Type)
7042 and then (Ada_Version < Ada_2012
7043 or else No (Underlying_Type (Formal_Type))
7044 or else not
7045 (Is_Limited_Type (Formal_Type)
7046 and then
7047 (Is_Tagged_Type
7048 (Underlying_Type (Formal_Type)))))
7049 then
7050 Set_Extra_Constrained
7051 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7052 end if;
7053 end if;
7055 -- Create extra formal for supporting accessibility checking. This
7056 -- is done for both anonymous access formals and formals of named
7057 -- access types that are marked as controlling formals. The latter
7058 -- case can occur when Expand_Dispatching_Call creates a subprogram
7059 -- type and substitutes the types of access-to-class-wide actuals
7060 -- for the anonymous access-to-specific-type of controlling formals.
7061 -- Base_Type is applied because in cases where there is a null
7062 -- exclusion the formal may have an access subtype.
7064 -- This is suppressed if we specifically suppress accessibility
7065 -- checks at the package level for either the subprogram, or the
7066 -- package in which it resides. However, we do not suppress it
7067 -- simply if the scope has accessibility checks suppressed, since
7068 -- this could cause trouble when clients are compiled with a
7069 -- different suppression setting. The explicit checks at the
7070 -- package level are safe from this point of view.
7072 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7073 or else (Is_Controlling_Formal (Formal)
7074 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7075 and then not
7076 (Explicit_Suppress (E, Accessibility_Check)
7077 or else
7078 Explicit_Suppress (Scope (E), Accessibility_Check))
7079 and then
7080 (No (P_Formal)
7081 or else Present (Extra_Accessibility (P_Formal)))
7082 then
7083 Set_Extra_Accessibility
7084 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7085 end if;
7087 -- This label is required when skipping extra formal generation for
7088 -- Unchecked_Union parameters.
7090 <<Skip_Extra_Formal_Generation>>
7092 if Present (P_Formal) then
7093 Next_Formal (P_Formal);
7094 end if;
7096 Next_Formal (Formal);
7097 end loop;
7099 <<Test_For_Func_Result_Extras>>
7101 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7102 -- function call is ... determined by the point of call ...".
7104 if Needs_Result_Accessibility_Level (E) then
7105 Set_Extra_Accessibility_Of_Result
7106 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7107 end if;
7109 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7110 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7112 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7113 declare
7114 Result_Subt : constant Entity_Id := Etype (E);
7115 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7116 Formal_Typ : Entity_Id;
7118 Discard : Entity_Id;
7119 pragma Warnings (Off, Discard);
7121 begin
7122 -- In the case of functions with unconstrained result subtypes,
7123 -- add a 4-state formal indicating whether the return object is
7124 -- allocated by the caller (1), or should be allocated by the
7125 -- callee on the secondary stack (2), in the global heap (3), or
7126 -- in a user-defined storage pool (4). For the moment we just use
7127 -- Natural for the type of this formal. Note that this formal
7128 -- isn't usually needed in the case where the result subtype is
7129 -- constrained, but it is needed when the function has a tagged
7130 -- result, because generally such functions can be called in a
7131 -- dispatching context and such calls must be handled like calls
7132 -- to a class-wide function.
7134 if Needs_BIP_Alloc_Form (E) then
7135 Discard :=
7136 Add_Extra_Formal
7137 (E, Standard_Natural,
7138 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7140 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7141 -- use a user-defined pool. This formal is not added on
7142 -- .NET/JVM/ZFP as those targets do not support pools.
7144 if VM_Target = No_VM
7145 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
7146 then
7147 Discard :=
7148 Add_Extra_Formal
7149 (E, RTE (RE_Root_Storage_Pool_Ptr),
7150 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7151 end if;
7152 end if;
7154 -- In the case of functions whose result type needs finalization,
7155 -- add an extra formal which represents the finalization master.
7157 if Needs_BIP_Finalization_Master (E) then
7158 Discard :=
7159 Add_Extra_Formal
7160 (E, RTE (RE_Finalization_Master_Ptr),
7161 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7162 end if;
7164 -- When the result type contains tasks, add two extra formals: the
7165 -- master of the tasks to be created, and the caller's activation
7166 -- chain.
7168 if Has_Task (Full_Subt) then
7169 Discard :=
7170 Add_Extra_Formal
7171 (E, RTE (RE_Master_Id),
7172 E, BIP_Formal_Suffix (BIP_Task_Master));
7173 Discard :=
7174 Add_Extra_Formal
7175 (E, RTE (RE_Activation_Chain_Access),
7176 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7177 end if;
7179 -- All build-in-place functions get an extra formal that will be
7180 -- passed the address of the return object within the caller.
7182 Formal_Typ :=
7183 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7185 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7186 Set_Etype (Formal_Typ, Formal_Typ);
7187 Set_Depends_On_Private
7188 (Formal_Typ, Has_Private_Component (Formal_Typ));
7189 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7190 Set_Is_Access_Constant (Formal_Typ, False);
7192 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7193 -- the designated type comes from the limited view (for back-end
7194 -- purposes).
7196 Set_From_Limited_With
7197 (Formal_Typ, From_Limited_With (Result_Subt));
7199 Layout_Type (Formal_Typ);
7201 Discard :=
7202 Add_Extra_Formal
7203 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7204 end;
7205 end if;
7206 end Create_Extra_Formals;
7208 -----------------------------
7209 -- Enter_Overloaded_Entity --
7210 -----------------------------
7212 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7213 E : Entity_Id := Current_Entity_In_Scope (S);
7214 C_E : Entity_Id := Current_Entity (S);
7216 begin
7217 if Present (E) then
7218 Set_Has_Homonym (E);
7219 Set_Has_Homonym (S);
7220 end if;
7222 Set_Is_Immediately_Visible (S);
7223 Set_Scope (S, Current_Scope);
7225 -- Chain new entity if front of homonym in current scope, so that
7226 -- homonyms are contiguous.
7228 if Present (E) and then E /= C_E then
7229 while Homonym (C_E) /= E loop
7230 C_E := Homonym (C_E);
7231 end loop;
7233 Set_Homonym (C_E, S);
7235 else
7236 E := C_E;
7237 Set_Current_Entity (S);
7238 end if;
7240 Set_Homonym (S, E);
7242 if Is_Inherited_Operation (S) then
7243 Append_Inherited_Subprogram (S);
7244 else
7245 Append_Entity (S, Current_Scope);
7246 end if;
7248 Set_Public_Status (S);
7250 if Debug_Flag_E then
7251 Write_Str ("New overloaded entity chain: ");
7252 Write_Name (Chars (S));
7254 E := S;
7255 while Present (E) loop
7256 Write_Str (" "); Write_Int (Int (E));
7257 E := Homonym (E);
7258 end loop;
7260 Write_Eol;
7261 end if;
7263 -- Generate warning for hiding
7265 if Warn_On_Hiding
7266 and then Comes_From_Source (S)
7267 and then In_Extended_Main_Source_Unit (S)
7268 then
7269 E := S;
7270 loop
7271 E := Homonym (E);
7272 exit when No (E);
7274 -- Warn unless genuine overloading. Do not emit warning on
7275 -- hiding predefined operators in Standard (these are either an
7276 -- (artifact of our implicit declarations, or simple noise) but
7277 -- keep warning on a operator defined on a local subtype, because
7278 -- of the real danger that different operators may be applied in
7279 -- various parts of the program.
7281 -- Note that if E and S have the same scope, there is never any
7282 -- hiding. Either the two conflict, and the program is illegal,
7283 -- or S is overriding an implicit inherited subprogram.
7285 if Scope (E) /= Scope (S)
7286 and then (not Is_Overloadable (E)
7287 or else Subtype_Conformant (E, S))
7288 and then (Is_Immediately_Visible (E)
7289 or else
7290 Is_Potentially_Use_Visible (S))
7291 then
7292 if Scope (E) /= Standard_Standard then
7293 Error_Msg_Sloc := Sloc (E);
7294 Error_Msg_N ("declaration of & hides one #?h?", S);
7296 elsif Nkind (S) = N_Defining_Operator_Symbol
7297 and then
7298 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7299 then
7300 Error_Msg_N
7301 ("declaration of & hides predefined operator?h?", S);
7302 end if;
7303 end if;
7304 end loop;
7305 end if;
7306 end Enter_Overloaded_Entity;
7308 -----------------------------
7309 -- Check_Untagged_Equality --
7310 -----------------------------
7312 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7313 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7314 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7315 Obj_Decl : Node_Id;
7317 begin
7318 -- This check applies only if we have a subprogram declaration with an
7319 -- untagged record type.
7321 if Nkind (Decl) /= N_Subprogram_Declaration
7322 or else not Is_Record_Type (Typ)
7323 or else Is_Tagged_Type (Typ)
7324 then
7325 return;
7326 end if;
7328 -- In Ada 2012 case, we will output errors or warnings depending on
7329 -- the setting of debug flag -gnatd.E.
7331 if Ada_Version >= Ada_2012 then
7332 Error_Msg_Warn := Debug_Flag_Dot_EE;
7334 -- In earlier versions of Ada, nothing to do unless we are warning on
7335 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7337 else
7338 if not Warn_On_Ada_2012_Compatibility then
7339 return;
7340 end if;
7341 end if;
7343 -- Cases where the type has already been frozen
7345 if Is_Frozen (Typ) then
7347 -- If the type is not declared in a package, or if we are in the body
7348 -- of the package or in some other scope, the new operation is not
7349 -- primitive, and therefore legal, though suspicious. Should we
7350 -- generate a warning in this case ???
7352 if Ekind (Scope (Typ)) /= E_Package
7353 or else Scope (Typ) /= Current_Scope
7354 then
7355 return;
7357 -- If the type is a generic actual (sub)type, the operation is not
7358 -- primitive either because the base type is declared elsewhere.
7360 elsif Is_Generic_Actual_Type (Typ) then
7361 return;
7363 -- Here we have a definite error of declaration after freezing
7365 else
7366 if Ada_Version >= Ada_2012 then
7367 Error_Msg_NE
7368 ("equality operator must be declared before type & is "
7369 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7371 -- In Ada 2012 mode with error turned to warning, output one
7372 -- more warning to warn that the equality operation may not
7373 -- compose. This is the consequence of ignoring the error.
7375 if Error_Msg_Warn then
7376 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7377 end if;
7379 else
7380 Error_Msg_NE
7381 ("equality operator must be declared before type& is "
7382 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7383 end if;
7385 -- If we are in the package body, we could just move the
7386 -- declaration to the package spec, so add a message saying that.
7388 if In_Package_Body (Scope (Typ)) then
7389 if Ada_Version >= Ada_2012 then
7390 Error_Msg_N
7391 ("\move declaration to package spec<<", Eq_Op);
7392 else
7393 Error_Msg_N
7394 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7395 end if;
7397 -- Otherwise try to find the freezing point
7399 else
7400 Obj_Decl := Next (Parent (Typ));
7401 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7402 if Nkind (Obj_Decl) = N_Object_Declaration
7403 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7404 then
7405 -- Freezing point, output warnings
7407 if Ada_Version >= Ada_2012 then
7408 Error_Msg_NE
7409 ("type& is frozen by declaration??", Obj_Decl, Typ);
7410 Error_Msg_N
7411 ("\an equality operator cannot be declared after "
7412 & "this point??",
7413 Obj_Decl);
7414 else
7415 Error_Msg_NE
7416 ("type& is frozen by declaration (Ada 2012)?y?",
7417 Obj_Decl, Typ);
7418 Error_Msg_N
7419 ("\an equality operator cannot be declared after "
7420 & "this point (Ada 2012)?y?",
7421 Obj_Decl);
7422 end if;
7424 exit;
7425 end if;
7427 Next (Obj_Decl);
7428 end loop;
7429 end if;
7430 end if;
7432 -- Here if type is not frozen yet. It is illegal to have a primitive
7433 -- equality declared in the private part if the type is visible.
7435 elsif not In_Same_List (Parent (Typ), Decl)
7436 and then not Is_Limited_Type (Typ)
7437 then
7438 -- Shouldn't we give an RM reference here???
7440 if Ada_Version >= Ada_2012 then
7441 Error_Msg_N
7442 ("equality operator appears too late<<", Eq_Op);
7443 else
7444 Error_Msg_N
7445 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7446 end if;
7448 -- No error detected
7450 else
7451 return;
7452 end if;
7453 end Check_Untagged_Equality;
7455 -----------------------------
7456 -- Find_Corresponding_Spec --
7457 -----------------------------
7459 function Find_Corresponding_Spec
7460 (N : Node_Id;
7461 Post_Error : Boolean := True) return Entity_Id
7463 Spec : constant Node_Id := Specification (N);
7464 Designator : constant Entity_Id := Defining_Entity (Spec);
7466 E : Entity_Id;
7468 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7469 -- Even if fully conformant, a body may depend on a generic actual when
7470 -- the spec does not, or vice versa, in which case they were distinct
7471 -- entities in the generic.
7473 -------------------------------
7474 -- Different_Generic_Profile --
7475 -------------------------------
7477 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7478 F1, F2 : Entity_Id;
7480 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7481 -- Check that the types of corresponding formals have the same
7482 -- generic actual if any. We have to account for subtypes of a
7483 -- generic formal, declared between a spec and a body, which may
7484 -- appear distinct in an instance but matched in the generic, and
7485 -- the subtype may be used either in the spec or the body of the
7486 -- subprogram being checked.
7488 -------------------------
7489 -- Same_Generic_Actual --
7490 -------------------------
7492 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7494 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7495 -- Predicate to check whether S1 is a subtype of S2 in the source
7496 -- of the instance.
7498 -------------------------
7499 -- Is_Declared_Subtype --
7500 -------------------------
7502 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7503 begin
7504 return Comes_From_Source (Parent (S1))
7505 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7506 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7507 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7508 end Is_Declared_Subtype;
7510 -- Start of processing for Same_Generic_Actual
7512 begin
7513 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7514 or else Is_Declared_Subtype (T1, T2)
7515 or else Is_Declared_Subtype (T2, T1);
7516 end Same_Generic_Actual;
7518 -- Start of processing for Different_Generic_Profile
7520 begin
7521 if not In_Instance then
7522 return False;
7524 elsif Ekind (E) = E_Function
7525 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7526 then
7527 return True;
7528 end if;
7530 F1 := First_Formal (Designator);
7531 F2 := First_Formal (E);
7532 while Present (F1) loop
7533 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7534 return True;
7535 end if;
7537 Next_Formal (F1);
7538 Next_Formal (F2);
7539 end loop;
7541 return False;
7542 end Different_Generic_Profile;
7544 -- Start of processing for Find_Corresponding_Spec
7546 begin
7547 E := Current_Entity (Designator);
7548 while Present (E) loop
7550 -- We are looking for a matching spec. It must have the same scope,
7551 -- and the same name, and either be type conformant, or be the case
7552 -- of a library procedure spec and its body (which belong to one
7553 -- another regardless of whether they are type conformant or not).
7555 if Scope (E) = Current_Scope then
7556 if Current_Scope = Standard_Standard
7557 or else (Ekind (E) = Ekind (Designator)
7558 and then Type_Conformant (E, Designator))
7559 then
7560 -- Within an instantiation, we know that spec and body are
7561 -- subtype conformant, because they were subtype conformant in
7562 -- the generic. We choose the subtype-conformant entity here as
7563 -- well, to resolve spurious ambiguities in the instance that
7564 -- were not present in the generic (i.e. when two different
7565 -- types are given the same actual). If we are looking for a
7566 -- spec to match a body, full conformance is expected.
7568 if In_Instance then
7570 -- Inherit the convention and "ghostness" of the matching
7571 -- spec to ensure proper full and subtype conformance.
7573 Set_Convention (Designator, Convention (E));
7575 if Is_Ghost_Entity (E) then
7576 Set_Is_Ghost_Entity (Designator);
7577 end if;
7579 -- Skip past subprogram bodies and subprogram renamings that
7580 -- may appear to have a matching spec, but that aren't fully
7581 -- conformant with it. That can occur in cases where an
7582 -- actual type causes unrelated homographs in the instance.
7584 if Nkind_In (N, N_Subprogram_Body,
7585 N_Subprogram_Renaming_Declaration)
7586 and then Present (Homonym (E))
7587 and then not Fully_Conformant (Designator, E)
7588 then
7589 goto Next_Entity;
7591 elsif not Subtype_Conformant (Designator, E) then
7592 goto Next_Entity;
7594 elsif Different_Generic_Profile (E) then
7595 goto Next_Entity;
7596 end if;
7597 end if;
7599 -- Ada 2012 (AI05-0165): For internally generated bodies of
7600 -- null procedures locate the internally generated spec. We
7601 -- enforce mode conformance since a tagged type may inherit
7602 -- from interfaces several null primitives which differ only
7603 -- in the mode of the formals.
7605 if not (Comes_From_Source (E))
7606 and then Is_Null_Procedure (E)
7607 and then not Mode_Conformant (Designator, E)
7608 then
7609 null;
7611 -- For null procedures coming from source that are completions,
7612 -- analysis of the generated body will establish the link.
7614 elsif Comes_From_Source (E)
7615 and then Nkind (Spec) = N_Procedure_Specification
7616 and then Null_Present (Spec)
7617 then
7618 return E;
7620 elsif not Has_Completion (E) then
7621 if Nkind (N) /= N_Subprogram_Body_Stub then
7622 Set_Corresponding_Spec (N, E);
7623 end if;
7625 Set_Has_Completion (E);
7626 return E;
7628 elsif Nkind (Parent (N)) = N_Subunit then
7630 -- If this is the proper body of a subunit, the completion
7631 -- flag is set when analyzing the stub.
7633 return E;
7635 -- If E is an internal function with a controlling result that
7636 -- was created for an operation inherited by a null extension,
7637 -- it may be overridden by a body without a previous spec (one
7638 -- more reason why these should be shunned). In that case we
7639 -- remove the generated body if present, because the current
7640 -- one is the explicit overriding.
7642 elsif Ekind (E) = E_Function
7643 and then Ada_Version >= Ada_2005
7644 and then not Comes_From_Source (E)
7645 and then Has_Controlling_Result (E)
7646 and then Is_Null_Extension (Etype (E))
7647 and then Comes_From_Source (Spec)
7648 then
7649 Set_Has_Completion (E, False);
7651 if Expander_Active
7652 and then Nkind (Parent (E)) = N_Function_Specification
7653 then
7654 Remove
7655 (Unit_Declaration_Node
7656 (Corresponding_Body (Unit_Declaration_Node (E))));
7658 return E;
7660 -- If expansion is disabled, or if the wrapper function has
7661 -- not been generated yet, this a late body overriding an
7662 -- inherited operation, or it is an overriding by some other
7663 -- declaration before the controlling result is frozen. In
7664 -- either case this is a declaration of a new entity.
7666 else
7667 return Empty;
7668 end if;
7670 -- If the body already exists, then this is an error unless
7671 -- the previous declaration is the implicit declaration of a
7672 -- derived subprogram. It is also legal for an instance to
7673 -- contain type conformant overloadable declarations (but the
7674 -- generic declaration may not), per 8.3(26/2).
7676 elsif No (Alias (E))
7677 and then not Is_Intrinsic_Subprogram (E)
7678 and then not In_Instance
7679 and then Post_Error
7680 then
7681 Error_Msg_Sloc := Sloc (E);
7683 if Is_Imported (E) then
7684 Error_Msg_NE
7685 ("body not allowed for imported subprogram & declared#",
7686 N, E);
7687 else
7688 Error_Msg_NE ("duplicate body for & declared#", N, E);
7689 end if;
7690 end if;
7692 -- Child units cannot be overloaded, so a conformance mismatch
7693 -- between body and a previous spec is an error.
7695 elsif Is_Child_Unit (E)
7696 and then
7697 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7698 and then
7699 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7700 N_Compilation_Unit
7701 and then Post_Error
7702 then
7703 Error_Msg_N
7704 ("body of child unit does not match previous declaration", N);
7705 end if;
7706 end if;
7708 <<Next_Entity>>
7709 E := Homonym (E);
7710 end loop;
7712 -- On exit, we know that no previous declaration of subprogram exists
7714 return Empty;
7715 end Find_Corresponding_Spec;
7717 ----------------------
7718 -- Fully_Conformant --
7719 ----------------------
7721 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7722 Result : Boolean;
7723 begin
7724 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7725 return Result;
7726 end Fully_Conformant;
7728 ----------------------------------
7729 -- Fully_Conformant_Expressions --
7730 ----------------------------------
7732 function Fully_Conformant_Expressions
7733 (Given_E1 : Node_Id;
7734 Given_E2 : Node_Id) return Boolean
7736 E1 : constant Node_Id := Original_Node (Given_E1);
7737 E2 : constant Node_Id := Original_Node (Given_E2);
7738 -- We always test conformance on original nodes, since it is possible
7739 -- for analysis and/or expansion to make things look as though they
7740 -- conform when they do not, e.g. by converting 1+2 into 3.
7742 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7743 renames Fully_Conformant_Expressions;
7745 function FCL (L1, L2 : List_Id) return Boolean;
7746 -- Compare elements of two lists for conformance. Elements have to be
7747 -- conformant, and actuals inserted as default parameters do not match
7748 -- explicit actuals with the same value.
7750 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7751 -- Compare an operator node with a function call
7753 ---------
7754 -- FCL --
7755 ---------
7757 function FCL (L1, L2 : List_Id) return Boolean is
7758 N1, N2 : Node_Id;
7760 begin
7761 if L1 = No_List then
7762 N1 := Empty;
7763 else
7764 N1 := First (L1);
7765 end if;
7767 if L2 = No_List then
7768 N2 := Empty;
7769 else
7770 N2 := First (L2);
7771 end if;
7773 -- Compare two lists, skipping rewrite insertions (we want to compare
7774 -- the original trees, not the expanded versions).
7776 loop
7777 if Is_Rewrite_Insertion (N1) then
7778 Next (N1);
7779 elsif Is_Rewrite_Insertion (N2) then
7780 Next (N2);
7781 elsif No (N1) then
7782 return No (N2);
7783 elsif No (N2) then
7784 return False;
7785 elsif not FCE (N1, N2) then
7786 return False;
7787 else
7788 Next (N1);
7789 Next (N2);
7790 end if;
7791 end loop;
7792 end FCL;
7794 ---------
7795 -- FCO --
7796 ---------
7798 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7799 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7800 Act : Node_Id;
7802 begin
7803 if No (Actuals)
7804 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7805 then
7806 return False;
7808 else
7809 Act := First (Actuals);
7811 if Nkind (Op_Node) in N_Binary_Op then
7812 if not FCE (Left_Opnd (Op_Node), Act) then
7813 return False;
7814 end if;
7816 Next (Act);
7817 end if;
7819 return Present (Act)
7820 and then FCE (Right_Opnd (Op_Node), Act)
7821 and then No (Next (Act));
7822 end if;
7823 end FCO;
7825 -- Start of processing for Fully_Conformant_Expressions
7827 begin
7828 -- Non-conformant if paren count does not match. Note: if some idiot
7829 -- complains that we don't do this right for more than 3 levels of
7830 -- parentheses, they will be treated with the respect they deserve.
7832 if Paren_Count (E1) /= Paren_Count (E2) then
7833 return False;
7835 -- If same entities are referenced, then they are conformant even if
7836 -- they have different forms (RM 8.3.1(19-20)).
7838 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7839 if Present (Entity (E1)) then
7840 return Entity (E1) = Entity (E2)
7841 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7842 and then Ekind (Entity (E1)) = E_Discriminant
7843 and then Ekind (Entity (E2)) = E_In_Parameter);
7845 elsif Nkind (E1) = N_Expanded_Name
7846 and then Nkind (E2) = N_Expanded_Name
7847 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7848 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7849 then
7850 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7852 else
7853 -- Identifiers in component associations don't always have
7854 -- entities, but their names must conform.
7856 return Nkind (E1) = N_Identifier
7857 and then Nkind (E2) = N_Identifier
7858 and then Chars (E1) = Chars (E2);
7859 end if;
7861 elsif Nkind (E1) = N_Character_Literal
7862 and then Nkind (E2) = N_Expanded_Name
7863 then
7864 return Nkind (Selector_Name (E2)) = N_Character_Literal
7865 and then Chars (E1) = Chars (Selector_Name (E2));
7867 elsif Nkind (E2) = N_Character_Literal
7868 and then Nkind (E1) = N_Expanded_Name
7869 then
7870 return Nkind (Selector_Name (E1)) = N_Character_Literal
7871 and then Chars (E2) = Chars (Selector_Name (E1));
7873 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7874 return FCO (E1, E2);
7876 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7877 return FCO (E2, E1);
7879 -- Otherwise we must have the same syntactic entity
7881 elsif Nkind (E1) /= Nkind (E2) then
7882 return False;
7884 -- At this point, we specialize by node type
7886 else
7887 case Nkind (E1) is
7889 when N_Aggregate =>
7890 return
7891 FCL (Expressions (E1), Expressions (E2))
7892 and then
7893 FCL (Component_Associations (E1),
7894 Component_Associations (E2));
7896 when N_Allocator =>
7897 if Nkind (Expression (E1)) = N_Qualified_Expression
7898 or else
7899 Nkind (Expression (E2)) = N_Qualified_Expression
7900 then
7901 return FCE (Expression (E1), Expression (E2));
7903 -- Check that the subtype marks and any constraints
7904 -- are conformant
7906 else
7907 declare
7908 Indic1 : constant Node_Id := Expression (E1);
7909 Indic2 : constant Node_Id := Expression (E2);
7910 Elt1 : Node_Id;
7911 Elt2 : Node_Id;
7913 begin
7914 if Nkind (Indic1) /= N_Subtype_Indication then
7915 return
7916 Nkind (Indic2) /= N_Subtype_Indication
7917 and then Entity (Indic1) = Entity (Indic2);
7919 elsif Nkind (Indic2) /= N_Subtype_Indication then
7920 return
7921 Nkind (Indic1) /= N_Subtype_Indication
7922 and then Entity (Indic1) = Entity (Indic2);
7924 else
7925 if Entity (Subtype_Mark (Indic1)) /=
7926 Entity (Subtype_Mark (Indic2))
7927 then
7928 return False;
7929 end if;
7931 Elt1 := First (Constraints (Constraint (Indic1)));
7932 Elt2 := First (Constraints (Constraint (Indic2)));
7933 while Present (Elt1) and then Present (Elt2) loop
7934 if not FCE (Elt1, Elt2) then
7935 return False;
7936 end if;
7938 Next (Elt1);
7939 Next (Elt2);
7940 end loop;
7942 return True;
7943 end if;
7944 end;
7945 end if;
7947 when N_Attribute_Reference =>
7948 return
7949 Attribute_Name (E1) = Attribute_Name (E2)
7950 and then FCL (Expressions (E1), Expressions (E2));
7952 when N_Binary_Op =>
7953 return
7954 Entity (E1) = Entity (E2)
7955 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7956 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7958 when N_Short_Circuit | N_Membership_Test =>
7959 return
7960 FCE (Left_Opnd (E1), Left_Opnd (E2))
7961 and then
7962 FCE (Right_Opnd (E1), Right_Opnd (E2));
7964 when N_Case_Expression =>
7965 declare
7966 Alt1 : Node_Id;
7967 Alt2 : Node_Id;
7969 begin
7970 if not FCE (Expression (E1), Expression (E2)) then
7971 return False;
7973 else
7974 Alt1 := First (Alternatives (E1));
7975 Alt2 := First (Alternatives (E2));
7976 loop
7977 if Present (Alt1) /= Present (Alt2) then
7978 return False;
7979 elsif No (Alt1) then
7980 return True;
7981 end if;
7983 if not FCE (Expression (Alt1), Expression (Alt2))
7984 or else not FCL (Discrete_Choices (Alt1),
7985 Discrete_Choices (Alt2))
7986 then
7987 return False;
7988 end if;
7990 Next (Alt1);
7991 Next (Alt2);
7992 end loop;
7993 end if;
7994 end;
7996 when N_Character_Literal =>
7997 return
7998 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8000 when N_Component_Association =>
8001 return
8002 FCL (Choices (E1), Choices (E2))
8003 and then
8004 FCE (Expression (E1), Expression (E2));
8006 when N_Explicit_Dereference =>
8007 return
8008 FCE (Prefix (E1), Prefix (E2));
8010 when N_Extension_Aggregate =>
8011 return
8012 FCL (Expressions (E1), Expressions (E2))
8013 and then Null_Record_Present (E1) =
8014 Null_Record_Present (E2)
8015 and then FCL (Component_Associations (E1),
8016 Component_Associations (E2));
8018 when N_Function_Call =>
8019 return
8020 FCE (Name (E1), Name (E2))
8021 and then
8022 FCL (Parameter_Associations (E1),
8023 Parameter_Associations (E2));
8025 when N_If_Expression =>
8026 return
8027 FCL (Expressions (E1), Expressions (E2));
8029 when N_Indexed_Component =>
8030 return
8031 FCE (Prefix (E1), Prefix (E2))
8032 and then
8033 FCL (Expressions (E1), Expressions (E2));
8035 when N_Integer_Literal =>
8036 return (Intval (E1) = Intval (E2));
8038 when N_Null =>
8039 return True;
8041 when N_Operator_Symbol =>
8042 return
8043 Chars (E1) = Chars (E2);
8045 when N_Others_Choice =>
8046 return True;
8048 when N_Parameter_Association =>
8049 return
8050 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8051 and then FCE (Explicit_Actual_Parameter (E1),
8052 Explicit_Actual_Parameter (E2));
8054 when N_Qualified_Expression =>
8055 return
8056 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8057 and then
8058 FCE (Expression (E1), Expression (E2));
8060 when N_Quantified_Expression =>
8061 if not FCE (Condition (E1), Condition (E2)) then
8062 return False;
8063 end if;
8065 if Present (Loop_Parameter_Specification (E1))
8066 and then Present (Loop_Parameter_Specification (E2))
8067 then
8068 declare
8069 L1 : constant Node_Id :=
8070 Loop_Parameter_Specification (E1);
8071 L2 : constant Node_Id :=
8072 Loop_Parameter_Specification (E2);
8074 begin
8075 return
8076 Reverse_Present (L1) = Reverse_Present (L2)
8077 and then
8078 FCE (Defining_Identifier (L1),
8079 Defining_Identifier (L2))
8080 and then
8081 FCE (Discrete_Subtype_Definition (L1),
8082 Discrete_Subtype_Definition (L2));
8083 end;
8085 elsif Present (Iterator_Specification (E1))
8086 and then Present (Iterator_Specification (E2))
8087 then
8088 declare
8089 I1 : constant Node_Id := Iterator_Specification (E1);
8090 I2 : constant Node_Id := Iterator_Specification (E2);
8092 begin
8093 return
8094 FCE (Defining_Identifier (I1),
8095 Defining_Identifier (I2))
8096 and then
8097 Of_Present (I1) = Of_Present (I2)
8098 and then
8099 Reverse_Present (I1) = Reverse_Present (I2)
8100 and then FCE (Name (I1), Name (I2))
8101 and then FCE (Subtype_Indication (I1),
8102 Subtype_Indication (I2));
8103 end;
8105 -- The quantified expressions used different specifications to
8106 -- walk their respective ranges.
8108 else
8109 return False;
8110 end if;
8112 when N_Range =>
8113 return
8114 FCE (Low_Bound (E1), Low_Bound (E2))
8115 and then
8116 FCE (High_Bound (E1), High_Bound (E2));
8118 when N_Real_Literal =>
8119 return (Realval (E1) = Realval (E2));
8121 when N_Selected_Component =>
8122 return
8123 FCE (Prefix (E1), Prefix (E2))
8124 and then
8125 FCE (Selector_Name (E1), Selector_Name (E2));
8127 when N_Slice =>
8128 return
8129 FCE (Prefix (E1), Prefix (E2))
8130 and then
8131 FCE (Discrete_Range (E1), Discrete_Range (E2));
8133 when N_String_Literal =>
8134 declare
8135 S1 : constant String_Id := Strval (E1);
8136 S2 : constant String_Id := Strval (E2);
8137 L1 : constant Nat := String_Length (S1);
8138 L2 : constant Nat := String_Length (S2);
8140 begin
8141 if L1 /= L2 then
8142 return False;
8144 else
8145 for J in 1 .. L1 loop
8146 if Get_String_Char (S1, J) /=
8147 Get_String_Char (S2, J)
8148 then
8149 return False;
8150 end if;
8151 end loop;
8153 return True;
8154 end if;
8155 end;
8157 when N_Type_Conversion =>
8158 return
8159 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8160 and then
8161 FCE (Expression (E1), Expression (E2));
8163 when N_Unary_Op =>
8164 return
8165 Entity (E1) = Entity (E2)
8166 and then
8167 FCE (Right_Opnd (E1), Right_Opnd (E2));
8169 when N_Unchecked_Type_Conversion =>
8170 return
8171 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8172 and then
8173 FCE (Expression (E1), Expression (E2));
8175 -- All other node types cannot appear in this context. Strictly
8176 -- we should raise a fatal internal error. Instead we just ignore
8177 -- the nodes. This means that if anyone makes a mistake in the
8178 -- expander and mucks an expression tree irretrievably, the result
8179 -- will be a failure to detect a (probably very obscure) case
8180 -- of non-conformance, which is better than bombing on some
8181 -- case where two expressions do in fact conform.
8183 when others =>
8184 return True;
8186 end case;
8187 end if;
8188 end Fully_Conformant_Expressions;
8190 ----------------------------------------
8191 -- Fully_Conformant_Discrete_Subtypes --
8192 ----------------------------------------
8194 function Fully_Conformant_Discrete_Subtypes
8195 (Given_S1 : Node_Id;
8196 Given_S2 : Node_Id) return Boolean
8198 S1 : constant Node_Id := Original_Node (Given_S1);
8199 S2 : constant Node_Id := Original_Node (Given_S2);
8201 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8202 -- Special-case for a bound given by a discriminant, which in the body
8203 -- is replaced with the discriminal of the enclosing type.
8205 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8206 -- Check both bounds
8208 -----------------------
8209 -- Conforming_Bounds --
8210 -----------------------
8212 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8213 begin
8214 if Is_Entity_Name (B1)
8215 and then Is_Entity_Name (B2)
8216 and then Ekind (Entity (B1)) = E_Discriminant
8217 then
8218 return Chars (B1) = Chars (B2);
8220 else
8221 return Fully_Conformant_Expressions (B1, B2);
8222 end if;
8223 end Conforming_Bounds;
8225 -----------------------
8226 -- Conforming_Ranges --
8227 -----------------------
8229 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8230 begin
8231 return
8232 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8233 and then
8234 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8235 end Conforming_Ranges;
8237 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8239 begin
8240 if Nkind (S1) /= Nkind (S2) then
8241 return False;
8243 elsif Is_Entity_Name (S1) then
8244 return Entity (S1) = Entity (S2);
8246 elsif Nkind (S1) = N_Range then
8247 return Conforming_Ranges (S1, S2);
8249 elsif Nkind (S1) = N_Subtype_Indication then
8250 return
8251 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8252 and then
8253 Conforming_Ranges
8254 (Range_Expression (Constraint (S1)),
8255 Range_Expression (Constraint (S2)));
8256 else
8257 return True;
8258 end if;
8259 end Fully_Conformant_Discrete_Subtypes;
8261 --------------------
8262 -- Install_Entity --
8263 --------------------
8265 procedure Install_Entity (E : Entity_Id) is
8266 Prev : constant Entity_Id := Current_Entity (E);
8267 begin
8268 Set_Is_Immediately_Visible (E);
8269 Set_Current_Entity (E);
8270 Set_Homonym (E, Prev);
8271 end Install_Entity;
8273 ---------------------
8274 -- Install_Formals --
8275 ---------------------
8277 procedure Install_Formals (Id : Entity_Id) is
8278 F : Entity_Id;
8279 begin
8280 F := First_Formal (Id);
8281 while Present (F) loop
8282 Install_Entity (F);
8283 Next_Formal (F);
8284 end loop;
8285 end Install_Formals;
8287 -----------------------------
8288 -- Is_Interface_Conformant --
8289 -----------------------------
8291 function Is_Interface_Conformant
8292 (Tagged_Type : Entity_Id;
8293 Iface_Prim : Entity_Id;
8294 Prim : Entity_Id) return Boolean
8296 -- The operation may in fact be an inherited (implicit) operation
8297 -- rather than the original interface primitive, so retrieve the
8298 -- ultimate ancestor.
8300 Iface : constant Entity_Id :=
8301 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8302 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8304 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8305 -- Return the controlling formal of Prim
8307 ------------------------
8308 -- Controlling_Formal --
8309 ------------------------
8311 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8312 E : Entity_Id;
8314 begin
8315 E := First_Entity (Prim);
8316 while Present (E) loop
8317 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8318 return E;
8319 end if;
8321 Next_Entity (E);
8322 end loop;
8324 return Empty;
8325 end Controlling_Formal;
8327 -- Local variables
8329 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8330 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8332 -- Start of processing for Is_Interface_Conformant
8334 begin
8335 pragma Assert (Is_Subprogram (Iface_Prim)
8336 and then Is_Subprogram (Prim)
8337 and then Is_Dispatching_Operation (Iface_Prim)
8338 and then Is_Dispatching_Operation (Prim));
8340 pragma Assert (Is_Interface (Iface)
8341 or else (Present (Alias (Iface_Prim))
8342 and then
8343 Is_Interface
8344 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8346 if Prim = Iface_Prim
8347 or else not Is_Subprogram (Prim)
8348 or else Ekind (Prim) /= Ekind (Iface_Prim)
8349 or else not Is_Dispatching_Operation (Prim)
8350 or else Scope (Prim) /= Scope (Tagged_Type)
8351 or else No (Typ)
8352 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8353 or else not Primitive_Names_Match (Iface_Prim, Prim)
8354 then
8355 return False;
8357 -- The mode of the controlling formals must match
8359 elsif Present (Iface_Ctrl_F)
8360 and then Present (Prim_Ctrl_F)
8361 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8362 then
8363 return False;
8365 -- Case of a procedure, or a function whose result type matches the
8366 -- result type of the interface primitive, or a function that has no
8367 -- controlling result (I or access I).
8369 elsif Ekind (Iface_Prim) = E_Procedure
8370 or else Etype (Prim) = Etype (Iface_Prim)
8371 or else not Has_Controlling_Result (Prim)
8372 then
8373 return Type_Conformant
8374 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8376 -- Case of a function returning an interface, or an access to one. Check
8377 -- that the return types correspond.
8379 elsif Implements_Interface (Typ, Iface) then
8380 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8382 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8383 then
8384 return False;
8385 else
8386 return
8387 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8388 Skip_Controlling_Formals => True);
8389 end if;
8391 else
8392 return False;
8393 end if;
8394 end Is_Interface_Conformant;
8396 ---------------------------------
8397 -- Is_Non_Overriding_Operation --
8398 ---------------------------------
8400 function Is_Non_Overriding_Operation
8401 (Prev_E : Entity_Id;
8402 New_E : Entity_Id) return Boolean
8404 Formal : Entity_Id;
8405 F_Typ : Entity_Id;
8406 G_Typ : Entity_Id := Empty;
8408 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8409 -- If F_Type is a derived type associated with a generic actual subtype,
8410 -- then return its Generic_Parent_Type attribute, else return Empty.
8412 function Types_Correspond
8413 (P_Type : Entity_Id;
8414 N_Type : Entity_Id) return Boolean;
8415 -- Returns true if and only if the types (or designated types in the
8416 -- case of anonymous access types) are the same or N_Type is derived
8417 -- directly or indirectly from P_Type.
8419 -----------------------------
8420 -- Get_Generic_Parent_Type --
8421 -----------------------------
8423 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8424 G_Typ : Entity_Id;
8425 Defn : Node_Id;
8426 Indic : Node_Id;
8428 begin
8429 if Is_Derived_Type (F_Typ)
8430 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8431 then
8432 -- The tree must be traversed to determine the parent subtype in
8433 -- the generic unit, which unfortunately isn't always available
8434 -- via semantic attributes. ??? (Note: The use of Original_Node
8435 -- is needed for cases where a full derived type has been
8436 -- rewritten.)
8438 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8439 if Nkind (Defn) = N_Derived_Type_Definition then
8440 Indic := Subtype_Indication (Defn);
8442 if Nkind (Indic) = N_Subtype_Indication then
8443 G_Typ := Entity (Subtype_Mark (Indic));
8444 else
8445 G_Typ := Entity (Indic);
8446 end if;
8448 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8449 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8450 then
8451 return Generic_Parent_Type (Parent (G_Typ));
8452 end if;
8453 end if;
8454 end if;
8456 return Empty;
8457 end Get_Generic_Parent_Type;
8459 ----------------------
8460 -- Types_Correspond --
8461 ----------------------
8463 function Types_Correspond
8464 (P_Type : Entity_Id;
8465 N_Type : Entity_Id) return Boolean
8467 Prev_Type : Entity_Id := Base_Type (P_Type);
8468 New_Type : Entity_Id := Base_Type (N_Type);
8470 begin
8471 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8472 Prev_Type := Designated_Type (Prev_Type);
8473 end if;
8475 if Ekind (New_Type) = E_Anonymous_Access_Type then
8476 New_Type := Designated_Type (New_Type);
8477 end if;
8479 if Prev_Type = New_Type then
8480 return True;
8482 elsif not Is_Class_Wide_Type (New_Type) then
8483 while Etype (New_Type) /= New_Type loop
8484 New_Type := Etype (New_Type);
8485 if New_Type = Prev_Type then
8486 return True;
8487 end if;
8488 end loop;
8489 end if;
8490 return False;
8491 end Types_Correspond;
8493 -- Start of processing for Is_Non_Overriding_Operation
8495 begin
8496 -- In the case where both operations are implicit derived subprograms
8497 -- then neither overrides the other. This can only occur in certain
8498 -- obscure cases (e.g., derivation from homographs created in a generic
8499 -- instantiation).
8501 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8502 return True;
8504 elsif Ekind (Current_Scope) = E_Package
8505 and then Is_Generic_Instance (Current_Scope)
8506 and then In_Private_Part (Current_Scope)
8507 and then Comes_From_Source (New_E)
8508 then
8509 -- We examine the formals and result type of the inherited operation,
8510 -- to determine whether their type is derived from (the instance of)
8511 -- a generic type. The first such formal or result type is the one
8512 -- tested.
8514 Formal := First_Formal (Prev_E);
8515 while Present (Formal) loop
8516 F_Typ := Base_Type (Etype (Formal));
8518 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8519 F_Typ := Designated_Type (F_Typ);
8520 end if;
8522 G_Typ := Get_Generic_Parent_Type (F_Typ);
8523 exit when Present (G_Typ);
8525 Next_Formal (Formal);
8526 end loop;
8528 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8529 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8530 end if;
8532 if No (G_Typ) then
8533 return False;
8534 end if;
8536 -- If the generic type is a private type, then the original operation
8537 -- was not overriding in the generic, because there was no primitive
8538 -- operation to override.
8540 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8541 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8542 N_Formal_Private_Type_Definition
8543 then
8544 return True;
8546 -- The generic parent type is the ancestor of a formal derived
8547 -- type declaration. We need to check whether it has a primitive
8548 -- operation that should be overridden by New_E in the generic.
8550 else
8551 declare
8552 P_Formal : Entity_Id;
8553 N_Formal : Entity_Id;
8554 P_Typ : Entity_Id;
8555 N_Typ : Entity_Id;
8556 P_Prim : Entity_Id;
8557 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8559 begin
8560 while Present (Prim_Elt) loop
8561 P_Prim := Node (Prim_Elt);
8563 if Chars (P_Prim) = Chars (New_E)
8564 and then Ekind (P_Prim) = Ekind (New_E)
8565 then
8566 P_Formal := First_Formal (P_Prim);
8567 N_Formal := First_Formal (New_E);
8568 while Present (P_Formal) and then Present (N_Formal) loop
8569 P_Typ := Etype (P_Formal);
8570 N_Typ := Etype (N_Formal);
8572 if not Types_Correspond (P_Typ, N_Typ) then
8573 exit;
8574 end if;
8576 Next_Entity (P_Formal);
8577 Next_Entity (N_Formal);
8578 end loop;
8580 -- Found a matching primitive operation belonging to the
8581 -- formal ancestor type, so the new subprogram is
8582 -- overriding.
8584 if No (P_Formal)
8585 and then No (N_Formal)
8586 and then (Ekind (New_E) /= E_Function
8587 or else
8588 Types_Correspond
8589 (Etype (P_Prim), Etype (New_E)))
8590 then
8591 return False;
8592 end if;
8593 end if;
8595 Next_Elmt (Prim_Elt);
8596 end loop;
8598 -- If no match found, then the new subprogram does not override
8599 -- in the generic (nor in the instance).
8601 -- If the type in question is not abstract, and the subprogram
8602 -- is, this will be an error if the new operation is in the
8603 -- private part of the instance. Emit a warning now, which will
8604 -- make the subsequent error message easier to understand.
8606 if not Is_Abstract_Type (F_Typ)
8607 and then Is_Abstract_Subprogram (Prev_E)
8608 and then In_Private_Part (Current_Scope)
8609 then
8610 Error_Msg_Node_2 := F_Typ;
8611 Error_Msg_NE
8612 ("private operation& in generic unit does not override "
8613 & "any primitive operation of& (RM 12.3 (18))??",
8614 New_E, New_E);
8615 end if;
8617 return True;
8618 end;
8619 end if;
8620 else
8621 return False;
8622 end if;
8623 end Is_Non_Overriding_Operation;
8625 -------------------------------------
8626 -- List_Inherited_Pre_Post_Aspects --
8627 -------------------------------------
8629 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8630 begin
8631 if Opt.List_Inherited_Aspects
8632 and then Is_Subprogram_Or_Generic_Subprogram (E)
8633 then
8634 declare
8635 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8636 Items : Node_Id;
8637 Prag : Node_Id;
8639 begin
8640 for Index in Subps'Range loop
8641 Items := Contract (Subps (Index));
8643 if Present (Items) then
8644 Prag := Pre_Post_Conditions (Items);
8645 while Present (Prag) loop
8646 Error_Msg_Sloc := Sloc (Prag);
8648 if Class_Present (Prag)
8649 and then not Split_PPC (Prag)
8650 then
8651 if Pragma_Name (Prag) = Name_Precondition then
8652 Error_Msg_N
8653 ("info: & inherits `Pre''Class` aspect from "
8654 & "#?L?", E);
8655 else
8656 Error_Msg_N
8657 ("info: & inherits `Post''Class` aspect from "
8658 & "#?L?", E);
8659 end if;
8660 end if;
8662 Prag := Next_Pragma (Prag);
8663 end loop;
8664 end if;
8665 end loop;
8666 end;
8667 end if;
8668 end List_Inherited_Pre_Post_Aspects;
8670 ------------------------------
8671 -- Make_Inequality_Operator --
8672 ------------------------------
8674 -- S is the defining identifier of an equality operator. We build a
8675 -- subprogram declaration with the right signature. This operation is
8676 -- intrinsic, because it is always expanded as the negation of the
8677 -- call to the equality function.
8679 procedure Make_Inequality_Operator (S : Entity_Id) is
8680 Loc : constant Source_Ptr := Sloc (S);
8681 Decl : Node_Id;
8682 Formals : List_Id;
8683 Op_Name : Entity_Id;
8685 FF : constant Entity_Id := First_Formal (S);
8686 NF : constant Entity_Id := Next_Formal (FF);
8688 begin
8689 -- Check that equality was properly defined, ignore call if not
8691 if No (NF) then
8692 return;
8693 end if;
8695 declare
8696 A : constant Entity_Id :=
8697 Make_Defining_Identifier (Sloc (FF),
8698 Chars => Chars (FF));
8700 B : constant Entity_Id :=
8701 Make_Defining_Identifier (Sloc (NF),
8702 Chars => Chars (NF));
8704 begin
8705 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8707 Formals := New_List (
8708 Make_Parameter_Specification (Loc,
8709 Defining_Identifier => A,
8710 Parameter_Type =>
8711 New_Occurrence_Of (Etype (First_Formal (S)),
8712 Sloc (Etype (First_Formal (S))))),
8714 Make_Parameter_Specification (Loc,
8715 Defining_Identifier => B,
8716 Parameter_Type =>
8717 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8718 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8720 Decl :=
8721 Make_Subprogram_Declaration (Loc,
8722 Specification =>
8723 Make_Function_Specification (Loc,
8724 Defining_Unit_Name => Op_Name,
8725 Parameter_Specifications => Formals,
8726 Result_Definition =>
8727 New_Occurrence_Of (Standard_Boolean, Loc)));
8729 -- Insert inequality right after equality if it is explicit or after
8730 -- the derived type when implicit. These entities are created only
8731 -- for visibility purposes, and eventually replaced in the course
8732 -- of expansion, so they do not need to be attached to the tree and
8733 -- seen by the back-end. Keeping them internal also avoids spurious
8734 -- freezing problems. The declaration is inserted in the tree for
8735 -- analysis, and removed afterwards. If the equality operator comes
8736 -- from an explicit declaration, attach the inequality immediately
8737 -- after. Else the equality is inherited from a derived type
8738 -- declaration, so insert inequality after that declaration.
8740 if No (Alias (S)) then
8741 Insert_After (Unit_Declaration_Node (S), Decl);
8742 elsif Is_List_Member (Parent (S)) then
8743 Insert_After (Parent (S), Decl);
8744 else
8745 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8746 end if;
8748 Mark_Rewrite_Insertion (Decl);
8749 Set_Is_Intrinsic_Subprogram (Op_Name);
8750 Analyze (Decl);
8751 Remove (Decl);
8752 Set_Has_Completion (Op_Name);
8753 Set_Corresponding_Equality (Op_Name, S);
8754 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8755 end;
8756 end Make_Inequality_Operator;
8758 ----------------------
8759 -- May_Need_Actuals --
8760 ----------------------
8762 procedure May_Need_Actuals (Fun : Entity_Id) is
8763 F : Entity_Id;
8764 B : Boolean;
8766 begin
8767 F := First_Formal (Fun);
8768 B := True;
8769 while Present (F) loop
8770 if No (Default_Value (F)) then
8771 B := False;
8772 exit;
8773 end if;
8775 Next_Formal (F);
8776 end loop;
8778 Set_Needs_No_Actuals (Fun, B);
8779 end May_Need_Actuals;
8781 ---------------------
8782 -- Mode_Conformant --
8783 ---------------------
8785 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8786 Result : Boolean;
8787 begin
8788 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8789 return Result;
8790 end Mode_Conformant;
8792 ---------------------------
8793 -- New_Overloaded_Entity --
8794 ---------------------------
8796 procedure New_Overloaded_Entity
8797 (S : Entity_Id;
8798 Derived_Type : Entity_Id := Empty)
8800 Overridden_Subp : Entity_Id := Empty;
8801 -- Set if the current scope has an operation that is type-conformant
8802 -- with S, and becomes hidden by S.
8804 Is_Primitive_Subp : Boolean;
8805 -- Set to True if the new subprogram is primitive
8807 E : Entity_Id;
8808 -- Entity that S overrides
8810 Prev_Vis : Entity_Id := Empty;
8811 -- Predecessor of E in Homonym chain
8813 procedure Check_For_Primitive_Subprogram
8814 (Is_Primitive : out Boolean;
8815 Is_Overriding : Boolean := False);
8816 -- If the subprogram being analyzed is a primitive operation of the type
8817 -- of a formal or result, set the Has_Primitive_Operations flag on the
8818 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8819 -- corresponding flag on the entity itself for later use.
8821 procedure Check_Synchronized_Overriding
8822 (Def_Id : Entity_Id;
8823 Overridden_Subp : out Entity_Id);
8824 -- First determine if Def_Id is an entry or a subprogram either defined
8825 -- in the scope of a task or protected type, or is a primitive of such
8826 -- a type. Check whether Def_Id overrides a subprogram of an interface
8827 -- implemented by the synchronized type, return the overridden entity
8828 -- or Empty.
8830 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8831 -- Check that E is declared in the private part of the current package,
8832 -- or in the package body, where it may hide a previous declaration.
8833 -- We can't use In_Private_Part by itself because this flag is also
8834 -- set when freezing entities, so we must examine the place of the
8835 -- declaration in the tree, and recognize wrapper packages as well.
8837 function Is_Overriding_Alias
8838 (Old_E : Entity_Id;
8839 New_E : Entity_Id) return Boolean;
8840 -- Check whether new subprogram and old subprogram are both inherited
8841 -- from subprograms that have distinct dispatch table entries. This can
8842 -- occur with derivations from instances with accidental homonyms. The
8843 -- function is conservative given that the converse is only true within
8844 -- instances that contain accidental overloadings.
8846 ------------------------------------
8847 -- Check_For_Primitive_Subprogram --
8848 ------------------------------------
8850 procedure Check_For_Primitive_Subprogram
8851 (Is_Primitive : out Boolean;
8852 Is_Overriding : Boolean := False)
8854 Formal : Entity_Id;
8855 F_Typ : Entity_Id;
8856 B_Typ : Entity_Id;
8858 function Visible_Part_Type (T : Entity_Id) return Boolean;
8859 -- Returns true if T is declared in the visible part of the current
8860 -- package scope; otherwise returns false. Assumes that T is declared
8861 -- in a package.
8863 procedure Check_Private_Overriding (T : Entity_Id);
8864 -- Checks that if a primitive abstract subprogram of a visible
8865 -- abstract type is declared in a private part, then it must override
8866 -- an abstract subprogram declared in the visible part. Also checks
8867 -- that if a primitive function with a controlling result is declared
8868 -- in a private part, then it must override a function declared in
8869 -- the visible part.
8871 ------------------------------
8872 -- Check_Private_Overriding --
8873 ------------------------------
8875 procedure Check_Private_Overriding (T : Entity_Id) is
8877 function Overrides_Visible_Function
8878 (Partial_View : Entity_Id) return Boolean;
8879 -- True if S overrides a function in the visible part. The
8880 -- overridden function could be explicitly or implicitly declared.
8882 function Overrides_Visible_Function
8883 (Partial_View : Entity_Id) return Boolean
8885 begin
8886 if not Is_Overriding or else not Has_Homonym (S) then
8887 return False;
8888 end if;
8890 if not Present (Partial_View) then
8891 return True;
8892 end if;
8894 -- Search through all the homonyms H of S in the current
8895 -- package spec, and return True if we find one that matches.
8896 -- Note that Parent (H) will be the declaration of the
8897 -- partial view of T for a match.
8899 declare
8900 H : Entity_Id := S;
8901 begin
8902 loop
8903 H := Homonym (H);
8904 exit when not Present (H) or else Scope (H) /= Scope (S);
8906 if Nkind_In
8907 (Parent (H),
8908 N_Private_Extension_Declaration,
8909 N_Private_Type_Declaration)
8910 and then Defining_Identifier (Parent (H)) = Partial_View
8911 then
8912 return True;
8913 end if;
8914 end loop;
8915 end;
8917 return False;
8918 end Overrides_Visible_Function;
8920 -- Start of processing for Check_Private_Overriding
8922 begin
8923 if Is_Package_Or_Generic_Package (Current_Scope)
8924 and then In_Private_Part (Current_Scope)
8925 and then Visible_Part_Type (T)
8926 and then not In_Instance
8927 then
8928 if Is_Abstract_Type (T)
8929 and then Is_Abstract_Subprogram (S)
8930 and then (not Is_Overriding
8931 or else not Is_Abstract_Subprogram (E))
8932 then
8933 Error_Msg_N ("abstract subprograms must be visible "
8934 & "(RM 3.9.3(10))!", S);
8936 elsif Ekind (S) = E_Function then
8937 declare
8938 Partial_View : constant Entity_Id :=
8939 Incomplete_Or_Partial_View (T);
8941 begin
8942 if not Overrides_Visible_Function (Partial_View) then
8944 -- Here, S is "function ... return T;" declared in
8945 -- the private part, not overriding some visible
8946 -- operation. That's illegal in the tagged case
8947 -- (but not if the private type is untagged).
8949 if ((Present (Partial_View)
8950 and then Is_Tagged_Type (Partial_View))
8951 or else (not Present (Partial_View)
8952 and then Is_Tagged_Type (T)))
8953 and then T = Base_Type (Etype (S))
8954 then
8955 Error_Msg_N
8956 ("private function with tagged result must"
8957 & " override visible-part function", S);
8958 Error_Msg_N
8959 ("\move subprogram to the visible part"
8960 & " (RM 3.9.3(10))", S);
8962 -- AI05-0073: extend this test to the case of a
8963 -- function with a controlling access result.
8965 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8966 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8967 and then
8968 not Is_Class_Wide_Type
8969 (Designated_Type (Etype (S)))
8970 and then Ada_Version >= Ada_2012
8971 then
8972 Error_Msg_N
8973 ("private function with controlling access "
8974 & "result must override visible-part function",
8976 Error_Msg_N
8977 ("\move subprogram to the visible part"
8978 & " (RM 3.9.3(10))", S);
8979 end if;
8980 end if;
8981 end;
8982 end if;
8983 end if;
8984 end Check_Private_Overriding;
8986 -----------------------
8987 -- Visible_Part_Type --
8988 -----------------------
8990 function Visible_Part_Type (T : Entity_Id) return Boolean is
8991 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8992 N : Node_Id;
8994 begin
8995 -- If the entity is a private type, then it must be declared in a
8996 -- visible part.
8998 if Ekind (T) in Private_Kind then
8999 return True;
9000 end if;
9002 -- Otherwise, we traverse the visible part looking for its
9003 -- corresponding declaration. We cannot use the declaration
9004 -- node directly because in the private part the entity of a
9005 -- private type is the one in the full view, which does not
9006 -- indicate that it is the completion of something visible.
9008 N := First (Visible_Declarations (Specification (P)));
9009 while Present (N) loop
9010 if Nkind (N) = N_Full_Type_Declaration
9011 and then Present (Defining_Identifier (N))
9012 and then T = Defining_Identifier (N)
9013 then
9014 return True;
9016 elsif Nkind_In (N, N_Private_Type_Declaration,
9017 N_Private_Extension_Declaration)
9018 and then Present (Defining_Identifier (N))
9019 and then T = Full_View (Defining_Identifier (N))
9020 then
9021 return True;
9022 end if;
9024 Next (N);
9025 end loop;
9027 return False;
9028 end Visible_Part_Type;
9030 -- Start of processing for Check_For_Primitive_Subprogram
9032 begin
9033 Is_Primitive := False;
9035 if not Comes_From_Source (S) then
9036 null;
9038 -- If subprogram is at library level, it is not primitive operation
9040 elsif Current_Scope = Standard_Standard then
9041 null;
9043 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9044 and then not In_Package_Body (Current_Scope))
9045 or else Is_Overriding
9046 then
9047 -- For function, check return type
9049 if Ekind (S) = E_Function then
9050 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9051 F_Typ := Designated_Type (Etype (S));
9052 else
9053 F_Typ := Etype (S);
9054 end if;
9056 B_Typ := Base_Type (F_Typ);
9058 if Scope (B_Typ) = Current_Scope
9059 and then not Is_Class_Wide_Type (B_Typ)
9060 and then not Is_Generic_Type (B_Typ)
9061 then
9062 Is_Primitive := True;
9063 Set_Has_Primitive_Operations (B_Typ);
9064 Set_Is_Primitive (S);
9065 Check_Private_Overriding (B_Typ);
9066 end if;
9067 end if;
9069 -- For all subprograms, check formals
9071 Formal := First_Formal (S);
9072 while Present (Formal) loop
9073 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9074 F_Typ := Designated_Type (Etype (Formal));
9075 else
9076 F_Typ := Etype (Formal);
9077 end if;
9079 B_Typ := Base_Type (F_Typ);
9081 if Ekind (B_Typ) = E_Access_Subtype then
9082 B_Typ := Base_Type (B_Typ);
9083 end if;
9085 if Scope (B_Typ) = Current_Scope
9086 and then not Is_Class_Wide_Type (B_Typ)
9087 and then not Is_Generic_Type (B_Typ)
9088 then
9089 Is_Primitive := True;
9090 Set_Is_Primitive (S);
9091 Set_Has_Primitive_Operations (B_Typ);
9092 Check_Private_Overriding (B_Typ);
9093 end if;
9095 Next_Formal (Formal);
9096 end loop;
9098 -- Special case: An equality function can be redefined for a type
9099 -- occurring in a declarative part, and won't otherwise be treated as
9100 -- a primitive because it doesn't occur in a package spec and doesn't
9101 -- override an inherited subprogram. It's important that we mark it
9102 -- primitive so it can be returned by Collect_Primitive_Operations
9103 -- and be used in composing the equality operation of later types
9104 -- that have a component of the type.
9106 elsif Chars (S) = Name_Op_Eq
9107 and then Etype (S) = Standard_Boolean
9108 then
9109 B_Typ := Base_Type (Etype (First_Formal (S)));
9111 if Scope (B_Typ) = Current_Scope
9112 and then
9113 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9114 and then not Is_Limited_Type (B_Typ)
9115 then
9116 Is_Primitive := True;
9117 Set_Is_Primitive (S);
9118 Set_Has_Primitive_Operations (B_Typ);
9119 Check_Private_Overriding (B_Typ);
9120 end if;
9121 end if;
9122 end Check_For_Primitive_Subprogram;
9124 -----------------------------------
9125 -- Check_Synchronized_Overriding --
9126 -----------------------------------
9128 procedure Check_Synchronized_Overriding
9129 (Def_Id : Entity_Id;
9130 Overridden_Subp : out Entity_Id)
9132 Ifaces_List : Elist_Id;
9133 In_Scope : Boolean;
9134 Typ : Entity_Id;
9136 function Matches_Prefixed_View_Profile
9137 (Prim_Params : List_Id;
9138 Iface_Params : List_Id) return Boolean;
9139 -- Determine whether a subprogram's parameter profile Prim_Params
9140 -- matches that of a potentially overridden interface subprogram
9141 -- Iface_Params. Also determine if the type of first parameter of
9142 -- Iface_Params is an implemented interface.
9144 -----------------------------------
9145 -- Matches_Prefixed_View_Profile --
9146 -----------------------------------
9148 function Matches_Prefixed_View_Profile
9149 (Prim_Params : List_Id;
9150 Iface_Params : List_Id) return Boolean
9152 Iface_Id : Entity_Id;
9153 Iface_Param : Node_Id;
9154 Iface_Typ : Entity_Id;
9155 Prim_Id : Entity_Id;
9156 Prim_Param : Node_Id;
9157 Prim_Typ : Entity_Id;
9159 function Is_Implemented
9160 (Ifaces_List : Elist_Id;
9161 Iface : Entity_Id) return Boolean;
9162 -- Determine if Iface is implemented by the current task or
9163 -- protected type.
9165 --------------------
9166 -- Is_Implemented --
9167 --------------------
9169 function Is_Implemented
9170 (Ifaces_List : Elist_Id;
9171 Iface : Entity_Id) return Boolean
9173 Iface_Elmt : Elmt_Id;
9175 begin
9176 Iface_Elmt := First_Elmt (Ifaces_List);
9177 while Present (Iface_Elmt) loop
9178 if Node (Iface_Elmt) = Iface then
9179 return True;
9180 end if;
9182 Next_Elmt (Iface_Elmt);
9183 end loop;
9185 return False;
9186 end Is_Implemented;
9188 -- Start of processing for Matches_Prefixed_View_Profile
9190 begin
9191 Iface_Param := First (Iface_Params);
9192 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9194 if Is_Access_Type (Iface_Typ) then
9195 Iface_Typ := Designated_Type (Iface_Typ);
9196 end if;
9198 Prim_Param := First (Prim_Params);
9200 -- The first parameter of the potentially overridden subprogram
9201 -- must be an interface implemented by Prim.
9203 if not Is_Interface (Iface_Typ)
9204 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9205 then
9206 return False;
9207 end if;
9209 -- The checks on the object parameters are done, move onto the
9210 -- rest of the parameters.
9212 if not In_Scope then
9213 Prim_Param := Next (Prim_Param);
9214 end if;
9216 Iface_Param := Next (Iface_Param);
9217 while Present (Iface_Param) and then Present (Prim_Param) loop
9218 Iface_Id := Defining_Identifier (Iface_Param);
9219 Iface_Typ := Find_Parameter_Type (Iface_Param);
9221 Prim_Id := Defining_Identifier (Prim_Param);
9222 Prim_Typ := Find_Parameter_Type (Prim_Param);
9224 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9225 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9226 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9227 then
9228 Iface_Typ := Designated_Type (Iface_Typ);
9229 Prim_Typ := Designated_Type (Prim_Typ);
9230 end if;
9232 -- Case of multiple interface types inside a parameter profile
9234 -- (Obj_Param : in out Iface; ...; Param : Iface)
9236 -- If the interface type is implemented, then the matching type
9237 -- in the primitive should be the implementing record type.
9239 if Ekind (Iface_Typ) = E_Record_Type
9240 and then Is_Interface (Iface_Typ)
9241 and then Is_Implemented (Ifaces_List, Iface_Typ)
9242 then
9243 if Prim_Typ /= Typ then
9244 return False;
9245 end if;
9247 -- The two parameters must be both mode and subtype conformant
9249 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9250 or else not
9251 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9252 then
9253 return False;
9254 end if;
9256 Next (Iface_Param);
9257 Next (Prim_Param);
9258 end loop;
9260 -- One of the two lists contains more parameters than the other
9262 if Present (Iface_Param) or else Present (Prim_Param) then
9263 return False;
9264 end if;
9266 return True;
9267 end Matches_Prefixed_View_Profile;
9269 -- Start of processing for Check_Synchronized_Overriding
9271 begin
9272 Overridden_Subp := Empty;
9274 -- Def_Id must be an entry or a subprogram. We should skip predefined
9275 -- primitives internally generated by the frontend; however at this
9276 -- stage predefined primitives are still not fully decorated. As a
9277 -- minor optimization we skip here internally generated subprograms.
9279 if (Ekind (Def_Id) /= E_Entry
9280 and then Ekind (Def_Id) /= E_Function
9281 and then Ekind (Def_Id) /= E_Procedure)
9282 or else not Comes_From_Source (Def_Id)
9283 then
9284 return;
9285 end if;
9287 -- Search for the concurrent declaration since it contains the list
9288 -- of all implemented interfaces. In this case, the subprogram is
9289 -- declared within the scope of a protected or a task type.
9291 if Present (Scope (Def_Id))
9292 and then Is_Concurrent_Type (Scope (Def_Id))
9293 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9294 then
9295 Typ := Scope (Def_Id);
9296 In_Scope := True;
9298 -- The enclosing scope is not a synchronized type and the subprogram
9299 -- has no formals.
9301 elsif No (First_Formal (Def_Id)) then
9302 return;
9304 -- The subprogram has formals and hence it may be a primitive of a
9305 -- concurrent type.
9307 else
9308 Typ := Etype (First_Formal (Def_Id));
9310 if Is_Access_Type (Typ) then
9311 Typ := Directly_Designated_Type (Typ);
9312 end if;
9314 if Is_Concurrent_Type (Typ)
9315 and then not Is_Generic_Actual_Type (Typ)
9316 then
9317 In_Scope := False;
9319 -- This case occurs when the concurrent type is declared within
9320 -- a generic unit. As a result the corresponding record has been
9321 -- built and used as the type of the first formal, we just have
9322 -- to retrieve the corresponding concurrent type.
9324 elsif Is_Concurrent_Record_Type (Typ)
9325 and then not Is_Class_Wide_Type (Typ)
9326 and then Present (Corresponding_Concurrent_Type (Typ))
9327 then
9328 Typ := Corresponding_Concurrent_Type (Typ);
9329 In_Scope := False;
9331 else
9332 return;
9333 end if;
9334 end if;
9336 -- There is no overriding to check if is an inherited operation in a
9337 -- type derivation on for a generic actual.
9339 Collect_Interfaces (Typ, Ifaces_List);
9341 if Is_Empty_Elmt_List (Ifaces_List) then
9342 return;
9343 end if;
9345 -- Determine whether entry or subprogram Def_Id overrides a primitive
9346 -- operation that belongs to one of the interfaces in Ifaces_List.
9348 declare
9349 Candidate : Entity_Id := Empty;
9350 Hom : Entity_Id := Empty;
9351 Subp : Entity_Id := Empty;
9353 begin
9354 -- Traverse the homonym chain, looking for a potentially
9355 -- overridden subprogram that belongs to an implemented
9356 -- interface.
9358 Hom := Current_Entity_In_Scope (Def_Id);
9359 while Present (Hom) loop
9360 Subp := Hom;
9362 if Subp = Def_Id
9363 or else not Is_Overloadable (Subp)
9364 or else not Is_Primitive (Subp)
9365 or else not Is_Dispatching_Operation (Subp)
9366 or else not Present (Find_Dispatching_Type (Subp))
9367 or else not Is_Interface (Find_Dispatching_Type (Subp))
9368 then
9369 null;
9371 -- Entries and procedures can override abstract or null
9372 -- interface procedures.
9374 elsif (Ekind (Def_Id) = E_Procedure
9375 or else Ekind (Def_Id) = E_Entry)
9376 and then Ekind (Subp) = E_Procedure
9377 and then Matches_Prefixed_View_Profile
9378 (Parameter_Specifications (Parent (Def_Id)),
9379 Parameter_Specifications (Parent (Subp)))
9380 then
9381 Candidate := Subp;
9383 -- For an overridden subprogram Subp, check whether the mode
9384 -- of its first parameter is correct depending on the kind
9385 -- of synchronized type.
9387 declare
9388 Formal : constant Node_Id := First_Formal (Candidate);
9390 begin
9391 -- In order for an entry or a protected procedure to
9392 -- override, the first parameter of the overridden
9393 -- routine must be of mode "out", "in out" or
9394 -- access-to-variable.
9396 if Ekind_In (Candidate, E_Entry, E_Procedure)
9397 and then Is_Protected_Type (Typ)
9398 and then Ekind (Formal) /= E_In_Out_Parameter
9399 and then Ekind (Formal) /= E_Out_Parameter
9400 and then Nkind (Parameter_Type (Parent (Formal))) /=
9401 N_Access_Definition
9402 then
9403 null;
9405 -- All other cases are OK since a task entry or routine
9406 -- does not have a restriction on the mode of the first
9407 -- parameter of the overridden interface routine.
9409 else
9410 Overridden_Subp := Candidate;
9411 return;
9412 end if;
9413 end;
9415 -- Functions can override abstract interface functions
9417 elsif Ekind (Def_Id) = E_Function
9418 and then Ekind (Subp) = E_Function
9419 and then Matches_Prefixed_View_Profile
9420 (Parameter_Specifications (Parent (Def_Id)),
9421 Parameter_Specifications (Parent (Subp)))
9422 and then Etype (Result_Definition (Parent (Def_Id))) =
9423 Etype (Result_Definition (Parent (Subp)))
9424 then
9425 Candidate := Subp;
9427 -- If an inherited subprogram is implemented by a protected
9428 -- function, then the first parameter of the inherited
9429 -- subprogram shall be of mode in, but not an
9430 -- access-to-variable parameter (RM 9.4(11/9)
9432 if Present (First_Formal (Subp))
9433 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9434 and then
9435 (not Is_Access_Type (Etype (First_Formal (Subp)))
9436 or else
9437 Is_Access_Constant (Etype (First_Formal (Subp))))
9438 then
9439 Overridden_Subp := Subp;
9440 return;
9441 end if;
9442 end if;
9444 Hom := Homonym (Hom);
9445 end loop;
9447 -- After examining all candidates for overriding, we are left with
9448 -- the best match which is a mode incompatible interface routine.
9450 if In_Scope and then Present (Candidate) then
9451 Error_Msg_PT (Def_Id, Candidate);
9452 end if;
9454 Overridden_Subp := Candidate;
9455 return;
9456 end;
9457 end Check_Synchronized_Overriding;
9459 ----------------------------
9460 -- Is_Private_Declaration --
9461 ----------------------------
9463 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9464 Priv_Decls : List_Id;
9465 Decl : constant Node_Id := Unit_Declaration_Node (E);
9467 begin
9468 if Is_Package_Or_Generic_Package (Current_Scope)
9469 and then In_Private_Part (Current_Scope)
9470 then
9471 Priv_Decls :=
9472 Private_Declarations (Package_Specification (Current_Scope));
9474 return In_Package_Body (Current_Scope)
9475 or else
9476 (Is_List_Member (Decl)
9477 and then List_Containing (Decl) = Priv_Decls)
9478 or else (Nkind (Parent (Decl)) = N_Package_Specification
9479 and then not
9480 Is_Compilation_Unit
9481 (Defining_Entity (Parent (Decl)))
9482 and then List_Containing (Parent (Parent (Decl))) =
9483 Priv_Decls);
9484 else
9485 return False;
9486 end if;
9487 end Is_Private_Declaration;
9489 --------------------------
9490 -- Is_Overriding_Alias --
9491 --------------------------
9493 function Is_Overriding_Alias
9494 (Old_E : Entity_Id;
9495 New_E : Entity_Id) return Boolean
9497 AO : constant Entity_Id := Alias (Old_E);
9498 AN : constant Entity_Id := Alias (New_E);
9499 begin
9500 return Scope (AO) /= Scope (AN)
9501 or else No (DTC_Entity (AO))
9502 or else No (DTC_Entity (AN))
9503 or else DT_Position (AO) = DT_Position (AN);
9504 end Is_Overriding_Alias;
9506 -- Start of processing for New_Overloaded_Entity
9508 begin
9509 -- We need to look for an entity that S may override. This must be a
9510 -- homonym in the current scope, so we look for the first homonym of
9511 -- S in the current scope as the starting point for the search.
9513 E := Current_Entity_In_Scope (S);
9515 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9516 -- They are directly added to the list of primitive operations of
9517 -- Derived_Type, unless this is a rederivation in the private part
9518 -- of an operation that was already derived in the visible part of
9519 -- the current package.
9521 if Ada_Version >= Ada_2005
9522 and then Present (Derived_Type)
9523 and then Present (Alias (S))
9524 and then Is_Dispatching_Operation (Alias (S))
9525 and then Present (Find_Dispatching_Type (Alias (S)))
9526 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9527 then
9528 -- For private types, when the full-view is processed we propagate to
9529 -- the full view the non-overridden entities whose attribute "alias"
9530 -- references an interface primitive. These entities were added by
9531 -- Derive_Subprograms to ensure that interface primitives are
9532 -- covered.
9534 -- Inside_Freeze_Actions is non zero when S corresponds with an
9535 -- internal entity that links an interface primitive with its
9536 -- covering primitive through attribute Interface_Alias (see
9537 -- Add_Internal_Interface_Entities).
9539 if Inside_Freezing_Actions = 0
9540 and then Is_Package_Or_Generic_Package (Current_Scope)
9541 and then In_Private_Part (Current_Scope)
9542 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9543 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9544 and then Full_View (Defining_Identifier (Parent (E)))
9545 = Defining_Identifier (Parent (S))
9546 and then Alias (E) = Alias (S)
9547 then
9548 Check_Operation_From_Private_View (S, E);
9549 Set_Is_Dispatching_Operation (S);
9551 -- Common case
9553 else
9554 Enter_Overloaded_Entity (S);
9555 Check_Dispatching_Operation (S, Empty);
9556 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9557 end if;
9559 return;
9560 end if;
9562 -- If there is no homonym then this is definitely not overriding
9564 if No (E) then
9565 Enter_Overloaded_Entity (S);
9566 Check_Dispatching_Operation (S, Empty);
9567 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9569 -- If subprogram has an explicit declaration, check whether it has an
9570 -- overriding indicator.
9572 if Comes_From_Source (S) then
9573 Check_Synchronized_Overriding (S, Overridden_Subp);
9575 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9576 -- it may have overridden some hidden inherited primitive. Update
9577 -- Overridden_Subp to avoid spurious errors when checking the
9578 -- overriding indicator.
9580 if Ada_Version >= Ada_2012
9581 and then No (Overridden_Subp)
9582 and then Is_Dispatching_Operation (S)
9583 and then Present (Overridden_Operation (S))
9584 then
9585 Overridden_Subp := Overridden_Operation (S);
9586 end if;
9588 Check_Overriding_Indicator
9589 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9590 end if;
9592 -- If there is a homonym that is not overloadable, then we have an
9593 -- error, except for the special cases checked explicitly below.
9595 elsif not Is_Overloadable (E) then
9597 -- Check for spurious conflict produced by a subprogram that has the
9598 -- same name as that of the enclosing generic package. The conflict
9599 -- occurs within an instance, between the subprogram and the renaming
9600 -- declaration for the package. After the subprogram, the package
9601 -- renaming declaration becomes hidden.
9603 if Ekind (E) = E_Package
9604 and then Present (Renamed_Object (E))
9605 and then Renamed_Object (E) = Current_Scope
9606 and then Nkind (Parent (Renamed_Object (E))) =
9607 N_Package_Specification
9608 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9609 then
9610 Set_Is_Hidden (E);
9611 Set_Is_Immediately_Visible (E, False);
9612 Enter_Overloaded_Entity (S);
9613 Set_Homonym (S, Homonym (E));
9614 Check_Dispatching_Operation (S, Empty);
9615 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9617 -- If the subprogram is implicit it is hidden by the previous
9618 -- declaration. However if it is dispatching, it must appear in the
9619 -- dispatch table anyway, because it can be dispatched to even if it
9620 -- cannot be called directly.
9622 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9623 Set_Scope (S, Current_Scope);
9625 if Is_Dispatching_Operation (Alias (S)) then
9626 Check_Dispatching_Operation (S, Empty);
9627 end if;
9629 return;
9631 else
9632 Error_Msg_Sloc := Sloc (E);
9634 -- Generate message, with useful additional warning if in generic
9636 if Is_Generic_Unit (E) then
9637 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9638 Error_Msg_N ("\& conflicts with declaration#", S);
9639 else
9640 Error_Msg_N ("& conflicts with declaration#", S);
9641 end if;
9643 return;
9644 end if;
9646 -- E exists and is overloadable
9648 else
9649 Check_Synchronized_Overriding (S, Overridden_Subp);
9651 -- Loop through E and its homonyms to determine if any of them is
9652 -- the candidate for overriding by S.
9654 while Present (E) loop
9656 -- Definitely not interesting if not in the current scope
9658 if Scope (E) /= Current_Scope then
9659 null;
9661 -- A function can overload the name of an abstract state. The
9662 -- state can be viewed as a function with a profile that cannot
9663 -- be matched by anything.
9665 elsif Ekind (S) = E_Function
9666 and then Ekind (E) = E_Abstract_State
9667 then
9668 Enter_Overloaded_Entity (S);
9669 return;
9671 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9672 -- procedures locate the internally generated spec. We enforce
9673 -- mode conformance since a tagged type may inherit from
9674 -- interfaces several null primitives which differ only in
9675 -- the mode of the formals.
9677 elsif not Comes_From_Source (S)
9678 and then Is_Null_Procedure (S)
9679 and then not Mode_Conformant (E, S)
9680 then
9681 null;
9683 -- Check if we have type conformance
9685 elsif Type_Conformant (E, S) then
9687 -- If the old and new entities have the same profile and one
9688 -- is not the body of the other, then this is an error, unless
9689 -- one of them is implicitly declared.
9691 -- There are some cases when both can be implicit, for example
9692 -- when both a literal and a function that overrides it are
9693 -- inherited in a derivation, or when an inherited operation
9694 -- of a tagged full type overrides the inherited operation of
9695 -- a private extension. Ada 83 had a special rule for the
9696 -- literal case. In Ada 95, the later implicit operation hides
9697 -- the former, and the literal is always the former. In the
9698 -- odd case where both are derived operations declared at the
9699 -- same point, both operations should be declared, and in that
9700 -- case we bypass the following test and proceed to the next
9701 -- part. This can only occur for certain obscure cases in
9702 -- instances, when an operation on a type derived from a formal
9703 -- private type does not override a homograph inherited from
9704 -- the actual. In subsequent derivations of such a type, the
9705 -- DT positions of these operations remain distinct, if they
9706 -- have been set.
9708 if Present (Alias (S))
9709 and then (No (Alias (E))
9710 or else Comes_From_Source (E)
9711 or else Is_Abstract_Subprogram (S)
9712 or else
9713 (Is_Dispatching_Operation (E)
9714 and then Is_Overriding_Alias (E, S)))
9715 and then Ekind (E) /= E_Enumeration_Literal
9716 then
9717 -- When an derived operation is overloaded it may be due to
9718 -- the fact that the full view of a private extension
9719 -- re-inherits. It has to be dealt with.
9721 if Is_Package_Or_Generic_Package (Current_Scope)
9722 and then In_Private_Part (Current_Scope)
9723 then
9724 Check_Operation_From_Private_View (S, E);
9725 end if;
9727 -- In any case the implicit operation remains hidden by the
9728 -- existing declaration, which is overriding. Indicate that
9729 -- E overrides the operation from which S is inherited.
9731 if Present (Alias (S)) then
9732 Set_Overridden_Operation (E, Alias (S));
9733 Inherit_Subprogram_Contract (E, Alias (S));
9735 else
9736 Set_Overridden_Operation (E, S);
9737 Inherit_Subprogram_Contract (E, S);
9738 end if;
9740 if Comes_From_Source (E) then
9741 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9742 end if;
9744 return;
9746 -- Within an instance, the renaming declarations for actual
9747 -- subprograms may become ambiguous, but they do not hide each
9748 -- other.
9750 elsif Ekind (E) /= E_Entry
9751 and then not Comes_From_Source (E)
9752 and then not Is_Generic_Instance (E)
9753 and then (Present (Alias (E))
9754 or else Is_Intrinsic_Subprogram (E))
9755 and then (not In_Instance
9756 or else No (Parent (E))
9757 or else Nkind (Unit_Declaration_Node (E)) /=
9758 N_Subprogram_Renaming_Declaration)
9759 then
9760 -- A subprogram child unit is not allowed to override an
9761 -- inherited subprogram (10.1.1(20)).
9763 if Is_Child_Unit (S) then
9764 Error_Msg_N
9765 ("child unit overrides inherited subprogram in parent",
9767 return;
9768 end if;
9770 if Is_Non_Overriding_Operation (E, S) then
9771 Enter_Overloaded_Entity (S);
9773 if No (Derived_Type)
9774 or else Is_Tagged_Type (Derived_Type)
9775 then
9776 Check_Dispatching_Operation (S, Empty);
9777 end if;
9779 return;
9780 end if;
9782 -- E is a derived operation or an internal operator which
9783 -- is being overridden. Remove E from further visibility.
9784 -- Furthermore, if E is a dispatching operation, it must be
9785 -- replaced in the list of primitive operations of its type
9786 -- (see Override_Dispatching_Operation).
9788 Overridden_Subp := E;
9790 declare
9791 Prev : Entity_Id;
9793 begin
9794 Prev := First_Entity (Current_Scope);
9795 while Present (Prev) and then Next_Entity (Prev) /= E loop
9796 Next_Entity (Prev);
9797 end loop;
9799 -- It is possible for E to be in the current scope and
9800 -- yet not in the entity chain. This can only occur in a
9801 -- generic context where E is an implicit concatenation
9802 -- in the formal part, because in a generic body the
9803 -- entity chain starts with the formals.
9805 -- In GNATprove mode, a wrapper for an operation with
9806 -- axiomatization may be a homonym of another declaration
9807 -- for an actual subprogram (needs refinement ???).
9809 if No (Prev) then
9810 if In_Instance
9811 and then GNATprove_Mode
9812 and then
9813 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9814 N_Subprogram_Renaming_Declaration
9815 then
9816 return;
9817 else
9818 pragma Assert (Chars (E) = Name_Op_Concat);
9819 null;
9820 end if;
9821 end if;
9823 -- E must be removed both from the entity_list of the
9824 -- current scope, and from the visibility chain.
9826 if Debug_Flag_E then
9827 Write_Str ("Override implicit operation ");
9828 Write_Int (Int (E));
9829 Write_Eol;
9830 end if;
9832 -- If E is a predefined concatenation, it stands for four
9833 -- different operations. As a result, a single explicit
9834 -- declaration does not hide it. In a possible ambiguous
9835 -- situation, Disambiguate chooses the user-defined op,
9836 -- so it is correct to retain the previous internal one.
9838 if Chars (E) /= Name_Op_Concat
9839 or else Ekind (E) /= E_Operator
9840 then
9841 -- For nondispatching derived operations that are
9842 -- overridden by a subprogram declared in the private
9843 -- part of a package, we retain the derived subprogram
9844 -- but mark it as not immediately visible. If the
9845 -- derived operation was declared in the visible part
9846 -- then this ensures that it will still be visible
9847 -- outside the package with the proper signature
9848 -- (calls from outside must also be directed to this
9849 -- version rather than the overriding one, unlike the
9850 -- dispatching case). Calls from inside the package
9851 -- will still resolve to the overriding subprogram
9852 -- since the derived one is marked as not visible
9853 -- within the package.
9855 -- If the private operation is dispatching, we achieve
9856 -- the overriding by keeping the implicit operation
9857 -- but setting its alias to be the overriding one. In
9858 -- this fashion the proper body is executed in all
9859 -- cases, but the original signature is used outside
9860 -- of the package.
9862 -- If the overriding is not in the private part, we
9863 -- remove the implicit operation altogether.
9865 if Is_Private_Declaration (S) then
9866 if not Is_Dispatching_Operation (E) then
9867 Set_Is_Immediately_Visible (E, False);
9868 else
9869 -- Work done in Override_Dispatching_Operation,
9870 -- so nothing else needs to be done here.
9872 null;
9873 end if;
9875 else
9876 -- Find predecessor of E in Homonym chain
9878 if E = Current_Entity (E) then
9879 Prev_Vis := Empty;
9880 else
9881 Prev_Vis := Current_Entity (E);
9882 while Homonym (Prev_Vis) /= E loop
9883 Prev_Vis := Homonym (Prev_Vis);
9884 end loop;
9885 end if;
9887 if Prev_Vis /= Empty then
9889 -- Skip E in the visibility chain
9891 Set_Homonym (Prev_Vis, Homonym (E));
9893 else
9894 Set_Name_Entity_Id (Chars (E), Homonym (E));
9895 end if;
9897 Set_Next_Entity (Prev, Next_Entity (E));
9899 if No (Next_Entity (Prev)) then
9900 Set_Last_Entity (Current_Scope, Prev);
9901 end if;
9902 end if;
9903 end if;
9905 Enter_Overloaded_Entity (S);
9907 -- For entities generated by Derive_Subprograms the
9908 -- overridden operation is the inherited primitive
9909 -- (which is available through the attribute alias).
9911 if not (Comes_From_Source (E))
9912 and then Is_Dispatching_Operation (E)
9913 and then Find_Dispatching_Type (E) =
9914 Find_Dispatching_Type (S)
9915 and then Present (Alias (E))
9916 and then Comes_From_Source (Alias (E))
9917 then
9918 Set_Overridden_Operation (S, Alias (E));
9919 Inherit_Subprogram_Contract (S, Alias (E));
9921 -- Normal case of setting entity as overridden
9923 -- Note: Static_Initialization and Overridden_Operation
9924 -- attributes use the same field in subprogram entities.
9925 -- Static_Initialization is only defined for internal
9926 -- initialization procedures, where Overridden_Operation
9927 -- is irrelevant. Therefore the setting of this attribute
9928 -- must check whether the target is an init_proc.
9930 elsif not Is_Init_Proc (S) then
9931 Set_Overridden_Operation (S, E);
9932 Inherit_Subprogram_Contract (S, E);
9933 end if;
9935 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9937 -- If S is a user-defined subprogram or a null procedure
9938 -- expanded to override an inherited null procedure, or a
9939 -- predefined dispatching primitive then indicate that E
9940 -- overrides the operation from which S is inherited.
9942 if Comes_From_Source (S)
9943 or else
9944 (Present (Parent (S))
9945 and then
9946 Nkind (Parent (S)) = N_Procedure_Specification
9947 and then
9948 Null_Present (Parent (S)))
9949 or else
9950 (Present (Alias (E))
9951 and then
9952 Is_Predefined_Dispatching_Operation (Alias (E)))
9953 then
9954 if Present (Alias (E)) then
9955 Set_Overridden_Operation (S, Alias (E));
9956 Inherit_Subprogram_Contract (S, Alias (E));
9957 end if;
9958 end if;
9960 if Is_Dispatching_Operation (E) then
9962 -- An overriding dispatching subprogram inherits the
9963 -- convention of the overridden subprogram (AI-117).
9965 Set_Convention (S, Convention (E));
9966 Check_Dispatching_Operation (S, E);
9968 else
9969 Check_Dispatching_Operation (S, Empty);
9970 end if;
9972 Check_For_Primitive_Subprogram
9973 (Is_Primitive_Subp, Is_Overriding => True);
9974 goto Check_Inequality;
9975 end;
9977 -- Apparent redeclarations in instances can occur when two
9978 -- formal types get the same actual type. The subprograms in
9979 -- in the instance are legal, even if not callable from the
9980 -- outside. Calls from within are disambiguated elsewhere.
9981 -- For dispatching operations in the visible part, the usual
9982 -- rules apply, and operations with the same profile are not
9983 -- legal (B830001).
9985 elsif (In_Instance_Visible_Part
9986 and then not Is_Dispatching_Operation (E))
9987 or else In_Instance_Not_Visible
9988 then
9989 null;
9991 -- Here we have a real error (identical profile)
9993 else
9994 Error_Msg_Sloc := Sloc (E);
9996 -- Avoid cascaded errors if the entity appears in
9997 -- subsequent calls.
9999 Set_Scope (S, Current_Scope);
10001 -- Generate error, with extra useful warning for the case
10002 -- of a generic instance with no completion.
10004 if Is_Generic_Instance (S)
10005 and then not Has_Completion (E)
10006 then
10007 Error_Msg_N
10008 ("instantiation cannot provide body for&", S);
10009 Error_Msg_N ("\& conflicts with declaration#", S);
10010 else
10011 Error_Msg_N ("& conflicts with declaration#", S);
10012 end if;
10014 return;
10015 end if;
10017 else
10018 -- If one subprogram has an access parameter and the other
10019 -- a parameter of an access type, calls to either might be
10020 -- ambiguous. Verify that parameters match except for the
10021 -- access parameter.
10023 if May_Hide_Profile then
10024 declare
10025 F1 : Entity_Id;
10026 F2 : Entity_Id;
10028 begin
10029 F1 := First_Formal (S);
10030 F2 := First_Formal (E);
10031 while Present (F1) and then Present (F2) loop
10032 if Is_Access_Type (Etype (F1)) then
10033 if not Is_Access_Type (Etype (F2))
10034 or else not Conforming_Types
10035 (Designated_Type (Etype (F1)),
10036 Designated_Type (Etype (F2)),
10037 Type_Conformant)
10038 then
10039 May_Hide_Profile := False;
10040 end if;
10042 elsif
10043 not Conforming_Types
10044 (Etype (F1), Etype (F2), Type_Conformant)
10045 then
10046 May_Hide_Profile := False;
10047 end if;
10049 Next_Formal (F1);
10050 Next_Formal (F2);
10051 end loop;
10053 if May_Hide_Profile
10054 and then No (F1)
10055 and then No (F2)
10056 then
10057 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10058 end if;
10059 end;
10060 end if;
10061 end if;
10063 E := Homonym (E);
10064 end loop;
10066 -- On exit, we know that S is a new entity
10068 Enter_Overloaded_Entity (S);
10069 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10070 Check_Overriding_Indicator
10071 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10073 -- Overloading is not allowed in SPARK, except for operators
10075 if Nkind (S) /= N_Defining_Operator_Symbol then
10076 Error_Msg_Sloc := Sloc (Homonym (S));
10077 Check_SPARK_05_Restriction
10078 ("overloading not allowed with entity#", S);
10079 end if;
10081 -- If S is a derived operation for an untagged type then by
10082 -- definition it's not a dispatching operation (even if the parent
10083 -- operation was dispatching), so Check_Dispatching_Operation is not
10084 -- called in that case.
10086 if No (Derived_Type)
10087 or else Is_Tagged_Type (Derived_Type)
10088 then
10089 Check_Dispatching_Operation (S, Empty);
10090 end if;
10091 end if;
10093 -- If this is a user-defined equality operator that is not a derived
10094 -- subprogram, create the corresponding inequality. If the operation is
10095 -- dispatching, the expansion is done elsewhere, and we do not create
10096 -- an explicit inequality operation.
10098 <<Check_Inequality>>
10099 if Chars (S) = Name_Op_Eq
10100 and then Etype (S) = Standard_Boolean
10101 and then Present (Parent (S))
10102 and then not Is_Dispatching_Operation (S)
10103 then
10104 Make_Inequality_Operator (S);
10105 Check_Untagged_Equality (S);
10106 end if;
10107 end New_Overloaded_Entity;
10109 ---------------------
10110 -- Process_Formals --
10111 ---------------------
10113 procedure Process_Formals
10114 (T : List_Id;
10115 Related_Nod : Node_Id)
10117 Param_Spec : Node_Id;
10118 Formal : Entity_Id;
10119 Formal_Type : Entity_Id;
10120 Default : Node_Id;
10121 Ptype : Entity_Id;
10123 Num_Out_Params : Nat := 0;
10124 First_Out_Param : Entity_Id := Empty;
10125 -- Used for setting Is_Only_Out_Parameter
10127 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10128 -- Determine whether an access type designates a type coming from a
10129 -- limited view.
10131 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10132 -- Check whether the default has a class-wide type. After analysis the
10133 -- default has the type of the formal, so we must also check explicitly
10134 -- for an access attribute.
10136 ----------------------------------
10137 -- Designates_From_Limited_With --
10138 ----------------------------------
10140 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10141 Desig : Entity_Id := Typ;
10143 begin
10144 if Is_Access_Type (Desig) then
10145 Desig := Directly_Designated_Type (Desig);
10146 end if;
10148 if Is_Class_Wide_Type (Desig) then
10149 Desig := Root_Type (Desig);
10150 end if;
10152 return
10153 Ekind (Desig) = E_Incomplete_Type
10154 and then From_Limited_With (Desig);
10155 end Designates_From_Limited_With;
10157 ---------------------------
10158 -- Is_Class_Wide_Default --
10159 ---------------------------
10161 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10162 begin
10163 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10164 or else (Nkind (D) = N_Attribute_Reference
10165 and then Attribute_Name (D) = Name_Access
10166 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10167 end Is_Class_Wide_Default;
10169 -- Start of processing for Process_Formals
10171 begin
10172 -- In order to prevent premature use of the formals in the same formal
10173 -- part, the Ekind is left undefined until all default expressions are
10174 -- analyzed. The Ekind is established in a separate loop at the end.
10176 Param_Spec := First (T);
10177 while Present (Param_Spec) loop
10178 Formal := Defining_Identifier (Param_Spec);
10179 Set_Never_Set_In_Source (Formal, True);
10180 Enter_Name (Formal);
10182 -- Case of ordinary parameters
10184 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10185 Find_Type (Parameter_Type (Param_Spec));
10186 Ptype := Parameter_Type (Param_Spec);
10188 if Ptype = Error then
10189 goto Continue;
10190 end if;
10192 Formal_Type := Entity (Ptype);
10194 if Is_Incomplete_Type (Formal_Type)
10195 or else
10196 (Is_Class_Wide_Type (Formal_Type)
10197 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10198 then
10199 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10200 -- primitive operations, as long as their completion is
10201 -- in the same declarative part. If in the private part
10202 -- this means that the type cannot be a Taft-amendment type.
10203 -- Check is done on package exit. For access to subprograms,
10204 -- the use is legal for Taft-amendment types.
10206 -- Ada 2012: tagged incomplete types are allowed as generic
10207 -- formal types. They do not introduce dependencies and the
10208 -- corresponding generic subprogram does not have a delayed
10209 -- freeze, because it does not need a freeze node. However,
10210 -- it is still the case that untagged incomplete types cannot
10211 -- be Taft-amendment types and must be completed in private
10212 -- part, so the subprogram must appear in the list of private
10213 -- dependents of the type.
10215 if Is_Tagged_Type (Formal_Type)
10216 or else (Ada_Version >= Ada_2012
10217 and then not From_Limited_With (Formal_Type)
10218 and then not Is_Generic_Type (Formal_Type))
10219 then
10220 if Ekind (Scope (Current_Scope)) = E_Package
10221 and then not Is_Generic_Type (Formal_Type)
10222 and then not Is_Class_Wide_Type (Formal_Type)
10223 then
10224 if not Nkind_In
10225 (Parent (T), N_Access_Function_Definition,
10226 N_Access_Procedure_Definition)
10227 then
10228 Append_Elmt
10229 (Current_Scope,
10230 To => Private_Dependents (Base_Type (Formal_Type)));
10232 -- Freezing is delayed to ensure that Register_Prim
10233 -- will get called for this operation, which is needed
10234 -- in cases where static dispatch tables aren't built.
10235 -- (Note that the same is done for controlling access
10236 -- parameter cases in function Access_Definition.)
10238 if not Is_Thunk (Current_Scope) then
10239 Set_Has_Delayed_Freeze (Current_Scope);
10240 end if;
10241 end if;
10242 end if;
10244 -- Special handling of Value_Type for CIL case
10246 elsif Is_Value_Type (Formal_Type) then
10247 null;
10249 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10250 N_Access_Procedure_Definition)
10251 then
10252 -- AI05-0151: Tagged incomplete types are allowed in all
10253 -- formal parts. Untagged incomplete types are not allowed
10254 -- in bodies. Limited views of either kind are not allowed
10255 -- if there is no place at which the non-limited view can
10256 -- become available.
10258 -- Incomplete formal untagged types are not allowed in
10259 -- subprogram bodies (but are legal in their declarations).
10261 if Is_Generic_Type (Formal_Type)
10262 and then not Is_Tagged_Type (Formal_Type)
10263 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10264 then
10265 Error_Msg_N
10266 ("invalid use of formal incomplete type", Param_Spec);
10268 elsif Ada_Version >= Ada_2012 then
10269 if Is_Tagged_Type (Formal_Type)
10270 and then (not From_Limited_With (Formal_Type)
10271 or else not In_Package_Body)
10272 then
10273 null;
10275 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10276 N_Accept_Alternative,
10277 N_Entry_Body,
10278 N_Subprogram_Body)
10279 then
10280 Error_Msg_NE
10281 ("invalid use of untagged incomplete type&",
10282 Ptype, Formal_Type);
10283 end if;
10285 else
10286 Error_Msg_NE
10287 ("invalid use of incomplete type&",
10288 Param_Spec, Formal_Type);
10290 -- Further checks on the legality of incomplete types
10291 -- in formal parts are delayed until the freeze point
10292 -- of the enclosing subprogram or access to subprogram.
10293 end if;
10294 end if;
10296 elsif Ekind (Formal_Type) = E_Void then
10297 Error_Msg_NE
10298 ("premature use of&",
10299 Parameter_Type (Param_Spec), Formal_Type);
10300 end if;
10302 -- Ada 2012 (AI-142): Handle aliased parameters
10304 if Ada_Version >= Ada_2012
10305 and then Aliased_Present (Param_Spec)
10306 then
10307 Set_Is_Aliased (Formal);
10308 end if;
10310 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10311 -- declaration corresponding to the null-excluding type of the
10312 -- formal in the enclosing scope. Finally, replace the parameter
10313 -- type of the formal with the internal subtype.
10315 if Ada_Version >= Ada_2005
10316 and then Null_Exclusion_Present (Param_Spec)
10317 then
10318 if not Is_Access_Type (Formal_Type) then
10319 Error_Msg_N
10320 ("`NOT NULL` allowed only for an access type", Param_Spec);
10322 else
10323 if Can_Never_Be_Null (Formal_Type)
10324 and then Comes_From_Source (Related_Nod)
10325 then
10326 Error_Msg_NE
10327 ("`NOT NULL` not allowed (& already excludes null)",
10328 Param_Spec, Formal_Type);
10329 end if;
10331 Formal_Type :=
10332 Create_Null_Excluding_Itype
10333 (T => Formal_Type,
10334 Related_Nod => Related_Nod,
10335 Scope_Id => Scope (Current_Scope));
10337 -- If the designated type of the itype is an itype that is
10338 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10339 -- on the access subtype, to prevent order-of-elaboration
10340 -- issues in the backend.
10342 -- Example:
10343 -- type T is access procedure;
10344 -- procedure Op (O : not null T);
10346 if Is_Itype (Directly_Designated_Type (Formal_Type))
10347 and then
10348 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10349 then
10350 Set_Has_Delayed_Freeze (Formal_Type);
10351 end if;
10352 end if;
10353 end if;
10355 -- An access formal type
10357 else
10358 Formal_Type :=
10359 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10361 -- No need to continue if we already notified errors
10363 if not Present (Formal_Type) then
10364 return;
10365 end if;
10367 -- Ada 2005 (AI-254)
10369 declare
10370 AD : constant Node_Id :=
10371 Access_To_Subprogram_Definition
10372 (Parameter_Type (Param_Spec));
10373 begin
10374 if Present (AD) and then Protected_Present (AD) then
10375 Formal_Type :=
10376 Replace_Anonymous_Access_To_Protected_Subprogram
10377 (Param_Spec);
10378 end if;
10379 end;
10380 end if;
10382 Set_Etype (Formal, Formal_Type);
10384 -- Deal with default expression if present
10386 Default := Expression (Param_Spec);
10388 if Present (Default) then
10389 Check_SPARK_05_Restriction
10390 ("default expression is not allowed", Default);
10392 if Out_Present (Param_Spec) then
10393 Error_Msg_N
10394 ("default initialization only allowed for IN parameters",
10395 Param_Spec);
10396 end if;
10398 -- Do the special preanalysis of the expression (see section on
10399 -- "Handling of Default Expressions" in the spec of package Sem).
10401 Preanalyze_Spec_Expression (Default, Formal_Type);
10403 -- An access to constant cannot be the default for
10404 -- an access parameter that is an access to variable.
10406 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10407 and then not Is_Access_Constant (Formal_Type)
10408 and then Is_Access_Type (Etype (Default))
10409 and then Is_Access_Constant (Etype (Default))
10410 then
10411 Error_Msg_N
10412 ("formal that is access to variable cannot be initialized "
10413 & "with an access-to-constant expression", Default);
10414 end if;
10416 -- Check that the designated type of an access parameter's default
10417 -- is not a class-wide type unless the parameter's designated type
10418 -- is also class-wide.
10420 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10421 and then not Designates_From_Limited_With (Formal_Type)
10422 and then Is_Class_Wide_Default (Default)
10423 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10424 then
10425 Error_Msg_N
10426 ("access to class-wide expression not allowed here", Default);
10427 end if;
10429 -- Check incorrect use of dynamically tagged expressions
10431 if Is_Tagged_Type (Formal_Type) then
10432 Check_Dynamically_Tagged_Expression
10433 (Expr => Default,
10434 Typ => Formal_Type,
10435 Related_Nod => Default);
10436 end if;
10437 end if;
10439 -- Ada 2005 (AI-231): Static checks
10441 if Ada_Version >= Ada_2005
10442 and then Is_Access_Type (Etype (Formal))
10443 and then Can_Never_Be_Null (Etype (Formal))
10444 then
10445 Null_Exclusion_Static_Checks (Param_Spec);
10446 end if;
10448 -- The following checks are relevant when SPARK_Mode is on as these
10449 -- are not standard Ada legality rules.
10451 if SPARK_Mode = On then
10452 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10454 -- A function cannot have a parameter of mode IN OUT or OUT
10455 -- (SPARK RM 6.1).
10457 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10458 Error_Msg_N
10459 ("function cannot have parameter of mode `OUT` or "
10460 & "`IN OUT`", Formal);
10462 -- A function cannot have an effectively volatile formal
10463 -- parameter (SPARK RM 7.1.3(10)).
10465 elsif Is_Effectively_Volatile (Formal) then
10466 Error_Msg_N
10467 ("function cannot have a volatile formal parameter",
10468 Formal);
10469 end if;
10471 -- A procedure cannot have an effectively volatile formal
10472 -- parameter of mode IN because it behaves as a constant
10473 -- (SPARK RM 7.1.3(6)).
10475 elsif Ekind (Scope (Formal)) = E_Procedure
10476 and then Ekind (Formal) = E_In_Parameter
10477 and then Is_Effectively_Volatile (Formal)
10478 then
10479 Error_Msg_N
10480 ("formal parameter of mode `IN` cannot be volatile", Formal);
10481 end if;
10482 end if;
10484 <<Continue>>
10485 Next (Param_Spec);
10486 end loop;
10488 -- If this is the formal part of a function specification, analyze the
10489 -- subtype mark in the context where the formals are visible but not
10490 -- yet usable, and may hide outer homographs.
10492 if Nkind (Related_Nod) = N_Function_Specification then
10493 Analyze_Return_Type (Related_Nod);
10494 end if;
10496 -- Now set the kind (mode) of each formal
10498 Param_Spec := First (T);
10499 while Present (Param_Spec) loop
10500 Formal := Defining_Identifier (Param_Spec);
10501 Set_Formal_Mode (Formal);
10503 if Ekind (Formal) = E_In_Parameter then
10504 Set_Default_Value (Formal, Expression (Param_Spec));
10506 if Present (Expression (Param_Spec)) then
10507 Default := Expression (Param_Spec);
10509 if Is_Scalar_Type (Etype (Default)) then
10510 if Nkind (Parameter_Type (Param_Spec)) /=
10511 N_Access_Definition
10512 then
10513 Formal_Type := Entity (Parameter_Type (Param_Spec));
10514 else
10515 Formal_Type :=
10516 Access_Definition
10517 (Related_Nod, Parameter_Type (Param_Spec));
10518 end if;
10520 Apply_Scalar_Range_Check (Default, Formal_Type);
10521 end if;
10522 end if;
10524 elsif Ekind (Formal) = E_Out_Parameter then
10525 Num_Out_Params := Num_Out_Params + 1;
10527 if Num_Out_Params = 1 then
10528 First_Out_Param := Formal;
10529 end if;
10531 elsif Ekind (Formal) = E_In_Out_Parameter then
10532 Num_Out_Params := Num_Out_Params + 1;
10533 end if;
10535 -- Skip remaining processing if formal type was in error
10537 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10538 goto Next_Parameter;
10539 end if;
10541 -- Force call by reference if aliased
10543 if Is_Aliased (Formal) then
10544 Set_Mechanism (Formal, By_Reference);
10546 -- Warn if user asked this to be passed by copy
10548 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10549 Error_Msg_N
10550 ("cannot pass aliased parameter & by copy??", Formal);
10551 end if;
10553 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10555 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10556 Set_Mechanism (Formal, By_Copy);
10558 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10559 Set_Mechanism (Formal, By_Reference);
10560 end if;
10562 <<Next_Parameter>>
10563 Next (Param_Spec);
10564 end loop;
10566 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10567 Set_Is_Only_Out_Parameter (First_Out_Param);
10568 end if;
10569 end Process_Formals;
10571 ----------------------------
10572 -- Reference_Body_Formals --
10573 ----------------------------
10575 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10576 Fs : Entity_Id;
10577 Fb : Entity_Id;
10579 begin
10580 if Error_Posted (Spec) then
10581 return;
10582 end if;
10584 -- Iterate over both lists. They may be of different lengths if the two
10585 -- specs are not conformant.
10587 Fs := First_Formal (Spec);
10588 Fb := First_Formal (Bod);
10589 while Present (Fs) and then Present (Fb) loop
10590 Generate_Reference (Fs, Fb, 'b');
10592 if Style_Check then
10593 Style.Check_Identifier (Fb, Fs);
10594 end if;
10596 Set_Spec_Entity (Fb, Fs);
10597 Set_Referenced (Fs, False);
10598 Next_Formal (Fs);
10599 Next_Formal (Fb);
10600 end loop;
10601 end Reference_Body_Formals;
10603 -------------------------
10604 -- Set_Actual_Subtypes --
10605 -------------------------
10607 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10608 Decl : Node_Id;
10609 Formal : Entity_Id;
10610 T : Entity_Id;
10611 First_Stmt : Node_Id := Empty;
10612 AS_Needed : Boolean;
10614 begin
10615 -- If this is an empty initialization procedure, no need to create
10616 -- actual subtypes (small optimization).
10618 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10619 return;
10620 end if;
10622 Formal := First_Formal (Subp);
10623 while Present (Formal) loop
10624 T := Etype (Formal);
10626 -- We never need an actual subtype for a constrained formal
10628 if Is_Constrained (T) then
10629 AS_Needed := False;
10631 -- If we have unknown discriminants, then we do not need an actual
10632 -- subtype, or more accurately we cannot figure it out. Note that
10633 -- all class-wide types have unknown discriminants.
10635 elsif Has_Unknown_Discriminants (T) then
10636 AS_Needed := False;
10638 -- At this stage we have an unconstrained type that may need an
10639 -- actual subtype. For sure the actual subtype is needed if we have
10640 -- an unconstrained array type.
10642 elsif Is_Array_Type (T) then
10643 AS_Needed := True;
10645 -- The only other case needing an actual subtype is an unconstrained
10646 -- record type which is an IN parameter (we cannot generate actual
10647 -- subtypes for the OUT or IN OUT case, since an assignment can
10648 -- change the discriminant values. However we exclude the case of
10649 -- initialization procedures, since discriminants are handled very
10650 -- specially in this context, see the section entitled "Handling of
10651 -- Discriminants" in Einfo.
10653 -- We also exclude the case of Discrim_SO_Functions (functions used
10654 -- in front end layout mode for size/offset values), since in such
10655 -- functions only discriminants are referenced, and not only are such
10656 -- subtypes not needed, but they cannot always be generated, because
10657 -- of order of elaboration issues.
10659 elsif Is_Record_Type (T)
10660 and then Ekind (Formal) = E_In_Parameter
10661 and then Chars (Formal) /= Name_uInit
10662 and then not Is_Unchecked_Union (T)
10663 and then not Is_Discrim_SO_Function (Subp)
10664 then
10665 AS_Needed := True;
10667 -- All other cases do not need an actual subtype
10669 else
10670 AS_Needed := False;
10671 end if;
10673 -- Generate actual subtypes for unconstrained arrays and
10674 -- unconstrained discriminated records.
10676 if AS_Needed then
10677 if Nkind (N) = N_Accept_Statement then
10679 -- If expansion is active, the formal is replaced by a local
10680 -- variable that renames the corresponding entry of the
10681 -- parameter block, and it is this local variable that may
10682 -- require an actual subtype.
10684 if Expander_Active then
10685 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10686 else
10687 Decl := Build_Actual_Subtype (T, Formal);
10688 end if;
10690 if Present (Handled_Statement_Sequence (N)) then
10691 First_Stmt :=
10692 First (Statements (Handled_Statement_Sequence (N)));
10693 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10694 Mark_Rewrite_Insertion (Decl);
10695 else
10696 -- If the accept statement has no body, there will be no
10697 -- reference to the actuals, so no need to compute actual
10698 -- subtypes.
10700 return;
10701 end if;
10703 else
10704 Decl := Build_Actual_Subtype (T, Formal);
10705 Prepend (Decl, Declarations (N));
10706 Mark_Rewrite_Insertion (Decl);
10707 end if;
10709 -- The declaration uses the bounds of an existing object, and
10710 -- therefore needs no constraint checks.
10712 Analyze (Decl, Suppress => All_Checks);
10714 -- We need to freeze manually the generated type when it is
10715 -- inserted anywhere else than in a declarative part.
10717 if Present (First_Stmt) then
10718 Insert_List_Before_And_Analyze (First_Stmt,
10719 Freeze_Entity (Defining_Identifier (Decl), N));
10721 -- Ditto if the type has a dynamic predicate, because the
10722 -- generated function will mention the actual subtype.
10724 elsif Has_Dynamic_Predicate_Aspect (T) then
10725 Insert_List_Before_And_Analyze (Decl,
10726 Freeze_Entity (Defining_Identifier (Decl), N));
10727 end if;
10729 if Nkind (N) = N_Accept_Statement
10730 and then Expander_Active
10731 then
10732 Set_Actual_Subtype (Renamed_Object (Formal),
10733 Defining_Identifier (Decl));
10734 else
10735 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10736 end if;
10737 end if;
10739 Next_Formal (Formal);
10740 end loop;
10741 end Set_Actual_Subtypes;
10743 ---------------------
10744 -- Set_Formal_Mode --
10745 ---------------------
10747 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10748 Spec : constant Node_Id := Parent (Formal_Id);
10750 begin
10751 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10752 -- since we ensure that corresponding actuals are always valid at the
10753 -- point of the call.
10755 if Out_Present (Spec) then
10756 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10758 -- [IN] OUT parameters allowed for functions in Ada 2012
10760 if Ada_Version >= Ada_2012 then
10762 -- Even in Ada 2012 operators can only have IN parameters
10764 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10765 Error_Msg_N ("operators can only have IN parameters", Spec);
10766 end if;
10768 if In_Present (Spec) then
10769 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10770 else
10771 Set_Ekind (Formal_Id, E_Out_Parameter);
10772 end if;
10774 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10776 -- But not in earlier versions of Ada
10778 else
10779 Error_Msg_N ("functions can only have IN parameters", Spec);
10780 Set_Ekind (Formal_Id, E_In_Parameter);
10781 end if;
10783 elsif In_Present (Spec) then
10784 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10786 else
10787 Set_Ekind (Formal_Id, E_Out_Parameter);
10788 Set_Never_Set_In_Source (Formal_Id, True);
10789 Set_Is_True_Constant (Formal_Id, False);
10790 Set_Current_Value (Formal_Id, Empty);
10791 end if;
10793 else
10794 Set_Ekind (Formal_Id, E_In_Parameter);
10795 end if;
10797 -- Set Is_Known_Non_Null for access parameters since the language
10798 -- guarantees that access parameters are always non-null. We also set
10799 -- Can_Never_Be_Null, since there is no way to change the value.
10801 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10803 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10804 -- null; In Ada 2005, only if then null_exclusion is explicit.
10806 if Ada_Version < Ada_2005
10807 or else Can_Never_Be_Null (Etype (Formal_Id))
10808 then
10809 Set_Is_Known_Non_Null (Formal_Id);
10810 Set_Can_Never_Be_Null (Formal_Id);
10811 end if;
10813 -- Ada 2005 (AI-231): Null-exclusion access subtype
10815 elsif Is_Access_Type (Etype (Formal_Id))
10816 and then Can_Never_Be_Null (Etype (Formal_Id))
10817 then
10818 Set_Is_Known_Non_Null (Formal_Id);
10820 -- We can also set Can_Never_Be_Null (thus preventing some junk
10821 -- access checks) for the case of an IN parameter, which cannot
10822 -- be changed, or for an IN OUT parameter, which can be changed but
10823 -- not to a null value. But for an OUT parameter, the initial value
10824 -- passed in can be null, so we can't set this flag in that case.
10826 if Ekind (Formal_Id) /= E_Out_Parameter then
10827 Set_Can_Never_Be_Null (Formal_Id);
10828 end if;
10829 end if;
10831 Set_Mechanism (Formal_Id, Default_Mechanism);
10832 Set_Formal_Validity (Formal_Id);
10833 end Set_Formal_Mode;
10835 -------------------------
10836 -- Set_Formal_Validity --
10837 -------------------------
10839 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10840 begin
10841 -- If no validity checking, then we cannot assume anything about the
10842 -- validity of parameters, since we do not know there is any checking
10843 -- of the validity on the call side.
10845 if not Validity_Checks_On then
10846 return;
10848 -- If validity checking for parameters is enabled, this means we are
10849 -- not supposed to make any assumptions about argument values.
10851 elsif Validity_Check_Parameters then
10852 return;
10854 -- If we are checking in parameters, we will assume that the caller is
10855 -- also checking parameters, so we can assume the parameter is valid.
10857 elsif Ekind (Formal_Id) = E_In_Parameter
10858 and then Validity_Check_In_Params
10859 then
10860 Set_Is_Known_Valid (Formal_Id, True);
10862 -- Similar treatment for IN OUT parameters
10864 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10865 and then Validity_Check_In_Out_Params
10866 then
10867 Set_Is_Known_Valid (Formal_Id, True);
10868 end if;
10869 end Set_Formal_Validity;
10871 ------------------------
10872 -- Subtype_Conformant --
10873 ------------------------
10875 function Subtype_Conformant
10876 (New_Id : Entity_Id;
10877 Old_Id : Entity_Id;
10878 Skip_Controlling_Formals : Boolean := False) return Boolean
10880 Result : Boolean;
10881 begin
10882 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10883 Skip_Controlling_Formals => Skip_Controlling_Formals);
10884 return Result;
10885 end Subtype_Conformant;
10887 ---------------------
10888 -- Type_Conformant --
10889 ---------------------
10891 function Type_Conformant
10892 (New_Id : Entity_Id;
10893 Old_Id : Entity_Id;
10894 Skip_Controlling_Formals : Boolean := False) return Boolean
10896 Result : Boolean;
10897 begin
10898 May_Hide_Profile := False;
10899 Check_Conformance
10900 (New_Id, Old_Id, Type_Conformant, False, Result,
10901 Skip_Controlling_Formals => Skip_Controlling_Formals);
10902 return Result;
10903 end Type_Conformant;
10905 -------------------------------
10906 -- Valid_Operator_Definition --
10907 -------------------------------
10909 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10910 N : Integer := 0;
10911 F : Entity_Id;
10912 Id : constant Name_Id := Chars (Designator);
10913 N_OK : Boolean;
10915 begin
10916 F := First_Formal (Designator);
10917 while Present (F) loop
10918 N := N + 1;
10920 if Present (Default_Value (F)) then
10921 Error_Msg_N
10922 ("default values not allowed for operator parameters",
10923 Parent (F));
10925 -- For function instantiations that are operators, we must check
10926 -- separately that the corresponding generic only has in-parameters.
10927 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10928 -- an error could not arise in earlier versions of the language.
10930 elsif Ekind (F) /= E_In_Parameter then
10931 Error_Msg_N ("operators can only have IN parameters", F);
10932 end if;
10934 Next_Formal (F);
10935 end loop;
10937 -- Verify that user-defined operators have proper number of arguments
10938 -- First case of operators which can only be unary
10940 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10941 N_OK := (N = 1);
10943 -- Case of operators which can be unary or binary
10945 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10946 N_OK := (N in 1 .. 2);
10948 -- All other operators can only be binary
10950 else
10951 N_OK := (N = 2);
10952 end if;
10954 if not N_OK then
10955 Error_Msg_N
10956 ("incorrect number of arguments for operator", Designator);
10957 end if;
10959 if Id = Name_Op_Ne
10960 and then Base_Type (Etype (Designator)) = Standard_Boolean
10961 and then not Is_Intrinsic_Subprogram (Designator)
10962 then
10963 Error_Msg_N
10964 ("explicit definition of inequality not allowed", Designator);
10965 end if;
10966 end Valid_Operator_Definition;
10968 end Sem_Ch6;