2015-01-30 Gary Dismukes <dismukes@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob575f0b68039bbe64031d97548133547d3e46dff1
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_Null_Procedure
108 (N : Node_Id;
109 Is_Completion : out Boolean);
110 -- A null procedure can be a declaration or (Ada 2012) a completion
112 procedure Analyze_Return_Statement (N : Node_Id);
113 -- Common processing for simple and extended return statements
115 procedure Analyze_Function_Return (N : Node_Id);
116 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
117 -- applies to a [generic] function.
119 procedure Analyze_Return_Type (N : Node_Id);
120 -- Subsidiary to Process_Formals: analyze subtype mark in function
121 -- specification in a context where the formals are visible and hide
122 -- outer homographs.
124 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
125 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
126 -- that we can use RETURN but not skip the debug output at the end.
128 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
129 -- Analyze a generic subprogram body. N is the body to be analyzed, and
130 -- Gen_Id is the defining entity Id for the corresponding spec.
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);
226 Set_Contract (Designator, Make_Contract (Sloc (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_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1270 Set_Ekind (Body_Id, E_Subprogram_Body);
1271 Set_Convention (Body_Id, Convention (Gen_Id));
1272 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1273 Set_Scope (Body_Id, Scope (Gen_Id));
1275 -- Inherit the "ghostness" of the generic spec. Note that this
1276 -- property is not directly inherited as the body may be subject
1277 -- to a different Ghost assertion policy.
1279 if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then
1280 Set_Is_Ghost_Entity (Body_Id);
1282 -- The Ghost policy in effect at the point of declaration and at
1283 -- the point of completion must match (SPARK RM 6.9(15)).
1285 Check_Ghost_Completion (Gen_Id, Body_Id);
1286 end if;
1288 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1290 if Nkind (N) = N_Subprogram_Body_Stub then
1292 -- No body to analyze, so restore state of generic unit
1294 Set_Ekind (Gen_Id, Kind);
1295 Set_Ekind (Body_Id, Kind);
1297 if Present (First_Ent) then
1298 Set_First_Entity (Gen_Id, First_Ent);
1299 end if;
1301 End_Scope;
1302 return;
1303 end if;
1305 -- If this is a compilation unit, it must be made visible explicitly,
1306 -- because the compilation of the declaration, unlike other library
1307 -- unit declarations, does not. If it is not a unit, the following
1308 -- is redundant but harmless.
1310 Set_Is_Immediately_Visible (Gen_Id);
1311 Reference_Body_Formals (Gen_Id, Body_Id);
1313 if Is_Child_Unit (Gen_Id) then
1314 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1315 end if;
1317 Set_Actual_Subtypes (N, Current_Scope);
1319 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1320 -- invariants and predicates associated with the body and its spec.
1321 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1322 -- prepares the contract assertions for generic subprograms or for
1323 -- ASIS. Do not generate contract checks in SPARK mode.
1325 if not GNATprove_Mode then
1326 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1327 end if;
1329 -- If the generic unit carries pre- or post-conditions, copy them
1330 -- to the original generic tree, so that they are properly added
1331 -- to any instantiation.
1333 declare
1334 Orig : constant Node_Id := Original_Node (N);
1335 Cond : Node_Id;
1337 begin
1338 Cond := First (Declarations (N));
1339 while Present (Cond) loop
1340 if Nkind (Cond) = N_Pragma
1341 and then Pragma_Name (Cond) = Name_Check
1342 then
1343 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1345 elsif Nkind (Cond) = N_Pragma
1346 and then Pragma_Name (Cond) = Name_Postcondition
1347 then
1348 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1349 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1350 else
1351 exit;
1352 end if;
1354 Next (Cond);
1355 end loop;
1356 end;
1358 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1359 Set_SPARK_Pragma_Inherited (Body_Id, True);
1361 Analyze_Declarations (Declarations (N));
1362 Check_Completion;
1363 Analyze (Handled_Statement_Sequence (N));
1365 Save_Global_References (Original_Node (N));
1367 -- Prior to exiting the scope, include generic formals again (if any
1368 -- are present) in the set of local entities.
1370 if Present (First_Ent) then
1371 Set_First_Entity (Gen_Id, First_Ent);
1372 end if;
1374 Check_References (Gen_Id);
1375 end;
1377 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1378 End_Scope;
1379 Check_Subprogram_Order (N);
1381 -- Outside of its body, unit is generic again
1383 Set_Ekind (Gen_Id, Kind);
1384 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1386 if Style_Check then
1387 Style.Check_Identifier (Body_Id, Gen_Id);
1388 end if;
1390 End_Generic;
1391 end Analyze_Generic_Subprogram_Body;
1393 ----------------------------
1394 -- Analyze_Null_Procedure --
1395 ----------------------------
1397 procedure Analyze_Null_Procedure
1398 (N : Node_Id;
1399 Is_Completion : out Boolean)
1401 Loc : constant Source_Ptr := Sloc (N);
1402 Spec : constant Node_Id := Specification (N);
1403 Designator : Entity_Id;
1404 Form : Node_Id;
1405 Null_Body : Node_Id := Empty;
1406 Prev : Entity_Id;
1408 begin
1409 -- Capture the profile of the null procedure before analysis, for
1410 -- expansion at the freeze point and at each point of call. The body is
1411 -- used if the procedure has preconditions, or if it is a completion. In
1412 -- the first case the body is analyzed at the freeze point, in the other
1413 -- it replaces the null procedure declaration.
1415 Null_Body :=
1416 Make_Subprogram_Body (Loc,
1417 Specification => New_Copy_Tree (Spec),
1418 Declarations => New_List,
1419 Handled_Statement_Sequence =>
1420 Make_Handled_Sequence_Of_Statements (Loc,
1421 Statements => New_List (Make_Null_Statement (Loc))));
1423 -- Create new entities for body and formals
1425 Set_Defining_Unit_Name (Specification (Null_Body),
1426 Make_Defining_Identifier
1427 (Sloc (Defining_Entity (N)),
1428 Chars (Defining_Entity (N))));
1430 Form := First (Parameter_Specifications (Specification (Null_Body)));
1431 while Present (Form) loop
1432 Set_Defining_Identifier (Form,
1433 Make_Defining_Identifier
1434 (Sloc (Defining_Identifier (Form)),
1435 Chars (Defining_Identifier (Form))));
1436 Next (Form);
1437 end loop;
1439 -- Determine whether the null procedure may be a completion of a generic
1440 -- suprogram, in which case we use the new null body as the completion
1441 -- and set minimal semantic information on the original declaration,
1442 -- which is rewritten as a null statement.
1444 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1446 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1447 Insert_Before (N, Null_Body);
1448 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1449 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1451 Rewrite (N, Make_Null_Statement (Loc));
1452 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1453 Is_Completion := True;
1454 return;
1456 else
1457 -- Resolve the types of the formals now, because the freeze point
1458 -- may appear in a different context, e.g. an instantiation.
1460 Form := First (Parameter_Specifications (Specification (Null_Body)));
1461 while Present (Form) loop
1462 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1463 Find_Type (Parameter_Type (Form));
1465 elsif
1466 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1467 then
1468 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1470 else
1471 -- The case of a null procedure with a formal that is an
1472 -- access_to_subprogram type, and that is used as an actual
1473 -- in an instantiation is left to the enthusiastic reader.
1475 null;
1476 end if;
1478 Next (Form);
1479 end loop;
1480 end if;
1482 -- If there are previous overloadable entities with the same name,
1483 -- check whether any of them is completed by the null procedure.
1485 if Present (Prev) and then Is_Overloadable (Prev) then
1486 Designator := Analyze_Subprogram_Specification (Spec);
1487 Prev := Find_Corresponding_Spec (N);
1488 end if;
1490 if No (Prev) or else not Comes_From_Source (Prev) then
1491 Designator := Analyze_Subprogram_Specification (Spec);
1492 Set_Has_Completion (Designator);
1494 -- Signal to caller that this is a procedure declaration
1496 Is_Completion := False;
1498 -- Null procedures are always inlined, but generic formal subprograms
1499 -- which appear as such in the internal instance of formal packages,
1500 -- need no completion and are not marked Inline.
1502 if Expander_Active
1503 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1504 then
1505 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1506 Set_Body_To_Inline (N, Null_Body);
1507 Set_Is_Inlined (Designator);
1508 end if;
1510 else
1511 -- The null procedure is a completion. We unconditionally rewrite
1512 -- this as a null body (even if expansion is not active), because
1513 -- there are various error checks that are applied on this body
1514 -- when it is analyzed (e.g. correct aspect placement).
1516 if Has_Completion (Prev) then
1517 Error_Msg_Sloc := Sloc (Prev);
1518 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1519 end if;
1521 Is_Completion := True;
1522 Rewrite (N, Null_Body);
1523 Analyze (N);
1524 end if;
1525 end Analyze_Null_Procedure;
1527 -----------------------------
1528 -- Analyze_Operator_Symbol --
1529 -----------------------------
1531 -- An operator symbol such as "+" or "and" may appear in context where the
1532 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1533 -- is just a string, as in (conjunction = "or"). In these cases the parser
1534 -- generates this node, and the semantics does the disambiguation. Other
1535 -- such case are actuals in an instantiation, the generic unit in an
1536 -- instantiation, and pragma arguments.
1538 procedure Analyze_Operator_Symbol (N : Node_Id) is
1539 Par : constant Node_Id := Parent (N);
1541 begin
1542 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1543 or else Nkind (Par) = N_Function_Instantiation
1544 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1545 or else (Nkind (Par) = N_Pragma_Argument_Association
1546 and then not Is_Pragma_String_Literal (Par))
1547 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1548 or else (Nkind (Par) = N_Attribute_Reference
1549 and then Attribute_Name (Par) /= Name_Value)
1550 then
1551 Find_Direct_Name (N);
1553 else
1554 Change_Operator_Symbol_To_String_Literal (N);
1555 Analyze (N);
1556 end if;
1557 end Analyze_Operator_Symbol;
1559 -----------------------------------
1560 -- Analyze_Parameter_Association --
1561 -----------------------------------
1563 procedure Analyze_Parameter_Association (N : Node_Id) is
1564 begin
1565 Analyze (Explicit_Actual_Parameter (N));
1566 end Analyze_Parameter_Association;
1568 ----------------------------
1569 -- Analyze_Procedure_Call --
1570 ----------------------------
1572 procedure Analyze_Procedure_Call (N : Node_Id) is
1573 Loc : constant Source_Ptr := Sloc (N);
1574 P : constant Node_Id := Name (N);
1575 Actuals : constant List_Id := Parameter_Associations (N);
1576 Actual : Node_Id;
1577 New_N : Node_Id;
1579 procedure Analyze_Call_And_Resolve;
1580 -- Do Analyze and Resolve calls for procedure call
1581 -- At end, check illegal order dependence.
1583 ------------------------------
1584 -- Analyze_Call_And_Resolve --
1585 ------------------------------
1587 procedure Analyze_Call_And_Resolve is
1588 begin
1589 if Nkind (N) = N_Procedure_Call_Statement then
1590 Analyze_Call (N);
1591 Resolve (N, Standard_Void_Type);
1592 else
1593 Analyze (N);
1594 end if;
1595 end Analyze_Call_And_Resolve;
1597 -- Start of processing for Analyze_Procedure_Call
1599 begin
1600 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1601 -- a procedure call or an entry call. The prefix may denote an access
1602 -- to subprogram type, in which case an implicit dereference applies.
1603 -- If the prefix is an indexed component (without implicit dereference)
1604 -- then the construct denotes a call to a member of an entire family.
1605 -- If the prefix is a simple name, it may still denote a call to a
1606 -- parameterless member of an entry family. Resolution of these various
1607 -- interpretations is delicate.
1609 Analyze (P);
1611 -- If this is a call of the form Obj.Op, the call may have been
1612 -- analyzed and possibly rewritten into a block, in which case
1613 -- we are done.
1615 if Analyzed (N) then
1616 return;
1617 end if;
1619 -- If there is an error analyzing the name (which may have been
1620 -- rewritten if the original call was in prefix notation) then error
1621 -- has been emitted already, mark node and return.
1623 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1624 Set_Etype (N, Any_Type);
1625 return;
1626 end if;
1628 -- The name of the procedure call may reference an entity subject to
1629 -- pragma Ghost with policy Ignore. Set the mode now to ensure that any
1630 -- nodes generated during analysis and expansion are properly flagged as
1631 -- ignored Ghost.
1633 Set_Ghost_Mode (N);
1635 -- Otherwise analyze the parameters
1637 if Present (Actuals) then
1638 Actual := First (Actuals);
1640 while Present (Actual) loop
1641 Analyze (Actual);
1642 Check_Parameterless_Call (Actual);
1643 Next (Actual);
1644 end loop;
1645 end if;
1647 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1649 if Nkind (P) = N_Attribute_Reference
1650 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1651 Name_Elab_Body,
1652 Name_Elab_Subp_Body)
1653 then
1654 if Present (Actuals) then
1655 Error_Msg_N
1656 ("no parameters allowed for this call", First (Actuals));
1657 return;
1658 end if;
1660 Set_Etype (N, Standard_Void_Type);
1661 Set_Analyzed (N);
1663 elsif Is_Entity_Name (P)
1664 and then Is_Record_Type (Etype (Entity (P)))
1665 and then Remote_AST_I_Dereference (P)
1666 then
1667 return;
1669 elsif Is_Entity_Name (P)
1670 and then Ekind (Entity (P)) /= E_Entry_Family
1671 then
1672 if Is_Access_Type (Etype (P))
1673 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1674 and then No (Actuals)
1675 and then Comes_From_Source (N)
1676 then
1677 Error_Msg_N ("missing explicit dereference in call", N);
1678 end if;
1680 Analyze_Call_And_Resolve;
1682 -- If the prefix is the simple name of an entry family, this is
1683 -- a parameterless call from within the task body itself.
1685 elsif Is_Entity_Name (P)
1686 and then Nkind (P) = N_Identifier
1687 and then Ekind (Entity (P)) = E_Entry_Family
1688 and then Present (Actuals)
1689 and then No (Next (First (Actuals)))
1690 then
1691 -- Can be call to parameterless entry family. What appears to be the
1692 -- sole argument is in fact the entry index. Rewrite prefix of node
1693 -- accordingly. Source representation is unchanged by this
1694 -- transformation.
1696 New_N :=
1697 Make_Indexed_Component (Loc,
1698 Prefix =>
1699 Make_Selected_Component (Loc,
1700 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1701 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1702 Expressions => Actuals);
1703 Set_Name (N, New_N);
1704 Set_Etype (New_N, Standard_Void_Type);
1705 Set_Parameter_Associations (N, No_List);
1706 Analyze_Call_And_Resolve;
1708 elsif Nkind (P) = N_Explicit_Dereference then
1709 if Ekind (Etype (P)) = E_Subprogram_Type then
1710 Analyze_Call_And_Resolve;
1711 else
1712 Error_Msg_N ("expect access to procedure in call", P);
1713 end if;
1715 -- The name can be a selected component or an indexed component that
1716 -- yields an access to subprogram. Such a prefix is legal if the call
1717 -- has parameter associations.
1719 elsif Is_Access_Type (Etype (P))
1720 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1721 then
1722 if Present (Actuals) then
1723 Analyze_Call_And_Resolve;
1724 else
1725 Error_Msg_N ("missing explicit dereference in call ", N);
1726 end if;
1728 -- If not an access to subprogram, then the prefix must resolve to the
1729 -- name of an entry, entry family, or protected operation.
1731 -- For the case of a simple entry call, P is a selected component where
1732 -- the prefix is the task and the selector name is the entry. A call to
1733 -- a protected procedure will have the same syntax. If the protected
1734 -- object contains overloaded operations, the entity may appear as a
1735 -- function, the context will select the operation whose type is Void.
1737 elsif Nkind (P) = N_Selected_Component
1738 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1739 E_Procedure,
1740 E_Function)
1741 then
1742 Analyze_Call_And_Resolve;
1744 elsif Nkind (P) = N_Selected_Component
1745 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1746 and then Present (Actuals)
1747 and then No (Next (First (Actuals)))
1748 then
1749 -- Can be call to parameterless entry family. What appears to be the
1750 -- sole argument is in fact the entry index. Rewrite prefix of node
1751 -- accordingly. Source representation is unchanged by this
1752 -- transformation.
1754 New_N :=
1755 Make_Indexed_Component (Loc,
1756 Prefix => New_Copy (P),
1757 Expressions => Actuals);
1758 Set_Name (N, New_N);
1759 Set_Etype (New_N, Standard_Void_Type);
1760 Set_Parameter_Associations (N, No_List);
1761 Analyze_Call_And_Resolve;
1763 -- For the case of a reference to an element of an entry family, P is
1764 -- an indexed component whose prefix is a selected component (task and
1765 -- entry family), and whose index is the entry family index.
1767 elsif Nkind (P) = N_Indexed_Component
1768 and then Nkind (Prefix (P)) = N_Selected_Component
1769 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1770 then
1771 Analyze_Call_And_Resolve;
1773 -- If the prefix is the name of an entry family, it is a call from
1774 -- within the task body itself.
1776 elsif Nkind (P) = N_Indexed_Component
1777 and then Nkind (Prefix (P)) = N_Identifier
1778 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1779 then
1780 New_N :=
1781 Make_Selected_Component (Loc,
1782 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1783 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1784 Rewrite (Prefix (P), New_N);
1785 Analyze (P);
1786 Analyze_Call_And_Resolve;
1788 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1789 -- procedure name, so the construct can only be a qualified expression.
1791 elsif Nkind (P) = N_Qualified_Expression
1792 and then Ada_Version >= Ada_2012
1793 then
1794 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1795 Analyze (N);
1797 -- Anything else is an error
1799 else
1800 Error_Msg_N ("invalid procedure or entry call", N);
1801 end if;
1802 end Analyze_Procedure_Call;
1804 ------------------------------
1805 -- Analyze_Return_Statement --
1806 ------------------------------
1808 procedure Analyze_Return_Statement (N : Node_Id) is
1810 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1811 N_Extended_Return_Statement));
1813 Returns_Object : constant Boolean :=
1814 Nkind (N) = N_Extended_Return_Statement
1815 or else
1816 (Nkind (N) = N_Simple_Return_Statement
1817 and then Present (Expression (N)));
1818 -- True if we're returning something; that is, "return <expression>;"
1819 -- or "return Result : T [:= ...]". False for "return;". Used for error
1820 -- checking: If Returns_Object is True, N should apply to a function
1821 -- body; otherwise N should apply to a procedure body, entry body,
1822 -- accept statement, or extended return statement.
1824 function Find_What_It_Applies_To return Entity_Id;
1825 -- Find the entity representing the innermost enclosing body, accept
1826 -- statement, or extended return statement. If the result is a callable
1827 -- construct or extended return statement, then this will be the value
1828 -- of the Return_Applies_To attribute. Otherwise, the program is
1829 -- illegal. See RM-6.5(4/2).
1831 -----------------------------
1832 -- Find_What_It_Applies_To --
1833 -----------------------------
1835 function Find_What_It_Applies_To return Entity_Id is
1836 Result : Entity_Id := Empty;
1838 begin
1839 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1840 -- and postconditions.
1842 for J in reverse 0 .. Scope_Stack.Last loop
1843 Result := Scope_Stack.Table (J).Entity;
1844 exit when not Ekind_In (Result, E_Block, E_Loop)
1845 and then Chars (Result) /= Name_uPostconditions;
1846 end loop;
1848 pragma Assert (Present (Result));
1849 return Result;
1850 end Find_What_It_Applies_To;
1852 -- Local declarations
1854 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1855 Kind : constant Entity_Kind := Ekind (Scope_Id);
1856 Loc : constant Source_Ptr := Sloc (N);
1857 Stm_Entity : constant Entity_Id :=
1858 New_Internal_Entity
1859 (E_Return_Statement, Current_Scope, Loc, 'R');
1861 -- Start of processing for Analyze_Return_Statement
1863 begin
1864 Set_Return_Statement_Entity (N, Stm_Entity);
1866 Set_Etype (Stm_Entity, Standard_Void_Type);
1867 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1869 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1870 -- (4/2): an inner return statement will apply to this extended return.
1872 if Nkind (N) = N_Extended_Return_Statement then
1873 Push_Scope (Stm_Entity);
1874 end if;
1876 -- Check that pragma No_Return is obeyed. Don't complain about the
1877 -- implicitly-generated return that is placed at the end.
1879 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1880 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1881 end if;
1883 -- Warn on any unassigned OUT parameters if in procedure
1885 if Ekind (Scope_Id) = E_Procedure then
1886 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1887 end if;
1889 -- Check that functions return objects, and other things do not
1891 if Kind = E_Function or else Kind = E_Generic_Function then
1892 if not Returns_Object then
1893 Error_Msg_N ("missing expression in return from function", N);
1894 end if;
1896 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1897 if Returns_Object then
1898 Error_Msg_N ("procedure cannot return value (use function)", N);
1899 end if;
1901 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1902 if Returns_Object then
1903 if Is_Protected_Type (Scope (Scope_Id)) then
1904 Error_Msg_N ("entry body cannot return value", N);
1905 else
1906 Error_Msg_N ("accept statement cannot return value", N);
1907 end if;
1908 end if;
1910 elsif Kind = E_Return_Statement then
1912 -- We are nested within another return statement, which must be an
1913 -- extended_return_statement.
1915 if Returns_Object then
1916 if Nkind (N) = N_Extended_Return_Statement then
1917 Error_Msg_N
1918 ("extended return statement cannot be nested (use `RETURN;`)",
1921 -- Case of a simple return statement with a value inside extended
1922 -- return statement.
1924 else
1925 Error_Msg_N
1926 ("return nested in extended return statement cannot return "
1927 & "value (use `RETURN;`)", N);
1928 end if;
1929 end if;
1931 else
1932 Error_Msg_N ("illegal context for return statement", N);
1933 end if;
1935 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1936 Analyze_Function_Return (N);
1938 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1939 Set_Return_Present (Scope_Id);
1940 end if;
1942 if Nkind (N) = N_Extended_Return_Statement then
1943 End_Scope;
1944 end if;
1946 Kill_Current_Values (Last_Assignment_Only => True);
1947 Check_Unreachable_Code (N);
1949 Analyze_Dimension (N);
1950 end Analyze_Return_Statement;
1952 -------------------------------------
1953 -- Analyze_Simple_Return_Statement --
1954 -------------------------------------
1956 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1957 begin
1958 if Present (Expression (N)) then
1959 Mark_Coextensions (N, Expression (N));
1960 end if;
1962 Analyze_Return_Statement (N);
1963 end Analyze_Simple_Return_Statement;
1965 -------------------------
1966 -- Analyze_Return_Type --
1967 -------------------------
1969 procedure Analyze_Return_Type (N : Node_Id) is
1970 Designator : constant Entity_Id := Defining_Entity (N);
1971 Typ : Entity_Id := Empty;
1973 begin
1974 -- Normal case where result definition does not indicate an error
1976 if Result_Definition (N) /= Error then
1977 if Nkind (Result_Definition (N)) = N_Access_Definition then
1978 Check_SPARK_05_Restriction
1979 ("access result is not allowed", Result_Definition (N));
1981 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1983 declare
1984 AD : constant Node_Id :=
1985 Access_To_Subprogram_Definition (Result_Definition (N));
1986 begin
1987 if Present (AD) and then Protected_Present (AD) then
1988 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1989 else
1990 Typ := Access_Definition (N, Result_Definition (N));
1991 end if;
1992 end;
1994 Set_Parent (Typ, Result_Definition (N));
1995 Set_Is_Local_Anonymous_Access (Typ);
1996 Set_Etype (Designator, Typ);
1998 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2000 Null_Exclusion_Static_Checks (N);
2002 -- Subtype_Mark case
2004 else
2005 Find_Type (Result_Definition (N));
2006 Typ := Entity (Result_Definition (N));
2007 Set_Etype (Designator, Typ);
2009 -- Unconstrained array as result is not allowed in SPARK
2011 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2012 Check_SPARK_05_Restriction
2013 ("returning an unconstrained array is not allowed",
2014 Result_Definition (N));
2015 end if;
2017 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2019 Null_Exclusion_Static_Checks (N);
2021 -- If a null exclusion is imposed on the result type, then create
2022 -- a null-excluding itype (an access subtype) and use it as the
2023 -- function's Etype. Note that the null exclusion checks are done
2024 -- right before this, because they don't get applied to types that
2025 -- do not come from source.
2027 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2028 Set_Etype (Designator,
2029 Create_Null_Excluding_Itype
2030 (T => Typ,
2031 Related_Nod => N,
2032 Scope_Id => Scope (Current_Scope)));
2034 -- The new subtype must be elaborated before use because
2035 -- it is visible outside of the function. However its base
2036 -- type may not be frozen yet, so the reference that will
2037 -- force elaboration must be attached to the freezing of
2038 -- the base type.
2040 -- If the return specification appears on a proper body,
2041 -- the subtype will have been created already on the spec.
2043 if Is_Frozen (Typ) then
2044 if Nkind (Parent (N)) = N_Subprogram_Body
2045 and then Nkind (Parent (Parent (N))) = N_Subunit
2046 then
2047 null;
2048 else
2049 Build_Itype_Reference (Etype (Designator), Parent (N));
2050 end if;
2052 else
2053 Ensure_Freeze_Node (Typ);
2055 declare
2056 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2057 begin
2058 Set_Itype (IR, Etype (Designator));
2059 Append_Freeze_Actions (Typ, New_List (IR));
2060 end;
2061 end if;
2063 else
2064 Set_Etype (Designator, Typ);
2065 end if;
2067 if Ekind (Typ) = E_Incomplete_Type
2068 and then Is_Value_Type (Typ)
2069 then
2070 null;
2072 elsif Ekind (Typ) = E_Incomplete_Type
2073 or else (Is_Class_Wide_Type (Typ)
2074 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2075 then
2076 -- AI05-0151: Tagged incomplete types are allowed in all formal
2077 -- parts. Untagged incomplete types are not allowed in bodies.
2078 -- As a consequence, limited views cannot appear in a basic
2079 -- declaration that is itself within a body, because there is
2080 -- no point at which the non-limited view will become visible.
2082 if Ada_Version >= Ada_2012 then
2083 if From_Limited_With (Typ) and then In_Package_Body then
2084 Error_Msg_NE
2085 ("invalid use of incomplete type&",
2086 Result_Definition (N), Typ);
2088 -- The return type of a subprogram body cannot be of a
2089 -- formal incomplete type.
2091 elsif Is_Generic_Type (Typ)
2092 and then Nkind (Parent (N)) = N_Subprogram_Body
2093 then
2094 Error_Msg_N
2095 ("return type cannot be a formal incomplete type",
2096 Result_Definition (N));
2098 elsif Is_Class_Wide_Type (Typ)
2099 and then Is_Generic_Type (Root_Type (Typ))
2100 and then Nkind (Parent (N)) = N_Subprogram_Body
2101 then
2102 Error_Msg_N
2103 ("return type cannot be a formal incomplete type",
2104 Result_Definition (N));
2106 elsif Is_Tagged_Type (Typ) then
2107 null;
2109 -- Use is legal in a thunk generated for an operation
2110 -- inherited from a progenitor.
2112 elsif Is_Thunk (Designator)
2113 and then Present (Non_Limited_View (Typ))
2114 then
2115 null;
2117 elsif Nkind (Parent (N)) = N_Subprogram_Body
2118 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2119 N_Entry_Body)
2120 then
2121 Error_Msg_NE
2122 ("invalid use of untagged incomplete type&",
2123 Designator, Typ);
2124 end if;
2126 -- The type must be completed in the current package. This
2127 -- is checked at the end of the package declaration when
2128 -- Taft-amendment types are identified. If the return type
2129 -- is class-wide, there is no required check, the type can
2130 -- be a bona fide TAT.
2132 if Ekind (Scope (Current_Scope)) = E_Package
2133 and then In_Private_Part (Scope (Current_Scope))
2134 and then not Is_Class_Wide_Type (Typ)
2135 then
2136 Append_Elmt (Designator, Private_Dependents (Typ));
2137 end if;
2139 else
2140 Error_Msg_NE
2141 ("invalid use of incomplete type&", Designator, Typ);
2142 end if;
2143 end if;
2144 end if;
2146 -- Case where result definition does indicate an error
2148 else
2149 Set_Etype (Designator, Any_Type);
2150 end if;
2151 end Analyze_Return_Type;
2153 -----------------------------
2154 -- Analyze_Subprogram_Body --
2155 -----------------------------
2157 procedure Analyze_Subprogram_Body (N : Node_Id) is
2158 Loc : constant Source_Ptr := Sloc (N);
2159 Body_Spec : constant Node_Id := Specification (N);
2160 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2162 begin
2163 if Debug_Flag_C then
2164 Write_Str ("==> subprogram body ");
2165 Write_Name (Chars (Body_Id));
2166 Write_Str (" from ");
2167 Write_Location (Loc);
2168 Write_Eol;
2169 Indent;
2170 end if;
2172 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2174 -- The real work is split out into the helper, so it can do "return;"
2175 -- without skipping the debug output:
2177 Analyze_Subprogram_Body_Helper (N);
2179 if Debug_Flag_C then
2180 Outdent;
2181 Write_Str ("<== subprogram body ");
2182 Write_Name (Chars (Body_Id));
2183 Write_Str (" from ");
2184 Write_Location (Loc);
2185 Write_Eol;
2186 end if;
2187 end Analyze_Subprogram_Body;
2189 --------------------------------------
2190 -- Analyze_Subprogram_Body_Contract --
2191 --------------------------------------
2193 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2194 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2195 Mode : SPARK_Mode_Type;
2196 Prag : Node_Id;
2197 Ref_Depends : Node_Id := Empty;
2198 Ref_Global : Node_Id := Empty;
2199 Spec_Id : Entity_Id;
2201 begin
2202 -- Due to the timing of contract analysis, delayed pragmas may be
2203 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2204 -- context. To remedy this, restore the original SPARK_Mode of the
2205 -- related subprogram body.
2207 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2209 -- When a subprogram body declaration is illegal, its defining entity is
2210 -- left unanalyzed. There is nothing left to do in this case because the
2211 -- body lacks a contract, or even a proper Ekind.
2213 if Ekind (Body_Id) = E_Void then
2214 return;
2215 end if;
2217 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2218 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2219 else
2220 Spec_Id := Corresponding_Spec (Body_Decl);
2221 end if;
2223 -- Locate and store pragmas Refined_Depends and Refined_Global since
2224 -- their order of analysis matters.
2226 Prag := Classifications (Contract (Body_Id));
2227 while Present (Prag) loop
2228 if Pragma_Name (Prag) = Name_Refined_Depends then
2229 Ref_Depends := Prag;
2230 elsif Pragma_Name (Prag) = Name_Refined_Global then
2231 Ref_Global := Prag;
2232 end if;
2234 Prag := Next_Pragma (Prag);
2235 end loop;
2237 -- Analyze Refined_Global first as Refined_Depends may mention items
2238 -- classified in the global refinement.
2240 if Present (Ref_Global) then
2241 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2243 -- When the corresponding Global aspect/pragma references a state with
2244 -- visible refinement, the body requires Refined_Global. Refinement is
2245 -- not required when SPARK checks are suppressed.
2247 elsif Present (Spec_Id) then
2248 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2250 if SPARK_Mode /= Off
2251 and then Present (Prag)
2252 and then Contains_Refined_State (Prag)
2253 then
2254 Error_Msg_NE
2255 ("body of subprogram& requires global refinement",
2256 Body_Decl, Spec_Id);
2257 end if;
2258 end if;
2260 -- Refined_Depends must be analyzed after Refined_Global in order to see
2261 -- the modes of all global refinements.
2263 if Present (Ref_Depends) then
2264 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2266 -- When the corresponding Depends aspect/pragma references a state with
2267 -- visible refinement, the body requires Refined_Depends. Refinement is
2268 -- not required when SPARK checks are suppressed.
2270 elsif Present (Spec_Id) then
2271 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2273 if SPARK_Mode /= Off
2274 and then Present (Prag)
2275 and then Contains_Refined_State (Prag)
2276 then
2277 Error_Msg_NE
2278 ("body of subprogram& requires dependance refinement",
2279 Body_Decl, Spec_Id);
2280 end if;
2281 end if;
2283 -- Restore the SPARK_Mode of the enclosing context after all delayed
2284 -- pragmas have been analyzed.
2286 Restore_SPARK_Mode (Mode);
2287 end Analyze_Subprogram_Body_Contract;
2289 ------------------------------------
2290 -- Analyze_Subprogram_Body_Helper --
2291 ------------------------------------
2293 -- This procedure is called for regular subprogram bodies, generic bodies,
2294 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2295 -- specification matters, and is used to create a proper declaration for
2296 -- the subprogram, or to perform conformance checks.
2298 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2299 Loc : constant Source_Ptr := Sloc (N);
2300 Body_Spec : constant Node_Id := Specification (N);
2301 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2302 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2303 Conformant : Boolean;
2304 HSS : Node_Id;
2305 Prot_Typ : Entity_Id := Empty;
2306 Spec_Id : Entity_Id;
2307 Spec_Decl : Node_Id := Empty;
2309 Last_Real_Spec_Entity : Entity_Id := Empty;
2310 -- When we analyze a separate spec, the entity chain ends up containing
2311 -- the formals, as well as any itypes generated during analysis of the
2312 -- default expressions for parameters, or the arguments of associated
2313 -- precondition/postcondition pragmas (which are analyzed in the context
2314 -- of the spec since they have visibility on formals).
2316 -- These entities belong with the spec and not the body. However we do
2317 -- the analysis of the body in the context of the spec (again to obtain
2318 -- visibility to the formals), and all the entities generated during
2319 -- this analysis end up also chained to the entity chain of the spec.
2320 -- But they really belong to the body, and there is circuitry to move
2321 -- them from the spec to the body.
2323 -- However, when we do this move, we don't want to move the real spec
2324 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2325 -- variable points to the last real spec entity, so we only move those
2326 -- chained beyond that point. It is initialized to Empty to deal with
2327 -- the case where there is no separate spec.
2329 procedure Analyze_Aspects_On_Body_Or_Stub;
2330 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2331 -- assumed that N has aspects.
2333 function Body_Has_Contract return Boolean;
2334 -- Check whether unanalyzed body has an aspect or pragma that may
2335 -- generate a SPARK contract.
2337 procedure Check_Anonymous_Return;
2338 -- Ada 2005: if a function returns an access type that denotes a task,
2339 -- or a type that contains tasks, we must create a master entity for
2340 -- the anonymous type, which typically will be used in an allocator
2341 -- in the body of the function.
2343 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2344 -- Look ahead to recognize a pragma that may appear after the body.
2345 -- If there is a previous spec, check that it appears in the same
2346 -- declarative part. If the pragma is Inline_Always, perform inlining
2347 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2348 -- If the body acts as a spec, and inlining is required, we create a
2349 -- subprogram declaration for it, in order to attach the body to inline.
2350 -- If pragma does not appear after the body, check whether there is
2351 -- an inline pragma before any local declarations.
2353 procedure Check_Missing_Return;
2354 -- Checks for a function with a no return statements, and also performs
2355 -- the warning checks implemented by Check_Returns. In formal mode, also
2356 -- verify that a function ends with a RETURN and that a procedure does
2357 -- not contain any RETURN.
2359 function Disambiguate_Spec return Entity_Id;
2360 -- When a primitive is declared between the private view and the full
2361 -- view of a concurrent type which implements an interface, a special
2362 -- mechanism is used to find the corresponding spec of the primitive
2363 -- body.
2365 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2366 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2367 -- incomplete types coming from a limited context and swap their limited
2368 -- views with the non-limited ones.
2370 function Is_Private_Concurrent_Primitive
2371 (Subp_Id : Entity_Id) return Boolean;
2372 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2373 -- type that implements an interface and has a private view.
2375 procedure Set_Trivial_Subprogram (N : Node_Id);
2376 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2377 -- subprogram whose body is being analyzed. N is the statement node
2378 -- causing the flag to be set, if the following statement is a return
2379 -- of an entity, we mark the entity as set in source to suppress any
2380 -- warning on the stylized use of function stubs with a dummy return.
2382 procedure Verify_Overriding_Indicator;
2383 -- If there was a previous spec, the entity has been entered in the
2384 -- current scope previously. If the body itself carries an overriding
2385 -- indicator, check that it is consistent with the known status of the
2386 -- entity.
2388 -------------------------------------
2389 -- Analyze_Aspects_On_Body_Or_Stub --
2390 -------------------------------------
2392 procedure Analyze_Aspects_On_Body_Or_Stub is
2393 procedure Diagnose_Misplaced_Aspects;
2394 -- Subprogram body [stub] N has aspects, but they are not properly
2395 -- placed. Provide precise diagnostics depending on the aspects
2396 -- involved.
2398 --------------------------------
2399 -- Diagnose_Misplaced_Aspects --
2400 --------------------------------
2402 procedure Diagnose_Misplaced_Aspects is
2403 Asp : Node_Id;
2404 Asp_Nam : Name_Id;
2405 Asp_Id : Aspect_Id;
2406 -- The current aspect along with its name and id
2408 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2409 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2410 -- the name of the refined version of the aspect.
2412 ------------------------
2413 -- SPARK_Aspect_Error --
2414 ------------------------
2416 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2417 begin
2418 -- The corresponding spec already contains the aspect in
2419 -- question and the one appearing on the body must be the
2420 -- refined form:
2422 -- procedure P with Global ...;
2423 -- procedure P with Global ... is ... end P;
2424 -- ^
2425 -- Refined_Global
2427 if Has_Aspect (Spec_Id, Asp_Id) then
2428 Error_Msg_Name_1 := Asp_Nam;
2430 -- Subunits cannot carry aspects that apply to a subprogram
2431 -- declaration.
2433 if Nkind (Parent (N)) = N_Subunit then
2434 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2436 else
2437 Error_Msg_Name_2 := Ref_Nam;
2438 Error_Msg_N ("aspect % should be %", Asp);
2439 end if;
2441 -- Otherwise the aspect must appear in the spec, not in the
2442 -- body:
2444 -- procedure P;
2445 -- procedure P with Global ... is ... end P;
2447 else
2448 Error_Msg_N
2449 ("aspect specification must appear in subprogram "
2450 & "declaration", Asp);
2451 end if;
2452 end SPARK_Aspect_Error;
2454 -- Start of processing for Diagnose_Misplaced_Aspects
2456 begin
2457 -- Iterate over the aspect specifications and emit specific errors
2458 -- where applicable.
2460 Asp := First (Aspect_Specifications (N));
2461 while Present (Asp) loop
2462 Asp_Nam := Chars (Identifier (Asp));
2463 Asp_Id := Get_Aspect_Id (Asp_Nam);
2465 -- Do not emit errors on aspects that can appear on a
2466 -- subprogram body. This scenario occurs when the aspect
2467 -- specification list contains both misplaced and properly
2468 -- placed aspects.
2470 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2471 null;
2473 -- Special diagnostics for SPARK aspects
2475 elsif Asp_Nam = Name_Depends then
2476 SPARK_Aspect_Error (Name_Refined_Depends);
2478 elsif Asp_Nam = Name_Global then
2479 SPARK_Aspect_Error (Name_Refined_Global);
2481 elsif Asp_Nam = Name_Post then
2482 SPARK_Aspect_Error (Name_Refined_Post);
2484 else
2485 Error_Msg_N
2486 ("aspect specification must appear in subprogram "
2487 & "declaration", Asp);
2488 end if;
2490 Next (Asp);
2491 end loop;
2492 end Diagnose_Misplaced_Aspects;
2494 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2496 begin
2497 -- Language-defined aspects cannot be associated with a subprogram
2498 -- body [stub] if the subprogram has a spec. Certain implementation
2499 -- defined aspects are allowed to break this rule (for list, see
2500 -- table Aspect_On_Body_Or_Stub_OK).
2502 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2503 Diagnose_Misplaced_Aspects;
2504 else
2505 Analyze_Aspect_Specifications (N, Body_Id);
2506 end if;
2507 end Analyze_Aspects_On_Body_Or_Stub;
2509 -----------------------
2510 -- Body_Has_Contract --
2511 -----------------------
2513 function Body_Has_Contract return Boolean is
2514 Decls : constant List_Id := Declarations (N);
2515 A_Spec : Node_Id;
2516 A : Aspect_Id;
2517 Decl : Node_Id;
2518 P_Id : Pragma_Id;
2520 begin
2521 -- Check for unanalyzed aspects in the body that will
2522 -- generate a contract.
2524 if Present (Aspect_Specifications (N)) then
2525 A_Spec := First (Aspect_Specifications (N));
2526 while Present (A_Spec) loop
2527 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2529 if A = Aspect_Contract_Cases or else
2530 A = Aspect_Depends or else
2531 A = Aspect_Global or else
2532 A = Aspect_Pre or else
2533 A = Aspect_Precondition or else
2534 A = Aspect_Post or else
2535 A = Aspect_Postcondition
2536 then
2537 return True;
2538 end if;
2540 Next (A_Spec);
2541 end loop;
2542 end if;
2544 -- Check for pragmas that may generate a contract
2546 if Present (Decls) then
2547 Decl := First (Decls);
2548 while Present (Decl) loop
2549 if Nkind (Decl) = N_Pragma then
2550 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2552 if P_Id = Pragma_Contract_Cases or else
2553 P_Id = Pragma_Depends or else
2554 P_Id = Pragma_Global or else
2555 P_Id = Pragma_Pre or else
2556 P_Id = Pragma_Precondition or else
2557 P_Id = Pragma_Post or else
2558 P_Id = Pragma_Postcondition
2559 then
2560 return True;
2561 end if;
2562 end if;
2564 Next (Decl);
2565 end loop;
2566 end if;
2568 return False;
2569 end Body_Has_Contract;
2571 ----------------------------
2572 -- Check_Anonymous_Return --
2573 ----------------------------
2575 procedure Check_Anonymous_Return is
2576 Decl : Node_Id;
2577 Par : Node_Id;
2578 Scop : Entity_Id;
2580 begin
2581 if Present (Spec_Id) then
2582 Scop := Spec_Id;
2583 else
2584 Scop := Body_Id;
2585 end if;
2587 if Ekind (Scop) = E_Function
2588 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2589 and then not Is_Thunk (Scop)
2591 -- Skip internally built functions which handle the case of
2592 -- a null access (see Expand_Interface_Conversion)
2594 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2595 and then not Comes_From_Source (Parent (Scop)))
2597 and then (Has_Task (Designated_Type (Etype (Scop)))
2598 or else
2599 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2600 and then
2601 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2602 and then Expander_Active
2604 -- Avoid cases with no tasking support
2606 and then RTE_Available (RE_Current_Master)
2607 and then not Restriction_Active (No_Task_Hierarchy)
2608 then
2609 Decl :=
2610 Make_Object_Declaration (Loc,
2611 Defining_Identifier =>
2612 Make_Defining_Identifier (Loc, Name_uMaster),
2613 Constant_Present => True,
2614 Object_Definition =>
2615 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2616 Expression =>
2617 Make_Explicit_Dereference (Loc,
2618 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2620 if Present (Declarations (N)) then
2621 Prepend (Decl, Declarations (N));
2622 else
2623 Set_Declarations (N, New_List (Decl));
2624 end if;
2626 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2627 Set_Has_Master_Entity (Scop);
2629 -- Now mark the containing scope as a task master
2631 Par := N;
2632 while Nkind (Par) /= N_Compilation_Unit loop
2633 Par := Parent (Par);
2634 pragma Assert (Present (Par));
2636 -- If we fall off the top, we are at the outer level, and
2637 -- the environment task is our effective master, so nothing
2638 -- to mark.
2640 if Nkind_In
2641 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2642 then
2643 Set_Is_Task_Master (Par, True);
2644 exit;
2645 end if;
2646 end loop;
2647 end if;
2648 end Check_Anonymous_Return;
2650 -------------------------
2651 -- Check_Inline_Pragma --
2652 -------------------------
2654 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2655 Prag : Node_Id;
2656 Plist : List_Id;
2658 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2659 -- True when N is a pragma Inline or Inline_Always that applies
2660 -- to this subprogram.
2662 -----------------------
2663 -- Is_Inline_Pragma --
2664 -----------------------
2666 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2667 begin
2668 return
2669 Nkind (N) = N_Pragma
2670 and then
2671 (Pragma_Name (N) = Name_Inline_Always
2672 or else (Front_End_Inlining
2673 and then Pragma_Name (N) = Name_Inline))
2674 and then
2675 Chars
2676 (Expression (First (Pragma_Argument_Associations (N)))) =
2677 Chars (Body_Id);
2678 end Is_Inline_Pragma;
2680 -- Start of processing for Check_Inline_Pragma
2682 begin
2683 if not Expander_Active then
2684 return;
2685 end if;
2687 if Is_List_Member (N)
2688 and then Present (Next (N))
2689 and then Is_Inline_Pragma (Next (N))
2690 then
2691 Prag := Next (N);
2693 elsif Nkind (N) /= N_Subprogram_Body_Stub
2694 and then Present (Declarations (N))
2695 and then Is_Inline_Pragma (First (Declarations (N)))
2696 then
2697 Prag := First (Declarations (N));
2699 else
2700 Prag := Empty;
2701 end if;
2703 if Present (Prag) then
2704 if Present (Spec_Id) then
2705 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2706 Analyze (Prag);
2707 end if;
2709 else
2710 -- Create a subprogram declaration, to make treatment uniform
2712 declare
2713 Subp : constant Entity_Id :=
2714 Make_Defining_Identifier (Loc, Chars (Body_Id));
2715 Decl : constant Node_Id :=
2716 Make_Subprogram_Declaration (Loc,
2717 Specification =>
2718 New_Copy_Tree (Specification (N)));
2720 begin
2721 Set_Defining_Unit_Name (Specification (Decl), Subp);
2723 if Present (First_Formal (Body_Id)) then
2724 Plist := Copy_Parameter_List (Body_Id);
2725 Set_Parameter_Specifications
2726 (Specification (Decl), Plist);
2727 end if;
2729 Insert_Before (N, Decl);
2730 Analyze (Decl);
2731 Analyze (Prag);
2732 Set_Has_Pragma_Inline (Subp);
2734 if Pragma_Name (Prag) = Name_Inline_Always then
2735 Set_Is_Inlined (Subp);
2736 Set_Has_Pragma_Inline_Always (Subp);
2737 end if;
2739 -- Prior to copying the subprogram body to create a template
2740 -- for it for subsequent inlining, remove the pragma from
2741 -- the current body so that the copy that will produce the
2742 -- new body will start from a completely unanalyzed tree.
2744 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2745 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2746 end if;
2748 Spec := Subp;
2749 end;
2750 end if;
2751 end if;
2752 end Check_Inline_Pragma;
2754 --------------------------
2755 -- Check_Missing_Return --
2756 --------------------------
2758 procedure Check_Missing_Return is
2759 Id : Entity_Id;
2760 Missing_Ret : Boolean;
2762 begin
2763 if Nkind (Body_Spec) = N_Function_Specification then
2764 if Present (Spec_Id) then
2765 Id := Spec_Id;
2766 else
2767 Id := Body_Id;
2768 end if;
2770 if Return_Present (Id) then
2771 Check_Returns (HSS, 'F', Missing_Ret);
2773 if Missing_Ret then
2774 Set_Has_Missing_Return (Id);
2775 end if;
2777 elsif Is_Generic_Subprogram (Id)
2778 or else not Is_Machine_Code_Subprogram (Id)
2779 then
2780 Error_Msg_N ("missing RETURN statement in function body", N);
2781 end if;
2783 -- If procedure with No_Return, check returns
2785 elsif Nkind (Body_Spec) = N_Procedure_Specification
2786 and then Present (Spec_Id)
2787 and then No_Return (Spec_Id)
2788 then
2789 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2790 end if;
2792 -- Special checks in SPARK mode
2794 if Nkind (Body_Spec) = N_Function_Specification then
2796 -- In SPARK mode, last statement of a function should be a return
2798 declare
2799 Stat : constant Node_Id := Last_Source_Statement (HSS);
2800 begin
2801 if Present (Stat)
2802 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2803 N_Extended_Return_Statement)
2804 then
2805 Check_SPARK_05_Restriction
2806 ("last statement in function should be RETURN", Stat);
2807 end if;
2808 end;
2810 -- In SPARK mode, verify that a procedure has no return
2812 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2813 if Present (Spec_Id) then
2814 Id := Spec_Id;
2815 else
2816 Id := Body_Id;
2817 end if;
2819 -- Would be nice to point to return statement here, can we
2820 -- borrow the Check_Returns procedure here ???
2822 if Return_Present (Id) then
2823 Check_SPARK_05_Restriction
2824 ("procedure should not have RETURN", N);
2825 end if;
2826 end if;
2827 end Check_Missing_Return;
2829 -----------------------
2830 -- Disambiguate_Spec --
2831 -----------------------
2833 function Disambiguate_Spec return Entity_Id is
2834 Priv_Spec : Entity_Id;
2835 Spec_N : Entity_Id;
2837 procedure Replace_Types (To_Corresponding : Boolean);
2838 -- Depending on the flag, replace the type of formal parameters of
2839 -- Body_Id if it is a concurrent type implementing interfaces with
2840 -- the corresponding record type or the other way around.
2842 procedure Replace_Types (To_Corresponding : Boolean) is
2843 Formal : Entity_Id;
2844 Formal_Typ : Entity_Id;
2846 begin
2847 Formal := First_Formal (Body_Id);
2848 while Present (Formal) loop
2849 Formal_Typ := Etype (Formal);
2851 if Is_Class_Wide_Type (Formal_Typ) then
2852 Formal_Typ := Root_Type (Formal_Typ);
2853 end if;
2855 -- From concurrent type to corresponding record
2857 if To_Corresponding then
2858 if Is_Concurrent_Type (Formal_Typ)
2859 and then Present (Corresponding_Record_Type (Formal_Typ))
2860 and then
2861 Present (Interfaces
2862 (Corresponding_Record_Type (Formal_Typ)))
2863 then
2864 Set_Etype (Formal,
2865 Corresponding_Record_Type (Formal_Typ));
2866 end if;
2868 -- From corresponding record to concurrent type
2870 else
2871 if Is_Concurrent_Record_Type (Formal_Typ)
2872 and then Present (Interfaces (Formal_Typ))
2873 then
2874 Set_Etype (Formal,
2875 Corresponding_Concurrent_Type (Formal_Typ));
2876 end if;
2877 end if;
2879 Next_Formal (Formal);
2880 end loop;
2881 end Replace_Types;
2883 -- Start of processing for Disambiguate_Spec
2885 begin
2886 -- Try to retrieve the specification of the body as is. All error
2887 -- messages are suppressed because the body may not have a spec in
2888 -- its current state.
2890 Spec_N := Find_Corresponding_Spec (N, False);
2892 -- It is possible that this is the body of a primitive declared
2893 -- between a private and a full view of a concurrent type. The
2894 -- controlling parameter of the spec carries the concurrent type,
2895 -- not the corresponding record type as transformed by Analyze_
2896 -- Subprogram_Specification. In such cases, we undo the change
2897 -- made by the analysis of the specification and try to find the
2898 -- spec again.
2900 -- Note that wrappers already have their corresponding specs and
2901 -- bodies set during their creation, so if the candidate spec is
2902 -- a wrapper, then we definitely need to swap all types to their
2903 -- original concurrent status.
2905 if No (Spec_N)
2906 or else Is_Primitive_Wrapper (Spec_N)
2907 then
2908 -- Restore all references of corresponding record types to the
2909 -- original concurrent types.
2911 Replace_Types (To_Corresponding => False);
2912 Priv_Spec := Find_Corresponding_Spec (N, False);
2914 -- The current body truly belongs to a primitive declared between
2915 -- a private and a full view. We leave the modified body as is,
2916 -- and return the true spec.
2918 if Present (Priv_Spec)
2919 and then Is_Private_Primitive (Priv_Spec)
2920 then
2921 return Priv_Spec;
2922 end if;
2924 -- In case that this is some sort of error, restore the original
2925 -- state of the body.
2927 Replace_Types (To_Corresponding => True);
2928 end if;
2930 return Spec_N;
2931 end Disambiguate_Spec;
2933 ----------------------------
2934 -- Exchange_Limited_Views --
2935 ----------------------------
2937 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2938 procedure Detect_And_Exchange (Id : Entity_Id);
2939 -- Determine whether Id's type denotes an incomplete type associated
2940 -- with a limited with clause and exchange the limited view with the
2941 -- non-limited one.
2943 -------------------------
2944 -- Detect_And_Exchange --
2945 -------------------------
2947 procedure Detect_And_Exchange (Id : Entity_Id) is
2948 Typ : constant Entity_Id := Etype (Id);
2950 begin
2951 if Ekind (Typ) = E_Incomplete_Type
2952 and then From_Limited_With (Typ)
2953 and then Present (Non_Limited_View (Typ))
2954 then
2955 Set_Etype (Id, Non_Limited_View (Typ));
2956 end if;
2957 end Detect_And_Exchange;
2959 -- Local variables
2961 Formal : Entity_Id;
2963 -- Start of processing for Exchange_Limited_Views
2965 begin
2966 if No (Subp_Id) then
2967 return;
2969 -- Do not process subprogram bodies as they already use the non-
2970 -- limited view of types.
2972 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2973 return;
2974 end if;
2976 -- Examine all formals and swap views when applicable
2978 Formal := First_Formal (Subp_Id);
2979 while Present (Formal) loop
2980 Detect_And_Exchange (Formal);
2982 Next_Formal (Formal);
2983 end loop;
2985 -- Process the return type of a function
2987 if Ekind (Subp_Id) = E_Function then
2988 Detect_And_Exchange (Subp_Id);
2989 end if;
2990 end Exchange_Limited_Views;
2992 -------------------------------------
2993 -- Is_Private_Concurrent_Primitive --
2994 -------------------------------------
2996 function Is_Private_Concurrent_Primitive
2997 (Subp_Id : Entity_Id) return Boolean
2999 Formal_Typ : Entity_Id;
3001 begin
3002 if Present (First_Formal (Subp_Id)) then
3003 Formal_Typ := Etype (First_Formal (Subp_Id));
3005 if Is_Concurrent_Record_Type (Formal_Typ) then
3006 if Is_Class_Wide_Type (Formal_Typ) then
3007 Formal_Typ := Root_Type (Formal_Typ);
3008 end if;
3010 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3011 end if;
3013 -- The type of the first formal is a concurrent tagged type with
3014 -- a private view.
3016 return
3017 Is_Concurrent_Type (Formal_Typ)
3018 and then Is_Tagged_Type (Formal_Typ)
3019 and then Has_Private_Declaration (Formal_Typ);
3020 end if;
3022 return False;
3023 end Is_Private_Concurrent_Primitive;
3025 ----------------------------
3026 -- Set_Trivial_Subprogram --
3027 ----------------------------
3029 procedure Set_Trivial_Subprogram (N : Node_Id) is
3030 Nxt : constant Node_Id := Next (N);
3032 begin
3033 Set_Is_Trivial_Subprogram (Body_Id);
3035 if Present (Spec_Id) then
3036 Set_Is_Trivial_Subprogram (Spec_Id);
3037 end if;
3039 if Present (Nxt)
3040 and then Nkind (Nxt) = N_Simple_Return_Statement
3041 and then No (Next (Nxt))
3042 and then Present (Expression (Nxt))
3043 and then Is_Entity_Name (Expression (Nxt))
3044 then
3045 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3046 end if;
3047 end Set_Trivial_Subprogram;
3049 ---------------------------------
3050 -- Verify_Overriding_Indicator --
3051 ---------------------------------
3053 procedure Verify_Overriding_Indicator is
3054 begin
3055 if Must_Override (Body_Spec) then
3056 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3057 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3058 then
3059 null;
3061 elsif not Present (Overridden_Operation (Spec_Id)) then
3062 Error_Msg_NE
3063 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3065 -- Overriding indicators aren't allowed for protected subprogram
3066 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3067 -- this to a warning if -gnatd.E is enabled.
3069 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3070 Error_Msg_Warn := Error_To_Warning;
3071 Error_Msg_N
3072 ("<<overriding indicator not allowed for protected "
3073 & "subprogram body", Body_Spec);
3074 end if;
3076 elsif Must_Not_Override (Body_Spec) then
3077 if Present (Overridden_Operation (Spec_Id)) then
3078 Error_Msg_NE
3079 ("subprogram& overrides inherited operation",
3080 Body_Spec, Spec_Id);
3082 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3083 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3084 then
3085 Error_Msg_NE
3086 ("subprogram& overrides predefined operator ",
3087 Body_Spec, Spec_Id);
3089 -- Overriding indicators aren't allowed for protected subprogram
3090 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3091 -- this to a warning if -gnatd.E is enabled.
3093 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3094 Error_Msg_Warn := Error_To_Warning;
3096 Error_Msg_N
3097 ("<<overriding indicator not allowed "
3098 & "for protected subprogram body", Body_Spec);
3100 -- If this is not a primitive operation, then the overriding
3101 -- indicator is altogether illegal.
3103 elsif not Is_Primitive (Spec_Id) then
3104 Error_Msg_N
3105 ("overriding indicator only allowed "
3106 & "if subprogram is primitive", Body_Spec);
3107 end if;
3109 -- If checking the style rule and the operation overrides, then
3110 -- issue a warning about a missing overriding_indicator. Protected
3111 -- subprogram bodies are excluded from this style checking, since
3112 -- they aren't primitives (even though their declarations can
3113 -- override) and aren't allowed to have an overriding_indicator.
3115 elsif Style_Check
3116 and then Present (Overridden_Operation (Spec_Id))
3117 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3118 then
3119 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3120 Style.Missing_Overriding (N, Body_Id);
3122 elsif Style_Check
3123 and then Can_Override_Operator (Spec_Id)
3124 and then not Is_Predefined_File_Name
3125 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3126 then
3127 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3128 Style.Missing_Overriding (N, Body_Id);
3129 end if;
3130 end Verify_Overriding_Indicator;
3132 -- Start of processing for Analyze_Subprogram_Body_Helper
3134 begin
3135 -- Generic subprograms are handled separately. They always have a
3136 -- generic specification. Determine whether current scope has a
3137 -- previous declaration.
3139 -- If the subprogram body is defined within an instance of the same
3140 -- name, the instance appears as a package renaming, and will be hidden
3141 -- within the subprogram.
3143 if Present (Prev_Id)
3144 and then not Is_Overloadable (Prev_Id)
3145 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3146 or else Comes_From_Source (Prev_Id))
3147 then
3148 if Is_Generic_Subprogram (Prev_Id) then
3149 Spec_Id := Prev_Id;
3151 -- The corresponding spec may be subject to pragma Ghost with
3152 -- policy Ignore. Set the mode now to ensure that any nodes
3153 -- generated during analysis and expansion are properly flagged
3154 -- as ignored Ghost.
3156 Set_Ghost_Mode (N, Spec_Id);
3157 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3158 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3160 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3162 if Nkind (N) = N_Subprogram_Body then
3163 HSS := Handled_Statement_Sequence (N);
3164 Check_Missing_Return;
3165 end if;
3167 return;
3169 else
3170 -- Previous entity conflicts with subprogram name. Attempting to
3171 -- enter name will post error.
3173 Enter_Name (Body_Id);
3174 return;
3175 end if;
3177 -- Non-generic case, find the subprogram declaration, if one was seen,
3178 -- or enter new overloaded entity in the current scope. If the
3179 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3180 -- part of the context of one of its subunits. No need to redo the
3181 -- analysis.
3183 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3184 return;
3186 else
3187 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3189 if Nkind (N) = N_Subprogram_Body_Stub
3190 or else No (Corresponding_Spec (N))
3191 then
3192 if Is_Private_Concurrent_Primitive (Body_Id) then
3193 Spec_Id := Disambiguate_Spec;
3195 -- The corresponding spec may be subject to pragma Ghost with
3196 -- policy Ignore. Set the mode now to ensure that any nodes
3197 -- generated during analysis and expansion are properly flagged
3198 -- as ignored Ghost.
3200 Set_Ghost_Mode (N, Spec_Id);
3202 else
3203 Spec_Id := Find_Corresponding_Spec (N);
3205 -- The corresponding spec may be subject to pragma Ghost with
3206 -- policy Ignore. Set the mode now to ensure that any nodes
3207 -- generated during analysis and expansion are properly flagged
3208 -- as ignored Ghost.
3210 Set_Ghost_Mode (N, Spec_Id);
3212 -- In GNATprove mode, if the body has no previous spec, create
3213 -- one so that the inlining machinery can operate properly.
3214 -- Transfer aspects, if any, to the new spec, so that they
3215 -- are legal and can be processed ahead of the body.
3216 -- We make two copies of the given spec, one for the new
3217 -- declaration, and one for the body.
3219 if No (Spec_Id)
3220 and then GNATprove_Mode
3222 -- Inlining does not apply during pre-analysis of code
3224 and then Full_Analysis
3226 -- Inlining only applies to full bodies, not stubs
3228 and then Nkind (N) /= N_Subprogram_Body_Stub
3230 -- Inlining only applies to bodies in the source code, not to
3231 -- those generated by the compiler. In particular, expression
3232 -- functions, whose body is generated by the compiler, are
3233 -- treated specially by GNATprove.
3235 and then Comes_From_Source (Body_Id)
3237 -- This cannot be done for a compilation unit, which is not
3238 -- in a context where we can insert a new spec.
3240 and then Is_List_Member (N)
3242 -- Inlining only applies to subprograms without contracts,
3243 -- as a contract is a sign that GNATprove should perform a
3244 -- modular analysis of the subprogram instead of a contextual
3245 -- analysis at each call site. The same test is performed in
3246 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3247 -- here in another form (because the contract has not
3248 -- been attached to the body) to avoid frontend errors in
3249 -- case pragmas are used instead of aspects, because the
3250 -- corresponding pragmas in the body would not be transferred
3251 -- to the spec, leading to legality errors.
3253 and then not Body_Has_Contract
3254 then
3255 declare
3256 Body_Spec : constant Node_Id :=
3257 Copy_Separate_Tree (Specification (N));
3258 New_Decl : constant Node_Id :=
3259 Make_Subprogram_Declaration (Loc,
3260 Copy_Separate_Tree (Specification (N)));
3262 SPARK_Mode_Aspect : Node_Id;
3263 Aspects : List_Id;
3264 Prag, Aspect : Node_Id;
3266 begin
3267 Insert_Before (N, New_Decl);
3268 Move_Aspects (From => N, To => New_Decl);
3270 -- Mark the newly moved aspects as not analyzed, so that
3271 -- their effect on New_Decl is properly analyzed.
3273 Aspect := First (Aspect_Specifications (New_Decl));
3274 while Present (Aspect) loop
3275 Set_Analyzed (Aspect, False);
3276 Next (Aspect);
3277 end loop;
3279 Analyze (New_Decl);
3281 -- The analysis of the generated subprogram declaration
3282 -- may have introduced pragmas that need to be analyzed.
3284 Prag := Next (New_Decl);
3285 while Prag /= N loop
3286 Analyze (Prag);
3287 Next (Prag);
3288 end loop;
3290 Spec_Id := Defining_Entity (New_Decl);
3292 -- As Body_Id originally comes from source, mark the new
3293 -- Spec_Id as such, which is required so that calls to
3294 -- this subprogram are registered in the local effects
3295 -- stored in ALI files for GNATprove.
3297 Set_Comes_From_Source (Spec_Id, True);
3299 -- If aspect SPARK_Mode was specified on the body, it
3300 -- needs to be repeated on the generated decl and the
3301 -- body. Since the original aspect was moved to the
3302 -- generated decl, copy it for the body.
3304 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3305 SPARK_Mode_Aspect :=
3306 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3307 Set_Analyzed (SPARK_Mode_Aspect, False);
3308 Aspects := New_List (SPARK_Mode_Aspect);
3309 Set_Aspect_Specifications (N, Aspects);
3310 end if;
3312 Set_Specification (N, Body_Spec);
3313 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3314 Set_Corresponding_Spec (N, Spec_Id);
3315 end;
3316 end if;
3317 end if;
3319 -- If this is a duplicate body, no point in analyzing it
3321 if Error_Posted (N) then
3322 return;
3323 end if;
3325 -- A subprogram body should cause freezing of its own declaration,
3326 -- but if there was no previous explicit declaration, then the
3327 -- subprogram will get frozen too late (there may be code within
3328 -- the body that depends on the subprogram having been frozen,
3329 -- such as uses of extra formals), so we force it to be frozen
3330 -- here. Same holds if the body and spec are compilation units.
3331 -- Finally, if the return type is an anonymous access to protected
3332 -- subprogram, it must be frozen before the body because its
3333 -- expansion has generated an equivalent type that is used when
3334 -- elaborating the body.
3336 -- An exception in the case of Ada 2012, AI05-177: The bodies
3337 -- created for expression functions do not freeze.
3339 if No (Spec_Id)
3340 and then Nkind (Original_Node (N)) /= N_Expression_Function
3341 then
3342 Freeze_Before (N, Body_Id);
3344 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3345 Freeze_Before (N, Spec_Id);
3347 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3348 Freeze_Before (N, Etype (Body_Id));
3349 end if;
3351 else
3352 Spec_Id := Corresponding_Spec (N);
3354 -- The corresponding spec may be subject to pragma Ghost with
3355 -- policy Ignore. Set the mode now to ensure that any nodes
3356 -- generated during analysis and expansion are properly flagged
3357 -- as ignored Ghost.
3359 Set_Ghost_Mode (N, Spec_Id);
3360 end if;
3361 end if;
3363 -- Previously we scanned the body to look for nested subprograms, and
3364 -- rejected an inline directive if nested subprograms were present,
3365 -- because the back-end would generate conflicting symbols for the
3366 -- nested bodies. This is now unnecessary.
3368 -- Look ahead to recognize a pragma Inline that appears after the body
3370 Check_Inline_Pragma (Spec_Id);
3372 -- Deal with special case of a fully private operation in the body of
3373 -- the protected type. We must create a declaration for the subprogram,
3374 -- in order to attach the protected subprogram that will be used in
3375 -- internal calls. We exclude compiler generated bodies from the
3376 -- expander since the issue does not arise for those cases.
3378 if No (Spec_Id)
3379 and then Comes_From_Source (N)
3380 and then Is_Protected_Type (Current_Scope)
3381 then
3382 Spec_Id := Build_Private_Protected_Declaration (N);
3383 end if;
3385 -- If a separate spec is present, then deal with freezing issues
3387 if Present (Spec_Id) then
3388 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3389 Verify_Overriding_Indicator;
3391 -- In general, the spec will be frozen when we start analyzing the
3392 -- body. However, for internally generated operations, such as
3393 -- wrapper functions for inherited operations with controlling
3394 -- results, the spec may not have been frozen by the time we expand
3395 -- the freeze actions that include the bodies. In particular, extra
3396 -- formals for accessibility or for return-in-place may need to be
3397 -- generated. Freeze nodes, if any, are inserted before the current
3398 -- body. These freeze actions are also needed in ASIS mode to enable
3399 -- the proper back-annotations.
3401 if not Is_Frozen (Spec_Id)
3402 and then (Expander_Active or ASIS_Mode)
3403 then
3404 -- Force the generation of its freezing node to ensure proper
3405 -- management of access types in the backend.
3407 -- This is definitely needed for some cases, but it is not clear
3408 -- why, to be investigated further???
3410 Set_Has_Delayed_Freeze (Spec_Id);
3411 Freeze_Before (N, Spec_Id);
3412 end if;
3413 end if;
3415 -- Mark presence of postcondition procedure in current scope and mark
3416 -- the procedure itself as needing debug info. The latter is important
3417 -- when analyzing decision coverage (for example, for MC/DC coverage).
3419 if Chars (Body_Id) = Name_uPostconditions then
3420 Set_Has_Postconditions (Current_Scope);
3421 Set_Debug_Info_Needed (Body_Id);
3422 end if;
3424 -- Place subprogram on scope stack, and make formals visible. If there
3425 -- is a spec, the visible entity remains that of the spec.
3427 if Present (Spec_Id) then
3428 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3430 if Is_Child_Unit (Spec_Id) then
3431 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3432 end if;
3434 if Style_Check then
3435 Style.Check_Identifier (Body_Id, Spec_Id);
3436 end if;
3438 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3439 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3441 if Is_Abstract_Subprogram (Spec_Id) then
3442 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3443 return;
3445 else
3446 Set_Convention (Body_Id, Convention (Spec_Id));
3447 Set_Has_Completion (Spec_Id);
3449 -- Inherit the "ghostness" of the subprogram spec. Note that this
3450 -- property is not directly inherited as the body may be subject
3451 -- to a different Ghost assertion policy.
3453 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
3454 Set_Is_Ghost_Entity (Body_Id);
3456 -- The Ghost policy in effect at the point of declaration and
3457 -- at the point of completion must match (SPARK RM 6.9(15)).
3459 Check_Ghost_Completion (Spec_Id, Body_Id);
3460 end if;
3462 if Is_Protected_Type (Scope (Spec_Id)) then
3463 Prot_Typ := Scope (Spec_Id);
3464 end if;
3466 -- If this is a body generated for a renaming, do not check for
3467 -- full conformance. The check is redundant, because the spec of
3468 -- the body is a copy of the spec in the renaming declaration,
3469 -- and the test can lead to spurious errors on nested defaults.
3471 if Present (Spec_Decl)
3472 and then not Comes_From_Source (N)
3473 and then
3474 (Nkind (Original_Node (Spec_Decl)) =
3475 N_Subprogram_Renaming_Declaration
3476 or else (Present (Corresponding_Body (Spec_Decl))
3477 and then
3478 Nkind (Unit_Declaration_Node
3479 (Corresponding_Body (Spec_Decl))) =
3480 N_Subprogram_Renaming_Declaration))
3481 then
3482 Conformant := True;
3484 -- Conversely, the spec may have been generated for specless body
3485 -- with an inline pragma.
3487 elsif Comes_From_Source (N)
3488 and then not Comes_From_Source (Spec_Id)
3489 and then Has_Pragma_Inline (Spec_Id)
3490 then
3491 Conformant := True;
3493 else
3494 Check_Conformance
3495 (Body_Id, Spec_Id,
3496 Fully_Conformant, True, Conformant, Body_Id);
3497 end if;
3499 -- If the body is not fully conformant, we have to decide if we
3500 -- should analyze it or not. If it has a really messed up profile
3501 -- then we probably should not analyze it, since we will get too
3502 -- many bogus messages.
3504 -- Our decision is to go ahead in the non-fully conformant case
3505 -- only if it is at least mode conformant with the spec. Note
3506 -- that the call to Check_Fully_Conformant has issued the proper
3507 -- error messages to complain about the lack of conformance.
3509 if not Conformant
3510 and then not Mode_Conformant (Body_Id, Spec_Id)
3511 then
3512 return;
3513 end if;
3514 end if;
3516 if Spec_Id /= Body_Id then
3517 Reference_Body_Formals (Spec_Id, Body_Id);
3518 end if;
3520 Set_Ekind (Body_Id, E_Subprogram_Body);
3522 if Nkind (N) = N_Subprogram_Body_Stub then
3523 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3525 -- Regular body
3527 else
3528 Set_Corresponding_Spec (N, Spec_Id);
3530 -- Ada 2005 (AI-345): If the operation is a primitive operation
3531 -- of a concurrent type, the type of the first parameter has been
3532 -- replaced with the corresponding record, which is the proper
3533 -- run-time structure to use. However, within the body there may
3534 -- be uses of the formals that depend on primitive operations
3535 -- of the type (in particular calls in prefixed form) for which
3536 -- we need the original concurrent type. The operation may have
3537 -- several controlling formals, so the replacement must be done
3538 -- for all of them.
3540 if Comes_From_Source (Spec_Id)
3541 and then Present (First_Entity (Spec_Id))
3542 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3543 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3544 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3545 and then Present (Corresponding_Concurrent_Type
3546 (Etype (First_Entity (Spec_Id))))
3547 then
3548 declare
3549 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3550 Form : Entity_Id;
3552 begin
3553 Form := First_Formal (Spec_Id);
3554 while Present (Form) loop
3555 if Etype (Form) = Typ then
3556 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3557 end if;
3559 Next_Formal (Form);
3560 end loop;
3561 end;
3562 end if;
3564 -- Make the formals visible, and place subprogram on scope stack.
3565 -- This is also the point at which we set Last_Real_Spec_Entity
3566 -- to mark the entities which will not be moved to the body.
3568 Install_Formals (Spec_Id);
3569 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3571 -- Within an instance, add local renaming declarations so that
3572 -- gdb can retrieve the values of actuals more easily. This is
3573 -- only relevant if generating code (and indeed we definitely
3574 -- do not want these definitions -gnatc mode, because that would
3575 -- confuse ASIS).
3577 if Is_Generic_Instance (Spec_Id)
3578 and then Is_Wrapper_Package (Current_Scope)
3579 and then Expander_Active
3580 then
3581 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3582 end if;
3584 Push_Scope (Spec_Id);
3586 -- Make sure that the subprogram is immediately visible. For
3587 -- child units that have no separate spec this is indispensable.
3588 -- Otherwise it is safe albeit redundant.
3590 Set_Is_Immediately_Visible (Spec_Id);
3591 end if;
3593 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3594 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3595 Set_Scope (Body_Id, Scope (Spec_Id));
3596 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3598 -- Case of subprogram body with no previous spec
3600 else
3601 -- Check for style warning required
3603 if Style_Check
3605 -- Only apply check for source level subprograms for which checks
3606 -- have not been suppressed.
3608 and then Comes_From_Source (Body_Id)
3609 and then not Suppress_Style_Checks (Body_Id)
3611 -- No warnings within an instance
3613 and then not In_Instance
3615 -- No warnings for expression functions
3617 and then Nkind (Original_Node (N)) /= N_Expression_Function
3618 then
3619 Style.Body_With_No_Spec (N);
3620 end if;
3622 New_Overloaded_Entity (Body_Id);
3624 if Nkind (N) /= N_Subprogram_Body_Stub then
3625 Set_Acts_As_Spec (N);
3626 Generate_Definition (Body_Id);
3627 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3628 Generate_Reference
3629 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3630 Install_Formals (Body_Id);
3632 Push_Scope (Body_Id);
3633 end if;
3635 -- For stubs and bodies with no previous spec, generate references to
3636 -- formals.
3638 Generate_Reference_To_Formals (Body_Id);
3639 end if;
3641 -- Set SPARK_Mode from context
3643 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3644 Set_SPARK_Pragma_Inherited (Body_Id, True);
3646 -- If the return type is an anonymous access type whose designated type
3647 -- is the limited view of a class-wide type and the non-limited view is
3648 -- available, update the return type accordingly.
3650 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3651 declare
3652 Etyp : Entity_Id;
3653 Rtyp : Entity_Id;
3655 begin
3656 Rtyp := Etype (Current_Scope);
3658 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3659 Etyp := Directly_Designated_Type (Rtyp);
3661 if Is_Class_Wide_Type (Etyp)
3662 and then From_Limited_With (Etyp)
3663 then
3664 Set_Directly_Designated_Type
3665 (Etype (Current_Scope), Available_View (Etyp));
3666 end if;
3667 end if;
3668 end;
3669 end if;
3671 -- If this is the proper body of a stub, we must verify that the stub
3672 -- conforms to the body, and to the previous spec if one was present.
3673 -- We know already that the body conforms to that spec. This test is
3674 -- only required for subprograms that come from source.
3676 if Nkind (Parent (N)) = N_Subunit
3677 and then Comes_From_Source (N)
3678 and then not Error_Posted (Body_Id)
3679 and then Nkind (Corresponding_Stub (Parent (N))) =
3680 N_Subprogram_Body_Stub
3681 then
3682 declare
3683 Old_Id : constant Entity_Id :=
3684 Defining_Entity
3685 (Specification (Corresponding_Stub (Parent (N))));
3687 Conformant : Boolean := False;
3689 begin
3690 if No (Spec_Id) then
3691 Check_Fully_Conformant (Body_Id, Old_Id);
3693 else
3694 Check_Conformance
3695 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3697 if not Conformant then
3699 -- The stub was taken to be a new declaration. Indicate that
3700 -- it lacks a body.
3702 Set_Has_Completion (Old_Id, False);
3703 end if;
3704 end if;
3705 end;
3706 end if;
3708 Set_Has_Completion (Body_Id);
3709 Check_Eliminated (Body_Id);
3711 if Nkind (N) = N_Subprogram_Body_Stub then
3713 -- Analyze any aspect specifications that appear on the subprogram
3714 -- body stub.
3716 if Has_Aspects (N) then
3717 Analyze_Aspects_On_Body_Or_Stub;
3718 end if;
3720 -- Stop the analysis now as the stub cannot be inlined, plus it does
3721 -- not have declarative or statement lists.
3723 return;
3724 end if;
3726 -- Handle frontend inlining
3728 -- Note: Normally we don't do any inlining if expansion is off, since
3729 -- we won't generate code in any case. An exception arises in GNATprove
3730 -- mode where we want to expand some calls in place, even with expansion
3731 -- disabled, since the inlining eases formal verification.
3733 if not GNATprove_Mode
3734 and then Expander_Active
3735 and then Serious_Errors_Detected = 0
3736 and then Present (Spec_Id)
3737 and then Has_Pragma_Inline (Spec_Id)
3738 then
3739 -- Legacy implementation (relying on frontend inlining)
3741 if not Back_End_Inlining then
3742 if Has_Pragma_Inline_Always (Spec_Id)
3743 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)
3744 then
3745 Build_Body_To_Inline (N, Spec_Id);
3746 end if;
3748 -- New implementation (relying on backend inlining)
3750 else
3751 if Has_Pragma_Inline_Always (Spec_Id)
3752 or else Optimization_Level > 0
3753 then
3754 -- Handle function returning an unconstrained type
3756 if Comes_From_Source (Body_Id)
3757 and then Ekind (Spec_Id) = E_Function
3758 and then Returns_Unconstrained_Type (Spec_Id)
3760 -- If function builds in place, i.e. returns a limited type,
3761 -- inlining cannot be done.
3763 and then not Is_Limited_Type (Etype (Spec_Id))
3764 then
3765 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3767 else
3768 declare
3769 Subp_Body : constant Node_Id :=
3770 Unit_Declaration_Node (Body_Id);
3771 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3773 begin
3774 -- Do not pass inlining to the backend if the subprogram
3775 -- has declarations or statements which cannot be inlined
3776 -- by the backend. This check is done here to emit an
3777 -- error instead of the generic warning message reported
3778 -- by the GCC backend (ie. "function might not be
3779 -- inlinable").
3781 if Present (Subp_Decl)
3782 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3783 then
3784 null;
3786 elsif Has_Excluded_Statement
3787 (Spec_Id,
3788 Statements
3789 (Handled_Statement_Sequence (Subp_Body)))
3790 then
3791 null;
3793 -- If the backend inlining is available then at this
3794 -- stage we only have to mark the subprogram as inlined.
3795 -- The expander will take care of registering it in the
3796 -- table of subprograms inlined by the backend a part of
3797 -- processing calls to it (cf. Expand_Call)
3799 else
3800 Set_Is_Inlined (Spec_Id);
3801 end if;
3802 end;
3803 end if;
3804 end if;
3805 end if;
3807 -- In GNATprove mode, inline only when there is a separate subprogram
3808 -- declaration for now, as inlining of subprogram bodies acting as
3809 -- declarations, or subprogram stubs, are not supported by frontend
3810 -- inlining. This inlining should occur after analysis of the body, so
3811 -- that it is known whether the value of SPARK_Mode applicable to the
3812 -- body, which can be defined by a pragma inside the body.
3814 elsif GNATprove_Mode
3815 and then Full_Analysis
3816 and then not Inside_A_Generic
3817 and then Present (Spec_Id)
3818 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3819 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3820 and then not Body_Has_Contract
3821 then
3822 Build_Body_To_Inline (N, Spec_Id);
3823 end if;
3825 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3826 -- of the specification we have to install the private withed units.
3827 -- This holds for child units as well.
3829 if Is_Compilation_Unit (Body_Id)
3830 or else Nkind (Parent (N)) = N_Compilation_Unit
3831 then
3832 Install_Private_With_Clauses (Body_Id);
3833 end if;
3835 Check_Anonymous_Return;
3837 -- Set the Protected_Formal field of each extra formal of the protected
3838 -- subprogram to reference the corresponding extra formal of the
3839 -- subprogram that implements it. For regular formals this occurs when
3840 -- the protected subprogram's declaration is expanded, but the extra
3841 -- formals don't get created until the subprogram is frozen. We need to
3842 -- do this before analyzing the protected subprogram's body so that any
3843 -- references to the original subprogram's extra formals will be changed
3844 -- refer to the implementing subprogram's formals (see Expand_Formal).
3846 if Present (Spec_Id)
3847 and then Is_Protected_Type (Scope (Spec_Id))
3848 and then Present (Protected_Body_Subprogram (Spec_Id))
3849 then
3850 declare
3851 Impl_Subp : constant Entity_Id :=
3852 Protected_Body_Subprogram (Spec_Id);
3853 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3854 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3855 begin
3856 while Present (Prot_Ext_Formal) loop
3857 pragma Assert (Present (Impl_Ext_Formal));
3858 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3859 Next_Formal_With_Extras (Prot_Ext_Formal);
3860 Next_Formal_With_Extras (Impl_Ext_Formal);
3861 end loop;
3862 end;
3863 end if;
3865 -- Now we can go on to analyze the body
3867 HSS := Handled_Statement_Sequence (N);
3868 Set_Actual_Subtypes (N, Current_Scope);
3870 -- Add a declaration for the Protection object, renaming declarations
3871 -- for discriminals and privals and finally a declaration for the entry
3872 -- family index (if applicable). This form of early expansion is done
3873 -- when the Expander is active because Install_Private_Data_Declarations
3874 -- references entities which were created during regular expansion. The
3875 -- subprogram entity must come from source, and not be an internally
3876 -- generated subprogram.
3878 if Expander_Active
3879 and then Present (Prot_Typ)
3880 and then Present (Spec_Id)
3881 and then Comes_From_Source (Spec_Id)
3882 and then not Is_Eliminated (Spec_Id)
3883 then
3884 Install_Private_Data_Declarations
3885 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3886 end if;
3888 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3889 -- may now appear in parameter and result profiles. Since the analysis
3890 -- of a subprogram body may use the parameter and result profile of the
3891 -- spec, swap any limited views with their non-limited counterpart.
3893 if Ada_Version >= Ada_2012 then
3894 Exchange_Limited_Views (Spec_Id);
3895 end if;
3897 -- Analyze any aspect specifications that appear on the subprogram body
3899 if Has_Aspects (N) then
3900 Analyze_Aspects_On_Body_Or_Stub;
3901 end if;
3903 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3904 -- invariants and predicates associated with the body and its spec.
3905 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3906 -- prepares the contract assertions for generic subprograms or for ASIS.
3907 -- Do not generate contract checks in SPARK mode.
3909 if not GNATprove_Mode then
3910 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3911 end if;
3913 -- Analyze the declarations (this call will analyze the precondition
3914 -- Check pragmas we prepended to the list, as well as the declaration
3915 -- of the _Postconditions procedure).
3917 Analyze_Declarations (Declarations (N));
3919 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3921 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3922 if Present (SPARK_Pragma (Spec_Id)) then
3923 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3924 and then
3925 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3926 then
3927 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3928 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3929 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3930 Error_Msg_NE
3931 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3932 end if;
3934 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3935 null;
3937 else
3938 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3939 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3940 Error_Msg_Sloc := Sloc (Spec_Id);
3941 Error_Msg_NE
3942 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3943 end if;
3944 end if;
3946 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3947 -- subprogram in GNATprove mode, as its body should not be analyzed.
3949 if SPARK_Mode /= On
3950 and then GNATprove_Mode
3951 and then Present (Spec_Id)
3952 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3953 then
3954 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3955 Set_Is_Inlined_Always (Spec_Id, False);
3956 end if;
3958 -- Check completion, and analyze the statements
3960 Check_Completion;
3961 Inspect_Deferred_Constant_Completion (Declarations (N));
3962 Analyze (HSS);
3964 -- Deal with end of scope processing for the body
3966 Process_End_Label (HSS, 't', Current_Scope);
3967 End_Scope;
3968 Check_Subprogram_Order (N);
3969 Set_Analyzed (Body_Id);
3971 -- If we have a separate spec, then the analysis of the declarations
3972 -- caused the entities in the body to be chained to the spec id, but
3973 -- we want them chained to the body id. Only the formal parameters
3974 -- end up chained to the spec id in this case.
3976 if Present (Spec_Id) then
3978 -- We must conform to the categorization of our spec
3980 Validate_Categorization_Dependency (N, Spec_Id);
3982 -- And if this is a child unit, the parent units must conform
3984 if Is_Child_Unit (Spec_Id) then
3985 Validate_Categorization_Dependency
3986 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3987 end if;
3989 -- Here is where we move entities from the spec to the body
3991 -- Case where there are entities that stay with the spec
3993 if Present (Last_Real_Spec_Entity) then
3995 -- No body entities (happens when the only real spec entities come
3996 -- from precondition and postcondition pragmas).
3998 if No (Last_Entity (Body_Id)) then
3999 Set_First_Entity
4000 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4002 -- Body entities present (formals), so chain stuff past them
4004 else
4005 Set_Next_Entity
4006 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4007 end if;
4009 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4010 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4011 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4013 -- Case where there are no spec entities, in this case there can be
4014 -- no body entities either, so just move everything.
4016 -- If the body is generated for an expression function, it may have
4017 -- been preanalyzed already, if 'access was applied to it.
4019 else
4020 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4021 N_Expression_Function
4022 then
4023 pragma Assert (No (Last_Entity (Body_Id)));
4024 null;
4025 end if;
4027 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4028 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4029 Set_First_Entity (Spec_Id, Empty);
4030 Set_Last_Entity (Spec_Id, Empty);
4031 end if;
4032 end if;
4034 Check_Missing_Return;
4036 -- Now we are going to check for variables that are never modified in
4037 -- the body of the procedure. But first we deal with a special case
4038 -- where we want to modify this check. If the body of the subprogram
4039 -- starts with a raise statement or its equivalent, or if the body
4040 -- consists entirely of a null statement, then it is pretty obvious that
4041 -- it is OK to not reference the parameters. For example, this might be
4042 -- the following common idiom for a stubbed function: statement of the
4043 -- procedure raises an exception. In particular this deals with the
4044 -- common idiom of a stubbed function, which appears something like:
4046 -- function F (A : Integer) return Some_Type;
4047 -- X : Some_Type;
4048 -- begin
4049 -- raise Program_Error;
4050 -- return X;
4051 -- end F;
4053 -- Here the purpose of X is simply to satisfy the annoying requirement
4054 -- in Ada that there be at least one return, and we certainly do not
4055 -- want to go posting warnings on X that it is not initialized. On
4056 -- the other hand, if X is entirely unreferenced that should still
4057 -- get a warning.
4059 -- What we do is to detect these cases, and if we find them, flag the
4060 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4061 -- suppress unwanted warnings. For the case of the function stub above
4062 -- we have a special test to set X as apparently assigned to suppress
4063 -- the warning.
4065 declare
4066 Stm : Node_Id;
4068 begin
4069 -- Skip initial labels (for one thing this occurs when we are in
4070 -- front end ZCX mode, but in any case it is irrelevant), and also
4071 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4073 Stm := First (Statements (HSS));
4074 while Nkind (Stm) = N_Label
4075 or else Nkind (Stm) in N_Push_xxx_Label
4076 loop
4077 Next (Stm);
4078 end loop;
4080 -- Do the test on the original statement before expansion
4082 declare
4083 Ostm : constant Node_Id := Original_Node (Stm);
4085 begin
4086 -- If explicit raise statement, turn on flag
4088 if Nkind (Ostm) = N_Raise_Statement then
4089 Set_Trivial_Subprogram (Stm);
4091 -- If null statement, and no following statements, turn on flag
4093 elsif Nkind (Stm) = N_Null_Statement
4094 and then Comes_From_Source (Stm)
4095 and then No (Next (Stm))
4096 then
4097 Set_Trivial_Subprogram (Stm);
4099 -- Check for explicit call cases which likely raise an exception
4101 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4102 if Is_Entity_Name (Name (Ostm)) then
4103 declare
4104 Ent : constant Entity_Id := Entity (Name (Ostm));
4106 begin
4107 -- If the procedure is marked No_Return, then likely it
4108 -- raises an exception, but in any case it is not coming
4109 -- back here, so turn on the flag.
4111 if Present (Ent)
4112 and then Ekind (Ent) = E_Procedure
4113 and then No_Return (Ent)
4114 then
4115 Set_Trivial_Subprogram (Stm);
4116 end if;
4117 end;
4118 end if;
4119 end if;
4120 end;
4121 end;
4123 -- Check for variables that are never modified
4125 declare
4126 E1, E2 : Entity_Id;
4128 begin
4129 -- If there is a separate spec, then transfer Never_Set_In_Source
4130 -- flags from out parameters to the corresponding entities in the
4131 -- body. The reason we do that is we want to post error flags on
4132 -- the body entities, not the spec entities.
4134 if Present (Spec_Id) then
4135 E1 := First_Entity (Spec_Id);
4136 while Present (E1) loop
4137 if Ekind (E1) = E_Out_Parameter then
4138 E2 := First_Entity (Body_Id);
4139 while Present (E2) loop
4140 exit when Chars (E1) = Chars (E2);
4141 Next_Entity (E2);
4142 end loop;
4144 if Present (E2) then
4145 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4146 end if;
4147 end if;
4149 Next_Entity (E1);
4150 end loop;
4151 end if;
4153 -- Check references in body
4155 Check_References (Body_Id);
4156 end;
4157 end Analyze_Subprogram_Body_Helper;
4159 ---------------------------------
4160 -- Analyze_Subprogram_Contract --
4161 ---------------------------------
4163 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
4164 Items : constant Node_Id := Contract (Subp);
4165 Case_Prag : Node_Id := Empty;
4166 Depends : Node_Id := Empty;
4167 Global : Node_Id := Empty;
4168 Mode : SPARK_Mode_Type;
4169 Nam : Name_Id;
4170 Post_Prag : Node_Id := Empty;
4171 Prag : Node_Id;
4172 Seen_In_Case : Boolean := False;
4173 Seen_In_Post : Boolean := False;
4175 begin
4176 -- Due to the timing of contract analysis, delayed pragmas may be
4177 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4178 -- context. To remedy this, restore the original SPARK_Mode of the
4179 -- related subprogram body.
4181 Save_SPARK_Mode_And_Set (Subp, Mode);
4183 if Present (Items) then
4185 -- Analyze pre- and postconditions
4187 Prag := Pre_Post_Conditions (Items);
4188 while Present (Prag) loop
4189 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
4191 -- Verify whether a postcondition mentions attribute 'Result and
4192 -- its expression introduces a post-state.
4194 if Warn_On_Suspicious_Contract
4195 and then Pragma_Name (Prag) = Name_Postcondition
4196 then
4197 Post_Prag := Prag;
4198 Check_Result_And_Post_State (Prag, Seen_In_Post);
4199 end if;
4201 Prag := Next_Pragma (Prag);
4202 end loop;
4204 -- Analyze contract-cases and test-cases
4206 Prag := Contract_Test_Cases (Items);
4207 while Present (Prag) loop
4208 Nam := Pragma_Name (Prag);
4210 if Nam = Name_Contract_Cases then
4211 Analyze_Contract_Cases_In_Decl_Part (Prag);
4213 -- Verify whether contract-cases mention attribute 'Result and
4214 -- its expression introduces a post-state. Perform the check
4215 -- only when the pragma is legal.
4217 if Warn_On_Suspicious_Contract
4218 and then not Error_Posted (Prag)
4219 then
4220 Case_Prag := Prag;
4221 Check_Result_And_Post_State (Prag, Seen_In_Case);
4222 end if;
4224 else
4225 pragma Assert (Nam = Name_Test_Case);
4226 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
4227 end if;
4229 Prag := Next_Pragma (Prag);
4230 end loop;
4232 -- Analyze classification pragmas
4234 Prag := Classifications (Items);
4235 while Present (Prag) loop
4236 Nam := Pragma_Name (Prag);
4238 if Nam = Name_Depends then
4239 Depends := Prag;
4241 elsif Nam = Name_Global then
4242 Global := Prag;
4244 -- Note that pragma Extensions_Visible has already been analyzed
4246 end if;
4248 Prag := Next_Pragma (Prag);
4249 end loop;
4251 -- Analyze Global first as Depends may mention items classified in
4252 -- the global categorization.
4254 if Present (Global) then
4255 Analyze_Global_In_Decl_Part (Global);
4256 end if;
4258 -- Depends must be analyzed after Global in order to see the modes of
4259 -- all global items.
4261 if Present (Depends) then
4262 Analyze_Depends_In_Decl_Part (Depends);
4263 end if;
4264 end if;
4266 -- Emit an error when neither the postconditions nor the contract-cases
4267 -- mention attribute 'Result in the context of a function.
4269 if Warn_On_Suspicious_Contract
4270 and then Ekind_In (Subp, E_Function, E_Generic_Function)
4271 then
4272 if Present (Case_Prag)
4273 and then not Seen_In_Case
4274 and then Present (Post_Prag)
4275 and then not Seen_In_Post
4276 then
4277 Error_Msg_N
4278 ("neither function postcondition nor contract cases mention "
4279 & "result?T?", Post_Prag);
4281 elsif Present (Case_Prag) and then not Seen_In_Case then
4282 Error_Msg_N
4283 ("contract cases do not mention result?T?", Case_Prag);
4285 -- OK if we have at least one IN OUT parameter
4287 elsif Present (Post_Prag) and then not Seen_In_Post then
4288 declare
4289 F : Entity_Id;
4290 begin
4291 F := First_Formal (Subp);
4292 while Present (F) loop
4293 if Ekind (F) = E_In_Out_Parameter then
4294 return;
4295 else
4296 Next_Formal (F);
4297 end if;
4298 end loop;
4299 end;
4301 -- If no in-out parameters and no mention of Result, the contract
4302 -- is certainly suspicious.
4304 Error_Msg_N
4305 ("function postcondition does not mention result?T?", Post_Prag);
4306 end if;
4307 end if;
4309 -- Restore the SPARK_Mode of the enclosing context after all delayed
4310 -- pragmas have been analyzed.
4312 Restore_SPARK_Mode (Mode);
4313 end Analyze_Subprogram_Contract;
4315 ------------------------------------
4316 -- Analyze_Subprogram_Declaration --
4317 ------------------------------------
4319 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4320 Scop : constant Entity_Id := Current_Scope;
4321 Designator : Entity_Id;
4323 Is_Completion : Boolean;
4324 -- Indicates whether a null procedure declaration is a completion
4326 begin
4327 -- The subprogram declaration may be subject to pragma Ghost with policy
4328 -- Ignore. Set the mode now to ensure that any nodes generated during
4329 -- analysis and expansion are properly flagged as ignored Ghost.
4331 Set_Ghost_Mode (N);
4333 -- Null procedures are not allowed in SPARK
4335 if Nkind (Specification (N)) = N_Procedure_Specification
4336 and then Null_Present (Specification (N))
4337 then
4338 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4340 if Is_Protected_Type (Current_Scope) then
4341 Error_Msg_N ("protected operation cannot be a null procedure", N);
4342 end if;
4344 Analyze_Null_Procedure (N, Is_Completion);
4346 if Is_Completion then
4348 -- The null procedure acts as a body, nothing further is needed.
4350 return;
4351 end if;
4352 end if;
4354 Designator := Analyze_Subprogram_Specification (Specification (N));
4356 -- A reference may already have been generated for the unit name, in
4357 -- which case the following call is redundant. However it is needed for
4358 -- declarations that are the rewriting of an expression function.
4360 Generate_Definition (Designator);
4362 -- Set SPARK mode from current context (may be overwritten later with
4363 -- explicit pragma).
4365 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4366 Set_SPARK_Pragma_Inherited (Designator);
4368 -- A subprogram declared within a Ghost region is automatically Ghost
4369 -- (SPARK RM 6.9(2)).
4371 if Comes_From_Source (Designator) and then Ghost_Mode > None then
4372 Set_Is_Ghost_Entity (Designator);
4373 end if;
4375 if Debug_Flag_C then
4376 Write_Str ("==> subprogram spec ");
4377 Write_Name (Chars (Designator));
4378 Write_Str (" from ");
4379 Write_Location (Sloc (N));
4380 Write_Eol;
4381 Indent;
4382 end if;
4384 Validate_RCI_Subprogram_Declaration (N);
4385 New_Overloaded_Entity (Designator);
4386 Check_Delayed_Subprogram (Designator);
4388 -- If the type of the first formal of the current subprogram is a non-
4389 -- generic tagged private type, mark the subprogram as being a private
4390 -- primitive. Ditto if this is a function with controlling result, and
4391 -- the return type is currently private. In both cases, the type of the
4392 -- controlling argument or result must be in the current scope for the
4393 -- operation to be primitive.
4395 if Has_Controlling_Result (Designator)
4396 and then Is_Private_Type (Etype (Designator))
4397 and then Scope (Etype (Designator)) = Current_Scope
4398 and then not Is_Generic_Actual_Type (Etype (Designator))
4399 then
4400 Set_Is_Private_Primitive (Designator);
4402 elsif Present (First_Formal (Designator)) then
4403 declare
4404 Formal_Typ : constant Entity_Id :=
4405 Etype (First_Formal (Designator));
4406 begin
4407 Set_Is_Private_Primitive (Designator,
4408 Is_Tagged_Type (Formal_Typ)
4409 and then Scope (Formal_Typ) = Current_Scope
4410 and then Is_Private_Type (Formal_Typ)
4411 and then not Is_Generic_Actual_Type (Formal_Typ));
4412 end;
4413 end if;
4415 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4416 -- or null.
4418 if Ada_Version >= Ada_2005
4419 and then Comes_From_Source (N)
4420 and then Is_Dispatching_Operation (Designator)
4421 then
4422 declare
4423 E : Entity_Id;
4424 Etyp : Entity_Id;
4426 begin
4427 if Has_Controlling_Result (Designator) then
4428 Etyp := Etype (Designator);
4430 else
4431 E := First_Entity (Designator);
4432 while Present (E)
4433 and then Is_Formal (E)
4434 and then not Is_Controlling_Formal (E)
4435 loop
4436 Next_Entity (E);
4437 end loop;
4439 Etyp := Etype (E);
4440 end if;
4442 if Is_Access_Type (Etyp) then
4443 Etyp := Directly_Designated_Type (Etyp);
4444 end if;
4446 if Is_Interface (Etyp)
4447 and then not Is_Abstract_Subprogram (Designator)
4448 and then not (Ekind (Designator) = E_Procedure
4449 and then Null_Present (Specification (N)))
4450 then
4451 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4453 -- Specialize error message based on procedures vs. functions,
4454 -- since functions can't be null subprograms.
4456 if Ekind (Designator) = E_Procedure then
4457 Error_Msg_N
4458 ("interface procedure % must be abstract or null", N);
4459 else
4460 Error_Msg_N
4461 ("interface function % must be abstract", N);
4462 end if;
4463 end if;
4464 end;
4465 end if;
4467 -- What is the following code for, it used to be
4469 -- ??? Set_Suppress_Elaboration_Checks
4470 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4472 -- The following seems equivalent, but a bit dubious
4474 if Elaboration_Checks_Suppressed (Designator) then
4475 Set_Kill_Elaboration_Checks (Designator);
4476 end if;
4478 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4479 Set_Categorization_From_Scope (Designator, Scop);
4481 else
4482 -- For a compilation unit, check for library-unit pragmas
4484 Push_Scope (Designator);
4485 Set_Categorization_From_Pragmas (N);
4486 Validate_Categorization_Dependency (N, Designator);
4487 Pop_Scope;
4488 end if;
4490 -- For a compilation unit, set body required. This flag will only be
4491 -- reset if a valid Import or Interface pragma is processed later on.
4493 if Nkind (Parent (N)) = N_Compilation_Unit then
4494 Set_Body_Required (Parent (N), True);
4496 if Ada_Version >= Ada_2005
4497 and then Nkind (Specification (N)) = N_Procedure_Specification
4498 and then Null_Present (Specification (N))
4499 then
4500 Error_Msg_N
4501 ("null procedure cannot be declared at library level", N);
4502 end if;
4503 end if;
4505 Generate_Reference_To_Formals (Designator);
4506 Check_Eliminated (Designator);
4508 if Debug_Flag_C then
4509 Outdent;
4510 Write_Str ("<== subprogram spec ");
4511 Write_Name (Chars (Designator));
4512 Write_Str (" from ");
4513 Write_Location (Sloc (N));
4514 Write_Eol;
4515 end if;
4517 if Is_Protected_Type (Current_Scope) then
4519 -- Indicate that this is a protected operation, because it may be
4520 -- used in subsequent declarations within the protected type.
4522 Set_Convention (Designator, Convention_Protected);
4523 end if;
4525 List_Inherited_Pre_Post_Aspects (Designator);
4527 if Has_Aspects (N) then
4528 Analyze_Aspect_Specifications (N, Designator);
4529 end if;
4530 end Analyze_Subprogram_Declaration;
4532 --------------------------------------
4533 -- Analyze_Subprogram_Specification --
4534 --------------------------------------
4536 -- Reminder: N here really is a subprogram specification (not a subprogram
4537 -- declaration). This procedure is called to analyze the specification in
4538 -- both subprogram bodies and subprogram declarations (specs).
4540 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4541 Designator : constant Entity_Id := Defining_Entity (N);
4542 Formals : constant List_Id := Parameter_Specifications (N);
4544 -- Start of processing for Analyze_Subprogram_Specification
4546 begin
4547 -- User-defined operator is not allowed in SPARK, except as a renaming
4549 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4550 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4551 then
4552 Check_SPARK_05_Restriction
4553 ("user-defined operator is not allowed", N);
4554 end if;
4556 -- Proceed with analysis. Do not emit a cross-reference entry if the
4557 -- specification comes from an expression function, because it may be
4558 -- the completion of a previous declaration. It is is not, the cross-
4559 -- reference entry will be emitted for the new subprogram declaration.
4561 if Nkind (Parent (N)) /= N_Expression_Function then
4562 Generate_Definition (Designator);
4563 end if;
4565 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4567 if Nkind (N) = N_Function_Specification then
4568 Set_Ekind (Designator, E_Function);
4569 Set_Mechanism (Designator, Default_Mechanism);
4570 else
4571 Set_Ekind (Designator, E_Procedure);
4572 Set_Etype (Designator, Standard_Void_Type);
4573 end if;
4575 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4576 -- those subprograms which could be inlined in GNATprove mode (because
4577 -- Body_To_Inline is non-Empty) but cannot be inlined.
4579 if GNATprove_Mode then
4580 Set_Is_Inlined_Always (Designator);
4581 end if;
4583 -- Introduce new scope for analysis of the formals and the return type
4585 Set_Scope (Designator, Current_Scope);
4587 if Present (Formals) then
4588 Push_Scope (Designator);
4589 Process_Formals (Formals, N);
4591 -- Check dimensions in N for formals with default expression
4593 Analyze_Dimension_Formals (N, Formals);
4595 -- Ada 2005 (AI-345): If this is an overriding operation of an
4596 -- inherited interface operation, and the controlling type is
4597 -- a synchronized type, replace the type with its corresponding
4598 -- record, to match the proper signature of an overriding operation.
4599 -- Same processing for an access parameter whose designated type is
4600 -- derived from a synchronized interface.
4602 if Ada_Version >= Ada_2005 then
4603 declare
4604 Formal : Entity_Id;
4605 Formal_Typ : Entity_Id;
4606 Rec_Typ : Entity_Id;
4607 Desig_Typ : Entity_Id;
4609 begin
4610 Formal := First_Formal (Designator);
4611 while Present (Formal) loop
4612 Formal_Typ := Etype (Formal);
4614 if Is_Concurrent_Type (Formal_Typ)
4615 and then Present (Corresponding_Record_Type (Formal_Typ))
4616 then
4617 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4619 if Present (Interfaces (Rec_Typ)) then
4620 Set_Etype (Formal, Rec_Typ);
4621 end if;
4623 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4624 Desig_Typ := Designated_Type (Formal_Typ);
4626 if Is_Concurrent_Type (Desig_Typ)
4627 and then Present (Corresponding_Record_Type (Desig_Typ))
4628 then
4629 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4631 if Present (Interfaces (Rec_Typ)) then
4632 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4633 end if;
4634 end if;
4635 end if;
4637 Next_Formal (Formal);
4638 end loop;
4639 end;
4640 end if;
4642 End_Scope;
4644 -- The subprogram scope is pushed and popped around the processing of
4645 -- the return type for consistency with call above to Process_Formals
4646 -- (which itself can call Analyze_Return_Type), and to ensure that any
4647 -- itype created for the return type will be associated with the proper
4648 -- scope.
4650 elsif Nkind (N) = N_Function_Specification then
4651 Push_Scope (Designator);
4652 Analyze_Return_Type (N);
4653 End_Scope;
4654 end if;
4656 -- Function case
4658 if Nkind (N) = N_Function_Specification then
4660 -- Deal with operator symbol case
4662 if Nkind (Designator) = N_Defining_Operator_Symbol then
4663 Valid_Operator_Definition (Designator);
4664 end if;
4666 May_Need_Actuals (Designator);
4668 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4669 -- the subprogram is abstract also. This does not apply to renaming
4670 -- declarations, where abstractness is inherited, and to subprogram
4671 -- bodies generated for stream operations, which become renamings as
4672 -- bodies.
4674 -- In case of primitives associated with abstract interface types
4675 -- the check is applied later (see Analyze_Subprogram_Declaration).
4677 if not Nkind_In (Original_Node (Parent (N)),
4678 N_Subprogram_Renaming_Declaration,
4679 N_Abstract_Subprogram_Declaration,
4680 N_Formal_Abstract_Subprogram_Declaration)
4681 then
4682 if Is_Abstract_Type (Etype (Designator))
4683 and then not Is_Interface (Etype (Designator))
4684 then
4685 Error_Msg_N
4686 ("function that returns abstract type must be abstract", N);
4688 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4689 -- access result whose designated type is abstract.
4691 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4692 and then
4693 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4694 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4695 and then Ada_Version >= Ada_2012
4696 then
4697 Error_Msg_N ("function whose access result designates "
4698 & "abstract type must be abstract", N);
4699 end if;
4700 end if;
4701 end if;
4703 return Designator;
4704 end Analyze_Subprogram_Specification;
4706 -----------------------
4707 -- Check_Conformance --
4708 -----------------------
4710 procedure Check_Conformance
4711 (New_Id : Entity_Id;
4712 Old_Id : Entity_Id;
4713 Ctype : Conformance_Type;
4714 Errmsg : Boolean;
4715 Conforms : out Boolean;
4716 Err_Loc : Node_Id := Empty;
4717 Get_Inst : Boolean := False;
4718 Skip_Controlling_Formals : Boolean := False)
4720 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4721 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4722 -- If Errmsg is True, then processing continues to post an error message
4723 -- for conformance error on given node. Two messages are output. The
4724 -- first message points to the previous declaration with a general "no
4725 -- conformance" message. The second is the detailed reason, supplied as
4726 -- Msg. The parameter N provide information for a possible & insertion
4727 -- in the message, and also provides the location for posting the
4728 -- message in the absence of a specified Err_Loc location.
4730 -----------------------
4731 -- Conformance_Error --
4732 -----------------------
4734 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4735 Enode : Node_Id;
4737 begin
4738 Conforms := False;
4740 if Errmsg then
4741 if No (Err_Loc) then
4742 Enode := N;
4743 else
4744 Enode := Err_Loc;
4745 end if;
4747 Error_Msg_Sloc := Sloc (Old_Id);
4749 case Ctype is
4750 when Type_Conformant =>
4751 Error_Msg_N -- CODEFIX
4752 ("not type conformant with declaration#!", Enode);
4754 when Mode_Conformant =>
4755 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4756 Error_Msg_N
4757 ("not mode conformant with operation inherited#!",
4758 Enode);
4759 else
4760 Error_Msg_N
4761 ("not mode conformant with declaration#!", Enode);
4762 end if;
4764 when Subtype_Conformant =>
4765 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4766 Error_Msg_N
4767 ("not subtype conformant with operation inherited#!",
4768 Enode);
4769 else
4770 Error_Msg_N
4771 ("not subtype conformant with declaration#!", Enode);
4772 end if;
4774 when Fully_Conformant =>
4775 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4776 Error_Msg_N -- CODEFIX
4777 ("not fully conformant with operation inherited#!",
4778 Enode);
4779 else
4780 Error_Msg_N -- CODEFIX
4781 ("not fully conformant with declaration#!", Enode);
4782 end if;
4783 end case;
4785 Error_Msg_NE (Msg, Enode, N);
4786 end if;
4787 end Conformance_Error;
4789 -- Local Variables
4791 Old_Type : constant Entity_Id := Etype (Old_Id);
4792 New_Type : constant Entity_Id := Etype (New_Id);
4793 Old_Formal : Entity_Id;
4794 New_Formal : Entity_Id;
4795 Access_Types_Match : Boolean;
4796 Old_Formal_Base : Entity_Id;
4797 New_Formal_Base : Entity_Id;
4799 -- Start of processing for Check_Conformance
4801 begin
4802 Conforms := True;
4804 -- We need a special case for operators, since they don't appear
4805 -- explicitly.
4807 if Ctype = Type_Conformant then
4808 if Ekind (New_Id) = E_Operator
4809 and then Operator_Matches_Spec (New_Id, Old_Id)
4810 then
4811 return;
4812 end if;
4813 end if;
4815 -- If both are functions/operators, check return types conform
4817 if Old_Type /= Standard_Void_Type
4818 and then
4819 New_Type /= Standard_Void_Type
4820 then
4821 -- If we are checking interface conformance we omit controlling
4822 -- arguments and result, because we are only checking the conformance
4823 -- of the remaining parameters.
4825 if Has_Controlling_Result (Old_Id)
4826 and then Has_Controlling_Result (New_Id)
4827 and then Skip_Controlling_Formals
4828 then
4829 null;
4831 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4832 if Ctype >= Subtype_Conformant
4833 and then not Predicates_Match (Old_Type, New_Type)
4834 then
4835 Conformance_Error
4836 ("\predicate of return type does not match!", New_Id);
4837 else
4838 Conformance_Error
4839 ("\return type does not match!", New_Id);
4840 end if;
4842 return;
4843 end if;
4845 -- Ada 2005 (AI-231): In case of anonymous access types check the
4846 -- null-exclusion and access-to-constant attributes match.
4848 if Ada_Version >= Ada_2005
4849 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4850 and then
4851 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4852 or else Is_Access_Constant (Etype (Old_Type)) /=
4853 Is_Access_Constant (Etype (New_Type)))
4854 then
4855 Conformance_Error ("\return type does not match!", New_Id);
4856 return;
4857 end if;
4859 -- If either is a function/operator and the other isn't, error
4861 elsif Old_Type /= Standard_Void_Type
4862 or else New_Type /= Standard_Void_Type
4863 then
4864 Conformance_Error ("\functions can only match functions!", New_Id);
4865 return;
4866 end if;
4868 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4869 -- If this is a renaming as body, refine error message to indicate that
4870 -- the conflict is with the original declaration. If the entity is not
4871 -- frozen, the conventions don't have to match, the one of the renamed
4872 -- entity is inherited.
4874 if Ctype >= Subtype_Conformant then
4875 if Convention (Old_Id) /= Convention (New_Id) then
4876 if not Is_Frozen (New_Id) then
4877 null;
4879 elsif Present (Err_Loc)
4880 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4881 and then Present (Corresponding_Spec (Err_Loc))
4882 then
4883 Error_Msg_Name_1 := Chars (New_Id);
4884 Error_Msg_Name_2 :=
4885 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4886 Conformance_Error ("\prior declaration for% has convention %!");
4888 else
4889 Conformance_Error ("\calling conventions do not match!");
4890 end if;
4892 return;
4894 elsif Is_Formal_Subprogram (Old_Id)
4895 or else Is_Formal_Subprogram (New_Id)
4896 then
4897 Conformance_Error ("\formal subprograms not allowed!");
4898 return;
4900 -- Pragma Ghost behaves as a convention in the context of subtype
4901 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4902 -- subprograms as their spec may reside in a Ghost region and their
4903 -- body not, or vice versa.
4905 elsif Comes_From_Source (Old_Id)
4906 and then Comes_From_Source (New_Id)
4907 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4908 then
4909 Conformance_Error ("\ghost modes do not match!");
4910 return;
4911 end if;
4912 end if;
4914 -- Deal with parameters
4916 -- Note: we use the entity information, rather than going directly
4917 -- to the specification in the tree. This is not only simpler, but
4918 -- absolutely necessary for some cases of conformance tests between
4919 -- operators, where the declaration tree simply does not exist.
4921 Old_Formal := First_Formal (Old_Id);
4922 New_Formal := First_Formal (New_Id);
4923 while Present (Old_Formal) and then Present (New_Formal) loop
4924 if Is_Controlling_Formal (Old_Formal)
4925 and then Is_Controlling_Formal (New_Formal)
4926 and then Skip_Controlling_Formals
4927 then
4928 -- The controlling formals will have different types when
4929 -- comparing an interface operation with its match, but both
4930 -- or neither must be access parameters.
4932 if Is_Access_Type (Etype (Old_Formal))
4934 Is_Access_Type (Etype (New_Formal))
4935 then
4936 goto Skip_Controlling_Formal;
4937 else
4938 Conformance_Error
4939 ("\access parameter does not match!", New_Formal);
4940 end if;
4941 end if;
4943 -- Ada 2012: Mode conformance also requires that formal parameters
4944 -- be both aliased, or neither.
4946 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4947 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4948 Conformance_Error
4949 ("\aliased parameter mismatch!", New_Formal);
4950 end if;
4951 end if;
4953 if Ctype = Fully_Conformant then
4955 -- Names must match. Error message is more accurate if we do
4956 -- this before checking that the types of the formals match.
4958 if Chars (Old_Formal) /= Chars (New_Formal) then
4959 Conformance_Error ("\name& does not match!", New_Formal);
4961 -- Set error posted flag on new formal as well to stop
4962 -- junk cascaded messages in some cases.
4964 Set_Error_Posted (New_Formal);
4965 return;
4966 end if;
4968 -- Null exclusion must match
4970 if Null_Exclusion_Present (Parent (Old_Formal))
4972 Null_Exclusion_Present (Parent (New_Formal))
4973 then
4974 -- Only give error if both come from source. This should be
4975 -- investigated some time, since it should not be needed ???
4977 if Comes_From_Source (Old_Formal)
4978 and then
4979 Comes_From_Source (New_Formal)
4980 then
4981 Conformance_Error
4982 ("\null exclusion for& does not match", New_Formal);
4984 -- Mark error posted on the new formal to avoid duplicated
4985 -- complaint about types not matching.
4987 Set_Error_Posted (New_Formal);
4988 end if;
4989 end if;
4990 end if;
4992 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4993 -- case occurs whenever a subprogram is being renamed and one of its
4994 -- parameters imposes a null exclusion. For example:
4996 -- type T is null record;
4997 -- type Acc_T is access T;
4998 -- subtype Acc_T_Sub is Acc_T;
5000 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5001 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5002 -- renames P;
5004 Old_Formal_Base := Etype (Old_Formal);
5005 New_Formal_Base := Etype (New_Formal);
5007 if Get_Inst then
5008 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5009 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5010 end if;
5012 Access_Types_Match := Ada_Version >= Ada_2005
5014 -- Ensure that this rule is only applied when New_Id is a
5015 -- renaming of Old_Id.
5017 and then Nkind (Parent (Parent (New_Id))) =
5018 N_Subprogram_Renaming_Declaration
5019 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5020 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5021 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5023 -- Now handle the allowed access-type case
5025 and then Is_Access_Type (Old_Formal_Base)
5026 and then Is_Access_Type (New_Formal_Base)
5028 -- The type kinds must match. The only exception occurs with
5029 -- multiple generics of the form:
5031 -- generic generic
5032 -- type F is private; type A is private;
5033 -- type F_Ptr is access F; type A_Ptr is access A;
5034 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5035 -- package F_Pack is ... package A_Pack is
5036 -- package F_Inst is
5037 -- new F_Pack (A, A_Ptr, A_P);
5039 -- When checking for conformance between the parameters of A_P
5040 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5041 -- because the compiler has transformed A_Ptr into a subtype of
5042 -- F_Ptr. We catch this case in the code below.
5044 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5045 or else
5046 (Is_Generic_Type (Old_Formal_Base)
5047 and then Is_Generic_Type (New_Formal_Base)
5048 and then Is_Internal (New_Formal_Base)
5049 and then Etype (Etype (New_Formal_Base)) =
5050 Old_Formal_Base))
5051 and then Directly_Designated_Type (Old_Formal_Base) =
5052 Directly_Designated_Type (New_Formal_Base)
5053 and then ((Is_Itype (Old_Formal_Base)
5054 and then Can_Never_Be_Null (Old_Formal_Base))
5055 or else
5056 (Is_Itype (New_Formal_Base)
5057 and then Can_Never_Be_Null (New_Formal_Base)));
5059 -- Types must always match. In the visible part of an instance,
5060 -- usual overloading rules for dispatching operations apply, and
5061 -- we check base types (not the actual subtypes).
5063 if In_Instance_Visible_Part
5064 and then Is_Dispatching_Operation (New_Id)
5065 then
5066 if not Conforming_Types
5067 (T1 => Base_Type (Etype (Old_Formal)),
5068 T2 => Base_Type (Etype (New_Formal)),
5069 Ctype => Ctype,
5070 Get_Inst => Get_Inst)
5071 and then not Access_Types_Match
5072 then
5073 Conformance_Error ("\type of & does not match!", New_Formal);
5074 return;
5075 end if;
5077 elsif not Conforming_Types
5078 (T1 => Old_Formal_Base,
5079 T2 => New_Formal_Base,
5080 Ctype => Ctype,
5081 Get_Inst => Get_Inst)
5082 and then not Access_Types_Match
5083 then
5084 -- Don't give error message if old type is Any_Type. This test
5085 -- avoids some cascaded errors, e.g. in case of a bad spec.
5087 if Errmsg and then Old_Formal_Base = Any_Type then
5088 Conforms := False;
5089 else
5090 if Ctype >= Subtype_Conformant
5091 and then
5092 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5093 then
5094 Conformance_Error
5095 ("\predicate of & does not match!", New_Formal);
5096 else
5097 Conformance_Error
5098 ("\type of & does not match!", New_Formal);
5099 end if;
5100 end if;
5102 return;
5103 end if;
5105 -- For mode conformance, mode must match
5107 if Ctype >= Mode_Conformant then
5108 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5109 if not Ekind_In (New_Id, E_Function, E_Procedure)
5110 or else not Is_Primitive_Wrapper (New_Id)
5111 then
5112 Conformance_Error ("\mode of & does not match!", New_Formal);
5114 else
5115 declare
5116 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5117 begin
5118 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5119 then
5120 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5121 else
5122 Conformance_Error
5123 ("\mode of & does not match!", New_Formal);
5124 end if;
5125 end;
5126 end if;
5128 return;
5130 -- Part of mode conformance for access types is having the same
5131 -- constant modifier.
5133 elsif Access_Types_Match
5134 and then Is_Access_Constant (Old_Formal_Base) /=
5135 Is_Access_Constant (New_Formal_Base)
5136 then
5137 Conformance_Error
5138 ("\constant modifier does not match!", New_Formal);
5139 return;
5140 end if;
5141 end if;
5143 if Ctype >= Subtype_Conformant then
5145 -- Ada 2005 (AI-231): In case of anonymous access types check
5146 -- the null-exclusion and access-to-constant attributes must
5147 -- match. For null exclusion, we test the types rather than the
5148 -- formals themselves, since the attribute is only set reliably
5149 -- on the formals in the Ada 95 case, and we exclude the case
5150 -- where Old_Formal is marked as controlling, to avoid errors
5151 -- when matching completing bodies with dispatching declarations
5152 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5154 if Ada_Version >= Ada_2005
5155 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5156 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5157 and then
5158 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5159 Can_Never_Be_Null (Etype (New_Formal))
5160 and then
5161 not Is_Controlling_Formal (Old_Formal))
5162 or else
5163 Is_Access_Constant (Etype (Old_Formal)) /=
5164 Is_Access_Constant (Etype (New_Formal)))
5166 -- Do not complain if error already posted on New_Formal. This
5167 -- avoids some redundant error messages.
5169 and then not Error_Posted (New_Formal)
5170 then
5171 -- It is allowed to omit the null-exclusion in case of stream
5172 -- attribute subprograms. We recognize stream subprograms
5173 -- through their TSS-generated suffix.
5175 declare
5176 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5178 begin
5179 if TSS_Name /= TSS_Stream_Read
5180 and then TSS_Name /= TSS_Stream_Write
5181 and then TSS_Name /= TSS_Stream_Input
5182 and then TSS_Name /= TSS_Stream_Output
5183 then
5184 -- Here we have a definite conformance error. It is worth
5185 -- special casing the error message for the case of a
5186 -- controlling formal (which excludes null).
5188 if Is_Controlling_Formal (New_Formal) then
5189 Error_Msg_Node_2 := Scope (New_Formal);
5190 Conformance_Error
5191 ("\controlling formal & of & excludes null, "
5192 & "declaration must exclude null as well",
5193 New_Formal);
5195 -- Normal case (couldn't we give more detail here???)
5197 else
5198 Conformance_Error
5199 ("\type of & does not match!", New_Formal);
5200 end if;
5202 return;
5203 end if;
5204 end;
5205 end if;
5206 end if;
5208 -- Full conformance checks
5210 if Ctype = Fully_Conformant then
5212 -- We have checked already that names match
5214 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5216 -- Check default expressions for in parameters
5218 declare
5219 NewD : constant Boolean :=
5220 Present (Default_Value (New_Formal));
5221 OldD : constant Boolean :=
5222 Present (Default_Value (Old_Formal));
5223 begin
5224 if NewD or OldD then
5226 -- The old default value has been analyzed because the
5227 -- current full declaration will have frozen everything
5228 -- before. The new default value has not been analyzed,
5229 -- so analyze it now before we check for conformance.
5231 if NewD then
5232 Push_Scope (New_Id);
5233 Preanalyze_Spec_Expression
5234 (Default_Value (New_Formal), Etype (New_Formal));
5235 End_Scope;
5236 end if;
5238 if not (NewD and OldD)
5239 or else not Fully_Conformant_Expressions
5240 (Default_Value (Old_Formal),
5241 Default_Value (New_Formal))
5242 then
5243 Conformance_Error
5244 ("\default expression for & does not match!",
5245 New_Formal);
5246 return;
5247 end if;
5248 end if;
5249 end;
5250 end if;
5251 end if;
5253 -- A couple of special checks for Ada 83 mode. These checks are
5254 -- skipped if either entity is an operator in package Standard,
5255 -- or if either old or new instance is not from the source program.
5257 if Ada_Version = Ada_83
5258 and then Sloc (Old_Id) > Standard_Location
5259 and then Sloc (New_Id) > Standard_Location
5260 and then Comes_From_Source (Old_Id)
5261 and then Comes_From_Source (New_Id)
5262 then
5263 declare
5264 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5265 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5267 begin
5268 -- Explicit IN must be present or absent in both cases. This
5269 -- test is required only in the full conformance case.
5271 if In_Present (Old_Param) /= In_Present (New_Param)
5272 and then Ctype = Fully_Conformant
5273 then
5274 Conformance_Error
5275 ("\(Ada 83) IN must appear in both declarations",
5276 New_Formal);
5277 return;
5278 end if;
5280 -- Grouping (use of comma in param lists) must be the same
5281 -- This is where we catch a misconformance like:
5283 -- A, B : Integer
5284 -- A : Integer; B : Integer
5286 -- which are represented identically in the tree except
5287 -- for the setting of the flags More_Ids and Prev_Ids.
5289 if More_Ids (Old_Param) /= More_Ids (New_Param)
5290 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5291 then
5292 Conformance_Error
5293 ("\grouping of & does not match!", New_Formal);
5294 return;
5295 end if;
5296 end;
5297 end if;
5299 -- This label is required when skipping controlling formals
5301 <<Skip_Controlling_Formal>>
5303 Next_Formal (Old_Formal);
5304 Next_Formal (New_Formal);
5305 end loop;
5307 if Present (Old_Formal) then
5308 Conformance_Error ("\too few parameters!");
5309 return;
5311 elsif Present (New_Formal) then
5312 Conformance_Error ("\too many parameters!", New_Formal);
5313 return;
5314 end if;
5315 end Check_Conformance;
5317 -----------------------
5318 -- Check_Conventions --
5319 -----------------------
5321 procedure Check_Conventions (Typ : Entity_Id) is
5322 Ifaces_List : Elist_Id;
5324 procedure Check_Convention (Op : Entity_Id);
5325 -- Verify that the convention of inherited dispatching operation Op is
5326 -- consistent among all subprograms it overrides. In order to minimize
5327 -- the search, Search_From is utilized to designate a specific point in
5328 -- the list rather than iterating over the whole list once more.
5330 ----------------------
5331 -- Check_Convention --
5332 ----------------------
5334 procedure Check_Convention (Op : Entity_Id) is
5335 Op_Conv : constant Convention_Id := Convention (Op);
5336 Iface_Conv : Convention_Id;
5337 Iface_Elmt : Elmt_Id;
5338 Iface_Prim_Elmt : Elmt_Id;
5339 Iface_Prim : Entity_Id;
5341 begin
5342 Iface_Elmt := First_Elmt (Ifaces_List);
5343 while Present (Iface_Elmt) loop
5344 Iface_Prim_Elmt :=
5345 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5346 while Present (Iface_Prim_Elmt) loop
5347 Iface_Prim := Node (Iface_Prim_Elmt);
5348 Iface_Conv := Convention (Iface_Prim);
5350 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5351 and then Iface_Conv /= Op_Conv
5352 then
5353 Error_Msg_N
5354 ("inconsistent conventions in primitive operations", Typ);
5356 Error_Msg_Name_1 := Chars (Op);
5357 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5358 Error_Msg_Sloc := Sloc (Op);
5360 if Comes_From_Source (Op) or else No (Alias (Op)) then
5361 if not Present (Overridden_Operation (Op)) then
5362 Error_Msg_N ("\\primitive % defined #", Typ);
5363 else
5364 Error_Msg_N
5365 ("\\overriding operation % with "
5366 & "convention % defined #", Typ);
5367 end if;
5369 else pragma Assert (Present (Alias (Op)));
5370 Error_Msg_Sloc := Sloc (Alias (Op));
5371 Error_Msg_N ("\\inherited operation % with "
5372 & "convention % defined #", Typ);
5373 end if;
5375 Error_Msg_Name_1 := Chars (Op);
5376 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5377 Error_Msg_Sloc := Sloc (Iface_Prim);
5378 Error_Msg_N ("\\overridden operation % with "
5379 & "convention % defined #", Typ);
5381 -- Avoid cascading errors
5383 return;
5384 end if;
5386 Next_Elmt (Iface_Prim_Elmt);
5387 end loop;
5389 Next_Elmt (Iface_Elmt);
5390 end loop;
5391 end Check_Convention;
5393 -- Local variables
5395 Prim_Op : Entity_Id;
5396 Prim_Op_Elmt : Elmt_Id;
5398 -- Start of processing for Check_Conventions
5400 begin
5401 if not Has_Interfaces (Typ) then
5402 return;
5403 end if;
5405 Collect_Interfaces (Typ, Ifaces_List);
5407 -- The algorithm checks every overriding dispatching operation against
5408 -- all the corresponding overridden dispatching operations, detecting
5409 -- differences in conventions.
5411 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5412 while Present (Prim_Op_Elmt) loop
5413 Prim_Op := Node (Prim_Op_Elmt);
5415 -- A small optimization: skip the predefined dispatching operations
5416 -- since they always have the same convention.
5418 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5419 Check_Convention (Prim_Op);
5420 end if;
5422 Next_Elmt (Prim_Op_Elmt);
5423 end loop;
5424 end Check_Conventions;
5426 ------------------------------
5427 -- Check_Delayed_Subprogram --
5428 ------------------------------
5430 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5431 F : Entity_Id;
5433 procedure Possible_Freeze (T : Entity_Id);
5434 -- T is the type of either a formal parameter or of the return type.
5435 -- If T is not yet frozen and needs a delayed freeze, then the
5436 -- subprogram itself must be delayed. If T is the limited view of an
5437 -- incomplete type the subprogram must be frozen as well, because
5438 -- T may depend on local types that have not been frozen yet.
5440 ---------------------
5441 -- Possible_Freeze --
5442 ---------------------
5444 procedure Possible_Freeze (T : Entity_Id) is
5445 begin
5446 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5447 Set_Has_Delayed_Freeze (Designator);
5449 elsif Is_Access_Type (T)
5450 and then Has_Delayed_Freeze (Designated_Type (T))
5451 and then not Is_Frozen (Designated_Type (T))
5452 then
5453 Set_Has_Delayed_Freeze (Designator);
5455 elsif Ekind (T) = E_Incomplete_Type
5456 and then From_Limited_With (T)
5457 then
5458 Set_Has_Delayed_Freeze (Designator);
5460 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5461 -- of a subprogram or entry declaration.
5463 elsif Ekind (T) = E_Incomplete_Type
5464 and then Ada_Version >= Ada_2012
5465 then
5466 Set_Has_Delayed_Freeze (Designator);
5467 end if;
5469 end Possible_Freeze;
5471 -- Start of processing for Check_Delayed_Subprogram
5473 begin
5474 -- All subprograms, including abstract subprograms, may need a freeze
5475 -- node if some formal type or the return type needs one.
5477 Possible_Freeze (Etype (Designator));
5478 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5480 -- Need delayed freeze if any of the formal types themselves need
5481 -- a delayed freeze and are not yet frozen.
5483 F := First_Formal (Designator);
5484 while Present (F) loop
5485 Possible_Freeze (Etype (F));
5486 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5487 Next_Formal (F);
5488 end loop;
5490 -- Mark functions that return by reference. Note that it cannot be
5491 -- done for delayed_freeze subprograms because the underlying
5492 -- returned type may not be known yet (for private types)
5494 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5495 declare
5496 Typ : constant Entity_Id := Etype (Designator);
5497 Utyp : constant Entity_Id := Underlying_Type (Typ);
5498 begin
5499 if Is_Limited_View (Typ) then
5500 Set_Returns_By_Ref (Designator);
5501 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5502 Set_Returns_By_Ref (Designator);
5503 end if;
5504 end;
5505 end if;
5506 end Check_Delayed_Subprogram;
5508 ------------------------------------
5509 -- Check_Discriminant_Conformance --
5510 ------------------------------------
5512 procedure Check_Discriminant_Conformance
5513 (N : Node_Id;
5514 Prev : Entity_Id;
5515 Prev_Loc : Node_Id)
5517 Old_Discr : Entity_Id := First_Discriminant (Prev);
5518 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5519 New_Discr_Id : Entity_Id;
5520 New_Discr_Type : Entity_Id;
5522 procedure Conformance_Error (Msg : String; N : Node_Id);
5523 -- Post error message for conformance error on given node. Two messages
5524 -- are output. The first points to the previous declaration with a
5525 -- general "no conformance" message. The second is the detailed reason,
5526 -- supplied as Msg. The parameter N provide information for a possible
5527 -- & insertion in the message.
5529 -----------------------
5530 -- Conformance_Error --
5531 -----------------------
5533 procedure Conformance_Error (Msg : String; N : Node_Id) is
5534 begin
5535 Error_Msg_Sloc := Sloc (Prev_Loc);
5536 Error_Msg_N -- CODEFIX
5537 ("not fully conformant with declaration#!", N);
5538 Error_Msg_NE (Msg, N, N);
5539 end Conformance_Error;
5541 -- Start of processing for Check_Discriminant_Conformance
5543 begin
5544 while Present (Old_Discr) and then Present (New_Discr) loop
5545 New_Discr_Id := Defining_Identifier (New_Discr);
5547 -- The subtype mark of the discriminant on the full type has not
5548 -- been analyzed so we do it here. For an access discriminant a new
5549 -- type is created.
5551 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5552 New_Discr_Type :=
5553 Access_Definition (N, Discriminant_Type (New_Discr));
5555 else
5556 Analyze (Discriminant_Type (New_Discr));
5557 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5559 -- Ada 2005: if the discriminant definition carries a null
5560 -- exclusion, create an itype to check properly for consistency
5561 -- with partial declaration.
5563 if Is_Access_Type (New_Discr_Type)
5564 and then Null_Exclusion_Present (New_Discr)
5565 then
5566 New_Discr_Type :=
5567 Create_Null_Excluding_Itype
5568 (T => New_Discr_Type,
5569 Related_Nod => New_Discr,
5570 Scope_Id => Current_Scope);
5571 end if;
5572 end if;
5574 if not Conforming_Types
5575 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5576 then
5577 Conformance_Error ("type of & does not match!", New_Discr_Id);
5578 return;
5579 else
5580 -- Treat the new discriminant as an occurrence of the old one,
5581 -- for navigation purposes, and fill in some semantic
5582 -- information, for completeness.
5584 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5585 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5586 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5587 end if;
5589 -- Names must match
5591 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5592 Conformance_Error ("name & does not match!", New_Discr_Id);
5593 return;
5594 end if;
5596 -- Default expressions must match
5598 declare
5599 NewD : constant Boolean :=
5600 Present (Expression (New_Discr));
5601 OldD : constant Boolean :=
5602 Present (Expression (Parent (Old_Discr)));
5604 begin
5605 if NewD or OldD then
5607 -- The old default value has been analyzed and expanded,
5608 -- because the current full declaration will have frozen
5609 -- everything before. The new default values have not been
5610 -- expanded, so expand now to check conformance.
5612 if NewD then
5613 Preanalyze_Spec_Expression
5614 (Expression (New_Discr), New_Discr_Type);
5615 end if;
5617 if not (NewD and OldD)
5618 or else not Fully_Conformant_Expressions
5619 (Expression (Parent (Old_Discr)),
5620 Expression (New_Discr))
5622 then
5623 Conformance_Error
5624 ("default expression for & does not match!",
5625 New_Discr_Id);
5626 return;
5627 end if;
5628 end if;
5629 end;
5631 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5633 if Ada_Version = Ada_83 then
5634 declare
5635 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5637 begin
5638 -- Grouping (use of comma in param lists) must be the same
5639 -- This is where we catch a misconformance like:
5641 -- A, B : Integer
5642 -- A : Integer; B : Integer
5644 -- which are represented identically in the tree except
5645 -- for the setting of the flags More_Ids and Prev_Ids.
5647 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5648 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5649 then
5650 Conformance_Error
5651 ("grouping of & does not match!", New_Discr_Id);
5652 return;
5653 end if;
5654 end;
5655 end if;
5657 Next_Discriminant (Old_Discr);
5658 Next (New_Discr);
5659 end loop;
5661 if Present (Old_Discr) then
5662 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5663 return;
5665 elsif Present (New_Discr) then
5666 Conformance_Error
5667 ("too many discriminants!", Defining_Identifier (New_Discr));
5668 return;
5669 end if;
5670 end Check_Discriminant_Conformance;
5672 ----------------------------
5673 -- Check_Fully_Conformant --
5674 ----------------------------
5676 procedure Check_Fully_Conformant
5677 (New_Id : Entity_Id;
5678 Old_Id : Entity_Id;
5679 Err_Loc : Node_Id := Empty)
5681 Result : Boolean;
5682 pragma Warnings (Off, Result);
5683 begin
5684 Check_Conformance
5685 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5686 end Check_Fully_Conformant;
5688 ---------------------------
5689 -- Check_Mode_Conformant --
5690 ---------------------------
5692 procedure Check_Mode_Conformant
5693 (New_Id : Entity_Id;
5694 Old_Id : Entity_Id;
5695 Err_Loc : Node_Id := Empty;
5696 Get_Inst : Boolean := False)
5698 Result : Boolean;
5699 pragma Warnings (Off, Result);
5700 begin
5701 Check_Conformance
5702 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5703 end Check_Mode_Conformant;
5705 --------------------------------
5706 -- Check_Overriding_Indicator --
5707 --------------------------------
5709 procedure Check_Overriding_Indicator
5710 (Subp : Entity_Id;
5711 Overridden_Subp : Entity_Id;
5712 Is_Primitive : Boolean)
5714 Decl : Node_Id;
5715 Spec : Node_Id;
5717 begin
5718 -- No overriding indicator for literals
5720 if Ekind (Subp) = E_Enumeration_Literal then
5721 return;
5723 elsif Ekind (Subp) = E_Entry then
5724 Decl := Parent (Subp);
5726 -- No point in analyzing a malformed operator
5728 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5729 and then Error_Posted (Subp)
5730 then
5731 return;
5733 else
5734 Decl := Unit_Declaration_Node (Subp);
5735 end if;
5737 if Nkind_In (Decl, N_Subprogram_Body,
5738 N_Subprogram_Body_Stub,
5739 N_Subprogram_Declaration,
5740 N_Abstract_Subprogram_Declaration,
5741 N_Subprogram_Renaming_Declaration)
5742 then
5743 Spec := Specification (Decl);
5745 elsif Nkind (Decl) = N_Entry_Declaration then
5746 Spec := Decl;
5748 else
5749 return;
5750 end if;
5752 -- The overriding operation is type conformant with the overridden one,
5753 -- but the names of the formals are not required to match. If the names
5754 -- appear permuted in the overriding operation, this is a possible
5755 -- source of confusion that is worth diagnosing. Controlling formals
5756 -- often carry names that reflect the type, and it is not worthwhile
5757 -- requiring that their names match.
5759 if Present (Overridden_Subp)
5760 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5761 then
5762 declare
5763 Form1 : Entity_Id;
5764 Form2 : Entity_Id;
5766 begin
5767 Form1 := First_Formal (Subp);
5768 Form2 := First_Formal (Overridden_Subp);
5770 -- If the overriding operation is a synchronized operation, skip
5771 -- the first parameter of the overridden operation, which is
5772 -- implicit in the new one. If the operation is declared in the
5773 -- body it is not primitive and all formals must match.
5775 if Is_Concurrent_Type (Scope (Subp))
5776 and then Is_Tagged_Type (Scope (Subp))
5777 and then not Has_Completion (Scope (Subp))
5778 then
5779 Form2 := Next_Formal (Form2);
5780 end if;
5782 if Present (Form1) then
5783 Form1 := Next_Formal (Form1);
5784 Form2 := Next_Formal (Form2);
5785 end if;
5787 while Present (Form1) loop
5788 if not Is_Controlling_Formal (Form1)
5789 and then Present (Next_Formal (Form2))
5790 and then Chars (Form1) = Chars (Next_Formal (Form2))
5791 then
5792 Error_Msg_Node_2 := Alias (Overridden_Subp);
5793 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5794 Error_Msg_NE
5795 ("& does not match corresponding formal of&#",
5796 Form1, Form1);
5797 exit;
5798 end if;
5800 Next_Formal (Form1);
5801 Next_Formal (Form2);
5802 end loop;
5803 end;
5804 end if;
5806 -- If there is an overridden subprogram, then check that there is no
5807 -- "not overriding" indicator, and mark the subprogram as overriding.
5808 -- This is not done if the overridden subprogram is marked as hidden,
5809 -- which can occur for the case of inherited controlled operations
5810 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5811 -- subprogram is not itself hidden. (Note: This condition could probably
5812 -- be simplified, leaving out the testing for the specific controlled
5813 -- cases, but it seems safer and clearer this way, and echoes similar
5814 -- special-case tests of this kind in other places.)
5816 if Present (Overridden_Subp)
5817 and then (not Is_Hidden (Overridden_Subp)
5818 or else
5819 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5820 Name_Adjust,
5821 Name_Finalize)
5822 and then Present (Alias (Overridden_Subp))
5823 and then not Is_Hidden (Alias (Overridden_Subp))))
5824 then
5825 if Must_Not_Override (Spec) then
5826 Error_Msg_Sloc := Sloc (Overridden_Subp);
5828 if Ekind (Subp) = E_Entry then
5829 Error_Msg_NE
5830 ("entry & overrides inherited operation #", Spec, Subp);
5831 else
5832 Error_Msg_NE
5833 ("subprogram & overrides inherited operation #", Spec, Subp);
5834 end if;
5836 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5837 -- as an extension of Root_Controlled, and thus has a useless Adjust
5838 -- operation. This operation should not be inherited by other limited
5839 -- controlled types. An explicit Adjust for them is not overriding.
5841 elsif Must_Override (Spec)
5842 and then Chars (Overridden_Subp) = Name_Adjust
5843 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5844 and then Present (Alias (Overridden_Subp))
5845 and then
5846 Is_Predefined_File_Name
5847 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5848 then
5849 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5851 elsif Is_Subprogram (Subp) then
5852 if Is_Init_Proc (Subp) then
5853 null;
5855 elsif No (Overridden_Operation (Subp)) then
5857 -- For entities generated by Derive_Subprograms the overridden
5858 -- operation is the inherited primitive (which is available
5859 -- through the attribute alias)
5861 if (Is_Dispatching_Operation (Subp)
5862 or else Is_Dispatching_Operation (Overridden_Subp))
5863 and then not Comes_From_Source (Overridden_Subp)
5864 and then Find_Dispatching_Type (Overridden_Subp) =
5865 Find_Dispatching_Type (Subp)
5866 and then Present (Alias (Overridden_Subp))
5867 and then Comes_From_Source (Alias (Overridden_Subp))
5868 then
5869 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5870 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5872 else
5873 Set_Overridden_Operation (Subp, Overridden_Subp);
5874 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5875 end if;
5876 end if;
5877 end if;
5879 -- If primitive flag is set or this is a protected operation, then
5880 -- the operation is overriding at the point of its declaration, so
5881 -- warn if necessary. Otherwise it may have been declared before the
5882 -- operation it overrides and no check is required.
5884 if Style_Check
5885 and then not Must_Override (Spec)
5886 and then (Is_Primitive
5887 or else Ekind (Scope (Subp)) = E_Protected_Type)
5888 then
5889 Style.Missing_Overriding (Decl, Subp);
5890 end if;
5892 -- If Subp is an operator, it may override a predefined operation, if
5893 -- it is defined in the same scope as the type to which it applies.
5894 -- In that case Overridden_Subp is empty because of our implicit
5895 -- representation for predefined operators. We have to check whether the
5896 -- signature of Subp matches that of a predefined operator. Note that
5897 -- first argument provides the name of the operator, and the second
5898 -- argument the signature that may match that of a standard operation.
5899 -- If the indicator is overriding, then the operator must match a
5900 -- predefined signature, because we know already that there is no
5901 -- explicit overridden operation.
5903 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5904 if Must_Not_Override (Spec) then
5906 -- If this is not a primitive or a protected subprogram, then
5907 -- "not overriding" is illegal.
5909 if not Is_Primitive
5910 and then Ekind (Scope (Subp)) /= E_Protected_Type
5911 then
5912 Error_Msg_N ("overriding indicator only allowed "
5913 & "if subprogram is primitive", Subp);
5915 elsif Can_Override_Operator (Subp) then
5916 Error_Msg_NE
5917 ("subprogram& overrides predefined operator ", Spec, Subp);
5918 end if;
5920 elsif Must_Override (Spec) then
5921 if No (Overridden_Operation (Subp))
5922 and then not Can_Override_Operator (Subp)
5923 then
5924 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5925 end if;
5927 elsif not Error_Posted (Subp)
5928 and then Style_Check
5929 and then Can_Override_Operator (Subp)
5930 and then
5931 not Is_Predefined_File_Name
5932 (Unit_File_Name (Get_Source_Unit (Subp)))
5933 then
5934 -- If style checks are enabled, indicate that the indicator is
5935 -- missing. However, at the point of declaration, the type of
5936 -- which this is a primitive operation may be private, in which
5937 -- case the indicator would be premature.
5939 if Has_Private_Declaration (Etype (Subp))
5940 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5941 then
5942 null;
5943 else
5944 Style.Missing_Overriding (Decl, Subp);
5945 end if;
5946 end if;
5948 elsif Must_Override (Spec) then
5949 if Ekind (Subp) = E_Entry then
5950 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5951 else
5952 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5953 end if;
5955 -- If the operation is marked "not overriding" and it's not primitive
5956 -- then an error is issued, unless this is an operation of a task or
5957 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5958 -- has been specified have already been checked above.
5960 elsif Must_Not_Override (Spec)
5961 and then not Is_Primitive
5962 and then Ekind (Subp) /= E_Entry
5963 and then Ekind (Scope (Subp)) /= E_Protected_Type
5964 then
5965 Error_Msg_N
5966 ("overriding indicator only allowed if subprogram is primitive",
5967 Subp);
5968 return;
5969 end if;
5970 end Check_Overriding_Indicator;
5972 -------------------
5973 -- Check_Returns --
5974 -------------------
5976 -- Note: this procedure needs to know far too much about how the expander
5977 -- messes with exceptions. The use of the flag Exception_Junk and the
5978 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5979 -- works, but is not very clean. It would be better if the expansion
5980 -- routines would leave Original_Node working nicely, and we could use
5981 -- Original_Node here to ignore all the peculiar expander messing ???
5983 procedure Check_Returns
5984 (HSS : Node_Id;
5985 Mode : Character;
5986 Err : out Boolean;
5987 Proc : Entity_Id := Empty)
5989 Handler : Node_Id;
5991 procedure Check_Statement_Sequence (L : List_Id);
5992 -- Internal recursive procedure to check a list of statements for proper
5993 -- termination by a return statement (or a transfer of control or a
5994 -- compound statement that is itself internally properly terminated).
5996 ------------------------------
5997 -- Check_Statement_Sequence --
5998 ------------------------------
6000 procedure Check_Statement_Sequence (L : List_Id) is
6001 Last_Stm : Node_Id;
6002 Stm : Node_Id;
6003 Kind : Node_Kind;
6005 function Assert_False return Boolean;
6006 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6007 -- rewritten as a null statement when assertions are off. The assert
6008 -- is not active, but it is still enough to kill the warning.
6010 ------------------
6011 -- Assert_False --
6012 ------------------
6014 function Assert_False return Boolean is
6015 Orig : constant Node_Id := Original_Node (Last_Stm);
6017 begin
6018 if Nkind (Orig) = N_Pragma
6019 and then Pragma_Name (Orig) = Name_Assert
6020 and then not Error_Posted (Orig)
6021 then
6022 declare
6023 Arg : constant Node_Id :=
6024 First (Pragma_Argument_Associations (Orig));
6025 Exp : constant Node_Id := Expression (Arg);
6026 begin
6027 return Nkind (Exp) = N_Identifier
6028 and then Chars (Exp) = Name_False;
6029 end;
6031 else
6032 return False;
6033 end if;
6034 end Assert_False;
6036 -- Local variables
6038 Raise_Exception_Call : Boolean;
6039 -- Set True if statement sequence terminated by Raise_Exception call
6040 -- or a Reraise_Occurrence call.
6042 -- Start of processing for Check_Statement_Sequence
6044 begin
6045 Raise_Exception_Call := False;
6047 -- Get last real statement
6049 Last_Stm := Last (L);
6051 -- Deal with digging out exception handler statement sequences that
6052 -- have been transformed by the local raise to goto optimization.
6053 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6054 -- optimization has occurred, we are looking at something like:
6056 -- begin
6057 -- original stmts in block
6059 -- exception \
6060 -- when excep1 => |
6061 -- goto L1; | omitted if No_Exception_Propagation
6062 -- when excep2 => |
6063 -- goto L2; /
6064 -- end;
6066 -- goto L3; -- skip handler when exception not raised
6068 -- <<L1>> -- target label for local exception
6069 -- begin
6070 -- estmts1
6071 -- end;
6073 -- goto L3;
6075 -- <<L2>>
6076 -- begin
6077 -- estmts2
6078 -- end;
6080 -- <<L3>>
6082 -- and what we have to do is to dig out the estmts1 and estmts2
6083 -- sequences (which were the original sequences of statements in
6084 -- the exception handlers) and check them.
6086 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6087 Stm := Last_Stm;
6088 loop
6089 Prev (Stm);
6090 exit when No (Stm);
6091 exit when Nkind (Stm) /= N_Block_Statement;
6092 exit when not Exception_Junk (Stm);
6093 Prev (Stm);
6094 exit when No (Stm);
6095 exit when Nkind (Stm) /= N_Label;
6096 exit when not Exception_Junk (Stm);
6097 Check_Statement_Sequence
6098 (Statements (Handled_Statement_Sequence (Next (Stm))));
6100 Prev (Stm);
6101 Last_Stm := Stm;
6102 exit when No (Stm);
6103 exit when Nkind (Stm) /= N_Goto_Statement;
6104 exit when not Exception_Junk (Stm);
6105 end loop;
6106 end if;
6108 -- Don't count pragmas
6110 while Nkind (Last_Stm) = N_Pragma
6112 -- Don't count call to SS_Release (can happen after Raise_Exception)
6114 or else
6115 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6116 and then
6117 Nkind (Name (Last_Stm)) = N_Identifier
6118 and then
6119 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6121 -- Don't count exception junk
6123 or else
6124 (Nkind_In (Last_Stm, N_Goto_Statement,
6125 N_Label,
6126 N_Object_Declaration)
6127 and then Exception_Junk (Last_Stm))
6128 or else Nkind (Last_Stm) in N_Push_xxx_Label
6129 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6131 -- Inserted code, such as finalization calls, is irrelevant: we only
6132 -- need to check original source.
6134 or else Is_Rewrite_Insertion (Last_Stm)
6135 loop
6136 Prev (Last_Stm);
6137 end loop;
6139 -- Here we have the "real" last statement
6141 Kind := Nkind (Last_Stm);
6143 -- Transfer of control, OK. Note that in the No_Return procedure
6144 -- case, we already diagnosed any explicit return statements, so
6145 -- we can treat them as OK in this context.
6147 if Is_Transfer (Last_Stm) then
6148 return;
6150 -- Check cases of explicit non-indirect procedure calls
6152 elsif Kind = N_Procedure_Call_Statement
6153 and then Is_Entity_Name (Name (Last_Stm))
6154 then
6155 -- Check call to Raise_Exception procedure which is treated
6156 -- specially, as is a call to Reraise_Occurrence.
6158 -- We suppress the warning in these cases since it is likely that
6159 -- the programmer really does not expect to deal with the case
6160 -- of Null_Occurrence, and thus would find a warning about a
6161 -- missing return curious, and raising Program_Error does not
6162 -- seem such a bad behavior if this does occur.
6164 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6165 -- behavior will be to raise Constraint_Error (see AI-329).
6167 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6168 or else
6169 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6170 then
6171 Raise_Exception_Call := True;
6173 -- For Raise_Exception call, test first argument, if it is
6174 -- an attribute reference for a 'Identity call, then we know
6175 -- that the call cannot possibly return.
6177 declare
6178 Arg : constant Node_Id :=
6179 Original_Node (First_Actual (Last_Stm));
6180 begin
6181 if Nkind (Arg) = N_Attribute_Reference
6182 and then Attribute_Name (Arg) = Name_Identity
6183 then
6184 return;
6185 end if;
6186 end;
6187 end if;
6189 -- If statement, need to look inside if there is an else and check
6190 -- each constituent statement sequence for proper termination.
6192 elsif Kind = N_If_Statement
6193 and then Present (Else_Statements (Last_Stm))
6194 then
6195 Check_Statement_Sequence (Then_Statements (Last_Stm));
6196 Check_Statement_Sequence (Else_Statements (Last_Stm));
6198 if Present (Elsif_Parts (Last_Stm)) then
6199 declare
6200 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6202 begin
6203 while Present (Elsif_Part) loop
6204 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6205 Next (Elsif_Part);
6206 end loop;
6207 end;
6208 end if;
6210 return;
6212 -- Case statement, check each case for proper termination
6214 elsif Kind = N_Case_Statement then
6215 declare
6216 Case_Alt : Node_Id;
6217 begin
6218 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6219 while Present (Case_Alt) loop
6220 Check_Statement_Sequence (Statements (Case_Alt));
6221 Next_Non_Pragma (Case_Alt);
6222 end loop;
6223 end;
6225 return;
6227 -- Block statement, check its handled sequence of statements
6229 elsif Kind = N_Block_Statement then
6230 declare
6231 Err1 : Boolean;
6233 begin
6234 Check_Returns
6235 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6237 if Err1 then
6238 Err := True;
6239 end if;
6241 return;
6242 end;
6244 -- Loop statement. If there is an iteration scheme, we can definitely
6245 -- fall out of the loop. Similarly if there is an exit statement, we
6246 -- can fall out. In either case we need a following return.
6248 elsif Kind = N_Loop_Statement then
6249 if Present (Iteration_Scheme (Last_Stm))
6250 or else Has_Exit (Entity (Identifier (Last_Stm)))
6251 then
6252 null;
6254 -- A loop with no exit statement or iteration scheme is either
6255 -- an infinite loop, or it has some other exit (raise/return).
6256 -- In either case, no warning is required.
6258 else
6259 return;
6260 end if;
6262 -- Timed entry call, check entry call and delay alternatives
6264 -- Note: in expanded code, the timed entry call has been converted
6265 -- to a set of expanded statements on which the check will work
6266 -- correctly in any case.
6268 elsif Kind = N_Timed_Entry_Call then
6269 declare
6270 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6271 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6273 begin
6274 -- If statement sequence of entry call alternative is missing,
6275 -- then we can definitely fall through, and we post the error
6276 -- message on the entry call alternative itself.
6278 if No (Statements (ECA)) then
6279 Last_Stm := ECA;
6281 -- If statement sequence of delay alternative is missing, then
6282 -- we can definitely fall through, and we post the error
6283 -- message on the delay alternative itself.
6285 -- Note: if both ECA and DCA are missing the return, then we
6286 -- post only one message, should be enough to fix the bugs.
6287 -- If not we will get a message next time on the DCA when the
6288 -- ECA is fixed.
6290 elsif No (Statements (DCA)) then
6291 Last_Stm := DCA;
6293 -- Else check both statement sequences
6295 else
6296 Check_Statement_Sequence (Statements (ECA));
6297 Check_Statement_Sequence (Statements (DCA));
6298 return;
6299 end if;
6300 end;
6302 -- Conditional entry call, check entry call and else part
6304 -- Note: in expanded code, the conditional entry call has been
6305 -- converted to a set of expanded statements on which the check
6306 -- will work correctly in any case.
6308 elsif Kind = N_Conditional_Entry_Call then
6309 declare
6310 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6312 begin
6313 -- If statement sequence of entry call alternative is missing,
6314 -- then we can definitely fall through, and we post the error
6315 -- message on the entry call alternative itself.
6317 if No (Statements (ECA)) then
6318 Last_Stm := ECA;
6320 -- Else check statement sequence and else part
6322 else
6323 Check_Statement_Sequence (Statements (ECA));
6324 Check_Statement_Sequence (Else_Statements (Last_Stm));
6325 return;
6326 end if;
6327 end;
6328 end if;
6330 -- If we fall through, issue appropriate message
6332 if Mode = 'F' then
6334 -- Kill warning if last statement is a raise exception call,
6335 -- or a pragma Assert (False). Note that with assertions enabled,
6336 -- such a pragma has been converted into a raise exception call
6337 -- already, so the Assert_False is for the assertions off case.
6339 if not Raise_Exception_Call and then not Assert_False then
6341 -- In GNATprove mode, it is an error to have a missing return
6343 Error_Msg_Warn := SPARK_Mode /= On;
6345 -- Issue error message or warning
6347 Error_Msg_N
6348 ("RETURN statement missing following this statement<<!",
6349 Last_Stm);
6350 Error_Msg_N
6351 ("\Program_Error ]<<!", Last_Stm);
6352 end if;
6354 -- Note: we set Err even though we have not issued a warning
6355 -- because we still have a case of a missing return. This is
6356 -- an extremely marginal case, probably will never be noticed
6357 -- but we might as well get it right.
6359 Err := True;
6361 -- Otherwise we have the case of a procedure marked No_Return
6363 else
6364 if not Raise_Exception_Call then
6365 if GNATprove_Mode then
6366 Error_Msg_N
6367 ("implied return after this statement "
6368 & "would have raised Program_Error", Last_Stm);
6369 else
6370 Error_Msg_N
6371 ("implied return after this statement "
6372 & "will raise Program_Error??", Last_Stm);
6373 end if;
6375 Error_Msg_Warn := SPARK_Mode /= On;
6376 Error_Msg_NE
6377 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6378 end if;
6380 declare
6381 RE : constant Node_Id :=
6382 Make_Raise_Program_Error (Sloc (Last_Stm),
6383 Reason => PE_Implicit_Return);
6384 begin
6385 Insert_After (Last_Stm, RE);
6386 Analyze (RE);
6387 end;
6388 end if;
6389 end Check_Statement_Sequence;
6391 -- Start of processing for Check_Returns
6393 begin
6394 Err := False;
6395 Check_Statement_Sequence (Statements (HSS));
6397 if Present (Exception_Handlers (HSS)) then
6398 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6399 while Present (Handler) loop
6400 Check_Statement_Sequence (Statements (Handler));
6401 Next_Non_Pragma (Handler);
6402 end loop;
6403 end if;
6404 end Check_Returns;
6406 ----------------------------
6407 -- Check_Subprogram_Order --
6408 ----------------------------
6410 procedure Check_Subprogram_Order (N : Node_Id) is
6412 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6413 -- This is used to check if S1 > S2 in the sense required by this test,
6414 -- for example nameab < namec, but name2 < name10.
6416 -----------------------------
6417 -- Subprogram_Name_Greater --
6418 -----------------------------
6420 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6421 L1, L2 : Positive;
6422 N1, N2 : Natural;
6424 begin
6425 -- Deal with special case where names are identical except for a
6426 -- numerical suffix. These are handled specially, taking the numeric
6427 -- ordering from the suffix into account.
6429 L1 := S1'Last;
6430 while S1 (L1) in '0' .. '9' loop
6431 L1 := L1 - 1;
6432 end loop;
6434 L2 := S2'Last;
6435 while S2 (L2) in '0' .. '9' loop
6436 L2 := L2 - 1;
6437 end loop;
6439 -- If non-numeric parts non-equal, do straight compare
6441 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6442 return S1 > S2;
6444 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6445 -- that a missing suffix is treated as numeric zero in this test.
6447 else
6448 N1 := 0;
6449 while L1 < S1'Last loop
6450 L1 := L1 + 1;
6451 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6452 end loop;
6454 N2 := 0;
6455 while L2 < S2'Last loop
6456 L2 := L2 + 1;
6457 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6458 end loop;
6460 return N1 > N2;
6461 end if;
6462 end Subprogram_Name_Greater;
6464 -- Start of processing for Check_Subprogram_Order
6466 begin
6467 -- Check body in alpha order if this is option
6469 if Style_Check
6470 and then Style_Check_Order_Subprograms
6471 and then Nkind (N) = N_Subprogram_Body
6472 and then Comes_From_Source (N)
6473 and then In_Extended_Main_Source_Unit (N)
6474 then
6475 declare
6476 LSN : String_Ptr
6477 renames Scope_Stack.Table
6478 (Scope_Stack.Last).Last_Subprogram_Name;
6480 Body_Id : constant Entity_Id :=
6481 Defining_Entity (Specification (N));
6483 begin
6484 Get_Decoded_Name_String (Chars (Body_Id));
6486 if LSN /= null then
6487 if Subprogram_Name_Greater
6488 (LSN.all, Name_Buffer (1 .. Name_Len))
6489 then
6490 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6491 end if;
6493 Free (LSN);
6494 end if;
6496 LSN := new String'(Name_Buffer (1 .. Name_Len));
6497 end;
6498 end if;
6499 end Check_Subprogram_Order;
6501 ------------------------------
6502 -- Check_Subtype_Conformant --
6503 ------------------------------
6505 procedure Check_Subtype_Conformant
6506 (New_Id : Entity_Id;
6507 Old_Id : Entity_Id;
6508 Err_Loc : Node_Id := Empty;
6509 Skip_Controlling_Formals : Boolean := False;
6510 Get_Inst : Boolean := False)
6512 Result : Boolean;
6513 pragma Warnings (Off, Result);
6514 begin
6515 Check_Conformance
6516 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6517 Skip_Controlling_Formals => Skip_Controlling_Formals,
6518 Get_Inst => Get_Inst);
6519 end Check_Subtype_Conformant;
6521 ---------------------------
6522 -- Check_Type_Conformant --
6523 ---------------------------
6525 procedure Check_Type_Conformant
6526 (New_Id : Entity_Id;
6527 Old_Id : Entity_Id;
6528 Err_Loc : Node_Id := Empty)
6530 Result : Boolean;
6531 pragma Warnings (Off, Result);
6532 begin
6533 Check_Conformance
6534 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6535 end Check_Type_Conformant;
6537 ---------------------------
6538 -- Can_Override_Operator --
6539 ---------------------------
6541 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6542 Typ : Entity_Id;
6544 begin
6545 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6546 return False;
6548 else
6549 Typ := Base_Type (Etype (First_Formal (Subp)));
6551 -- Check explicitly that the operation is a primitive of the type
6553 return Operator_Matches_Spec (Subp, Subp)
6554 and then not Is_Generic_Type (Typ)
6555 and then Scope (Subp) = Scope (Typ)
6556 and then not Is_Class_Wide_Type (Typ);
6557 end if;
6558 end Can_Override_Operator;
6560 ----------------------
6561 -- Conforming_Types --
6562 ----------------------
6564 function Conforming_Types
6565 (T1 : Entity_Id;
6566 T2 : Entity_Id;
6567 Ctype : Conformance_Type;
6568 Get_Inst : Boolean := False) return Boolean
6570 Type_1 : Entity_Id := T1;
6571 Type_2 : Entity_Id := T2;
6572 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6574 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6575 -- If neither T1 nor T2 are generic actual types, or if they are in
6576 -- different scopes (e.g. parent and child instances), then verify that
6577 -- the base types are equal. Otherwise T1 and T2 must be on the same
6578 -- subtype chain. The whole purpose of this procedure is to prevent
6579 -- spurious ambiguities in an instantiation that may arise if two
6580 -- distinct generic types are instantiated with the same actual.
6582 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6583 -- An access parameter can designate an incomplete type. If the
6584 -- incomplete type is the limited view of a type from a limited_
6585 -- with_clause, check whether the non-limited view is available. If
6586 -- it is a (non-limited) incomplete type, get the full view.
6588 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6589 -- Returns True if and only if either T1 denotes a limited view of T2
6590 -- or T2 denotes a limited view of T1. This can arise when the limited
6591 -- with view of a type is used in a subprogram declaration and the
6592 -- subprogram body is in the scope of a regular with clause for the
6593 -- same unit. In such a case, the two type entities can be considered
6594 -- identical for purposes of conformance checking.
6596 ----------------------
6597 -- Base_Types_Match --
6598 ----------------------
6600 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6601 BT1 : constant Entity_Id := Base_Type (T1);
6602 BT2 : constant Entity_Id := Base_Type (T2);
6604 begin
6605 if T1 = T2 then
6606 return True;
6608 elsif BT1 = BT2 then
6610 -- The following is too permissive. A more precise test should
6611 -- check that the generic actual is an ancestor subtype of the
6612 -- other ???.
6614 -- See code in Find_Corresponding_Spec that applies an additional
6615 -- filter to handle accidental amiguities in instances.
6617 return not Is_Generic_Actual_Type (T1)
6618 or else not Is_Generic_Actual_Type (T2)
6619 or else Scope (T1) /= Scope (T2);
6621 -- If T2 is a generic actual type it is declared as the subtype of
6622 -- the actual. If that actual is itself a subtype we need to use its
6623 -- own base type to check for compatibility.
6625 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6626 return True;
6628 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6629 return True;
6631 else
6632 return False;
6633 end if;
6634 end Base_Types_Match;
6636 --------------------------
6637 -- Find_Designated_Type --
6638 --------------------------
6640 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6641 Desig : Entity_Id;
6643 begin
6644 Desig := Directly_Designated_Type (T);
6646 if Ekind (Desig) = E_Incomplete_Type then
6648 -- If regular incomplete type, get full view if available
6650 if Present (Full_View (Desig)) then
6651 Desig := Full_View (Desig);
6653 -- If limited view of a type, get non-limited view if available,
6654 -- and check again for a regular incomplete type.
6656 elsif Present (Non_Limited_View (Desig)) then
6657 Desig := Get_Full_View (Non_Limited_View (Desig));
6658 end if;
6659 end if;
6661 return Desig;
6662 end Find_Designated_Type;
6664 -------------------------------
6665 -- Matches_Limited_With_View --
6666 -------------------------------
6668 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6669 begin
6670 -- In some cases a type imported through a limited_with clause, and
6671 -- its nonlimited view are both visible, for example in an anonymous
6672 -- access-to-class-wide type in a formal, or when building the body
6673 -- for a subprogram renaming after the subprogram has been frozen.
6674 -- In these cases Both entities designate the same type. In addition,
6675 -- if one of them is an actual in an instance, it may be a subtype of
6676 -- the non-limited view of the other.
6678 if From_Limited_With (T1)
6679 and then (T2 = Available_View (T1)
6680 or else Is_Subtype_Of (T2, Available_View (T1)))
6681 then
6682 return True;
6684 elsif From_Limited_With (T2)
6685 and then (T1 = Available_View (T2)
6686 or else Is_Subtype_Of (T1, Available_View (T2)))
6687 then
6688 return True;
6690 elsif From_Limited_With (T1)
6691 and then From_Limited_With (T2)
6692 and then Available_View (T1) = Available_View (T2)
6693 then
6694 return True;
6696 else
6697 return False;
6698 end if;
6699 end Matches_Limited_With_View;
6701 -- Start of processing for Conforming_Types
6703 begin
6704 -- The context is an instance association for a formal access-to-
6705 -- subprogram type; the formal parameter types require mapping because
6706 -- they may denote other formal parameters of the generic unit.
6708 if Get_Inst then
6709 Type_1 := Get_Instance_Of (T1);
6710 Type_2 := Get_Instance_Of (T2);
6711 end if;
6713 -- If one of the types is a view of the other introduced by a limited
6714 -- with clause, treat these as conforming for all purposes.
6716 if Matches_Limited_With_View (T1, T2) then
6717 return True;
6719 elsif Base_Types_Match (Type_1, Type_2) then
6720 return Ctype <= Mode_Conformant
6721 or else Subtypes_Statically_Match (Type_1, Type_2);
6723 elsif Is_Incomplete_Or_Private_Type (Type_1)
6724 and then Present (Full_View (Type_1))
6725 and then Base_Types_Match (Full_View (Type_1), Type_2)
6726 then
6727 return Ctype <= Mode_Conformant
6728 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6730 elsif Ekind (Type_2) = E_Incomplete_Type
6731 and then Present (Full_View (Type_2))
6732 and then Base_Types_Match (Type_1, Full_View (Type_2))
6733 then
6734 return Ctype <= Mode_Conformant
6735 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6737 elsif Is_Private_Type (Type_2)
6738 and then In_Instance
6739 and then Present (Full_View (Type_2))
6740 and then Base_Types_Match (Type_1, Full_View (Type_2))
6741 then
6742 return Ctype <= Mode_Conformant
6743 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6744 end if;
6746 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6747 -- treated recursively because they carry a signature. As far as
6748 -- conformance is concerned, convention plays no role, and either
6749 -- or both could be access to protected subprograms.
6751 Are_Anonymous_Access_To_Subprogram_Types :=
6752 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6753 E_Anonymous_Access_Protected_Subprogram_Type)
6754 and then
6755 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6756 E_Anonymous_Access_Protected_Subprogram_Type);
6758 -- Test anonymous access type case. For this case, static subtype
6759 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6760 -- the base types because we may have built internal subtype entities
6761 -- to handle null-excluding types (see Process_Formals).
6763 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6764 and then
6765 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6767 -- Ada 2005 (AI-254)
6769 or else Are_Anonymous_Access_To_Subprogram_Types
6770 then
6771 declare
6772 Desig_1 : Entity_Id;
6773 Desig_2 : Entity_Id;
6775 begin
6776 -- In Ada 2005, access constant indicators must match for
6777 -- subtype conformance.
6779 if Ada_Version >= Ada_2005
6780 and then Ctype >= Subtype_Conformant
6781 and then
6782 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6783 then
6784 return False;
6785 end if;
6787 Desig_1 := Find_Designated_Type (Type_1);
6788 Desig_2 := Find_Designated_Type (Type_2);
6790 -- If the context is an instance association for a formal
6791 -- access-to-subprogram type; formal access parameter designated
6792 -- types require mapping because they may denote other formal
6793 -- parameters of the generic unit.
6795 if Get_Inst then
6796 Desig_1 := Get_Instance_Of (Desig_1);
6797 Desig_2 := Get_Instance_Of (Desig_2);
6798 end if;
6800 -- It is possible for a Class_Wide_Type to be introduced for an
6801 -- incomplete type, in which case there is a separate class_ wide
6802 -- type for the full view. The types conform if their Etypes
6803 -- conform, i.e. one may be the full view of the other. This can
6804 -- only happen in the context of an access parameter, other uses
6805 -- of an incomplete Class_Wide_Type are illegal.
6807 if Is_Class_Wide_Type (Desig_1)
6808 and then
6809 Is_Class_Wide_Type (Desig_2)
6810 then
6811 return
6812 Conforming_Types
6813 (Etype (Base_Type (Desig_1)),
6814 Etype (Base_Type (Desig_2)), Ctype);
6816 elsif Are_Anonymous_Access_To_Subprogram_Types then
6817 if Ada_Version < Ada_2005 then
6818 return Ctype = Type_Conformant
6819 or else
6820 Subtypes_Statically_Match (Desig_1, Desig_2);
6822 -- We must check the conformance of the signatures themselves
6824 else
6825 declare
6826 Conformant : Boolean;
6827 begin
6828 Check_Conformance
6829 (Desig_1, Desig_2, Ctype, False, Conformant);
6830 return Conformant;
6831 end;
6832 end if;
6834 else
6835 return Base_Type (Desig_1) = Base_Type (Desig_2)
6836 and then (Ctype = Type_Conformant
6837 or else
6838 Subtypes_Statically_Match (Desig_1, Desig_2));
6839 end if;
6840 end;
6842 -- Otherwise definitely no match
6844 else
6845 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6846 and then Is_Access_Type (Type_2))
6847 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6848 and then Is_Access_Type (Type_1)))
6849 and then
6850 Conforming_Types
6851 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6852 then
6853 May_Hide_Profile := True;
6854 end if;
6856 return False;
6857 end if;
6858 end Conforming_Types;
6860 --------------------------
6861 -- Create_Extra_Formals --
6862 --------------------------
6864 procedure Create_Extra_Formals (E : Entity_Id) is
6865 Formal : Entity_Id;
6866 First_Extra : Entity_Id := Empty;
6867 Last_Extra : Entity_Id;
6868 Formal_Type : Entity_Id;
6869 P_Formal : Entity_Id := Empty;
6871 function Add_Extra_Formal
6872 (Assoc_Entity : Entity_Id;
6873 Typ : Entity_Id;
6874 Scope : Entity_Id;
6875 Suffix : String) return Entity_Id;
6876 -- Add an extra formal to the current list of formals and extra formals.
6877 -- The extra formal is added to the end of the list of extra formals,
6878 -- and also returned as the result. These formals are always of mode IN.
6879 -- The new formal has the type Typ, is declared in Scope, and its name
6880 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6881 -- The following suffixes are currently used. They should not be changed
6882 -- without coordinating with CodePeer, which makes use of these to
6883 -- provide better messages.
6885 -- O denotes the Constrained bit.
6886 -- L denotes the accessibility level.
6887 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6888 -- the full list in exp_ch6.BIP_Formal_Kind.
6890 ----------------------
6891 -- Add_Extra_Formal --
6892 ----------------------
6894 function Add_Extra_Formal
6895 (Assoc_Entity : Entity_Id;
6896 Typ : Entity_Id;
6897 Scope : Entity_Id;
6898 Suffix : String) return Entity_Id
6900 EF : constant Entity_Id :=
6901 Make_Defining_Identifier (Sloc (Assoc_Entity),
6902 Chars => New_External_Name (Chars (Assoc_Entity),
6903 Suffix => Suffix));
6905 begin
6906 -- A little optimization. Never generate an extra formal for the
6907 -- _init operand of an initialization procedure, since it could
6908 -- never be used.
6910 if Chars (Formal) = Name_uInit then
6911 return Empty;
6912 end if;
6914 Set_Ekind (EF, E_In_Parameter);
6915 Set_Actual_Subtype (EF, Typ);
6916 Set_Etype (EF, Typ);
6917 Set_Scope (EF, Scope);
6918 Set_Mechanism (EF, Default_Mechanism);
6919 Set_Formal_Validity (EF);
6921 if No (First_Extra) then
6922 First_Extra := EF;
6923 Set_Extra_Formals (Scope, First_Extra);
6924 end if;
6926 if Present (Last_Extra) then
6927 Set_Extra_Formal (Last_Extra, EF);
6928 end if;
6930 Last_Extra := EF;
6932 return EF;
6933 end Add_Extra_Formal;
6935 -- Start of processing for Create_Extra_Formals
6937 begin
6938 -- We never generate extra formals if expansion is not active because we
6939 -- don't need them unless we are generating code.
6941 if not Expander_Active then
6942 return;
6943 end if;
6945 -- No need to generate extra formals in interface thunks whose target
6946 -- primitive has no extra formals.
6948 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6949 return;
6950 end if;
6952 -- If this is a derived subprogram then the subtypes of the parent
6953 -- subprogram's formal parameters will be used to determine the need
6954 -- for extra formals.
6956 if Is_Overloadable (E) and then Present (Alias (E)) then
6957 P_Formal := First_Formal (Alias (E));
6958 end if;
6960 Last_Extra := Empty;
6961 Formal := First_Formal (E);
6962 while Present (Formal) loop
6963 Last_Extra := Formal;
6964 Next_Formal (Formal);
6965 end loop;
6967 -- If Extra_formals were already created, don't do it again. This
6968 -- situation may arise for subprogram types created as part of
6969 -- dispatching calls (see Expand_Dispatching_Call)
6971 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6972 return;
6973 end if;
6975 -- If the subprogram is a predefined dispatching subprogram then don't
6976 -- generate any extra constrained or accessibility level formals. In
6977 -- general we suppress these for internal subprograms (by not calling
6978 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6979 -- generated stream attributes do get passed through because extra
6980 -- build-in-place formals are needed in some cases (limited 'Input).
6982 if Is_Predefined_Internal_Operation (E) then
6983 goto Test_For_Func_Result_Extras;
6984 end if;
6986 Formal := First_Formal (E);
6987 while Present (Formal) loop
6989 -- Create extra formal for supporting the attribute 'Constrained.
6990 -- The case of a private type view without discriminants also
6991 -- requires the extra formal if the underlying type has defaulted
6992 -- discriminants.
6994 if Ekind (Formal) /= E_In_Parameter then
6995 if Present (P_Formal) then
6996 Formal_Type := Etype (P_Formal);
6997 else
6998 Formal_Type := Etype (Formal);
6999 end if;
7001 -- Do not produce extra formals for Unchecked_Union parameters.
7002 -- Jump directly to the end of the loop.
7004 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7005 goto Skip_Extra_Formal_Generation;
7006 end if;
7008 if not Has_Discriminants (Formal_Type)
7009 and then Ekind (Formal_Type) in Private_Kind
7010 and then Present (Underlying_Type (Formal_Type))
7011 then
7012 Formal_Type := Underlying_Type (Formal_Type);
7013 end if;
7015 -- Suppress the extra formal if formal's subtype is constrained or
7016 -- indefinite, or we're compiling for Ada 2012 and the underlying
7017 -- type is tagged and limited. In Ada 2012, a limited tagged type
7018 -- can have defaulted discriminants, but 'Constrained is required
7019 -- to return True, so the formal is never needed (see AI05-0214).
7020 -- Note that this ensures consistency of calling sequences for
7021 -- dispatching operations when some types in a class have defaults
7022 -- on discriminants and others do not (and requiring the extra
7023 -- formal would introduce distributed overhead).
7025 -- If the type does not have a completion yet, treat as prior to
7026 -- Ada 2012 for consistency.
7028 if Has_Discriminants (Formal_Type)
7029 and then not Is_Constrained (Formal_Type)
7030 and then not Is_Indefinite_Subtype (Formal_Type)
7031 and then (Ada_Version < Ada_2012
7032 or else No (Underlying_Type (Formal_Type))
7033 or else not
7034 (Is_Limited_Type (Formal_Type)
7035 and then
7036 (Is_Tagged_Type
7037 (Underlying_Type (Formal_Type)))))
7038 then
7039 Set_Extra_Constrained
7040 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7041 end if;
7042 end if;
7044 -- Create extra formal for supporting accessibility checking. This
7045 -- is done for both anonymous access formals and formals of named
7046 -- access types that are marked as controlling formals. The latter
7047 -- case can occur when Expand_Dispatching_Call creates a subprogram
7048 -- type and substitutes the types of access-to-class-wide actuals
7049 -- for the anonymous access-to-specific-type of controlling formals.
7050 -- Base_Type is applied because in cases where there is a null
7051 -- exclusion the formal may have an access subtype.
7053 -- This is suppressed if we specifically suppress accessibility
7054 -- checks at the package level for either the subprogram, or the
7055 -- package in which it resides. However, we do not suppress it
7056 -- simply if the scope has accessibility checks suppressed, since
7057 -- this could cause trouble when clients are compiled with a
7058 -- different suppression setting. The explicit checks at the
7059 -- package level are safe from this point of view.
7061 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7062 or else (Is_Controlling_Formal (Formal)
7063 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7064 and then not
7065 (Explicit_Suppress (E, Accessibility_Check)
7066 or else
7067 Explicit_Suppress (Scope (E), Accessibility_Check))
7068 and then
7069 (No (P_Formal)
7070 or else Present (Extra_Accessibility (P_Formal)))
7071 then
7072 Set_Extra_Accessibility
7073 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7074 end if;
7076 -- This label is required when skipping extra formal generation for
7077 -- Unchecked_Union parameters.
7079 <<Skip_Extra_Formal_Generation>>
7081 if Present (P_Formal) then
7082 Next_Formal (P_Formal);
7083 end if;
7085 Next_Formal (Formal);
7086 end loop;
7088 <<Test_For_Func_Result_Extras>>
7090 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7091 -- function call is ... determined by the point of call ...".
7093 if Needs_Result_Accessibility_Level (E) then
7094 Set_Extra_Accessibility_Of_Result
7095 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7096 end if;
7098 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7099 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7101 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7102 declare
7103 Result_Subt : constant Entity_Id := Etype (E);
7104 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7105 Formal_Typ : Entity_Id;
7107 Discard : Entity_Id;
7108 pragma Warnings (Off, Discard);
7110 begin
7111 -- In the case of functions with unconstrained result subtypes,
7112 -- add a 4-state formal indicating whether the return object is
7113 -- allocated by the caller (1), or should be allocated by the
7114 -- callee on the secondary stack (2), in the global heap (3), or
7115 -- in a user-defined storage pool (4). For the moment we just use
7116 -- Natural for the type of this formal. Note that this formal
7117 -- isn't usually needed in the case where the result subtype is
7118 -- constrained, but it is needed when the function has a tagged
7119 -- result, because generally such functions can be called in a
7120 -- dispatching context and such calls must be handled like calls
7121 -- to a class-wide function.
7123 if Needs_BIP_Alloc_Form (E) then
7124 Discard :=
7125 Add_Extra_Formal
7126 (E, Standard_Natural,
7127 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7129 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7130 -- use a user-defined pool. This formal is not added on
7131 -- .NET/JVM/ZFP as those targets do not support pools.
7133 if VM_Target = No_VM
7134 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
7135 then
7136 Discard :=
7137 Add_Extra_Formal
7138 (E, RTE (RE_Root_Storage_Pool_Ptr),
7139 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7140 end if;
7141 end if;
7143 -- In the case of functions whose result type needs finalization,
7144 -- add an extra formal which represents the finalization master.
7146 if Needs_BIP_Finalization_Master (E) then
7147 Discard :=
7148 Add_Extra_Formal
7149 (E, RTE (RE_Finalization_Master_Ptr),
7150 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7151 end if;
7153 -- When the result type contains tasks, add two extra formals: the
7154 -- master of the tasks to be created, and the caller's activation
7155 -- chain.
7157 if Has_Task (Full_Subt) then
7158 Discard :=
7159 Add_Extra_Formal
7160 (E, RTE (RE_Master_Id),
7161 E, BIP_Formal_Suffix (BIP_Task_Master));
7162 Discard :=
7163 Add_Extra_Formal
7164 (E, RTE (RE_Activation_Chain_Access),
7165 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7166 end if;
7168 -- All build-in-place functions get an extra formal that will be
7169 -- passed the address of the return object within the caller.
7171 Formal_Typ :=
7172 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7174 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7175 Set_Etype (Formal_Typ, Formal_Typ);
7176 Set_Depends_On_Private
7177 (Formal_Typ, Has_Private_Component (Formal_Typ));
7178 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7179 Set_Is_Access_Constant (Formal_Typ, False);
7181 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7182 -- the designated type comes from the limited view (for back-end
7183 -- purposes).
7185 Set_From_Limited_With
7186 (Formal_Typ, From_Limited_With (Result_Subt));
7188 Layout_Type (Formal_Typ);
7190 Discard :=
7191 Add_Extra_Formal
7192 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7193 end;
7194 end if;
7195 end Create_Extra_Formals;
7197 -----------------------------
7198 -- Enter_Overloaded_Entity --
7199 -----------------------------
7201 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7202 E : Entity_Id := Current_Entity_In_Scope (S);
7203 C_E : Entity_Id := Current_Entity (S);
7205 begin
7206 if Present (E) then
7207 Set_Has_Homonym (E);
7208 Set_Has_Homonym (S);
7209 end if;
7211 Set_Is_Immediately_Visible (S);
7212 Set_Scope (S, Current_Scope);
7214 -- Chain new entity if front of homonym in current scope, so that
7215 -- homonyms are contiguous.
7217 if Present (E) and then E /= C_E then
7218 while Homonym (C_E) /= E loop
7219 C_E := Homonym (C_E);
7220 end loop;
7222 Set_Homonym (C_E, S);
7224 else
7225 E := C_E;
7226 Set_Current_Entity (S);
7227 end if;
7229 Set_Homonym (S, E);
7231 if Is_Inherited_Operation (S) then
7232 Append_Inherited_Subprogram (S);
7233 else
7234 Append_Entity (S, Current_Scope);
7235 end if;
7237 Set_Public_Status (S);
7239 if Debug_Flag_E then
7240 Write_Str ("New overloaded entity chain: ");
7241 Write_Name (Chars (S));
7243 E := S;
7244 while Present (E) loop
7245 Write_Str (" "); Write_Int (Int (E));
7246 E := Homonym (E);
7247 end loop;
7249 Write_Eol;
7250 end if;
7252 -- Generate warning for hiding
7254 if Warn_On_Hiding
7255 and then Comes_From_Source (S)
7256 and then In_Extended_Main_Source_Unit (S)
7257 then
7258 E := S;
7259 loop
7260 E := Homonym (E);
7261 exit when No (E);
7263 -- Warn unless genuine overloading. Do not emit warning on
7264 -- hiding predefined operators in Standard (these are either an
7265 -- (artifact of our implicit declarations, or simple noise) but
7266 -- keep warning on a operator defined on a local subtype, because
7267 -- of the real danger that different operators may be applied in
7268 -- various parts of the program.
7270 -- Note that if E and S have the same scope, there is never any
7271 -- hiding. Either the two conflict, and the program is illegal,
7272 -- or S is overriding an implicit inherited subprogram.
7274 if Scope (E) /= Scope (S)
7275 and then (not Is_Overloadable (E)
7276 or else Subtype_Conformant (E, S))
7277 and then (Is_Immediately_Visible (E)
7278 or else
7279 Is_Potentially_Use_Visible (S))
7280 then
7281 if Scope (E) /= Standard_Standard then
7282 Error_Msg_Sloc := Sloc (E);
7283 Error_Msg_N ("declaration of & hides one #?h?", S);
7285 elsif Nkind (S) = N_Defining_Operator_Symbol
7286 and then
7287 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7288 then
7289 Error_Msg_N
7290 ("declaration of & hides predefined operator?h?", S);
7291 end if;
7292 end if;
7293 end loop;
7294 end if;
7295 end Enter_Overloaded_Entity;
7297 -----------------------------
7298 -- Check_Untagged_Equality --
7299 -----------------------------
7301 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7302 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7303 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7304 Obj_Decl : Node_Id;
7306 begin
7307 -- This check applies only if we have a subprogram declaration with an
7308 -- untagged record type.
7310 if Nkind (Decl) /= N_Subprogram_Declaration
7311 or else not Is_Record_Type (Typ)
7312 or else Is_Tagged_Type (Typ)
7313 then
7314 return;
7315 end if;
7317 -- In Ada 2012 case, we will output errors or warnings depending on
7318 -- the setting of debug flag -gnatd.E.
7320 if Ada_Version >= Ada_2012 then
7321 Error_Msg_Warn := Debug_Flag_Dot_EE;
7323 -- In earlier versions of Ada, nothing to do unless we are warning on
7324 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7326 else
7327 if not Warn_On_Ada_2012_Compatibility then
7328 return;
7329 end if;
7330 end if;
7332 -- Cases where the type has already been frozen
7334 if Is_Frozen (Typ) then
7336 -- If the type is not declared in a package, or if we are in the body
7337 -- of the package or in some other scope, the new operation is not
7338 -- primitive, and therefore legal, though suspicious. Should we
7339 -- generate a warning in this case ???
7341 if Ekind (Scope (Typ)) /= E_Package
7342 or else Scope (Typ) /= Current_Scope
7343 then
7344 return;
7346 -- If the type is a generic actual (sub)type, the operation is not
7347 -- primitive either because the base type is declared elsewhere.
7349 elsif Is_Generic_Actual_Type (Typ) then
7350 return;
7352 -- Here we have a definite error of declaration after freezing
7354 else
7355 if Ada_Version >= Ada_2012 then
7356 Error_Msg_NE
7357 ("equality operator must be declared before type & is "
7358 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7360 -- In Ada 2012 mode with error turned to warning, output one
7361 -- more warning to warn that the equality operation may not
7362 -- compose. This is the consequence of ignoring the error.
7364 if Error_Msg_Warn then
7365 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7366 end if;
7368 else
7369 Error_Msg_NE
7370 ("equality operator must be declared before type& is "
7371 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7372 end if;
7374 -- If we are in the package body, we could just move the
7375 -- declaration to the package spec, so add a message saying that.
7377 if In_Package_Body (Scope (Typ)) then
7378 if Ada_Version >= Ada_2012 then
7379 Error_Msg_N
7380 ("\move declaration to package spec<<", Eq_Op);
7381 else
7382 Error_Msg_N
7383 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7384 end if;
7386 -- Otherwise try to find the freezing point
7388 else
7389 Obj_Decl := Next (Parent (Typ));
7390 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7391 if Nkind (Obj_Decl) = N_Object_Declaration
7392 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7393 then
7394 -- Freezing point, output warnings
7396 if Ada_Version >= Ada_2012 then
7397 Error_Msg_NE
7398 ("type& is frozen by declaration??", Obj_Decl, Typ);
7399 Error_Msg_N
7400 ("\an equality operator cannot be declared after "
7401 & "this point??",
7402 Obj_Decl);
7403 else
7404 Error_Msg_NE
7405 ("type& is frozen by declaration (Ada 2012)?y?",
7406 Obj_Decl, Typ);
7407 Error_Msg_N
7408 ("\an equality operator cannot be declared after "
7409 & "this point (Ada 2012)?y?",
7410 Obj_Decl);
7411 end if;
7413 exit;
7414 end if;
7416 Next (Obj_Decl);
7417 end loop;
7418 end if;
7419 end if;
7421 -- Here if type is not frozen yet. It is illegal to have a primitive
7422 -- equality declared in the private part if the type is visible.
7424 elsif not In_Same_List (Parent (Typ), Decl)
7425 and then not Is_Limited_Type (Typ)
7426 then
7427 -- Shouldn't we give an RM reference here???
7429 if Ada_Version >= Ada_2012 then
7430 Error_Msg_N
7431 ("equality operator appears too late<<", Eq_Op);
7432 else
7433 Error_Msg_N
7434 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7435 end if;
7437 -- No error detected
7439 else
7440 return;
7441 end if;
7442 end Check_Untagged_Equality;
7444 -----------------------------
7445 -- Find_Corresponding_Spec --
7446 -----------------------------
7448 function Find_Corresponding_Spec
7449 (N : Node_Id;
7450 Post_Error : Boolean := True) return Entity_Id
7452 Spec : constant Node_Id := Specification (N);
7453 Designator : constant Entity_Id := Defining_Entity (Spec);
7455 E : Entity_Id;
7457 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7458 -- Even if fully conformant, a body may depend on a generic actual when
7459 -- the spec does not, or vice versa, in which case they were distinct
7460 -- entities in the generic.
7462 -------------------------------
7463 -- Different_Generic_Profile --
7464 -------------------------------
7466 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7467 F1, F2 : Entity_Id;
7469 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7470 -- Check that the types of corresponding formals have the same
7471 -- generic actual if any. We have to account for subtypes of a
7472 -- generic formal, declared between a spec and a body, which may
7473 -- appear distinct in an instance but matched in the generic, and
7474 -- the subtype may be used either in the spec or the body of the
7475 -- subprogram being checked.
7477 -------------------------
7478 -- Same_Generic_Actual --
7479 -------------------------
7481 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7483 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7484 -- Predicate to check whether S1 is a subtype of S2 in the source
7485 -- of the instance.
7487 -------------------------
7488 -- Is_Declared_Subtype --
7489 -------------------------
7491 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7492 begin
7493 return Comes_From_Source (Parent (S1))
7494 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7495 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7496 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7497 end Is_Declared_Subtype;
7499 -- Start of processing for Same_Generic_Actual
7501 begin
7502 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7503 or else Is_Declared_Subtype (T1, T2)
7504 or else Is_Declared_Subtype (T2, T1);
7505 end Same_Generic_Actual;
7507 -- Start of processing for Different_Generic_Profile
7509 begin
7510 if not In_Instance then
7511 return False;
7513 elsif Ekind (E) = E_Function
7514 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7515 then
7516 return True;
7517 end if;
7519 F1 := First_Formal (Designator);
7520 F2 := First_Formal (E);
7521 while Present (F1) loop
7522 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7523 return True;
7524 end if;
7526 Next_Formal (F1);
7527 Next_Formal (F2);
7528 end loop;
7530 return False;
7531 end Different_Generic_Profile;
7533 -- Start of processing for Find_Corresponding_Spec
7535 begin
7536 E := Current_Entity (Designator);
7537 while Present (E) loop
7539 -- We are looking for a matching spec. It must have the same scope,
7540 -- and the same name, and either be type conformant, or be the case
7541 -- of a library procedure spec and its body (which belong to one
7542 -- another regardless of whether they are type conformant or not).
7544 if Scope (E) = Current_Scope then
7545 if Current_Scope = Standard_Standard
7546 or else (Ekind (E) = Ekind (Designator)
7547 and then Type_Conformant (E, Designator))
7548 then
7549 -- Within an instantiation, we know that spec and body are
7550 -- subtype conformant, because they were subtype conformant in
7551 -- the generic. We choose the subtype-conformant entity here as
7552 -- well, to resolve spurious ambiguities in the instance that
7553 -- were not present in the generic (i.e. when two different
7554 -- types are given the same actual). If we are looking for a
7555 -- spec to match a body, full conformance is expected.
7557 if In_Instance then
7559 -- Inherit the convention and "ghostness" of the matching
7560 -- spec to ensure proper full and subtype conformance.
7562 Set_Convention (Designator, Convention (E));
7564 if Is_Ghost_Entity (E) then
7565 Set_Is_Ghost_Entity (Designator);
7566 end if;
7568 -- Skip past subprogram bodies and subprogram renamings that
7569 -- may appear to have a matching spec, but that aren't fully
7570 -- conformant with it. That can occur in cases where an
7571 -- actual type causes unrelated homographs in the instance.
7573 if Nkind_In (N, N_Subprogram_Body,
7574 N_Subprogram_Renaming_Declaration)
7575 and then Present (Homonym (E))
7576 and then not Fully_Conformant (Designator, E)
7577 then
7578 goto Next_Entity;
7580 elsif not Subtype_Conformant (Designator, E) then
7581 goto Next_Entity;
7583 elsif Different_Generic_Profile (E) then
7584 goto Next_Entity;
7585 end if;
7586 end if;
7588 -- Ada 2012 (AI05-0165): For internally generated bodies of
7589 -- null procedures locate the internally generated spec. We
7590 -- enforce mode conformance since a tagged type may inherit
7591 -- from interfaces several null primitives which differ only
7592 -- in the mode of the formals.
7594 if not (Comes_From_Source (E))
7595 and then Is_Null_Procedure (E)
7596 and then not Mode_Conformant (Designator, E)
7597 then
7598 null;
7600 -- For null procedures coming from source that are completions,
7601 -- analysis of the generated body will establish the link.
7603 elsif Comes_From_Source (E)
7604 and then Nkind (Spec) = N_Procedure_Specification
7605 and then Null_Present (Spec)
7606 then
7607 return E;
7609 elsif not Has_Completion (E) then
7610 if Nkind (N) /= N_Subprogram_Body_Stub then
7611 Set_Corresponding_Spec (N, E);
7612 end if;
7614 Set_Has_Completion (E);
7615 return E;
7617 elsif Nkind (Parent (N)) = N_Subunit then
7619 -- If this is the proper body of a subunit, the completion
7620 -- flag is set when analyzing the stub.
7622 return E;
7624 -- If E is an internal function with a controlling result that
7625 -- was created for an operation inherited by a null extension,
7626 -- it may be overridden by a body without a previous spec (one
7627 -- more reason why these should be shunned). In that case we
7628 -- remove the generated body if present, because the current
7629 -- one is the explicit overriding.
7631 elsif Ekind (E) = E_Function
7632 and then Ada_Version >= Ada_2005
7633 and then not Comes_From_Source (E)
7634 and then Has_Controlling_Result (E)
7635 and then Is_Null_Extension (Etype (E))
7636 and then Comes_From_Source (Spec)
7637 then
7638 Set_Has_Completion (E, False);
7640 if Expander_Active
7641 and then Nkind (Parent (E)) = N_Function_Specification
7642 then
7643 Remove
7644 (Unit_Declaration_Node
7645 (Corresponding_Body (Unit_Declaration_Node (E))));
7647 return E;
7649 -- If expansion is disabled, or if the wrapper function has
7650 -- not been generated yet, this a late body overriding an
7651 -- inherited operation, or it is an overriding by some other
7652 -- declaration before the controlling result is frozen. In
7653 -- either case this is a declaration of a new entity.
7655 else
7656 return Empty;
7657 end if;
7659 -- If the body already exists, then this is an error unless
7660 -- the previous declaration is the implicit declaration of a
7661 -- derived subprogram. It is also legal for an instance to
7662 -- contain type conformant overloadable declarations (but the
7663 -- generic declaration may not), per 8.3(26/2).
7665 elsif No (Alias (E))
7666 and then not Is_Intrinsic_Subprogram (E)
7667 and then not In_Instance
7668 and then Post_Error
7669 then
7670 Error_Msg_Sloc := Sloc (E);
7672 if Is_Imported (E) then
7673 Error_Msg_NE
7674 ("body not allowed for imported subprogram & declared#",
7675 N, E);
7676 else
7677 Error_Msg_NE ("duplicate body for & declared#", N, E);
7678 end if;
7679 end if;
7681 -- Child units cannot be overloaded, so a conformance mismatch
7682 -- between body and a previous spec is an error.
7684 elsif Is_Child_Unit (E)
7685 and then
7686 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7687 and then
7688 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7689 N_Compilation_Unit
7690 and then Post_Error
7691 then
7692 Error_Msg_N
7693 ("body of child unit does not match previous declaration", N);
7694 end if;
7695 end if;
7697 <<Next_Entity>>
7698 E := Homonym (E);
7699 end loop;
7701 -- On exit, we know that no previous declaration of subprogram exists
7703 return Empty;
7704 end Find_Corresponding_Spec;
7706 ----------------------
7707 -- Fully_Conformant --
7708 ----------------------
7710 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7711 Result : Boolean;
7712 begin
7713 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7714 return Result;
7715 end Fully_Conformant;
7717 ----------------------------------
7718 -- Fully_Conformant_Expressions --
7719 ----------------------------------
7721 function Fully_Conformant_Expressions
7722 (Given_E1 : Node_Id;
7723 Given_E2 : Node_Id) return Boolean
7725 E1 : constant Node_Id := Original_Node (Given_E1);
7726 E2 : constant Node_Id := Original_Node (Given_E2);
7727 -- We always test conformance on original nodes, since it is possible
7728 -- for analysis and/or expansion to make things look as though they
7729 -- conform when they do not, e.g. by converting 1+2 into 3.
7731 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7732 renames Fully_Conformant_Expressions;
7734 function FCL (L1, L2 : List_Id) return Boolean;
7735 -- Compare elements of two lists for conformance. Elements have to be
7736 -- conformant, and actuals inserted as default parameters do not match
7737 -- explicit actuals with the same value.
7739 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7740 -- Compare an operator node with a function call
7742 ---------
7743 -- FCL --
7744 ---------
7746 function FCL (L1, L2 : List_Id) return Boolean is
7747 N1, N2 : Node_Id;
7749 begin
7750 if L1 = No_List then
7751 N1 := Empty;
7752 else
7753 N1 := First (L1);
7754 end if;
7756 if L2 = No_List then
7757 N2 := Empty;
7758 else
7759 N2 := First (L2);
7760 end if;
7762 -- Compare two lists, skipping rewrite insertions (we want to compare
7763 -- the original trees, not the expanded versions).
7765 loop
7766 if Is_Rewrite_Insertion (N1) then
7767 Next (N1);
7768 elsif Is_Rewrite_Insertion (N2) then
7769 Next (N2);
7770 elsif No (N1) then
7771 return No (N2);
7772 elsif No (N2) then
7773 return False;
7774 elsif not FCE (N1, N2) then
7775 return False;
7776 else
7777 Next (N1);
7778 Next (N2);
7779 end if;
7780 end loop;
7781 end FCL;
7783 ---------
7784 -- FCO --
7785 ---------
7787 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7788 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7789 Act : Node_Id;
7791 begin
7792 if No (Actuals)
7793 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7794 then
7795 return False;
7797 else
7798 Act := First (Actuals);
7800 if Nkind (Op_Node) in N_Binary_Op then
7801 if not FCE (Left_Opnd (Op_Node), Act) then
7802 return False;
7803 end if;
7805 Next (Act);
7806 end if;
7808 return Present (Act)
7809 and then FCE (Right_Opnd (Op_Node), Act)
7810 and then No (Next (Act));
7811 end if;
7812 end FCO;
7814 -- Start of processing for Fully_Conformant_Expressions
7816 begin
7817 -- Non-conformant if paren count does not match. Note: if some idiot
7818 -- complains that we don't do this right for more than 3 levels of
7819 -- parentheses, they will be treated with the respect they deserve.
7821 if Paren_Count (E1) /= Paren_Count (E2) then
7822 return False;
7824 -- If same entities are referenced, then they are conformant even if
7825 -- they have different forms (RM 8.3.1(19-20)).
7827 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7828 if Present (Entity (E1)) then
7829 return Entity (E1) = Entity (E2)
7830 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7831 and then Ekind (Entity (E1)) = E_Discriminant
7832 and then Ekind (Entity (E2)) = E_In_Parameter);
7834 elsif Nkind (E1) = N_Expanded_Name
7835 and then Nkind (E2) = N_Expanded_Name
7836 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7837 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7838 then
7839 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7841 else
7842 -- Identifiers in component associations don't always have
7843 -- entities, but their names must conform.
7845 return Nkind (E1) = N_Identifier
7846 and then Nkind (E2) = N_Identifier
7847 and then Chars (E1) = Chars (E2);
7848 end if;
7850 elsif Nkind (E1) = N_Character_Literal
7851 and then Nkind (E2) = N_Expanded_Name
7852 then
7853 return Nkind (Selector_Name (E2)) = N_Character_Literal
7854 and then Chars (E1) = Chars (Selector_Name (E2));
7856 elsif Nkind (E2) = N_Character_Literal
7857 and then Nkind (E1) = N_Expanded_Name
7858 then
7859 return Nkind (Selector_Name (E1)) = N_Character_Literal
7860 and then Chars (E2) = Chars (Selector_Name (E1));
7862 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7863 return FCO (E1, E2);
7865 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7866 return FCO (E2, E1);
7868 -- Otherwise we must have the same syntactic entity
7870 elsif Nkind (E1) /= Nkind (E2) then
7871 return False;
7873 -- At this point, we specialize by node type
7875 else
7876 case Nkind (E1) is
7878 when N_Aggregate =>
7879 return
7880 FCL (Expressions (E1), Expressions (E2))
7881 and then
7882 FCL (Component_Associations (E1),
7883 Component_Associations (E2));
7885 when N_Allocator =>
7886 if Nkind (Expression (E1)) = N_Qualified_Expression
7887 or else
7888 Nkind (Expression (E2)) = N_Qualified_Expression
7889 then
7890 return FCE (Expression (E1), Expression (E2));
7892 -- Check that the subtype marks and any constraints
7893 -- are conformant
7895 else
7896 declare
7897 Indic1 : constant Node_Id := Expression (E1);
7898 Indic2 : constant Node_Id := Expression (E2);
7899 Elt1 : Node_Id;
7900 Elt2 : Node_Id;
7902 begin
7903 if Nkind (Indic1) /= N_Subtype_Indication then
7904 return
7905 Nkind (Indic2) /= N_Subtype_Indication
7906 and then Entity (Indic1) = Entity (Indic2);
7908 elsif Nkind (Indic2) /= N_Subtype_Indication then
7909 return
7910 Nkind (Indic1) /= N_Subtype_Indication
7911 and then Entity (Indic1) = Entity (Indic2);
7913 else
7914 if Entity (Subtype_Mark (Indic1)) /=
7915 Entity (Subtype_Mark (Indic2))
7916 then
7917 return False;
7918 end if;
7920 Elt1 := First (Constraints (Constraint (Indic1)));
7921 Elt2 := First (Constraints (Constraint (Indic2)));
7922 while Present (Elt1) and then Present (Elt2) loop
7923 if not FCE (Elt1, Elt2) then
7924 return False;
7925 end if;
7927 Next (Elt1);
7928 Next (Elt2);
7929 end loop;
7931 return True;
7932 end if;
7933 end;
7934 end if;
7936 when N_Attribute_Reference =>
7937 return
7938 Attribute_Name (E1) = Attribute_Name (E2)
7939 and then FCL (Expressions (E1), Expressions (E2));
7941 when N_Binary_Op =>
7942 return
7943 Entity (E1) = Entity (E2)
7944 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7945 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7947 when N_Short_Circuit | N_Membership_Test =>
7948 return
7949 FCE (Left_Opnd (E1), Left_Opnd (E2))
7950 and then
7951 FCE (Right_Opnd (E1), Right_Opnd (E2));
7953 when N_Case_Expression =>
7954 declare
7955 Alt1 : Node_Id;
7956 Alt2 : Node_Id;
7958 begin
7959 if not FCE (Expression (E1), Expression (E2)) then
7960 return False;
7962 else
7963 Alt1 := First (Alternatives (E1));
7964 Alt2 := First (Alternatives (E2));
7965 loop
7966 if Present (Alt1) /= Present (Alt2) then
7967 return False;
7968 elsif No (Alt1) then
7969 return True;
7970 end if;
7972 if not FCE (Expression (Alt1), Expression (Alt2))
7973 or else not FCL (Discrete_Choices (Alt1),
7974 Discrete_Choices (Alt2))
7975 then
7976 return False;
7977 end if;
7979 Next (Alt1);
7980 Next (Alt2);
7981 end loop;
7982 end if;
7983 end;
7985 when N_Character_Literal =>
7986 return
7987 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7989 when N_Component_Association =>
7990 return
7991 FCL (Choices (E1), Choices (E2))
7992 and then
7993 FCE (Expression (E1), Expression (E2));
7995 when N_Explicit_Dereference =>
7996 return
7997 FCE (Prefix (E1), Prefix (E2));
7999 when N_Extension_Aggregate =>
8000 return
8001 FCL (Expressions (E1), Expressions (E2))
8002 and then Null_Record_Present (E1) =
8003 Null_Record_Present (E2)
8004 and then FCL (Component_Associations (E1),
8005 Component_Associations (E2));
8007 when N_Function_Call =>
8008 return
8009 FCE (Name (E1), Name (E2))
8010 and then
8011 FCL (Parameter_Associations (E1),
8012 Parameter_Associations (E2));
8014 when N_If_Expression =>
8015 return
8016 FCL (Expressions (E1), Expressions (E2));
8018 when N_Indexed_Component =>
8019 return
8020 FCE (Prefix (E1), Prefix (E2))
8021 and then
8022 FCL (Expressions (E1), Expressions (E2));
8024 when N_Integer_Literal =>
8025 return (Intval (E1) = Intval (E2));
8027 when N_Null =>
8028 return True;
8030 when N_Operator_Symbol =>
8031 return
8032 Chars (E1) = Chars (E2);
8034 when N_Others_Choice =>
8035 return True;
8037 when N_Parameter_Association =>
8038 return
8039 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8040 and then FCE (Explicit_Actual_Parameter (E1),
8041 Explicit_Actual_Parameter (E2));
8043 when N_Qualified_Expression =>
8044 return
8045 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8046 and then
8047 FCE (Expression (E1), Expression (E2));
8049 when N_Quantified_Expression =>
8050 if not FCE (Condition (E1), Condition (E2)) then
8051 return False;
8052 end if;
8054 if Present (Loop_Parameter_Specification (E1))
8055 and then Present (Loop_Parameter_Specification (E2))
8056 then
8057 declare
8058 L1 : constant Node_Id :=
8059 Loop_Parameter_Specification (E1);
8060 L2 : constant Node_Id :=
8061 Loop_Parameter_Specification (E2);
8063 begin
8064 return
8065 Reverse_Present (L1) = Reverse_Present (L2)
8066 and then
8067 FCE (Defining_Identifier (L1),
8068 Defining_Identifier (L2))
8069 and then
8070 FCE (Discrete_Subtype_Definition (L1),
8071 Discrete_Subtype_Definition (L2));
8072 end;
8074 elsif Present (Iterator_Specification (E1))
8075 and then Present (Iterator_Specification (E2))
8076 then
8077 declare
8078 I1 : constant Node_Id := Iterator_Specification (E1);
8079 I2 : constant Node_Id := Iterator_Specification (E2);
8081 begin
8082 return
8083 FCE (Defining_Identifier (I1),
8084 Defining_Identifier (I2))
8085 and then
8086 Of_Present (I1) = Of_Present (I2)
8087 and then
8088 Reverse_Present (I1) = Reverse_Present (I2)
8089 and then FCE (Name (I1), Name (I2))
8090 and then FCE (Subtype_Indication (I1),
8091 Subtype_Indication (I2));
8092 end;
8094 -- The quantified expressions used different specifications to
8095 -- walk their respective ranges.
8097 else
8098 return False;
8099 end if;
8101 when N_Range =>
8102 return
8103 FCE (Low_Bound (E1), Low_Bound (E2))
8104 and then
8105 FCE (High_Bound (E1), High_Bound (E2));
8107 when N_Real_Literal =>
8108 return (Realval (E1) = Realval (E2));
8110 when N_Selected_Component =>
8111 return
8112 FCE (Prefix (E1), Prefix (E2))
8113 and then
8114 FCE (Selector_Name (E1), Selector_Name (E2));
8116 when N_Slice =>
8117 return
8118 FCE (Prefix (E1), Prefix (E2))
8119 and then
8120 FCE (Discrete_Range (E1), Discrete_Range (E2));
8122 when N_String_Literal =>
8123 declare
8124 S1 : constant String_Id := Strval (E1);
8125 S2 : constant String_Id := Strval (E2);
8126 L1 : constant Nat := String_Length (S1);
8127 L2 : constant Nat := String_Length (S2);
8129 begin
8130 if L1 /= L2 then
8131 return False;
8133 else
8134 for J in 1 .. L1 loop
8135 if Get_String_Char (S1, J) /=
8136 Get_String_Char (S2, J)
8137 then
8138 return False;
8139 end if;
8140 end loop;
8142 return True;
8143 end if;
8144 end;
8146 when N_Type_Conversion =>
8147 return
8148 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8149 and then
8150 FCE (Expression (E1), Expression (E2));
8152 when N_Unary_Op =>
8153 return
8154 Entity (E1) = Entity (E2)
8155 and then
8156 FCE (Right_Opnd (E1), Right_Opnd (E2));
8158 when N_Unchecked_Type_Conversion =>
8159 return
8160 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8161 and then
8162 FCE (Expression (E1), Expression (E2));
8164 -- All other node types cannot appear in this context. Strictly
8165 -- we should raise a fatal internal error. Instead we just ignore
8166 -- the nodes. This means that if anyone makes a mistake in the
8167 -- expander and mucks an expression tree irretrievably, the result
8168 -- will be a failure to detect a (probably very obscure) case
8169 -- of non-conformance, which is better than bombing on some
8170 -- case where two expressions do in fact conform.
8172 when others =>
8173 return True;
8175 end case;
8176 end if;
8177 end Fully_Conformant_Expressions;
8179 ----------------------------------------
8180 -- Fully_Conformant_Discrete_Subtypes --
8181 ----------------------------------------
8183 function Fully_Conformant_Discrete_Subtypes
8184 (Given_S1 : Node_Id;
8185 Given_S2 : Node_Id) return Boolean
8187 S1 : constant Node_Id := Original_Node (Given_S1);
8188 S2 : constant Node_Id := Original_Node (Given_S2);
8190 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8191 -- Special-case for a bound given by a discriminant, which in the body
8192 -- is replaced with the discriminal of the enclosing type.
8194 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8195 -- Check both bounds
8197 -----------------------
8198 -- Conforming_Bounds --
8199 -----------------------
8201 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8202 begin
8203 if Is_Entity_Name (B1)
8204 and then Is_Entity_Name (B2)
8205 and then Ekind (Entity (B1)) = E_Discriminant
8206 then
8207 return Chars (B1) = Chars (B2);
8209 else
8210 return Fully_Conformant_Expressions (B1, B2);
8211 end if;
8212 end Conforming_Bounds;
8214 -----------------------
8215 -- Conforming_Ranges --
8216 -----------------------
8218 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8219 begin
8220 return
8221 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8222 and then
8223 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8224 end Conforming_Ranges;
8226 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8228 begin
8229 if Nkind (S1) /= Nkind (S2) then
8230 return False;
8232 elsif Is_Entity_Name (S1) then
8233 return Entity (S1) = Entity (S2);
8235 elsif Nkind (S1) = N_Range then
8236 return Conforming_Ranges (S1, S2);
8238 elsif Nkind (S1) = N_Subtype_Indication then
8239 return
8240 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8241 and then
8242 Conforming_Ranges
8243 (Range_Expression (Constraint (S1)),
8244 Range_Expression (Constraint (S2)));
8245 else
8246 return True;
8247 end if;
8248 end Fully_Conformant_Discrete_Subtypes;
8250 --------------------
8251 -- Install_Entity --
8252 --------------------
8254 procedure Install_Entity (E : Entity_Id) is
8255 Prev : constant Entity_Id := Current_Entity (E);
8256 begin
8257 Set_Is_Immediately_Visible (E);
8258 Set_Current_Entity (E);
8259 Set_Homonym (E, Prev);
8260 end Install_Entity;
8262 ---------------------
8263 -- Install_Formals --
8264 ---------------------
8266 procedure Install_Formals (Id : Entity_Id) is
8267 F : Entity_Id;
8268 begin
8269 F := First_Formal (Id);
8270 while Present (F) loop
8271 Install_Entity (F);
8272 Next_Formal (F);
8273 end loop;
8274 end Install_Formals;
8276 -----------------------------
8277 -- Is_Interface_Conformant --
8278 -----------------------------
8280 function Is_Interface_Conformant
8281 (Tagged_Type : Entity_Id;
8282 Iface_Prim : Entity_Id;
8283 Prim : Entity_Id) return Boolean
8285 -- The operation may in fact be an inherited (implicit) operation
8286 -- rather than the original interface primitive, so retrieve the
8287 -- ultimate ancestor.
8289 Iface : constant Entity_Id :=
8290 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8291 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8293 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8294 -- Return the controlling formal of Prim
8296 ------------------------
8297 -- Controlling_Formal --
8298 ------------------------
8300 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8301 E : Entity_Id;
8303 begin
8304 E := First_Entity (Prim);
8305 while Present (E) loop
8306 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8307 return E;
8308 end if;
8310 Next_Entity (E);
8311 end loop;
8313 return Empty;
8314 end Controlling_Formal;
8316 -- Local variables
8318 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8319 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8321 -- Start of processing for Is_Interface_Conformant
8323 begin
8324 pragma Assert (Is_Subprogram (Iface_Prim)
8325 and then Is_Subprogram (Prim)
8326 and then Is_Dispatching_Operation (Iface_Prim)
8327 and then Is_Dispatching_Operation (Prim));
8329 pragma Assert (Is_Interface (Iface)
8330 or else (Present (Alias (Iface_Prim))
8331 and then
8332 Is_Interface
8333 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8335 if Prim = Iface_Prim
8336 or else not Is_Subprogram (Prim)
8337 or else Ekind (Prim) /= Ekind (Iface_Prim)
8338 or else not Is_Dispatching_Operation (Prim)
8339 or else Scope (Prim) /= Scope (Tagged_Type)
8340 or else No (Typ)
8341 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8342 or else not Primitive_Names_Match (Iface_Prim, Prim)
8343 then
8344 return False;
8346 -- The mode of the controlling formals must match
8348 elsif Present (Iface_Ctrl_F)
8349 and then Present (Prim_Ctrl_F)
8350 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8351 then
8352 return False;
8354 -- Case of a procedure, or a function whose result type matches the
8355 -- result type of the interface primitive, or a function that has no
8356 -- controlling result (I or access I).
8358 elsif Ekind (Iface_Prim) = E_Procedure
8359 or else Etype (Prim) = Etype (Iface_Prim)
8360 or else not Has_Controlling_Result (Prim)
8361 then
8362 return Type_Conformant
8363 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8365 -- Case of a function returning an interface, or an access to one. Check
8366 -- that the return types correspond.
8368 elsif Implements_Interface (Typ, Iface) then
8369 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8371 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8372 then
8373 return False;
8374 else
8375 return
8376 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8377 Skip_Controlling_Formals => True);
8378 end if;
8380 else
8381 return False;
8382 end if;
8383 end Is_Interface_Conformant;
8385 ---------------------------------
8386 -- Is_Non_Overriding_Operation --
8387 ---------------------------------
8389 function Is_Non_Overriding_Operation
8390 (Prev_E : Entity_Id;
8391 New_E : Entity_Id) return Boolean
8393 Formal : Entity_Id;
8394 F_Typ : Entity_Id;
8395 G_Typ : Entity_Id := Empty;
8397 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8398 -- If F_Type is a derived type associated with a generic actual subtype,
8399 -- then return its Generic_Parent_Type attribute, else return Empty.
8401 function Types_Correspond
8402 (P_Type : Entity_Id;
8403 N_Type : Entity_Id) return Boolean;
8404 -- Returns true if and only if the types (or designated types in the
8405 -- case of anonymous access types) are the same or N_Type is derived
8406 -- directly or indirectly from P_Type.
8408 -----------------------------
8409 -- Get_Generic_Parent_Type --
8410 -----------------------------
8412 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8413 G_Typ : Entity_Id;
8414 Defn : Node_Id;
8415 Indic : Node_Id;
8417 begin
8418 if Is_Derived_Type (F_Typ)
8419 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8420 then
8421 -- The tree must be traversed to determine the parent subtype in
8422 -- the generic unit, which unfortunately isn't always available
8423 -- via semantic attributes. ??? (Note: The use of Original_Node
8424 -- is needed for cases where a full derived type has been
8425 -- rewritten.)
8427 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8428 if Nkind (Defn) = N_Derived_Type_Definition then
8429 Indic := Subtype_Indication (Defn);
8431 if Nkind (Indic) = N_Subtype_Indication then
8432 G_Typ := Entity (Subtype_Mark (Indic));
8433 else
8434 G_Typ := Entity (Indic);
8435 end if;
8437 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8438 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8439 then
8440 return Generic_Parent_Type (Parent (G_Typ));
8441 end if;
8442 end if;
8443 end if;
8445 return Empty;
8446 end Get_Generic_Parent_Type;
8448 ----------------------
8449 -- Types_Correspond --
8450 ----------------------
8452 function Types_Correspond
8453 (P_Type : Entity_Id;
8454 N_Type : Entity_Id) return Boolean
8456 Prev_Type : Entity_Id := Base_Type (P_Type);
8457 New_Type : Entity_Id := Base_Type (N_Type);
8459 begin
8460 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8461 Prev_Type := Designated_Type (Prev_Type);
8462 end if;
8464 if Ekind (New_Type) = E_Anonymous_Access_Type then
8465 New_Type := Designated_Type (New_Type);
8466 end if;
8468 if Prev_Type = New_Type then
8469 return True;
8471 elsif not Is_Class_Wide_Type (New_Type) then
8472 while Etype (New_Type) /= New_Type loop
8473 New_Type := Etype (New_Type);
8474 if New_Type = Prev_Type then
8475 return True;
8476 end if;
8477 end loop;
8478 end if;
8479 return False;
8480 end Types_Correspond;
8482 -- Start of processing for Is_Non_Overriding_Operation
8484 begin
8485 -- In the case where both operations are implicit derived subprograms
8486 -- then neither overrides the other. This can only occur in certain
8487 -- obscure cases (e.g., derivation from homographs created in a generic
8488 -- instantiation).
8490 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8491 return True;
8493 elsif Ekind (Current_Scope) = E_Package
8494 and then Is_Generic_Instance (Current_Scope)
8495 and then In_Private_Part (Current_Scope)
8496 and then Comes_From_Source (New_E)
8497 then
8498 -- We examine the formals and result type of the inherited operation,
8499 -- to determine whether their type is derived from (the instance of)
8500 -- a generic type. The first such formal or result type is the one
8501 -- tested.
8503 Formal := First_Formal (Prev_E);
8504 while Present (Formal) loop
8505 F_Typ := Base_Type (Etype (Formal));
8507 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8508 F_Typ := Designated_Type (F_Typ);
8509 end if;
8511 G_Typ := Get_Generic_Parent_Type (F_Typ);
8512 exit when Present (G_Typ);
8514 Next_Formal (Formal);
8515 end loop;
8517 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8518 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8519 end if;
8521 if No (G_Typ) then
8522 return False;
8523 end if;
8525 -- If the generic type is a private type, then the original operation
8526 -- was not overriding in the generic, because there was no primitive
8527 -- operation to override.
8529 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8530 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8531 N_Formal_Private_Type_Definition
8532 then
8533 return True;
8535 -- The generic parent type is the ancestor of a formal derived
8536 -- type declaration. We need to check whether it has a primitive
8537 -- operation that should be overridden by New_E in the generic.
8539 else
8540 declare
8541 P_Formal : Entity_Id;
8542 N_Formal : Entity_Id;
8543 P_Typ : Entity_Id;
8544 N_Typ : Entity_Id;
8545 P_Prim : Entity_Id;
8546 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8548 begin
8549 while Present (Prim_Elt) loop
8550 P_Prim := Node (Prim_Elt);
8552 if Chars (P_Prim) = Chars (New_E)
8553 and then Ekind (P_Prim) = Ekind (New_E)
8554 then
8555 P_Formal := First_Formal (P_Prim);
8556 N_Formal := First_Formal (New_E);
8557 while Present (P_Formal) and then Present (N_Formal) loop
8558 P_Typ := Etype (P_Formal);
8559 N_Typ := Etype (N_Formal);
8561 if not Types_Correspond (P_Typ, N_Typ) then
8562 exit;
8563 end if;
8565 Next_Entity (P_Formal);
8566 Next_Entity (N_Formal);
8567 end loop;
8569 -- Found a matching primitive operation belonging to the
8570 -- formal ancestor type, so the new subprogram is
8571 -- overriding.
8573 if No (P_Formal)
8574 and then No (N_Formal)
8575 and then (Ekind (New_E) /= E_Function
8576 or else
8577 Types_Correspond
8578 (Etype (P_Prim), Etype (New_E)))
8579 then
8580 return False;
8581 end if;
8582 end if;
8584 Next_Elmt (Prim_Elt);
8585 end loop;
8587 -- If no match found, then the new subprogram does not override
8588 -- in the generic (nor in the instance).
8590 -- If the type in question is not abstract, and the subprogram
8591 -- is, this will be an error if the new operation is in the
8592 -- private part of the instance. Emit a warning now, which will
8593 -- make the subsequent error message easier to understand.
8595 if not Is_Abstract_Type (F_Typ)
8596 and then Is_Abstract_Subprogram (Prev_E)
8597 and then In_Private_Part (Current_Scope)
8598 then
8599 Error_Msg_Node_2 := F_Typ;
8600 Error_Msg_NE
8601 ("private operation& in generic unit does not override "
8602 & "any primitive operation of& (RM 12.3 (18))??",
8603 New_E, New_E);
8604 end if;
8606 return True;
8607 end;
8608 end if;
8609 else
8610 return False;
8611 end if;
8612 end Is_Non_Overriding_Operation;
8614 -------------------------------------
8615 -- List_Inherited_Pre_Post_Aspects --
8616 -------------------------------------
8618 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8619 begin
8620 if Opt.List_Inherited_Aspects
8621 and then Is_Subprogram_Or_Generic_Subprogram (E)
8622 then
8623 declare
8624 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
8625 P : Node_Id;
8627 begin
8628 for J in Inherited'Range loop
8629 P := Pre_Post_Conditions (Contract (Inherited (J)));
8630 while Present (P) loop
8631 Error_Msg_Sloc := Sloc (P);
8633 if Class_Present (P) and then not Split_PPC (P) then
8634 if Pragma_Name (P) = Name_Precondition then
8635 Error_Msg_N ("info: & inherits `Pre''Class` aspect "
8636 & "from #?L?", E);
8637 else
8638 Error_Msg_N ("info: & inherits `Post''Class` aspect "
8639 & "from #?L?", E);
8640 end if;
8641 end if;
8643 P := Next_Pragma (P);
8644 end loop;
8645 end loop;
8646 end;
8647 end if;
8648 end List_Inherited_Pre_Post_Aspects;
8650 ------------------------------
8651 -- Make_Inequality_Operator --
8652 ------------------------------
8654 -- S is the defining identifier of an equality operator. We build a
8655 -- subprogram declaration with the right signature. This operation is
8656 -- intrinsic, because it is always expanded as the negation of the
8657 -- call to the equality function.
8659 procedure Make_Inequality_Operator (S : Entity_Id) is
8660 Loc : constant Source_Ptr := Sloc (S);
8661 Decl : Node_Id;
8662 Formals : List_Id;
8663 Op_Name : Entity_Id;
8665 FF : constant Entity_Id := First_Formal (S);
8666 NF : constant Entity_Id := Next_Formal (FF);
8668 begin
8669 -- Check that equality was properly defined, ignore call if not
8671 if No (NF) then
8672 return;
8673 end if;
8675 declare
8676 A : constant Entity_Id :=
8677 Make_Defining_Identifier (Sloc (FF),
8678 Chars => Chars (FF));
8680 B : constant Entity_Id :=
8681 Make_Defining_Identifier (Sloc (NF),
8682 Chars => Chars (NF));
8684 begin
8685 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8687 Formals := New_List (
8688 Make_Parameter_Specification (Loc,
8689 Defining_Identifier => A,
8690 Parameter_Type =>
8691 New_Occurrence_Of (Etype (First_Formal (S)),
8692 Sloc (Etype (First_Formal (S))))),
8694 Make_Parameter_Specification (Loc,
8695 Defining_Identifier => B,
8696 Parameter_Type =>
8697 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8698 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8700 Decl :=
8701 Make_Subprogram_Declaration (Loc,
8702 Specification =>
8703 Make_Function_Specification (Loc,
8704 Defining_Unit_Name => Op_Name,
8705 Parameter_Specifications => Formals,
8706 Result_Definition =>
8707 New_Occurrence_Of (Standard_Boolean, Loc)));
8709 -- Insert inequality right after equality if it is explicit or after
8710 -- the derived type when implicit. These entities are created only
8711 -- for visibility purposes, and eventually replaced in the course
8712 -- of expansion, so they do not need to be attached to the tree and
8713 -- seen by the back-end. Keeping them internal also avoids spurious
8714 -- freezing problems. The declaration is inserted in the tree for
8715 -- analysis, and removed afterwards. If the equality operator comes
8716 -- from an explicit declaration, attach the inequality immediately
8717 -- after. Else the equality is inherited from a derived type
8718 -- declaration, so insert inequality after that declaration.
8720 if No (Alias (S)) then
8721 Insert_After (Unit_Declaration_Node (S), Decl);
8722 elsif Is_List_Member (Parent (S)) then
8723 Insert_After (Parent (S), Decl);
8724 else
8725 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8726 end if;
8728 Mark_Rewrite_Insertion (Decl);
8729 Set_Is_Intrinsic_Subprogram (Op_Name);
8730 Analyze (Decl);
8731 Remove (Decl);
8732 Set_Has_Completion (Op_Name);
8733 Set_Corresponding_Equality (Op_Name, S);
8734 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8735 end;
8736 end Make_Inequality_Operator;
8738 ----------------------
8739 -- May_Need_Actuals --
8740 ----------------------
8742 procedure May_Need_Actuals (Fun : Entity_Id) is
8743 F : Entity_Id;
8744 B : Boolean;
8746 begin
8747 F := First_Formal (Fun);
8748 B := True;
8749 while Present (F) loop
8750 if No (Default_Value (F)) then
8751 B := False;
8752 exit;
8753 end if;
8755 Next_Formal (F);
8756 end loop;
8758 Set_Needs_No_Actuals (Fun, B);
8759 end May_Need_Actuals;
8761 ---------------------
8762 -- Mode_Conformant --
8763 ---------------------
8765 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8766 Result : Boolean;
8767 begin
8768 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8769 return Result;
8770 end Mode_Conformant;
8772 ---------------------------
8773 -- New_Overloaded_Entity --
8774 ---------------------------
8776 procedure New_Overloaded_Entity
8777 (S : Entity_Id;
8778 Derived_Type : Entity_Id := Empty)
8780 Overridden_Subp : Entity_Id := Empty;
8781 -- Set if the current scope has an operation that is type-conformant
8782 -- with S, and becomes hidden by S.
8784 Is_Primitive_Subp : Boolean;
8785 -- Set to True if the new subprogram is primitive
8787 E : Entity_Id;
8788 -- Entity that S overrides
8790 Prev_Vis : Entity_Id := Empty;
8791 -- Predecessor of E in Homonym chain
8793 procedure Check_For_Primitive_Subprogram
8794 (Is_Primitive : out Boolean;
8795 Is_Overriding : Boolean := False);
8796 -- If the subprogram being analyzed is a primitive operation of the type
8797 -- of a formal or result, set the Has_Primitive_Operations flag on the
8798 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8799 -- corresponding flag on the entity itself for later use.
8801 procedure Check_Synchronized_Overriding
8802 (Def_Id : Entity_Id;
8803 Overridden_Subp : out Entity_Id);
8804 -- First determine if Def_Id is an entry or a subprogram either defined
8805 -- in the scope of a task or protected type, or is a primitive of such
8806 -- a type. Check whether Def_Id overrides a subprogram of an interface
8807 -- implemented by the synchronized type, return the overridden entity
8808 -- or Empty.
8810 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8811 -- Check that E is declared in the private part of the current package,
8812 -- or in the package body, where it may hide a previous declaration.
8813 -- We can't use In_Private_Part by itself because this flag is also
8814 -- set when freezing entities, so we must examine the place of the
8815 -- declaration in the tree, and recognize wrapper packages as well.
8817 function Is_Overriding_Alias
8818 (Old_E : Entity_Id;
8819 New_E : Entity_Id) return Boolean;
8820 -- Check whether new subprogram and old subprogram are both inherited
8821 -- from subprograms that have distinct dispatch table entries. This can
8822 -- occur with derivations from instances with accidental homonyms. The
8823 -- function is conservative given that the converse is only true within
8824 -- instances that contain accidental overloadings.
8826 ------------------------------------
8827 -- Check_For_Primitive_Subprogram --
8828 ------------------------------------
8830 procedure Check_For_Primitive_Subprogram
8831 (Is_Primitive : out Boolean;
8832 Is_Overriding : Boolean := False)
8834 Formal : Entity_Id;
8835 F_Typ : Entity_Id;
8836 B_Typ : Entity_Id;
8838 function Visible_Part_Type (T : Entity_Id) return Boolean;
8839 -- Returns true if T is declared in the visible part of the current
8840 -- package scope; otherwise returns false. Assumes that T is declared
8841 -- in a package.
8843 procedure Check_Private_Overriding (T : Entity_Id);
8844 -- Checks that if a primitive abstract subprogram of a visible
8845 -- abstract type is declared in a private part, then it must override
8846 -- an abstract subprogram declared in the visible part. Also checks
8847 -- that if a primitive function with a controlling result is declared
8848 -- in a private part, then it must override a function declared in
8849 -- the visible part.
8851 ------------------------------
8852 -- Check_Private_Overriding --
8853 ------------------------------
8855 procedure Check_Private_Overriding (T : Entity_Id) is
8856 begin
8857 if Is_Package_Or_Generic_Package (Current_Scope)
8858 and then In_Private_Part (Current_Scope)
8859 and then Visible_Part_Type (T)
8860 and then not In_Instance
8861 then
8862 if Is_Abstract_Type (T)
8863 and then Is_Abstract_Subprogram (S)
8864 and then (not Is_Overriding
8865 or else not Is_Abstract_Subprogram (E))
8866 then
8867 Error_Msg_N ("abstract subprograms must be visible "
8868 & "(RM 3.9.3(10))!", S);
8870 elsif Ekind (S) = E_Function and then not Is_Overriding then
8871 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
8872 Error_Msg_N ("private function with tagged result must"
8873 & " override visible-part function", S);
8874 Error_Msg_N ("\move subprogram to the visible part"
8875 & " (RM 3.9.3(10))", S);
8877 -- AI05-0073: extend this test to the case of a function
8878 -- with a controlling access result.
8880 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8881 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8882 and then
8883 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
8884 and then Ada_Version >= Ada_2012
8885 then
8886 Error_Msg_N
8887 ("private function with controlling access result "
8888 & "must override visible-part function", S);
8889 Error_Msg_N
8890 ("\move subprogram to the visible part"
8891 & " (RM 3.9.3(10))", S);
8892 end if;
8893 end if;
8894 end if;
8895 end Check_Private_Overriding;
8897 -----------------------
8898 -- Visible_Part_Type --
8899 -----------------------
8901 function Visible_Part_Type (T : Entity_Id) return Boolean is
8902 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8903 N : Node_Id;
8905 begin
8906 -- If the entity is a private type, then it must be declared in a
8907 -- visible part.
8909 if Ekind (T) in Private_Kind then
8910 return True;
8911 end if;
8913 -- Otherwise, we traverse the visible part looking for its
8914 -- corresponding declaration. We cannot use the declaration
8915 -- node directly because in the private part the entity of a
8916 -- private type is the one in the full view, which does not
8917 -- indicate that it is the completion of something visible.
8919 N := First (Visible_Declarations (Specification (P)));
8920 while Present (N) loop
8921 if Nkind (N) = N_Full_Type_Declaration
8922 and then Present (Defining_Identifier (N))
8923 and then T = Defining_Identifier (N)
8924 then
8925 return True;
8927 elsif Nkind_In (N, N_Private_Type_Declaration,
8928 N_Private_Extension_Declaration)
8929 and then Present (Defining_Identifier (N))
8930 and then T = Full_View (Defining_Identifier (N))
8931 then
8932 return True;
8933 end if;
8935 Next (N);
8936 end loop;
8938 return False;
8939 end Visible_Part_Type;
8941 -- Start of processing for Check_For_Primitive_Subprogram
8943 begin
8944 Is_Primitive := False;
8946 if not Comes_From_Source (S) then
8947 null;
8949 -- If subprogram is at library level, it is not primitive operation
8951 elsif Current_Scope = Standard_Standard then
8952 null;
8954 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8955 and then not In_Package_Body (Current_Scope))
8956 or else Is_Overriding
8957 then
8958 -- For function, check return type
8960 if Ekind (S) = E_Function then
8961 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8962 F_Typ := Designated_Type (Etype (S));
8963 else
8964 F_Typ := Etype (S);
8965 end if;
8967 B_Typ := Base_Type (F_Typ);
8969 if Scope (B_Typ) = Current_Scope
8970 and then not Is_Class_Wide_Type (B_Typ)
8971 and then not Is_Generic_Type (B_Typ)
8972 then
8973 Is_Primitive := True;
8974 Set_Has_Primitive_Operations (B_Typ);
8975 Set_Is_Primitive (S);
8976 Check_Private_Overriding (B_Typ);
8977 end if;
8978 end if;
8980 -- For all subprograms, check formals
8982 Formal := First_Formal (S);
8983 while Present (Formal) loop
8984 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8985 F_Typ := Designated_Type (Etype (Formal));
8986 else
8987 F_Typ := Etype (Formal);
8988 end if;
8990 B_Typ := Base_Type (F_Typ);
8992 if Ekind (B_Typ) = E_Access_Subtype then
8993 B_Typ := Base_Type (B_Typ);
8994 end if;
8996 if Scope (B_Typ) = Current_Scope
8997 and then not Is_Class_Wide_Type (B_Typ)
8998 and then not Is_Generic_Type (B_Typ)
8999 then
9000 Is_Primitive := True;
9001 Set_Is_Primitive (S);
9002 Set_Has_Primitive_Operations (B_Typ);
9003 Check_Private_Overriding (B_Typ);
9004 end if;
9006 Next_Formal (Formal);
9007 end loop;
9009 -- Special case: An equality function can be redefined for a type
9010 -- occurring in a declarative part, and won't otherwise be treated as
9011 -- a primitive because it doesn't occur in a package spec and doesn't
9012 -- override an inherited subprogram. It's important that we mark it
9013 -- primitive so it can be returned by Collect_Primitive_Operations
9014 -- and be used in composing the equality operation of later types
9015 -- that have a component of the type.
9017 elsif Chars (S) = Name_Op_Eq
9018 and then Etype (S) = Standard_Boolean
9019 then
9020 B_Typ := Base_Type (Etype (First_Formal (S)));
9022 if Scope (B_Typ) = Current_Scope
9023 and then
9024 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9025 and then not Is_Limited_Type (B_Typ)
9026 then
9027 Is_Primitive := True;
9028 Set_Is_Primitive (S);
9029 Set_Has_Primitive_Operations (B_Typ);
9030 Check_Private_Overriding (B_Typ);
9031 end if;
9032 end if;
9033 end Check_For_Primitive_Subprogram;
9035 -----------------------------------
9036 -- Check_Synchronized_Overriding --
9037 -----------------------------------
9039 procedure Check_Synchronized_Overriding
9040 (Def_Id : Entity_Id;
9041 Overridden_Subp : out Entity_Id)
9043 Ifaces_List : Elist_Id;
9044 In_Scope : Boolean;
9045 Typ : Entity_Id;
9047 function Matches_Prefixed_View_Profile
9048 (Prim_Params : List_Id;
9049 Iface_Params : List_Id) return Boolean;
9050 -- Determine whether a subprogram's parameter profile Prim_Params
9051 -- matches that of a potentially overridden interface subprogram
9052 -- Iface_Params. Also determine if the type of first parameter of
9053 -- Iface_Params is an implemented interface.
9055 -----------------------------------
9056 -- Matches_Prefixed_View_Profile --
9057 -----------------------------------
9059 function Matches_Prefixed_View_Profile
9060 (Prim_Params : List_Id;
9061 Iface_Params : List_Id) return Boolean
9063 Iface_Id : Entity_Id;
9064 Iface_Param : Node_Id;
9065 Iface_Typ : Entity_Id;
9066 Prim_Id : Entity_Id;
9067 Prim_Param : Node_Id;
9068 Prim_Typ : Entity_Id;
9070 function Is_Implemented
9071 (Ifaces_List : Elist_Id;
9072 Iface : Entity_Id) return Boolean;
9073 -- Determine if Iface is implemented by the current task or
9074 -- protected type.
9076 --------------------
9077 -- Is_Implemented --
9078 --------------------
9080 function Is_Implemented
9081 (Ifaces_List : Elist_Id;
9082 Iface : Entity_Id) return Boolean
9084 Iface_Elmt : Elmt_Id;
9086 begin
9087 Iface_Elmt := First_Elmt (Ifaces_List);
9088 while Present (Iface_Elmt) loop
9089 if Node (Iface_Elmt) = Iface then
9090 return True;
9091 end if;
9093 Next_Elmt (Iface_Elmt);
9094 end loop;
9096 return False;
9097 end Is_Implemented;
9099 -- Start of processing for Matches_Prefixed_View_Profile
9101 begin
9102 Iface_Param := First (Iface_Params);
9103 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9105 if Is_Access_Type (Iface_Typ) then
9106 Iface_Typ := Designated_Type (Iface_Typ);
9107 end if;
9109 Prim_Param := First (Prim_Params);
9111 -- The first parameter of the potentially overridden subprogram
9112 -- must be an interface implemented by Prim.
9114 if not Is_Interface (Iface_Typ)
9115 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9116 then
9117 return False;
9118 end if;
9120 -- The checks on the object parameters are done, move onto the
9121 -- rest of the parameters.
9123 if not In_Scope then
9124 Prim_Param := Next (Prim_Param);
9125 end if;
9127 Iface_Param := Next (Iface_Param);
9128 while Present (Iface_Param) and then Present (Prim_Param) loop
9129 Iface_Id := Defining_Identifier (Iface_Param);
9130 Iface_Typ := Find_Parameter_Type (Iface_Param);
9132 Prim_Id := Defining_Identifier (Prim_Param);
9133 Prim_Typ := Find_Parameter_Type (Prim_Param);
9135 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9136 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9137 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9138 then
9139 Iface_Typ := Designated_Type (Iface_Typ);
9140 Prim_Typ := Designated_Type (Prim_Typ);
9141 end if;
9143 -- Case of multiple interface types inside a parameter profile
9145 -- (Obj_Param : in out Iface; ...; Param : Iface)
9147 -- If the interface type is implemented, then the matching type
9148 -- in the primitive should be the implementing record type.
9150 if Ekind (Iface_Typ) = E_Record_Type
9151 and then Is_Interface (Iface_Typ)
9152 and then Is_Implemented (Ifaces_List, Iface_Typ)
9153 then
9154 if Prim_Typ /= Typ then
9155 return False;
9156 end if;
9158 -- The two parameters must be both mode and subtype conformant
9160 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9161 or else not
9162 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9163 then
9164 return False;
9165 end if;
9167 Next (Iface_Param);
9168 Next (Prim_Param);
9169 end loop;
9171 -- One of the two lists contains more parameters than the other
9173 if Present (Iface_Param) or else Present (Prim_Param) then
9174 return False;
9175 end if;
9177 return True;
9178 end Matches_Prefixed_View_Profile;
9180 -- Start of processing for Check_Synchronized_Overriding
9182 begin
9183 Overridden_Subp := Empty;
9185 -- Def_Id must be an entry or a subprogram. We should skip predefined
9186 -- primitives internally generated by the frontend; however at this
9187 -- stage predefined primitives are still not fully decorated. As a
9188 -- minor optimization we skip here internally generated subprograms.
9190 if (Ekind (Def_Id) /= E_Entry
9191 and then Ekind (Def_Id) /= E_Function
9192 and then Ekind (Def_Id) /= E_Procedure)
9193 or else not Comes_From_Source (Def_Id)
9194 then
9195 return;
9196 end if;
9198 -- Search for the concurrent declaration since it contains the list
9199 -- of all implemented interfaces. In this case, the subprogram is
9200 -- declared within the scope of a protected or a task type.
9202 if Present (Scope (Def_Id))
9203 and then Is_Concurrent_Type (Scope (Def_Id))
9204 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9205 then
9206 Typ := Scope (Def_Id);
9207 In_Scope := True;
9209 -- The enclosing scope is not a synchronized type and the subprogram
9210 -- has no formals.
9212 elsif No (First_Formal (Def_Id)) then
9213 return;
9215 -- The subprogram has formals and hence it may be a primitive of a
9216 -- concurrent type.
9218 else
9219 Typ := Etype (First_Formal (Def_Id));
9221 if Is_Access_Type (Typ) then
9222 Typ := Directly_Designated_Type (Typ);
9223 end if;
9225 if Is_Concurrent_Type (Typ)
9226 and then not Is_Generic_Actual_Type (Typ)
9227 then
9228 In_Scope := False;
9230 -- This case occurs when the concurrent type is declared within
9231 -- a generic unit. As a result the corresponding record has been
9232 -- built and used as the type of the first formal, we just have
9233 -- to retrieve the corresponding concurrent type.
9235 elsif Is_Concurrent_Record_Type (Typ)
9236 and then not Is_Class_Wide_Type (Typ)
9237 and then Present (Corresponding_Concurrent_Type (Typ))
9238 then
9239 Typ := Corresponding_Concurrent_Type (Typ);
9240 In_Scope := False;
9242 else
9243 return;
9244 end if;
9245 end if;
9247 -- There is no overriding to check if is an inherited operation in a
9248 -- type derivation on for a generic actual.
9250 Collect_Interfaces (Typ, Ifaces_List);
9252 if Is_Empty_Elmt_List (Ifaces_List) then
9253 return;
9254 end if;
9256 -- Determine whether entry or subprogram Def_Id overrides a primitive
9257 -- operation that belongs to one of the interfaces in Ifaces_List.
9259 declare
9260 Candidate : Entity_Id := Empty;
9261 Hom : Entity_Id := Empty;
9262 Iface_Typ : Entity_Id;
9263 Subp : Entity_Id := Empty;
9265 begin
9266 -- Traverse the homonym chain, looking for a potentially
9267 -- overridden subprogram that belongs to an implemented
9268 -- interface.
9270 Hom := Current_Entity_In_Scope (Def_Id);
9271 while Present (Hom) loop
9272 Subp := Hom;
9274 if Subp = Def_Id
9275 or else not Is_Overloadable (Subp)
9276 or else not Is_Primitive (Subp)
9277 or else not Is_Dispatching_Operation (Subp)
9278 or else not Present (Find_Dispatching_Type (Subp))
9279 or else not Is_Interface (Find_Dispatching_Type (Subp))
9280 then
9281 null;
9283 -- Entries and procedures can override abstract or null
9284 -- interface procedures.
9286 elsif (Ekind (Def_Id) = E_Procedure
9287 or else Ekind (Def_Id) = E_Entry)
9288 and then Ekind (Subp) = E_Procedure
9289 and then Matches_Prefixed_View_Profile
9290 (Parameter_Specifications (Parent (Def_Id)),
9291 Parameter_Specifications (Parent (Subp)))
9292 then
9293 Candidate := Subp;
9295 -- For an overridden subprogram Subp, check whether the mode
9296 -- of its first parameter is correct depending on the kind
9297 -- of synchronized type.
9299 declare
9300 Formal : constant Node_Id := First_Formal (Candidate);
9302 begin
9303 -- In order for an entry or a protected procedure to
9304 -- override, the first parameter of the overridden
9305 -- routine must be of mode "out", "in out" or
9306 -- access-to-variable.
9308 if Ekind_In (Candidate, E_Entry, E_Procedure)
9309 and then Is_Protected_Type (Typ)
9310 and then Ekind (Formal) /= E_In_Out_Parameter
9311 and then Ekind (Formal) /= E_Out_Parameter
9312 and then Nkind (Parameter_Type (Parent (Formal))) /=
9313 N_Access_Definition
9314 then
9315 null;
9317 -- All other cases are OK since a task entry or routine
9318 -- does not have a restriction on the mode of the first
9319 -- parameter of the overridden interface routine.
9321 else
9322 Overridden_Subp := Candidate;
9323 return;
9324 end if;
9325 end;
9327 -- Functions can override abstract interface functions
9329 elsif Ekind (Def_Id) = E_Function
9330 and then Ekind (Subp) = E_Function
9331 and then Matches_Prefixed_View_Profile
9332 (Parameter_Specifications (Parent (Def_Id)),
9333 Parameter_Specifications (Parent (Subp)))
9334 and then Etype (Result_Definition (Parent (Def_Id))) =
9335 Etype (Result_Definition (Parent (Subp)))
9336 then
9337 Overridden_Subp := Subp;
9338 return;
9339 end if;
9341 Hom := Homonym (Hom);
9342 end loop;
9344 -- After examining all candidates for overriding, we are left with
9345 -- the best match which is a mode incompatible interface routine.
9346 -- Do not emit an error if the Expander is active since this error
9347 -- will be detected later on after all concurrent types are
9348 -- expanded and all wrappers are built. This check is meant for
9349 -- spec-only compilations.
9351 if Present (Candidate) and then not Expander_Active then
9352 Iface_Typ :=
9353 Find_Parameter_Type (Parent (First_Formal (Candidate)));
9355 -- Def_Id is primitive of a protected type, declared inside the
9356 -- type, and the candidate is primitive of a limited or
9357 -- synchronized interface.
9359 if In_Scope
9360 and then Is_Protected_Type (Typ)
9361 and then
9362 (Is_Limited_Interface (Iface_Typ)
9363 or else Is_Protected_Interface (Iface_Typ)
9364 or else Is_Synchronized_Interface (Iface_Typ)
9365 or else Is_Task_Interface (Iface_Typ))
9366 then
9367 Error_Msg_PT (Def_Id, Candidate);
9368 end if;
9369 end if;
9371 Overridden_Subp := Candidate;
9372 return;
9373 end;
9374 end Check_Synchronized_Overriding;
9376 ----------------------------
9377 -- Is_Private_Declaration --
9378 ----------------------------
9380 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9381 Priv_Decls : List_Id;
9382 Decl : constant Node_Id := Unit_Declaration_Node (E);
9384 begin
9385 if Is_Package_Or_Generic_Package (Current_Scope)
9386 and then In_Private_Part (Current_Scope)
9387 then
9388 Priv_Decls :=
9389 Private_Declarations (Package_Specification (Current_Scope));
9391 return In_Package_Body (Current_Scope)
9392 or else
9393 (Is_List_Member (Decl)
9394 and then List_Containing (Decl) = Priv_Decls)
9395 or else (Nkind (Parent (Decl)) = N_Package_Specification
9396 and then not
9397 Is_Compilation_Unit
9398 (Defining_Entity (Parent (Decl)))
9399 and then List_Containing (Parent (Parent (Decl))) =
9400 Priv_Decls);
9401 else
9402 return False;
9403 end if;
9404 end Is_Private_Declaration;
9406 --------------------------
9407 -- Is_Overriding_Alias --
9408 --------------------------
9410 function Is_Overriding_Alias
9411 (Old_E : Entity_Id;
9412 New_E : Entity_Id) return Boolean
9414 AO : constant Entity_Id := Alias (Old_E);
9415 AN : constant Entity_Id := Alias (New_E);
9416 begin
9417 return Scope (AO) /= Scope (AN)
9418 or else No (DTC_Entity (AO))
9419 or else No (DTC_Entity (AN))
9420 or else DT_Position (AO) = DT_Position (AN);
9421 end Is_Overriding_Alias;
9423 -- Start of processing for New_Overloaded_Entity
9425 begin
9426 -- We need to look for an entity that S may override. This must be a
9427 -- homonym in the current scope, so we look for the first homonym of
9428 -- S in the current scope as the starting point for the search.
9430 E := Current_Entity_In_Scope (S);
9432 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9433 -- They are directly added to the list of primitive operations of
9434 -- Derived_Type, unless this is a rederivation in the private part
9435 -- of an operation that was already derived in the visible part of
9436 -- the current package.
9438 if Ada_Version >= Ada_2005
9439 and then Present (Derived_Type)
9440 and then Present (Alias (S))
9441 and then Is_Dispatching_Operation (Alias (S))
9442 and then Present (Find_Dispatching_Type (Alias (S)))
9443 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9444 then
9445 -- For private types, when the full-view is processed we propagate to
9446 -- the full view the non-overridden entities whose attribute "alias"
9447 -- references an interface primitive. These entities were added by
9448 -- Derive_Subprograms to ensure that interface primitives are
9449 -- covered.
9451 -- Inside_Freeze_Actions is non zero when S corresponds with an
9452 -- internal entity that links an interface primitive with its
9453 -- covering primitive through attribute Interface_Alias (see
9454 -- Add_Internal_Interface_Entities).
9456 if Inside_Freezing_Actions = 0
9457 and then Is_Package_Or_Generic_Package (Current_Scope)
9458 and then In_Private_Part (Current_Scope)
9459 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9460 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9461 and then Full_View (Defining_Identifier (Parent (E)))
9462 = Defining_Identifier (Parent (S))
9463 and then Alias (E) = Alias (S)
9464 then
9465 Check_Operation_From_Private_View (S, E);
9466 Set_Is_Dispatching_Operation (S);
9468 -- Common case
9470 else
9471 Enter_Overloaded_Entity (S);
9472 Check_Dispatching_Operation (S, Empty);
9473 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9474 end if;
9476 return;
9477 end if;
9479 -- If there is no homonym then this is definitely not overriding
9481 if No (E) then
9482 Enter_Overloaded_Entity (S);
9483 Check_Dispatching_Operation (S, Empty);
9484 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9486 -- If subprogram has an explicit declaration, check whether it has an
9487 -- overriding indicator.
9489 if Comes_From_Source (S) then
9490 Check_Synchronized_Overriding (S, Overridden_Subp);
9492 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9493 -- it may have overridden some hidden inherited primitive. Update
9494 -- Overridden_Subp to avoid spurious errors when checking the
9495 -- overriding indicator.
9497 if Ada_Version >= Ada_2012
9498 and then No (Overridden_Subp)
9499 and then Is_Dispatching_Operation (S)
9500 and then Present (Overridden_Operation (S))
9501 then
9502 Overridden_Subp := Overridden_Operation (S);
9503 end if;
9505 Check_Overriding_Indicator
9506 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9507 end if;
9509 -- If there is a homonym that is not overloadable, then we have an
9510 -- error, except for the special cases checked explicitly below.
9512 elsif not Is_Overloadable (E) then
9514 -- Check for spurious conflict produced by a subprogram that has the
9515 -- same name as that of the enclosing generic package. The conflict
9516 -- occurs within an instance, between the subprogram and the renaming
9517 -- declaration for the package. After the subprogram, the package
9518 -- renaming declaration becomes hidden.
9520 if Ekind (E) = E_Package
9521 and then Present (Renamed_Object (E))
9522 and then Renamed_Object (E) = Current_Scope
9523 and then Nkind (Parent (Renamed_Object (E))) =
9524 N_Package_Specification
9525 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9526 then
9527 Set_Is_Hidden (E);
9528 Set_Is_Immediately_Visible (E, False);
9529 Enter_Overloaded_Entity (S);
9530 Set_Homonym (S, Homonym (E));
9531 Check_Dispatching_Operation (S, Empty);
9532 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9534 -- If the subprogram is implicit it is hidden by the previous
9535 -- declaration. However if it is dispatching, it must appear in the
9536 -- dispatch table anyway, because it can be dispatched to even if it
9537 -- cannot be called directly.
9539 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9540 Set_Scope (S, Current_Scope);
9542 if Is_Dispatching_Operation (Alias (S)) then
9543 Check_Dispatching_Operation (S, Empty);
9544 end if;
9546 return;
9548 else
9549 Error_Msg_Sloc := Sloc (E);
9551 -- Generate message, with useful additional warning if in generic
9553 if Is_Generic_Unit (E) then
9554 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9555 Error_Msg_N ("\& conflicts with declaration#", S);
9556 else
9557 Error_Msg_N ("& conflicts with declaration#", S);
9558 end if;
9560 return;
9561 end if;
9563 -- E exists and is overloadable
9565 else
9566 Check_Synchronized_Overriding (S, Overridden_Subp);
9568 -- Loop through E and its homonyms to determine if any of them is
9569 -- the candidate for overriding by S.
9571 while Present (E) loop
9573 -- Definitely not interesting if not in the current scope
9575 if Scope (E) /= Current_Scope then
9576 null;
9578 -- A function can overload the name of an abstract state. The
9579 -- state can be viewed as a function with a profile that cannot
9580 -- be matched by anything.
9582 elsif Ekind (S) = E_Function
9583 and then Ekind (E) = E_Abstract_State
9584 then
9585 Enter_Overloaded_Entity (S);
9586 return;
9588 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9589 -- procedures locate the internally generated spec. We enforce
9590 -- mode conformance since a tagged type may inherit from
9591 -- interfaces several null primitives which differ only in
9592 -- the mode of the formals.
9594 elsif not Comes_From_Source (S)
9595 and then Is_Null_Procedure (S)
9596 and then not Mode_Conformant (E, S)
9597 then
9598 null;
9600 -- Check if we have type conformance
9602 elsif Type_Conformant (E, S) then
9604 -- If the old and new entities have the same profile and one
9605 -- is not the body of the other, then this is an error, unless
9606 -- one of them is implicitly declared.
9608 -- There are some cases when both can be implicit, for example
9609 -- when both a literal and a function that overrides it are
9610 -- inherited in a derivation, or when an inherited operation
9611 -- of a tagged full type overrides the inherited operation of
9612 -- a private extension. Ada 83 had a special rule for the
9613 -- literal case. In Ada 95, the later implicit operation hides
9614 -- the former, and the literal is always the former. In the
9615 -- odd case where both are derived operations declared at the
9616 -- same point, both operations should be declared, and in that
9617 -- case we bypass the following test and proceed to the next
9618 -- part. This can only occur for certain obscure cases in
9619 -- instances, when an operation on a type derived from a formal
9620 -- private type does not override a homograph inherited from
9621 -- the actual. In subsequent derivations of such a type, the
9622 -- DT positions of these operations remain distinct, if they
9623 -- have been set.
9625 if Present (Alias (S))
9626 and then (No (Alias (E))
9627 or else Comes_From_Source (E)
9628 or else Is_Abstract_Subprogram (S)
9629 or else
9630 (Is_Dispatching_Operation (E)
9631 and then Is_Overriding_Alias (E, S)))
9632 and then Ekind (E) /= E_Enumeration_Literal
9633 then
9634 -- When an derived operation is overloaded it may be due to
9635 -- the fact that the full view of a private extension
9636 -- re-inherits. It has to be dealt with.
9638 if Is_Package_Or_Generic_Package (Current_Scope)
9639 and then In_Private_Part (Current_Scope)
9640 then
9641 Check_Operation_From_Private_View (S, E);
9642 end if;
9644 -- In any case the implicit operation remains hidden by the
9645 -- existing declaration, which is overriding. Indicate that
9646 -- E overrides the operation from which S is inherited.
9648 if Present (Alias (S)) then
9649 Set_Overridden_Operation (E, Alias (S));
9650 Inherit_Subprogram_Contract (E, Alias (S));
9652 else
9653 Set_Overridden_Operation (E, S);
9654 Inherit_Subprogram_Contract (E, S);
9655 end if;
9657 if Comes_From_Source (E) then
9658 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9659 end if;
9661 return;
9663 -- Within an instance, the renaming declarations for actual
9664 -- subprograms may become ambiguous, but they do not hide each
9665 -- other.
9667 elsif Ekind (E) /= E_Entry
9668 and then not Comes_From_Source (E)
9669 and then not Is_Generic_Instance (E)
9670 and then (Present (Alias (E))
9671 or else Is_Intrinsic_Subprogram (E))
9672 and then (not In_Instance
9673 or else No (Parent (E))
9674 or else Nkind (Unit_Declaration_Node (E)) /=
9675 N_Subprogram_Renaming_Declaration)
9676 then
9677 -- A subprogram child unit is not allowed to override an
9678 -- inherited subprogram (10.1.1(20)).
9680 if Is_Child_Unit (S) then
9681 Error_Msg_N
9682 ("child unit overrides inherited subprogram in parent",
9684 return;
9685 end if;
9687 if Is_Non_Overriding_Operation (E, S) then
9688 Enter_Overloaded_Entity (S);
9690 if No (Derived_Type)
9691 or else Is_Tagged_Type (Derived_Type)
9692 then
9693 Check_Dispatching_Operation (S, Empty);
9694 end if;
9696 return;
9697 end if;
9699 -- E is a derived operation or an internal operator which
9700 -- is being overridden. Remove E from further visibility.
9701 -- Furthermore, if E is a dispatching operation, it must be
9702 -- replaced in the list of primitive operations of its type
9703 -- (see Override_Dispatching_Operation).
9705 Overridden_Subp := E;
9707 declare
9708 Prev : Entity_Id;
9710 begin
9711 Prev := First_Entity (Current_Scope);
9712 while Present (Prev) and then Next_Entity (Prev) /= E loop
9713 Next_Entity (Prev);
9714 end loop;
9716 -- It is possible for E to be in the current scope and
9717 -- yet not in the entity chain. This can only occur in a
9718 -- generic context where E is an implicit concatenation
9719 -- in the formal part, because in a generic body the
9720 -- entity chain starts with the formals.
9722 -- In GNATprove mode, a wrapper for an operation with
9723 -- axiomatization may be a homonym of another declaration
9724 -- for an actual subprogram (needs refinement ???).
9726 if No (Prev) then
9727 if In_Instance
9728 and then GNATprove_Mode
9729 and then
9730 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9731 N_Subprogram_Renaming_Declaration
9732 then
9733 return;
9734 else
9735 pragma Assert (Chars (E) = Name_Op_Concat);
9736 null;
9737 end if;
9738 end if;
9740 -- E must be removed both from the entity_list of the
9741 -- current scope, and from the visibility chain.
9743 if Debug_Flag_E then
9744 Write_Str ("Override implicit operation ");
9745 Write_Int (Int (E));
9746 Write_Eol;
9747 end if;
9749 -- If E is a predefined concatenation, it stands for four
9750 -- different operations. As a result, a single explicit
9751 -- declaration does not hide it. In a possible ambiguous
9752 -- situation, Disambiguate chooses the user-defined op,
9753 -- so it is correct to retain the previous internal one.
9755 if Chars (E) /= Name_Op_Concat
9756 or else Ekind (E) /= E_Operator
9757 then
9758 -- For nondispatching derived operations that are
9759 -- overridden by a subprogram declared in the private
9760 -- part of a package, we retain the derived subprogram
9761 -- but mark it as not immediately visible. If the
9762 -- derived operation was declared in the visible part
9763 -- then this ensures that it will still be visible
9764 -- outside the package with the proper signature
9765 -- (calls from outside must also be directed to this
9766 -- version rather than the overriding one, unlike the
9767 -- dispatching case). Calls from inside the package
9768 -- will still resolve to the overriding subprogram
9769 -- since the derived one is marked as not visible
9770 -- within the package.
9772 -- If the private operation is dispatching, we achieve
9773 -- the overriding by keeping the implicit operation
9774 -- but setting its alias to be the overriding one. In
9775 -- this fashion the proper body is executed in all
9776 -- cases, but the original signature is used outside
9777 -- of the package.
9779 -- If the overriding is not in the private part, we
9780 -- remove the implicit operation altogether.
9782 if Is_Private_Declaration (S) then
9783 if not Is_Dispatching_Operation (E) then
9784 Set_Is_Immediately_Visible (E, False);
9785 else
9786 -- Work done in Override_Dispatching_Operation,
9787 -- so nothing else needs to be done here.
9789 null;
9790 end if;
9792 else
9793 -- Find predecessor of E in Homonym chain
9795 if E = Current_Entity (E) then
9796 Prev_Vis := Empty;
9797 else
9798 Prev_Vis := Current_Entity (E);
9799 while Homonym (Prev_Vis) /= E loop
9800 Prev_Vis := Homonym (Prev_Vis);
9801 end loop;
9802 end if;
9804 if Prev_Vis /= Empty then
9806 -- Skip E in the visibility chain
9808 Set_Homonym (Prev_Vis, Homonym (E));
9810 else
9811 Set_Name_Entity_Id (Chars (E), Homonym (E));
9812 end if;
9814 Set_Next_Entity (Prev, Next_Entity (E));
9816 if No (Next_Entity (Prev)) then
9817 Set_Last_Entity (Current_Scope, Prev);
9818 end if;
9819 end if;
9820 end if;
9822 Enter_Overloaded_Entity (S);
9824 -- For entities generated by Derive_Subprograms the
9825 -- overridden operation is the inherited primitive
9826 -- (which is available through the attribute alias).
9828 if not (Comes_From_Source (E))
9829 and then Is_Dispatching_Operation (E)
9830 and then Find_Dispatching_Type (E) =
9831 Find_Dispatching_Type (S)
9832 and then Present (Alias (E))
9833 and then Comes_From_Source (Alias (E))
9834 then
9835 Set_Overridden_Operation (S, Alias (E));
9836 Inherit_Subprogram_Contract (S, Alias (E));
9838 -- Normal case of setting entity as overridden
9840 -- Note: Static_Initialization and Overridden_Operation
9841 -- attributes use the same field in subprogram entities.
9842 -- Static_Initialization is only defined for internal
9843 -- initialization procedures, where Overridden_Operation
9844 -- is irrelevant. Therefore the setting of this attribute
9845 -- must check whether the target is an init_proc.
9847 elsif not Is_Init_Proc (S) then
9848 Set_Overridden_Operation (S, E);
9849 Inherit_Subprogram_Contract (S, E);
9850 end if;
9852 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9854 -- If S is a user-defined subprogram or a null procedure
9855 -- expanded to override an inherited null procedure, or a
9856 -- predefined dispatching primitive then indicate that E
9857 -- overrides the operation from which S is inherited.
9859 if Comes_From_Source (S)
9860 or else
9861 (Present (Parent (S))
9862 and then
9863 Nkind (Parent (S)) = N_Procedure_Specification
9864 and then
9865 Null_Present (Parent (S)))
9866 or else
9867 (Present (Alias (E))
9868 and then
9869 Is_Predefined_Dispatching_Operation (Alias (E)))
9870 then
9871 if Present (Alias (E)) then
9872 Set_Overridden_Operation (S, Alias (E));
9873 Inherit_Subprogram_Contract (S, Alias (E));
9874 end if;
9875 end if;
9877 if Is_Dispatching_Operation (E) then
9879 -- An overriding dispatching subprogram inherits the
9880 -- convention of the overridden subprogram (AI-117).
9882 Set_Convention (S, Convention (E));
9883 Check_Dispatching_Operation (S, E);
9885 else
9886 Check_Dispatching_Operation (S, Empty);
9887 end if;
9889 Check_For_Primitive_Subprogram
9890 (Is_Primitive_Subp, Is_Overriding => True);
9891 goto Check_Inequality;
9892 end;
9894 -- Apparent redeclarations in instances can occur when two
9895 -- formal types get the same actual type. The subprograms in
9896 -- in the instance are legal, even if not callable from the
9897 -- outside. Calls from within are disambiguated elsewhere.
9898 -- For dispatching operations in the visible part, the usual
9899 -- rules apply, and operations with the same profile are not
9900 -- legal (B830001).
9902 elsif (In_Instance_Visible_Part
9903 and then not Is_Dispatching_Operation (E))
9904 or else In_Instance_Not_Visible
9905 then
9906 null;
9908 -- Here we have a real error (identical profile)
9910 else
9911 Error_Msg_Sloc := Sloc (E);
9913 -- Avoid cascaded errors if the entity appears in
9914 -- subsequent calls.
9916 Set_Scope (S, Current_Scope);
9918 -- Generate error, with extra useful warning for the case
9919 -- of a generic instance with no completion.
9921 if Is_Generic_Instance (S)
9922 and then not Has_Completion (E)
9923 then
9924 Error_Msg_N
9925 ("instantiation cannot provide body for&", S);
9926 Error_Msg_N ("\& conflicts with declaration#", S);
9927 else
9928 Error_Msg_N ("& conflicts with declaration#", S);
9929 end if;
9931 return;
9932 end if;
9934 else
9935 -- If one subprogram has an access parameter and the other
9936 -- a parameter of an access type, calls to either might be
9937 -- ambiguous. Verify that parameters match except for the
9938 -- access parameter.
9940 if May_Hide_Profile then
9941 declare
9942 F1 : Entity_Id;
9943 F2 : Entity_Id;
9945 begin
9946 F1 := First_Formal (S);
9947 F2 := First_Formal (E);
9948 while Present (F1) and then Present (F2) loop
9949 if Is_Access_Type (Etype (F1)) then
9950 if not Is_Access_Type (Etype (F2))
9951 or else not Conforming_Types
9952 (Designated_Type (Etype (F1)),
9953 Designated_Type (Etype (F2)),
9954 Type_Conformant)
9955 then
9956 May_Hide_Profile := False;
9957 end if;
9959 elsif
9960 not Conforming_Types
9961 (Etype (F1), Etype (F2), Type_Conformant)
9962 then
9963 May_Hide_Profile := False;
9964 end if;
9966 Next_Formal (F1);
9967 Next_Formal (F2);
9968 end loop;
9970 if May_Hide_Profile
9971 and then No (F1)
9972 and then No (F2)
9973 then
9974 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9975 end if;
9976 end;
9977 end if;
9978 end if;
9980 E := Homonym (E);
9981 end loop;
9983 -- On exit, we know that S is a new entity
9985 Enter_Overloaded_Entity (S);
9986 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9987 Check_Overriding_Indicator
9988 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9990 -- Overloading is not allowed in SPARK, except for operators
9992 if Nkind (S) /= N_Defining_Operator_Symbol then
9993 Error_Msg_Sloc := Sloc (Homonym (S));
9994 Check_SPARK_05_Restriction
9995 ("overloading not allowed with entity#", S);
9996 end if;
9998 -- If S is a derived operation for an untagged type then by
9999 -- definition it's not a dispatching operation (even if the parent
10000 -- operation was dispatching), so Check_Dispatching_Operation is not
10001 -- called in that case.
10003 if No (Derived_Type)
10004 or else Is_Tagged_Type (Derived_Type)
10005 then
10006 Check_Dispatching_Operation (S, Empty);
10007 end if;
10008 end if;
10010 -- If this is a user-defined equality operator that is not a derived
10011 -- subprogram, create the corresponding inequality. If the operation is
10012 -- dispatching, the expansion is done elsewhere, and we do not create
10013 -- an explicit inequality operation.
10015 <<Check_Inequality>>
10016 if Chars (S) = Name_Op_Eq
10017 and then Etype (S) = Standard_Boolean
10018 and then Present (Parent (S))
10019 and then not Is_Dispatching_Operation (S)
10020 then
10021 Make_Inequality_Operator (S);
10022 Check_Untagged_Equality (S);
10023 end if;
10024 end New_Overloaded_Entity;
10026 ---------------------
10027 -- Process_Formals --
10028 ---------------------
10030 procedure Process_Formals
10031 (T : List_Id;
10032 Related_Nod : Node_Id)
10034 Param_Spec : Node_Id;
10035 Formal : Entity_Id;
10036 Formal_Type : Entity_Id;
10037 Default : Node_Id;
10038 Ptype : Entity_Id;
10040 Num_Out_Params : Nat := 0;
10041 First_Out_Param : Entity_Id := Empty;
10042 -- Used for setting Is_Only_Out_Parameter
10044 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10045 -- Determine whether an access type designates a type coming from a
10046 -- limited view.
10048 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10049 -- Check whether the default has a class-wide type. After analysis the
10050 -- default has the type of the formal, so we must also check explicitly
10051 -- for an access attribute.
10053 ----------------------------------
10054 -- Designates_From_Limited_With --
10055 ----------------------------------
10057 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10058 Desig : Entity_Id := Typ;
10060 begin
10061 if Is_Access_Type (Desig) then
10062 Desig := Directly_Designated_Type (Desig);
10063 end if;
10065 if Is_Class_Wide_Type (Desig) then
10066 Desig := Root_Type (Desig);
10067 end if;
10069 return
10070 Ekind (Desig) = E_Incomplete_Type
10071 and then From_Limited_With (Desig);
10072 end Designates_From_Limited_With;
10074 ---------------------------
10075 -- Is_Class_Wide_Default --
10076 ---------------------------
10078 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10079 begin
10080 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10081 or else (Nkind (D) = N_Attribute_Reference
10082 and then Attribute_Name (D) = Name_Access
10083 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10084 end Is_Class_Wide_Default;
10086 -- Start of processing for Process_Formals
10088 begin
10089 -- In order to prevent premature use of the formals in the same formal
10090 -- part, the Ekind is left undefined until all default expressions are
10091 -- analyzed. The Ekind is established in a separate loop at the end.
10093 Param_Spec := First (T);
10094 while Present (Param_Spec) loop
10095 Formal := Defining_Identifier (Param_Spec);
10096 Set_Never_Set_In_Source (Formal, True);
10097 Enter_Name (Formal);
10099 -- Case of ordinary parameters
10101 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10102 Find_Type (Parameter_Type (Param_Spec));
10103 Ptype := Parameter_Type (Param_Spec);
10105 if Ptype = Error then
10106 goto Continue;
10107 end if;
10109 Formal_Type := Entity (Ptype);
10111 if Is_Incomplete_Type (Formal_Type)
10112 or else
10113 (Is_Class_Wide_Type (Formal_Type)
10114 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10115 then
10116 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10117 -- primitive operations, as long as their completion is
10118 -- in the same declarative part. If in the private part
10119 -- this means that the type cannot be a Taft-amendment type.
10120 -- Check is done on package exit. For access to subprograms,
10121 -- the use is legal for Taft-amendment types.
10123 -- Ada 2012: tagged incomplete types are allowed as generic
10124 -- formal types. They do not introduce dependencies and the
10125 -- corresponding generic subprogram does not have a delayed
10126 -- freeze, because it does not need a freeze node. However,
10127 -- it is still the case that untagged incomplete types cannot
10128 -- be Taft-amendment types and must be completed in private
10129 -- part, so the subprogram must appear in the list of private
10130 -- dependents of the type.
10132 if Is_Tagged_Type (Formal_Type)
10133 or else (Ada_Version >= Ada_2012
10134 and then not From_Limited_With (Formal_Type)
10135 and then not Is_Generic_Type (Formal_Type))
10136 then
10137 if Ekind (Scope (Current_Scope)) = E_Package
10138 and then not Is_Generic_Type (Formal_Type)
10139 and then not Is_Class_Wide_Type (Formal_Type)
10140 then
10141 if not Nkind_In
10142 (Parent (T), N_Access_Function_Definition,
10143 N_Access_Procedure_Definition)
10144 then
10145 Append_Elmt
10146 (Current_Scope,
10147 To => Private_Dependents (Base_Type (Formal_Type)));
10149 -- Freezing is delayed to ensure that Register_Prim
10150 -- will get called for this operation, which is needed
10151 -- in cases where static dispatch tables aren't built.
10152 -- (Note that the same is done for controlling access
10153 -- parameter cases in function Access_Definition.)
10155 if not Is_Thunk (Current_Scope) then
10156 Set_Has_Delayed_Freeze (Current_Scope);
10157 end if;
10158 end if;
10159 end if;
10161 -- Special handling of Value_Type for CIL case
10163 elsif Is_Value_Type (Formal_Type) then
10164 null;
10166 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10167 N_Access_Procedure_Definition)
10168 then
10169 -- AI05-0151: Tagged incomplete types are allowed in all
10170 -- formal parts. Untagged incomplete types are not allowed
10171 -- in bodies. Limited views of either kind are not allowed
10172 -- if there is no place at which the non-limited view can
10173 -- become available.
10175 -- Incomplete formal untagged types are not allowed in
10176 -- subprogram bodies (but are legal in their declarations).
10178 if Is_Generic_Type (Formal_Type)
10179 and then not Is_Tagged_Type (Formal_Type)
10180 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10181 then
10182 Error_Msg_N
10183 ("invalid use of formal incomplete type", Param_Spec);
10185 elsif Ada_Version >= Ada_2012 then
10186 if Is_Tagged_Type (Formal_Type)
10187 and then (not From_Limited_With (Formal_Type)
10188 or else not In_Package_Body)
10189 then
10190 null;
10192 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10193 N_Accept_Alternative,
10194 N_Entry_Body,
10195 N_Subprogram_Body)
10196 then
10197 Error_Msg_NE
10198 ("invalid use of untagged incomplete type&",
10199 Ptype, Formal_Type);
10200 end if;
10202 else
10203 Error_Msg_NE
10204 ("invalid use of incomplete type&",
10205 Param_Spec, Formal_Type);
10207 -- Further checks on the legality of incomplete types
10208 -- in formal parts are delayed until the freeze point
10209 -- of the enclosing subprogram or access to subprogram.
10210 end if;
10211 end if;
10213 elsif Ekind (Formal_Type) = E_Void then
10214 Error_Msg_NE
10215 ("premature use of&",
10216 Parameter_Type (Param_Spec), Formal_Type);
10217 end if;
10219 -- Ada 2012 (AI-142): Handle aliased parameters
10221 if Ada_Version >= Ada_2012
10222 and then Aliased_Present (Param_Spec)
10223 then
10224 Set_Is_Aliased (Formal);
10225 end if;
10227 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10228 -- declaration corresponding to the null-excluding type of the
10229 -- formal in the enclosing scope. Finally, replace the parameter
10230 -- type of the formal with the internal subtype.
10232 if Ada_Version >= Ada_2005
10233 and then Null_Exclusion_Present (Param_Spec)
10234 then
10235 if not Is_Access_Type (Formal_Type) then
10236 Error_Msg_N
10237 ("`NOT NULL` allowed only for an access type", Param_Spec);
10239 else
10240 if Can_Never_Be_Null (Formal_Type)
10241 and then Comes_From_Source (Related_Nod)
10242 then
10243 Error_Msg_NE
10244 ("`NOT NULL` not allowed (& already excludes null)",
10245 Param_Spec, Formal_Type);
10246 end if;
10248 Formal_Type :=
10249 Create_Null_Excluding_Itype
10250 (T => Formal_Type,
10251 Related_Nod => Related_Nod,
10252 Scope_Id => Scope (Current_Scope));
10254 -- If the designated type of the itype is an itype that is
10255 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10256 -- on the access subtype, to prevent order-of-elaboration
10257 -- issues in the backend.
10259 -- Example:
10260 -- type T is access procedure;
10261 -- procedure Op (O : not null T);
10263 if Is_Itype (Directly_Designated_Type (Formal_Type))
10264 and then
10265 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10266 then
10267 Set_Has_Delayed_Freeze (Formal_Type);
10268 end if;
10269 end if;
10270 end if;
10272 -- An access formal type
10274 else
10275 Formal_Type :=
10276 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10278 -- No need to continue if we already notified errors
10280 if not Present (Formal_Type) then
10281 return;
10282 end if;
10284 -- Ada 2005 (AI-254)
10286 declare
10287 AD : constant Node_Id :=
10288 Access_To_Subprogram_Definition
10289 (Parameter_Type (Param_Spec));
10290 begin
10291 if Present (AD) and then Protected_Present (AD) then
10292 Formal_Type :=
10293 Replace_Anonymous_Access_To_Protected_Subprogram
10294 (Param_Spec);
10295 end if;
10296 end;
10297 end if;
10299 Set_Etype (Formal, Formal_Type);
10301 -- Deal with default expression if present
10303 Default := Expression (Param_Spec);
10305 if Present (Default) then
10306 Check_SPARK_05_Restriction
10307 ("default expression is not allowed", Default);
10309 if Out_Present (Param_Spec) then
10310 Error_Msg_N
10311 ("default initialization only allowed for IN parameters",
10312 Param_Spec);
10313 end if;
10315 -- Do the special preanalysis of the expression (see section on
10316 -- "Handling of Default Expressions" in the spec of package Sem).
10318 Preanalyze_Spec_Expression (Default, Formal_Type);
10320 -- An access to constant cannot be the default for
10321 -- an access parameter that is an access to variable.
10323 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10324 and then not Is_Access_Constant (Formal_Type)
10325 and then Is_Access_Type (Etype (Default))
10326 and then Is_Access_Constant (Etype (Default))
10327 then
10328 Error_Msg_N
10329 ("formal that is access to variable cannot be initialized "
10330 & "with an access-to-constant expression", Default);
10331 end if;
10333 -- Check that the designated type of an access parameter's default
10334 -- is not a class-wide type unless the parameter's designated type
10335 -- is also class-wide.
10337 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10338 and then not Designates_From_Limited_With (Formal_Type)
10339 and then Is_Class_Wide_Default (Default)
10340 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10341 then
10342 Error_Msg_N
10343 ("access to class-wide expression not allowed here", Default);
10344 end if;
10346 -- Check incorrect use of dynamically tagged expressions
10348 if Is_Tagged_Type (Formal_Type) then
10349 Check_Dynamically_Tagged_Expression
10350 (Expr => Default,
10351 Typ => Formal_Type,
10352 Related_Nod => Default);
10353 end if;
10354 end if;
10356 -- Ada 2005 (AI-231): Static checks
10358 if Ada_Version >= Ada_2005
10359 and then Is_Access_Type (Etype (Formal))
10360 and then Can_Never_Be_Null (Etype (Formal))
10361 then
10362 Null_Exclusion_Static_Checks (Param_Spec);
10363 end if;
10365 -- The following checks are relevant when SPARK_Mode is on as these
10366 -- are not standard Ada legality rules.
10368 if SPARK_Mode = On then
10369 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10371 -- A function cannot have a parameter of mode IN OUT or OUT
10372 -- (SPARK RM 6.1).
10374 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10375 Error_Msg_N
10376 ("function cannot have parameter of mode `OUT` or "
10377 & "`IN OUT`", Formal);
10379 -- A function cannot have an effectively volatile formal
10380 -- parameter (SPARK RM 7.1.3(10)).
10382 elsif Is_Effectively_Volatile (Formal) then
10383 Error_Msg_N
10384 ("function cannot have a volatile formal parameter",
10385 Formal);
10386 end if;
10388 -- A procedure cannot have an effectively volatile formal
10389 -- parameter of mode IN because it behaves as a constant
10390 -- (SPARK RM 7.1.3(6)).
10392 elsif Ekind (Scope (Formal)) = E_Procedure
10393 and then Ekind (Formal) = E_In_Parameter
10394 and then Is_Effectively_Volatile (Formal)
10395 then
10396 Error_Msg_N
10397 ("formal parameter of mode `IN` cannot be volatile", Formal);
10398 end if;
10399 end if;
10401 <<Continue>>
10402 Next (Param_Spec);
10403 end loop;
10405 -- If this is the formal part of a function specification, analyze the
10406 -- subtype mark in the context where the formals are visible but not
10407 -- yet usable, and may hide outer homographs.
10409 if Nkind (Related_Nod) = N_Function_Specification then
10410 Analyze_Return_Type (Related_Nod);
10411 end if;
10413 -- Now set the kind (mode) of each formal
10415 Param_Spec := First (T);
10416 while Present (Param_Spec) loop
10417 Formal := Defining_Identifier (Param_Spec);
10418 Set_Formal_Mode (Formal);
10420 if Ekind (Formal) = E_In_Parameter then
10421 Set_Default_Value (Formal, Expression (Param_Spec));
10423 if Present (Expression (Param_Spec)) then
10424 Default := Expression (Param_Spec);
10426 if Is_Scalar_Type (Etype (Default)) then
10427 if Nkind (Parameter_Type (Param_Spec)) /=
10428 N_Access_Definition
10429 then
10430 Formal_Type := Entity (Parameter_Type (Param_Spec));
10431 else
10432 Formal_Type :=
10433 Access_Definition
10434 (Related_Nod, Parameter_Type (Param_Spec));
10435 end if;
10437 Apply_Scalar_Range_Check (Default, Formal_Type);
10438 end if;
10439 end if;
10441 elsif Ekind (Formal) = E_Out_Parameter then
10442 Num_Out_Params := Num_Out_Params + 1;
10444 if Num_Out_Params = 1 then
10445 First_Out_Param := Formal;
10446 end if;
10448 elsif Ekind (Formal) = E_In_Out_Parameter then
10449 Num_Out_Params := Num_Out_Params + 1;
10450 end if;
10452 -- Skip remaining processing if formal type was in error
10454 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10455 goto Next_Parameter;
10456 end if;
10458 -- Force call by reference if aliased
10460 if Is_Aliased (Formal) then
10461 Set_Mechanism (Formal, By_Reference);
10463 -- Warn if user asked this to be passed by copy
10465 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10466 Error_Msg_N
10467 ("cannot pass aliased parameter & by copy??", Formal);
10468 end if;
10470 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10472 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10473 Set_Mechanism (Formal, By_Copy);
10475 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10476 Set_Mechanism (Formal, By_Reference);
10477 end if;
10479 <<Next_Parameter>>
10480 Next (Param_Spec);
10481 end loop;
10483 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10484 Set_Is_Only_Out_Parameter (First_Out_Param);
10485 end if;
10486 end Process_Formals;
10488 ----------------------------
10489 -- Reference_Body_Formals --
10490 ----------------------------
10492 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10493 Fs : Entity_Id;
10494 Fb : Entity_Id;
10496 begin
10497 if Error_Posted (Spec) then
10498 return;
10499 end if;
10501 -- Iterate over both lists. They may be of different lengths if the two
10502 -- specs are not conformant.
10504 Fs := First_Formal (Spec);
10505 Fb := First_Formal (Bod);
10506 while Present (Fs) and then Present (Fb) loop
10507 Generate_Reference (Fs, Fb, 'b');
10509 if Style_Check then
10510 Style.Check_Identifier (Fb, Fs);
10511 end if;
10513 Set_Spec_Entity (Fb, Fs);
10514 Set_Referenced (Fs, False);
10515 Next_Formal (Fs);
10516 Next_Formal (Fb);
10517 end loop;
10518 end Reference_Body_Formals;
10520 -------------------------
10521 -- Set_Actual_Subtypes --
10522 -------------------------
10524 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10525 Decl : Node_Id;
10526 Formal : Entity_Id;
10527 T : Entity_Id;
10528 First_Stmt : Node_Id := Empty;
10529 AS_Needed : Boolean;
10531 begin
10532 -- If this is an empty initialization procedure, no need to create
10533 -- actual subtypes (small optimization).
10535 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10536 return;
10537 end if;
10539 Formal := First_Formal (Subp);
10540 while Present (Formal) loop
10541 T := Etype (Formal);
10543 -- We never need an actual subtype for a constrained formal
10545 if Is_Constrained (T) then
10546 AS_Needed := False;
10548 -- If we have unknown discriminants, then we do not need an actual
10549 -- subtype, or more accurately we cannot figure it out. Note that
10550 -- all class-wide types have unknown discriminants.
10552 elsif Has_Unknown_Discriminants (T) then
10553 AS_Needed := False;
10555 -- At this stage we have an unconstrained type that may need an
10556 -- actual subtype. For sure the actual subtype is needed if we have
10557 -- an unconstrained array type.
10559 elsif Is_Array_Type (T) then
10560 AS_Needed := True;
10562 -- The only other case needing an actual subtype is an unconstrained
10563 -- record type which is an IN parameter (we cannot generate actual
10564 -- subtypes for the OUT or IN OUT case, since an assignment can
10565 -- change the discriminant values. However we exclude the case of
10566 -- initialization procedures, since discriminants are handled very
10567 -- specially in this context, see the section entitled "Handling of
10568 -- Discriminants" in Einfo.
10570 -- We also exclude the case of Discrim_SO_Functions (functions used
10571 -- in front end layout mode for size/offset values), since in such
10572 -- functions only discriminants are referenced, and not only are such
10573 -- subtypes not needed, but they cannot always be generated, because
10574 -- of order of elaboration issues.
10576 elsif Is_Record_Type (T)
10577 and then Ekind (Formal) = E_In_Parameter
10578 and then Chars (Formal) /= Name_uInit
10579 and then not Is_Unchecked_Union (T)
10580 and then not Is_Discrim_SO_Function (Subp)
10581 then
10582 AS_Needed := True;
10584 -- All other cases do not need an actual subtype
10586 else
10587 AS_Needed := False;
10588 end if;
10590 -- Generate actual subtypes for unconstrained arrays and
10591 -- unconstrained discriminated records.
10593 if AS_Needed then
10594 if Nkind (N) = N_Accept_Statement then
10596 -- If expansion is active, the formal is replaced by a local
10597 -- variable that renames the corresponding entry of the
10598 -- parameter block, and it is this local variable that may
10599 -- require an actual subtype.
10601 if Expander_Active then
10602 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10603 else
10604 Decl := Build_Actual_Subtype (T, Formal);
10605 end if;
10607 if Present (Handled_Statement_Sequence (N)) then
10608 First_Stmt :=
10609 First (Statements (Handled_Statement_Sequence (N)));
10610 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10611 Mark_Rewrite_Insertion (Decl);
10612 else
10613 -- If the accept statement has no body, there will be no
10614 -- reference to the actuals, so no need to compute actual
10615 -- subtypes.
10617 return;
10618 end if;
10620 else
10621 Decl := Build_Actual_Subtype (T, Formal);
10622 Prepend (Decl, Declarations (N));
10623 Mark_Rewrite_Insertion (Decl);
10624 end if;
10626 -- The declaration uses the bounds of an existing object, and
10627 -- therefore needs no constraint checks.
10629 Analyze (Decl, Suppress => All_Checks);
10631 -- We need to freeze manually the generated type when it is
10632 -- inserted anywhere else than in a declarative part.
10634 if Present (First_Stmt) then
10635 Insert_List_Before_And_Analyze (First_Stmt,
10636 Freeze_Entity (Defining_Identifier (Decl), N));
10638 -- Ditto if the type has a dynamic predicate, because the
10639 -- generated function will mention the actual subtype.
10641 elsif Has_Dynamic_Predicate_Aspect (T) then
10642 Insert_List_Before_And_Analyze (Decl,
10643 Freeze_Entity (Defining_Identifier (Decl), N));
10644 end if;
10646 if Nkind (N) = N_Accept_Statement
10647 and then Expander_Active
10648 then
10649 Set_Actual_Subtype (Renamed_Object (Formal),
10650 Defining_Identifier (Decl));
10651 else
10652 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10653 end if;
10654 end if;
10656 Next_Formal (Formal);
10657 end loop;
10658 end Set_Actual_Subtypes;
10660 ---------------------
10661 -- Set_Formal_Mode --
10662 ---------------------
10664 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10665 Spec : constant Node_Id := Parent (Formal_Id);
10667 begin
10668 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10669 -- since we ensure that corresponding actuals are always valid at the
10670 -- point of the call.
10672 if Out_Present (Spec) then
10673 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10675 -- [IN] OUT parameters allowed for functions in Ada 2012
10677 if Ada_Version >= Ada_2012 then
10679 -- Even in Ada 2012 operators can only have IN parameters
10681 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10682 Error_Msg_N ("operators can only have IN parameters", Spec);
10683 end if;
10685 if In_Present (Spec) then
10686 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10687 else
10688 Set_Ekind (Formal_Id, E_Out_Parameter);
10689 end if;
10691 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10693 -- But not in earlier versions of Ada
10695 else
10696 Error_Msg_N ("functions can only have IN parameters", Spec);
10697 Set_Ekind (Formal_Id, E_In_Parameter);
10698 end if;
10700 elsif In_Present (Spec) then
10701 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10703 else
10704 Set_Ekind (Formal_Id, E_Out_Parameter);
10705 Set_Never_Set_In_Source (Formal_Id, True);
10706 Set_Is_True_Constant (Formal_Id, False);
10707 Set_Current_Value (Formal_Id, Empty);
10708 end if;
10710 else
10711 Set_Ekind (Formal_Id, E_In_Parameter);
10712 end if;
10714 -- Set Is_Known_Non_Null for access parameters since the language
10715 -- guarantees that access parameters are always non-null. We also set
10716 -- Can_Never_Be_Null, since there is no way to change the value.
10718 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10720 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10721 -- null; In Ada 2005, only if then null_exclusion is explicit.
10723 if Ada_Version < Ada_2005
10724 or else Can_Never_Be_Null (Etype (Formal_Id))
10725 then
10726 Set_Is_Known_Non_Null (Formal_Id);
10727 Set_Can_Never_Be_Null (Formal_Id);
10728 end if;
10730 -- Ada 2005 (AI-231): Null-exclusion access subtype
10732 elsif Is_Access_Type (Etype (Formal_Id))
10733 and then Can_Never_Be_Null (Etype (Formal_Id))
10734 then
10735 Set_Is_Known_Non_Null (Formal_Id);
10737 -- We can also set Can_Never_Be_Null (thus preventing some junk
10738 -- access checks) for the case of an IN parameter, which cannot
10739 -- be changed, or for an IN OUT parameter, which can be changed but
10740 -- not to a null value. But for an OUT parameter, the initial value
10741 -- passed in can be null, so we can't set this flag in that case.
10743 if Ekind (Formal_Id) /= E_Out_Parameter then
10744 Set_Can_Never_Be_Null (Formal_Id);
10745 end if;
10746 end if;
10748 Set_Mechanism (Formal_Id, Default_Mechanism);
10749 Set_Formal_Validity (Formal_Id);
10750 end Set_Formal_Mode;
10752 -------------------------
10753 -- Set_Formal_Validity --
10754 -------------------------
10756 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10757 begin
10758 -- If no validity checking, then we cannot assume anything about the
10759 -- validity of parameters, since we do not know there is any checking
10760 -- of the validity on the call side.
10762 if not Validity_Checks_On then
10763 return;
10765 -- If validity checking for parameters is enabled, this means we are
10766 -- not supposed to make any assumptions about argument values.
10768 elsif Validity_Check_Parameters then
10769 return;
10771 -- If we are checking in parameters, we will assume that the caller is
10772 -- also checking parameters, so we can assume the parameter is valid.
10774 elsif Ekind (Formal_Id) = E_In_Parameter
10775 and then Validity_Check_In_Params
10776 then
10777 Set_Is_Known_Valid (Formal_Id, True);
10779 -- Similar treatment for IN OUT parameters
10781 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10782 and then Validity_Check_In_Out_Params
10783 then
10784 Set_Is_Known_Valid (Formal_Id, True);
10785 end if;
10786 end Set_Formal_Validity;
10788 ------------------------
10789 -- Subtype_Conformant --
10790 ------------------------
10792 function Subtype_Conformant
10793 (New_Id : Entity_Id;
10794 Old_Id : Entity_Id;
10795 Skip_Controlling_Formals : Boolean := False) return Boolean
10797 Result : Boolean;
10798 begin
10799 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10800 Skip_Controlling_Formals => Skip_Controlling_Formals);
10801 return Result;
10802 end Subtype_Conformant;
10804 ---------------------
10805 -- Type_Conformant --
10806 ---------------------
10808 function Type_Conformant
10809 (New_Id : Entity_Id;
10810 Old_Id : Entity_Id;
10811 Skip_Controlling_Formals : Boolean := False) return Boolean
10813 Result : Boolean;
10814 begin
10815 May_Hide_Profile := False;
10816 Check_Conformance
10817 (New_Id, Old_Id, Type_Conformant, False, Result,
10818 Skip_Controlling_Formals => Skip_Controlling_Formals);
10819 return Result;
10820 end Type_Conformant;
10822 -------------------------------
10823 -- Valid_Operator_Definition --
10824 -------------------------------
10826 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10827 N : Integer := 0;
10828 F : Entity_Id;
10829 Id : constant Name_Id := Chars (Designator);
10830 N_OK : Boolean;
10832 begin
10833 F := First_Formal (Designator);
10834 while Present (F) loop
10835 N := N + 1;
10837 if Present (Default_Value (F)) then
10838 Error_Msg_N
10839 ("default values not allowed for operator parameters",
10840 Parent (F));
10842 -- For function instantiations that are operators, we must check
10843 -- separately that the corresponding generic only has in-parameters.
10844 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10845 -- an error could not arise in earlier versions of the language.
10847 elsif Ekind (F) /= E_In_Parameter then
10848 Error_Msg_N ("operators can only have IN parameters", F);
10849 end if;
10851 Next_Formal (F);
10852 end loop;
10854 -- Verify that user-defined operators have proper number of arguments
10855 -- First case of operators which can only be unary
10857 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10858 N_OK := (N = 1);
10860 -- Case of operators which can be unary or binary
10862 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10863 N_OK := (N in 1 .. 2);
10865 -- All other operators can only be binary
10867 else
10868 N_OK := (N = 2);
10869 end if;
10871 if not N_OK then
10872 Error_Msg_N
10873 ("incorrect number of arguments for operator", Designator);
10874 end if;
10876 if Id = Name_Op_Ne
10877 and then Base_Type (Etype (Designator)) = Standard_Boolean
10878 and then not Is_Intrinsic_Subprogram (Designator)
10879 then
10880 Error_Msg_N
10881 ("explicit definition of inequality not allowed", Designator);
10882 end if;
10883 end Valid_Operator_Definition;
10885 end Sem_Ch6;