2015-05-12 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blobc4c47a5e96895df349b370f7e94009fb7021599b
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);
2924 begin
2925 if From_Limited_With (Typ) and then Has_Non_Limited_View (Typ) then
2926 Set_Etype (Id, Non_Limited_View (Typ));
2927 end if;
2928 end Detect_And_Exchange;
2930 -- Local variables
2932 Formal : Entity_Id;
2934 -- Start of processing for Exchange_Limited_Views
2936 begin
2937 if No (Subp_Id) then
2938 return;
2940 -- Do not process subprogram bodies as they already use the non-
2941 -- limited view of types.
2943 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2944 return;
2945 end if;
2947 -- Examine all formals and swap views when applicable
2949 Formal := First_Formal (Subp_Id);
2950 while Present (Formal) loop
2951 Detect_And_Exchange (Formal);
2953 Next_Formal (Formal);
2954 end loop;
2956 -- Process the return type of a function
2958 if Ekind (Subp_Id) = E_Function then
2959 Detect_And_Exchange (Subp_Id);
2960 end if;
2961 end Exchange_Limited_Views;
2963 -------------------------------------
2964 -- Is_Private_Concurrent_Primitive --
2965 -------------------------------------
2967 function Is_Private_Concurrent_Primitive
2968 (Subp_Id : Entity_Id) return Boolean
2970 Formal_Typ : Entity_Id;
2972 begin
2973 if Present (First_Formal (Subp_Id)) then
2974 Formal_Typ := Etype (First_Formal (Subp_Id));
2976 if Is_Concurrent_Record_Type (Formal_Typ) then
2977 if Is_Class_Wide_Type (Formal_Typ) then
2978 Formal_Typ := Root_Type (Formal_Typ);
2979 end if;
2981 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2982 end if;
2984 -- The type of the first formal is a concurrent tagged type with
2985 -- a private view.
2987 return
2988 Is_Concurrent_Type (Formal_Typ)
2989 and then Is_Tagged_Type (Formal_Typ)
2990 and then Has_Private_Declaration (Formal_Typ);
2991 end if;
2993 return False;
2994 end Is_Private_Concurrent_Primitive;
2996 ----------------------------
2997 -- Set_Trivial_Subprogram --
2998 ----------------------------
3000 procedure Set_Trivial_Subprogram (N : Node_Id) is
3001 Nxt : constant Node_Id := Next (N);
3003 begin
3004 Set_Is_Trivial_Subprogram (Body_Id);
3006 if Present (Spec_Id) then
3007 Set_Is_Trivial_Subprogram (Spec_Id);
3008 end if;
3010 if Present (Nxt)
3011 and then Nkind (Nxt) = N_Simple_Return_Statement
3012 and then No (Next (Nxt))
3013 and then Present (Expression (Nxt))
3014 and then Is_Entity_Name (Expression (Nxt))
3015 then
3016 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3017 end if;
3018 end Set_Trivial_Subprogram;
3020 ---------------------------------
3021 -- Verify_Overriding_Indicator --
3022 ---------------------------------
3024 procedure Verify_Overriding_Indicator is
3025 begin
3026 if Must_Override (Body_Spec) then
3027 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3028 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3029 then
3030 null;
3032 elsif not Present (Overridden_Operation (Spec_Id)) then
3033 Error_Msg_NE
3034 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3036 -- Overriding indicators aren't allowed for protected subprogram
3037 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3038 -- this to a warning if -gnatd.E is enabled.
3040 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3041 Error_Msg_Warn := Error_To_Warning;
3042 Error_Msg_N
3043 ("<<overriding indicator not allowed for protected "
3044 & "subprogram body", Body_Spec);
3045 end if;
3047 elsif Must_Not_Override (Body_Spec) then
3048 if Present (Overridden_Operation (Spec_Id)) then
3049 Error_Msg_NE
3050 ("subprogram& overrides inherited operation",
3051 Body_Spec, Spec_Id);
3053 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3054 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3055 then
3056 Error_Msg_NE
3057 ("subprogram& overrides predefined operator ",
3058 Body_Spec, Spec_Id);
3060 -- Overriding indicators aren't allowed for protected subprogram
3061 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3062 -- this to a warning if -gnatd.E is enabled.
3064 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3065 Error_Msg_Warn := Error_To_Warning;
3067 Error_Msg_N
3068 ("<<overriding indicator not allowed "
3069 & "for protected subprogram body", Body_Spec);
3071 -- If this is not a primitive operation, then the overriding
3072 -- indicator is altogether illegal.
3074 elsif not Is_Primitive (Spec_Id) then
3075 Error_Msg_N
3076 ("overriding indicator only allowed "
3077 & "if subprogram is primitive", Body_Spec);
3078 end if;
3080 -- If checking the style rule and the operation overrides, then
3081 -- issue a warning about a missing overriding_indicator. Protected
3082 -- subprogram bodies are excluded from this style checking, since
3083 -- they aren't primitives (even though their declarations can
3084 -- override) and aren't allowed to have an overriding_indicator.
3086 elsif Style_Check
3087 and then Present (Overridden_Operation (Spec_Id))
3088 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3089 then
3090 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3091 Style.Missing_Overriding (N, Body_Id);
3093 elsif Style_Check
3094 and then Can_Override_Operator (Spec_Id)
3095 and then not Is_Predefined_File_Name
3096 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3097 then
3098 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3099 Style.Missing_Overriding (N, Body_Id);
3100 end if;
3101 end Verify_Overriding_Indicator;
3103 -- Start of processing for Analyze_Subprogram_Body_Helper
3105 begin
3106 -- Generic subprograms are handled separately. They always have a
3107 -- generic specification. Determine whether current scope has a
3108 -- previous declaration.
3110 -- If the subprogram body is defined within an instance of the same
3111 -- name, the instance appears as a package renaming, and will be hidden
3112 -- within the subprogram.
3114 if Present (Prev_Id)
3115 and then not Is_Overloadable (Prev_Id)
3116 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3117 or else Comes_From_Source (Prev_Id))
3118 then
3119 if Is_Generic_Subprogram (Prev_Id) then
3120 Spec_Id := Prev_Id;
3122 -- The corresponding spec may be subject to pragma Ghost with
3123 -- policy Ignore. Set the mode now to ensure that any nodes
3124 -- generated during analysis and expansion are properly flagged
3125 -- as ignored Ghost.
3127 Set_Ghost_Mode (N, Spec_Id);
3128 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3129 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3131 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3133 if Nkind (N) = N_Subprogram_Body then
3134 HSS := Handled_Statement_Sequence (N);
3135 Check_Missing_Return;
3136 end if;
3138 return;
3140 else
3141 -- Previous entity conflicts with subprogram name. Attempting to
3142 -- enter name will post error.
3144 Enter_Name (Body_Id);
3145 return;
3146 end if;
3148 -- Non-generic case, find the subprogram declaration, if one was seen,
3149 -- or enter new overloaded entity in the current scope. If the
3150 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3151 -- part of the context of one of its subunits. No need to redo the
3152 -- analysis.
3154 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3155 return;
3157 else
3158 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3160 if Nkind (N) = N_Subprogram_Body_Stub
3161 or else No (Corresponding_Spec (N))
3162 then
3163 if Is_Private_Concurrent_Primitive (Body_Id) then
3164 Spec_Id := Disambiguate_Spec;
3166 -- The corresponding spec may be subject to pragma Ghost with
3167 -- policy Ignore. Set the mode now to ensure that any nodes
3168 -- generated during analysis and expansion are properly flagged
3169 -- as ignored Ghost.
3171 Set_Ghost_Mode (N, Spec_Id);
3173 else
3174 Spec_Id := Find_Corresponding_Spec (N);
3176 -- The corresponding spec may be subject to pragma Ghost with
3177 -- policy Ignore. Set the mode now to ensure that any nodes
3178 -- generated during analysis and expansion are properly flagged
3179 -- as ignored Ghost.
3181 Set_Ghost_Mode (N, Spec_Id);
3183 -- In GNATprove mode, if the body has no previous spec, create
3184 -- one so that the inlining machinery can operate properly.
3185 -- Transfer aspects, if any, to the new spec, so that they
3186 -- are legal and can be processed ahead of the body.
3187 -- We make two copies of the given spec, one for the new
3188 -- declaration, and one for the body.
3190 if No (Spec_Id) and then GNATprove_Mode
3192 -- Inlining does not apply during pre-analysis of code
3194 and then Full_Analysis
3196 -- Inlining only applies to full bodies, not stubs
3198 and then Nkind (N) /= N_Subprogram_Body_Stub
3200 -- Inlining only applies to bodies in the source code, not to
3201 -- those generated by the compiler. In particular, expression
3202 -- functions, whose body is generated by the compiler, are
3203 -- treated specially by GNATprove.
3205 and then Comes_From_Source (Body_Id)
3207 -- This cannot be done for a compilation unit, which is not
3208 -- in a context where we can insert a new spec.
3210 and then Is_List_Member (N)
3212 -- Inlining only applies to subprograms without contracts,
3213 -- as a contract is a sign that GNATprove should perform a
3214 -- modular analysis of the subprogram instead of a contextual
3215 -- analysis at each call site. The same test is performed in
3216 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3217 -- here in another form (because the contract has not
3218 -- been attached to the body) to avoid frontend errors in
3219 -- case pragmas are used instead of aspects, because the
3220 -- corresponding pragmas in the body would not be transferred
3221 -- to the spec, leading to legality errors.
3223 and then not Body_Has_Contract
3224 then
3225 declare
3226 Body_Spec : constant Node_Id :=
3227 Copy_Separate_Tree (Specification (N));
3228 New_Decl : constant Node_Id :=
3229 Make_Subprogram_Declaration (Loc,
3230 Copy_Separate_Tree (Specification (N)));
3232 SPARK_Mode_Aspect : Node_Id;
3233 Aspects : List_Id;
3234 Prag, Aspect : Node_Id;
3236 begin
3237 Insert_Before (N, New_Decl);
3238 Move_Aspects (From => N, To => New_Decl);
3240 -- Mark the newly moved aspects as not analyzed, so that
3241 -- their effect on New_Decl is properly analyzed.
3243 Aspect := First (Aspect_Specifications (New_Decl));
3244 while Present (Aspect) loop
3245 Set_Analyzed (Aspect, False);
3246 Next (Aspect);
3247 end loop;
3249 Analyze (New_Decl);
3251 -- The analysis of the generated subprogram declaration
3252 -- may have introduced pragmas that need to be analyzed.
3254 Prag := Next (New_Decl);
3255 while Prag /= N loop
3256 Analyze (Prag);
3257 Next (Prag);
3258 end loop;
3260 Spec_Id := Defining_Entity (New_Decl);
3262 -- As Body_Id originally comes from source, mark the new
3263 -- Spec_Id as such, which is required so that calls to
3264 -- this subprogram are registered in the local effects
3265 -- stored in ALI files for GNATprove.
3267 Set_Comes_From_Source (Spec_Id, True);
3269 -- If aspect SPARK_Mode was specified on the body, it
3270 -- needs to be repeated on the generated decl and the
3271 -- body. Since the original aspect was moved to the
3272 -- generated decl, copy it for the body.
3274 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3275 SPARK_Mode_Aspect :=
3276 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3277 Set_Analyzed (SPARK_Mode_Aspect, False);
3278 Aspects := New_List (SPARK_Mode_Aspect);
3279 Set_Aspect_Specifications (N, Aspects);
3280 end if;
3282 Set_Specification (N, Body_Spec);
3283 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3284 Set_Corresponding_Spec (N, Spec_Id);
3285 end;
3286 end if;
3287 end if;
3289 -- If this is a duplicate body, no point in analyzing it
3291 if Error_Posted (N) then
3292 return;
3293 end if;
3295 -- A subprogram body should cause freezing of its own declaration,
3296 -- but if there was no previous explicit declaration, then the
3297 -- subprogram will get frozen too late (there may be code within
3298 -- the body that depends on the subprogram having been frozen,
3299 -- such as uses of extra formals), so we force it to be frozen
3300 -- here. Same holds if the body and spec are compilation units.
3301 -- Finally, if the return type is an anonymous access to protected
3302 -- subprogram, it must be frozen before the body because its
3303 -- expansion has generated an equivalent type that is used when
3304 -- elaborating the body.
3306 -- An exception in the case of Ada 2012, AI05-177: The bodies
3307 -- created for expression functions do not freeze.
3309 if No (Spec_Id)
3310 and then Nkind (Original_Node (N)) /= N_Expression_Function
3311 then
3312 Freeze_Before (N, Body_Id);
3314 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3315 Freeze_Before (N, Spec_Id);
3317 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3318 Freeze_Before (N, Etype (Body_Id));
3319 end if;
3321 else
3322 Spec_Id := Corresponding_Spec (N);
3324 -- The corresponding spec may be subject to pragma Ghost with
3325 -- policy Ignore. Set the mode now to ensure that any nodes
3326 -- generated during analysis and expansion are properly flagged
3327 -- as ignored Ghost.
3329 Set_Ghost_Mode (N, Spec_Id);
3330 end if;
3331 end if;
3333 -- Previously we scanned the body to look for nested subprograms, and
3334 -- rejected an inline directive if nested subprograms were present,
3335 -- because the back-end would generate conflicting symbols for the
3336 -- nested bodies. This is now unnecessary.
3338 -- Look ahead to recognize a pragma Inline that appears after the body
3340 Check_Inline_Pragma (Spec_Id);
3342 -- Deal with special case of a fully private operation in the body of
3343 -- the protected type. We must create a declaration for the subprogram,
3344 -- in order to attach the protected subprogram that will be used in
3345 -- internal calls. We exclude compiler generated bodies from the
3346 -- expander since the issue does not arise for those cases.
3348 if No (Spec_Id)
3349 and then Comes_From_Source (N)
3350 and then Is_Protected_Type (Current_Scope)
3351 then
3352 Spec_Id := Build_Private_Protected_Declaration (N);
3353 end if;
3355 -- If a separate spec is present, then deal with freezing issues
3357 if Present (Spec_Id) then
3358 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3359 Verify_Overriding_Indicator;
3361 -- In general, the spec will be frozen when we start analyzing the
3362 -- body. However, for internally generated operations, such as
3363 -- wrapper functions for inherited operations with controlling
3364 -- results, the spec may not have been frozen by the time we expand
3365 -- the freeze actions that include the bodies. In particular, extra
3366 -- formals for accessibility or for return-in-place may need to be
3367 -- generated. Freeze nodes, if any, are inserted before the current
3368 -- body. These freeze actions are also needed in ASIS mode to enable
3369 -- the proper back-annotations.
3371 if not Is_Frozen (Spec_Id)
3372 and then (Expander_Active or ASIS_Mode)
3373 then
3374 -- Force the generation of its freezing node to ensure proper
3375 -- management of access types in the backend.
3377 -- This is definitely needed for some cases, but it is not clear
3378 -- why, to be investigated further???
3380 Set_Has_Delayed_Freeze (Spec_Id);
3381 Freeze_Before (N, Spec_Id);
3382 end if;
3383 end if;
3385 -- Place subprogram on scope stack, and make formals visible. If there
3386 -- is a spec, the visible entity remains that of the spec.
3388 if Present (Spec_Id) then
3389 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3391 if Is_Child_Unit (Spec_Id) then
3392 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3393 end if;
3395 if Style_Check then
3396 Style.Check_Identifier (Body_Id, Spec_Id);
3397 end if;
3399 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3400 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3402 if Is_Abstract_Subprogram (Spec_Id) then
3403 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3404 return;
3406 else
3407 Set_Convention (Body_Id, Convention (Spec_Id));
3408 Set_Has_Completion (Spec_Id);
3410 -- Inherit the "ghostness" of the subprogram spec. Note that this
3411 -- property is not directly inherited as the body may be subject
3412 -- to a different Ghost assertion policy.
3414 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
3415 Set_Is_Ghost_Entity (Body_Id);
3417 -- The Ghost policy in effect at the point of declaration and
3418 -- at the point of completion must match (SPARK RM 6.9(15)).
3420 Check_Ghost_Completion (Spec_Id, Body_Id);
3421 end if;
3423 if Is_Protected_Type (Scope (Spec_Id)) then
3424 Prot_Typ := Scope (Spec_Id);
3425 end if;
3427 -- If this is a body generated for a renaming, do not check for
3428 -- full conformance. The check is redundant, because the spec of
3429 -- the body is a copy of the spec in the renaming declaration,
3430 -- and the test can lead to spurious errors on nested defaults.
3432 if Present (Spec_Decl)
3433 and then not Comes_From_Source (N)
3434 and then
3435 (Nkind (Original_Node (Spec_Decl)) =
3436 N_Subprogram_Renaming_Declaration
3437 or else (Present (Corresponding_Body (Spec_Decl))
3438 and then
3439 Nkind (Unit_Declaration_Node
3440 (Corresponding_Body (Spec_Decl))) =
3441 N_Subprogram_Renaming_Declaration))
3442 then
3443 Conformant := True;
3445 -- Conversely, the spec may have been generated for specless body
3446 -- with an inline pragma.
3448 elsif Comes_From_Source (N)
3449 and then not Comes_From_Source (Spec_Id)
3450 and then Has_Pragma_Inline (Spec_Id)
3451 then
3452 Conformant := True;
3454 else
3455 Check_Conformance
3456 (Body_Id, Spec_Id,
3457 Fully_Conformant, True, Conformant, Body_Id);
3458 end if;
3460 -- If the body is not fully conformant, we have to decide if we
3461 -- should analyze it or not. If it has a really messed up profile
3462 -- then we probably should not analyze it, since we will get too
3463 -- many bogus messages.
3465 -- Our decision is to go ahead in the non-fully conformant case
3466 -- only if it is at least mode conformant with the spec. Note
3467 -- that the call to Check_Fully_Conformant has issued the proper
3468 -- error messages to complain about the lack of conformance.
3470 if not Conformant
3471 and then not Mode_Conformant (Body_Id, Spec_Id)
3472 then
3473 return;
3474 end if;
3475 end if;
3477 if Spec_Id /= Body_Id then
3478 Reference_Body_Formals (Spec_Id, Body_Id);
3479 end if;
3481 Set_Ekind (Body_Id, E_Subprogram_Body);
3483 if Nkind (N) = N_Subprogram_Body_Stub then
3484 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3486 -- Regular body
3488 else
3489 Set_Corresponding_Spec (N, Spec_Id);
3491 -- Ada 2005 (AI-345): If the operation is a primitive operation
3492 -- of a concurrent type, the type of the first parameter has been
3493 -- replaced with the corresponding record, which is the proper
3494 -- run-time structure to use. However, within the body there may
3495 -- be uses of the formals that depend on primitive operations
3496 -- of the type (in particular calls in prefixed form) for which
3497 -- we need the original concurrent type. The operation may have
3498 -- several controlling formals, so the replacement must be done
3499 -- for all of them.
3501 if Comes_From_Source (Spec_Id)
3502 and then Present (First_Entity (Spec_Id))
3503 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3504 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3505 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3506 and then Present (Corresponding_Concurrent_Type
3507 (Etype (First_Entity (Spec_Id))))
3508 then
3509 declare
3510 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3511 Form : Entity_Id;
3513 begin
3514 Form := First_Formal (Spec_Id);
3515 while Present (Form) loop
3516 if Etype (Form) = Typ then
3517 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3518 end if;
3520 Next_Formal (Form);
3521 end loop;
3522 end;
3523 end if;
3525 -- Make the formals visible, and place subprogram on scope stack.
3526 -- This is also the point at which we set Last_Real_Spec_Entity
3527 -- to mark the entities which will not be moved to the body.
3529 Install_Formals (Spec_Id);
3530 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3532 -- Within an instance, add local renaming declarations so that
3533 -- gdb can retrieve the values of actuals more easily. This is
3534 -- only relevant if generating code (and indeed we definitely
3535 -- do not want these definitions -gnatc mode, because that would
3536 -- confuse ASIS).
3538 if Is_Generic_Instance (Spec_Id)
3539 and then Is_Wrapper_Package (Current_Scope)
3540 and then Expander_Active
3541 then
3542 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3543 end if;
3545 Push_Scope (Spec_Id);
3547 -- Make sure that the subprogram is immediately visible. For
3548 -- child units that have no separate spec this is indispensable.
3549 -- Otherwise it is safe albeit redundant.
3551 Set_Is_Immediately_Visible (Spec_Id);
3552 end if;
3554 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3555 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3556 Set_Scope (Body_Id, Scope (Spec_Id));
3558 -- Case of subprogram body with no previous spec
3560 else
3561 -- Check for style warning required
3563 if Style_Check
3565 -- Only apply check for source level subprograms for which checks
3566 -- have not been suppressed.
3568 and then Comes_From_Source (Body_Id)
3569 and then not Suppress_Style_Checks (Body_Id)
3571 -- No warnings within an instance
3573 and then not In_Instance
3575 -- No warnings for expression functions
3577 and then Nkind (Original_Node (N)) /= N_Expression_Function
3578 then
3579 Style.Body_With_No_Spec (N);
3580 end if;
3582 New_Overloaded_Entity (Body_Id);
3584 if Nkind (N) /= N_Subprogram_Body_Stub then
3585 Set_Acts_As_Spec (N);
3586 Generate_Definition (Body_Id);
3587 Generate_Reference
3588 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3589 Install_Formals (Body_Id);
3591 Push_Scope (Body_Id);
3592 end if;
3594 -- For stubs and bodies with no previous spec, generate references to
3595 -- formals.
3597 Generate_Reference_To_Formals (Body_Id);
3598 end if;
3600 -- Set SPARK_Mode from context
3602 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3603 Set_SPARK_Pragma_Inherited (Body_Id, True);
3605 -- If the return type is an anonymous access type whose designated type
3606 -- is the limited view of a class-wide type and the non-limited view is
3607 -- available, update the return type accordingly.
3609 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3610 declare
3611 Etyp : Entity_Id;
3612 Rtyp : Entity_Id;
3614 begin
3615 Rtyp := Etype (Current_Scope);
3617 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3618 Etyp := Directly_Designated_Type (Rtyp);
3620 if Is_Class_Wide_Type (Etyp)
3621 and then From_Limited_With (Etyp)
3622 then
3623 Set_Directly_Designated_Type
3624 (Etype (Current_Scope), Available_View (Etyp));
3625 end if;
3626 end if;
3627 end;
3628 end if;
3630 -- If this is the proper body of a stub, we must verify that the stub
3631 -- conforms to the body, and to the previous spec if one was present.
3632 -- We know already that the body conforms to that spec. This test is
3633 -- only required for subprograms that come from source.
3635 if Nkind (Parent (N)) = N_Subunit
3636 and then Comes_From_Source (N)
3637 and then not Error_Posted (Body_Id)
3638 and then Nkind (Corresponding_Stub (Parent (N))) =
3639 N_Subprogram_Body_Stub
3640 then
3641 declare
3642 Old_Id : constant Entity_Id :=
3643 Defining_Entity
3644 (Specification (Corresponding_Stub (Parent (N))));
3646 Conformant : Boolean := False;
3648 begin
3649 if No (Spec_Id) then
3650 Check_Fully_Conformant (Body_Id, Old_Id);
3652 else
3653 Check_Conformance
3654 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3656 if not Conformant then
3658 -- The stub was taken to be a new declaration. Indicate that
3659 -- it lacks a body.
3661 Set_Has_Completion (Old_Id, False);
3662 end if;
3663 end if;
3664 end;
3665 end if;
3667 Set_Has_Completion (Body_Id);
3668 Check_Eliminated (Body_Id);
3670 if Nkind (N) = N_Subprogram_Body_Stub then
3672 -- Analyze any aspect specifications that appear on the subprogram
3673 -- body stub.
3675 if Has_Aspects (N) then
3676 Analyze_Aspects_On_Body_Or_Stub;
3677 end if;
3679 -- Stop the analysis now as the stub cannot be inlined, plus it does
3680 -- not have declarative or statement lists.
3682 return;
3683 end if;
3685 -- Handle frontend inlining
3687 -- Note: Normally we don't do any inlining if expansion is off, since
3688 -- we won't generate code in any case. An exception arises in GNATprove
3689 -- mode where we want to expand some calls in place, even with expansion
3690 -- disabled, since the inlining eases formal verification.
3692 if not GNATprove_Mode
3693 and then Expander_Active
3694 and then Serious_Errors_Detected = 0
3695 and then Present (Spec_Id)
3696 and then Has_Pragma_Inline (Spec_Id)
3697 then
3698 -- Legacy implementation (relying on frontend inlining)
3700 if not Back_End_Inlining then
3701 if (Has_Pragma_Inline_Always (Spec_Id)
3702 and then not Opt.Disable_FE_Inline_Always)
3703 or else
3704 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3705 and then not Opt.Disable_FE_Inline)
3706 then
3707 Build_Body_To_Inline (N, Spec_Id);
3708 end if;
3710 -- New implementation (relying on backend inlining)
3712 else
3713 if Has_Pragma_Inline_Always (Spec_Id)
3714 or else Optimization_Level > 0
3715 then
3716 -- Handle function returning an unconstrained type
3718 if Comes_From_Source (Body_Id)
3719 and then Ekind (Spec_Id) = E_Function
3720 and then Returns_Unconstrained_Type (Spec_Id)
3722 -- If function builds in place, i.e. returns a limited type,
3723 -- inlining cannot be done.
3725 and then not Is_Limited_Type (Etype (Spec_Id))
3726 then
3727 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3729 else
3730 declare
3731 Subp_Body : constant Node_Id :=
3732 Unit_Declaration_Node (Body_Id);
3733 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3735 begin
3736 -- Do not pass inlining to the backend if the subprogram
3737 -- has declarations or statements which cannot be inlined
3738 -- by the backend. This check is done here to emit an
3739 -- error instead of the generic warning message reported
3740 -- by the GCC backend (ie. "function might not be
3741 -- inlinable").
3743 if Present (Subp_Decl)
3744 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3745 then
3746 null;
3748 elsif Has_Excluded_Statement
3749 (Spec_Id,
3750 Statements
3751 (Handled_Statement_Sequence (Subp_Body)))
3752 then
3753 null;
3755 -- If the backend inlining is available then at this
3756 -- stage we only have to mark the subprogram as inlined.
3757 -- The expander will take care of registering it in the
3758 -- table of subprograms inlined by the backend a part of
3759 -- processing calls to it (cf. Expand_Call)
3761 else
3762 Set_Is_Inlined (Spec_Id);
3763 end if;
3764 end;
3765 end if;
3766 end if;
3767 end if;
3769 -- In GNATprove mode, inline only when there is a separate subprogram
3770 -- declaration for now, as inlining of subprogram bodies acting as
3771 -- declarations, or subprogram stubs, are not supported by frontend
3772 -- inlining. This inlining should occur after analysis of the body, so
3773 -- that it is known whether the value of SPARK_Mode applicable to the
3774 -- body, which can be defined by a pragma inside the body.
3776 elsif GNATprove_Mode
3777 and then Full_Analysis
3778 and then not Inside_A_Generic
3779 and then Present (Spec_Id)
3780 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3781 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3782 and then not Body_Has_Contract
3783 then
3784 Build_Body_To_Inline (N, Spec_Id);
3785 end if;
3787 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3788 -- of the specification we have to install the private withed units.
3789 -- This holds for child units as well.
3791 if Is_Compilation_Unit (Body_Id)
3792 or else Nkind (Parent (N)) = N_Compilation_Unit
3793 then
3794 Install_Private_With_Clauses (Body_Id);
3795 end if;
3797 Check_Anonymous_Return;
3799 -- Set the Protected_Formal field of each extra formal of the protected
3800 -- subprogram to reference the corresponding extra formal of the
3801 -- subprogram that implements it. For regular formals this occurs when
3802 -- the protected subprogram's declaration is expanded, but the extra
3803 -- formals don't get created until the subprogram is frozen. We need to
3804 -- do this before analyzing the protected subprogram's body so that any
3805 -- references to the original subprogram's extra formals will be changed
3806 -- refer to the implementing subprogram's formals (see Expand_Formal).
3808 if Present (Spec_Id)
3809 and then Is_Protected_Type (Scope (Spec_Id))
3810 and then Present (Protected_Body_Subprogram (Spec_Id))
3811 then
3812 declare
3813 Impl_Subp : constant Entity_Id :=
3814 Protected_Body_Subprogram (Spec_Id);
3815 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3816 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3817 begin
3818 while Present (Prot_Ext_Formal) loop
3819 pragma Assert (Present (Impl_Ext_Formal));
3820 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3821 Next_Formal_With_Extras (Prot_Ext_Formal);
3822 Next_Formal_With_Extras (Impl_Ext_Formal);
3823 end loop;
3824 end;
3825 end if;
3827 -- Now we can go on to analyze the body
3829 HSS := Handled_Statement_Sequence (N);
3830 Set_Actual_Subtypes (N, Current_Scope);
3832 -- Add a declaration for the Protection object, renaming declarations
3833 -- for discriminals and privals and finally a declaration for the entry
3834 -- family index (if applicable). This form of early expansion is done
3835 -- when the Expander is active because Install_Private_Data_Declarations
3836 -- references entities which were created during regular expansion. The
3837 -- subprogram entity must come from source, and not be an internally
3838 -- generated subprogram.
3840 if Expander_Active
3841 and then Present (Prot_Typ)
3842 and then Present (Spec_Id)
3843 and then Comes_From_Source (Spec_Id)
3844 and then not Is_Eliminated (Spec_Id)
3845 then
3846 Install_Private_Data_Declarations
3847 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3848 end if;
3850 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3851 -- may now appear in parameter and result profiles. Since the analysis
3852 -- of a subprogram body may use the parameter and result profile of the
3853 -- spec, swap any limited views with their non-limited counterpart.
3855 if Ada_Version >= Ada_2012 then
3856 Exchange_Limited_Views (Spec_Id);
3857 end if;
3859 -- Analyze any aspect specifications that appear on the subprogram body
3861 if Has_Aspects (N) then
3862 Analyze_Aspects_On_Body_Or_Stub;
3863 end if;
3865 Analyze_Declarations (Declarations (N));
3867 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3869 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3870 if Present (SPARK_Pragma (Spec_Id)) then
3871 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3872 and then
3873 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3874 then
3875 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3876 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3877 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3878 Error_Msg_NE
3879 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3880 end if;
3882 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3883 null;
3885 else
3886 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3887 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3888 Error_Msg_Sloc := Sloc (Spec_Id);
3889 Error_Msg_NE
3890 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3891 end if;
3892 end if;
3894 -- When a subprogram body appears inside a package, its contract is
3895 -- analyzed at the end of the package body declarations. This is due
3896 -- to the delay with respect of the package contract upon which the
3897 -- body contract may depend. When the subprogram body is stand alone
3898 -- and acts as a compilation unit, this delay is not necessary.
3900 if Nkind (Parent (N)) = N_Compilation_Unit then
3901 Analyze_Subprogram_Body_Contract (Body_Id);
3902 end if;
3904 -- Deal with preconditions, [refined] postconditions, Contract_Cases,
3905 -- invariants and predicates associated with body and its spec. Since
3906 -- there is no routine Expand_Declarations which would otherwise deal
3907 -- with the contract expansion, generate all necessary mechanisms to
3908 -- verify the contract assertions now.
3910 Expand_Subprogram_Contract (N);
3912 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3913 -- subprogram in GNATprove mode, as its body should not be analyzed.
3915 if SPARK_Mode /= On
3916 and then GNATprove_Mode
3917 and then Present (Spec_Id)
3918 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3919 then
3920 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3921 Set_Is_Inlined_Always (Spec_Id, False);
3922 end if;
3924 -- Check completion, and analyze the statements
3926 Check_Completion;
3927 Inspect_Deferred_Constant_Completion (Declarations (N));
3928 Analyze (HSS);
3930 -- Deal with end of scope processing for the body
3932 Process_End_Label (HSS, 't', Current_Scope);
3933 End_Scope;
3934 Check_Subprogram_Order (N);
3935 Set_Analyzed (Body_Id);
3937 -- If we have a separate spec, then the analysis of the declarations
3938 -- caused the entities in the body to be chained to the spec id, but
3939 -- we want them chained to the body id. Only the formal parameters
3940 -- end up chained to the spec id in this case.
3942 if Present (Spec_Id) then
3944 -- We must conform to the categorization of our spec
3946 Validate_Categorization_Dependency (N, Spec_Id);
3948 -- And if this is a child unit, the parent units must conform
3950 if Is_Child_Unit (Spec_Id) then
3951 Validate_Categorization_Dependency
3952 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3953 end if;
3955 -- Here is where we move entities from the spec to the body
3957 -- Case where there are entities that stay with the spec
3959 if Present (Last_Real_Spec_Entity) then
3961 -- No body entities (happens when the only real spec entities come
3962 -- from precondition and postcondition pragmas).
3964 if No (Last_Entity (Body_Id)) then
3965 Set_First_Entity
3966 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3968 -- Body entities present (formals), so chain stuff past them
3970 else
3971 Set_Next_Entity
3972 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3973 end if;
3975 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3976 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3977 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3979 -- Case where there are no spec entities, in this case there can be
3980 -- no body entities either, so just move everything.
3982 -- If the body is generated for an expression function, it may have
3983 -- been preanalyzed already, if 'access was applied to it.
3985 else
3986 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3987 N_Expression_Function
3988 then
3989 pragma Assert (No (Last_Entity (Body_Id)));
3990 null;
3991 end if;
3993 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3994 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3995 Set_First_Entity (Spec_Id, Empty);
3996 Set_Last_Entity (Spec_Id, Empty);
3997 end if;
3998 end if;
4000 Check_Missing_Return;
4002 -- Now we are going to check for variables that are never modified in
4003 -- the body of the procedure. But first we deal with a special case
4004 -- where we want to modify this check. If the body of the subprogram
4005 -- starts with a raise statement or its equivalent, or if the body
4006 -- consists entirely of a null statement, then it is pretty obvious that
4007 -- it is OK to not reference the parameters. For example, this might be
4008 -- the following common idiom for a stubbed function: statement of the
4009 -- procedure raises an exception. In particular this deals with the
4010 -- common idiom of a stubbed function, which appears something like:
4012 -- function F (A : Integer) return Some_Type;
4013 -- X : Some_Type;
4014 -- begin
4015 -- raise Program_Error;
4016 -- return X;
4017 -- end F;
4019 -- Here the purpose of X is simply to satisfy the annoying requirement
4020 -- in Ada that there be at least one return, and we certainly do not
4021 -- want to go posting warnings on X that it is not initialized. On
4022 -- the other hand, if X is entirely unreferenced that should still
4023 -- get a warning.
4025 -- What we do is to detect these cases, and if we find them, flag the
4026 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4027 -- suppress unwanted warnings. For the case of the function stub above
4028 -- we have a special test to set X as apparently assigned to suppress
4029 -- the warning.
4031 declare
4032 Stm : Node_Id;
4034 begin
4035 -- Skip initial labels (for one thing this occurs when we are in
4036 -- front end ZCX mode, but in any case it is irrelevant), and also
4037 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4039 Stm := First (Statements (HSS));
4040 while Nkind (Stm) = N_Label
4041 or else Nkind (Stm) in N_Push_xxx_Label
4042 loop
4043 Next (Stm);
4044 end loop;
4046 -- Do the test on the original statement before expansion
4048 declare
4049 Ostm : constant Node_Id := Original_Node (Stm);
4051 begin
4052 -- If explicit raise statement, turn on flag
4054 if Nkind (Ostm) = N_Raise_Statement then
4055 Set_Trivial_Subprogram (Stm);
4057 -- If null statement, and no following statements, turn on flag
4059 elsif Nkind (Stm) = N_Null_Statement
4060 and then Comes_From_Source (Stm)
4061 and then No (Next (Stm))
4062 then
4063 Set_Trivial_Subprogram (Stm);
4065 -- Check for explicit call cases which likely raise an exception
4067 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4068 if Is_Entity_Name (Name (Ostm)) then
4069 declare
4070 Ent : constant Entity_Id := Entity (Name (Ostm));
4072 begin
4073 -- If the procedure is marked No_Return, then likely it
4074 -- raises an exception, but in any case it is not coming
4075 -- back here, so turn on the flag.
4077 if Present (Ent)
4078 and then Ekind (Ent) = E_Procedure
4079 and then No_Return (Ent)
4080 then
4081 Set_Trivial_Subprogram (Stm);
4082 end if;
4083 end;
4084 end if;
4085 end if;
4086 end;
4087 end;
4089 -- Check for variables that are never modified
4091 declare
4092 E1, E2 : Entity_Id;
4094 begin
4095 -- If there is a separate spec, then transfer Never_Set_In_Source
4096 -- flags from out parameters to the corresponding entities in the
4097 -- body. The reason we do that is we want to post error flags on
4098 -- the body entities, not the spec entities.
4100 if Present (Spec_Id) then
4101 E1 := First_Entity (Spec_Id);
4102 while Present (E1) loop
4103 if Ekind (E1) = E_Out_Parameter then
4104 E2 := First_Entity (Body_Id);
4105 while Present (E2) loop
4106 exit when Chars (E1) = Chars (E2);
4107 Next_Entity (E2);
4108 end loop;
4110 if Present (E2) then
4111 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4112 end if;
4113 end if;
4115 Next_Entity (E1);
4116 end loop;
4117 end if;
4119 -- Check references in body
4121 Check_References (Body_Id);
4122 end;
4124 -- Check for nested subprogram, and mark outer level subprogram if so
4126 declare
4127 Ent : Entity_Id;
4129 begin
4130 if Present (Spec_Id) then
4131 Ent := Spec_Id;
4132 else
4133 Ent := Body_Id;
4134 end if;
4136 loop
4137 Ent := Enclosing_Subprogram (Ent);
4138 exit when No (Ent) or else Is_Subprogram (Ent);
4139 end loop;
4141 if Present (Ent) then
4142 Set_Has_Nested_Subprogram (Ent);
4143 end if;
4144 end;
4145 end Analyze_Subprogram_Body_Helper;
4147 ---------------------------------
4148 -- Analyze_Subprogram_Contract --
4149 ---------------------------------
4151 procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is
4152 procedure Save_Global_References_In_List (First_Prag : Node_Id);
4153 -- Save all global references in contract-related source pragma found in
4154 -- the list starting from pragma First_Prag.
4156 ------------------------------------
4157 -- Save_Global_References_In_List --
4158 ------------------------------------
4160 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
4161 Prag : Node_Id;
4163 begin
4164 Prag := First_Prag;
4165 while Present (Prag) loop
4166 if Comes_From_Source (Prag)
4167 and then Nam_In (Pragma_Name (Prag), Name_Contract_Cases,
4168 Name_Depends,
4169 Name_Extensions_Visible,
4170 Name_Global,
4171 Name_Postcondition,
4172 Name_Precondition,
4173 Name_Test_Case)
4174 then
4175 Save_Global_References (Original_Node (Prag));
4176 end if;
4178 Prag := Next_Pragma (Prag);
4179 end loop;
4180 end Save_Global_References_In_List;
4182 -- Local variables
4184 Items : constant Node_Id := Contract (Subp_Id);
4185 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
4186 Depends : Node_Id := Empty;
4187 Global : Node_Id := Empty;
4188 Mode : SPARK_Mode_Type;
4189 Prag : Node_Id;
4190 Prag_Nam : Name_Id;
4191 Restore_Scope : Boolean := False;
4193 -- Start of processing for Analyze_Subprogram_Contract
4195 begin
4196 -- All subprograms carry a contract, but for some it is not significant
4197 -- and should not be processed.
4199 if not Has_Significant_Contract (Subp_Id) then
4200 return;
4201 end if;
4203 -- Due to the timing of contract analysis, delayed pragmas may be
4204 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4205 -- context. To remedy this, restore the original SPARK_Mode of the
4206 -- related subprogram body.
4208 Save_SPARK_Mode_And_Set (Subp_Id, Mode);
4210 -- Ensure that the formal parameters are visible when analyzing all
4211 -- contract items.
4213 if not In_Open_Scopes (Subp_Id) then
4214 Restore_Scope := True;
4215 Push_Scope (Subp_Id);
4217 if Is_Generic_Subprogram (Subp_Id) then
4218 Install_Generic_Formals (Subp_Id);
4219 else
4220 Install_Formals (Subp_Id);
4221 end if;
4222 end if;
4224 if Present (Items) then
4226 -- Analyze pre- and postconditions
4228 Prag := Pre_Post_Conditions (Items);
4229 while Present (Prag) loop
4230 Analyze_Pre_Post_Condition_In_Decl_Part (Prag);
4231 Prag := Next_Pragma (Prag);
4232 end loop;
4234 -- Analyze contract-cases and test-cases
4236 Prag := Contract_Test_Cases (Items);
4237 while Present (Prag) loop
4238 Prag_Nam := Pragma_Name (Prag);
4240 if Prag_Nam = Name_Contract_Cases then
4241 Analyze_Contract_Cases_In_Decl_Part (Prag);
4242 else
4243 pragma Assert (Prag_Nam = Name_Test_Case);
4244 Analyze_Test_Case_In_Decl_Part (Prag);
4245 end if;
4247 Prag := Next_Pragma (Prag);
4248 end loop;
4250 -- Analyze classification pragmas
4252 Prag := Classifications (Items);
4253 while Present (Prag) loop
4254 Prag_Nam := Pragma_Name (Prag);
4256 if Prag_Nam = Name_Depends then
4257 Depends := Prag;
4259 elsif Prag_Nam = Name_Global then
4260 Global := Prag;
4262 -- Note that pragma Extensions_Visible has already been analyzed
4264 end if;
4266 Prag := Next_Pragma (Prag);
4267 end loop;
4269 -- Analyze Global first as Depends may mention items classified in
4270 -- the global categorization.
4272 if Present (Global) then
4273 Analyze_Global_In_Decl_Part (Global);
4274 end if;
4276 -- Depends must be analyzed after Global in order to see the modes of
4277 -- all global items.
4279 if Present (Depends) then
4280 Analyze_Depends_In_Decl_Part (Depends);
4281 end if;
4283 -- Ensure that the contract cases or postconditions mention 'Result
4284 -- or define a post-state.
4286 Check_Result_And_Post_State (Subp_Id);
4287 end if;
4289 -- The aspects and contract-related source pragmas associated with a
4290 -- generic subprogram are treated separately from the declaration as
4291 -- they need to be analyzed when the subprogram contract is analyzed.
4292 -- Once this is done, global references can be successfully saved.
4294 if Nkind (Subp_Decl) = N_Generic_Subprogram_Declaration then
4296 -- Save all global references found in the aspect specifications of
4297 -- the parameter profile of the generic subprogram.
4299 Save_Global_References_In_Aspects (Original_Node (Subp_Decl));
4301 -- Save all global references found in contract-related source
4302 -- pragmas. These pragmas usually appear after the declaration of
4303 -- the generic subprogram, either in the same declarative part or
4304 -- in the Pragmas_After list when the generic subprogram is a
4305 -- compilation unit.
4307 if Present (Items) then
4308 Save_Global_References_In_List (Pre_Post_Conditions (Items));
4309 Save_Global_References_In_List (Contract_Test_Cases (Items));
4310 Save_Global_References_In_List (Classifications (Items));
4311 end if;
4312 end if;
4314 if Restore_Scope then
4315 End_Scope;
4316 end if;
4318 -- Restore the SPARK_Mode of the enclosing context after all delayed
4319 -- pragmas have been analyzed.
4321 Restore_SPARK_Mode (Mode);
4322 end Analyze_Subprogram_Contract;
4324 ------------------------------------
4325 -- Analyze_Subprogram_Declaration --
4326 ------------------------------------
4328 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4329 Scop : constant Entity_Id := Current_Scope;
4330 Designator : Entity_Id;
4332 Is_Completion : Boolean;
4333 -- Indicates whether a null procedure declaration is a completion
4335 begin
4336 -- The subprogram declaration may be subject to pragma Ghost with policy
4337 -- Ignore. Set the mode now to ensure that any nodes generated during
4338 -- analysis and expansion are properly flagged as ignored Ghost.
4340 Set_Ghost_Mode (N);
4342 -- Null procedures are not allowed in SPARK
4344 if Nkind (Specification (N)) = N_Procedure_Specification
4345 and then Null_Present (Specification (N))
4346 then
4347 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4349 -- Null procedures are allowed in protected types, following the
4350 -- recent AI12-0147.
4352 if Is_Protected_Type (Current_Scope)
4353 and then Ada_Version < Ada_2012
4354 then
4355 Error_Msg_N ("protected operation cannot be a null procedure", N);
4356 end if;
4358 Analyze_Null_Procedure (N, Is_Completion);
4360 if Is_Completion then
4362 -- The null procedure acts as a body, nothing further is needed
4364 return;
4365 end if;
4366 end if;
4368 Designator := Analyze_Subprogram_Specification (Specification (N));
4370 -- A reference may already have been generated for the unit name, in
4371 -- which case the following call is redundant. However it is needed for
4372 -- declarations that are the rewriting of an expression function.
4374 Generate_Definition (Designator);
4376 -- Set SPARK mode from current context (may be overwritten later with
4377 -- explicit pragma).
4379 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4380 Set_SPARK_Pragma_Inherited (Designator);
4382 -- A subprogram declared within a Ghost region is automatically Ghost
4383 -- (SPARK RM 6.9(2)).
4385 if Comes_From_Source (Designator) and then Ghost_Mode > None then
4386 Set_Is_Ghost_Entity (Designator);
4387 end if;
4389 if Debug_Flag_C then
4390 Write_Str ("==> subprogram spec ");
4391 Write_Name (Chars (Designator));
4392 Write_Str (" from ");
4393 Write_Location (Sloc (N));
4394 Write_Eol;
4395 Indent;
4396 end if;
4398 Validate_RCI_Subprogram_Declaration (N);
4399 New_Overloaded_Entity (Designator);
4400 Check_Delayed_Subprogram (Designator);
4402 -- If the type of the first formal of the current subprogram is a non-
4403 -- generic tagged private type, mark the subprogram as being a private
4404 -- primitive. Ditto if this is a function with controlling result, and
4405 -- the return type is currently private. In both cases, the type of the
4406 -- controlling argument or result must be in the current scope for the
4407 -- operation to be primitive.
4409 if Has_Controlling_Result (Designator)
4410 and then Is_Private_Type (Etype (Designator))
4411 and then Scope (Etype (Designator)) = Current_Scope
4412 and then not Is_Generic_Actual_Type (Etype (Designator))
4413 then
4414 Set_Is_Private_Primitive (Designator);
4416 elsif Present (First_Formal (Designator)) then
4417 declare
4418 Formal_Typ : constant Entity_Id :=
4419 Etype (First_Formal (Designator));
4420 begin
4421 Set_Is_Private_Primitive (Designator,
4422 Is_Tagged_Type (Formal_Typ)
4423 and then Scope (Formal_Typ) = Current_Scope
4424 and then Is_Private_Type (Formal_Typ)
4425 and then not Is_Generic_Actual_Type (Formal_Typ));
4426 end;
4427 end if;
4429 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4430 -- or null.
4432 if Ada_Version >= Ada_2005
4433 and then Comes_From_Source (N)
4434 and then Is_Dispatching_Operation (Designator)
4435 then
4436 declare
4437 E : Entity_Id;
4438 Etyp : Entity_Id;
4440 begin
4441 if Has_Controlling_Result (Designator) then
4442 Etyp := Etype (Designator);
4444 else
4445 E := First_Entity (Designator);
4446 while Present (E)
4447 and then Is_Formal (E)
4448 and then not Is_Controlling_Formal (E)
4449 loop
4450 Next_Entity (E);
4451 end loop;
4453 Etyp := Etype (E);
4454 end if;
4456 if Is_Access_Type (Etyp) then
4457 Etyp := Directly_Designated_Type (Etyp);
4458 end if;
4460 if Is_Interface (Etyp)
4461 and then not Is_Abstract_Subprogram (Designator)
4462 and then not (Ekind (Designator) = E_Procedure
4463 and then Null_Present (Specification (N)))
4464 then
4465 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4467 -- Specialize error message based on procedures vs. functions,
4468 -- since functions can't be null subprograms.
4470 if Ekind (Designator) = E_Procedure then
4471 Error_Msg_N
4472 ("interface procedure % must be abstract or null", N);
4473 else
4474 Error_Msg_N
4475 ("interface function % must be abstract", N);
4476 end if;
4477 end if;
4478 end;
4479 end if;
4481 -- What is the following code for, it used to be
4483 -- ??? Set_Suppress_Elaboration_Checks
4484 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4486 -- The following seems equivalent, but a bit dubious
4488 if Elaboration_Checks_Suppressed (Designator) then
4489 Set_Kill_Elaboration_Checks (Designator);
4490 end if;
4492 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4493 Set_Categorization_From_Scope (Designator, Scop);
4495 else
4496 -- For a compilation unit, check for library-unit pragmas
4498 Push_Scope (Designator);
4499 Set_Categorization_From_Pragmas (N);
4500 Validate_Categorization_Dependency (N, Designator);
4501 Pop_Scope;
4502 end if;
4504 -- For a compilation unit, set body required. This flag will only be
4505 -- reset if a valid Import or Interface pragma is processed later on.
4507 if Nkind (Parent (N)) = N_Compilation_Unit then
4508 Set_Body_Required (Parent (N), True);
4510 if Ada_Version >= Ada_2005
4511 and then Nkind (Specification (N)) = N_Procedure_Specification
4512 and then Null_Present (Specification (N))
4513 then
4514 Error_Msg_N
4515 ("null procedure cannot be declared at library level", N);
4516 end if;
4517 end if;
4519 Generate_Reference_To_Formals (Designator);
4520 Check_Eliminated (Designator);
4522 if Debug_Flag_C then
4523 Outdent;
4524 Write_Str ("<== subprogram spec ");
4525 Write_Name (Chars (Designator));
4526 Write_Str (" from ");
4527 Write_Location (Sloc (N));
4528 Write_Eol;
4529 end if;
4531 if Is_Protected_Type (Current_Scope) then
4533 -- Indicate that this is a protected operation, because it may be
4534 -- used in subsequent declarations within the protected type.
4536 Set_Convention (Designator, Convention_Protected);
4537 end if;
4539 List_Inherited_Pre_Post_Aspects (Designator);
4541 if Has_Aspects (N) then
4542 Analyze_Aspect_Specifications (N, Designator);
4543 end if;
4544 end Analyze_Subprogram_Declaration;
4546 --------------------------------------
4547 -- Analyze_Subprogram_Specification --
4548 --------------------------------------
4550 -- Reminder: N here really is a subprogram specification (not a subprogram
4551 -- declaration). This procedure is called to analyze the specification in
4552 -- both subprogram bodies and subprogram declarations (specs).
4554 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4555 Designator : constant Entity_Id := Defining_Entity (N);
4556 Formals : constant List_Id := Parameter_Specifications (N);
4558 -- Start of processing for Analyze_Subprogram_Specification
4560 begin
4561 -- User-defined operator is not allowed in SPARK, except as a renaming
4563 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4564 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4565 then
4566 Check_SPARK_05_Restriction
4567 ("user-defined operator is not allowed", N);
4568 end if;
4570 -- Proceed with analysis. Do not emit a cross-reference entry if the
4571 -- specification comes from an expression function, because it may be
4572 -- the completion of a previous declaration. It is is not, the cross-
4573 -- reference entry will be emitted for the new subprogram declaration.
4575 if Nkind (Parent (N)) /= N_Expression_Function then
4576 Generate_Definition (Designator);
4577 end if;
4579 if Nkind (N) = N_Function_Specification then
4580 Set_Ekind (Designator, E_Function);
4581 Set_Mechanism (Designator, Default_Mechanism);
4582 else
4583 Set_Ekind (Designator, E_Procedure);
4584 Set_Etype (Designator, Standard_Void_Type);
4585 end if;
4587 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4588 -- those subprograms which could be inlined in GNATprove mode (because
4589 -- Body_To_Inline is non-Empty) but cannot be inlined.
4591 if GNATprove_Mode then
4592 Set_Is_Inlined_Always (Designator);
4593 end if;
4595 -- Introduce new scope for analysis of the formals and the return type
4597 Set_Scope (Designator, Current_Scope);
4599 if Present (Formals) then
4600 Push_Scope (Designator);
4601 Process_Formals (Formals, N);
4603 -- Check dimensions in N for formals with default expression
4605 Analyze_Dimension_Formals (N, Formals);
4607 -- Ada 2005 (AI-345): If this is an overriding operation of an
4608 -- inherited interface operation, and the controlling type is
4609 -- a synchronized type, replace the type with its corresponding
4610 -- record, to match the proper signature of an overriding operation.
4611 -- Same processing for an access parameter whose designated type is
4612 -- derived from a synchronized interface.
4614 if Ada_Version >= Ada_2005 then
4615 declare
4616 Formal : Entity_Id;
4617 Formal_Typ : Entity_Id;
4618 Rec_Typ : Entity_Id;
4619 Desig_Typ : Entity_Id;
4621 begin
4622 Formal := First_Formal (Designator);
4623 while Present (Formal) loop
4624 Formal_Typ := Etype (Formal);
4626 if Is_Concurrent_Type (Formal_Typ)
4627 and then Present (Corresponding_Record_Type (Formal_Typ))
4628 then
4629 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4631 if Present (Interfaces (Rec_Typ)) then
4632 Set_Etype (Formal, Rec_Typ);
4633 end if;
4635 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4636 Desig_Typ := Designated_Type (Formal_Typ);
4638 if Is_Concurrent_Type (Desig_Typ)
4639 and then Present (Corresponding_Record_Type (Desig_Typ))
4640 then
4641 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4643 if Present (Interfaces (Rec_Typ)) then
4644 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4645 end if;
4646 end if;
4647 end if;
4649 Next_Formal (Formal);
4650 end loop;
4651 end;
4652 end if;
4654 End_Scope;
4656 -- The subprogram scope is pushed and popped around the processing of
4657 -- the return type for consistency with call above to Process_Formals
4658 -- (which itself can call Analyze_Return_Type), and to ensure that any
4659 -- itype created for the return type will be associated with the proper
4660 -- scope.
4662 elsif Nkind (N) = N_Function_Specification then
4663 Push_Scope (Designator);
4664 Analyze_Return_Type (N);
4665 End_Scope;
4666 end if;
4668 -- Function case
4670 if Nkind (N) = N_Function_Specification then
4672 -- Deal with operator symbol case
4674 if Nkind (Designator) = N_Defining_Operator_Symbol then
4675 Valid_Operator_Definition (Designator);
4676 end if;
4678 May_Need_Actuals (Designator);
4680 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4681 -- the subprogram is abstract also. This does not apply to renaming
4682 -- declarations, where abstractness is inherited, and to subprogram
4683 -- bodies generated for stream operations, which become renamings as
4684 -- bodies.
4686 -- In case of primitives associated with abstract interface types
4687 -- the check is applied later (see Analyze_Subprogram_Declaration).
4689 if not Nkind_In (Original_Node (Parent (N)),
4690 N_Subprogram_Renaming_Declaration,
4691 N_Abstract_Subprogram_Declaration,
4692 N_Formal_Abstract_Subprogram_Declaration)
4693 then
4694 if Is_Abstract_Type (Etype (Designator))
4695 and then not Is_Interface (Etype (Designator))
4696 then
4697 Error_Msg_N
4698 ("function that returns abstract type must be abstract", N);
4700 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4701 -- access result whose designated type is abstract.
4703 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4704 and then
4705 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4706 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4707 and then Ada_Version >= Ada_2012
4708 then
4709 Error_Msg_N ("function whose access result designates "
4710 & "abstract type must be abstract", N);
4711 end if;
4712 end if;
4713 end if;
4715 return Designator;
4716 end Analyze_Subprogram_Specification;
4718 -----------------------
4719 -- Check_Conformance --
4720 -----------------------
4722 procedure Check_Conformance
4723 (New_Id : Entity_Id;
4724 Old_Id : Entity_Id;
4725 Ctype : Conformance_Type;
4726 Errmsg : Boolean;
4727 Conforms : out Boolean;
4728 Err_Loc : Node_Id := Empty;
4729 Get_Inst : Boolean := False;
4730 Skip_Controlling_Formals : Boolean := False)
4732 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4733 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4734 -- If Errmsg is True, then processing continues to post an error message
4735 -- for conformance error on given node. Two messages are output. The
4736 -- first message points to the previous declaration with a general "no
4737 -- conformance" message. The second is the detailed reason, supplied as
4738 -- Msg. The parameter N provide information for a possible & insertion
4739 -- in the message, and also provides the location for posting the
4740 -- message in the absence of a specified Err_Loc location.
4742 -----------------------
4743 -- Conformance_Error --
4744 -----------------------
4746 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4747 Enode : Node_Id;
4749 begin
4750 Conforms := False;
4752 if Errmsg then
4753 if No (Err_Loc) then
4754 Enode := N;
4755 else
4756 Enode := Err_Loc;
4757 end if;
4759 Error_Msg_Sloc := Sloc (Old_Id);
4761 case Ctype is
4762 when Type_Conformant =>
4763 Error_Msg_N -- CODEFIX
4764 ("not type conformant with declaration#!", Enode);
4766 when Mode_Conformant =>
4767 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4768 Error_Msg_N
4769 ("not mode conformant with operation inherited#!",
4770 Enode);
4771 else
4772 Error_Msg_N
4773 ("not mode conformant with declaration#!", Enode);
4774 end if;
4776 when Subtype_Conformant =>
4777 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4778 Error_Msg_N
4779 ("not subtype conformant with operation inherited#!",
4780 Enode);
4781 else
4782 Error_Msg_N
4783 ("not subtype conformant with declaration#!", Enode);
4784 end if;
4786 when Fully_Conformant =>
4787 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4788 Error_Msg_N -- CODEFIX
4789 ("not fully conformant with operation inherited#!",
4790 Enode);
4791 else
4792 Error_Msg_N -- CODEFIX
4793 ("not fully conformant with declaration#!", Enode);
4794 end if;
4795 end case;
4797 Error_Msg_NE (Msg, Enode, N);
4798 end if;
4799 end Conformance_Error;
4801 -- Local Variables
4803 Old_Type : constant Entity_Id := Etype (Old_Id);
4804 New_Type : constant Entity_Id := Etype (New_Id);
4805 Old_Formal : Entity_Id;
4806 New_Formal : Entity_Id;
4807 Access_Types_Match : Boolean;
4808 Old_Formal_Base : Entity_Id;
4809 New_Formal_Base : Entity_Id;
4811 -- Start of processing for Check_Conformance
4813 begin
4814 Conforms := True;
4816 -- We need a special case for operators, since they don't appear
4817 -- explicitly.
4819 if Ctype = Type_Conformant then
4820 if Ekind (New_Id) = E_Operator
4821 and then Operator_Matches_Spec (New_Id, Old_Id)
4822 then
4823 return;
4824 end if;
4825 end if;
4827 -- If both are functions/operators, check return types conform
4829 if Old_Type /= Standard_Void_Type
4830 and then
4831 New_Type /= Standard_Void_Type
4832 then
4833 -- If we are checking interface conformance we omit controlling
4834 -- arguments and result, because we are only checking the conformance
4835 -- of the remaining parameters.
4837 if Has_Controlling_Result (Old_Id)
4838 and then Has_Controlling_Result (New_Id)
4839 and then Skip_Controlling_Formals
4840 then
4841 null;
4843 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4844 if Ctype >= Subtype_Conformant
4845 and then not Predicates_Match (Old_Type, New_Type)
4846 then
4847 Conformance_Error
4848 ("\predicate of return type does not match!", New_Id);
4849 else
4850 Conformance_Error
4851 ("\return type does not match!", New_Id);
4852 end if;
4854 return;
4855 end if;
4857 -- Ada 2005 (AI-231): In case of anonymous access types check the
4858 -- null-exclusion and access-to-constant attributes match.
4860 if Ada_Version >= Ada_2005
4861 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4862 and then
4863 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4864 or else Is_Access_Constant (Etype (Old_Type)) /=
4865 Is_Access_Constant (Etype (New_Type)))
4866 then
4867 Conformance_Error ("\return type does not match!", New_Id);
4868 return;
4869 end if;
4871 -- If either is a function/operator and the other isn't, error
4873 elsif Old_Type /= Standard_Void_Type
4874 or else New_Type /= Standard_Void_Type
4875 then
4876 Conformance_Error ("\functions can only match functions!", New_Id);
4877 return;
4878 end if;
4880 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4881 -- If this is a renaming as body, refine error message to indicate that
4882 -- the conflict is with the original declaration. If the entity is not
4883 -- frozen, the conventions don't have to match, the one of the renamed
4884 -- entity is inherited.
4886 if Ctype >= Subtype_Conformant then
4887 if Convention (Old_Id) /= Convention (New_Id) then
4888 if not Is_Frozen (New_Id) then
4889 null;
4891 elsif Present (Err_Loc)
4892 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4893 and then Present (Corresponding_Spec (Err_Loc))
4894 then
4895 Error_Msg_Name_1 := Chars (New_Id);
4896 Error_Msg_Name_2 :=
4897 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4898 Conformance_Error ("\prior declaration for% has convention %!");
4900 else
4901 Conformance_Error ("\calling conventions do not match!");
4902 end if;
4904 return;
4906 elsif Is_Formal_Subprogram (Old_Id)
4907 or else Is_Formal_Subprogram (New_Id)
4908 then
4909 Conformance_Error ("\formal subprograms not allowed!");
4910 return;
4912 -- Pragma Ghost behaves as a convention in the context of subtype
4913 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4914 -- subprograms as their spec may reside in a Ghost region and their
4915 -- body not, or vice versa.
4917 elsif Comes_From_Source (Old_Id)
4918 and then Comes_From_Source (New_Id)
4919 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4920 then
4921 Conformance_Error ("\ghost modes do not match!");
4922 return;
4923 end if;
4924 end if;
4926 -- Deal with parameters
4928 -- Note: we use the entity information, rather than going directly
4929 -- to the specification in the tree. This is not only simpler, but
4930 -- absolutely necessary for some cases of conformance tests between
4931 -- operators, where the declaration tree simply does not exist.
4933 Old_Formal := First_Formal (Old_Id);
4934 New_Formal := First_Formal (New_Id);
4935 while Present (Old_Formal) and then Present (New_Formal) loop
4936 if Is_Controlling_Formal (Old_Formal)
4937 and then Is_Controlling_Formal (New_Formal)
4938 and then Skip_Controlling_Formals
4939 then
4940 -- The controlling formals will have different types when
4941 -- comparing an interface operation with its match, but both
4942 -- or neither must be access parameters.
4944 if Is_Access_Type (Etype (Old_Formal))
4946 Is_Access_Type (Etype (New_Formal))
4947 then
4948 goto Skip_Controlling_Formal;
4949 else
4950 Conformance_Error
4951 ("\access parameter does not match!", New_Formal);
4952 end if;
4953 end if;
4955 -- Ada 2012: Mode conformance also requires that formal parameters
4956 -- be both aliased, or neither.
4958 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4959 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4960 Conformance_Error
4961 ("\aliased parameter mismatch!", New_Formal);
4962 end if;
4963 end if;
4965 if Ctype = Fully_Conformant then
4967 -- Names must match. Error message is more accurate if we do
4968 -- this before checking that the types of the formals match.
4970 if Chars (Old_Formal) /= Chars (New_Formal) then
4971 Conformance_Error ("\name& does not match!", New_Formal);
4973 -- Set error posted flag on new formal as well to stop
4974 -- junk cascaded messages in some cases.
4976 Set_Error_Posted (New_Formal);
4977 return;
4978 end if;
4980 -- Null exclusion must match
4982 if Null_Exclusion_Present (Parent (Old_Formal))
4984 Null_Exclusion_Present (Parent (New_Formal))
4985 then
4986 -- Only give error if both come from source. This should be
4987 -- investigated some time, since it should not be needed ???
4989 if Comes_From_Source (Old_Formal)
4990 and then
4991 Comes_From_Source (New_Formal)
4992 then
4993 Conformance_Error
4994 ("\null exclusion for& does not match", New_Formal);
4996 -- Mark error posted on the new formal to avoid duplicated
4997 -- complaint about types not matching.
4999 Set_Error_Posted (New_Formal);
5000 end if;
5001 end if;
5002 end if;
5004 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5005 -- case occurs whenever a subprogram is being renamed and one of its
5006 -- parameters imposes a null exclusion. For example:
5008 -- type T is null record;
5009 -- type Acc_T is access T;
5010 -- subtype Acc_T_Sub is Acc_T;
5012 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5013 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5014 -- renames P;
5016 Old_Formal_Base := Etype (Old_Formal);
5017 New_Formal_Base := Etype (New_Formal);
5019 if Get_Inst then
5020 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5021 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5022 end if;
5024 Access_Types_Match := Ada_Version >= Ada_2005
5026 -- Ensure that this rule is only applied when New_Id is a
5027 -- renaming of Old_Id.
5029 and then Nkind (Parent (Parent (New_Id))) =
5030 N_Subprogram_Renaming_Declaration
5031 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5032 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5033 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5035 -- Now handle the allowed access-type case
5037 and then Is_Access_Type (Old_Formal_Base)
5038 and then Is_Access_Type (New_Formal_Base)
5040 -- The type kinds must match. The only exception occurs with
5041 -- multiple generics of the form:
5043 -- generic generic
5044 -- type F is private; type A is private;
5045 -- type F_Ptr is access F; type A_Ptr is access A;
5046 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5047 -- package F_Pack is ... package A_Pack is
5048 -- package F_Inst is
5049 -- new F_Pack (A, A_Ptr, A_P);
5051 -- When checking for conformance between the parameters of A_P
5052 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5053 -- because the compiler has transformed A_Ptr into a subtype of
5054 -- F_Ptr. We catch this case in the code below.
5056 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5057 or else
5058 (Is_Generic_Type (Old_Formal_Base)
5059 and then Is_Generic_Type (New_Formal_Base)
5060 and then Is_Internal (New_Formal_Base)
5061 and then Etype (Etype (New_Formal_Base)) =
5062 Old_Formal_Base))
5063 and then Directly_Designated_Type (Old_Formal_Base) =
5064 Directly_Designated_Type (New_Formal_Base)
5065 and then ((Is_Itype (Old_Formal_Base)
5066 and then Can_Never_Be_Null (Old_Formal_Base))
5067 or else
5068 (Is_Itype (New_Formal_Base)
5069 and then Can_Never_Be_Null (New_Formal_Base)));
5071 -- Types must always match. In the visible part of an instance,
5072 -- usual overloading rules for dispatching operations apply, and
5073 -- we check base types (not the actual subtypes).
5075 if In_Instance_Visible_Part
5076 and then Is_Dispatching_Operation (New_Id)
5077 then
5078 if not Conforming_Types
5079 (T1 => Base_Type (Etype (Old_Formal)),
5080 T2 => Base_Type (Etype (New_Formal)),
5081 Ctype => Ctype,
5082 Get_Inst => Get_Inst)
5083 and then not Access_Types_Match
5084 then
5085 Conformance_Error ("\type of & does not match!", New_Formal);
5086 return;
5087 end if;
5089 elsif not Conforming_Types
5090 (T1 => Old_Formal_Base,
5091 T2 => New_Formal_Base,
5092 Ctype => Ctype,
5093 Get_Inst => Get_Inst)
5094 and then not Access_Types_Match
5095 then
5096 -- Don't give error message if old type is Any_Type. This test
5097 -- avoids some cascaded errors, e.g. in case of a bad spec.
5099 if Errmsg and then Old_Formal_Base = Any_Type then
5100 Conforms := False;
5101 else
5102 if Ctype >= Subtype_Conformant
5103 and then
5104 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5105 then
5106 Conformance_Error
5107 ("\predicate of & does not match!", New_Formal);
5108 else
5109 Conformance_Error
5110 ("\type of & does not match!", New_Formal);
5111 end if;
5112 end if;
5114 return;
5115 end if;
5117 -- For mode conformance, mode must match
5119 if Ctype >= Mode_Conformant then
5120 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5121 if not Ekind_In (New_Id, E_Function, E_Procedure)
5122 or else not Is_Primitive_Wrapper (New_Id)
5123 then
5124 Conformance_Error ("\mode of & does not match!", New_Formal);
5126 else
5127 declare
5128 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5129 begin
5130 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5131 then
5132 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5133 else
5134 Conformance_Error
5135 ("\mode of & does not match!", New_Formal);
5136 end if;
5137 end;
5138 end if;
5140 return;
5142 -- Part of mode conformance for access types is having the same
5143 -- constant modifier.
5145 elsif Access_Types_Match
5146 and then Is_Access_Constant (Old_Formal_Base) /=
5147 Is_Access_Constant (New_Formal_Base)
5148 then
5149 Conformance_Error
5150 ("\constant modifier does not match!", New_Formal);
5151 return;
5152 end if;
5153 end if;
5155 if Ctype >= Subtype_Conformant then
5157 -- Ada 2005 (AI-231): In case of anonymous access types check
5158 -- the null-exclusion and access-to-constant attributes must
5159 -- match. For null exclusion, we test the types rather than the
5160 -- formals themselves, since the attribute is only set reliably
5161 -- on the formals in the Ada 95 case, and we exclude the case
5162 -- where Old_Formal is marked as controlling, to avoid errors
5163 -- when matching completing bodies with dispatching declarations
5164 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5166 if Ada_Version >= Ada_2005
5167 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5168 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5169 and then
5170 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5171 Can_Never_Be_Null (Etype (New_Formal))
5172 and then
5173 not Is_Controlling_Formal (Old_Formal))
5174 or else
5175 Is_Access_Constant (Etype (Old_Formal)) /=
5176 Is_Access_Constant (Etype (New_Formal)))
5178 -- Do not complain if error already posted on New_Formal. This
5179 -- avoids some redundant error messages.
5181 and then not Error_Posted (New_Formal)
5182 then
5183 -- It is allowed to omit the null-exclusion in case of stream
5184 -- attribute subprograms. We recognize stream subprograms
5185 -- through their TSS-generated suffix.
5187 declare
5188 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5190 begin
5191 if TSS_Name /= TSS_Stream_Read
5192 and then TSS_Name /= TSS_Stream_Write
5193 and then TSS_Name /= TSS_Stream_Input
5194 and then TSS_Name /= TSS_Stream_Output
5195 then
5196 -- Here we have a definite conformance error. It is worth
5197 -- special casing the error message for the case of a
5198 -- controlling formal (which excludes null).
5200 if Is_Controlling_Formal (New_Formal) then
5201 Error_Msg_Node_2 := Scope (New_Formal);
5202 Conformance_Error
5203 ("\controlling formal & of & excludes null, "
5204 & "declaration must exclude null as well",
5205 New_Formal);
5207 -- Normal case (couldn't we give more detail here???)
5209 else
5210 Conformance_Error
5211 ("\type of & does not match!", New_Formal);
5212 end if;
5214 return;
5215 end if;
5216 end;
5217 end if;
5218 end if;
5220 -- Full conformance checks
5222 if Ctype = Fully_Conformant then
5224 -- We have checked already that names match
5226 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5228 -- Check default expressions for in parameters
5230 declare
5231 NewD : constant Boolean :=
5232 Present (Default_Value (New_Formal));
5233 OldD : constant Boolean :=
5234 Present (Default_Value (Old_Formal));
5235 begin
5236 if NewD or OldD then
5238 -- The old default value has been analyzed because the
5239 -- current full declaration will have frozen everything
5240 -- before. The new default value has not been analyzed,
5241 -- so analyze it now before we check for conformance.
5243 if NewD then
5244 Push_Scope (New_Id);
5245 Preanalyze_Spec_Expression
5246 (Default_Value (New_Formal), Etype (New_Formal));
5247 End_Scope;
5248 end if;
5250 if not (NewD and OldD)
5251 or else not Fully_Conformant_Expressions
5252 (Default_Value (Old_Formal),
5253 Default_Value (New_Formal))
5254 then
5255 Conformance_Error
5256 ("\default expression for & does not match!",
5257 New_Formal);
5258 return;
5259 end if;
5260 end if;
5261 end;
5262 end if;
5263 end if;
5265 -- A couple of special checks for Ada 83 mode. These checks are
5266 -- skipped if either entity is an operator in package Standard,
5267 -- or if either old or new instance is not from the source program.
5269 if Ada_Version = Ada_83
5270 and then Sloc (Old_Id) > Standard_Location
5271 and then Sloc (New_Id) > Standard_Location
5272 and then Comes_From_Source (Old_Id)
5273 and then Comes_From_Source (New_Id)
5274 then
5275 declare
5276 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5277 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5279 begin
5280 -- Explicit IN must be present or absent in both cases. This
5281 -- test is required only in the full conformance case.
5283 if In_Present (Old_Param) /= In_Present (New_Param)
5284 and then Ctype = Fully_Conformant
5285 then
5286 Conformance_Error
5287 ("\(Ada 83) IN must appear in both declarations",
5288 New_Formal);
5289 return;
5290 end if;
5292 -- Grouping (use of comma in param lists) must be the same
5293 -- This is where we catch a misconformance like:
5295 -- A, B : Integer
5296 -- A : Integer; B : Integer
5298 -- which are represented identically in the tree except
5299 -- for the setting of the flags More_Ids and Prev_Ids.
5301 if More_Ids (Old_Param) /= More_Ids (New_Param)
5302 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5303 then
5304 Conformance_Error
5305 ("\grouping of & does not match!", New_Formal);
5306 return;
5307 end if;
5308 end;
5309 end if;
5311 -- This label is required when skipping controlling formals
5313 <<Skip_Controlling_Formal>>
5315 Next_Formal (Old_Formal);
5316 Next_Formal (New_Formal);
5317 end loop;
5319 if Present (Old_Formal) then
5320 Conformance_Error ("\too few parameters!");
5321 return;
5323 elsif Present (New_Formal) then
5324 Conformance_Error ("\too many parameters!", New_Formal);
5325 return;
5326 end if;
5327 end Check_Conformance;
5329 -----------------------
5330 -- Check_Conventions --
5331 -----------------------
5333 procedure Check_Conventions (Typ : Entity_Id) is
5334 Ifaces_List : Elist_Id;
5336 procedure Check_Convention (Op : Entity_Id);
5337 -- Verify that the convention of inherited dispatching operation Op is
5338 -- consistent among all subprograms it overrides. In order to minimize
5339 -- the search, Search_From is utilized to designate a specific point in
5340 -- the list rather than iterating over the whole list once more.
5342 ----------------------
5343 -- Check_Convention --
5344 ----------------------
5346 procedure Check_Convention (Op : Entity_Id) is
5347 Op_Conv : constant Convention_Id := Convention (Op);
5348 Iface_Conv : Convention_Id;
5349 Iface_Elmt : Elmt_Id;
5350 Iface_Prim_Elmt : Elmt_Id;
5351 Iface_Prim : Entity_Id;
5353 begin
5354 Iface_Elmt := First_Elmt (Ifaces_List);
5355 while Present (Iface_Elmt) loop
5356 Iface_Prim_Elmt :=
5357 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5358 while Present (Iface_Prim_Elmt) loop
5359 Iface_Prim := Node (Iface_Prim_Elmt);
5360 Iface_Conv := Convention (Iface_Prim);
5362 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5363 and then Iface_Conv /= Op_Conv
5364 then
5365 Error_Msg_N
5366 ("inconsistent conventions in primitive operations", Typ);
5368 Error_Msg_Name_1 := Chars (Op);
5369 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5370 Error_Msg_Sloc := Sloc (Op);
5372 if Comes_From_Source (Op) or else No (Alias (Op)) then
5373 if not Present (Overridden_Operation (Op)) then
5374 Error_Msg_N ("\\primitive % defined #", Typ);
5375 else
5376 Error_Msg_N
5377 ("\\overriding operation % with "
5378 & "convention % defined #", Typ);
5379 end if;
5381 else pragma Assert (Present (Alias (Op)));
5382 Error_Msg_Sloc := Sloc (Alias (Op));
5383 Error_Msg_N ("\\inherited operation % with "
5384 & "convention % defined #", Typ);
5385 end if;
5387 Error_Msg_Name_1 := Chars (Op);
5388 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5389 Error_Msg_Sloc := Sloc (Iface_Prim);
5390 Error_Msg_N ("\\overridden operation % with "
5391 & "convention % defined #", Typ);
5393 -- Avoid cascading errors
5395 return;
5396 end if;
5398 Next_Elmt (Iface_Prim_Elmt);
5399 end loop;
5401 Next_Elmt (Iface_Elmt);
5402 end loop;
5403 end Check_Convention;
5405 -- Local variables
5407 Prim_Op : Entity_Id;
5408 Prim_Op_Elmt : Elmt_Id;
5410 -- Start of processing for Check_Conventions
5412 begin
5413 if not Has_Interfaces (Typ) then
5414 return;
5415 end if;
5417 Collect_Interfaces (Typ, Ifaces_List);
5419 -- The algorithm checks every overriding dispatching operation against
5420 -- all the corresponding overridden dispatching operations, detecting
5421 -- differences in conventions.
5423 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5424 while Present (Prim_Op_Elmt) loop
5425 Prim_Op := Node (Prim_Op_Elmt);
5427 -- A small optimization: skip the predefined dispatching operations
5428 -- since they always have the same convention.
5430 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5431 Check_Convention (Prim_Op);
5432 end if;
5434 Next_Elmt (Prim_Op_Elmt);
5435 end loop;
5436 end Check_Conventions;
5438 ------------------------------
5439 -- Check_Delayed_Subprogram --
5440 ------------------------------
5442 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5443 F : Entity_Id;
5445 procedure Possible_Freeze (T : Entity_Id);
5446 -- T is the type of either a formal parameter or of the return type.
5447 -- If T is not yet frozen and needs a delayed freeze, then the
5448 -- subprogram itself must be delayed. If T is the limited view of an
5449 -- incomplete type the subprogram must be frozen as well, because
5450 -- T may depend on local types that have not been frozen yet.
5452 ---------------------
5453 -- Possible_Freeze --
5454 ---------------------
5456 procedure Possible_Freeze (T : Entity_Id) is
5457 begin
5458 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5459 Set_Has_Delayed_Freeze (Designator);
5461 elsif Is_Access_Type (T)
5462 and then Has_Delayed_Freeze (Designated_Type (T))
5463 and then not Is_Frozen (Designated_Type (T))
5464 then
5465 Set_Has_Delayed_Freeze (Designator);
5467 elsif Ekind (T) = E_Incomplete_Type
5468 and then From_Limited_With (T)
5469 then
5470 Set_Has_Delayed_Freeze (Designator);
5472 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5473 -- of a subprogram or entry declaration.
5475 elsif Ekind (T) = E_Incomplete_Type
5476 and then Ada_Version >= Ada_2012
5477 then
5478 Set_Has_Delayed_Freeze (Designator);
5479 end if;
5481 end Possible_Freeze;
5483 -- Start of processing for Check_Delayed_Subprogram
5485 begin
5486 -- All subprograms, including abstract subprograms, may need a freeze
5487 -- node if some formal type or the return type needs one.
5489 Possible_Freeze (Etype (Designator));
5490 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5492 -- Need delayed freeze if any of the formal types themselves need
5493 -- a delayed freeze and are not yet frozen.
5495 F := First_Formal (Designator);
5496 while Present (F) loop
5497 Possible_Freeze (Etype (F));
5498 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5499 Next_Formal (F);
5500 end loop;
5502 -- Mark functions that return by reference. Note that it cannot be
5503 -- done for delayed_freeze subprograms because the underlying
5504 -- returned type may not be known yet (for private types)
5506 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5507 declare
5508 Typ : constant Entity_Id := Etype (Designator);
5509 Utyp : constant Entity_Id := Underlying_Type (Typ);
5510 begin
5511 if Is_Limited_View (Typ) then
5512 Set_Returns_By_Ref (Designator);
5513 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5514 Set_Returns_By_Ref (Designator);
5515 end if;
5516 end;
5517 end if;
5518 end Check_Delayed_Subprogram;
5520 ------------------------------------
5521 -- Check_Discriminant_Conformance --
5522 ------------------------------------
5524 procedure Check_Discriminant_Conformance
5525 (N : Node_Id;
5526 Prev : Entity_Id;
5527 Prev_Loc : Node_Id)
5529 Old_Discr : Entity_Id := First_Discriminant (Prev);
5530 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5531 New_Discr_Id : Entity_Id;
5532 New_Discr_Type : Entity_Id;
5534 procedure Conformance_Error (Msg : String; N : Node_Id);
5535 -- Post error message for conformance error on given node. Two messages
5536 -- are output. The first points to the previous declaration with a
5537 -- general "no conformance" message. The second is the detailed reason,
5538 -- supplied as Msg. The parameter N provide information for a possible
5539 -- & insertion in the message.
5541 -----------------------
5542 -- Conformance_Error --
5543 -----------------------
5545 procedure Conformance_Error (Msg : String; N : Node_Id) is
5546 begin
5547 Error_Msg_Sloc := Sloc (Prev_Loc);
5548 Error_Msg_N -- CODEFIX
5549 ("not fully conformant with declaration#!", N);
5550 Error_Msg_NE (Msg, N, N);
5551 end Conformance_Error;
5553 -- Start of processing for Check_Discriminant_Conformance
5555 begin
5556 while Present (Old_Discr) and then Present (New_Discr) loop
5557 New_Discr_Id := Defining_Identifier (New_Discr);
5559 -- The subtype mark of the discriminant on the full type has not
5560 -- been analyzed so we do it here. For an access discriminant a new
5561 -- type is created.
5563 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5564 New_Discr_Type :=
5565 Access_Definition (N, Discriminant_Type (New_Discr));
5567 else
5568 Analyze (Discriminant_Type (New_Discr));
5569 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5571 -- Ada 2005: if the discriminant definition carries a null
5572 -- exclusion, create an itype to check properly for consistency
5573 -- with partial declaration.
5575 if Is_Access_Type (New_Discr_Type)
5576 and then Null_Exclusion_Present (New_Discr)
5577 then
5578 New_Discr_Type :=
5579 Create_Null_Excluding_Itype
5580 (T => New_Discr_Type,
5581 Related_Nod => New_Discr,
5582 Scope_Id => Current_Scope);
5583 end if;
5584 end if;
5586 if not Conforming_Types
5587 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5588 then
5589 Conformance_Error ("type of & does not match!", New_Discr_Id);
5590 return;
5591 else
5592 -- Treat the new discriminant as an occurrence of the old one,
5593 -- for navigation purposes, and fill in some semantic
5594 -- information, for completeness.
5596 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5597 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5598 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5599 end if;
5601 -- Names must match
5603 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5604 Conformance_Error ("name & does not match!", New_Discr_Id);
5605 return;
5606 end if;
5608 -- Default expressions must match
5610 declare
5611 NewD : constant Boolean :=
5612 Present (Expression (New_Discr));
5613 OldD : constant Boolean :=
5614 Present (Expression (Parent (Old_Discr)));
5616 begin
5617 if NewD or OldD then
5619 -- The old default value has been analyzed and expanded,
5620 -- because the current full declaration will have frozen
5621 -- everything before. The new default values have not been
5622 -- expanded, so expand now to check conformance.
5624 if NewD then
5625 Preanalyze_Spec_Expression
5626 (Expression (New_Discr), New_Discr_Type);
5627 end if;
5629 if not (NewD and OldD)
5630 or else not Fully_Conformant_Expressions
5631 (Expression (Parent (Old_Discr)),
5632 Expression (New_Discr))
5634 then
5635 Conformance_Error
5636 ("default expression for & does not match!",
5637 New_Discr_Id);
5638 return;
5639 end if;
5640 end if;
5641 end;
5643 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5645 if Ada_Version = Ada_83 then
5646 declare
5647 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5649 begin
5650 -- Grouping (use of comma in param lists) must be the same
5651 -- This is where we catch a misconformance like:
5653 -- A, B : Integer
5654 -- A : Integer; B : Integer
5656 -- which are represented identically in the tree except
5657 -- for the setting of the flags More_Ids and Prev_Ids.
5659 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5660 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5661 then
5662 Conformance_Error
5663 ("grouping of & does not match!", New_Discr_Id);
5664 return;
5665 end if;
5666 end;
5667 end if;
5669 Next_Discriminant (Old_Discr);
5670 Next (New_Discr);
5671 end loop;
5673 if Present (Old_Discr) then
5674 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5675 return;
5677 elsif Present (New_Discr) then
5678 Conformance_Error
5679 ("too many discriminants!", Defining_Identifier (New_Discr));
5680 return;
5681 end if;
5682 end Check_Discriminant_Conformance;
5684 ----------------------------
5685 -- Check_Fully_Conformant --
5686 ----------------------------
5688 procedure Check_Fully_Conformant
5689 (New_Id : Entity_Id;
5690 Old_Id : Entity_Id;
5691 Err_Loc : Node_Id := Empty)
5693 Result : Boolean;
5694 pragma Warnings (Off, Result);
5695 begin
5696 Check_Conformance
5697 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5698 end Check_Fully_Conformant;
5700 ---------------------------
5701 -- Check_Mode_Conformant --
5702 ---------------------------
5704 procedure Check_Mode_Conformant
5705 (New_Id : Entity_Id;
5706 Old_Id : Entity_Id;
5707 Err_Loc : Node_Id := Empty;
5708 Get_Inst : Boolean := False)
5710 Result : Boolean;
5711 pragma Warnings (Off, Result);
5712 begin
5713 Check_Conformance
5714 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5715 end Check_Mode_Conformant;
5717 --------------------------------
5718 -- Check_Overriding_Indicator --
5719 --------------------------------
5721 procedure Check_Overriding_Indicator
5722 (Subp : Entity_Id;
5723 Overridden_Subp : Entity_Id;
5724 Is_Primitive : Boolean)
5726 Decl : Node_Id;
5727 Spec : Node_Id;
5729 begin
5730 -- No overriding indicator for literals
5732 if Ekind (Subp) = E_Enumeration_Literal then
5733 return;
5735 elsif Ekind (Subp) = E_Entry then
5736 Decl := Parent (Subp);
5738 -- No point in analyzing a malformed operator
5740 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5741 and then Error_Posted (Subp)
5742 then
5743 return;
5745 else
5746 Decl := Unit_Declaration_Node (Subp);
5747 end if;
5749 if Nkind_In (Decl, N_Subprogram_Body,
5750 N_Subprogram_Body_Stub,
5751 N_Subprogram_Declaration,
5752 N_Abstract_Subprogram_Declaration,
5753 N_Subprogram_Renaming_Declaration)
5754 then
5755 Spec := Specification (Decl);
5757 elsif Nkind (Decl) = N_Entry_Declaration then
5758 Spec := Decl;
5760 else
5761 return;
5762 end if;
5764 -- The overriding operation is type conformant with the overridden one,
5765 -- but the names of the formals are not required to match. If the names
5766 -- appear permuted in the overriding operation, this is a possible
5767 -- source of confusion that is worth diagnosing. Controlling formals
5768 -- often carry names that reflect the type, and it is not worthwhile
5769 -- requiring that their names match.
5771 if Present (Overridden_Subp)
5772 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5773 then
5774 declare
5775 Form1 : Entity_Id;
5776 Form2 : Entity_Id;
5778 begin
5779 Form1 := First_Formal (Subp);
5780 Form2 := First_Formal (Overridden_Subp);
5782 -- If the overriding operation is a synchronized operation, skip
5783 -- the first parameter of the overridden operation, which is
5784 -- implicit in the new one. If the operation is declared in the
5785 -- body it is not primitive and all formals must match.
5787 if Is_Concurrent_Type (Scope (Subp))
5788 and then Is_Tagged_Type (Scope (Subp))
5789 and then not Has_Completion (Scope (Subp))
5790 then
5791 Form2 := Next_Formal (Form2);
5792 end if;
5794 if Present (Form1) then
5795 Form1 := Next_Formal (Form1);
5796 Form2 := Next_Formal (Form2);
5797 end if;
5799 while Present (Form1) loop
5800 if not Is_Controlling_Formal (Form1)
5801 and then Present (Next_Formal (Form2))
5802 and then Chars (Form1) = Chars (Next_Formal (Form2))
5803 then
5804 Error_Msg_Node_2 := Alias (Overridden_Subp);
5805 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5806 Error_Msg_NE
5807 ("& does not match corresponding formal of&#",
5808 Form1, Form1);
5809 exit;
5810 end if;
5812 Next_Formal (Form1);
5813 Next_Formal (Form2);
5814 end loop;
5815 end;
5816 end if;
5818 -- If there is an overridden subprogram, then check that there is no
5819 -- "not overriding" indicator, and mark the subprogram as overriding.
5820 -- This is not done if the overridden subprogram is marked as hidden,
5821 -- which can occur for the case of inherited controlled operations
5822 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5823 -- subprogram is not itself hidden. (Note: This condition could probably
5824 -- be simplified, leaving out the testing for the specific controlled
5825 -- cases, but it seems safer and clearer this way, and echoes similar
5826 -- special-case tests of this kind in other places.)
5828 if Present (Overridden_Subp)
5829 and then (not Is_Hidden (Overridden_Subp)
5830 or else
5831 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5832 Name_Adjust,
5833 Name_Finalize)
5834 and then Present (Alias (Overridden_Subp))
5835 and then not Is_Hidden (Alias (Overridden_Subp))))
5836 then
5837 if Must_Not_Override (Spec) then
5838 Error_Msg_Sloc := Sloc (Overridden_Subp);
5840 if Ekind (Subp) = E_Entry then
5841 Error_Msg_NE
5842 ("entry & overrides inherited operation #", Spec, Subp);
5843 else
5844 Error_Msg_NE
5845 ("subprogram & overrides inherited operation #", Spec, Subp);
5846 end if;
5848 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5849 -- as an extension of Root_Controlled, and thus has a useless Adjust
5850 -- operation. This operation should not be inherited by other limited
5851 -- controlled types. An explicit Adjust for them is not overriding.
5853 elsif Must_Override (Spec)
5854 and then Chars (Overridden_Subp) = Name_Adjust
5855 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5856 and then Present (Alias (Overridden_Subp))
5857 and then
5858 Is_Predefined_File_Name
5859 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5860 then
5861 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5863 elsif Is_Subprogram (Subp) then
5864 if Is_Init_Proc (Subp) then
5865 null;
5867 elsif No (Overridden_Operation (Subp)) then
5869 -- For entities generated by Derive_Subprograms the overridden
5870 -- operation is the inherited primitive (which is available
5871 -- through the attribute alias)
5873 if (Is_Dispatching_Operation (Subp)
5874 or else Is_Dispatching_Operation (Overridden_Subp))
5875 and then not Comes_From_Source (Overridden_Subp)
5876 and then Find_Dispatching_Type (Overridden_Subp) =
5877 Find_Dispatching_Type (Subp)
5878 and then Present (Alias (Overridden_Subp))
5879 and then Comes_From_Source (Alias (Overridden_Subp))
5880 then
5881 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5882 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5884 else
5885 Set_Overridden_Operation (Subp, Overridden_Subp);
5886 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5887 end if;
5888 end if;
5889 end if;
5891 -- If primitive flag is set or this is a protected operation, then
5892 -- the operation is overriding at the point of its declaration, so
5893 -- warn if necessary. Otherwise it may have been declared before the
5894 -- operation it overrides and no check is required.
5896 if Style_Check
5897 and then not Must_Override (Spec)
5898 and then (Is_Primitive
5899 or else Ekind (Scope (Subp)) = E_Protected_Type)
5900 then
5901 Style.Missing_Overriding (Decl, Subp);
5902 end if;
5904 -- If Subp is an operator, it may override a predefined operation, if
5905 -- it is defined in the same scope as the type to which it applies.
5906 -- In that case Overridden_Subp is empty because of our implicit
5907 -- representation for predefined operators. We have to check whether the
5908 -- signature of Subp matches that of a predefined operator. Note that
5909 -- first argument provides the name of the operator, and the second
5910 -- argument the signature that may match that of a standard operation.
5911 -- If the indicator is overriding, then the operator must match a
5912 -- predefined signature, because we know already that there is no
5913 -- explicit overridden operation.
5915 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5916 if Must_Not_Override (Spec) then
5918 -- If this is not a primitive or a protected subprogram, then
5919 -- "not overriding" is illegal.
5921 if not Is_Primitive
5922 and then Ekind (Scope (Subp)) /= E_Protected_Type
5923 then
5924 Error_Msg_N ("overriding indicator only allowed "
5925 & "if subprogram is primitive", Subp);
5927 elsif Can_Override_Operator (Subp) then
5928 Error_Msg_NE
5929 ("subprogram& overrides predefined operator ", Spec, Subp);
5930 end if;
5932 elsif Must_Override (Spec) then
5933 if No (Overridden_Operation (Subp))
5934 and then not Can_Override_Operator (Subp)
5935 then
5936 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5937 end if;
5939 elsif not Error_Posted (Subp)
5940 and then Style_Check
5941 and then Can_Override_Operator (Subp)
5942 and then
5943 not Is_Predefined_File_Name
5944 (Unit_File_Name (Get_Source_Unit (Subp)))
5945 then
5946 -- If style checks are enabled, indicate that the indicator is
5947 -- missing. However, at the point of declaration, the type of
5948 -- which this is a primitive operation may be private, in which
5949 -- case the indicator would be premature.
5951 if Has_Private_Declaration (Etype (Subp))
5952 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5953 then
5954 null;
5955 else
5956 Style.Missing_Overriding (Decl, Subp);
5957 end if;
5958 end if;
5960 elsif Must_Override (Spec) then
5961 if Ekind (Subp) = E_Entry then
5962 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5963 else
5964 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5965 end if;
5967 -- If the operation is marked "not overriding" and it's not primitive
5968 -- then an error is issued, unless this is an operation of a task or
5969 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5970 -- has been specified have already been checked above.
5972 elsif Must_Not_Override (Spec)
5973 and then not Is_Primitive
5974 and then Ekind (Subp) /= E_Entry
5975 and then Ekind (Scope (Subp)) /= E_Protected_Type
5976 then
5977 Error_Msg_N
5978 ("overriding indicator only allowed if subprogram is primitive",
5979 Subp);
5980 return;
5981 end if;
5982 end Check_Overriding_Indicator;
5984 -------------------
5985 -- Check_Returns --
5986 -------------------
5988 -- Note: this procedure needs to know far too much about how the expander
5989 -- messes with exceptions. The use of the flag Exception_Junk and the
5990 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5991 -- works, but is not very clean. It would be better if the expansion
5992 -- routines would leave Original_Node working nicely, and we could use
5993 -- Original_Node here to ignore all the peculiar expander messing ???
5995 procedure Check_Returns
5996 (HSS : Node_Id;
5997 Mode : Character;
5998 Err : out Boolean;
5999 Proc : Entity_Id := Empty)
6001 Handler : Node_Id;
6003 procedure Check_Statement_Sequence (L : List_Id);
6004 -- Internal recursive procedure to check a list of statements for proper
6005 -- termination by a return statement (or a transfer of control or a
6006 -- compound statement that is itself internally properly terminated).
6008 ------------------------------
6009 -- Check_Statement_Sequence --
6010 ------------------------------
6012 procedure Check_Statement_Sequence (L : List_Id) is
6013 Last_Stm : Node_Id;
6014 Stm : Node_Id;
6015 Kind : Node_Kind;
6017 function Assert_False return Boolean;
6018 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6019 -- rewritten as a null statement when assertions are off. The assert
6020 -- is not active, but it is still enough to kill the warning.
6022 ------------------
6023 -- Assert_False --
6024 ------------------
6026 function Assert_False return Boolean is
6027 Orig : constant Node_Id := Original_Node (Last_Stm);
6029 begin
6030 if Nkind (Orig) = N_Pragma
6031 and then Pragma_Name (Orig) = Name_Assert
6032 and then not Error_Posted (Orig)
6033 then
6034 declare
6035 Arg : constant Node_Id :=
6036 First (Pragma_Argument_Associations (Orig));
6037 Exp : constant Node_Id := Expression (Arg);
6038 begin
6039 return Nkind (Exp) = N_Identifier
6040 and then Chars (Exp) = Name_False;
6041 end;
6043 else
6044 return False;
6045 end if;
6046 end Assert_False;
6048 -- Local variables
6050 Raise_Exception_Call : Boolean;
6051 -- Set True if statement sequence terminated by Raise_Exception call
6052 -- or a Reraise_Occurrence call.
6054 -- Start of processing for Check_Statement_Sequence
6056 begin
6057 Raise_Exception_Call := False;
6059 -- Get last real statement
6061 Last_Stm := Last (L);
6063 -- Deal with digging out exception handler statement sequences that
6064 -- have been transformed by the local raise to goto optimization.
6065 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6066 -- optimization has occurred, we are looking at something like:
6068 -- begin
6069 -- original stmts in block
6071 -- exception \
6072 -- when excep1 => |
6073 -- goto L1; | omitted if No_Exception_Propagation
6074 -- when excep2 => |
6075 -- goto L2; /
6076 -- end;
6078 -- goto L3; -- skip handler when exception not raised
6080 -- <<L1>> -- target label for local exception
6081 -- begin
6082 -- estmts1
6083 -- end;
6085 -- goto L3;
6087 -- <<L2>>
6088 -- begin
6089 -- estmts2
6090 -- end;
6092 -- <<L3>>
6094 -- and what we have to do is to dig out the estmts1 and estmts2
6095 -- sequences (which were the original sequences of statements in
6096 -- the exception handlers) and check them.
6098 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6099 Stm := Last_Stm;
6100 loop
6101 Prev (Stm);
6102 exit when No (Stm);
6103 exit when Nkind (Stm) /= N_Block_Statement;
6104 exit when not Exception_Junk (Stm);
6105 Prev (Stm);
6106 exit when No (Stm);
6107 exit when Nkind (Stm) /= N_Label;
6108 exit when not Exception_Junk (Stm);
6109 Check_Statement_Sequence
6110 (Statements (Handled_Statement_Sequence (Next (Stm))));
6112 Prev (Stm);
6113 Last_Stm := Stm;
6114 exit when No (Stm);
6115 exit when Nkind (Stm) /= N_Goto_Statement;
6116 exit when not Exception_Junk (Stm);
6117 end loop;
6118 end if;
6120 -- Don't count pragmas
6122 while Nkind (Last_Stm) = N_Pragma
6124 -- Don't count call to SS_Release (can happen after Raise_Exception)
6126 or else
6127 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6128 and then
6129 Nkind (Name (Last_Stm)) = N_Identifier
6130 and then
6131 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6133 -- Don't count exception junk
6135 or else
6136 (Nkind_In (Last_Stm, N_Goto_Statement,
6137 N_Label,
6138 N_Object_Declaration)
6139 and then Exception_Junk (Last_Stm))
6140 or else Nkind (Last_Stm) in N_Push_xxx_Label
6141 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6143 -- Inserted code, such as finalization calls, is irrelevant: we only
6144 -- need to check original source.
6146 or else Is_Rewrite_Insertion (Last_Stm)
6147 loop
6148 Prev (Last_Stm);
6149 end loop;
6151 -- Here we have the "real" last statement
6153 Kind := Nkind (Last_Stm);
6155 -- Transfer of control, OK. Note that in the No_Return procedure
6156 -- case, we already diagnosed any explicit return statements, so
6157 -- we can treat them as OK in this context.
6159 if Is_Transfer (Last_Stm) then
6160 return;
6162 -- Check cases of explicit non-indirect procedure calls
6164 elsif Kind = N_Procedure_Call_Statement
6165 and then Is_Entity_Name (Name (Last_Stm))
6166 then
6167 -- Check call to Raise_Exception procedure which is treated
6168 -- specially, as is a call to Reraise_Occurrence.
6170 -- We suppress the warning in these cases since it is likely that
6171 -- the programmer really does not expect to deal with the case
6172 -- of Null_Occurrence, and thus would find a warning about a
6173 -- missing return curious, and raising Program_Error does not
6174 -- seem such a bad behavior if this does occur.
6176 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6177 -- behavior will be to raise Constraint_Error (see AI-329).
6179 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6180 or else
6181 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6182 then
6183 Raise_Exception_Call := True;
6185 -- For Raise_Exception call, test first argument, if it is
6186 -- an attribute reference for a 'Identity call, then we know
6187 -- that the call cannot possibly return.
6189 declare
6190 Arg : constant Node_Id :=
6191 Original_Node (First_Actual (Last_Stm));
6192 begin
6193 if Nkind (Arg) = N_Attribute_Reference
6194 and then Attribute_Name (Arg) = Name_Identity
6195 then
6196 return;
6197 end if;
6198 end;
6199 end if;
6201 -- If statement, need to look inside if there is an else and check
6202 -- each constituent statement sequence for proper termination.
6204 elsif Kind = N_If_Statement
6205 and then Present (Else_Statements (Last_Stm))
6206 then
6207 Check_Statement_Sequence (Then_Statements (Last_Stm));
6208 Check_Statement_Sequence (Else_Statements (Last_Stm));
6210 if Present (Elsif_Parts (Last_Stm)) then
6211 declare
6212 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6214 begin
6215 while Present (Elsif_Part) loop
6216 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6217 Next (Elsif_Part);
6218 end loop;
6219 end;
6220 end if;
6222 return;
6224 -- Case statement, check each case for proper termination
6226 elsif Kind = N_Case_Statement then
6227 declare
6228 Case_Alt : Node_Id;
6229 begin
6230 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6231 while Present (Case_Alt) loop
6232 Check_Statement_Sequence (Statements (Case_Alt));
6233 Next_Non_Pragma (Case_Alt);
6234 end loop;
6235 end;
6237 return;
6239 -- Block statement, check its handled sequence of statements
6241 elsif Kind = N_Block_Statement then
6242 declare
6243 Err1 : Boolean;
6245 begin
6246 Check_Returns
6247 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6249 if Err1 then
6250 Err := True;
6251 end if;
6253 return;
6254 end;
6256 -- Loop statement. If there is an iteration scheme, we can definitely
6257 -- fall out of the loop. Similarly if there is an exit statement, we
6258 -- can fall out. In either case we need a following return.
6260 elsif Kind = N_Loop_Statement then
6261 if Present (Iteration_Scheme (Last_Stm))
6262 or else Has_Exit (Entity (Identifier (Last_Stm)))
6263 then
6264 null;
6266 -- A loop with no exit statement or iteration scheme is either
6267 -- an infinite loop, or it has some other exit (raise/return).
6268 -- In either case, no warning is required.
6270 else
6271 return;
6272 end if;
6274 -- Timed entry call, check entry call and delay alternatives
6276 -- Note: in expanded code, the timed entry call has been converted
6277 -- to a set of expanded statements on which the check will work
6278 -- correctly in any case.
6280 elsif Kind = N_Timed_Entry_Call then
6281 declare
6282 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6283 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6285 begin
6286 -- If statement sequence of entry call alternative is missing,
6287 -- then we can definitely fall through, and we post the error
6288 -- message on the entry call alternative itself.
6290 if No (Statements (ECA)) then
6291 Last_Stm := ECA;
6293 -- If statement sequence of delay alternative is missing, then
6294 -- we can definitely fall through, and we post the error
6295 -- message on the delay alternative itself.
6297 -- Note: if both ECA and DCA are missing the return, then we
6298 -- post only one message, should be enough to fix the bugs.
6299 -- If not we will get a message next time on the DCA when the
6300 -- ECA is fixed.
6302 elsif No (Statements (DCA)) then
6303 Last_Stm := DCA;
6305 -- Else check both statement sequences
6307 else
6308 Check_Statement_Sequence (Statements (ECA));
6309 Check_Statement_Sequence (Statements (DCA));
6310 return;
6311 end if;
6312 end;
6314 -- Conditional entry call, check entry call and else part
6316 -- Note: in expanded code, the conditional entry call has been
6317 -- converted to a set of expanded statements on which the check
6318 -- will work correctly in any case.
6320 elsif Kind = N_Conditional_Entry_Call then
6321 declare
6322 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6324 begin
6325 -- If statement sequence of entry call alternative is missing,
6326 -- then we can definitely fall through, and we post the error
6327 -- message on the entry call alternative itself.
6329 if No (Statements (ECA)) then
6330 Last_Stm := ECA;
6332 -- Else check statement sequence and else part
6334 else
6335 Check_Statement_Sequence (Statements (ECA));
6336 Check_Statement_Sequence (Else_Statements (Last_Stm));
6337 return;
6338 end if;
6339 end;
6340 end if;
6342 -- If we fall through, issue appropriate message
6344 if Mode = 'F' then
6346 -- Kill warning if last statement is a raise exception call,
6347 -- or a pragma Assert (False). Note that with assertions enabled,
6348 -- such a pragma has been converted into a raise exception call
6349 -- already, so the Assert_False is for the assertions off case.
6351 if not Raise_Exception_Call and then not Assert_False then
6353 -- In GNATprove mode, it is an error to have a missing return
6355 Error_Msg_Warn := SPARK_Mode /= On;
6357 -- Issue error message or warning
6359 Error_Msg_N
6360 ("RETURN statement missing following this statement<<!",
6361 Last_Stm);
6362 Error_Msg_N
6363 ("\Program_Error ]<<!", Last_Stm);
6364 end if;
6366 -- Note: we set Err even though we have not issued a warning
6367 -- because we still have a case of a missing return. This is
6368 -- an extremely marginal case, probably will never be noticed
6369 -- but we might as well get it right.
6371 Err := True;
6373 -- Otherwise we have the case of a procedure marked No_Return
6375 else
6376 if not Raise_Exception_Call then
6377 if GNATprove_Mode then
6378 Error_Msg_N
6379 ("implied return after this statement "
6380 & "would have raised Program_Error", Last_Stm);
6381 else
6382 Error_Msg_N
6383 ("implied return after this statement "
6384 & "will raise Program_Error??", Last_Stm);
6385 end if;
6387 Error_Msg_Warn := SPARK_Mode /= On;
6388 Error_Msg_NE
6389 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6390 end if;
6392 declare
6393 RE : constant Node_Id :=
6394 Make_Raise_Program_Error (Sloc (Last_Stm),
6395 Reason => PE_Implicit_Return);
6396 begin
6397 Insert_After (Last_Stm, RE);
6398 Analyze (RE);
6399 end;
6400 end if;
6401 end Check_Statement_Sequence;
6403 -- Start of processing for Check_Returns
6405 begin
6406 Err := False;
6407 Check_Statement_Sequence (Statements (HSS));
6409 if Present (Exception_Handlers (HSS)) then
6410 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6411 while Present (Handler) loop
6412 Check_Statement_Sequence (Statements (Handler));
6413 Next_Non_Pragma (Handler);
6414 end loop;
6415 end if;
6416 end Check_Returns;
6418 ----------------------------
6419 -- Check_Subprogram_Order --
6420 ----------------------------
6422 procedure Check_Subprogram_Order (N : Node_Id) is
6424 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6425 -- This is used to check if S1 > S2 in the sense required by this test,
6426 -- for example nameab < namec, but name2 < name10.
6428 -----------------------------
6429 -- Subprogram_Name_Greater --
6430 -----------------------------
6432 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6433 L1, L2 : Positive;
6434 N1, N2 : Natural;
6436 begin
6437 -- Deal with special case where names are identical except for a
6438 -- numerical suffix. These are handled specially, taking the numeric
6439 -- ordering from the suffix into account.
6441 L1 := S1'Last;
6442 while S1 (L1) in '0' .. '9' loop
6443 L1 := L1 - 1;
6444 end loop;
6446 L2 := S2'Last;
6447 while S2 (L2) in '0' .. '9' loop
6448 L2 := L2 - 1;
6449 end loop;
6451 -- If non-numeric parts non-equal, do straight compare
6453 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6454 return S1 > S2;
6456 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6457 -- that a missing suffix is treated as numeric zero in this test.
6459 else
6460 N1 := 0;
6461 while L1 < S1'Last loop
6462 L1 := L1 + 1;
6463 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6464 end loop;
6466 N2 := 0;
6467 while L2 < S2'Last loop
6468 L2 := L2 + 1;
6469 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6470 end loop;
6472 return N1 > N2;
6473 end if;
6474 end Subprogram_Name_Greater;
6476 -- Start of processing for Check_Subprogram_Order
6478 begin
6479 -- Check body in alpha order if this is option
6481 if Style_Check
6482 and then Style_Check_Order_Subprograms
6483 and then Nkind (N) = N_Subprogram_Body
6484 and then Comes_From_Source (N)
6485 and then In_Extended_Main_Source_Unit (N)
6486 then
6487 declare
6488 LSN : String_Ptr
6489 renames Scope_Stack.Table
6490 (Scope_Stack.Last).Last_Subprogram_Name;
6492 Body_Id : constant Entity_Id :=
6493 Defining_Entity (Specification (N));
6495 begin
6496 Get_Decoded_Name_String (Chars (Body_Id));
6498 if LSN /= null then
6499 if Subprogram_Name_Greater
6500 (LSN.all, Name_Buffer (1 .. Name_Len))
6501 then
6502 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6503 end if;
6505 Free (LSN);
6506 end if;
6508 LSN := new String'(Name_Buffer (1 .. Name_Len));
6509 end;
6510 end if;
6511 end Check_Subprogram_Order;
6513 ------------------------------
6514 -- Check_Subtype_Conformant --
6515 ------------------------------
6517 procedure Check_Subtype_Conformant
6518 (New_Id : Entity_Id;
6519 Old_Id : Entity_Id;
6520 Err_Loc : Node_Id := Empty;
6521 Skip_Controlling_Formals : Boolean := False;
6522 Get_Inst : Boolean := False)
6524 Result : Boolean;
6525 pragma Warnings (Off, Result);
6526 begin
6527 Check_Conformance
6528 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6529 Skip_Controlling_Formals => Skip_Controlling_Formals,
6530 Get_Inst => Get_Inst);
6531 end Check_Subtype_Conformant;
6533 ---------------------------
6534 -- Check_Type_Conformant --
6535 ---------------------------
6537 procedure Check_Type_Conformant
6538 (New_Id : Entity_Id;
6539 Old_Id : Entity_Id;
6540 Err_Loc : Node_Id := Empty)
6542 Result : Boolean;
6543 pragma Warnings (Off, Result);
6544 begin
6545 Check_Conformance
6546 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6547 end Check_Type_Conformant;
6549 ---------------------------
6550 -- Can_Override_Operator --
6551 ---------------------------
6553 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6554 Typ : Entity_Id;
6556 begin
6557 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6558 return False;
6560 else
6561 Typ := Base_Type (Etype (First_Formal (Subp)));
6563 -- Check explicitly that the operation is a primitive of the type
6565 return Operator_Matches_Spec (Subp, Subp)
6566 and then not Is_Generic_Type (Typ)
6567 and then Scope (Subp) = Scope (Typ)
6568 and then not Is_Class_Wide_Type (Typ);
6569 end if;
6570 end Can_Override_Operator;
6572 ----------------------
6573 -- Conforming_Types --
6574 ----------------------
6576 function Conforming_Types
6577 (T1 : Entity_Id;
6578 T2 : Entity_Id;
6579 Ctype : Conformance_Type;
6580 Get_Inst : Boolean := False) return Boolean
6582 Type_1 : Entity_Id := T1;
6583 Type_2 : Entity_Id := T2;
6584 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6586 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6587 -- If neither T1 nor T2 are generic actual types, or if they are in
6588 -- different scopes (e.g. parent and child instances), then verify that
6589 -- the base types are equal. Otherwise T1 and T2 must be on the same
6590 -- subtype chain. The whole purpose of this procedure is to prevent
6591 -- spurious ambiguities in an instantiation that may arise if two
6592 -- distinct generic types are instantiated with the same actual.
6594 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6595 -- An access parameter can designate an incomplete type. If the
6596 -- incomplete type is the limited view of a type from a limited_
6597 -- with_clause, check whether the non-limited view is available. If
6598 -- it is a (non-limited) incomplete type, get the full view.
6600 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6601 -- Returns True if and only if either T1 denotes a limited view of T2
6602 -- or T2 denotes a limited view of T1. This can arise when the limited
6603 -- with view of a type is used in a subprogram declaration and the
6604 -- subprogram body is in the scope of a regular with clause for the
6605 -- same unit. In such a case, the two type entities can be considered
6606 -- identical for purposes of conformance checking.
6608 ----------------------
6609 -- Base_Types_Match --
6610 ----------------------
6612 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6613 BT1 : constant Entity_Id := Base_Type (T1);
6614 BT2 : constant Entity_Id := Base_Type (T2);
6616 begin
6617 if T1 = T2 then
6618 return True;
6620 elsif BT1 = BT2 then
6622 -- The following is too permissive. A more precise test should
6623 -- check that the generic actual is an ancestor subtype of the
6624 -- other ???.
6626 -- See code in Find_Corresponding_Spec that applies an additional
6627 -- filter to handle accidental amiguities in instances.
6629 return not Is_Generic_Actual_Type (T1)
6630 or else not Is_Generic_Actual_Type (T2)
6631 or else Scope (T1) /= Scope (T2);
6633 -- If T2 is a generic actual type it is declared as the subtype of
6634 -- the actual. If that actual is itself a subtype we need to use its
6635 -- own base type to check for compatibility.
6637 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6638 return True;
6640 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6641 return True;
6643 else
6644 return False;
6645 end if;
6646 end Base_Types_Match;
6648 --------------------------
6649 -- Find_Designated_Type --
6650 --------------------------
6652 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6653 Desig : Entity_Id;
6655 begin
6656 Desig := Directly_Designated_Type (T);
6658 if Ekind (Desig) = E_Incomplete_Type then
6660 -- If regular incomplete type, get full view if available
6662 if Present (Full_View (Desig)) then
6663 Desig := Full_View (Desig);
6665 -- If limited view of a type, get non-limited view if available,
6666 -- and check again for a regular incomplete type.
6668 elsif Present (Non_Limited_View (Desig)) then
6669 Desig := Get_Full_View (Non_Limited_View (Desig));
6670 end if;
6671 end if;
6673 return Desig;
6674 end Find_Designated_Type;
6676 -------------------------------
6677 -- Matches_Limited_With_View --
6678 -------------------------------
6680 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6681 begin
6682 -- In some cases a type imported through a limited_with clause, and
6683 -- its nonlimited view are both visible, for example in an anonymous
6684 -- access-to-class-wide type in a formal, or when building the body
6685 -- for a subprogram renaming after the subprogram has been frozen.
6686 -- In these cases Both entities designate the same type. In addition,
6687 -- if one of them is an actual in an instance, it may be a subtype of
6688 -- the non-limited view of the other.
6690 if From_Limited_With (T1)
6691 and then (T2 = Available_View (T1)
6692 or else Is_Subtype_Of (T2, Available_View (T1)))
6693 then
6694 return True;
6696 elsif From_Limited_With (T2)
6697 and then (T1 = Available_View (T2)
6698 or else Is_Subtype_Of (T1, Available_View (T2)))
6699 then
6700 return True;
6702 elsif From_Limited_With (T1)
6703 and then From_Limited_With (T2)
6704 and then Available_View (T1) = Available_View (T2)
6705 then
6706 return True;
6708 else
6709 return False;
6710 end if;
6711 end Matches_Limited_With_View;
6713 -- Start of processing for Conforming_Types
6715 begin
6716 -- The context is an instance association for a formal access-to-
6717 -- subprogram type; the formal parameter types require mapping because
6718 -- they may denote other formal parameters of the generic unit.
6720 if Get_Inst then
6721 Type_1 := Get_Instance_Of (T1);
6722 Type_2 := Get_Instance_Of (T2);
6723 end if;
6725 -- If one of the types is a view of the other introduced by a limited
6726 -- with clause, treat these as conforming for all purposes.
6728 if Matches_Limited_With_View (T1, T2) then
6729 return True;
6731 elsif Base_Types_Match (Type_1, Type_2) then
6732 return Ctype <= Mode_Conformant
6733 or else Subtypes_Statically_Match (Type_1, Type_2);
6735 elsif Is_Incomplete_Or_Private_Type (Type_1)
6736 and then Present (Full_View (Type_1))
6737 and then Base_Types_Match (Full_View (Type_1), Type_2)
6738 then
6739 return Ctype <= Mode_Conformant
6740 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6742 elsif Ekind (Type_2) = E_Incomplete_Type
6743 and then Present (Full_View (Type_2))
6744 and then Base_Types_Match (Type_1, Full_View (Type_2))
6745 then
6746 return Ctype <= Mode_Conformant
6747 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6749 elsif Is_Private_Type (Type_2)
6750 and then In_Instance
6751 and then Present (Full_View (Type_2))
6752 and then Base_Types_Match (Type_1, Full_View (Type_2))
6753 then
6754 return Ctype <= Mode_Conformant
6755 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6756 end if;
6758 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6759 -- treated recursively because they carry a signature. As far as
6760 -- conformance is concerned, convention plays no role, and either
6761 -- or both could be access to protected subprograms.
6763 Are_Anonymous_Access_To_Subprogram_Types :=
6764 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6765 E_Anonymous_Access_Protected_Subprogram_Type)
6766 and then
6767 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6768 E_Anonymous_Access_Protected_Subprogram_Type);
6770 -- Test anonymous access type case. For this case, static subtype
6771 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6772 -- the base types because we may have built internal subtype entities
6773 -- to handle null-excluding types (see Process_Formals).
6775 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6776 and then
6777 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6779 -- Ada 2005 (AI-254)
6781 or else Are_Anonymous_Access_To_Subprogram_Types
6782 then
6783 declare
6784 Desig_1 : Entity_Id;
6785 Desig_2 : Entity_Id;
6787 begin
6788 -- In Ada 2005, access constant indicators must match for
6789 -- subtype conformance.
6791 if Ada_Version >= Ada_2005
6792 and then Ctype >= Subtype_Conformant
6793 and then
6794 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6795 then
6796 return False;
6797 end if;
6799 Desig_1 := Find_Designated_Type (Type_1);
6800 Desig_2 := Find_Designated_Type (Type_2);
6802 -- If the context is an instance association for a formal
6803 -- access-to-subprogram type; formal access parameter designated
6804 -- types require mapping because they may denote other formal
6805 -- parameters of the generic unit.
6807 if Get_Inst then
6808 Desig_1 := Get_Instance_Of (Desig_1);
6809 Desig_2 := Get_Instance_Of (Desig_2);
6810 end if;
6812 -- It is possible for a Class_Wide_Type to be introduced for an
6813 -- incomplete type, in which case there is a separate class_ wide
6814 -- type for the full view. The types conform if their Etypes
6815 -- conform, i.e. one may be the full view of the other. This can
6816 -- only happen in the context of an access parameter, other uses
6817 -- of an incomplete Class_Wide_Type are illegal.
6819 if Is_Class_Wide_Type (Desig_1)
6820 and then
6821 Is_Class_Wide_Type (Desig_2)
6822 then
6823 return
6824 Conforming_Types
6825 (Etype (Base_Type (Desig_1)),
6826 Etype (Base_Type (Desig_2)), Ctype);
6828 elsif Are_Anonymous_Access_To_Subprogram_Types then
6829 if Ada_Version < Ada_2005 then
6830 return Ctype = Type_Conformant
6831 or else
6832 Subtypes_Statically_Match (Desig_1, Desig_2);
6834 -- We must check the conformance of the signatures themselves
6836 else
6837 declare
6838 Conformant : Boolean;
6839 begin
6840 Check_Conformance
6841 (Desig_1, Desig_2, Ctype, False, Conformant);
6842 return Conformant;
6843 end;
6844 end if;
6846 else
6847 return Base_Type (Desig_1) = Base_Type (Desig_2)
6848 and then (Ctype = Type_Conformant
6849 or else
6850 Subtypes_Statically_Match (Desig_1, Desig_2));
6851 end if;
6852 end;
6854 -- Otherwise definitely no match
6856 else
6857 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6858 and then Is_Access_Type (Type_2))
6859 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6860 and then Is_Access_Type (Type_1)))
6861 and then
6862 Conforming_Types
6863 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6864 then
6865 May_Hide_Profile := True;
6866 end if;
6868 return False;
6869 end if;
6870 end Conforming_Types;
6872 --------------------------
6873 -- Create_Extra_Formals --
6874 --------------------------
6876 procedure Create_Extra_Formals (E : Entity_Id) is
6877 Formal : Entity_Id;
6878 First_Extra : Entity_Id := Empty;
6879 Last_Extra : Entity_Id;
6880 Formal_Type : Entity_Id;
6881 P_Formal : Entity_Id := Empty;
6883 function Add_Extra_Formal
6884 (Assoc_Entity : Entity_Id;
6885 Typ : Entity_Id;
6886 Scope : Entity_Id;
6887 Suffix : String) return Entity_Id;
6888 -- Add an extra formal to the current list of formals and extra formals.
6889 -- The extra formal is added to the end of the list of extra formals,
6890 -- and also returned as the result. These formals are always of mode IN.
6891 -- The new formal has the type Typ, is declared in Scope, and its name
6892 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6893 -- The following suffixes are currently used. They should not be changed
6894 -- without coordinating with CodePeer, which makes use of these to
6895 -- provide better messages.
6897 -- O denotes the Constrained bit.
6898 -- L denotes the accessibility level.
6899 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6900 -- the full list in exp_ch6.BIP_Formal_Kind.
6902 ----------------------
6903 -- Add_Extra_Formal --
6904 ----------------------
6906 function Add_Extra_Formal
6907 (Assoc_Entity : Entity_Id;
6908 Typ : Entity_Id;
6909 Scope : Entity_Id;
6910 Suffix : String) return Entity_Id
6912 EF : constant Entity_Id :=
6913 Make_Defining_Identifier (Sloc (Assoc_Entity),
6914 Chars => New_External_Name (Chars (Assoc_Entity),
6915 Suffix => Suffix));
6917 begin
6918 -- A little optimization. Never generate an extra formal for the
6919 -- _init operand of an initialization procedure, since it could
6920 -- never be used.
6922 if Chars (Formal) = Name_uInit then
6923 return Empty;
6924 end if;
6926 Set_Ekind (EF, E_In_Parameter);
6927 Set_Actual_Subtype (EF, Typ);
6928 Set_Etype (EF, Typ);
6929 Set_Scope (EF, Scope);
6930 Set_Mechanism (EF, Default_Mechanism);
6931 Set_Formal_Validity (EF);
6933 if No (First_Extra) then
6934 First_Extra := EF;
6935 Set_Extra_Formals (Scope, First_Extra);
6936 end if;
6938 if Present (Last_Extra) then
6939 Set_Extra_Formal (Last_Extra, EF);
6940 end if;
6942 Last_Extra := EF;
6944 return EF;
6945 end Add_Extra_Formal;
6947 -- Start of processing for Create_Extra_Formals
6949 begin
6950 -- We never generate extra formals if expansion is not active because we
6951 -- don't need them unless we are generating code.
6953 if not Expander_Active then
6954 return;
6955 end if;
6957 -- No need to generate extra formals in interface thunks whose target
6958 -- primitive has no extra formals.
6960 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6961 return;
6962 end if;
6964 -- If this is a derived subprogram then the subtypes of the parent
6965 -- subprogram's formal parameters will be used to determine the need
6966 -- for extra formals.
6968 if Is_Overloadable (E) and then Present (Alias (E)) then
6969 P_Formal := First_Formal (Alias (E));
6970 end if;
6972 Last_Extra := Empty;
6973 Formal := First_Formal (E);
6974 while Present (Formal) loop
6975 Last_Extra := Formal;
6976 Next_Formal (Formal);
6977 end loop;
6979 -- If Extra_formals were already created, don't do it again. This
6980 -- situation may arise for subprogram types created as part of
6981 -- dispatching calls (see Expand_Dispatching_Call)
6983 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6984 return;
6985 end if;
6987 -- If the subprogram is a predefined dispatching subprogram then don't
6988 -- generate any extra constrained or accessibility level formals. In
6989 -- general we suppress these for internal subprograms (by not calling
6990 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6991 -- generated stream attributes do get passed through because extra
6992 -- build-in-place formals are needed in some cases (limited 'Input).
6994 if Is_Predefined_Internal_Operation (E) then
6995 goto Test_For_Func_Result_Extras;
6996 end if;
6998 Formal := First_Formal (E);
6999 while Present (Formal) loop
7001 -- Create extra formal for supporting the attribute 'Constrained.
7002 -- The case of a private type view without discriminants also
7003 -- requires the extra formal if the underlying type has defaulted
7004 -- discriminants.
7006 if Ekind (Formal) /= E_In_Parameter then
7007 if Present (P_Formal) then
7008 Formal_Type := Etype (P_Formal);
7009 else
7010 Formal_Type := Etype (Formal);
7011 end if;
7013 -- Do not produce extra formals for Unchecked_Union parameters.
7014 -- Jump directly to the end of the loop.
7016 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7017 goto Skip_Extra_Formal_Generation;
7018 end if;
7020 if not Has_Discriminants (Formal_Type)
7021 and then Ekind (Formal_Type) in Private_Kind
7022 and then Present (Underlying_Type (Formal_Type))
7023 then
7024 Formal_Type := Underlying_Type (Formal_Type);
7025 end if;
7027 -- Suppress the extra formal if formal's subtype is constrained or
7028 -- indefinite, or we're compiling for Ada 2012 and the underlying
7029 -- type is tagged and limited. In Ada 2012, a limited tagged type
7030 -- can have defaulted discriminants, but 'Constrained is required
7031 -- to return True, so the formal is never needed (see AI05-0214).
7032 -- Note that this ensures consistency of calling sequences for
7033 -- dispatching operations when some types in a class have defaults
7034 -- on discriminants and others do not (and requiring the extra
7035 -- formal would introduce distributed overhead).
7037 -- If the type does not have a completion yet, treat as prior to
7038 -- Ada 2012 for consistency.
7040 if Has_Discriminants (Formal_Type)
7041 and then not Is_Constrained (Formal_Type)
7042 and then not Is_Indefinite_Subtype (Formal_Type)
7043 and then (Ada_Version < Ada_2012
7044 or else No (Underlying_Type (Formal_Type))
7045 or else not
7046 (Is_Limited_Type (Formal_Type)
7047 and then
7048 (Is_Tagged_Type
7049 (Underlying_Type (Formal_Type)))))
7050 then
7051 Set_Extra_Constrained
7052 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7053 end if;
7054 end if;
7056 -- Create extra formal for supporting accessibility checking. This
7057 -- is done for both anonymous access formals and formals of named
7058 -- access types that are marked as controlling formals. The latter
7059 -- case can occur when Expand_Dispatching_Call creates a subprogram
7060 -- type and substitutes the types of access-to-class-wide actuals
7061 -- for the anonymous access-to-specific-type of controlling formals.
7062 -- Base_Type is applied because in cases where there is a null
7063 -- exclusion the formal may have an access subtype.
7065 -- This is suppressed if we specifically suppress accessibility
7066 -- checks at the package level for either the subprogram, or the
7067 -- package in which it resides. However, we do not suppress it
7068 -- simply if the scope has accessibility checks suppressed, since
7069 -- this could cause trouble when clients are compiled with a
7070 -- different suppression setting. The explicit checks at the
7071 -- package level are safe from this point of view.
7073 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7074 or else (Is_Controlling_Formal (Formal)
7075 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7076 and then not
7077 (Explicit_Suppress (E, Accessibility_Check)
7078 or else
7079 Explicit_Suppress (Scope (E), Accessibility_Check))
7080 and then
7081 (No (P_Formal)
7082 or else Present (Extra_Accessibility (P_Formal)))
7083 then
7084 Set_Extra_Accessibility
7085 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7086 end if;
7088 -- This label is required when skipping extra formal generation for
7089 -- Unchecked_Union parameters.
7091 <<Skip_Extra_Formal_Generation>>
7093 if Present (P_Formal) then
7094 Next_Formal (P_Formal);
7095 end if;
7097 Next_Formal (Formal);
7098 end loop;
7100 <<Test_For_Func_Result_Extras>>
7102 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7103 -- function call is ... determined by the point of call ...".
7105 if Needs_Result_Accessibility_Level (E) then
7106 Set_Extra_Accessibility_Of_Result
7107 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7108 end if;
7110 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7111 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7113 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7114 declare
7115 Result_Subt : constant Entity_Id := Etype (E);
7116 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7117 Formal_Typ : Entity_Id;
7119 Discard : Entity_Id;
7120 pragma Warnings (Off, Discard);
7122 begin
7123 -- In the case of functions with unconstrained result subtypes,
7124 -- add a 4-state formal indicating whether the return object is
7125 -- allocated by the caller (1), or should be allocated by the
7126 -- callee on the secondary stack (2), in the global heap (3), or
7127 -- in a user-defined storage pool (4). For the moment we just use
7128 -- Natural for the type of this formal. Note that this formal
7129 -- isn't usually needed in the case where the result subtype is
7130 -- constrained, but it is needed when the function has a tagged
7131 -- result, because generally such functions can be called in a
7132 -- dispatching context and such calls must be handled like calls
7133 -- to a class-wide function.
7135 if Needs_BIP_Alloc_Form (E) then
7136 Discard :=
7137 Add_Extra_Formal
7138 (E, Standard_Natural,
7139 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7141 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7142 -- use a user-defined pool. This formal is not added on
7143 -- .NET/JVM/ZFP as those targets do not support pools.
7145 if VM_Target = No_VM
7146 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
7147 then
7148 Discard :=
7149 Add_Extra_Formal
7150 (E, RTE (RE_Root_Storage_Pool_Ptr),
7151 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7152 end if;
7153 end if;
7155 -- In the case of functions whose result type needs finalization,
7156 -- add an extra formal which represents the finalization master.
7158 if Needs_BIP_Finalization_Master (E) then
7159 Discard :=
7160 Add_Extra_Formal
7161 (E, RTE (RE_Finalization_Master_Ptr),
7162 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7163 end if;
7165 -- When the result type contains tasks, add two extra formals: the
7166 -- master of the tasks to be created, and the caller's activation
7167 -- chain.
7169 if Has_Task (Full_Subt) then
7170 Discard :=
7171 Add_Extra_Formal
7172 (E, RTE (RE_Master_Id),
7173 E, BIP_Formal_Suffix (BIP_Task_Master));
7174 Discard :=
7175 Add_Extra_Formal
7176 (E, RTE (RE_Activation_Chain_Access),
7177 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7178 end if;
7180 -- All build-in-place functions get an extra formal that will be
7181 -- passed the address of the return object within the caller.
7183 Formal_Typ :=
7184 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7186 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7187 Set_Etype (Formal_Typ, Formal_Typ);
7188 Set_Depends_On_Private
7189 (Formal_Typ, Has_Private_Component (Formal_Typ));
7190 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7191 Set_Is_Access_Constant (Formal_Typ, False);
7193 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7194 -- the designated type comes from the limited view (for back-end
7195 -- purposes).
7197 Set_From_Limited_With
7198 (Formal_Typ, From_Limited_With (Result_Subt));
7200 Layout_Type (Formal_Typ);
7202 Discard :=
7203 Add_Extra_Formal
7204 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7205 end;
7206 end if;
7207 end Create_Extra_Formals;
7209 -----------------------------
7210 -- Enter_Overloaded_Entity --
7211 -----------------------------
7213 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7214 E : Entity_Id := Current_Entity_In_Scope (S);
7215 C_E : Entity_Id := Current_Entity (S);
7217 begin
7218 if Present (E) then
7219 Set_Has_Homonym (E);
7220 Set_Has_Homonym (S);
7221 end if;
7223 Set_Is_Immediately_Visible (S);
7224 Set_Scope (S, Current_Scope);
7226 -- Chain new entity if front of homonym in current scope, so that
7227 -- homonyms are contiguous.
7229 if Present (E) and then E /= C_E then
7230 while Homonym (C_E) /= E loop
7231 C_E := Homonym (C_E);
7232 end loop;
7234 Set_Homonym (C_E, S);
7236 else
7237 E := C_E;
7238 Set_Current_Entity (S);
7239 end if;
7241 Set_Homonym (S, E);
7243 if Is_Inherited_Operation (S) then
7244 Append_Inherited_Subprogram (S);
7245 else
7246 Append_Entity (S, Current_Scope);
7247 end if;
7249 Set_Public_Status (S);
7251 if Debug_Flag_E then
7252 Write_Str ("New overloaded entity chain: ");
7253 Write_Name (Chars (S));
7255 E := S;
7256 while Present (E) loop
7257 Write_Str (" "); Write_Int (Int (E));
7258 E := Homonym (E);
7259 end loop;
7261 Write_Eol;
7262 end if;
7264 -- Generate warning for hiding
7266 if Warn_On_Hiding
7267 and then Comes_From_Source (S)
7268 and then In_Extended_Main_Source_Unit (S)
7269 then
7270 E := S;
7271 loop
7272 E := Homonym (E);
7273 exit when No (E);
7275 -- Warn unless genuine overloading. Do not emit warning on
7276 -- hiding predefined operators in Standard (these are either an
7277 -- (artifact of our implicit declarations, or simple noise) but
7278 -- keep warning on a operator defined on a local subtype, because
7279 -- of the real danger that different operators may be applied in
7280 -- various parts of the program.
7282 -- Note that if E and S have the same scope, there is never any
7283 -- hiding. Either the two conflict, and the program is illegal,
7284 -- or S is overriding an implicit inherited subprogram.
7286 if Scope (E) /= Scope (S)
7287 and then (not Is_Overloadable (E)
7288 or else Subtype_Conformant (E, S))
7289 and then (Is_Immediately_Visible (E)
7290 or else
7291 Is_Potentially_Use_Visible (S))
7292 then
7293 if Scope (E) /= Standard_Standard then
7294 Error_Msg_Sloc := Sloc (E);
7295 Error_Msg_N ("declaration of & hides one #?h?", S);
7297 elsif Nkind (S) = N_Defining_Operator_Symbol
7298 and then
7299 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7300 then
7301 Error_Msg_N
7302 ("declaration of & hides predefined operator?h?", S);
7303 end if;
7304 end if;
7305 end loop;
7306 end if;
7307 end Enter_Overloaded_Entity;
7309 -----------------------------
7310 -- Check_Untagged_Equality --
7311 -----------------------------
7313 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7314 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7315 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7316 Obj_Decl : Node_Id;
7318 begin
7319 -- This check applies only if we have a subprogram declaration with an
7320 -- untagged record type.
7322 if Nkind (Decl) /= N_Subprogram_Declaration
7323 or else not Is_Record_Type (Typ)
7324 or else Is_Tagged_Type (Typ)
7325 then
7326 return;
7327 end if;
7329 -- In Ada 2012 case, we will output errors or warnings depending on
7330 -- the setting of debug flag -gnatd.E.
7332 if Ada_Version >= Ada_2012 then
7333 Error_Msg_Warn := Debug_Flag_Dot_EE;
7335 -- In earlier versions of Ada, nothing to do unless we are warning on
7336 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7338 else
7339 if not Warn_On_Ada_2012_Compatibility then
7340 return;
7341 end if;
7342 end if;
7344 -- Cases where the type has already been frozen
7346 if Is_Frozen (Typ) then
7348 -- If the type is not declared in a package, or if we are in the body
7349 -- of the package or in some other scope, the new operation is not
7350 -- primitive, and therefore legal, though suspicious. Should we
7351 -- generate a warning in this case ???
7353 if Ekind (Scope (Typ)) /= E_Package
7354 or else Scope (Typ) /= Current_Scope
7355 then
7356 return;
7358 -- If the type is a generic actual (sub)type, the operation is not
7359 -- primitive either because the base type is declared elsewhere.
7361 elsif Is_Generic_Actual_Type (Typ) then
7362 return;
7364 -- Here we have a definite error of declaration after freezing
7366 else
7367 if Ada_Version >= Ada_2012 then
7368 Error_Msg_NE
7369 ("equality operator must be declared before type & is "
7370 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7372 -- In Ada 2012 mode with error turned to warning, output one
7373 -- more warning to warn that the equality operation may not
7374 -- compose. This is the consequence of ignoring the error.
7376 if Error_Msg_Warn then
7377 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7378 end if;
7380 else
7381 Error_Msg_NE
7382 ("equality operator must be declared before type& is "
7383 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7384 end if;
7386 -- If we are in the package body, we could just move the
7387 -- declaration to the package spec, so add a message saying that.
7389 if In_Package_Body (Scope (Typ)) then
7390 if Ada_Version >= Ada_2012 then
7391 Error_Msg_N
7392 ("\move declaration to package spec<<", Eq_Op);
7393 else
7394 Error_Msg_N
7395 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7396 end if;
7398 -- Otherwise try to find the freezing point
7400 else
7401 Obj_Decl := Next (Parent (Typ));
7402 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7403 if Nkind (Obj_Decl) = N_Object_Declaration
7404 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7405 then
7406 -- Freezing point, output warnings
7408 if Ada_Version >= Ada_2012 then
7409 Error_Msg_NE
7410 ("type& is frozen by declaration??", Obj_Decl, Typ);
7411 Error_Msg_N
7412 ("\an equality operator cannot be declared after "
7413 & "this point??",
7414 Obj_Decl);
7415 else
7416 Error_Msg_NE
7417 ("type& is frozen by declaration (Ada 2012)?y?",
7418 Obj_Decl, Typ);
7419 Error_Msg_N
7420 ("\an equality operator cannot be declared after "
7421 & "this point (Ada 2012)?y?",
7422 Obj_Decl);
7423 end if;
7425 exit;
7426 end if;
7428 Next (Obj_Decl);
7429 end loop;
7430 end if;
7431 end if;
7433 -- Here if type is not frozen yet. It is illegal to have a primitive
7434 -- equality declared in the private part if the type is visible.
7436 elsif not In_Same_List (Parent (Typ), Decl)
7437 and then not Is_Limited_Type (Typ)
7438 then
7439 -- Shouldn't we give an RM reference here???
7441 if Ada_Version >= Ada_2012 then
7442 Error_Msg_N
7443 ("equality operator appears too late<<", Eq_Op);
7444 else
7445 Error_Msg_N
7446 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7447 end if;
7449 -- No error detected
7451 else
7452 return;
7453 end if;
7454 end Check_Untagged_Equality;
7456 -----------------------------
7457 -- Find_Corresponding_Spec --
7458 -----------------------------
7460 function Find_Corresponding_Spec
7461 (N : Node_Id;
7462 Post_Error : Boolean := True) return Entity_Id
7464 Spec : constant Node_Id := Specification (N);
7465 Designator : constant Entity_Id := Defining_Entity (Spec);
7467 E : Entity_Id;
7469 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7470 -- Even if fully conformant, a body may depend on a generic actual when
7471 -- the spec does not, or vice versa, in which case they were distinct
7472 -- entities in the generic.
7474 -------------------------------
7475 -- Different_Generic_Profile --
7476 -------------------------------
7478 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7479 F1, F2 : Entity_Id;
7481 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7482 -- Check that the types of corresponding formals have the same
7483 -- generic actual if any. We have to account for subtypes of a
7484 -- generic formal, declared between a spec and a body, which may
7485 -- appear distinct in an instance but matched in the generic, and
7486 -- the subtype may be used either in the spec or the body of the
7487 -- subprogram being checked.
7489 -------------------------
7490 -- Same_Generic_Actual --
7491 -------------------------
7493 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7495 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7496 -- Predicate to check whether S1 is a subtype of S2 in the source
7497 -- of the instance.
7499 -------------------------
7500 -- Is_Declared_Subtype --
7501 -------------------------
7503 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7504 begin
7505 return Comes_From_Source (Parent (S1))
7506 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7507 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7508 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7509 end Is_Declared_Subtype;
7511 -- Start of processing for Same_Generic_Actual
7513 begin
7514 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7515 or else Is_Declared_Subtype (T1, T2)
7516 or else Is_Declared_Subtype (T2, T1);
7517 end Same_Generic_Actual;
7519 -- Start of processing for Different_Generic_Profile
7521 begin
7522 if not In_Instance then
7523 return False;
7525 elsif Ekind (E) = E_Function
7526 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7527 then
7528 return True;
7529 end if;
7531 F1 := First_Formal (Designator);
7532 F2 := First_Formal (E);
7533 while Present (F1) loop
7534 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7535 return True;
7536 end if;
7538 Next_Formal (F1);
7539 Next_Formal (F2);
7540 end loop;
7542 return False;
7543 end Different_Generic_Profile;
7545 -- Start of processing for Find_Corresponding_Spec
7547 begin
7548 E := Current_Entity (Designator);
7549 while Present (E) loop
7551 -- We are looking for a matching spec. It must have the same scope,
7552 -- and the same name, and either be type conformant, or be the case
7553 -- of a library procedure spec and its body (which belong to one
7554 -- another regardless of whether they are type conformant or not).
7556 if Scope (E) = Current_Scope then
7557 if Current_Scope = Standard_Standard
7558 or else (Ekind (E) = Ekind (Designator)
7559 and then Type_Conformant (E, Designator))
7560 then
7561 -- Within an instantiation, we know that spec and body are
7562 -- subtype conformant, because they were subtype conformant in
7563 -- the generic. We choose the subtype-conformant entity here as
7564 -- well, to resolve spurious ambiguities in the instance that
7565 -- were not present in the generic (i.e. when two different
7566 -- types are given the same actual). If we are looking for a
7567 -- spec to match a body, full conformance is expected.
7569 if In_Instance then
7571 -- Inherit the convention and "ghostness" of the matching
7572 -- spec to ensure proper full and subtype conformance.
7574 Set_Convention (Designator, Convention (E));
7576 if Is_Ghost_Entity (E) then
7577 Set_Is_Ghost_Entity (Designator);
7578 end if;
7580 -- Skip past subprogram bodies and subprogram renamings that
7581 -- may appear to have a matching spec, but that aren't fully
7582 -- conformant with it. That can occur in cases where an
7583 -- actual type causes unrelated homographs in the instance.
7585 if Nkind_In (N, N_Subprogram_Body,
7586 N_Subprogram_Renaming_Declaration)
7587 and then Present (Homonym (E))
7588 and then not Fully_Conformant (Designator, E)
7589 then
7590 goto Next_Entity;
7592 elsif not Subtype_Conformant (Designator, E) then
7593 goto Next_Entity;
7595 elsif Different_Generic_Profile (E) then
7596 goto Next_Entity;
7597 end if;
7598 end if;
7600 -- Ada 2012 (AI05-0165): For internally generated bodies of
7601 -- null procedures locate the internally generated spec. We
7602 -- enforce mode conformance since a tagged type may inherit
7603 -- from interfaces several null primitives which differ only
7604 -- in the mode of the formals.
7606 if not (Comes_From_Source (E))
7607 and then Is_Null_Procedure (E)
7608 and then not Mode_Conformant (Designator, E)
7609 then
7610 null;
7612 -- For null procedures coming from source that are completions,
7613 -- analysis of the generated body will establish the link.
7615 elsif Comes_From_Source (E)
7616 and then Nkind (Spec) = N_Procedure_Specification
7617 and then Null_Present (Spec)
7618 then
7619 return E;
7621 elsif not Has_Completion (E) then
7622 if Nkind (N) /= N_Subprogram_Body_Stub then
7623 Set_Corresponding_Spec (N, E);
7624 end if;
7626 Set_Has_Completion (E);
7627 return E;
7629 elsif Nkind (Parent (N)) = N_Subunit then
7631 -- If this is the proper body of a subunit, the completion
7632 -- flag is set when analyzing the stub.
7634 return E;
7636 -- If E is an internal function with a controlling result that
7637 -- was created for an operation inherited by a null extension,
7638 -- it may be overridden by a body without a previous spec (one
7639 -- more reason why these should be shunned). In that case we
7640 -- remove the generated body if present, because the current
7641 -- one is the explicit overriding.
7643 elsif Ekind (E) = E_Function
7644 and then Ada_Version >= Ada_2005
7645 and then not Comes_From_Source (E)
7646 and then Has_Controlling_Result (E)
7647 and then Is_Null_Extension (Etype (E))
7648 and then Comes_From_Source (Spec)
7649 then
7650 Set_Has_Completion (E, False);
7652 if Expander_Active
7653 and then Nkind (Parent (E)) = N_Function_Specification
7654 then
7655 Remove
7656 (Unit_Declaration_Node
7657 (Corresponding_Body (Unit_Declaration_Node (E))));
7659 return E;
7661 -- If expansion is disabled, or if the wrapper function has
7662 -- not been generated yet, this a late body overriding an
7663 -- inherited operation, or it is an overriding by some other
7664 -- declaration before the controlling result is frozen. In
7665 -- either case this is a declaration of a new entity.
7667 else
7668 return Empty;
7669 end if;
7671 -- If the body already exists, then this is an error unless
7672 -- the previous declaration is the implicit declaration of a
7673 -- derived subprogram. It is also legal for an instance to
7674 -- contain type conformant overloadable declarations (but the
7675 -- generic declaration may not), per 8.3(26/2).
7677 elsif No (Alias (E))
7678 and then not Is_Intrinsic_Subprogram (E)
7679 and then not In_Instance
7680 and then Post_Error
7681 then
7682 Error_Msg_Sloc := Sloc (E);
7684 if Is_Imported (E) then
7685 Error_Msg_NE
7686 ("body not allowed for imported subprogram & declared#",
7687 N, E);
7688 else
7689 Error_Msg_NE ("duplicate body for & declared#", N, E);
7690 end if;
7691 end if;
7693 -- Child units cannot be overloaded, so a conformance mismatch
7694 -- between body and a previous spec is an error.
7696 elsif Is_Child_Unit (E)
7697 and then
7698 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7699 and then
7700 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7701 N_Compilation_Unit
7702 and then Post_Error
7703 then
7704 Error_Msg_N
7705 ("body of child unit does not match previous declaration", N);
7706 end if;
7707 end if;
7709 <<Next_Entity>>
7710 E := Homonym (E);
7711 end loop;
7713 -- On exit, we know that no previous declaration of subprogram exists
7715 return Empty;
7716 end Find_Corresponding_Spec;
7718 ----------------------
7719 -- Fully_Conformant --
7720 ----------------------
7722 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7723 Result : Boolean;
7724 begin
7725 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7726 return Result;
7727 end Fully_Conformant;
7729 ----------------------------------
7730 -- Fully_Conformant_Expressions --
7731 ----------------------------------
7733 function Fully_Conformant_Expressions
7734 (Given_E1 : Node_Id;
7735 Given_E2 : Node_Id) return Boolean
7737 E1 : constant Node_Id := Original_Node (Given_E1);
7738 E2 : constant Node_Id := Original_Node (Given_E2);
7739 -- We always test conformance on original nodes, since it is possible
7740 -- for analysis and/or expansion to make things look as though they
7741 -- conform when they do not, e.g. by converting 1+2 into 3.
7743 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7744 renames Fully_Conformant_Expressions;
7746 function FCL (L1, L2 : List_Id) return Boolean;
7747 -- Compare elements of two lists for conformance. Elements have to be
7748 -- conformant, and actuals inserted as default parameters do not match
7749 -- explicit actuals with the same value.
7751 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7752 -- Compare an operator node with a function call
7754 ---------
7755 -- FCL --
7756 ---------
7758 function FCL (L1, L2 : List_Id) return Boolean is
7759 N1, N2 : Node_Id;
7761 begin
7762 if L1 = No_List then
7763 N1 := Empty;
7764 else
7765 N1 := First (L1);
7766 end if;
7768 if L2 = No_List then
7769 N2 := Empty;
7770 else
7771 N2 := First (L2);
7772 end if;
7774 -- Compare two lists, skipping rewrite insertions (we want to compare
7775 -- the original trees, not the expanded versions).
7777 loop
7778 if Is_Rewrite_Insertion (N1) then
7779 Next (N1);
7780 elsif Is_Rewrite_Insertion (N2) then
7781 Next (N2);
7782 elsif No (N1) then
7783 return No (N2);
7784 elsif No (N2) then
7785 return False;
7786 elsif not FCE (N1, N2) then
7787 return False;
7788 else
7789 Next (N1);
7790 Next (N2);
7791 end if;
7792 end loop;
7793 end FCL;
7795 ---------
7796 -- FCO --
7797 ---------
7799 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7800 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7801 Act : Node_Id;
7803 begin
7804 if No (Actuals)
7805 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7806 then
7807 return False;
7809 else
7810 Act := First (Actuals);
7812 if Nkind (Op_Node) in N_Binary_Op then
7813 if not FCE (Left_Opnd (Op_Node), Act) then
7814 return False;
7815 end if;
7817 Next (Act);
7818 end if;
7820 return Present (Act)
7821 and then FCE (Right_Opnd (Op_Node), Act)
7822 and then No (Next (Act));
7823 end if;
7824 end FCO;
7826 -- Start of processing for Fully_Conformant_Expressions
7828 begin
7829 -- Non-conformant if paren count does not match. Note: if some idiot
7830 -- complains that we don't do this right for more than 3 levels of
7831 -- parentheses, they will be treated with the respect they deserve.
7833 if Paren_Count (E1) /= Paren_Count (E2) then
7834 return False;
7836 -- If same entities are referenced, then they are conformant even if
7837 -- they have different forms (RM 8.3.1(19-20)).
7839 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7840 if Present (Entity (E1)) then
7841 return Entity (E1) = Entity (E2)
7842 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7843 and then Ekind (Entity (E1)) = E_Discriminant
7844 and then Ekind (Entity (E2)) = E_In_Parameter);
7846 elsif Nkind (E1) = N_Expanded_Name
7847 and then Nkind (E2) = N_Expanded_Name
7848 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7849 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7850 then
7851 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7853 else
7854 -- Identifiers in component associations don't always have
7855 -- entities, but their names must conform.
7857 return Nkind (E1) = N_Identifier
7858 and then Nkind (E2) = N_Identifier
7859 and then Chars (E1) = Chars (E2);
7860 end if;
7862 elsif Nkind (E1) = N_Character_Literal
7863 and then Nkind (E2) = N_Expanded_Name
7864 then
7865 return Nkind (Selector_Name (E2)) = N_Character_Literal
7866 and then Chars (E1) = Chars (Selector_Name (E2));
7868 elsif Nkind (E2) = N_Character_Literal
7869 and then Nkind (E1) = N_Expanded_Name
7870 then
7871 return Nkind (Selector_Name (E1)) = N_Character_Literal
7872 and then Chars (E2) = Chars (Selector_Name (E1));
7874 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7875 return FCO (E1, E2);
7877 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7878 return FCO (E2, E1);
7880 -- Otherwise we must have the same syntactic entity
7882 elsif Nkind (E1) /= Nkind (E2) then
7883 return False;
7885 -- At this point, we specialize by node type
7887 else
7888 case Nkind (E1) is
7890 when N_Aggregate =>
7891 return
7892 FCL (Expressions (E1), Expressions (E2))
7893 and then
7894 FCL (Component_Associations (E1),
7895 Component_Associations (E2));
7897 when N_Allocator =>
7898 if Nkind (Expression (E1)) = N_Qualified_Expression
7899 or else
7900 Nkind (Expression (E2)) = N_Qualified_Expression
7901 then
7902 return FCE (Expression (E1), Expression (E2));
7904 -- Check that the subtype marks and any constraints
7905 -- are conformant
7907 else
7908 declare
7909 Indic1 : constant Node_Id := Expression (E1);
7910 Indic2 : constant Node_Id := Expression (E2);
7911 Elt1 : Node_Id;
7912 Elt2 : Node_Id;
7914 begin
7915 if Nkind (Indic1) /= N_Subtype_Indication then
7916 return
7917 Nkind (Indic2) /= N_Subtype_Indication
7918 and then Entity (Indic1) = Entity (Indic2);
7920 elsif Nkind (Indic2) /= N_Subtype_Indication then
7921 return
7922 Nkind (Indic1) /= N_Subtype_Indication
7923 and then Entity (Indic1) = Entity (Indic2);
7925 else
7926 if Entity (Subtype_Mark (Indic1)) /=
7927 Entity (Subtype_Mark (Indic2))
7928 then
7929 return False;
7930 end if;
7932 Elt1 := First (Constraints (Constraint (Indic1)));
7933 Elt2 := First (Constraints (Constraint (Indic2)));
7934 while Present (Elt1) and then Present (Elt2) loop
7935 if not FCE (Elt1, Elt2) then
7936 return False;
7937 end if;
7939 Next (Elt1);
7940 Next (Elt2);
7941 end loop;
7943 return True;
7944 end if;
7945 end;
7946 end if;
7948 when N_Attribute_Reference =>
7949 return
7950 Attribute_Name (E1) = Attribute_Name (E2)
7951 and then FCL (Expressions (E1), Expressions (E2));
7953 when N_Binary_Op =>
7954 return
7955 Entity (E1) = Entity (E2)
7956 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7957 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7959 when N_Short_Circuit | N_Membership_Test =>
7960 return
7961 FCE (Left_Opnd (E1), Left_Opnd (E2))
7962 and then
7963 FCE (Right_Opnd (E1), Right_Opnd (E2));
7965 when N_Case_Expression =>
7966 declare
7967 Alt1 : Node_Id;
7968 Alt2 : Node_Id;
7970 begin
7971 if not FCE (Expression (E1), Expression (E2)) then
7972 return False;
7974 else
7975 Alt1 := First (Alternatives (E1));
7976 Alt2 := First (Alternatives (E2));
7977 loop
7978 if Present (Alt1) /= Present (Alt2) then
7979 return False;
7980 elsif No (Alt1) then
7981 return True;
7982 end if;
7984 if not FCE (Expression (Alt1), Expression (Alt2))
7985 or else not FCL (Discrete_Choices (Alt1),
7986 Discrete_Choices (Alt2))
7987 then
7988 return False;
7989 end if;
7991 Next (Alt1);
7992 Next (Alt2);
7993 end loop;
7994 end if;
7995 end;
7997 when N_Character_Literal =>
7998 return
7999 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8001 when N_Component_Association =>
8002 return
8003 FCL (Choices (E1), Choices (E2))
8004 and then
8005 FCE (Expression (E1), Expression (E2));
8007 when N_Explicit_Dereference =>
8008 return
8009 FCE (Prefix (E1), Prefix (E2));
8011 when N_Extension_Aggregate =>
8012 return
8013 FCL (Expressions (E1), Expressions (E2))
8014 and then Null_Record_Present (E1) =
8015 Null_Record_Present (E2)
8016 and then FCL (Component_Associations (E1),
8017 Component_Associations (E2));
8019 when N_Function_Call =>
8020 return
8021 FCE (Name (E1), Name (E2))
8022 and then
8023 FCL (Parameter_Associations (E1),
8024 Parameter_Associations (E2));
8026 when N_If_Expression =>
8027 return
8028 FCL (Expressions (E1), Expressions (E2));
8030 when N_Indexed_Component =>
8031 return
8032 FCE (Prefix (E1), Prefix (E2))
8033 and then
8034 FCL (Expressions (E1), Expressions (E2));
8036 when N_Integer_Literal =>
8037 return (Intval (E1) = Intval (E2));
8039 when N_Null =>
8040 return True;
8042 when N_Operator_Symbol =>
8043 return
8044 Chars (E1) = Chars (E2);
8046 when N_Others_Choice =>
8047 return True;
8049 when N_Parameter_Association =>
8050 return
8051 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8052 and then FCE (Explicit_Actual_Parameter (E1),
8053 Explicit_Actual_Parameter (E2));
8055 when N_Qualified_Expression =>
8056 return
8057 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8058 and then
8059 FCE (Expression (E1), Expression (E2));
8061 when N_Quantified_Expression =>
8062 if not FCE (Condition (E1), Condition (E2)) then
8063 return False;
8064 end if;
8066 if Present (Loop_Parameter_Specification (E1))
8067 and then Present (Loop_Parameter_Specification (E2))
8068 then
8069 declare
8070 L1 : constant Node_Id :=
8071 Loop_Parameter_Specification (E1);
8072 L2 : constant Node_Id :=
8073 Loop_Parameter_Specification (E2);
8075 begin
8076 return
8077 Reverse_Present (L1) = Reverse_Present (L2)
8078 and then
8079 FCE (Defining_Identifier (L1),
8080 Defining_Identifier (L2))
8081 and then
8082 FCE (Discrete_Subtype_Definition (L1),
8083 Discrete_Subtype_Definition (L2));
8084 end;
8086 elsif Present (Iterator_Specification (E1))
8087 and then Present (Iterator_Specification (E2))
8088 then
8089 declare
8090 I1 : constant Node_Id := Iterator_Specification (E1);
8091 I2 : constant Node_Id := Iterator_Specification (E2);
8093 begin
8094 return
8095 FCE (Defining_Identifier (I1),
8096 Defining_Identifier (I2))
8097 and then
8098 Of_Present (I1) = Of_Present (I2)
8099 and then
8100 Reverse_Present (I1) = Reverse_Present (I2)
8101 and then FCE (Name (I1), Name (I2))
8102 and then FCE (Subtype_Indication (I1),
8103 Subtype_Indication (I2));
8104 end;
8106 -- The quantified expressions used different specifications to
8107 -- walk their respective ranges.
8109 else
8110 return False;
8111 end if;
8113 when N_Range =>
8114 return
8115 FCE (Low_Bound (E1), Low_Bound (E2))
8116 and then
8117 FCE (High_Bound (E1), High_Bound (E2));
8119 when N_Real_Literal =>
8120 return (Realval (E1) = Realval (E2));
8122 when N_Selected_Component =>
8123 return
8124 FCE (Prefix (E1), Prefix (E2))
8125 and then
8126 FCE (Selector_Name (E1), Selector_Name (E2));
8128 when N_Slice =>
8129 return
8130 FCE (Prefix (E1), Prefix (E2))
8131 and then
8132 FCE (Discrete_Range (E1), Discrete_Range (E2));
8134 when N_String_Literal =>
8135 declare
8136 S1 : constant String_Id := Strval (E1);
8137 S2 : constant String_Id := Strval (E2);
8138 L1 : constant Nat := String_Length (S1);
8139 L2 : constant Nat := String_Length (S2);
8141 begin
8142 if L1 /= L2 then
8143 return False;
8145 else
8146 for J in 1 .. L1 loop
8147 if Get_String_Char (S1, J) /=
8148 Get_String_Char (S2, J)
8149 then
8150 return False;
8151 end if;
8152 end loop;
8154 return True;
8155 end if;
8156 end;
8158 when N_Type_Conversion =>
8159 return
8160 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8161 and then
8162 FCE (Expression (E1), Expression (E2));
8164 when N_Unary_Op =>
8165 return
8166 Entity (E1) = Entity (E2)
8167 and then
8168 FCE (Right_Opnd (E1), Right_Opnd (E2));
8170 when N_Unchecked_Type_Conversion =>
8171 return
8172 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8173 and then
8174 FCE (Expression (E1), Expression (E2));
8176 -- All other node types cannot appear in this context. Strictly
8177 -- we should raise a fatal internal error. Instead we just ignore
8178 -- the nodes. This means that if anyone makes a mistake in the
8179 -- expander and mucks an expression tree irretrievably, the result
8180 -- will be a failure to detect a (probably very obscure) case
8181 -- of non-conformance, which is better than bombing on some
8182 -- case where two expressions do in fact conform.
8184 when others =>
8185 return True;
8187 end case;
8188 end if;
8189 end Fully_Conformant_Expressions;
8191 ----------------------------------------
8192 -- Fully_Conformant_Discrete_Subtypes --
8193 ----------------------------------------
8195 function Fully_Conformant_Discrete_Subtypes
8196 (Given_S1 : Node_Id;
8197 Given_S2 : Node_Id) return Boolean
8199 S1 : constant Node_Id := Original_Node (Given_S1);
8200 S2 : constant Node_Id := Original_Node (Given_S2);
8202 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8203 -- Special-case for a bound given by a discriminant, which in the body
8204 -- is replaced with the discriminal of the enclosing type.
8206 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8207 -- Check both bounds
8209 -----------------------
8210 -- Conforming_Bounds --
8211 -----------------------
8213 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8214 begin
8215 if Is_Entity_Name (B1)
8216 and then Is_Entity_Name (B2)
8217 and then Ekind (Entity (B1)) = E_Discriminant
8218 then
8219 return Chars (B1) = Chars (B2);
8221 else
8222 return Fully_Conformant_Expressions (B1, B2);
8223 end if;
8224 end Conforming_Bounds;
8226 -----------------------
8227 -- Conforming_Ranges --
8228 -----------------------
8230 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8231 begin
8232 return
8233 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8234 and then
8235 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8236 end Conforming_Ranges;
8238 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8240 begin
8241 if Nkind (S1) /= Nkind (S2) then
8242 return False;
8244 elsif Is_Entity_Name (S1) then
8245 return Entity (S1) = Entity (S2);
8247 elsif Nkind (S1) = N_Range then
8248 return Conforming_Ranges (S1, S2);
8250 elsif Nkind (S1) = N_Subtype_Indication then
8251 return
8252 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8253 and then
8254 Conforming_Ranges
8255 (Range_Expression (Constraint (S1)),
8256 Range_Expression (Constraint (S2)));
8257 else
8258 return True;
8259 end if;
8260 end Fully_Conformant_Discrete_Subtypes;
8262 --------------------
8263 -- Install_Entity --
8264 --------------------
8266 procedure Install_Entity (E : Entity_Id) is
8267 Prev : constant Entity_Id := Current_Entity (E);
8268 begin
8269 Set_Is_Immediately_Visible (E);
8270 Set_Current_Entity (E);
8271 Set_Homonym (E, Prev);
8272 end Install_Entity;
8274 ---------------------
8275 -- Install_Formals --
8276 ---------------------
8278 procedure Install_Formals (Id : Entity_Id) is
8279 F : Entity_Id;
8280 begin
8281 F := First_Formal (Id);
8282 while Present (F) loop
8283 Install_Entity (F);
8284 Next_Formal (F);
8285 end loop;
8286 end Install_Formals;
8288 -----------------------------
8289 -- Is_Interface_Conformant --
8290 -----------------------------
8292 function Is_Interface_Conformant
8293 (Tagged_Type : Entity_Id;
8294 Iface_Prim : Entity_Id;
8295 Prim : Entity_Id) return Boolean
8297 -- The operation may in fact be an inherited (implicit) operation
8298 -- rather than the original interface primitive, so retrieve the
8299 -- ultimate ancestor.
8301 Iface : constant Entity_Id :=
8302 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8303 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8305 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8306 -- Return the controlling formal of Prim
8308 ------------------------
8309 -- Controlling_Formal --
8310 ------------------------
8312 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8313 E : Entity_Id;
8315 begin
8316 E := First_Entity (Prim);
8317 while Present (E) loop
8318 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8319 return E;
8320 end if;
8322 Next_Entity (E);
8323 end loop;
8325 return Empty;
8326 end Controlling_Formal;
8328 -- Local variables
8330 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8331 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8333 -- Start of processing for Is_Interface_Conformant
8335 begin
8336 pragma Assert (Is_Subprogram (Iface_Prim)
8337 and then Is_Subprogram (Prim)
8338 and then Is_Dispatching_Operation (Iface_Prim)
8339 and then Is_Dispatching_Operation (Prim));
8341 pragma Assert (Is_Interface (Iface)
8342 or else (Present (Alias (Iface_Prim))
8343 and then
8344 Is_Interface
8345 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8347 if Prim = Iface_Prim
8348 or else not Is_Subprogram (Prim)
8349 or else Ekind (Prim) /= Ekind (Iface_Prim)
8350 or else not Is_Dispatching_Operation (Prim)
8351 or else Scope (Prim) /= Scope (Tagged_Type)
8352 or else No (Typ)
8353 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8354 or else not Primitive_Names_Match (Iface_Prim, Prim)
8355 then
8356 return False;
8358 -- The mode of the controlling formals must match
8360 elsif Present (Iface_Ctrl_F)
8361 and then Present (Prim_Ctrl_F)
8362 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8363 then
8364 return False;
8366 -- Case of a procedure, or a function whose result type matches the
8367 -- result type of the interface primitive, or a function that has no
8368 -- controlling result (I or access I).
8370 elsif Ekind (Iface_Prim) = E_Procedure
8371 or else Etype (Prim) = Etype (Iface_Prim)
8372 or else not Has_Controlling_Result (Prim)
8373 then
8374 return Type_Conformant
8375 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8377 -- Case of a function returning an interface, or an access to one. Check
8378 -- that the return types correspond.
8380 elsif Implements_Interface (Typ, Iface) then
8381 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8383 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8384 then
8385 return False;
8386 else
8387 return
8388 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8389 Skip_Controlling_Formals => True);
8390 end if;
8392 else
8393 return False;
8394 end if;
8395 end Is_Interface_Conformant;
8397 ---------------------------------
8398 -- Is_Non_Overriding_Operation --
8399 ---------------------------------
8401 function Is_Non_Overriding_Operation
8402 (Prev_E : Entity_Id;
8403 New_E : Entity_Id) return Boolean
8405 Formal : Entity_Id;
8406 F_Typ : Entity_Id;
8407 G_Typ : Entity_Id := Empty;
8409 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8410 -- If F_Type is a derived type associated with a generic actual subtype,
8411 -- then return its Generic_Parent_Type attribute, else return Empty.
8413 function Types_Correspond
8414 (P_Type : Entity_Id;
8415 N_Type : Entity_Id) return Boolean;
8416 -- Returns true if and only if the types (or designated types in the
8417 -- case of anonymous access types) are the same or N_Type is derived
8418 -- directly or indirectly from P_Type.
8420 -----------------------------
8421 -- Get_Generic_Parent_Type --
8422 -----------------------------
8424 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8425 G_Typ : Entity_Id;
8426 Defn : Node_Id;
8427 Indic : Node_Id;
8429 begin
8430 if Is_Derived_Type (F_Typ)
8431 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8432 then
8433 -- The tree must be traversed to determine the parent subtype in
8434 -- the generic unit, which unfortunately isn't always available
8435 -- via semantic attributes. ??? (Note: The use of Original_Node
8436 -- is needed for cases where a full derived type has been
8437 -- rewritten.)
8439 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8440 if Nkind (Defn) = N_Derived_Type_Definition then
8441 Indic := Subtype_Indication (Defn);
8443 if Nkind (Indic) = N_Subtype_Indication then
8444 G_Typ := Entity (Subtype_Mark (Indic));
8445 else
8446 G_Typ := Entity (Indic);
8447 end if;
8449 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8450 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8451 then
8452 return Generic_Parent_Type (Parent (G_Typ));
8453 end if;
8454 end if;
8455 end if;
8457 return Empty;
8458 end Get_Generic_Parent_Type;
8460 ----------------------
8461 -- Types_Correspond --
8462 ----------------------
8464 function Types_Correspond
8465 (P_Type : Entity_Id;
8466 N_Type : Entity_Id) return Boolean
8468 Prev_Type : Entity_Id := Base_Type (P_Type);
8469 New_Type : Entity_Id := Base_Type (N_Type);
8471 begin
8472 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8473 Prev_Type := Designated_Type (Prev_Type);
8474 end if;
8476 if Ekind (New_Type) = E_Anonymous_Access_Type then
8477 New_Type := Designated_Type (New_Type);
8478 end if;
8480 if Prev_Type = New_Type then
8481 return True;
8483 elsif not Is_Class_Wide_Type (New_Type) then
8484 while Etype (New_Type) /= New_Type loop
8485 New_Type := Etype (New_Type);
8486 if New_Type = Prev_Type then
8487 return True;
8488 end if;
8489 end loop;
8490 end if;
8491 return False;
8492 end Types_Correspond;
8494 -- Start of processing for Is_Non_Overriding_Operation
8496 begin
8497 -- In the case where both operations are implicit derived subprograms
8498 -- then neither overrides the other. This can only occur in certain
8499 -- obscure cases (e.g., derivation from homographs created in a generic
8500 -- instantiation).
8502 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8503 return True;
8505 elsif Ekind (Current_Scope) = E_Package
8506 and then Is_Generic_Instance (Current_Scope)
8507 and then In_Private_Part (Current_Scope)
8508 and then Comes_From_Source (New_E)
8509 then
8510 -- We examine the formals and result type of the inherited operation,
8511 -- to determine whether their type is derived from (the instance of)
8512 -- a generic type. The first such formal or result type is the one
8513 -- tested.
8515 Formal := First_Formal (Prev_E);
8516 while Present (Formal) loop
8517 F_Typ := Base_Type (Etype (Formal));
8519 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8520 F_Typ := Designated_Type (F_Typ);
8521 end if;
8523 G_Typ := Get_Generic_Parent_Type (F_Typ);
8524 exit when Present (G_Typ);
8526 Next_Formal (Formal);
8527 end loop;
8529 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8530 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8531 end if;
8533 if No (G_Typ) then
8534 return False;
8535 end if;
8537 -- If the generic type is a private type, then the original operation
8538 -- was not overriding in the generic, because there was no primitive
8539 -- operation to override.
8541 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8542 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8543 N_Formal_Private_Type_Definition
8544 then
8545 return True;
8547 -- The generic parent type is the ancestor of a formal derived
8548 -- type declaration. We need to check whether it has a primitive
8549 -- operation that should be overridden by New_E in the generic.
8551 else
8552 declare
8553 P_Formal : Entity_Id;
8554 N_Formal : Entity_Id;
8555 P_Typ : Entity_Id;
8556 N_Typ : Entity_Id;
8557 P_Prim : Entity_Id;
8558 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8560 begin
8561 while Present (Prim_Elt) loop
8562 P_Prim := Node (Prim_Elt);
8564 if Chars (P_Prim) = Chars (New_E)
8565 and then Ekind (P_Prim) = Ekind (New_E)
8566 then
8567 P_Formal := First_Formal (P_Prim);
8568 N_Formal := First_Formal (New_E);
8569 while Present (P_Formal) and then Present (N_Formal) loop
8570 P_Typ := Etype (P_Formal);
8571 N_Typ := Etype (N_Formal);
8573 if not Types_Correspond (P_Typ, N_Typ) then
8574 exit;
8575 end if;
8577 Next_Entity (P_Formal);
8578 Next_Entity (N_Formal);
8579 end loop;
8581 -- Found a matching primitive operation belonging to the
8582 -- formal ancestor type, so the new subprogram is
8583 -- overriding.
8585 if No (P_Formal)
8586 and then No (N_Formal)
8587 and then (Ekind (New_E) /= E_Function
8588 or else
8589 Types_Correspond
8590 (Etype (P_Prim), Etype (New_E)))
8591 then
8592 return False;
8593 end if;
8594 end if;
8596 Next_Elmt (Prim_Elt);
8597 end loop;
8599 -- If no match found, then the new subprogram does not override
8600 -- in the generic (nor in the instance).
8602 -- If the type in question is not abstract, and the subprogram
8603 -- is, this will be an error if the new operation is in the
8604 -- private part of the instance. Emit a warning now, which will
8605 -- make the subsequent error message easier to understand.
8607 if not Is_Abstract_Type (F_Typ)
8608 and then Is_Abstract_Subprogram (Prev_E)
8609 and then In_Private_Part (Current_Scope)
8610 then
8611 Error_Msg_Node_2 := F_Typ;
8612 Error_Msg_NE
8613 ("private operation& in generic unit does not override "
8614 & "any primitive operation of& (RM 12.3 (18))??",
8615 New_E, New_E);
8616 end if;
8618 return True;
8619 end;
8620 end if;
8621 else
8622 return False;
8623 end if;
8624 end Is_Non_Overriding_Operation;
8626 -------------------------------------
8627 -- List_Inherited_Pre_Post_Aspects --
8628 -------------------------------------
8630 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8631 begin
8632 if Opt.List_Inherited_Aspects
8633 and then Is_Subprogram_Or_Generic_Subprogram (E)
8634 then
8635 declare
8636 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8637 Items : Node_Id;
8638 Prag : Node_Id;
8640 begin
8641 for Index in Subps'Range loop
8642 Items := Contract (Subps (Index));
8644 if Present (Items) then
8645 Prag := Pre_Post_Conditions (Items);
8646 while Present (Prag) loop
8647 Error_Msg_Sloc := Sloc (Prag);
8649 if Class_Present (Prag)
8650 and then not Split_PPC (Prag)
8651 then
8652 if Pragma_Name (Prag) = Name_Precondition then
8653 Error_Msg_N
8654 ("info: & inherits `Pre''Class` aspect from "
8655 & "#?L?", E);
8656 else
8657 Error_Msg_N
8658 ("info: & inherits `Post''Class` aspect from "
8659 & "#?L?", E);
8660 end if;
8661 end if;
8663 Prag := Next_Pragma (Prag);
8664 end loop;
8665 end if;
8666 end loop;
8667 end;
8668 end if;
8669 end List_Inherited_Pre_Post_Aspects;
8671 ------------------------------
8672 -- Make_Inequality_Operator --
8673 ------------------------------
8675 -- S is the defining identifier of an equality operator. We build a
8676 -- subprogram declaration with the right signature. This operation is
8677 -- intrinsic, because it is always expanded as the negation of the
8678 -- call to the equality function.
8680 procedure Make_Inequality_Operator (S : Entity_Id) is
8681 Loc : constant Source_Ptr := Sloc (S);
8682 Decl : Node_Id;
8683 Formals : List_Id;
8684 Op_Name : Entity_Id;
8686 FF : constant Entity_Id := First_Formal (S);
8687 NF : constant Entity_Id := Next_Formal (FF);
8689 begin
8690 -- Check that equality was properly defined, ignore call if not
8692 if No (NF) then
8693 return;
8694 end if;
8696 declare
8697 A : constant Entity_Id :=
8698 Make_Defining_Identifier (Sloc (FF),
8699 Chars => Chars (FF));
8701 B : constant Entity_Id :=
8702 Make_Defining_Identifier (Sloc (NF),
8703 Chars => Chars (NF));
8705 begin
8706 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8708 Formals := New_List (
8709 Make_Parameter_Specification (Loc,
8710 Defining_Identifier => A,
8711 Parameter_Type =>
8712 New_Occurrence_Of (Etype (First_Formal (S)),
8713 Sloc (Etype (First_Formal (S))))),
8715 Make_Parameter_Specification (Loc,
8716 Defining_Identifier => B,
8717 Parameter_Type =>
8718 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8719 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8721 Decl :=
8722 Make_Subprogram_Declaration (Loc,
8723 Specification =>
8724 Make_Function_Specification (Loc,
8725 Defining_Unit_Name => Op_Name,
8726 Parameter_Specifications => Formals,
8727 Result_Definition =>
8728 New_Occurrence_Of (Standard_Boolean, Loc)));
8730 -- Insert inequality right after equality if it is explicit or after
8731 -- the derived type when implicit. These entities are created only
8732 -- for visibility purposes, and eventually replaced in the course
8733 -- of expansion, so they do not need to be attached to the tree and
8734 -- seen by the back-end. Keeping them internal also avoids spurious
8735 -- freezing problems. The declaration is inserted in the tree for
8736 -- analysis, and removed afterwards. If the equality operator comes
8737 -- from an explicit declaration, attach the inequality immediately
8738 -- after. Else the equality is inherited from a derived type
8739 -- declaration, so insert inequality after that declaration.
8741 if No (Alias (S)) then
8742 Insert_After (Unit_Declaration_Node (S), Decl);
8743 elsif Is_List_Member (Parent (S)) then
8744 Insert_After (Parent (S), Decl);
8745 else
8746 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8747 end if;
8749 Mark_Rewrite_Insertion (Decl);
8750 Set_Is_Intrinsic_Subprogram (Op_Name);
8751 Analyze (Decl);
8752 Remove (Decl);
8753 Set_Has_Completion (Op_Name);
8754 Set_Corresponding_Equality (Op_Name, S);
8755 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8756 end;
8757 end Make_Inequality_Operator;
8759 ----------------------
8760 -- May_Need_Actuals --
8761 ----------------------
8763 procedure May_Need_Actuals (Fun : Entity_Id) is
8764 F : Entity_Id;
8765 B : Boolean;
8767 begin
8768 F := First_Formal (Fun);
8769 B := True;
8770 while Present (F) loop
8771 if No (Default_Value (F)) then
8772 B := False;
8773 exit;
8774 end if;
8776 Next_Formal (F);
8777 end loop;
8779 Set_Needs_No_Actuals (Fun, B);
8780 end May_Need_Actuals;
8782 ---------------------
8783 -- Mode_Conformant --
8784 ---------------------
8786 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8787 Result : Boolean;
8788 begin
8789 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8790 return Result;
8791 end Mode_Conformant;
8793 ---------------------------
8794 -- New_Overloaded_Entity --
8795 ---------------------------
8797 procedure New_Overloaded_Entity
8798 (S : Entity_Id;
8799 Derived_Type : Entity_Id := Empty)
8801 Overridden_Subp : Entity_Id := Empty;
8802 -- Set if the current scope has an operation that is type-conformant
8803 -- with S, and becomes hidden by S.
8805 Is_Primitive_Subp : Boolean;
8806 -- Set to True if the new subprogram is primitive
8808 E : Entity_Id;
8809 -- Entity that S overrides
8811 Prev_Vis : Entity_Id := Empty;
8812 -- Predecessor of E in Homonym chain
8814 procedure Check_For_Primitive_Subprogram
8815 (Is_Primitive : out Boolean;
8816 Is_Overriding : Boolean := False);
8817 -- If the subprogram being analyzed is a primitive operation of the type
8818 -- of a formal or result, set the Has_Primitive_Operations flag on the
8819 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8820 -- corresponding flag on the entity itself for later use.
8822 procedure Check_Synchronized_Overriding
8823 (Def_Id : Entity_Id;
8824 Overridden_Subp : out Entity_Id);
8825 -- First determine if Def_Id is an entry or a subprogram either defined
8826 -- in the scope of a task or protected type, or is a primitive of such
8827 -- a type. Check whether Def_Id overrides a subprogram of an interface
8828 -- implemented by the synchronized type, return the overridden entity
8829 -- or Empty.
8831 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8832 -- Check that E is declared in the private part of the current package,
8833 -- or in the package body, where it may hide a previous declaration.
8834 -- We can't use In_Private_Part by itself because this flag is also
8835 -- set when freezing entities, so we must examine the place of the
8836 -- declaration in the tree, and recognize wrapper packages as well.
8838 function Is_Overriding_Alias
8839 (Old_E : Entity_Id;
8840 New_E : Entity_Id) return Boolean;
8841 -- Check whether new subprogram and old subprogram are both inherited
8842 -- from subprograms that have distinct dispatch table entries. This can
8843 -- occur with derivations from instances with accidental homonyms. The
8844 -- function is conservative given that the converse is only true within
8845 -- instances that contain accidental overloadings.
8847 ------------------------------------
8848 -- Check_For_Primitive_Subprogram --
8849 ------------------------------------
8851 procedure Check_For_Primitive_Subprogram
8852 (Is_Primitive : out Boolean;
8853 Is_Overriding : Boolean := False)
8855 Formal : Entity_Id;
8856 F_Typ : Entity_Id;
8857 B_Typ : Entity_Id;
8859 function Visible_Part_Type (T : Entity_Id) return Boolean;
8860 -- Returns true if T is declared in the visible part of the current
8861 -- package scope; otherwise returns false. Assumes that T is declared
8862 -- in a package.
8864 procedure Check_Private_Overriding (T : Entity_Id);
8865 -- Checks that if a primitive abstract subprogram of a visible
8866 -- abstract type is declared in a private part, then it must override
8867 -- an abstract subprogram declared in the visible part. Also checks
8868 -- that if a primitive function with a controlling result is declared
8869 -- in a private part, then it must override a function declared in
8870 -- the visible part.
8872 ------------------------------
8873 -- Check_Private_Overriding --
8874 ------------------------------
8876 procedure Check_Private_Overriding (T : Entity_Id) is
8878 function Overrides_Visible_Function
8879 (Partial_View : Entity_Id) return Boolean;
8880 -- True if S overrides a function in the visible part. The
8881 -- overridden function could be explicitly or implicitly declared.
8883 function Overrides_Visible_Function
8884 (Partial_View : Entity_Id) return Boolean
8886 begin
8887 if not Is_Overriding or else not Has_Homonym (S) then
8888 return False;
8889 end if;
8891 if not Present (Partial_View) then
8892 return True;
8893 end if;
8895 -- Search through all the homonyms H of S in the current
8896 -- package spec, and return True if we find one that matches.
8897 -- Note that Parent (H) will be the declaration of the
8898 -- partial view of T for a match.
8900 declare
8901 H : Entity_Id := S;
8902 begin
8903 loop
8904 H := Homonym (H);
8905 exit when not Present (H) or else Scope (H) /= Scope (S);
8907 if Nkind_In
8908 (Parent (H),
8909 N_Private_Extension_Declaration,
8910 N_Private_Type_Declaration)
8911 and then Defining_Identifier (Parent (H)) = Partial_View
8912 then
8913 return True;
8914 end if;
8915 end loop;
8916 end;
8918 return False;
8919 end Overrides_Visible_Function;
8921 -- Start of processing for Check_Private_Overriding
8923 begin
8924 if Is_Package_Or_Generic_Package (Current_Scope)
8925 and then In_Private_Part (Current_Scope)
8926 and then Visible_Part_Type (T)
8927 and then not In_Instance
8928 then
8929 if Is_Abstract_Type (T)
8930 and then Is_Abstract_Subprogram (S)
8931 and then (not Is_Overriding
8932 or else not Is_Abstract_Subprogram (E))
8933 then
8934 Error_Msg_N ("abstract subprograms must be visible "
8935 & "(RM 3.9.3(10))!", S);
8937 elsif Ekind (S) = E_Function then
8938 declare
8939 Partial_View : constant Entity_Id :=
8940 Incomplete_Or_Partial_View (T);
8942 begin
8943 if not Overrides_Visible_Function (Partial_View) then
8945 -- Here, S is "function ... return T;" declared in
8946 -- the private part, not overriding some visible
8947 -- operation. That's illegal in the tagged case
8948 -- (but not if the private type is untagged).
8950 if ((Present (Partial_View)
8951 and then Is_Tagged_Type (Partial_View))
8952 or else (not Present (Partial_View)
8953 and then Is_Tagged_Type (T)))
8954 and then T = Base_Type (Etype (S))
8955 then
8956 Error_Msg_N
8957 ("private function with tagged result must"
8958 & " override visible-part function", S);
8959 Error_Msg_N
8960 ("\move subprogram to the visible part"
8961 & " (RM 3.9.3(10))", S);
8963 -- AI05-0073: extend this test to the case of a
8964 -- function with a controlling access result.
8966 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8967 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8968 and then
8969 not Is_Class_Wide_Type
8970 (Designated_Type (Etype (S)))
8971 and then Ada_Version >= Ada_2012
8972 then
8973 Error_Msg_N
8974 ("private function with controlling access "
8975 & "result must override visible-part function",
8977 Error_Msg_N
8978 ("\move subprogram to the visible part"
8979 & " (RM 3.9.3(10))", S);
8980 end if;
8981 end if;
8982 end;
8983 end if;
8984 end if;
8985 end Check_Private_Overriding;
8987 -----------------------
8988 -- Visible_Part_Type --
8989 -----------------------
8991 function Visible_Part_Type (T : Entity_Id) return Boolean is
8992 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8993 N : Node_Id;
8995 begin
8996 -- If the entity is a private type, then it must be declared in a
8997 -- visible part.
8999 if Ekind (T) in Private_Kind then
9000 return True;
9001 end if;
9003 -- Otherwise, we traverse the visible part looking for its
9004 -- corresponding declaration. We cannot use the declaration
9005 -- node directly because in the private part the entity of a
9006 -- private type is the one in the full view, which does not
9007 -- indicate that it is the completion of something visible.
9009 N := First (Visible_Declarations (Specification (P)));
9010 while Present (N) loop
9011 if Nkind (N) = N_Full_Type_Declaration
9012 and then Present (Defining_Identifier (N))
9013 and then T = Defining_Identifier (N)
9014 then
9015 return True;
9017 elsif Nkind_In (N, N_Private_Type_Declaration,
9018 N_Private_Extension_Declaration)
9019 and then Present (Defining_Identifier (N))
9020 and then T = Full_View (Defining_Identifier (N))
9021 then
9022 return True;
9023 end if;
9025 Next (N);
9026 end loop;
9028 return False;
9029 end Visible_Part_Type;
9031 -- Start of processing for Check_For_Primitive_Subprogram
9033 begin
9034 Is_Primitive := False;
9036 if not Comes_From_Source (S) then
9037 null;
9039 -- If subprogram is at library level, it is not primitive operation
9041 elsif Current_Scope = Standard_Standard then
9042 null;
9044 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9045 and then not In_Package_Body (Current_Scope))
9046 or else Is_Overriding
9047 then
9048 -- For function, check return type
9050 if Ekind (S) = E_Function then
9051 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9052 F_Typ := Designated_Type (Etype (S));
9053 else
9054 F_Typ := Etype (S);
9055 end if;
9057 B_Typ := Base_Type (F_Typ);
9059 if Scope (B_Typ) = Current_Scope
9060 and then not Is_Class_Wide_Type (B_Typ)
9061 and then not Is_Generic_Type (B_Typ)
9062 then
9063 Is_Primitive := True;
9064 Set_Has_Primitive_Operations (B_Typ);
9065 Set_Is_Primitive (S);
9066 Check_Private_Overriding (B_Typ);
9067 end if;
9068 end if;
9070 -- For all subprograms, check formals
9072 Formal := First_Formal (S);
9073 while Present (Formal) loop
9074 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9075 F_Typ := Designated_Type (Etype (Formal));
9076 else
9077 F_Typ := Etype (Formal);
9078 end if;
9080 B_Typ := Base_Type (F_Typ);
9082 if Ekind (B_Typ) = E_Access_Subtype then
9083 B_Typ := Base_Type (B_Typ);
9084 end if;
9086 if Scope (B_Typ) = Current_Scope
9087 and then not Is_Class_Wide_Type (B_Typ)
9088 and then not Is_Generic_Type (B_Typ)
9089 then
9090 Is_Primitive := True;
9091 Set_Is_Primitive (S);
9092 Set_Has_Primitive_Operations (B_Typ);
9093 Check_Private_Overriding (B_Typ);
9094 end if;
9096 Next_Formal (Formal);
9097 end loop;
9099 -- Special case: An equality function can be redefined for a type
9100 -- occurring in a declarative part, and won't otherwise be treated as
9101 -- a primitive because it doesn't occur in a package spec and doesn't
9102 -- override an inherited subprogram. It's important that we mark it
9103 -- primitive so it can be returned by Collect_Primitive_Operations
9104 -- and be used in composing the equality operation of later types
9105 -- that have a component of the type.
9107 elsif Chars (S) = Name_Op_Eq
9108 and then Etype (S) = Standard_Boolean
9109 then
9110 B_Typ := Base_Type (Etype (First_Formal (S)));
9112 if Scope (B_Typ) = Current_Scope
9113 and then
9114 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9115 and then not Is_Limited_Type (B_Typ)
9116 then
9117 Is_Primitive := True;
9118 Set_Is_Primitive (S);
9119 Set_Has_Primitive_Operations (B_Typ);
9120 Check_Private_Overriding (B_Typ);
9121 end if;
9122 end if;
9123 end Check_For_Primitive_Subprogram;
9125 -----------------------------------
9126 -- Check_Synchronized_Overriding --
9127 -----------------------------------
9129 procedure Check_Synchronized_Overriding
9130 (Def_Id : Entity_Id;
9131 Overridden_Subp : out Entity_Id)
9133 Ifaces_List : Elist_Id;
9134 In_Scope : Boolean;
9135 Typ : Entity_Id;
9137 function Matches_Prefixed_View_Profile
9138 (Prim_Params : List_Id;
9139 Iface_Params : List_Id) return Boolean;
9140 -- Determine whether a subprogram's parameter profile Prim_Params
9141 -- matches that of a potentially overridden interface subprogram
9142 -- Iface_Params. Also determine if the type of first parameter of
9143 -- Iface_Params is an implemented interface.
9145 -----------------------------------
9146 -- Matches_Prefixed_View_Profile --
9147 -----------------------------------
9149 function Matches_Prefixed_View_Profile
9150 (Prim_Params : List_Id;
9151 Iface_Params : List_Id) return Boolean
9153 Iface_Id : Entity_Id;
9154 Iface_Param : Node_Id;
9155 Iface_Typ : Entity_Id;
9156 Prim_Id : Entity_Id;
9157 Prim_Param : Node_Id;
9158 Prim_Typ : Entity_Id;
9160 function Is_Implemented
9161 (Ifaces_List : Elist_Id;
9162 Iface : Entity_Id) return Boolean;
9163 -- Determine if Iface is implemented by the current task or
9164 -- protected type.
9166 --------------------
9167 -- Is_Implemented --
9168 --------------------
9170 function Is_Implemented
9171 (Ifaces_List : Elist_Id;
9172 Iface : Entity_Id) return Boolean
9174 Iface_Elmt : Elmt_Id;
9176 begin
9177 Iface_Elmt := First_Elmt (Ifaces_List);
9178 while Present (Iface_Elmt) loop
9179 if Node (Iface_Elmt) = Iface then
9180 return True;
9181 end if;
9183 Next_Elmt (Iface_Elmt);
9184 end loop;
9186 return False;
9187 end Is_Implemented;
9189 -- Start of processing for Matches_Prefixed_View_Profile
9191 begin
9192 Iface_Param := First (Iface_Params);
9193 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9195 if Is_Access_Type (Iface_Typ) then
9196 Iface_Typ := Designated_Type (Iface_Typ);
9197 end if;
9199 Prim_Param := First (Prim_Params);
9201 -- The first parameter of the potentially overridden subprogram
9202 -- must be an interface implemented by Prim.
9204 if not Is_Interface (Iface_Typ)
9205 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9206 then
9207 return False;
9208 end if;
9210 -- The checks on the object parameters are done, move onto the
9211 -- rest of the parameters.
9213 if not In_Scope then
9214 Prim_Param := Next (Prim_Param);
9215 end if;
9217 Iface_Param := Next (Iface_Param);
9218 while Present (Iface_Param) and then Present (Prim_Param) loop
9219 Iface_Id := Defining_Identifier (Iface_Param);
9220 Iface_Typ := Find_Parameter_Type (Iface_Param);
9222 Prim_Id := Defining_Identifier (Prim_Param);
9223 Prim_Typ := Find_Parameter_Type (Prim_Param);
9225 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9226 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9227 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9228 then
9229 Iface_Typ := Designated_Type (Iface_Typ);
9230 Prim_Typ := Designated_Type (Prim_Typ);
9231 end if;
9233 -- Case of multiple interface types inside a parameter profile
9235 -- (Obj_Param : in out Iface; ...; Param : Iface)
9237 -- If the interface type is implemented, then the matching type
9238 -- in the primitive should be the implementing record type.
9240 if Ekind (Iface_Typ) = E_Record_Type
9241 and then Is_Interface (Iface_Typ)
9242 and then Is_Implemented (Ifaces_List, Iface_Typ)
9243 then
9244 if Prim_Typ /= Typ then
9245 return False;
9246 end if;
9248 -- The two parameters must be both mode and subtype conformant
9250 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9251 or else not
9252 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9253 then
9254 return False;
9255 end if;
9257 Next (Iface_Param);
9258 Next (Prim_Param);
9259 end loop;
9261 -- One of the two lists contains more parameters than the other
9263 if Present (Iface_Param) or else Present (Prim_Param) then
9264 return False;
9265 end if;
9267 return True;
9268 end Matches_Prefixed_View_Profile;
9270 -- Start of processing for Check_Synchronized_Overriding
9272 begin
9273 Overridden_Subp := Empty;
9275 -- Def_Id must be an entry or a subprogram. We should skip predefined
9276 -- primitives internally generated by the frontend; however at this
9277 -- stage predefined primitives are still not fully decorated. As a
9278 -- minor optimization we skip here internally generated subprograms.
9280 if (Ekind (Def_Id) /= E_Entry
9281 and then Ekind (Def_Id) /= E_Function
9282 and then Ekind (Def_Id) /= E_Procedure)
9283 or else not Comes_From_Source (Def_Id)
9284 then
9285 return;
9286 end if;
9288 -- Search for the concurrent declaration since it contains the list
9289 -- of all implemented interfaces. In this case, the subprogram is
9290 -- declared within the scope of a protected or a task type.
9292 if Present (Scope (Def_Id))
9293 and then Is_Concurrent_Type (Scope (Def_Id))
9294 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9295 then
9296 Typ := Scope (Def_Id);
9297 In_Scope := True;
9299 -- The enclosing scope is not a synchronized type and the subprogram
9300 -- has no formals.
9302 elsif No (First_Formal (Def_Id)) then
9303 return;
9305 -- The subprogram has formals and hence it may be a primitive of a
9306 -- concurrent type.
9308 else
9309 Typ := Etype (First_Formal (Def_Id));
9311 if Is_Access_Type (Typ) then
9312 Typ := Directly_Designated_Type (Typ);
9313 end if;
9315 if Is_Concurrent_Type (Typ)
9316 and then not Is_Generic_Actual_Type (Typ)
9317 then
9318 In_Scope := False;
9320 -- This case occurs when the concurrent type is declared within
9321 -- a generic unit. As a result the corresponding record has been
9322 -- built and used as the type of the first formal, we just have
9323 -- to retrieve the corresponding concurrent type.
9325 elsif Is_Concurrent_Record_Type (Typ)
9326 and then not Is_Class_Wide_Type (Typ)
9327 and then Present (Corresponding_Concurrent_Type (Typ))
9328 then
9329 Typ := Corresponding_Concurrent_Type (Typ);
9330 In_Scope := False;
9332 else
9333 return;
9334 end if;
9335 end if;
9337 -- There is no overriding to check if is an inherited operation in a
9338 -- type derivation on for a generic actual.
9340 Collect_Interfaces (Typ, Ifaces_List);
9342 if Is_Empty_Elmt_List (Ifaces_List) then
9343 return;
9344 end if;
9346 -- Determine whether entry or subprogram Def_Id overrides a primitive
9347 -- operation that belongs to one of the interfaces in Ifaces_List.
9349 declare
9350 Candidate : Entity_Id := Empty;
9351 Hom : Entity_Id := Empty;
9352 Subp : Entity_Id := Empty;
9354 begin
9355 -- Traverse the homonym chain, looking for a potentially
9356 -- overridden subprogram that belongs to an implemented
9357 -- interface.
9359 Hom := Current_Entity_In_Scope (Def_Id);
9360 while Present (Hom) loop
9361 Subp := Hom;
9363 if Subp = Def_Id
9364 or else not Is_Overloadable (Subp)
9365 or else not Is_Primitive (Subp)
9366 or else not Is_Dispatching_Operation (Subp)
9367 or else not Present (Find_Dispatching_Type (Subp))
9368 or else not Is_Interface (Find_Dispatching_Type (Subp))
9369 then
9370 null;
9372 -- Entries and procedures can override abstract or null
9373 -- interface procedures.
9375 elsif (Ekind (Def_Id) = E_Procedure
9376 or else Ekind (Def_Id) = E_Entry)
9377 and then Ekind (Subp) = E_Procedure
9378 and then Matches_Prefixed_View_Profile
9379 (Parameter_Specifications (Parent (Def_Id)),
9380 Parameter_Specifications (Parent (Subp)))
9381 then
9382 Candidate := Subp;
9384 -- For an overridden subprogram Subp, check whether the mode
9385 -- of its first parameter is correct depending on the kind
9386 -- of synchronized type.
9388 declare
9389 Formal : constant Node_Id := First_Formal (Candidate);
9391 begin
9392 -- In order for an entry or a protected procedure to
9393 -- override, the first parameter of the overridden
9394 -- routine must be of mode "out", "in out" or
9395 -- access-to-variable.
9397 if Ekind_In (Candidate, E_Entry, E_Procedure)
9398 and then Is_Protected_Type (Typ)
9399 and then Ekind (Formal) /= E_In_Out_Parameter
9400 and then Ekind (Formal) /= E_Out_Parameter
9401 and then Nkind (Parameter_Type (Parent (Formal))) /=
9402 N_Access_Definition
9403 then
9404 null;
9406 -- All other cases are OK since a task entry or routine
9407 -- does not have a restriction on the mode of the first
9408 -- parameter of the overridden interface routine.
9410 else
9411 Overridden_Subp := Candidate;
9412 return;
9413 end if;
9414 end;
9416 -- Functions can override abstract interface functions
9418 elsif Ekind (Def_Id) = E_Function
9419 and then Ekind (Subp) = E_Function
9420 and then Matches_Prefixed_View_Profile
9421 (Parameter_Specifications (Parent (Def_Id)),
9422 Parameter_Specifications (Parent (Subp)))
9423 and then Etype (Result_Definition (Parent (Def_Id))) =
9424 Etype (Result_Definition (Parent (Subp)))
9425 then
9426 Candidate := Subp;
9428 -- If an inherited subprogram is implemented by a protected
9429 -- function, then the first parameter of the inherited
9430 -- subprogram shall be of mode in, but not an
9431 -- access-to-variable parameter (RM 9.4(11/9)
9433 if Present (First_Formal (Subp))
9434 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9435 and then
9436 (not Is_Access_Type (Etype (First_Formal (Subp)))
9437 or else
9438 Is_Access_Constant (Etype (First_Formal (Subp))))
9439 then
9440 Overridden_Subp := Subp;
9441 return;
9442 end if;
9443 end if;
9445 Hom := Homonym (Hom);
9446 end loop;
9448 -- After examining all candidates for overriding, we are left with
9449 -- the best match which is a mode incompatible interface routine.
9451 if In_Scope and then Present (Candidate) then
9452 Error_Msg_PT (Def_Id, Candidate);
9453 end if;
9455 Overridden_Subp := Candidate;
9456 return;
9457 end;
9458 end Check_Synchronized_Overriding;
9460 ----------------------------
9461 -- Is_Private_Declaration --
9462 ----------------------------
9464 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9465 Priv_Decls : List_Id;
9466 Decl : constant Node_Id := Unit_Declaration_Node (E);
9468 begin
9469 if Is_Package_Or_Generic_Package (Current_Scope)
9470 and then In_Private_Part (Current_Scope)
9471 then
9472 Priv_Decls :=
9473 Private_Declarations (Package_Specification (Current_Scope));
9475 return In_Package_Body (Current_Scope)
9476 or else
9477 (Is_List_Member (Decl)
9478 and then List_Containing (Decl) = Priv_Decls)
9479 or else (Nkind (Parent (Decl)) = N_Package_Specification
9480 and then not
9481 Is_Compilation_Unit
9482 (Defining_Entity (Parent (Decl)))
9483 and then List_Containing (Parent (Parent (Decl))) =
9484 Priv_Decls);
9485 else
9486 return False;
9487 end if;
9488 end Is_Private_Declaration;
9490 --------------------------
9491 -- Is_Overriding_Alias --
9492 --------------------------
9494 function Is_Overriding_Alias
9495 (Old_E : Entity_Id;
9496 New_E : Entity_Id) return Boolean
9498 AO : constant Entity_Id := Alias (Old_E);
9499 AN : constant Entity_Id := Alias (New_E);
9500 begin
9501 return Scope (AO) /= Scope (AN)
9502 or else No (DTC_Entity (AO))
9503 or else No (DTC_Entity (AN))
9504 or else DT_Position (AO) = DT_Position (AN);
9505 end Is_Overriding_Alias;
9507 -- Start of processing for New_Overloaded_Entity
9509 begin
9510 -- We need to look for an entity that S may override. This must be a
9511 -- homonym in the current scope, so we look for the first homonym of
9512 -- S in the current scope as the starting point for the search.
9514 E := Current_Entity_In_Scope (S);
9516 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9517 -- They are directly added to the list of primitive operations of
9518 -- Derived_Type, unless this is a rederivation in the private part
9519 -- of an operation that was already derived in the visible part of
9520 -- the current package.
9522 if Ada_Version >= Ada_2005
9523 and then Present (Derived_Type)
9524 and then Present (Alias (S))
9525 and then Is_Dispatching_Operation (Alias (S))
9526 and then Present (Find_Dispatching_Type (Alias (S)))
9527 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9528 then
9529 -- For private types, when the full-view is processed we propagate to
9530 -- the full view the non-overridden entities whose attribute "alias"
9531 -- references an interface primitive. These entities were added by
9532 -- Derive_Subprograms to ensure that interface primitives are
9533 -- covered.
9535 -- Inside_Freeze_Actions is non zero when S corresponds with an
9536 -- internal entity that links an interface primitive with its
9537 -- covering primitive through attribute Interface_Alias (see
9538 -- Add_Internal_Interface_Entities).
9540 if Inside_Freezing_Actions = 0
9541 and then Is_Package_Or_Generic_Package (Current_Scope)
9542 and then In_Private_Part (Current_Scope)
9543 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9544 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9545 and then Full_View (Defining_Identifier (Parent (E)))
9546 = Defining_Identifier (Parent (S))
9547 and then Alias (E) = Alias (S)
9548 then
9549 Check_Operation_From_Private_View (S, E);
9550 Set_Is_Dispatching_Operation (S);
9552 -- Common case
9554 else
9555 Enter_Overloaded_Entity (S);
9556 Check_Dispatching_Operation (S, Empty);
9557 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9558 end if;
9560 return;
9561 end if;
9563 -- If there is no homonym then this is definitely not overriding
9565 if No (E) then
9566 Enter_Overloaded_Entity (S);
9567 Check_Dispatching_Operation (S, Empty);
9568 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9570 -- If subprogram has an explicit declaration, check whether it has an
9571 -- overriding indicator.
9573 if Comes_From_Source (S) then
9574 Check_Synchronized_Overriding (S, Overridden_Subp);
9576 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9577 -- it may have overridden some hidden inherited primitive. Update
9578 -- Overridden_Subp to avoid spurious errors when checking the
9579 -- overriding indicator.
9581 if Ada_Version >= Ada_2012
9582 and then No (Overridden_Subp)
9583 and then Is_Dispatching_Operation (S)
9584 and then Present (Overridden_Operation (S))
9585 then
9586 Overridden_Subp := Overridden_Operation (S);
9587 end if;
9589 Check_Overriding_Indicator
9590 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9591 end if;
9593 -- If there is a homonym that is not overloadable, then we have an
9594 -- error, except for the special cases checked explicitly below.
9596 elsif not Is_Overloadable (E) then
9598 -- Check for spurious conflict produced by a subprogram that has the
9599 -- same name as that of the enclosing generic package. The conflict
9600 -- occurs within an instance, between the subprogram and the renaming
9601 -- declaration for the package. After the subprogram, the package
9602 -- renaming declaration becomes hidden.
9604 if Ekind (E) = E_Package
9605 and then Present (Renamed_Object (E))
9606 and then Renamed_Object (E) = Current_Scope
9607 and then Nkind (Parent (Renamed_Object (E))) =
9608 N_Package_Specification
9609 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9610 then
9611 Set_Is_Hidden (E);
9612 Set_Is_Immediately_Visible (E, False);
9613 Enter_Overloaded_Entity (S);
9614 Set_Homonym (S, Homonym (E));
9615 Check_Dispatching_Operation (S, Empty);
9616 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9618 -- If the subprogram is implicit it is hidden by the previous
9619 -- declaration. However if it is dispatching, it must appear in the
9620 -- dispatch table anyway, because it can be dispatched to even if it
9621 -- cannot be called directly.
9623 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9624 Set_Scope (S, Current_Scope);
9626 if Is_Dispatching_Operation (Alias (S)) then
9627 Check_Dispatching_Operation (S, Empty);
9628 end if;
9630 return;
9632 else
9633 Error_Msg_Sloc := Sloc (E);
9635 -- Generate message, with useful additional warning if in generic
9637 if Is_Generic_Unit (E) then
9638 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9639 Error_Msg_N ("\& conflicts with declaration#", S);
9640 else
9641 Error_Msg_N ("& conflicts with declaration#", S);
9642 end if;
9644 return;
9645 end if;
9647 -- E exists and is overloadable
9649 else
9650 Check_Synchronized_Overriding (S, Overridden_Subp);
9652 -- Loop through E and its homonyms to determine if any of them is
9653 -- the candidate for overriding by S.
9655 while Present (E) loop
9657 -- Definitely not interesting if not in the current scope
9659 if Scope (E) /= Current_Scope then
9660 null;
9662 -- A function can overload the name of an abstract state. The
9663 -- state can be viewed as a function with a profile that cannot
9664 -- be matched by anything.
9666 elsif Ekind (S) = E_Function
9667 and then Ekind (E) = E_Abstract_State
9668 then
9669 Enter_Overloaded_Entity (S);
9670 return;
9672 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9673 -- procedures locate the internally generated spec. We enforce
9674 -- mode conformance since a tagged type may inherit from
9675 -- interfaces several null primitives which differ only in
9676 -- the mode of the formals.
9678 elsif not Comes_From_Source (S)
9679 and then Is_Null_Procedure (S)
9680 and then not Mode_Conformant (E, S)
9681 then
9682 null;
9684 -- Check if we have type conformance
9686 elsif Type_Conformant (E, S) then
9688 -- If the old and new entities have the same profile and one
9689 -- is not the body of the other, then this is an error, unless
9690 -- one of them is implicitly declared.
9692 -- There are some cases when both can be implicit, for example
9693 -- when both a literal and a function that overrides it are
9694 -- inherited in a derivation, or when an inherited operation
9695 -- of a tagged full type overrides the inherited operation of
9696 -- a private extension. Ada 83 had a special rule for the
9697 -- literal case. In Ada 95, the later implicit operation hides
9698 -- the former, and the literal is always the former. In the
9699 -- odd case where both are derived operations declared at the
9700 -- same point, both operations should be declared, and in that
9701 -- case we bypass the following test and proceed to the next
9702 -- part. This can only occur for certain obscure cases in
9703 -- instances, when an operation on a type derived from a formal
9704 -- private type does not override a homograph inherited from
9705 -- the actual. In subsequent derivations of such a type, the
9706 -- DT positions of these operations remain distinct, if they
9707 -- have been set.
9709 if Present (Alias (S))
9710 and then (No (Alias (E))
9711 or else Comes_From_Source (E)
9712 or else Is_Abstract_Subprogram (S)
9713 or else
9714 (Is_Dispatching_Operation (E)
9715 and then Is_Overriding_Alias (E, S)))
9716 and then Ekind (E) /= E_Enumeration_Literal
9717 then
9718 -- When an derived operation is overloaded it may be due to
9719 -- the fact that the full view of a private extension
9720 -- re-inherits. It has to be dealt with.
9722 if Is_Package_Or_Generic_Package (Current_Scope)
9723 and then In_Private_Part (Current_Scope)
9724 then
9725 Check_Operation_From_Private_View (S, E);
9726 end if;
9728 -- In any case the implicit operation remains hidden by the
9729 -- existing declaration, which is overriding. Indicate that
9730 -- E overrides the operation from which S is inherited.
9732 if Present (Alias (S)) then
9733 Set_Overridden_Operation (E, Alias (S));
9734 Inherit_Subprogram_Contract (E, Alias (S));
9736 else
9737 Set_Overridden_Operation (E, S);
9738 Inherit_Subprogram_Contract (E, S);
9739 end if;
9741 if Comes_From_Source (E) then
9742 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9743 end if;
9745 return;
9747 -- Within an instance, the renaming declarations for actual
9748 -- subprograms may become ambiguous, but they do not hide each
9749 -- other.
9751 elsif Ekind (E) /= E_Entry
9752 and then not Comes_From_Source (E)
9753 and then not Is_Generic_Instance (E)
9754 and then (Present (Alias (E))
9755 or else Is_Intrinsic_Subprogram (E))
9756 and then (not In_Instance
9757 or else No (Parent (E))
9758 or else Nkind (Unit_Declaration_Node (E)) /=
9759 N_Subprogram_Renaming_Declaration)
9760 then
9761 -- A subprogram child unit is not allowed to override an
9762 -- inherited subprogram (10.1.1(20)).
9764 if Is_Child_Unit (S) then
9765 Error_Msg_N
9766 ("child unit overrides inherited subprogram in parent",
9768 return;
9769 end if;
9771 if Is_Non_Overriding_Operation (E, S) then
9772 Enter_Overloaded_Entity (S);
9774 if No (Derived_Type)
9775 or else Is_Tagged_Type (Derived_Type)
9776 then
9777 Check_Dispatching_Operation (S, Empty);
9778 end if;
9780 return;
9781 end if;
9783 -- E is a derived operation or an internal operator which
9784 -- is being overridden. Remove E from further visibility.
9785 -- Furthermore, if E is a dispatching operation, it must be
9786 -- replaced in the list of primitive operations of its type
9787 -- (see Override_Dispatching_Operation).
9789 Overridden_Subp := E;
9791 declare
9792 Prev : Entity_Id;
9794 begin
9795 Prev := First_Entity (Current_Scope);
9796 while Present (Prev) and then Next_Entity (Prev) /= E loop
9797 Next_Entity (Prev);
9798 end loop;
9800 -- It is possible for E to be in the current scope and
9801 -- yet not in the entity chain. This can only occur in a
9802 -- generic context where E is an implicit concatenation
9803 -- in the formal part, because in a generic body the
9804 -- entity chain starts with the formals.
9806 -- In GNATprove mode, a wrapper for an operation with
9807 -- axiomatization may be a homonym of another declaration
9808 -- for an actual subprogram (needs refinement ???).
9810 if No (Prev) then
9811 if In_Instance
9812 and then GNATprove_Mode
9813 and then
9814 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9815 N_Subprogram_Renaming_Declaration
9816 then
9817 return;
9818 else
9819 pragma Assert (Chars (E) = Name_Op_Concat);
9820 null;
9821 end if;
9822 end if;
9824 -- E must be removed both from the entity_list of the
9825 -- current scope, and from the visibility chain.
9827 if Debug_Flag_E then
9828 Write_Str ("Override implicit operation ");
9829 Write_Int (Int (E));
9830 Write_Eol;
9831 end if;
9833 -- If E is a predefined concatenation, it stands for four
9834 -- different operations. As a result, a single explicit
9835 -- declaration does not hide it. In a possible ambiguous
9836 -- situation, Disambiguate chooses the user-defined op,
9837 -- so it is correct to retain the previous internal one.
9839 if Chars (E) /= Name_Op_Concat
9840 or else Ekind (E) /= E_Operator
9841 then
9842 -- For nondispatching derived operations that are
9843 -- overridden by a subprogram declared in the private
9844 -- part of a package, we retain the derived subprogram
9845 -- but mark it as not immediately visible. If the
9846 -- derived operation was declared in the visible part
9847 -- then this ensures that it will still be visible
9848 -- outside the package with the proper signature
9849 -- (calls from outside must also be directed to this
9850 -- version rather than the overriding one, unlike the
9851 -- dispatching case). Calls from inside the package
9852 -- will still resolve to the overriding subprogram
9853 -- since the derived one is marked as not visible
9854 -- within the package.
9856 -- If the private operation is dispatching, we achieve
9857 -- the overriding by keeping the implicit operation
9858 -- but setting its alias to be the overriding one. In
9859 -- this fashion the proper body is executed in all
9860 -- cases, but the original signature is used outside
9861 -- of the package.
9863 -- If the overriding is not in the private part, we
9864 -- remove the implicit operation altogether.
9866 if Is_Private_Declaration (S) then
9867 if not Is_Dispatching_Operation (E) then
9868 Set_Is_Immediately_Visible (E, False);
9869 else
9870 -- Work done in Override_Dispatching_Operation,
9871 -- so nothing else needs to be done here.
9873 null;
9874 end if;
9876 else
9877 -- Find predecessor of E in Homonym chain
9879 if E = Current_Entity (E) then
9880 Prev_Vis := Empty;
9881 else
9882 Prev_Vis := Current_Entity (E);
9883 while Homonym (Prev_Vis) /= E loop
9884 Prev_Vis := Homonym (Prev_Vis);
9885 end loop;
9886 end if;
9888 if Prev_Vis /= Empty then
9890 -- Skip E in the visibility chain
9892 Set_Homonym (Prev_Vis, Homonym (E));
9894 else
9895 Set_Name_Entity_Id (Chars (E), Homonym (E));
9896 end if;
9898 Set_Next_Entity (Prev, Next_Entity (E));
9900 if No (Next_Entity (Prev)) then
9901 Set_Last_Entity (Current_Scope, Prev);
9902 end if;
9903 end if;
9904 end if;
9906 Enter_Overloaded_Entity (S);
9908 -- For entities generated by Derive_Subprograms the
9909 -- overridden operation is the inherited primitive
9910 -- (which is available through the attribute alias).
9912 if not (Comes_From_Source (E))
9913 and then Is_Dispatching_Operation (E)
9914 and then Find_Dispatching_Type (E) =
9915 Find_Dispatching_Type (S)
9916 and then Present (Alias (E))
9917 and then Comes_From_Source (Alias (E))
9918 then
9919 Set_Overridden_Operation (S, Alias (E));
9920 Inherit_Subprogram_Contract (S, Alias (E));
9922 -- Normal case of setting entity as overridden
9924 -- Note: Static_Initialization and Overridden_Operation
9925 -- attributes use the same field in subprogram entities.
9926 -- Static_Initialization is only defined for internal
9927 -- initialization procedures, where Overridden_Operation
9928 -- is irrelevant. Therefore the setting of this attribute
9929 -- must check whether the target is an init_proc.
9931 elsif not Is_Init_Proc (S) then
9932 Set_Overridden_Operation (S, E);
9933 Inherit_Subprogram_Contract (S, E);
9934 end if;
9936 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9938 -- If S is a user-defined subprogram or a null procedure
9939 -- expanded to override an inherited null procedure, or a
9940 -- predefined dispatching primitive then indicate that E
9941 -- overrides the operation from which S is inherited.
9943 if Comes_From_Source (S)
9944 or else
9945 (Present (Parent (S))
9946 and then
9947 Nkind (Parent (S)) = N_Procedure_Specification
9948 and then
9949 Null_Present (Parent (S)))
9950 or else
9951 (Present (Alias (E))
9952 and then
9953 Is_Predefined_Dispatching_Operation (Alias (E)))
9954 then
9955 if Present (Alias (E)) then
9956 Set_Overridden_Operation (S, Alias (E));
9957 Inherit_Subprogram_Contract (S, Alias (E));
9958 end if;
9959 end if;
9961 if Is_Dispatching_Operation (E) then
9963 -- An overriding dispatching subprogram inherits the
9964 -- convention of the overridden subprogram (AI-117).
9966 Set_Convention (S, Convention (E));
9967 Check_Dispatching_Operation (S, E);
9969 else
9970 Check_Dispatching_Operation (S, Empty);
9971 end if;
9973 Check_For_Primitive_Subprogram
9974 (Is_Primitive_Subp, Is_Overriding => True);
9975 goto Check_Inequality;
9976 end;
9978 -- Apparent redeclarations in instances can occur when two
9979 -- formal types get the same actual type. The subprograms in
9980 -- in the instance are legal, even if not callable from the
9981 -- outside. Calls from within are disambiguated elsewhere.
9982 -- For dispatching operations in the visible part, the usual
9983 -- rules apply, and operations with the same profile are not
9984 -- legal (B830001).
9986 elsif (In_Instance_Visible_Part
9987 and then not Is_Dispatching_Operation (E))
9988 or else In_Instance_Not_Visible
9989 then
9990 null;
9992 -- Here we have a real error (identical profile)
9994 else
9995 Error_Msg_Sloc := Sloc (E);
9997 -- Avoid cascaded errors if the entity appears in
9998 -- subsequent calls.
10000 Set_Scope (S, Current_Scope);
10002 -- Generate error, with extra useful warning for the case
10003 -- of a generic instance with no completion.
10005 if Is_Generic_Instance (S)
10006 and then not Has_Completion (E)
10007 then
10008 Error_Msg_N
10009 ("instantiation cannot provide body for&", S);
10010 Error_Msg_N ("\& conflicts with declaration#", S);
10011 else
10012 Error_Msg_N ("& conflicts with declaration#", S);
10013 end if;
10015 return;
10016 end if;
10018 else
10019 -- If one subprogram has an access parameter and the other
10020 -- a parameter of an access type, calls to either might be
10021 -- ambiguous. Verify that parameters match except for the
10022 -- access parameter.
10024 if May_Hide_Profile then
10025 declare
10026 F1 : Entity_Id;
10027 F2 : Entity_Id;
10029 begin
10030 F1 := First_Formal (S);
10031 F2 := First_Formal (E);
10032 while Present (F1) and then Present (F2) loop
10033 if Is_Access_Type (Etype (F1)) then
10034 if not Is_Access_Type (Etype (F2))
10035 or else not Conforming_Types
10036 (Designated_Type (Etype (F1)),
10037 Designated_Type (Etype (F2)),
10038 Type_Conformant)
10039 then
10040 May_Hide_Profile := False;
10041 end if;
10043 elsif
10044 not Conforming_Types
10045 (Etype (F1), Etype (F2), Type_Conformant)
10046 then
10047 May_Hide_Profile := False;
10048 end if;
10050 Next_Formal (F1);
10051 Next_Formal (F2);
10052 end loop;
10054 if May_Hide_Profile
10055 and then No (F1)
10056 and then No (F2)
10057 then
10058 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10059 end if;
10060 end;
10061 end if;
10062 end if;
10064 E := Homonym (E);
10065 end loop;
10067 -- On exit, we know that S is a new entity
10069 Enter_Overloaded_Entity (S);
10070 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10071 Check_Overriding_Indicator
10072 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10074 -- Overloading is not allowed in SPARK, except for operators
10076 if Nkind (S) /= N_Defining_Operator_Symbol then
10077 Error_Msg_Sloc := Sloc (Homonym (S));
10078 Check_SPARK_05_Restriction
10079 ("overloading not allowed with entity#", S);
10080 end if;
10082 -- If S is a derived operation for an untagged type then by
10083 -- definition it's not a dispatching operation (even if the parent
10084 -- operation was dispatching), so Check_Dispatching_Operation is not
10085 -- called in that case.
10087 if No (Derived_Type)
10088 or else Is_Tagged_Type (Derived_Type)
10089 then
10090 Check_Dispatching_Operation (S, Empty);
10091 end if;
10092 end if;
10094 -- If this is a user-defined equality operator that is not a derived
10095 -- subprogram, create the corresponding inequality. If the operation is
10096 -- dispatching, the expansion is done elsewhere, and we do not create
10097 -- an explicit inequality operation.
10099 <<Check_Inequality>>
10100 if Chars (S) = Name_Op_Eq
10101 and then Etype (S) = Standard_Boolean
10102 and then Present (Parent (S))
10103 and then not Is_Dispatching_Operation (S)
10104 then
10105 Make_Inequality_Operator (S);
10106 Check_Untagged_Equality (S);
10107 end if;
10108 end New_Overloaded_Entity;
10110 ---------------------
10111 -- Process_Formals --
10112 ---------------------
10114 procedure Process_Formals
10115 (T : List_Id;
10116 Related_Nod : Node_Id)
10118 Param_Spec : Node_Id;
10119 Formal : Entity_Id;
10120 Formal_Type : Entity_Id;
10121 Default : Node_Id;
10122 Ptype : Entity_Id;
10124 Num_Out_Params : Nat := 0;
10125 First_Out_Param : Entity_Id := Empty;
10126 -- Used for setting Is_Only_Out_Parameter
10128 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10129 -- Determine whether an access type designates a type coming from a
10130 -- limited view.
10132 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10133 -- Check whether the default has a class-wide type. After analysis the
10134 -- default has the type of the formal, so we must also check explicitly
10135 -- for an access attribute.
10137 ----------------------------------
10138 -- Designates_From_Limited_With --
10139 ----------------------------------
10141 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10142 Desig : Entity_Id := Typ;
10144 begin
10145 if Is_Access_Type (Desig) then
10146 Desig := Directly_Designated_Type (Desig);
10147 end if;
10149 if Is_Class_Wide_Type (Desig) then
10150 Desig := Root_Type (Desig);
10151 end if;
10153 return
10154 Ekind (Desig) = E_Incomplete_Type
10155 and then From_Limited_With (Desig);
10156 end Designates_From_Limited_With;
10158 ---------------------------
10159 -- Is_Class_Wide_Default --
10160 ---------------------------
10162 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10163 begin
10164 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10165 or else (Nkind (D) = N_Attribute_Reference
10166 and then Attribute_Name (D) = Name_Access
10167 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10168 end Is_Class_Wide_Default;
10170 -- Start of processing for Process_Formals
10172 begin
10173 -- In order to prevent premature use of the formals in the same formal
10174 -- part, the Ekind is left undefined until all default expressions are
10175 -- analyzed. The Ekind is established in a separate loop at the end.
10177 Param_Spec := First (T);
10178 while Present (Param_Spec) loop
10179 Formal := Defining_Identifier (Param_Spec);
10180 Set_Never_Set_In_Source (Formal, True);
10181 Enter_Name (Formal);
10183 -- Case of ordinary parameters
10185 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10186 Find_Type (Parameter_Type (Param_Spec));
10187 Ptype := Parameter_Type (Param_Spec);
10189 if Ptype = Error then
10190 goto Continue;
10191 end if;
10193 Formal_Type := Entity (Ptype);
10195 if Is_Incomplete_Type (Formal_Type)
10196 or else
10197 (Is_Class_Wide_Type (Formal_Type)
10198 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10199 then
10200 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10201 -- primitive operations, as long as their completion is
10202 -- in the same declarative part. If in the private part
10203 -- this means that the type cannot be a Taft-amendment type.
10204 -- Check is done on package exit. For access to subprograms,
10205 -- the use is legal for Taft-amendment types.
10207 -- Ada 2012: tagged incomplete types are allowed as generic
10208 -- formal types. They do not introduce dependencies and the
10209 -- corresponding generic subprogram does not have a delayed
10210 -- freeze, because it does not need a freeze node. However,
10211 -- it is still the case that untagged incomplete types cannot
10212 -- be Taft-amendment types and must be completed in private
10213 -- part, so the subprogram must appear in the list of private
10214 -- dependents of the type. If the type is class-wide, it is
10215 -- not a primitive, but the freezing of the subprogram must
10216 -- also be delayed to force the creation of a freeze node.
10218 if Is_Tagged_Type (Formal_Type)
10219 or else (Ada_Version >= Ada_2012
10220 and then not From_Limited_With (Formal_Type)
10221 and then not Is_Generic_Type (Formal_Type))
10222 then
10223 if Ekind (Scope (Current_Scope)) = E_Package
10224 and then not Is_Generic_Type (Formal_Type)
10225 then
10226 if not Nkind_In
10227 (Parent (T), N_Access_Function_Definition,
10228 N_Access_Procedure_Definition)
10229 then
10230 if not Is_Class_Wide_Type (Formal_Type) then
10231 Append_Elmt (Current_Scope,
10232 Private_Dependents (Base_Type (Formal_Type)));
10233 end if;
10235 -- Freezing is delayed to ensure that Register_Prim
10236 -- will get called for this operation, which is needed
10237 -- in cases where static dispatch tables aren't built.
10238 -- (Note that the same is done for controlling access
10239 -- parameter cases in function Access_Definition.)
10241 if not Is_Thunk (Current_Scope) then
10242 Set_Has_Delayed_Freeze (Current_Scope);
10243 end if;
10244 end if;
10245 end if;
10247 -- Special handling of Value_Type for CIL case
10249 elsif Is_Value_Type (Formal_Type) then
10250 null;
10252 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10253 N_Access_Procedure_Definition)
10254 then
10255 -- AI05-0151: Tagged incomplete types are allowed in all
10256 -- formal parts. Untagged incomplete types are not allowed
10257 -- in bodies. Limited views of either kind are not allowed
10258 -- if there is no place at which the non-limited view can
10259 -- become available.
10261 -- Incomplete formal untagged types are not allowed in
10262 -- subprogram bodies (but are legal in their declarations).
10264 if Is_Generic_Type (Formal_Type)
10265 and then not Is_Tagged_Type (Formal_Type)
10266 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10267 then
10268 Error_Msg_N
10269 ("invalid use of formal incomplete type", Param_Spec);
10271 elsif Ada_Version >= Ada_2012 then
10272 if Is_Tagged_Type (Formal_Type)
10273 and then (not From_Limited_With (Formal_Type)
10274 or else not In_Package_Body)
10275 then
10276 null;
10278 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10279 N_Accept_Alternative,
10280 N_Entry_Body,
10281 N_Subprogram_Body)
10282 then
10283 Error_Msg_NE
10284 ("invalid use of untagged incomplete type&",
10285 Ptype, Formal_Type);
10286 end if;
10288 else
10289 Error_Msg_NE
10290 ("invalid use of incomplete type&",
10291 Param_Spec, Formal_Type);
10293 -- Further checks on the legality of incomplete types
10294 -- in formal parts are delayed until the freeze point
10295 -- of the enclosing subprogram or access to subprogram.
10296 end if;
10297 end if;
10299 elsif Ekind (Formal_Type) = E_Void then
10300 Error_Msg_NE
10301 ("premature use of&",
10302 Parameter_Type (Param_Spec), Formal_Type);
10303 end if;
10305 -- Ada 2012 (AI-142): Handle aliased parameters
10307 if Ada_Version >= Ada_2012
10308 and then Aliased_Present (Param_Spec)
10309 then
10310 Set_Is_Aliased (Formal);
10311 end if;
10313 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10314 -- declaration corresponding to the null-excluding type of the
10315 -- formal in the enclosing scope. Finally, replace the parameter
10316 -- type of the formal with the internal subtype.
10318 if Ada_Version >= Ada_2005
10319 and then Null_Exclusion_Present (Param_Spec)
10320 then
10321 if not Is_Access_Type (Formal_Type) then
10322 Error_Msg_N
10323 ("`NOT NULL` allowed only for an access type", Param_Spec);
10325 else
10326 if Can_Never_Be_Null (Formal_Type)
10327 and then Comes_From_Source (Related_Nod)
10328 then
10329 Error_Msg_NE
10330 ("`NOT NULL` not allowed (& already excludes null)",
10331 Param_Spec, Formal_Type);
10332 end if;
10334 Formal_Type :=
10335 Create_Null_Excluding_Itype
10336 (T => Formal_Type,
10337 Related_Nod => Related_Nod,
10338 Scope_Id => Scope (Current_Scope));
10340 -- If the designated type of the itype is an itype that is
10341 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10342 -- on the access subtype, to prevent order-of-elaboration
10343 -- issues in the backend.
10345 -- Example:
10346 -- type T is access procedure;
10347 -- procedure Op (O : not null T);
10349 if Is_Itype (Directly_Designated_Type (Formal_Type))
10350 and then
10351 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10352 then
10353 Set_Has_Delayed_Freeze (Formal_Type);
10354 end if;
10355 end if;
10356 end if;
10358 -- An access formal type
10360 else
10361 Formal_Type :=
10362 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10364 -- No need to continue if we already notified errors
10366 if not Present (Formal_Type) then
10367 return;
10368 end if;
10370 -- Ada 2005 (AI-254)
10372 declare
10373 AD : constant Node_Id :=
10374 Access_To_Subprogram_Definition
10375 (Parameter_Type (Param_Spec));
10376 begin
10377 if Present (AD) and then Protected_Present (AD) then
10378 Formal_Type :=
10379 Replace_Anonymous_Access_To_Protected_Subprogram
10380 (Param_Spec);
10381 end if;
10382 end;
10383 end if;
10385 Set_Etype (Formal, Formal_Type);
10387 -- Deal with default expression if present
10389 Default := Expression (Param_Spec);
10391 if Present (Default) then
10392 Check_SPARK_05_Restriction
10393 ("default expression is not allowed", Default);
10395 if Out_Present (Param_Spec) then
10396 Error_Msg_N
10397 ("default initialization only allowed for IN parameters",
10398 Param_Spec);
10399 end if;
10401 -- Do the special preanalysis of the expression (see section on
10402 -- "Handling of Default Expressions" in the spec of package Sem).
10404 Preanalyze_Spec_Expression (Default, Formal_Type);
10406 -- An access to constant cannot be the default for
10407 -- an access parameter that is an access to variable.
10409 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10410 and then not Is_Access_Constant (Formal_Type)
10411 and then Is_Access_Type (Etype (Default))
10412 and then Is_Access_Constant (Etype (Default))
10413 then
10414 Error_Msg_N
10415 ("formal that is access to variable cannot be initialized "
10416 & "with an access-to-constant expression", Default);
10417 end if;
10419 -- Check that the designated type of an access parameter's default
10420 -- is not a class-wide type unless the parameter's designated type
10421 -- is also class-wide.
10423 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10424 and then not Designates_From_Limited_With (Formal_Type)
10425 and then Is_Class_Wide_Default (Default)
10426 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10427 then
10428 Error_Msg_N
10429 ("access to class-wide expression not allowed here", Default);
10430 end if;
10432 -- Check incorrect use of dynamically tagged expressions
10434 if Is_Tagged_Type (Formal_Type) then
10435 Check_Dynamically_Tagged_Expression
10436 (Expr => Default,
10437 Typ => Formal_Type,
10438 Related_Nod => Default);
10439 end if;
10440 end if;
10442 -- Ada 2005 (AI-231): Static checks
10444 if Ada_Version >= Ada_2005
10445 and then Is_Access_Type (Etype (Formal))
10446 and then Can_Never_Be_Null (Etype (Formal))
10447 then
10448 Null_Exclusion_Static_Checks (Param_Spec);
10449 end if;
10451 -- The following checks are relevant when SPARK_Mode is on as these
10452 -- are not standard Ada legality rules.
10454 if SPARK_Mode = On then
10455 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10457 -- A function cannot have a parameter of mode IN OUT or OUT
10458 -- (SPARK RM 6.1).
10460 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10461 Error_Msg_N
10462 ("function cannot have parameter of mode `OUT` or "
10463 & "`IN OUT`", Formal);
10465 -- A function cannot have an effectively volatile formal
10466 -- parameter (SPARK RM 7.1.3(10)).
10468 elsif Is_Effectively_Volatile (Formal) then
10469 Error_Msg_N
10470 ("function cannot have a volatile formal parameter",
10471 Formal);
10472 end if;
10474 -- A procedure cannot have an effectively volatile formal
10475 -- parameter of mode IN because it behaves as a constant
10476 -- (SPARK RM 7.1.3(6)).
10478 elsif Ekind (Scope (Formal)) = E_Procedure
10479 and then Ekind (Formal) = E_In_Parameter
10480 and then Is_Effectively_Volatile (Formal)
10481 then
10482 Error_Msg_N
10483 ("formal parameter of mode `IN` cannot be volatile", Formal);
10484 end if;
10485 end if;
10487 <<Continue>>
10488 Next (Param_Spec);
10489 end loop;
10491 -- If this is the formal part of a function specification, analyze the
10492 -- subtype mark in the context where the formals are visible but not
10493 -- yet usable, and may hide outer homographs.
10495 if Nkind (Related_Nod) = N_Function_Specification then
10496 Analyze_Return_Type (Related_Nod);
10497 end if;
10499 -- Now set the kind (mode) of each formal
10501 Param_Spec := First (T);
10502 while Present (Param_Spec) loop
10503 Formal := Defining_Identifier (Param_Spec);
10504 Set_Formal_Mode (Formal);
10506 if Ekind (Formal) = E_In_Parameter then
10507 Set_Default_Value (Formal, Expression (Param_Spec));
10509 if Present (Expression (Param_Spec)) then
10510 Default := Expression (Param_Spec);
10512 if Is_Scalar_Type (Etype (Default)) then
10513 if Nkind (Parameter_Type (Param_Spec)) /=
10514 N_Access_Definition
10515 then
10516 Formal_Type := Entity (Parameter_Type (Param_Spec));
10517 else
10518 Formal_Type :=
10519 Access_Definition
10520 (Related_Nod, Parameter_Type (Param_Spec));
10521 end if;
10523 Apply_Scalar_Range_Check (Default, Formal_Type);
10524 end if;
10525 end if;
10527 elsif Ekind (Formal) = E_Out_Parameter then
10528 Num_Out_Params := Num_Out_Params + 1;
10530 if Num_Out_Params = 1 then
10531 First_Out_Param := Formal;
10532 end if;
10534 elsif Ekind (Formal) = E_In_Out_Parameter then
10535 Num_Out_Params := Num_Out_Params + 1;
10536 end if;
10538 -- Skip remaining processing if formal type was in error
10540 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10541 goto Next_Parameter;
10542 end if;
10544 -- Force call by reference if aliased
10546 if Is_Aliased (Formal) then
10547 Set_Mechanism (Formal, By_Reference);
10549 -- Warn if user asked this to be passed by copy
10551 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10552 Error_Msg_N
10553 ("cannot pass aliased parameter & by copy??", Formal);
10554 end if;
10556 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10558 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10559 Set_Mechanism (Formal, By_Copy);
10561 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10562 Set_Mechanism (Formal, By_Reference);
10563 end if;
10565 <<Next_Parameter>>
10566 Next (Param_Spec);
10567 end loop;
10569 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10570 Set_Is_Only_Out_Parameter (First_Out_Param);
10571 end if;
10572 end Process_Formals;
10574 ----------------------------
10575 -- Reference_Body_Formals --
10576 ----------------------------
10578 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10579 Fs : Entity_Id;
10580 Fb : Entity_Id;
10582 begin
10583 if Error_Posted (Spec) then
10584 return;
10585 end if;
10587 -- Iterate over both lists. They may be of different lengths if the two
10588 -- specs are not conformant.
10590 Fs := First_Formal (Spec);
10591 Fb := First_Formal (Bod);
10592 while Present (Fs) and then Present (Fb) loop
10593 Generate_Reference (Fs, Fb, 'b');
10595 if Style_Check then
10596 Style.Check_Identifier (Fb, Fs);
10597 end if;
10599 Set_Spec_Entity (Fb, Fs);
10600 Set_Referenced (Fs, False);
10601 Next_Formal (Fs);
10602 Next_Formal (Fb);
10603 end loop;
10604 end Reference_Body_Formals;
10606 -------------------------
10607 -- Set_Actual_Subtypes --
10608 -------------------------
10610 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10611 Decl : Node_Id;
10612 Formal : Entity_Id;
10613 T : Entity_Id;
10614 First_Stmt : Node_Id := Empty;
10615 AS_Needed : Boolean;
10617 begin
10618 -- If this is an empty initialization procedure, no need to create
10619 -- actual subtypes (small optimization).
10621 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10622 return;
10623 end if;
10625 Formal := First_Formal (Subp);
10626 while Present (Formal) loop
10627 T := Etype (Formal);
10629 -- We never need an actual subtype for a constrained formal
10631 if Is_Constrained (T) then
10632 AS_Needed := False;
10634 -- If we have unknown discriminants, then we do not need an actual
10635 -- subtype, or more accurately we cannot figure it out. Note that
10636 -- all class-wide types have unknown discriminants.
10638 elsif Has_Unknown_Discriminants (T) then
10639 AS_Needed := False;
10641 -- At this stage we have an unconstrained type that may need an
10642 -- actual subtype. For sure the actual subtype is needed if we have
10643 -- an unconstrained array type.
10645 elsif Is_Array_Type (T) then
10646 AS_Needed := True;
10648 -- The only other case needing an actual subtype is an unconstrained
10649 -- record type which is an IN parameter (we cannot generate actual
10650 -- subtypes for the OUT or IN OUT case, since an assignment can
10651 -- change the discriminant values. However we exclude the case of
10652 -- initialization procedures, since discriminants are handled very
10653 -- specially in this context, see the section entitled "Handling of
10654 -- Discriminants" in Einfo.
10656 -- We also exclude the case of Discrim_SO_Functions (functions used
10657 -- in front end layout mode for size/offset values), since in such
10658 -- functions only discriminants are referenced, and not only are such
10659 -- subtypes not needed, but they cannot always be generated, because
10660 -- of order of elaboration issues.
10662 elsif Is_Record_Type (T)
10663 and then Ekind (Formal) = E_In_Parameter
10664 and then Chars (Formal) /= Name_uInit
10665 and then not Is_Unchecked_Union (T)
10666 and then not Is_Discrim_SO_Function (Subp)
10667 then
10668 AS_Needed := True;
10670 -- All other cases do not need an actual subtype
10672 else
10673 AS_Needed := False;
10674 end if;
10676 -- Generate actual subtypes for unconstrained arrays and
10677 -- unconstrained discriminated records.
10679 if AS_Needed then
10680 if Nkind (N) = N_Accept_Statement then
10682 -- If expansion is active, the formal is replaced by a local
10683 -- variable that renames the corresponding entry of the
10684 -- parameter block, and it is this local variable that may
10685 -- require an actual subtype.
10687 if Expander_Active then
10688 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10689 else
10690 Decl := Build_Actual_Subtype (T, Formal);
10691 end if;
10693 if Present (Handled_Statement_Sequence (N)) then
10694 First_Stmt :=
10695 First (Statements (Handled_Statement_Sequence (N)));
10696 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10697 Mark_Rewrite_Insertion (Decl);
10698 else
10699 -- If the accept statement has no body, there will be no
10700 -- reference to the actuals, so no need to compute actual
10701 -- subtypes.
10703 return;
10704 end if;
10706 else
10707 Decl := Build_Actual_Subtype (T, Formal);
10708 Prepend (Decl, Declarations (N));
10709 Mark_Rewrite_Insertion (Decl);
10710 end if;
10712 -- The declaration uses the bounds of an existing object, and
10713 -- therefore needs no constraint checks.
10715 Analyze (Decl, Suppress => All_Checks);
10717 -- We need to freeze manually the generated type when it is
10718 -- inserted anywhere else than in a declarative part.
10720 if Present (First_Stmt) then
10721 Insert_List_Before_And_Analyze (First_Stmt,
10722 Freeze_Entity (Defining_Identifier (Decl), N));
10724 -- Ditto if the type has a dynamic predicate, because the
10725 -- generated function will mention the actual subtype.
10727 elsif Has_Dynamic_Predicate_Aspect (T) then
10728 Insert_List_Before_And_Analyze (Decl,
10729 Freeze_Entity (Defining_Identifier (Decl), N));
10730 end if;
10732 if Nkind (N) = N_Accept_Statement
10733 and then Expander_Active
10734 then
10735 Set_Actual_Subtype (Renamed_Object (Formal),
10736 Defining_Identifier (Decl));
10737 else
10738 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10739 end if;
10740 end if;
10742 Next_Formal (Formal);
10743 end loop;
10744 end Set_Actual_Subtypes;
10746 ---------------------
10747 -- Set_Formal_Mode --
10748 ---------------------
10750 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10751 Spec : constant Node_Id := Parent (Formal_Id);
10753 begin
10754 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10755 -- since we ensure that corresponding actuals are always valid at the
10756 -- point of the call.
10758 if Out_Present (Spec) then
10759 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10761 -- [IN] OUT parameters allowed for functions in Ada 2012
10763 if Ada_Version >= Ada_2012 then
10765 -- Even in Ada 2012 operators can only have IN parameters
10767 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10768 Error_Msg_N ("operators can only have IN parameters", Spec);
10769 end if;
10771 if In_Present (Spec) then
10772 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10773 else
10774 Set_Ekind (Formal_Id, E_Out_Parameter);
10775 end if;
10777 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10779 -- But not in earlier versions of Ada
10781 else
10782 Error_Msg_N ("functions can only have IN parameters", Spec);
10783 Set_Ekind (Formal_Id, E_In_Parameter);
10784 end if;
10786 elsif In_Present (Spec) then
10787 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10789 else
10790 Set_Ekind (Formal_Id, E_Out_Parameter);
10791 Set_Never_Set_In_Source (Formal_Id, True);
10792 Set_Is_True_Constant (Formal_Id, False);
10793 Set_Current_Value (Formal_Id, Empty);
10794 end if;
10796 else
10797 Set_Ekind (Formal_Id, E_In_Parameter);
10798 end if;
10800 -- Set Is_Known_Non_Null for access parameters since the language
10801 -- guarantees that access parameters are always non-null. We also set
10802 -- Can_Never_Be_Null, since there is no way to change the value.
10804 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10806 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10807 -- null; In Ada 2005, only if then null_exclusion is explicit.
10809 if Ada_Version < Ada_2005
10810 or else Can_Never_Be_Null (Etype (Formal_Id))
10811 then
10812 Set_Is_Known_Non_Null (Formal_Id);
10813 Set_Can_Never_Be_Null (Formal_Id);
10814 end if;
10816 -- Ada 2005 (AI-231): Null-exclusion access subtype
10818 elsif Is_Access_Type (Etype (Formal_Id))
10819 and then Can_Never_Be_Null (Etype (Formal_Id))
10820 then
10821 Set_Is_Known_Non_Null (Formal_Id);
10823 -- We can also set Can_Never_Be_Null (thus preventing some junk
10824 -- access checks) for the case of an IN parameter, which cannot
10825 -- be changed, or for an IN OUT parameter, which can be changed but
10826 -- not to a null value. But for an OUT parameter, the initial value
10827 -- passed in can be null, so we can't set this flag in that case.
10829 if Ekind (Formal_Id) /= E_Out_Parameter then
10830 Set_Can_Never_Be_Null (Formal_Id);
10831 end if;
10832 end if;
10834 Set_Mechanism (Formal_Id, Default_Mechanism);
10835 Set_Formal_Validity (Formal_Id);
10836 end Set_Formal_Mode;
10838 -------------------------
10839 -- Set_Formal_Validity --
10840 -------------------------
10842 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10843 begin
10844 -- If no validity checking, then we cannot assume anything about the
10845 -- validity of parameters, since we do not know there is any checking
10846 -- of the validity on the call side.
10848 if not Validity_Checks_On then
10849 return;
10851 -- If validity checking for parameters is enabled, this means we are
10852 -- not supposed to make any assumptions about argument values.
10854 elsif Validity_Check_Parameters then
10855 return;
10857 -- If we are checking in parameters, we will assume that the caller is
10858 -- also checking parameters, so we can assume the parameter is valid.
10860 elsif Ekind (Formal_Id) = E_In_Parameter
10861 and then Validity_Check_In_Params
10862 then
10863 Set_Is_Known_Valid (Formal_Id, True);
10865 -- Similar treatment for IN OUT parameters
10867 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10868 and then Validity_Check_In_Out_Params
10869 then
10870 Set_Is_Known_Valid (Formal_Id, True);
10871 end if;
10872 end Set_Formal_Validity;
10874 ------------------------
10875 -- Subtype_Conformant --
10876 ------------------------
10878 function Subtype_Conformant
10879 (New_Id : Entity_Id;
10880 Old_Id : Entity_Id;
10881 Skip_Controlling_Formals : Boolean := False) return Boolean
10883 Result : Boolean;
10884 begin
10885 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10886 Skip_Controlling_Formals => Skip_Controlling_Formals);
10887 return Result;
10888 end Subtype_Conformant;
10890 ---------------------
10891 -- Type_Conformant --
10892 ---------------------
10894 function Type_Conformant
10895 (New_Id : Entity_Id;
10896 Old_Id : Entity_Id;
10897 Skip_Controlling_Formals : Boolean := False) return Boolean
10899 Result : Boolean;
10900 begin
10901 May_Hide_Profile := False;
10902 Check_Conformance
10903 (New_Id, Old_Id, Type_Conformant, False, Result,
10904 Skip_Controlling_Formals => Skip_Controlling_Formals);
10905 return Result;
10906 end Type_Conformant;
10908 -------------------------------
10909 -- Valid_Operator_Definition --
10910 -------------------------------
10912 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10913 N : Integer := 0;
10914 F : Entity_Id;
10915 Id : constant Name_Id := Chars (Designator);
10916 N_OK : Boolean;
10918 begin
10919 F := First_Formal (Designator);
10920 while Present (F) loop
10921 N := N + 1;
10923 if Present (Default_Value (F)) then
10924 Error_Msg_N
10925 ("default values not allowed for operator parameters",
10926 Parent (F));
10928 -- For function instantiations that are operators, we must check
10929 -- separately that the corresponding generic only has in-parameters.
10930 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10931 -- an error could not arise in earlier versions of the language.
10933 elsif Ekind (F) /= E_In_Parameter then
10934 Error_Msg_N ("operators can only have IN parameters", F);
10935 end if;
10937 Next_Formal (F);
10938 end loop;
10940 -- Verify that user-defined operators have proper number of arguments
10941 -- First case of operators which can only be unary
10943 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10944 N_OK := (N = 1);
10946 -- Case of operators which can be unary or binary
10948 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10949 N_OK := (N in 1 .. 2);
10951 -- All other operators can only be binary
10953 else
10954 N_OK := (N = 2);
10955 end if;
10957 if not N_OK then
10958 Error_Msg_N
10959 ("incorrect number of arguments for operator", Designator);
10960 end if;
10962 if Id = Name_Op_Ne
10963 and then Base_Type (Etype (Designator)) = Standard_Boolean
10964 and then not Is_Intrinsic_Subprogram (Designator)
10965 then
10966 Error_Msg_N
10967 ("explicit definition of inequality not allowed", Designator);
10968 end if;
10969 end Valid_Operator_Definition;
10971 end Sem_Ch6;