jit: document union types
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob18a9b0299ee80be15cc7a18f0e3b5ebd76326dac
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Inline; use Inline;
45 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Layout; use Layout;
48 with Namet; use Namet;
49 with Lib; use Lib;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Output; use Output;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch10; use Sem_Ch10;
65 with Sem_Ch12; use Sem_Ch12;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Mech; use Sem_Mech;
73 with Sem_Prag; use Sem_Prag;
74 with Sem_Res; use Sem_Res;
75 with Sem_Util; use Sem_Util;
76 with Sem_Type; use Sem_Type;
77 with Sem_Warn; use Sem_Warn;
78 with Sinput; use Sinput;
79 with Stand; use Stand;
80 with Sinfo; use Sinfo;
81 with Sinfo.CN; use Sinfo.CN;
82 with Snames; use Snames;
83 with Stringt; use Stringt;
84 with Style;
85 with Stylesw; use Stylesw;
86 with Targparm; use Targparm;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
116 (N : Node_Id;
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
126 -- outer homographs.
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
136 (New_Id : Entity_Id;
137 Old_Id : Entity_Id;
138 Ctype : Conformance_Type;
139 Errmsg : Boolean;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 -- be called.
156 procedure Check_Subprogram_Order (N : Node_Id);
157 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
158 -- the alpha ordering rule for N if this ordering requirement applicable.
160 procedure Check_Returns
161 (HSS : Node_Id;
162 Mode : Character;
163 Err : out Boolean;
164 Proc : Entity_Id := Empty);
165 -- Called to check for missing return statements in a function body, or for
166 -- returns present in a procedure body which has No_Return set. HSS is the
167 -- handled statement sequence for the subprogram body. This procedure
168 -- checks all flow paths to make sure they either have return (Mode = 'F',
169 -- used for functions) or do not have a return (Mode = 'P', used for
170 -- No_Return procedures). The flag Err is set if there are any control
171 -- paths not explicitly terminated by a return in the function case, and is
172 -- True otherwise. Proc is the entity for the procedure case and is used
173 -- in posting the warning message.
175 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
176 -- In Ada 2012, a primitive equality operator on an untagged record type
177 -- must appear before the type is frozen, and have the same visibility as
178 -- that of the type. This procedure checks that this rule is met, and
179 -- otherwise emits an error on the subprogram declaration and a warning
180 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
181 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
182 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
183 -- is set, otherwise the call has no effect.
185 procedure Enter_Overloaded_Entity (S : Entity_Id);
186 -- This procedure makes S, a new overloaded entity, into the first visible
187 -- entity with that name.
189 function Is_Non_Overriding_Operation
190 (Prev_E : Entity_Id;
191 New_E : Entity_Id) return Boolean;
192 -- Enforce the rule given in 12.3(18): a private operation in an instance
193 -- overrides an inherited operation only if the corresponding operation
194 -- was overriding in the generic. This needs to be checked for primitive
195 -- operations of types derived (in the generic unit) from formal private
196 -- or formal derived types.
198 procedure Make_Inequality_Operator (S : Entity_Id);
199 -- Create the declaration for an inequality operator that is implicitly
200 -- created by a user-defined equality operator that yields a boolean.
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ---------------------------------------------
208 -- Analyze_Abstract_Subprogram_Declaration --
209 ---------------------------------------------
211 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
212 GM : constant Ghost_Mode_Type := Ghost_Mode;
213 Scop : constant Entity_Id := Current_Scope;
214 Subp_Id : constant Entity_Id :=
215 Analyze_Subprogram_Specification (Specification (N));
217 begin
218 -- The abstract subprogram declaration may be subject to pragma Ghost
219 -- with policy Ignore. Set the mode now to ensure that any nodes
220 -- generated during analysis and expansion are properly flagged as
221 -- ignored Ghost.
223 Set_Ghost_Mode (N);
224 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
226 Generate_Definition (Subp_Id);
228 Set_Is_Abstract_Subprogram (Subp_Id);
229 New_Overloaded_Entity (Subp_Id);
230 Check_Delayed_Subprogram (Subp_Id);
232 Set_Categorization_From_Scope (Subp_Id, Scop);
234 -- An abstract subprogram declared within a Ghost region is rendered
235 -- Ghost (SPARK RM 6.9(2)).
237 if Ghost_Mode > None then
238 Set_Is_Ghost_Entity (Subp_Id);
239 end if;
241 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
242 Error_Msg_N ("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 (Subp_Id)
250 and then not Present (Overridden_Operation (Subp_Id))
251 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
252 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
253 then
254 Error_Msg_N
255 ("abstract subprogram is not dispatching or overriding?r?", N);
256 end if;
258 Generate_Reference_To_Formals (Subp_Id);
259 Check_Eliminated (Subp_Id);
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Subp_Id);
263 end if;
265 -- Restore the original Ghost mode once analysis and expansion have
266 -- taken place.
268 Ghost_Mode := GM;
269 end Analyze_Abstract_Subprogram_Declaration;
271 ---------------------------------
272 -- Analyze_Expression_Function --
273 ---------------------------------
275 procedure Analyze_Expression_Function (N : Node_Id) is
276 Expr : constant Node_Id := Expression (N);
277 Loc : constant Source_Ptr := Sloc (N);
278 LocX : constant Source_Ptr := Sloc (Expr);
279 Spec : constant Node_Id := Specification (N);
281 Def_Id : Entity_Id;
283 Prev : Entity_Id;
284 -- If the expression is a completion, Prev is the entity whose
285 -- declaration is completed. Def_Id is needed to analyze the spec.
287 New_Body : Node_Id;
288 New_Spec : Node_Id;
289 Ret : Node_Id;
291 begin
292 -- This is one of the occasions on which we transform the tree during
293 -- semantic analysis. If this is a completion, transform the expression
294 -- function into an equivalent subprogram body, and analyze it.
296 -- Expression functions are inlined unconditionally. The back-end will
297 -- determine whether this is possible.
299 Inline_Processing_Required := True;
301 -- Create a specification for the generated body. This must be done
302 -- prior to the analysis of the initial declaration.
304 New_Spec := Copy_Subprogram_Spec (Spec);
305 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
307 -- If there are previous overloadable entities with the same name,
308 -- check whether any of them is completed by the expression function.
309 -- In a generic context a formal subprogram has no completion.
311 if Present (Prev)
312 and then Is_Overloadable (Prev)
313 and then not Is_Formal_Subprogram (Prev)
314 then
315 Def_Id := Analyze_Subprogram_Specification (Spec);
316 Prev := Find_Corresponding_Spec (N);
318 -- The previous entity may be an expression function as well, in
319 -- which case the redeclaration is illegal.
321 if Present (Prev)
322 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
323 N_Expression_Function
324 then
325 Error_Msg_Sloc := Sloc (Prev);
326 Error_Msg_N ("& conflicts with declaration#", Def_Id);
327 return;
328 end if;
329 end if;
331 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
333 New_Body :=
334 Make_Subprogram_Body (Loc,
335 Specification => New_Spec,
336 Declarations => Empty_List,
337 Handled_Statement_Sequence =>
338 Make_Handled_Sequence_Of_Statements (LocX,
339 Statements => New_List (Ret)));
341 -- If the expression completes a generic subprogram, we must create a
342 -- separate node for the body, because at instantiation the original
343 -- node of the generic copy must be a generic subprogram body, and
344 -- cannot be a expression function. Otherwise we just rewrite the
345 -- expression with the non-generic body.
347 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
348 Insert_After (N, New_Body);
350 -- Propagate any aspects or pragmas that apply to the expression
351 -- function to the proper body when the expression function acts
352 -- as a completion.
354 if Has_Aspects (N) then
355 Move_Aspects (N, To => New_Body);
356 end if;
358 Relocate_Pragmas_To_Body (New_Body);
360 Rewrite (N, Make_Null_Statement (Loc));
361 Set_Has_Completion (Prev, False);
362 Analyze (N);
363 Analyze (New_Body);
364 Set_Is_Inlined (Prev);
366 -- If the expression function is a completion, the previous declaration
367 -- must come from source. We know already that appears in the current
368 -- scope. The entity itself may be internally created if within a body
369 -- to be inlined.
371 elsif Present (Prev)
372 and then Comes_From_Source (Parent (Prev))
373 and then not Is_Formal_Subprogram (Prev)
374 then
375 Set_Has_Completion (Prev, False);
377 -- An expression function that is a completion freezes the
378 -- expression. This means freezing the return type, and if it is
379 -- an access type, freezing its designated type as well.
381 -- Note that we cannot defer this freezing to the analysis of the
382 -- expression itself, because a freeze node might appear in a nested
383 -- scope, leading to an elaboration order issue in gigi.
385 Freeze_Before (N, Etype (Prev));
387 if Is_Access_Type (Etype (Prev)) then
388 Freeze_Before (N, Designated_Type (Etype (Prev)));
389 end if;
391 -- For navigation purposes, indicate that the function is a body
393 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
394 Rewrite (N, New_Body);
396 -- Correct the parent pointer of the aspect specification list to
397 -- reference the rewritten node.
399 if Has_Aspects (N) then
400 Set_Parent (Aspect_Specifications (N), N);
401 end if;
403 -- Propagate any pragmas that apply to the expression function to the
404 -- proper body when the expression function acts as a completion.
405 -- Aspects are automatically transfered because of node rewriting.
407 Relocate_Pragmas_To_Body (N);
408 Analyze (N);
410 -- Prev is the previous entity with the same name, but it is can
411 -- be an unrelated spec that is not completed by the expression
412 -- function. In that case the relevant entity is the one in the body.
413 -- Not clear that the backend can inline it in this case ???
415 if Has_Completion (Prev) then
416 Set_Is_Inlined (Prev);
418 -- The formals of the expression function are body formals,
419 -- and do not appear in the ali file, which will only contain
420 -- references to the formals of the original subprogram spec.
422 declare
423 F1 : Entity_Id;
424 F2 : Entity_Id;
426 begin
427 F1 := First_Formal (Def_Id);
428 F2 := First_Formal (Prev);
430 while Present (F1) loop
431 Set_Spec_Entity (F1, F2);
432 Next_Formal (F1);
433 Next_Formal (F2);
434 end loop;
435 end;
437 else
438 Set_Is_Inlined (Defining_Entity (New_Body));
439 end if;
441 -- If this is not a completion, create both a declaration and a body, so
442 -- that the expression can be inlined whenever possible.
444 else
445 -- An expression function that is not a completion is not a
446 -- subprogram declaration, and thus cannot appear in a protected
447 -- definition.
449 if Nkind (Parent (N)) = N_Protected_Definition then
450 Error_Msg_N
451 ("an expression function is not a legal protected operation", N);
452 end if;
454 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
456 -- Correct the parent pointer of the aspect specification list to
457 -- reference the rewritten node.
459 if Has_Aspects (N) then
460 Set_Parent (Aspect_Specifications (N), N);
461 end if;
463 Analyze (N);
465 -- Within a generic pre-analyze the original expression for name
466 -- capture. The body is also generated but plays no role in
467 -- this because it is not part of the original source.
469 if Inside_A_Generic then
470 declare
471 Id : constant Entity_Id := Defining_Entity (N);
473 begin
474 Set_Has_Completion (Id);
475 Push_Scope (Id);
476 Install_Formals (Id);
477 Preanalyze_Spec_Expression (Expr, Etype (Id));
478 End_Scope;
479 end;
480 end if;
482 Set_Is_Inlined (Defining_Entity (N));
484 -- Establish the linkages between the spec and the body. These are
485 -- used when the expression function acts as the prefix of attribute
486 -- 'Access in order to freeze the original expression which has been
487 -- moved to the generated body.
489 Set_Corresponding_Body (N, Defining_Entity (New_Body));
490 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
492 -- To prevent premature freeze action, insert the new body at the end
493 -- of the current declarations, or at the end of the package spec.
494 -- However, resolve usage names now, to prevent spurious visibility
495 -- on later entities. Note that the function can now be called in
496 -- the current declarative part, which will appear to be prior to
497 -- the presence of the body in the code. There are nevertheless no
498 -- order of elaboration issues because all name resolution has taken
499 -- place at the point of declaration.
501 declare
502 Decls : List_Id := List_Containing (N);
503 Par : constant Node_Id := Parent (Decls);
504 Id : constant Entity_Id := Defining_Entity (N);
506 begin
507 -- If this is a wrapper created for in an instance for a formal
508 -- subprogram, insert body after declaration, to be analyzed when
509 -- the enclosing instance is analyzed.
511 if GNATprove_Mode
512 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
513 then
514 Insert_After (N, New_Body);
516 else
517 if Nkind (Par) = N_Package_Specification
518 and then Decls = Visible_Declarations (Par)
519 and then Present (Private_Declarations (Par))
520 and then not Is_Empty_List (Private_Declarations (Par))
521 then
522 Decls := Private_Declarations (Par);
523 end if;
525 Insert_After (Last (Decls), New_Body);
526 Push_Scope (Id);
527 Install_Formals (Id);
529 -- Preanalyze the expression for name capture, except in an
530 -- instance, where this has been done during generic analysis,
531 -- and will be redone when analyzing the body.
533 declare
534 Expr : constant Node_Id := Expression (Ret);
536 begin
537 Set_Parent (Expr, Ret);
539 if not In_Instance then
540 Preanalyze_Spec_Expression (Expr, Etype (Id));
541 end if;
542 end;
544 End_Scope;
545 end if;
546 end;
547 end if;
549 -- If the return expression is a static constant, we suppress warning
550 -- messages on unused formals, which in most cases will be noise.
552 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
553 Is_OK_Static_Expression (Expr));
554 end Analyze_Expression_Function;
556 ----------------------------------------
557 -- Analyze_Extended_Return_Statement --
558 ----------------------------------------
560 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
561 begin
562 Check_Compiler_Unit ("extended return statement", N);
563 Analyze_Return_Statement (N);
564 end Analyze_Extended_Return_Statement;
566 ----------------------------
567 -- Analyze_Function_Call --
568 ----------------------------
570 procedure Analyze_Function_Call (N : Node_Id) is
571 Actuals : constant List_Id := Parameter_Associations (N);
572 Func_Nam : constant Node_Id := Name (N);
573 Actual : Node_Id;
575 begin
576 Analyze (Func_Nam);
578 -- A call of the form A.B (X) may be an Ada 2005 call, which is
579 -- rewritten as B (A, X). If the rewriting is successful, the call
580 -- has been analyzed and we just return.
582 if Nkind (Func_Nam) = N_Selected_Component
583 and then Name (N) /= Func_Nam
584 and then Is_Rewrite_Substitution (N)
585 and then Present (Etype (N))
586 then
587 return;
588 end if;
590 -- If error analyzing name, then set Any_Type as result type and return
592 if Etype (Func_Nam) = Any_Type then
593 Set_Etype (N, Any_Type);
594 return;
595 end if;
597 -- Otherwise analyze the parameters
599 if Present (Actuals) then
600 Actual := First (Actuals);
601 while Present (Actual) loop
602 Analyze (Actual);
603 Check_Parameterless_Call (Actual);
604 Next (Actual);
605 end loop;
606 end if;
608 Analyze_Call (N);
609 end Analyze_Function_Call;
611 -----------------------------
612 -- Analyze_Function_Return --
613 -----------------------------
615 procedure Analyze_Function_Return (N : Node_Id) is
616 Loc : constant Source_Ptr := Sloc (N);
617 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
618 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
620 R_Type : constant Entity_Id := Etype (Scope_Id);
621 -- Function result subtype
623 procedure Check_Limited_Return (Expr : Node_Id);
624 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
625 -- limited types. Used only for simple return statements.
626 -- Expr is the expression returned.
628 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
629 -- Check that the return_subtype_indication properly matches the result
630 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
632 --------------------------
633 -- Check_Limited_Return --
634 --------------------------
636 procedure Check_Limited_Return (Expr : Node_Id) is
637 begin
638 -- Ada 2005 (AI-318-02): Return-by-reference types have been
639 -- removed and replaced by anonymous access results. This is an
640 -- incompatibility with Ada 95. Not clear whether this should be
641 -- enforced yet or perhaps controllable with special switch. ???
643 -- A limited interface that is not immutably limited is OK.
645 if Is_Limited_Interface (R_Type)
646 and then
647 not (Is_Task_Interface (R_Type)
648 or else Is_Protected_Interface (R_Type)
649 or else Is_Synchronized_Interface (R_Type))
650 then
651 null;
653 elsif Is_Limited_Type (R_Type)
654 and then not Is_Interface (R_Type)
655 and then Comes_From_Source (N)
656 and then not In_Instance_Body
657 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
658 then
659 -- Error in Ada 2005
661 if Ada_Version >= Ada_2005
662 and then not Debug_Flag_Dot_L
663 and then not GNAT_Mode
664 then
665 Error_Msg_N
666 ("(Ada 2005) cannot copy object of a limited type "
667 & "(RM-2005 6.5(5.5/2))", Expr);
669 if Is_Limited_View (R_Type) then
670 Error_Msg_N
671 ("\return by reference not permitted in Ada 2005", Expr);
672 end if;
674 -- Warn in Ada 95 mode, to give folks a heads up about this
675 -- incompatibility.
677 -- In GNAT mode, this is just a warning, to allow it to be
678 -- evilly turned off. Otherwise it is a real error.
680 -- In a generic context, simplify the warning because it makes
681 -- no sense to discuss pass-by-reference or copy.
683 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
684 if Inside_A_Generic then
685 Error_Msg_N
686 ("return of limited object not permitted in Ada 2005 "
687 & "(RM-2005 6.5(5.5/2))?y?", Expr);
689 elsif Is_Limited_View (R_Type) then
690 Error_Msg_N
691 ("return by reference not permitted in Ada 2005 "
692 & "(RM-2005 6.5(5.5/2))?y?", Expr);
693 else
694 Error_Msg_N
695 ("cannot copy object of a limited type in Ada 2005 "
696 & "(RM-2005 6.5(5.5/2))?y?", Expr);
697 end if;
699 -- Ada 95 mode, compatibility warnings disabled
701 else
702 return; -- skip continuation messages below
703 end if;
705 if not Inside_A_Generic then
706 Error_Msg_N
707 ("\consider switching to return of access type", Expr);
708 Explain_Limited_Type (R_Type, Expr);
709 end if;
710 end if;
711 end Check_Limited_Return;
713 -------------------------------------
714 -- Check_Return_Subtype_Indication --
715 -------------------------------------
717 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
718 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
720 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
721 -- Subtype given in the extended return statement (must match R_Type)
723 Subtype_Ind : constant Node_Id :=
724 Object_Definition (Original_Node (Obj_Decl));
726 R_Type_Is_Anon_Access : constant Boolean :=
727 Ekind_In (R_Type,
728 E_Anonymous_Access_Subprogram_Type,
729 E_Anonymous_Access_Protected_Subprogram_Type,
730 E_Anonymous_Access_Type);
731 -- True if return type of the function is an anonymous access type
732 -- Can't we make Is_Anonymous_Access_Type in einfo ???
734 R_Stm_Type_Is_Anon_Access : constant Boolean :=
735 Ekind_In (R_Stm_Type,
736 E_Anonymous_Access_Subprogram_Type,
737 E_Anonymous_Access_Protected_Subprogram_Type,
738 E_Anonymous_Access_Type);
739 -- True if type of the return object is an anonymous access type
741 procedure Error_No_Match (N : Node_Id);
742 -- Output error messages for case where types do not statically
743 -- match. N is the location for the messages.
745 --------------------
746 -- Error_No_Match --
747 --------------------
749 procedure Error_No_Match (N : Node_Id) is
750 begin
751 Error_Msg_N
752 ("subtype must statically match function result subtype", N);
754 if not Predicates_Match (R_Stm_Type, R_Type) then
755 Error_Msg_Node_2 := R_Type;
756 Error_Msg_NE
757 ("\predicate of& does not match predicate of&",
758 N, R_Stm_Type);
759 end if;
760 end Error_No_Match;
762 -- Start of processing for Check_Return_Subtype_Indication
764 begin
765 -- First, avoid cascaded errors
767 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
768 return;
769 end if;
771 -- "return access T" case; check that the return statement also has
772 -- "access T", and that the subtypes statically match:
773 -- if this is an access to subprogram the signatures must match.
775 if R_Type_Is_Anon_Access then
776 if R_Stm_Type_Is_Anon_Access then
778 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
779 then
780 if Base_Type (Designated_Type (R_Stm_Type)) /=
781 Base_Type (Designated_Type (R_Type))
782 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
783 then
784 Error_No_Match (Subtype_Mark (Subtype_Ind));
785 end if;
787 else
788 -- For two anonymous access to subprogram types, the
789 -- types themselves must be type conformant.
791 if not Conforming_Types
792 (R_Stm_Type, R_Type, Fully_Conformant)
793 then
794 Error_No_Match (Subtype_Ind);
795 end if;
796 end if;
798 else
799 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
800 end if;
802 -- If the return object is of an anonymous access type, then report
803 -- an error if the function's result type is not also anonymous.
805 elsif R_Stm_Type_Is_Anon_Access
806 and then not R_Type_Is_Anon_Access
807 then
808 Error_Msg_N ("anonymous access not allowed for function with "
809 & "named access result", Subtype_Ind);
811 -- Subtype indication case: check that the return object's type is
812 -- covered by the result type, and that the subtypes statically match
813 -- when the result subtype is constrained. Also handle record types
814 -- with unknown discriminants for which we have built the underlying
815 -- record view. Coverage is needed to allow specific-type return
816 -- objects when the result type is class-wide (see AI05-32).
818 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
819 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
820 and then
821 Covers
822 (Base_Type (R_Type),
823 Underlying_Record_View (Base_Type (R_Stm_Type))))
824 then
825 -- A null exclusion may be present on the return type, on the
826 -- function specification, on the object declaration or on the
827 -- subtype itself.
829 if Is_Access_Type (R_Type)
830 and then
831 (Can_Never_Be_Null (R_Type)
832 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
833 Can_Never_Be_Null (R_Stm_Type)
834 then
835 Error_No_Match (Subtype_Ind);
836 end if;
838 -- AI05-103: for elementary types, subtypes must statically match
840 if Is_Constrained (R_Type)
841 or else Is_Access_Type (R_Type)
842 then
843 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
844 Error_No_Match (Subtype_Ind);
845 end if;
846 end if;
848 -- All remaining cases are illegal
850 -- Note: previous versions of this subprogram allowed the return
851 -- value to be the ancestor of the return type if the return type
852 -- was a null extension. This was plainly incorrect.
854 else
855 Error_Msg_N
856 ("wrong type for return_subtype_indication", Subtype_Ind);
857 end if;
858 end Check_Return_Subtype_Indication;
860 ---------------------
861 -- Local Variables --
862 ---------------------
864 Expr : Node_Id;
865 Obj_Decl : Node_Id;
867 -- Start of processing for Analyze_Function_Return
869 begin
870 Set_Return_Present (Scope_Id);
872 if Nkind (N) = N_Simple_Return_Statement then
873 Expr := Expression (N);
875 -- Guard against a malformed expression. The parser may have tried to
876 -- recover but the node is not analyzable.
878 if Nkind (Expr) = N_Error then
879 Set_Etype (Expr, Any_Type);
880 Expander_Mode_Save_And_Set (False);
881 return;
883 else
884 -- The resolution of a controlled [extension] aggregate associated
885 -- with a return statement creates a temporary which needs to be
886 -- finalized on function exit. Wrap the return statement inside a
887 -- block so that the finalization machinery can detect this case.
888 -- This early expansion is done only when the return statement is
889 -- not part of a handled sequence of statements.
891 if Nkind_In (Expr, N_Aggregate,
892 N_Extension_Aggregate)
893 and then Needs_Finalization (R_Type)
894 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
895 then
896 Rewrite (N,
897 Make_Block_Statement (Loc,
898 Handled_Statement_Sequence =>
899 Make_Handled_Sequence_Of_Statements (Loc,
900 Statements => New_List (Relocate_Node (N)))));
902 Analyze (N);
903 return;
904 end if;
906 Analyze (Expr);
908 -- Ada 2005 (AI-251): If the type of the returned object is
909 -- an access to an interface type then we add an implicit type
910 -- conversion to force the displacement of the "this" pointer to
911 -- reference the secondary dispatch table. We cannot delay the
912 -- generation of this implicit conversion until the expansion
913 -- because in this case the type resolution changes the decoration
914 -- of the expression node to match R_Type; by contrast, if the
915 -- returned object is a class-wide interface type then it is too
916 -- early to generate here the implicit conversion since the return
917 -- statement may be rewritten by the expander into an extended
918 -- return statement whose expansion takes care of adding the
919 -- implicit type conversion to displace the pointer to the object.
921 if Expander_Active
922 and then Serious_Errors_Detected = 0
923 and then Is_Access_Type (R_Type)
924 and then Nkind (Expr) /= N_Null
925 and then Is_Interface (Designated_Type (R_Type))
926 and then Is_Progenitor (Designated_Type (R_Type),
927 Designated_Type (Etype (Expr)))
928 then
929 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
930 Analyze (Expr);
931 end if;
933 Resolve (Expr, R_Type);
934 Check_Limited_Return (Expr);
935 end if;
937 -- RETURN only allowed in SPARK as the last statement in function
939 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
940 and then
941 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
942 or else Present (Next (N)))
943 then
944 Check_SPARK_05_Restriction
945 ("RETURN should be the last statement in function", N);
946 end if;
948 else
949 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
950 Obj_Decl := Last (Return_Object_Declarations (N));
952 -- Analyze parts specific to extended_return_statement:
954 declare
955 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
956 HSS : constant Node_Id := Handled_Statement_Sequence (N);
958 begin
959 Expr := Expression (Obj_Decl);
961 -- Note: The check for OK_For_Limited_Init will happen in
962 -- Analyze_Object_Declaration; we treat it as a normal
963 -- object declaration.
965 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
966 Analyze (Obj_Decl);
968 Check_Return_Subtype_Indication (Obj_Decl);
970 if Present (HSS) then
971 Analyze (HSS);
973 if Present (Exception_Handlers (HSS)) then
975 -- ???Has_Nested_Block_With_Handler needs to be set.
976 -- Probably by creating an actual N_Block_Statement.
977 -- Probably in Expand.
979 null;
980 end if;
981 end if;
983 -- Mark the return object as referenced, since the return is an
984 -- implicit reference of the object.
986 Set_Referenced (Defining_Identifier (Obj_Decl));
988 Check_References (Stm_Entity);
990 -- Check RM 6.5 (5.9/3)
992 if Has_Aliased then
993 if Ada_Version < Ada_2012 then
995 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
996 -- Can it really happen (extended return???)
998 Error_Msg_N
999 ("aliased only allowed for limited return objects "
1000 & "in Ada 2012??", N);
1002 elsif not Is_Limited_View (R_Type) then
1003 Error_Msg_N
1004 ("aliased only allowed for limited return objects", N);
1005 end if;
1006 end if;
1007 end;
1008 end if;
1010 -- Case of Expr present
1012 if Present (Expr)
1014 -- Defend against previous errors
1016 and then Nkind (Expr) /= N_Empty
1017 and then Present (Etype (Expr))
1018 then
1019 -- Apply constraint check. Note that this is done before the implicit
1020 -- conversion of the expression done for anonymous access types to
1021 -- ensure correct generation of the null-excluding check associated
1022 -- with null-excluding expressions found in return statements.
1024 Apply_Constraint_Check (Expr, R_Type);
1026 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1027 -- type, apply an implicit conversion of the expression to that type
1028 -- to force appropriate static and run-time accessibility checks.
1030 if Ada_Version >= Ada_2005
1031 and then Ekind (R_Type) = E_Anonymous_Access_Type
1032 then
1033 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1034 Analyze_And_Resolve (Expr, R_Type);
1036 -- If this is a local anonymous access to subprogram, the
1037 -- accessibility check can be applied statically. The return is
1038 -- illegal if the access type of the return expression is declared
1039 -- inside of the subprogram (except if it is the subtype indication
1040 -- of an extended return statement).
1042 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1043 if not Comes_From_Source (Current_Scope)
1044 or else Ekind (Current_Scope) = E_Return_Statement
1045 then
1046 null;
1048 elsif
1049 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1050 then
1051 Error_Msg_N ("cannot return local access to subprogram", N);
1052 end if;
1054 -- The expression cannot be of a formal incomplete type
1056 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1057 and then Is_Generic_Type (Etype (Expr))
1058 then
1059 Error_Msg_N
1060 ("cannot return expression of a formal incomplete type", N);
1061 end if;
1063 -- If the result type is class-wide, then check that the return
1064 -- expression's type is not declared at a deeper level than the
1065 -- function (RM05-6.5(5.6/2)).
1067 if Ada_Version >= Ada_2005
1068 and then Is_Class_Wide_Type (R_Type)
1069 then
1070 if Type_Access_Level (Etype (Expr)) >
1071 Subprogram_Access_Level (Scope_Id)
1072 then
1073 Error_Msg_N
1074 ("level of return expression type is deeper than "
1075 & "class-wide function!", Expr);
1076 end if;
1077 end if;
1079 -- Check incorrect use of dynamically tagged expression
1081 if Is_Tagged_Type (R_Type) then
1082 Check_Dynamically_Tagged_Expression
1083 (Expr => Expr,
1084 Typ => R_Type,
1085 Related_Nod => N);
1086 end if;
1088 -- ??? A real run-time accessibility check is needed in cases
1089 -- involving dereferences of access parameters. For now we just
1090 -- check the static cases.
1092 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1093 and then Is_Limited_View (Etype (Scope_Id))
1094 and then Object_Access_Level (Expr) >
1095 Subprogram_Access_Level (Scope_Id)
1096 then
1097 -- Suppress the message in a generic, where the rewriting
1098 -- is irrelevant.
1100 if Inside_A_Generic then
1101 null;
1103 else
1104 Rewrite (N,
1105 Make_Raise_Program_Error (Loc,
1106 Reason => PE_Accessibility_Check_Failed));
1107 Analyze (N);
1109 Error_Msg_Warn := SPARK_Mode /= On;
1110 Error_Msg_N ("cannot return a local value by reference<<", N);
1111 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1112 end if;
1113 end if;
1115 if Known_Null (Expr)
1116 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1117 and then Null_Exclusion_Present (Parent (Scope_Id))
1118 then
1119 Apply_Compile_Time_Constraint_Error
1120 (N => Expr,
1121 Msg => "(Ada 2005) null not allowed for "
1122 & "null-excluding return??",
1123 Reason => CE_Null_Not_Allowed);
1124 end if;
1126 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1127 -- has no initializing expression.
1129 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1130 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1131 Subprogram_Access_Level (Scope_Id)
1132 then
1133 Error_Msg_N
1134 ("level of return expression type is deeper than "
1135 & "class-wide function!", Obj_Decl);
1136 end if;
1137 end if;
1138 end Analyze_Function_Return;
1140 -------------------------------------
1141 -- Analyze_Generic_Subprogram_Body --
1142 -------------------------------------
1144 procedure Analyze_Generic_Subprogram_Body
1145 (N : Node_Id;
1146 Gen_Id : Entity_Id)
1148 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1149 Kind : constant Entity_Kind := Ekind (Gen_Id);
1150 Body_Id : Entity_Id;
1151 New_N : Node_Id;
1152 Spec : Node_Id;
1154 begin
1155 -- Copy body and disable expansion while analyzing the generic For a
1156 -- stub, do not copy the stub (which would load the proper body), this
1157 -- will be done when the proper body is analyzed.
1159 if Nkind (N) /= N_Subprogram_Body_Stub then
1160 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1161 Rewrite (N, New_N);
1163 -- Once the contents of the generic copy and the template are
1164 -- swapped, do the same for their respective aspect specifications.
1166 Exchange_Aspects (N, New_N);
1168 -- Collect all contract-related source pragmas found within the
1169 -- template and attach them to the contract of the subprogram body.
1170 -- This contract is used in the capture of global references within
1171 -- annotations.
1173 Create_Generic_Contract (N);
1175 Start_Generic;
1176 end if;
1178 Spec := Specification (N);
1180 -- Within the body of the generic, the subprogram is callable, and
1181 -- behaves like the corresponding non-generic unit.
1183 Body_Id := Defining_Entity (Spec);
1185 if Kind = E_Generic_Procedure
1186 and then Nkind (Spec) /= N_Procedure_Specification
1187 then
1188 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1189 return;
1191 elsif Kind = E_Generic_Function
1192 and then Nkind (Spec) /= N_Function_Specification
1193 then
1194 Error_Msg_N ("invalid body for generic function ", Body_Id);
1195 return;
1196 end if;
1198 Set_Corresponding_Body (Gen_Decl, Body_Id);
1200 if Has_Completion (Gen_Id)
1201 and then Nkind (Parent (N)) /= N_Subunit
1202 then
1203 Error_Msg_N ("duplicate generic body", N);
1204 return;
1205 else
1206 Set_Has_Completion (Gen_Id);
1207 end if;
1209 if Nkind (N) = N_Subprogram_Body_Stub then
1210 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1211 else
1212 Set_Corresponding_Spec (N, Gen_Id);
1213 end if;
1215 if Nkind (Parent (N)) = N_Compilation_Unit then
1216 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1217 end if;
1219 -- Make generic parameters immediately visible in the body. They are
1220 -- needed to process the formals declarations. Then make the formals
1221 -- visible in a separate step.
1223 Push_Scope (Gen_Id);
1225 declare
1226 E : Entity_Id;
1227 First_Ent : Entity_Id;
1229 begin
1230 First_Ent := First_Entity (Gen_Id);
1232 E := First_Ent;
1233 while Present (E) and then not Is_Formal (E) loop
1234 Install_Entity (E);
1235 Next_Entity (E);
1236 end loop;
1238 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1240 -- Now generic formals are visible, and the specification can be
1241 -- analyzed, for subsequent conformance check.
1243 Body_Id := Analyze_Subprogram_Specification (Spec);
1245 -- Make formal parameters visible
1247 if Present (E) then
1249 -- E is the first formal parameter, we loop through the formals
1250 -- installing them so that they will be visible.
1252 Set_First_Entity (Gen_Id, E);
1253 while Present (E) loop
1254 Install_Entity (E);
1255 Next_Formal (E);
1256 end loop;
1257 end if;
1259 -- Visible generic entity is callable within its own body
1261 Set_Ekind (Gen_Id, Ekind (Body_Id));
1262 Set_Ekind (Body_Id, E_Subprogram_Body);
1263 Set_Convention (Body_Id, Convention (Gen_Id));
1264 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1265 Set_Scope (Body_Id, Scope (Gen_Id));
1267 -- Inherit the "ghostness" of the generic spec. Note that this
1268 -- property is not directly inherited as the body may be subject
1269 -- to a different Ghost assertion policy.
1271 if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then
1272 Set_Is_Ghost_Entity (Body_Id);
1274 -- The Ghost policy in effect at the point of declaration and at
1275 -- the point of completion must match (SPARK RM 6.9(14)).
1277 Check_Ghost_Completion (Gen_Id, Body_Id);
1278 end if;
1280 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1282 if Nkind (N) = N_Subprogram_Body_Stub then
1284 -- No body to analyze, so restore state of generic unit
1286 Set_Ekind (Gen_Id, Kind);
1287 Set_Ekind (Body_Id, Kind);
1289 if Present (First_Ent) then
1290 Set_First_Entity (Gen_Id, First_Ent);
1291 end if;
1293 End_Scope;
1294 return;
1295 end if;
1297 -- If this is a compilation unit, it must be made visible explicitly,
1298 -- because the compilation of the declaration, unlike other library
1299 -- unit declarations, does not. If it is not a unit, the following
1300 -- is redundant but harmless.
1302 Set_Is_Immediately_Visible (Gen_Id);
1303 Reference_Body_Formals (Gen_Id, Body_Id);
1305 if Is_Child_Unit (Gen_Id) then
1306 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1307 end if;
1309 Set_Actual_Subtypes (N, Current_Scope);
1311 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1312 Set_SPARK_Pragma_Inherited (Body_Id, True);
1314 -- Analyze any aspect specifications that appear on the generic
1315 -- subprogram body.
1317 if Has_Aspects (N) then
1318 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1319 end if;
1321 Analyze_Declarations (Declarations (N));
1322 Check_Completion;
1324 -- When a generic subprogram body appears inside a package, its
1325 -- contract is analyzed at the end of the package body declarations.
1326 -- This is due to the delay with respect of the package contract upon
1327 -- which the body contract may depend. When the generic subprogram
1328 -- body is a compilation unit, this delay is not necessary.
1330 if Nkind (Parent (N)) = N_Compilation_Unit then
1331 Analyze_Subprogram_Body_Contract (Body_Id);
1333 -- Capture all global references in a generic subprogram body
1334 -- that acts as a compilation unit now that the contract has
1335 -- been analyzed.
1337 Save_Global_References_In_Contract
1338 (Templ => Original_Node (N),
1339 Gen_Id => Gen_Id);
1340 end if;
1342 Analyze (Handled_Statement_Sequence (N));
1343 Save_Global_References (Original_Node (N));
1345 -- Prior to exiting the scope, include generic formals again (if any
1346 -- are present) in the set of local entities.
1348 if Present (First_Ent) then
1349 Set_First_Entity (Gen_Id, First_Ent);
1350 end if;
1352 Check_References (Gen_Id);
1353 end;
1355 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1356 End_Scope;
1357 Check_Subprogram_Order (N);
1359 -- Outside of its body, unit is generic again
1361 Set_Ekind (Gen_Id, Kind);
1362 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1364 if Style_Check then
1365 Style.Check_Identifier (Body_Id, Gen_Id);
1366 end if;
1368 End_Generic;
1369 end Analyze_Generic_Subprogram_Body;
1371 ----------------------------
1372 -- Analyze_Null_Procedure --
1373 ----------------------------
1375 procedure Analyze_Null_Procedure
1376 (N : Node_Id;
1377 Is_Completion : out Boolean)
1379 Loc : constant Source_Ptr := Sloc (N);
1380 Spec : constant Node_Id := Specification (N);
1381 Designator : Entity_Id;
1382 Form : Node_Id;
1383 Null_Body : Node_Id := Empty;
1384 Prev : Entity_Id;
1386 begin
1387 -- Capture the profile of the null procedure before analysis, for
1388 -- expansion at the freeze point and at each point of call. The body is
1389 -- used if the procedure has preconditions, or if it is a completion. In
1390 -- the first case the body is analyzed at the freeze point, in the other
1391 -- it replaces the null procedure declaration.
1393 Null_Body :=
1394 Make_Subprogram_Body (Loc,
1395 Specification => New_Copy_Tree (Spec),
1396 Declarations => New_List,
1397 Handled_Statement_Sequence =>
1398 Make_Handled_Sequence_Of_Statements (Loc,
1399 Statements => New_List (Make_Null_Statement (Loc))));
1401 -- Create new entities for body and formals
1403 Set_Defining_Unit_Name (Specification (Null_Body),
1404 Make_Defining_Identifier
1405 (Sloc (Defining_Entity (N)),
1406 Chars (Defining_Entity (N))));
1408 Form := First (Parameter_Specifications (Specification (Null_Body)));
1409 while Present (Form) loop
1410 Set_Defining_Identifier (Form,
1411 Make_Defining_Identifier
1412 (Sloc (Defining_Identifier (Form)),
1413 Chars (Defining_Identifier (Form))));
1414 Next (Form);
1415 end loop;
1417 -- Determine whether the null procedure may be a completion of a generic
1418 -- suprogram, in which case we use the new null body as the completion
1419 -- and set minimal semantic information on the original declaration,
1420 -- which is rewritten as a null statement.
1422 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1424 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1425 Insert_Before (N, Null_Body);
1426 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1428 Rewrite (N, Make_Null_Statement (Loc));
1429 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1430 Is_Completion := True;
1431 return;
1433 else
1434 -- Resolve the types of the formals now, because the freeze point
1435 -- may appear in a different context, e.g. an instantiation.
1437 Form := First (Parameter_Specifications (Specification (Null_Body)));
1438 while Present (Form) loop
1439 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1440 Find_Type (Parameter_Type (Form));
1442 elsif
1443 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1444 then
1445 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1447 else
1448 -- The case of a null procedure with a formal that is an
1449 -- access_to_subprogram type, and that is used as an actual
1450 -- in an instantiation is left to the enthusiastic reader.
1452 null;
1453 end if;
1455 Next (Form);
1456 end loop;
1457 end if;
1459 -- If there are previous overloadable entities with the same name,
1460 -- check whether any of them is completed by the null procedure.
1462 if Present (Prev) and then Is_Overloadable (Prev) then
1463 Designator := Analyze_Subprogram_Specification (Spec);
1464 Prev := Find_Corresponding_Spec (N);
1465 end if;
1467 if No (Prev) or else not Comes_From_Source (Prev) then
1468 Designator := Analyze_Subprogram_Specification (Spec);
1469 Set_Has_Completion (Designator);
1471 -- Signal to caller that this is a procedure declaration
1473 Is_Completion := False;
1475 -- Null procedures are always inlined, but generic formal subprograms
1476 -- which appear as such in the internal instance of formal packages,
1477 -- need no completion and are not marked Inline.
1479 if Expander_Active
1480 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1481 then
1482 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1483 Set_Body_To_Inline (N, Null_Body);
1484 Set_Is_Inlined (Designator);
1485 end if;
1487 else
1488 -- The null procedure is a completion. We unconditionally rewrite
1489 -- this as a null body (even if expansion is not active), because
1490 -- there are various error checks that are applied on this body
1491 -- when it is analyzed (e.g. correct aspect placement).
1493 if Has_Completion (Prev) then
1494 Error_Msg_Sloc := Sloc (Prev);
1495 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1496 end if;
1498 Is_Completion := True;
1499 Rewrite (N, Null_Body);
1500 Analyze (N);
1501 end if;
1502 end Analyze_Null_Procedure;
1504 -----------------------------
1505 -- Analyze_Operator_Symbol --
1506 -----------------------------
1508 -- An operator symbol such as "+" or "and" may appear in context where the
1509 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1510 -- is just a string, as in (conjunction = "or"). In these cases the parser
1511 -- generates this node, and the semantics does the disambiguation. Other
1512 -- such case are actuals in an instantiation, the generic unit in an
1513 -- instantiation, and pragma arguments.
1515 procedure Analyze_Operator_Symbol (N : Node_Id) is
1516 Par : constant Node_Id := Parent (N);
1518 begin
1519 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1520 or else Nkind (Par) = N_Function_Instantiation
1521 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1522 or else (Nkind (Par) = N_Pragma_Argument_Association
1523 and then not Is_Pragma_String_Literal (Par))
1524 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1525 or else (Nkind (Par) = N_Attribute_Reference
1526 and then Attribute_Name (Par) /= Name_Value)
1527 then
1528 Find_Direct_Name (N);
1530 else
1531 Change_Operator_Symbol_To_String_Literal (N);
1532 Analyze (N);
1533 end if;
1534 end Analyze_Operator_Symbol;
1536 -----------------------------------
1537 -- Analyze_Parameter_Association --
1538 -----------------------------------
1540 procedure Analyze_Parameter_Association (N : Node_Id) is
1541 begin
1542 Analyze (Explicit_Actual_Parameter (N));
1543 end Analyze_Parameter_Association;
1545 ----------------------------
1546 -- Analyze_Procedure_Call --
1547 ----------------------------
1549 procedure Analyze_Procedure_Call (N : Node_Id) is
1550 GM : constant Ghost_Mode_Type := Ghost_Mode;
1552 procedure Analyze_Call_And_Resolve;
1553 -- Do Analyze and Resolve calls for procedure call
1554 -- At end, check illegal order dependence.
1556 procedure Restore_Globals;
1557 -- Restore the values of all saved global variables
1559 ------------------------------
1560 -- Analyze_Call_And_Resolve --
1561 ------------------------------
1563 procedure Analyze_Call_And_Resolve is
1564 begin
1565 if Nkind (N) = N_Procedure_Call_Statement then
1566 Analyze_Call (N);
1567 Resolve (N, Standard_Void_Type);
1568 else
1569 Analyze (N);
1570 end if;
1571 end Analyze_Call_And_Resolve;
1573 ---------------------
1574 -- Restore_Globals --
1575 ---------------------
1577 procedure Restore_Globals is
1578 begin
1579 Ghost_Mode := GM;
1580 end Restore_Globals;
1582 -- Local variables
1584 Actuals : constant List_Id := Parameter_Associations (N);
1585 Loc : constant Source_Ptr := Sloc (N);
1586 P : constant Node_Id := Name (N);
1587 Actual : Node_Id;
1588 New_N : Node_Id;
1590 -- Start of processing for Analyze_Procedure_Call
1592 begin
1593 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1594 -- a procedure call or an entry call. The prefix may denote an access
1595 -- to subprogram type, in which case an implicit dereference applies.
1596 -- If the prefix is an indexed component (without implicit dereference)
1597 -- then the construct denotes a call to a member of an entire family.
1598 -- If the prefix is a simple name, it may still denote a call to a
1599 -- parameterless member of an entry family. Resolution of these various
1600 -- interpretations is delicate.
1602 Analyze (P);
1604 -- If this is a call of the form Obj.Op, the call may have been
1605 -- analyzed and possibly rewritten into a block, in which case
1606 -- we are done.
1608 if Analyzed (N) then
1609 return;
1610 end if;
1612 -- If there is an error analyzing the name (which may have been
1613 -- rewritten if the original call was in prefix notation) then error
1614 -- has been emitted already, mark node and return.
1616 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1617 Set_Etype (N, Any_Type);
1618 return;
1619 end if;
1621 -- The name of the procedure call may reference an entity subject to
1622 -- pragma Ghost with policy Ignore. Set the mode now to ensure that any
1623 -- nodes generated during analysis and expansion are properly flagged as
1624 -- ignored Ghost.
1626 Set_Ghost_Mode (N);
1628 -- Otherwise analyze the parameters
1630 if Present (Actuals) then
1631 Actual := First (Actuals);
1633 while Present (Actual) loop
1634 Analyze (Actual);
1635 Check_Parameterless_Call (Actual);
1636 Next (Actual);
1637 end loop;
1638 end if;
1640 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1642 if Nkind (P) = N_Attribute_Reference
1643 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1644 Name_Elab_Body,
1645 Name_Elab_Subp_Body)
1646 then
1647 if Present (Actuals) then
1648 Error_Msg_N
1649 ("no parameters allowed for this call", First (Actuals));
1650 return;
1651 end if;
1653 Set_Etype (N, Standard_Void_Type);
1654 Set_Analyzed (N);
1656 elsif Is_Entity_Name (P)
1657 and then Is_Record_Type (Etype (Entity (P)))
1658 and then Remote_AST_I_Dereference (P)
1659 then
1660 Restore_Globals;
1661 return;
1663 elsif Is_Entity_Name (P)
1664 and then Ekind (Entity (P)) /= E_Entry_Family
1665 then
1666 if Is_Access_Type (Etype (P))
1667 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1668 and then No (Actuals)
1669 and then Comes_From_Source (N)
1670 then
1671 Error_Msg_N ("missing explicit dereference in call", N);
1672 end if;
1674 Analyze_Call_And_Resolve;
1676 -- If the prefix is the simple name of an entry family, this is
1677 -- a parameterless call from within the task body itself.
1679 elsif Is_Entity_Name (P)
1680 and then Nkind (P) = N_Identifier
1681 and then Ekind (Entity (P)) = E_Entry_Family
1682 and then Present (Actuals)
1683 and then No (Next (First (Actuals)))
1684 then
1685 -- Can be call to parameterless entry family. What appears to be the
1686 -- sole argument is in fact the entry index. Rewrite prefix of node
1687 -- accordingly. Source representation is unchanged by this
1688 -- transformation.
1690 New_N :=
1691 Make_Indexed_Component (Loc,
1692 Prefix =>
1693 Make_Selected_Component (Loc,
1694 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1695 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1696 Expressions => Actuals);
1697 Set_Name (N, New_N);
1698 Set_Etype (New_N, Standard_Void_Type);
1699 Set_Parameter_Associations (N, No_List);
1700 Analyze_Call_And_Resolve;
1702 elsif Nkind (P) = N_Explicit_Dereference then
1703 if Ekind (Etype (P)) = E_Subprogram_Type then
1704 Analyze_Call_And_Resolve;
1705 else
1706 Error_Msg_N ("expect access to procedure in call", P);
1707 end if;
1709 -- The name can be a selected component or an indexed component that
1710 -- yields an access to subprogram. Such a prefix is legal if the call
1711 -- has parameter associations.
1713 elsif Is_Access_Type (Etype (P))
1714 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1715 then
1716 if Present (Actuals) then
1717 Analyze_Call_And_Resolve;
1718 else
1719 Error_Msg_N ("missing explicit dereference in call ", N);
1720 end if;
1722 -- If not an access to subprogram, then the prefix must resolve to the
1723 -- name of an entry, entry family, or protected operation.
1725 -- For the case of a simple entry call, P is a selected component where
1726 -- the prefix is the task and the selector name is the entry. A call to
1727 -- a protected procedure will have the same syntax. If the protected
1728 -- object contains overloaded operations, the entity may appear as a
1729 -- function, the context will select the operation whose type is Void.
1731 elsif Nkind (P) = N_Selected_Component
1732 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1733 E_Procedure,
1734 E_Function)
1735 then
1736 Analyze_Call_And_Resolve;
1738 elsif Nkind (P) = N_Selected_Component
1739 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1740 and then Present (Actuals)
1741 and then No (Next (First (Actuals)))
1742 then
1743 -- Can be call to parameterless entry family. What appears to be the
1744 -- sole argument is in fact the entry index. Rewrite prefix of node
1745 -- accordingly. Source representation is unchanged by this
1746 -- transformation.
1748 New_N :=
1749 Make_Indexed_Component (Loc,
1750 Prefix => New_Copy (P),
1751 Expressions => Actuals);
1752 Set_Name (N, New_N);
1753 Set_Etype (New_N, Standard_Void_Type);
1754 Set_Parameter_Associations (N, No_List);
1755 Analyze_Call_And_Resolve;
1757 -- For the case of a reference to an element of an entry family, P is
1758 -- an indexed component whose prefix is a selected component (task and
1759 -- entry family), and whose index is the entry family index.
1761 elsif Nkind (P) = N_Indexed_Component
1762 and then Nkind (Prefix (P)) = N_Selected_Component
1763 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1764 then
1765 Analyze_Call_And_Resolve;
1767 -- If the prefix is the name of an entry family, it is a call from
1768 -- within the task body itself.
1770 elsif Nkind (P) = N_Indexed_Component
1771 and then Nkind (Prefix (P)) = N_Identifier
1772 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1773 then
1774 New_N :=
1775 Make_Selected_Component (Loc,
1776 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1777 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1778 Rewrite (Prefix (P), New_N);
1779 Analyze (P);
1780 Analyze_Call_And_Resolve;
1782 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1783 -- procedure name, so the construct can only be a qualified expression.
1785 elsif Nkind (P) = N_Qualified_Expression
1786 and then Ada_Version >= Ada_2012
1787 then
1788 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1789 Analyze (N);
1791 -- Anything else is an error
1793 else
1794 Error_Msg_N ("invalid procedure or entry call", N);
1795 end if;
1797 Restore_Globals;
1798 end Analyze_Procedure_Call;
1800 ------------------------------
1801 -- Analyze_Return_Statement --
1802 ------------------------------
1804 procedure Analyze_Return_Statement (N : Node_Id) is
1806 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1807 N_Extended_Return_Statement));
1809 Returns_Object : constant Boolean :=
1810 Nkind (N) = N_Extended_Return_Statement
1811 or else
1812 (Nkind (N) = N_Simple_Return_Statement
1813 and then Present (Expression (N)));
1814 -- True if we're returning something; that is, "return <expression>;"
1815 -- or "return Result : T [:= ...]". False for "return;". Used for error
1816 -- checking: If Returns_Object is True, N should apply to a function
1817 -- body; otherwise N should apply to a procedure body, entry body,
1818 -- accept statement, or extended return statement.
1820 function Find_What_It_Applies_To return Entity_Id;
1821 -- Find the entity representing the innermost enclosing body, accept
1822 -- statement, or extended return statement. If the result is a callable
1823 -- construct or extended return statement, then this will be the value
1824 -- of the Return_Applies_To attribute. Otherwise, the program is
1825 -- illegal. See RM-6.5(4/2).
1827 -----------------------------
1828 -- Find_What_It_Applies_To --
1829 -----------------------------
1831 function Find_What_It_Applies_To return Entity_Id is
1832 Result : Entity_Id := Empty;
1834 begin
1835 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1836 -- and postconditions.
1838 for J in reverse 0 .. Scope_Stack.Last loop
1839 Result := Scope_Stack.Table (J).Entity;
1840 exit when not Ekind_In (Result, E_Block, E_Loop)
1841 and then Chars (Result) /= Name_uPostconditions;
1842 end loop;
1844 pragma Assert (Present (Result));
1845 return Result;
1846 end Find_What_It_Applies_To;
1848 -- Local declarations
1850 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1851 Kind : constant Entity_Kind := Ekind (Scope_Id);
1852 Loc : constant Source_Ptr := Sloc (N);
1853 Stm_Entity : constant Entity_Id :=
1854 New_Internal_Entity
1855 (E_Return_Statement, Current_Scope, Loc, 'R');
1857 -- Start of processing for Analyze_Return_Statement
1859 begin
1860 Set_Return_Statement_Entity (N, Stm_Entity);
1862 Set_Etype (Stm_Entity, Standard_Void_Type);
1863 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1865 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1866 -- (4/2): an inner return statement will apply to this extended return.
1868 if Nkind (N) = N_Extended_Return_Statement then
1869 Push_Scope (Stm_Entity);
1870 end if;
1872 -- Check that pragma No_Return is obeyed. Don't complain about the
1873 -- implicitly-generated return that is placed at the end.
1875 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1876 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1877 end if;
1879 -- Warn on any unassigned OUT parameters if in procedure
1881 if Ekind (Scope_Id) = E_Procedure then
1882 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1883 end if;
1885 -- Check that functions return objects, and other things do not
1887 if Kind = E_Function or else Kind = E_Generic_Function then
1888 if not Returns_Object then
1889 Error_Msg_N ("missing expression in return from function", N);
1890 end if;
1892 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1893 if Returns_Object then
1894 Error_Msg_N ("procedure cannot return value (use function)", N);
1895 end if;
1897 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1898 if Returns_Object then
1899 if Is_Protected_Type (Scope (Scope_Id)) then
1900 Error_Msg_N ("entry body cannot return value", N);
1901 else
1902 Error_Msg_N ("accept statement cannot return value", N);
1903 end if;
1904 end if;
1906 elsif Kind = E_Return_Statement then
1908 -- We are nested within another return statement, which must be an
1909 -- extended_return_statement.
1911 if Returns_Object then
1912 if Nkind (N) = N_Extended_Return_Statement then
1913 Error_Msg_N
1914 ("extended return statement cannot be nested (use `RETURN;`)",
1917 -- Case of a simple return statement with a value inside extended
1918 -- return statement.
1920 else
1921 Error_Msg_N
1922 ("return nested in extended return statement cannot return "
1923 & "value (use `RETURN;`)", N);
1924 end if;
1925 end if;
1927 else
1928 Error_Msg_N ("illegal context for return statement", N);
1929 end if;
1931 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1932 Analyze_Function_Return (N);
1934 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1935 Set_Return_Present (Scope_Id);
1936 end if;
1938 if Nkind (N) = N_Extended_Return_Statement then
1939 End_Scope;
1940 end if;
1942 Kill_Current_Values (Last_Assignment_Only => True);
1943 Check_Unreachable_Code (N);
1945 Analyze_Dimension (N);
1946 end Analyze_Return_Statement;
1948 -------------------------------------
1949 -- Analyze_Simple_Return_Statement --
1950 -------------------------------------
1952 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1953 begin
1954 if Present (Expression (N)) then
1955 Mark_Coextensions (N, Expression (N));
1956 end if;
1958 Analyze_Return_Statement (N);
1959 end Analyze_Simple_Return_Statement;
1961 -------------------------
1962 -- Analyze_Return_Type --
1963 -------------------------
1965 procedure Analyze_Return_Type (N : Node_Id) is
1966 Designator : constant Entity_Id := Defining_Entity (N);
1967 Typ : Entity_Id := Empty;
1969 begin
1970 -- Normal case where result definition does not indicate an error
1972 if Result_Definition (N) /= Error then
1973 if Nkind (Result_Definition (N)) = N_Access_Definition then
1974 Check_SPARK_05_Restriction
1975 ("access result is not allowed", Result_Definition (N));
1977 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1979 declare
1980 AD : constant Node_Id :=
1981 Access_To_Subprogram_Definition (Result_Definition (N));
1982 begin
1983 if Present (AD) and then Protected_Present (AD) then
1984 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1985 else
1986 Typ := Access_Definition (N, Result_Definition (N));
1987 end if;
1988 end;
1990 Set_Parent (Typ, Result_Definition (N));
1991 Set_Is_Local_Anonymous_Access (Typ);
1992 Set_Etype (Designator, Typ);
1994 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1996 Null_Exclusion_Static_Checks (N);
1998 -- Subtype_Mark case
2000 else
2001 Find_Type (Result_Definition (N));
2002 Typ := Entity (Result_Definition (N));
2003 Set_Etype (Designator, Typ);
2005 -- Unconstrained array as result is not allowed in SPARK
2007 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2008 Check_SPARK_05_Restriction
2009 ("returning an unconstrained array is not allowed",
2010 Result_Definition (N));
2011 end if;
2013 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2015 Null_Exclusion_Static_Checks (N);
2017 -- If a null exclusion is imposed on the result type, then create
2018 -- a null-excluding itype (an access subtype) and use it as the
2019 -- function's Etype. Note that the null exclusion checks are done
2020 -- right before this, because they don't get applied to types that
2021 -- do not come from source.
2023 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2024 Set_Etype (Designator,
2025 Create_Null_Excluding_Itype
2026 (T => Typ,
2027 Related_Nod => N,
2028 Scope_Id => Scope (Current_Scope)));
2030 -- The new subtype must be elaborated before use because
2031 -- it is visible outside of the function. However its base
2032 -- type may not be frozen yet, so the reference that will
2033 -- force elaboration must be attached to the freezing of
2034 -- the base type.
2036 -- If the return specification appears on a proper body,
2037 -- the subtype will have been created already on the spec.
2039 if Is_Frozen (Typ) then
2040 if Nkind (Parent (N)) = N_Subprogram_Body
2041 and then Nkind (Parent (Parent (N))) = N_Subunit
2042 then
2043 null;
2044 else
2045 Build_Itype_Reference (Etype (Designator), Parent (N));
2046 end if;
2048 else
2049 Ensure_Freeze_Node (Typ);
2051 declare
2052 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2053 begin
2054 Set_Itype (IR, Etype (Designator));
2055 Append_Freeze_Actions (Typ, New_List (IR));
2056 end;
2057 end if;
2059 else
2060 Set_Etype (Designator, Typ);
2061 end if;
2063 if Ekind (Typ) = E_Incomplete_Type
2064 and then Is_Value_Type (Typ)
2065 then
2066 null;
2068 elsif Ekind (Typ) = E_Incomplete_Type
2069 or else (Is_Class_Wide_Type (Typ)
2070 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2071 then
2072 -- AI05-0151: Tagged incomplete types are allowed in all formal
2073 -- parts. Untagged incomplete types are not allowed in bodies.
2074 -- As a consequence, limited views cannot appear in a basic
2075 -- declaration that is itself within a body, because there is
2076 -- no point at which the non-limited view will become visible.
2078 if Ada_Version >= Ada_2012 then
2079 if From_Limited_With (Typ) and then In_Package_Body then
2080 Error_Msg_NE
2081 ("invalid use of incomplete type&",
2082 Result_Definition (N), Typ);
2084 -- The return type of a subprogram body cannot be of a
2085 -- formal incomplete type.
2087 elsif Is_Generic_Type (Typ)
2088 and then Nkind (Parent (N)) = N_Subprogram_Body
2089 then
2090 Error_Msg_N
2091 ("return type cannot be a formal incomplete type",
2092 Result_Definition (N));
2094 elsif Is_Class_Wide_Type (Typ)
2095 and then Is_Generic_Type (Root_Type (Typ))
2096 and then Nkind (Parent (N)) = N_Subprogram_Body
2097 then
2098 Error_Msg_N
2099 ("return type cannot be a formal incomplete type",
2100 Result_Definition (N));
2102 elsif Is_Tagged_Type (Typ) then
2103 null;
2105 -- Use is legal in a thunk generated for an operation
2106 -- inherited from a progenitor.
2108 elsif Is_Thunk (Designator)
2109 and then Present (Non_Limited_View (Typ))
2110 then
2111 null;
2113 elsif Nkind (Parent (N)) = N_Subprogram_Body
2114 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2115 N_Entry_Body)
2116 then
2117 Error_Msg_NE
2118 ("invalid use of untagged incomplete type&",
2119 Designator, Typ);
2120 end if;
2122 -- The type must be completed in the current package. This
2123 -- is checked at the end of the package declaration when
2124 -- Taft-amendment types are identified. If the return type
2125 -- is class-wide, there is no required check, the type can
2126 -- be a bona fide TAT.
2128 if Ekind (Scope (Current_Scope)) = E_Package
2129 and then In_Private_Part (Scope (Current_Scope))
2130 and then not Is_Class_Wide_Type (Typ)
2131 then
2132 Append_Elmt (Designator, Private_Dependents (Typ));
2133 end if;
2135 else
2136 Error_Msg_NE
2137 ("invalid use of incomplete type&", Designator, Typ);
2138 end if;
2139 end if;
2140 end if;
2142 -- Case where result definition does indicate an error
2144 else
2145 Set_Etype (Designator, Any_Type);
2146 end if;
2147 end Analyze_Return_Type;
2149 -----------------------------
2150 -- Analyze_Subprogram_Body --
2151 -----------------------------
2153 procedure Analyze_Subprogram_Body (N : Node_Id) is
2154 Loc : constant Source_Ptr := Sloc (N);
2155 Body_Spec : constant Node_Id := Specification (N);
2156 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2158 begin
2159 if Debug_Flag_C then
2160 Write_Str ("==> subprogram body ");
2161 Write_Name (Chars (Body_Id));
2162 Write_Str (" from ");
2163 Write_Location (Loc);
2164 Write_Eol;
2165 Indent;
2166 end if;
2168 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2170 -- The real work is split out into the helper, so it can do "return;"
2171 -- without skipping the debug output:
2173 Analyze_Subprogram_Body_Helper (N);
2175 if Debug_Flag_C then
2176 Outdent;
2177 Write_Str ("<== subprogram body ");
2178 Write_Name (Chars (Body_Id));
2179 Write_Str (" from ");
2180 Write_Location (Loc);
2181 Write_Eol;
2182 end if;
2183 end Analyze_Subprogram_Body;
2185 --------------------------------------
2186 -- Analyze_Subprogram_Body_Contract --
2187 --------------------------------------
2189 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2190 Items : constant Node_Id := Contract (Body_Id);
2191 Mode : SPARK_Mode_Type;
2192 Prag : Node_Id;
2193 Prag_Nam : Name_Id;
2194 Ref_Depends : Node_Id := Empty;
2195 Ref_Global : Node_Id := Empty;
2197 begin
2198 -- When a subprogram body declaration is illegal, its defining entity is
2199 -- left unanalyzed. There is nothing left to do in this case because the
2200 -- body lacks a contract, or even a proper Ekind.
2202 if Ekind (Body_Id) = E_Void then
2203 return;
2204 end if;
2206 -- Due to the timing of contract analysis, delayed pragmas may be
2207 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2208 -- context. To remedy this, restore the original SPARK_Mode of the
2209 -- related subprogram body.
2211 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2213 -- All subprograms carry a contract, but for some it is not significant
2214 -- and should not be processed.
2216 if not Has_Significant_Contract (Body_Id) then
2217 null;
2219 -- The subprogram body is a completion, analyze all delayed pragmas that
2220 -- apply. Note that when the body is stand alone, the pragmas are always
2221 -- analyzed on the spot.
2223 elsif Present (Items) then
2225 -- Locate and store pragmas Refined_Depends and Refined_Global since
2226 -- their order of analysis matters.
2228 Prag := Classifications (Items);
2229 while Present (Prag) loop
2230 Prag_Nam := Pragma_Name (Prag);
2232 if Prag_Nam = Name_Refined_Depends then
2233 Ref_Depends := Prag;
2235 elsif Prag_Nam = Name_Refined_Global then
2236 Ref_Global := Prag;
2237 end if;
2239 Prag := Next_Pragma (Prag);
2240 end loop;
2242 -- Analyze Refined_Global first as Refined_Depends may mention items
2243 -- classified in the global refinement.
2245 if Present (Ref_Global) then
2246 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2247 end if;
2249 -- Refined_Depends must be analyzed after Refined_Global in order to
2250 -- see the modes of all global refinements.
2252 if Present (Ref_Depends) then
2253 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2254 end if;
2255 end if;
2257 -- Ensure that the contract cases or postconditions mention 'Result or
2258 -- define a post-state.
2260 Check_Result_And_Post_State (Body_Id);
2262 -- Restore the SPARK_Mode of the enclosing context after all delayed
2263 -- pragmas have been analyzed.
2265 Restore_SPARK_Mode (Mode);
2266 end Analyze_Subprogram_Body_Contract;
2268 ------------------------------------
2269 -- Analyze_Subprogram_Body_Helper --
2270 ------------------------------------
2272 -- This procedure is called for regular subprogram bodies, generic bodies,
2273 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2274 -- specification matters, and is used to create a proper declaration for
2275 -- the subprogram, or to perform conformance checks.
2277 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2278 GM : constant Ghost_Mode_Type := Ghost_Mode;
2279 Loc : constant Source_Ptr := Sloc (N);
2280 Body_Spec : Node_Id := Specification (N);
2281 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2282 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2283 Conformant : Boolean;
2284 HSS : Node_Id;
2285 Prot_Typ : Entity_Id := Empty;
2286 Spec_Id : Entity_Id;
2287 Spec_Decl : Node_Id := Empty;
2289 Last_Real_Spec_Entity : Entity_Id := Empty;
2290 -- When we analyze a separate spec, the entity chain ends up containing
2291 -- the formals, as well as any itypes generated during analysis of the
2292 -- default expressions for parameters, or the arguments of associated
2293 -- precondition/postcondition pragmas (which are analyzed in the context
2294 -- of the spec since they have visibility on formals).
2296 -- These entities belong with the spec and not the body. However we do
2297 -- the analysis of the body in the context of the spec (again to obtain
2298 -- visibility to the formals), and all the entities generated during
2299 -- this analysis end up also chained to the entity chain of the spec.
2300 -- But they really belong to the body, and there is circuitry to move
2301 -- them from the spec to the body.
2303 -- However, when we do this move, we don't want to move the real spec
2304 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2305 -- variable points to the last real spec entity, so we only move those
2306 -- chained beyond that point. It is initialized to Empty to deal with
2307 -- the case where there is no separate spec.
2309 function Body_Has_Contract return Boolean;
2310 -- Check whether unanalyzed body has an aspect or pragma that may
2311 -- generate a SPARK contract.
2313 procedure Build_Subprogram_Declaration;
2314 -- Create a matching subprogram declaration for subprogram body N
2316 procedure Check_Anonymous_Return;
2317 -- Ada 2005: if a function returns an access type that denotes a task,
2318 -- or a type that contains tasks, we must create a master entity for
2319 -- the anonymous type, which typically will be used in an allocator
2320 -- in the body of the function.
2322 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2323 -- Look ahead to recognize a pragma that may appear after the body.
2324 -- If there is a previous spec, check that it appears in the same
2325 -- declarative part. If the pragma is Inline_Always, perform inlining
2326 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2327 -- If the body acts as a spec, and inlining is required, we create a
2328 -- subprogram declaration for it, in order to attach the body to inline.
2329 -- If pragma does not appear after the body, check whether there is
2330 -- an inline pragma before any local declarations.
2332 procedure Check_Missing_Return;
2333 -- Checks for a function with a no return statements, and also performs
2334 -- the warning checks implemented by Check_Returns. In formal mode, also
2335 -- verify that a function ends with a RETURN and that a procedure does
2336 -- not contain any RETURN.
2338 function Disambiguate_Spec return Entity_Id;
2339 -- When a primitive is declared between the private view and the full
2340 -- view of a concurrent type which implements an interface, a special
2341 -- mechanism is used to find the corresponding spec of the primitive
2342 -- body.
2344 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2345 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2346 -- incomplete types coming from a limited context and swap their limited
2347 -- views with the non-limited ones.
2349 function Is_Private_Concurrent_Primitive
2350 (Subp_Id : Entity_Id) return Boolean;
2351 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2352 -- type that implements an interface and has a private view.
2354 procedure Restore_Globals;
2355 -- Restore the values of all saved global variables
2357 procedure Set_Trivial_Subprogram (N : Node_Id);
2358 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2359 -- subprogram whose body is being analyzed. N is the statement node
2360 -- causing the flag to be set, if the following statement is a return
2361 -- of an entity, we mark the entity as set in source to suppress any
2362 -- warning on the stylized use of function stubs with a dummy return.
2364 procedure Verify_Overriding_Indicator;
2365 -- If there was a previous spec, the entity has been entered in the
2366 -- current scope previously. If the body itself carries an overriding
2367 -- indicator, check that it is consistent with the known status of the
2368 -- entity.
2370 -----------------------
2371 -- Body_Has_Contract --
2372 -----------------------
2374 function Body_Has_Contract return Boolean is
2375 Decls : constant List_Id := Declarations (N);
2376 Item : Node_Id;
2378 begin
2379 -- Check for unanalyzed aspects in the body that will generate a
2380 -- contract.
2382 if Present (Aspect_Specifications (N)) then
2383 Item := First (Aspect_Specifications (N));
2384 while Present (Item) loop
2385 if Is_Contract_Annotation (Item) then
2386 return True;
2387 end if;
2389 Next (Item);
2390 end loop;
2391 end if;
2393 -- Check for pragmas that may generate a contract
2395 if Present (Decls) then
2396 Item := First (Decls);
2397 while Present (Item) loop
2398 if Nkind (Item) = N_Pragma
2399 and then Is_Contract_Annotation (Item)
2400 then
2401 return True;
2402 end if;
2404 Next (Item);
2405 end loop;
2406 end if;
2408 return False;
2409 end Body_Has_Contract;
2411 ----------------------------------
2412 -- Build_Subprogram_Declaration --
2413 ----------------------------------
2415 procedure Build_Subprogram_Declaration is
2416 Asp : Node_Id;
2417 Decl : Node_Id;
2418 Subp_Decl : Node_Id;
2420 begin
2421 -- Create a matching subprogram spec using the profile of the body.
2422 -- The structure of the tree is identical, but has new entities for
2423 -- the defining unit name and formal parameters.
2425 Subp_Decl :=
2426 Make_Subprogram_Declaration (Loc,
2427 Specification => Copy_Subprogram_Spec (Body_Spec));
2429 -- Relocate the aspects of the subprogram body to the new subprogram
2430 -- spec because it acts as the initial declaration.
2431 -- ??? what about pragmas
2433 Move_Aspects (N, To => Subp_Decl);
2434 Insert_Before_And_Analyze (N, Subp_Decl);
2436 -- The analysis of the subprogram spec aspects may introduce pragmas
2437 -- that need to be analyzed.
2439 Decl := Next (Subp_Decl);
2440 while Present (Decl) loop
2442 -- Stop the search for pragmas once the body has been reached as
2443 -- this terminates the region where pragmas may appear.
2445 if Decl = N then
2446 exit;
2448 elsif Nkind (Decl) = N_Pragma then
2449 Analyze (Decl);
2450 end if;
2452 Next (Decl);
2453 end loop;
2455 Spec_Id := Defining_Entity (Subp_Decl);
2456 Set_Corresponding_Spec (N, Spec_Id);
2458 -- Mark the generated spec as a source construct to ensure that all
2459 -- calls to it are properly registered in ALI files for GNATprove.
2461 Set_Comes_From_Source (Spec_Id, True);
2463 -- If aspect SPARK_Mode was specified on the body, it needs to be
2464 -- repeated both on the generated spec and the body.
2466 Asp := Find_Aspect (Spec_Id, Aspect_SPARK_Mode);
2468 if Present (Asp) then
2469 Asp := New_Copy_Tree (Asp);
2470 Set_Analyzed (Asp, False);
2471 Set_Aspect_Specifications (N, New_List (Asp));
2472 end if;
2474 -- Ensure that the specs of the subprogram declaration and its body
2475 -- are identical, otherwise they will appear non-conformant due to
2476 -- rewritings in the default values of formal parameters.
2478 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2479 Set_Specification (N, Body_Spec);
2480 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2481 end Build_Subprogram_Declaration;
2483 ----------------------------
2484 -- Check_Anonymous_Return --
2485 ----------------------------
2487 procedure Check_Anonymous_Return is
2488 Decl : Node_Id;
2489 Par : Node_Id;
2490 Scop : Entity_Id;
2492 begin
2493 if Present (Spec_Id) then
2494 Scop := Spec_Id;
2495 else
2496 Scop := Body_Id;
2497 end if;
2499 if Ekind (Scop) = E_Function
2500 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2501 and then not Is_Thunk (Scop)
2503 -- Skip internally built functions which handle the case of
2504 -- a null access (see Expand_Interface_Conversion)
2506 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2507 and then not Comes_From_Source (Parent (Scop)))
2509 and then (Has_Task (Designated_Type (Etype (Scop)))
2510 or else
2511 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2512 and then
2513 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2514 and then Expander_Active
2516 -- Avoid cases with no tasking support
2518 and then RTE_Available (RE_Current_Master)
2519 and then not Restriction_Active (No_Task_Hierarchy)
2520 then
2521 Decl :=
2522 Make_Object_Declaration (Loc,
2523 Defining_Identifier =>
2524 Make_Defining_Identifier (Loc, Name_uMaster),
2525 Constant_Present => True,
2526 Object_Definition =>
2527 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2528 Expression =>
2529 Make_Explicit_Dereference (Loc,
2530 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2532 if Present (Declarations (N)) then
2533 Prepend (Decl, Declarations (N));
2534 else
2535 Set_Declarations (N, New_List (Decl));
2536 end if;
2538 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2539 Set_Has_Master_Entity (Scop);
2541 -- Now mark the containing scope as a task master
2543 Par := N;
2544 while Nkind (Par) /= N_Compilation_Unit loop
2545 Par := Parent (Par);
2546 pragma Assert (Present (Par));
2548 -- If we fall off the top, we are at the outer level, and
2549 -- the environment task is our effective master, so nothing
2550 -- to mark.
2552 if Nkind_In
2553 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2554 then
2555 Set_Is_Task_Master (Par, True);
2556 exit;
2557 end if;
2558 end loop;
2559 end if;
2560 end Check_Anonymous_Return;
2562 -------------------------
2563 -- Check_Inline_Pragma --
2564 -------------------------
2566 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2567 Prag : Node_Id;
2568 Plist : List_Id;
2570 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2571 -- True when N is a pragma Inline or Inline_Always that applies
2572 -- to this subprogram.
2574 -----------------------
2575 -- Is_Inline_Pragma --
2576 -----------------------
2578 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2579 begin
2580 return
2581 Nkind (N) = N_Pragma
2582 and then
2583 (Pragma_Name (N) = Name_Inline_Always
2584 or else (Front_End_Inlining
2585 and then Pragma_Name (N) = Name_Inline))
2586 and then
2587 Chars
2588 (Expression (First (Pragma_Argument_Associations (N)))) =
2589 Chars (Body_Id);
2590 end Is_Inline_Pragma;
2592 -- Start of processing for Check_Inline_Pragma
2594 begin
2595 if not Expander_Active then
2596 return;
2597 end if;
2599 if Is_List_Member (N)
2600 and then Present (Next (N))
2601 and then Is_Inline_Pragma (Next (N))
2602 then
2603 Prag := Next (N);
2605 elsif Nkind (N) /= N_Subprogram_Body_Stub
2606 and then Present (Declarations (N))
2607 and then Is_Inline_Pragma (First (Declarations (N)))
2608 then
2609 Prag := First (Declarations (N));
2611 else
2612 Prag := Empty;
2613 end if;
2615 if Present (Prag) then
2616 if Present (Spec_Id) then
2617 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2618 Analyze (Prag);
2619 end if;
2621 else
2622 -- Create a subprogram declaration, to make treatment uniform
2624 declare
2625 Subp : constant Entity_Id :=
2626 Make_Defining_Identifier (Loc, Chars (Body_Id));
2627 Decl : constant Node_Id :=
2628 Make_Subprogram_Declaration (Loc,
2629 Specification =>
2630 New_Copy_Tree (Specification (N)));
2632 begin
2633 Set_Defining_Unit_Name (Specification (Decl), Subp);
2635 if Present (First_Formal (Body_Id)) then
2636 Plist := Copy_Parameter_List (Body_Id);
2637 Set_Parameter_Specifications
2638 (Specification (Decl), Plist);
2639 end if;
2641 Insert_Before (N, Decl);
2642 Analyze (Decl);
2643 Analyze (Prag);
2644 Set_Has_Pragma_Inline (Subp);
2646 if Pragma_Name (Prag) = Name_Inline_Always then
2647 Set_Is_Inlined (Subp);
2648 Set_Has_Pragma_Inline_Always (Subp);
2649 end if;
2651 -- Prior to copying the subprogram body to create a template
2652 -- for it for subsequent inlining, remove the pragma from
2653 -- the current body so that the copy that will produce the
2654 -- new body will start from a completely unanalyzed tree.
2656 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2657 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2658 end if;
2660 Spec := Subp;
2661 end;
2662 end if;
2663 end if;
2664 end Check_Inline_Pragma;
2666 --------------------------
2667 -- Check_Missing_Return --
2668 --------------------------
2670 procedure Check_Missing_Return is
2671 Id : Entity_Id;
2672 Missing_Ret : Boolean;
2674 begin
2675 if Nkind (Body_Spec) = N_Function_Specification then
2676 if Present (Spec_Id) then
2677 Id := Spec_Id;
2678 else
2679 Id := Body_Id;
2680 end if;
2682 if Return_Present (Id) then
2683 Check_Returns (HSS, 'F', Missing_Ret);
2685 if Missing_Ret then
2686 Set_Has_Missing_Return (Id);
2687 end if;
2689 elsif Is_Generic_Subprogram (Id)
2690 or else not Is_Machine_Code_Subprogram (Id)
2691 then
2692 Error_Msg_N ("missing RETURN statement in function body", N);
2693 end if;
2695 -- If procedure with No_Return, check returns
2697 elsif Nkind (Body_Spec) = N_Procedure_Specification
2698 and then Present (Spec_Id)
2699 and then No_Return (Spec_Id)
2700 then
2701 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2702 end if;
2704 -- Special checks in SPARK mode
2706 if Nkind (Body_Spec) = N_Function_Specification then
2708 -- In SPARK mode, last statement of a function should be a return
2710 declare
2711 Stat : constant Node_Id := Last_Source_Statement (HSS);
2712 begin
2713 if Present (Stat)
2714 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2715 N_Extended_Return_Statement)
2716 then
2717 Check_SPARK_05_Restriction
2718 ("last statement in function should be RETURN", Stat);
2719 end if;
2720 end;
2722 -- In SPARK mode, verify that a procedure has no return
2724 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2725 if Present (Spec_Id) then
2726 Id := Spec_Id;
2727 else
2728 Id := Body_Id;
2729 end if;
2731 -- Would be nice to point to return statement here, can we
2732 -- borrow the Check_Returns procedure here ???
2734 if Return_Present (Id) then
2735 Check_SPARK_05_Restriction
2736 ("procedure should not have RETURN", N);
2737 end if;
2738 end if;
2739 end Check_Missing_Return;
2741 -----------------------
2742 -- Disambiguate_Spec --
2743 -----------------------
2745 function Disambiguate_Spec return Entity_Id is
2746 Priv_Spec : Entity_Id;
2747 Spec_N : Entity_Id;
2749 procedure Replace_Types (To_Corresponding : Boolean);
2750 -- Depending on the flag, replace the type of formal parameters of
2751 -- Body_Id if it is a concurrent type implementing interfaces with
2752 -- the corresponding record type or the other way around.
2754 procedure Replace_Types (To_Corresponding : Boolean) is
2755 Formal : Entity_Id;
2756 Formal_Typ : Entity_Id;
2758 begin
2759 Formal := First_Formal (Body_Id);
2760 while Present (Formal) loop
2761 Formal_Typ := Etype (Formal);
2763 if Is_Class_Wide_Type (Formal_Typ) then
2764 Formal_Typ := Root_Type (Formal_Typ);
2765 end if;
2767 -- From concurrent type to corresponding record
2769 if To_Corresponding then
2770 if Is_Concurrent_Type (Formal_Typ)
2771 and then Present (Corresponding_Record_Type (Formal_Typ))
2772 and then
2773 Present (Interfaces
2774 (Corresponding_Record_Type (Formal_Typ)))
2775 then
2776 Set_Etype (Formal,
2777 Corresponding_Record_Type (Formal_Typ));
2778 end if;
2780 -- From corresponding record to concurrent type
2782 else
2783 if Is_Concurrent_Record_Type (Formal_Typ)
2784 and then Present (Interfaces (Formal_Typ))
2785 then
2786 Set_Etype (Formal,
2787 Corresponding_Concurrent_Type (Formal_Typ));
2788 end if;
2789 end if;
2791 Next_Formal (Formal);
2792 end loop;
2793 end Replace_Types;
2795 -- Start of processing for Disambiguate_Spec
2797 begin
2798 -- Try to retrieve the specification of the body as is. All error
2799 -- messages are suppressed because the body may not have a spec in
2800 -- its current state.
2802 Spec_N := Find_Corresponding_Spec (N, False);
2804 -- It is possible that this is the body of a primitive declared
2805 -- between a private and a full view of a concurrent type. The
2806 -- controlling parameter of the spec carries the concurrent type,
2807 -- not the corresponding record type as transformed by Analyze_
2808 -- Subprogram_Specification. In such cases, we undo the change
2809 -- made by the analysis of the specification and try to find the
2810 -- spec again.
2812 -- Note that wrappers already have their corresponding specs and
2813 -- bodies set during their creation, so if the candidate spec is
2814 -- a wrapper, then we definitely need to swap all types to their
2815 -- original concurrent status.
2817 if No (Spec_N)
2818 or else Is_Primitive_Wrapper (Spec_N)
2819 then
2820 -- Restore all references of corresponding record types to the
2821 -- original concurrent types.
2823 Replace_Types (To_Corresponding => False);
2824 Priv_Spec := Find_Corresponding_Spec (N, False);
2826 -- The current body truly belongs to a primitive declared between
2827 -- a private and a full view. We leave the modified body as is,
2828 -- and return the true spec.
2830 if Present (Priv_Spec)
2831 and then Is_Private_Primitive (Priv_Spec)
2832 then
2833 return Priv_Spec;
2834 end if;
2836 -- In case that this is some sort of error, restore the original
2837 -- state of the body.
2839 Replace_Types (To_Corresponding => True);
2840 end if;
2842 return Spec_N;
2843 end Disambiguate_Spec;
2845 ----------------------------
2846 -- Exchange_Limited_Views --
2847 ----------------------------
2849 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2850 procedure Detect_And_Exchange (Id : Entity_Id);
2851 -- Determine whether Id's type denotes an incomplete type associated
2852 -- with a limited with clause and exchange the limited view with the
2853 -- non-limited one when available.
2855 -------------------------
2856 -- Detect_And_Exchange --
2857 -------------------------
2859 procedure Detect_And_Exchange (Id : Entity_Id) is
2860 Typ : constant Entity_Id := Etype (Id);
2861 begin
2862 if From_Limited_With (Typ) and then Has_Non_Limited_View (Typ) then
2863 Set_Etype (Id, Non_Limited_View (Typ));
2864 end if;
2865 end Detect_And_Exchange;
2867 -- Local variables
2869 Formal : Entity_Id;
2871 -- Start of processing for Exchange_Limited_Views
2873 begin
2874 if No (Subp_Id) then
2875 return;
2877 -- Do not process subprogram bodies as they already use the non-
2878 -- limited view of types.
2880 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2881 return;
2882 end if;
2884 -- Examine all formals and swap views when applicable
2886 Formal := First_Formal (Subp_Id);
2887 while Present (Formal) loop
2888 Detect_And_Exchange (Formal);
2890 Next_Formal (Formal);
2891 end loop;
2893 -- Process the return type of a function
2895 if Ekind (Subp_Id) = E_Function then
2896 Detect_And_Exchange (Subp_Id);
2897 end if;
2898 end Exchange_Limited_Views;
2900 -------------------------------------
2901 -- Is_Private_Concurrent_Primitive --
2902 -------------------------------------
2904 function Is_Private_Concurrent_Primitive
2905 (Subp_Id : Entity_Id) return Boolean
2907 Formal_Typ : Entity_Id;
2909 begin
2910 if Present (First_Formal (Subp_Id)) then
2911 Formal_Typ := Etype (First_Formal (Subp_Id));
2913 if Is_Concurrent_Record_Type (Formal_Typ) then
2914 if Is_Class_Wide_Type (Formal_Typ) then
2915 Formal_Typ := Root_Type (Formal_Typ);
2916 end if;
2918 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2919 end if;
2921 -- The type of the first formal is a concurrent tagged type with
2922 -- a private view.
2924 return
2925 Is_Concurrent_Type (Formal_Typ)
2926 and then Is_Tagged_Type (Formal_Typ)
2927 and then Has_Private_Declaration (Formal_Typ);
2928 end if;
2930 return False;
2931 end Is_Private_Concurrent_Primitive;
2933 ---------------------
2934 -- Restore_Globals --
2935 ---------------------
2937 procedure Restore_Globals is
2938 begin
2939 Ghost_Mode := GM;
2940 end Restore_Globals;
2942 ----------------------------
2943 -- Set_Trivial_Subprogram --
2944 ----------------------------
2946 procedure Set_Trivial_Subprogram (N : Node_Id) is
2947 Nxt : constant Node_Id := Next (N);
2949 begin
2950 Set_Is_Trivial_Subprogram (Body_Id);
2952 if Present (Spec_Id) then
2953 Set_Is_Trivial_Subprogram (Spec_Id);
2954 end if;
2956 if Present (Nxt)
2957 and then Nkind (Nxt) = N_Simple_Return_Statement
2958 and then No (Next (Nxt))
2959 and then Present (Expression (Nxt))
2960 and then Is_Entity_Name (Expression (Nxt))
2961 then
2962 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2963 end if;
2964 end Set_Trivial_Subprogram;
2966 ---------------------------------
2967 -- Verify_Overriding_Indicator --
2968 ---------------------------------
2970 procedure Verify_Overriding_Indicator is
2971 begin
2972 if Must_Override (Body_Spec) then
2973 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2974 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2975 then
2976 null;
2978 elsif not Present (Overridden_Operation (Spec_Id)) then
2979 Error_Msg_NE
2980 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2982 -- Overriding indicators aren't allowed for protected subprogram
2983 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2984 -- this to a warning if -gnatd.E is enabled.
2986 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2987 Error_Msg_Warn := Error_To_Warning;
2988 Error_Msg_N
2989 ("<<overriding indicator not allowed for protected "
2990 & "subprogram body", Body_Spec);
2991 end if;
2993 elsif Must_Not_Override (Body_Spec) then
2994 if Present (Overridden_Operation (Spec_Id)) then
2995 Error_Msg_NE
2996 ("subprogram& overrides inherited operation",
2997 Body_Spec, Spec_Id);
2999 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3000 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3001 then
3002 Error_Msg_NE
3003 ("subprogram& overrides predefined operator ",
3004 Body_Spec, Spec_Id);
3006 -- Overriding indicators aren't allowed for protected subprogram
3007 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3008 -- this to a warning if -gnatd.E is enabled.
3010 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3011 Error_Msg_Warn := Error_To_Warning;
3013 Error_Msg_N
3014 ("<<overriding indicator not allowed "
3015 & "for protected subprogram body", Body_Spec);
3017 -- If this is not a primitive operation, then the overriding
3018 -- indicator is altogether illegal.
3020 elsif not Is_Primitive (Spec_Id) then
3021 Error_Msg_N
3022 ("overriding indicator only allowed "
3023 & "if subprogram is primitive", Body_Spec);
3024 end if;
3026 -- If checking the style rule and the operation overrides, then
3027 -- issue a warning about a missing overriding_indicator. Protected
3028 -- subprogram bodies are excluded from this style checking, since
3029 -- they aren't primitives (even though their declarations can
3030 -- override) and aren't allowed to have an overriding_indicator.
3032 elsif Style_Check
3033 and then Present (Overridden_Operation (Spec_Id))
3034 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3035 then
3036 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3037 Style.Missing_Overriding (N, Body_Id);
3039 elsif Style_Check
3040 and then Can_Override_Operator (Spec_Id)
3041 and then not Is_Predefined_File_Name
3042 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3043 then
3044 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3045 Style.Missing_Overriding (N, Body_Id);
3046 end if;
3047 end Verify_Overriding_Indicator;
3049 -- Start of processing for Analyze_Subprogram_Body_Helper
3051 begin
3052 -- Generic subprograms are handled separately. They always have a
3053 -- generic specification. Determine whether current scope has a
3054 -- previous declaration.
3056 -- If the subprogram body is defined within an instance of the same
3057 -- name, the instance appears as a package renaming, and will be hidden
3058 -- within the subprogram.
3060 if Present (Prev_Id)
3061 and then not Is_Overloadable (Prev_Id)
3062 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3063 or else Comes_From_Source (Prev_Id))
3064 then
3065 if Is_Generic_Subprogram (Prev_Id) then
3066 Spec_Id := Prev_Id;
3068 -- The corresponding spec may be subject to pragma Ghost with
3069 -- policy Ignore. Set the mode now to ensure that any nodes
3070 -- generated during analysis and expansion are properly flagged
3071 -- as ignored Ghost.
3073 Set_Ghost_Mode (N, Spec_Id);
3074 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3075 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3077 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3079 if Nkind (N) = N_Subprogram_Body then
3080 HSS := Handled_Statement_Sequence (N);
3081 Check_Missing_Return;
3082 end if;
3084 Restore_Globals;
3085 return;
3087 else
3088 -- Previous entity conflicts with subprogram name. Attempting to
3089 -- enter name will post error.
3091 Enter_Name (Body_Id);
3092 Restore_Globals;
3093 return;
3094 end if;
3096 -- Non-generic case, find the subprogram declaration, if one was seen,
3097 -- or enter new overloaded entity in the current scope. If the
3098 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3099 -- part of the context of one of its subunits. No need to redo the
3100 -- analysis.
3102 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3103 Restore_Globals;
3104 return;
3106 else
3107 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3109 if Nkind (N) = N_Subprogram_Body_Stub
3110 or else No (Corresponding_Spec (N))
3111 then
3112 if Is_Private_Concurrent_Primitive (Body_Id) then
3113 Spec_Id := Disambiguate_Spec;
3115 -- The corresponding spec may be subject to pragma Ghost with
3116 -- policy Ignore. Set the mode now to ensure that any nodes
3117 -- generated during analysis and expansion are properly flagged
3118 -- as ignored Ghost.
3120 Set_Ghost_Mode (N, Spec_Id);
3122 else
3123 Spec_Id := Find_Corresponding_Spec (N);
3125 -- The corresponding spec may be subject to pragma Ghost with
3126 -- policy Ignore. Set the mode now to ensure that any nodes
3127 -- generated during analysis and expansion are properly flagged
3128 -- as ignored Ghost.
3130 Set_Ghost_Mode (N, Spec_Id);
3132 -- In GNATprove mode, if the body has no previous spec, create
3133 -- one so that the inlining machinery can operate properly.
3134 -- Transfer aspects, if any, to the new spec, so that they
3135 -- are legal and can be processed ahead of the body.
3136 -- We make two copies of the given spec, one for the new
3137 -- declaration, and one for the body.
3139 if No (Spec_Id) and then GNATprove_Mode
3141 -- Inlining does not apply during pre-analysis of code
3143 and then Full_Analysis
3145 -- Inlining only applies to full bodies, not stubs
3147 and then Nkind (N) /= N_Subprogram_Body_Stub
3149 -- Inlining only applies to bodies in the source code, not to
3150 -- those generated by the compiler. In particular, expression
3151 -- functions, whose body is generated by the compiler, are
3152 -- treated specially by GNATprove.
3154 and then Comes_From_Source (Body_Id)
3156 -- This cannot be done for a compilation unit, which is not
3157 -- in a context where we can insert a new spec.
3159 and then Is_List_Member (N)
3161 -- Inlining only applies to subprograms without contracts,
3162 -- as a contract is a sign that GNATprove should perform a
3163 -- modular analysis of the subprogram instead of a contextual
3164 -- analysis at each call site. The same test is performed in
3165 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3166 -- here in another form (because the contract has not
3167 -- been attached to the body) to avoid frontend errors in
3168 -- case pragmas are used instead of aspects, because the
3169 -- corresponding pragmas in the body would not be transferred
3170 -- to the spec, leading to legality errors.
3172 and then not Body_Has_Contract
3173 and then not Inside_A_Generic
3174 then
3175 Build_Subprogram_Declaration;
3176 end if;
3177 end if;
3179 -- If this is a duplicate body, no point in analyzing it
3181 if Error_Posted (N) then
3182 Restore_Globals;
3183 return;
3184 end if;
3186 -- A subprogram body should cause freezing of its own declaration,
3187 -- but if there was no previous explicit declaration, then the
3188 -- subprogram will get frozen too late (there may be code within
3189 -- the body that depends on the subprogram having been frozen,
3190 -- such as uses of extra formals), so we force it to be frozen
3191 -- here. Same holds if the body and spec are compilation units.
3192 -- Finally, if the return type is an anonymous access to protected
3193 -- subprogram, it must be frozen before the body because its
3194 -- expansion has generated an equivalent type that is used when
3195 -- elaborating the body.
3197 -- An exception in the case of Ada 2012, AI05-177: The bodies
3198 -- created for expression functions do not freeze.
3200 if No (Spec_Id)
3201 and then Nkind (Original_Node (N)) /= N_Expression_Function
3202 then
3203 Freeze_Before (N, Body_Id);
3205 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3206 Freeze_Before (N, Spec_Id);
3208 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3209 Freeze_Before (N, Etype (Body_Id));
3210 end if;
3212 else
3213 Spec_Id := Corresponding_Spec (N);
3215 -- The corresponding spec may be subject to pragma Ghost with
3216 -- policy Ignore. Set the mode now to ensure that any nodes
3217 -- generated during analysis and expansion are properly flagged
3218 -- as ignored Ghost.
3220 Set_Ghost_Mode (N, Spec_Id);
3221 end if;
3222 end if;
3224 -- Previously we scanned the body to look for nested subprograms, and
3225 -- rejected an inline directive if nested subprograms were present,
3226 -- because the back-end would generate conflicting symbols for the
3227 -- nested bodies. This is now unnecessary.
3229 -- Look ahead to recognize a pragma Inline that appears after the body
3231 Check_Inline_Pragma (Spec_Id);
3233 -- Deal with special case of a fully private operation in the body of
3234 -- the protected type. We must create a declaration for the subprogram,
3235 -- in order to attach the protected subprogram that will be used in
3236 -- internal calls. We exclude compiler generated bodies from the
3237 -- expander since the issue does not arise for those cases.
3239 if No (Spec_Id)
3240 and then Comes_From_Source (N)
3241 and then Is_Protected_Type (Current_Scope)
3242 then
3243 Spec_Id := Build_Private_Protected_Declaration (N);
3244 end if;
3246 -- If a separate spec is present, then deal with freezing issues
3248 if Present (Spec_Id) then
3249 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3250 Verify_Overriding_Indicator;
3252 -- In general, the spec will be frozen when we start analyzing the
3253 -- body. However, for internally generated operations, such as
3254 -- wrapper functions for inherited operations with controlling
3255 -- results, the spec may not have been frozen by the time we expand
3256 -- the freeze actions that include the bodies. In particular, extra
3257 -- formals for accessibility or for return-in-place may need to be
3258 -- generated. Freeze nodes, if any, are inserted before the current
3259 -- body. These freeze actions are also needed in ASIS mode to enable
3260 -- the proper back-annotations.
3262 if not Is_Frozen (Spec_Id)
3263 and then (Expander_Active or ASIS_Mode)
3264 then
3265 -- Force the generation of its freezing node to ensure proper
3266 -- management of access types in the backend.
3268 -- This is definitely needed for some cases, but it is not clear
3269 -- why, to be investigated further???
3271 Set_Has_Delayed_Freeze (Spec_Id);
3272 Freeze_Before (N, Spec_Id);
3273 end if;
3274 end if;
3276 -- Place subprogram on scope stack, and make formals visible. If there
3277 -- is a spec, the visible entity remains that of the spec.
3279 if Present (Spec_Id) then
3280 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3282 if Is_Child_Unit (Spec_Id) then
3283 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3284 end if;
3286 if Style_Check then
3287 Style.Check_Identifier (Body_Id, Spec_Id);
3288 end if;
3290 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3291 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3293 if Is_Abstract_Subprogram (Spec_Id) then
3294 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3295 Restore_Globals;
3296 return;
3298 else
3299 Set_Convention (Body_Id, Convention (Spec_Id));
3300 Set_Has_Completion (Spec_Id);
3302 -- Inherit the "ghostness" of the subprogram spec. Note that this
3303 -- property is not directly inherited as the body may be subject
3304 -- to a different Ghost assertion policy.
3306 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
3307 Set_Is_Ghost_Entity (Body_Id);
3309 -- The Ghost policy in effect at the point of declaration and
3310 -- at the point of completion must match (SPARK RM 6.9(14)).
3312 Check_Ghost_Completion (Spec_Id, Body_Id);
3313 end if;
3315 if Is_Protected_Type (Scope (Spec_Id)) then
3316 Prot_Typ := Scope (Spec_Id);
3317 end if;
3319 -- If this is a body generated for a renaming, do not check for
3320 -- full conformance. The check is redundant, because the spec of
3321 -- the body is a copy of the spec in the renaming declaration,
3322 -- and the test can lead to spurious errors on nested defaults.
3324 if Present (Spec_Decl)
3325 and then not Comes_From_Source (N)
3326 and then
3327 (Nkind (Original_Node (Spec_Decl)) =
3328 N_Subprogram_Renaming_Declaration
3329 or else (Present (Corresponding_Body (Spec_Decl))
3330 and then
3331 Nkind (Unit_Declaration_Node
3332 (Corresponding_Body (Spec_Decl))) =
3333 N_Subprogram_Renaming_Declaration))
3334 then
3335 Conformant := True;
3337 -- Conversely, the spec may have been generated for specless body
3338 -- with an inline pragma.
3340 elsif Comes_From_Source (N)
3341 and then not Comes_From_Source (Spec_Id)
3342 and then Has_Pragma_Inline (Spec_Id)
3343 then
3344 Conformant := True;
3346 else
3347 Check_Conformance
3348 (Body_Id, Spec_Id,
3349 Fully_Conformant, True, Conformant, Body_Id);
3350 end if;
3352 -- If the body is not fully conformant, we have to decide if we
3353 -- should analyze it or not. If it has a really messed up profile
3354 -- then we probably should not analyze it, since we will get too
3355 -- many bogus messages.
3357 -- Our decision is to go ahead in the non-fully conformant case
3358 -- only if it is at least mode conformant with the spec. Note
3359 -- that the call to Check_Fully_Conformant has issued the proper
3360 -- error messages to complain about the lack of conformance.
3362 if not Conformant
3363 and then not Mode_Conformant (Body_Id, Spec_Id)
3364 then
3365 Restore_Globals;
3366 return;
3367 end if;
3368 end if;
3370 if Spec_Id /= Body_Id then
3371 Reference_Body_Formals (Spec_Id, Body_Id);
3372 end if;
3374 Set_Ekind (Body_Id, E_Subprogram_Body);
3376 if Nkind (N) = N_Subprogram_Body_Stub then
3377 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3379 -- Regular body
3381 else
3382 Set_Corresponding_Spec (N, Spec_Id);
3384 -- Ada 2005 (AI-345): If the operation is a primitive operation
3385 -- of a concurrent type, the type of the first parameter has been
3386 -- replaced with the corresponding record, which is the proper
3387 -- run-time structure to use. However, within the body there may
3388 -- be uses of the formals that depend on primitive operations
3389 -- of the type (in particular calls in prefixed form) for which
3390 -- we need the original concurrent type. The operation may have
3391 -- several controlling formals, so the replacement must be done
3392 -- for all of them.
3394 if Comes_From_Source (Spec_Id)
3395 and then Present (First_Entity (Spec_Id))
3396 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3397 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3398 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3399 and then Present (Corresponding_Concurrent_Type
3400 (Etype (First_Entity (Spec_Id))))
3401 then
3402 declare
3403 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3404 Form : Entity_Id;
3406 begin
3407 Form := First_Formal (Spec_Id);
3408 while Present (Form) loop
3409 if Etype (Form) = Typ then
3410 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3411 end if;
3413 Next_Formal (Form);
3414 end loop;
3415 end;
3416 end if;
3418 -- Make the formals visible, and place subprogram on scope stack.
3419 -- This is also the point at which we set Last_Real_Spec_Entity
3420 -- to mark the entities which will not be moved to the body.
3422 Install_Formals (Spec_Id);
3423 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3425 -- Within an instance, add local renaming declarations so that
3426 -- gdb can retrieve the values of actuals more easily. This is
3427 -- only relevant if generating code (and indeed we definitely
3428 -- do not want these definitions -gnatc mode, because that would
3429 -- confuse ASIS).
3431 if Is_Generic_Instance (Spec_Id)
3432 and then Is_Wrapper_Package (Current_Scope)
3433 and then Expander_Active
3434 then
3435 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3436 end if;
3438 Push_Scope (Spec_Id);
3440 -- Make sure that the subprogram is immediately visible. For
3441 -- child units that have no separate spec this is indispensable.
3442 -- Otherwise it is safe albeit redundant.
3444 Set_Is_Immediately_Visible (Spec_Id);
3445 end if;
3447 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3448 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3449 Set_Scope (Body_Id, Scope (Spec_Id));
3451 -- Case of subprogram body with no previous spec
3453 else
3454 -- Check for style warning required
3456 if Style_Check
3458 -- Only apply check for source level subprograms for which checks
3459 -- have not been suppressed.
3461 and then Comes_From_Source (Body_Id)
3462 and then not Suppress_Style_Checks (Body_Id)
3464 -- No warnings within an instance
3466 and then not In_Instance
3468 -- No warnings for expression functions
3470 and then Nkind (Original_Node (N)) /= N_Expression_Function
3471 then
3472 Style.Body_With_No_Spec (N);
3473 end if;
3475 New_Overloaded_Entity (Body_Id);
3477 if Nkind (N) /= N_Subprogram_Body_Stub then
3478 Set_Acts_As_Spec (N);
3479 Generate_Definition (Body_Id);
3480 Generate_Reference
3481 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3482 Install_Formals (Body_Id);
3484 Push_Scope (Body_Id);
3485 end if;
3487 -- For stubs and bodies with no previous spec, generate references to
3488 -- formals.
3490 Generate_Reference_To_Formals (Body_Id);
3491 end if;
3493 -- Set SPARK_Mode from context
3495 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3496 Set_SPARK_Pragma_Inherited (Body_Id, True);
3498 -- If the return type is an anonymous access type whose designated type
3499 -- is the limited view of a class-wide type and the non-limited view is
3500 -- available, update the return type accordingly.
3502 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3503 declare
3504 Etyp : Entity_Id;
3505 Rtyp : Entity_Id;
3507 begin
3508 Rtyp := Etype (Current_Scope);
3510 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3511 Etyp := Directly_Designated_Type (Rtyp);
3513 if Is_Class_Wide_Type (Etyp)
3514 and then From_Limited_With (Etyp)
3515 then
3516 Set_Directly_Designated_Type
3517 (Etype (Current_Scope), Available_View (Etyp));
3518 end if;
3519 end if;
3520 end;
3521 end if;
3523 -- If this is the proper body of a stub, we must verify that the stub
3524 -- conforms to the body, and to the previous spec if one was present.
3525 -- We know already that the body conforms to that spec. This test is
3526 -- only required for subprograms that come from source.
3528 if Nkind (Parent (N)) = N_Subunit
3529 and then Comes_From_Source (N)
3530 and then not Error_Posted (Body_Id)
3531 and then Nkind (Corresponding_Stub (Parent (N))) =
3532 N_Subprogram_Body_Stub
3533 then
3534 declare
3535 Old_Id : constant Entity_Id :=
3536 Defining_Entity
3537 (Specification (Corresponding_Stub (Parent (N))));
3539 Conformant : Boolean := False;
3541 begin
3542 if No (Spec_Id) then
3543 Check_Fully_Conformant (Body_Id, Old_Id);
3545 else
3546 Check_Conformance
3547 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3549 if not Conformant then
3551 -- The stub was taken to be a new declaration. Indicate that
3552 -- it lacks a body.
3554 Set_Has_Completion (Old_Id, False);
3555 end if;
3556 end if;
3557 end;
3558 end if;
3560 Set_Has_Completion (Body_Id);
3561 Check_Eliminated (Body_Id);
3563 -- Analyze any aspect specifications that appear on the subprogram body
3564 -- stub. Stop the analysis now as the stub does not have a declarative
3565 -- or a statement part, and it cannot be inlined.
3567 if Nkind (N) = N_Subprogram_Body_Stub then
3568 if Has_Aspects (N) then
3569 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3570 end if;
3572 Restore_Globals;
3573 return;
3574 end if;
3576 -- Handle frontend inlining
3578 -- Note: Normally we don't do any inlining if expansion is off, since
3579 -- we won't generate code in any case. An exception arises in GNATprove
3580 -- mode where we want to expand some calls in place, even with expansion
3581 -- disabled, since the inlining eases formal verification.
3583 if not GNATprove_Mode
3584 and then Expander_Active
3585 and then Serious_Errors_Detected = 0
3586 and then Present (Spec_Id)
3587 and then Has_Pragma_Inline (Spec_Id)
3588 then
3589 -- Legacy implementation (relying on frontend inlining)
3591 if not Back_End_Inlining then
3592 if (Has_Pragma_Inline_Always (Spec_Id)
3593 and then not Opt.Disable_FE_Inline_Always)
3594 or else
3595 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3596 and then not Opt.Disable_FE_Inline)
3597 then
3598 Build_Body_To_Inline (N, Spec_Id);
3599 end if;
3601 -- New implementation (relying on backend inlining)
3603 else
3604 if Has_Pragma_Inline_Always (Spec_Id)
3605 or else Optimization_Level > 0
3606 then
3607 -- Handle function returning an unconstrained type
3609 if Comes_From_Source (Body_Id)
3610 and then Ekind (Spec_Id) = E_Function
3611 and then Returns_Unconstrained_Type (Spec_Id)
3613 -- If function builds in place, i.e. returns a limited type,
3614 -- inlining cannot be done.
3616 and then not Is_Limited_Type (Etype (Spec_Id))
3617 then
3618 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3620 else
3621 declare
3622 Subp_Body : constant Node_Id :=
3623 Unit_Declaration_Node (Body_Id);
3624 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3626 begin
3627 -- Do not pass inlining to the backend if the subprogram
3628 -- has declarations or statements which cannot be inlined
3629 -- by the backend. This check is done here to emit an
3630 -- error instead of the generic warning message reported
3631 -- by the GCC backend (ie. "function might not be
3632 -- inlinable").
3634 if Present (Subp_Decl)
3635 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3636 then
3637 null;
3639 elsif Has_Excluded_Statement
3640 (Spec_Id,
3641 Statements
3642 (Handled_Statement_Sequence (Subp_Body)))
3643 then
3644 null;
3646 -- If the backend inlining is available then at this
3647 -- stage we only have to mark the subprogram as inlined.
3648 -- The expander will take care of registering it in the
3649 -- table of subprograms inlined by the backend a part of
3650 -- processing calls to it (cf. Expand_Call)
3652 else
3653 Set_Is_Inlined (Spec_Id);
3654 end if;
3655 end;
3656 end if;
3657 end if;
3658 end if;
3660 -- In GNATprove mode, inline only when there is a separate subprogram
3661 -- declaration for now, as inlining of subprogram bodies acting as
3662 -- declarations, or subprogram stubs, are not supported by frontend
3663 -- inlining. This inlining should occur after analysis of the body, so
3664 -- that it is known whether the value of SPARK_Mode applicable to the
3665 -- body, which can be defined by a pragma inside the body.
3667 elsif GNATprove_Mode
3668 and then Full_Analysis
3669 and then not Inside_A_Generic
3670 and then Present (Spec_Id)
3671 and then
3672 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
3673 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3674 and then not Body_Has_Contract
3675 then
3676 Build_Body_To_Inline (N, Spec_Id);
3677 end if;
3679 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3680 -- of the specification we have to install the private withed units.
3681 -- This holds for child units as well.
3683 if Is_Compilation_Unit (Body_Id)
3684 or else Nkind (Parent (N)) = N_Compilation_Unit
3685 then
3686 Install_Private_With_Clauses (Body_Id);
3687 end if;
3689 Check_Anonymous_Return;
3691 -- Set the Protected_Formal field of each extra formal of the protected
3692 -- subprogram to reference the corresponding extra formal of the
3693 -- subprogram that implements it. For regular formals this occurs when
3694 -- the protected subprogram's declaration is expanded, but the extra
3695 -- formals don't get created until the subprogram is frozen. We need to
3696 -- do this before analyzing the protected subprogram's body so that any
3697 -- references to the original subprogram's extra formals will be changed
3698 -- refer to the implementing subprogram's formals (see Expand_Formal).
3700 if Present (Spec_Id)
3701 and then Is_Protected_Type (Scope (Spec_Id))
3702 and then Present (Protected_Body_Subprogram (Spec_Id))
3703 then
3704 declare
3705 Impl_Subp : constant Entity_Id :=
3706 Protected_Body_Subprogram (Spec_Id);
3707 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3708 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3709 begin
3710 while Present (Prot_Ext_Formal) loop
3711 pragma Assert (Present (Impl_Ext_Formal));
3712 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3713 Next_Formal_With_Extras (Prot_Ext_Formal);
3714 Next_Formal_With_Extras (Impl_Ext_Formal);
3715 end loop;
3716 end;
3717 end if;
3719 -- Now we can go on to analyze the body
3721 HSS := Handled_Statement_Sequence (N);
3722 Set_Actual_Subtypes (N, Current_Scope);
3724 -- Add a declaration for the Protection object, renaming declarations
3725 -- for discriminals and privals and finally a declaration for the entry
3726 -- family index (if applicable). This form of early expansion is done
3727 -- when the Expander is active because Install_Private_Data_Declarations
3728 -- references entities which were created during regular expansion. The
3729 -- subprogram entity must come from source, and not be an internally
3730 -- generated subprogram.
3732 if Expander_Active
3733 and then Present (Prot_Typ)
3734 and then Present (Spec_Id)
3735 and then Comes_From_Source (Spec_Id)
3736 and then not Is_Eliminated (Spec_Id)
3737 then
3738 Install_Private_Data_Declarations
3739 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3740 end if;
3742 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3743 -- may now appear in parameter and result profiles. Since the analysis
3744 -- of a subprogram body may use the parameter and result profile of the
3745 -- spec, swap any limited views with their non-limited counterpart.
3747 if Ada_Version >= Ada_2012 then
3748 Exchange_Limited_Views (Spec_Id);
3749 end if;
3751 -- Analyze any aspect specifications that appear on the subprogram body
3753 if Has_Aspects (N) then
3754 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3755 end if;
3757 Analyze_Declarations (Declarations (N));
3759 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3761 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3762 if Present (SPARK_Pragma (Spec_Id)) then
3763 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3764 and then
3765 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3766 then
3767 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3768 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3769 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3770 Error_Msg_NE
3771 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3772 end if;
3774 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3775 null;
3777 else
3778 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3779 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3780 Error_Msg_Sloc := Sloc (Spec_Id);
3781 Error_Msg_NE
3782 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3783 end if;
3784 end if;
3786 -- When a subprogram body appears inside a package, its contract is
3787 -- analyzed at the end of the package body declarations. This is due
3788 -- to the delay with respect of the package contract upon which the
3789 -- body contract may depend. When the subprogram body is stand alone
3790 -- and acts as a compilation unit, this delay is not necessary.
3792 if Nkind (Parent (N)) = N_Compilation_Unit then
3793 Analyze_Subprogram_Body_Contract (Body_Id);
3794 end if;
3796 -- Deal with preconditions, [refined] postconditions, Contract_Cases,
3797 -- invariants and predicates associated with body and its spec. Since
3798 -- there is no routine Expand_Declarations which would otherwise deal
3799 -- with the contract expansion, generate all necessary mechanisms to
3800 -- verify the contract assertions now.
3802 Expand_Subprogram_Contract (N);
3804 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3805 -- subprogram in GNATprove mode, as its body should not be analyzed.
3807 if SPARK_Mode /= On
3808 and then GNATprove_Mode
3809 and then Present (Spec_Id)
3810 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3811 then
3812 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3813 Set_Is_Inlined_Always (Spec_Id, False);
3814 end if;
3816 -- Check completion, and analyze the statements
3818 Check_Completion;
3819 Inspect_Deferred_Constant_Completion (Declarations (N));
3820 Analyze (HSS);
3822 -- Deal with end of scope processing for the body
3824 Process_End_Label (HSS, 't', Current_Scope);
3825 End_Scope;
3826 Check_Subprogram_Order (N);
3827 Set_Analyzed (Body_Id);
3829 -- If we have a separate spec, then the analysis of the declarations
3830 -- caused the entities in the body to be chained to the spec id, but
3831 -- we want them chained to the body id. Only the formal parameters
3832 -- end up chained to the spec id in this case.
3834 if Present (Spec_Id) then
3836 -- We must conform to the categorization of our spec
3838 Validate_Categorization_Dependency (N, Spec_Id);
3840 -- And if this is a child unit, the parent units must conform
3842 if Is_Child_Unit (Spec_Id) then
3843 Validate_Categorization_Dependency
3844 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3845 end if;
3847 -- Here is where we move entities from the spec to the body
3849 -- Case where there are entities that stay with the spec
3851 if Present (Last_Real_Spec_Entity) then
3853 -- No body entities (happens when the only real spec entities come
3854 -- from precondition and postcondition pragmas).
3856 if No (Last_Entity (Body_Id)) then
3857 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3859 -- Body entities present (formals), so chain stuff past them
3861 else
3862 Set_Next_Entity
3863 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3864 end if;
3866 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3867 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3868 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3870 -- Case where there are no spec entities, in this case there can be
3871 -- no body entities either, so just move everything.
3873 -- If the body is generated for an expression function, it may have
3874 -- been preanalyzed already, if 'access was applied to it.
3876 else
3877 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3878 N_Expression_Function
3879 then
3880 pragma Assert (No (Last_Entity (Body_Id)));
3881 null;
3882 end if;
3884 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3885 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3886 Set_First_Entity (Spec_Id, Empty);
3887 Set_Last_Entity (Spec_Id, Empty);
3888 end if;
3889 end if;
3891 Check_Missing_Return;
3893 -- Now we are going to check for variables that are never modified in
3894 -- the body of the procedure. But first we deal with a special case
3895 -- where we want to modify this check. If the body of the subprogram
3896 -- starts with a raise statement or its equivalent, or if the body
3897 -- consists entirely of a null statement, then it is pretty obvious that
3898 -- it is OK to not reference the parameters. For example, this might be
3899 -- the following common idiom for a stubbed function: statement of the
3900 -- procedure raises an exception. In particular this deals with the
3901 -- common idiom of a stubbed function, which appears something like:
3903 -- function F (A : Integer) return Some_Type;
3904 -- X : Some_Type;
3905 -- begin
3906 -- raise Program_Error;
3907 -- return X;
3908 -- end F;
3910 -- Here the purpose of X is simply to satisfy the annoying requirement
3911 -- in Ada that there be at least one return, and we certainly do not
3912 -- want to go posting warnings on X that it is not initialized. On
3913 -- the other hand, if X is entirely unreferenced that should still
3914 -- get a warning.
3916 -- What we do is to detect these cases, and if we find them, flag the
3917 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3918 -- suppress unwanted warnings. For the case of the function stub above
3919 -- we have a special test to set X as apparently assigned to suppress
3920 -- the warning.
3922 declare
3923 Stm : Node_Id;
3925 begin
3926 -- Skip initial labels (for one thing this occurs when we are in
3927 -- front end ZCX mode, but in any case it is irrelevant), and also
3928 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3930 Stm := First (Statements (HSS));
3931 while Nkind (Stm) = N_Label
3932 or else Nkind (Stm) in N_Push_xxx_Label
3933 loop
3934 Next (Stm);
3935 end loop;
3937 -- Do the test on the original statement before expansion
3939 declare
3940 Ostm : constant Node_Id := Original_Node (Stm);
3942 begin
3943 -- If explicit raise statement, turn on flag
3945 if Nkind (Ostm) = N_Raise_Statement then
3946 Set_Trivial_Subprogram (Stm);
3948 -- If null statement, and no following statements, turn on flag
3950 elsif Nkind (Stm) = N_Null_Statement
3951 and then Comes_From_Source (Stm)
3952 and then No (Next (Stm))
3953 then
3954 Set_Trivial_Subprogram (Stm);
3956 -- Check for explicit call cases which likely raise an exception
3958 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3959 if Is_Entity_Name (Name (Ostm)) then
3960 declare
3961 Ent : constant Entity_Id := Entity (Name (Ostm));
3963 begin
3964 -- If the procedure is marked No_Return, then likely it
3965 -- raises an exception, but in any case it is not coming
3966 -- back here, so turn on the flag.
3968 if Present (Ent)
3969 and then Ekind (Ent) = E_Procedure
3970 and then No_Return (Ent)
3971 then
3972 Set_Trivial_Subprogram (Stm);
3973 end if;
3974 end;
3975 end if;
3976 end if;
3977 end;
3978 end;
3980 -- Check for variables that are never modified
3982 declare
3983 E1, E2 : Entity_Id;
3985 begin
3986 -- If there is a separate spec, then transfer Never_Set_In_Source
3987 -- flags from out parameters to the corresponding entities in the
3988 -- body. The reason we do that is we want to post error flags on
3989 -- the body entities, not the spec entities.
3991 if Present (Spec_Id) then
3992 E1 := First_Entity (Spec_Id);
3993 while Present (E1) loop
3994 if Ekind (E1) = E_Out_Parameter then
3995 E2 := First_Entity (Body_Id);
3996 while Present (E2) loop
3997 exit when Chars (E1) = Chars (E2);
3998 Next_Entity (E2);
3999 end loop;
4001 if Present (E2) then
4002 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4003 end if;
4004 end if;
4006 Next_Entity (E1);
4007 end loop;
4008 end if;
4010 -- Check references in body
4012 Check_References (Body_Id);
4013 end;
4015 -- Check for nested subprogram, and mark outer level subprogram if so
4017 declare
4018 Ent : Entity_Id;
4020 begin
4021 if Present (Spec_Id) then
4022 Ent := Spec_Id;
4023 else
4024 Ent := Body_Id;
4025 end if;
4027 loop
4028 Ent := Enclosing_Subprogram (Ent);
4029 exit when No (Ent) or else Is_Subprogram (Ent);
4030 end loop;
4032 if Present (Ent) then
4033 Set_Has_Nested_Subprogram (Ent);
4034 end if;
4035 end;
4037 Restore_Globals;
4038 end Analyze_Subprogram_Body_Helper;
4040 ---------------------------------
4041 -- Analyze_Subprogram_Contract --
4042 ---------------------------------
4044 procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is
4045 Items : constant Node_Id := Contract (Subp_Id);
4046 Depends : Node_Id := Empty;
4047 Global : Node_Id := Empty;
4048 Mode : SPARK_Mode_Type;
4049 Prag : Node_Id;
4050 Prag_Nam : Name_Id;
4052 begin
4053 -- Due to the timing of contract analysis, delayed pragmas may be
4054 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4055 -- context. To remedy this, restore the original SPARK_Mode of the
4056 -- related subprogram body.
4058 Save_SPARK_Mode_And_Set (Subp_Id, Mode);
4060 -- All subprograms carry a contract, but for some it is not significant
4061 -- and should not be processed.
4063 if not Has_Significant_Contract (Subp_Id) then
4064 null;
4066 elsif Present (Items) then
4068 -- Analyze pre- and postconditions
4070 Prag := Pre_Post_Conditions (Items);
4071 while Present (Prag) loop
4072 Analyze_Pre_Post_Condition_In_Decl_Part (Prag);
4073 Prag := Next_Pragma (Prag);
4074 end loop;
4076 -- Analyze contract-cases and test-cases
4078 Prag := Contract_Test_Cases (Items);
4079 while Present (Prag) loop
4080 Prag_Nam := Pragma_Name (Prag);
4082 if Prag_Nam = Name_Contract_Cases then
4083 Analyze_Contract_Cases_In_Decl_Part (Prag);
4084 else
4085 pragma Assert (Prag_Nam = Name_Test_Case);
4086 Analyze_Test_Case_In_Decl_Part (Prag);
4087 end if;
4089 Prag := Next_Pragma (Prag);
4090 end loop;
4092 -- Analyze classification pragmas
4094 Prag := Classifications (Items);
4095 while Present (Prag) loop
4096 Prag_Nam := Pragma_Name (Prag);
4098 if Prag_Nam = Name_Depends then
4099 Depends := Prag;
4101 elsif Prag_Nam = Name_Global then
4102 Global := Prag;
4104 -- Note that pragma Extensions_Visible has already been analyzed
4106 end if;
4108 Prag := Next_Pragma (Prag);
4109 end loop;
4111 -- Analyze Global first as Depends may mention items classified in
4112 -- the global categorization.
4114 if Present (Global) then
4115 Analyze_Global_In_Decl_Part (Global);
4116 end if;
4118 -- Depends must be analyzed after Global in order to see the modes of
4119 -- all global items.
4121 if Present (Depends) then
4122 Analyze_Depends_In_Decl_Part (Depends);
4123 end if;
4125 -- Ensure that the contract cases or postconditions mention 'Result
4126 -- or define a post-state.
4128 Check_Result_And_Post_State (Subp_Id);
4129 end if;
4131 -- Restore the SPARK_Mode of the enclosing context after all delayed
4132 -- pragmas have been analyzed.
4134 Restore_SPARK_Mode (Mode);
4135 end Analyze_Subprogram_Contract;
4137 ------------------------------------
4138 -- Analyze_Subprogram_Declaration --
4139 ------------------------------------
4141 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4142 GM : constant Ghost_Mode_Type := Ghost_Mode;
4144 procedure Restore_Globals;
4145 -- Restore the values of all saved global variables
4147 ---------------------
4148 -- Restore_Globals --
4149 ---------------------
4151 procedure Restore_Globals is
4152 begin
4153 Ghost_Mode := GM;
4154 end Restore_Globals;
4156 -- Local variables
4158 Scop : constant Entity_Id := Current_Scope;
4159 Designator : Entity_Id;
4161 Is_Completion : Boolean;
4162 -- Indicates whether a null procedure declaration is a completion
4164 -- Start of processing for Analyze_Subprogram_Declaration
4166 begin
4167 -- The subprogram declaration may be subject to pragma Ghost with policy
4168 -- Ignore. Set the mode now to ensure that any nodes generated during
4169 -- analysis and expansion are properly flagged as ignored Ghost.
4171 Set_Ghost_Mode (N);
4173 -- Null procedures are not allowed in SPARK
4175 if Nkind (Specification (N)) = N_Procedure_Specification
4176 and then Null_Present (Specification (N))
4177 then
4178 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4180 -- Null procedures are allowed in protected types, following the
4181 -- recent AI12-0147.
4183 if Is_Protected_Type (Current_Scope)
4184 and then Ada_Version < Ada_2012
4185 then
4186 Error_Msg_N ("protected operation cannot be a null procedure", N);
4187 end if;
4189 Analyze_Null_Procedure (N, Is_Completion);
4191 -- The null procedure acts as a body, nothing further is needed
4193 if Is_Completion then
4194 Restore_Globals;
4195 return;
4196 end if;
4197 end if;
4199 Designator := Analyze_Subprogram_Specification (Specification (N));
4201 -- A reference may already have been generated for the unit name, in
4202 -- which case the following call is redundant. However it is needed for
4203 -- declarations that are the rewriting of an expression function.
4205 Generate_Definition (Designator);
4207 -- Set SPARK mode from current context (may be overwritten later with
4208 -- explicit pragma).
4210 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4211 Set_SPARK_Pragma_Inherited (Designator);
4213 -- A subprogram declared within a Ghost region is automatically Ghost
4214 -- (SPARK RM 6.9(2)).
4216 if Comes_From_Source (Designator) and then Ghost_Mode > None then
4217 Set_Is_Ghost_Entity (Designator);
4218 end if;
4220 if Debug_Flag_C then
4221 Write_Str ("==> subprogram spec ");
4222 Write_Name (Chars (Designator));
4223 Write_Str (" from ");
4224 Write_Location (Sloc (N));
4225 Write_Eol;
4226 Indent;
4227 end if;
4229 Validate_RCI_Subprogram_Declaration (N);
4230 New_Overloaded_Entity (Designator);
4231 Check_Delayed_Subprogram (Designator);
4233 -- If the type of the first formal of the current subprogram is a non-
4234 -- generic tagged private type, mark the subprogram as being a private
4235 -- primitive. Ditto if this is a function with controlling result, and
4236 -- the return type is currently private. In both cases, the type of the
4237 -- controlling argument or result must be in the current scope for the
4238 -- operation to be primitive.
4240 if Has_Controlling_Result (Designator)
4241 and then Is_Private_Type (Etype (Designator))
4242 and then Scope (Etype (Designator)) = Current_Scope
4243 and then not Is_Generic_Actual_Type (Etype (Designator))
4244 then
4245 Set_Is_Private_Primitive (Designator);
4247 elsif Present (First_Formal (Designator)) then
4248 declare
4249 Formal_Typ : constant Entity_Id :=
4250 Etype (First_Formal (Designator));
4251 begin
4252 Set_Is_Private_Primitive (Designator,
4253 Is_Tagged_Type (Formal_Typ)
4254 and then Scope (Formal_Typ) = Current_Scope
4255 and then Is_Private_Type (Formal_Typ)
4256 and then not Is_Generic_Actual_Type (Formal_Typ));
4257 end;
4258 end if;
4260 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4261 -- or null.
4263 if Ada_Version >= Ada_2005
4264 and then Comes_From_Source (N)
4265 and then Is_Dispatching_Operation (Designator)
4266 then
4267 declare
4268 E : Entity_Id;
4269 Etyp : Entity_Id;
4271 begin
4272 if Has_Controlling_Result (Designator) then
4273 Etyp := Etype (Designator);
4275 else
4276 E := First_Entity (Designator);
4277 while Present (E)
4278 and then Is_Formal (E)
4279 and then not Is_Controlling_Formal (E)
4280 loop
4281 Next_Entity (E);
4282 end loop;
4284 Etyp := Etype (E);
4285 end if;
4287 if Is_Access_Type (Etyp) then
4288 Etyp := Directly_Designated_Type (Etyp);
4289 end if;
4291 if Is_Interface (Etyp)
4292 and then not Is_Abstract_Subprogram (Designator)
4293 and then not (Ekind (Designator) = E_Procedure
4294 and then Null_Present (Specification (N)))
4295 then
4296 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4298 -- Specialize error message based on procedures vs. functions,
4299 -- since functions can't be null subprograms.
4301 if Ekind (Designator) = E_Procedure then
4302 Error_Msg_N
4303 ("interface procedure % must be abstract or null", N);
4304 else
4305 Error_Msg_N
4306 ("interface function % must be abstract", N);
4307 end if;
4308 end if;
4309 end;
4310 end if;
4312 -- What is the following code for, it used to be
4314 -- ??? Set_Suppress_Elaboration_Checks
4315 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4317 -- The following seems equivalent, but a bit dubious
4319 if Elaboration_Checks_Suppressed (Designator) then
4320 Set_Kill_Elaboration_Checks (Designator);
4321 end if;
4323 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4324 Set_Categorization_From_Scope (Designator, Scop);
4326 else
4327 -- For a compilation unit, check for library-unit pragmas
4329 Push_Scope (Designator);
4330 Set_Categorization_From_Pragmas (N);
4331 Validate_Categorization_Dependency (N, Designator);
4332 Pop_Scope;
4333 end if;
4335 -- For a compilation unit, set body required. This flag will only be
4336 -- reset if a valid Import or Interface pragma is processed later on.
4338 if Nkind (Parent (N)) = N_Compilation_Unit then
4339 Set_Body_Required (Parent (N), True);
4341 if Ada_Version >= Ada_2005
4342 and then Nkind (Specification (N)) = N_Procedure_Specification
4343 and then Null_Present (Specification (N))
4344 then
4345 Error_Msg_N
4346 ("null procedure cannot be declared at library level", N);
4347 end if;
4348 end if;
4350 Generate_Reference_To_Formals (Designator);
4351 Check_Eliminated (Designator);
4353 if Debug_Flag_C then
4354 Outdent;
4355 Write_Str ("<== subprogram spec ");
4356 Write_Name (Chars (Designator));
4357 Write_Str (" from ");
4358 Write_Location (Sloc (N));
4359 Write_Eol;
4360 end if;
4362 if Is_Protected_Type (Current_Scope) then
4364 -- Indicate that this is a protected operation, because it may be
4365 -- used in subsequent declarations within the protected type.
4367 Set_Convention (Designator, Convention_Protected);
4368 end if;
4370 List_Inherited_Pre_Post_Aspects (Designator);
4372 if Has_Aspects (N) then
4373 Analyze_Aspect_Specifications (N, Designator);
4374 end if;
4376 Restore_Globals;
4377 end Analyze_Subprogram_Declaration;
4379 --------------------------------------
4380 -- Analyze_Subprogram_Specification --
4381 --------------------------------------
4383 -- Reminder: N here really is a subprogram specification (not a subprogram
4384 -- declaration). This procedure is called to analyze the specification in
4385 -- both subprogram bodies and subprogram declarations (specs).
4387 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4388 Designator : constant Entity_Id := Defining_Entity (N);
4389 Formals : constant List_Id := Parameter_Specifications (N);
4391 -- Start of processing for Analyze_Subprogram_Specification
4393 begin
4394 -- User-defined operator is not allowed in SPARK, except as a renaming
4396 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4397 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4398 then
4399 Check_SPARK_05_Restriction
4400 ("user-defined operator is not allowed", N);
4401 end if;
4403 -- Proceed with analysis. Do not emit a cross-reference entry if the
4404 -- specification comes from an expression function, because it may be
4405 -- the completion of a previous declaration. It is is not, the cross-
4406 -- reference entry will be emitted for the new subprogram declaration.
4408 if Nkind (Parent (N)) /= N_Expression_Function then
4409 Generate_Definition (Designator);
4410 end if;
4412 if Nkind (N) = N_Function_Specification then
4413 Set_Ekind (Designator, E_Function);
4414 Set_Mechanism (Designator, Default_Mechanism);
4415 else
4416 Set_Ekind (Designator, E_Procedure);
4417 Set_Etype (Designator, Standard_Void_Type);
4418 end if;
4420 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4421 -- those subprograms which could be inlined in GNATprove mode (because
4422 -- Body_To_Inline is non-Empty) but cannot be inlined.
4424 if GNATprove_Mode then
4425 Set_Is_Inlined_Always (Designator);
4426 end if;
4428 -- Introduce new scope for analysis of the formals and the return type
4430 Set_Scope (Designator, Current_Scope);
4432 if Present (Formals) then
4433 Push_Scope (Designator);
4434 Process_Formals (Formals, N);
4436 -- Check dimensions in N for formals with default expression
4438 Analyze_Dimension_Formals (N, Formals);
4440 -- Ada 2005 (AI-345): If this is an overriding operation of an
4441 -- inherited interface operation, and the controlling type is
4442 -- a synchronized type, replace the type with its corresponding
4443 -- record, to match the proper signature of an overriding operation.
4444 -- Same processing for an access parameter whose designated type is
4445 -- derived from a synchronized interface.
4447 if Ada_Version >= Ada_2005 then
4448 declare
4449 Formal : Entity_Id;
4450 Formal_Typ : Entity_Id;
4451 Rec_Typ : Entity_Id;
4452 Desig_Typ : Entity_Id;
4454 begin
4455 Formal := First_Formal (Designator);
4456 while Present (Formal) loop
4457 Formal_Typ := Etype (Formal);
4459 if Is_Concurrent_Type (Formal_Typ)
4460 and then Present (Corresponding_Record_Type (Formal_Typ))
4461 then
4462 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4464 if Present (Interfaces (Rec_Typ)) then
4465 Set_Etype (Formal, Rec_Typ);
4466 end if;
4468 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4469 Desig_Typ := Designated_Type (Formal_Typ);
4471 if Is_Concurrent_Type (Desig_Typ)
4472 and then Present (Corresponding_Record_Type (Desig_Typ))
4473 then
4474 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4476 if Present (Interfaces (Rec_Typ)) then
4477 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4478 end if;
4479 end if;
4480 end if;
4482 Next_Formal (Formal);
4483 end loop;
4484 end;
4485 end if;
4487 End_Scope;
4489 -- The subprogram scope is pushed and popped around the processing of
4490 -- the return type for consistency with call above to Process_Formals
4491 -- (which itself can call Analyze_Return_Type), and to ensure that any
4492 -- itype created for the return type will be associated with the proper
4493 -- scope.
4495 elsif Nkind (N) = N_Function_Specification then
4496 Push_Scope (Designator);
4497 Analyze_Return_Type (N);
4498 End_Scope;
4499 end if;
4501 -- Function case
4503 if Nkind (N) = N_Function_Specification then
4505 -- Deal with operator symbol case
4507 if Nkind (Designator) = N_Defining_Operator_Symbol then
4508 Valid_Operator_Definition (Designator);
4509 end if;
4511 May_Need_Actuals (Designator);
4513 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4514 -- the subprogram is abstract also. This does not apply to renaming
4515 -- declarations, where abstractness is inherited, and to subprogram
4516 -- bodies generated for stream operations, which become renamings as
4517 -- bodies.
4519 -- In case of primitives associated with abstract interface types
4520 -- the check is applied later (see Analyze_Subprogram_Declaration).
4522 if not Nkind_In (Original_Node (Parent (N)),
4523 N_Subprogram_Renaming_Declaration,
4524 N_Abstract_Subprogram_Declaration,
4525 N_Formal_Abstract_Subprogram_Declaration)
4526 then
4527 if Is_Abstract_Type (Etype (Designator))
4528 and then not Is_Interface (Etype (Designator))
4529 then
4530 Error_Msg_N
4531 ("function that returns abstract type must be abstract", N);
4533 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4534 -- access result whose designated type is abstract.
4536 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4537 and then
4538 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4539 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4540 and then Ada_Version >= Ada_2012
4541 then
4542 Error_Msg_N ("function whose access result designates "
4543 & "abstract type must be abstract", N);
4544 end if;
4545 end if;
4546 end if;
4548 return Designator;
4549 end Analyze_Subprogram_Specification;
4551 -----------------------
4552 -- Check_Conformance --
4553 -----------------------
4555 procedure Check_Conformance
4556 (New_Id : Entity_Id;
4557 Old_Id : Entity_Id;
4558 Ctype : Conformance_Type;
4559 Errmsg : Boolean;
4560 Conforms : out Boolean;
4561 Err_Loc : Node_Id := Empty;
4562 Get_Inst : Boolean := False;
4563 Skip_Controlling_Formals : Boolean := False)
4565 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4566 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4567 -- If Errmsg is True, then processing continues to post an error message
4568 -- for conformance error on given node. Two messages are output. The
4569 -- first message points to the previous declaration with a general "no
4570 -- conformance" message. The second is the detailed reason, supplied as
4571 -- Msg. The parameter N provide information for a possible & insertion
4572 -- in the message, and also provides the location for posting the
4573 -- message in the absence of a specified Err_Loc location.
4575 -----------------------
4576 -- Conformance_Error --
4577 -----------------------
4579 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4580 Enode : Node_Id;
4582 begin
4583 Conforms := False;
4585 if Errmsg then
4586 if No (Err_Loc) then
4587 Enode := N;
4588 else
4589 Enode := Err_Loc;
4590 end if;
4592 Error_Msg_Sloc := Sloc (Old_Id);
4594 case Ctype is
4595 when Type_Conformant =>
4596 Error_Msg_N -- CODEFIX
4597 ("not type conformant with declaration#!", Enode);
4599 when Mode_Conformant =>
4600 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4601 Error_Msg_N
4602 ("not mode conformant with operation inherited#!",
4603 Enode);
4604 else
4605 Error_Msg_N
4606 ("not mode conformant with declaration#!", Enode);
4607 end if;
4609 when Subtype_Conformant =>
4610 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4611 Error_Msg_N
4612 ("not subtype conformant with operation inherited#!",
4613 Enode);
4614 else
4615 Error_Msg_N
4616 ("not subtype conformant with declaration#!", Enode);
4617 end if;
4619 when Fully_Conformant =>
4620 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4621 Error_Msg_N -- CODEFIX
4622 ("not fully conformant with operation inherited#!",
4623 Enode);
4624 else
4625 Error_Msg_N -- CODEFIX
4626 ("not fully conformant with declaration#!", Enode);
4627 end if;
4628 end case;
4630 Error_Msg_NE (Msg, Enode, N);
4631 end if;
4632 end Conformance_Error;
4634 -- Local Variables
4636 Old_Type : constant Entity_Id := Etype (Old_Id);
4637 New_Type : constant Entity_Id := Etype (New_Id);
4638 Old_Formal : Entity_Id;
4639 New_Formal : Entity_Id;
4640 Access_Types_Match : Boolean;
4641 Old_Formal_Base : Entity_Id;
4642 New_Formal_Base : Entity_Id;
4644 -- Start of processing for Check_Conformance
4646 begin
4647 Conforms := True;
4649 -- We need a special case for operators, since they don't appear
4650 -- explicitly.
4652 if Ctype = Type_Conformant then
4653 if Ekind (New_Id) = E_Operator
4654 and then Operator_Matches_Spec (New_Id, Old_Id)
4655 then
4656 return;
4657 end if;
4658 end if;
4660 -- If both are functions/operators, check return types conform
4662 if Old_Type /= Standard_Void_Type
4663 and then
4664 New_Type /= Standard_Void_Type
4665 then
4666 -- If we are checking interface conformance we omit controlling
4667 -- arguments and result, because we are only checking the conformance
4668 -- of the remaining parameters.
4670 if Has_Controlling_Result (Old_Id)
4671 and then Has_Controlling_Result (New_Id)
4672 and then Skip_Controlling_Formals
4673 then
4674 null;
4676 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4677 if Ctype >= Subtype_Conformant
4678 and then not Predicates_Match (Old_Type, New_Type)
4679 then
4680 Conformance_Error
4681 ("\predicate of return type does not match!", New_Id);
4682 else
4683 Conformance_Error
4684 ("\return type does not match!", New_Id);
4685 end if;
4687 return;
4688 end if;
4690 -- Ada 2005 (AI-231): In case of anonymous access types check the
4691 -- null-exclusion and access-to-constant attributes match.
4693 if Ada_Version >= Ada_2005
4694 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4695 and then
4696 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4697 or else Is_Access_Constant (Etype (Old_Type)) /=
4698 Is_Access_Constant (Etype (New_Type)))
4699 then
4700 Conformance_Error ("\return type does not match!", New_Id);
4701 return;
4702 end if;
4704 -- If either is a function/operator and the other isn't, error
4706 elsif Old_Type /= Standard_Void_Type
4707 or else New_Type /= Standard_Void_Type
4708 then
4709 Conformance_Error ("\functions can only match functions!", New_Id);
4710 return;
4711 end if;
4713 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4714 -- If this is a renaming as body, refine error message to indicate that
4715 -- the conflict is with the original declaration. If the entity is not
4716 -- frozen, the conventions don't have to match, the one of the renamed
4717 -- entity is inherited.
4719 if Ctype >= Subtype_Conformant then
4720 if Convention (Old_Id) /= Convention (New_Id) then
4721 if not Is_Frozen (New_Id) then
4722 null;
4724 elsif Present (Err_Loc)
4725 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4726 and then Present (Corresponding_Spec (Err_Loc))
4727 then
4728 Error_Msg_Name_1 := Chars (New_Id);
4729 Error_Msg_Name_2 :=
4730 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4731 Conformance_Error ("\prior declaration for% has convention %!");
4733 else
4734 Conformance_Error ("\calling conventions do not match!");
4735 end if;
4737 return;
4739 elsif Is_Formal_Subprogram (Old_Id)
4740 or else Is_Formal_Subprogram (New_Id)
4741 then
4742 Conformance_Error ("\formal subprograms not allowed!");
4743 return;
4745 -- Pragma Ghost behaves as a convention in the context of subtype
4746 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4747 -- subprograms as their spec may reside in a Ghost region and their
4748 -- body not, or vice versa.
4750 elsif Comes_From_Source (Old_Id)
4751 and then Comes_From_Source (New_Id)
4752 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4753 then
4754 Conformance_Error ("\ghost modes do not match!");
4755 return;
4756 end if;
4757 end if;
4759 -- Deal with parameters
4761 -- Note: we use the entity information, rather than going directly
4762 -- to the specification in the tree. This is not only simpler, but
4763 -- absolutely necessary for some cases of conformance tests between
4764 -- operators, where the declaration tree simply does not exist.
4766 Old_Formal := First_Formal (Old_Id);
4767 New_Formal := First_Formal (New_Id);
4768 while Present (Old_Formal) and then Present (New_Formal) loop
4769 if Is_Controlling_Formal (Old_Formal)
4770 and then Is_Controlling_Formal (New_Formal)
4771 and then Skip_Controlling_Formals
4772 then
4773 -- The controlling formals will have different types when
4774 -- comparing an interface operation with its match, but both
4775 -- or neither must be access parameters.
4777 if Is_Access_Type (Etype (Old_Formal))
4779 Is_Access_Type (Etype (New_Formal))
4780 then
4781 goto Skip_Controlling_Formal;
4782 else
4783 Conformance_Error
4784 ("\access parameter does not match!", New_Formal);
4785 end if;
4786 end if;
4788 -- Ada 2012: Mode conformance also requires that formal parameters
4789 -- be both aliased, or neither.
4791 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4792 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4793 Conformance_Error
4794 ("\aliased parameter mismatch!", New_Formal);
4795 end if;
4796 end if;
4798 if Ctype = Fully_Conformant then
4800 -- Names must match. Error message is more accurate if we do
4801 -- this before checking that the types of the formals match.
4803 if Chars (Old_Formal) /= Chars (New_Formal) then
4804 Conformance_Error ("\name& does not match!", New_Formal);
4806 -- Set error posted flag on new formal as well to stop
4807 -- junk cascaded messages in some cases.
4809 Set_Error_Posted (New_Formal);
4810 return;
4811 end if;
4813 -- Null exclusion must match
4815 if Null_Exclusion_Present (Parent (Old_Formal))
4817 Null_Exclusion_Present (Parent (New_Formal))
4818 then
4819 -- Only give error if both come from source. This should be
4820 -- investigated some time, since it should not be needed ???
4822 if Comes_From_Source (Old_Formal)
4823 and then
4824 Comes_From_Source (New_Formal)
4825 then
4826 Conformance_Error
4827 ("\null exclusion for& does not match", New_Formal);
4829 -- Mark error posted on the new formal to avoid duplicated
4830 -- complaint about types not matching.
4832 Set_Error_Posted (New_Formal);
4833 end if;
4834 end if;
4835 end if;
4837 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4838 -- case occurs whenever a subprogram is being renamed and one of its
4839 -- parameters imposes a null exclusion. For example:
4841 -- type T is null record;
4842 -- type Acc_T is access T;
4843 -- subtype Acc_T_Sub is Acc_T;
4845 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4846 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4847 -- renames P;
4849 Old_Formal_Base := Etype (Old_Formal);
4850 New_Formal_Base := Etype (New_Formal);
4852 if Get_Inst then
4853 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4854 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4855 end if;
4857 Access_Types_Match := Ada_Version >= Ada_2005
4859 -- Ensure that this rule is only applied when New_Id is a
4860 -- renaming of Old_Id.
4862 and then Nkind (Parent (Parent (New_Id))) =
4863 N_Subprogram_Renaming_Declaration
4864 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4865 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4866 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4868 -- Now handle the allowed access-type case
4870 and then Is_Access_Type (Old_Formal_Base)
4871 and then Is_Access_Type (New_Formal_Base)
4873 -- The type kinds must match. The only exception occurs with
4874 -- multiple generics of the form:
4876 -- generic generic
4877 -- type F is private; type A is private;
4878 -- type F_Ptr is access F; type A_Ptr is access A;
4879 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4880 -- package F_Pack is ... package A_Pack is
4881 -- package F_Inst is
4882 -- new F_Pack (A, A_Ptr, A_P);
4884 -- When checking for conformance between the parameters of A_P
4885 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4886 -- because the compiler has transformed A_Ptr into a subtype of
4887 -- F_Ptr. We catch this case in the code below.
4889 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4890 or else
4891 (Is_Generic_Type (Old_Formal_Base)
4892 and then Is_Generic_Type (New_Formal_Base)
4893 and then Is_Internal (New_Formal_Base)
4894 and then Etype (Etype (New_Formal_Base)) =
4895 Old_Formal_Base))
4896 and then Directly_Designated_Type (Old_Formal_Base) =
4897 Directly_Designated_Type (New_Formal_Base)
4898 and then ((Is_Itype (Old_Formal_Base)
4899 and then Can_Never_Be_Null (Old_Formal_Base))
4900 or else
4901 (Is_Itype (New_Formal_Base)
4902 and then Can_Never_Be_Null (New_Formal_Base)));
4904 -- Types must always match. In the visible part of an instance,
4905 -- usual overloading rules for dispatching operations apply, and
4906 -- we check base types (not the actual subtypes).
4908 if In_Instance_Visible_Part
4909 and then Is_Dispatching_Operation (New_Id)
4910 then
4911 if not Conforming_Types
4912 (T1 => Base_Type (Etype (Old_Formal)),
4913 T2 => Base_Type (Etype (New_Formal)),
4914 Ctype => Ctype,
4915 Get_Inst => Get_Inst)
4916 and then not Access_Types_Match
4917 then
4918 Conformance_Error ("\type of & does not match!", New_Formal);
4919 return;
4920 end if;
4922 elsif not Conforming_Types
4923 (T1 => Old_Formal_Base,
4924 T2 => New_Formal_Base,
4925 Ctype => Ctype,
4926 Get_Inst => Get_Inst)
4927 and then not Access_Types_Match
4928 then
4929 -- Don't give error message if old type is Any_Type. This test
4930 -- avoids some cascaded errors, e.g. in case of a bad spec.
4932 if Errmsg and then Old_Formal_Base = Any_Type then
4933 Conforms := False;
4934 else
4935 if Ctype >= Subtype_Conformant
4936 and then
4937 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4938 then
4939 Conformance_Error
4940 ("\predicate of & does not match!", New_Formal);
4941 else
4942 Conformance_Error
4943 ("\type of & does not match!", New_Formal);
4944 end if;
4945 end if;
4947 return;
4948 end if;
4950 -- For mode conformance, mode must match
4952 if Ctype >= Mode_Conformant then
4953 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4954 if not Ekind_In (New_Id, E_Function, E_Procedure)
4955 or else not Is_Primitive_Wrapper (New_Id)
4956 then
4957 Conformance_Error ("\mode of & does not match!", New_Formal);
4959 else
4960 declare
4961 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4962 begin
4963 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
4964 then
4965 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
4966 else
4967 Conformance_Error
4968 ("\mode of & does not match!", New_Formal);
4969 end if;
4970 end;
4971 end if;
4973 return;
4975 -- Part of mode conformance for access types is having the same
4976 -- constant modifier.
4978 elsif Access_Types_Match
4979 and then Is_Access_Constant (Old_Formal_Base) /=
4980 Is_Access_Constant (New_Formal_Base)
4981 then
4982 Conformance_Error
4983 ("\constant modifier does not match!", New_Formal);
4984 return;
4985 end if;
4986 end if;
4988 if Ctype >= Subtype_Conformant then
4990 -- Ada 2005 (AI-231): In case of anonymous access types check
4991 -- the null-exclusion and access-to-constant attributes must
4992 -- match. For null exclusion, we test the types rather than the
4993 -- formals themselves, since the attribute is only set reliably
4994 -- on the formals in the Ada 95 case, and we exclude the case
4995 -- where Old_Formal is marked as controlling, to avoid errors
4996 -- when matching completing bodies with dispatching declarations
4997 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4999 if Ada_Version >= Ada_2005
5000 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5001 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5002 and then
5003 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5004 Can_Never_Be_Null (Etype (New_Formal))
5005 and then
5006 not Is_Controlling_Formal (Old_Formal))
5007 or else
5008 Is_Access_Constant (Etype (Old_Formal)) /=
5009 Is_Access_Constant (Etype (New_Formal)))
5011 -- Do not complain if error already posted on New_Formal. This
5012 -- avoids some redundant error messages.
5014 and then not Error_Posted (New_Formal)
5015 then
5016 -- It is allowed to omit the null-exclusion in case of stream
5017 -- attribute subprograms. We recognize stream subprograms
5018 -- through their TSS-generated suffix.
5020 declare
5021 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5023 begin
5024 if TSS_Name /= TSS_Stream_Read
5025 and then TSS_Name /= TSS_Stream_Write
5026 and then TSS_Name /= TSS_Stream_Input
5027 and then TSS_Name /= TSS_Stream_Output
5028 then
5029 -- Here we have a definite conformance error. It is worth
5030 -- special casing the error message for the case of a
5031 -- controlling formal (which excludes null).
5033 if Is_Controlling_Formal (New_Formal) then
5034 Error_Msg_Node_2 := Scope (New_Formal);
5035 Conformance_Error
5036 ("\controlling formal & of & excludes null, "
5037 & "declaration must exclude null as well",
5038 New_Formal);
5040 -- Normal case (couldn't we give more detail here???)
5042 else
5043 Conformance_Error
5044 ("\type of & does not match!", New_Formal);
5045 end if;
5047 return;
5048 end if;
5049 end;
5050 end if;
5051 end if;
5053 -- Full conformance checks
5055 if Ctype = Fully_Conformant then
5057 -- We have checked already that names match
5059 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5061 -- Check default expressions for in parameters
5063 declare
5064 NewD : constant Boolean :=
5065 Present (Default_Value (New_Formal));
5066 OldD : constant Boolean :=
5067 Present (Default_Value (Old_Formal));
5068 begin
5069 if NewD or OldD then
5071 -- The old default value has been analyzed because the
5072 -- current full declaration will have frozen everything
5073 -- before. The new default value has not been analyzed,
5074 -- so analyze it now before we check for conformance.
5076 if NewD then
5077 Push_Scope (New_Id);
5078 Preanalyze_Spec_Expression
5079 (Default_Value (New_Formal), Etype (New_Formal));
5080 End_Scope;
5081 end if;
5083 if not (NewD and OldD)
5084 or else not Fully_Conformant_Expressions
5085 (Default_Value (Old_Formal),
5086 Default_Value (New_Formal))
5087 then
5088 Conformance_Error
5089 ("\default expression for & does not match!",
5090 New_Formal);
5091 return;
5092 end if;
5093 end if;
5094 end;
5095 end if;
5096 end if;
5098 -- A couple of special checks for Ada 83 mode. These checks are
5099 -- skipped if either entity is an operator in package Standard,
5100 -- or if either old or new instance is not from the source program.
5102 if Ada_Version = Ada_83
5103 and then Sloc (Old_Id) > Standard_Location
5104 and then Sloc (New_Id) > Standard_Location
5105 and then Comes_From_Source (Old_Id)
5106 and then Comes_From_Source (New_Id)
5107 then
5108 declare
5109 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5110 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5112 begin
5113 -- Explicit IN must be present or absent in both cases. This
5114 -- test is required only in the full conformance case.
5116 if In_Present (Old_Param) /= In_Present (New_Param)
5117 and then Ctype = Fully_Conformant
5118 then
5119 Conformance_Error
5120 ("\(Ada 83) IN must appear in both declarations",
5121 New_Formal);
5122 return;
5123 end if;
5125 -- Grouping (use of comma in param lists) must be the same
5126 -- This is where we catch a misconformance like:
5128 -- A, B : Integer
5129 -- A : Integer; B : Integer
5131 -- which are represented identically in the tree except
5132 -- for the setting of the flags More_Ids and Prev_Ids.
5134 if More_Ids (Old_Param) /= More_Ids (New_Param)
5135 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5136 then
5137 Conformance_Error
5138 ("\grouping of & does not match!", New_Formal);
5139 return;
5140 end if;
5141 end;
5142 end if;
5144 -- This label is required when skipping controlling formals
5146 <<Skip_Controlling_Formal>>
5148 Next_Formal (Old_Formal);
5149 Next_Formal (New_Formal);
5150 end loop;
5152 if Present (Old_Formal) then
5153 Conformance_Error ("\too few parameters!");
5154 return;
5156 elsif Present (New_Formal) then
5157 Conformance_Error ("\too many parameters!", New_Formal);
5158 return;
5159 end if;
5160 end Check_Conformance;
5162 -----------------------
5163 -- Check_Conventions --
5164 -----------------------
5166 procedure Check_Conventions (Typ : Entity_Id) is
5167 Ifaces_List : Elist_Id;
5169 procedure Check_Convention (Op : Entity_Id);
5170 -- Verify that the convention of inherited dispatching operation Op is
5171 -- consistent among all subprograms it overrides. In order to minimize
5172 -- the search, Search_From is utilized to designate a specific point in
5173 -- the list rather than iterating over the whole list once more.
5175 ----------------------
5176 -- Check_Convention --
5177 ----------------------
5179 procedure Check_Convention (Op : Entity_Id) is
5180 Op_Conv : constant Convention_Id := Convention (Op);
5181 Iface_Conv : Convention_Id;
5182 Iface_Elmt : Elmt_Id;
5183 Iface_Prim_Elmt : Elmt_Id;
5184 Iface_Prim : Entity_Id;
5186 begin
5187 Iface_Elmt := First_Elmt (Ifaces_List);
5188 while Present (Iface_Elmt) loop
5189 Iface_Prim_Elmt :=
5190 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5191 while Present (Iface_Prim_Elmt) loop
5192 Iface_Prim := Node (Iface_Prim_Elmt);
5193 Iface_Conv := Convention (Iface_Prim);
5195 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5196 and then Iface_Conv /= Op_Conv
5197 then
5198 Error_Msg_N
5199 ("inconsistent conventions in primitive operations", Typ);
5201 Error_Msg_Name_1 := Chars (Op);
5202 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5203 Error_Msg_Sloc := Sloc (Op);
5205 if Comes_From_Source (Op) or else No (Alias (Op)) then
5206 if not Present (Overridden_Operation (Op)) then
5207 Error_Msg_N ("\\primitive % defined #", Typ);
5208 else
5209 Error_Msg_N
5210 ("\\overriding operation % with "
5211 & "convention % defined #", Typ);
5212 end if;
5214 else pragma Assert (Present (Alias (Op)));
5215 Error_Msg_Sloc := Sloc (Alias (Op));
5216 Error_Msg_N ("\\inherited operation % with "
5217 & "convention % defined #", Typ);
5218 end if;
5220 Error_Msg_Name_1 := Chars (Op);
5221 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5222 Error_Msg_Sloc := Sloc (Iface_Prim);
5223 Error_Msg_N ("\\overridden operation % with "
5224 & "convention % defined #", Typ);
5226 -- Avoid cascading errors
5228 return;
5229 end if;
5231 Next_Elmt (Iface_Prim_Elmt);
5232 end loop;
5234 Next_Elmt (Iface_Elmt);
5235 end loop;
5236 end Check_Convention;
5238 -- Local variables
5240 Prim_Op : Entity_Id;
5241 Prim_Op_Elmt : Elmt_Id;
5243 -- Start of processing for Check_Conventions
5245 begin
5246 if not Has_Interfaces (Typ) then
5247 return;
5248 end if;
5250 Collect_Interfaces (Typ, Ifaces_List);
5252 -- The algorithm checks every overriding dispatching operation against
5253 -- all the corresponding overridden dispatching operations, detecting
5254 -- differences in conventions.
5256 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5257 while Present (Prim_Op_Elmt) loop
5258 Prim_Op := Node (Prim_Op_Elmt);
5260 -- A small optimization: skip the predefined dispatching operations
5261 -- since they always have the same convention.
5263 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5264 Check_Convention (Prim_Op);
5265 end if;
5267 Next_Elmt (Prim_Op_Elmt);
5268 end loop;
5269 end Check_Conventions;
5271 ------------------------------
5272 -- Check_Delayed_Subprogram --
5273 ------------------------------
5275 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5276 F : Entity_Id;
5278 procedure Possible_Freeze (T : Entity_Id);
5279 -- T is the type of either a formal parameter or of the return type.
5280 -- If T is not yet frozen and needs a delayed freeze, then the
5281 -- subprogram itself must be delayed. If T is the limited view of an
5282 -- incomplete type the subprogram must be frozen as well, because
5283 -- T may depend on local types that have not been frozen yet.
5285 ---------------------
5286 -- Possible_Freeze --
5287 ---------------------
5289 procedure Possible_Freeze (T : Entity_Id) is
5290 begin
5291 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5292 Set_Has_Delayed_Freeze (Designator);
5294 elsif Is_Access_Type (T)
5295 and then Has_Delayed_Freeze (Designated_Type (T))
5296 and then not Is_Frozen (Designated_Type (T))
5297 then
5298 Set_Has_Delayed_Freeze (Designator);
5300 elsif Ekind (T) = E_Incomplete_Type
5301 and then From_Limited_With (T)
5302 then
5303 Set_Has_Delayed_Freeze (Designator);
5305 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5306 -- of a subprogram or entry declaration.
5308 elsif Ekind (T) = E_Incomplete_Type
5309 and then Ada_Version >= Ada_2012
5310 then
5311 Set_Has_Delayed_Freeze (Designator);
5312 end if;
5314 end Possible_Freeze;
5316 -- Start of processing for Check_Delayed_Subprogram
5318 begin
5319 -- All subprograms, including abstract subprograms, may need a freeze
5320 -- node if some formal type or the return type needs one.
5322 Possible_Freeze (Etype (Designator));
5323 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5325 -- Need delayed freeze if any of the formal types themselves need
5326 -- a delayed freeze and are not yet frozen.
5328 F := First_Formal (Designator);
5329 while Present (F) loop
5330 Possible_Freeze (Etype (F));
5331 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5332 Next_Formal (F);
5333 end loop;
5335 -- Mark functions that return by reference. Note that it cannot be
5336 -- done for delayed_freeze subprograms because the underlying
5337 -- returned type may not be known yet (for private types)
5339 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5340 declare
5341 Typ : constant Entity_Id := Etype (Designator);
5342 Utyp : constant Entity_Id := Underlying_Type (Typ);
5343 begin
5344 if Is_Limited_View (Typ) then
5345 Set_Returns_By_Ref (Designator);
5346 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5347 Set_Returns_By_Ref (Designator);
5348 end if;
5349 end;
5350 end if;
5351 end Check_Delayed_Subprogram;
5353 ------------------------------------
5354 -- Check_Discriminant_Conformance --
5355 ------------------------------------
5357 procedure Check_Discriminant_Conformance
5358 (N : Node_Id;
5359 Prev : Entity_Id;
5360 Prev_Loc : Node_Id)
5362 Old_Discr : Entity_Id := First_Discriminant (Prev);
5363 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5364 New_Discr_Id : Entity_Id;
5365 New_Discr_Type : Entity_Id;
5367 procedure Conformance_Error (Msg : String; N : Node_Id);
5368 -- Post error message for conformance error on given node. Two messages
5369 -- are output. The first points to the previous declaration with a
5370 -- general "no conformance" message. The second is the detailed reason,
5371 -- supplied as Msg. The parameter N provide information for a possible
5372 -- & insertion in the message.
5374 -----------------------
5375 -- Conformance_Error --
5376 -----------------------
5378 procedure Conformance_Error (Msg : String; N : Node_Id) is
5379 begin
5380 Error_Msg_Sloc := Sloc (Prev_Loc);
5381 Error_Msg_N -- CODEFIX
5382 ("not fully conformant with declaration#!", N);
5383 Error_Msg_NE (Msg, N, N);
5384 end Conformance_Error;
5386 -- Start of processing for Check_Discriminant_Conformance
5388 begin
5389 while Present (Old_Discr) and then Present (New_Discr) loop
5390 New_Discr_Id := Defining_Identifier (New_Discr);
5392 -- The subtype mark of the discriminant on the full type has not
5393 -- been analyzed so we do it here. For an access discriminant a new
5394 -- type is created.
5396 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5397 New_Discr_Type :=
5398 Access_Definition (N, Discriminant_Type (New_Discr));
5400 else
5401 Analyze (Discriminant_Type (New_Discr));
5402 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5404 -- Ada 2005: if the discriminant definition carries a null
5405 -- exclusion, create an itype to check properly for consistency
5406 -- with partial declaration.
5408 if Is_Access_Type (New_Discr_Type)
5409 and then Null_Exclusion_Present (New_Discr)
5410 then
5411 New_Discr_Type :=
5412 Create_Null_Excluding_Itype
5413 (T => New_Discr_Type,
5414 Related_Nod => New_Discr,
5415 Scope_Id => Current_Scope);
5416 end if;
5417 end if;
5419 if not Conforming_Types
5420 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5421 then
5422 Conformance_Error ("type of & does not match!", New_Discr_Id);
5423 return;
5424 else
5425 -- Treat the new discriminant as an occurrence of the old one,
5426 -- for navigation purposes, and fill in some semantic
5427 -- information, for completeness.
5429 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5430 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5431 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5432 end if;
5434 -- Names must match
5436 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5437 Conformance_Error ("name & does not match!", New_Discr_Id);
5438 return;
5439 end if;
5441 -- Default expressions must match
5443 declare
5444 NewD : constant Boolean :=
5445 Present (Expression (New_Discr));
5446 OldD : constant Boolean :=
5447 Present (Expression (Parent (Old_Discr)));
5449 begin
5450 if NewD or OldD then
5452 -- The old default value has been analyzed and expanded,
5453 -- because the current full declaration will have frozen
5454 -- everything before. The new default values have not been
5455 -- expanded, so expand now to check conformance.
5457 if NewD then
5458 Preanalyze_Spec_Expression
5459 (Expression (New_Discr), New_Discr_Type);
5460 end if;
5462 if not (NewD and OldD)
5463 or else not Fully_Conformant_Expressions
5464 (Expression (Parent (Old_Discr)),
5465 Expression (New_Discr))
5467 then
5468 Conformance_Error
5469 ("default expression for & does not match!",
5470 New_Discr_Id);
5471 return;
5472 end if;
5473 end if;
5474 end;
5476 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5478 if Ada_Version = Ada_83 then
5479 declare
5480 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5482 begin
5483 -- Grouping (use of comma in param lists) must be the same
5484 -- This is where we catch a misconformance like:
5486 -- A, B : Integer
5487 -- A : Integer; B : Integer
5489 -- which are represented identically in the tree except
5490 -- for the setting of the flags More_Ids and Prev_Ids.
5492 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5493 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5494 then
5495 Conformance_Error
5496 ("grouping of & does not match!", New_Discr_Id);
5497 return;
5498 end if;
5499 end;
5500 end if;
5502 Next_Discriminant (Old_Discr);
5503 Next (New_Discr);
5504 end loop;
5506 if Present (Old_Discr) then
5507 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5508 return;
5510 elsif Present (New_Discr) then
5511 Conformance_Error
5512 ("too many discriminants!", Defining_Identifier (New_Discr));
5513 return;
5514 end if;
5515 end Check_Discriminant_Conformance;
5517 ----------------------------
5518 -- Check_Fully_Conformant --
5519 ----------------------------
5521 procedure Check_Fully_Conformant
5522 (New_Id : Entity_Id;
5523 Old_Id : Entity_Id;
5524 Err_Loc : Node_Id := Empty)
5526 Result : Boolean;
5527 pragma Warnings (Off, Result);
5528 begin
5529 Check_Conformance
5530 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5531 end Check_Fully_Conformant;
5533 ---------------------------
5534 -- Check_Mode_Conformant --
5535 ---------------------------
5537 procedure Check_Mode_Conformant
5538 (New_Id : Entity_Id;
5539 Old_Id : Entity_Id;
5540 Err_Loc : Node_Id := Empty;
5541 Get_Inst : Boolean := False)
5543 Result : Boolean;
5544 pragma Warnings (Off, Result);
5545 begin
5546 Check_Conformance
5547 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5548 end Check_Mode_Conformant;
5550 --------------------------------
5551 -- Check_Overriding_Indicator --
5552 --------------------------------
5554 procedure Check_Overriding_Indicator
5555 (Subp : Entity_Id;
5556 Overridden_Subp : Entity_Id;
5557 Is_Primitive : Boolean)
5559 Decl : Node_Id;
5560 Spec : Node_Id;
5562 begin
5563 -- No overriding indicator for literals
5565 if Ekind (Subp) = E_Enumeration_Literal then
5566 return;
5568 elsif Ekind (Subp) = E_Entry then
5569 Decl := Parent (Subp);
5571 -- No point in analyzing a malformed operator
5573 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5574 and then Error_Posted (Subp)
5575 then
5576 return;
5578 else
5579 Decl := Unit_Declaration_Node (Subp);
5580 end if;
5582 if Nkind_In (Decl, N_Subprogram_Body,
5583 N_Subprogram_Body_Stub,
5584 N_Subprogram_Declaration,
5585 N_Abstract_Subprogram_Declaration,
5586 N_Subprogram_Renaming_Declaration)
5587 then
5588 Spec := Specification (Decl);
5590 elsif Nkind (Decl) = N_Entry_Declaration then
5591 Spec := Decl;
5593 else
5594 return;
5595 end if;
5597 -- The overriding operation is type conformant with the overridden one,
5598 -- but the names of the formals are not required to match. If the names
5599 -- appear permuted in the overriding operation, this is a possible
5600 -- source of confusion that is worth diagnosing. Controlling formals
5601 -- often carry names that reflect the type, and it is not worthwhile
5602 -- requiring that their names match.
5604 if Present (Overridden_Subp)
5605 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5606 then
5607 declare
5608 Form1 : Entity_Id;
5609 Form2 : Entity_Id;
5611 begin
5612 Form1 := First_Formal (Subp);
5613 Form2 := First_Formal (Overridden_Subp);
5615 -- If the overriding operation is a synchronized operation, skip
5616 -- the first parameter of the overridden operation, which is
5617 -- implicit in the new one. If the operation is declared in the
5618 -- body it is not primitive and all formals must match.
5620 if Is_Concurrent_Type (Scope (Subp))
5621 and then Is_Tagged_Type (Scope (Subp))
5622 and then not Has_Completion (Scope (Subp))
5623 then
5624 Form2 := Next_Formal (Form2);
5625 end if;
5627 if Present (Form1) then
5628 Form1 := Next_Formal (Form1);
5629 Form2 := Next_Formal (Form2);
5630 end if;
5632 while Present (Form1) loop
5633 if not Is_Controlling_Formal (Form1)
5634 and then Present (Next_Formal (Form2))
5635 and then Chars (Form1) = Chars (Next_Formal (Form2))
5636 then
5637 Error_Msg_Node_2 := Alias (Overridden_Subp);
5638 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5639 Error_Msg_NE
5640 ("& does not match corresponding formal of&#",
5641 Form1, Form1);
5642 exit;
5643 end if;
5645 Next_Formal (Form1);
5646 Next_Formal (Form2);
5647 end loop;
5648 end;
5649 end if;
5651 -- If there is an overridden subprogram, then check that there is no
5652 -- "not overriding" indicator, and mark the subprogram as overriding.
5653 -- This is not done if the overridden subprogram is marked as hidden,
5654 -- which can occur for the case of inherited controlled operations
5655 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5656 -- subprogram is not itself hidden. (Note: This condition could probably
5657 -- be simplified, leaving out the testing for the specific controlled
5658 -- cases, but it seems safer and clearer this way, and echoes similar
5659 -- special-case tests of this kind in other places.)
5661 if Present (Overridden_Subp)
5662 and then (not Is_Hidden (Overridden_Subp)
5663 or else
5664 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5665 Name_Adjust,
5666 Name_Finalize)
5667 and then Present (Alias (Overridden_Subp))
5668 and then not Is_Hidden (Alias (Overridden_Subp))))
5669 then
5670 if Must_Not_Override (Spec) then
5671 Error_Msg_Sloc := Sloc (Overridden_Subp);
5673 if Ekind (Subp) = E_Entry then
5674 Error_Msg_NE
5675 ("entry & overrides inherited operation #", Spec, Subp);
5676 else
5677 Error_Msg_NE
5678 ("subprogram & overrides inherited operation #", Spec, Subp);
5679 end if;
5681 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5682 -- as an extension of Root_Controlled, and thus has a useless Adjust
5683 -- operation. This operation should not be inherited by other limited
5684 -- controlled types. An explicit Adjust for them is not overriding.
5686 elsif Must_Override (Spec)
5687 and then Chars (Overridden_Subp) = Name_Adjust
5688 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5689 and then Present (Alias (Overridden_Subp))
5690 and then
5691 Is_Predefined_File_Name
5692 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5693 then
5694 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5696 elsif Is_Subprogram (Subp) then
5697 if Is_Init_Proc (Subp) then
5698 null;
5700 elsif No (Overridden_Operation (Subp)) then
5702 -- For entities generated by Derive_Subprograms the overridden
5703 -- operation is the inherited primitive (which is available
5704 -- through the attribute alias)
5706 if (Is_Dispatching_Operation (Subp)
5707 or else Is_Dispatching_Operation (Overridden_Subp))
5708 and then not Comes_From_Source (Overridden_Subp)
5709 and then Find_Dispatching_Type (Overridden_Subp) =
5710 Find_Dispatching_Type (Subp)
5711 and then Present (Alias (Overridden_Subp))
5712 and then Comes_From_Source (Alias (Overridden_Subp))
5713 then
5714 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5715 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5717 else
5718 Set_Overridden_Operation (Subp, Overridden_Subp);
5719 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5720 end if;
5721 end if;
5722 end if;
5724 -- If primitive flag is set or this is a protected operation, then
5725 -- the operation is overriding at the point of its declaration, so
5726 -- warn if necessary. Otherwise it may have been declared before the
5727 -- operation it overrides and no check is required.
5729 if Style_Check
5730 and then not Must_Override (Spec)
5731 and then (Is_Primitive
5732 or else Ekind (Scope (Subp)) = E_Protected_Type)
5733 then
5734 Style.Missing_Overriding (Decl, Subp);
5735 end if;
5737 -- If Subp is an operator, it may override a predefined operation, if
5738 -- it is defined in the same scope as the type to which it applies.
5739 -- In that case Overridden_Subp is empty because of our implicit
5740 -- representation for predefined operators. We have to check whether the
5741 -- signature of Subp matches that of a predefined operator. Note that
5742 -- first argument provides the name of the operator, and the second
5743 -- argument the signature that may match that of a standard operation.
5744 -- If the indicator is overriding, then the operator must match a
5745 -- predefined signature, because we know already that there is no
5746 -- explicit overridden operation.
5748 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5749 if Must_Not_Override (Spec) then
5751 -- If this is not a primitive or a protected subprogram, then
5752 -- "not overriding" is illegal.
5754 if not Is_Primitive
5755 and then Ekind (Scope (Subp)) /= E_Protected_Type
5756 then
5757 Error_Msg_N ("overriding indicator only allowed "
5758 & "if subprogram is primitive", Subp);
5760 elsif Can_Override_Operator (Subp) then
5761 Error_Msg_NE
5762 ("subprogram& overrides predefined operator ", Spec, Subp);
5763 end if;
5765 elsif Must_Override (Spec) then
5766 if No (Overridden_Operation (Subp))
5767 and then not Can_Override_Operator (Subp)
5768 then
5769 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5770 end if;
5772 elsif not Error_Posted (Subp)
5773 and then Style_Check
5774 and then Can_Override_Operator (Subp)
5775 and then
5776 not Is_Predefined_File_Name
5777 (Unit_File_Name (Get_Source_Unit (Subp)))
5778 then
5779 -- If style checks are enabled, indicate that the indicator is
5780 -- missing. However, at the point of declaration, the type of
5781 -- which this is a primitive operation may be private, in which
5782 -- case the indicator would be premature.
5784 if Has_Private_Declaration (Etype (Subp))
5785 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5786 then
5787 null;
5788 else
5789 Style.Missing_Overriding (Decl, Subp);
5790 end if;
5791 end if;
5793 elsif Must_Override (Spec) then
5794 if Ekind (Subp) = E_Entry then
5795 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5796 else
5797 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5798 end if;
5800 -- If the operation is marked "not overriding" and it's not primitive
5801 -- then an error is issued, unless this is an operation of a task or
5802 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5803 -- has been specified have already been checked above.
5805 elsif Must_Not_Override (Spec)
5806 and then not Is_Primitive
5807 and then Ekind (Subp) /= E_Entry
5808 and then Ekind (Scope (Subp)) /= E_Protected_Type
5809 then
5810 Error_Msg_N
5811 ("overriding indicator only allowed if subprogram is primitive",
5812 Subp);
5813 return;
5814 end if;
5815 end Check_Overriding_Indicator;
5817 -------------------
5818 -- Check_Returns --
5819 -------------------
5821 -- Note: this procedure needs to know far too much about how the expander
5822 -- messes with exceptions. The use of the flag Exception_Junk and the
5823 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5824 -- works, but is not very clean. It would be better if the expansion
5825 -- routines would leave Original_Node working nicely, and we could use
5826 -- Original_Node here to ignore all the peculiar expander messing ???
5828 procedure Check_Returns
5829 (HSS : Node_Id;
5830 Mode : Character;
5831 Err : out Boolean;
5832 Proc : Entity_Id := Empty)
5834 Handler : Node_Id;
5836 procedure Check_Statement_Sequence (L : List_Id);
5837 -- Internal recursive procedure to check a list of statements for proper
5838 -- termination by a return statement (or a transfer of control or a
5839 -- compound statement that is itself internally properly terminated).
5841 ------------------------------
5842 -- Check_Statement_Sequence --
5843 ------------------------------
5845 procedure Check_Statement_Sequence (L : List_Id) is
5846 Last_Stm : Node_Id;
5847 Stm : Node_Id;
5848 Kind : Node_Kind;
5850 function Assert_False return Boolean;
5851 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5852 -- rewritten as a null statement when assertions are off. The assert
5853 -- is not active, but it is still enough to kill the warning.
5855 ------------------
5856 -- Assert_False --
5857 ------------------
5859 function Assert_False return Boolean is
5860 Orig : constant Node_Id := Original_Node (Last_Stm);
5862 begin
5863 if Nkind (Orig) = N_Pragma
5864 and then Pragma_Name (Orig) = Name_Assert
5865 and then not Error_Posted (Orig)
5866 then
5867 declare
5868 Arg : constant Node_Id :=
5869 First (Pragma_Argument_Associations (Orig));
5870 Exp : constant Node_Id := Expression (Arg);
5871 begin
5872 return Nkind (Exp) = N_Identifier
5873 and then Chars (Exp) = Name_False;
5874 end;
5876 else
5877 return False;
5878 end if;
5879 end Assert_False;
5881 -- Local variables
5883 Raise_Exception_Call : Boolean;
5884 -- Set True if statement sequence terminated by Raise_Exception call
5885 -- or a Reraise_Occurrence call.
5887 -- Start of processing for Check_Statement_Sequence
5889 begin
5890 Raise_Exception_Call := False;
5892 -- Get last real statement
5894 Last_Stm := Last (L);
5896 -- Deal with digging out exception handler statement sequences that
5897 -- have been transformed by the local raise to goto optimization.
5898 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5899 -- optimization has occurred, we are looking at something like:
5901 -- begin
5902 -- original stmts in block
5904 -- exception \
5905 -- when excep1 => |
5906 -- goto L1; | omitted if No_Exception_Propagation
5907 -- when excep2 => |
5908 -- goto L2; /
5909 -- end;
5911 -- goto L3; -- skip handler when exception not raised
5913 -- <<L1>> -- target label for local exception
5914 -- begin
5915 -- estmts1
5916 -- end;
5918 -- goto L3;
5920 -- <<L2>>
5921 -- begin
5922 -- estmts2
5923 -- end;
5925 -- <<L3>>
5927 -- and what we have to do is to dig out the estmts1 and estmts2
5928 -- sequences (which were the original sequences of statements in
5929 -- the exception handlers) and check them.
5931 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
5932 Stm := Last_Stm;
5933 loop
5934 Prev (Stm);
5935 exit when No (Stm);
5936 exit when Nkind (Stm) /= N_Block_Statement;
5937 exit when not Exception_Junk (Stm);
5938 Prev (Stm);
5939 exit when No (Stm);
5940 exit when Nkind (Stm) /= N_Label;
5941 exit when not Exception_Junk (Stm);
5942 Check_Statement_Sequence
5943 (Statements (Handled_Statement_Sequence (Next (Stm))));
5945 Prev (Stm);
5946 Last_Stm := Stm;
5947 exit when No (Stm);
5948 exit when Nkind (Stm) /= N_Goto_Statement;
5949 exit when not Exception_Junk (Stm);
5950 end loop;
5951 end if;
5953 -- Don't count pragmas
5955 while Nkind (Last_Stm) = N_Pragma
5957 -- Don't count call to SS_Release (can happen after Raise_Exception)
5959 or else
5960 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5961 and then
5962 Nkind (Name (Last_Stm)) = N_Identifier
5963 and then
5964 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5966 -- Don't count exception junk
5968 or else
5969 (Nkind_In (Last_Stm, N_Goto_Statement,
5970 N_Label,
5971 N_Object_Declaration)
5972 and then Exception_Junk (Last_Stm))
5973 or else Nkind (Last_Stm) in N_Push_xxx_Label
5974 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5976 -- Inserted code, such as finalization calls, is irrelevant: we only
5977 -- need to check original source.
5979 or else Is_Rewrite_Insertion (Last_Stm)
5980 loop
5981 Prev (Last_Stm);
5982 end loop;
5984 -- Here we have the "real" last statement
5986 Kind := Nkind (Last_Stm);
5988 -- Transfer of control, OK. Note that in the No_Return procedure
5989 -- case, we already diagnosed any explicit return statements, so
5990 -- we can treat them as OK in this context.
5992 if Is_Transfer (Last_Stm) then
5993 return;
5995 -- Check cases of explicit non-indirect procedure calls
5997 elsif Kind = N_Procedure_Call_Statement
5998 and then Is_Entity_Name (Name (Last_Stm))
5999 then
6000 -- Check call to Raise_Exception procedure which is treated
6001 -- specially, as is a call to Reraise_Occurrence.
6003 -- We suppress the warning in these cases since it is likely that
6004 -- the programmer really does not expect to deal with the case
6005 -- of Null_Occurrence, and thus would find a warning about a
6006 -- missing return curious, and raising Program_Error does not
6007 -- seem such a bad behavior if this does occur.
6009 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6010 -- behavior will be to raise Constraint_Error (see AI-329).
6012 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6013 or else
6014 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6015 then
6016 Raise_Exception_Call := True;
6018 -- For Raise_Exception call, test first argument, if it is
6019 -- an attribute reference for a 'Identity call, then we know
6020 -- that the call cannot possibly return.
6022 declare
6023 Arg : constant Node_Id :=
6024 Original_Node (First_Actual (Last_Stm));
6025 begin
6026 if Nkind (Arg) = N_Attribute_Reference
6027 and then Attribute_Name (Arg) = Name_Identity
6028 then
6029 return;
6030 end if;
6031 end;
6032 end if;
6034 -- If statement, need to look inside if there is an else and check
6035 -- each constituent statement sequence for proper termination.
6037 elsif Kind = N_If_Statement
6038 and then Present (Else_Statements (Last_Stm))
6039 then
6040 Check_Statement_Sequence (Then_Statements (Last_Stm));
6041 Check_Statement_Sequence (Else_Statements (Last_Stm));
6043 if Present (Elsif_Parts (Last_Stm)) then
6044 declare
6045 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6047 begin
6048 while Present (Elsif_Part) loop
6049 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6050 Next (Elsif_Part);
6051 end loop;
6052 end;
6053 end if;
6055 return;
6057 -- Case statement, check each case for proper termination
6059 elsif Kind = N_Case_Statement then
6060 declare
6061 Case_Alt : Node_Id;
6062 begin
6063 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6064 while Present (Case_Alt) loop
6065 Check_Statement_Sequence (Statements (Case_Alt));
6066 Next_Non_Pragma (Case_Alt);
6067 end loop;
6068 end;
6070 return;
6072 -- Block statement, check its handled sequence of statements
6074 elsif Kind = N_Block_Statement then
6075 declare
6076 Err1 : Boolean;
6078 begin
6079 Check_Returns
6080 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6082 if Err1 then
6083 Err := True;
6084 end if;
6086 return;
6087 end;
6089 -- Loop statement. If there is an iteration scheme, we can definitely
6090 -- fall out of the loop. Similarly if there is an exit statement, we
6091 -- can fall out. In either case we need a following return.
6093 elsif Kind = N_Loop_Statement then
6094 if Present (Iteration_Scheme (Last_Stm))
6095 or else Has_Exit (Entity (Identifier (Last_Stm)))
6096 then
6097 null;
6099 -- A loop with no exit statement or iteration scheme is either
6100 -- an infinite loop, or it has some other exit (raise/return).
6101 -- In either case, no warning is required.
6103 else
6104 return;
6105 end if;
6107 -- Timed entry call, check entry call and delay alternatives
6109 -- Note: in expanded code, the timed entry call has been converted
6110 -- to a set of expanded statements on which the check will work
6111 -- correctly in any case.
6113 elsif Kind = N_Timed_Entry_Call then
6114 declare
6115 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6116 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6118 begin
6119 -- If statement sequence of entry call alternative is missing,
6120 -- then we can definitely fall through, and we post the error
6121 -- message on the entry call alternative itself.
6123 if No (Statements (ECA)) then
6124 Last_Stm := ECA;
6126 -- If statement sequence of delay alternative is missing, then
6127 -- we can definitely fall through, and we post the error
6128 -- message on the delay alternative itself.
6130 -- Note: if both ECA and DCA are missing the return, then we
6131 -- post only one message, should be enough to fix the bugs.
6132 -- If not we will get a message next time on the DCA when the
6133 -- ECA is fixed.
6135 elsif No (Statements (DCA)) then
6136 Last_Stm := DCA;
6138 -- Else check both statement sequences
6140 else
6141 Check_Statement_Sequence (Statements (ECA));
6142 Check_Statement_Sequence (Statements (DCA));
6143 return;
6144 end if;
6145 end;
6147 -- Conditional entry call, check entry call and else part
6149 -- Note: in expanded code, the conditional entry call has been
6150 -- converted to a set of expanded statements on which the check
6151 -- will work correctly in any case.
6153 elsif Kind = N_Conditional_Entry_Call then
6154 declare
6155 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6157 begin
6158 -- If statement sequence of entry call alternative is missing,
6159 -- then we can definitely fall through, and we post the error
6160 -- message on the entry call alternative itself.
6162 if No (Statements (ECA)) then
6163 Last_Stm := ECA;
6165 -- Else check statement sequence and else part
6167 else
6168 Check_Statement_Sequence (Statements (ECA));
6169 Check_Statement_Sequence (Else_Statements (Last_Stm));
6170 return;
6171 end if;
6172 end;
6173 end if;
6175 -- If we fall through, issue appropriate message
6177 if Mode = 'F' then
6179 -- Kill warning if last statement is a raise exception call,
6180 -- or a pragma Assert (False). Note that with assertions enabled,
6181 -- such a pragma has been converted into a raise exception call
6182 -- already, so the Assert_False is for the assertions off case.
6184 if not Raise_Exception_Call and then not Assert_False then
6186 -- In GNATprove mode, it is an error to have a missing return
6188 Error_Msg_Warn := SPARK_Mode /= On;
6190 -- Issue error message or warning
6192 Error_Msg_N
6193 ("RETURN statement missing following this statement<<!",
6194 Last_Stm);
6195 Error_Msg_N
6196 ("\Program_Error ]<<!", Last_Stm);
6197 end if;
6199 -- Note: we set Err even though we have not issued a warning
6200 -- because we still have a case of a missing return. This is
6201 -- an extremely marginal case, probably will never be noticed
6202 -- but we might as well get it right.
6204 Err := True;
6206 -- Otherwise we have the case of a procedure marked No_Return
6208 else
6209 if not Raise_Exception_Call then
6210 if GNATprove_Mode then
6211 Error_Msg_N
6212 ("implied return after this statement "
6213 & "would have raised Program_Error", Last_Stm);
6214 else
6215 Error_Msg_N
6216 ("implied return after this statement "
6217 & "will raise Program_Error??", Last_Stm);
6218 end if;
6220 Error_Msg_Warn := SPARK_Mode /= On;
6221 Error_Msg_NE
6222 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6223 end if;
6225 declare
6226 RE : constant Node_Id :=
6227 Make_Raise_Program_Error (Sloc (Last_Stm),
6228 Reason => PE_Implicit_Return);
6229 begin
6230 Insert_After (Last_Stm, RE);
6231 Analyze (RE);
6232 end;
6233 end if;
6234 end Check_Statement_Sequence;
6236 -- Start of processing for Check_Returns
6238 begin
6239 Err := False;
6240 Check_Statement_Sequence (Statements (HSS));
6242 if Present (Exception_Handlers (HSS)) then
6243 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6244 while Present (Handler) loop
6245 Check_Statement_Sequence (Statements (Handler));
6246 Next_Non_Pragma (Handler);
6247 end loop;
6248 end if;
6249 end Check_Returns;
6251 ----------------------------
6252 -- Check_Subprogram_Order --
6253 ----------------------------
6255 procedure Check_Subprogram_Order (N : Node_Id) is
6257 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6258 -- This is used to check if S1 > S2 in the sense required by this test,
6259 -- for example nameab < namec, but name2 < name10.
6261 -----------------------------
6262 -- Subprogram_Name_Greater --
6263 -----------------------------
6265 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6266 L1, L2 : Positive;
6267 N1, N2 : Natural;
6269 begin
6270 -- Deal with special case where names are identical except for a
6271 -- numerical suffix. These are handled specially, taking the numeric
6272 -- ordering from the suffix into account.
6274 L1 := S1'Last;
6275 while S1 (L1) in '0' .. '9' loop
6276 L1 := L1 - 1;
6277 end loop;
6279 L2 := S2'Last;
6280 while S2 (L2) in '0' .. '9' loop
6281 L2 := L2 - 1;
6282 end loop;
6284 -- If non-numeric parts non-equal, do straight compare
6286 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6287 return S1 > S2;
6289 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6290 -- that a missing suffix is treated as numeric zero in this test.
6292 else
6293 N1 := 0;
6294 while L1 < S1'Last loop
6295 L1 := L1 + 1;
6296 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6297 end loop;
6299 N2 := 0;
6300 while L2 < S2'Last loop
6301 L2 := L2 + 1;
6302 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6303 end loop;
6305 return N1 > N2;
6306 end if;
6307 end Subprogram_Name_Greater;
6309 -- Start of processing for Check_Subprogram_Order
6311 begin
6312 -- Check body in alpha order if this is option
6314 if Style_Check
6315 and then Style_Check_Order_Subprograms
6316 and then Nkind (N) = N_Subprogram_Body
6317 and then Comes_From_Source (N)
6318 and then In_Extended_Main_Source_Unit (N)
6319 then
6320 declare
6321 LSN : String_Ptr
6322 renames Scope_Stack.Table
6323 (Scope_Stack.Last).Last_Subprogram_Name;
6325 Body_Id : constant Entity_Id :=
6326 Defining_Entity (Specification (N));
6328 begin
6329 Get_Decoded_Name_String (Chars (Body_Id));
6331 if LSN /= null then
6332 if Subprogram_Name_Greater
6333 (LSN.all, Name_Buffer (1 .. Name_Len))
6334 then
6335 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6336 end if;
6338 Free (LSN);
6339 end if;
6341 LSN := new String'(Name_Buffer (1 .. Name_Len));
6342 end;
6343 end if;
6344 end Check_Subprogram_Order;
6346 ------------------------------
6347 -- Check_Subtype_Conformant --
6348 ------------------------------
6350 procedure Check_Subtype_Conformant
6351 (New_Id : Entity_Id;
6352 Old_Id : Entity_Id;
6353 Err_Loc : Node_Id := Empty;
6354 Skip_Controlling_Formals : Boolean := False;
6355 Get_Inst : Boolean := False)
6357 Result : Boolean;
6358 pragma Warnings (Off, Result);
6359 begin
6360 Check_Conformance
6361 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6362 Skip_Controlling_Formals => Skip_Controlling_Formals,
6363 Get_Inst => Get_Inst);
6364 end Check_Subtype_Conformant;
6366 ---------------------------
6367 -- Check_Type_Conformant --
6368 ---------------------------
6370 procedure Check_Type_Conformant
6371 (New_Id : Entity_Id;
6372 Old_Id : Entity_Id;
6373 Err_Loc : Node_Id := Empty)
6375 Result : Boolean;
6376 pragma Warnings (Off, Result);
6377 begin
6378 Check_Conformance
6379 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6380 end Check_Type_Conformant;
6382 ---------------------------
6383 -- Can_Override_Operator --
6384 ---------------------------
6386 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6387 Typ : Entity_Id;
6389 begin
6390 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6391 return False;
6393 else
6394 Typ := Base_Type (Etype (First_Formal (Subp)));
6396 -- Check explicitly that the operation is a primitive of the type
6398 return Operator_Matches_Spec (Subp, Subp)
6399 and then not Is_Generic_Type (Typ)
6400 and then Scope (Subp) = Scope (Typ)
6401 and then not Is_Class_Wide_Type (Typ);
6402 end if;
6403 end Can_Override_Operator;
6405 ----------------------
6406 -- Conforming_Types --
6407 ----------------------
6409 function Conforming_Types
6410 (T1 : Entity_Id;
6411 T2 : Entity_Id;
6412 Ctype : Conformance_Type;
6413 Get_Inst : Boolean := False) return Boolean
6415 Type_1 : Entity_Id := T1;
6416 Type_2 : Entity_Id := T2;
6417 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6419 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6420 -- If neither T1 nor T2 are generic actual types, or if they are in
6421 -- different scopes (e.g. parent and child instances), then verify that
6422 -- the base types are equal. Otherwise T1 and T2 must be on the same
6423 -- subtype chain. The whole purpose of this procedure is to prevent
6424 -- spurious ambiguities in an instantiation that may arise if two
6425 -- distinct generic types are instantiated with the same actual.
6427 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6428 -- An access parameter can designate an incomplete type. If the
6429 -- incomplete type is the limited view of a type from a limited_
6430 -- with_clause, check whether the non-limited view is available. If
6431 -- it is a (non-limited) incomplete type, get the full view.
6433 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6434 -- Returns True if and only if either T1 denotes a limited view of T2
6435 -- or T2 denotes a limited view of T1. This can arise when the limited
6436 -- with view of a type is used in a subprogram declaration and the
6437 -- subprogram body is in the scope of a regular with clause for the
6438 -- same unit. In such a case, the two type entities can be considered
6439 -- identical for purposes of conformance checking.
6441 ----------------------
6442 -- Base_Types_Match --
6443 ----------------------
6445 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6446 BT1 : constant Entity_Id := Base_Type (T1);
6447 BT2 : constant Entity_Id := Base_Type (T2);
6449 begin
6450 if T1 = T2 then
6451 return True;
6453 elsif BT1 = BT2 then
6455 -- The following is too permissive. A more precise test should
6456 -- check that the generic actual is an ancestor subtype of the
6457 -- other ???.
6459 -- See code in Find_Corresponding_Spec that applies an additional
6460 -- filter to handle accidental amiguities in instances.
6462 return not Is_Generic_Actual_Type (T1)
6463 or else not Is_Generic_Actual_Type (T2)
6464 or else Scope (T1) /= Scope (T2);
6466 -- If T2 is a generic actual type it is declared as the subtype of
6467 -- the actual. If that actual is itself a subtype we need to use its
6468 -- own base type to check for compatibility.
6470 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6471 return True;
6473 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6474 return True;
6476 else
6477 return False;
6478 end if;
6479 end Base_Types_Match;
6481 --------------------------
6482 -- Find_Designated_Type --
6483 --------------------------
6485 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6486 Desig : Entity_Id;
6488 begin
6489 Desig := Directly_Designated_Type (T);
6491 if Ekind (Desig) = E_Incomplete_Type then
6493 -- If regular incomplete type, get full view if available
6495 if Present (Full_View (Desig)) then
6496 Desig := Full_View (Desig);
6498 -- If limited view of a type, get non-limited view if available,
6499 -- and check again for a regular incomplete type.
6501 elsif Present (Non_Limited_View (Desig)) then
6502 Desig := Get_Full_View (Non_Limited_View (Desig));
6503 end if;
6504 end if;
6506 return Desig;
6507 end Find_Designated_Type;
6509 -------------------------------
6510 -- Matches_Limited_With_View --
6511 -------------------------------
6513 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6514 begin
6515 -- In some cases a type imported through a limited_with clause, and
6516 -- its nonlimited view are both visible, for example in an anonymous
6517 -- access-to-class-wide type in a formal, or when building the body
6518 -- for a subprogram renaming after the subprogram has been frozen.
6519 -- In these cases Both entities designate the same type. In addition,
6520 -- if one of them is an actual in an instance, it may be a subtype of
6521 -- the non-limited view of the other.
6523 if From_Limited_With (T1)
6524 and then (T2 = Available_View (T1)
6525 or else Is_Subtype_Of (T2, Available_View (T1)))
6526 then
6527 return True;
6529 elsif From_Limited_With (T2)
6530 and then (T1 = Available_View (T2)
6531 or else Is_Subtype_Of (T1, Available_View (T2)))
6532 then
6533 return True;
6535 elsif From_Limited_With (T1)
6536 and then From_Limited_With (T2)
6537 and then Available_View (T1) = Available_View (T2)
6538 then
6539 return True;
6541 else
6542 return False;
6543 end if;
6544 end Matches_Limited_With_View;
6546 -- Start of processing for Conforming_Types
6548 begin
6549 -- The context is an instance association for a formal access-to-
6550 -- subprogram type; the formal parameter types require mapping because
6551 -- they may denote other formal parameters of the generic unit.
6553 if Get_Inst then
6554 Type_1 := Get_Instance_Of (T1);
6555 Type_2 := Get_Instance_Of (T2);
6556 end if;
6558 -- If one of the types is a view of the other introduced by a limited
6559 -- with clause, treat these as conforming for all purposes.
6561 if Matches_Limited_With_View (T1, T2) then
6562 return True;
6564 elsif Base_Types_Match (Type_1, Type_2) then
6565 return Ctype <= Mode_Conformant
6566 or else Subtypes_Statically_Match (Type_1, Type_2);
6568 elsif Is_Incomplete_Or_Private_Type (Type_1)
6569 and then Present (Full_View (Type_1))
6570 and then Base_Types_Match (Full_View (Type_1), Type_2)
6571 then
6572 return Ctype <= Mode_Conformant
6573 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6575 elsif Ekind (Type_2) = E_Incomplete_Type
6576 and then Present (Full_View (Type_2))
6577 and then Base_Types_Match (Type_1, Full_View (Type_2))
6578 then
6579 return Ctype <= Mode_Conformant
6580 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6582 elsif Is_Private_Type (Type_2)
6583 and then In_Instance
6584 and then Present (Full_View (Type_2))
6585 and then Base_Types_Match (Type_1, Full_View (Type_2))
6586 then
6587 return Ctype <= Mode_Conformant
6588 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6590 -- In Ada 2012, incomplete types (including limited views) can appear
6591 -- as actuals in instantiations.
6593 elsif Is_Incomplete_Type (Type_1)
6594 and then Is_Incomplete_Type (Type_2)
6595 and then (Used_As_Generic_Actual (Type_1)
6596 or else Used_As_Generic_Actual (Type_2))
6597 then
6598 return True;
6599 end if;
6601 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6602 -- treated recursively because they carry a signature. As far as
6603 -- conformance is concerned, convention plays no role, and either
6604 -- or both could be access to protected subprograms.
6606 Are_Anonymous_Access_To_Subprogram_Types :=
6607 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6608 E_Anonymous_Access_Protected_Subprogram_Type)
6609 and then
6610 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6611 E_Anonymous_Access_Protected_Subprogram_Type);
6613 -- Test anonymous access type case. For this case, static subtype
6614 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6615 -- the base types because we may have built internal subtype entities
6616 -- to handle null-excluding types (see Process_Formals).
6618 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6619 and then
6620 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6622 -- Ada 2005 (AI-254)
6624 or else Are_Anonymous_Access_To_Subprogram_Types
6625 then
6626 declare
6627 Desig_1 : Entity_Id;
6628 Desig_2 : Entity_Id;
6630 begin
6631 -- In Ada 2005, access constant indicators must match for
6632 -- subtype conformance.
6634 if Ada_Version >= Ada_2005
6635 and then Ctype >= Subtype_Conformant
6636 and then
6637 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6638 then
6639 return False;
6640 end if;
6642 Desig_1 := Find_Designated_Type (Type_1);
6643 Desig_2 := Find_Designated_Type (Type_2);
6645 -- If the context is an instance association for a formal
6646 -- access-to-subprogram type; formal access parameter designated
6647 -- types require mapping because they may denote other formal
6648 -- parameters of the generic unit.
6650 if Get_Inst then
6651 Desig_1 := Get_Instance_Of (Desig_1);
6652 Desig_2 := Get_Instance_Of (Desig_2);
6653 end if;
6655 -- It is possible for a Class_Wide_Type to be introduced for an
6656 -- incomplete type, in which case there is a separate class_ wide
6657 -- type for the full view. The types conform if their Etypes
6658 -- conform, i.e. one may be the full view of the other. This can
6659 -- only happen in the context of an access parameter, other uses
6660 -- of an incomplete Class_Wide_Type are illegal.
6662 if Is_Class_Wide_Type (Desig_1)
6663 and then
6664 Is_Class_Wide_Type (Desig_2)
6665 then
6666 return
6667 Conforming_Types
6668 (Etype (Base_Type (Desig_1)),
6669 Etype (Base_Type (Desig_2)), Ctype);
6671 elsif Are_Anonymous_Access_To_Subprogram_Types then
6672 if Ada_Version < Ada_2005 then
6673 return Ctype = Type_Conformant
6674 or else
6675 Subtypes_Statically_Match (Desig_1, Desig_2);
6677 -- We must check the conformance of the signatures themselves
6679 else
6680 declare
6681 Conformant : Boolean;
6682 begin
6683 Check_Conformance
6684 (Desig_1, Desig_2, Ctype, False, Conformant);
6685 return Conformant;
6686 end;
6687 end if;
6689 -- A limited view of an actual matches the corresponding
6690 -- incomplete formal.
6692 elsif Ekind (Desig_2) = E_Incomplete_Subtype
6693 and then From_Limited_With (Desig_2)
6694 and then Used_As_Generic_Actual (Etype (Desig_2))
6695 then
6696 return True;
6698 else
6699 return Base_Type (Desig_1) = Base_Type (Desig_2)
6700 and then (Ctype = Type_Conformant
6701 or else
6702 Subtypes_Statically_Match (Desig_1, Desig_2));
6703 end if;
6704 end;
6706 -- Otherwise definitely no match
6708 else
6709 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6710 and then Is_Access_Type (Type_2))
6711 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6712 and then Is_Access_Type (Type_1)))
6713 and then
6714 Conforming_Types
6715 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6716 then
6717 May_Hide_Profile := True;
6718 end if;
6720 return False;
6721 end if;
6722 end Conforming_Types;
6724 --------------------------
6725 -- Create_Extra_Formals --
6726 --------------------------
6728 procedure Create_Extra_Formals (E : Entity_Id) is
6729 Formal : Entity_Id;
6730 First_Extra : Entity_Id := Empty;
6731 Last_Extra : Entity_Id;
6732 Formal_Type : Entity_Id;
6733 P_Formal : Entity_Id := Empty;
6735 function Add_Extra_Formal
6736 (Assoc_Entity : Entity_Id;
6737 Typ : Entity_Id;
6738 Scope : Entity_Id;
6739 Suffix : String) return Entity_Id;
6740 -- Add an extra formal to the current list of formals and extra formals.
6741 -- The extra formal is added to the end of the list of extra formals,
6742 -- and also returned as the result. These formals are always of mode IN.
6743 -- The new formal has the type Typ, is declared in Scope, and its name
6744 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6745 -- The following suffixes are currently used. They should not be changed
6746 -- without coordinating with CodePeer, which makes use of these to
6747 -- provide better messages.
6749 -- O denotes the Constrained bit.
6750 -- L denotes the accessibility level.
6751 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6752 -- the full list in exp_ch6.BIP_Formal_Kind.
6754 ----------------------
6755 -- Add_Extra_Formal --
6756 ----------------------
6758 function Add_Extra_Formal
6759 (Assoc_Entity : Entity_Id;
6760 Typ : Entity_Id;
6761 Scope : Entity_Id;
6762 Suffix : String) return Entity_Id
6764 EF : constant Entity_Id :=
6765 Make_Defining_Identifier (Sloc (Assoc_Entity),
6766 Chars => New_External_Name (Chars (Assoc_Entity),
6767 Suffix => Suffix));
6769 begin
6770 -- A little optimization. Never generate an extra formal for the
6771 -- _init operand of an initialization procedure, since it could
6772 -- never be used.
6774 if Chars (Formal) = Name_uInit then
6775 return Empty;
6776 end if;
6778 Set_Ekind (EF, E_In_Parameter);
6779 Set_Actual_Subtype (EF, Typ);
6780 Set_Etype (EF, Typ);
6781 Set_Scope (EF, Scope);
6782 Set_Mechanism (EF, Default_Mechanism);
6783 Set_Formal_Validity (EF);
6785 if No (First_Extra) then
6786 First_Extra := EF;
6787 Set_Extra_Formals (Scope, First_Extra);
6788 end if;
6790 if Present (Last_Extra) then
6791 Set_Extra_Formal (Last_Extra, EF);
6792 end if;
6794 Last_Extra := EF;
6796 return EF;
6797 end Add_Extra_Formal;
6799 -- Start of processing for Create_Extra_Formals
6801 begin
6802 -- We never generate extra formals if expansion is not active because we
6803 -- don't need them unless we are generating code.
6805 if not Expander_Active then
6806 return;
6807 end if;
6809 -- No need to generate extra formals in interface thunks whose target
6810 -- primitive has no extra formals.
6812 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6813 return;
6814 end if;
6816 -- If this is a derived subprogram then the subtypes of the parent
6817 -- subprogram's formal parameters will be used to determine the need
6818 -- for extra formals.
6820 if Is_Overloadable (E) and then Present (Alias (E)) then
6821 P_Formal := First_Formal (Alias (E));
6822 end if;
6824 Last_Extra := Empty;
6825 Formal := First_Formal (E);
6826 while Present (Formal) loop
6827 Last_Extra := Formal;
6828 Next_Formal (Formal);
6829 end loop;
6831 -- If Extra_formals were already created, don't do it again. This
6832 -- situation may arise for subprogram types created as part of
6833 -- dispatching calls (see Expand_Dispatching_Call)
6835 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6836 return;
6837 end if;
6839 -- If the subprogram is a predefined dispatching subprogram then don't
6840 -- generate any extra constrained or accessibility level formals. In
6841 -- general we suppress these for internal subprograms (by not calling
6842 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6843 -- generated stream attributes do get passed through because extra
6844 -- build-in-place formals are needed in some cases (limited 'Input).
6846 if Is_Predefined_Internal_Operation (E) then
6847 goto Test_For_Func_Result_Extras;
6848 end if;
6850 Formal := First_Formal (E);
6851 while Present (Formal) loop
6853 -- Create extra formal for supporting the attribute 'Constrained.
6854 -- The case of a private type view without discriminants also
6855 -- requires the extra formal if the underlying type has defaulted
6856 -- discriminants.
6858 if Ekind (Formal) /= E_In_Parameter then
6859 if Present (P_Formal) then
6860 Formal_Type := Etype (P_Formal);
6861 else
6862 Formal_Type := Etype (Formal);
6863 end if;
6865 -- Do not produce extra formals for Unchecked_Union parameters.
6866 -- Jump directly to the end of the loop.
6868 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6869 goto Skip_Extra_Formal_Generation;
6870 end if;
6872 if not Has_Discriminants (Formal_Type)
6873 and then Ekind (Formal_Type) in Private_Kind
6874 and then Present (Underlying_Type (Formal_Type))
6875 then
6876 Formal_Type := Underlying_Type (Formal_Type);
6877 end if;
6879 -- Suppress the extra formal if formal's subtype is constrained or
6880 -- indefinite, or we're compiling for Ada 2012 and the underlying
6881 -- type is tagged and limited. In Ada 2012, a limited tagged type
6882 -- can have defaulted discriminants, but 'Constrained is required
6883 -- to return True, so the formal is never needed (see AI05-0214).
6884 -- Note that this ensures consistency of calling sequences for
6885 -- dispatching operations when some types in a class have defaults
6886 -- on discriminants and others do not (and requiring the extra
6887 -- formal would introduce distributed overhead).
6889 -- If the type does not have a completion yet, treat as prior to
6890 -- Ada 2012 for consistency.
6892 if Has_Discriminants (Formal_Type)
6893 and then not Is_Constrained (Formal_Type)
6894 and then Is_Definite_Subtype (Formal_Type)
6895 and then (Ada_Version < Ada_2012
6896 or else No (Underlying_Type (Formal_Type))
6897 or else not
6898 (Is_Limited_Type (Formal_Type)
6899 and then
6900 (Is_Tagged_Type
6901 (Underlying_Type (Formal_Type)))))
6902 then
6903 Set_Extra_Constrained
6904 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6905 end if;
6906 end if;
6908 -- Create extra formal for supporting accessibility checking. This
6909 -- is done for both anonymous access formals and formals of named
6910 -- access types that are marked as controlling formals. The latter
6911 -- case can occur when Expand_Dispatching_Call creates a subprogram
6912 -- type and substitutes the types of access-to-class-wide actuals
6913 -- for the anonymous access-to-specific-type of controlling formals.
6914 -- Base_Type is applied because in cases where there is a null
6915 -- exclusion the formal may have an access subtype.
6917 -- This is suppressed if we specifically suppress accessibility
6918 -- checks at the package level for either the subprogram, or the
6919 -- package in which it resides. However, we do not suppress it
6920 -- simply if the scope has accessibility checks suppressed, since
6921 -- this could cause trouble when clients are compiled with a
6922 -- different suppression setting. The explicit checks at the
6923 -- package level are safe from this point of view.
6925 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6926 or else (Is_Controlling_Formal (Formal)
6927 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6928 and then not
6929 (Explicit_Suppress (E, Accessibility_Check)
6930 or else
6931 Explicit_Suppress (Scope (E), Accessibility_Check))
6932 and then
6933 (No (P_Formal)
6934 or else Present (Extra_Accessibility (P_Formal)))
6935 then
6936 Set_Extra_Accessibility
6937 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6938 end if;
6940 -- This label is required when skipping extra formal generation for
6941 -- Unchecked_Union parameters.
6943 <<Skip_Extra_Formal_Generation>>
6945 if Present (P_Formal) then
6946 Next_Formal (P_Formal);
6947 end if;
6949 Next_Formal (Formal);
6950 end loop;
6952 <<Test_For_Func_Result_Extras>>
6954 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6955 -- function call is ... determined by the point of call ...".
6957 if Needs_Result_Accessibility_Level (E) then
6958 Set_Extra_Accessibility_Of_Result
6959 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6960 end if;
6962 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6963 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6965 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6966 declare
6967 Result_Subt : constant Entity_Id := Etype (E);
6968 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
6969 Formal_Typ : Entity_Id;
6971 Discard : Entity_Id;
6972 pragma Warnings (Off, Discard);
6974 begin
6975 -- In the case of functions with unconstrained result subtypes,
6976 -- add a 4-state formal indicating whether the return object is
6977 -- allocated by the caller (1), or should be allocated by the
6978 -- callee on the secondary stack (2), in the global heap (3), or
6979 -- in a user-defined storage pool (4). For the moment we just use
6980 -- Natural for the type of this formal. Note that this formal
6981 -- isn't usually needed in the case where the result subtype is
6982 -- constrained, but it is needed when the function has a tagged
6983 -- result, because generally such functions can be called in a
6984 -- dispatching context and such calls must be handled like calls
6985 -- to a class-wide function.
6987 if Needs_BIP_Alloc_Form (E) then
6988 Discard :=
6989 Add_Extra_Formal
6990 (E, Standard_Natural,
6991 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6993 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
6994 -- use a user-defined pool. This formal is not added on
6995 -- .NET/JVM/ZFP as those targets do not support pools.
6997 if VM_Target = No_VM
6998 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6999 then
7000 Discard :=
7001 Add_Extra_Formal
7002 (E, RTE (RE_Root_Storage_Pool_Ptr),
7003 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7004 end if;
7005 end if;
7007 -- In the case of functions whose result type needs finalization,
7008 -- add an extra formal which represents the finalization master.
7010 if Needs_BIP_Finalization_Master (E) then
7011 Discard :=
7012 Add_Extra_Formal
7013 (E, RTE (RE_Finalization_Master_Ptr),
7014 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7015 end if;
7017 -- When the result type contains tasks, add two extra formals: the
7018 -- master of the tasks to be created, and the caller's activation
7019 -- chain.
7021 if Has_Task (Full_Subt) then
7022 Discard :=
7023 Add_Extra_Formal
7024 (E, RTE (RE_Master_Id),
7025 E, BIP_Formal_Suffix (BIP_Task_Master));
7026 Discard :=
7027 Add_Extra_Formal
7028 (E, RTE (RE_Activation_Chain_Access),
7029 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7030 end if;
7032 -- All build-in-place functions get an extra formal that will be
7033 -- passed the address of the return object within the caller.
7035 Formal_Typ :=
7036 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7038 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7039 Set_Etype (Formal_Typ, Formal_Typ);
7040 Set_Depends_On_Private
7041 (Formal_Typ, Has_Private_Component (Formal_Typ));
7042 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7043 Set_Is_Access_Constant (Formal_Typ, False);
7045 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7046 -- the designated type comes from the limited view (for back-end
7047 -- purposes).
7049 Set_From_Limited_With
7050 (Formal_Typ, From_Limited_With (Result_Subt));
7052 Layout_Type (Formal_Typ);
7054 Discard :=
7055 Add_Extra_Formal
7056 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7057 end;
7058 end if;
7059 end Create_Extra_Formals;
7061 -----------------------------
7062 -- Enter_Overloaded_Entity --
7063 -----------------------------
7065 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7066 E : Entity_Id := Current_Entity_In_Scope (S);
7067 C_E : Entity_Id := Current_Entity (S);
7069 begin
7070 if Present (E) then
7071 Set_Has_Homonym (E);
7072 Set_Has_Homonym (S);
7073 end if;
7075 Set_Is_Immediately_Visible (S);
7076 Set_Scope (S, Current_Scope);
7078 -- Chain new entity if front of homonym in current scope, so that
7079 -- homonyms are contiguous.
7081 if Present (E) and then E /= C_E then
7082 while Homonym (C_E) /= E loop
7083 C_E := Homonym (C_E);
7084 end loop;
7086 Set_Homonym (C_E, S);
7088 else
7089 E := C_E;
7090 Set_Current_Entity (S);
7091 end if;
7093 Set_Homonym (S, E);
7095 if Is_Inherited_Operation (S) then
7096 Append_Inherited_Subprogram (S);
7097 else
7098 Append_Entity (S, Current_Scope);
7099 end if;
7101 Set_Public_Status (S);
7103 if Debug_Flag_E then
7104 Write_Str ("New overloaded entity chain: ");
7105 Write_Name (Chars (S));
7107 E := S;
7108 while Present (E) loop
7109 Write_Str (" "); Write_Int (Int (E));
7110 E := Homonym (E);
7111 end loop;
7113 Write_Eol;
7114 end if;
7116 -- Generate warning for hiding
7118 if Warn_On_Hiding
7119 and then Comes_From_Source (S)
7120 and then In_Extended_Main_Source_Unit (S)
7121 then
7122 E := S;
7123 loop
7124 E := Homonym (E);
7125 exit when No (E);
7127 -- Warn unless genuine overloading. Do not emit warning on
7128 -- hiding predefined operators in Standard (these are either an
7129 -- (artifact of our implicit declarations, or simple noise) but
7130 -- keep warning on a operator defined on a local subtype, because
7131 -- of the real danger that different operators may be applied in
7132 -- various parts of the program.
7134 -- Note that if E and S have the same scope, there is never any
7135 -- hiding. Either the two conflict, and the program is illegal,
7136 -- or S is overriding an implicit inherited subprogram.
7138 if Scope (E) /= Scope (S)
7139 and then (not Is_Overloadable (E)
7140 or else Subtype_Conformant (E, S))
7141 and then (Is_Immediately_Visible (E)
7142 or else
7143 Is_Potentially_Use_Visible (S))
7144 then
7145 if Scope (E) /= Standard_Standard then
7146 Error_Msg_Sloc := Sloc (E);
7147 Error_Msg_N ("declaration of & hides one #?h?", S);
7149 elsif Nkind (S) = N_Defining_Operator_Symbol
7150 and then
7151 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7152 then
7153 Error_Msg_N
7154 ("declaration of & hides predefined operator?h?", S);
7155 end if;
7156 end if;
7157 end loop;
7158 end if;
7159 end Enter_Overloaded_Entity;
7161 -----------------------------
7162 -- Check_Untagged_Equality --
7163 -----------------------------
7165 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7166 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7167 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7168 Obj_Decl : Node_Id;
7170 begin
7171 -- This check applies only if we have a subprogram declaration with an
7172 -- untagged record type.
7174 if Nkind (Decl) /= N_Subprogram_Declaration
7175 or else not Is_Record_Type (Typ)
7176 or else Is_Tagged_Type (Typ)
7177 then
7178 return;
7179 end if;
7181 -- In Ada 2012 case, we will output errors or warnings depending on
7182 -- the setting of debug flag -gnatd.E.
7184 if Ada_Version >= Ada_2012 then
7185 Error_Msg_Warn := Debug_Flag_Dot_EE;
7187 -- In earlier versions of Ada, nothing to do unless we are warning on
7188 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7190 else
7191 if not Warn_On_Ada_2012_Compatibility then
7192 return;
7193 end if;
7194 end if;
7196 -- Cases where the type has already been frozen
7198 if Is_Frozen (Typ) then
7200 -- If the type is not declared in a package, or if we are in the body
7201 -- of the package or in some other scope, the new operation is not
7202 -- primitive, and therefore legal, though suspicious. Should we
7203 -- generate a warning in this case ???
7205 if Ekind (Scope (Typ)) /= E_Package
7206 or else Scope (Typ) /= Current_Scope
7207 then
7208 return;
7210 -- If the type is a generic actual (sub)type, the operation is not
7211 -- primitive either because the base type is declared elsewhere.
7213 elsif Is_Generic_Actual_Type (Typ) then
7214 return;
7216 -- Here we have a definite error of declaration after freezing
7218 else
7219 if Ada_Version >= Ada_2012 then
7220 Error_Msg_NE
7221 ("equality operator must be declared before type & is "
7222 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7224 -- In Ada 2012 mode with error turned to warning, output one
7225 -- more warning to warn that the equality operation may not
7226 -- compose. This is the consequence of ignoring the error.
7228 if Error_Msg_Warn then
7229 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7230 end if;
7232 else
7233 Error_Msg_NE
7234 ("equality operator must be declared before type& is "
7235 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7236 end if;
7238 -- If we are in the package body, we could just move the
7239 -- declaration to the package spec, so add a message saying that.
7241 if In_Package_Body (Scope (Typ)) then
7242 if Ada_Version >= Ada_2012 then
7243 Error_Msg_N
7244 ("\move declaration to package spec<<", Eq_Op);
7245 else
7246 Error_Msg_N
7247 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7248 end if;
7250 -- Otherwise try to find the freezing point
7252 else
7253 Obj_Decl := Next (Parent (Typ));
7254 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7255 if Nkind (Obj_Decl) = N_Object_Declaration
7256 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7257 then
7258 -- Freezing point, output warnings
7260 if Ada_Version >= Ada_2012 then
7261 Error_Msg_NE
7262 ("type& is frozen by declaration??", Obj_Decl, Typ);
7263 Error_Msg_N
7264 ("\an equality operator cannot be declared after "
7265 & "this point??",
7266 Obj_Decl);
7267 else
7268 Error_Msg_NE
7269 ("type& is frozen by declaration (Ada 2012)?y?",
7270 Obj_Decl, Typ);
7271 Error_Msg_N
7272 ("\an equality operator cannot be declared after "
7273 & "this point (Ada 2012)?y?",
7274 Obj_Decl);
7275 end if;
7277 exit;
7278 end if;
7280 Next (Obj_Decl);
7281 end loop;
7282 end if;
7283 end if;
7285 -- Here if type is not frozen yet. It is illegal to have a primitive
7286 -- equality declared in the private part if the type is visible.
7288 elsif not In_Same_List (Parent (Typ), Decl)
7289 and then not Is_Limited_Type (Typ)
7290 then
7291 -- Shouldn't we give an RM reference here???
7293 if Ada_Version >= Ada_2012 then
7294 Error_Msg_N
7295 ("equality operator appears too late<<", Eq_Op);
7296 else
7297 Error_Msg_N
7298 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7299 end if;
7301 -- No error detected
7303 else
7304 return;
7305 end if;
7306 end Check_Untagged_Equality;
7308 -----------------------------
7309 -- Find_Corresponding_Spec --
7310 -----------------------------
7312 function Find_Corresponding_Spec
7313 (N : Node_Id;
7314 Post_Error : Boolean := True) return Entity_Id
7316 Spec : constant Node_Id := Specification (N);
7317 Designator : constant Entity_Id := Defining_Entity (Spec);
7319 E : Entity_Id;
7321 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7322 -- Even if fully conformant, a body may depend on a generic actual when
7323 -- the spec does not, or vice versa, in which case they were distinct
7324 -- entities in the generic.
7326 -------------------------------
7327 -- Different_Generic_Profile --
7328 -------------------------------
7330 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7331 F1, F2 : Entity_Id;
7333 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7334 -- Check that the types of corresponding formals have the same
7335 -- generic actual if any. We have to account for subtypes of a
7336 -- generic formal, declared between a spec and a body, which may
7337 -- appear distinct in an instance but matched in the generic, and
7338 -- the subtype may be used either in the spec or the body of the
7339 -- subprogram being checked.
7341 -------------------------
7342 -- Same_Generic_Actual --
7343 -------------------------
7345 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7347 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7348 -- Predicate to check whether S1 is a subtype of S2 in the source
7349 -- of the instance.
7351 -------------------------
7352 -- Is_Declared_Subtype --
7353 -------------------------
7355 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7356 begin
7357 return Comes_From_Source (Parent (S1))
7358 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7359 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7360 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7361 end Is_Declared_Subtype;
7363 -- Start of processing for Same_Generic_Actual
7365 begin
7366 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7367 or else Is_Declared_Subtype (T1, T2)
7368 or else Is_Declared_Subtype (T2, T1);
7369 end Same_Generic_Actual;
7371 -- Start of processing for Different_Generic_Profile
7373 begin
7374 if not In_Instance then
7375 return False;
7377 elsif Ekind (E) = E_Function
7378 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7379 then
7380 return True;
7381 end if;
7383 F1 := First_Formal (Designator);
7384 F2 := First_Formal (E);
7385 while Present (F1) loop
7386 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7387 return True;
7388 end if;
7390 Next_Formal (F1);
7391 Next_Formal (F2);
7392 end loop;
7394 return False;
7395 end Different_Generic_Profile;
7397 -- Start of processing for Find_Corresponding_Spec
7399 begin
7400 E := Current_Entity (Designator);
7401 while Present (E) loop
7403 -- We are looking for a matching spec. It must have the same scope,
7404 -- and the same name, and either be type conformant, or be the case
7405 -- of a library procedure spec and its body (which belong to one
7406 -- another regardless of whether they are type conformant or not).
7408 if Scope (E) = Current_Scope then
7409 if Current_Scope = Standard_Standard
7410 or else (Ekind (E) = Ekind (Designator)
7411 and then Type_Conformant (E, Designator))
7412 then
7413 -- Within an instantiation, we know that spec and body are
7414 -- subtype conformant, because they were subtype conformant in
7415 -- the generic. We choose the subtype-conformant entity here as
7416 -- well, to resolve spurious ambiguities in the instance that
7417 -- were not present in the generic (i.e. when two different
7418 -- types are given the same actual). If we are looking for a
7419 -- spec to match a body, full conformance is expected.
7421 if In_Instance then
7423 -- Inherit the convention and "ghostness" of the matching
7424 -- spec to ensure proper full and subtype conformance.
7426 Set_Convention (Designator, Convention (E));
7428 if Is_Ghost_Entity (E) then
7429 Set_Is_Ghost_Entity (Designator);
7430 end if;
7432 -- Skip past subprogram bodies and subprogram renamings that
7433 -- may appear to have a matching spec, but that aren't fully
7434 -- conformant with it. That can occur in cases where an
7435 -- actual type causes unrelated homographs in the instance.
7437 if Nkind_In (N, N_Subprogram_Body,
7438 N_Subprogram_Renaming_Declaration)
7439 and then Present (Homonym (E))
7440 and then not Fully_Conformant (Designator, E)
7441 then
7442 goto Next_Entity;
7444 elsif not Subtype_Conformant (Designator, E) then
7445 goto Next_Entity;
7447 elsif Different_Generic_Profile (E) then
7448 goto Next_Entity;
7449 end if;
7450 end if;
7452 -- Ada 2012 (AI05-0165): For internally generated bodies of
7453 -- null procedures locate the internally generated spec. We
7454 -- enforce mode conformance since a tagged type may inherit
7455 -- from interfaces several null primitives which differ only
7456 -- in the mode of the formals.
7458 if not (Comes_From_Source (E))
7459 and then Is_Null_Procedure (E)
7460 and then not Mode_Conformant (Designator, E)
7461 then
7462 null;
7464 -- For null procedures coming from source that are completions,
7465 -- analysis of the generated body will establish the link.
7467 elsif Comes_From_Source (E)
7468 and then Nkind (Spec) = N_Procedure_Specification
7469 and then Null_Present (Spec)
7470 then
7471 return E;
7473 elsif not Has_Completion (E) then
7474 if Nkind (N) /= N_Subprogram_Body_Stub then
7475 Set_Corresponding_Spec (N, E);
7476 end if;
7478 Set_Has_Completion (E);
7479 return E;
7481 elsif Nkind (Parent (N)) = N_Subunit then
7483 -- If this is the proper body of a subunit, the completion
7484 -- flag is set when analyzing the stub.
7486 return E;
7488 -- If E is an internal function with a controlling result that
7489 -- was created for an operation inherited by a null extension,
7490 -- it may be overridden by a body without a previous spec (one
7491 -- more reason why these should be shunned). In that case we
7492 -- remove the generated body if present, because the current
7493 -- one is the explicit overriding.
7495 elsif Ekind (E) = E_Function
7496 and then Ada_Version >= Ada_2005
7497 and then not Comes_From_Source (E)
7498 and then Has_Controlling_Result (E)
7499 and then Is_Null_Extension (Etype (E))
7500 and then Comes_From_Source (Spec)
7501 then
7502 Set_Has_Completion (E, False);
7504 if Expander_Active
7505 and then Nkind (Parent (E)) = N_Function_Specification
7506 then
7507 Remove
7508 (Unit_Declaration_Node
7509 (Corresponding_Body (Unit_Declaration_Node (E))));
7511 return E;
7513 -- If expansion is disabled, or if the wrapper function has
7514 -- not been generated yet, this a late body overriding an
7515 -- inherited operation, or it is an overriding by some other
7516 -- declaration before the controlling result is frozen. In
7517 -- either case this is a declaration of a new entity.
7519 else
7520 return Empty;
7521 end if;
7523 -- If the body already exists, then this is an error unless
7524 -- the previous declaration is the implicit declaration of a
7525 -- derived subprogram. It is also legal for an instance to
7526 -- contain type conformant overloadable declarations (but the
7527 -- generic declaration may not), per 8.3(26/2).
7529 elsif No (Alias (E))
7530 and then not Is_Intrinsic_Subprogram (E)
7531 and then not In_Instance
7532 and then Post_Error
7533 then
7534 Error_Msg_Sloc := Sloc (E);
7536 if Is_Imported (E) then
7537 Error_Msg_NE
7538 ("body not allowed for imported subprogram & declared#",
7539 N, E);
7540 else
7541 Error_Msg_NE ("duplicate body for & declared#", N, E);
7542 end if;
7543 end if;
7545 -- Child units cannot be overloaded, so a conformance mismatch
7546 -- between body and a previous spec is an error.
7548 elsif Is_Child_Unit (E)
7549 and then
7550 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7551 and then
7552 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7553 N_Compilation_Unit
7554 and then Post_Error
7555 then
7556 Error_Msg_N
7557 ("body of child unit does not match previous declaration", N);
7558 end if;
7559 end if;
7561 <<Next_Entity>>
7562 E := Homonym (E);
7563 end loop;
7565 -- On exit, we know that no previous declaration of subprogram exists
7567 return Empty;
7568 end Find_Corresponding_Spec;
7570 ----------------------
7571 -- Fully_Conformant --
7572 ----------------------
7574 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7575 Result : Boolean;
7576 begin
7577 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7578 return Result;
7579 end Fully_Conformant;
7581 ----------------------------------
7582 -- Fully_Conformant_Expressions --
7583 ----------------------------------
7585 function Fully_Conformant_Expressions
7586 (Given_E1 : Node_Id;
7587 Given_E2 : Node_Id) return Boolean
7589 E1 : constant Node_Id := Original_Node (Given_E1);
7590 E2 : constant Node_Id := Original_Node (Given_E2);
7591 -- We always test conformance on original nodes, since it is possible
7592 -- for analysis and/or expansion to make things look as though they
7593 -- conform when they do not, e.g. by converting 1+2 into 3.
7595 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7596 renames Fully_Conformant_Expressions;
7598 function FCL (L1, L2 : List_Id) return Boolean;
7599 -- Compare elements of two lists for conformance. Elements have to be
7600 -- conformant, and actuals inserted as default parameters do not match
7601 -- explicit actuals with the same value.
7603 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7604 -- Compare an operator node with a function call
7606 ---------
7607 -- FCL --
7608 ---------
7610 function FCL (L1, L2 : List_Id) return Boolean is
7611 N1, N2 : Node_Id;
7613 begin
7614 if L1 = No_List then
7615 N1 := Empty;
7616 else
7617 N1 := First (L1);
7618 end if;
7620 if L2 = No_List then
7621 N2 := Empty;
7622 else
7623 N2 := First (L2);
7624 end if;
7626 -- Compare two lists, skipping rewrite insertions (we want to compare
7627 -- the original trees, not the expanded versions).
7629 loop
7630 if Is_Rewrite_Insertion (N1) then
7631 Next (N1);
7632 elsif Is_Rewrite_Insertion (N2) then
7633 Next (N2);
7634 elsif No (N1) then
7635 return No (N2);
7636 elsif No (N2) then
7637 return False;
7638 elsif not FCE (N1, N2) then
7639 return False;
7640 else
7641 Next (N1);
7642 Next (N2);
7643 end if;
7644 end loop;
7645 end FCL;
7647 ---------
7648 -- FCO --
7649 ---------
7651 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7652 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7653 Act : Node_Id;
7655 begin
7656 if No (Actuals)
7657 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7658 then
7659 return False;
7661 else
7662 Act := First (Actuals);
7664 if Nkind (Op_Node) in N_Binary_Op then
7665 if not FCE (Left_Opnd (Op_Node), Act) then
7666 return False;
7667 end if;
7669 Next (Act);
7670 end if;
7672 return Present (Act)
7673 and then FCE (Right_Opnd (Op_Node), Act)
7674 and then No (Next (Act));
7675 end if;
7676 end FCO;
7678 -- Start of processing for Fully_Conformant_Expressions
7680 begin
7681 -- Non-conformant if paren count does not match. Note: if some idiot
7682 -- complains that we don't do this right for more than 3 levels of
7683 -- parentheses, they will be treated with the respect they deserve.
7685 if Paren_Count (E1) /= Paren_Count (E2) then
7686 return False;
7688 -- If same entities are referenced, then they are conformant even if
7689 -- they have different forms (RM 8.3.1(19-20)).
7691 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7692 if Present (Entity (E1)) then
7693 return Entity (E1) = Entity (E2)
7694 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7695 and then Ekind (Entity (E1)) = E_Discriminant
7696 and then Ekind (Entity (E2)) = E_In_Parameter);
7698 elsif Nkind (E1) = N_Expanded_Name
7699 and then Nkind (E2) = N_Expanded_Name
7700 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7701 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7702 then
7703 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7705 else
7706 -- Identifiers in component associations don't always have
7707 -- entities, but their names must conform.
7709 return Nkind (E1) = N_Identifier
7710 and then Nkind (E2) = N_Identifier
7711 and then Chars (E1) = Chars (E2);
7712 end if;
7714 elsif Nkind (E1) = N_Character_Literal
7715 and then Nkind (E2) = N_Expanded_Name
7716 then
7717 return Nkind (Selector_Name (E2)) = N_Character_Literal
7718 and then Chars (E1) = Chars (Selector_Name (E2));
7720 elsif Nkind (E2) = N_Character_Literal
7721 and then Nkind (E1) = N_Expanded_Name
7722 then
7723 return Nkind (Selector_Name (E1)) = N_Character_Literal
7724 and then Chars (E2) = Chars (Selector_Name (E1));
7726 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7727 return FCO (E1, E2);
7729 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7730 return FCO (E2, E1);
7732 -- Otherwise we must have the same syntactic entity
7734 elsif Nkind (E1) /= Nkind (E2) then
7735 return False;
7737 -- At this point, we specialize by node type
7739 else
7740 case Nkind (E1) is
7742 when N_Aggregate =>
7743 return
7744 FCL (Expressions (E1), Expressions (E2))
7745 and then
7746 FCL (Component_Associations (E1),
7747 Component_Associations (E2));
7749 when N_Allocator =>
7750 if Nkind (Expression (E1)) = N_Qualified_Expression
7751 or else
7752 Nkind (Expression (E2)) = N_Qualified_Expression
7753 then
7754 return FCE (Expression (E1), Expression (E2));
7756 -- Check that the subtype marks and any constraints
7757 -- are conformant
7759 else
7760 declare
7761 Indic1 : constant Node_Id := Expression (E1);
7762 Indic2 : constant Node_Id := Expression (E2);
7763 Elt1 : Node_Id;
7764 Elt2 : Node_Id;
7766 begin
7767 if Nkind (Indic1) /= N_Subtype_Indication then
7768 return
7769 Nkind (Indic2) /= N_Subtype_Indication
7770 and then Entity (Indic1) = Entity (Indic2);
7772 elsif Nkind (Indic2) /= N_Subtype_Indication then
7773 return
7774 Nkind (Indic1) /= N_Subtype_Indication
7775 and then Entity (Indic1) = Entity (Indic2);
7777 else
7778 if Entity (Subtype_Mark (Indic1)) /=
7779 Entity (Subtype_Mark (Indic2))
7780 then
7781 return False;
7782 end if;
7784 Elt1 := First (Constraints (Constraint (Indic1)));
7785 Elt2 := First (Constraints (Constraint (Indic2)));
7786 while Present (Elt1) and then Present (Elt2) loop
7787 if not FCE (Elt1, Elt2) then
7788 return False;
7789 end if;
7791 Next (Elt1);
7792 Next (Elt2);
7793 end loop;
7795 return True;
7796 end if;
7797 end;
7798 end if;
7800 when N_Attribute_Reference =>
7801 return
7802 Attribute_Name (E1) = Attribute_Name (E2)
7803 and then FCL (Expressions (E1), Expressions (E2));
7805 when N_Binary_Op =>
7806 return
7807 Entity (E1) = Entity (E2)
7808 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7809 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7811 when N_Short_Circuit | N_Membership_Test =>
7812 return
7813 FCE (Left_Opnd (E1), Left_Opnd (E2))
7814 and then
7815 FCE (Right_Opnd (E1), Right_Opnd (E2));
7817 when N_Case_Expression =>
7818 declare
7819 Alt1 : Node_Id;
7820 Alt2 : Node_Id;
7822 begin
7823 if not FCE (Expression (E1), Expression (E2)) then
7824 return False;
7826 else
7827 Alt1 := First (Alternatives (E1));
7828 Alt2 := First (Alternatives (E2));
7829 loop
7830 if Present (Alt1) /= Present (Alt2) then
7831 return False;
7832 elsif No (Alt1) then
7833 return True;
7834 end if;
7836 if not FCE (Expression (Alt1), Expression (Alt2))
7837 or else not FCL (Discrete_Choices (Alt1),
7838 Discrete_Choices (Alt2))
7839 then
7840 return False;
7841 end if;
7843 Next (Alt1);
7844 Next (Alt2);
7845 end loop;
7846 end if;
7847 end;
7849 when N_Character_Literal =>
7850 return
7851 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7853 when N_Component_Association =>
7854 return
7855 FCL (Choices (E1), Choices (E2))
7856 and then
7857 FCE (Expression (E1), Expression (E2));
7859 when N_Explicit_Dereference =>
7860 return
7861 FCE (Prefix (E1), Prefix (E2));
7863 when N_Extension_Aggregate =>
7864 return
7865 FCL (Expressions (E1), Expressions (E2))
7866 and then Null_Record_Present (E1) =
7867 Null_Record_Present (E2)
7868 and then FCL (Component_Associations (E1),
7869 Component_Associations (E2));
7871 when N_Function_Call =>
7872 return
7873 FCE (Name (E1), Name (E2))
7874 and then
7875 FCL (Parameter_Associations (E1),
7876 Parameter_Associations (E2));
7878 when N_If_Expression =>
7879 return
7880 FCL (Expressions (E1), Expressions (E2));
7882 when N_Indexed_Component =>
7883 return
7884 FCE (Prefix (E1), Prefix (E2))
7885 and then
7886 FCL (Expressions (E1), Expressions (E2));
7888 when N_Integer_Literal =>
7889 return (Intval (E1) = Intval (E2));
7891 when N_Null =>
7892 return True;
7894 when N_Operator_Symbol =>
7895 return
7896 Chars (E1) = Chars (E2);
7898 when N_Others_Choice =>
7899 return True;
7901 when N_Parameter_Association =>
7902 return
7903 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7904 and then FCE (Explicit_Actual_Parameter (E1),
7905 Explicit_Actual_Parameter (E2));
7907 when N_Qualified_Expression =>
7908 return
7909 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7910 and then
7911 FCE (Expression (E1), Expression (E2));
7913 when N_Quantified_Expression =>
7914 if not FCE (Condition (E1), Condition (E2)) then
7915 return False;
7916 end if;
7918 if Present (Loop_Parameter_Specification (E1))
7919 and then Present (Loop_Parameter_Specification (E2))
7920 then
7921 declare
7922 L1 : constant Node_Id :=
7923 Loop_Parameter_Specification (E1);
7924 L2 : constant Node_Id :=
7925 Loop_Parameter_Specification (E2);
7927 begin
7928 return
7929 Reverse_Present (L1) = Reverse_Present (L2)
7930 and then
7931 FCE (Defining_Identifier (L1),
7932 Defining_Identifier (L2))
7933 and then
7934 FCE (Discrete_Subtype_Definition (L1),
7935 Discrete_Subtype_Definition (L2));
7936 end;
7938 elsif Present (Iterator_Specification (E1))
7939 and then Present (Iterator_Specification (E2))
7940 then
7941 declare
7942 I1 : constant Node_Id := Iterator_Specification (E1);
7943 I2 : constant Node_Id := Iterator_Specification (E2);
7945 begin
7946 return
7947 FCE (Defining_Identifier (I1),
7948 Defining_Identifier (I2))
7949 and then
7950 Of_Present (I1) = Of_Present (I2)
7951 and then
7952 Reverse_Present (I1) = Reverse_Present (I2)
7953 and then FCE (Name (I1), Name (I2))
7954 and then FCE (Subtype_Indication (I1),
7955 Subtype_Indication (I2));
7956 end;
7958 -- The quantified expressions used different specifications to
7959 -- walk their respective ranges.
7961 else
7962 return False;
7963 end if;
7965 when N_Range =>
7966 return
7967 FCE (Low_Bound (E1), Low_Bound (E2))
7968 and then
7969 FCE (High_Bound (E1), High_Bound (E2));
7971 when N_Real_Literal =>
7972 return (Realval (E1) = Realval (E2));
7974 when N_Selected_Component =>
7975 return
7976 FCE (Prefix (E1), Prefix (E2))
7977 and then
7978 FCE (Selector_Name (E1), Selector_Name (E2));
7980 when N_Slice =>
7981 return
7982 FCE (Prefix (E1), Prefix (E2))
7983 and then
7984 FCE (Discrete_Range (E1), Discrete_Range (E2));
7986 when N_String_Literal =>
7987 declare
7988 S1 : constant String_Id := Strval (E1);
7989 S2 : constant String_Id := Strval (E2);
7990 L1 : constant Nat := String_Length (S1);
7991 L2 : constant Nat := String_Length (S2);
7993 begin
7994 if L1 /= L2 then
7995 return False;
7997 else
7998 for J in 1 .. L1 loop
7999 if Get_String_Char (S1, J) /=
8000 Get_String_Char (S2, J)
8001 then
8002 return False;
8003 end if;
8004 end loop;
8006 return True;
8007 end if;
8008 end;
8010 when N_Type_Conversion =>
8011 return
8012 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8013 and then
8014 FCE (Expression (E1), Expression (E2));
8016 when N_Unary_Op =>
8017 return
8018 Entity (E1) = Entity (E2)
8019 and then
8020 FCE (Right_Opnd (E1), Right_Opnd (E2));
8022 when N_Unchecked_Type_Conversion =>
8023 return
8024 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8025 and then
8026 FCE (Expression (E1), Expression (E2));
8028 -- All other node types cannot appear in this context. Strictly
8029 -- we should raise a fatal internal error. Instead we just ignore
8030 -- the nodes. This means that if anyone makes a mistake in the
8031 -- expander and mucks an expression tree irretrievably, the result
8032 -- will be a failure to detect a (probably very obscure) case
8033 -- of non-conformance, which is better than bombing on some
8034 -- case where two expressions do in fact conform.
8036 when others =>
8037 return True;
8039 end case;
8040 end if;
8041 end Fully_Conformant_Expressions;
8043 ----------------------------------------
8044 -- Fully_Conformant_Discrete_Subtypes --
8045 ----------------------------------------
8047 function Fully_Conformant_Discrete_Subtypes
8048 (Given_S1 : Node_Id;
8049 Given_S2 : Node_Id) return Boolean
8051 S1 : constant Node_Id := Original_Node (Given_S1);
8052 S2 : constant Node_Id := Original_Node (Given_S2);
8054 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8055 -- Special-case for a bound given by a discriminant, which in the body
8056 -- is replaced with the discriminal of the enclosing type.
8058 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8059 -- Check both bounds
8061 -----------------------
8062 -- Conforming_Bounds --
8063 -----------------------
8065 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8066 begin
8067 if Is_Entity_Name (B1)
8068 and then Is_Entity_Name (B2)
8069 and then Ekind (Entity (B1)) = E_Discriminant
8070 then
8071 return Chars (B1) = Chars (B2);
8073 else
8074 return Fully_Conformant_Expressions (B1, B2);
8075 end if;
8076 end Conforming_Bounds;
8078 -----------------------
8079 -- Conforming_Ranges --
8080 -----------------------
8082 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8083 begin
8084 return
8085 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8086 and then
8087 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8088 end Conforming_Ranges;
8090 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8092 begin
8093 if Nkind (S1) /= Nkind (S2) then
8094 return False;
8096 elsif Is_Entity_Name (S1) then
8097 return Entity (S1) = Entity (S2);
8099 elsif Nkind (S1) = N_Range then
8100 return Conforming_Ranges (S1, S2);
8102 elsif Nkind (S1) = N_Subtype_Indication then
8103 return
8104 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8105 and then
8106 Conforming_Ranges
8107 (Range_Expression (Constraint (S1)),
8108 Range_Expression (Constraint (S2)));
8109 else
8110 return True;
8111 end if;
8112 end Fully_Conformant_Discrete_Subtypes;
8114 --------------------
8115 -- Install_Entity --
8116 --------------------
8118 procedure Install_Entity (E : Entity_Id) is
8119 Prev : constant Entity_Id := Current_Entity (E);
8120 begin
8121 Set_Is_Immediately_Visible (E);
8122 Set_Current_Entity (E);
8123 Set_Homonym (E, Prev);
8124 end Install_Entity;
8126 ---------------------
8127 -- Install_Formals --
8128 ---------------------
8130 procedure Install_Formals (Id : Entity_Id) is
8131 F : Entity_Id;
8132 begin
8133 F := First_Formal (Id);
8134 while Present (F) loop
8135 Install_Entity (F);
8136 Next_Formal (F);
8137 end loop;
8138 end Install_Formals;
8140 -----------------------------
8141 -- Is_Interface_Conformant --
8142 -----------------------------
8144 function Is_Interface_Conformant
8145 (Tagged_Type : Entity_Id;
8146 Iface_Prim : Entity_Id;
8147 Prim : Entity_Id) return Boolean
8149 -- The operation may in fact be an inherited (implicit) operation
8150 -- rather than the original interface primitive, so retrieve the
8151 -- ultimate ancestor.
8153 Iface : constant Entity_Id :=
8154 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8155 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8157 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8158 -- Return the controlling formal of Prim
8160 ------------------------
8161 -- Controlling_Formal --
8162 ------------------------
8164 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8165 E : Entity_Id;
8167 begin
8168 E := First_Entity (Prim);
8169 while Present (E) loop
8170 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8171 return E;
8172 end if;
8174 Next_Entity (E);
8175 end loop;
8177 return Empty;
8178 end Controlling_Formal;
8180 -- Local variables
8182 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8183 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8185 -- Start of processing for Is_Interface_Conformant
8187 begin
8188 pragma Assert (Is_Subprogram (Iface_Prim)
8189 and then Is_Subprogram (Prim)
8190 and then Is_Dispatching_Operation (Iface_Prim)
8191 and then Is_Dispatching_Operation (Prim));
8193 pragma Assert (Is_Interface (Iface)
8194 or else (Present (Alias (Iface_Prim))
8195 and then
8196 Is_Interface
8197 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8199 if Prim = Iface_Prim
8200 or else not Is_Subprogram (Prim)
8201 or else Ekind (Prim) /= Ekind (Iface_Prim)
8202 or else not Is_Dispatching_Operation (Prim)
8203 or else Scope (Prim) /= Scope (Tagged_Type)
8204 or else No (Typ)
8205 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8206 or else not Primitive_Names_Match (Iface_Prim, Prim)
8207 then
8208 return False;
8210 -- The mode of the controlling formals must match
8212 elsif Present (Iface_Ctrl_F)
8213 and then Present (Prim_Ctrl_F)
8214 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8215 then
8216 return False;
8218 -- Case of a procedure, or a function whose result type matches the
8219 -- result type of the interface primitive, or a function that has no
8220 -- controlling result (I or access I).
8222 elsif Ekind (Iface_Prim) = E_Procedure
8223 or else Etype (Prim) = Etype (Iface_Prim)
8224 or else not Has_Controlling_Result (Prim)
8225 then
8226 return Type_Conformant
8227 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8229 -- Case of a function returning an interface, or an access to one. Check
8230 -- that the return types correspond.
8232 elsif Implements_Interface (Typ, Iface) then
8233 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8235 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8236 then
8237 return False;
8238 else
8239 return
8240 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8241 Skip_Controlling_Formals => True);
8242 end if;
8244 else
8245 return False;
8246 end if;
8247 end Is_Interface_Conformant;
8249 ---------------------------------
8250 -- Is_Non_Overriding_Operation --
8251 ---------------------------------
8253 function Is_Non_Overriding_Operation
8254 (Prev_E : Entity_Id;
8255 New_E : Entity_Id) return Boolean
8257 Formal : Entity_Id;
8258 F_Typ : Entity_Id;
8259 G_Typ : Entity_Id := Empty;
8261 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8262 -- If F_Type is a derived type associated with a generic actual subtype,
8263 -- then return its Generic_Parent_Type attribute, else return Empty.
8265 function Types_Correspond
8266 (P_Type : Entity_Id;
8267 N_Type : Entity_Id) return Boolean;
8268 -- Returns true if and only if the types (or designated types in the
8269 -- case of anonymous access types) are the same or N_Type is derived
8270 -- directly or indirectly from P_Type.
8272 -----------------------------
8273 -- Get_Generic_Parent_Type --
8274 -----------------------------
8276 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8277 G_Typ : Entity_Id;
8278 Defn : Node_Id;
8279 Indic : Node_Id;
8281 begin
8282 if Is_Derived_Type (F_Typ)
8283 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8284 then
8285 -- The tree must be traversed to determine the parent subtype in
8286 -- the generic unit, which unfortunately isn't always available
8287 -- via semantic attributes. ??? (Note: The use of Original_Node
8288 -- is needed for cases where a full derived type has been
8289 -- rewritten.)
8291 -- If the parent type is a scalar type, the derivation creates
8292 -- an anonymous base type for it, and the source type is its
8293 -- first subtype.
8295 if Is_Scalar_Type (F_Typ)
8296 and then not Comes_From_Source (F_Typ)
8297 then
8298 Defn :=
8299 Type_Definition
8300 (Original_Node (Parent (First_Subtype (F_Typ))));
8301 else
8302 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8303 end if;
8304 if Nkind (Defn) = N_Derived_Type_Definition then
8305 Indic := Subtype_Indication (Defn);
8307 if Nkind (Indic) = N_Subtype_Indication then
8308 G_Typ := Entity (Subtype_Mark (Indic));
8309 else
8310 G_Typ := Entity (Indic);
8311 end if;
8313 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8314 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8315 then
8316 return Generic_Parent_Type (Parent (G_Typ));
8317 end if;
8318 end if;
8319 end if;
8321 return Empty;
8322 end Get_Generic_Parent_Type;
8324 ----------------------
8325 -- Types_Correspond --
8326 ----------------------
8328 function Types_Correspond
8329 (P_Type : Entity_Id;
8330 N_Type : Entity_Id) return Boolean
8332 Prev_Type : Entity_Id := Base_Type (P_Type);
8333 New_Type : Entity_Id := Base_Type (N_Type);
8335 begin
8336 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8337 Prev_Type := Designated_Type (Prev_Type);
8338 end if;
8340 if Ekind (New_Type) = E_Anonymous_Access_Type then
8341 New_Type := Designated_Type (New_Type);
8342 end if;
8344 if Prev_Type = New_Type then
8345 return True;
8347 elsif not Is_Class_Wide_Type (New_Type) then
8348 while Etype (New_Type) /= New_Type loop
8349 New_Type := Etype (New_Type);
8351 if New_Type = Prev_Type then
8352 return True;
8353 end if;
8354 end loop;
8355 end if;
8356 return False;
8357 end Types_Correspond;
8359 -- Start of processing for Is_Non_Overriding_Operation
8361 begin
8362 -- In the case where both operations are implicit derived subprograms
8363 -- then neither overrides the other. This can only occur in certain
8364 -- obscure cases (e.g., derivation from homographs created in a generic
8365 -- instantiation).
8367 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8368 return True;
8370 elsif Ekind (Current_Scope) = E_Package
8371 and then Is_Generic_Instance (Current_Scope)
8372 and then In_Private_Part (Current_Scope)
8373 and then Comes_From_Source (New_E)
8374 then
8375 -- We examine the formals and result type of the inherited operation,
8376 -- to determine whether their type is derived from (the instance of)
8377 -- a generic type. The first such formal or result type is the one
8378 -- tested.
8380 Formal := First_Formal (Prev_E);
8381 while Present (Formal) loop
8382 F_Typ := Base_Type (Etype (Formal));
8384 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8385 F_Typ := Designated_Type (F_Typ);
8386 end if;
8388 G_Typ := Get_Generic_Parent_Type (F_Typ);
8389 exit when Present (G_Typ);
8391 Next_Formal (Formal);
8392 end loop;
8394 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8395 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8396 end if;
8398 if No (G_Typ) then
8399 return False;
8400 end if;
8402 -- If the generic type is a private type, then the original operation
8403 -- was not overriding in the generic, because there was no primitive
8404 -- operation to override.
8406 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8407 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8408 N_Formal_Private_Type_Definition
8409 then
8410 return True;
8412 -- The generic parent type is the ancestor of a formal derived
8413 -- type declaration. We need to check whether it has a primitive
8414 -- operation that should be overridden by New_E in the generic.
8416 else
8417 declare
8418 P_Formal : Entity_Id;
8419 N_Formal : Entity_Id;
8420 P_Typ : Entity_Id;
8421 N_Typ : Entity_Id;
8422 P_Prim : Entity_Id;
8423 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8425 begin
8426 while Present (Prim_Elt) loop
8427 P_Prim := Node (Prim_Elt);
8429 if Chars (P_Prim) = Chars (New_E)
8430 and then Ekind (P_Prim) = Ekind (New_E)
8431 then
8432 P_Formal := First_Formal (P_Prim);
8433 N_Formal := First_Formal (New_E);
8434 while Present (P_Formal) and then Present (N_Formal) loop
8435 P_Typ := Etype (P_Formal);
8436 N_Typ := Etype (N_Formal);
8438 if not Types_Correspond (P_Typ, N_Typ) then
8439 exit;
8440 end if;
8442 Next_Entity (P_Formal);
8443 Next_Entity (N_Formal);
8444 end loop;
8446 -- Found a matching primitive operation belonging to the
8447 -- formal ancestor type, so the new subprogram is
8448 -- overriding.
8450 if No (P_Formal)
8451 and then No (N_Formal)
8452 and then (Ekind (New_E) /= E_Function
8453 or else
8454 Types_Correspond
8455 (Etype (P_Prim), Etype (New_E)))
8456 then
8457 return False;
8458 end if;
8459 end if;
8461 Next_Elmt (Prim_Elt);
8462 end loop;
8464 -- If no match found, then the new subprogram does not override
8465 -- in the generic (nor in the instance).
8467 -- If the type in question is not abstract, and the subprogram
8468 -- is, this will be an error if the new operation is in the
8469 -- private part of the instance. Emit a warning now, which will
8470 -- make the subsequent error message easier to understand.
8472 if not Is_Abstract_Type (F_Typ)
8473 and then Is_Abstract_Subprogram (Prev_E)
8474 and then In_Private_Part (Current_Scope)
8475 then
8476 Error_Msg_Node_2 := F_Typ;
8477 Error_Msg_NE
8478 ("private operation& in generic unit does not override "
8479 & "any primitive operation of& (RM 12.3 (18))??",
8480 New_E, New_E);
8481 end if;
8483 return True;
8484 end;
8485 end if;
8486 else
8487 return False;
8488 end if;
8489 end Is_Non_Overriding_Operation;
8491 -------------------------------------
8492 -- List_Inherited_Pre_Post_Aspects --
8493 -------------------------------------
8495 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8496 begin
8497 if Opt.List_Inherited_Aspects
8498 and then Is_Subprogram_Or_Generic_Subprogram (E)
8499 then
8500 declare
8501 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8502 Items : Node_Id;
8503 Prag : Node_Id;
8505 begin
8506 for Index in Subps'Range loop
8507 Items := Contract (Subps (Index));
8509 if Present (Items) then
8510 Prag := Pre_Post_Conditions (Items);
8511 while Present (Prag) loop
8512 Error_Msg_Sloc := Sloc (Prag);
8514 if Class_Present (Prag)
8515 and then not Split_PPC (Prag)
8516 then
8517 if Pragma_Name (Prag) = Name_Precondition then
8518 Error_Msg_N
8519 ("info: & inherits `Pre''Class` aspect from "
8520 & "#?L?", E);
8521 else
8522 Error_Msg_N
8523 ("info: & inherits `Post''Class` aspect from "
8524 & "#?L?", E);
8525 end if;
8526 end if;
8528 Prag := Next_Pragma (Prag);
8529 end loop;
8530 end if;
8531 end loop;
8532 end;
8533 end if;
8534 end List_Inherited_Pre_Post_Aspects;
8536 ------------------------------
8537 -- Make_Inequality_Operator --
8538 ------------------------------
8540 -- S is the defining identifier of an equality operator. We build a
8541 -- subprogram declaration with the right signature. This operation is
8542 -- intrinsic, because it is always expanded as the negation of the
8543 -- call to the equality function.
8545 procedure Make_Inequality_Operator (S : Entity_Id) is
8546 Loc : constant Source_Ptr := Sloc (S);
8547 Decl : Node_Id;
8548 Formals : List_Id;
8549 Op_Name : Entity_Id;
8551 FF : constant Entity_Id := First_Formal (S);
8552 NF : constant Entity_Id := Next_Formal (FF);
8554 begin
8555 -- Check that equality was properly defined, ignore call if not
8557 if No (NF) then
8558 return;
8559 end if;
8561 declare
8562 A : constant Entity_Id :=
8563 Make_Defining_Identifier (Sloc (FF),
8564 Chars => Chars (FF));
8566 B : constant Entity_Id :=
8567 Make_Defining_Identifier (Sloc (NF),
8568 Chars => Chars (NF));
8570 begin
8571 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8573 Formals := New_List (
8574 Make_Parameter_Specification (Loc,
8575 Defining_Identifier => A,
8576 Parameter_Type =>
8577 New_Occurrence_Of (Etype (First_Formal (S)),
8578 Sloc (Etype (First_Formal (S))))),
8580 Make_Parameter_Specification (Loc,
8581 Defining_Identifier => B,
8582 Parameter_Type =>
8583 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8584 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8586 Decl :=
8587 Make_Subprogram_Declaration (Loc,
8588 Specification =>
8589 Make_Function_Specification (Loc,
8590 Defining_Unit_Name => Op_Name,
8591 Parameter_Specifications => Formals,
8592 Result_Definition =>
8593 New_Occurrence_Of (Standard_Boolean, Loc)));
8595 -- Insert inequality right after equality if it is explicit or after
8596 -- the derived type when implicit. These entities are created only
8597 -- for visibility purposes, and eventually replaced in the course
8598 -- of expansion, so they do not need to be attached to the tree and
8599 -- seen by the back-end. Keeping them internal also avoids spurious
8600 -- freezing problems. The declaration is inserted in the tree for
8601 -- analysis, and removed afterwards. If the equality operator comes
8602 -- from an explicit declaration, attach the inequality immediately
8603 -- after. Else the equality is inherited from a derived type
8604 -- declaration, so insert inequality after that declaration.
8606 if No (Alias (S)) then
8607 Insert_After (Unit_Declaration_Node (S), Decl);
8608 elsif Is_List_Member (Parent (S)) then
8609 Insert_After (Parent (S), Decl);
8610 else
8611 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8612 end if;
8614 Mark_Rewrite_Insertion (Decl);
8615 Set_Is_Intrinsic_Subprogram (Op_Name);
8616 Analyze (Decl);
8617 Remove (Decl);
8618 Set_Has_Completion (Op_Name);
8619 Set_Corresponding_Equality (Op_Name, S);
8620 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8621 end;
8622 end Make_Inequality_Operator;
8624 ----------------------
8625 -- May_Need_Actuals --
8626 ----------------------
8628 procedure May_Need_Actuals (Fun : Entity_Id) is
8629 F : Entity_Id;
8630 B : Boolean;
8632 begin
8633 F := First_Formal (Fun);
8634 B := True;
8635 while Present (F) loop
8636 if No (Default_Value (F)) then
8637 B := False;
8638 exit;
8639 end if;
8641 Next_Formal (F);
8642 end loop;
8644 Set_Needs_No_Actuals (Fun, B);
8645 end May_Need_Actuals;
8647 ---------------------
8648 -- Mode_Conformant --
8649 ---------------------
8651 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8652 Result : Boolean;
8653 begin
8654 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8655 return Result;
8656 end Mode_Conformant;
8658 ---------------------------
8659 -- New_Overloaded_Entity --
8660 ---------------------------
8662 procedure New_Overloaded_Entity
8663 (S : Entity_Id;
8664 Derived_Type : Entity_Id := Empty)
8666 Overridden_Subp : Entity_Id := Empty;
8667 -- Set if the current scope has an operation that is type-conformant
8668 -- with S, and becomes hidden by S.
8670 Is_Primitive_Subp : Boolean;
8671 -- Set to True if the new subprogram is primitive
8673 E : Entity_Id;
8674 -- Entity that S overrides
8676 Prev_Vis : Entity_Id := Empty;
8677 -- Predecessor of E in Homonym chain
8679 procedure Check_For_Primitive_Subprogram
8680 (Is_Primitive : out Boolean;
8681 Is_Overriding : Boolean := False);
8682 -- If the subprogram being analyzed is a primitive operation of the type
8683 -- of a formal or result, set the Has_Primitive_Operations flag on the
8684 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8685 -- corresponding flag on the entity itself for later use.
8687 procedure Check_Synchronized_Overriding
8688 (Def_Id : Entity_Id;
8689 Overridden_Subp : out Entity_Id);
8690 -- First determine if Def_Id is an entry or a subprogram either defined
8691 -- in the scope of a task or protected type, or is a primitive of such
8692 -- a type. Check whether Def_Id overrides a subprogram of an interface
8693 -- implemented by the synchronized type, return the overridden entity
8694 -- or Empty.
8696 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8697 -- Check that E is declared in the private part of the current package,
8698 -- or in the package body, where it may hide a previous declaration.
8699 -- We can't use In_Private_Part by itself because this flag is also
8700 -- set when freezing entities, so we must examine the place of the
8701 -- declaration in the tree, and recognize wrapper packages as well.
8703 function Is_Overriding_Alias
8704 (Old_E : Entity_Id;
8705 New_E : Entity_Id) return Boolean;
8706 -- Check whether new subprogram and old subprogram are both inherited
8707 -- from subprograms that have distinct dispatch table entries. This can
8708 -- occur with derivations from instances with accidental homonyms. The
8709 -- function is conservative given that the converse is only true within
8710 -- instances that contain accidental overloadings.
8712 ------------------------------------
8713 -- Check_For_Primitive_Subprogram --
8714 ------------------------------------
8716 procedure Check_For_Primitive_Subprogram
8717 (Is_Primitive : out Boolean;
8718 Is_Overriding : Boolean := False)
8720 Formal : Entity_Id;
8721 F_Typ : Entity_Id;
8722 B_Typ : Entity_Id;
8724 function Visible_Part_Type (T : Entity_Id) return Boolean;
8725 -- Returns true if T is declared in the visible part of the current
8726 -- package scope; otherwise returns false. Assumes that T is declared
8727 -- in a package.
8729 procedure Check_Private_Overriding (T : Entity_Id);
8730 -- Checks that if a primitive abstract subprogram of a visible
8731 -- abstract type is declared in a private part, then it must override
8732 -- an abstract subprogram declared in the visible part. Also checks
8733 -- that if a primitive function with a controlling result is declared
8734 -- in a private part, then it must override a function declared in
8735 -- the visible part.
8737 ------------------------------
8738 -- Check_Private_Overriding --
8739 ------------------------------
8741 procedure Check_Private_Overriding (T : Entity_Id) is
8743 function Overrides_Visible_Function
8744 (Partial_View : Entity_Id) return Boolean;
8745 -- True if S overrides a function in the visible part. The
8746 -- overridden function could be explicitly or implicitly declared.
8748 function Overrides_Visible_Function
8749 (Partial_View : Entity_Id) return Boolean
8751 begin
8752 if not Is_Overriding or else not Has_Homonym (S) then
8753 return False;
8754 end if;
8756 if not Present (Partial_View) then
8757 return True;
8758 end if;
8760 -- Search through all the homonyms H of S in the current
8761 -- package spec, and return True if we find one that matches.
8762 -- Note that Parent (H) will be the declaration of the
8763 -- partial view of T for a match.
8765 declare
8766 H : Entity_Id := S;
8767 begin
8768 loop
8769 H := Homonym (H);
8770 exit when not Present (H) or else Scope (H) /= Scope (S);
8772 if Nkind_In
8773 (Parent (H),
8774 N_Private_Extension_Declaration,
8775 N_Private_Type_Declaration)
8776 and then Defining_Identifier (Parent (H)) = Partial_View
8777 then
8778 return True;
8779 end if;
8780 end loop;
8781 end;
8783 return False;
8784 end Overrides_Visible_Function;
8786 -- Start of processing for Check_Private_Overriding
8788 begin
8789 if Is_Package_Or_Generic_Package (Current_Scope)
8790 and then In_Private_Part (Current_Scope)
8791 and then Visible_Part_Type (T)
8792 and then not In_Instance
8793 then
8794 if Is_Abstract_Type (T)
8795 and then Is_Abstract_Subprogram (S)
8796 and then (not Is_Overriding
8797 or else not Is_Abstract_Subprogram (E))
8798 then
8799 Error_Msg_N ("abstract subprograms must be visible "
8800 & "(RM 3.9.3(10))!", S);
8802 elsif Ekind (S) = E_Function then
8803 declare
8804 Partial_View : constant Entity_Id :=
8805 Incomplete_Or_Partial_View (T);
8807 begin
8808 if not Overrides_Visible_Function (Partial_View) then
8810 -- Here, S is "function ... return T;" declared in
8811 -- the private part, not overriding some visible
8812 -- operation. That's illegal in the tagged case
8813 -- (but not if the private type is untagged).
8815 if ((Present (Partial_View)
8816 and then Is_Tagged_Type (Partial_View))
8817 or else (not Present (Partial_View)
8818 and then Is_Tagged_Type (T)))
8819 and then T = Base_Type (Etype (S))
8820 then
8821 Error_Msg_N
8822 ("private function with tagged result must"
8823 & " override visible-part function", S);
8824 Error_Msg_N
8825 ("\move subprogram to the visible part"
8826 & " (RM 3.9.3(10))", S);
8828 -- AI05-0073: extend this test to the case of a
8829 -- function with a controlling access result.
8831 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8832 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8833 and then
8834 not Is_Class_Wide_Type
8835 (Designated_Type (Etype (S)))
8836 and then Ada_Version >= Ada_2012
8837 then
8838 Error_Msg_N
8839 ("private function with controlling access "
8840 & "result must override visible-part function",
8842 Error_Msg_N
8843 ("\move subprogram to the visible part"
8844 & " (RM 3.9.3(10))", S);
8845 end if;
8846 end if;
8847 end;
8848 end if;
8849 end if;
8850 end Check_Private_Overriding;
8852 -----------------------
8853 -- Visible_Part_Type --
8854 -----------------------
8856 function Visible_Part_Type (T : Entity_Id) return Boolean is
8857 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8858 N : Node_Id;
8860 begin
8861 -- If the entity is a private type, then it must be declared in a
8862 -- visible part.
8864 if Ekind (T) in Private_Kind then
8865 return True;
8866 end if;
8868 -- Otherwise, we traverse the visible part looking for its
8869 -- corresponding declaration. We cannot use the declaration
8870 -- node directly because in the private part the entity of a
8871 -- private type is the one in the full view, which does not
8872 -- indicate that it is the completion of something visible.
8874 N := First (Visible_Declarations (Specification (P)));
8875 while Present (N) loop
8876 if Nkind (N) = N_Full_Type_Declaration
8877 and then Present (Defining_Identifier (N))
8878 and then T = Defining_Identifier (N)
8879 then
8880 return True;
8882 elsif Nkind_In (N, N_Private_Type_Declaration,
8883 N_Private_Extension_Declaration)
8884 and then Present (Defining_Identifier (N))
8885 and then T = Full_View (Defining_Identifier (N))
8886 then
8887 return True;
8888 end if;
8890 Next (N);
8891 end loop;
8893 return False;
8894 end Visible_Part_Type;
8896 -- Start of processing for Check_For_Primitive_Subprogram
8898 begin
8899 Is_Primitive := False;
8901 if not Comes_From_Source (S) then
8902 null;
8904 -- If subprogram is at library level, it is not primitive operation
8906 elsif Current_Scope = Standard_Standard then
8907 null;
8909 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8910 and then not In_Package_Body (Current_Scope))
8911 or else Is_Overriding
8912 then
8913 -- For function, check return type
8915 if Ekind (S) = E_Function then
8916 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8917 F_Typ := Designated_Type (Etype (S));
8918 else
8919 F_Typ := Etype (S);
8920 end if;
8922 B_Typ := Base_Type (F_Typ);
8924 if Scope (B_Typ) = Current_Scope
8925 and then not Is_Class_Wide_Type (B_Typ)
8926 and then not Is_Generic_Type (B_Typ)
8927 then
8928 Is_Primitive := True;
8929 Set_Has_Primitive_Operations (B_Typ);
8930 Set_Is_Primitive (S);
8931 Check_Private_Overriding (B_Typ);
8932 end if;
8933 end if;
8935 -- For all subprograms, check formals
8937 Formal := First_Formal (S);
8938 while Present (Formal) loop
8939 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8940 F_Typ := Designated_Type (Etype (Formal));
8941 else
8942 F_Typ := Etype (Formal);
8943 end if;
8945 B_Typ := Base_Type (F_Typ);
8947 if Ekind (B_Typ) = E_Access_Subtype then
8948 B_Typ := Base_Type (B_Typ);
8949 end if;
8951 if Scope (B_Typ) = Current_Scope
8952 and then not Is_Class_Wide_Type (B_Typ)
8953 and then not Is_Generic_Type (B_Typ)
8954 then
8955 Is_Primitive := True;
8956 Set_Is_Primitive (S);
8957 Set_Has_Primitive_Operations (B_Typ);
8958 Check_Private_Overriding (B_Typ);
8959 end if;
8961 Next_Formal (Formal);
8962 end loop;
8964 -- Special case: An equality function can be redefined for a type
8965 -- occurring in a declarative part, and won't otherwise be treated as
8966 -- a primitive because it doesn't occur in a package spec and doesn't
8967 -- override an inherited subprogram. It's important that we mark it
8968 -- primitive so it can be returned by Collect_Primitive_Operations
8969 -- and be used in composing the equality operation of later types
8970 -- that have a component of the type.
8972 elsif Chars (S) = Name_Op_Eq
8973 and then Etype (S) = Standard_Boolean
8974 then
8975 B_Typ := Base_Type (Etype (First_Formal (S)));
8977 if Scope (B_Typ) = Current_Scope
8978 and then
8979 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8980 and then not Is_Limited_Type (B_Typ)
8981 then
8982 Is_Primitive := True;
8983 Set_Is_Primitive (S);
8984 Set_Has_Primitive_Operations (B_Typ);
8985 Check_Private_Overriding (B_Typ);
8986 end if;
8987 end if;
8988 end Check_For_Primitive_Subprogram;
8990 -----------------------------------
8991 -- Check_Synchronized_Overriding --
8992 -----------------------------------
8994 procedure Check_Synchronized_Overriding
8995 (Def_Id : Entity_Id;
8996 Overridden_Subp : out Entity_Id)
8998 Ifaces_List : Elist_Id;
8999 In_Scope : Boolean;
9000 Typ : Entity_Id;
9002 function Matches_Prefixed_View_Profile
9003 (Prim_Params : List_Id;
9004 Iface_Params : List_Id) return Boolean;
9005 -- Determine whether a subprogram's parameter profile Prim_Params
9006 -- matches that of a potentially overridden interface subprogram
9007 -- Iface_Params. Also determine if the type of first parameter of
9008 -- Iface_Params is an implemented interface.
9010 -----------------------------------
9011 -- Matches_Prefixed_View_Profile --
9012 -----------------------------------
9014 function Matches_Prefixed_View_Profile
9015 (Prim_Params : List_Id;
9016 Iface_Params : List_Id) return Boolean
9018 Iface_Id : Entity_Id;
9019 Iface_Param : Node_Id;
9020 Iface_Typ : Entity_Id;
9021 Prim_Id : Entity_Id;
9022 Prim_Param : Node_Id;
9023 Prim_Typ : Entity_Id;
9025 function Is_Implemented
9026 (Ifaces_List : Elist_Id;
9027 Iface : Entity_Id) return Boolean;
9028 -- Determine if Iface is implemented by the current task or
9029 -- protected type.
9031 --------------------
9032 -- Is_Implemented --
9033 --------------------
9035 function Is_Implemented
9036 (Ifaces_List : Elist_Id;
9037 Iface : Entity_Id) return Boolean
9039 Iface_Elmt : Elmt_Id;
9041 begin
9042 Iface_Elmt := First_Elmt (Ifaces_List);
9043 while Present (Iface_Elmt) loop
9044 if Node (Iface_Elmt) = Iface then
9045 return True;
9046 end if;
9048 Next_Elmt (Iface_Elmt);
9049 end loop;
9051 return False;
9052 end Is_Implemented;
9054 -- Start of processing for Matches_Prefixed_View_Profile
9056 begin
9057 Iface_Param := First (Iface_Params);
9058 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9060 if Is_Access_Type (Iface_Typ) then
9061 Iface_Typ := Designated_Type (Iface_Typ);
9062 end if;
9064 Prim_Param := First (Prim_Params);
9066 -- The first parameter of the potentially overridden subprogram
9067 -- must be an interface implemented by Prim.
9069 if not Is_Interface (Iface_Typ)
9070 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9071 then
9072 return False;
9073 end if;
9075 -- The checks on the object parameters are done, move onto the
9076 -- rest of the parameters.
9078 if not In_Scope then
9079 Prim_Param := Next (Prim_Param);
9080 end if;
9082 Iface_Param := Next (Iface_Param);
9083 while Present (Iface_Param) and then Present (Prim_Param) loop
9084 Iface_Id := Defining_Identifier (Iface_Param);
9085 Iface_Typ := Find_Parameter_Type (Iface_Param);
9087 Prim_Id := Defining_Identifier (Prim_Param);
9088 Prim_Typ := Find_Parameter_Type (Prim_Param);
9090 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9091 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9092 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9093 then
9094 Iface_Typ := Designated_Type (Iface_Typ);
9095 Prim_Typ := Designated_Type (Prim_Typ);
9096 end if;
9098 -- Case of multiple interface types inside a parameter profile
9100 -- (Obj_Param : in out Iface; ...; Param : Iface)
9102 -- If the interface type is implemented, then the matching type
9103 -- in the primitive should be the implementing record type.
9105 if Ekind (Iface_Typ) = E_Record_Type
9106 and then Is_Interface (Iface_Typ)
9107 and then Is_Implemented (Ifaces_List, Iface_Typ)
9108 then
9109 if Prim_Typ /= Typ then
9110 return False;
9111 end if;
9113 -- The two parameters must be both mode and subtype conformant
9115 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9116 or else not
9117 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9118 then
9119 return False;
9120 end if;
9122 Next (Iface_Param);
9123 Next (Prim_Param);
9124 end loop;
9126 -- One of the two lists contains more parameters than the other
9128 if Present (Iface_Param) or else Present (Prim_Param) then
9129 return False;
9130 end if;
9132 return True;
9133 end Matches_Prefixed_View_Profile;
9135 -- Start of processing for Check_Synchronized_Overriding
9137 begin
9138 Overridden_Subp := Empty;
9140 -- Def_Id must be an entry or a subprogram. We should skip predefined
9141 -- primitives internally generated by the frontend; however at this
9142 -- stage predefined primitives are still not fully decorated. As a
9143 -- minor optimization we skip here internally generated subprograms.
9145 if (Ekind (Def_Id) /= E_Entry
9146 and then Ekind (Def_Id) /= E_Function
9147 and then Ekind (Def_Id) /= E_Procedure)
9148 or else not Comes_From_Source (Def_Id)
9149 then
9150 return;
9151 end if;
9153 -- Search for the concurrent declaration since it contains the list
9154 -- of all implemented interfaces. In this case, the subprogram is
9155 -- declared within the scope of a protected or a task type.
9157 if Present (Scope (Def_Id))
9158 and then Is_Concurrent_Type (Scope (Def_Id))
9159 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9160 then
9161 Typ := Scope (Def_Id);
9162 In_Scope := True;
9164 -- The enclosing scope is not a synchronized type and the subprogram
9165 -- has no formals.
9167 elsif No (First_Formal (Def_Id)) then
9168 return;
9170 -- The subprogram has formals and hence it may be a primitive of a
9171 -- concurrent type.
9173 else
9174 Typ := Etype (First_Formal (Def_Id));
9176 if Is_Access_Type (Typ) then
9177 Typ := Directly_Designated_Type (Typ);
9178 end if;
9180 if Is_Concurrent_Type (Typ)
9181 and then not Is_Generic_Actual_Type (Typ)
9182 then
9183 In_Scope := False;
9185 -- This case occurs when the concurrent type is declared within
9186 -- a generic unit. As a result the corresponding record has been
9187 -- built and used as the type of the first formal, we just have
9188 -- to retrieve the corresponding concurrent type.
9190 elsif Is_Concurrent_Record_Type (Typ)
9191 and then not Is_Class_Wide_Type (Typ)
9192 and then Present (Corresponding_Concurrent_Type (Typ))
9193 then
9194 Typ := Corresponding_Concurrent_Type (Typ);
9195 In_Scope := False;
9197 else
9198 return;
9199 end if;
9200 end if;
9202 -- There is no overriding to check if is an inherited operation in a
9203 -- type derivation on for a generic actual.
9205 Collect_Interfaces (Typ, Ifaces_List);
9207 if Is_Empty_Elmt_List (Ifaces_List) then
9208 return;
9209 end if;
9211 -- Determine whether entry or subprogram Def_Id overrides a primitive
9212 -- operation that belongs to one of the interfaces in Ifaces_List.
9214 declare
9215 Candidate : Entity_Id := Empty;
9216 Hom : Entity_Id := Empty;
9217 Subp : Entity_Id := Empty;
9219 begin
9220 -- Traverse the homonym chain, looking for a potentially
9221 -- overridden subprogram that belongs to an implemented
9222 -- interface.
9224 Hom := Current_Entity_In_Scope (Def_Id);
9225 while Present (Hom) loop
9226 Subp := Hom;
9228 if Subp = Def_Id
9229 or else not Is_Overloadable (Subp)
9230 or else not Is_Primitive (Subp)
9231 or else not Is_Dispatching_Operation (Subp)
9232 or else not Present (Find_Dispatching_Type (Subp))
9233 or else not Is_Interface (Find_Dispatching_Type (Subp))
9234 then
9235 null;
9237 -- Entries and procedures can override abstract or null
9238 -- interface procedures.
9240 elsif (Ekind (Def_Id) = E_Procedure
9241 or else Ekind (Def_Id) = E_Entry)
9242 and then Ekind (Subp) = E_Procedure
9243 and then Matches_Prefixed_View_Profile
9244 (Parameter_Specifications (Parent (Def_Id)),
9245 Parameter_Specifications (Parent (Subp)))
9246 then
9247 Candidate := Subp;
9249 -- For an overridden subprogram Subp, check whether the mode
9250 -- of its first parameter is correct depending on the kind
9251 -- of synchronized type.
9253 declare
9254 Formal : constant Node_Id := First_Formal (Candidate);
9256 begin
9257 -- In order for an entry or a protected procedure to
9258 -- override, the first parameter of the overridden
9259 -- routine must be of mode "out", "in out" or
9260 -- access-to-variable.
9262 if Ekind_In (Candidate, E_Entry, E_Procedure)
9263 and then Is_Protected_Type (Typ)
9264 and then Ekind (Formal) /= E_In_Out_Parameter
9265 and then Ekind (Formal) /= E_Out_Parameter
9266 and then Nkind (Parameter_Type (Parent (Formal))) /=
9267 N_Access_Definition
9268 then
9269 null;
9271 -- All other cases are OK since a task entry or routine
9272 -- does not have a restriction on the mode of the first
9273 -- parameter of the overridden interface routine.
9275 else
9276 Overridden_Subp := Candidate;
9277 return;
9278 end if;
9279 end;
9281 -- Functions can override abstract interface functions
9283 elsif Ekind (Def_Id) = E_Function
9284 and then Ekind (Subp) = E_Function
9285 and then Matches_Prefixed_View_Profile
9286 (Parameter_Specifications (Parent (Def_Id)),
9287 Parameter_Specifications (Parent (Subp)))
9288 and then Etype (Result_Definition (Parent (Def_Id))) =
9289 Etype (Result_Definition (Parent (Subp)))
9290 then
9291 Candidate := Subp;
9293 -- If an inherited subprogram is implemented by a protected
9294 -- function, then the first parameter of the inherited
9295 -- subprogram shall be of mode in, but not an
9296 -- access-to-variable parameter (RM 9.4(11/9)
9298 if Present (First_Formal (Subp))
9299 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9300 and then
9301 (not Is_Access_Type (Etype (First_Formal (Subp)))
9302 or else
9303 Is_Access_Constant (Etype (First_Formal (Subp))))
9304 then
9305 Overridden_Subp := Subp;
9306 return;
9307 end if;
9308 end if;
9310 Hom := Homonym (Hom);
9311 end loop;
9313 -- After examining all candidates for overriding, we are left with
9314 -- the best match which is a mode incompatible interface routine.
9316 if In_Scope and then Present (Candidate) then
9317 Error_Msg_PT (Def_Id, Candidate);
9318 end if;
9320 Overridden_Subp := Candidate;
9321 return;
9322 end;
9323 end Check_Synchronized_Overriding;
9325 ----------------------------
9326 -- Is_Private_Declaration --
9327 ----------------------------
9329 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9330 Priv_Decls : List_Id;
9331 Decl : constant Node_Id := Unit_Declaration_Node (E);
9333 begin
9334 if Is_Package_Or_Generic_Package (Current_Scope)
9335 and then In_Private_Part (Current_Scope)
9336 then
9337 Priv_Decls :=
9338 Private_Declarations (Package_Specification (Current_Scope));
9340 return In_Package_Body (Current_Scope)
9341 or else
9342 (Is_List_Member (Decl)
9343 and then List_Containing (Decl) = Priv_Decls)
9344 or else (Nkind (Parent (Decl)) = N_Package_Specification
9345 and then not
9346 Is_Compilation_Unit
9347 (Defining_Entity (Parent (Decl)))
9348 and then List_Containing (Parent (Parent (Decl))) =
9349 Priv_Decls);
9350 else
9351 return False;
9352 end if;
9353 end Is_Private_Declaration;
9355 --------------------------
9356 -- Is_Overriding_Alias --
9357 --------------------------
9359 function Is_Overriding_Alias
9360 (Old_E : Entity_Id;
9361 New_E : Entity_Id) return Boolean
9363 AO : constant Entity_Id := Alias (Old_E);
9364 AN : constant Entity_Id := Alias (New_E);
9365 begin
9366 return Scope (AO) /= Scope (AN)
9367 or else No (DTC_Entity (AO))
9368 or else No (DTC_Entity (AN))
9369 or else DT_Position (AO) = DT_Position (AN);
9370 end Is_Overriding_Alias;
9372 -- Start of processing for New_Overloaded_Entity
9374 begin
9375 -- We need to look for an entity that S may override. This must be a
9376 -- homonym in the current scope, so we look for the first homonym of
9377 -- S in the current scope as the starting point for the search.
9379 E := Current_Entity_In_Scope (S);
9381 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9382 -- They are directly added to the list of primitive operations of
9383 -- Derived_Type, unless this is a rederivation in the private part
9384 -- of an operation that was already derived in the visible part of
9385 -- the current package.
9387 if Ada_Version >= Ada_2005
9388 and then Present (Derived_Type)
9389 and then Present (Alias (S))
9390 and then Is_Dispatching_Operation (Alias (S))
9391 and then Present (Find_Dispatching_Type (Alias (S)))
9392 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9393 then
9394 -- For private types, when the full-view is processed we propagate to
9395 -- the full view the non-overridden entities whose attribute "alias"
9396 -- references an interface primitive. These entities were added by
9397 -- Derive_Subprograms to ensure that interface primitives are
9398 -- covered.
9400 -- Inside_Freeze_Actions is non zero when S corresponds with an
9401 -- internal entity that links an interface primitive with its
9402 -- covering primitive through attribute Interface_Alias (see
9403 -- Add_Internal_Interface_Entities).
9405 if Inside_Freezing_Actions = 0
9406 and then Is_Package_Or_Generic_Package (Current_Scope)
9407 and then In_Private_Part (Current_Scope)
9408 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9409 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9410 and then Full_View (Defining_Identifier (Parent (E)))
9411 = Defining_Identifier (Parent (S))
9412 and then Alias (E) = Alias (S)
9413 then
9414 Check_Operation_From_Private_View (S, E);
9415 Set_Is_Dispatching_Operation (S);
9417 -- Common case
9419 else
9420 Enter_Overloaded_Entity (S);
9421 Check_Dispatching_Operation (S, Empty);
9422 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9423 end if;
9425 return;
9426 end if;
9428 -- If there is no homonym then this is definitely not overriding
9430 if No (E) then
9431 Enter_Overloaded_Entity (S);
9432 Check_Dispatching_Operation (S, Empty);
9433 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9435 -- If subprogram has an explicit declaration, check whether it has an
9436 -- overriding indicator.
9438 if Comes_From_Source (S) then
9439 Check_Synchronized_Overriding (S, Overridden_Subp);
9441 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9442 -- it may have overridden some hidden inherited primitive. Update
9443 -- Overridden_Subp to avoid spurious errors when checking the
9444 -- overriding indicator.
9446 if Ada_Version >= Ada_2012
9447 and then No (Overridden_Subp)
9448 and then Is_Dispatching_Operation (S)
9449 and then Present (Overridden_Operation (S))
9450 then
9451 Overridden_Subp := Overridden_Operation (S);
9452 end if;
9454 Check_Overriding_Indicator
9455 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9457 -- The Ghost policy in effect at the point of declaration of a
9458 -- parent subprogram and an overriding subprogram must match
9459 -- (SPARK RM 6.9(17)).
9461 Check_Ghost_Overriding (S, Overridden_Subp);
9462 end if;
9464 -- If there is a homonym that is not overloadable, then we have an
9465 -- error, except for the special cases checked explicitly below.
9467 elsif not Is_Overloadable (E) then
9469 -- Check for spurious conflict produced by a subprogram that has the
9470 -- same name as that of the enclosing generic package. The conflict
9471 -- occurs within an instance, between the subprogram and the renaming
9472 -- declaration for the package. After the subprogram, the package
9473 -- renaming declaration becomes hidden.
9475 if Ekind (E) = E_Package
9476 and then Present (Renamed_Object (E))
9477 and then Renamed_Object (E) = Current_Scope
9478 and then Nkind (Parent (Renamed_Object (E))) =
9479 N_Package_Specification
9480 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9481 then
9482 Set_Is_Hidden (E);
9483 Set_Is_Immediately_Visible (E, False);
9484 Enter_Overloaded_Entity (S);
9485 Set_Homonym (S, Homonym (E));
9486 Check_Dispatching_Operation (S, Empty);
9487 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9489 -- If the subprogram is implicit it is hidden by the previous
9490 -- declaration. However if it is dispatching, it must appear in the
9491 -- dispatch table anyway, because it can be dispatched to even if it
9492 -- cannot be called directly.
9494 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9495 Set_Scope (S, Current_Scope);
9497 if Is_Dispatching_Operation (Alias (S)) then
9498 Check_Dispatching_Operation (S, Empty);
9499 end if;
9501 return;
9503 else
9504 Error_Msg_Sloc := Sloc (E);
9506 -- Generate message, with useful additional warning if in generic
9508 if Is_Generic_Unit (E) then
9509 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9510 Error_Msg_N ("\& conflicts with declaration#", S);
9511 else
9512 Error_Msg_N ("& conflicts with declaration#", S);
9513 end if;
9515 return;
9516 end if;
9518 -- E exists and is overloadable
9520 else
9521 Check_Synchronized_Overriding (S, Overridden_Subp);
9523 -- Loop through E and its homonyms to determine if any of them is
9524 -- the candidate for overriding by S.
9526 while Present (E) loop
9528 -- Definitely not interesting if not in the current scope
9530 if Scope (E) /= Current_Scope then
9531 null;
9533 -- A function can overload the name of an abstract state. The
9534 -- state can be viewed as a function with a profile that cannot
9535 -- be matched by anything.
9537 elsif Ekind (S) = E_Function
9538 and then Ekind (E) = E_Abstract_State
9539 then
9540 Enter_Overloaded_Entity (S);
9541 return;
9543 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9544 -- procedures locate the internally generated spec. We enforce
9545 -- mode conformance since a tagged type may inherit from
9546 -- interfaces several null primitives which differ only in
9547 -- the mode of the formals.
9549 elsif not Comes_From_Source (S)
9550 and then Is_Null_Procedure (S)
9551 and then not Mode_Conformant (E, S)
9552 then
9553 null;
9555 -- Check if we have type conformance
9557 elsif Type_Conformant (E, S) then
9559 -- If the old and new entities have the same profile and one
9560 -- is not the body of the other, then this is an error, unless
9561 -- one of them is implicitly declared.
9563 -- There are some cases when both can be implicit, for example
9564 -- when both a literal and a function that overrides it are
9565 -- inherited in a derivation, or when an inherited operation
9566 -- of a tagged full type overrides the inherited operation of
9567 -- a private extension. Ada 83 had a special rule for the
9568 -- literal case. In Ada 95, the later implicit operation hides
9569 -- the former, and the literal is always the former. In the
9570 -- odd case where both are derived operations declared at the
9571 -- same point, both operations should be declared, and in that
9572 -- case we bypass the following test and proceed to the next
9573 -- part. This can only occur for certain obscure cases in
9574 -- instances, when an operation on a type derived from a formal
9575 -- private type does not override a homograph inherited from
9576 -- the actual. In subsequent derivations of such a type, the
9577 -- DT positions of these operations remain distinct, if they
9578 -- have been set.
9580 if Present (Alias (S))
9581 and then (No (Alias (E))
9582 or else Comes_From_Source (E)
9583 or else Is_Abstract_Subprogram (S)
9584 or else
9585 (Is_Dispatching_Operation (E)
9586 and then Is_Overriding_Alias (E, S)))
9587 and then Ekind (E) /= E_Enumeration_Literal
9588 then
9589 -- When an derived operation is overloaded it may be due to
9590 -- the fact that the full view of a private extension
9591 -- re-inherits. It has to be dealt with.
9593 if Is_Package_Or_Generic_Package (Current_Scope)
9594 and then In_Private_Part (Current_Scope)
9595 then
9596 Check_Operation_From_Private_View (S, E);
9597 end if;
9599 -- In any case the implicit operation remains hidden by the
9600 -- existing declaration, which is overriding. Indicate that
9601 -- E overrides the operation from which S is inherited.
9603 if Present (Alias (S)) then
9604 Set_Overridden_Operation (E, Alias (S));
9605 Inherit_Subprogram_Contract (E, Alias (S));
9607 else
9608 Set_Overridden_Operation (E, S);
9609 Inherit_Subprogram_Contract (E, S);
9610 end if;
9612 if Comes_From_Source (E) then
9613 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9615 -- The Ghost policy in effect at the point of declaration
9616 -- of a parent subprogram and an overriding subprogram
9617 -- must match (SPARK RM 6.9(17)).
9619 Check_Ghost_Overriding (E, S);
9620 end if;
9622 return;
9624 -- Within an instance, the renaming declarations for actual
9625 -- subprograms may become ambiguous, but they do not hide each
9626 -- other.
9628 elsif Ekind (E) /= E_Entry
9629 and then not Comes_From_Source (E)
9630 and then not Is_Generic_Instance (E)
9631 and then (Present (Alias (E))
9632 or else Is_Intrinsic_Subprogram (E))
9633 and then (not In_Instance
9634 or else No (Parent (E))
9635 or else Nkind (Unit_Declaration_Node (E)) /=
9636 N_Subprogram_Renaming_Declaration)
9637 then
9638 -- A subprogram child unit is not allowed to override an
9639 -- inherited subprogram (10.1.1(20)).
9641 if Is_Child_Unit (S) then
9642 Error_Msg_N
9643 ("child unit overrides inherited subprogram in parent",
9645 return;
9646 end if;
9648 if Is_Non_Overriding_Operation (E, S) then
9649 Enter_Overloaded_Entity (S);
9651 if No (Derived_Type)
9652 or else Is_Tagged_Type (Derived_Type)
9653 then
9654 Check_Dispatching_Operation (S, Empty);
9655 end if;
9657 return;
9658 end if;
9660 -- E is a derived operation or an internal operator which
9661 -- is being overridden. Remove E from further visibility.
9662 -- Furthermore, if E is a dispatching operation, it must be
9663 -- replaced in the list of primitive operations of its type
9664 -- (see Override_Dispatching_Operation).
9666 Overridden_Subp := E;
9668 declare
9669 Prev : Entity_Id;
9671 begin
9672 Prev := First_Entity (Current_Scope);
9673 while Present (Prev) and then Next_Entity (Prev) /= E loop
9674 Next_Entity (Prev);
9675 end loop;
9677 -- It is possible for E to be in the current scope and
9678 -- yet not in the entity chain. This can only occur in a
9679 -- generic context where E is an implicit concatenation
9680 -- in the formal part, because in a generic body the
9681 -- entity chain starts with the formals.
9683 -- In GNATprove mode, a wrapper for an operation with
9684 -- axiomatization may be a homonym of another declaration
9685 -- for an actual subprogram (needs refinement ???).
9687 if No (Prev) then
9688 if In_Instance
9689 and then GNATprove_Mode
9690 and then
9691 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9692 N_Subprogram_Renaming_Declaration
9693 then
9694 return;
9695 else
9696 pragma Assert (Chars (E) = Name_Op_Concat);
9697 null;
9698 end if;
9699 end if;
9701 -- E must be removed both from the entity_list of the
9702 -- current scope, and from the visibility chain.
9704 if Debug_Flag_E then
9705 Write_Str ("Override implicit operation ");
9706 Write_Int (Int (E));
9707 Write_Eol;
9708 end if;
9710 -- If E is a predefined concatenation, it stands for four
9711 -- different operations. As a result, a single explicit
9712 -- declaration does not hide it. In a possible ambiguous
9713 -- situation, Disambiguate chooses the user-defined op,
9714 -- so it is correct to retain the previous internal one.
9716 if Chars (E) /= Name_Op_Concat
9717 or else Ekind (E) /= E_Operator
9718 then
9719 -- For nondispatching derived operations that are
9720 -- overridden by a subprogram declared in the private
9721 -- part of a package, we retain the derived subprogram
9722 -- but mark it as not immediately visible. If the
9723 -- derived operation was declared in the visible part
9724 -- then this ensures that it will still be visible
9725 -- outside the package with the proper signature
9726 -- (calls from outside must also be directed to this
9727 -- version rather than the overriding one, unlike the
9728 -- dispatching case). Calls from inside the package
9729 -- will still resolve to the overriding subprogram
9730 -- since the derived one is marked as not visible
9731 -- within the package.
9733 -- If the private operation is dispatching, we achieve
9734 -- the overriding by keeping the implicit operation
9735 -- but setting its alias to be the overriding one. In
9736 -- this fashion the proper body is executed in all
9737 -- cases, but the original signature is used outside
9738 -- of the package.
9740 -- If the overriding is not in the private part, we
9741 -- remove the implicit operation altogether.
9743 if Is_Private_Declaration (S) then
9744 if not Is_Dispatching_Operation (E) then
9745 Set_Is_Immediately_Visible (E, False);
9746 else
9747 -- Work done in Override_Dispatching_Operation,
9748 -- so nothing else needs to be done here.
9750 null;
9751 end if;
9753 else
9754 -- Find predecessor of E in Homonym chain
9756 if E = Current_Entity (E) then
9757 Prev_Vis := Empty;
9758 else
9759 Prev_Vis := Current_Entity (E);
9760 while Homonym (Prev_Vis) /= E loop
9761 Prev_Vis := Homonym (Prev_Vis);
9762 end loop;
9763 end if;
9765 if Prev_Vis /= Empty then
9767 -- Skip E in the visibility chain
9769 Set_Homonym (Prev_Vis, Homonym (E));
9771 else
9772 Set_Name_Entity_Id (Chars (E), Homonym (E));
9773 end if;
9775 Set_Next_Entity (Prev, Next_Entity (E));
9777 if No (Next_Entity (Prev)) then
9778 Set_Last_Entity (Current_Scope, Prev);
9779 end if;
9780 end if;
9781 end if;
9783 Enter_Overloaded_Entity (S);
9785 -- For entities generated by Derive_Subprograms the
9786 -- overridden operation is the inherited primitive
9787 -- (which is available through the attribute alias).
9789 if not (Comes_From_Source (E))
9790 and then Is_Dispatching_Operation (E)
9791 and then Find_Dispatching_Type (E) =
9792 Find_Dispatching_Type (S)
9793 and then Present (Alias (E))
9794 and then Comes_From_Source (Alias (E))
9795 then
9796 Set_Overridden_Operation (S, Alias (E));
9797 Inherit_Subprogram_Contract (S, Alias (E));
9799 -- Normal case of setting entity as overridden
9801 -- Note: Static_Initialization and Overridden_Operation
9802 -- attributes use the same field in subprogram entities.
9803 -- Static_Initialization is only defined for internal
9804 -- initialization procedures, where Overridden_Operation
9805 -- is irrelevant. Therefore the setting of this attribute
9806 -- must check whether the target is an init_proc.
9808 elsif not Is_Init_Proc (S) then
9809 Set_Overridden_Operation (S, E);
9810 Inherit_Subprogram_Contract (S, E);
9811 end if;
9813 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9815 -- The Ghost policy in effect at the point of declaration
9816 -- of a parent subprogram and an overriding subprogram
9817 -- must match (SPARK RM 6.9(17)).
9819 Check_Ghost_Overriding (S, E);
9821 -- If S is a user-defined subprogram or a null procedure
9822 -- expanded to override an inherited null procedure, or a
9823 -- predefined dispatching primitive then indicate that E
9824 -- overrides the operation from which S is inherited.
9826 if Comes_From_Source (S)
9827 or else
9828 (Present (Parent (S))
9829 and then
9830 Nkind (Parent (S)) = N_Procedure_Specification
9831 and then
9832 Null_Present (Parent (S)))
9833 or else
9834 (Present (Alias (E))
9835 and then
9836 Is_Predefined_Dispatching_Operation (Alias (E)))
9837 then
9838 if Present (Alias (E)) then
9839 Set_Overridden_Operation (S, Alias (E));
9840 Inherit_Subprogram_Contract (S, Alias (E));
9841 end if;
9842 end if;
9844 if Is_Dispatching_Operation (E) then
9846 -- An overriding dispatching subprogram inherits the
9847 -- convention of the overridden subprogram (AI-117).
9849 Set_Convention (S, Convention (E));
9850 Check_Dispatching_Operation (S, E);
9852 else
9853 Check_Dispatching_Operation (S, Empty);
9854 end if;
9856 Check_For_Primitive_Subprogram
9857 (Is_Primitive_Subp, Is_Overriding => True);
9858 goto Check_Inequality;
9859 end;
9861 -- Apparent redeclarations in instances can occur when two
9862 -- formal types get the same actual type. The subprograms in
9863 -- in the instance are legal, even if not callable from the
9864 -- outside. Calls from within are disambiguated elsewhere.
9865 -- For dispatching operations in the visible part, the usual
9866 -- rules apply, and operations with the same profile are not
9867 -- legal (B830001).
9869 elsif (In_Instance_Visible_Part
9870 and then not Is_Dispatching_Operation (E))
9871 or else In_Instance_Not_Visible
9872 then
9873 null;
9875 -- Here we have a real error (identical profile)
9877 else
9878 Error_Msg_Sloc := Sloc (E);
9880 -- Avoid cascaded errors if the entity appears in
9881 -- subsequent calls.
9883 Set_Scope (S, Current_Scope);
9885 -- Generate error, with extra useful warning for the case
9886 -- of a generic instance with no completion.
9888 if Is_Generic_Instance (S)
9889 and then not Has_Completion (E)
9890 then
9891 Error_Msg_N
9892 ("instantiation cannot provide body for&", S);
9893 Error_Msg_N ("\& conflicts with declaration#", S);
9894 else
9895 Error_Msg_N ("& conflicts with declaration#", S);
9896 end if;
9898 return;
9899 end if;
9901 else
9902 -- If one subprogram has an access parameter and the other
9903 -- a parameter of an access type, calls to either might be
9904 -- ambiguous. Verify that parameters match except for the
9905 -- access parameter.
9907 if May_Hide_Profile then
9908 declare
9909 F1 : Entity_Id;
9910 F2 : Entity_Id;
9912 begin
9913 F1 := First_Formal (S);
9914 F2 := First_Formal (E);
9915 while Present (F1) and then Present (F2) loop
9916 if Is_Access_Type (Etype (F1)) then
9917 if not Is_Access_Type (Etype (F2))
9918 or else not Conforming_Types
9919 (Designated_Type (Etype (F1)),
9920 Designated_Type (Etype (F2)),
9921 Type_Conformant)
9922 then
9923 May_Hide_Profile := False;
9924 end if;
9926 elsif
9927 not Conforming_Types
9928 (Etype (F1), Etype (F2), Type_Conformant)
9929 then
9930 May_Hide_Profile := False;
9931 end if;
9933 Next_Formal (F1);
9934 Next_Formal (F2);
9935 end loop;
9937 if May_Hide_Profile
9938 and then No (F1)
9939 and then No (F2)
9940 then
9941 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9942 end if;
9943 end;
9944 end if;
9945 end if;
9947 E := Homonym (E);
9948 end loop;
9950 -- On exit, we know that S is a new entity
9952 Enter_Overloaded_Entity (S);
9953 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9954 Check_Overriding_Indicator
9955 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9957 -- The Ghost policy in effect at the point of declaration of a parent
9958 -- subprogram and an overriding subprogram must match
9959 -- (SPARK RM 6.9(17)).
9961 Check_Ghost_Overriding (S, Overridden_Subp);
9963 -- Overloading is not allowed in SPARK, except for operators
9965 if Nkind (S) /= N_Defining_Operator_Symbol then
9966 Error_Msg_Sloc := Sloc (Homonym (S));
9967 Check_SPARK_05_Restriction
9968 ("overloading not allowed with entity#", S);
9969 end if;
9971 -- If S is a derived operation for an untagged type then by
9972 -- definition it's not a dispatching operation (even if the parent
9973 -- operation was dispatching), so Check_Dispatching_Operation is not
9974 -- called in that case.
9976 if No (Derived_Type)
9977 or else Is_Tagged_Type (Derived_Type)
9978 then
9979 Check_Dispatching_Operation (S, Empty);
9980 end if;
9981 end if;
9983 -- If this is a user-defined equality operator that is not a derived
9984 -- subprogram, create the corresponding inequality. If the operation is
9985 -- dispatching, the expansion is done elsewhere, and we do not create
9986 -- an explicit inequality operation.
9988 <<Check_Inequality>>
9989 if Chars (S) = Name_Op_Eq
9990 and then Etype (S) = Standard_Boolean
9991 and then Present (Parent (S))
9992 and then not Is_Dispatching_Operation (S)
9993 then
9994 Make_Inequality_Operator (S);
9995 Check_Untagged_Equality (S);
9996 end if;
9997 end New_Overloaded_Entity;
9999 ---------------------
10000 -- Process_Formals --
10001 ---------------------
10003 procedure Process_Formals
10004 (T : List_Id;
10005 Related_Nod : Node_Id)
10007 Context : constant Node_Id := Parent (Parent (T));
10008 Param_Spec : Node_Id;
10009 Formal : Entity_Id;
10010 Formal_Type : Entity_Id;
10011 Default : Node_Id;
10012 Ptype : Entity_Id;
10014 Num_Out_Params : Nat := 0;
10015 First_Out_Param : Entity_Id := Empty;
10016 -- Used for setting Is_Only_Out_Parameter
10018 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
10019 -- Determine whether an access type designates a type coming from a
10020 -- limited view.
10022 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10023 -- Check whether the default has a class-wide type. After analysis the
10024 -- default has the type of the formal, so we must also check explicitly
10025 -- for an access attribute.
10027 ----------------------------------
10028 -- Designates_From_Limited_With --
10029 ----------------------------------
10031 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
10032 Desig : Entity_Id := Typ;
10034 begin
10035 if Is_Access_Type (Desig) then
10036 Desig := Directly_Designated_Type (Desig);
10037 end if;
10039 if Is_Class_Wide_Type (Desig) then
10040 Desig := Root_Type (Desig);
10041 end if;
10043 return
10044 Ekind (Desig) = E_Incomplete_Type
10045 and then From_Limited_With (Desig);
10046 end Designates_From_Limited_With;
10048 ---------------------------
10049 -- Is_Class_Wide_Default --
10050 ---------------------------
10052 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10053 begin
10054 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10055 or else (Nkind (D) = N_Attribute_Reference
10056 and then Attribute_Name (D) = Name_Access
10057 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10058 end Is_Class_Wide_Default;
10060 -- Start of processing for Process_Formals
10062 begin
10063 -- In order to prevent premature use of the formals in the same formal
10064 -- part, the Ekind is left undefined until all default expressions are
10065 -- analyzed. The Ekind is established in a separate loop at the end.
10067 Param_Spec := First (T);
10068 while Present (Param_Spec) loop
10069 Formal := Defining_Identifier (Param_Spec);
10070 Set_Never_Set_In_Source (Formal, True);
10071 Enter_Name (Formal);
10073 -- Case of ordinary parameters
10075 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10076 Find_Type (Parameter_Type (Param_Spec));
10077 Ptype := Parameter_Type (Param_Spec);
10079 if Ptype = Error then
10080 goto Continue;
10081 end if;
10083 Formal_Type := Entity (Ptype);
10085 if Is_Incomplete_Type (Formal_Type)
10086 or else
10087 (Is_Class_Wide_Type (Formal_Type)
10088 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10089 then
10090 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10091 -- primitive operations, as long as their completion is
10092 -- in the same declarative part. If in the private part
10093 -- this means that the type cannot be a Taft-amendment type.
10094 -- Check is done on package exit. For access to subprograms,
10095 -- the use is legal for Taft-amendment types.
10097 -- Ada 2012: tagged incomplete types are allowed as generic
10098 -- formal types. They do not introduce dependencies and the
10099 -- corresponding generic subprogram does not have a delayed
10100 -- freeze, because it does not need a freeze node. However,
10101 -- it is still the case that untagged incomplete types cannot
10102 -- be Taft-amendment types and must be completed in private
10103 -- part, so the subprogram must appear in the list of private
10104 -- dependents of the type. If the type is class-wide, it is
10105 -- not a primitive, but the freezing of the subprogram must
10106 -- also be delayed to force the creation of a freeze node.
10108 if Is_Tagged_Type (Formal_Type)
10109 or else (Ada_Version >= Ada_2012
10110 and then not From_Limited_With (Formal_Type)
10111 and then not Is_Generic_Type (Formal_Type))
10112 then
10113 if Ekind (Scope (Current_Scope)) = E_Package
10114 and then not Is_Generic_Type (Formal_Type)
10115 then
10116 if not Nkind_In
10117 (Parent (T), N_Access_Function_Definition,
10118 N_Access_Procedure_Definition)
10119 then
10120 -- A limited view has no private dependents
10122 if not Is_Class_Wide_Type (Formal_Type)
10123 and then not From_Limited_With (Formal_Type)
10124 then
10125 Append_Elmt (Current_Scope,
10126 Private_Dependents (Base_Type (Formal_Type)));
10127 end if;
10129 -- Freezing is delayed to ensure that Register_Prim
10130 -- will get called for this operation, which is needed
10131 -- in cases where static dispatch tables aren't built.
10132 -- (Note that the same is done for controlling access
10133 -- parameter cases in function Access_Definition.)
10135 if not Is_Thunk (Current_Scope) then
10136 Set_Has_Delayed_Freeze (Current_Scope);
10137 end if;
10138 end if;
10139 end if;
10141 -- Special handling of Value_Type for CIL case
10143 elsif Is_Value_Type (Formal_Type) then
10144 null;
10146 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10147 N_Access_Procedure_Definition)
10148 then
10149 -- AI05-0151: Tagged incomplete types are allowed in all
10150 -- formal parts. Untagged incomplete types are not allowed
10151 -- in bodies. Limited views of either kind are not allowed
10152 -- if there is no place at which the non-limited view can
10153 -- become available.
10155 -- Incomplete formal untagged types are not allowed in
10156 -- subprogram bodies (but are legal in their declarations).
10157 -- This excludes bodies created for null procedures, which
10158 -- are basic declarations.
10160 if Is_Generic_Type (Formal_Type)
10161 and then not Is_Tagged_Type (Formal_Type)
10162 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10163 then
10164 Error_Msg_N
10165 ("invalid use of formal incomplete type", Param_Spec);
10167 elsif Ada_Version >= Ada_2012 then
10168 if Is_Tagged_Type (Formal_Type)
10169 and then (not From_Limited_With (Formal_Type)
10170 or else not In_Package_Body)
10171 then
10172 null;
10174 elsif Nkind_In (Context, N_Accept_Statement,
10175 N_Accept_Alternative,
10176 N_Entry_Body)
10177 or else (Nkind (Context) = N_Subprogram_Body
10178 and then Comes_From_Source (Context))
10179 then
10180 Error_Msg_NE
10181 ("invalid use of untagged incomplete type &",
10182 Ptype, Formal_Type);
10183 end if;
10185 else
10186 Error_Msg_NE
10187 ("invalid use of incomplete type&",
10188 Param_Spec, Formal_Type);
10190 -- Further checks on the legality of incomplete types
10191 -- in formal parts are delayed until the freeze point
10192 -- of the enclosing subprogram or access to subprogram.
10193 end if;
10194 end if;
10196 elsif Ekind (Formal_Type) = E_Void then
10197 Error_Msg_NE
10198 ("premature use of&",
10199 Parameter_Type (Param_Spec), Formal_Type);
10200 end if;
10202 -- Ada 2012 (AI-142): Handle aliased parameters
10204 if Ada_Version >= Ada_2012
10205 and then Aliased_Present (Param_Spec)
10206 then
10207 Set_Is_Aliased (Formal);
10208 end if;
10210 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10211 -- declaration corresponding to the null-excluding type of the
10212 -- formal in the enclosing scope. Finally, replace the parameter
10213 -- type of the formal with the internal subtype.
10215 if Ada_Version >= Ada_2005
10216 and then Null_Exclusion_Present (Param_Spec)
10217 then
10218 if not Is_Access_Type (Formal_Type) then
10219 Error_Msg_N
10220 ("`NOT NULL` allowed only for an access type", Param_Spec);
10222 else
10223 if Can_Never_Be_Null (Formal_Type)
10224 and then Comes_From_Source (Related_Nod)
10225 then
10226 Error_Msg_NE
10227 ("`NOT NULL` not allowed (& already excludes null)",
10228 Param_Spec, Formal_Type);
10229 end if;
10231 Formal_Type :=
10232 Create_Null_Excluding_Itype
10233 (T => Formal_Type,
10234 Related_Nod => Related_Nod,
10235 Scope_Id => Scope (Current_Scope));
10237 -- If the designated type of the itype is an itype that is
10238 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10239 -- on the access subtype, to prevent order-of-elaboration
10240 -- issues in the backend.
10242 -- Example:
10243 -- type T is access procedure;
10244 -- procedure Op (O : not null T);
10246 if Is_Itype (Directly_Designated_Type (Formal_Type))
10247 and then
10248 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10249 then
10250 Set_Has_Delayed_Freeze (Formal_Type);
10251 end if;
10252 end if;
10253 end if;
10255 -- An access formal type
10257 else
10258 Formal_Type :=
10259 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10261 -- No need to continue if we already notified errors
10263 if not Present (Formal_Type) then
10264 return;
10265 end if;
10267 -- Ada 2005 (AI-254)
10269 declare
10270 AD : constant Node_Id :=
10271 Access_To_Subprogram_Definition
10272 (Parameter_Type (Param_Spec));
10273 begin
10274 if Present (AD) and then Protected_Present (AD) then
10275 Formal_Type :=
10276 Replace_Anonymous_Access_To_Protected_Subprogram
10277 (Param_Spec);
10278 end if;
10279 end;
10280 end if;
10282 Set_Etype (Formal, Formal_Type);
10284 -- Deal with default expression if present
10286 Default := Expression (Param_Spec);
10288 if Present (Default) then
10289 Check_SPARK_05_Restriction
10290 ("default expression is not allowed", Default);
10292 if Out_Present (Param_Spec) then
10293 Error_Msg_N
10294 ("default initialization only allowed for IN parameters",
10295 Param_Spec);
10296 end if;
10298 -- Do the special preanalysis of the expression (see section on
10299 -- "Handling of Default Expressions" in the spec of package Sem).
10301 Preanalyze_Spec_Expression (Default, Formal_Type);
10303 -- An access to constant cannot be the default for
10304 -- an access parameter that is an access to variable.
10306 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10307 and then not Is_Access_Constant (Formal_Type)
10308 and then Is_Access_Type (Etype (Default))
10309 and then Is_Access_Constant (Etype (Default))
10310 then
10311 Error_Msg_N
10312 ("formal that is access to variable cannot be initialized "
10313 & "with an access-to-constant expression", Default);
10314 end if;
10316 -- Check that the designated type of an access parameter's default
10317 -- is not a class-wide type unless the parameter's designated type
10318 -- is also class-wide.
10320 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10321 and then not Designates_From_Limited_With (Formal_Type)
10322 and then Is_Class_Wide_Default (Default)
10323 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10324 then
10325 Error_Msg_N
10326 ("access to class-wide expression not allowed here", Default);
10327 end if;
10329 -- Check incorrect use of dynamically tagged expressions
10331 if Is_Tagged_Type (Formal_Type) then
10332 Check_Dynamically_Tagged_Expression
10333 (Expr => Default,
10334 Typ => Formal_Type,
10335 Related_Nod => Default);
10336 end if;
10337 end if;
10339 -- Ada 2005 (AI-231): Static checks
10341 if Ada_Version >= Ada_2005
10342 and then Is_Access_Type (Etype (Formal))
10343 and then Can_Never_Be_Null (Etype (Formal))
10344 then
10345 Null_Exclusion_Static_Checks (Param_Spec);
10346 end if;
10348 -- The following checks are relevant when SPARK_Mode is on as these
10349 -- are not standard Ada legality rules.
10351 if SPARK_Mode = On then
10352 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10354 -- A function cannot have a parameter of mode IN OUT or OUT
10355 -- (SPARK RM 6.1).
10357 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10358 Error_Msg_N
10359 ("function cannot have parameter of mode `OUT` or "
10360 & "`IN OUT`", Formal);
10362 -- A function cannot have an effectively volatile formal
10363 -- parameter (SPARK RM 7.1.3(10)).
10365 elsif Is_Effectively_Volatile (Formal) then
10366 Error_Msg_N
10367 ("function cannot have a volatile formal parameter",
10368 Formal);
10369 end if;
10371 -- A procedure cannot have an effectively volatile formal
10372 -- parameter of mode IN because it behaves as a constant
10373 -- (SPARK RM 7.1.3(6)).
10375 elsif Ekind (Scope (Formal)) = E_Procedure
10376 and then Ekind (Formal) = E_In_Parameter
10377 and then Is_Effectively_Volatile (Formal)
10378 then
10379 Error_Msg_N
10380 ("formal parameter of mode `IN` cannot be volatile", Formal);
10381 end if;
10382 end if;
10384 <<Continue>>
10385 Next (Param_Spec);
10386 end loop;
10388 -- If this is the formal part of a function specification, analyze the
10389 -- subtype mark in the context where the formals are visible but not
10390 -- yet usable, and may hide outer homographs.
10392 if Nkind (Related_Nod) = N_Function_Specification then
10393 Analyze_Return_Type (Related_Nod);
10394 end if;
10396 -- Now set the kind (mode) of each formal
10398 Param_Spec := First (T);
10399 while Present (Param_Spec) loop
10400 Formal := Defining_Identifier (Param_Spec);
10401 Set_Formal_Mode (Formal);
10403 if Ekind (Formal) = E_In_Parameter then
10404 Set_Default_Value (Formal, Expression (Param_Spec));
10406 if Present (Expression (Param_Spec)) then
10407 Default := Expression (Param_Spec);
10409 if Is_Scalar_Type (Etype (Default)) then
10410 if Nkind (Parameter_Type (Param_Spec)) /=
10411 N_Access_Definition
10412 then
10413 Formal_Type := Entity (Parameter_Type (Param_Spec));
10414 else
10415 Formal_Type :=
10416 Access_Definition
10417 (Related_Nod, Parameter_Type (Param_Spec));
10418 end if;
10420 Apply_Scalar_Range_Check (Default, Formal_Type);
10421 end if;
10422 end if;
10424 elsif Ekind (Formal) = E_Out_Parameter then
10425 Num_Out_Params := Num_Out_Params + 1;
10427 if Num_Out_Params = 1 then
10428 First_Out_Param := Formal;
10429 end if;
10431 elsif Ekind (Formal) = E_In_Out_Parameter then
10432 Num_Out_Params := Num_Out_Params + 1;
10433 end if;
10435 -- Skip remaining processing if formal type was in error
10437 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10438 goto Next_Parameter;
10439 end if;
10441 -- Force call by reference if aliased
10443 if Is_Aliased (Formal) then
10444 Set_Mechanism (Formal, By_Reference);
10446 -- Warn if user asked this to be passed by copy
10448 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10449 Error_Msg_N
10450 ("cannot pass aliased parameter & by copy??", Formal);
10451 end if;
10453 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10455 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10456 Set_Mechanism (Formal, By_Copy);
10458 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10459 Set_Mechanism (Formal, By_Reference);
10460 end if;
10462 <<Next_Parameter>>
10463 Next (Param_Spec);
10464 end loop;
10466 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10467 Set_Is_Only_Out_Parameter (First_Out_Param);
10468 end if;
10469 end Process_Formals;
10471 ----------------------------
10472 -- Reference_Body_Formals --
10473 ----------------------------
10475 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10476 Fs : Entity_Id;
10477 Fb : Entity_Id;
10479 begin
10480 if Error_Posted (Spec) then
10481 return;
10482 end if;
10484 -- Iterate over both lists. They may be of different lengths if the two
10485 -- specs are not conformant.
10487 Fs := First_Formal (Spec);
10488 Fb := First_Formal (Bod);
10489 while Present (Fs) and then Present (Fb) loop
10490 Generate_Reference (Fs, Fb, 'b');
10492 if Style_Check then
10493 Style.Check_Identifier (Fb, Fs);
10494 end if;
10496 Set_Spec_Entity (Fb, Fs);
10497 Set_Referenced (Fs, False);
10498 Next_Formal (Fs);
10499 Next_Formal (Fb);
10500 end loop;
10501 end Reference_Body_Formals;
10503 -------------------------
10504 -- Set_Actual_Subtypes --
10505 -------------------------
10507 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10508 Decl : Node_Id;
10509 Formal : Entity_Id;
10510 T : Entity_Id;
10511 First_Stmt : Node_Id := Empty;
10512 AS_Needed : Boolean;
10514 begin
10515 -- If this is an empty initialization procedure, no need to create
10516 -- actual subtypes (small optimization).
10518 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10519 return;
10520 end if;
10522 Formal := First_Formal (Subp);
10523 while Present (Formal) loop
10524 T := Etype (Formal);
10526 -- We never need an actual subtype for a constrained formal
10528 if Is_Constrained (T) then
10529 AS_Needed := False;
10531 -- If we have unknown discriminants, then we do not need an actual
10532 -- subtype, or more accurately we cannot figure it out. Note that
10533 -- all class-wide types have unknown discriminants.
10535 elsif Has_Unknown_Discriminants (T) then
10536 AS_Needed := False;
10538 -- At this stage we have an unconstrained type that may need an
10539 -- actual subtype. For sure the actual subtype is needed if we have
10540 -- an unconstrained array type.
10542 elsif Is_Array_Type (T) then
10543 AS_Needed := True;
10545 -- The only other case needing an actual subtype is an unconstrained
10546 -- record type which is an IN parameter (we cannot generate actual
10547 -- subtypes for the OUT or IN OUT case, since an assignment can
10548 -- change the discriminant values. However we exclude the case of
10549 -- initialization procedures, since discriminants are handled very
10550 -- specially in this context, see the section entitled "Handling of
10551 -- Discriminants" in Einfo.
10553 -- We also exclude the case of Discrim_SO_Functions (functions used
10554 -- in front end layout mode for size/offset values), since in such
10555 -- functions only discriminants are referenced, and not only are such
10556 -- subtypes not needed, but they cannot always be generated, because
10557 -- of order of elaboration issues.
10559 elsif Is_Record_Type (T)
10560 and then Ekind (Formal) = E_In_Parameter
10561 and then Chars (Formal) /= Name_uInit
10562 and then not Is_Unchecked_Union (T)
10563 and then not Is_Discrim_SO_Function (Subp)
10564 then
10565 AS_Needed := True;
10567 -- All other cases do not need an actual subtype
10569 else
10570 AS_Needed := False;
10571 end if;
10573 -- Generate actual subtypes for unconstrained arrays and
10574 -- unconstrained discriminated records.
10576 if AS_Needed then
10577 if Nkind (N) = N_Accept_Statement then
10579 -- If expansion is active, the formal is replaced by a local
10580 -- variable that renames the corresponding entry of the
10581 -- parameter block, and it is this local variable that may
10582 -- require an actual subtype.
10584 if Expander_Active then
10585 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10586 else
10587 Decl := Build_Actual_Subtype (T, Formal);
10588 end if;
10590 if Present (Handled_Statement_Sequence (N)) then
10591 First_Stmt :=
10592 First (Statements (Handled_Statement_Sequence (N)));
10593 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10594 Mark_Rewrite_Insertion (Decl);
10595 else
10596 -- If the accept statement has no body, there will be no
10597 -- reference to the actuals, so no need to compute actual
10598 -- subtypes.
10600 return;
10601 end if;
10603 else
10604 Decl := Build_Actual_Subtype (T, Formal);
10605 Prepend (Decl, Declarations (N));
10606 Mark_Rewrite_Insertion (Decl);
10607 end if;
10609 -- The declaration uses the bounds of an existing object, and
10610 -- therefore needs no constraint checks.
10612 Analyze (Decl, Suppress => All_Checks);
10614 -- We need to freeze manually the generated type when it is
10615 -- inserted anywhere else than in a declarative part.
10617 if Present (First_Stmt) then
10618 Insert_List_Before_And_Analyze (First_Stmt,
10619 Freeze_Entity (Defining_Identifier (Decl), N));
10621 -- Ditto if the type has a dynamic predicate, because the
10622 -- generated function will mention the actual subtype.
10624 elsif Has_Dynamic_Predicate_Aspect (T) then
10625 Insert_List_Before_And_Analyze (Decl,
10626 Freeze_Entity (Defining_Identifier (Decl), N));
10627 end if;
10629 if Nkind (N) = N_Accept_Statement
10630 and then Expander_Active
10631 then
10632 Set_Actual_Subtype (Renamed_Object (Formal),
10633 Defining_Identifier (Decl));
10634 else
10635 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10636 end if;
10637 end if;
10639 Next_Formal (Formal);
10640 end loop;
10641 end Set_Actual_Subtypes;
10643 ---------------------
10644 -- Set_Formal_Mode --
10645 ---------------------
10647 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10648 Spec : constant Node_Id := Parent (Formal_Id);
10649 Id : constant Entity_Id := Scope (Formal_Id);
10651 begin
10652 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10653 -- since we ensure that corresponding actuals are always valid at the
10654 -- point of the call.
10656 if Out_Present (Spec) then
10657 if Ekind_In (Id, E_Entry, E_Entry_Family)
10658 or else Is_Subprogram_Or_Generic_Subprogram (Id)
10659 then
10660 Set_Has_Out_Or_In_Out_Parameter (Id, True);
10661 end if;
10663 if Ekind_In (Id, E_Function, E_Generic_Function) then
10665 -- [IN] OUT parameters allowed for functions in Ada 2012
10667 if Ada_Version >= Ada_2012 then
10669 -- Even in Ada 2012 operators can only have IN parameters
10671 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10672 Error_Msg_N ("operators can only have IN parameters", Spec);
10673 end if;
10675 if In_Present (Spec) then
10676 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10677 else
10678 Set_Ekind (Formal_Id, E_Out_Parameter);
10679 end if;
10681 -- But not in earlier versions of Ada
10683 else
10684 Error_Msg_N ("functions can only have IN parameters", Spec);
10685 Set_Ekind (Formal_Id, E_In_Parameter);
10686 end if;
10688 elsif In_Present (Spec) then
10689 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10691 else
10692 Set_Ekind (Formal_Id, E_Out_Parameter);
10693 Set_Never_Set_In_Source (Formal_Id, True);
10694 Set_Is_True_Constant (Formal_Id, False);
10695 Set_Current_Value (Formal_Id, Empty);
10696 end if;
10698 else
10699 Set_Ekind (Formal_Id, E_In_Parameter);
10700 end if;
10702 -- Set Is_Known_Non_Null for access parameters since the language
10703 -- guarantees that access parameters are always non-null. We also set
10704 -- Can_Never_Be_Null, since there is no way to change the value.
10706 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10708 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10709 -- null; In Ada 2005, only if then null_exclusion is explicit.
10711 if Ada_Version < Ada_2005
10712 or else Can_Never_Be_Null (Etype (Formal_Id))
10713 then
10714 Set_Is_Known_Non_Null (Formal_Id);
10715 Set_Can_Never_Be_Null (Formal_Id);
10716 end if;
10718 -- Ada 2005 (AI-231): Null-exclusion access subtype
10720 elsif Is_Access_Type (Etype (Formal_Id))
10721 and then Can_Never_Be_Null (Etype (Formal_Id))
10722 then
10723 Set_Is_Known_Non_Null (Formal_Id);
10725 -- We can also set Can_Never_Be_Null (thus preventing some junk
10726 -- access checks) for the case of an IN parameter, which cannot
10727 -- be changed, or for an IN OUT parameter, which can be changed but
10728 -- not to a null value. But for an OUT parameter, the initial value
10729 -- passed in can be null, so we can't set this flag in that case.
10731 if Ekind (Formal_Id) /= E_Out_Parameter then
10732 Set_Can_Never_Be_Null (Formal_Id);
10733 end if;
10734 end if;
10736 Set_Mechanism (Formal_Id, Default_Mechanism);
10737 Set_Formal_Validity (Formal_Id);
10738 end Set_Formal_Mode;
10740 -------------------------
10741 -- Set_Formal_Validity --
10742 -------------------------
10744 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10745 begin
10746 -- If no validity checking, then we cannot assume anything about the
10747 -- validity of parameters, since we do not know there is any checking
10748 -- of the validity on the call side.
10750 if not Validity_Checks_On then
10751 return;
10753 -- If validity checking for parameters is enabled, this means we are
10754 -- not supposed to make any assumptions about argument values.
10756 elsif Validity_Check_Parameters then
10757 return;
10759 -- If we are checking in parameters, we will assume that the caller is
10760 -- also checking parameters, so we can assume the parameter is valid.
10762 elsif Ekind (Formal_Id) = E_In_Parameter
10763 and then Validity_Check_In_Params
10764 then
10765 Set_Is_Known_Valid (Formal_Id, True);
10767 -- Similar treatment for IN OUT parameters
10769 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10770 and then Validity_Check_In_Out_Params
10771 then
10772 Set_Is_Known_Valid (Formal_Id, True);
10773 end if;
10774 end Set_Formal_Validity;
10776 ------------------------
10777 -- Subtype_Conformant --
10778 ------------------------
10780 function Subtype_Conformant
10781 (New_Id : Entity_Id;
10782 Old_Id : Entity_Id;
10783 Skip_Controlling_Formals : Boolean := False) return Boolean
10785 Result : Boolean;
10786 begin
10787 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10788 Skip_Controlling_Formals => Skip_Controlling_Formals);
10789 return Result;
10790 end Subtype_Conformant;
10792 ---------------------
10793 -- Type_Conformant --
10794 ---------------------
10796 function Type_Conformant
10797 (New_Id : Entity_Id;
10798 Old_Id : Entity_Id;
10799 Skip_Controlling_Formals : Boolean := False) return Boolean
10801 Result : Boolean;
10802 begin
10803 May_Hide_Profile := False;
10804 Check_Conformance
10805 (New_Id, Old_Id, Type_Conformant, False, Result,
10806 Skip_Controlling_Formals => Skip_Controlling_Formals);
10807 return Result;
10808 end Type_Conformant;
10810 -------------------------------
10811 -- Valid_Operator_Definition --
10812 -------------------------------
10814 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10815 N : Integer := 0;
10816 F : Entity_Id;
10817 Id : constant Name_Id := Chars (Designator);
10818 N_OK : Boolean;
10820 begin
10821 F := First_Formal (Designator);
10822 while Present (F) loop
10823 N := N + 1;
10825 if Present (Default_Value (F)) then
10826 Error_Msg_N
10827 ("default values not allowed for operator parameters",
10828 Parent (F));
10830 -- For function instantiations that are operators, we must check
10831 -- separately that the corresponding generic only has in-parameters.
10832 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10833 -- an error could not arise in earlier versions of the language.
10835 elsif Ekind (F) /= E_In_Parameter then
10836 Error_Msg_N ("operators can only have IN parameters", F);
10837 end if;
10839 Next_Formal (F);
10840 end loop;
10842 -- Verify that user-defined operators have proper number of arguments
10843 -- First case of operators which can only be unary
10845 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10846 N_OK := (N = 1);
10848 -- Case of operators which can be unary or binary
10850 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10851 N_OK := (N in 1 .. 2);
10853 -- All other operators can only be binary
10855 else
10856 N_OK := (N = 2);
10857 end if;
10859 if not N_OK then
10860 Error_Msg_N
10861 ("incorrect number of arguments for operator", Designator);
10862 end if;
10864 if Id = Name_Op_Ne
10865 and then Base_Type (Etype (Designator)) = Standard_Boolean
10866 and then not Is_Intrinsic_Subprogram (Designator)
10867 then
10868 Error_Msg_N
10869 ("explicit definition of inequality not allowed", Designator);
10870 end if;
10871 end Valid_Operator_Definition;
10873 end Sem_Ch6;