2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blobe851346a50896e9953c61e2ce46c102cb2d06075
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Inline; use Inline;
45 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Layout; use Layout;
48 with Namet; use Namet;
49 with Lib; use Lib;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Output; use Output;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch10; use Sem_Ch10;
65 with Sem_Ch12; use Sem_Ch12;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Mech; use Sem_Mech;
73 with Sem_Prag; use Sem_Prag;
74 with Sem_Res; use Sem_Res;
75 with Sem_Util; use Sem_Util;
76 with Sem_Type; use Sem_Type;
77 with Sem_Warn; use Sem_Warn;
78 with Sinput; use Sinput;
79 with Stand; use Stand;
80 with Sinfo; use Sinfo;
81 with Sinfo.CN; use Sinfo.CN;
82 with Snames; use Snames;
83 with Stringt; use Stringt;
84 with Style;
85 with Stylesw; use Stylesw;
86 with Targparm; use Targparm;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
116 (N : Node_Id;
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
126 -- outer homographs.
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
136 (New_Id : Entity_Id;
137 Old_Id : Entity_Id;
138 Ctype : Conformance_Type;
139 Errmsg : Boolean;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 -- be called.
156 procedure Check_Subprogram_Order (N : Node_Id);
157 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
158 -- the alpha ordering rule for N if this ordering requirement applicable.
160 procedure Check_Returns
161 (HSS : Node_Id;
162 Mode : Character;
163 Err : out Boolean;
164 Proc : Entity_Id := Empty);
165 -- Called to check for missing return statements in a function body, or for
166 -- returns present in a procedure body which has No_Return set. HSS is the
167 -- handled statement sequence for the subprogram body. This procedure
168 -- checks all flow paths to make sure they either have return (Mode = 'F',
169 -- used for functions) or do not have a return (Mode = 'P', used for
170 -- No_Return procedures). The flag Err is set if there are any control
171 -- paths not explicitly terminated by a return in the function case, and is
172 -- True otherwise. Proc is the entity for the procedure case and is used
173 -- in posting the warning message.
175 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
176 -- In Ada 2012, a primitive equality operator on an untagged record type
177 -- must appear before the type is frozen, and have the same visibility as
178 -- that of the type. This procedure checks that this rule is met, and
179 -- otherwise emits an error on the subprogram declaration and a warning
180 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
181 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
182 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
183 -- is set, otherwise the call has no effect.
185 procedure Enter_Overloaded_Entity (S : Entity_Id);
186 -- This procedure makes S, a new overloaded entity, into the first visible
187 -- entity with that name.
189 function Is_Non_Overriding_Operation
190 (Prev_E : Entity_Id;
191 New_E : Entity_Id) return Boolean;
192 -- Enforce the rule given in 12.3(18): a private operation in an instance
193 -- overrides an inherited operation only if the corresponding operation
194 -- was overriding in the generic. This needs to be checked for primitive
195 -- operations of types derived (in the generic unit) from formal private
196 -- or formal derived types.
198 procedure Make_Inequality_Operator (S : Entity_Id);
199 -- Create the declaration for an inequality operator that is implicitly
200 -- created by a user-defined equality operator that yields a boolean.
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ---------------------------------------------
208 -- Analyze_Abstract_Subprogram_Declaration --
209 ---------------------------------------------
211 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
212 Designator : constant Entity_Id :=
213 Analyze_Subprogram_Specification (Specification (N));
214 Scop : constant Entity_Id := Current_Scope;
216 begin
217 -- The abstract subprogram declaration may be subject to pragma Ghost
218 -- with policy Ignore. Set the mode now to ensure that any nodes
219 -- generated during analysis and expansion are properly flagged as
220 -- ignored Ghost.
222 Set_Ghost_Mode (N);
223 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
225 Generate_Definition (Designator);
227 Set_Is_Abstract_Subprogram (Designator);
228 New_Overloaded_Entity (Designator);
229 Check_Delayed_Subprogram (Designator);
231 Set_Categorization_From_Scope (Designator, Scop);
233 -- An abstract subprogram declared within a Ghost region is rendered
234 -- Ghost (SPARK RM 6.9(2)).
236 if Comes_From_Source (Designator) and then Ghost_Mode > None then
237 Set_Is_Ghost_Entity (Designator);
238 end if;
240 if Ekind (Scope (Designator)) = E_Protected_Type then
241 Error_Msg_N
242 ("abstract subprogram not allowed in protected type", N);
244 -- Issue a warning if the abstract subprogram is neither a dispatching
245 -- operation nor an operation that overrides an inherited subprogram or
246 -- predefined operator, since this most likely indicates a mistake.
248 elsif Warn_On_Redundant_Constructs
249 and then not Is_Dispatching_Operation (Designator)
250 and then not Present (Overridden_Operation (Designator))
251 and then (not Is_Operator_Symbol_Name (Chars (Designator))
252 or else Scop /= Scope (Etype (First_Formal (Designator))))
253 then
254 Error_Msg_N
255 ("abstract subprogram is not dispatching or overriding?r?", N);
256 end if;
258 Generate_Reference_To_Formals (Designator);
259 Check_Eliminated (Designator);
261 if Has_Aspects (N) then
262 Analyze_Aspect_Specifications (N, Designator);
263 end if;
264 end Analyze_Abstract_Subprogram_Declaration;
266 ---------------------------------
267 -- Analyze_Expression_Function --
268 ---------------------------------
270 procedure Analyze_Expression_Function (N : Node_Id) is
271 Expr : constant Node_Id := Expression (N);
272 Loc : constant Source_Ptr := Sloc (N);
273 LocX : constant Source_Ptr := Sloc (Expr);
274 Spec : constant Node_Id := Specification (N);
276 Def_Id : Entity_Id;
278 Prev : Entity_Id;
279 -- If the expression is a completion, Prev is the entity whose
280 -- declaration is completed. Def_Id is needed to analyze the spec.
282 New_Body : Node_Id;
283 New_Spec : Node_Id;
284 Ret : Node_Id;
286 begin
287 -- This is one of the occasions on which we transform the tree during
288 -- semantic analysis. If this is a completion, transform the expression
289 -- function into an equivalent subprogram body, and analyze it.
291 -- Expression functions are inlined unconditionally. The back-end will
292 -- determine whether this is possible.
294 Inline_Processing_Required := True;
296 -- Create a specification for the generated body. This must be done
297 -- prior to the analysis of the initial declaration.
299 New_Spec := Copy_Subprogram_Spec (Spec);
300 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
302 -- If there are previous overloadable entities with the same name,
303 -- check whether any of them is completed by the expression function.
304 -- In a generic context a formal subprogram has no completion.
306 if Present (Prev)
307 and then Is_Overloadable (Prev)
308 and then not Is_Formal_Subprogram (Prev)
309 then
310 Def_Id := Analyze_Subprogram_Specification (Spec);
311 Prev := Find_Corresponding_Spec (N);
313 -- The previous entity may be an expression function as well, in
314 -- which case the redeclaration is illegal.
316 if Present (Prev)
317 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
318 N_Expression_Function
319 then
320 Error_Msg_Sloc := Sloc (Prev);
321 Error_Msg_N ("& conflicts with declaration#", Def_Id);
322 return;
323 end if;
324 end if;
326 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
328 New_Body :=
329 Make_Subprogram_Body (Loc,
330 Specification => New_Spec,
331 Declarations => Empty_List,
332 Handled_Statement_Sequence =>
333 Make_Handled_Sequence_Of_Statements (LocX,
334 Statements => New_List (Ret)));
336 -- If the expression completes a generic subprogram, we must create a
337 -- separate node for the body, because at instantiation the original
338 -- node of the generic copy must be a generic subprogram body, and
339 -- cannot be a expression function. Otherwise we just rewrite the
340 -- expression with the non-generic body.
342 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
343 Insert_After (N, New_Body);
345 -- Propagate any aspects or pragmas that apply to the expression
346 -- function to the proper body when the expression function acts
347 -- as a completion.
349 if Has_Aspects (N) then
350 Move_Aspects (N, To => New_Body);
351 end if;
353 Relocate_Pragmas_To_Body (New_Body);
355 Rewrite (N, Make_Null_Statement (Loc));
356 Set_Has_Completion (Prev, False);
357 Analyze (N);
358 Analyze (New_Body);
359 Set_Is_Inlined (Prev);
361 -- If the expression function is a completion, the previous declaration
362 -- must come from source. We know already that appears in the current
363 -- scope. The entity itself may be internally created if within a body
364 -- to be inlined.
366 elsif Present (Prev)
367 and then Comes_From_Source (Parent (Prev))
368 and then not Is_Formal_Subprogram (Prev)
369 then
370 Set_Has_Completion (Prev, False);
372 -- An expression function that is a completion freezes the
373 -- expression. This means freezing the return type, and if it is
374 -- an access type, freezing its designated type as well.
376 -- Note that we cannot defer this freezing to the analysis of the
377 -- expression itself, because a freeze node might appear in a nested
378 -- scope, leading to an elaboration order issue in gigi.
380 Freeze_Before (N, Etype (Prev));
382 if Is_Access_Type (Etype (Prev)) then
383 Freeze_Before (N, Designated_Type (Etype (Prev)));
384 end if;
386 -- For navigation purposes, indicate that the function is a body
388 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
389 Rewrite (N, New_Body);
391 -- Correct the parent pointer of the aspect specification list to
392 -- reference the rewritten node.
394 if Has_Aspects (N) then
395 Set_Parent (Aspect_Specifications (N), N);
396 end if;
398 -- Propagate any pragmas that apply to the expression function to the
399 -- proper body when the expression function acts as a completion.
400 -- Aspects are automatically transfered because of node rewriting.
402 Relocate_Pragmas_To_Body (N);
403 Analyze (N);
405 -- Prev is the previous entity with the same name, but it is can
406 -- be an unrelated spec that is not completed by the expression
407 -- function. In that case the relevant entity is the one in the body.
408 -- Not clear that the backend can inline it in this case ???
410 if Has_Completion (Prev) then
411 Set_Is_Inlined (Prev);
413 -- The formals of the expression function are body formals,
414 -- and do not appear in the ali file, which will only contain
415 -- references to the formals of the original subprogram spec.
417 declare
418 F1 : Entity_Id;
419 F2 : Entity_Id;
421 begin
422 F1 := First_Formal (Def_Id);
423 F2 := First_Formal (Prev);
425 while Present (F1) loop
426 Set_Spec_Entity (F1, F2);
427 Next_Formal (F1);
428 Next_Formal (F2);
429 end loop;
430 end;
432 else
433 Set_Is_Inlined (Defining_Entity (New_Body));
434 end if;
436 -- If this is not a completion, create both a declaration and a body, so
437 -- that the expression can be inlined whenever possible.
439 else
440 -- An expression function that is not a completion is not a
441 -- subprogram declaration, and thus cannot appear in a protected
442 -- definition.
444 if Nkind (Parent (N)) = N_Protected_Definition then
445 Error_Msg_N
446 ("an expression function is not a legal protected operation", N);
447 end if;
449 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
451 -- Correct the parent pointer of the aspect specification list to
452 -- reference the rewritten node.
454 if Has_Aspects (N) then
455 Set_Parent (Aspect_Specifications (N), N);
456 end if;
458 Analyze (N);
460 -- Within a generic pre-analyze the original expression for name
461 -- capture. The body is also generated but plays no role in
462 -- this because it is not part of the original source.
464 if Inside_A_Generic then
465 declare
466 Id : constant Entity_Id := Defining_Entity (N);
468 begin
469 Set_Has_Completion (Id);
470 Push_Scope (Id);
471 Install_Formals (Id);
472 Preanalyze_Spec_Expression (Expr, Etype (Id));
473 End_Scope;
474 end;
475 end if;
477 Set_Is_Inlined (Defining_Entity (N));
479 -- Establish the linkages between the spec and the body. These are
480 -- used when the expression function acts as the prefix of attribute
481 -- 'Access in order to freeze the original expression which has been
482 -- moved to the generated body.
484 Set_Corresponding_Body (N, Defining_Entity (New_Body));
485 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
487 -- To prevent premature freeze action, insert the new body at the end
488 -- of the current declarations, or at the end of the package spec.
489 -- However, resolve usage names now, to prevent spurious visibility
490 -- on later entities. Note that the function can now be called in
491 -- the current declarative part, which will appear to be prior to
492 -- the presence of the body in the code. There are nevertheless no
493 -- order of elaboration issues because all name resolution has taken
494 -- place at the point of declaration.
496 declare
497 Decls : List_Id := List_Containing (N);
498 Par : constant Node_Id := Parent (Decls);
499 Id : constant Entity_Id := Defining_Entity (N);
501 begin
502 -- If this is a wrapper created for in an instance for a formal
503 -- subprogram, insert body after declaration, to be analyzed when
504 -- the enclosing instance is analyzed.
506 if GNATprove_Mode
507 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
508 then
509 Insert_After (N, New_Body);
511 else
512 if Nkind (Par) = N_Package_Specification
513 and then Decls = Visible_Declarations (Par)
514 and then Present (Private_Declarations (Par))
515 and then not Is_Empty_List (Private_Declarations (Par))
516 then
517 Decls := Private_Declarations (Par);
518 end if;
520 Insert_After (Last (Decls), New_Body);
521 Push_Scope (Id);
522 Install_Formals (Id);
524 -- Preanalyze the expression for name capture, except in an
525 -- instance, where this has been done during generic analysis,
526 -- and will be redone when analyzing the body.
528 declare
529 Expr : constant Node_Id := Expression (Ret);
531 begin
532 Set_Parent (Expr, Ret);
534 if not In_Instance then
535 Preanalyze_Spec_Expression (Expr, Etype (Id));
536 end if;
537 end;
539 End_Scope;
540 end if;
541 end;
542 end if;
544 -- If the return expression is a static constant, we suppress warning
545 -- messages on unused formals, which in most cases will be noise.
547 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
548 Is_OK_Static_Expression (Expr));
549 end Analyze_Expression_Function;
551 ----------------------------------------
552 -- Analyze_Extended_Return_Statement --
553 ----------------------------------------
555 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
556 begin
557 Check_Compiler_Unit ("extended return statement", N);
558 Analyze_Return_Statement (N);
559 end Analyze_Extended_Return_Statement;
561 ----------------------------
562 -- Analyze_Function_Call --
563 ----------------------------
565 procedure Analyze_Function_Call (N : Node_Id) is
566 Actuals : constant List_Id := Parameter_Associations (N);
567 Func_Nam : constant Node_Id := Name (N);
568 Actual : Node_Id;
570 begin
571 Analyze (Func_Nam);
573 -- A call of the form A.B (X) may be an Ada 2005 call, which is
574 -- rewritten as B (A, X). If the rewriting is successful, the call
575 -- has been analyzed and we just return.
577 if Nkind (Func_Nam) = N_Selected_Component
578 and then Name (N) /= Func_Nam
579 and then Is_Rewrite_Substitution (N)
580 and then Present (Etype (N))
581 then
582 return;
583 end if;
585 -- If error analyzing name, then set Any_Type as result type and return
587 if Etype (Func_Nam) = Any_Type then
588 Set_Etype (N, Any_Type);
589 return;
590 end if;
592 -- Otherwise analyze the parameters
594 if Present (Actuals) then
595 Actual := First (Actuals);
596 while Present (Actual) loop
597 Analyze (Actual);
598 Check_Parameterless_Call (Actual);
599 Next (Actual);
600 end loop;
601 end if;
603 Analyze_Call (N);
604 end Analyze_Function_Call;
606 -----------------------------
607 -- Analyze_Function_Return --
608 -----------------------------
610 procedure Analyze_Function_Return (N : Node_Id) is
611 Loc : constant Source_Ptr := Sloc (N);
612 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
613 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
615 R_Type : constant Entity_Id := Etype (Scope_Id);
616 -- Function result subtype
618 procedure Check_Limited_Return (Expr : Node_Id);
619 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
620 -- limited types. Used only for simple return statements.
621 -- Expr is the expression returned.
623 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
624 -- Check that the return_subtype_indication properly matches the result
625 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
627 --------------------------
628 -- Check_Limited_Return --
629 --------------------------
631 procedure Check_Limited_Return (Expr : Node_Id) is
632 begin
633 -- Ada 2005 (AI-318-02): Return-by-reference types have been
634 -- removed and replaced by anonymous access results. This is an
635 -- incompatibility with Ada 95. Not clear whether this should be
636 -- enforced yet or perhaps controllable with special switch. ???
638 -- A limited interface that is not immutably limited is OK.
640 if Is_Limited_Interface (R_Type)
641 and then
642 not (Is_Task_Interface (R_Type)
643 or else Is_Protected_Interface (R_Type)
644 or else Is_Synchronized_Interface (R_Type))
645 then
646 null;
648 elsif Is_Limited_Type (R_Type)
649 and then not Is_Interface (R_Type)
650 and then Comes_From_Source (N)
651 and then not In_Instance_Body
652 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
653 then
654 -- Error in Ada 2005
656 if Ada_Version >= Ada_2005
657 and then not Debug_Flag_Dot_L
658 and then not GNAT_Mode
659 then
660 Error_Msg_N
661 ("(Ada 2005) cannot copy object of a limited type "
662 & "(RM-2005 6.5(5.5/2))", Expr);
664 if Is_Limited_View (R_Type) then
665 Error_Msg_N
666 ("\return by reference not permitted in Ada 2005", Expr);
667 end if;
669 -- Warn in Ada 95 mode, to give folks a heads up about this
670 -- incompatibility.
672 -- In GNAT mode, this is just a warning, to allow it to be
673 -- evilly turned off. Otherwise it is a real error.
675 -- In a generic context, simplify the warning because it makes
676 -- no sense to discuss pass-by-reference or copy.
678 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
679 if Inside_A_Generic then
680 Error_Msg_N
681 ("return of limited object not permitted in Ada 2005 "
682 & "(RM-2005 6.5(5.5/2))?y?", Expr);
684 elsif Is_Limited_View (R_Type) then
685 Error_Msg_N
686 ("return by reference not permitted in Ada 2005 "
687 & "(RM-2005 6.5(5.5/2))?y?", Expr);
688 else
689 Error_Msg_N
690 ("cannot copy object of a limited type in Ada 2005 "
691 & "(RM-2005 6.5(5.5/2))?y?", Expr);
692 end if;
694 -- Ada 95 mode, compatibility warnings disabled
696 else
697 return; -- skip continuation messages below
698 end if;
700 if not Inside_A_Generic then
701 Error_Msg_N
702 ("\consider switching to return of access type", Expr);
703 Explain_Limited_Type (R_Type, Expr);
704 end if;
705 end if;
706 end Check_Limited_Return;
708 -------------------------------------
709 -- Check_Return_Subtype_Indication --
710 -------------------------------------
712 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
713 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
715 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
716 -- Subtype given in the extended return statement (must match R_Type)
718 Subtype_Ind : constant Node_Id :=
719 Object_Definition (Original_Node (Obj_Decl));
721 R_Type_Is_Anon_Access : constant Boolean :=
722 Ekind_In (R_Type,
723 E_Anonymous_Access_Subprogram_Type,
724 E_Anonymous_Access_Protected_Subprogram_Type,
725 E_Anonymous_Access_Type);
726 -- True if return type of the function is an anonymous access type
727 -- Can't we make Is_Anonymous_Access_Type in einfo ???
729 R_Stm_Type_Is_Anon_Access : constant Boolean :=
730 Ekind_In (R_Stm_Type,
731 E_Anonymous_Access_Subprogram_Type,
732 E_Anonymous_Access_Protected_Subprogram_Type,
733 E_Anonymous_Access_Type);
734 -- True if type of the return object is an anonymous access type
736 procedure Error_No_Match (N : Node_Id);
737 -- Output error messages for case where types do not statically
738 -- match. N is the location for the messages.
740 --------------------
741 -- Error_No_Match --
742 --------------------
744 procedure Error_No_Match (N : Node_Id) is
745 begin
746 Error_Msg_N
747 ("subtype must statically match function result subtype", N);
749 if not Predicates_Match (R_Stm_Type, R_Type) then
750 Error_Msg_Node_2 := R_Type;
751 Error_Msg_NE
752 ("\predicate of& does not match predicate of&",
753 N, R_Stm_Type);
754 end if;
755 end Error_No_Match;
757 -- Start of processing for Check_Return_Subtype_Indication
759 begin
760 -- First, avoid cascaded errors
762 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
763 return;
764 end if;
766 -- "return access T" case; check that the return statement also has
767 -- "access T", and that the subtypes statically match:
768 -- if this is an access to subprogram the signatures must match.
770 if R_Type_Is_Anon_Access then
771 if R_Stm_Type_Is_Anon_Access then
773 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
774 then
775 if Base_Type (Designated_Type (R_Stm_Type)) /=
776 Base_Type (Designated_Type (R_Type))
777 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
778 then
779 Error_No_Match (Subtype_Mark (Subtype_Ind));
780 end if;
782 else
783 -- For two anonymous access to subprogram types, the
784 -- types themselves must be type conformant.
786 if not Conforming_Types
787 (R_Stm_Type, R_Type, Fully_Conformant)
788 then
789 Error_No_Match (Subtype_Ind);
790 end if;
791 end if;
793 else
794 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
795 end if;
797 -- If the return object is of an anonymous access type, then report
798 -- an error if the function's result type is not also anonymous.
800 elsif R_Stm_Type_Is_Anon_Access
801 and then not R_Type_Is_Anon_Access
802 then
803 Error_Msg_N ("anonymous access not allowed for function with "
804 & "named access result", Subtype_Ind);
806 -- Subtype indication case: check that the return object's type is
807 -- covered by the result type, and that the subtypes statically match
808 -- when the result subtype is constrained. Also handle record types
809 -- with unknown discriminants for which we have built the underlying
810 -- record view. Coverage is needed to allow specific-type return
811 -- objects when the result type is class-wide (see AI05-32).
813 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
814 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
815 and then
816 Covers
817 (Base_Type (R_Type),
818 Underlying_Record_View (Base_Type (R_Stm_Type))))
819 then
820 -- A null exclusion may be present on the return type, on the
821 -- function specification, on the object declaration or on the
822 -- subtype itself.
824 if Is_Access_Type (R_Type)
825 and then
826 (Can_Never_Be_Null (R_Type)
827 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
828 Can_Never_Be_Null (R_Stm_Type)
829 then
830 Error_No_Match (Subtype_Ind);
831 end if;
833 -- AI05-103: for elementary types, subtypes must statically match
835 if Is_Constrained (R_Type)
836 or else Is_Access_Type (R_Type)
837 then
838 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
839 Error_No_Match (Subtype_Ind);
840 end if;
841 end if;
843 -- All remaining cases are illegal
845 -- Note: previous versions of this subprogram allowed the return
846 -- value to be the ancestor of the return type if the return type
847 -- was a null extension. This was plainly incorrect.
849 else
850 Error_Msg_N
851 ("wrong type for return_subtype_indication", Subtype_Ind);
852 end if;
853 end Check_Return_Subtype_Indication;
855 ---------------------
856 -- Local Variables --
857 ---------------------
859 Expr : Node_Id;
860 Obj_Decl : Node_Id;
862 -- Start of processing for Analyze_Function_Return
864 begin
865 Set_Return_Present (Scope_Id);
867 if Nkind (N) = N_Simple_Return_Statement then
868 Expr := Expression (N);
870 -- Guard against a malformed expression. The parser may have tried to
871 -- recover but the node is not analyzable.
873 if Nkind (Expr) = N_Error then
874 Set_Etype (Expr, Any_Type);
875 Expander_Mode_Save_And_Set (False);
876 return;
878 else
879 -- The resolution of a controlled [extension] aggregate associated
880 -- with a return statement creates a temporary which needs to be
881 -- finalized on function exit. Wrap the return statement inside a
882 -- block so that the finalization machinery can detect this case.
883 -- This early expansion is done only when the return statement is
884 -- not part of a handled sequence of statements.
886 if Nkind_In (Expr, N_Aggregate,
887 N_Extension_Aggregate)
888 and then Needs_Finalization (R_Type)
889 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
890 then
891 Rewrite (N,
892 Make_Block_Statement (Loc,
893 Handled_Statement_Sequence =>
894 Make_Handled_Sequence_Of_Statements (Loc,
895 Statements => New_List (Relocate_Node (N)))));
897 Analyze (N);
898 return;
899 end if;
901 Analyze (Expr);
903 -- Ada 2005 (AI-251): If the type of the returned object is
904 -- an access to an interface type then we add an implicit type
905 -- conversion to force the displacement of the "this" pointer to
906 -- reference the secondary dispatch table. We cannot delay the
907 -- generation of this implicit conversion until the expansion
908 -- because in this case the type resolution changes the decoration
909 -- of the expression node to match R_Type; by contrast, if the
910 -- returned object is a class-wide interface type then it is too
911 -- early to generate here the implicit conversion since the return
912 -- statement may be rewritten by the expander into an extended
913 -- return statement whose expansion takes care of adding the
914 -- implicit type conversion to displace the pointer to the object.
916 if Expander_Active
917 and then Serious_Errors_Detected = 0
918 and then Is_Access_Type (R_Type)
919 and then Nkind (Expr) /= N_Null
920 and then Is_Interface (Designated_Type (R_Type))
921 and then Is_Progenitor (Designated_Type (R_Type),
922 Designated_Type (Etype (Expr)))
923 then
924 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
925 Analyze (Expr);
926 end if;
928 Resolve (Expr, R_Type);
929 Check_Limited_Return (Expr);
930 end if;
932 -- RETURN only allowed in SPARK as the last statement in function
934 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
935 and then
936 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
937 or else Present (Next (N)))
938 then
939 Check_SPARK_05_Restriction
940 ("RETURN should be the last statement in function", N);
941 end if;
943 else
944 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
945 Obj_Decl := Last (Return_Object_Declarations (N));
947 -- Analyze parts specific to extended_return_statement:
949 declare
950 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
951 HSS : constant Node_Id := Handled_Statement_Sequence (N);
953 begin
954 Expr := Expression (Obj_Decl);
956 -- Note: The check for OK_For_Limited_Init will happen in
957 -- Analyze_Object_Declaration; we treat it as a normal
958 -- object declaration.
960 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
961 Analyze (Obj_Decl);
963 Check_Return_Subtype_Indication (Obj_Decl);
965 if Present (HSS) then
966 Analyze (HSS);
968 if Present (Exception_Handlers (HSS)) then
970 -- ???Has_Nested_Block_With_Handler needs to be set.
971 -- Probably by creating an actual N_Block_Statement.
972 -- Probably in Expand.
974 null;
975 end if;
976 end if;
978 -- Mark the return object as referenced, since the return is an
979 -- implicit reference of the object.
981 Set_Referenced (Defining_Identifier (Obj_Decl));
983 Check_References (Stm_Entity);
985 -- Check RM 6.5 (5.9/3)
987 if Has_Aliased then
988 if Ada_Version < Ada_2012 then
990 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
991 -- Can it really happen (extended return???)
993 Error_Msg_N
994 ("aliased only allowed for limited return objects "
995 & "in Ada 2012??", N);
997 elsif not Is_Limited_View (R_Type) then
998 Error_Msg_N
999 ("aliased only allowed for limited return objects", N);
1000 end if;
1001 end if;
1002 end;
1003 end if;
1005 -- Case of Expr present
1007 if Present (Expr)
1009 -- Defend against previous errors
1011 and then Nkind (Expr) /= N_Empty
1012 and then Present (Etype (Expr))
1013 then
1014 -- Apply constraint check. Note that this is done before the implicit
1015 -- conversion of the expression done for anonymous access types to
1016 -- ensure correct generation of the null-excluding check associated
1017 -- with null-excluding expressions found in return statements.
1019 Apply_Constraint_Check (Expr, R_Type);
1021 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1022 -- type, apply an implicit conversion of the expression to that type
1023 -- to force appropriate static and run-time accessibility checks.
1025 if Ada_Version >= Ada_2005
1026 and then Ekind (R_Type) = E_Anonymous_Access_Type
1027 then
1028 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1029 Analyze_And_Resolve (Expr, R_Type);
1031 -- If this is a local anonymous access to subprogram, the
1032 -- accessibility check can be applied statically. The return is
1033 -- illegal if the access type of the return expression is declared
1034 -- inside of the subprogram (except if it is the subtype indication
1035 -- of an extended return statement).
1037 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1038 if not Comes_From_Source (Current_Scope)
1039 or else Ekind (Current_Scope) = E_Return_Statement
1040 then
1041 null;
1043 elsif
1044 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1045 then
1046 Error_Msg_N ("cannot return local access to subprogram", N);
1047 end if;
1049 -- The expression cannot be of a formal incomplete type
1051 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1052 and then Is_Generic_Type (Etype (Expr))
1053 then
1054 Error_Msg_N
1055 ("cannot return expression of a formal incomplete type", N);
1056 end if;
1058 -- If the result type is class-wide, then check that the return
1059 -- expression's type is not declared at a deeper level than the
1060 -- function (RM05-6.5(5.6/2)).
1062 if Ada_Version >= Ada_2005
1063 and then Is_Class_Wide_Type (R_Type)
1064 then
1065 if Type_Access_Level (Etype (Expr)) >
1066 Subprogram_Access_Level (Scope_Id)
1067 then
1068 Error_Msg_N
1069 ("level of return expression type is deeper than "
1070 & "class-wide function!", Expr);
1071 end if;
1072 end if;
1074 -- Check incorrect use of dynamically tagged expression
1076 if Is_Tagged_Type (R_Type) then
1077 Check_Dynamically_Tagged_Expression
1078 (Expr => Expr,
1079 Typ => R_Type,
1080 Related_Nod => N);
1081 end if;
1083 -- ??? A real run-time accessibility check is needed in cases
1084 -- involving dereferences of access parameters. For now we just
1085 -- check the static cases.
1087 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1088 and then Is_Limited_View (Etype (Scope_Id))
1089 and then Object_Access_Level (Expr) >
1090 Subprogram_Access_Level (Scope_Id)
1091 then
1092 -- Suppress the message in a generic, where the rewriting
1093 -- is irrelevant.
1095 if Inside_A_Generic then
1096 null;
1098 else
1099 Rewrite (N,
1100 Make_Raise_Program_Error (Loc,
1101 Reason => PE_Accessibility_Check_Failed));
1102 Analyze (N);
1104 Error_Msg_Warn := SPARK_Mode /= On;
1105 Error_Msg_N ("cannot return a local value by reference<<", N);
1106 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1107 end if;
1108 end if;
1110 if Known_Null (Expr)
1111 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1112 and then Null_Exclusion_Present (Parent (Scope_Id))
1113 then
1114 Apply_Compile_Time_Constraint_Error
1115 (N => Expr,
1116 Msg => "(Ada 2005) null not allowed for "
1117 & "null-excluding return??",
1118 Reason => CE_Null_Not_Allowed);
1119 end if;
1121 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1122 -- has no initializing expression.
1124 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1125 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1126 Subprogram_Access_Level (Scope_Id)
1127 then
1128 Error_Msg_N
1129 ("level of return expression type is deeper than "
1130 & "class-wide function!", Obj_Decl);
1131 end if;
1132 end if;
1133 end Analyze_Function_Return;
1135 -------------------------------------
1136 -- Analyze_Generic_Subprogram_Body --
1137 -------------------------------------
1139 procedure Analyze_Generic_Subprogram_Body
1140 (N : Node_Id;
1141 Gen_Id : Entity_Id)
1143 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1144 Kind : constant Entity_Kind := Ekind (Gen_Id);
1145 Body_Id : Entity_Id;
1146 New_N : Node_Id;
1147 Spec : Node_Id;
1149 begin
1150 -- Copy body and disable expansion while analyzing the generic For a
1151 -- stub, do not copy the stub (which would load the proper body), this
1152 -- will be done when the proper body is analyzed.
1154 if Nkind (N) /= N_Subprogram_Body_Stub then
1155 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1156 Rewrite (N, New_N);
1158 -- Once the contents of the generic copy and the template are
1159 -- swapped, do the same for their respective aspect specifications.
1161 Exchange_Aspects (N, New_N);
1163 -- Collect all contract-related source pragmas found within the
1164 -- template and attach them to the contract of the subprogram body.
1165 -- This contract is used in the capture of global references within
1166 -- annotations.
1168 Create_Generic_Contract (N);
1170 Start_Generic;
1171 end if;
1173 Spec := Specification (N);
1175 -- Within the body of the generic, the subprogram is callable, and
1176 -- behaves like the corresponding non-generic unit.
1178 Body_Id := Defining_Entity (Spec);
1180 if Kind = E_Generic_Procedure
1181 and then Nkind (Spec) /= N_Procedure_Specification
1182 then
1183 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1184 return;
1186 elsif Kind = E_Generic_Function
1187 and then Nkind (Spec) /= N_Function_Specification
1188 then
1189 Error_Msg_N ("invalid body for generic function ", Body_Id);
1190 return;
1191 end if;
1193 Set_Corresponding_Body (Gen_Decl, Body_Id);
1195 if Has_Completion (Gen_Id)
1196 and then Nkind (Parent (N)) /= N_Subunit
1197 then
1198 Error_Msg_N ("duplicate generic body", N);
1199 return;
1200 else
1201 Set_Has_Completion (Gen_Id);
1202 end if;
1204 if Nkind (N) = N_Subprogram_Body_Stub then
1205 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1206 else
1207 Set_Corresponding_Spec (N, Gen_Id);
1208 end if;
1210 if Nkind (Parent (N)) = N_Compilation_Unit then
1211 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1212 end if;
1214 -- Make generic parameters immediately visible in the body. They are
1215 -- needed to process the formals declarations. Then make the formals
1216 -- visible in a separate step.
1218 Push_Scope (Gen_Id);
1220 declare
1221 E : Entity_Id;
1222 First_Ent : Entity_Id;
1224 begin
1225 First_Ent := First_Entity (Gen_Id);
1227 E := First_Ent;
1228 while Present (E) and then not Is_Formal (E) loop
1229 Install_Entity (E);
1230 Next_Entity (E);
1231 end loop;
1233 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1235 -- Now generic formals are visible, and the specification can be
1236 -- analyzed, for subsequent conformance check.
1238 Body_Id := Analyze_Subprogram_Specification (Spec);
1240 -- Make formal parameters visible
1242 if Present (E) then
1244 -- E is the first formal parameter, we loop through the formals
1245 -- installing them so that they will be visible.
1247 Set_First_Entity (Gen_Id, E);
1248 while Present (E) loop
1249 Install_Entity (E);
1250 Next_Formal (E);
1251 end loop;
1252 end if;
1254 -- Visible generic entity is callable within its own body
1256 Set_Ekind (Gen_Id, Ekind (Body_Id));
1257 Set_Ekind (Body_Id, E_Subprogram_Body);
1258 Set_Convention (Body_Id, Convention (Gen_Id));
1259 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1260 Set_Scope (Body_Id, Scope (Gen_Id));
1262 -- Inherit the "ghostness" of the generic spec. Note that this
1263 -- property is not directly inherited as the body may be subject
1264 -- to a different Ghost assertion policy.
1266 if Is_Ghost_Entity (Gen_Id) or else Ghost_Mode > None then
1267 Set_Is_Ghost_Entity (Body_Id);
1269 -- The Ghost policy in effect at the point of declaration and at
1270 -- the point of completion must match (SPARK RM 6.9(14)).
1272 Check_Ghost_Completion (Gen_Id, Body_Id);
1273 end if;
1275 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1277 if Nkind (N) = N_Subprogram_Body_Stub then
1279 -- No body to analyze, so restore state of generic unit
1281 Set_Ekind (Gen_Id, Kind);
1282 Set_Ekind (Body_Id, Kind);
1284 if Present (First_Ent) then
1285 Set_First_Entity (Gen_Id, First_Ent);
1286 end if;
1288 End_Scope;
1289 return;
1290 end if;
1292 -- If this is a compilation unit, it must be made visible explicitly,
1293 -- because the compilation of the declaration, unlike other library
1294 -- unit declarations, does not. If it is not a unit, the following
1295 -- is redundant but harmless.
1297 Set_Is_Immediately_Visible (Gen_Id);
1298 Reference_Body_Formals (Gen_Id, Body_Id);
1300 if Is_Child_Unit (Gen_Id) then
1301 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1302 end if;
1304 Set_Actual_Subtypes (N, Current_Scope);
1306 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1307 Set_SPARK_Pragma_Inherited (Body_Id, True);
1309 -- Analyze any aspect specifications that appear on the generic
1310 -- subprogram body.
1312 if Has_Aspects (N) then
1313 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1314 end if;
1316 Analyze_Declarations (Declarations (N));
1317 Check_Completion;
1319 -- When a generic subprogram body appears inside a package, its
1320 -- contract is analyzed at the end of the package body declarations.
1321 -- This is due to the delay with respect of the package contract upon
1322 -- which the body contract may depend. When the generic subprogram
1323 -- body is a compilation unit, this delay is not necessary.
1325 if Nkind (Parent (N)) = N_Compilation_Unit then
1326 Analyze_Subprogram_Body_Contract (Body_Id);
1328 -- Capture all global references in a generic subprogram body
1329 -- that acts as a compilation unit now that the contract has
1330 -- been analyzed.
1332 Save_Global_References_In_Contract
1333 (Templ => Original_Node (N),
1334 Gen_Id => Gen_Id);
1335 end if;
1337 Analyze (Handled_Statement_Sequence (N));
1338 Save_Global_References (Original_Node (N));
1340 -- Prior to exiting the scope, include generic formals again (if any
1341 -- are present) in the set of local entities.
1343 if Present (First_Ent) then
1344 Set_First_Entity (Gen_Id, First_Ent);
1345 end if;
1347 Check_References (Gen_Id);
1348 end;
1350 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1351 End_Scope;
1352 Check_Subprogram_Order (N);
1354 -- Outside of its body, unit is generic again
1356 Set_Ekind (Gen_Id, Kind);
1357 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1359 if Style_Check then
1360 Style.Check_Identifier (Body_Id, Gen_Id);
1361 end if;
1363 End_Generic;
1364 end Analyze_Generic_Subprogram_Body;
1366 ----------------------------
1367 -- Analyze_Null_Procedure --
1368 ----------------------------
1370 procedure Analyze_Null_Procedure
1371 (N : Node_Id;
1372 Is_Completion : out Boolean)
1374 Loc : constant Source_Ptr := Sloc (N);
1375 Spec : constant Node_Id := Specification (N);
1376 Designator : Entity_Id;
1377 Form : Node_Id;
1378 Null_Body : Node_Id := Empty;
1379 Prev : Entity_Id;
1381 begin
1382 -- Capture the profile of the null procedure before analysis, for
1383 -- expansion at the freeze point and at each point of call. The body is
1384 -- used if the procedure has preconditions, or if it is a completion. In
1385 -- the first case the body is analyzed at the freeze point, in the other
1386 -- it replaces the null procedure declaration.
1388 Null_Body :=
1389 Make_Subprogram_Body (Loc,
1390 Specification => New_Copy_Tree (Spec),
1391 Declarations => New_List,
1392 Handled_Statement_Sequence =>
1393 Make_Handled_Sequence_Of_Statements (Loc,
1394 Statements => New_List (Make_Null_Statement (Loc))));
1396 -- Create new entities for body and formals
1398 Set_Defining_Unit_Name (Specification (Null_Body),
1399 Make_Defining_Identifier
1400 (Sloc (Defining_Entity (N)),
1401 Chars (Defining_Entity (N))));
1403 Form := First (Parameter_Specifications (Specification (Null_Body)));
1404 while Present (Form) loop
1405 Set_Defining_Identifier (Form,
1406 Make_Defining_Identifier
1407 (Sloc (Defining_Identifier (Form)),
1408 Chars (Defining_Identifier (Form))));
1409 Next (Form);
1410 end loop;
1412 -- Determine whether the null procedure may be a completion of a generic
1413 -- suprogram, in which case we use the new null body as the completion
1414 -- and set minimal semantic information on the original declaration,
1415 -- which is rewritten as a null statement.
1417 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1419 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1420 Insert_Before (N, Null_Body);
1421 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1423 Rewrite (N, Make_Null_Statement (Loc));
1424 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1425 Is_Completion := True;
1426 return;
1428 else
1429 -- Resolve the types of the formals now, because the freeze point
1430 -- may appear in a different context, e.g. an instantiation.
1432 Form := First (Parameter_Specifications (Specification (Null_Body)));
1433 while Present (Form) loop
1434 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1435 Find_Type (Parameter_Type (Form));
1437 elsif
1438 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1439 then
1440 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1442 else
1443 -- The case of a null procedure with a formal that is an
1444 -- access_to_subprogram type, and that is used as an actual
1445 -- in an instantiation is left to the enthusiastic reader.
1447 null;
1448 end if;
1450 Next (Form);
1451 end loop;
1452 end if;
1454 -- If there are previous overloadable entities with the same name,
1455 -- check whether any of them is completed by the null procedure.
1457 if Present (Prev) and then Is_Overloadable (Prev) then
1458 Designator := Analyze_Subprogram_Specification (Spec);
1459 Prev := Find_Corresponding_Spec (N);
1460 end if;
1462 if No (Prev) or else not Comes_From_Source (Prev) then
1463 Designator := Analyze_Subprogram_Specification (Spec);
1464 Set_Has_Completion (Designator);
1466 -- Signal to caller that this is a procedure declaration
1468 Is_Completion := False;
1470 -- Null procedures are always inlined, but generic formal subprograms
1471 -- which appear as such in the internal instance of formal packages,
1472 -- need no completion and are not marked Inline.
1474 if Expander_Active
1475 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1476 then
1477 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1478 Set_Body_To_Inline (N, Null_Body);
1479 Set_Is_Inlined (Designator);
1480 end if;
1482 else
1483 -- The null procedure is a completion. We unconditionally rewrite
1484 -- this as a null body (even if expansion is not active), because
1485 -- there are various error checks that are applied on this body
1486 -- when it is analyzed (e.g. correct aspect placement).
1488 if Has_Completion (Prev) then
1489 Error_Msg_Sloc := Sloc (Prev);
1490 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1491 end if;
1493 Is_Completion := True;
1494 Rewrite (N, Null_Body);
1495 Analyze (N);
1496 end if;
1497 end Analyze_Null_Procedure;
1499 -----------------------------
1500 -- Analyze_Operator_Symbol --
1501 -----------------------------
1503 -- An operator symbol such as "+" or "and" may appear in context where the
1504 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1505 -- is just a string, as in (conjunction = "or"). In these cases the parser
1506 -- generates this node, and the semantics does the disambiguation. Other
1507 -- such case are actuals in an instantiation, the generic unit in an
1508 -- instantiation, and pragma arguments.
1510 procedure Analyze_Operator_Symbol (N : Node_Id) is
1511 Par : constant Node_Id := Parent (N);
1513 begin
1514 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1515 or else Nkind (Par) = N_Function_Instantiation
1516 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1517 or else (Nkind (Par) = N_Pragma_Argument_Association
1518 and then not Is_Pragma_String_Literal (Par))
1519 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1520 or else (Nkind (Par) = N_Attribute_Reference
1521 and then Attribute_Name (Par) /= Name_Value)
1522 then
1523 Find_Direct_Name (N);
1525 else
1526 Change_Operator_Symbol_To_String_Literal (N);
1527 Analyze (N);
1528 end if;
1529 end Analyze_Operator_Symbol;
1531 -----------------------------------
1532 -- Analyze_Parameter_Association --
1533 -----------------------------------
1535 procedure Analyze_Parameter_Association (N : Node_Id) is
1536 begin
1537 Analyze (Explicit_Actual_Parameter (N));
1538 end Analyze_Parameter_Association;
1540 ----------------------------
1541 -- Analyze_Procedure_Call --
1542 ----------------------------
1544 procedure Analyze_Procedure_Call (N : Node_Id) is
1545 Loc : constant Source_Ptr := Sloc (N);
1546 P : constant Node_Id := Name (N);
1547 Actuals : constant List_Id := Parameter_Associations (N);
1548 Actual : Node_Id;
1549 New_N : Node_Id;
1551 procedure Analyze_Call_And_Resolve;
1552 -- Do Analyze and Resolve calls for procedure call
1553 -- At end, check illegal order dependence.
1555 ------------------------------
1556 -- Analyze_Call_And_Resolve --
1557 ------------------------------
1559 procedure Analyze_Call_And_Resolve is
1560 begin
1561 if Nkind (N) = N_Procedure_Call_Statement then
1562 Analyze_Call (N);
1563 Resolve (N, Standard_Void_Type);
1564 else
1565 Analyze (N);
1566 end if;
1567 end Analyze_Call_And_Resolve;
1569 -- Start of processing for Analyze_Procedure_Call
1571 begin
1572 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1573 -- a procedure call or an entry call. The prefix may denote an access
1574 -- to subprogram type, in which case an implicit dereference applies.
1575 -- If the prefix is an indexed component (without implicit dereference)
1576 -- then the construct denotes a call to a member of an entire family.
1577 -- If the prefix is a simple name, it may still denote a call to a
1578 -- parameterless member of an entry family. Resolution of these various
1579 -- interpretations is delicate.
1581 Analyze (P);
1583 -- If this is a call of the form Obj.Op, the call may have been
1584 -- analyzed and possibly rewritten into a block, in which case
1585 -- we are done.
1587 if Analyzed (N) then
1588 return;
1589 end if;
1591 -- If there is an error analyzing the name (which may have been
1592 -- rewritten if the original call was in prefix notation) then error
1593 -- has been emitted already, mark node and return.
1595 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1596 Set_Etype (N, Any_Type);
1597 return;
1598 end if;
1600 -- The name of the procedure call may reference an entity subject to
1601 -- pragma Ghost with policy Ignore. Set the mode now to ensure that any
1602 -- nodes generated during analysis and expansion are properly flagged as
1603 -- ignored Ghost.
1605 Set_Ghost_Mode (N);
1607 -- Otherwise analyze the parameters
1609 if Present (Actuals) then
1610 Actual := First (Actuals);
1612 while Present (Actual) loop
1613 Analyze (Actual);
1614 Check_Parameterless_Call (Actual);
1615 Next (Actual);
1616 end loop;
1617 end if;
1619 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1621 if Nkind (P) = N_Attribute_Reference
1622 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1623 Name_Elab_Body,
1624 Name_Elab_Subp_Body)
1625 then
1626 if Present (Actuals) then
1627 Error_Msg_N
1628 ("no parameters allowed for this call", First (Actuals));
1629 return;
1630 end if;
1632 Set_Etype (N, Standard_Void_Type);
1633 Set_Analyzed (N);
1635 elsif Is_Entity_Name (P)
1636 and then Is_Record_Type (Etype (Entity (P)))
1637 and then Remote_AST_I_Dereference (P)
1638 then
1639 return;
1641 elsif Is_Entity_Name (P)
1642 and then Ekind (Entity (P)) /= E_Entry_Family
1643 then
1644 if Is_Access_Type (Etype (P))
1645 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1646 and then No (Actuals)
1647 and then Comes_From_Source (N)
1648 then
1649 Error_Msg_N ("missing explicit dereference in call", N);
1650 end if;
1652 Analyze_Call_And_Resolve;
1654 -- If the prefix is the simple name of an entry family, this is
1655 -- a parameterless call from within the task body itself.
1657 elsif Is_Entity_Name (P)
1658 and then Nkind (P) = N_Identifier
1659 and then Ekind (Entity (P)) = E_Entry_Family
1660 and then Present (Actuals)
1661 and then No (Next (First (Actuals)))
1662 then
1663 -- Can be call to parameterless entry family. What appears to be the
1664 -- sole argument is in fact the entry index. Rewrite prefix of node
1665 -- accordingly. Source representation is unchanged by this
1666 -- transformation.
1668 New_N :=
1669 Make_Indexed_Component (Loc,
1670 Prefix =>
1671 Make_Selected_Component (Loc,
1672 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1673 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1674 Expressions => Actuals);
1675 Set_Name (N, New_N);
1676 Set_Etype (New_N, Standard_Void_Type);
1677 Set_Parameter_Associations (N, No_List);
1678 Analyze_Call_And_Resolve;
1680 elsif Nkind (P) = N_Explicit_Dereference then
1681 if Ekind (Etype (P)) = E_Subprogram_Type then
1682 Analyze_Call_And_Resolve;
1683 else
1684 Error_Msg_N ("expect access to procedure in call", P);
1685 end if;
1687 -- The name can be a selected component or an indexed component that
1688 -- yields an access to subprogram. Such a prefix is legal if the call
1689 -- has parameter associations.
1691 elsif Is_Access_Type (Etype (P))
1692 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1693 then
1694 if Present (Actuals) then
1695 Analyze_Call_And_Resolve;
1696 else
1697 Error_Msg_N ("missing explicit dereference in call ", N);
1698 end if;
1700 -- If not an access to subprogram, then the prefix must resolve to the
1701 -- name of an entry, entry family, or protected operation.
1703 -- For the case of a simple entry call, P is a selected component where
1704 -- the prefix is the task and the selector name is the entry. A call to
1705 -- a protected procedure will have the same syntax. If the protected
1706 -- object contains overloaded operations, the entity may appear as a
1707 -- function, the context will select the operation whose type is Void.
1709 elsif Nkind (P) = N_Selected_Component
1710 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1711 E_Procedure,
1712 E_Function)
1713 then
1714 Analyze_Call_And_Resolve;
1716 elsif Nkind (P) = N_Selected_Component
1717 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1718 and then Present (Actuals)
1719 and then No (Next (First (Actuals)))
1720 then
1721 -- Can be call to parameterless entry family. What appears to be the
1722 -- sole argument is in fact the entry index. Rewrite prefix of node
1723 -- accordingly. Source representation is unchanged by this
1724 -- transformation.
1726 New_N :=
1727 Make_Indexed_Component (Loc,
1728 Prefix => New_Copy (P),
1729 Expressions => Actuals);
1730 Set_Name (N, New_N);
1731 Set_Etype (New_N, Standard_Void_Type);
1732 Set_Parameter_Associations (N, No_List);
1733 Analyze_Call_And_Resolve;
1735 -- For the case of a reference to an element of an entry family, P is
1736 -- an indexed component whose prefix is a selected component (task and
1737 -- entry family), and whose index is the entry family index.
1739 elsif Nkind (P) = N_Indexed_Component
1740 and then Nkind (Prefix (P)) = N_Selected_Component
1741 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1742 then
1743 Analyze_Call_And_Resolve;
1745 -- If the prefix is the name of an entry family, it is a call from
1746 -- within the task body itself.
1748 elsif Nkind (P) = N_Indexed_Component
1749 and then Nkind (Prefix (P)) = N_Identifier
1750 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1751 then
1752 New_N :=
1753 Make_Selected_Component (Loc,
1754 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1755 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1756 Rewrite (Prefix (P), New_N);
1757 Analyze (P);
1758 Analyze_Call_And_Resolve;
1760 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1761 -- procedure name, so the construct can only be a qualified expression.
1763 elsif Nkind (P) = N_Qualified_Expression
1764 and then Ada_Version >= Ada_2012
1765 then
1766 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1767 Analyze (N);
1769 -- Anything else is an error
1771 else
1772 Error_Msg_N ("invalid procedure or entry call", N);
1773 end if;
1774 end Analyze_Procedure_Call;
1776 ------------------------------
1777 -- Analyze_Return_Statement --
1778 ------------------------------
1780 procedure Analyze_Return_Statement (N : Node_Id) is
1782 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1783 N_Extended_Return_Statement));
1785 Returns_Object : constant Boolean :=
1786 Nkind (N) = N_Extended_Return_Statement
1787 or else
1788 (Nkind (N) = N_Simple_Return_Statement
1789 and then Present (Expression (N)));
1790 -- True if we're returning something; that is, "return <expression>;"
1791 -- or "return Result : T [:= ...]". False for "return;". Used for error
1792 -- checking: If Returns_Object is True, N should apply to a function
1793 -- body; otherwise N should apply to a procedure body, entry body,
1794 -- accept statement, or extended return statement.
1796 function Find_What_It_Applies_To return Entity_Id;
1797 -- Find the entity representing the innermost enclosing body, accept
1798 -- statement, or extended return statement. If the result is a callable
1799 -- construct or extended return statement, then this will be the value
1800 -- of the Return_Applies_To attribute. Otherwise, the program is
1801 -- illegal. See RM-6.5(4/2).
1803 -----------------------------
1804 -- Find_What_It_Applies_To --
1805 -----------------------------
1807 function Find_What_It_Applies_To return Entity_Id is
1808 Result : Entity_Id := Empty;
1810 begin
1811 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1812 -- and postconditions.
1814 for J in reverse 0 .. Scope_Stack.Last loop
1815 Result := Scope_Stack.Table (J).Entity;
1816 exit when not Ekind_In (Result, E_Block, E_Loop)
1817 and then Chars (Result) /= Name_uPostconditions;
1818 end loop;
1820 pragma Assert (Present (Result));
1821 return Result;
1822 end Find_What_It_Applies_To;
1824 -- Local declarations
1826 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1827 Kind : constant Entity_Kind := Ekind (Scope_Id);
1828 Loc : constant Source_Ptr := Sloc (N);
1829 Stm_Entity : constant Entity_Id :=
1830 New_Internal_Entity
1831 (E_Return_Statement, Current_Scope, Loc, 'R');
1833 -- Start of processing for Analyze_Return_Statement
1835 begin
1836 Set_Return_Statement_Entity (N, Stm_Entity);
1838 Set_Etype (Stm_Entity, Standard_Void_Type);
1839 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1841 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1842 -- (4/2): an inner return statement will apply to this extended return.
1844 if Nkind (N) = N_Extended_Return_Statement then
1845 Push_Scope (Stm_Entity);
1846 end if;
1848 -- Check that pragma No_Return is obeyed. Don't complain about the
1849 -- implicitly-generated return that is placed at the end.
1851 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1852 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1853 end if;
1855 -- Warn on any unassigned OUT parameters if in procedure
1857 if Ekind (Scope_Id) = E_Procedure then
1858 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1859 end if;
1861 -- Check that functions return objects, and other things do not
1863 if Kind = E_Function or else Kind = E_Generic_Function then
1864 if not Returns_Object then
1865 Error_Msg_N ("missing expression in return from function", N);
1866 end if;
1868 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1869 if Returns_Object then
1870 Error_Msg_N ("procedure cannot return value (use function)", N);
1871 end if;
1873 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1874 if Returns_Object then
1875 if Is_Protected_Type (Scope (Scope_Id)) then
1876 Error_Msg_N ("entry body cannot return value", N);
1877 else
1878 Error_Msg_N ("accept statement cannot return value", N);
1879 end if;
1880 end if;
1882 elsif Kind = E_Return_Statement then
1884 -- We are nested within another return statement, which must be an
1885 -- extended_return_statement.
1887 if Returns_Object then
1888 if Nkind (N) = N_Extended_Return_Statement then
1889 Error_Msg_N
1890 ("extended return statement cannot be nested (use `RETURN;`)",
1893 -- Case of a simple return statement with a value inside extended
1894 -- return statement.
1896 else
1897 Error_Msg_N
1898 ("return nested in extended return statement cannot return "
1899 & "value (use `RETURN;`)", N);
1900 end if;
1901 end if;
1903 else
1904 Error_Msg_N ("illegal context for return statement", N);
1905 end if;
1907 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1908 Analyze_Function_Return (N);
1910 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1911 Set_Return_Present (Scope_Id);
1912 end if;
1914 if Nkind (N) = N_Extended_Return_Statement then
1915 End_Scope;
1916 end if;
1918 Kill_Current_Values (Last_Assignment_Only => True);
1919 Check_Unreachable_Code (N);
1921 Analyze_Dimension (N);
1922 end Analyze_Return_Statement;
1924 -------------------------------------
1925 -- Analyze_Simple_Return_Statement --
1926 -------------------------------------
1928 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1929 begin
1930 if Present (Expression (N)) then
1931 Mark_Coextensions (N, Expression (N));
1932 end if;
1934 Analyze_Return_Statement (N);
1935 end Analyze_Simple_Return_Statement;
1937 -------------------------
1938 -- Analyze_Return_Type --
1939 -------------------------
1941 procedure Analyze_Return_Type (N : Node_Id) is
1942 Designator : constant Entity_Id := Defining_Entity (N);
1943 Typ : Entity_Id := Empty;
1945 begin
1946 -- Normal case where result definition does not indicate an error
1948 if Result_Definition (N) /= Error then
1949 if Nkind (Result_Definition (N)) = N_Access_Definition then
1950 Check_SPARK_05_Restriction
1951 ("access result is not allowed", Result_Definition (N));
1953 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1955 declare
1956 AD : constant Node_Id :=
1957 Access_To_Subprogram_Definition (Result_Definition (N));
1958 begin
1959 if Present (AD) and then Protected_Present (AD) then
1960 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1961 else
1962 Typ := Access_Definition (N, Result_Definition (N));
1963 end if;
1964 end;
1966 Set_Parent (Typ, Result_Definition (N));
1967 Set_Is_Local_Anonymous_Access (Typ);
1968 Set_Etype (Designator, Typ);
1970 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1972 Null_Exclusion_Static_Checks (N);
1974 -- Subtype_Mark case
1976 else
1977 Find_Type (Result_Definition (N));
1978 Typ := Entity (Result_Definition (N));
1979 Set_Etype (Designator, Typ);
1981 -- Unconstrained array as result is not allowed in SPARK
1983 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1984 Check_SPARK_05_Restriction
1985 ("returning an unconstrained array is not allowed",
1986 Result_Definition (N));
1987 end if;
1989 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1991 Null_Exclusion_Static_Checks (N);
1993 -- If a null exclusion is imposed on the result type, then create
1994 -- a null-excluding itype (an access subtype) and use it as the
1995 -- function's Etype. Note that the null exclusion checks are done
1996 -- right before this, because they don't get applied to types that
1997 -- do not come from source.
1999 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2000 Set_Etype (Designator,
2001 Create_Null_Excluding_Itype
2002 (T => Typ,
2003 Related_Nod => N,
2004 Scope_Id => Scope (Current_Scope)));
2006 -- The new subtype must be elaborated before use because
2007 -- it is visible outside of the function. However its base
2008 -- type may not be frozen yet, so the reference that will
2009 -- force elaboration must be attached to the freezing of
2010 -- the base type.
2012 -- If the return specification appears on a proper body,
2013 -- the subtype will have been created already on the spec.
2015 if Is_Frozen (Typ) then
2016 if Nkind (Parent (N)) = N_Subprogram_Body
2017 and then Nkind (Parent (Parent (N))) = N_Subunit
2018 then
2019 null;
2020 else
2021 Build_Itype_Reference (Etype (Designator), Parent (N));
2022 end if;
2024 else
2025 Ensure_Freeze_Node (Typ);
2027 declare
2028 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2029 begin
2030 Set_Itype (IR, Etype (Designator));
2031 Append_Freeze_Actions (Typ, New_List (IR));
2032 end;
2033 end if;
2035 else
2036 Set_Etype (Designator, Typ);
2037 end if;
2039 if Ekind (Typ) = E_Incomplete_Type
2040 and then Is_Value_Type (Typ)
2041 then
2042 null;
2044 elsif Ekind (Typ) = E_Incomplete_Type
2045 or else (Is_Class_Wide_Type (Typ)
2046 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2047 then
2048 -- AI05-0151: Tagged incomplete types are allowed in all formal
2049 -- parts. Untagged incomplete types are not allowed in bodies.
2050 -- As a consequence, limited views cannot appear in a basic
2051 -- declaration that is itself within a body, because there is
2052 -- no point at which the non-limited view will become visible.
2054 if Ada_Version >= Ada_2012 then
2055 if From_Limited_With (Typ) and then In_Package_Body then
2056 Error_Msg_NE
2057 ("invalid use of incomplete type&",
2058 Result_Definition (N), Typ);
2060 -- The return type of a subprogram body cannot be of a
2061 -- formal incomplete type.
2063 elsif Is_Generic_Type (Typ)
2064 and then Nkind (Parent (N)) = N_Subprogram_Body
2065 then
2066 Error_Msg_N
2067 ("return type cannot be a formal incomplete type",
2068 Result_Definition (N));
2070 elsif Is_Class_Wide_Type (Typ)
2071 and then Is_Generic_Type (Root_Type (Typ))
2072 and then Nkind (Parent (N)) = N_Subprogram_Body
2073 then
2074 Error_Msg_N
2075 ("return type cannot be a formal incomplete type",
2076 Result_Definition (N));
2078 elsif Is_Tagged_Type (Typ) then
2079 null;
2081 -- Use is legal in a thunk generated for an operation
2082 -- inherited from a progenitor.
2084 elsif Is_Thunk (Designator)
2085 and then Present (Non_Limited_View (Typ))
2086 then
2087 null;
2089 elsif Nkind (Parent (N)) = N_Subprogram_Body
2090 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2091 N_Entry_Body)
2092 then
2093 Error_Msg_NE
2094 ("invalid use of untagged incomplete type&",
2095 Designator, Typ);
2096 end if;
2098 -- The type must be completed in the current package. This
2099 -- is checked at the end of the package declaration when
2100 -- Taft-amendment types are identified. If the return type
2101 -- is class-wide, there is no required check, the type can
2102 -- be a bona fide TAT.
2104 if Ekind (Scope (Current_Scope)) = E_Package
2105 and then In_Private_Part (Scope (Current_Scope))
2106 and then not Is_Class_Wide_Type (Typ)
2107 then
2108 Append_Elmt (Designator, Private_Dependents (Typ));
2109 end if;
2111 else
2112 Error_Msg_NE
2113 ("invalid use of incomplete type&", Designator, Typ);
2114 end if;
2115 end if;
2116 end if;
2118 -- Case where result definition does indicate an error
2120 else
2121 Set_Etype (Designator, Any_Type);
2122 end if;
2123 end Analyze_Return_Type;
2125 -----------------------------
2126 -- Analyze_Subprogram_Body --
2127 -----------------------------
2129 procedure Analyze_Subprogram_Body (N : Node_Id) is
2130 Loc : constant Source_Ptr := Sloc (N);
2131 Body_Spec : constant Node_Id := Specification (N);
2132 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2134 begin
2135 if Debug_Flag_C then
2136 Write_Str ("==> subprogram body ");
2137 Write_Name (Chars (Body_Id));
2138 Write_Str (" from ");
2139 Write_Location (Loc);
2140 Write_Eol;
2141 Indent;
2142 end if;
2144 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2146 -- The real work is split out into the helper, so it can do "return;"
2147 -- without skipping the debug output:
2149 Analyze_Subprogram_Body_Helper (N);
2151 if Debug_Flag_C then
2152 Outdent;
2153 Write_Str ("<== subprogram body ");
2154 Write_Name (Chars (Body_Id));
2155 Write_Str (" from ");
2156 Write_Location (Loc);
2157 Write_Eol;
2158 end if;
2159 end Analyze_Subprogram_Body;
2161 --------------------------------------
2162 -- Analyze_Subprogram_Body_Contract --
2163 --------------------------------------
2165 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2166 Items : constant Node_Id := Contract (Body_Id);
2167 Mode : SPARK_Mode_Type;
2168 Prag : Node_Id;
2169 Prag_Nam : Name_Id;
2170 Ref_Depends : Node_Id := Empty;
2171 Ref_Global : Node_Id := Empty;
2173 begin
2174 -- When a subprogram body declaration is illegal, its defining entity is
2175 -- left unanalyzed. There is nothing left to do in this case because the
2176 -- body lacks a contract, or even a proper Ekind.
2178 if Ekind (Body_Id) = E_Void then
2179 return;
2180 end if;
2182 -- Due to the timing of contract analysis, delayed pragmas may be
2183 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2184 -- context. To remedy this, restore the original SPARK_Mode of the
2185 -- related subprogram body.
2187 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2189 -- All subprograms carry a contract, but for some it is not significant
2190 -- and should not be processed.
2192 if not Has_Significant_Contract (Body_Id) then
2193 null;
2195 -- The subprogram body is a completion, analyze all delayed pragmas that
2196 -- apply. Note that when the body is stand alone, the pragmas are always
2197 -- analyzed on the spot.
2199 elsif Present (Items) then
2201 -- Locate and store pragmas Refined_Depends and Refined_Global since
2202 -- their order of analysis matters.
2204 Prag := Classifications (Items);
2205 while Present (Prag) loop
2206 Prag_Nam := Pragma_Name (Prag);
2208 if Prag_Nam = Name_Refined_Depends then
2209 Ref_Depends := Prag;
2211 elsif Prag_Nam = Name_Refined_Global then
2212 Ref_Global := Prag;
2213 end if;
2215 Prag := Next_Pragma (Prag);
2216 end loop;
2218 -- Analyze Refined_Global first as Refined_Depends may mention items
2219 -- classified in the global refinement.
2221 if Present (Ref_Global) then
2222 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2223 end if;
2225 -- Refined_Depends must be analyzed after Refined_Global in order to
2226 -- see the modes of all global refinements.
2228 if Present (Ref_Depends) then
2229 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2230 end if;
2231 end if;
2233 -- Ensure that the contract cases or postconditions mention 'Result or
2234 -- define a post-state.
2236 Check_Result_And_Post_State (Body_Id);
2238 -- Restore the SPARK_Mode of the enclosing context after all delayed
2239 -- pragmas have been analyzed.
2241 Restore_SPARK_Mode (Mode);
2242 end Analyze_Subprogram_Body_Contract;
2244 ------------------------------------
2245 -- Analyze_Subprogram_Body_Helper --
2246 ------------------------------------
2248 -- This procedure is called for regular subprogram bodies, generic bodies,
2249 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2250 -- specification matters, and is used to create a proper declaration for
2251 -- the subprogram, or to perform conformance checks.
2253 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2254 Loc : constant Source_Ptr := Sloc (N);
2255 Body_Spec : Node_Id := Specification (N);
2256 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2257 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2258 Conformant : Boolean;
2259 HSS : Node_Id;
2260 Prot_Typ : Entity_Id := Empty;
2261 Spec_Id : Entity_Id;
2262 Spec_Decl : Node_Id := Empty;
2264 Last_Real_Spec_Entity : Entity_Id := Empty;
2265 -- When we analyze a separate spec, the entity chain ends up containing
2266 -- the formals, as well as any itypes generated during analysis of the
2267 -- default expressions for parameters, or the arguments of associated
2268 -- precondition/postcondition pragmas (which are analyzed in the context
2269 -- of the spec since they have visibility on formals).
2271 -- These entities belong with the spec and not the body. However we do
2272 -- the analysis of the body in the context of the spec (again to obtain
2273 -- visibility to the formals), and all the entities generated during
2274 -- this analysis end up also chained to the entity chain of the spec.
2275 -- But they really belong to the body, and there is circuitry to move
2276 -- them from the spec to the body.
2278 -- However, when we do this move, we don't want to move the real spec
2279 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2280 -- variable points to the last real spec entity, so we only move those
2281 -- chained beyond that point. It is initialized to Empty to deal with
2282 -- the case where there is no separate spec.
2284 function Body_Has_Contract return Boolean;
2285 -- Check whether unanalyzed body has an aspect or pragma that may
2286 -- generate a SPARK contract.
2288 procedure Build_Subprogram_Declaration;
2289 -- Create a matching subprogram declaration for subprogram body N
2291 procedure Check_Anonymous_Return;
2292 -- Ada 2005: if a function returns an access type that denotes a task,
2293 -- or a type that contains tasks, we must create a master entity for
2294 -- the anonymous type, which typically will be used in an allocator
2295 -- in the body of the function.
2297 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2298 -- Look ahead to recognize a pragma that may appear after the body.
2299 -- If there is a previous spec, check that it appears in the same
2300 -- declarative part. If the pragma is Inline_Always, perform inlining
2301 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2302 -- If the body acts as a spec, and inlining is required, we create a
2303 -- subprogram declaration for it, in order to attach the body to inline.
2304 -- If pragma does not appear after the body, check whether there is
2305 -- an inline pragma before any local declarations.
2307 procedure Check_Missing_Return;
2308 -- Checks for a function with a no return statements, and also performs
2309 -- the warning checks implemented by Check_Returns. In formal mode, also
2310 -- verify that a function ends with a RETURN and that a procedure does
2311 -- not contain any RETURN.
2313 function Disambiguate_Spec return Entity_Id;
2314 -- When a primitive is declared between the private view and the full
2315 -- view of a concurrent type which implements an interface, a special
2316 -- mechanism is used to find the corresponding spec of the primitive
2317 -- body.
2319 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2320 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2321 -- incomplete types coming from a limited context and swap their limited
2322 -- views with the non-limited ones.
2324 function Is_Private_Concurrent_Primitive
2325 (Subp_Id : Entity_Id) return Boolean;
2326 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2327 -- type that implements an interface and has a private view.
2329 procedure Set_Trivial_Subprogram (N : Node_Id);
2330 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2331 -- subprogram whose body is being analyzed. N is the statement node
2332 -- causing the flag to be set, if the following statement is a return
2333 -- of an entity, we mark the entity as set in source to suppress any
2334 -- warning on the stylized use of function stubs with a dummy return.
2336 procedure Verify_Overriding_Indicator;
2337 -- If there was a previous spec, the entity has been entered in the
2338 -- current scope previously. If the body itself carries an overriding
2339 -- indicator, check that it is consistent with the known status of the
2340 -- entity.
2342 -----------------------
2343 -- Body_Has_Contract --
2344 -----------------------
2346 function Body_Has_Contract return Boolean is
2347 Decls : constant List_Id := Declarations (N);
2348 Item : Node_Id;
2350 begin
2351 -- Check for unanalyzed aspects in the body that will generate a
2352 -- contract.
2354 if Present (Aspect_Specifications (N)) then
2355 Item := First (Aspect_Specifications (N));
2356 while Present (Item) loop
2357 if Is_Contract_Annotation (Item) then
2358 return True;
2359 end if;
2361 Next (Item);
2362 end loop;
2363 end if;
2365 -- Check for pragmas that may generate a contract
2367 if Present (Decls) then
2368 Item := First (Decls);
2369 while Present (Item) loop
2370 if Nkind (Item) = N_Pragma
2371 and then Is_Contract_Annotation (Item)
2372 then
2373 return True;
2374 end if;
2376 Next (Item);
2377 end loop;
2378 end if;
2380 return False;
2381 end Body_Has_Contract;
2383 ----------------------------------
2384 -- Build_Subprogram_Declaration --
2385 ----------------------------------
2387 procedure Build_Subprogram_Declaration is
2388 Asp : Node_Id;
2389 Decl : Node_Id;
2390 Subp_Decl : Node_Id;
2392 begin
2393 -- Create a matching subprogram spec using the profile of the body.
2394 -- The structure of the tree is identical, but has new entities for
2395 -- the defining unit name and formal parameters.
2397 Subp_Decl :=
2398 Make_Subprogram_Declaration (Loc,
2399 Specification => Copy_Subprogram_Spec (Body_Spec));
2401 -- Relocate the aspects of the subprogram body to the new subprogram
2402 -- spec because it acts as the initial declaration.
2403 -- ??? what about pragmas
2405 Move_Aspects (N, To => Subp_Decl);
2406 Insert_Before_And_Analyze (N, Subp_Decl);
2408 -- The analysis of the subprogram spec aspects may introduce pragmas
2409 -- that need to be analyzed.
2411 Decl := Next (Subp_Decl);
2412 while Present (Decl) loop
2414 -- Stop the search for pragmas once the body has been reached as
2415 -- this terminates the region where pragmas may appear.
2417 if Decl = N then
2418 exit;
2420 elsif Nkind (Decl) = N_Pragma then
2421 Analyze (Decl);
2422 end if;
2424 Next (Decl);
2425 end loop;
2427 Spec_Id := Defining_Entity (Subp_Decl);
2428 Set_Corresponding_Spec (N, Spec_Id);
2430 -- Mark the generated spec as a source construct to ensure that all
2431 -- calls to it are properly registered in ALI files for GNATprove.
2433 Set_Comes_From_Source (Spec_Id, True);
2435 -- If aspect SPARK_Mode was specified on the body, it needs to be
2436 -- repeated both on the generated spec and the body.
2438 Asp := Find_Aspect (Spec_Id, Aspect_SPARK_Mode);
2440 if Present (Asp) then
2441 Asp := New_Copy_Tree (Asp);
2442 Set_Analyzed (Asp, False);
2443 Set_Aspect_Specifications (N, New_List (Asp));
2444 end if;
2446 -- Ensure that the specs of the subprogram declaration and its body
2447 -- are identical, otherwise they will appear non-conformant due to
2448 -- rewritings in the default values of formal parameters.
2450 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2451 Set_Specification (N, Body_Spec);
2452 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2453 end Build_Subprogram_Declaration;
2455 ----------------------------
2456 -- Check_Anonymous_Return --
2457 ----------------------------
2459 procedure Check_Anonymous_Return is
2460 Decl : Node_Id;
2461 Par : Node_Id;
2462 Scop : Entity_Id;
2464 begin
2465 if Present (Spec_Id) then
2466 Scop := Spec_Id;
2467 else
2468 Scop := Body_Id;
2469 end if;
2471 if Ekind (Scop) = E_Function
2472 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2473 and then not Is_Thunk (Scop)
2475 -- Skip internally built functions which handle the case of
2476 -- a null access (see Expand_Interface_Conversion)
2478 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2479 and then not Comes_From_Source (Parent (Scop)))
2481 and then (Has_Task (Designated_Type (Etype (Scop)))
2482 or else
2483 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2484 and then
2485 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2486 and then Expander_Active
2488 -- Avoid cases with no tasking support
2490 and then RTE_Available (RE_Current_Master)
2491 and then not Restriction_Active (No_Task_Hierarchy)
2492 then
2493 Decl :=
2494 Make_Object_Declaration (Loc,
2495 Defining_Identifier =>
2496 Make_Defining_Identifier (Loc, Name_uMaster),
2497 Constant_Present => True,
2498 Object_Definition =>
2499 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2500 Expression =>
2501 Make_Explicit_Dereference (Loc,
2502 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2504 if Present (Declarations (N)) then
2505 Prepend (Decl, Declarations (N));
2506 else
2507 Set_Declarations (N, New_List (Decl));
2508 end if;
2510 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2511 Set_Has_Master_Entity (Scop);
2513 -- Now mark the containing scope as a task master
2515 Par := N;
2516 while Nkind (Par) /= N_Compilation_Unit loop
2517 Par := Parent (Par);
2518 pragma Assert (Present (Par));
2520 -- If we fall off the top, we are at the outer level, and
2521 -- the environment task is our effective master, so nothing
2522 -- to mark.
2524 if Nkind_In
2525 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2526 then
2527 Set_Is_Task_Master (Par, True);
2528 exit;
2529 end if;
2530 end loop;
2531 end if;
2532 end Check_Anonymous_Return;
2534 -------------------------
2535 -- Check_Inline_Pragma --
2536 -------------------------
2538 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2539 Prag : Node_Id;
2540 Plist : List_Id;
2542 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2543 -- True when N is a pragma Inline or Inline_Always that applies
2544 -- to this subprogram.
2546 -----------------------
2547 -- Is_Inline_Pragma --
2548 -----------------------
2550 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2551 begin
2552 return
2553 Nkind (N) = N_Pragma
2554 and then
2555 (Pragma_Name (N) = Name_Inline_Always
2556 or else (Front_End_Inlining
2557 and then Pragma_Name (N) = Name_Inline))
2558 and then
2559 Chars
2560 (Expression (First (Pragma_Argument_Associations (N)))) =
2561 Chars (Body_Id);
2562 end Is_Inline_Pragma;
2564 -- Start of processing for Check_Inline_Pragma
2566 begin
2567 if not Expander_Active then
2568 return;
2569 end if;
2571 if Is_List_Member (N)
2572 and then Present (Next (N))
2573 and then Is_Inline_Pragma (Next (N))
2574 then
2575 Prag := Next (N);
2577 elsif Nkind (N) /= N_Subprogram_Body_Stub
2578 and then Present (Declarations (N))
2579 and then Is_Inline_Pragma (First (Declarations (N)))
2580 then
2581 Prag := First (Declarations (N));
2583 else
2584 Prag := Empty;
2585 end if;
2587 if Present (Prag) then
2588 if Present (Spec_Id) then
2589 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2590 Analyze (Prag);
2591 end if;
2593 else
2594 -- Create a subprogram declaration, to make treatment uniform
2596 declare
2597 Subp : constant Entity_Id :=
2598 Make_Defining_Identifier (Loc, Chars (Body_Id));
2599 Decl : constant Node_Id :=
2600 Make_Subprogram_Declaration (Loc,
2601 Specification =>
2602 New_Copy_Tree (Specification (N)));
2604 begin
2605 Set_Defining_Unit_Name (Specification (Decl), Subp);
2607 if Present (First_Formal (Body_Id)) then
2608 Plist := Copy_Parameter_List (Body_Id);
2609 Set_Parameter_Specifications
2610 (Specification (Decl), Plist);
2611 end if;
2613 Insert_Before (N, Decl);
2614 Analyze (Decl);
2615 Analyze (Prag);
2616 Set_Has_Pragma_Inline (Subp);
2618 if Pragma_Name (Prag) = Name_Inline_Always then
2619 Set_Is_Inlined (Subp);
2620 Set_Has_Pragma_Inline_Always (Subp);
2621 end if;
2623 -- Prior to copying the subprogram body to create a template
2624 -- for it for subsequent inlining, remove the pragma from
2625 -- the current body so that the copy that will produce the
2626 -- new body will start from a completely unanalyzed tree.
2628 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2629 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2630 end if;
2632 Spec := Subp;
2633 end;
2634 end if;
2635 end if;
2636 end Check_Inline_Pragma;
2638 --------------------------
2639 -- Check_Missing_Return --
2640 --------------------------
2642 procedure Check_Missing_Return is
2643 Id : Entity_Id;
2644 Missing_Ret : Boolean;
2646 begin
2647 if Nkind (Body_Spec) = N_Function_Specification then
2648 if Present (Spec_Id) then
2649 Id := Spec_Id;
2650 else
2651 Id := Body_Id;
2652 end if;
2654 if Return_Present (Id) then
2655 Check_Returns (HSS, 'F', Missing_Ret);
2657 if Missing_Ret then
2658 Set_Has_Missing_Return (Id);
2659 end if;
2661 elsif Is_Generic_Subprogram (Id)
2662 or else not Is_Machine_Code_Subprogram (Id)
2663 then
2664 Error_Msg_N ("missing RETURN statement in function body", N);
2665 end if;
2667 -- If procedure with No_Return, check returns
2669 elsif Nkind (Body_Spec) = N_Procedure_Specification
2670 and then Present (Spec_Id)
2671 and then No_Return (Spec_Id)
2672 then
2673 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2674 end if;
2676 -- Special checks in SPARK mode
2678 if Nkind (Body_Spec) = N_Function_Specification then
2680 -- In SPARK mode, last statement of a function should be a return
2682 declare
2683 Stat : constant Node_Id := Last_Source_Statement (HSS);
2684 begin
2685 if Present (Stat)
2686 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2687 N_Extended_Return_Statement)
2688 then
2689 Check_SPARK_05_Restriction
2690 ("last statement in function should be RETURN", Stat);
2691 end if;
2692 end;
2694 -- In SPARK mode, verify that a procedure has no return
2696 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2697 if Present (Spec_Id) then
2698 Id := Spec_Id;
2699 else
2700 Id := Body_Id;
2701 end if;
2703 -- Would be nice to point to return statement here, can we
2704 -- borrow the Check_Returns procedure here ???
2706 if Return_Present (Id) then
2707 Check_SPARK_05_Restriction
2708 ("procedure should not have RETURN", N);
2709 end if;
2710 end if;
2711 end Check_Missing_Return;
2713 -----------------------
2714 -- Disambiguate_Spec --
2715 -----------------------
2717 function Disambiguate_Spec return Entity_Id is
2718 Priv_Spec : Entity_Id;
2719 Spec_N : Entity_Id;
2721 procedure Replace_Types (To_Corresponding : Boolean);
2722 -- Depending on the flag, replace the type of formal parameters of
2723 -- Body_Id if it is a concurrent type implementing interfaces with
2724 -- the corresponding record type or the other way around.
2726 procedure Replace_Types (To_Corresponding : Boolean) is
2727 Formal : Entity_Id;
2728 Formal_Typ : Entity_Id;
2730 begin
2731 Formal := First_Formal (Body_Id);
2732 while Present (Formal) loop
2733 Formal_Typ := Etype (Formal);
2735 if Is_Class_Wide_Type (Formal_Typ) then
2736 Formal_Typ := Root_Type (Formal_Typ);
2737 end if;
2739 -- From concurrent type to corresponding record
2741 if To_Corresponding then
2742 if Is_Concurrent_Type (Formal_Typ)
2743 and then Present (Corresponding_Record_Type (Formal_Typ))
2744 and then
2745 Present (Interfaces
2746 (Corresponding_Record_Type (Formal_Typ)))
2747 then
2748 Set_Etype (Formal,
2749 Corresponding_Record_Type (Formal_Typ));
2750 end if;
2752 -- From corresponding record to concurrent type
2754 else
2755 if Is_Concurrent_Record_Type (Formal_Typ)
2756 and then Present (Interfaces (Formal_Typ))
2757 then
2758 Set_Etype (Formal,
2759 Corresponding_Concurrent_Type (Formal_Typ));
2760 end if;
2761 end if;
2763 Next_Formal (Formal);
2764 end loop;
2765 end Replace_Types;
2767 -- Start of processing for Disambiguate_Spec
2769 begin
2770 -- Try to retrieve the specification of the body as is. All error
2771 -- messages are suppressed because the body may not have a spec in
2772 -- its current state.
2774 Spec_N := Find_Corresponding_Spec (N, False);
2776 -- It is possible that this is the body of a primitive declared
2777 -- between a private and a full view of a concurrent type. The
2778 -- controlling parameter of the spec carries the concurrent type,
2779 -- not the corresponding record type as transformed by Analyze_
2780 -- Subprogram_Specification. In such cases, we undo the change
2781 -- made by the analysis of the specification and try to find the
2782 -- spec again.
2784 -- Note that wrappers already have their corresponding specs and
2785 -- bodies set during their creation, so if the candidate spec is
2786 -- a wrapper, then we definitely need to swap all types to their
2787 -- original concurrent status.
2789 if No (Spec_N)
2790 or else Is_Primitive_Wrapper (Spec_N)
2791 then
2792 -- Restore all references of corresponding record types to the
2793 -- original concurrent types.
2795 Replace_Types (To_Corresponding => False);
2796 Priv_Spec := Find_Corresponding_Spec (N, False);
2798 -- The current body truly belongs to a primitive declared between
2799 -- a private and a full view. We leave the modified body as is,
2800 -- and return the true spec.
2802 if Present (Priv_Spec)
2803 and then Is_Private_Primitive (Priv_Spec)
2804 then
2805 return Priv_Spec;
2806 end if;
2808 -- In case that this is some sort of error, restore the original
2809 -- state of the body.
2811 Replace_Types (To_Corresponding => True);
2812 end if;
2814 return Spec_N;
2815 end Disambiguate_Spec;
2817 ----------------------------
2818 -- Exchange_Limited_Views --
2819 ----------------------------
2821 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2822 procedure Detect_And_Exchange (Id : Entity_Id);
2823 -- Determine whether Id's type denotes an incomplete type associated
2824 -- with a limited with clause and exchange the limited view with the
2825 -- non-limited one when available.
2827 -------------------------
2828 -- Detect_And_Exchange --
2829 -------------------------
2831 procedure Detect_And_Exchange (Id : Entity_Id) is
2832 Typ : constant Entity_Id := Etype (Id);
2833 begin
2834 if From_Limited_With (Typ)
2835 and then Has_Non_Limited_View (Typ)
2836 then
2837 Set_Etype (Id, Non_Limited_View (Typ));
2838 end if;
2839 end Detect_And_Exchange;
2841 -- Local variables
2843 Formal : Entity_Id;
2845 -- Start of processing for Exchange_Limited_Views
2847 begin
2848 if No (Subp_Id) then
2849 return;
2851 -- Do not process subprogram bodies as they already use the non-
2852 -- limited view of types.
2854 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2855 return;
2856 end if;
2858 -- Examine all formals and swap views when applicable
2860 Formal := First_Formal (Subp_Id);
2861 while Present (Formal) loop
2862 Detect_And_Exchange (Formal);
2864 Next_Formal (Formal);
2865 end loop;
2867 -- Process the return type of a function
2869 if Ekind (Subp_Id) = E_Function then
2870 Detect_And_Exchange (Subp_Id);
2871 end if;
2872 end Exchange_Limited_Views;
2874 -------------------------------------
2875 -- Is_Private_Concurrent_Primitive --
2876 -------------------------------------
2878 function Is_Private_Concurrent_Primitive
2879 (Subp_Id : Entity_Id) return Boolean
2881 Formal_Typ : Entity_Id;
2883 begin
2884 if Present (First_Formal (Subp_Id)) then
2885 Formal_Typ := Etype (First_Formal (Subp_Id));
2887 if Is_Concurrent_Record_Type (Formal_Typ) then
2888 if Is_Class_Wide_Type (Formal_Typ) then
2889 Formal_Typ := Root_Type (Formal_Typ);
2890 end if;
2892 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2893 end if;
2895 -- The type of the first formal is a concurrent tagged type with
2896 -- a private view.
2898 return
2899 Is_Concurrent_Type (Formal_Typ)
2900 and then Is_Tagged_Type (Formal_Typ)
2901 and then Has_Private_Declaration (Formal_Typ);
2902 end if;
2904 return False;
2905 end Is_Private_Concurrent_Primitive;
2907 ----------------------------
2908 -- Set_Trivial_Subprogram --
2909 ----------------------------
2911 procedure Set_Trivial_Subprogram (N : Node_Id) is
2912 Nxt : constant Node_Id := Next (N);
2914 begin
2915 Set_Is_Trivial_Subprogram (Body_Id);
2917 if Present (Spec_Id) then
2918 Set_Is_Trivial_Subprogram (Spec_Id);
2919 end if;
2921 if Present (Nxt)
2922 and then Nkind (Nxt) = N_Simple_Return_Statement
2923 and then No (Next (Nxt))
2924 and then Present (Expression (Nxt))
2925 and then Is_Entity_Name (Expression (Nxt))
2926 then
2927 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2928 end if;
2929 end Set_Trivial_Subprogram;
2931 ---------------------------------
2932 -- Verify_Overriding_Indicator --
2933 ---------------------------------
2935 procedure Verify_Overriding_Indicator is
2936 begin
2937 if Must_Override (Body_Spec) then
2938 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2939 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2940 then
2941 null;
2943 elsif not Present (Overridden_Operation (Spec_Id)) then
2944 Error_Msg_NE
2945 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2947 -- Overriding indicators aren't allowed for protected subprogram
2948 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2949 -- this to a warning if -gnatd.E is enabled.
2951 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2952 Error_Msg_Warn := Error_To_Warning;
2953 Error_Msg_N
2954 ("<<overriding indicator not allowed for protected "
2955 & "subprogram body", Body_Spec);
2956 end if;
2958 elsif Must_Not_Override (Body_Spec) then
2959 if Present (Overridden_Operation (Spec_Id)) then
2960 Error_Msg_NE
2961 ("subprogram& overrides inherited operation",
2962 Body_Spec, Spec_Id);
2964 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2965 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2966 then
2967 Error_Msg_NE
2968 ("subprogram& overrides predefined operator ",
2969 Body_Spec, Spec_Id);
2971 -- Overriding indicators aren't allowed for protected subprogram
2972 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2973 -- this to a warning if -gnatd.E is enabled.
2975 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2976 Error_Msg_Warn := Error_To_Warning;
2978 Error_Msg_N
2979 ("<<overriding indicator not allowed "
2980 & "for protected subprogram body", Body_Spec);
2982 -- If this is not a primitive operation, then the overriding
2983 -- indicator is altogether illegal.
2985 elsif not Is_Primitive (Spec_Id) then
2986 Error_Msg_N
2987 ("overriding indicator only allowed "
2988 & "if subprogram is primitive", Body_Spec);
2989 end if;
2991 -- If checking the style rule and the operation overrides, then
2992 -- issue a warning about a missing overriding_indicator. Protected
2993 -- subprogram bodies are excluded from this style checking, since
2994 -- they aren't primitives (even though their declarations can
2995 -- override) and aren't allowed to have an overriding_indicator.
2997 elsif Style_Check
2998 and then Present (Overridden_Operation (Spec_Id))
2999 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3000 then
3001 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3002 Style.Missing_Overriding (N, Body_Id);
3004 elsif Style_Check
3005 and then Can_Override_Operator (Spec_Id)
3006 and then not Is_Predefined_File_Name
3007 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3008 then
3009 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3010 Style.Missing_Overriding (N, Body_Id);
3011 end if;
3012 end Verify_Overriding_Indicator;
3014 -- Start of processing for Analyze_Subprogram_Body_Helper
3016 begin
3017 -- Generic subprograms are handled separately. They always have a
3018 -- generic specification. Determine whether current scope has a
3019 -- previous declaration.
3021 -- If the subprogram body is defined within an instance of the same
3022 -- name, the instance appears as a package renaming, and will be hidden
3023 -- within the subprogram.
3025 if Present (Prev_Id)
3026 and then not Is_Overloadable (Prev_Id)
3027 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3028 or else Comes_From_Source (Prev_Id))
3029 then
3030 if Is_Generic_Subprogram (Prev_Id) then
3031 Spec_Id := Prev_Id;
3033 -- The corresponding spec may be subject to pragma Ghost with
3034 -- policy Ignore. Set the mode now to ensure that any nodes
3035 -- generated during analysis and expansion are properly flagged
3036 -- as ignored Ghost.
3038 Set_Ghost_Mode (N, Spec_Id);
3039 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3040 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3042 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3044 if Nkind (N) = N_Subprogram_Body then
3045 HSS := Handled_Statement_Sequence (N);
3046 Check_Missing_Return;
3047 end if;
3049 return;
3051 else
3052 -- Previous entity conflicts with subprogram name. Attempting to
3053 -- enter name will post error.
3055 Enter_Name (Body_Id);
3056 return;
3057 end if;
3059 -- Non-generic case, find the subprogram declaration, if one was seen,
3060 -- or enter new overloaded entity in the current scope. If the
3061 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3062 -- part of the context of one of its subunits. No need to redo the
3063 -- analysis.
3065 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3066 return;
3068 else
3069 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3071 if Nkind (N) = N_Subprogram_Body_Stub
3072 or else No (Corresponding_Spec (N))
3073 then
3074 if Is_Private_Concurrent_Primitive (Body_Id) then
3075 Spec_Id := Disambiguate_Spec;
3077 -- The corresponding spec may be subject to pragma Ghost with
3078 -- policy Ignore. Set the mode now to ensure that any nodes
3079 -- generated during analysis and expansion are properly flagged
3080 -- as ignored Ghost.
3082 Set_Ghost_Mode (N, Spec_Id);
3084 else
3085 Spec_Id := Find_Corresponding_Spec (N);
3087 -- The corresponding spec may be subject to pragma Ghost with
3088 -- policy Ignore. Set the mode now to ensure that any nodes
3089 -- generated during analysis and expansion are properly flagged
3090 -- as ignored Ghost.
3092 Set_Ghost_Mode (N, Spec_Id);
3094 -- In GNATprove mode, if the body has no previous spec, create
3095 -- one so that the inlining machinery can operate properly.
3096 -- Transfer aspects, if any, to the new spec, so that they
3097 -- are legal and can be processed ahead of the body.
3098 -- We make two copies of the given spec, one for the new
3099 -- declaration, and one for the body.
3101 if No (Spec_Id) and then GNATprove_Mode
3103 -- Inlining does not apply during pre-analysis of code
3105 and then Full_Analysis
3107 -- Inlining only applies to full bodies, not stubs
3109 and then Nkind (N) /= N_Subprogram_Body_Stub
3111 -- Inlining only applies to bodies in the source code, not to
3112 -- those generated by the compiler. In particular, expression
3113 -- functions, whose body is generated by the compiler, are
3114 -- treated specially by GNATprove.
3116 and then Comes_From_Source (Body_Id)
3118 -- This cannot be done for a compilation unit, which is not
3119 -- in a context where we can insert a new spec.
3121 and then Is_List_Member (N)
3123 -- Inlining only applies to subprograms without contracts,
3124 -- as a contract is a sign that GNATprove should perform a
3125 -- modular analysis of the subprogram instead of a contextual
3126 -- analysis at each call site. The same test is performed in
3127 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3128 -- here in another form (because the contract has not
3129 -- been attached to the body) to avoid frontend errors in
3130 -- case pragmas are used instead of aspects, because the
3131 -- corresponding pragmas in the body would not be transferred
3132 -- to the spec, leading to legality errors.
3134 and then not Body_Has_Contract
3135 and then not Inside_A_Generic
3136 then
3137 Build_Subprogram_Declaration;
3138 end if;
3139 end if;
3141 -- If this is a duplicate body, no point in analyzing it
3143 if Error_Posted (N) then
3144 return;
3145 end if;
3147 -- A subprogram body should cause freezing of its own declaration,
3148 -- but if there was no previous explicit declaration, then the
3149 -- subprogram will get frozen too late (there may be code within
3150 -- the body that depends on the subprogram having been frozen,
3151 -- such as uses of extra formals), so we force it to be frozen
3152 -- here. Same holds if the body and spec are compilation units.
3153 -- Finally, if the return type is an anonymous access to protected
3154 -- subprogram, it must be frozen before the body because its
3155 -- expansion has generated an equivalent type that is used when
3156 -- elaborating the body.
3158 -- An exception in the case of Ada 2012, AI05-177: The bodies
3159 -- created for expression functions do not freeze.
3161 if No (Spec_Id)
3162 and then Nkind (Original_Node (N)) /= N_Expression_Function
3163 then
3164 Freeze_Before (N, Body_Id);
3166 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3167 Freeze_Before (N, Spec_Id);
3169 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3170 Freeze_Before (N, Etype (Body_Id));
3171 end if;
3173 else
3174 Spec_Id := Corresponding_Spec (N);
3176 -- The corresponding spec may be subject to pragma Ghost with
3177 -- policy Ignore. Set the mode now to ensure that any nodes
3178 -- generated during analysis and expansion are properly flagged
3179 -- as ignored Ghost.
3181 Set_Ghost_Mode (N, Spec_Id);
3182 end if;
3183 end if;
3185 -- Previously we scanned the body to look for nested subprograms, and
3186 -- rejected an inline directive if nested subprograms were present,
3187 -- because the back-end would generate conflicting symbols for the
3188 -- nested bodies. This is now unnecessary.
3190 -- Look ahead to recognize a pragma Inline that appears after the body
3192 Check_Inline_Pragma (Spec_Id);
3194 -- Deal with special case of a fully private operation in the body of
3195 -- the protected type. We must create a declaration for the subprogram,
3196 -- in order to attach the protected subprogram that will be used in
3197 -- internal calls. We exclude compiler generated bodies from the
3198 -- expander since the issue does not arise for those cases.
3200 if No (Spec_Id)
3201 and then Comes_From_Source (N)
3202 and then Is_Protected_Type (Current_Scope)
3203 then
3204 Spec_Id := Build_Private_Protected_Declaration (N);
3205 end if;
3207 -- If a separate spec is present, then deal with freezing issues
3209 if Present (Spec_Id) then
3210 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3211 Verify_Overriding_Indicator;
3213 -- In general, the spec will be frozen when we start analyzing the
3214 -- body. However, for internally generated operations, such as
3215 -- wrapper functions for inherited operations with controlling
3216 -- results, the spec may not have been frozen by the time we expand
3217 -- the freeze actions that include the bodies. In particular, extra
3218 -- formals for accessibility or for return-in-place may need to be
3219 -- generated. Freeze nodes, if any, are inserted before the current
3220 -- body. These freeze actions are also needed in ASIS mode to enable
3221 -- the proper back-annotations.
3223 if not Is_Frozen (Spec_Id)
3224 and then (Expander_Active or ASIS_Mode)
3225 then
3226 -- Force the generation of its freezing node to ensure proper
3227 -- management of access types in the backend.
3229 -- This is definitely needed for some cases, but it is not clear
3230 -- why, to be investigated further???
3232 Set_Has_Delayed_Freeze (Spec_Id);
3233 Freeze_Before (N, Spec_Id);
3234 end if;
3235 end if;
3237 -- Place subprogram on scope stack, and make formals visible. If there
3238 -- is a spec, the visible entity remains that of the spec.
3240 if Present (Spec_Id) then
3241 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3243 if Is_Child_Unit (Spec_Id) then
3244 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3245 end if;
3247 if Style_Check then
3248 Style.Check_Identifier (Body_Id, Spec_Id);
3249 end if;
3251 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3252 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3254 if Is_Abstract_Subprogram (Spec_Id) then
3255 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3256 return;
3258 else
3259 Set_Convention (Body_Id, Convention (Spec_Id));
3260 Set_Has_Completion (Spec_Id);
3262 -- Inherit the "ghostness" of the subprogram spec. Note that this
3263 -- property is not directly inherited as the body may be subject
3264 -- to a different Ghost assertion policy.
3266 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
3267 Set_Is_Ghost_Entity (Body_Id);
3269 -- The Ghost policy in effect at the point of declaration and
3270 -- at the point of completion must match (SPARK RM 6.9(14)).
3272 Check_Ghost_Completion (Spec_Id, Body_Id);
3273 end if;
3275 if Is_Protected_Type (Scope (Spec_Id)) then
3276 Prot_Typ := Scope (Spec_Id);
3277 end if;
3279 -- If this is a body generated for a renaming, do not check for
3280 -- full conformance. The check is redundant, because the spec of
3281 -- the body is a copy of the spec in the renaming declaration,
3282 -- and the test can lead to spurious errors on nested defaults.
3284 if Present (Spec_Decl)
3285 and then not Comes_From_Source (N)
3286 and then
3287 (Nkind (Original_Node (Spec_Decl)) =
3288 N_Subprogram_Renaming_Declaration
3289 or else (Present (Corresponding_Body (Spec_Decl))
3290 and then
3291 Nkind (Unit_Declaration_Node
3292 (Corresponding_Body (Spec_Decl))) =
3293 N_Subprogram_Renaming_Declaration))
3294 then
3295 Conformant := True;
3297 -- Conversely, the spec may have been generated for specless body
3298 -- with an inline pragma.
3300 elsif Comes_From_Source (N)
3301 and then not Comes_From_Source (Spec_Id)
3302 and then Has_Pragma_Inline (Spec_Id)
3303 then
3304 Conformant := True;
3306 else
3307 Check_Conformance
3308 (Body_Id, Spec_Id,
3309 Fully_Conformant, True, Conformant, Body_Id);
3310 end if;
3312 -- If the body is not fully conformant, we have to decide if we
3313 -- should analyze it or not. If it has a really messed up profile
3314 -- then we probably should not analyze it, since we will get too
3315 -- many bogus messages.
3317 -- Our decision is to go ahead in the non-fully conformant case
3318 -- only if it is at least mode conformant with the spec. Note
3319 -- that the call to Check_Fully_Conformant has issued the proper
3320 -- error messages to complain about the lack of conformance.
3322 if not Conformant
3323 and then not Mode_Conformant (Body_Id, Spec_Id)
3324 then
3325 return;
3326 end if;
3327 end if;
3329 if Spec_Id /= Body_Id then
3330 Reference_Body_Formals (Spec_Id, Body_Id);
3331 end if;
3333 Set_Ekind (Body_Id, E_Subprogram_Body);
3335 if Nkind (N) = N_Subprogram_Body_Stub then
3336 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3338 -- Regular body
3340 else
3341 Set_Corresponding_Spec (N, Spec_Id);
3343 -- Ada 2005 (AI-345): If the operation is a primitive operation
3344 -- of a concurrent type, the type of the first parameter has been
3345 -- replaced with the corresponding record, which is the proper
3346 -- run-time structure to use. However, within the body there may
3347 -- be uses of the formals that depend on primitive operations
3348 -- of the type (in particular calls in prefixed form) for which
3349 -- we need the original concurrent type. The operation may have
3350 -- several controlling formals, so the replacement must be done
3351 -- for all of them.
3353 if Comes_From_Source (Spec_Id)
3354 and then Present (First_Entity (Spec_Id))
3355 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3356 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3357 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3358 and then Present (Corresponding_Concurrent_Type
3359 (Etype (First_Entity (Spec_Id))))
3360 then
3361 declare
3362 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3363 Form : Entity_Id;
3365 begin
3366 Form := First_Formal (Spec_Id);
3367 while Present (Form) loop
3368 if Etype (Form) = Typ then
3369 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3370 end if;
3372 Next_Formal (Form);
3373 end loop;
3374 end;
3375 end if;
3377 -- Make the formals visible, and place subprogram on scope stack.
3378 -- This is also the point at which we set Last_Real_Spec_Entity
3379 -- to mark the entities which will not be moved to the body.
3381 Install_Formals (Spec_Id);
3382 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3384 -- Within an instance, add local renaming declarations so that
3385 -- gdb can retrieve the values of actuals more easily. This is
3386 -- only relevant if generating code (and indeed we definitely
3387 -- do not want these definitions -gnatc mode, because that would
3388 -- confuse ASIS).
3390 if Is_Generic_Instance (Spec_Id)
3391 and then Is_Wrapper_Package (Current_Scope)
3392 and then Expander_Active
3393 then
3394 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3395 end if;
3397 Push_Scope (Spec_Id);
3399 -- Make sure that the subprogram is immediately visible. For
3400 -- child units that have no separate spec this is indispensable.
3401 -- Otherwise it is safe albeit redundant.
3403 Set_Is_Immediately_Visible (Spec_Id);
3404 end if;
3406 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3407 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3408 Set_Scope (Body_Id, Scope (Spec_Id));
3410 -- Case of subprogram body with no previous spec
3412 else
3413 -- Check for style warning required
3415 if Style_Check
3417 -- Only apply check for source level subprograms for which checks
3418 -- have not been suppressed.
3420 and then Comes_From_Source (Body_Id)
3421 and then not Suppress_Style_Checks (Body_Id)
3423 -- No warnings within an instance
3425 and then not In_Instance
3427 -- No warnings for expression functions
3429 and then Nkind (Original_Node (N)) /= N_Expression_Function
3430 then
3431 Style.Body_With_No_Spec (N);
3432 end if;
3434 New_Overloaded_Entity (Body_Id);
3436 if Nkind (N) /= N_Subprogram_Body_Stub then
3437 Set_Acts_As_Spec (N);
3438 Generate_Definition (Body_Id);
3439 Generate_Reference
3440 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3441 Install_Formals (Body_Id);
3443 Push_Scope (Body_Id);
3444 end if;
3446 -- For stubs and bodies with no previous spec, generate references to
3447 -- formals.
3449 Generate_Reference_To_Formals (Body_Id);
3450 end if;
3452 -- Set SPARK_Mode from context
3454 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3455 Set_SPARK_Pragma_Inherited (Body_Id, True);
3457 -- If the return type is an anonymous access type whose designated type
3458 -- is the limited view of a class-wide type and the non-limited view is
3459 -- available, update the return type accordingly.
3461 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3462 declare
3463 Etyp : Entity_Id;
3464 Rtyp : Entity_Id;
3466 begin
3467 Rtyp := Etype (Current_Scope);
3469 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3470 Etyp := Directly_Designated_Type (Rtyp);
3472 if Is_Class_Wide_Type (Etyp)
3473 and then From_Limited_With (Etyp)
3474 then
3475 Set_Directly_Designated_Type
3476 (Etype (Current_Scope), Available_View (Etyp));
3477 end if;
3478 end if;
3479 end;
3480 end if;
3482 -- If this is the proper body of a stub, we must verify that the stub
3483 -- conforms to the body, and to the previous spec if one was present.
3484 -- We know already that the body conforms to that spec. This test is
3485 -- only required for subprograms that come from source.
3487 if Nkind (Parent (N)) = N_Subunit
3488 and then Comes_From_Source (N)
3489 and then not Error_Posted (Body_Id)
3490 and then Nkind (Corresponding_Stub (Parent (N))) =
3491 N_Subprogram_Body_Stub
3492 then
3493 declare
3494 Old_Id : constant Entity_Id :=
3495 Defining_Entity
3496 (Specification (Corresponding_Stub (Parent (N))));
3498 Conformant : Boolean := False;
3500 begin
3501 if No (Spec_Id) then
3502 Check_Fully_Conformant (Body_Id, Old_Id);
3504 else
3505 Check_Conformance
3506 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3508 if not Conformant then
3510 -- The stub was taken to be a new declaration. Indicate that
3511 -- it lacks a body.
3513 Set_Has_Completion (Old_Id, False);
3514 end if;
3515 end if;
3516 end;
3517 end if;
3519 Set_Has_Completion (Body_Id);
3520 Check_Eliminated (Body_Id);
3522 -- Analyze any aspect specifications that appear on the subprogram body
3523 -- stub. Stop the analysis now as the stub does not have a declarative
3524 -- or a statement part, and it cannot be inlined.
3526 if Nkind (N) = N_Subprogram_Body_Stub then
3527 if Has_Aspects (N) then
3528 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3529 end if;
3531 return;
3532 end if;
3534 -- Handle frontend inlining
3536 -- Note: Normally we don't do any inlining if expansion is off, since
3537 -- we won't generate code in any case. An exception arises in GNATprove
3538 -- mode where we want to expand some calls in place, even with expansion
3539 -- disabled, since the inlining eases formal verification.
3541 if not GNATprove_Mode
3542 and then Expander_Active
3543 and then Serious_Errors_Detected = 0
3544 and then Present (Spec_Id)
3545 and then Has_Pragma_Inline (Spec_Id)
3546 then
3547 -- Legacy implementation (relying on frontend inlining)
3549 if not Back_End_Inlining then
3550 if (Has_Pragma_Inline_Always (Spec_Id)
3551 and then not Opt.Disable_FE_Inline_Always)
3552 or else
3553 (Has_Pragma_Inline (Spec_Id) and then Front_End_Inlining
3554 and then not Opt.Disable_FE_Inline)
3555 then
3556 Build_Body_To_Inline (N, Spec_Id);
3557 end if;
3559 -- New implementation (relying on backend inlining)
3561 else
3562 if Has_Pragma_Inline_Always (Spec_Id)
3563 or else Optimization_Level > 0
3564 then
3565 -- Handle function returning an unconstrained type
3567 if Comes_From_Source (Body_Id)
3568 and then Ekind (Spec_Id) = E_Function
3569 and then Returns_Unconstrained_Type (Spec_Id)
3571 -- If function builds in place, i.e. returns a limited type,
3572 -- inlining cannot be done.
3574 and then not Is_Limited_Type (Etype (Spec_Id))
3575 then
3576 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3578 else
3579 declare
3580 Subp_Body : constant Node_Id :=
3581 Unit_Declaration_Node (Body_Id);
3582 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3584 begin
3585 -- Do not pass inlining to the backend if the subprogram
3586 -- has declarations or statements which cannot be inlined
3587 -- by the backend. This check is done here to emit an
3588 -- error instead of the generic warning message reported
3589 -- by the GCC backend (ie. "function might not be
3590 -- inlinable").
3592 if Present (Subp_Decl)
3593 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3594 then
3595 null;
3597 elsif Has_Excluded_Statement
3598 (Spec_Id,
3599 Statements
3600 (Handled_Statement_Sequence (Subp_Body)))
3601 then
3602 null;
3604 -- If the backend inlining is available then at this
3605 -- stage we only have to mark the subprogram as inlined.
3606 -- The expander will take care of registering it in the
3607 -- table of subprograms inlined by the backend a part of
3608 -- processing calls to it (cf. Expand_Call)
3610 else
3611 Set_Is_Inlined (Spec_Id);
3612 end if;
3613 end;
3614 end if;
3615 end if;
3616 end if;
3618 -- In GNATprove mode, inline only when there is a separate subprogram
3619 -- declaration for now, as inlining of subprogram bodies acting as
3620 -- declarations, or subprogram stubs, are not supported by frontend
3621 -- inlining. This inlining should occur after analysis of the body, so
3622 -- that it is known whether the value of SPARK_Mode applicable to the
3623 -- body, which can be defined by a pragma inside the body.
3625 elsif GNATprove_Mode
3626 and then Full_Analysis
3627 and then not Inside_A_Generic
3628 and then Present (Spec_Id)
3629 and then
3630 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
3631 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3632 and then not Body_Has_Contract
3633 then
3634 Build_Body_To_Inline (N, Spec_Id);
3635 end if;
3637 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3638 -- of the specification we have to install the private withed units.
3639 -- This holds for child units as well.
3641 if Is_Compilation_Unit (Body_Id)
3642 or else Nkind (Parent (N)) = N_Compilation_Unit
3643 then
3644 Install_Private_With_Clauses (Body_Id);
3645 end if;
3647 Check_Anonymous_Return;
3649 -- Set the Protected_Formal field of each extra formal of the protected
3650 -- subprogram to reference the corresponding extra formal of the
3651 -- subprogram that implements it. For regular formals this occurs when
3652 -- the protected subprogram's declaration is expanded, but the extra
3653 -- formals don't get created until the subprogram is frozen. We need to
3654 -- do this before analyzing the protected subprogram's body so that any
3655 -- references to the original subprogram's extra formals will be changed
3656 -- refer to the implementing subprogram's formals (see Expand_Formal).
3658 if Present (Spec_Id)
3659 and then Is_Protected_Type (Scope (Spec_Id))
3660 and then Present (Protected_Body_Subprogram (Spec_Id))
3661 then
3662 declare
3663 Impl_Subp : constant Entity_Id :=
3664 Protected_Body_Subprogram (Spec_Id);
3665 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3666 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3667 begin
3668 while Present (Prot_Ext_Formal) loop
3669 pragma Assert (Present (Impl_Ext_Formal));
3670 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3671 Next_Formal_With_Extras (Prot_Ext_Formal);
3672 Next_Formal_With_Extras (Impl_Ext_Formal);
3673 end loop;
3674 end;
3675 end if;
3677 -- Now we can go on to analyze the body
3679 HSS := Handled_Statement_Sequence (N);
3680 Set_Actual_Subtypes (N, Current_Scope);
3682 -- Add a declaration for the Protection object, renaming declarations
3683 -- for discriminals and privals and finally a declaration for the entry
3684 -- family index (if applicable). This form of early expansion is done
3685 -- when the Expander is active because Install_Private_Data_Declarations
3686 -- references entities which were created during regular expansion. The
3687 -- subprogram entity must come from source, and not be an internally
3688 -- generated subprogram.
3690 if Expander_Active
3691 and then Present (Prot_Typ)
3692 and then Present (Spec_Id)
3693 and then Comes_From_Source (Spec_Id)
3694 and then not Is_Eliminated (Spec_Id)
3695 then
3696 Install_Private_Data_Declarations
3697 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3698 end if;
3700 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3701 -- may now appear in parameter and result profiles. Since the analysis
3702 -- of a subprogram body may use the parameter and result profile of the
3703 -- spec, swap any limited views with their non-limited counterpart.
3705 if Ada_Version >= Ada_2012 then
3706 Exchange_Limited_Views (Spec_Id);
3707 end if;
3709 -- Analyze any aspect specifications that appear on the subprogram body
3711 if Has_Aspects (N) then
3712 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3713 end if;
3715 Analyze_Declarations (Declarations (N));
3717 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3719 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3720 if Present (SPARK_Pragma (Spec_Id)) then
3721 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3722 and then
3723 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3724 then
3725 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3726 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3727 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3728 Error_Msg_NE
3729 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3730 end if;
3732 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3733 null;
3735 else
3736 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3737 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3738 Error_Msg_Sloc := Sloc (Spec_Id);
3739 Error_Msg_NE
3740 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3741 end if;
3742 end if;
3744 -- When a subprogram body appears inside a package, its contract is
3745 -- analyzed at the end of the package body declarations. This is due
3746 -- to the delay with respect of the package contract upon which the
3747 -- body contract may depend. When the subprogram body is stand alone
3748 -- and acts as a compilation unit, this delay is not necessary.
3750 if Nkind (Parent (N)) = N_Compilation_Unit then
3751 Analyze_Subprogram_Body_Contract (Body_Id);
3752 end if;
3754 -- Deal with preconditions, [refined] postconditions, Contract_Cases,
3755 -- invariants and predicates associated with body and its spec. Since
3756 -- there is no routine Expand_Declarations which would otherwise deal
3757 -- with the contract expansion, generate all necessary mechanisms to
3758 -- verify the contract assertions now.
3760 Expand_Subprogram_Contract (N);
3762 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3763 -- subprogram in GNATprove mode, as its body should not be analyzed.
3765 if SPARK_Mode /= On
3766 and then GNATprove_Mode
3767 and then Present (Spec_Id)
3768 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3769 then
3770 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3771 Set_Is_Inlined_Always (Spec_Id, False);
3772 end if;
3774 -- Check completion, and analyze the statements
3776 Check_Completion;
3777 Inspect_Deferred_Constant_Completion (Declarations (N));
3778 Analyze (HSS);
3780 -- Deal with end of scope processing for the body
3782 Process_End_Label (HSS, 't', Current_Scope);
3783 End_Scope;
3784 Check_Subprogram_Order (N);
3785 Set_Analyzed (Body_Id);
3787 -- If we have a separate spec, then the analysis of the declarations
3788 -- caused the entities in the body to be chained to the spec id, but
3789 -- we want them chained to the body id. Only the formal parameters
3790 -- end up chained to the spec id in this case.
3792 if Present (Spec_Id) then
3794 -- We must conform to the categorization of our spec
3796 Validate_Categorization_Dependency (N, Spec_Id);
3798 -- And if this is a child unit, the parent units must conform
3800 if Is_Child_Unit (Spec_Id) then
3801 Validate_Categorization_Dependency
3802 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3803 end if;
3805 -- Here is where we move entities from the spec to the body
3807 -- Case where there are entities that stay with the spec
3809 if Present (Last_Real_Spec_Entity) then
3811 -- No body entities (happens when the only real spec entities come
3812 -- from precondition and postcondition pragmas).
3814 if No (Last_Entity (Body_Id)) then
3815 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3817 -- Body entities present (formals), so chain stuff past them
3819 else
3820 Set_Next_Entity
3821 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3822 end if;
3824 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3825 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3826 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3828 -- Case where there are no spec entities, in this case there can be
3829 -- no body entities either, so just move everything.
3831 -- If the body is generated for an expression function, it may have
3832 -- been preanalyzed already, if 'access was applied to it.
3834 else
3835 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
3836 N_Expression_Function
3837 then
3838 pragma Assert (No (Last_Entity (Body_Id)));
3839 null;
3840 end if;
3842 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3843 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3844 Set_First_Entity (Spec_Id, Empty);
3845 Set_Last_Entity (Spec_Id, Empty);
3846 end if;
3847 end if;
3849 Check_Missing_Return;
3851 -- Now we are going to check for variables that are never modified in
3852 -- the body of the procedure. But first we deal with a special case
3853 -- where we want to modify this check. If the body of the subprogram
3854 -- starts with a raise statement or its equivalent, or if the body
3855 -- consists entirely of a null statement, then it is pretty obvious that
3856 -- it is OK to not reference the parameters. For example, this might be
3857 -- the following common idiom for a stubbed function: statement of the
3858 -- procedure raises an exception. In particular this deals with the
3859 -- common idiom of a stubbed function, which appears something like:
3861 -- function F (A : Integer) return Some_Type;
3862 -- X : Some_Type;
3863 -- begin
3864 -- raise Program_Error;
3865 -- return X;
3866 -- end F;
3868 -- Here the purpose of X is simply to satisfy the annoying requirement
3869 -- in Ada that there be at least one return, and we certainly do not
3870 -- want to go posting warnings on X that it is not initialized. On
3871 -- the other hand, if X is entirely unreferenced that should still
3872 -- get a warning.
3874 -- What we do is to detect these cases, and if we find them, flag the
3875 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3876 -- suppress unwanted warnings. For the case of the function stub above
3877 -- we have a special test to set X as apparently assigned to suppress
3878 -- the warning.
3880 declare
3881 Stm : Node_Id;
3883 begin
3884 -- Skip initial labels (for one thing this occurs when we are in
3885 -- front end ZCX mode, but in any case it is irrelevant), and also
3886 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3888 Stm := First (Statements (HSS));
3889 while Nkind (Stm) = N_Label
3890 or else Nkind (Stm) in N_Push_xxx_Label
3891 loop
3892 Next (Stm);
3893 end loop;
3895 -- Do the test on the original statement before expansion
3897 declare
3898 Ostm : constant Node_Id := Original_Node (Stm);
3900 begin
3901 -- If explicit raise statement, turn on flag
3903 if Nkind (Ostm) = N_Raise_Statement then
3904 Set_Trivial_Subprogram (Stm);
3906 -- If null statement, and no following statements, turn on flag
3908 elsif Nkind (Stm) = N_Null_Statement
3909 and then Comes_From_Source (Stm)
3910 and then No (Next (Stm))
3911 then
3912 Set_Trivial_Subprogram (Stm);
3914 -- Check for explicit call cases which likely raise an exception
3916 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3917 if Is_Entity_Name (Name (Ostm)) then
3918 declare
3919 Ent : constant Entity_Id := Entity (Name (Ostm));
3921 begin
3922 -- If the procedure is marked No_Return, then likely it
3923 -- raises an exception, but in any case it is not coming
3924 -- back here, so turn on the flag.
3926 if Present (Ent)
3927 and then Ekind (Ent) = E_Procedure
3928 and then No_Return (Ent)
3929 then
3930 Set_Trivial_Subprogram (Stm);
3931 end if;
3932 end;
3933 end if;
3934 end if;
3935 end;
3936 end;
3938 -- Check for variables that are never modified
3940 declare
3941 E1, E2 : Entity_Id;
3943 begin
3944 -- If there is a separate spec, then transfer Never_Set_In_Source
3945 -- flags from out parameters to the corresponding entities in the
3946 -- body. The reason we do that is we want to post error flags on
3947 -- the body entities, not the spec entities.
3949 if Present (Spec_Id) then
3950 E1 := First_Entity (Spec_Id);
3951 while Present (E1) loop
3952 if Ekind (E1) = E_Out_Parameter then
3953 E2 := First_Entity (Body_Id);
3954 while Present (E2) loop
3955 exit when Chars (E1) = Chars (E2);
3956 Next_Entity (E2);
3957 end loop;
3959 if Present (E2) then
3960 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3961 end if;
3962 end if;
3964 Next_Entity (E1);
3965 end loop;
3966 end if;
3968 -- Check references in body
3970 Check_References (Body_Id);
3971 end;
3973 -- Check for nested subprogram, and mark outer level subprogram if so
3975 declare
3976 Ent : Entity_Id;
3978 begin
3979 if Present (Spec_Id) then
3980 Ent := Spec_Id;
3981 else
3982 Ent := Body_Id;
3983 end if;
3985 loop
3986 Ent := Enclosing_Subprogram (Ent);
3987 exit when No (Ent) or else Is_Subprogram (Ent);
3988 end loop;
3990 if Present (Ent) then
3991 Set_Has_Nested_Subprogram (Ent);
3992 end if;
3993 end;
3994 end Analyze_Subprogram_Body_Helper;
3996 ---------------------------------
3997 -- Analyze_Subprogram_Contract --
3998 ---------------------------------
4000 procedure Analyze_Subprogram_Contract (Subp_Id : Entity_Id) is
4001 Items : constant Node_Id := Contract (Subp_Id);
4002 Depends : Node_Id := Empty;
4003 Global : Node_Id := Empty;
4004 Mode : SPARK_Mode_Type;
4005 Prag : Node_Id;
4006 Prag_Nam : Name_Id;
4008 begin
4009 -- Due to the timing of contract analysis, delayed pragmas may be
4010 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4011 -- context. To remedy this, restore the original SPARK_Mode of the
4012 -- related subprogram body.
4014 Save_SPARK_Mode_And_Set (Subp_Id, Mode);
4016 -- All subprograms carry a contract, but for some it is not significant
4017 -- and should not be processed.
4019 if not Has_Significant_Contract (Subp_Id) then
4020 null;
4022 elsif Present (Items) then
4024 -- Analyze pre- and postconditions
4026 Prag := Pre_Post_Conditions (Items);
4027 while Present (Prag) loop
4028 Analyze_Pre_Post_Condition_In_Decl_Part (Prag);
4029 Prag := Next_Pragma (Prag);
4030 end loop;
4032 -- Analyze contract-cases and test-cases
4034 Prag := Contract_Test_Cases (Items);
4035 while Present (Prag) loop
4036 Prag_Nam := Pragma_Name (Prag);
4038 if Prag_Nam = Name_Contract_Cases then
4039 Analyze_Contract_Cases_In_Decl_Part (Prag);
4040 else
4041 pragma Assert (Prag_Nam = Name_Test_Case);
4042 Analyze_Test_Case_In_Decl_Part (Prag);
4043 end if;
4045 Prag := Next_Pragma (Prag);
4046 end loop;
4048 -- Analyze classification pragmas
4050 Prag := Classifications (Items);
4051 while Present (Prag) loop
4052 Prag_Nam := Pragma_Name (Prag);
4054 if Prag_Nam = Name_Depends then
4055 Depends := Prag;
4057 elsif Prag_Nam = Name_Global then
4058 Global := Prag;
4060 -- Note that pragma Extensions_Visible has already been analyzed
4062 end if;
4064 Prag := Next_Pragma (Prag);
4065 end loop;
4067 -- Analyze Global first as Depends may mention items classified in
4068 -- the global categorization.
4070 if Present (Global) then
4071 Analyze_Global_In_Decl_Part (Global);
4072 end if;
4074 -- Depends must be analyzed after Global in order to see the modes of
4075 -- all global items.
4077 if Present (Depends) then
4078 Analyze_Depends_In_Decl_Part (Depends);
4079 end if;
4081 -- Ensure that the contract cases or postconditions mention 'Result
4082 -- or define a post-state.
4084 Check_Result_And_Post_State (Subp_Id);
4085 end if;
4087 -- Restore the SPARK_Mode of the enclosing context after all delayed
4088 -- pragmas have been analyzed.
4090 Restore_SPARK_Mode (Mode);
4091 end Analyze_Subprogram_Contract;
4093 ------------------------------------
4094 -- Analyze_Subprogram_Declaration --
4095 ------------------------------------
4097 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4098 Scop : constant Entity_Id := Current_Scope;
4099 Designator : Entity_Id;
4101 Is_Completion : Boolean;
4102 -- Indicates whether a null procedure declaration is a completion
4104 begin
4105 -- The subprogram declaration may be subject to pragma Ghost with policy
4106 -- Ignore. Set the mode now to ensure that any nodes generated during
4107 -- analysis and expansion are properly flagged as ignored Ghost.
4109 Set_Ghost_Mode (N);
4111 -- Null procedures are not allowed in SPARK
4113 if Nkind (Specification (N)) = N_Procedure_Specification
4114 and then Null_Present (Specification (N))
4115 then
4116 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4118 -- Null procedures are allowed in protected types, following the
4119 -- recent AI12-0147.
4121 if Is_Protected_Type (Current_Scope)
4122 and then Ada_Version < Ada_2012
4123 then
4124 Error_Msg_N ("protected operation cannot be a null procedure", N);
4125 end if;
4127 Analyze_Null_Procedure (N, Is_Completion);
4129 if Is_Completion then
4131 -- The null procedure acts as a body, nothing further is needed
4133 return;
4134 end if;
4135 end if;
4137 Designator := Analyze_Subprogram_Specification (Specification (N));
4139 -- A reference may already have been generated for the unit name, in
4140 -- which case the following call is redundant. However it is needed for
4141 -- declarations that are the rewriting of an expression function.
4143 Generate_Definition (Designator);
4145 -- Set SPARK mode from current context (may be overwritten later with
4146 -- explicit pragma).
4148 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4149 Set_SPARK_Pragma_Inherited (Designator);
4151 -- A subprogram declared within a Ghost region is automatically Ghost
4152 -- (SPARK RM 6.9(2)).
4154 if Comes_From_Source (Designator) and then Ghost_Mode > None then
4155 Set_Is_Ghost_Entity (Designator);
4156 end if;
4158 if Debug_Flag_C then
4159 Write_Str ("==> subprogram spec ");
4160 Write_Name (Chars (Designator));
4161 Write_Str (" from ");
4162 Write_Location (Sloc (N));
4163 Write_Eol;
4164 Indent;
4165 end if;
4167 Validate_RCI_Subprogram_Declaration (N);
4168 New_Overloaded_Entity (Designator);
4169 Check_Delayed_Subprogram (Designator);
4171 -- If the type of the first formal of the current subprogram is a non-
4172 -- generic tagged private type, mark the subprogram as being a private
4173 -- primitive. Ditto if this is a function with controlling result, and
4174 -- the return type is currently private. In both cases, the type of the
4175 -- controlling argument or result must be in the current scope for the
4176 -- operation to be primitive.
4178 if Has_Controlling_Result (Designator)
4179 and then Is_Private_Type (Etype (Designator))
4180 and then Scope (Etype (Designator)) = Current_Scope
4181 and then not Is_Generic_Actual_Type (Etype (Designator))
4182 then
4183 Set_Is_Private_Primitive (Designator);
4185 elsif Present (First_Formal (Designator)) then
4186 declare
4187 Formal_Typ : constant Entity_Id :=
4188 Etype (First_Formal (Designator));
4189 begin
4190 Set_Is_Private_Primitive (Designator,
4191 Is_Tagged_Type (Formal_Typ)
4192 and then Scope (Formal_Typ) = Current_Scope
4193 and then Is_Private_Type (Formal_Typ)
4194 and then not Is_Generic_Actual_Type (Formal_Typ));
4195 end;
4196 end if;
4198 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4199 -- or null.
4201 if Ada_Version >= Ada_2005
4202 and then Comes_From_Source (N)
4203 and then Is_Dispatching_Operation (Designator)
4204 then
4205 declare
4206 E : Entity_Id;
4207 Etyp : Entity_Id;
4209 begin
4210 if Has_Controlling_Result (Designator) then
4211 Etyp := Etype (Designator);
4213 else
4214 E := First_Entity (Designator);
4215 while Present (E)
4216 and then Is_Formal (E)
4217 and then not Is_Controlling_Formal (E)
4218 loop
4219 Next_Entity (E);
4220 end loop;
4222 Etyp := Etype (E);
4223 end if;
4225 if Is_Access_Type (Etyp) then
4226 Etyp := Directly_Designated_Type (Etyp);
4227 end if;
4229 if Is_Interface (Etyp)
4230 and then not Is_Abstract_Subprogram (Designator)
4231 and then not (Ekind (Designator) = E_Procedure
4232 and then Null_Present (Specification (N)))
4233 then
4234 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4236 -- Specialize error message based on procedures vs. functions,
4237 -- since functions can't be null subprograms.
4239 if Ekind (Designator) = E_Procedure then
4240 Error_Msg_N
4241 ("interface procedure % must be abstract or null", N);
4242 else
4243 Error_Msg_N
4244 ("interface function % must be abstract", N);
4245 end if;
4246 end if;
4247 end;
4248 end if;
4250 -- What is the following code for, it used to be
4252 -- ??? Set_Suppress_Elaboration_Checks
4253 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4255 -- The following seems equivalent, but a bit dubious
4257 if Elaboration_Checks_Suppressed (Designator) then
4258 Set_Kill_Elaboration_Checks (Designator);
4259 end if;
4261 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4262 Set_Categorization_From_Scope (Designator, Scop);
4264 else
4265 -- For a compilation unit, check for library-unit pragmas
4267 Push_Scope (Designator);
4268 Set_Categorization_From_Pragmas (N);
4269 Validate_Categorization_Dependency (N, Designator);
4270 Pop_Scope;
4271 end if;
4273 -- For a compilation unit, set body required. This flag will only be
4274 -- reset if a valid Import or Interface pragma is processed later on.
4276 if Nkind (Parent (N)) = N_Compilation_Unit then
4277 Set_Body_Required (Parent (N), True);
4279 if Ada_Version >= Ada_2005
4280 and then Nkind (Specification (N)) = N_Procedure_Specification
4281 and then Null_Present (Specification (N))
4282 then
4283 Error_Msg_N
4284 ("null procedure cannot be declared at library level", N);
4285 end if;
4286 end if;
4288 Generate_Reference_To_Formals (Designator);
4289 Check_Eliminated (Designator);
4291 if Debug_Flag_C then
4292 Outdent;
4293 Write_Str ("<== subprogram spec ");
4294 Write_Name (Chars (Designator));
4295 Write_Str (" from ");
4296 Write_Location (Sloc (N));
4297 Write_Eol;
4298 end if;
4300 if Is_Protected_Type (Current_Scope) then
4302 -- Indicate that this is a protected operation, because it may be
4303 -- used in subsequent declarations within the protected type.
4305 Set_Convention (Designator, Convention_Protected);
4306 end if;
4308 List_Inherited_Pre_Post_Aspects (Designator);
4310 if Has_Aspects (N) then
4311 Analyze_Aspect_Specifications (N, Designator);
4312 end if;
4313 end Analyze_Subprogram_Declaration;
4315 --------------------------------------
4316 -- Analyze_Subprogram_Specification --
4317 --------------------------------------
4319 -- Reminder: N here really is a subprogram specification (not a subprogram
4320 -- declaration). This procedure is called to analyze the specification in
4321 -- both subprogram bodies and subprogram declarations (specs).
4323 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4324 Designator : constant Entity_Id := Defining_Entity (N);
4325 Formals : constant List_Id := Parameter_Specifications (N);
4327 -- Start of processing for Analyze_Subprogram_Specification
4329 begin
4330 -- User-defined operator is not allowed in SPARK, except as a renaming
4332 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4333 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4334 then
4335 Check_SPARK_05_Restriction
4336 ("user-defined operator is not allowed", N);
4337 end if;
4339 -- Proceed with analysis. Do not emit a cross-reference entry if the
4340 -- specification comes from an expression function, because it may be
4341 -- the completion of a previous declaration. It is is not, the cross-
4342 -- reference entry will be emitted for the new subprogram declaration.
4344 if Nkind (Parent (N)) /= N_Expression_Function then
4345 Generate_Definition (Designator);
4346 end if;
4348 if Nkind (N) = N_Function_Specification then
4349 Set_Ekind (Designator, E_Function);
4350 Set_Mechanism (Designator, Default_Mechanism);
4351 else
4352 Set_Ekind (Designator, E_Procedure);
4353 Set_Etype (Designator, Standard_Void_Type);
4354 end if;
4356 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4357 -- those subprograms which could be inlined in GNATprove mode (because
4358 -- Body_To_Inline is non-Empty) but cannot be inlined.
4360 if GNATprove_Mode then
4361 Set_Is_Inlined_Always (Designator);
4362 end if;
4364 -- Introduce new scope for analysis of the formals and the return type
4366 Set_Scope (Designator, Current_Scope);
4368 if Present (Formals) then
4369 Push_Scope (Designator);
4370 Process_Formals (Formals, N);
4372 -- Check dimensions in N for formals with default expression
4374 Analyze_Dimension_Formals (N, Formals);
4376 -- Ada 2005 (AI-345): If this is an overriding operation of an
4377 -- inherited interface operation, and the controlling type is
4378 -- a synchronized type, replace the type with its corresponding
4379 -- record, to match the proper signature of an overriding operation.
4380 -- Same processing for an access parameter whose designated type is
4381 -- derived from a synchronized interface.
4383 if Ada_Version >= Ada_2005 then
4384 declare
4385 Formal : Entity_Id;
4386 Formal_Typ : Entity_Id;
4387 Rec_Typ : Entity_Id;
4388 Desig_Typ : Entity_Id;
4390 begin
4391 Formal := First_Formal (Designator);
4392 while Present (Formal) loop
4393 Formal_Typ := Etype (Formal);
4395 if Is_Concurrent_Type (Formal_Typ)
4396 and then Present (Corresponding_Record_Type (Formal_Typ))
4397 then
4398 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4400 if Present (Interfaces (Rec_Typ)) then
4401 Set_Etype (Formal, Rec_Typ);
4402 end if;
4404 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4405 Desig_Typ := Designated_Type (Formal_Typ);
4407 if Is_Concurrent_Type (Desig_Typ)
4408 and then Present (Corresponding_Record_Type (Desig_Typ))
4409 then
4410 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4412 if Present (Interfaces (Rec_Typ)) then
4413 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4414 end if;
4415 end if;
4416 end if;
4418 Next_Formal (Formal);
4419 end loop;
4420 end;
4421 end if;
4423 End_Scope;
4425 -- The subprogram scope is pushed and popped around the processing of
4426 -- the return type for consistency with call above to Process_Formals
4427 -- (which itself can call Analyze_Return_Type), and to ensure that any
4428 -- itype created for the return type will be associated with the proper
4429 -- scope.
4431 elsif Nkind (N) = N_Function_Specification then
4432 Push_Scope (Designator);
4433 Analyze_Return_Type (N);
4434 End_Scope;
4435 end if;
4437 -- Function case
4439 if Nkind (N) = N_Function_Specification then
4441 -- Deal with operator symbol case
4443 if Nkind (Designator) = N_Defining_Operator_Symbol then
4444 Valid_Operator_Definition (Designator);
4445 end if;
4447 May_Need_Actuals (Designator);
4449 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4450 -- the subprogram is abstract also. This does not apply to renaming
4451 -- declarations, where abstractness is inherited, and to subprogram
4452 -- bodies generated for stream operations, which become renamings as
4453 -- bodies.
4455 -- In case of primitives associated with abstract interface types
4456 -- the check is applied later (see Analyze_Subprogram_Declaration).
4458 if not Nkind_In (Original_Node (Parent (N)),
4459 N_Subprogram_Renaming_Declaration,
4460 N_Abstract_Subprogram_Declaration,
4461 N_Formal_Abstract_Subprogram_Declaration)
4462 then
4463 if Is_Abstract_Type (Etype (Designator))
4464 and then not Is_Interface (Etype (Designator))
4465 then
4466 Error_Msg_N
4467 ("function that returns abstract type must be abstract", N);
4469 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4470 -- access result whose designated type is abstract.
4472 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4473 and then
4474 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4475 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4476 and then Ada_Version >= Ada_2012
4477 then
4478 Error_Msg_N ("function whose access result designates "
4479 & "abstract type must be abstract", N);
4480 end if;
4481 end if;
4482 end if;
4484 return Designator;
4485 end Analyze_Subprogram_Specification;
4487 -----------------------
4488 -- Check_Conformance --
4489 -----------------------
4491 procedure Check_Conformance
4492 (New_Id : Entity_Id;
4493 Old_Id : Entity_Id;
4494 Ctype : Conformance_Type;
4495 Errmsg : Boolean;
4496 Conforms : out Boolean;
4497 Err_Loc : Node_Id := Empty;
4498 Get_Inst : Boolean := False;
4499 Skip_Controlling_Formals : Boolean := False)
4501 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4502 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4503 -- If Errmsg is True, then processing continues to post an error message
4504 -- for conformance error on given node. Two messages are output. The
4505 -- first message points to the previous declaration with a general "no
4506 -- conformance" message. The second is the detailed reason, supplied as
4507 -- Msg. The parameter N provide information for a possible & insertion
4508 -- in the message, and also provides the location for posting the
4509 -- message in the absence of a specified Err_Loc location.
4511 -----------------------
4512 -- Conformance_Error --
4513 -----------------------
4515 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4516 Enode : Node_Id;
4518 begin
4519 Conforms := False;
4521 if Errmsg then
4522 if No (Err_Loc) then
4523 Enode := N;
4524 else
4525 Enode := Err_Loc;
4526 end if;
4528 Error_Msg_Sloc := Sloc (Old_Id);
4530 case Ctype is
4531 when Type_Conformant =>
4532 Error_Msg_N -- CODEFIX
4533 ("not type conformant with declaration#!", Enode);
4535 when Mode_Conformant =>
4536 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4537 Error_Msg_N
4538 ("not mode conformant with operation inherited#!",
4539 Enode);
4540 else
4541 Error_Msg_N
4542 ("not mode conformant with declaration#!", Enode);
4543 end if;
4545 when Subtype_Conformant =>
4546 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4547 Error_Msg_N
4548 ("not subtype conformant with operation inherited#!",
4549 Enode);
4550 else
4551 Error_Msg_N
4552 ("not subtype conformant with declaration#!", Enode);
4553 end if;
4555 when Fully_Conformant =>
4556 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4557 Error_Msg_N -- CODEFIX
4558 ("not fully conformant with operation inherited#!",
4559 Enode);
4560 else
4561 Error_Msg_N -- CODEFIX
4562 ("not fully conformant with declaration#!", Enode);
4563 end if;
4564 end case;
4566 Error_Msg_NE (Msg, Enode, N);
4567 end if;
4568 end Conformance_Error;
4570 -- Local Variables
4572 Old_Type : constant Entity_Id := Etype (Old_Id);
4573 New_Type : constant Entity_Id := Etype (New_Id);
4574 Old_Formal : Entity_Id;
4575 New_Formal : Entity_Id;
4576 Access_Types_Match : Boolean;
4577 Old_Formal_Base : Entity_Id;
4578 New_Formal_Base : Entity_Id;
4580 -- Start of processing for Check_Conformance
4582 begin
4583 Conforms := True;
4585 -- We need a special case for operators, since they don't appear
4586 -- explicitly.
4588 if Ctype = Type_Conformant then
4589 if Ekind (New_Id) = E_Operator
4590 and then Operator_Matches_Spec (New_Id, Old_Id)
4591 then
4592 return;
4593 end if;
4594 end if;
4596 -- If both are functions/operators, check return types conform
4598 if Old_Type /= Standard_Void_Type
4599 and then
4600 New_Type /= Standard_Void_Type
4601 then
4602 -- If we are checking interface conformance we omit controlling
4603 -- arguments and result, because we are only checking the conformance
4604 -- of the remaining parameters.
4606 if Has_Controlling_Result (Old_Id)
4607 and then Has_Controlling_Result (New_Id)
4608 and then Skip_Controlling_Formals
4609 then
4610 null;
4612 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4613 if Ctype >= Subtype_Conformant
4614 and then not Predicates_Match (Old_Type, New_Type)
4615 then
4616 Conformance_Error
4617 ("\predicate of return type does not match!", New_Id);
4618 else
4619 Conformance_Error
4620 ("\return type does not match!", New_Id);
4621 end if;
4623 return;
4624 end if;
4626 -- Ada 2005 (AI-231): In case of anonymous access types check the
4627 -- null-exclusion and access-to-constant attributes match.
4629 if Ada_Version >= Ada_2005
4630 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4631 and then
4632 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4633 or else Is_Access_Constant (Etype (Old_Type)) /=
4634 Is_Access_Constant (Etype (New_Type)))
4635 then
4636 Conformance_Error ("\return type does not match!", New_Id);
4637 return;
4638 end if;
4640 -- If either is a function/operator and the other isn't, error
4642 elsif Old_Type /= Standard_Void_Type
4643 or else New_Type /= Standard_Void_Type
4644 then
4645 Conformance_Error ("\functions can only match functions!", New_Id);
4646 return;
4647 end if;
4649 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4650 -- If this is a renaming as body, refine error message to indicate that
4651 -- the conflict is with the original declaration. If the entity is not
4652 -- frozen, the conventions don't have to match, the one of the renamed
4653 -- entity is inherited.
4655 if Ctype >= Subtype_Conformant then
4656 if Convention (Old_Id) /= Convention (New_Id) then
4657 if not Is_Frozen (New_Id) then
4658 null;
4660 elsif Present (Err_Loc)
4661 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4662 and then Present (Corresponding_Spec (Err_Loc))
4663 then
4664 Error_Msg_Name_1 := Chars (New_Id);
4665 Error_Msg_Name_2 :=
4666 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4667 Conformance_Error ("\prior declaration for% has convention %!");
4669 else
4670 Conformance_Error ("\calling conventions do not match!");
4671 end if;
4673 return;
4675 elsif Is_Formal_Subprogram (Old_Id)
4676 or else Is_Formal_Subprogram (New_Id)
4677 then
4678 Conformance_Error ("\formal subprograms not allowed!");
4679 return;
4681 -- Pragma Ghost behaves as a convention in the context of subtype
4682 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4683 -- subprograms as their spec may reside in a Ghost region and their
4684 -- body not, or vice versa.
4686 elsif Comes_From_Source (Old_Id)
4687 and then Comes_From_Source (New_Id)
4688 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4689 then
4690 Conformance_Error ("\ghost modes do not match!");
4691 return;
4692 end if;
4693 end if;
4695 -- Deal with parameters
4697 -- Note: we use the entity information, rather than going directly
4698 -- to the specification in the tree. This is not only simpler, but
4699 -- absolutely necessary for some cases of conformance tests between
4700 -- operators, where the declaration tree simply does not exist.
4702 Old_Formal := First_Formal (Old_Id);
4703 New_Formal := First_Formal (New_Id);
4704 while Present (Old_Formal) and then Present (New_Formal) loop
4705 if Is_Controlling_Formal (Old_Formal)
4706 and then Is_Controlling_Formal (New_Formal)
4707 and then Skip_Controlling_Formals
4708 then
4709 -- The controlling formals will have different types when
4710 -- comparing an interface operation with its match, but both
4711 -- or neither must be access parameters.
4713 if Is_Access_Type (Etype (Old_Formal))
4715 Is_Access_Type (Etype (New_Formal))
4716 then
4717 goto Skip_Controlling_Formal;
4718 else
4719 Conformance_Error
4720 ("\access parameter does not match!", New_Formal);
4721 end if;
4722 end if;
4724 -- Ada 2012: Mode conformance also requires that formal parameters
4725 -- be both aliased, or neither.
4727 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4728 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4729 Conformance_Error
4730 ("\aliased parameter mismatch!", New_Formal);
4731 end if;
4732 end if;
4734 if Ctype = Fully_Conformant then
4736 -- Names must match. Error message is more accurate if we do
4737 -- this before checking that the types of the formals match.
4739 if Chars (Old_Formal) /= Chars (New_Formal) then
4740 Conformance_Error ("\name& does not match!", New_Formal);
4742 -- Set error posted flag on new formal as well to stop
4743 -- junk cascaded messages in some cases.
4745 Set_Error_Posted (New_Formal);
4746 return;
4747 end if;
4749 -- Null exclusion must match
4751 if Null_Exclusion_Present (Parent (Old_Formal))
4753 Null_Exclusion_Present (Parent (New_Formal))
4754 then
4755 -- Only give error if both come from source. This should be
4756 -- investigated some time, since it should not be needed ???
4758 if Comes_From_Source (Old_Formal)
4759 and then
4760 Comes_From_Source (New_Formal)
4761 then
4762 Conformance_Error
4763 ("\null exclusion for& does not match", New_Formal);
4765 -- Mark error posted on the new formal to avoid duplicated
4766 -- complaint about types not matching.
4768 Set_Error_Posted (New_Formal);
4769 end if;
4770 end if;
4771 end if;
4773 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4774 -- case occurs whenever a subprogram is being renamed and one of its
4775 -- parameters imposes a null exclusion. For example:
4777 -- type T is null record;
4778 -- type Acc_T is access T;
4779 -- subtype Acc_T_Sub is Acc_T;
4781 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4782 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4783 -- renames P;
4785 Old_Formal_Base := Etype (Old_Formal);
4786 New_Formal_Base := Etype (New_Formal);
4788 if Get_Inst then
4789 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4790 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4791 end if;
4793 Access_Types_Match := Ada_Version >= Ada_2005
4795 -- Ensure that this rule is only applied when New_Id is a
4796 -- renaming of Old_Id.
4798 and then Nkind (Parent (Parent (New_Id))) =
4799 N_Subprogram_Renaming_Declaration
4800 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4801 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4802 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4804 -- Now handle the allowed access-type case
4806 and then Is_Access_Type (Old_Formal_Base)
4807 and then Is_Access_Type (New_Formal_Base)
4809 -- The type kinds must match. The only exception occurs with
4810 -- multiple generics of the form:
4812 -- generic generic
4813 -- type F is private; type A is private;
4814 -- type F_Ptr is access F; type A_Ptr is access A;
4815 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4816 -- package F_Pack is ... package A_Pack is
4817 -- package F_Inst is
4818 -- new F_Pack (A, A_Ptr, A_P);
4820 -- When checking for conformance between the parameters of A_P
4821 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4822 -- because the compiler has transformed A_Ptr into a subtype of
4823 -- F_Ptr. We catch this case in the code below.
4825 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4826 or else
4827 (Is_Generic_Type (Old_Formal_Base)
4828 and then Is_Generic_Type (New_Formal_Base)
4829 and then Is_Internal (New_Formal_Base)
4830 and then Etype (Etype (New_Formal_Base)) =
4831 Old_Formal_Base))
4832 and then Directly_Designated_Type (Old_Formal_Base) =
4833 Directly_Designated_Type (New_Formal_Base)
4834 and then ((Is_Itype (Old_Formal_Base)
4835 and then Can_Never_Be_Null (Old_Formal_Base))
4836 or else
4837 (Is_Itype (New_Formal_Base)
4838 and then Can_Never_Be_Null (New_Formal_Base)));
4840 -- Types must always match. In the visible part of an instance,
4841 -- usual overloading rules for dispatching operations apply, and
4842 -- we check base types (not the actual subtypes).
4844 if In_Instance_Visible_Part
4845 and then Is_Dispatching_Operation (New_Id)
4846 then
4847 if not Conforming_Types
4848 (T1 => Base_Type (Etype (Old_Formal)),
4849 T2 => Base_Type (Etype (New_Formal)),
4850 Ctype => Ctype,
4851 Get_Inst => Get_Inst)
4852 and then not Access_Types_Match
4853 then
4854 Conformance_Error ("\type of & does not match!", New_Formal);
4855 return;
4856 end if;
4858 elsif not Conforming_Types
4859 (T1 => Old_Formal_Base,
4860 T2 => New_Formal_Base,
4861 Ctype => Ctype,
4862 Get_Inst => Get_Inst)
4863 and then not Access_Types_Match
4864 then
4865 -- Don't give error message if old type is Any_Type. This test
4866 -- avoids some cascaded errors, e.g. in case of a bad spec.
4868 if Errmsg and then Old_Formal_Base = Any_Type then
4869 Conforms := False;
4870 else
4871 if Ctype >= Subtype_Conformant
4872 and then
4873 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
4874 then
4875 Conformance_Error
4876 ("\predicate of & does not match!", New_Formal);
4877 else
4878 Conformance_Error
4879 ("\type of & does not match!", New_Formal);
4880 end if;
4881 end if;
4883 return;
4884 end if;
4886 -- For mode conformance, mode must match
4888 if Ctype >= Mode_Conformant then
4889 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4890 if not Ekind_In (New_Id, E_Function, E_Procedure)
4891 or else not Is_Primitive_Wrapper (New_Id)
4892 then
4893 Conformance_Error ("\mode of & does not match!", New_Formal);
4895 else
4896 declare
4897 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4898 begin
4899 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
4900 then
4901 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
4902 else
4903 Conformance_Error
4904 ("\mode of & does not match!", New_Formal);
4905 end if;
4906 end;
4907 end if;
4909 return;
4911 -- Part of mode conformance for access types is having the same
4912 -- constant modifier.
4914 elsif Access_Types_Match
4915 and then Is_Access_Constant (Old_Formal_Base) /=
4916 Is_Access_Constant (New_Formal_Base)
4917 then
4918 Conformance_Error
4919 ("\constant modifier does not match!", New_Formal);
4920 return;
4921 end if;
4922 end if;
4924 if Ctype >= Subtype_Conformant then
4926 -- Ada 2005 (AI-231): In case of anonymous access types check
4927 -- the null-exclusion and access-to-constant attributes must
4928 -- match. For null exclusion, we test the types rather than the
4929 -- formals themselves, since the attribute is only set reliably
4930 -- on the formals in the Ada 95 case, and we exclude the case
4931 -- where Old_Formal is marked as controlling, to avoid errors
4932 -- when matching completing bodies with dispatching declarations
4933 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4935 if Ada_Version >= Ada_2005
4936 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4937 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4938 and then
4939 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4940 Can_Never_Be_Null (Etype (New_Formal))
4941 and then
4942 not Is_Controlling_Formal (Old_Formal))
4943 or else
4944 Is_Access_Constant (Etype (Old_Formal)) /=
4945 Is_Access_Constant (Etype (New_Formal)))
4947 -- Do not complain if error already posted on New_Formal. This
4948 -- avoids some redundant error messages.
4950 and then not Error_Posted (New_Formal)
4951 then
4952 -- It is allowed to omit the null-exclusion in case of stream
4953 -- attribute subprograms. We recognize stream subprograms
4954 -- through their TSS-generated suffix.
4956 declare
4957 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4959 begin
4960 if TSS_Name /= TSS_Stream_Read
4961 and then TSS_Name /= TSS_Stream_Write
4962 and then TSS_Name /= TSS_Stream_Input
4963 and then TSS_Name /= TSS_Stream_Output
4964 then
4965 -- Here we have a definite conformance error. It is worth
4966 -- special casing the error message for the case of a
4967 -- controlling formal (which excludes null).
4969 if Is_Controlling_Formal (New_Formal) then
4970 Error_Msg_Node_2 := Scope (New_Formal);
4971 Conformance_Error
4972 ("\controlling formal & of & excludes null, "
4973 & "declaration must exclude null as well",
4974 New_Formal);
4976 -- Normal case (couldn't we give more detail here???)
4978 else
4979 Conformance_Error
4980 ("\type of & does not match!", New_Formal);
4981 end if;
4983 return;
4984 end if;
4985 end;
4986 end if;
4987 end if;
4989 -- Full conformance checks
4991 if Ctype = Fully_Conformant then
4993 -- We have checked already that names match
4995 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4997 -- Check default expressions for in parameters
4999 declare
5000 NewD : constant Boolean :=
5001 Present (Default_Value (New_Formal));
5002 OldD : constant Boolean :=
5003 Present (Default_Value (Old_Formal));
5004 begin
5005 if NewD or OldD then
5007 -- The old default value has been analyzed because the
5008 -- current full declaration will have frozen everything
5009 -- before. The new default value has not been analyzed,
5010 -- so analyze it now before we check for conformance.
5012 if NewD then
5013 Push_Scope (New_Id);
5014 Preanalyze_Spec_Expression
5015 (Default_Value (New_Formal), Etype (New_Formal));
5016 End_Scope;
5017 end if;
5019 if not (NewD and OldD)
5020 or else not Fully_Conformant_Expressions
5021 (Default_Value (Old_Formal),
5022 Default_Value (New_Formal))
5023 then
5024 Conformance_Error
5025 ("\default expression for & does not match!",
5026 New_Formal);
5027 return;
5028 end if;
5029 end if;
5030 end;
5031 end if;
5032 end if;
5034 -- A couple of special checks for Ada 83 mode. These checks are
5035 -- skipped if either entity is an operator in package Standard,
5036 -- or if either old or new instance is not from the source program.
5038 if Ada_Version = Ada_83
5039 and then Sloc (Old_Id) > Standard_Location
5040 and then Sloc (New_Id) > Standard_Location
5041 and then Comes_From_Source (Old_Id)
5042 and then Comes_From_Source (New_Id)
5043 then
5044 declare
5045 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5046 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5048 begin
5049 -- Explicit IN must be present or absent in both cases. This
5050 -- test is required only in the full conformance case.
5052 if In_Present (Old_Param) /= In_Present (New_Param)
5053 and then Ctype = Fully_Conformant
5054 then
5055 Conformance_Error
5056 ("\(Ada 83) IN must appear in both declarations",
5057 New_Formal);
5058 return;
5059 end if;
5061 -- Grouping (use of comma in param lists) must be the same
5062 -- This is where we catch a misconformance like:
5064 -- A, B : Integer
5065 -- A : Integer; B : Integer
5067 -- which are represented identically in the tree except
5068 -- for the setting of the flags More_Ids and Prev_Ids.
5070 if More_Ids (Old_Param) /= More_Ids (New_Param)
5071 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5072 then
5073 Conformance_Error
5074 ("\grouping of & does not match!", New_Formal);
5075 return;
5076 end if;
5077 end;
5078 end if;
5080 -- This label is required when skipping controlling formals
5082 <<Skip_Controlling_Formal>>
5084 Next_Formal (Old_Formal);
5085 Next_Formal (New_Formal);
5086 end loop;
5088 if Present (Old_Formal) then
5089 Conformance_Error ("\too few parameters!");
5090 return;
5092 elsif Present (New_Formal) then
5093 Conformance_Error ("\too many parameters!", New_Formal);
5094 return;
5095 end if;
5096 end Check_Conformance;
5098 -----------------------
5099 -- Check_Conventions --
5100 -----------------------
5102 procedure Check_Conventions (Typ : Entity_Id) is
5103 Ifaces_List : Elist_Id;
5105 procedure Check_Convention (Op : Entity_Id);
5106 -- Verify that the convention of inherited dispatching operation Op is
5107 -- consistent among all subprograms it overrides. In order to minimize
5108 -- the search, Search_From is utilized to designate a specific point in
5109 -- the list rather than iterating over the whole list once more.
5111 ----------------------
5112 -- Check_Convention --
5113 ----------------------
5115 procedure Check_Convention (Op : Entity_Id) is
5116 Op_Conv : constant Convention_Id := Convention (Op);
5117 Iface_Conv : Convention_Id;
5118 Iface_Elmt : Elmt_Id;
5119 Iface_Prim_Elmt : Elmt_Id;
5120 Iface_Prim : Entity_Id;
5122 begin
5123 Iface_Elmt := First_Elmt (Ifaces_List);
5124 while Present (Iface_Elmt) loop
5125 Iface_Prim_Elmt :=
5126 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5127 while Present (Iface_Prim_Elmt) loop
5128 Iface_Prim := Node (Iface_Prim_Elmt);
5129 Iface_Conv := Convention (Iface_Prim);
5131 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5132 and then Iface_Conv /= Op_Conv
5133 then
5134 Error_Msg_N
5135 ("inconsistent conventions in primitive operations", Typ);
5137 Error_Msg_Name_1 := Chars (Op);
5138 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5139 Error_Msg_Sloc := Sloc (Op);
5141 if Comes_From_Source (Op) or else No (Alias (Op)) then
5142 if not Present (Overridden_Operation (Op)) then
5143 Error_Msg_N ("\\primitive % defined #", Typ);
5144 else
5145 Error_Msg_N
5146 ("\\overriding operation % with "
5147 & "convention % defined #", Typ);
5148 end if;
5150 else pragma Assert (Present (Alias (Op)));
5151 Error_Msg_Sloc := Sloc (Alias (Op));
5152 Error_Msg_N ("\\inherited operation % with "
5153 & "convention % defined #", Typ);
5154 end if;
5156 Error_Msg_Name_1 := Chars (Op);
5157 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5158 Error_Msg_Sloc := Sloc (Iface_Prim);
5159 Error_Msg_N ("\\overridden operation % with "
5160 & "convention % defined #", Typ);
5162 -- Avoid cascading errors
5164 return;
5165 end if;
5167 Next_Elmt (Iface_Prim_Elmt);
5168 end loop;
5170 Next_Elmt (Iface_Elmt);
5171 end loop;
5172 end Check_Convention;
5174 -- Local variables
5176 Prim_Op : Entity_Id;
5177 Prim_Op_Elmt : Elmt_Id;
5179 -- Start of processing for Check_Conventions
5181 begin
5182 if not Has_Interfaces (Typ) then
5183 return;
5184 end if;
5186 Collect_Interfaces (Typ, Ifaces_List);
5188 -- The algorithm checks every overriding dispatching operation against
5189 -- all the corresponding overridden dispatching operations, detecting
5190 -- differences in conventions.
5192 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5193 while Present (Prim_Op_Elmt) loop
5194 Prim_Op := Node (Prim_Op_Elmt);
5196 -- A small optimization: skip the predefined dispatching operations
5197 -- since they always have the same convention.
5199 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5200 Check_Convention (Prim_Op);
5201 end if;
5203 Next_Elmt (Prim_Op_Elmt);
5204 end loop;
5205 end Check_Conventions;
5207 ------------------------------
5208 -- Check_Delayed_Subprogram --
5209 ------------------------------
5211 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5212 F : Entity_Id;
5214 procedure Possible_Freeze (T : Entity_Id);
5215 -- T is the type of either a formal parameter or of the return type.
5216 -- If T is not yet frozen and needs a delayed freeze, then the
5217 -- subprogram itself must be delayed. If T is the limited view of an
5218 -- incomplete type the subprogram must be frozen as well, because
5219 -- T may depend on local types that have not been frozen yet.
5221 ---------------------
5222 -- Possible_Freeze --
5223 ---------------------
5225 procedure Possible_Freeze (T : Entity_Id) is
5226 begin
5227 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5228 Set_Has_Delayed_Freeze (Designator);
5230 elsif Is_Access_Type (T)
5231 and then Has_Delayed_Freeze (Designated_Type (T))
5232 and then not Is_Frozen (Designated_Type (T))
5233 then
5234 Set_Has_Delayed_Freeze (Designator);
5236 elsif Ekind (T) = E_Incomplete_Type
5237 and then From_Limited_With (T)
5238 then
5239 Set_Has_Delayed_Freeze (Designator);
5241 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5242 -- of a subprogram or entry declaration.
5244 elsif Ekind (T) = E_Incomplete_Type
5245 and then Ada_Version >= Ada_2012
5246 then
5247 Set_Has_Delayed_Freeze (Designator);
5248 end if;
5250 end Possible_Freeze;
5252 -- Start of processing for Check_Delayed_Subprogram
5254 begin
5255 -- All subprograms, including abstract subprograms, may need a freeze
5256 -- node if some formal type or the return type needs one.
5258 Possible_Freeze (Etype (Designator));
5259 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5261 -- Need delayed freeze if any of the formal types themselves need
5262 -- a delayed freeze and are not yet frozen.
5264 F := First_Formal (Designator);
5265 while Present (F) loop
5266 Possible_Freeze (Etype (F));
5267 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5268 Next_Formal (F);
5269 end loop;
5271 -- Mark functions that return by reference. Note that it cannot be
5272 -- done for delayed_freeze subprograms because the underlying
5273 -- returned type may not be known yet (for private types)
5275 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5276 declare
5277 Typ : constant Entity_Id := Etype (Designator);
5278 Utyp : constant Entity_Id := Underlying_Type (Typ);
5279 begin
5280 if Is_Limited_View (Typ) then
5281 Set_Returns_By_Ref (Designator);
5282 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5283 Set_Returns_By_Ref (Designator);
5284 end if;
5285 end;
5286 end if;
5287 end Check_Delayed_Subprogram;
5289 ------------------------------------
5290 -- Check_Discriminant_Conformance --
5291 ------------------------------------
5293 procedure Check_Discriminant_Conformance
5294 (N : Node_Id;
5295 Prev : Entity_Id;
5296 Prev_Loc : Node_Id)
5298 Old_Discr : Entity_Id := First_Discriminant (Prev);
5299 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5300 New_Discr_Id : Entity_Id;
5301 New_Discr_Type : Entity_Id;
5303 procedure Conformance_Error (Msg : String; N : Node_Id);
5304 -- Post error message for conformance error on given node. Two messages
5305 -- are output. The first points to the previous declaration with a
5306 -- general "no conformance" message. The second is the detailed reason,
5307 -- supplied as Msg. The parameter N provide information for a possible
5308 -- & insertion in the message.
5310 -----------------------
5311 -- Conformance_Error --
5312 -----------------------
5314 procedure Conformance_Error (Msg : String; N : Node_Id) is
5315 begin
5316 Error_Msg_Sloc := Sloc (Prev_Loc);
5317 Error_Msg_N -- CODEFIX
5318 ("not fully conformant with declaration#!", N);
5319 Error_Msg_NE (Msg, N, N);
5320 end Conformance_Error;
5322 -- Start of processing for Check_Discriminant_Conformance
5324 begin
5325 while Present (Old_Discr) and then Present (New_Discr) loop
5326 New_Discr_Id := Defining_Identifier (New_Discr);
5328 -- The subtype mark of the discriminant on the full type has not
5329 -- been analyzed so we do it here. For an access discriminant a new
5330 -- type is created.
5332 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5333 New_Discr_Type :=
5334 Access_Definition (N, Discriminant_Type (New_Discr));
5336 else
5337 Analyze (Discriminant_Type (New_Discr));
5338 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5340 -- Ada 2005: if the discriminant definition carries a null
5341 -- exclusion, create an itype to check properly for consistency
5342 -- with partial declaration.
5344 if Is_Access_Type (New_Discr_Type)
5345 and then Null_Exclusion_Present (New_Discr)
5346 then
5347 New_Discr_Type :=
5348 Create_Null_Excluding_Itype
5349 (T => New_Discr_Type,
5350 Related_Nod => New_Discr,
5351 Scope_Id => Current_Scope);
5352 end if;
5353 end if;
5355 if not Conforming_Types
5356 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5357 then
5358 Conformance_Error ("type of & does not match!", New_Discr_Id);
5359 return;
5360 else
5361 -- Treat the new discriminant as an occurrence of the old one,
5362 -- for navigation purposes, and fill in some semantic
5363 -- information, for completeness.
5365 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5366 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5367 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5368 end if;
5370 -- Names must match
5372 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5373 Conformance_Error ("name & does not match!", New_Discr_Id);
5374 return;
5375 end if;
5377 -- Default expressions must match
5379 declare
5380 NewD : constant Boolean :=
5381 Present (Expression (New_Discr));
5382 OldD : constant Boolean :=
5383 Present (Expression (Parent (Old_Discr)));
5385 begin
5386 if NewD or OldD then
5388 -- The old default value has been analyzed and expanded,
5389 -- because the current full declaration will have frozen
5390 -- everything before. The new default values have not been
5391 -- expanded, so expand now to check conformance.
5393 if NewD then
5394 Preanalyze_Spec_Expression
5395 (Expression (New_Discr), New_Discr_Type);
5396 end if;
5398 if not (NewD and OldD)
5399 or else not Fully_Conformant_Expressions
5400 (Expression (Parent (Old_Discr)),
5401 Expression (New_Discr))
5403 then
5404 Conformance_Error
5405 ("default expression for & does not match!",
5406 New_Discr_Id);
5407 return;
5408 end if;
5409 end if;
5410 end;
5412 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5414 if Ada_Version = Ada_83 then
5415 declare
5416 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5418 begin
5419 -- Grouping (use of comma in param lists) must be the same
5420 -- This is where we catch a misconformance like:
5422 -- A, B : Integer
5423 -- A : Integer; B : Integer
5425 -- which are represented identically in the tree except
5426 -- for the setting of the flags More_Ids and Prev_Ids.
5428 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5429 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5430 then
5431 Conformance_Error
5432 ("grouping of & does not match!", New_Discr_Id);
5433 return;
5434 end if;
5435 end;
5436 end if;
5438 Next_Discriminant (Old_Discr);
5439 Next (New_Discr);
5440 end loop;
5442 if Present (Old_Discr) then
5443 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5444 return;
5446 elsif Present (New_Discr) then
5447 Conformance_Error
5448 ("too many discriminants!", Defining_Identifier (New_Discr));
5449 return;
5450 end if;
5451 end Check_Discriminant_Conformance;
5453 ----------------------------
5454 -- Check_Fully_Conformant --
5455 ----------------------------
5457 procedure Check_Fully_Conformant
5458 (New_Id : Entity_Id;
5459 Old_Id : Entity_Id;
5460 Err_Loc : Node_Id := Empty)
5462 Result : Boolean;
5463 pragma Warnings (Off, Result);
5464 begin
5465 Check_Conformance
5466 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5467 end Check_Fully_Conformant;
5469 ---------------------------
5470 -- Check_Mode_Conformant --
5471 ---------------------------
5473 procedure Check_Mode_Conformant
5474 (New_Id : Entity_Id;
5475 Old_Id : Entity_Id;
5476 Err_Loc : Node_Id := Empty;
5477 Get_Inst : Boolean := False)
5479 Result : Boolean;
5480 pragma Warnings (Off, Result);
5481 begin
5482 Check_Conformance
5483 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5484 end Check_Mode_Conformant;
5486 --------------------------------
5487 -- Check_Overriding_Indicator --
5488 --------------------------------
5490 procedure Check_Overriding_Indicator
5491 (Subp : Entity_Id;
5492 Overridden_Subp : Entity_Id;
5493 Is_Primitive : Boolean)
5495 Decl : Node_Id;
5496 Spec : Node_Id;
5498 begin
5499 -- No overriding indicator for literals
5501 if Ekind (Subp) = E_Enumeration_Literal then
5502 return;
5504 elsif Ekind (Subp) = E_Entry then
5505 Decl := Parent (Subp);
5507 -- No point in analyzing a malformed operator
5509 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5510 and then Error_Posted (Subp)
5511 then
5512 return;
5514 else
5515 Decl := Unit_Declaration_Node (Subp);
5516 end if;
5518 if Nkind_In (Decl, N_Subprogram_Body,
5519 N_Subprogram_Body_Stub,
5520 N_Subprogram_Declaration,
5521 N_Abstract_Subprogram_Declaration,
5522 N_Subprogram_Renaming_Declaration)
5523 then
5524 Spec := Specification (Decl);
5526 elsif Nkind (Decl) = N_Entry_Declaration then
5527 Spec := Decl;
5529 else
5530 return;
5531 end if;
5533 -- The overriding operation is type conformant with the overridden one,
5534 -- but the names of the formals are not required to match. If the names
5535 -- appear permuted in the overriding operation, this is a possible
5536 -- source of confusion that is worth diagnosing. Controlling formals
5537 -- often carry names that reflect the type, and it is not worthwhile
5538 -- requiring that their names match.
5540 if Present (Overridden_Subp)
5541 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5542 then
5543 declare
5544 Form1 : Entity_Id;
5545 Form2 : Entity_Id;
5547 begin
5548 Form1 := First_Formal (Subp);
5549 Form2 := First_Formal (Overridden_Subp);
5551 -- If the overriding operation is a synchronized operation, skip
5552 -- the first parameter of the overridden operation, which is
5553 -- implicit in the new one. If the operation is declared in the
5554 -- body it is not primitive and all formals must match.
5556 if Is_Concurrent_Type (Scope (Subp))
5557 and then Is_Tagged_Type (Scope (Subp))
5558 and then not Has_Completion (Scope (Subp))
5559 then
5560 Form2 := Next_Formal (Form2);
5561 end if;
5563 if Present (Form1) then
5564 Form1 := Next_Formal (Form1);
5565 Form2 := Next_Formal (Form2);
5566 end if;
5568 while Present (Form1) loop
5569 if not Is_Controlling_Formal (Form1)
5570 and then Present (Next_Formal (Form2))
5571 and then Chars (Form1) = Chars (Next_Formal (Form2))
5572 then
5573 Error_Msg_Node_2 := Alias (Overridden_Subp);
5574 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5575 Error_Msg_NE
5576 ("& does not match corresponding formal of&#",
5577 Form1, Form1);
5578 exit;
5579 end if;
5581 Next_Formal (Form1);
5582 Next_Formal (Form2);
5583 end loop;
5584 end;
5585 end if;
5587 -- If there is an overridden subprogram, then check that there is no
5588 -- "not overriding" indicator, and mark the subprogram as overriding.
5589 -- This is not done if the overridden subprogram is marked as hidden,
5590 -- which can occur for the case of inherited controlled operations
5591 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5592 -- subprogram is not itself hidden. (Note: This condition could probably
5593 -- be simplified, leaving out the testing for the specific controlled
5594 -- cases, but it seems safer and clearer this way, and echoes similar
5595 -- special-case tests of this kind in other places.)
5597 if Present (Overridden_Subp)
5598 and then (not Is_Hidden (Overridden_Subp)
5599 or else
5600 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5601 Name_Adjust,
5602 Name_Finalize)
5603 and then Present (Alias (Overridden_Subp))
5604 and then not Is_Hidden (Alias (Overridden_Subp))))
5605 then
5606 if Must_Not_Override (Spec) then
5607 Error_Msg_Sloc := Sloc (Overridden_Subp);
5609 if Ekind (Subp) = E_Entry then
5610 Error_Msg_NE
5611 ("entry & overrides inherited operation #", Spec, Subp);
5612 else
5613 Error_Msg_NE
5614 ("subprogram & overrides inherited operation #", Spec, Subp);
5615 end if;
5617 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5618 -- as an extension of Root_Controlled, and thus has a useless Adjust
5619 -- operation. This operation should not be inherited by other limited
5620 -- controlled types. An explicit Adjust for them is not overriding.
5622 elsif Must_Override (Spec)
5623 and then Chars (Overridden_Subp) = Name_Adjust
5624 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5625 and then Present (Alias (Overridden_Subp))
5626 and then
5627 Is_Predefined_File_Name
5628 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5629 then
5630 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5632 elsif Is_Subprogram (Subp) then
5633 if Is_Init_Proc (Subp) then
5634 null;
5636 elsif No (Overridden_Operation (Subp)) then
5638 -- For entities generated by Derive_Subprograms the overridden
5639 -- operation is the inherited primitive (which is available
5640 -- through the attribute alias)
5642 if (Is_Dispatching_Operation (Subp)
5643 or else Is_Dispatching_Operation (Overridden_Subp))
5644 and then not Comes_From_Source (Overridden_Subp)
5645 and then Find_Dispatching_Type (Overridden_Subp) =
5646 Find_Dispatching_Type (Subp)
5647 and then Present (Alias (Overridden_Subp))
5648 and then Comes_From_Source (Alias (Overridden_Subp))
5649 then
5650 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5651 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5653 else
5654 Set_Overridden_Operation (Subp, Overridden_Subp);
5655 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5656 end if;
5657 end if;
5658 end if;
5660 -- If primitive flag is set or this is a protected operation, then
5661 -- the operation is overriding at the point of its declaration, so
5662 -- warn if necessary. Otherwise it may have been declared before the
5663 -- operation it overrides and no check is required.
5665 if Style_Check
5666 and then not Must_Override (Spec)
5667 and then (Is_Primitive
5668 or else Ekind (Scope (Subp)) = E_Protected_Type)
5669 then
5670 Style.Missing_Overriding (Decl, Subp);
5671 end if;
5673 -- If Subp is an operator, it may override a predefined operation, if
5674 -- it is defined in the same scope as the type to which it applies.
5675 -- In that case Overridden_Subp is empty because of our implicit
5676 -- representation for predefined operators. We have to check whether the
5677 -- signature of Subp matches that of a predefined operator. Note that
5678 -- first argument provides the name of the operator, and the second
5679 -- argument the signature that may match that of a standard operation.
5680 -- If the indicator is overriding, then the operator must match a
5681 -- predefined signature, because we know already that there is no
5682 -- explicit overridden operation.
5684 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5685 if Must_Not_Override (Spec) then
5687 -- If this is not a primitive or a protected subprogram, then
5688 -- "not overriding" is illegal.
5690 if not Is_Primitive
5691 and then Ekind (Scope (Subp)) /= E_Protected_Type
5692 then
5693 Error_Msg_N ("overriding indicator only allowed "
5694 & "if subprogram is primitive", Subp);
5696 elsif Can_Override_Operator (Subp) then
5697 Error_Msg_NE
5698 ("subprogram& overrides predefined operator ", Spec, Subp);
5699 end if;
5701 elsif Must_Override (Spec) then
5702 if No (Overridden_Operation (Subp))
5703 and then not Can_Override_Operator (Subp)
5704 then
5705 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5706 end if;
5708 elsif not Error_Posted (Subp)
5709 and then Style_Check
5710 and then Can_Override_Operator (Subp)
5711 and then
5712 not Is_Predefined_File_Name
5713 (Unit_File_Name (Get_Source_Unit (Subp)))
5714 then
5715 -- If style checks are enabled, indicate that the indicator is
5716 -- missing. However, at the point of declaration, the type of
5717 -- which this is a primitive operation may be private, in which
5718 -- case the indicator would be premature.
5720 if Has_Private_Declaration (Etype (Subp))
5721 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5722 then
5723 null;
5724 else
5725 Style.Missing_Overriding (Decl, Subp);
5726 end if;
5727 end if;
5729 elsif Must_Override (Spec) then
5730 if Ekind (Subp) = E_Entry then
5731 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5732 else
5733 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5734 end if;
5736 -- If the operation is marked "not overriding" and it's not primitive
5737 -- then an error is issued, unless this is an operation of a task or
5738 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5739 -- has been specified have already been checked above.
5741 elsif Must_Not_Override (Spec)
5742 and then not Is_Primitive
5743 and then Ekind (Subp) /= E_Entry
5744 and then Ekind (Scope (Subp)) /= E_Protected_Type
5745 then
5746 Error_Msg_N
5747 ("overriding indicator only allowed if subprogram is primitive",
5748 Subp);
5749 return;
5750 end if;
5751 end Check_Overriding_Indicator;
5753 -------------------
5754 -- Check_Returns --
5755 -------------------
5757 -- Note: this procedure needs to know far too much about how the expander
5758 -- messes with exceptions. The use of the flag Exception_Junk and the
5759 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5760 -- works, but is not very clean. It would be better if the expansion
5761 -- routines would leave Original_Node working nicely, and we could use
5762 -- Original_Node here to ignore all the peculiar expander messing ???
5764 procedure Check_Returns
5765 (HSS : Node_Id;
5766 Mode : Character;
5767 Err : out Boolean;
5768 Proc : Entity_Id := Empty)
5770 Handler : Node_Id;
5772 procedure Check_Statement_Sequence (L : List_Id);
5773 -- Internal recursive procedure to check a list of statements for proper
5774 -- termination by a return statement (or a transfer of control or a
5775 -- compound statement that is itself internally properly terminated).
5777 ------------------------------
5778 -- Check_Statement_Sequence --
5779 ------------------------------
5781 procedure Check_Statement_Sequence (L : List_Id) is
5782 Last_Stm : Node_Id;
5783 Stm : Node_Id;
5784 Kind : Node_Kind;
5786 function Assert_False return Boolean;
5787 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5788 -- rewritten as a null statement when assertions are off. The assert
5789 -- is not active, but it is still enough to kill the warning.
5791 ------------------
5792 -- Assert_False --
5793 ------------------
5795 function Assert_False return Boolean is
5796 Orig : constant Node_Id := Original_Node (Last_Stm);
5798 begin
5799 if Nkind (Orig) = N_Pragma
5800 and then Pragma_Name (Orig) = Name_Assert
5801 and then not Error_Posted (Orig)
5802 then
5803 declare
5804 Arg : constant Node_Id :=
5805 First (Pragma_Argument_Associations (Orig));
5806 Exp : constant Node_Id := Expression (Arg);
5807 begin
5808 return Nkind (Exp) = N_Identifier
5809 and then Chars (Exp) = Name_False;
5810 end;
5812 else
5813 return False;
5814 end if;
5815 end Assert_False;
5817 -- Local variables
5819 Raise_Exception_Call : Boolean;
5820 -- Set True if statement sequence terminated by Raise_Exception call
5821 -- or a Reraise_Occurrence call.
5823 -- Start of processing for Check_Statement_Sequence
5825 begin
5826 Raise_Exception_Call := False;
5828 -- Get last real statement
5830 Last_Stm := Last (L);
5832 -- Deal with digging out exception handler statement sequences that
5833 -- have been transformed by the local raise to goto optimization.
5834 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5835 -- optimization has occurred, we are looking at something like:
5837 -- begin
5838 -- original stmts in block
5840 -- exception \
5841 -- when excep1 => |
5842 -- goto L1; | omitted if No_Exception_Propagation
5843 -- when excep2 => |
5844 -- goto L2; /
5845 -- end;
5847 -- goto L3; -- skip handler when exception not raised
5849 -- <<L1>> -- target label for local exception
5850 -- begin
5851 -- estmts1
5852 -- end;
5854 -- goto L3;
5856 -- <<L2>>
5857 -- begin
5858 -- estmts2
5859 -- end;
5861 -- <<L3>>
5863 -- and what we have to do is to dig out the estmts1 and estmts2
5864 -- sequences (which were the original sequences of statements in
5865 -- the exception handlers) and check them.
5867 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
5868 Stm := Last_Stm;
5869 loop
5870 Prev (Stm);
5871 exit when No (Stm);
5872 exit when Nkind (Stm) /= N_Block_Statement;
5873 exit when not Exception_Junk (Stm);
5874 Prev (Stm);
5875 exit when No (Stm);
5876 exit when Nkind (Stm) /= N_Label;
5877 exit when not Exception_Junk (Stm);
5878 Check_Statement_Sequence
5879 (Statements (Handled_Statement_Sequence (Next (Stm))));
5881 Prev (Stm);
5882 Last_Stm := Stm;
5883 exit when No (Stm);
5884 exit when Nkind (Stm) /= N_Goto_Statement;
5885 exit when not Exception_Junk (Stm);
5886 end loop;
5887 end if;
5889 -- Don't count pragmas
5891 while Nkind (Last_Stm) = N_Pragma
5893 -- Don't count call to SS_Release (can happen after Raise_Exception)
5895 or else
5896 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5897 and then
5898 Nkind (Name (Last_Stm)) = N_Identifier
5899 and then
5900 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5902 -- Don't count exception junk
5904 or else
5905 (Nkind_In (Last_Stm, N_Goto_Statement,
5906 N_Label,
5907 N_Object_Declaration)
5908 and then Exception_Junk (Last_Stm))
5909 or else Nkind (Last_Stm) in N_Push_xxx_Label
5910 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5912 -- Inserted code, such as finalization calls, is irrelevant: we only
5913 -- need to check original source.
5915 or else Is_Rewrite_Insertion (Last_Stm)
5916 loop
5917 Prev (Last_Stm);
5918 end loop;
5920 -- Here we have the "real" last statement
5922 Kind := Nkind (Last_Stm);
5924 -- Transfer of control, OK. Note that in the No_Return procedure
5925 -- case, we already diagnosed any explicit return statements, so
5926 -- we can treat them as OK in this context.
5928 if Is_Transfer (Last_Stm) then
5929 return;
5931 -- Check cases of explicit non-indirect procedure calls
5933 elsif Kind = N_Procedure_Call_Statement
5934 and then Is_Entity_Name (Name (Last_Stm))
5935 then
5936 -- Check call to Raise_Exception procedure which is treated
5937 -- specially, as is a call to Reraise_Occurrence.
5939 -- We suppress the warning in these cases since it is likely that
5940 -- the programmer really does not expect to deal with the case
5941 -- of Null_Occurrence, and thus would find a warning about a
5942 -- missing return curious, and raising Program_Error does not
5943 -- seem such a bad behavior if this does occur.
5945 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5946 -- behavior will be to raise Constraint_Error (see AI-329).
5948 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5949 or else
5950 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5951 then
5952 Raise_Exception_Call := True;
5954 -- For Raise_Exception call, test first argument, if it is
5955 -- an attribute reference for a 'Identity call, then we know
5956 -- that the call cannot possibly return.
5958 declare
5959 Arg : constant Node_Id :=
5960 Original_Node (First_Actual (Last_Stm));
5961 begin
5962 if Nkind (Arg) = N_Attribute_Reference
5963 and then Attribute_Name (Arg) = Name_Identity
5964 then
5965 return;
5966 end if;
5967 end;
5968 end if;
5970 -- If statement, need to look inside if there is an else and check
5971 -- each constituent statement sequence for proper termination.
5973 elsif Kind = N_If_Statement
5974 and then Present (Else_Statements (Last_Stm))
5975 then
5976 Check_Statement_Sequence (Then_Statements (Last_Stm));
5977 Check_Statement_Sequence (Else_Statements (Last_Stm));
5979 if Present (Elsif_Parts (Last_Stm)) then
5980 declare
5981 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5983 begin
5984 while Present (Elsif_Part) loop
5985 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5986 Next (Elsif_Part);
5987 end loop;
5988 end;
5989 end if;
5991 return;
5993 -- Case statement, check each case for proper termination
5995 elsif Kind = N_Case_Statement then
5996 declare
5997 Case_Alt : Node_Id;
5998 begin
5999 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6000 while Present (Case_Alt) loop
6001 Check_Statement_Sequence (Statements (Case_Alt));
6002 Next_Non_Pragma (Case_Alt);
6003 end loop;
6004 end;
6006 return;
6008 -- Block statement, check its handled sequence of statements
6010 elsif Kind = N_Block_Statement then
6011 declare
6012 Err1 : Boolean;
6014 begin
6015 Check_Returns
6016 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6018 if Err1 then
6019 Err := True;
6020 end if;
6022 return;
6023 end;
6025 -- Loop statement. If there is an iteration scheme, we can definitely
6026 -- fall out of the loop. Similarly if there is an exit statement, we
6027 -- can fall out. In either case we need a following return.
6029 elsif Kind = N_Loop_Statement then
6030 if Present (Iteration_Scheme (Last_Stm))
6031 or else Has_Exit (Entity (Identifier (Last_Stm)))
6032 then
6033 null;
6035 -- A loop with no exit statement or iteration scheme is either
6036 -- an infinite loop, or it has some other exit (raise/return).
6037 -- In either case, no warning is required.
6039 else
6040 return;
6041 end if;
6043 -- Timed entry call, check entry call and delay alternatives
6045 -- Note: in expanded code, the timed entry call has been converted
6046 -- to a set of expanded statements on which the check will work
6047 -- correctly in any case.
6049 elsif Kind = N_Timed_Entry_Call then
6050 declare
6051 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6052 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6054 begin
6055 -- If statement sequence of entry call alternative is missing,
6056 -- then we can definitely fall through, and we post the error
6057 -- message on the entry call alternative itself.
6059 if No (Statements (ECA)) then
6060 Last_Stm := ECA;
6062 -- If statement sequence of delay alternative is missing, then
6063 -- we can definitely fall through, and we post the error
6064 -- message on the delay alternative itself.
6066 -- Note: if both ECA and DCA are missing the return, then we
6067 -- post only one message, should be enough to fix the bugs.
6068 -- If not we will get a message next time on the DCA when the
6069 -- ECA is fixed.
6071 elsif No (Statements (DCA)) then
6072 Last_Stm := DCA;
6074 -- Else check both statement sequences
6076 else
6077 Check_Statement_Sequence (Statements (ECA));
6078 Check_Statement_Sequence (Statements (DCA));
6079 return;
6080 end if;
6081 end;
6083 -- Conditional entry call, check entry call and else part
6085 -- Note: in expanded code, the conditional entry call has been
6086 -- converted to a set of expanded statements on which the check
6087 -- will work correctly in any case.
6089 elsif Kind = N_Conditional_Entry_Call then
6090 declare
6091 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6093 begin
6094 -- If statement sequence of entry call alternative is missing,
6095 -- then we can definitely fall through, and we post the error
6096 -- message on the entry call alternative itself.
6098 if No (Statements (ECA)) then
6099 Last_Stm := ECA;
6101 -- Else check statement sequence and else part
6103 else
6104 Check_Statement_Sequence (Statements (ECA));
6105 Check_Statement_Sequence (Else_Statements (Last_Stm));
6106 return;
6107 end if;
6108 end;
6109 end if;
6111 -- If we fall through, issue appropriate message
6113 if Mode = 'F' then
6115 -- Kill warning if last statement is a raise exception call,
6116 -- or a pragma Assert (False). Note that with assertions enabled,
6117 -- such a pragma has been converted into a raise exception call
6118 -- already, so the Assert_False is for the assertions off case.
6120 if not Raise_Exception_Call and then not Assert_False then
6122 -- In GNATprove mode, it is an error to have a missing return
6124 Error_Msg_Warn := SPARK_Mode /= On;
6126 -- Issue error message or warning
6128 Error_Msg_N
6129 ("RETURN statement missing following this statement<<!",
6130 Last_Stm);
6131 Error_Msg_N
6132 ("\Program_Error ]<<!", Last_Stm);
6133 end if;
6135 -- Note: we set Err even though we have not issued a warning
6136 -- because we still have a case of a missing return. This is
6137 -- an extremely marginal case, probably will never be noticed
6138 -- but we might as well get it right.
6140 Err := True;
6142 -- Otherwise we have the case of a procedure marked No_Return
6144 else
6145 if not Raise_Exception_Call then
6146 if GNATprove_Mode then
6147 Error_Msg_N
6148 ("implied return after this statement "
6149 & "would have raised Program_Error", Last_Stm);
6150 else
6151 Error_Msg_N
6152 ("implied return after this statement "
6153 & "will raise Program_Error??", Last_Stm);
6154 end if;
6156 Error_Msg_Warn := SPARK_Mode /= On;
6157 Error_Msg_NE
6158 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6159 end if;
6161 declare
6162 RE : constant Node_Id :=
6163 Make_Raise_Program_Error (Sloc (Last_Stm),
6164 Reason => PE_Implicit_Return);
6165 begin
6166 Insert_After (Last_Stm, RE);
6167 Analyze (RE);
6168 end;
6169 end if;
6170 end Check_Statement_Sequence;
6172 -- Start of processing for Check_Returns
6174 begin
6175 Err := False;
6176 Check_Statement_Sequence (Statements (HSS));
6178 if Present (Exception_Handlers (HSS)) then
6179 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6180 while Present (Handler) loop
6181 Check_Statement_Sequence (Statements (Handler));
6182 Next_Non_Pragma (Handler);
6183 end loop;
6184 end if;
6185 end Check_Returns;
6187 ----------------------------
6188 -- Check_Subprogram_Order --
6189 ----------------------------
6191 procedure Check_Subprogram_Order (N : Node_Id) is
6193 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6194 -- This is used to check if S1 > S2 in the sense required by this test,
6195 -- for example nameab < namec, but name2 < name10.
6197 -----------------------------
6198 -- Subprogram_Name_Greater --
6199 -----------------------------
6201 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6202 L1, L2 : Positive;
6203 N1, N2 : Natural;
6205 begin
6206 -- Deal with special case where names are identical except for a
6207 -- numerical suffix. These are handled specially, taking the numeric
6208 -- ordering from the suffix into account.
6210 L1 := S1'Last;
6211 while S1 (L1) in '0' .. '9' loop
6212 L1 := L1 - 1;
6213 end loop;
6215 L2 := S2'Last;
6216 while S2 (L2) in '0' .. '9' loop
6217 L2 := L2 - 1;
6218 end loop;
6220 -- If non-numeric parts non-equal, do straight compare
6222 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6223 return S1 > S2;
6225 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6226 -- that a missing suffix is treated as numeric zero in this test.
6228 else
6229 N1 := 0;
6230 while L1 < S1'Last loop
6231 L1 := L1 + 1;
6232 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6233 end loop;
6235 N2 := 0;
6236 while L2 < S2'Last loop
6237 L2 := L2 + 1;
6238 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6239 end loop;
6241 return N1 > N2;
6242 end if;
6243 end Subprogram_Name_Greater;
6245 -- Start of processing for Check_Subprogram_Order
6247 begin
6248 -- Check body in alpha order if this is option
6250 if Style_Check
6251 and then Style_Check_Order_Subprograms
6252 and then Nkind (N) = N_Subprogram_Body
6253 and then Comes_From_Source (N)
6254 and then In_Extended_Main_Source_Unit (N)
6255 then
6256 declare
6257 LSN : String_Ptr
6258 renames Scope_Stack.Table
6259 (Scope_Stack.Last).Last_Subprogram_Name;
6261 Body_Id : constant Entity_Id :=
6262 Defining_Entity (Specification (N));
6264 begin
6265 Get_Decoded_Name_String (Chars (Body_Id));
6267 if LSN /= null then
6268 if Subprogram_Name_Greater
6269 (LSN.all, Name_Buffer (1 .. Name_Len))
6270 then
6271 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6272 end if;
6274 Free (LSN);
6275 end if;
6277 LSN := new String'(Name_Buffer (1 .. Name_Len));
6278 end;
6279 end if;
6280 end Check_Subprogram_Order;
6282 ------------------------------
6283 -- Check_Subtype_Conformant --
6284 ------------------------------
6286 procedure Check_Subtype_Conformant
6287 (New_Id : Entity_Id;
6288 Old_Id : Entity_Id;
6289 Err_Loc : Node_Id := Empty;
6290 Skip_Controlling_Formals : Boolean := False;
6291 Get_Inst : Boolean := False)
6293 Result : Boolean;
6294 pragma Warnings (Off, Result);
6295 begin
6296 Check_Conformance
6297 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6298 Skip_Controlling_Formals => Skip_Controlling_Formals,
6299 Get_Inst => Get_Inst);
6300 end Check_Subtype_Conformant;
6302 ---------------------------
6303 -- Check_Type_Conformant --
6304 ---------------------------
6306 procedure Check_Type_Conformant
6307 (New_Id : Entity_Id;
6308 Old_Id : Entity_Id;
6309 Err_Loc : Node_Id := Empty)
6311 Result : Boolean;
6312 pragma Warnings (Off, Result);
6313 begin
6314 Check_Conformance
6315 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6316 end Check_Type_Conformant;
6318 ---------------------------
6319 -- Can_Override_Operator --
6320 ---------------------------
6322 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6323 Typ : Entity_Id;
6325 begin
6326 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6327 return False;
6329 else
6330 Typ := Base_Type (Etype (First_Formal (Subp)));
6332 -- Check explicitly that the operation is a primitive of the type
6334 return Operator_Matches_Spec (Subp, Subp)
6335 and then not Is_Generic_Type (Typ)
6336 and then Scope (Subp) = Scope (Typ)
6337 and then not Is_Class_Wide_Type (Typ);
6338 end if;
6339 end Can_Override_Operator;
6341 ----------------------
6342 -- Conforming_Types --
6343 ----------------------
6345 function Conforming_Types
6346 (T1 : Entity_Id;
6347 T2 : Entity_Id;
6348 Ctype : Conformance_Type;
6349 Get_Inst : Boolean := False) return Boolean
6351 Type_1 : Entity_Id := T1;
6352 Type_2 : Entity_Id := T2;
6353 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6355 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6356 -- If neither T1 nor T2 are generic actual types, or if they are in
6357 -- different scopes (e.g. parent and child instances), then verify that
6358 -- the base types are equal. Otherwise T1 and T2 must be on the same
6359 -- subtype chain. The whole purpose of this procedure is to prevent
6360 -- spurious ambiguities in an instantiation that may arise if two
6361 -- distinct generic types are instantiated with the same actual.
6363 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6364 -- An access parameter can designate an incomplete type. If the
6365 -- incomplete type is the limited view of a type from a limited_
6366 -- with_clause, check whether the non-limited view is available. If
6367 -- it is a (non-limited) incomplete type, get the full view.
6369 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6370 -- Returns True if and only if either T1 denotes a limited view of T2
6371 -- or T2 denotes a limited view of T1. This can arise when the limited
6372 -- with view of a type is used in a subprogram declaration and the
6373 -- subprogram body is in the scope of a regular with clause for the
6374 -- same unit. In such a case, the two type entities can be considered
6375 -- identical for purposes of conformance checking.
6377 ----------------------
6378 -- Base_Types_Match --
6379 ----------------------
6381 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6382 BT1 : constant Entity_Id := Base_Type (T1);
6383 BT2 : constant Entity_Id := Base_Type (T2);
6385 begin
6386 if T1 = T2 then
6387 return True;
6389 elsif BT1 = BT2 then
6391 -- The following is too permissive. A more precise test should
6392 -- check that the generic actual is an ancestor subtype of the
6393 -- other ???.
6395 -- See code in Find_Corresponding_Spec that applies an additional
6396 -- filter to handle accidental amiguities in instances.
6398 return not Is_Generic_Actual_Type (T1)
6399 or else not Is_Generic_Actual_Type (T2)
6400 or else Scope (T1) /= Scope (T2);
6402 -- If T2 is a generic actual type it is declared as the subtype of
6403 -- the actual. If that actual is itself a subtype we need to use its
6404 -- own base type to check for compatibility.
6406 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6407 return True;
6409 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6410 return True;
6412 else
6413 return False;
6414 end if;
6415 end Base_Types_Match;
6417 --------------------------
6418 -- Find_Designated_Type --
6419 --------------------------
6421 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6422 Desig : Entity_Id;
6424 begin
6425 Desig := Directly_Designated_Type (T);
6427 if Ekind (Desig) = E_Incomplete_Type then
6429 -- If regular incomplete type, get full view if available
6431 if Present (Full_View (Desig)) then
6432 Desig := Full_View (Desig);
6434 -- If limited view of a type, get non-limited view if available,
6435 -- and check again for a regular incomplete type.
6437 elsif Present (Non_Limited_View (Desig)) then
6438 Desig := Get_Full_View (Non_Limited_View (Desig));
6439 end if;
6440 end if;
6442 return Desig;
6443 end Find_Designated_Type;
6445 -------------------------------
6446 -- Matches_Limited_With_View --
6447 -------------------------------
6449 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6450 begin
6451 -- In some cases a type imported through a limited_with clause, and
6452 -- its nonlimited view are both visible, for example in an anonymous
6453 -- access-to-class-wide type in a formal, or when building the body
6454 -- for a subprogram renaming after the subprogram has been frozen.
6455 -- In these cases Both entities designate the same type. In addition,
6456 -- if one of them is an actual in an instance, it may be a subtype of
6457 -- the non-limited view of the other.
6459 if From_Limited_With (T1)
6460 and then (T2 = Available_View (T1)
6461 or else Is_Subtype_Of (T2, Available_View (T1)))
6462 then
6463 return True;
6465 elsif From_Limited_With (T2)
6466 and then (T1 = Available_View (T2)
6467 or else Is_Subtype_Of (T1, Available_View (T2)))
6468 then
6469 return True;
6471 elsif From_Limited_With (T1)
6472 and then From_Limited_With (T2)
6473 and then Available_View (T1) = Available_View (T2)
6474 then
6475 return True;
6477 else
6478 return False;
6479 end if;
6480 end Matches_Limited_With_View;
6482 -- Start of processing for Conforming_Types
6484 begin
6485 -- The context is an instance association for a formal access-to-
6486 -- subprogram type; the formal parameter types require mapping because
6487 -- they may denote other formal parameters of the generic unit.
6489 if Get_Inst then
6490 Type_1 := Get_Instance_Of (T1);
6491 Type_2 := Get_Instance_Of (T2);
6492 end if;
6494 -- If one of the types is a view of the other introduced by a limited
6495 -- with clause, treat these as conforming for all purposes.
6497 if Matches_Limited_With_View (T1, T2) then
6498 return True;
6500 elsif Base_Types_Match (Type_1, Type_2) then
6501 return Ctype <= Mode_Conformant
6502 or else Subtypes_Statically_Match (Type_1, Type_2);
6504 elsif Is_Incomplete_Or_Private_Type (Type_1)
6505 and then Present (Full_View (Type_1))
6506 and then Base_Types_Match (Full_View (Type_1), Type_2)
6507 then
6508 return Ctype <= Mode_Conformant
6509 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6511 elsif Ekind (Type_2) = E_Incomplete_Type
6512 and then Present (Full_View (Type_2))
6513 and then Base_Types_Match (Type_1, Full_View (Type_2))
6514 then
6515 return Ctype <= Mode_Conformant
6516 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6518 elsif Is_Private_Type (Type_2)
6519 and then In_Instance
6520 and then Present (Full_View (Type_2))
6521 and then Base_Types_Match (Type_1, Full_View (Type_2))
6522 then
6523 return Ctype <= Mode_Conformant
6524 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6526 -- In Ada2012, incomplete types (including limited views) can appear
6527 -- as actuals in instantiations.
6529 elsif Is_Incomplete_Type (Type_1)
6530 and then Is_Incomplete_Type (Type_2)
6531 and then (Used_As_Generic_Actual (Type_1)
6532 or else Used_As_Generic_Actual (Type_2))
6533 then
6534 return True;
6535 end if;
6537 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6538 -- treated recursively because they carry a signature. As far as
6539 -- conformance is concerned, convention plays no role, and either
6540 -- or both could be access to protected subprograms.
6542 Are_Anonymous_Access_To_Subprogram_Types :=
6543 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6544 E_Anonymous_Access_Protected_Subprogram_Type)
6545 and then
6546 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6547 E_Anonymous_Access_Protected_Subprogram_Type);
6549 -- Test anonymous access type case. For this case, static subtype
6550 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6551 -- the base types because we may have built internal subtype entities
6552 -- to handle null-excluding types (see Process_Formals).
6554 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6555 and then
6556 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6558 -- Ada 2005 (AI-254)
6560 or else Are_Anonymous_Access_To_Subprogram_Types
6561 then
6562 declare
6563 Desig_1 : Entity_Id;
6564 Desig_2 : Entity_Id;
6566 begin
6567 -- In Ada 2005, access constant indicators must match for
6568 -- subtype conformance.
6570 if Ada_Version >= Ada_2005
6571 and then Ctype >= Subtype_Conformant
6572 and then
6573 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6574 then
6575 return False;
6576 end if;
6578 Desig_1 := Find_Designated_Type (Type_1);
6579 Desig_2 := Find_Designated_Type (Type_2);
6581 -- If the context is an instance association for a formal
6582 -- access-to-subprogram type; formal access parameter designated
6583 -- types require mapping because they may denote other formal
6584 -- parameters of the generic unit.
6586 if Get_Inst then
6587 Desig_1 := Get_Instance_Of (Desig_1);
6588 Desig_2 := Get_Instance_Of (Desig_2);
6589 end if;
6591 -- It is possible for a Class_Wide_Type to be introduced for an
6592 -- incomplete type, in which case there is a separate class_ wide
6593 -- type for the full view. The types conform if their Etypes
6594 -- conform, i.e. one may be the full view of the other. This can
6595 -- only happen in the context of an access parameter, other uses
6596 -- of an incomplete Class_Wide_Type are illegal.
6598 if Is_Class_Wide_Type (Desig_1)
6599 and then
6600 Is_Class_Wide_Type (Desig_2)
6601 then
6602 return
6603 Conforming_Types
6604 (Etype (Base_Type (Desig_1)),
6605 Etype (Base_Type (Desig_2)), Ctype);
6607 elsif Are_Anonymous_Access_To_Subprogram_Types then
6608 if Ada_Version < Ada_2005 then
6609 return Ctype = Type_Conformant
6610 or else
6611 Subtypes_Statically_Match (Desig_1, Desig_2);
6613 -- We must check the conformance of the signatures themselves
6615 else
6616 declare
6617 Conformant : Boolean;
6618 begin
6619 Check_Conformance
6620 (Desig_1, Desig_2, Ctype, False, Conformant);
6621 return Conformant;
6622 end;
6623 end if;
6625 -- A limited view of an actual matches the corresponding
6626 -- incomplete formal.
6628 elsif Ekind (Desig_2) = E_Incomplete_Subtype
6629 and then From_Limited_With (Desig_2)
6630 and then Used_As_Generic_Actual (Etype (Desig_2))
6631 then
6632 return True;
6634 else
6635 return Base_Type (Desig_1) = Base_Type (Desig_2)
6636 and then (Ctype = Type_Conformant
6637 or else
6638 Subtypes_Statically_Match (Desig_1, Desig_2));
6639 end if;
6640 end;
6642 -- Otherwise definitely no match
6644 else
6645 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6646 and then Is_Access_Type (Type_2))
6647 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6648 and then Is_Access_Type (Type_1)))
6649 and then
6650 Conforming_Types
6651 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6652 then
6653 May_Hide_Profile := True;
6654 end if;
6656 return False;
6657 end if;
6658 end Conforming_Types;
6660 --------------------------
6661 -- Create_Extra_Formals --
6662 --------------------------
6664 procedure Create_Extra_Formals (E : Entity_Id) is
6665 Formal : Entity_Id;
6666 First_Extra : Entity_Id := Empty;
6667 Last_Extra : Entity_Id;
6668 Formal_Type : Entity_Id;
6669 P_Formal : Entity_Id := Empty;
6671 function Add_Extra_Formal
6672 (Assoc_Entity : Entity_Id;
6673 Typ : Entity_Id;
6674 Scope : Entity_Id;
6675 Suffix : String) return Entity_Id;
6676 -- Add an extra formal to the current list of formals and extra formals.
6677 -- The extra formal is added to the end of the list of extra formals,
6678 -- and also returned as the result. These formals are always of mode IN.
6679 -- The new formal has the type Typ, is declared in Scope, and its name
6680 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6681 -- The following suffixes are currently used. They should not be changed
6682 -- without coordinating with CodePeer, which makes use of these to
6683 -- provide better messages.
6685 -- O denotes the Constrained bit.
6686 -- L denotes the accessibility level.
6687 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6688 -- the full list in exp_ch6.BIP_Formal_Kind.
6690 ----------------------
6691 -- Add_Extra_Formal --
6692 ----------------------
6694 function Add_Extra_Formal
6695 (Assoc_Entity : Entity_Id;
6696 Typ : Entity_Id;
6697 Scope : Entity_Id;
6698 Suffix : String) return Entity_Id
6700 EF : constant Entity_Id :=
6701 Make_Defining_Identifier (Sloc (Assoc_Entity),
6702 Chars => New_External_Name (Chars (Assoc_Entity),
6703 Suffix => Suffix));
6705 begin
6706 -- A little optimization. Never generate an extra formal for the
6707 -- _init operand of an initialization procedure, since it could
6708 -- never be used.
6710 if Chars (Formal) = Name_uInit then
6711 return Empty;
6712 end if;
6714 Set_Ekind (EF, E_In_Parameter);
6715 Set_Actual_Subtype (EF, Typ);
6716 Set_Etype (EF, Typ);
6717 Set_Scope (EF, Scope);
6718 Set_Mechanism (EF, Default_Mechanism);
6719 Set_Formal_Validity (EF);
6721 if No (First_Extra) then
6722 First_Extra := EF;
6723 Set_Extra_Formals (Scope, First_Extra);
6724 end if;
6726 if Present (Last_Extra) then
6727 Set_Extra_Formal (Last_Extra, EF);
6728 end if;
6730 Last_Extra := EF;
6732 return EF;
6733 end Add_Extra_Formal;
6735 -- Start of processing for Create_Extra_Formals
6737 begin
6738 -- We never generate extra formals if expansion is not active because we
6739 -- don't need them unless we are generating code.
6741 if not Expander_Active then
6742 return;
6743 end if;
6745 -- No need to generate extra formals in interface thunks whose target
6746 -- primitive has no extra formals.
6748 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6749 return;
6750 end if;
6752 -- If this is a derived subprogram then the subtypes of the parent
6753 -- subprogram's formal parameters will be used to determine the need
6754 -- for extra formals.
6756 if Is_Overloadable (E) and then Present (Alias (E)) then
6757 P_Formal := First_Formal (Alias (E));
6758 end if;
6760 Last_Extra := Empty;
6761 Formal := First_Formal (E);
6762 while Present (Formal) loop
6763 Last_Extra := Formal;
6764 Next_Formal (Formal);
6765 end loop;
6767 -- If Extra_formals were already created, don't do it again. This
6768 -- situation may arise for subprogram types created as part of
6769 -- dispatching calls (see Expand_Dispatching_Call)
6771 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6772 return;
6773 end if;
6775 -- If the subprogram is a predefined dispatching subprogram then don't
6776 -- generate any extra constrained or accessibility level formals. In
6777 -- general we suppress these for internal subprograms (by not calling
6778 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6779 -- generated stream attributes do get passed through because extra
6780 -- build-in-place formals are needed in some cases (limited 'Input).
6782 if Is_Predefined_Internal_Operation (E) then
6783 goto Test_For_Func_Result_Extras;
6784 end if;
6786 Formal := First_Formal (E);
6787 while Present (Formal) loop
6789 -- Create extra formal for supporting the attribute 'Constrained.
6790 -- The case of a private type view without discriminants also
6791 -- requires the extra formal if the underlying type has defaulted
6792 -- discriminants.
6794 if Ekind (Formal) /= E_In_Parameter then
6795 if Present (P_Formal) then
6796 Formal_Type := Etype (P_Formal);
6797 else
6798 Formal_Type := Etype (Formal);
6799 end if;
6801 -- Do not produce extra formals for Unchecked_Union parameters.
6802 -- Jump directly to the end of the loop.
6804 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6805 goto Skip_Extra_Formal_Generation;
6806 end if;
6808 if not Has_Discriminants (Formal_Type)
6809 and then Ekind (Formal_Type) in Private_Kind
6810 and then Present (Underlying_Type (Formal_Type))
6811 then
6812 Formal_Type := Underlying_Type (Formal_Type);
6813 end if;
6815 -- Suppress the extra formal if formal's subtype is constrained or
6816 -- indefinite, or we're compiling for Ada 2012 and the underlying
6817 -- type is tagged and limited. In Ada 2012, a limited tagged type
6818 -- can have defaulted discriminants, but 'Constrained is required
6819 -- to return True, so the formal is never needed (see AI05-0214).
6820 -- Note that this ensures consistency of calling sequences for
6821 -- dispatching operations when some types in a class have defaults
6822 -- on discriminants and others do not (and requiring the extra
6823 -- formal would introduce distributed overhead).
6825 -- If the type does not have a completion yet, treat as prior to
6826 -- Ada 2012 for consistency.
6828 if Has_Discriminants (Formal_Type)
6829 and then not Is_Constrained (Formal_Type)
6830 and then not Is_Indefinite_Subtype (Formal_Type)
6831 and then (Ada_Version < Ada_2012
6832 or else No (Underlying_Type (Formal_Type))
6833 or else not
6834 (Is_Limited_Type (Formal_Type)
6835 and then
6836 (Is_Tagged_Type
6837 (Underlying_Type (Formal_Type)))))
6838 then
6839 Set_Extra_Constrained
6840 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6841 end if;
6842 end if;
6844 -- Create extra formal for supporting accessibility checking. This
6845 -- is done for both anonymous access formals and formals of named
6846 -- access types that are marked as controlling formals. The latter
6847 -- case can occur when Expand_Dispatching_Call creates a subprogram
6848 -- type and substitutes the types of access-to-class-wide actuals
6849 -- for the anonymous access-to-specific-type of controlling formals.
6850 -- Base_Type is applied because in cases where there is a null
6851 -- exclusion the formal may have an access subtype.
6853 -- This is suppressed if we specifically suppress accessibility
6854 -- checks at the package level for either the subprogram, or the
6855 -- package in which it resides. However, we do not suppress it
6856 -- simply if the scope has accessibility checks suppressed, since
6857 -- this could cause trouble when clients are compiled with a
6858 -- different suppression setting. The explicit checks at the
6859 -- package level are safe from this point of view.
6861 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6862 or else (Is_Controlling_Formal (Formal)
6863 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6864 and then not
6865 (Explicit_Suppress (E, Accessibility_Check)
6866 or else
6867 Explicit_Suppress (Scope (E), Accessibility_Check))
6868 and then
6869 (No (P_Formal)
6870 or else Present (Extra_Accessibility (P_Formal)))
6871 then
6872 Set_Extra_Accessibility
6873 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6874 end if;
6876 -- This label is required when skipping extra formal generation for
6877 -- Unchecked_Union parameters.
6879 <<Skip_Extra_Formal_Generation>>
6881 if Present (P_Formal) then
6882 Next_Formal (P_Formal);
6883 end if;
6885 Next_Formal (Formal);
6886 end loop;
6888 <<Test_For_Func_Result_Extras>>
6890 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6891 -- function call is ... determined by the point of call ...".
6893 if Needs_Result_Accessibility_Level (E) then
6894 Set_Extra_Accessibility_Of_Result
6895 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6896 end if;
6898 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6899 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6901 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6902 declare
6903 Result_Subt : constant Entity_Id := Etype (E);
6904 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
6905 Formal_Typ : Entity_Id;
6907 Discard : Entity_Id;
6908 pragma Warnings (Off, Discard);
6910 begin
6911 -- In the case of functions with unconstrained result subtypes,
6912 -- add a 4-state formal indicating whether the return object is
6913 -- allocated by the caller (1), or should be allocated by the
6914 -- callee on the secondary stack (2), in the global heap (3), or
6915 -- in a user-defined storage pool (4). For the moment we just use
6916 -- Natural for the type of this formal. Note that this formal
6917 -- isn't usually needed in the case where the result subtype is
6918 -- constrained, but it is needed when the function has a tagged
6919 -- result, because generally such functions can be called in a
6920 -- dispatching context and such calls must be handled like calls
6921 -- to a class-wide function.
6923 if Needs_BIP_Alloc_Form (E) then
6924 Discard :=
6925 Add_Extra_Formal
6926 (E, Standard_Natural,
6927 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6929 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
6930 -- use a user-defined pool. This formal is not added on
6931 -- .NET/JVM/ZFP as those targets do not support pools.
6933 if VM_Target = No_VM
6934 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6935 then
6936 Discard :=
6937 Add_Extra_Formal
6938 (E, RTE (RE_Root_Storage_Pool_Ptr),
6939 E, BIP_Formal_Suffix (BIP_Storage_Pool));
6940 end if;
6941 end if;
6943 -- In the case of functions whose result type needs finalization,
6944 -- add an extra formal which represents the finalization master.
6946 if Needs_BIP_Finalization_Master (E) then
6947 Discard :=
6948 Add_Extra_Formal
6949 (E, RTE (RE_Finalization_Master_Ptr),
6950 E, BIP_Formal_Suffix (BIP_Finalization_Master));
6951 end if;
6953 -- When the result type contains tasks, add two extra formals: the
6954 -- master of the tasks to be created, and the caller's activation
6955 -- chain.
6957 if Has_Task (Full_Subt) then
6958 Discard :=
6959 Add_Extra_Formal
6960 (E, RTE (RE_Master_Id),
6961 E, BIP_Formal_Suffix (BIP_Task_Master));
6962 Discard :=
6963 Add_Extra_Formal
6964 (E, RTE (RE_Activation_Chain_Access),
6965 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6966 end if;
6968 -- All build-in-place functions get an extra formal that will be
6969 -- passed the address of the return object within the caller.
6971 Formal_Typ :=
6972 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
6974 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
6975 Set_Etype (Formal_Typ, Formal_Typ);
6976 Set_Depends_On_Private
6977 (Formal_Typ, Has_Private_Component (Formal_Typ));
6978 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
6979 Set_Is_Access_Constant (Formal_Typ, False);
6981 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6982 -- the designated type comes from the limited view (for back-end
6983 -- purposes).
6985 Set_From_Limited_With
6986 (Formal_Typ, From_Limited_With (Result_Subt));
6988 Layout_Type (Formal_Typ);
6990 Discard :=
6991 Add_Extra_Formal
6992 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
6993 end;
6994 end if;
6995 end Create_Extra_Formals;
6997 -----------------------------
6998 -- Enter_Overloaded_Entity --
6999 -----------------------------
7001 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7002 E : Entity_Id := Current_Entity_In_Scope (S);
7003 C_E : Entity_Id := Current_Entity (S);
7005 begin
7006 if Present (E) then
7007 Set_Has_Homonym (E);
7008 Set_Has_Homonym (S);
7009 end if;
7011 Set_Is_Immediately_Visible (S);
7012 Set_Scope (S, Current_Scope);
7014 -- Chain new entity if front of homonym in current scope, so that
7015 -- homonyms are contiguous.
7017 if Present (E) and then E /= C_E then
7018 while Homonym (C_E) /= E loop
7019 C_E := Homonym (C_E);
7020 end loop;
7022 Set_Homonym (C_E, S);
7024 else
7025 E := C_E;
7026 Set_Current_Entity (S);
7027 end if;
7029 Set_Homonym (S, E);
7031 if Is_Inherited_Operation (S) then
7032 Append_Inherited_Subprogram (S);
7033 else
7034 Append_Entity (S, Current_Scope);
7035 end if;
7037 Set_Public_Status (S);
7039 if Debug_Flag_E then
7040 Write_Str ("New overloaded entity chain: ");
7041 Write_Name (Chars (S));
7043 E := S;
7044 while Present (E) loop
7045 Write_Str (" "); Write_Int (Int (E));
7046 E := Homonym (E);
7047 end loop;
7049 Write_Eol;
7050 end if;
7052 -- Generate warning for hiding
7054 if Warn_On_Hiding
7055 and then Comes_From_Source (S)
7056 and then In_Extended_Main_Source_Unit (S)
7057 then
7058 E := S;
7059 loop
7060 E := Homonym (E);
7061 exit when No (E);
7063 -- Warn unless genuine overloading. Do not emit warning on
7064 -- hiding predefined operators in Standard (these are either an
7065 -- (artifact of our implicit declarations, or simple noise) but
7066 -- keep warning on a operator defined on a local subtype, because
7067 -- of the real danger that different operators may be applied in
7068 -- various parts of the program.
7070 -- Note that if E and S have the same scope, there is never any
7071 -- hiding. Either the two conflict, and the program is illegal,
7072 -- or S is overriding an implicit inherited subprogram.
7074 if Scope (E) /= Scope (S)
7075 and then (not Is_Overloadable (E)
7076 or else Subtype_Conformant (E, S))
7077 and then (Is_Immediately_Visible (E)
7078 or else
7079 Is_Potentially_Use_Visible (S))
7080 then
7081 if Scope (E) /= Standard_Standard then
7082 Error_Msg_Sloc := Sloc (E);
7083 Error_Msg_N ("declaration of & hides one #?h?", S);
7085 elsif Nkind (S) = N_Defining_Operator_Symbol
7086 and then
7087 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7088 then
7089 Error_Msg_N
7090 ("declaration of & hides predefined operator?h?", S);
7091 end if;
7092 end if;
7093 end loop;
7094 end if;
7095 end Enter_Overloaded_Entity;
7097 -----------------------------
7098 -- Check_Untagged_Equality --
7099 -----------------------------
7101 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7102 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7103 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7104 Obj_Decl : Node_Id;
7106 begin
7107 -- This check applies only if we have a subprogram declaration with an
7108 -- untagged record type.
7110 if Nkind (Decl) /= N_Subprogram_Declaration
7111 or else not Is_Record_Type (Typ)
7112 or else Is_Tagged_Type (Typ)
7113 then
7114 return;
7115 end if;
7117 -- In Ada 2012 case, we will output errors or warnings depending on
7118 -- the setting of debug flag -gnatd.E.
7120 if Ada_Version >= Ada_2012 then
7121 Error_Msg_Warn := Debug_Flag_Dot_EE;
7123 -- In earlier versions of Ada, nothing to do unless we are warning on
7124 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7126 else
7127 if not Warn_On_Ada_2012_Compatibility then
7128 return;
7129 end if;
7130 end if;
7132 -- Cases where the type has already been frozen
7134 if Is_Frozen (Typ) then
7136 -- If the type is not declared in a package, or if we are in the body
7137 -- of the package or in some other scope, the new operation is not
7138 -- primitive, and therefore legal, though suspicious. Should we
7139 -- generate a warning in this case ???
7141 if Ekind (Scope (Typ)) /= E_Package
7142 or else Scope (Typ) /= Current_Scope
7143 then
7144 return;
7146 -- If the type is a generic actual (sub)type, the operation is not
7147 -- primitive either because the base type is declared elsewhere.
7149 elsif Is_Generic_Actual_Type (Typ) then
7150 return;
7152 -- Here we have a definite error of declaration after freezing
7154 else
7155 if Ada_Version >= Ada_2012 then
7156 Error_Msg_NE
7157 ("equality operator must be declared before type & is "
7158 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7160 -- In Ada 2012 mode with error turned to warning, output one
7161 -- more warning to warn that the equality operation may not
7162 -- compose. This is the consequence of ignoring the error.
7164 if Error_Msg_Warn then
7165 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7166 end if;
7168 else
7169 Error_Msg_NE
7170 ("equality operator must be declared before type& is "
7171 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7172 end if;
7174 -- If we are in the package body, we could just move the
7175 -- declaration to the package spec, so add a message saying that.
7177 if In_Package_Body (Scope (Typ)) then
7178 if Ada_Version >= Ada_2012 then
7179 Error_Msg_N
7180 ("\move declaration to package spec<<", Eq_Op);
7181 else
7182 Error_Msg_N
7183 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7184 end if;
7186 -- Otherwise try to find the freezing point
7188 else
7189 Obj_Decl := Next (Parent (Typ));
7190 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7191 if Nkind (Obj_Decl) = N_Object_Declaration
7192 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7193 then
7194 -- Freezing point, output warnings
7196 if Ada_Version >= Ada_2012 then
7197 Error_Msg_NE
7198 ("type& is frozen by declaration??", Obj_Decl, Typ);
7199 Error_Msg_N
7200 ("\an equality operator cannot be declared after "
7201 & "this point??",
7202 Obj_Decl);
7203 else
7204 Error_Msg_NE
7205 ("type& is frozen by declaration (Ada 2012)?y?",
7206 Obj_Decl, Typ);
7207 Error_Msg_N
7208 ("\an equality operator cannot be declared after "
7209 & "this point (Ada 2012)?y?",
7210 Obj_Decl);
7211 end if;
7213 exit;
7214 end if;
7216 Next (Obj_Decl);
7217 end loop;
7218 end if;
7219 end if;
7221 -- Here if type is not frozen yet. It is illegal to have a primitive
7222 -- equality declared in the private part if the type is visible.
7224 elsif not In_Same_List (Parent (Typ), Decl)
7225 and then not Is_Limited_Type (Typ)
7226 then
7227 -- Shouldn't we give an RM reference here???
7229 if Ada_Version >= Ada_2012 then
7230 Error_Msg_N
7231 ("equality operator appears too late<<", Eq_Op);
7232 else
7233 Error_Msg_N
7234 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7235 end if;
7237 -- No error detected
7239 else
7240 return;
7241 end if;
7242 end Check_Untagged_Equality;
7244 -----------------------------
7245 -- Find_Corresponding_Spec --
7246 -----------------------------
7248 function Find_Corresponding_Spec
7249 (N : Node_Id;
7250 Post_Error : Boolean := True) return Entity_Id
7252 Spec : constant Node_Id := Specification (N);
7253 Designator : constant Entity_Id := Defining_Entity (Spec);
7255 E : Entity_Id;
7257 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7258 -- Even if fully conformant, a body may depend on a generic actual when
7259 -- the spec does not, or vice versa, in which case they were distinct
7260 -- entities in the generic.
7262 -------------------------------
7263 -- Different_Generic_Profile --
7264 -------------------------------
7266 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7267 F1, F2 : Entity_Id;
7269 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7270 -- Check that the types of corresponding formals have the same
7271 -- generic actual if any. We have to account for subtypes of a
7272 -- generic formal, declared between a spec and a body, which may
7273 -- appear distinct in an instance but matched in the generic, and
7274 -- the subtype may be used either in the spec or the body of the
7275 -- subprogram being checked.
7277 -------------------------
7278 -- Same_Generic_Actual --
7279 -------------------------
7281 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7283 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7284 -- Predicate to check whether S1 is a subtype of S2 in the source
7285 -- of the instance.
7287 -------------------------
7288 -- Is_Declared_Subtype --
7289 -------------------------
7291 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7292 begin
7293 return Comes_From_Source (Parent (S1))
7294 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7295 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7296 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7297 end Is_Declared_Subtype;
7299 -- Start of processing for Same_Generic_Actual
7301 begin
7302 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7303 or else Is_Declared_Subtype (T1, T2)
7304 or else Is_Declared_Subtype (T2, T1);
7305 end Same_Generic_Actual;
7307 -- Start of processing for Different_Generic_Profile
7309 begin
7310 if not In_Instance then
7311 return False;
7313 elsif Ekind (E) = E_Function
7314 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7315 then
7316 return True;
7317 end if;
7319 F1 := First_Formal (Designator);
7320 F2 := First_Formal (E);
7321 while Present (F1) loop
7322 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7323 return True;
7324 end if;
7326 Next_Formal (F1);
7327 Next_Formal (F2);
7328 end loop;
7330 return False;
7331 end Different_Generic_Profile;
7333 -- Start of processing for Find_Corresponding_Spec
7335 begin
7336 E := Current_Entity (Designator);
7337 while Present (E) loop
7339 -- We are looking for a matching spec. It must have the same scope,
7340 -- and the same name, and either be type conformant, or be the case
7341 -- of a library procedure spec and its body (which belong to one
7342 -- another regardless of whether they are type conformant or not).
7344 if Scope (E) = Current_Scope then
7345 if Current_Scope = Standard_Standard
7346 or else (Ekind (E) = Ekind (Designator)
7347 and then Type_Conformant (E, Designator))
7348 then
7349 -- Within an instantiation, we know that spec and body are
7350 -- subtype conformant, because they were subtype conformant in
7351 -- the generic. We choose the subtype-conformant entity here as
7352 -- well, to resolve spurious ambiguities in the instance that
7353 -- were not present in the generic (i.e. when two different
7354 -- types are given the same actual). If we are looking for a
7355 -- spec to match a body, full conformance is expected.
7357 if In_Instance then
7359 -- Inherit the convention and "ghostness" of the matching
7360 -- spec to ensure proper full and subtype conformance.
7362 Set_Convention (Designator, Convention (E));
7364 if Is_Ghost_Entity (E) then
7365 Set_Is_Ghost_Entity (Designator);
7366 end if;
7368 -- Skip past subprogram bodies and subprogram renamings that
7369 -- may appear to have a matching spec, but that aren't fully
7370 -- conformant with it. That can occur in cases where an
7371 -- actual type causes unrelated homographs in the instance.
7373 if Nkind_In (N, N_Subprogram_Body,
7374 N_Subprogram_Renaming_Declaration)
7375 and then Present (Homonym (E))
7376 and then not Fully_Conformant (Designator, E)
7377 then
7378 goto Next_Entity;
7380 elsif not Subtype_Conformant (Designator, E) then
7381 goto Next_Entity;
7383 elsif Different_Generic_Profile (E) then
7384 goto Next_Entity;
7385 end if;
7386 end if;
7388 -- Ada 2012 (AI05-0165): For internally generated bodies of
7389 -- null procedures locate the internally generated spec. We
7390 -- enforce mode conformance since a tagged type may inherit
7391 -- from interfaces several null primitives which differ only
7392 -- in the mode of the formals.
7394 if not (Comes_From_Source (E))
7395 and then Is_Null_Procedure (E)
7396 and then not Mode_Conformant (Designator, E)
7397 then
7398 null;
7400 -- For null procedures coming from source that are completions,
7401 -- analysis of the generated body will establish the link.
7403 elsif Comes_From_Source (E)
7404 and then Nkind (Spec) = N_Procedure_Specification
7405 and then Null_Present (Spec)
7406 then
7407 return E;
7409 elsif not Has_Completion (E) then
7410 if Nkind (N) /= N_Subprogram_Body_Stub then
7411 Set_Corresponding_Spec (N, E);
7412 end if;
7414 Set_Has_Completion (E);
7415 return E;
7417 elsif Nkind (Parent (N)) = N_Subunit then
7419 -- If this is the proper body of a subunit, the completion
7420 -- flag is set when analyzing the stub.
7422 return E;
7424 -- If E is an internal function with a controlling result that
7425 -- was created for an operation inherited by a null extension,
7426 -- it may be overridden by a body without a previous spec (one
7427 -- more reason why these should be shunned). In that case we
7428 -- remove the generated body if present, because the current
7429 -- one is the explicit overriding.
7431 elsif Ekind (E) = E_Function
7432 and then Ada_Version >= Ada_2005
7433 and then not Comes_From_Source (E)
7434 and then Has_Controlling_Result (E)
7435 and then Is_Null_Extension (Etype (E))
7436 and then Comes_From_Source (Spec)
7437 then
7438 Set_Has_Completion (E, False);
7440 if Expander_Active
7441 and then Nkind (Parent (E)) = N_Function_Specification
7442 then
7443 Remove
7444 (Unit_Declaration_Node
7445 (Corresponding_Body (Unit_Declaration_Node (E))));
7447 return E;
7449 -- If expansion is disabled, or if the wrapper function has
7450 -- not been generated yet, this a late body overriding an
7451 -- inherited operation, or it is an overriding by some other
7452 -- declaration before the controlling result is frozen. In
7453 -- either case this is a declaration of a new entity.
7455 else
7456 return Empty;
7457 end if;
7459 -- If the body already exists, then this is an error unless
7460 -- the previous declaration is the implicit declaration of a
7461 -- derived subprogram. It is also legal for an instance to
7462 -- contain type conformant overloadable declarations (but the
7463 -- generic declaration may not), per 8.3(26/2).
7465 elsif No (Alias (E))
7466 and then not Is_Intrinsic_Subprogram (E)
7467 and then not In_Instance
7468 and then Post_Error
7469 then
7470 Error_Msg_Sloc := Sloc (E);
7472 if Is_Imported (E) then
7473 Error_Msg_NE
7474 ("body not allowed for imported subprogram & declared#",
7475 N, E);
7476 else
7477 Error_Msg_NE ("duplicate body for & declared#", N, E);
7478 end if;
7479 end if;
7481 -- Child units cannot be overloaded, so a conformance mismatch
7482 -- between body and a previous spec is an error.
7484 elsif Is_Child_Unit (E)
7485 and then
7486 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7487 and then
7488 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7489 N_Compilation_Unit
7490 and then Post_Error
7491 then
7492 Error_Msg_N
7493 ("body of child unit does not match previous declaration", N);
7494 end if;
7495 end if;
7497 <<Next_Entity>>
7498 E := Homonym (E);
7499 end loop;
7501 -- On exit, we know that no previous declaration of subprogram exists
7503 return Empty;
7504 end Find_Corresponding_Spec;
7506 ----------------------
7507 -- Fully_Conformant --
7508 ----------------------
7510 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7511 Result : Boolean;
7512 begin
7513 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7514 return Result;
7515 end Fully_Conformant;
7517 ----------------------------------
7518 -- Fully_Conformant_Expressions --
7519 ----------------------------------
7521 function Fully_Conformant_Expressions
7522 (Given_E1 : Node_Id;
7523 Given_E2 : Node_Id) return Boolean
7525 E1 : constant Node_Id := Original_Node (Given_E1);
7526 E2 : constant Node_Id := Original_Node (Given_E2);
7527 -- We always test conformance on original nodes, since it is possible
7528 -- for analysis and/or expansion to make things look as though they
7529 -- conform when they do not, e.g. by converting 1+2 into 3.
7531 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7532 renames Fully_Conformant_Expressions;
7534 function FCL (L1, L2 : List_Id) return Boolean;
7535 -- Compare elements of two lists for conformance. Elements have to be
7536 -- conformant, and actuals inserted as default parameters do not match
7537 -- explicit actuals with the same value.
7539 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7540 -- Compare an operator node with a function call
7542 ---------
7543 -- FCL --
7544 ---------
7546 function FCL (L1, L2 : List_Id) return Boolean is
7547 N1, N2 : Node_Id;
7549 begin
7550 if L1 = No_List then
7551 N1 := Empty;
7552 else
7553 N1 := First (L1);
7554 end if;
7556 if L2 = No_List then
7557 N2 := Empty;
7558 else
7559 N2 := First (L2);
7560 end if;
7562 -- Compare two lists, skipping rewrite insertions (we want to compare
7563 -- the original trees, not the expanded versions).
7565 loop
7566 if Is_Rewrite_Insertion (N1) then
7567 Next (N1);
7568 elsif Is_Rewrite_Insertion (N2) then
7569 Next (N2);
7570 elsif No (N1) then
7571 return No (N2);
7572 elsif No (N2) then
7573 return False;
7574 elsif not FCE (N1, N2) then
7575 return False;
7576 else
7577 Next (N1);
7578 Next (N2);
7579 end if;
7580 end loop;
7581 end FCL;
7583 ---------
7584 -- FCO --
7585 ---------
7587 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7588 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7589 Act : Node_Id;
7591 begin
7592 if No (Actuals)
7593 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7594 then
7595 return False;
7597 else
7598 Act := First (Actuals);
7600 if Nkind (Op_Node) in N_Binary_Op then
7601 if not FCE (Left_Opnd (Op_Node), Act) then
7602 return False;
7603 end if;
7605 Next (Act);
7606 end if;
7608 return Present (Act)
7609 and then FCE (Right_Opnd (Op_Node), Act)
7610 and then No (Next (Act));
7611 end if;
7612 end FCO;
7614 -- Start of processing for Fully_Conformant_Expressions
7616 begin
7617 -- Non-conformant if paren count does not match. Note: if some idiot
7618 -- complains that we don't do this right for more than 3 levels of
7619 -- parentheses, they will be treated with the respect they deserve.
7621 if Paren_Count (E1) /= Paren_Count (E2) then
7622 return False;
7624 -- If same entities are referenced, then they are conformant even if
7625 -- they have different forms (RM 8.3.1(19-20)).
7627 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7628 if Present (Entity (E1)) then
7629 return Entity (E1) = Entity (E2)
7630 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7631 and then Ekind (Entity (E1)) = E_Discriminant
7632 and then Ekind (Entity (E2)) = E_In_Parameter);
7634 elsif Nkind (E1) = N_Expanded_Name
7635 and then Nkind (E2) = N_Expanded_Name
7636 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7637 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7638 then
7639 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7641 else
7642 -- Identifiers in component associations don't always have
7643 -- entities, but their names must conform.
7645 return Nkind (E1) = N_Identifier
7646 and then Nkind (E2) = N_Identifier
7647 and then Chars (E1) = Chars (E2);
7648 end if;
7650 elsif Nkind (E1) = N_Character_Literal
7651 and then Nkind (E2) = N_Expanded_Name
7652 then
7653 return Nkind (Selector_Name (E2)) = N_Character_Literal
7654 and then Chars (E1) = Chars (Selector_Name (E2));
7656 elsif Nkind (E2) = N_Character_Literal
7657 and then Nkind (E1) = N_Expanded_Name
7658 then
7659 return Nkind (Selector_Name (E1)) = N_Character_Literal
7660 and then Chars (E2) = Chars (Selector_Name (E1));
7662 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7663 return FCO (E1, E2);
7665 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7666 return FCO (E2, E1);
7668 -- Otherwise we must have the same syntactic entity
7670 elsif Nkind (E1) /= Nkind (E2) then
7671 return False;
7673 -- At this point, we specialize by node type
7675 else
7676 case Nkind (E1) is
7678 when N_Aggregate =>
7679 return
7680 FCL (Expressions (E1), Expressions (E2))
7681 and then
7682 FCL (Component_Associations (E1),
7683 Component_Associations (E2));
7685 when N_Allocator =>
7686 if Nkind (Expression (E1)) = N_Qualified_Expression
7687 or else
7688 Nkind (Expression (E2)) = N_Qualified_Expression
7689 then
7690 return FCE (Expression (E1), Expression (E2));
7692 -- Check that the subtype marks and any constraints
7693 -- are conformant
7695 else
7696 declare
7697 Indic1 : constant Node_Id := Expression (E1);
7698 Indic2 : constant Node_Id := Expression (E2);
7699 Elt1 : Node_Id;
7700 Elt2 : Node_Id;
7702 begin
7703 if Nkind (Indic1) /= N_Subtype_Indication then
7704 return
7705 Nkind (Indic2) /= N_Subtype_Indication
7706 and then Entity (Indic1) = Entity (Indic2);
7708 elsif Nkind (Indic2) /= N_Subtype_Indication then
7709 return
7710 Nkind (Indic1) /= N_Subtype_Indication
7711 and then Entity (Indic1) = Entity (Indic2);
7713 else
7714 if Entity (Subtype_Mark (Indic1)) /=
7715 Entity (Subtype_Mark (Indic2))
7716 then
7717 return False;
7718 end if;
7720 Elt1 := First (Constraints (Constraint (Indic1)));
7721 Elt2 := First (Constraints (Constraint (Indic2)));
7722 while Present (Elt1) and then Present (Elt2) loop
7723 if not FCE (Elt1, Elt2) then
7724 return False;
7725 end if;
7727 Next (Elt1);
7728 Next (Elt2);
7729 end loop;
7731 return True;
7732 end if;
7733 end;
7734 end if;
7736 when N_Attribute_Reference =>
7737 return
7738 Attribute_Name (E1) = Attribute_Name (E2)
7739 and then FCL (Expressions (E1), Expressions (E2));
7741 when N_Binary_Op =>
7742 return
7743 Entity (E1) = Entity (E2)
7744 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7745 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7747 when N_Short_Circuit | N_Membership_Test =>
7748 return
7749 FCE (Left_Opnd (E1), Left_Opnd (E2))
7750 and then
7751 FCE (Right_Opnd (E1), Right_Opnd (E2));
7753 when N_Case_Expression =>
7754 declare
7755 Alt1 : Node_Id;
7756 Alt2 : Node_Id;
7758 begin
7759 if not FCE (Expression (E1), Expression (E2)) then
7760 return False;
7762 else
7763 Alt1 := First (Alternatives (E1));
7764 Alt2 := First (Alternatives (E2));
7765 loop
7766 if Present (Alt1) /= Present (Alt2) then
7767 return False;
7768 elsif No (Alt1) then
7769 return True;
7770 end if;
7772 if not FCE (Expression (Alt1), Expression (Alt2))
7773 or else not FCL (Discrete_Choices (Alt1),
7774 Discrete_Choices (Alt2))
7775 then
7776 return False;
7777 end if;
7779 Next (Alt1);
7780 Next (Alt2);
7781 end loop;
7782 end if;
7783 end;
7785 when N_Character_Literal =>
7786 return
7787 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7789 when N_Component_Association =>
7790 return
7791 FCL (Choices (E1), Choices (E2))
7792 and then
7793 FCE (Expression (E1), Expression (E2));
7795 when N_Explicit_Dereference =>
7796 return
7797 FCE (Prefix (E1), Prefix (E2));
7799 when N_Extension_Aggregate =>
7800 return
7801 FCL (Expressions (E1), Expressions (E2))
7802 and then Null_Record_Present (E1) =
7803 Null_Record_Present (E2)
7804 and then FCL (Component_Associations (E1),
7805 Component_Associations (E2));
7807 when N_Function_Call =>
7808 return
7809 FCE (Name (E1), Name (E2))
7810 and then
7811 FCL (Parameter_Associations (E1),
7812 Parameter_Associations (E2));
7814 when N_If_Expression =>
7815 return
7816 FCL (Expressions (E1), Expressions (E2));
7818 when N_Indexed_Component =>
7819 return
7820 FCE (Prefix (E1), Prefix (E2))
7821 and then
7822 FCL (Expressions (E1), Expressions (E2));
7824 when N_Integer_Literal =>
7825 return (Intval (E1) = Intval (E2));
7827 when N_Null =>
7828 return True;
7830 when N_Operator_Symbol =>
7831 return
7832 Chars (E1) = Chars (E2);
7834 when N_Others_Choice =>
7835 return True;
7837 when N_Parameter_Association =>
7838 return
7839 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7840 and then FCE (Explicit_Actual_Parameter (E1),
7841 Explicit_Actual_Parameter (E2));
7843 when N_Qualified_Expression =>
7844 return
7845 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7846 and then
7847 FCE (Expression (E1), Expression (E2));
7849 when N_Quantified_Expression =>
7850 if not FCE (Condition (E1), Condition (E2)) then
7851 return False;
7852 end if;
7854 if Present (Loop_Parameter_Specification (E1))
7855 and then Present (Loop_Parameter_Specification (E2))
7856 then
7857 declare
7858 L1 : constant Node_Id :=
7859 Loop_Parameter_Specification (E1);
7860 L2 : constant Node_Id :=
7861 Loop_Parameter_Specification (E2);
7863 begin
7864 return
7865 Reverse_Present (L1) = Reverse_Present (L2)
7866 and then
7867 FCE (Defining_Identifier (L1),
7868 Defining_Identifier (L2))
7869 and then
7870 FCE (Discrete_Subtype_Definition (L1),
7871 Discrete_Subtype_Definition (L2));
7872 end;
7874 elsif Present (Iterator_Specification (E1))
7875 and then Present (Iterator_Specification (E2))
7876 then
7877 declare
7878 I1 : constant Node_Id := Iterator_Specification (E1);
7879 I2 : constant Node_Id := Iterator_Specification (E2);
7881 begin
7882 return
7883 FCE (Defining_Identifier (I1),
7884 Defining_Identifier (I2))
7885 and then
7886 Of_Present (I1) = Of_Present (I2)
7887 and then
7888 Reverse_Present (I1) = Reverse_Present (I2)
7889 and then FCE (Name (I1), Name (I2))
7890 and then FCE (Subtype_Indication (I1),
7891 Subtype_Indication (I2));
7892 end;
7894 -- The quantified expressions used different specifications to
7895 -- walk their respective ranges.
7897 else
7898 return False;
7899 end if;
7901 when N_Range =>
7902 return
7903 FCE (Low_Bound (E1), Low_Bound (E2))
7904 and then
7905 FCE (High_Bound (E1), High_Bound (E2));
7907 when N_Real_Literal =>
7908 return (Realval (E1) = Realval (E2));
7910 when N_Selected_Component =>
7911 return
7912 FCE (Prefix (E1), Prefix (E2))
7913 and then
7914 FCE (Selector_Name (E1), Selector_Name (E2));
7916 when N_Slice =>
7917 return
7918 FCE (Prefix (E1), Prefix (E2))
7919 and then
7920 FCE (Discrete_Range (E1), Discrete_Range (E2));
7922 when N_String_Literal =>
7923 declare
7924 S1 : constant String_Id := Strval (E1);
7925 S2 : constant String_Id := Strval (E2);
7926 L1 : constant Nat := String_Length (S1);
7927 L2 : constant Nat := String_Length (S2);
7929 begin
7930 if L1 /= L2 then
7931 return False;
7933 else
7934 for J in 1 .. L1 loop
7935 if Get_String_Char (S1, J) /=
7936 Get_String_Char (S2, J)
7937 then
7938 return False;
7939 end if;
7940 end loop;
7942 return True;
7943 end if;
7944 end;
7946 when N_Type_Conversion =>
7947 return
7948 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7949 and then
7950 FCE (Expression (E1), Expression (E2));
7952 when N_Unary_Op =>
7953 return
7954 Entity (E1) = Entity (E2)
7955 and then
7956 FCE (Right_Opnd (E1), Right_Opnd (E2));
7958 when N_Unchecked_Type_Conversion =>
7959 return
7960 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7961 and then
7962 FCE (Expression (E1), Expression (E2));
7964 -- All other node types cannot appear in this context. Strictly
7965 -- we should raise a fatal internal error. Instead we just ignore
7966 -- the nodes. This means that if anyone makes a mistake in the
7967 -- expander and mucks an expression tree irretrievably, the result
7968 -- will be a failure to detect a (probably very obscure) case
7969 -- of non-conformance, which is better than bombing on some
7970 -- case where two expressions do in fact conform.
7972 when others =>
7973 return True;
7975 end case;
7976 end if;
7977 end Fully_Conformant_Expressions;
7979 ----------------------------------------
7980 -- Fully_Conformant_Discrete_Subtypes --
7981 ----------------------------------------
7983 function Fully_Conformant_Discrete_Subtypes
7984 (Given_S1 : Node_Id;
7985 Given_S2 : Node_Id) return Boolean
7987 S1 : constant Node_Id := Original_Node (Given_S1);
7988 S2 : constant Node_Id := Original_Node (Given_S2);
7990 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
7991 -- Special-case for a bound given by a discriminant, which in the body
7992 -- is replaced with the discriminal of the enclosing type.
7994 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
7995 -- Check both bounds
7997 -----------------------
7998 -- Conforming_Bounds --
7999 -----------------------
8001 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8002 begin
8003 if Is_Entity_Name (B1)
8004 and then Is_Entity_Name (B2)
8005 and then Ekind (Entity (B1)) = E_Discriminant
8006 then
8007 return Chars (B1) = Chars (B2);
8009 else
8010 return Fully_Conformant_Expressions (B1, B2);
8011 end if;
8012 end Conforming_Bounds;
8014 -----------------------
8015 -- Conforming_Ranges --
8016 -----------------------
8018 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8019 begin
8020 return
8021 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8022 and then
8023 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8024 end Conforming_Ranges;
8026 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8028 begin
8029 if Nkind (S1) /= Nkind (S2) then
8030 return False;
8032 elsif Is_Entity_Name (S1) then
8033 return Entity (S1) = Entity (S2);
8035 elsif Nkind (S1) = N_Range then
8036 return Conforming_Ranges (S1, S2);
8038 elsif Nkind (S1) = N_Subtype_Indication then
8039 return
8040 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8041 and then
8042 Conforming_Ranges
8043 (Range_Expression (Constraint (S1)),
8044 Range_Expression (Constraint (S2)));
8045 else
8046 return True;
8047 end if;
8048 end Fully_Conformant_Discrete_Subtypes;
8050 --------------------
8051 -- Install_Entity --
8052 --------------------
8054 procedure Install_Entity (E : Entity_Id) is
8055 Prev : constant Entity_Id := Current_Entity (E);
8056 begin
8057 Set_Is_Immediately_Visible (E);
8058 Set_Current_Entity (E);
8059 Set_Homonym (E, Prev);
8060 end Install_Entity;
8062 ---------------------
8063 -- Install_Formals --
8064 ---------------------
8066 procedure Install_Formals (Id : Entity_Id) is
8067 F : Entity_Id;
8068 begin
8069 F := First_Formal (Id);
8070 while Present (F) loop
8071 Install_Entity (F);
8072 Next_Formal (F);
8073 end loop;
8074 end Install_Formals;
8076 -----------------------------
8077 -- Is_Interface_Conformant --
8078 -----------------------------
8080 function Is_Interface_Conformant
8081 (Tagged_Type : Entity_Id;
8082 Iface_Prim : Entity_Id;
8083 Prim : Entity_Id) return Boolean
8085 -- The operation may in fact be an inherited (implicit) operation
8086 -- rather than the original interface primitive, so retrieve the
8087 -- ultimate ancestor.
8089 Iface : constant Entity_Id :=
8090 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8091 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8093 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8094 -- Return the controlling formal of Prim
8096 ------------------------
8097 -- Controlling_Formal --
8098 ------------------------
8100 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8101 E : Entity_Id;
8103 begin
8104 E := First_Entity (Prim);
8105 while Present (E) loop
8106 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8107 return E;
8108 end if;
8110 Next_Entity (E);
8111 end loop;
8113 return Empty;
8114 end Controlling_Formal;
8116 -- Local variables
8118 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8119 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8121 -- Start of processing for Is_Interface_Conformant
8123 begin
8124 pragma Assert (Is_Subprogram (Iface_Prim)
8125 and then Is_Subprogram (Prim)
8126 and then Is_Dispatching_Operation (Iface_Prim)
8127 and then Is_Dispatching_Operation (Prim));
8129 pragma Assert (Is_Interface (Iface)
8130 or else (Present (Alias (Iface_Prim))
8131 and then
8132 Is_Interface
8133 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8135 if Prim = Iface_Prim
8136 or else not Is_Subprogram (Prim)
8137 or else Ekind (Prim) /= Ekind (Iface_Prim)
8138 or else not Is_Dispatching_Operation (Prim)
8139 or else Scope (Prim) /= Scope (Tagged_Type)
8140 or else No (Typ)
8141 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8142 or else not Primitive_Names_Match (Iface_Prim, Prim)
8143 then
8144 return False;
8146 -- The mode of the controlling formals must match
8148 elsif Present (Iface_Ctrl_F)
8149 and then Present (Prim_Ctrl_F)
8150 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8151 then
8152 return False;
8154 -- Case of a procedure, or a function whose result type matches the
8155 -- result type of the interface primitive, or a function that has no
8156 -- controlling result (I or access I).
8158 elsif Ekind (Iface_Prim) = E_Procedure
8159 or else Etype (Prim) = Etype (Iface_Prim)
8160 or else not Has_Controlling_Result (Prim)
8161 then
8162 return Type_Conformant
8163 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8165 -- Case of a function returning an interface, or an access to one. Check
8166 -- that the return types correspond.
8168 elsif Implements_Interface (Typ, Iface) then
8169 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8171 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8172 then
8173 return False;
8174 else
8175 return
8176 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8177 Skip_Controlling_Formals => True);
8178 end if;
8180 else
8181 return False;
8182 end if;
8183 end Is_Interface_Conformant;
8185 ---------------------------------
8186 -- Is_Non_Overriding_Operation --
8187 ---------------------------------
8189 function Is_Non_Overriding_Operation
8190 (Prev_E : Entity_Id;
8191 New_E : Entity_Id) return Boolean
8193 Formal : Entity_Id;
8194 F_Typ : Entity_Id;
8195 G_Typ : Entity_Id := Empty;
8197 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8198 -- If F_Type is a derived type associated with a generic actual subtype,
8199 -- then return its Generic_Parent_Type attribute, else return Empty.
8201 function Types_Correspond
8202 (P_Type : Entity_Id;
8203 N_Type : Entity_Id) return Boolean;
8204 -- Returns true if and only if the types (or designated types in the
8205 -- case of anonymous access types) are the same or N_Type is derived
8206 -- directly or indirectly from P_Type.
8208 -----------------------------
8209 -- Get_Generic_Parent_Type --
8210 -----------------------------
8212 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8213 G_Typ : Entity_Id;
8214 Defn : Node_Id;
8215 Indic : Node_Id;
8217 begin
8218 if Is_Derived_Type (F_Typ)
8219 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8220 then
8221 -- The tree must be traversed to determine the parent subtype in
8222 -- the generic unit, which unfortunately isn't always available
8223 -- via semantic attributes. ??? (Note: The use of Original_Node
8224 -- is needed for cases where a full derived type has been
8225 -- rewritten.)
8227 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8228 if Nkind (Defn) = N_Derived_Type_Definition then
8229 Indic := Subtype_Indication (Defn);
8231 if Nkind (Indic) = N_Subtype_Indication then
8232 G_Typ := Entity (Subtype_Mark (Indic));
8233 else
8234 G_Typ := Entity (Indic);
8235 end if;
8237 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8238 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8239 then
8240 return Generic_Parent_Type (Parent (G_Typ));
8241 end if;
8242 end if;
8243 end if;
8245 return Empty;
8246 end Get_Generic_Parent_Type;
8248 ----------------------
8249 -- Types_Correspond --
8250 ----------------------
8252 function Types_Correspond
8253 (P_Type : Entity_Id;
8254 N_Type : Entity_Id) return Boolean
8256 Prev_Type : Entity_Id := Base_Type (P_Type);
8257 New_Type : Entity_Id := Base_Type (N_Type);
8259 begin
8260 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8261 Prev_Type := Designated_Type (Prev_Type);
8262 end if;
8264 if Ekind (New_Type) = E_Anonymous_Access_Type then
8265 New_Type := Designated_Type (New_Type);
8266 end if;
8268 if Prev_Type = New_Type then
8269 return True;
8271 elsif not Is_Class_Wide_Type (New_Type) then
8272 while Etype (New_Type) /= New_Type loop
8273 New_Type := Etype (New_Type);
8274 if New_Type = Prev_Type then
8275 return True;
8276 end if;
8277 end loop;
8278 end if;
8279 return False;
8280 end Types_Correspond;
8282 -- Start of processing for Is_Non_Overriding_Operation
8284 begin
8285 -- In the case where both operations are implicit derived subprograms
8286 -- then neither overrides the other. This can only occur in certain
8287 -- obscure cases (e.g., derivation from homographs created in a generic
8288 -- instantiation).
8290 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8291 return True;
8293 elsif Ekind (Current_Scope) = E_Package
8294 and then Is_Generic_Instance (Current_Scope)
8295 and then In_Private_Part (Current_Scope)
8296 and then Comes_From_Source (New_E)
8297 then
8298 -- We examine the formals and result type of the inherited operation,
8299 -- to determine whether their type is derived from (the instance of)
8300 -- a generic type. The first such formal or result type is the one
8301 -- tested.
8303 Formal := First_Formal (Prev_E);
8304 while Present (Formal) loop
8305 F_Typ := Base_Type (Etype (Formal));
8307 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8308 F_Typ := Designated_Type (F_Typ);
8309 end if;
8311 G_Typ := Get_Generic_Parent_Type (F_Typ);
8312 exit when Present (G_Typ);
8314 Next_Formal (Formal);
8315 end loop;
8317 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8318 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8319 end if;
8321 if No (G_Typ) then
8322 return False;
8323 end if;
8325 -- If the generic type is a private type, then the original operation
8326 -- was not overriding in the generic, because there was no primitive
8327 -- operation to override.
8329 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8330 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8331 N_Formal_Private_Type_Definition
8332 then
8333 return True;
8335 -- The generic parent type is the ancestor of a formal derived
8336 -- type declaration. We need to check whether it has a primitive
8337 -- operation that should be overridden by New_E in the generic.
8339 else
8340 declare
8341 P_Formal : Entity_Id;
8342 N_Formal : Entity_Id;
8343 P_Typ : Entity_Id;
8344 N_Typ : Entity_Id;
8345 P_Prim : Entity_Id;
8346 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8348 begin
8349 while Present (Prim_Elt) loop
8350 P_Prim := Node (Prim_Elt);
8352 if Chars (P_Prim) = Chars (New_E)
8353 and then Ekind (P_Prim) = Ekind (New_E)
8354 then
8355 P_Formal := First_Formal (P_Prim);
8356 N_Formal := First_Formal (New_E);
8357 while Present (P_Formal) and then Present (N_Formal) loop
8358 P_Typ := Etype (P_Formal);
8359 N_Typ := Etype (N_Formal);
8361 if not Types_Correspond (P_Typ, N_Typ) then
8362 exit;
8363 end if;
8365 Next_Entity (P_Formal);
8366 Next_Entity (N_Formal);
8367 end loop;
8369 -- Found a matching primitive operation belonging to the
8370 -- formal ancestor type, so the new subprogram is
8371 -- overriding.
8373 if No (P_Formal)
8374 and then No (N_Formal)
8375 and then (Ekind (New_E) /= E_Function
8376 or else
8377 Types_Correspond
8378 (Etype (P_Prim), Etype (New_E)))
8379 then
8380 return False;
8381 end if;
8382 end if;
8384 Next_Elmt (Prim_Elt);
8385 end loop;
8387 -- If no match found, then the new subprogram does not override
8388 -- in the generic (nor in the instance).
8390 -- If the type in question is not abstract, and the subprogram
8391 -- is, this will be an error if the new operation is in the
8392 -- private part of the instance. Emit a warning now, which will
8393 -- make the subsequent error message easier to understand.
8395 if not Is_Abstract_Type (F_Typ)
8396 and then Is_Abstract_Subprogram (Prev_E)
8397 and then In_Private_Part (Current_Scope)
8398 then
8399 Error_Msg_Node_2 := F_Typ;
8400 Error_Msg_NE
8401 ("private operation& in generic unit does not override "
8402 & "any primitive operation of& (RM 12.3 (18))??",
8403 New_E, New_E);
8404 end if;
8406 return True;
8407 end;
8408 end if;
8409 else
8410 return False;
8411 end if;
8412 end Is_Non_Overriding_Operation;
8414 -------------------------------------
8415 -- List_Inherited_Pre_Post_Aspects --
8416 -------------------------------------
8418 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8419 begin
8420 if Opt.List_Inherited_Aspects
8421 and then Is_Subprogram_Or_Generic_Subprogram (E)
8422 then
8423 declare
8424 Subps : constant Subprogram_List := Inherited_Subprograms (E);
8425 Items : Node_Id;
8426 Prag : Node_Id;
8428 begin
8429 for Index in Subps'Range loop
8430 Items := Contract (Subps (Index));
8432 if Present (Items) then
8433 Prag := Pre_Post_Conditions (Items);
8434 while Present (Prag) loop
8435 Error_Msg_Sloc := Sloc (Prag);
8437 if Class_Present (Prag)
8438 and then not Split_PPC (Prag)
8439 then
8440 if Pragma_Name (Prag) = Name_Precondition then
8441 Error_Msg_N
8442 ("info: & inherits `Pre''Class` aspect from "
8443 & "#?L?", E);
8444 else
8445 Error_Msg_N
8446 ("info: & inherits `Post''Class` aspect from "
8447 & "#?L?", E);
8448 end if;
8449 end if;
8451 Prag := Next_Pragma (Prag);
8452 end loop;
8453 end if;
8454 end loop;
8455 end;
8456 end if;
8457 end List_Inherited_Pre_Post_Aspects;
8459 ------------------------------
8460 -- Make_Inequality_Operator --
8461 ------------------------------
8463 -- S is the defining identifier of an equality operator. We build a
8464 -- subprogram declaration with the right signature. This operation is
8465 -- intrinsic, because it is always expanded as the negation of the
8466 -- call to the equality function.
8468 procedure Make_Inequality_Operator (S : Entity_Id) is
8469 Loc : constant Source_Ptr := Sloc (S);
8470 Decl : Node_Id;
8471 Formals : List_Id;
8472 Op_Name : Entity_Id;
8474 FF : constant Entity_Id := First_Formal (S);
8475 NF : constant Entity_Id := Next_Formal (FF);
8477 begin
8478 -- Check that equality was properly defined, ignore call if not
8480 if No (NF) then
8481 return;
8482 end if;
8484 declare
8485 A : constant Entity_Id :=
8486 Make_Defining_Identifier (Sloc (FF),
8487 Chars => Chars (FF));
8489 B : constant Entity_Id :=
8490 Make_Defining_Identifier (Sloc (NF),
8491 Chars => Chars (NF));
8493 begin
8494 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8496 Formals := New_List (
8497 Make_Parameter_Specification (Loc,
8498 Defining_Identifier => A,
8499 Parameter_Type =>
8500 New_Occurrence_Of (Etype (First_Formal (S)),
8501 Sloc (Etype (First_Formal (S))))),
8503 Make_Parameter_Specification (Loc,
8504 Defining_Identifier => B,
8505 Parameter_Type =>
8506 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8507 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8509 Decl :=
8510 Make_Subprogram_Declaration (Loc,
8511 Specification =>
8512 Make_Function_Specification (Loc,
8513 Defining_Unit_Name => Op_Name,
8514 Parameter_Specifications => Formals,
8515 Result_Definition =>
8516 New_Occurrence_Of (Standard_Boolean, Loc)));
8518 -- Insert inequality right after equality if it is explicit or after
8519 -- the derived type when implicit. These entities are created only
8520 -- for visibility purposes, and eventually replaced in the course
8521 -- of expansion, so they do not need to be attached to the tree and
8522 -- seen by the back-end. Keeping them internal also avoids spurious
8523 -- freezing problems. The declaration is inserted in the tree for
8524 -- analysis, and removed afterwards. If the equality operator comes
8525 -- from an explicit declaration, attach the inequality immediately
8526 -- after. Else the equality is inherited from a derived type
8527 -- declaration, so insert inequality after that declaration.
8529 if No (Alias (S)) then
8530 Insert_After (Unit_Declaration_Node (S), Decl);
8531 elsif Is_List_Member (Parent (S)) then
8532 Insert_After (Parent (S), Decl);
8533 else
8534 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8535 end if;
8537 Mark_Rewrite_Insertion (Decl);
8538 Set_Is_Intrinsic_Subprogram (Op_Name);
8539 Analyze (Decl);
8540 Remove (Decl);
8541 Set_Has_Completion (Op_Name);
8542 Set_Corresponding_Equality (Op_Name, S);
8543 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8544 end;
8545 end Make_Inequality_Operator;
8547 ----------------------
8548 -- May_Need_Actuals --
8549 ----------------------
8551 procedure May_Need_Actuals (Fun : Entity_Id) is
8552 F : Entity_Id;
8553 B : Boolean;
8555 begin
8556 F := First_Formal (Fun);
8557 B := True;
8558 while Present (F) loop
8559 if No (Default_Value (F)) then
8560 B := False;
8561 exit;
8562 end if;
8564 Next_Formal (F);
8565 end loop;
8567 Set_Needs_No_Actuals (Fun, B);
8568 end May_Need_Actuals;
8570 ---------------------
8571 -- Mode_Conformant --
8572 ---------------------
8574 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8575 Result : Boolean;
8576 begin
8577 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8578 return Result;
8579 end Mode_Conformant;
8581 ---------------------------
8582 -- New_Overloaded_Entity --
8583 ---------------------------
8585 procedure New_Overloaded_Entity
8586 (S : Entity_Id;
8587 Derived_Type : Entity_Id := Empty)
8589 Overridden_Subp : Entity_Id := Empty;
8590 -- Set if the current scope has an operation that is type-conformant
8591 -- with S, and becomes hidden by S.
8593 Is_Primitive_Subp : Boolean;
8594 -- Set to True if the new subprogram is primitive
8596 E : Entity_Id;
8597 -- Entity that S overrides
8599 Prev_Vis : Entity_Id := Empty;
8600 -- Predecessor of E in Homonym chain
8602 procedure Check_For_Primitive_Subprogram
8603 (Is_Primitive : out Boolean;
8604 Is_Overriding : Boolean := False);
8605 -- If the subprogram being analyzed is a primitive operation of the type
8606 -- of a formal or result, set the Has_Primitive_Operations flag on the
8607 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8608 -- corresponding flag on the entity itself for later use.
8610 procedure Check_Synchronized_Overriding
8611 (Def_Id : Entity_Id;
8612 Overridden_Subp : out Entity_Id);
8613 -- First determine if Def_Id is an entry or a subprogram either defined
8614 -- in the scope of a task or protected type, or is a primitive of such
8615 -- a type. Check whether Def_Id overrides a subprogram of an interface
8616 -- implemented by the synchronized type, return the overridden entity
8617 -- or Empty.
8619 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8620 -- Check that E is declared in the private part of the current package,
8621 -- or in the package body, where it may hide a previous declaration.
8622 -- We can't use In_Private_Part by itself because this flag is also
8623 -- set when freezing entities, so we must examine the place of the
8624 -- declaration in the tree, and recognize wrapper packages as well.
8626 function Is_Overriding_Alias
8627 (Old_E : Entity_Id;
8628 New_E : Entity_Id) return Boolean;
8629 -- Check whether new subprogram and old subprogram are both inherited
8630 -- from subprograms that have distinct dispatch table entries. This can
8631 -- occur with derivations from instances with accidental homonyms. The
8632 -- function is conservative given that the converse is only true within
8633 -- instances that contain accidental overloadings.
8635 ------------------------------------
8636 -- Check_For_Primitive_Subprogram --
8637 ------------------------------------
8639 procedure Check_For_Primitive_Subprogram
8640 (Is_Primitive : out Boolean;
8641 Is_Overriding : Boolean := False)
8643 Formal : Entity_Id;
8644 F_Typ : Entity_Id;
8645 B_Typ : Entity_Id;
8647 function Visible_Part_Type (T : Entity_Id) return Boolean;
8648 -- Returns true if T is declared in the visible part of the current
8649 -- package scope; otherwise returns false. Assumes that T is declared
8650 -- in a package.
8652 procedure Check_Private_Overriding (T : Entity_Id);
8653 -- Checks that if a primitive abstract subprogram of a visible
8654 -- abstract type is declared in a private part, then it must override
8655 -- an abstract subprogram declared in the visible part. Also checks
8656 -- that if a primitive function with a controlling result is declared
8657 -- in a private part, then it must override a function declared in
8658 -- the visible part.
8660 ------------------------------
8661 -- Check_Private_Overriding --
8662 ------------------------------
8664 procedure Check_Private_Overriding (T : Entity_Id) is
8666 function Overrides_Visible_Function
8667 (Partial_View : Entity_Id) return Boolean;
8668 -- True if S overrides a function in the visible part. The
8669 -- overridden function could be explicitly or implicitly declared.
8671 function Overrides_Visible_Function
8672 (Partial_View : Entity_Id) return Boolean
8674 begin
8675 if not Is_Overriding or else not Has_Homonym (S) then
8676 return False;
8677 end if;
8679 if not Present (Partial_View) then
8680 return True;
8681 end if;
8683 -- Search through all the homonyms H of S in the current
8684 -- package spec, and return True if we find one that matches.
8685 -- Note that Parent (H) will be the declaration of the
8686 -- partial view of T for a match.
8688 declare
8689 H : Entity_Id := S;
8690 begin
8691 loop
8692 H := Homonym (H);
8693 exit when not Present (H) or else Scope (H) /= Scope (S);
8695 if Nkind_In
8696 (Parent (H),
8697 N_Private_Extension_Declaration,
8698 N_Private_Type_Declaration)
8699 and then Defining_Identifier (Parent (H)) = Partial_View
8700 then
8701 return True;
8702 end if;
8703 end loop;
8704 end;
8706 return False;
8707 end Overrides_Visible_Function;
8709 -- Start of processing for Check_Private_Overriding
8711 begin
8712 if Is_Package_Or_Generic_Package (Current_Scope)
8713 and then In_Private_Part (Current_Scope)
8714 and then Visible_Part_Type (T)
8715 and then not In_Instance
8716 then
8717 if Is_Abstract_Type (T)
8718 and then Is_Abstract_Subprogram (S)
8719 and then (not Is_Overriding
8720 or else not Is_Abstract_Subprogram (E))
8721 then
8722 Error_Msg_N ("abstract subprograms must be visible "
8723 & "(RM 3.9.3(10))!", S);
8725 elsif Ekind (S) = E_Function then
8726 declare
8727 Partial_View : constant Entity_Id :=
8728 Incomplete_Or_Partial_View (T);
8730 begin
8731 if not Overrides_Visible_Function (Partial_View) then
8733 -- Here, S is "function ... return T;" declared in
8734 -- the private part, not overriding some visible
8735 -- operation. That's illegal in the tagged case
8736 -- (but not if the private type is untagged).
8738 if ((Present (Partial_View)
8739 and then Is_Tagged_Type (Partial_View))
8740 or else (not Present (Partial_View)
8741 and then Is_Tagged_Type (T)))
8742 and then T = Base_Type (Etype (S))
8743 then
8744 Error_Msg_N
8745 ("private function with tagged result must"
8746 & " override visible-part function", S);
8747 Error_Msg_N
8748 ("\move subprogram to the visible part"
8749 & " (RM 3.9.3(10))", S);
8751 -- AI05-0073: extend this test to the case of a
8752 -- function with a controlling access result.
8754 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8755 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8756 and then
8757 not Is_Class_Wide_Type
8758 (Designated_Type (Etype (S)))
8759 and then Ada_Version >= Ada_2012
8760 then
8761 Error_Msg_N
8762 ("private function with controlling access "
8763 & "result must override visible-part function",
8765 Error_Msg_N
8766 ("\move subprogram to the visible part"
8767 & " (RM 3.9.3(10))", S);
8768 end if;
8769 end if;
8770 end;
8771 end if;
8772 end if;
8773 end Check_Private_Overriding;
8775 -----------------------
8776 -- Visible_Part_Type --
8777 -----------------------
8779 function Visible_Part_Type (T : Entity_Id) return Boolean is
8780 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8781 N : Node_Id;
8783 begin
8784 -- If the entity is a private type, then it must be declared in a
8785 -- visible part.
8787 if Ekind (T) in Private_Kind then
8788 return True;
8789 end if;
8791 -- Otherwise, we traverse the visible part looking for its
8792 -- corresponding declaration. We cannot use the declaration
8793 -- node directly because in the private part the entity of a
8794 -- private type is the one in the full view, which does not
8795 -- indicate that it is the completion of something visible.
8797 N := First (Visible_Declarations (Specification (P)));
8798 while Present (N) loop
8799 if Nkind (N) = N_Full_Type_Declaration
8800 and then Present (Defining_Identifier (N))
8801 and then T = Defining_Identifier (N)
8802 then
8803 return True;
8805 elsif Nkind_In (N, N_Private_Type_Declaration,
8806 N_Private_Extension_Declaration)
8807 and then Present (Defining_Identifier (N))
8808 and then T = Full_View (Defining_Identifier (N))
8809 then
8810 return True;
8811 end if;
8813 Next (N);
8814 end loop;
8816 return False;
8817 end Visible_Part_Type;
8819 -- Start of processing for Check_For_Primitive_Subprogram
8821 begin
8822 Is_Primitive := False;
8824 if not Comes_From_Source (S) then
8825 null;
8827 -- If subprogram is at library level, it is not primitive operation
8829 elsif Current_Scope = Standard_Standard then
8830 null;
8832 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8833 and then not In_Package_Body (Current_Scope))
8834 or else Is_Overriding
8835 then
8836 -- For function, check return type
8838 if Ekind (S) = E_Function then
8839 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8840 F_Typ := Designated_Type (Etype (S));
8841 else
8842 F_Typ := Etype (S);
8843 end if;
8845 B_Typ := Base_Type (F_Typ);
8847 if Scope (B_Typ) = Current_Scope
8848 and then not Is_Class_Wide_Type (B_Typ)
8849 and then not Is_Generic_Type (B_Typ)
8850 then
8851 Is_Primitive := True;
8852 Set_Has_Primitive_Operations (B_Typ);
8853 Set_Is_Primitive (S);
8854 Check_Private_Overriding (B_Typ);
8855 end if;
8856 end if;
8858 -- For all subprograms, check formals
8860 Formal := First_Formal (S);
8861 while Present (Formal) loop
8862 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8863 F_Typ := Designated_Type (Etype (Formal));
8864 else
8865 F_Typ := Etype (Formal);
8866 end if;
8868 B_Typ := Base_Type (F_Typ);
8870 if Ekind (B_Typ) = E_Access_Subtype then
8871 B_Typ := Base_Type (B_Typ);
8872 end if;
8874 if Scope (B_Typ) = Current_Scope
8875 and then not Is_Class_Wide_Type (B_Typ)
8876 and then not Is_Generic_Type (B_Typ)
8877 then
8878 Is_Primitive := True;
8879 Set_Is_Primitive (S);
8880 Set_Has_Primitive_Operations (B_Typ);
8881 Check_Private_Overriding (B_Typ);
8882 end if;
8884 Next_Formal (Formal);
8885 end loop;
8887 -- Special case: An equality function can be redefined for a type
8888 -- occurring in a declarative part, and won't otherwise be treated as
8889 -- a primitive because it doesn't occur in a package spec and doesn't
8890 -- override an inherited subprogram. It's important that we mark it
8891 -- primitive so it can be returned by Collect_Primitive_Operations
8892 -- and be used in composing the equality operation of later types
8893 -- that have a component of the type.
8895 elsif Chars (S) = Name_Op_Eq
8896 and then Etype (S) = Standard_Boolean
8897 then
8898 B_Typ := Base_Type (Etype (First_Formal (S)));
8900 if Scope (B_Typ) = Current_Scope
8901 and then
8902 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8903 and then not Is_Limited_Type (B_Typ)
8904 then
8905 Is_Primitive := True;
8906 Set_Is_Primitive (S);
8907 Set_Has_Primitive_Operations (B_Typ);
8908 Check_Private_Overriding (B_Typ);
8909 end if;
8910 end if;
8911 end Check_For_Primitive_Subprogram;
8913 -----------------------------------
8914 -- Check_Synchronized_Overriding --
8915 -----------------------------------
8917 procedure Check_Synchronized_Overriding
8918 (Def_Id : Entity_Id;
8919 Overridden_Subp : out Entity_Id)
8921 Ifaces_List : Elist_Id;
8922 In_Scope : Boolean;
8923 Typ : Entity_Id;
8925 function Matches_Prefixed_View_Profile
8926 (Prim_Params : List_Id;
8927 Iface_Params : List_Id) return Boolean;
8928 -- Determine whether a subprogram's parameter profile Prim_Params
8929 -- matches that of a potentially overridden interface subprogram
8930 -- Iface_Params. Also determine if the type of first parameter of
8931 -- Iface_Params is an implemented interface.
8933 -----------------------------------
8934 -- Matches_Prefixed_View_Profile --
8935 -----------------------------------
8937 function Matches_Prefixed_View_Profile
8938 (Prim_Params : List_Id;
8939 Iface_Params : List_Id) return Boolean
8941 Iface_Id : Entity_Id;
8942 Iface_Param : Node_Id;
8943 Iface_Typ : Entity_Id;
8944 Prim_Id : Entity_Id;
8945 Prim_Param : Node_Id;
8946 Prim_Typ : Entity_Id;
8948 function Is_Implemented
8949 (Ifaces_List : Elist_Id;
8950 Iface : Entity_Id) return Boolean;
8951 -- Determine if Iface is implemented by the current task or
8952 -- protected type.
8954 --------------------
8955 -- Is_Implemented --
8956 --------------------
8958 function Is_Implemented
8959 (Ifaces_List : Elist_Id;
8960 Iface : Entity_Id) return Boolean
8962 Iface_Elmt : Elmt_Id;
8964 begin
8965 Iface_Elmt := First_Elmt (Ifaces_List);
8966 while Present (Iface_Elmt) loop
8967 if Node (Iface_Elmt) = Iface then
8968 return True;
8969 end if;
8971 Next_Elmt (Iface_Elmt);
8972 end loop;
8974 return False;
8975 end Is_Implemented;
8977 -- Start of processing for Matches_Prefixed_View_Profile
8979 begin
8980 Iface_Param := First (Iface_Params);
8981 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8983 if Is_Access_Type (Iface_Typ) then
8984 Iface_Typ := Designated_Type (Iface_Typ);
8985 end if;
8987 Prim_Param := First (Prim_Params);
8989 -- The first parameter of the potentially overridden subprogram
8990 -- must be an interface implemented by Prim.
8992 if not Is_Interface (Iface_Typ)
8993 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8994 then
8995 return False;
8996 end if;
8998 -- The checks on the object parameters are done, move onto the
8999 -- rest of the parameters.
9001 if not In_Scope then
9002 Prim_Param := Next (Prim_Param);
9003 end if;
9005 Iface_Param := Next (Iface_Param);
9006 while Present (Iface_Param) and then Present (Prim_Param) loop
9007 Iface_Id := Defining_Identifier (Iface_Param);
9008 Iface_Typ := Find_Parameter_Type (Iface_Param);
9010 Prim_Id := Defining_Identifier (Prim_Param);
9011 Prim_Typ := Find_Parameter_Type (Prim_Param);
9013 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9014 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9015 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9016 then
9017 Iface_Typ := Designated_Type (Iface_Typ);
9018 Prim_Typ := Designated_Type (Prim_Typ);
9019 end if;
9021 -- Case of multiple interface types inside a parameter profile
9023 -- (Obj_Param : in out Iface; ...; Param : Iface)
9025 -- If the interface type is implemented, then the matching type
9026 -- in the primitive should be the implementing record type.
9028 if Ekind (Iface_Typ) = E_Record_Type
9029 and then Is_Interface (Iface_Typ)
9030 and then Is_Implemented (Ifaces_List, Iface_Typ)
9031 then
9032 if Prim_Typ /= Typ then
9033 return False;
9034 end if;
9036 -- The two parameters must be both mode and subtype conformant
9038 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9039 or else not
9040 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9041 then
9042 return False;
9043 end if;
9045 Next (Iface_Param);
9046 Next (Prim_Param);
9047 end loop;
9049 -- One of the two lists contains more parameters than the other
9051 if Present (Iface_Param) or else Present (Prim_Param) then
9052 return False;
9053 end if;
9055 return True;
9056 end Matches_Prefixed_View_Profile;
9058 -- Start of processing for Check_Synchronized_Overriding
9060 begin
9061 Overridden_Subp := Empty;
9063 -- Def_Id must be an entry or a subprogram. We should skip predefined
9064 -- primitives internally generated by the frontend; however at this
9065 -- stage predefined primitives are still not fully decorated. As a
9066 -- minor optimization we skip here internally generated subprograms.
9068 if (Ekind (Def_Id) /= E_Entry
9069 and then Ekind (Def_Id) /= E_Function
9070 and then Ekind (Def_Id) /= E_Procedure)
9071 or else not Comes_From_Source (Def_Id)
9072 then
9073 return;
9074 end if;
9076 -- Search for the concurrent declaration since it contains the list
9077 -- of all implemented interfaces. In this case, the subprogram is
9078 -- declared within the scope of a protected or a task type.
9080 if Present (Scope (Def_Id))
9081 and then Is_Concurrent_Type (Scope (Def_Id))
9082 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9083 then
9084 Typ := Scope (Def_Id);
9085 In_Scope := True;
9087 -- The enclosing scope is not a synchronized type and the subprogram
9088 -- has no formals.
9090 elsif No (First_Formal (Def_Id)) then
9091 return;
9093 -- The subprogram has formals and hence it may be a primitive of a
9094 -- concurrent type.
9096 else
9097 Typ := Etype (First_Formal (Def_Id));
9099 if Is_Access_Type (Typ) then
9100 Typ := Directly_Designated_Type (Typ);
9101 end if;
9103 if Is_Concurrent_Type (Typ)
9104 and then not Is_Generic_Actual_Type (Typ)
9105 then
9106 In_Scope := False;
9108 -- This case occurs when the concurrent type is declared within
9109 -- a generic unit. As a result the corresponding record has been
9110 -- built and used as the type of the first formal, we just have
9111 -- to retrieve the corresponding concurrent type.
9113 elsif Is_Concurrent_Record_Type (Typ)
9114 and then not Is_Class_Wide_Type (Typ)
9115 and then Present (Corresponding_Concurrent_Type (Typ))
9116 then
9117 Typ := Corresponding_Concurrent_Type (Typ);
9118 In_Scope := False;
9120 else
9121 return;
9122 end if;
9123 end if;
9125 -- There is no overriding to check if is an inherited operation in a
9126 -- type derivation on for a generic actual.
9128 Collect_Interfaces (Typ, Ifaces_List);
9130 if Is_Empty_Elmt_List (Ifaces_List) then
9131 return;
9132 end if;
9134 -- Determine whether entry or subprogram Def_Id overrides a primitive
9135 -- operation that belongs to one of the interfaces in Ifaces_List.
9137 declare
9138 Candidate : Entity_Id := Empty;
9139 Hom : Entity_Id := Empty;
9140 Subp : Entity_Id := Empty;
9142 begin
9143 -- Traverse the homonym chain, looking for a potentially
9144 -- overridden subprogram that belongs to an implemented
9145 -- interface.
9147 Hom := Current_Entity_In_Scope (Def_Id);
9148 while Present (Hom) loop
9149 Subp := Hom;
9151 if Subp = Def_Id
9152 or else not Is_Overloadable (Subp)
9153 or else not Is_Primitive (Subp)
9154 or else not Is_Dispatching_Operation (Subp)
9155 or else not Present (Find_Dispatching_Type (Subp))
9156 or else not Is_Interface (Find_Dispatching_Type (Subp))
9157 then
9158 null;
9160 -- Entries and procedures can override abstract or null
9161 -- interface procedures.
9163 elsif (Ekind (Def_Id) = E_Procedure
9164 or else Ekind (Def_Id) = E_Entry)
9165 and then Ekind (Subp) = E_Procedure
9166 and then Matches_Prefixed_View_Profile
9167 (Parameter_Specifications (Parent (Def_Id)),
9168 Parameter_Specifications (Parent (Subp)))
9169 then
9170 Candidate := Subp;
9172 -- For an overridden subprogram Subp, check whether the mode
9173 -- of its first parameter is correct depending on the kind
9174 -- of synchronized type.
9176 declare
9177 Formal : constant Node_Id := First_Formal (Candidate);
9179 begin
9180 -- In order for an entry or a protected procedure to
9181 -- override, the first parameter of the overridden
9182 -- routine must be of mode "out", "in out" or
9183 -- access-to-variable.
9185 if Ekind_In (Candidate, E_Entry, E_Procedure)
9186 and then Is_Protected_Type (Typ)
9187 and then Ekind (Formal) /= E_In_Out_Parameter
9188 and then Ekind (Formal) /= E_Out_Parameter
9189 and then Nkind (Parameter_Type (Parent (Formal))) /=
9190 N_Access_Definition
9191 then
9192 null;
9194 -- All other cases are OK since a task entry or routine
9195 -- does not have a restriction on the mode of the first
9196 -- parameter of the overridden interface routine.
9198 else
9199 Overridden_Subp := Candidate;
9200 return;
9201 end if;
9202 end;
9204 -- Functions can override abstract interface functions
9206 elsif Ekind (Def_Id) = E_Function
9207 and then Ekind (Subp) = E_Function
9208 and then Matches_Prefixed_View_Profile
9209 (Parameter_Specifications (Parent (Def_Id)),
9210 Parameter_Specifications (Parent (Subp)))
9211 and then Etype (Result_Definition (Parent (Def_Id))) =
9212 Etype (Result_Definition (Parent (Subp)))
9213 then
9214 Candidate := Subp;
9216 -- If an inherited subprogram is implemented by a protected
9217 -- function, then the first parameter of the inherited
9218 -- subprogram shall be of mode in, but not an
9219 -- access-to-variable parameter (RM 9.4(11/9)
9221 if Present (First_Formal (Subp))
9222 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9223 and then
9224 (not Is_Access_Type (Etype (First_Formal (Subp)))
9225 or else
9226 Is_Access_Constant (Etype (First_Formal (Subp))))
9227 then
9228 Overridden_Subp := Subp;
9229 return;
9230 end if;
9231 end if;
9233 Hom := Homonym (Hom);
9234 end loop;
9236 -- After examining all candidates for overriding, we are left with
9237 -- the best match which is a mode incompatible interface routine.
9239 if In_Scope and then Present (Candidate) then
9240 Error_Msg_PT (Def_Id, Candidate);
9241 end if;
9243 Overridden_Subp := Candidate;
9244 return;
9245 end;
9246 end Check_Synchronized_Overriding;
9248 ----------------------------
9249 -- Is_Private_Declaration --
9250 ----------------------------
9252 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9253 Priv_Decls : List_Id;
9254 Decl : constant Node_Id := Unit_Declaration_Node (E);
9256 begin
9257 if Is_Package_Or_Generic_Package (Current_Scope)
9258 and then In_Private_Part (Current_Scope)
9259 then
9260 Priv_Decls :=
9261 Private_Declarations (Package_Specification (Current_Scope));
9263 return In_Package_Body (Current_Scope)
9264 or else
9265 (Is_List_Member (Decl)
9266 and then List_Containing (Decl) = Priv_Decls)
9267 or else (Nkind (Parent (Decl)) = N_Package_Specification
9268 and then not
9269 Is_Compilation_Unit
9270 (Defining_Entity (Parent (Decl)))
9271 and then List_Containing (Parent (Parent (Decl))) =
9272 Priv_Decls);
9273 else
9274 return False;
9275 end if;
9276 end Is_Private_Declaration;
9278 --------------------------
9279 -- Is_Overriding_Alias --
9280 --------------------------
9282 function Is_Overriding_Alias
9283 (Old_E : Entity_Id;
9284 New_E : Entity_Id) return Boolean
9286 AO : constant Entity_Id := Alias (Old_E);
9287 AN : constant Entity_Id := Alias (New_E);
9288 begin
9289 return Scope (AO) /= Scope (AN)
9290 or else No (DTC_Entity (AO))
9291 or else No (DTC_Entity (AN))
9292 or else DT_Position (AO) = DT_Position (AN);
9293 end Is_Overriding_Alias;
9295 -- Start of processing for New_Overloaded_Entity
9297 begin
9298 -- We need to look for an entity that S may override. This must be a
9299 -- homonym in the current scope, so we look for the first homonym of
9300 -- S in the current scope as the starting point for the search.
9302 E := Current_Entity_In_Scope (S);
9304 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9305 -- They are directly added to the list of primitive operations of
9306 -- Derived_Type, unless this is a rederivation in the private part
9307 -- of an operation that was already derived in the visible part of
9308 -- the current package.
9310 if Ada_Version >= Ada_2005
9311 and then Present (Derived_Type)
9312 and then Present (Alias (S))
9313 and then Is_Dispatching_Operation (Alias (S))
9314 and then Present (Find_Dispatching_Type (Alias (S)))
9315 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9316 then
9317 -- For private types, when the full-view is processed we propagate to
9318 -- the full view the non-overridden entities whose attribute "alias"
9319 -- references an interface primitive. These entities were added by
9320 -- Derive_Subprograms to ensure that interface primitives are
9321 -- covered.
9323 -- Inside_Freeze_Actions is non zero when S corresponds with an
9324 -- internal entity that links an interface primitive with its
9325 -- covering primitive through attribute Interface_Alias (see
9326 -- Add_Internal_Interface_Entities).
9328 if Inside_Freezing_Actions = 0
9329 and then Is_Package_Or_Generic_Package (Current_Scope)
9330 and then In_Private_Part (Current_Scope)
9331 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9332 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9333 and then Full_View (Defining_Identifier (Parent (E)))
9334 = Defining_Identifier (Parent (S))
9335 and then Alias (E) = Alias (S)
9336 then
9337 Check_Operation_From_Private_View (S, E);
9338 Set_Is_Dispatching_Operation (S);
9340 -- Common case
9342 else
9343 Enter_Overloaded_Entity (S);
9344 Check_Dispatching_Operation (S, Empty);
9345 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9346 end if;
9348 return;
9349 end if;
9351 -- If there is no homonym then this is definitely not overriding
9353 if No (E) then
9354 Enter_Overloaded_Entity (S);
9355 Check_Dispatching_Operation (S, Empty);
9356 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9358 -- If subprogram has an explicit declaration, check whether it has an
9359 -- overriding indicator.
9361 if Comes_From_Source (S) then
9362 Check_Synchronized_Overriding (S, Overridden_Subp);
9364 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9365 -- it may have overridden some hidden inherited primitive. Update
9366 -- Overridden_Subp to avoid spurious errors when checking the
9367 -- overriding indicator.
9369 if Ada_Version >= Ada_2012
9370 and then No (Overridden_Subp)
9371 and then Is_Dispatching_Operation (S)
9372 and then Present (Overridden_Operation (S))
9373 then
9374 Overridden_Subp := Overridden_Operation (S);
9375 end if;
9377 Check_Overriding_Indicator
9378 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9379 end if;
9381 -- If there is a homonym that is not overloadable, then we have an
9382 -- error, except for the special cases checked explicitly below.
9384 elsif not Is_Overloadable (E) then
9386 -- Check for spurious conflict produced by a subprogram that has the
9387 -- same name as that of the enclosing generic package. The conflict
9388 -- occurs within an instance, between the subprogram and the renaming
9389 -- declaration for the package. After the subprogram, the package
9390 -- renaming declaration becomes hidden.
9392 if Ekind (E) = E_Package
9393 and then Present (Renamed_Object (E))
9394 and then Renamed_Object (E) = Current_Scope
9395 and then Nkind (Parent (Renamed_Object (E))) =
9396 N_Package_Specification
9397 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9398 then
9399 Set_Is_Hidden (E);
9400 Set_Is_Immediately_Visible (E, False);
9401 Enter_Overloaded_Entity (S);
9402 Set_Homonym (S, Homonym (E));
9403 Check_Dispatching_Operation (S, Empty);
9404 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9406 -- If the subprogram is implicit it is hidden by the previous
9407 -- declaration. However if it is dispatching, it must appear in the
9408 -- dispatch table anyway, because it can be dispatched to even if it
9409 -- cannot be called directly.
9411 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9412 Set_Scope (S, Current_Scope);
9414 if Is_Dispatching_Operation (Alias (S)) then
9415 Check_Dispatching_Operation (S, Empty);
9416 end if;
9418 return;
9420 else
9421 Error_Msg_Sloc := Sloc (E);
9423 -- Generate message, with useful additional warning if in generic
9425 if Is_Generic_Unit (E) then
9426 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9427 Error_Msg_N ("\& conflicts with declaration#", S);
9428 else
9429 Error_Msg_N ("& conflicts with declaration#", S);
9430 end if;
9432 return;
9433 end if;
9435 -- E exists and is overloadable
9437 else
9438 Check_Synchronized_Overriding (S, Overridden_Subp);
9440 -- Loop through E and its homonyms to determine if any of them is
9441 -- the candidate for overriding by S.
9443 while Present (E) loop
9445 -- Definitely not interesting if not in the current scope
9447 if Scope (E) /= Current_Scope then
9448 null;
9450 -- A function can overload the name of an abstract state. The
9451 -- state can be viewed as a function with a profile that cannot
9452 -- be matched by anything.
9454 elsif Ekind (S) = E_Function
9455 and then Ekind (E) = E_Abstract_State
9456 then
9457 Enter_Overloaded_Entity (S);
9458 return;
9460 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9461 -- procedures locate the internally generated spec. We enforce
9462 -- mode conformance since a tagged type may inherit from
9463 -- interfaces several null primitives which differ only in
9464 -- the mode of the formals.
9466 elsif not Comes_From_Source (S)
9467 and then Is_Null_Procedure (S)
9468 and then not Mode_Conformant (E, S)
9469 then
9470 null;
9472 -- Check if we have type conformance
9474 elsif Type_Conformant (E, S) then
9476 -- If the old and new entities have the same profile and one
9477 -- is not the body of the other, then this is an error, unless
9478 -- one of them is implicitly declared.
9480 -- There are some cases when both can be implicit, for example
9481 -- when both a literal and a function that overrides it are
9482 -- inherited in a derivation, or when an inherited operation
9483 -- of a tagged full type overrides the inherited operation of
9484 -- a private extension. Ada 83 had a special rule for the
9485 -- literal case. In Ada 95, the later implicit operation hides
9486 -- the former, and the literal is always the former. In the
9487 -- odd case where both are derived operations declared at the
9488 -- same point, both operations should be declared, and in that
9489 -- case we bypass the following test and proceed to the next
9490 -- part. This can only occur for certain obscure cases in
9491 -- instances, when an operation on a type derived from a formal
9492 -- private type does not override a homograph inherited from
9493 -- the actual. In subsequent derivations of such a type, the
9494 -- DT positions of these operations remain distinct, if they
9495 -- have been set.
9497 if Present (Alias (S))
9498 and then (No (Alias (E))
9499 or else Comes_From_Source (E)
9500 or else Is_Abstract_Subprogram (S)
9501 or else
9502 (Is_Dispatching_Operation (E)
9503 and then Is_Overriding_Alias (E, S)))
9504 and then Ekind (E) /= E_Enumeration_Literal
9505 then
9506 -- When an derived operation is overloaded it may be due to
9507 -- the fact that the full view of a private extension
9508 -- re-inherits. It has to be dealt with.
9510 if Is_Package_Or_Generic_Package (Current_Scope)
9511 and then In_Private_Part (Current_Scope)
9512 then
9513 Check_Operation_From_Private_View (S, E);
9514 end if;
9516 -- In any case the implicit operation remains hidden by the
9517 -- existing declaration, which is overriding. Indicate that
9518 -- E overrides the operation from which S is inherited.
9520 if Present (Alias (S)) then
9521 Set_Overridden_Operation (E, Alias (S));
9522 Inherit_Subprogram_Contract (E, Alias (S));
9524 else
9525 Set_Overridden_Operation (E, S);
9526 Inherit_Subprogram_Contract (E, S);
9527 end if;
9529 if Comes_From_Source (E) then
9530 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9531 end if;
9533 return;
9535 -- Within an instance, the renaming declarations for actual
9536 -- subprograms may become ambiguous, but they do not hide each
9537 -- other.
9539 elsif Ekind (E) /= E_Entry
9540 and then not Comes_From_Source (E)
9541 and then not Is_Generic_Instance (E)
9542 and then (Present (Alias (E))
9543 or else Is_Intrinsic_Subprogram (E))
9544 and then (not In_Instance
9545 or else No (Parent (E))
9546 or else Nkind (Unit_Declaration_Node (E)) /=
9547 N_Subprogram_Renaming_Declaration)
9548 then
9549 -- A subprogram child unit is not allowed to override an
9550 -- inherited subprogram (10.1.1(20)).
9552 if Is_Child_Unit (S) then
9553 Error_Msg_N
9554 ("child unit overrides inherited subprogram in parent",
9556 return;
9557 end if;
9559 if Is_Non_Overriding_Operation (E, S) then
9560 Enter_Overloaded_Entity (S);
9562 if No (Derived_Type)
9563 or else Is_Tagged_Type (Derived_Type)
9564 then
9565 Check_Dispatching_Operation (S, Empty);
9566 end if;
9568 return;
9569 end if;
9571 -- E is a derived operation or an internal operator which
9572 -- is being overridden. Remove E from further visibility.
9573 -- Furthermore, if E is a dispatching operation, it must be
9574 -- replaced in the list of primitive operations of its type
9575 -- (see Override_Dispatching_Operation).
9577 Overridden_Subp := E;
9579 declare
9580 Prev : Entity_Id;
9582 begin
9583 Prev := First_Entity (Current_Scope);
9584 while Present (Prev) and then Next_Entity (Prev) /= E loop
9585 Next_Entity (Prev);
9586 end loop;
9588 -- It is possible for E to be in the current scope and
9589 -- yet not in the entity chain. This can only occur in a
9590 -- generic context where E is an implicit concatenation
9591 -- in the formal part, because in a generic body the
9592 -- entity chain starts with the formals.
9594 -- In GNATprove mode, a wrapper for an operation with
9595 -- axiomatization may be a homonym of another declaration
9596 -- for an actual subprogram (needs refinement ???).
9598 if No (Prev) then
9599 if In_Instance
9600 and then GNATprove_Mode
9601 and then
9602 Nkind (Original_Node (Unit_Declaration_Node (S))) =
9603 N_Subprogram_Renaming_Declaration
9604 then
9605 return;
9606 else
9607 pragma Assert (Chars (E) = Name_Op_Concat);
9608 null;
9609 end if;
9610 end if;
9612 -- E must be removed both from the entity_list of the
9613 -- current scope, and from the visibility chain.
9615 if Debug_Flag_E then
9616 Write_Str ("Override implicit operation ");
9617 Write_Int (Int (E));
9618 Write_Eol;
9619 end if;
9621 -- If E is a predefined concatenation, it stands for four
9622 -- different operations. As a result, a single explicit
9623 -- declaration does not hide it. In a possible ambiguous
9624 -- situation, Disambiguate chooses the user-defined op,
9625 -- so it is correct to retain the previous internal one.
9627 if Chars (E) /= Name_Op_Concat
9628 or else Ekind (E) /= E_Operator
9629 then
9630 -- For nondispatching derived operations that are
9631 -- overridden by a subprogram declared in the private
9632 -- part of a package, we retain the derived subprogram
9633 -- but mark it as not immediately visible. If the
9634 -- derived operation was declared in the visible part
9635 -- then this ensures that it will still be visible
9636 -- outside the package with the proper signature
9637 -- (calls from outside must also be directed to this
9638 -- version rather than the overriding one, unlike the
9639 -- dispatching case). Calls from inside the package
9640 -- will still resolve to the overriding subprogram
9641 -- since the derived one is marked as not visible
9642 -- within the package.
9644 -- If the private operation is dispatching, we achieve
9645 -- the overriding by keeping the implicit operation
9646 -- but setting its alias to be the overriding one. In
9647 -- this fashion the proper body is executed in all
9648 -- cases, but the original signature is used outside
9649 -- of the package.
9651 -- If the overriding is not in the private part, we
9652 -- remove the implicit operation altogether.
9654 if Is_Private_Declaration (S) then
9655 if not Is_Dispatching_Operation (E) then
9656 Set_Is_Immediately_Visible (E, False);
9657 else
9658 -- Work done in Override_Dispatching_Operation,
9659 -- so nothing else needs to be done here.
9661 null;
9662 end if;
9664 else
9665 -- Find predecessor of E in Homonym chain
9667 if E = Current_Entity (E) then
9668 Prev_Vis := Empty;
9669 else
9670 Prev_Vis := Current_Entity (E);
9671 while Homonym (Prev_Vis) /= E loop
9672 Prev_Vis := Homonym (Prev_Vis);
9673 end loop;
9674 end if;
9676 if Prev_Vis /= Empty then
9678 -- Skip E in the visibility chain
9680 Set_Homonym (Prev_Vis, Homonym (E));
9682 else
9683 Set_Name_Entity_Id (Chars (E), Homonym (E));
9684 end if;
9686 Set_Next_Entity (Prev, Next_Entity (E));
9688 if No (Next_Entity (Prev)) then
9689 Set_Last_Entity (Current_Scope, Prev);
9690 end if;
9691 end if;
9692 end if;
9694 Enter_Overloaded_Entity (S);
9696 -- For entities generated by Derive_Subprograms the
9697 -- overridden operation is the inherited primitive
9698 -- (which is available through the attribute alias).
9700 if not (Comes_From_Source (E))
9701 and then Is_Dispatching_Operation (E)
9702 and then Find_Dispatching_Type (E) =
9703 Find_Dispatching_Type (S)
9704 and then Present (Alias (E))
9705 and then Comes_From_Source (Alias (E))
9706 then
9707 Set_Overridden_Operation (S, Alias (E));
9708 Inherit_Subprogram_Contract (S, Alias (E));
9710 -- Normal case of setting entity as overridden
9712 -- Note: Static_Initialization and Overridden_Operation
9713 -- attributes use the same field in subprogram entities.
9714 -- Static_Initialization is only defined for internal
9715 -- initialization procedures, where Overridden_Operation
9716 -- is irrelevant. Therefore the setting of this attribute
9717 -- must check whether the target is an init_proc.
9719 elsif not Is_Init_Proc (S) then
9720 Set_Overridden_Operation (S, E);
9721 Inherit_Subprogram_Contract (S, E);
9722 end if;
9724 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9726 -- If S is a user-defined subprogram or a null procedure
9727 -- expanded to override an inherited null procedure, or a
9728 -- predefined dispatching primitive then indicate that E
9729 -- overrides the operation from which S is inherited.
9731 if Comes_From_Source (S)
9732 or else
9733 (Present (Parent (S))
9734 and then
9735 Nkind (Parent (S)) = N_Procedure_Specification
9736 and then
9737 Null_Present (Parent (S)))
9738 or else
9739 (Present (Alias (E))
9740 and then
9741 Is_Predefined_Dispatching_Operation (Alias (E)))
9742 then
9743 if Present (Alias (E)) then
9744 Set_Overridden_Operation (S, Alias (E));
9745 Inherit_Subprogram_Contract (S, Alias (E));
9746 end if;
9747 end if;
9749 if Is_Dispatching_Operation (E) then
9751 -- An overriding dispatching subprogram inherits the
9752 -- convention of the overridden subprogram (AI-117).
9754 Set_Convention (S, Convention (E));
9755 Check_Dispatching_Operation (S, E);
9757 else
9758 Check_Dispatching_Operation (S, Empty);
9759 end if;
9761 Check_For_Primitive_Subprogram
9762 (Is_Primitive_Subp, Is_Overriding => True);
9763 goto Check_Inequality;
9764 end;
9766 -- Apparent redeclarations in instances can occur when two
9767 -- formal types get the same actual type. The subprograms in
9768 -- in the instance are legal, even if not callable from the
9769 -- outside. Calls from within are disambiguated elsewhere.
9770 -- For dispatching operations in the visible part, the usual
9771 -- rules apply, and operations with the same profile are not
9772 -- legal (B830001).
9774 elsif (In_Instance_Visible_Part
9775 and then not Is_Dispatching_Operation (E))
9776 or else In_Instance_Not_Visible
9777 then
9778 null;
9780 -- Here we have a real error (identical profile)
9782 else
9783 Error_Msg_Sloc := Sloc (E);
9785 -- Avoid cascaded errors if the entity appears in
9786 -- subsequent calls.
9788 Set_Scope (S, Current_Scope);
9790 -- Generate error, with extra useful warning for the case
9791 -- of a generic instance with no completion.
9793 if Is_Generic_Instance (S)
9794 and then not Has_Completion (E)
9795 then
9796 Error_Msg_N
9797 ("instantiation cannot provide body for&", S);
9798 Error_Msg_N ("\& conflicts with declaration#", S);
9799 else
9800 Error_Msg_N ("& conflicts with declaration#", S);
9801 end if;
9803 return;
9804 end if;
9806 else
9807 -- If one subprogram has an access parameter and the other
9808 -- a parameter of an access type, calls to either might be
9809 -- ambiguous. Verify that parameters match except for the
9810 -- access parameter.
9812 if May_Hide_Profile then
9813 declare
9814 F1 : Entity_Id;
9815 F2 : Entity_Id;
9817 begin
9818 F1 := First_Formal (S);
9819 F2 := First_Formal (E);
9820 while Present (F1) and then Present (F2) loop
9821 if Is_Access_Type (Etype (F1)) then
9822 if not Is_Access_Type (Etype (F2))
9823 or else not Conforming_Types
9824 (Designated_Type (Etype (F1)),
9825 Designated_Type (Etype (F2)),
9826 Type_Conformant)
9827 then
9828 May_Hide_Profile := False;
9829 end if;
9831 elsif
9832 not Conforming_Types
9833 (Etype (F1), Etype (F2), Type_Conformant)
9834 then
9835 May_Hide_Profile := False;
9836 end if;
9838 Next_Formal (F1);
9839 Next_Formal (F2);
9840 end loop;
9842 if May_Hide_Profile
9843 and then No (F1)
9844 and then No (F2)
9845 then
9846 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9847 end if;
9848 end;
9849 end if;
9850 end if;
9852 E := Homonym (E);
9853 end loop;
9855 -- On exit, we know that S is a new entity
9857 Enter_Overloaded_Entity (S);
9858 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9859 Check_Overriding_Indicator
9860 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9862 -- Overloading is not allowed in SPARK, except for operators
9864 if Nkind (S) /= N_Defining_Operator_Symbol then
9865 Error_Msg_Sloc := Sloc (Homonym (S));
9866 Check_SPARK_05_Restriction
9867 ("overloading not allowed with entity#", S);
9868 end if;
9870 -- If S is a derived operation for an untagged type then by
9871 -- definition it's not a dispatching operation (even if the parent
9872 -- operation was dispatching), so Check_Dispatching_Operation is not
9873 -- called in that case.
9875 if No (Derived_Type)
9876 or else Is_Tagged_Type (Derived_Type)
9877 then
9878 Check_Dispatching_Operation (S, Empty);
9879 end if;
9880 end if;
9882 -- If this is a user-defined equality operator that is not a derived
9883 -- subprogram, create the corresponding inequality. If the operation is
9884 -- dispatching, the expansion is done elsewhere, and we do not create
9885 -- an explicit inequality operation.
9887 <<Check_Inequality>>
9888 if Chars (S) = Name_Op_Eq
9889 and then Etype (S) = Standard_Boolean
9890 and then Present (Parent (S))
9891 and then not Is_Dispatching_Operation (S)
9892 then
9893 Make_Inequality_Operator (S);
9894 Check_Untagged_Equality (S);
9895 end if;
9896 end New_Overloaded_Entity;
9898 ---------------------
9899 -- Process_Formals --
9900 ---------------------
9902 procedure Process_Formals
9903 (T : List_Id;
9904 Related_Nod : Node_Id)
9906 Context : constant Node_Id := Parent (Parent (T));
9907 Param_Spec : Node_Id;
9908 Formal : Entity_Id;
9909 Formal_Type : Entity_Id;
9910 Default : Node_Id;
9911 Ptype : Entity_Id;
9913 Num_Out_Params : Nat := 0;
9914 First_Out_Param : Entity_Id := Empty;
9915 -- Used for setting Is_Only_Out_Parameter
9917 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
9918 -- Determine whether an access type designates a type coming from a
9919 -- limited view.
9921 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
9922 -- Check whether the default has a class-wide type. After analysis the
9923 -- default has the type of the formal, so we must also check explicitly
9924 -- for an access attribute.
9926 ----------------------------------
9927 -- Designates_From_Limited_With --
9928 ----------------------------------
9930 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
9931 Desig : Entity_Id := Typ;
9933 begin
9934 if Is_Access_Type (Desig) then
9935 Desig := Directly_Designated_Type (Desig);
9936 end if;
9938 if Is_Class_Wide_Type (Desig) then
9939 Desig := Root_Type (Desig);
9940 end if;
9942 return
9943 Ekind (Desig) = E_Incomplete_Type
9944 and then From_Limited_With (Desig);
9945 end Designates_From_Limited_With;
9947 ---------------------------
9948 -- Is_Class_Wide_Default --
9949 ---------------------------
9951 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9952 begin
9953 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9954 or else (Nkind (D) = N_Attribute_Reference
9955 and then Attribute_Name (D) = Name_Access
9956 and then Is_Class_Wide_Type (Etype (Prefix (D))));
9957 end Is_Class_Wide_Default;
9959 -- Start of processing for Process_Formals
9961 begin
9962 -- In order to prevent premature use of the formals in the same formal
9963 -- part, the Ekind is left undefined until all default expressions are
9964 -- analyzed. The Ekind is established in a separate loop at the end.
9966 Param_Spec := First (T);
9967 while Present (Param_Spec) loop
9968 Formal := Defining_Identifier (Param_Spec);
9969 Set_Never_Set_In_Source (Formal, True);
9970 Enter_Name (Formal);
9972 -- Case of ordinary parameters
9974 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9975 Find_Type (Parameter_Type (Param_Spec));
9976 Ptype := Parameter_Type (Param_Spec);
9978 if Ptype = Error then
9979 goto Continue;
9980 end if;
9982 Formal_Type := Entity (Ptype);
9984 if Is_Incomplete_Type (Formal_Type)
9985 or else
9986 (Is_Class_Wide_Type (Formal_Type)
9987 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
9988 then
9989 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
9990 -- primitive operations, as long as their completion is
9991 -- in the same declarative part. If in the private part
9992 -- this means that the type cannot be a Taft-amendment type.
9993 -- Check is done on package exit. For access to subprograms,
9994 -- the use is legal for Taft-amendment types.
9996 -- Ada 2012: tagged incomplete types are allowed as generic
9997 -- formal types. They do not introduce dependencies and the
9998 -- corresponding generic subprogram does not have a delayed
9999 -- freeze, because it does not need a freeze node. However,
10000 -- it is still the case that untagged incomplete types cannot
10001 -- be Taft-amendment types and must be completed in private
10002 -- part, so the subprogram must appear in the list of private
10003 -- dependents of the type. If the type is class-wide, it is
10004 -- not a primitive, but the freezing of the subprogram must
10005 -- also be delayed to force the creation of a freeze node.
10007 if Is_Tagged_Type (Formal_Type)
10008 or else (Ada_Version >= Ada_2012
10009 and then not From_Limited_With (Formal_Type)
10010 and then not Is_Generic_Type (Formal_Type))
10011 then
10012 if Ekind (Scope (Current_Scope)) = E_Package
10013 and then not Is_Generic_Type (Formal_Type)
10014 then
10015 if not Nkind_In
10016 (Parent (T), N_Access_Function_Definition,
10017 N_Access_Procedure_Definition)
10018 then
10019 if not Is_Class_Wide_Type (Formal_Type) then
10020 Append_Elmt (Current_Scope,
10021 Private_Dependents (Base_Type (Formal_Type)));
10022 end if;
10024 -- Freezing is delayed to ensure that Register_Prim
10025 -- will get called for this operation, which is needed
10026 -- in cases where static dispatch tables aren't built.
10027 -- (Note that the same is done for controlling access
10028 -- parameter cases in function Access_Definition.)
10030 if not Is_Thunk (Current_Scope) then
10031 Set_Has_Delayed_Freeze (Current_Scope);
10032 end if;
10033 end if;
10034 end if;
10036 -- Special handling of Value_Type for CIL case
10038 elsif Is_Value_Type (Formal_Type) then
10039 null;
10041 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10042 N_Access_Procedure_Definition)
10043 then
10044 -- AI05-0151: Tagged incomplete types are allowed in all
10045 -- formal parts. Untagged incomplete types are not allowed
10046 -- in bodies. Limited views of either kind are not allowed
10047 -- if there is no place at which the non-limited view can
10048 -- become available.
10050 -- Incomplete formal untagged types are not allowed in
10051 -- subprogram bodies (but are legal in their declarations).
10052 -- This excludes bodies created for null procedures, which
10053 -- are basic declarations.
10055 if Is_Generic_Type (Formal_Type)
10056 and then not Is_Tagged_Type (Formal_Type)
10057 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10058 then
10059 Error_Msg_N
10060 ("invalid use of formal incomplete type", Param_Spec);
10062 elsif Ada_Version >= Ada_2012 then
10063 if Is_Tagged_Type (Formal_Type)
10064 and then (not From_Limited_With (Formal_Type)
10065 or else not In_Package_Body)
10066 then
10067 null;
10069 elsif Nkind_In (Context, N_Accept_Statement,
10070 N_Accept_Alternative,
10071 N_Entry_Body)
10072 or else (Nkind (Context) = N_Subprogram_Body
10073 and then Comes_From_Source (Context))
10074 then
10075 Error_Msg_NE
10076 ("invalid use of untagged incomplete type &",
10077 Ptype, Formal_Type);
10078 end if;
10080 else
10081 Error_Msg_NE
10082 ("invalid use of incomplete type&",
10083 Param_Spec, Formal_Type);
10085 -- Further checks on the legality of incomplete types
10086 -- in formal parts are delayed until the freeze point
10087 -- of the enclosing subprogram or access to subprogram.
10088 end if;
10089 end if;
10091 elsif Ekind (Formal_Type) = E_Void then
10092 Error_Msg_NE
10093 ("premature use of&",
10094 Parameter_Type (Param_Spec), Formal_Type);
10095 end if;
10097 -- Ada 2012 (AI-142): Handle aliased parameters
10099 if Ada_Version >= Ada_2012
10100 and then Aliased_Present (Param_Spec)
10101 then
10102 Set_Is_Aliased (Formal);
10103 end if;
10105 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10106 -- declaration corresponding to the null-excluding type of the
10107 -- formal in the enclosing scope. Finally, replace the parameter
10108 -- type of the formal with the internal subtype.
10110 if Ada_Version >= Ada_2005
10111 and then Null_Exclusion_Present (Param_Spec)
10112 then
10113 if not Is_Access_Type (Formal_Type) then
10114 Error_Msg_N
10115 ("`NOT NULL` allowed only for an access type", Param_Spec);
10117 else
10118 if Can_Never_Be_Null (Formal_Type)
10119 and then Comes_From_Source (Related_Nod)
10120 then
10121 Error_Msg_NE
10122 ("`NOT NULL` not allowed (& already excludes null)",
10123 Param_Spec, Formal_Type);
10124 end if;
10126 Formal_Type :=
10127 Create_Null_Excluding_Itype
10128 (T => Formal_Type,
10129 Related_Nod => Related_Nod,
10130 Scope_Id => Scope (Current_Scope));
10132 -- If the designated type of the itype is an itype that is
10133 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10134 -- on the access subtype, to prevent order-of-elaboration
10135 -- issues in the backend.
10137 -- Example:
10138 -- type T is access procedure;
10139 -- procedure Op (O : not null T);
10141 if Is_Itype (Directly_Designated_Type (Formal_Type))
10142 and then
10143 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10144 then
10145 Set_Has_Delayed_Freeze (Formal_Type);
10146 end if;
10147 end if;
10148 end if;
10150 -- An access formal type
10152 else
10153 Formal_Type :=
10154 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10156 -- No need to continue if we already notified errors
10158 if not Present (Formal_Type) then
10159 return;
10160 end if;
10162 -- Ada 2005 (AI-254)
10164 declare
10165 AD : constant Node_Id :=
10166 Access_To_Subprogram_Definition
10167 (Parameter_Type (Param_Spec));
10168 begin
10169 if Present (AD) and then Protected_Present (AD) then
10170 Formal_Type :=
10171 Replace_Anonymous_Access_To_Protected_Subprogram
10172 (Param_Spec);
10173 end if;
10174 end;
10175 end if;
10177 Set_Etype (Formal, Formal_Type);
10179 -- Deal with default expression if present
10181 Default := Expression (Param_Spec);
10183 if Present (Default) then
10184 Check_SPARK_05_Restriction
10185 ("default expression is not allowed", Default);
10187 if Out_Present (Param_Spec) then
10188 Error_Msg_N
10189 ("default initialization only allowed for IN parameters",
10190 Param_Spec);
10191 end if;
10193 -- Do the special preanalysis of the expression (see section on
10194 -- "Handling of Default Expressions" in the spec of package Sem).
10196 Preanalyze_Spec_Expression (Default, Formal_Type);
10198 -- An access to constant cannot be the default for
10199 -- an access parameter that is an access to variable.
10201 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10202 and then not Is_Access_Constant (Formal_Type)
10203 and then Is_Access_Type (Etype (Default))
10204 and then Is_Access_Constant (Etype (Default))
10205 then
10206 Error_Msg_N
10207 ("formal that is access to variable cannot be initialized "
10208 & "with an access-to-constant expression", Default);
10209 end if;
10211 -- Check that the designated type of an access parameter's default
10212 -- is not a class-wide type unless the parameter's designated type
10213 -- is also class-wide.
10215 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10216 and then not Designates_From_Limited_With (Formal_Type)
10217 and then Is_Class_Wide_Default (Default)
10218 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10219 then
10220 Error_Msg_N
10221 ("access to class-wide expression not allowed here", Default);
10222 end if;
10224 -- Check incorrect use of dynamically tagged expressions
10226 if Is_Tagged_Type (Formal_Type) then
10227 Check_Dynamically_Tagged_Expression
10228 (Expr => Default,
10229 Typ => Formal_Type,
10230 Related_Nod => Default);
10231 end if;
10232 end if;
10234 -- Ada 2005 (AI-231): Static checks
10236 if Ada_Version >= Ada_2005
10237 and then Is_Access_Type (Etype (Formal))
10238 and then Can_Never_Be_Null (Etype (Formal))
10239 then
10240 Null_Exclusion_Static_Checks (Param_Spec);
10241 end if;
10243 -- The following checks are relevant when SPARK_Mode is on as these
10244 -- are not standard Ada legality rules.
10246 if SPARK_Mode = On then
10247 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10249 -- A function cannot have a parameter of mode IN OUT or OUT
10250 -- (SPARK RM 6.1).
10252 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10253 Error_Msg_N
10254 ("function cannot have parameter of mode `OUT` or "
10255 & "`IN OUT`", Formal);
10257 -- A function cannot have an effectively volatile formal
10258 -- parameter (SPARK RM 7.1.3(10)).
10260 elsif Is_Effectively_Volatile (Formal) then
10261 Error_Msg_N
10262 ("function cannot have a volatile formal parameter",
10263 Formal);
10264 end if;
10266 -- A procedure cannot have an effectively volatile formal
10267 -- parameter of mode IN because it behaves as a constant
10268 -- (SPARK RM 7.1.3(6)).
10270 elsif Ekind (Scope (Formal)) = E_Procedure
10271 and then Ekind (Formal) = E_In_Parameter
10272 and then Is_Effectively_Volatile (Formal)
10273 then
10274 Error_Msg_N
10275 ("formal parameter of mode `IN` cannot be volatile", Formal);
10276 end if;
10277 end if;
10279 <<Continue>>
10280 Next (Param_Spec);
10281 end loop;
10283 -- If this is the formal part of a function specification, analyze the
10284 -- subtype mark in the context where the formals are visible but not
10285 -- yet usable, and may hide outer homographs.
10287 if Nkind (Related_Nod) = N_Function_Specification then
10288 Analyze_Return_Type (Related_Nod);
10289 end if;
10291 -- Now set the kind (mode) of each formal
10293 Param_Spec := First (T);
10294 while Present (Param_Spec) loop
10295 Formal := Defining_Identifier (Param_Spec);
10296 Set_Formal_Mode (Formal);
10298 if Ekind (Formal) = E_In_Parameter then
10299 Set_Default_Value (Formal, Expression (Param_Spec));
10301 if Present (Expression (Param_Spec)) then
10302 Default := Expression (Param_Spec);
10304 if Is_Scalar_Type (Etype (Default)) then
10305 if Nkind (Parameter_Type (Param_Spec)) /=
10306 N_Access_Definition
10307 then
10308 Formal_Type := Entity (Parameter_Type (Param_Spec));
10309 else
10310 Formal_Type :=
10311 Access_Definition
10312 (Related_Nod, Parameter_Type (Param_Spec));
10313 end if;
10315 Apply_Scalar_Range_Check (Default, Formal_Type);
10316 end if;
10317 end if;
10319 elsif Ekind (Formal) = E_Out_Parameter then
10320 Num_Out_Params := Num_Out_Params + 1;
10322 if Num_Out_Params = 1 then
10323 First_Out_Param := Formal;
10324 end if;
10326 elsif Ekind (Formal) = E_In_Out_Parameter then
10327 Num_Out_Params := Num_Out_Params + 1;
10328 end if;
10330 -- Skip remaining processing if formal type was in error
10332 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10333 goto Next_Parameter;
10334 end if;
10336 -- Force call by reference if aliased
10338 if Is_Aliased (Formal) then
10339 Set_Mechanism (Formal, By_Reference);
10341 -- Warn if user asked this to be passed by copy
10343 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10344 Error_Msg_N
10345 ("cannot pass aliased parameter & by copy??", Formal);
10346 end if;
10348 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10350 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10351 Set_Mechanism (Formal, By_Copy);
10353 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10354 Set_Mechanism (Formal, By_Reference);
10355 end if;
10357 <<Next_Parameter>>
10358 Next (Param_Spec);
10359 end loop;
10361 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10362 Set_Is_Only_Out_Parameter (First_Out_Param);
10363 end if;
10364 end Process_Formals;
10366 ----------------------------
10367 -- Reference_Body_Formals --
10368 ----------------------------
10370 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10371 Fs : Entity_Id;
10372 Fb : Entity_Id;
10374 begin
10375 if Error_Posted (Spec) then
10376 return;
10377 end if;
10379 -- Iterate over both lists. They may be of different lengths if the two
10380 -- specs are not conformant.
10382 Fs := First_Formal (Spec);
10383 Fb := First_Formal (Bod);
10384 while Present (Fs) and then Present (Fb) loop
10385 Generate_Reference (Fs, Fb, 'b');
10387 if Style_Check then
10388 Style.Check_Identifier (Fb, Fs);
10389 end if;
10391 Set_Spec_Entity (Fb, Fs);
10392 Set_Referenced (Fs, False);
10393 Next_Formal (Fs);
10394 Next_Formal (Fb);
10395 end loop;
10396 end Reference_Body_Formals;
10398 -------------------------
10399 -- Set_Actual_Subtypes --
10400 -------------------------
10402 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10403 Decl : Node_Id;
10404 Formal : Entity_Id;
10405 T : Entity_Id;
10406 First_Stmt : Node_Id := Empty;
10407 AS_Needed : Boolean;
10409 begin
10410 -- If this is an empty initialization procedure, no need to create
10411 -- actual subtypes (small optimization).
10413 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10414 return;
10415 end if;
10417 Formal := First_Formal (Subp);
10418 while Present (Formal) loop
10419 T := Etype (Formal);
10421 -- We never need an actual subtype for a constrained formal
10423 if Is_Constrained (T) then
10424 AS_Needed := False;
10426 -- If we have unknown discriminants, then we do not need an actual
10427 -- subtype, or more accurately we cannot figure it out. Note that
10428 -- all class-wide types have unknown discriminants.
10430 elsif Has_Unknown_Discriminants (T) then
10431 AS_Needed := False;
10433 -- At this stage we have an unconstrained type that may need an
10434 -- actual subtype. For sure the actual subtype is needed if we have
10435 -- an unconstrained array type.
10437 elsif Is_Array_Type (T) then
10438 AS_Needed := True;
10440 -- The only other case needing an actual subtype is an unconstrained
10441 -- record type which is an IN parameter (we cannot generate actual
10442 -- subtypes for the OUT or IN OUT case, since an assignment can
10443 -- change the discriminant values. However we exclude the case of
10444 -- initialization procedures, since discriminants are handled very
10445 -- specially in this context, see the section entitled "Handling of
10446 -- Discriminants" in Einfo.
10448 -- We also exclude the case of Discrim_SO_Functions (functions used
10449 -- in front end layout mode for size/offset values), since in such
10450 -- functions only discriminants are referenced, and not only are such
10451 -- subtypes not needed, but they cannot always be generated, because
10452 -- of order of elaboration issues.
10454 elsif Is_Record_Type (T)
10455 and then Ekind (Formal) = E_In_Parameter
10456 and then Chars (Formal) /= Name_uInit
10457 and then not Is_Unchecked_Union (T)
10458 and then not Is_Discrim_SO_Function (Subp)
10459 then
10460 AS_Needed := True;
10462 -- All other cases do not need an actual subtype
10464 else
10465 AS_Needed := False;
10466 end if;
10468 -- Generate actual subtypes for unconstrained arrays and
10469 -- unconstrained discriminated records.
10471 if AS_Needed then
10472 if Nkind (N) = N_Accept_Statement then
10474 -- If expansion is active, the formal is replaced by a local
10475 -- variable that renames the corresponding entry of the
10476 -- parameter block, and it is this local variable that may
10477 -- require an actual subtype.
10479 if Expander_Active then
10480 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10481 else
10482 Decl := Build_Actual_Subtype (T, Formal);
10483 end if;
10485 if Present (Handled_Statement_Sequence (N)) then
10486 First_Stmt :=
10487 First (Statements (Handled_Statement_Sequence (N)));
10488 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10489 Mark_Rewrite_Insertion (Decl);
10490 else
10491 -- If the accept statement has no body, there will be no
10492 -- reference to the actuals, so no need to compute actual
10493 -- subtypes.
10495 return;
10496 end if;
10498 else
10499 Decl := Build_Actual_Subtype (T, Formal);
10500 Prepend (Decl, Declarations (N));
10501 Mark_Rewrite_Insertion (Decl);
10502 end if;
10504 -- The declaration uses the bounds of an existing object, and
10505 -- therefore needs no constraint checks.
10507 Analyze (Decl, Suppress => All_Checks);
10509 -- We need to freeze manually the generated type when it is
10510 -- inserted anywhere else than in a declarative part.
10512 if Present (First_Stmt) then
10513 Insert_List_Before_And_Analyze (First_Stmt,
10514 Freeze_Entity (Defining_Identifier (Decl), N));
10516 -- Ditto if the type has a dynamic predicate, because the
10517 -- generated function will mention the actual subtype.
10519 elsif Has_Dynamic_Predicate_Aspect (T) then
10520 Insert_List_Before_And_Analyze (Decl,
10521 Freeze_Entity (Defining_Identifier (Decl), N));
10522 end if;
10524 if Nkind (N) = N_Accept_Statement
10525 and then Expander_Active
10526 then
10527 Set_Actual_Subtype (Renamed_Object (Formal),
10528 Defining_Identifier (Decl));
10529 else
10530 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10531 end if;
10532 end if;
10534 Next_Formal (Formal);
10535 end loop;
10536 end Set_Actual_Subtypes;
10538 ---------------------
10539 -- Set_Formal_Mode --
10540 ---------------------
10542 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10543 Spec : constant Node_Id := Parent (Formal_Id);
10545 begin
10546 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10547 -- since we ensure that corresponding actuals are always valid at the
10548 -- point of the call.
10550 if Out_Present (Spec) then
10551 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10553 -- [IN] OUT parameters allowed for functions in Ada 2012
10555 if Ada_Version >= Ada_2012 then
10557 -- Even in Ada 2012 operators can only have IN parameters
10559 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10560 Error_Msg_N ("operators can only have IN parameters", Spec);
10561 end if;
10563 if In_Present (Spec) then
10564 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10565 else
10566 Set_Ekind (Formal_Id, E_Out_Parameter);
10567 end if;
10569 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10571 -- But not in earlier versions of Ada
10573 else
10574 Error_Msg_N ("functions can only have IN parameters", Spec);
10575 Set_Ekind (Formal_Id, E_In_Parameter);
10576 end if;
10578 elsif In_Present (Spec) then
10579 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10581 else
10582 Set_Ekind (Formal_Id, E_Out_Parameter);
10583 Set_Never_Set_In_Source (Formal_Id, True);
10584 Set_Is_True_Constant (Formal_Id, False);
10585 Set_Current_Value (Formal_Id, Empty);
10586 end if;
10588 else
10589 Set_Ekind (Formal_Id, E_In_Parameter);
10590 end if;
10592 -- Set Is_Known_Non_Null for access parameters since the language
10593 -- guarantees that access parameters are always non-null. We also set
10594 -- Can_Never_Be_Null, since there is no way to change the value.
10596 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10598 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10599 -- null; In Ada 2005, only if then null_exclusion is explicit.
10601 if Ada_Version < Ada_2005
10602 or else Can_Never_Be_Null (Etype (Formal_Id))
10603 then
10604 Set_Is_Known_Non_Null (Formal_Id);
10605 Set_Can_Never_Be_Null (Formal_Id);
10606 end if;
10608 -- Ada 2005 (AI-231): Null-exclusion access subtype
10610 elsif Is_Access_Type (Etype (Formal_Id))
10611 and then Can_Never_Be_Null (Etype (Formal_Id))
10612 then
10613 Set_Is_Known_Non_Null (Formal_Id);
10615 -- We can also set Can_Never_Be_Null (thus preventing some junk
10616 -- access checks) for the case of an IN parameter, which cannot
10617 -- be changed, or for an IN OUT parameter, which can be changed but
10618 -- not to a null value. But for an OUT parameter, the initial value
10619 -- passed in can be null, so we can't set this flag in that case.
10621 if Ekind (Formal_Id) /= E_Out_Parameter then
10622 Set_Can_Never_Be_Null (Formal_Id);
10623 end if;
10624 end if;
10626 Set_Mechanism (Formal_Id, Default_Mechanism);
10627 Set_Formal_Validity (Formal_Id);
10628 end Set_Formal_Mode;
10630 -------------------------
10631 -- Set_Formal_Validity --
10632 -------------------------
10634 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10635 begin
10636 -- If no validity checking, then we cannot assume anything about the
10637 -- validity of parameters, since we do not know there is any checking
10638 -- of the validity on the call side.
10640 if not Validity_Checks_On then
10641 return;
10643 -- If validity checking for parameters is enabled, this means we are
10644 -- not supposed to make any assumptions about argument values.
10646 elsif Validity_Check_Parameters then
10647 return;
10649 -- If we are checking in parameters, we will assume that the caller is
10650 -- also checking parameters, so we can assume the parameter is valid.
10652 elsif Ekind (Formal_Id) = E_In_Parameter
10653 and then Validity_Check_In_Params
10654 then
10655 Set_Is_Known_Valid (Formal_Id, True);
10657 -- Similar treatment for IN OUT parameters
10659 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10660 and then Validity_Check_In_Out_Params
10661 then
10662 Set_Is_Known_Valid (Formal_Id, True);
10663 end if;
10664 end Set_Formal_Validity;
10666 ------------------------
10667 -- Subtype_Conformant --
10668 ------------------------
10670 function Subtype_Conformant
10671 (New_Id : Entity_Id;
10672 Old_Id : Entity_Id;
10673 Skip_Controlling_Formals : Boolean := False) return Boolean
10675 Result : Boolean;
10676 begin
10677 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10678 Skip_Controlling_Formals => Skip_Controlling_Formals);
10679 return Result;
10680 end Subtype_Conformant;
10682 ---------------------
10683 -- Type_Conformant --
10684 ---------------------
10686 function Type_Conformant
10687 (New_Id : Entity_Id;
10688 Old_Id : Entity_Id;
10689 Skip_Controlling_Formals : Boolean := False) return Boolean
10691 Result : Boolean;
10692 begin
10693 May_Hide_Profile := False;
10694 Check_Conformance
10695 (New_Id, Old_Id, Type_Conformant, False, Result,
10696 Skip_Controlling_Formals => Skip_Controlling_Formals);
10697 return Result;
10698 end Type_Conformant;
10700 -------------------------------
10701 -- Valid_Operator_Definition --
10702 -------------------------------
10704 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10705 N : Integer := 0;
10706 F : Entity_Id;
10707 Id : constant Name_Id := Chars (Designator);
10708 N_OK : Boolean;
10710 begin
10711 F := First_Formal (Designator);
10712 while Present (F) loop
10713 N := N + 1;
10715 if Present (Default_Value (F)) then
10716 Error_Msg_N
10717 ("default values not allowed for operator parameters",
10718 Parent (F));
10720 -- For function instantiations that are operators, we must check
10721 -- separately that the corresponding generic only has in-parameters.
10722 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10723 -- an error could not arise in earlier versions of the language.
10725 elsif Ekind (F) /= E_In_Parameter then
10726 Error_Msg_N ("operators can only have IN parameters", F);
10727 end if;
10729 Next_Formal (F);
10730 end loop;
10732 -- Verify that user-defined operators have proper number of arguments
10733 -- First case of operators which can only be unary
10735 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10736 N_OK := (N = 1);
10738 -- Case of operators which can be unary or binary
10740 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10741 N_OK := (N in 1 .. 2);
10743 -- All other operators can only be binary
10745 else
10746 N_OK := (N = 2);
10747 end if;
10749 if not N_OK then
10750 Error_Msg_N
10751 ("incorrect number of arguments for operator", Designator);
10752 end if;
10754 if Id = Name_Op_Ne
10755 and then Base_Type (Etype (Designator)) = Standard_Boolean
10756 and then not Is_Intrinsic_Subprogram (Designator)
10757 then
10758 Error_Msg_N
10759 ("explicit definition of inequality not allowed", Designator);
10760 end if;
10761 end Valid_Operator_Definition;
10763 end Sem_Ch6;