2014-11-20 Thomas Quinot <quinot@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch6.adb
blob5a5265c2778e116c9aae1b5506c331e40e4412f9
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-2014, 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 Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib.Xref; use Lib.Xref;
46 with Layout; use Layout;
47 with Namet; use Namet;
48 with Lib; use Lib;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
51 with Opt; use Opt;
52 with Output; use Output;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch3; use Sem_Ch3;
60 with Sem_Ch4; use Sem_Ch4;
61 with Sem_Ch5; use Sem_Ch5;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch10; use Sem_Ch10;
64 with Sem_Ch12; use Sem_Ch12;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Elim; use Sem_Elim;
70 with Sem_Eval; use Sem_Eval;
71 with Sem_Mech; use Sem_Mech;
72 with Sem_Prag; use Sem_Prag;
73 with Sem_Res; use Sem_Res;
74 with Sem_Util; use Sem_Util;
75 with Sem_Type; use Sem_Type;
76 with Sem_Warn; use Sem_Warn;
77 with Sinput; use Sinput;
78 with Stand; use Stand;
79 with Sinfo; use Sinfo;
80 with Sinfo.CN; use Sinfo.CN;
81 with Snames; use Snames;
82 with Stringt; use Stringt;
83 with Style;
84 with Stylesw; use Stylesw;
85 with Targparm; use Targparm;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Urealp; use Urealp;
89 with Validsw; use Validsw;
91 package body Sem_Ch6 is
93 May_Hide_Profile : Boolean := False;
94 -- This flag is used to indicate that two formals in two subprograms being
95 -- checked for conformance differ only in that one is an access parameter
96 -- while the other is of a general access type with the same designated
97 -- type. In this case, if the rest of the signatures match, a call to
98 -- either subprogram may be ambiguous, which is worth a warning. The flag
99 -- is set in Compatible_Types, and the warning emitted in
100 -- New_Overloaded_Entity.
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
106 procedure Analyze_Null_Procedure
107 (N : Node_Id;
108 Is_Completion : out Boolean);
109 -- A null procedure can be a declaration or (Ada 2012) a completion
111 procedure Analyze_Return_Statement (N : Node_Id);
112 -- Common processing for simple and extended return statements
114 procedure Analyze_Function_Return (N : Node_Id);
115 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
116 -- applies to a [generic] function.
118 procedure Analyze_Return_Type (N : Node_Id);
119 -- Subsidiary to Process_Formals: analyze subtype mark in function
120 -- specification in a context where the formals are visible and hide
121 -- outer homographs.
123 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
124 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
125 -- that we can use RETURN but not skip the debug output at the end.
127 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
128 -- Analyze a generic subprogram body. N is the body to be analyzed, and
129 -- Gen_Id is the defining entity Id for the corresponding spec.
131 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
132 -- Returns true if Subp can override a predefined operator.
134 procedure Check_Conformance
135 (New_Id : Entity_Id;
136 Old_Id : Entity_Id;
137 Ctype : Conformance_Type;
138 Errmsg : Boolean;
139 Conforms : out Boolean;
140 Err_Loc : Node_Id := Empty;
141 Get_Inst : Boolean := False;
142 Skip_Controlling_Formals : Boolean := False);
143 -- Given two entities, this procedure checks that the profiles associated
144 -- with these entities meet the conformance criterion given by the third
145 -- parameter. If they conform, Conforms is set True and control returns
146 -- to the caller. If they do not conform, Conforms is set to False, and
147 -- in addition, if Errmsg is True on the call, proper messages are output
148 -- to complain about the conformance failure. If Err_Loc is non_Empty
149 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
150 -- error messages are placed on the appropriate part of the construct
151 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
152 -- against a formal access-to-subprogram type so Get_Instance_Of must
153 -- be called.
155 procedure Check_Subprogram_Order (N : Node_Id);
156 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
157 -- the alpha ordering rule for N if this ordering requirement applicable.
159 procedure Check_Returns
160 (HSS : Node_Id;
161 Mode : Character;
162 Err : out Boolean;
163 Proc : Entity_Id := Empty);
164 -- Called to check for missing return statements in a function body, or for
165 -- returns present in a procedure body which has No_Return set. HSS is the
166 -- handled statement sequence for the subprogram body. This procedure
167 -- checks all flow paths to make sure they either have return (Mode = 'F',
168 -- used for functions) or do not have a return (Mode = 'P', used for
169 -- No_Return procedures). The flag Err is set if there are any control
170 -- paths not explicitly terminated by a return in the function case, and is
171 -- True otherwise. Proc is the entity for the procedure case and is used
172 -- in posting the warning message.
174 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
175 -- In Ada 2012, a primitive equality operator on an untagged record type
176 -- must appear before the type is frozen, and have the same visibility as
177 -- that of the type. This procedure checks that this rule is met, and
178 -- otherwise emits an error on the subprogram declaration and a warning
179 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
180 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
181 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
182 -- is set, otherwise the call has no effect.
184 procedure Enter_Overloaded_Entity (S : Entity_Id);
185 -- This procedure makes S, a new overloaded entity, into the first visible
186 -- entity with that name.
188 function Is_Non_Overriding_Operation
189 (Prev_E : Entity_Id;
190 New_E : Entity_Id) return Boolean;
191 -- Enforce the rule given in 12.3(18): a private operation in an instance
192 -- overrides an inherited operation only if the corresponding operation
193 -- was overriding in the generic. This needs to be checked for primitive
194 -- operations of types derived (in the generic unit) from formal private
195 -- or formal derived types.
197 procedure Make_Inequality_Operator (S : Entity_Id);
198 -- Create the declaration for an inequality operator that is implicitly
199 -- created by a user-defined equality operator that yields a boolean.
201 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
202 -- Formal_Id is an formal parameter entity. This procedure deals with
203 -- setting the proper validity status for this entity, which depends on
204 -- the kind of parameter and the validity checking mode.
206 ---------------------------------------------
207 -- Analyze_Abstract_Subprogram_Declaration --
208 ---------------------------------------------
210 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
211 Designator : constant Entity_Id :=
212 Analyze_Subprogram_Specification (Specification (N));
213 Scop : constant Entity_Id := Current_Scope;
215 begin
216 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
218 Generate_Definition (Designator);
219 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
220 Set_Is_Abstract_Subprogram (Designator);
221 New_Overloaded_Entity (Designator);
222 Check_Delayed_Subprogram (Designator);
224 Set_Categorization_From_Scope (Designator, Scop);
226 -- An abstract subprogram declared within a Ghost scope is automatically
227 -- Ghost (SPARK RM 6.9(2)).
229 if Comes_From_Source (Designator) and then Within_Ghost_Scope then
230 Set_Is_Ghost_Entity (Designator);
231 end if;
233 if Ekind (Scope (Designator)) = E_Protected_Type then
234 Error_Msg_N
235 ("abstract subprogram not allowed in protected type", N);
237 -- Issue a warning if the abstract subprogram is neither a dispatching
238 -- operation nor an operation that overrides an inherited subprogram or
239 -- predefined operator, since this most likely indicates a mistake.
241 elsif Warn_On_Redundant_Constructs
242 and then not Is_Dispatching_Operation (Designator)
243 and then not Present (Overridden_Operation (Designator))
244 and then (not Is_Operator_Symbol_Name (Chars (Designator))
245 or else Scop /= Scope (Etype (First_Formal (Designator))))
246 then
247 Error_Msg_N
248 ("abstract subprogram is not dispatching or overriding?r?", N);
249 end if;
251 Generate_Reference_To_Formals (Designator);
252 Check_Eliminated (Designator);
254 if Has_Aspects (N) then
255 Analyze_Aspect_Specifications (N, Designator);
256 end if;
257 end Analyze_Abstract_Subprogram_Declaration;
259 ---------------------------------
260 -- Analyze_Expression_Function --
261 ---------------------------------
263 procedure Analyze_Expression_Function (N : Node_Id) is
264 Loc : constant Source_Ptr := Sloc (N);
265 LocX : constant Source_Ptr := Sloc (Expression (N));
266 Expr : constant Node_Id := Expression (N);
267 Spec : constant Node_Id := Specification (N);
269 Def_Id : Entity_Id;
271 Prev : Entity_Id;
272 -- If the expression is a completion, Prev is the entity whose
273 -- declaration is completed. Def_Id is needed to analyze the spec.
275 New_Body : Node_Id;
276 New_Spec : Node_Id;
277 Ret : Node_Id;
279 begin
280 -- This is one of the occasions on which we transform the tree during
281 -- semantic analysis. If this is a completion, transform the expression
282 -- function into an equivalent subprogram body, and analyze it.
284 -- Expression functions are inlined unconditionally. The back-end will
285 -- determine whether this is possible.
287 Inline_Processing_Required := True;
289 -- Create a specification for the generated body. Types and defauts in
290 -- the profile are copies of the spec, but new entities must be created
291 -- for the unit name and the formals.
293 New_Spec := New_Copy_Tree (Spec);
294 Set_Defining_Unit_Name (New_Spec,
295 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
296 Chars (Defining_Unit_Name (Spec))));
298 if Present (Parameter_Specifications (New_Spec)) then
299 declare
300 Formal_Spec : Node_Id;
301 Def : Entity_Id;
303 begin
304 Formal_Spec := First (Parameter_Specifications (New_Spec));
306 -- Create a new formal parameter at the same source position
308 while Present (Formal_Spec) loop
309 Def := Defining_Identifier (Formal_Spec);
310 Set_Defining_Identifier (Formal_Spec,
311 Make_Defining_Identifier (Sloc (Def),
312 Chars => Chars (Def)));
313 Next (Formal_Spec);
314 end loop;
315 end;
316 end if;
318 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
320 -- If there are previous overloadable entities with the same name,
321 -- check whether any of them is completed by the expression function.
322 -- In a generic context a formal subprogram has no completion.
324 if Present (Prev) and then Is_Overloadable (Prev)
325 and then not Is_Formal_Subprogram (Prev)
326 then
327 Def_Id := Analyze_Subprogram_Specification (Spec);
328 Prev := Find_Corresponding_Spec (N);
330 -- The previous entity may be an expression function as well, in
331 -- which case the redeclaration is illegal.
333 if Present (Prev)
334 and then Nkind (Original_Node (Unit_Declaration_Node (Prev)))
335 = N_Expression_Function
336 then
337 Error_Msg_N ("Duplicate expression function", N);
338 return;
339 end if;
340 end if;
342 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
344 New_Body :=
345 Make_Subprogram_Body (Loc,
346 Specification => New_Spec,
347 Declarations => Empty_List,
348 Handled_Statement_Sequence =>
349 Make_Handled_Sequence_Of_Statements (LocX,
350 Statements => New_List (Ret)));
352 -- If the expression completes a generic subprogram, we must create a
353 -- separate node for the body, because at instantiation the original
354 -- node of the generic copy must be a generic subprogram body, and
355 -- cannot be a expression function. Otherwise we just rewrite the
356 -- expression with the non-generic body.
358 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
359 Insert_After (N, New_Body);
361 -- Propagate any aspects or pragmas that apply to the expression
362 -- function to the proper body when the expression function acts
363 -- as a completion.
365 if Has_Aspects (N) then
366 Move_Aspects (N, To => New_Body);
367 end if;
369 Relocate_Pragmas_To_Body (New_Body);
371 Rewrite (N, Make_Null_Statement (Loc));
372 Set_Has_Completion (Prev, False);
373 Analyze (N);
374 Analyze (New_Body);
375 Set_Is_Inlined (Prev);
377 -- If the expression function is a completion, the previous declaration
378 -- must come from source. We know already that appears in the current
379 -- scope. The entity itself may be internally created if within a body
380 -- to be inlined.
382 elsif Present (Prev) and then Comes_From_Source (Parent (Prev))
383 and then not Is_Formal_Subprogram (Prev)
384 then
385 Set_Has_Completion (Prev, False);
387 -- An expression function that is a completion freezes the
388 -- expression. This means freezing the return type, and if it is
389 -- an access type, freezing its designated type as well.
391 -- Note that we cannot defer this freezing to the analysis of the
392 -- expression itself, because a freeze node might appear in a nested
393 -- scope, leading to an elaboration order issue in gigi.
395 Freeze_Before (N, Etype (Prev));
397 if Is_Access_Type (Etype (Prev)) then
398 Freeze_Before (N, Designated_Type (Etype (Prev)));
399 end if;
401 -- For navigation purposes, indicate that the function is a body
403 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
404 Rewrite (N, New_Body);
406 -- Correct the parent pointer of the aspect specification list to
407 -- reference the rewritten node.
409 if Has_Aspects (N) then
410 Set_Parent (Aspect_Specifications (N), N);
411 end if;
413 -- Propagate any pragmas that apply to the expression function to the
414 -- proper body when the expression function acts as a completion.
415 -- Aspects are automatically transfered because of node rewriting.
417 Relocate_Pragmas_To_Body (N);
418 Analyze (N);
420 -- Prev is the previous entity with the same name, but it is can
421 -- be an unrelated spec that is not completed by the expression
422 -- function. In that case the relevant entity is the one in the body.
423 -- Not clear that the backend can inline it in this case ???
425 if Has_Completion (Prev) then
426 Set_Is_Inlined (Prev);
428 -- The formals of the expression function are body formals,
429 -- and do not appear in the ali file, which will only contain
430 -- references to the formals of the original subprogram spec.
432 declare
433 F1 : Entity_Id;
434 F2 : Entity_Id;
436 begin
437 F1 := First_Formal (Def_Id);
438 F2 := First_Formal (Prev);
440 while Present (F1) loop
441 Set_Spec_Entity (F1, F2);
442 Next_Formal (F1);
443 Next_Formal (F2);
444 end loop;
445 end;
447 else
448 Set_Is_Inlined (Defining_Entity (New_Body));
449 end if;
451 -- If this is not a completion, create both a declaration and a body, so
452 -- that the expression can be inlined whenever possible.
454 else
455 -- An expression function that is not a completion is not a
456 -- subprogram declaration, and thus cannot appear in a protected
457 -- definition.
459 if Nkind (Parent (N)) = N_Protected_Definition then
460 Error_Msg_N
461 ("an expression function is not a legal protected operation", N);
462 end if;
464 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
466 -- Correct the parent pointer of the aspect specification list to
467 -- reference the rewritten node.
469 if Has_Aspects (N) then
470 Set_Parent (Aspect_Specifications (N), N);
471 end if;
473 Analyze (N);
475 -- Within a generic pre-analyze the original expression for name
476 -- capture. The body is also generated but plays no role in
477 -- this because it is not part of the original source.
479 if Inside_A_Generic then
480 declare
481 Id : constant Entity_Id := Defining_Entity (N);
483 begin
484 Set_Has_Completion (Id);
485 Push_Scope (Id);
486 Install_Formals (Id);
487 Preanalyze_Spec_Expression (Expr, Etype (Id));
488 End_Scope;
489 end;
490 end if;
492 Set_Is_Inlined (Defining_Entity (N));
494 -- Establish the linkages between the spec and the body. These are
495 -- used when the expression function acts as the prefix of attribute
496 -- 'Access in order to freeze the original expression which has been
497 -- moved to the generated body.
499 Set_Corresponding_Body (N, Defining_Entity (New_Body));
500 Set_Corresponding_Spec (New_Body, Defining_Entity (N));
502 -- To prevent premature freeze action, insert the new body at the end
503 -- of the current declarations, or at the end of the package spec.
504 -- However, resolve usage names now, to prevent spurious visibility
505 -- on later entities. Note that the function can now be called in
506 -- the current declarative part, which will appear to be prior to
507 -- the presence of the body in the code. There are nevertheless no
508 -- order of elaboration issues because all name resolution has taken
509 -- place at the point of declaration.
511 declare
512 Decls : List_Id := List_Containing (N);
513 Par : constant Node_Id := Parent (Decls);
514 Id : constant Entity_Id := Defining_Entity (N);
516 begin
517 -- If this is a wrapper created for in an instance for a formal
518 -- subprogram, insert body after declaration, to be analyzed when
519 -- the enclosing instance is analyzed.
521 if GNATprove_Mode
522 and then Is_Generic_Actual_Subprogram (Defining_Entity (N))
523 then
524 Insert_After (N, New_Body);
526 else
527 if Nkind (Par) = N_Package_Specification
528 and then Decls = Visible_Declarations (Par)
529 and then Present (Private_Declarations (Par))
530 and then not Is_Empty_List (Private_Declarations (Par))
531 then
532 Decls := Private_Declarations (Par);
533 end if;
535 Insert_After (Last (Decls), New_Body);
536 Push_Scope (Id);
537 Install_Formals (Id);
539 -- Preanalyze the expression for name capture, except in an
540 -- instance, where this has been done during generic analysis,
541 -- and will be redone when analyzing the body.
543 declare
544 Expr : constant Node_Id := Expression (Ret);
546 begin
547 Set_Parent (Expr, Ret);
549 if not In_Instance then
550 Preanalyze_Spec_Expression (Expr, Etype (Id));
551 end if;
552 end;
554 End_Scope;
555 end if;
556 end;
557 end if;
559 -- If the return expression is a static constant, we suppress warning
560 -- messages on unused formals, which in most cases will be noise.
562 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
563 Is_OK_Static_Expression (Expr));
564 end Analyze_Expression_Function;
566 ----------------------------------------
567 -- Analyze_Extended_Return_Statement --
568 ----------------------------------------
570 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
571 begin
572 Check_Compiler_Unit ("extended return statement", N);
573 Analyze_Return_Statement (N);
574 end Analyze_Extended_Return_Statement;
576 ----------------------------
577 -- Analyze_Function_Call --
578 ----------------------------
580 procedure Analyze_Function_Call (N : Node_Id) is
581 Actuals : constant List_Id := Parameter_Associations (N);
582 Func_Nam : constant Node_Id := Name (N);
583 Actual : Node_Id;
585 begin
586 Analyze (Func_Nam);
588 -- A call of the form A.B (X) may be an Ada 2005 call, which is
589 -- rewritten as B (A, X). If the rewriting is successful, the call
590 -- has been analyzed and we just return.
592 if Nkind (Func_Nam) = N_Selected_Component
593 and then Name (N) /= Func_Nam
594 and then Is_Rewrite_Substitution (N)
595 and then Present (Etype (N))
596 then
597 return;
598 end if;
600 -- If error analyzing name, then set Any_Type as result type and return
602 if Etype (Func_Nam) = Any_Type then
603 Set_Etype (N, Any_Type);
604 return;
605 end if;
607 -- Otherwise analyze the parameters
609 if Present (Actuals) then
610 Actual := First (Actuals);
611 while Present (Actual) loop
612 Analyze (Actual);
613 Check_Parameterless_Call (Actual);
614 Next (Actual);
615 end loop;
616 end if;
618 Analyze_Call (N);
619 end Analyze_Function_Call;
621 -----------------------------
622 -- Analyze_Function_Return --
623 -----------------------------
625 procedure Analyze_Function_Return (N : Node_Id) is
626 Loc : constant Source_Ptr := Sloc (N);
627 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
628 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
630 R_Type : constant Entity_Id := Etype (Scope_Id);
631 -- Function result subtype
633 procedure Check_Limited_Return (Expr : Node_Id);
634 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
635 -- limited types. Used only for simple return statements.
636 -- Expr is the expression returned.
638 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
639 -- Check that the return_subtype_indication properly matches the result
640 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
642 --------------------------
643 -- Check_Limited_Return --
644 --------------------------
646 procedure Check_Limited_Return (Expr : Node_Id) is
647 begin
648 -- Ada 2005 (AI-318-02): Return-by-reference types have been
649 -- removed and replaced by anonymous access results. This is an
650 -- incompatibility with Ada 95. Not clear whether this should be
651 -- enforced yet or perhaps controllable with special switch. ???
653 -- A limited interface that is not immutably limited is OK.
655 if Is_Limited_Interface (R_Type)
656 and then
657 not (Is_Task_Interface (R_Type)
658 or else Is_Protected_Interface (R_Type)
659 or else Is_Synchronized_Interface (R_Type))
660 then
661 null;
663 elsif Is_Limited_Type (R_Type)
664 and then not Is_Interface (R_Type)
665 and then Comes_From_Source (N)
666 and then not In_Instance_Body
667 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
668 then
669 -- Error in Ada 2005
671 if Ada_Version >= Ada_2005
672 and then not Debug_Flag_Dot_L
673 and then not GNAT_Mode
674 then
675 Error_Msg_N
676 ("(Ada 2005) cannot copy object of a limited type "
677 & "(RM-2005 6.5(5.5/2))", Expr);
679 if Is_Limited_View (R_Type) then
680 Error_Msg_N
681 ("\return by reference not permitted in Ada 2005", Expr);
682 end if;
684 -- Warn in Ada 95 mode, to give folks a heads up about this
685 -- incompatibility.
687 -- In GNAT mode, this is just a warning, to allow it to be
688 -- evilly turned off. Otherwise it is a real error.
690 -- In a generic context, simplify the warning because it makes
691 -- no sense to discuss pass-by-reference or copy.
693 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
694 if Inside_A_Generic then
695 Error_Msg_N
696 ("return of limited object not permitted in Ada 2005 "
697 & "(RM-2005 6.5(5.5/2))?y?", Expr);
699 elsif Is_Limited_View (R_Type) then
700 Error_Msg_N
701 ("return by reference not permitted in Ada 2005 "
702 & "(RM-2005 6.5(5.5/2))?y?", Expr);
703 else
704 Error_Msg_N
705 ("cannot copy object of a limited type in Ada 2005 "
706 & "(RM-2005 6.5(5.5/2))?y?", Expr);
707 end if;
709 -- Ada 95 mode, compatibility warnings disabled
711 else
712 return; -- skip continuation messages below
713 end if;
715 if not Inside_A_Generic then
716 Error_Msg_N
717 ("\consider switching to return of access type", Expr);
718 Explain_Limited_Type (R_Type, Expr);
719 end if;
720 end if;
721 end Check_Limited_Return;
723 -------------------------------------
724 -- Check_Return_Subtype_Indication --
725 -------------------------------------
727 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
728 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
730 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
731 -- Subtype given in the extended return statement (must match R_Type)
733 Subtype_Ind : constant Node_Id :=
734 Object_Definition (Original_Node (Obj_Decl));
736 R_Type_Is_Anon_Access : constant Boolean :=
737 Ekind_In (R_Type,
738 E_Anonymous_Access_Subprogram_Type,
739 E_Anonymous_Access_Protected_Subprogram_Type,
740 E_Anonymous_Access_Type);
741 -- True if return type of the function is an anonymous access type
742 -- Can't we make Is_Anonymous_Access_Type in einfo ???
744 R_Stm_Type_Is_Anon_Access : constant Boolean :=
745 Ekind_In (R_Stm_Type,
746 E_Anonymous_Access_Subprogram_Type,
747 E_Anonymous_Access_Protected_Subprogram_Type,
748 E_Anonymous_Access_Type);
749 -- True if type of the return object is an anonymous access type
751 procedure Error_No_Match (N : Node_Id);
752 -- Output error messages for case where types do not statically
753 -- match. N is the location for the messages.
755 --------------------
756 -- Error_No_Match --
757 --------------------
759 procedure Error_No_Match (N : Node_Id) is
760 begin
761 Error_Msg_N
762 ("subtype must statically match function result subtype", N);
764 if not Predicates_Match (R_Stm_Type, R_Type) then
765 Error_Msg_Node_2 := R_Type;
766 Error_Msg_NE
767 ("\predicate of& does not match predicate of&",
768 N, R_Stm_Type);
769 end if;
770 end Error_No_Match;
772 -- Start of processing for Check_Return_Subtype_Indication
774 begin
775 -- First, avoid cascaded errors
777 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
778 return;
779 end if;
781 -- "return access T" case; check that the return statement also has
782 -- "access T", and that the subtypes statically match:
783 -- if this is an access to subprogram the signatures must match.
785 if R_Type_Is_Anon_Access then
786 if R_Stm_Type_Is_Anon_Access then
788 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
789 then
790 if Base_Type (Designated_Type (R_Stm_Type)) /=
791 Base_Type (Designated_Type (R_Type))
792 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
793 then
794 Error_No_Match (Subtype_Mark (Subtype_Ind));
795 end if;
797 else
798 -- For two anonymous access to subprogram types, the
799 -- types themselves must be type conformant.
801 if not Conforming_Types
802 (R_Stm_Type, R_Type, Fully_Conformant)
803 then
804 Error_No_Match (Subtype_Ind);
805 end if;
806 end if;
808 else
809 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
810 end if;
812 -- If the return object is of an anonymous access type, then report
813 -- an error if the function's result type is not also anonymous.
815 elsif R_Stm_Type_Is_Anon_Access
816 and then not R_Type_Is_Anon_Access
817 then
818 Error_Msg_N ("anonymous access not allowed for function with "
819 & "named access result", Subtype_Ind);
821 -- Subtype indication case: check that the return object's type is
822 -- covered by the result type, and that the subtypes statically match
823 -- when the result subtype is constrained. Also handle record types
824 -- with unknown discriminants for which we have built the underlying
825 -- record view. Coverage is needed to allow specific-type return
826 -- objects when the result type is class-wide (see AI05-32).
828 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
829 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
830 and then
831 Covers
832 (Base_Type (R_Type),
833 Underlying_Record_View (Base_Type (R_Stm_Type))))
834 then
835 -- A null exclusion may be present on the return type, on the
836 -- function specification, on the object declaration or on the
837 -- subtype itself.
839 if Is_Access_Type (R_Type)
840 and then
841 (Can_Never_Be_Null (R_Type)
842 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
843 Can_Never_Be_Null (R_Stm_Type)
844 then
845 Error_No_Match (Subtype_Ind);
846 end if;
848 -- AI05-103: for elementary types, subtypes must statically match
850 if Is_Constrained (R_Type)
851 or else Is_Access_Type (R_Type)
852 then
853 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
854 Error_No_Match (Subtype_Ind);
855 end if;
856 end if;
858 -- All remaining cases are illegal
860 -- Note: previous versions of this subprogram allowed the return
861 -- value to be the ancestor of the return type if the return type
862 -- was a null extension. This was plainly incorrect.
864 else
865 Error_Msg_N
866 ("wrong type for return_subtype_indication", Subtype_Ind);
867 end if;
868 end Check_Return_Subtype_Indication;
870 ---------------------
871 -- Local Variables --
872 ---------------------
874 Expr : Node_Id;
876 -- Start of processing for Analyze_Function_Return
878 begin
879 Set_Return_Present (Scope_Id);
881 if Nkind (N) = N_Simple_Return_Statement then
882 Expr := Expression (N);
884 -- Guard against a malformed expression. The parser may have tried to
885 -- recover but the node is not analyzable.
887 if Nkind (Expr) = N_Error then
888 Set_Etype (Expr, Any_Type);
889 Expander_Mode_Save_And_Set (False);
890 return;
892 else
893 -- The resolution of a controlled [extension] aggregate associated
894 -- with a return statement creates a temporary which needs to be
895 -- finalized on function exit. Wrap the return statement inside a
896 -- block so that the finalization machinery can detect this case.
897 -- This early expansion is done only when the return statement is
898 -- not part of a handled sequence of statements.
900 if Nkind_In (Expr, N_Aggregate,
901 N_Extension_Aggregate)
902 and then Needs_Finalization (R_Type)
903 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
904 then
905 Rewrite (N,
906 Make_Block_Statement (Loc,
907 Handled_Statement_Sequence =>
908 Make_Handled_Sequence_Of_Statements (Loc,
909 Statements => New_List (Relocate_Node (N)))));
911 Analyze (N);
912 return;
913 end if;
915 Analyze (Expr);
917 -- Ada 2005 (AI-251): If the type of the returned object is
918 -- an access to an interface type then we add an implicit type
919 -- conversion to force the displacement of the "this" pointer to
920 -- reference the secondary dispatch table. We cannot delay the
921 -- generation of this implicit conversion until the expansion
922 -- because in this case the type resolution changes the decoration
923 -- of the expression node to match R_Type; by contrast, if the
924 -- returned object is a class-wide interface type then it is too
925 -- early to generate here the implicit conversion since the return
926 -- statement may be rewritten by the expander into an extended
927 -- return statement whose expansion takes care of adding the
928 -- implicit type conversion to displace the pointer to the object.
930 if Expander_Active
931 and then Serious_Errors_Detected = 0
932 and then Is_Access_Type (R_Type)
933 and then Nkind (Expr) /= N_Null
934 and then Is_Interface (Designated_Type (R_Type))
935 and then Is_Progenitor (Designated_Type (R_Type),
936 Designated_Type (Etype (Expr)))
937 then
938 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
939 Analyze (Expr);
940 end if;
942 Resolve (Expr, R_Type);
943 Check_Limited_Return (Expr);
944 end if;
946 -- RETURN only allowed in SPARK as the last statement in function
948 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
949 and then
950 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
951 or else Present (Next (N)))
952 then
953 Check_SPARK_05_Restriction
954 ("RETURN should be the last statement in function", N);
955 end if;
957 else
958 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
960 -- Analyze parts specific to extended_return_statement:
962 declare
963 Obj_Decl : constant Node_Id :=
964 Last (Return_Object_Declarations (N));
965 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
966 HSS : constant Node_Id := Handled_Statement_Sequence (N);
968 begin
969 Expr := Expression (Obj_Decl);
971 -- Note: The check for OK_For_Limited_Init will happen in
972 -- Analyze_Object_Declaration; we treat it as a normal
973 -- object declaration.
975 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
976 Analyze (Obj_Decl);
978 Check_Return_Subtype_Indication (Obj_Decl);
980 if Present (HSS) then
981 Analyze (HSS);
983 if Present (Exception_Handlers (HSS)) then
985 -- ???Has_Nested_Block_With_Handler needs to be set.
986 -- Probably by creating an actual N_Block_Statement.
987 -- Probably in Expand.
989 null;
990 end if;
991 end if;
993 -- Mark the return object as referenced, since the return is an
994 -- implicit reference of the object.
996 Set_Referenced (Defining_Identifier (Obj_Decl));
998 Check_References (Stm_Entity);
1000 -- Check RM 6.5 (5.9/3)
1002 if Has_Aliased then
1003 if Ada_Version < Ada_2012 then
1005 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1006 -- Can it really happen (extended return???)
1008 Error_Msg_N
1009 ("aliased only allowed for limited return objects "
1010 & "in Ada 2012??", N);
1012 elsif not Is_Limited_View (R_Type) then
1013 Error_Msg_N
1014 ("aliased only allowed for limited return objects", N);
1015 end if;
1016 end if;
1017 end;
1018 end if;
1020 -- Case of Expr present
1022 if Present (Expr)
1024 -- Defend against previous errors
1026 and then Nkind (Expr) /= N_Empty
1027 and then Present (Etype (Expr))
1028 then
1029 -- Apply constraint check. Note that this is done before the implicit
1030 -- conversion of the expression done for anonymous access types to
1031 -- ensure correct generation of the null-excluding check associated
1032 -- with null-excluding expressions found in return statements.
1034 Apply_Constraint_Check (Expr, R_Type);
1036 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1037 -- type, apply an implicit conversion of the expression to that type
1038 -- to force appropriate static and run-time accessibility checks.
1040 if Ada_Version >= Ada_2005
1041 and then Ekind (R_Type) = E_Anonymous_Access_Type
1042 then
1043 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1044 Analyze_And_Resolve (Expr, R_Type);
1046 -- If this is a local anonymous access to subprogram, the
1047 -- accessibility check can be applied statically. The return is
1048 -- illegal if the access type of the return expression is declared
1049 -- inside of the subprogram (except if it is the subtype indication
1050 -- of an extended return statement).
1052 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1053 if not Comes_From_Source (Current_Scope)
1054 or else Ekind (Current_Scope) = E_Return_Statement
1055 then
1056 null;
1058 elsif
1059 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1060 then
1061 Error_Msg_N ("cannot return local access to subprogram", N);
1062 end if;
1064 -- The expression cannot be of a formal incomplete type
1066 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1067 and then Is_Generic_Type (Etype (Expr))
1068 then
1069 Error_Msg_N
1070 ("cannot return expression of a formal incomplete type", N);
1071 end if;
1073 -- If the result type is class-wide, then check that the return
1074 -- expression's type is not declared at a deeper level than the
1075 -- function (RM05-6.5(5.6/2)).
1077 if Ada_Version >= Ada_2005
1078 and then Is_Class_Wide_Type (R_Type)
1079 then
1080 if Type_Access_Level (Etype (Expr)) >
1081 Subprogram_Access_Level (Scope_Id)
1082 then
1083 Error_Msg_N
1084 ("level of return expression type is deeper than "
1085 & "class-wide function!", Expr);
1086 end if;
1087 end if;
1089 -- Check incorrect use of dynamically tagged expression
1091 if Is_Tagged_Type (R_Type) then
1092 Check_Dynamically_Tagged_Expression
1093 (Expr => Expr,
1094 Typ => R_Type,
1095 Related_Nod => N);
1096 end if;
1098 -- ??? A real run-time accessibility check is needed in cases
1099 -- involving dereferences of access parameters. For now we just
1100 -- check the static cases.
1102 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1103 and then Is_Limited_View (Etype (Scope_Id))
1104 and then Object_Access_Level (Expr) >
1105 Subprogram_Access_Level (Scope_Id)
1106 then
1107 -- Suppress the message in a generic, where the rewriting
1108 -- is irrelevant.
1110 if Inside_A_Generic then
1111 null;
1113 else
1114 Rewrite (N,
1115 Make_Raise_Program_Error (Loc,
1116 Reason => PE_Accessibility_Check_Failed));
1117 Analyze (N);
1119 Error_Msg_Warn := SPARK_Mode /= On;
1120 Error_Msg_N ("cannot return a local value by reference<<", N);
1121 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1122 end if;
1123 end if;
1125 if Known_Null (Expr)
1126 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1127 and then Null_Exclusion_Present (Parent (Scope_Id))
1128 then
1129 Apply_Compile_Time_Constraint_Error
1130 (N => Expr,
1131 Msg => "(Ada 2005) null not allowed for "
1132 & "null-excluding return??",
1133 Reason => CE_Null_Not_Allowed);
1134 end if;
1135 end if;
1136 end Analyze_Function_Return;
1138 -------------------------------------
1139 -- Analyze_Generic_Subprogram_Body --
1140 -------------------------------------
1142 procedure Analyze_Generic_Subprogram_Body
1143 (N : Node_Id;
1144 Gen_Id : Entity_Id)
1146 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1147 Kind : constant Entity_Kind := Ekind (Gen_Id);
1148 Body_Id : Entity_Id;
1149 New_N : Node_Id;
1150 Spec : Node_Id;
1152 begin
1153 -- Copy body and disable expansion while analyzing the generic For a
1154 -- stub, do not copy the stub (which would load the proper body), this
1155 -- will be done when the proper body is analyzed.
1157 if Nkind (N) /= N_Subprogram_Body_Stub then
1158 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1159 Rewrite (N, New_N);
1160 Start_Generic;
1161 end if;
1163 Spec := Specification (N);
1165 -- Within the body of the generic, the subprogram is callable, and
1166 -- behaves like the corresponding non-generic unit.
1168 Body_Id := Defining_Entity (Spec);
1170 if Kind = E_Generic_Procedure
1171 and then Nkind (Spec) /= N_Procedure_Specification
1172 then
1173 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1174 return;
1176 elsif Kind = E_Generic_Function
1177 and then Nkind (Spec) /= N_Function_Specification
1178 then
1179 Error_Msg_N ("invalid body for generic function ", Body_Id);
1180 return;
1181 end if;
1183 Set_Corresponding_Body (Gen_Decl, Body_Id);
1185 if Has_Completion (Gen_Id)
1186 and then Nkind (Parent (N)) /= N_Subunit
1187 then
1188 Error_Msg_N ("duplicate generic body", N);
1189 return;
1190 else
1191 Set_Has_Completion (Gen_Id);
1192 end if;
1194 if Nkind (N) = N_Subprogram_Body_Stub then
1195 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1196 else
1197 Set_Corresponding_Spec (N, Gen_Id);
1198 end if;
1200 if Nkind (Parent (N)) = N_Compilation_Unit then
1201 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1202 end if;
1204 -- Make generic parameters immediately visible in the body. They are
1205 -- needed to process the formals declarations. Then make the formals
1206 -- visible in a separate step.
1208 Push_Scope (Gen_Id);
1210 declare
1211 E : Entity_Id;
1212 First_Ent : Entity_Id;
1214 begin
1215 First_Ent := First_Entity (Gen_Id);
1217 E := First_Ent;
1218 while Present (E) and then not Is_Formal (E) loop
1219 Install_Entity (E);
1220 Next_Entity (E);
1221 end loop;
1223 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1225 -- Now generic formals are visible, and the specification can be
1226 -- analyzed, for subsequent conformance check.
1228 Body_Id := Analyze_Subprogram_Specification (Spec);
1230 -- Make formal parameters visible
1232 if Present (E) then
1234 -- E is the first formal parameter, we loop through the formals
1235 -- installing them so that they will be visible.
1237 Set_First_Entity (Gen_Id, E);
1238 while Present (E) loop
1239 Install_Entity (E);
1240 Next_Formal (E);
1241 end loop;
1242 end if;
1244 -- Visible generic entity is callable within its own body
1246 Set_Ekind (Gen_Id, Ekind (Body_Id));
1247 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1248 Set_Ekind (Body_Id, E_Subprogram_Body);
1249 Set_Convention (Body_Id, Convention (Gen_Id));
1250 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1251 Set_Scope (Body_Id, Scope (Gen_Id));
1253 -- Inherit the "ghostness" of the generic spec. Note that this
1254 -- property is not directly inherited as the body may be subject
1255 -- to a different Ghost assertion policy.
1257 if Is_Ghost_Entity (Gen_Id) or else Within_Ghost_Scope then
1258 Set_Is_Ghost_Entity (Body_Id);
1260 -- The Ghost policy in effect at the point of declaration and at
1261 -- the point of completion must match (SPARK RM 6.9(15)).
1263 Check_Ghost_Completion (Gen_Id, Body_Id);
1264 end if;
1266 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1268 if Nkind (N) = N_Subprogram_Body_Stub then
1270 -- No body to analyze, so restore state of generic unit
1272 Set_Ekind (Gen_Id, Kind);
1273 Set_Ekind (Body_Id, Kind);
1275 if Present (First_Ent) then
1276 Set_First_Entity (Gen_Id, First_Ent);
1277 end if;
1279 End_Scope;
1280 return;
1281 end if;
1283 -- If this is a compilation unit, it must be made visible explicitly,
1284 -- because the compilation of the declaration, unlike other library
1285 -- unit declarations, does not. If it is not a unit, the following
1286 -- is redundant but harmless.
1288 Set_Is_Immediately_Visible (Gen_Id);
1289 Reference_Body_Formals (Gen_Id, Body_Id);
1291 if Is_Child_Unit (Gen_Id) then
1292 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1293 end if;
1295 Set_Actual_Subtypes (N, Current_Scope);
1297 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1298 -- invariants and predicates associated with the body and its spec.
1299 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1300 -- prepares the contract assertions for generic subprograms or for
1301 -- ASIS. Do not generate contract checks in SPARK mode.
1303 if not GNATprove_Mode then
1304 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1305 end if;
1307 -- If the generic unit carries pre- or post-conditions, copy them
1308 -- to the original generic tree, so that they are properly added
1309 -- to any instantiation.
1311 declare
1312 Orig : constant Node_Id := Original_Node (N);
1313 Cond : Node_Id;
1315 begin
1316 Cond := First (Declarations (N));
1317 while Present (Cond) loop
1318 if Nkind (Cond) = N_Pragma
1319 and then Pragma_Name (Cond) = Name_Check
1320 then
1321 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1323 elsif Nkind (Cond) = N_Pragma
1324 and then Pragma_Name (Cond) = Name_Postcondition
1325 then
1326 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1327 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1328 else
1329 exit;
1330 end if;
1332 Next (Cond);
1333 end loop;
1334 end;
1336 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1337 Set_SPARK_Pragma_Inherited (Body_Id, True);
1339 Analyze_Declarations (Declarations (N));
1340 Check_Completion;
1341 Analyze (Handled_Statement_Sequence (N));
1343 Save_Global_References (Original_Node (N));
1345 -- Prior to exiting the scope, include generic formals again (if any
1346 -- are present) in the set of local entities.
1348 if Present (First_Ent) then
1349 Set_First_Entity (Gen_Id, First_Ent);
1350 end if;
1352 Check_References (Gen_Id);
1353 end;
1355 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1356 End_Scope;
1357 Check_Subprogram_Order (N);
1359 -- Outside of its body, unit is generic again
1361 Set_Ekind (Gen_Id, Kind);
1362 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1364 if Style_Check then
1365 Style.Check_Identifier (Body_Id, Gen_Id);
1366 end if;
1368 End_Generic;
1369 end Analyze_Generic_Subprogram_Body;
1371 ----------------------------
1372 -- Analyze_Null_Procedure --
1373 ----------------------------
1375 procedure Analyze_Null_Procedure
1376 (N : Node_Id;
1377 Is_Completion : out Boolean)
1379 Loc : constant Source_Ptr := Sloc (N);
1380 Spec : constant Node_Id := Specification (N);
1381 Designator : Entity_Id;
1382 Form : Node_Id;
1383 Null_Body : Node_Id := Empty;
1384 Prev : Entity_Id;
1386 begin
1387 -- Capture the profile of the null procedure before analysis, for
1388 -- expansion at the freeze point and at each point of call. The body is
1389 -- used if the procedure has preconditions, or if it is a completion. In
1390 -- the first case the body is analyzed at the freeze point, in the other
1391 -- it replaces the null procedure declaration.
1393 Null_Body :=
1394 Make_Subprogram_Body (Loc,
1395 Specification => New_Copy_Tree (Spec),
1396 Declarations => New_List,
1397 Handled_Statement_Sequence =>
1398 Make_Handled_Sequence_Of_Statements (Loc,
1399 Statements => New_List (Make_Null_Statement (Loc))));
1401 -- Create new entities for body and formals
1403 Set_Defining_Unit_Name (Specification (Null_Body),
1404 Make_Defining_Identifier
1405 (Sloc (Defining_Entity (N)),
1406 Chars (Defining_Entity (N))));
1408 Form := First (Parameter_Specifications (Specification (Null_Body)));
1409 while Present (Form) loop
1410 Set_Defining_Identifier (Form,
1411 Make_Defining_Identifier
1412 (Sloc (Defining_Identifier (Form)),
1413 Chars (Defining_Identifier (Form))));
1414 Next (Form);
1415 end loop;
1417 -- Determine whether the null procedure may be a completion of a generic
1418 -- suprogram, in which case we use the new null body as the completion
1419 -- and set minimal semantic information on the original declaration,
1420 -- which is rewritten as a null statement.
1422 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1424 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1425 Insert_Before (N, Null_Body);
1426 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1427 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1429 Rewrite (N, Make_Null_Statement (Loc));
1430 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1431 Is_Completion := True;
1432 return;
1434 else
1435 -- Resolve the types of the formals now, because the freeze point
1436 -- may appear in a different context, e.g. an instantiation.
1438 Form := First (Parameter_Specifications (Specification (Null_Body)));
1439 while Present (Form) loop
1440 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1441 Find_Type (Parameter_Type (Form));
1443 elsif
1444 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1445 then
1446 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1448 else
1449 -- The case of a null procedure with a formal that is an
1450 -- access_to_subprogram type, and that is used as an actual
1451 -- in an instantiation is left to the enthusiastic reader.
1453 null;
1454 end if;
1456 Next (Form);
1457 end loop;
1458 end if;
1460 -- If there are previous overloadable entities with the same name,
1461 -- check whether any of them is completed by the null procedure.
1463 if Present (Prev) and then Is_Overloadable (Prev) then
1464 Designator := Analyze_Subprogram_Specification (Spec);
1465 Prev := Find_Corresponding_Spec (N);
1466 end if;
1468 if No (Prev) or else not Comes_From_Source (Prev) then
1469 Designator := Analyze_Subprogram_Specification (Spec);
1470 Set_Has_Completion (Designator);
1472 -- Signal to caller that this is a procedure declaration
1474 Is_Completion := False;
1476 -- Null procedures are always inlined, but generic formal subprograms
1477 -- which appear as such in the internal instance of formal packages,
1478 -- need no completion and are not marked Inline.
1480 if Expander_Active
1481 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1482 then
1483 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1484 Set_Body_To_Inline (N, Null_Body);
1485 Set_Is_Inlined (Designator);
1486 end if;
1488 else
1489 -- The null procedure is a completion. We unconditionally rewrite
1490 -- this as a null body (even if expansion is not active), because
1491 -- there are various error checks that are applied on this body
1492 -- when it is analyzed (e.g. correct aspect placement).
1494 if Has_Completion (Prev) then
1495 Error_Msg_Sloc := Sloc (Prev);
1496 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1497 end if;
1499 Is_Completion := True;
1500 Rewrite (N, Null_Body);
1501 Analyze (N);
1502 end if;
1503 end Analyze_Null_Procedure;
1505 -----------------------------
1506 -- Analyze_Operator_Symbol --
1507 -----------------------------
1509 -- An operator symbol such as "+" or "and" may appear in context where the
1510 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1511 -- is just a string, as in (conjunction = "or"). In these cases the parser
1512 -- generates this node, and the semantics does the disambiguation. Other
1513 -- such case are actuals in an instantiation, the generic unit in an
1514 -- instantiation, and pragma arguments.
1516 procedure Analyze_Operator_Symbol (N : Node_Id) is
1517 Par : constant Node_Id := Parent (N);
1519 begin
1520 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1521 or else Nkind (Par) = N_Function_Instantiation
1522 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1523 or else (Nkind (Par) = N_Pragma_Argument_Association
1524 and then not Is_Pragma_String_Literal (Par))
1525 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1526 or else (Nkind (Par) = N_Attribute_Reference
1527 and then Attribute_Name (Par) /= Name_Value)
1528 then
1529 Find_Direct_Name (N);
1531 else
1532 Change_Operator_Symbol_To_String_Literal (N);
1533 Analyze (N);
1534 end if;
1535 end Analyze_Operator_Symbol;
1537 -----------------------------------
1538 -- Analyze_Parameter_Association --
1539 -----------------------------------
1541 procedure Analyze_Parameter_Association (N : Node_Id) is
1542 begin
1543 Analyze (Explicit_Actual_Parameter (N));
1544 end Analyze_Parameter_Association;
1546 ----------------------------
1547 -- Analyze_Procedure_Call --
1548 ----------------------------
1550 procedure Analyze_Procedure_Call (N : Node_Id) is
1551 Loc : constant Source_Ptr := Sloc (N);
1552 P : constant Node_Id := Name (N);
1553 Actuals : constant List_Id := Parameter_Associations (N);
1554 Actual : Node_Id;
1555 New_N : Node_Id;
1557 procedure Analyze_Call_And_Resolve;
1558 -- Do Analyze and Resolve calls for procedure call
1559 -- At end, check illegal order dependence.
1561 ------------------------------
1562 -- Analyze_Call_And_Resolve --
1563 ------------------------------
1565 procedure Analyze_Call_And_Resolve is
1566 begin
1567 if Nkind (N) = N_Procedure_Call_Statement then
1568 Analyze_Call (N);
1569 Resolve (N, Standard_Void_Type);
1570 else
1571 Analyze (N);
1572 end if;
1573 end Analyze_Call_And_Resolve;
1575 -- Start of processing for Analyze_Procedure_Call
1577 begin
1578 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1579 -- a procedure call or an entry call. The prefix may denote an access
1580 -- to subprogram type, in which case an implicit dereference applies.
1581 -- If the prefix is an indexed component (without implicit dereference)
1582 -- then the construct denotes a call to a member of an entire family.
1583 -- If the prefix is a simple name, it may still denote a call to a
1584 -- parameterless member of an entry family. Resolution of these various
1585 -- interpretations is delicate.
1587 Analyze (P);
1589 -- If this is a call of the form Obj.Op, the call may have been
1590 -- analyzed and possibly rewritten into a block, in which case
1591 -- we are done.
1593 if Analyzed (N) then
1594 return;
1595 end if;
1597 -- If there is an error analyzing the name (which may have been
1598 -- rewritten if the original call was in prefix notation) then error
1599 -- has been emitted already, mark node and return.
1601 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1602 Set_Etype (N, Any_Type);
1603 return;
1604 end if;
1606 -- Otherwise analyze the parameters
1608 if Present (Actuals) then
1609 Actual := First (Actuals);
1611 while Present (Actual) loop
1612 Analyze (Actual);
1613 Check_Parameterless_Call (Actual);
1614 Next (Actual);
1615 end loop;
1616 end if;
1618 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1620 if Nkind (P) = N_Attribute_Reference
1621 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1622 Name_Elab_Body,
1623 Name_Elab_Subp_Body)
1624 then
1625 if Present (Actuals) then
1626 Error_Msg_N
1627 ("no parameters allowed for this call", First (Actuals));
1628 return;
1629 end if;
1631 Set_Etype (N, Standard_Void_Type);
1632 Set_Analyzed (N);
1634 elsif Is_Entity_Name (P)
1635 and then Is_Record_Type (Etype (Entity (P)))
1636 and then Remote_AST_I_Dereference (P)
1637 then
1638 return;
1640 elsif Is_Entity_Name (P)
1641 and then Ekind (Entity (P)) /= E_Entry_Family
1642 then
1643 if Is_Access_Type (Etype (P))
1644 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1645 and then No (Actuals)
1646 and then Comes_From_Source (N)
1647 then
1648 Error_Msg_N ("missing explicit dereference in call", N);
1649 end if;
1651 Analyze_Call_And_Resolve;
1653 -- If the prefix is the simple name of an entry family, this is
1654 -- a parameterless call from within the task body itself.
1656 elsif Is_Entity_Name (P)
1657 and then Nkind (P) = N_Identifier
1658 and then Ekind (Entity (P)) = E_Entry_Family
1659 and then Present (Actuals)
1660 and then No (Next (First (Actuals)))
1661 then
1662 -- Can be call to parameterless entry family. What appears to be the
1663 -- sole argument is in fact the entry index. Rewrite prefix of node
1664 -- accordingly. Source representation is unchanged by this
1665 -- transformation.
1667 New_N :=
1668 Make_Indexed_Component (Loc,
1669 Prefix =>
1670 Make_Selected_Component (Loc,
1671 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1672 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1673 Expressions => Actuals);
1674 Set_Name (N, New_N);
1675 Set_Etype (New_N, Standard_Void_Type);
1676 Set_Parameter_Associations (N, No_List);
1677 Analyze_Call_And_Resolve;
1679 elsif Nkind (P) = N_Explicit_Dereference then
1680 if Ekind (Etype (P)) = E_Subprogram_Type then
1681 Analyze_Call_And_Resolve;
1682 else
1683 Error_Msg_N ("expect access to procedure in call", P);
1684 end if;
1686 -- The name can be a selected component or an indexed component that
1687 -- yields an access to subprogram. Such a prefix is legal if the call
1688 -- has parameter associations.
1690 elsif Is_Access_Type (Etype (P))
1691 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1692 then
1693 if Present (Actuals) then
1694 Analyze_Call_And_Resolve;
1695 else
1696 Error_Msg_N ("missing explicit dereference in call ", N);
1697 end if;
1699 -- If not an access to subprogram, then the prefix must resolve to the
1700 -- name of an entry, entry family, or protected operation.
1702 -- For the case of a simple entry call, P is a selected component where
1703 -- the prefix is the task and the selector name is the entry. A call to
1704 -- a protected procedure will have the same syntax. If the protected
1705 -- object contains overloaded operations, the entity may appear as a
1706 -- function, the context will select the operation whose type is Void.
1708 elsif Nkind (P) = N_Selected_Component
1709 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1710 E_Procedure,
1711 E_Function)
1712 then
1713 Analyze_Call_And_Resolve;
1715 elsif Nkind (P) = N_Selected_Component
1716 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1717 and then Present (Actuals)
1718 and then No (Next (First (Actuals)))
1719 then
1720 -- Can be call to parameterless entry family. What appears to be the
1721 -- sole argument is in fact the entry index. Rewrite prefix of node
1722 -- accordingly. Source representation is unchanged by this
1723 -- transformation.
1725 New_N :=
1726 Make_Indexed_Component (Loc,
1727 Prefix => New_Copy (P),
1728 Expressions => Actuals);
1729 Set_Name (N, New_N);
1730 Set_Etype (New_N, Standard_Void_Type);
1731 Set_Parameter_Associations (N, No_List);
1732 Analyze_Call_And_Resolve;
1734 -- For the case of a reference to an element of an entry family, P is
1735 -- an indexed component whose prefix is a selected component (task and
1736 -- entry family), and whose index is the entry family index.
1738 elsif Nkind (P) = N_Indexed_Component
1739 and then Nkind (Prefix (P)) = N_Selected_Component
1740 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1741 then
1742 Analyze_Call_And_Resolve;
1744 -- If the prefix is the name of an entry family, it is a call from
1745 -- within the task body itself.
1747 elsif Nkind (P) = N_Indexed_Component
1748 and then Nkind (Prefix (P)) = N_Identifier
1749 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1750 then
1751 New_N :=
1752 Make_Selected_Component (Loc,
1753 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1754 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1755 Rewrite (Prefix (P), New_N);
1756 Analyze (P);
1757 Analyze_Call_And_Resolve;
1759 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1760 -- procedure name, so the construct can only be a qualified expression.
1762 elsif Nkind (P) = N_Qualified_Expression
1763 and then Ada_Version >= Ada_2012
1764 then
1765 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1766 Analyze (N);
1768 -- Anything else is an error
1770 else
1771 Error_Msg_N ("invalid procedure or entry call", N);
1772 end if;
1773 end Analyze_Procedure_Call;
1775 ------------------------------
1776 -- Analyze_Return_Statement --
1777 ------------------------------
1779 procedure Analyze_Return_Statement (N : Node_Id) is
1781 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1782 N_Extended_Return_Statement));
1784 Returns_Object : constant Boolean :=
1785 Nkind (N) = N_Extended_Return_Statement
1786 or else
1787 (Nkind (N) = N_Simple_Return_Statement
1788 and then Present (Expression (N)));
1789 -- True if we're returning something; that is, "return <expression>;"
1790 -- or "return Result : T [:= ...]". False for "return;". Used for error
1791 -- checking: If Returns_Object is True, N should apply to a function
1792 -- body; otherwise N should apply to a procedure body, entry body,
1793 -- accept statement, or extended return statement.
1795 function Find_What_It_Applies_To return Entity_Id;
1796 -- Find the entity representing the innermost enclosing body, accept
1797 -- statement, or extended return statement. If the result is a callable
1798 -- construct or extended return statement, then this will be the value
1799 -- of the Return_Applies_To attribute. Otherwise, the program is
1800 -- illegal. See RM-6.5(4/2).
1802 -----------------------------
1803 -- Find_What_It_Applies_To --
1804 -----------------------------
1806 function Find_What_It_Applies_To return Entity_Id is
1807 Result : Entity_Id := Empty;
1809 begin
1810 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1811 -- and postconditions.
1813 for J in reverse 0 .. Scope_Stack.Last loop
1814 Result := Scope_Stack.Table (J).Entity;
1815 exit when not Ekind_In (Result, E_Block, E_Loop)
1816 and then Chars (Result) /= Name_uPostconditions;
1817 end loop;
1819 pragma Assert (Present (Result));
1820 return Result;
1821 end Find_What_It_Applies_To;
1823 -- Local declarations
1825 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1826 Kind : constant Entity_Kind := Ekind (Scope_Id);
1827 Loc : constant Source_Ptr := Sloc (N);
1828 Stm_Entity : constant Entity_Id :=
1829 New_Internal_Entity
1830 (E_Return_Statement, Current_Scope, Loc, 'R');
1832 -- Start of processing for Analyze_Return_Statement
1834 begin
1835 Set_Return_Statement_Entity (N, Stm_Entity);
1837 Set_Etype (Stm_Entity, Standard_Void_Type);
1838 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1840 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1841 -- (4/2): an inner return statement will apply to this extended return.
1843 if Nkind (N) = N_Extended_Return_Statement then
1844 Push_Scope (Stm_Entity);
1845 end if;
1847 -- Check that pragma No_Return is obeyed. Don't complain about the
1848 -- implicitly-generated return that is placed at the end.
1850 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1851 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1852 end if;
1854 -- Warn on any unassigned OUT parameters if in procedure
1856 if Ekind (Scope_Id) = E_Procedure then
1857 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1858 end if;
1860 -- Check that functions return objects, and other things do not
1862 if Kind = E_Function or else Kind = E_Generic_Function then
1863 if not Returns_Object then
1864 Error_Msg_N ("missing expression in return from function", N);
1865 end if;
1867 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1868 if Returns_Object then
1869 Error_Msg_N ("procedure cannot return value (use function)", N);
1870 end if;
1872 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1873 if Returns_Object then
1874 if Is_Protected_Type (Scope (Scope_Id)) then
1875 Error_Msg_N ("entry body cannot return value", N);
1876 else
1877 Error_Msg_N ("accept statement cannot return value", N);
1878 end if;
1879 end if;
1881 elsif Kind = E_Return_Statement then
1883 -- We are nested within another return statement, which must be an
1884 -- extended_return_statement.
1886 if Returns_Object then
1887 if Nkind (N) = N_Extended_Return_Statement then
1888 Error_Msg_N
1889 ("extended return statement cannot be nested (use `RETURN;`)",
1892 -- Case of a simple return statement with a value inside extended
1893 -- return statement.
1895 else
1896 Error_Msg_N
1897 ("return nested in extended return statement cannot return "
1898 & "value (use `RETURN;`)", N);
1899 end if;
1900 end if;
1902 else
1903 Error_Msg_N ("illegal context for return statement", N);
1904 end if;
1906 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1907 Analyze_Function_Return (N);
1909 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1910 Set_Return_Present (Scope_Id);
1911 end if;
1913 if Nkind (N) = N_Extended_Return_Statement then
1914 End_Scope;
1915 end if;
1917 Kill_Current_Values (Last_Assignment_Only => True);
1918 Check_Unreachable_Code (N);
1920 Analyze_Dimension (N);
1921 end Analyze_Return_Statement;
1923 -------------------------------------
1924 -- Analyze_Simple_Return_Statement --
1925 -------------------------------------
1927 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1928 begin
1929 if Present (Expression (N)) then
1930 Mark_Coextensions (N, Expression (N));
1931 end if;
1933 Analyze_Return_Statement (N);
1934 end Analyze_Simple_Return_Statement;
1936 -------------------------
1937 -- Analyze_Return_Type --
1938 -------------------------
1940 procedure Analyze_Return_Type (N : Node_Id) is
1941 Designator : constant Entity_Id := Defining_Entity (N);
1942 Typ : Entity_Id := Empty;
1944 begin
1945 -- Normal case where result definition does not indicate an error
1947 if Result_Definition (N) /= Error then
1948 if Nkind (Result_Definition (N)) = N_Access_Definition then
1949 Check_SPARK_05_Restriction
1950 ("access result is not allowed", Result_Definition (N));
1952 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1954 declare
1955 AD : constant Node_Id :=
1956 Access_To_Subprogram_Definition (Result_Definition (N));
1957 begin
1958 if Present (AD) and then Protected_Present (AD) then
1959 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1960 else
1961 Typ := Access_Definition (N, Result_Definition (N));
1962 end if;
1963 end;
1965 Set_Parent (Typ, Result_Definition (N));
1966 Set_Is_Local_Anonymous_Access (Typ);
1967 Set_Etype (Designator, Typ);
1969 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1971 Null_Exclusion_Static_Checks (N);
1973 -- Subtype_Mark case
1975 else
1976 Find_Type (Result_Definition (N));
1977 Typ := Entity (Result_Definition (N));
1978 Set_Etype (Designator, Typ);
1980 -- Unconstrained array as result is not allowed in SPARK
1982 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1983 Check_SPARK_05_Restriction
1984 ("returning an unconstrained array is not allowed",
1985 Result_Definition (N));
1986 end if;
1988 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1990 Null_Exclusion_Static_Checks (N);
1992 -- If a null exclusion is imposed on the result type, then create
1993 -- a null-excluding itype (an access subtype) and use it as the
1994 -- function's Etype. Note that the null exclusion checks are done
1995 -- right before this, because they don't get applied to types that
1996 -- do not come from source.
1998 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1999 Set_Etype (Designator,
2000 Create_Null_Excluding_Itype
2001 (T => Typ,
2002 Related_Nod => N,
2003 Scope_Id => Scope (Current_Scope)));
2005 -- The new subtype must be elaborated before use because
2006 -- it is visible outside of the function. However its base
2007 -- type may not be frozen yet, so the reference that will
2008 -- force elaboration must be attached to the freezing of
2009 -- the base type.
2011 -- If the return specification appears on a proper body,
2012 -- the subtype will have been created already on the spec.
2014 if Is_Frozen (Typ) then
2015 if Nkind (Parent (N)) = N_Subprogram_Body
2016 and then Nkind (Parent (Parent (N))) = N_Subunit
2017 then
2018 null;
2019 else
2020 Build_Itype_Reference (Etype (Designator), Parent (N));
2021 end if;
2023 else
2024 Ensure_Freeze_Node (Typ);
2026 declare
2027 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2028 begin
2029 Set_Itype (IR, Etype (Designator));
2030 Append_Freeze_Actions (Typ, New_List (IR));
2031 end;
2032 end if;
2034 else
2035 Set_Etype (Designator, Typ);
2036 end if;
2038 if Ekind (Typ) = E_Incomplete_Type
2039 and then Is_Value_Type (Typ)
2040 then
2041 null;
2043 elsif Ekind (Typ) = E_Incomplete_Type
2044 or else (Is_Class_Wide_Type (Typ)
2045 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2046 then
2047 -- AI05-0151: Tagged incomplete types are allowed in all formal
2048 -- parts. Untagged incomplete types are not allowed in bodies.
2049 -- As a consequence, limited views cannot appear in a basic
2050 -- declaration that is itself within a body, because there is
2051 -- no point at which the non-limited view will become visible.
2053 if Ada_Version >= Ada_2012 then
2054 if From_Limited_With (Typ) and then In_Package_Body then
2055 Error_Msg_NE
2056 ("invalid use of incomplete type&",
2057 Result_Definition (N), Typ);
2059 -- The return type of a subprogram body cannot be of a
2060 -- formal incomplete type.
2062 elsif Is_Generic_Type (Typ)
2063 and then Nkind (Parent (N)) = N_Subprogram_Body
2064 then
2065 Error_Msg_N
2066 ("return type cannot be a formal incomplete type",
2067 Result_Definition (N));
2069 elsif Is_Class_Wide_Type (Typ)
2070 and then Is_Generic_Type (Root_Type (Typ))
2071 and then Nkind (Parent (N)) = N_Subprogram_Body
2072 then
2073 Error_Msg_N
2074 ("return type cannot be a formal incomplete type",
2075 Result_Definition (N));
2077 elsif Is_Tagged_Type (Typ) then
2078 null;
2080 elsif Nkind (Parent (N)) = N_Subprogram_Body
2081 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2082 N_Entry_Body)
2083 then
2084 Error_Msg_NE
2085 ("invalid use of untagged incomplete type&",
2086 Designator, Typ);
2087 end if;
2089 -- The type must be completed in the current package. This
2090 -- is checked at the end of the package declaration when
2091 -- Taft-amendment types are identified. If the return type
2092 -- is class-wide, there is no required check, the type can
2093 -- be a bona fide TAT.
2095 if Ekind (Scope (Current_Scope)) = E_Package
2096 and then In_Private_Part (Scope (Current_Scope))
2097 and then not Is_Class_Wide_Type (Typ)
2098 then
2099 Append_Elmt (Designator, Private_Dependents (Typ));
2100 end if;
2102 else
2103 Error_Msg_NE
2104 ("invalid use of incomplete type&", Designator, Typ);
2105 end if;
2106 end if;
2107 end if;
2109 -- Case where result definition does indicate an error
2111 else
2112 Set_Etype (Designator, Any_Type);
2113 end if;
2114 end Analyze_Return_Type;
2116 -----------------------------
2117 -- Analyze_Subprogram_Body --
2118 -----------------------------
2120 procedure Analyze_Subprogram_Body (N : Node_Id) is
2121 Loc : constant Source_Ptr := Sloc (N);
2122 Body_Spec : constant Node_Id := Specification (N);
2123 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2125 begin
2126 if Debug_Flag_C then
2127 Write_Str ("==> subprogram body ");
2128 Write_Name (Chars (Body_Id));
2129 Write_Str (" from ");
2130 Write_Location (Loc);
2131 Write_Eol;
2132 Indent;
2133 end if;
2135 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2137 -- The real work is split out into the helper, so it can do "return;"
2138 -- without skipping the debug output:
2140 Analyze_Subprogram_Body_Helper (N);
2142 if Debug_Flag_C then
2143 Outdent;
2144 Write_Str ("<== subprogram body ");
2145 Write_Name (Chars (Body_Id));
2146 Write_Str (" from ");
2147 Write_Location (Loc);
2148 Write_Eol;
2149 end if;
2150 end Analyze_Subprogram_Body;
2152 --------------------------------------
2153 -- Analyze_Subprogram_Body_Contract --
2154 --------------------------------------
2156 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2157 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2158 Mode : SPARK_Mode_Type;
2159 Prag : Node_Id;
2160 Ref_Depends : Node_Id := Empty;
2161 Ref_Global : Node_Id := Empty;
2162 Spec_Id : Entity_Id;
2164 begin
2165 -- Due to the timing of contract analysis, delayed pragmas may be
2166 -- subject to the wrong SPARK_Mode, usually that of the enclosing
2167 -- context. To remedy this, restore the original SPARK_Mode of the
2168 -- related subprogram body.
2170 Save_SPARK_Mode_And_Set (Body_Id, Mode);
2172 -- When a subprogram body declaration is illegal, its defining entity is
2173 -- left unanalyzed. There is nothing left to do in this case because the
2174 -- body lacks a contract, or even a proper Ekind.
2176 if Ekind (Body_Id) = E_Void then
2177 return;
2178 end if;
2180 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2181 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2182 else
2183 Spec_Id := Corresponding_Spec (Body_Decl);
2184 end if;
2186 -- Locate and store pragmas Refined_Depends and Refined_Global since
2187 -- their order of analysis matters.
2189 Prag := Classifications (Contract (Body_Id));
2190 while Present (Prag) loop
2191 if Pragma_Name (Prag) = Name_Refined_Depends then
2192 Ref_Depends := Prag;
2193 elsif Pragma_Name (Prag) = Name_Refined_Global then
2194 Ref_Global := Prag;
2195 end if;
2197 Prag := Next_Pragma (Prag);
2198 end loop;
2200 -- Analyze Refined_Global first as Refined_Depends may mention items
2201 -- classified in the global refinement.
2203 if Present (Ref_Global) then
2204 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2206 -- When the corresponding Global aspect/pragma references a state with
2207 -- visible refinement, the body requires Refined_Global. Refinement is
2208 -- not required when SPARK checks are suppressed.
2210 elsif Present (Spec_Id) then
2211 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2213 if SPARK_Mode /= Off
2214 and then Present (Prag)
2215 and then Contains_Refined_State (Prag)
2216 then
2217 Error_Msg_NE
2218 ("body of subprogram& requires global refinement",
2219 Body_Decl, Spec_Id);
2220 end if;
2221 end if;
2223 -- Refined_Depends must be analyzed after Refined_Global in order to see
2224 -- the modes of all global refinements.
2226 if Present (Ref_Depends) then
2227 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2229 -- When the corresponding Depends aspect/pragma references a state with
2230 -- visible refinement, the body requires Refined_Depends. Refinement is
2231 -- not required when SPARK checks are suppressed.
2233 elsif Present (Spec_Id) then
2234 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2236 if SPARK_Mode /= Off
2237 and then Present (Prag)
2238 and then Contains_Refined_State (Prag)
2239 then
2240 Error_Msg_NE
2241 ("body of subprogram& requires dependance refinement",
2242 Body_Decl, Spec_Id);
2243 end if;
2244 end if;
2246 -- Restore the SPARK_Mode of the enclosing context after all delayed
2247 -- pragmas have been analyzed.
2249 Restore_SPARK_Mode (Mode);
2250 end Analyze_Subprogram_Body_Contract;
2252 ------------------------------------
2253 -- Analyze_Subprogram_Body_Helper --
2254 ------------------------------------
2256 -- This procedure is called for regular subprogram bodies, generic bodies,
2257 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2258 -- specification matters, and is used to create a proper declaration for
2259 -- the subprogram, or to perform conformance checks.
2261 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2262 Loc : constant Source_Ptr := Sloc (N);
2263 Body_Spec : constant Node_Id := Specification (N);
2264 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2265 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2266 Conformant : Boolean;
2267 HSS : Node_Id;
2268 Prot_Typ : Entity_Id := Empty;
2269 Spec_Id : Entity_Id;
2270 Spec_Decl : Node_Id := Empty;
2272 Last_Real_Spec_Entity : Entity_Id := Empty;
2273 -- When we analyze a separate spec, the entity chain ends up containing
2274 -- the formals, as well as any itypes generated during analysis of the
2275 -- default expressions for parameters, or the arguments of associated
2276 -- precondition/postcondition pragmas (which are analyzed in the context
2277 -- of the spec since they have visibility on formals).
2279 -- These entities belong with the spec and not the body. However we do
2280 -- the analysis of the body in the context of the spec (again to obtain
2281 -- visibility to the formals), and all the entities generated during
2282 -- this analysis end up also chained to the entity chain of the spec.
2283 -- But they really belong to the body, and there is circuitry to move
2284 -- them from the spec to the body.
2286 -- However, when we do this move, we don't want to move the real spec
2287 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2288 -- variable points to the last real spec entity, so we only move those
2289 -- chained beyond that point. It is initialized to Empty to deal with
2290 -- the case where there is no separate spec.
2292 procedure Analyze_Aspects_On_Body_Or_Stub;
2293 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2294 -- assumed that N has aspects.
2296 function Body_Has_Contract return Boolean;
2297 -- Check whether unanalyzed body has an aspect or pragma that may
2298 -- generate a SPARK contract.
2300 procedure Check_Anonymous_Return;
2301 -- Ada 2005: if a function returns an access type that denotes a task,
2302 -- or a type that contains tasks, we must create a master entity for
2303 -- the anonymous type, which typically will be used in an allocator
2304 -- in the body of the function.
2306 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2307 -- Look ahead to recognize a pragma that may appear after the body.
2308 -- If there is a previous spec, check that it appears in the same
2309 -- declarative part. If the pragma is Inline_Always, perform inlining
2310 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2311 -- If the body acts as a spec, and inlining is required, we create a
2312 -- subprogram declaration for it, in order to attach the body to inline.
2313 -- If pragma does not appear after the body, check whether there is
2314 -- an inline pragma before any local declarations.
2316 procedure Check_Missing_Return;
2317 -- Checks for a function with a no return statements, and also performs
2318 -- the warning checks implemented by Check_Returns. In formal mode, also
2319 -- verify that a function ends with a RETURN and that a procedure does
2320 -- not contain any RETURN.
2322 function Disambiguate_Spec return Entity_Id;
2323 -- When a primitive is declared between the private view and the full
2324 -- view of a concurrent type which implements an interface, a special
2325 -- mechanism is used to find the corresponding spec of the primitive
2326 -- body.
2328 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2329 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2330 -- incomplete types coming from a limited context and swap their limited
2331 -- views with the non-limited ones.
2333 function Is_Private_Concurrent_Primitive
2334 (Subp_Id : Entity_Id) return Boolean;
2335 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2336 -- type that implements an interface and has a private view.
2338 procedure Set_Trivial_Subprogram (N : Node_Id);
2339 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2340 -- subprogram whose body is being analyzed. N is the statement node
2341 -- causing the flag to be set, if the following statement is a return
2342 -- of an entity, we mark the entity as set in source to suppress any
2343 -- warning on the stylized use of function stubs with a dummy return.
2345 procedure Verify_Overriding_Indicator;
2346 -- If there was a previous spec, the entity has been entered in the
2347 -- current scope previously. If the body itself carries an overriding
2348 -- indicator, check that it is consistent with the known status of the
2349 -- entity.
2351 -------------------------------------
2352 -- Analyze_Aspects_On_Body_Or_Stub --
2353 -------------------------------------
2355 procedure Analyze_Aspects_On_Body_Or_Stub is
2356 procedure Diagnose_Misplaced_Aspects;
2357 -- Subprogram body [stub] N has aspects, but they are not properly
2358 -- placed. Provide precise diagnostics depending on the aspects
2359 -- involved.
2361 --------------------------------
2362 -- Diagnose_Misplaced_Aspects --
2363 --------------------------------
2365 procedure Diagnose_Misplaced_Aspects is
2366 Asp : Node_Id;
2367 Asp_Nam : Name_Id;
2368 Asp_Id : Aspect_Id;
2369 -- The current aspect along with its name and id
2371 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2372 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2373 -- the name of the refined version of the aspect.
2375 ------------------------
2376 -- SPARK_Aspect_Error --
2377 ------------------------
2379 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2380 begin
2381 -- The corresponding spec already contains the aspect in
2382 -- question and the one appearing on the body must be the
2383 -- refined form:
2385 -- procedure P with Global ...;
2386 -- procedure P with Global ... is ... end P;
2387 -- ^
2388 -- Refined_Global
2390 if Has_Aspect (Spec_Id, Asp_Id) then
2391 Error_Msg_Name_1 := Asp_Nam;
2393 -- Subunits cannot carry aspects that apply to a subprogram
2394 -- declaration.
2396 if Nkind (Parent (N)) = N_Subunit then
2397 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2399 else
2400 Error_Msg_Name_2 := Ref_Nam;
2401 Error_Msg_N ("aspect % should be %", Asp);
2402 end if;
2404 -- Otherwise the aspect must appear in the spec, not in the
2405 -- body:
2407 -- procedure P;
2408 -- procedure P with Global ... is ... end P;
2410 else
2411 Error_Msg_N
2412 ("aspect specification must appear in subprogram "
2413 & "declaration", Asp);
2414 end if;
2415 end SPARK_Aspect_Error;
2417 -- Start of processing for Diagnose_Misplaced_Aspects
2419 begin
2420 -- Iterate over the aspect specifications and emit specific errors
2421 -- where applicable.
2423 Asp := First (Aspect_Specifications (N));
2424 while Present (Asp) loop
2425 Asp_Nam := Chars (Identifier (Asp));
2426 Asp_Id := Get_Aspect_Id (Asp_Nam);
2428 -- Do not emit errors on aspects that can appear on a
2429 -- subprogram body. This scenario occurs when the aspect
2430 -- specification list contains both misplaced and properly
2431 -- placed aspects.
2433 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2434 null;
2436 -- Special diagnostics for SPARK aspects
2438 elsif Asp_Nam = Name_Depends then
2439 SPARK_Aspect_Error (Name_Refined_Depends);
2441 elsif Asp_Nam = Name_Global then
2442 SPARK_Aspect_Error (Name_Refined_Global);
2444 elsif Asp_Nam = Name_Post then
2445 SPARK_Aspect_Error (Name_Refined_Post);
2447 else
2448 Error_Msg_N
2449 ("aspect specification must appear in subprogram "
2450 & "declaration", Asp);
2451 end if;
2453 Next (Asp);
2454 end loop;
2455 end Diagnose_Misplaced_Aspects;
2457 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2459 begin
2460 -- Language-defined aspects cannot be associated with a subprogram
2461 -- body [stub] if the subprogram has a spec. Certain implementation
2462 -- defined aspects are allowed to break this rule (for list, see
2463 -- table Aspect_On_Body_Or_Stub_OK).
2465 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2466 Diagnose_Misplaced_Aspects;
2467 else
2468 Analyze_Aspect_Specifications (N, Body_Id);
2469 end if;
2470 end Analyze_Aspects_On_Body_Or_Stub;
2472 -----------------------
2473 -- Body_Has_Contract --
2474 -----------------------
2476 function Body_Has_Contract return Boolean is
2477 Decls : constant List_Id := Declarations (N);
2478 A_Spec : Node_Id;
2479 A : Aspect_Id;
2480 Decl : Node_Id;
2481 P_Id : Pragma_Id;
2483 begin
2484 -- Check for unanalyzed aspects in the body that will
2485 -- generate a contract.
2487 if Present (Aspect_Specifications (N)) then
2488 A_Spec := First (Aspect_Specifications (N));
2489 while Present (A_Spec) loop
2490 A := Get_Aspect_Id (Chars (Identifier (A_Spec)));
2492 if A = Aspect_Contract_Cases or else
2493 A = Aspect_Depends or else
2494 A = Aspect_Global or else
2495 A = Aspect_Pre or else
2496 A = Aspect_Precondition or else
2497 A = Aspect_Post or else
2498 A = Aspect_Postcondition
2499 then
2500 return True;
2501 end if;
2503 Next (A_Spec);
2504 end loop;
2505 end if;
2507 -- Check for pragmas that may generate a contract
2509 if Present (Decls) then
2510 Decl := First (Decls);
2511 while Present (Decl) loop
2512 if Nkind (Decl) = N_Pragma then
2513 P_Id := Get_Pragma_Id (Pragma_Name (Decl));
2515 if P_Id = Pragma_Contract_Cases or else
2516 P_Id = Pragma_Depends or else
2517 P_Id = Pragma_Global or else
2518 P_Id = Pragma_Pre or else
2519 P_Id = Pragma_Precondition or else
2520 P_Id = Pragma_Post or else
2521 P_Id = Pragma_Postcondition
2522 then
2523 return True;
2524 end if;
2525 end if;
2527 Next (Decl);
2528 end loop;
2529 end if;
2531 return False;
2532 end Body_Has_Contract;
2534 ----------------------------
2535 -- Check_Anonymous_Return --
2536 ----------------------------
2538 procedure Check_Anonymous_Return is
2539 Decl : Node_Id;
2540 Par : Node_Id;
2541 Scop : Entity_Id;
2543 begin
2544 if Present (Spec_Id) then
2545 Scop := Spec_Id;
2546 else
2547 Scop := Body_Id;
2548 end if;
2550 if Ekind (Scop) = E_Function
2551 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2552 and then not Is_Thunk (Scop)
2554 -- Skip internally built functions which handle the case of
2555 -- a null access (see Expand_Interface_Conversion)
2557 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2558 and then not Comes_From_Source (Parent (Scop)))
2560 and then (Has_Task (Designated_Type (Etype (Scop)))
2561 or else
2562 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2563 and then
2564 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2565 and then Expander_Active
2567 -- Avoid cases with no tasking support
2569 and then RTE_Available (RE_Current_Master)
2570 and then not Restriction_Active (No_Task_Hierarchy)
2571 then
2572 Decl :=
2573 Make_Object_Declaration (Loc,
2574 Defining_Identifier =>
2575 Make_Defining_Identifier (Loc, Name_uMaster),
2576 Constant_Present => True,
2577 Object_Definition =>
2578 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2579 Expression =>
2580 Make_Explicit_Dereference (Loc,
2581 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2583 if Present (Declarations (N)) then
2584 Prepend (Decl, Declarations (N));
2585 else
2586 Set_Declarations (N, New_List (Decl));
2587 end if;
2589 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2590 Set_Has_Master_Entity (Scop);
2592 -- Now mark the containing scope as a task master
2594 Par := N;
2595 while Nkind (Par) /= N_Compilation_Unit loop
2596 Par := Parent (Par);
2597 pragma Assert (Present (Par));
2599 -- If we fall off the top, we are at the outer level, and
2600 -- the environment task is our effective master, so nothing
2601 -- to mark.
2603 if Nkind_In
2604 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2605 then
2606 Set_Is_Task_Master (Par, True);
2607 exit;
2608 end if;
2609 end loop;
2610 end if;
2611 end Check_Anonymous_Return;
2613 -------------------------
2614 -- Check_Inline_Pragma --
2615 -------------------------
2617 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2618 Prag : Node_Id;
2619 Plist : List_Id;
2621 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2622 -- True when N is a pragma Inline or Inline_Always that applies
2623 -- to this subprogram.
2625 -----------------------
2626 -- Is_Inline_Pragma --
2627 -----------------------
2629 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2630 begin
2631 return
2632 Nkind (N) = N_Pragma
2633 and then
2634 (Pragma_Name (N) = Name_Inline_Always
2635 or else
2636 (Front_End_Inlining
2637 and then Pragma_Name (N) = Name_Inline))
2638 and then
2639 Chars
2640 (Expression (First (Pragma_Argument_Associations (N)))) =
2641 Chars (Body_Id);
2642 end Is_Inline_Pragma;
2644 -- Start of processing for Check_Inline_Pragma
2646 begin
2647 if not Expander_Active then
2648 return;
2649 end if;
2651 if Is_List_Member (N)
2652 and then Present (Next (N))
2653 and then Is_Inline_Pragma (Next (N))
2654 then
2655 Prag := Next (N);
2657 elsif Nkind (N) /= N_Subprogram_Body_Stub
2658 and then Present (Declarations (N))
2659 and then Is_Inline_Pragma (First (Declarations (N)))
2660 then
2661 Prag := First (Declarations (N));
2663 else
2664 Prag := Empty;
2665 end if;
2667 if Present (Prag) then
2668 if Present (Spec_Id) then
2669 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2670 Analyze (Prag);
2671 end if;
2673 else
2674 -- Create a subprogram declaration, to make treatment uniform
2676 declare
2677 Subp : constant Entity_Id :=
2678 Make_Defining_Identifier (Loc, Chars (Body_Id));
2679 Decl : constant Node_Id :=
2680 Make_Subprogram_Declaration (Loc,
2681 Specification =>
2682 New_Copy_Tree (Specification (N)));
2684 begin
2685 Set_Defining_Unit_Name (Specification (Decl), Subp);
2687 if Present (First_Formal (Body_Id)) then
2688 Plist := Copy_Parameter_List (Body_Id);
2689 Set_Parameter_Specifications
2690 (Specification (Decl), Plist);
2691 end if;
2693 Insert_Before (N, Decl);
2694 Analyze (Decl);
2695 Analyze (Prag);
2696 Set_Has_Pragma_Inline (Subp);
2698 if Pragma_Name (Prag) = Name_Inline_Always then
2699 Set_Is_Inlined (Subp);
2700 Set_Has_Pragma_Inline_Always (Subp);
2701 end if;
2703 -- Prior to copying the subprogram body to create a template
2704 -- for it for subsequent inlining, remove the pragma from
2705 -- the current body so that the copy that will produce the
2706 -- new body will start from a completely unanalyzed tree.
2708 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2709 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2710 end if;
2712 Spec := Subp;
2713 end;
2714 end if;
2715 end if;
2716 end Check_Inline_Pragma;
2718 --------------------------
2719 -- Check_Missing_Return --
2720 --------------------------
2722 procedure Check_Missing_Return is
2723 Id : Entity_Id;
2724 Missing_Ret : Boolean;
2726 begin
2727 if Nkind (Body_Spec) = N_Function_Specification then
2728 if Present (Spec_Id) then
2729 Id := Spec_Id;
2730 else
2731 Id := Body_Id;
2732 end if;
2734 if Return_Present (Id) then
2735 Check_Returns (HSS, 'F', Missing_Ret);
2737 if Missing_Ret then
2738 Set_Has_Missing_Return (Id);
2739 end if;
2741 elsif Is_Generic_Subprogram (Id)
2742 or else not Is_Machine_Code_Subprogram (Id)
2743 then
2744 Error_Msg_N ("missing RETURN statement in function body", N);
2745 end if;
2747 -- If procedure with No_Return, check returns
2749 elsif Nkind (Body_Spec) = N_Procedure_Specification
2750 and then Present (Spec_Id)
2751 and then No_Return (Spec_Id)
2752 then
2753 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2754 end if;
2756 -- Special checks in SPARK mode
2758 if Nkind (Body_Spec) = N_Function_Specification then
2760 -- In SPARK mode, last statement of a function should be a return
2762 declare
2763 Stat : constant Node_Id := Last_Source_Statement (HSS);
2764 begin
2765 if Present (Stat)
2766 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2767 N_Extended_Return_Statement)
2768 then
2769 Check_SPARK_05_Restriction
2770 ("last statement in function should be RETURN", Stat);
2771 end if;
2772 end;
2774 -- In SPARK mode, verify that a procedure has no return
2776 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2777 if Present (Spec_Id) then
2778 Id := Spec_Id;
2779 else
2780 Id := Body_Id;
2781 end if;
2783 -- Would be nice to point to return statement here, can we
2784 -- borrow the Check_Returns procedure here ???
2786 if Return_Present (Id) then
2787 Check_SPARK_05_Restriction
2788 ("procedure should not have RETURN", N);
2789 end if;
2790 end if;
2791 end Check_Missing_Return;
2793 -----------------------
2794 -- Disambiguate_Spec --
2795 -----------------------
2797 function Disambiguate_Spec return Entity_Id is
2798 Priv_Spec : Entity_Id;
2799 Spec_N : Entity_Id;
2801 procedure Replace_Types (To_Corresponding : Boolean);
2802 -- Depending on the flag, replace the type of formal parameters of
2803 -- Body_Id if it is a concurrent type implementing interfaces with
2804 -- the corresponding record type or the other way around.
2806 procedure Replace_Types (To_Corresponding : Boolean) is
2807 Formal : Entity_Id;
2808 Formal_Typ : Entity_Id;
2810 begin
2811 Formal := First_Formal (Body_Id);
2812 while Present (Formal) loop
2813 Formal_Typ := Etype (Formal);
2815 if Is_Class_Wide_Type (Formal_Typ) then
2816 Formal_Typ := Root_Type (Formal_Typ);
2817 end if;
2819 -- From concurrent type to corresponding record
2821 if To_Corresponding then
2822 if Is_Concurrent_Type (Formal_Typ)
2823 and then Present (Corresponding_Record_Type (Formal_Typ))
2824 and then Present (Interfaces (
2825 Corresponding_Record_Type (Formal_Typ)))
2826 then
2827 Set_Etype (Formal,
2828 Corresponding_Record_Type (Formal_Typ));
2829 end if;
2831 -- From corresponding record to concurrent type
2833 else
2834 if Is_Concurrent_Record_Type (Formal_Typ)
2835 and then Present (Interfaces (Formal_Typ))
2836 then
2837 Set_Etype (Formal,
2838 Corresponding_Concurrent_Type (Formal_Typ));
2839 end if;
2840 end if;
2842 Next_Formal (Formal);
2843 end loop;
2844 end Replace_Types;
2846 -- Start of processing for Disambiguate_Spec
2848 begin
2849 -- Try to retrieve the specification of the body as is. All error
2850 -- messages are suppressed because the body may not have a spec in
2851 -- its current state.
2853 Spec_N := Find_Corresponding_Spec (N, False);
2855 -- It is possible that this is the body of a primitive declared
2856 -- between a private and a full view of a concurrent type. The
2857 -- controlling parameter of the spec carries the concurrent type,
2858 -- not the corresponding record type as transformed by Analyze_
2859 -- Subprogram_Specification. In such cases, we undo the change
2860 -- made by the analysis of the specification and try to find the
2861 -- spec again.
2863 -- Note that wrappers already have their corresponding specs and
2864 -- bodies set during their creation, so if the candidate spec is
2865 -- a wrapper, then we definitely need to swap all types to their
2866 -- original concurrent status.
2868 if No (Spec_N)
2869 or else Is_Primitive_Wrapper (Spec_N)
2870 then
2871 -- Restore all references of corresponding record types to the
2872 -- original concurrent types.
2874 Replace_Types (To_Corresponding => False);
2875 Priv_Spec := Find_Corresponding_Spec (N, False);
2877 -- The current body truly belongs to a primitive declared between
2878 -- a private and a full view. We leave the modified body as is,
2879 -- and return the true spec.
2881 if Present (Priv_Spec)
2882 and then Is_Private_Primitive (Priv_Spec)
2883 then
2884 return Priv_Spec;
2885 end if;
2887 -- In case that this is some sort of error, restore the original
2888 -- state of the body.
2890 Replace_Types (To_Corresponding => True);
2891 end if;
2893 return Spec_N;
2894 end Disambiguate_Spec;
2896 ----------------------------
2897 -- Exchange_Limited_Views --
2898 ----------------------------
2900 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2901 procedure Detect_And_Exchange (Id : Entity_Id);
2902 -- Determine whether Id's type denotes an incomplete type associated
2903 -- with a limited with clause and exchange the limited view with the
2904 -- non-limited one.
2906 -------------------------
2907 -- Detect_And_Exchange --
2908 -------------------------
2910 procedure Detect_And_Exchange (Id : Entity_Id) is
2911 Typ : constant Entity_Id := Etype (Id);
2913 begin
2914 if Ekind (Typ) = E_Incomplete_Type
2915 and then From_Limited_With (Typ)
2916 and then Present (Non_Limited_View (Typ))
2917 then
2918 Set_Etype (Id, Non_Limited_View (Typ));
2919 end if;
2920 end Detect_And_Exchange;
2922 -- Local variables
2924 Formal : Entity_Id;
2926 -- Start of processing for Exchange_Limited_Views
2928 begin
2929 if No (Subp_Id) then
2930 return;
2932 -- Do not process subprogram bodies as they already use the non-
2933 -- limited view of types.
2935 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2936 return;
2937 end if;
2939 -- Examine all formals and swap views when applicable
2941 Formal := First_Formal (Subp_Id);
2942 while Present (Formal) loop
2943 Detect_And_Exchange (Formal);
2945 Next_Formal (Formal);
2946 end loop;
2948 -- Process the return type of a function
2950 if Ekind (Subp_Id) = E_Function then
2951 Detect_And_Exchange (Subp_Id);
2952 end if;
2953 end Exchange_Limited_Views;
2955 -------------------------------------
2956 -- Is_Private_Concurrent_Primitive --
2957 -------------------------------------
2959 function Is_Private_Concurrent_Primitive
2960 (Subp_Id : Entity_Id) return Boolean
2962 Formal_Typ : Entity_Id;
2964 begin
2965 if Present (First_Formal (Subp_Id)) then
2966 Formal_Typ := Etype (First_Formal (Subp_Id));
2968 if Is_Concurrent_Record_Type (Formal_Typ) then
2969 if Is_Class_Wide_Type (Formal_Typ) then
2970 Formal_Typ := Root_Type (Formal_Typ);
2971 end if;
2973 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2974 end if;
2976 -- The type of the first formal is a concurrent tagged type with
2977 -- a private view.
2979 return
2980 Is_Concurrent_Type (Formal_Typ)
2981 and then Is_Tagged_Type (Formal_Typ)
2982 and then Has_Private_Declaration (Formal_Typ);
2983 end if;
2985 return False;
2986 end Is_Private_Concurrent_Primitive;
2988 ----------------------------
2989 -- Set_Trivial_Subprogram --
2990 ----------------------------
2992 procedure Set_Trivial_Subprogram (N : Node_Id) is
2993 Nxt : constant Node_Id := Next (N);
2995 begin
2996 Set_Is_Trivial_Subprogram (Body_Id);
2998 if Present (Spec_Id) then
2999 Set_Is_Trivial_Subprogram (Spec_Id);
3000 end if;
3002 if Present (Nxt)
3003 and then Nkind (Nxt) = N_Simple_Return_Statement
3004 and then No (Next (Nxt))
3005 and then Present (Expression (Nxt))
3006 and then Is_Entity_Name (Expression (Nxt))
3007 then
3008 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3009 end if;
3010 end Set_Trivial_Subprogram;
3012 ---------------------------------
3013 -- Verify_Overriding_Indicator --
3014 ---------------------------------
3016 procedure Verify_Overriding_Indicator is
3017 begin
3018 if Must_Override (Body_Spec) then
3019 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3020 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3021 then
3022 null;
3024 elsif not Present (Overridden_Operation (Spec_Id)) then
3025 Error_Msg_NE
3026 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3028 -- Overriding indicators aren't allowed for protected subprogram
3029 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3030 -- this to a warning if -gnatd.E is enabled.
3032 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3033 Error_Msg_Warn := Error_To_Warning;
3034 Error_Msg_N
3035 ("<<overriding indicator not allowed for protected "
3036 & "subprogram body", Body_Spec);
3037 end if;
3039 elsif Must_Not_Override (Body_Spec) then
3040 if Present (Overridden_Operation (Spec_Id)) then
3041 Error_Msg_NE
3042 ("subprogram& overrides inherited operation",
3043 Body_Spec, Spec_Id);
3045 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3046 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3047 then
3048 Error_Msg_NE
3049 ("subprogram& overrides predefined operator ",
3050 Body_Spec, Spec_Id);
3052 -- Overriding indicators aren't allowed for protected subprogram
3053 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3054 -- this to a warning if -gnatd.E is enabled.
3056 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3057 Error_Msg_Warn := Error_To_Warning;
3059 Error_Msg_N
3060 ("<<overriding indicator not allowed "
3061 & "for protected subprogram body", Body_Spec);
3063 -- If this is not a primitive operation, then the overriding
3064 -- indicator is altogether illegal.
3066 elsif not Is_Primitive (Spec_Id) then
3067 Error_Msg_N
3068 ("overriding indicator only allowed "
3069 & "if subprogram is primitive", Body_Spec);
3070 end if;
3072 -- If checking the style rule and the operation overrides, then
3073 -- issue a warning about a missing overriding_indicator. Protected
3074 -- subprogram bodies are excluded from this style checking, since
3075 -- they aren't primitives (even though their declarations can
3076 -- override) and aren't allowed to have an overriding_indicator.
3078 elsif Style_Check
3079 and then Present (Overridden_Operation (Spec_Id))
3080 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3081 then
3082 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3083 Style.Missing_Overriding (N, Body_Id);
3085 elsif Style_Check
3086 and then Can_Override_Operator (Spec_Id)
3087 and then not Is_Predefined_File_Name
3088 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
3089 then
3090 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3091 Style.Missing_Overriding (N, Body_Id);
3092 end if;
3093 end Verify_Overriding_Indicator;
3095 -- Start of processing for Analyze_Subprogram_Body_Helper
3097 begin
3098 -- Generic subprograms are handled separately. They always have a
3099 -- generic specification. Determine whether current scope has a
3100 -- previous declaration.
3102 -- If the subprogram body is defined within an instance of the same
3103 -- name, the instance appears as a package renaming, and will be hidden
3104 -- within the subprogram.
3106 if Present (Prev_Id)
3107 and then not Is_Overloadable (Prev_Id)
3108 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3109 or else Comes_From_Source (Prev_Id))
3110 then
3111 if Is_Generic_Subprogram (Prev_Id) then
3112 Spec_Id := Prev_Id;
3113 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3114 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3116 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3118 if Nkind (N) = N_Subprogram_Body then
3119 HSS := Handled_Statement_Sequence (N);
3120 Check_Missing_Return;
3121 end if;
3123 return;
3125 else
3126 -- Previous entity conflicts with subprogram name. Attempting to
3127 -- enter name will post error.
3129 Enter_Name (Body_Id);
3130 return;
3131 end if;
3133 -- Non-generic case, find the subprogram declaration, if one was seen,
3134 -- or enter new overloaded entity in the current scope. If the
3135 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3136 -- part of the context of one of its subunits. No need to redo the
3137 -- analysis.
3139 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3140 return;
3142 else
3143 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3145 if Nkind (N) = N_Subprogram_Body_Stub
3146 or else No (Corresponding_Spec (N))
3147 then
3148 if Is_Private_Concurrent_Primitive (Body_Id) then
3149 Spec_Id := Disambiguate_Spec;
3150 else
3151 Spec_Id := Find_Corresponding_Spec (N);
3153 -- In GNATprove mode, if the body has no previous spec, create
3154 -- one so that the inlining machinery can operate properly.
3155 -- Transfer aspects, if any, to the new spec, so that they
3156 -- are legal and can be processed ahead of the body.
3157 -- We make two copies of the given spec, one for the new
3158 -- declaration, and one for the body.
3160 if No (Spec_Id)
3161 and then GNATprove_Mode
3163 -- Inlining does not apply during pre-analysis of code
3165 and then Full_Analysis
3167 -- Inlining only applies to full bodies, not stubs
3169 and then Nkind (N) /= N_Subprogram_Body_Stub
3171 -- Inlining only applies to bodies in the source code, not to
3172 -- those generated by the compiler. In particular, expression
3173 -- functions, whose body is generated by the compiler, are
3174 -- treated specially by GNATprove.
3176 and then Comes_From_Source (Body_Id)
3178 -- This cannot be done for a compilation unit, which is not
3179 -- in a context where we can insert a new spec.
3181 and then Is_List_Member (N)
3183 -- Inlining only applies to subprograms without contracts,
3184 -- as a contract is a sign that GNATprove should perform a
3185 -- modular analysis of the subprogram instead of a contextual
3186 -- analysis at each call site. The same test is performed in
3187 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3188 -- here in another form (because the contract has not
3189 -- been attached to the body) to avoid frontend errors in
3190 -- case pragmas are used instead of aspects, because the
3191 -- corresponding pragmas in the body would not be transferred
3192 -- to the spec, leading to legality errors.
3194 and then not Body_Has_Contract
3195 then
3196 declare
3197 Body_Spec : constant Node_Id :=
3198 Copy_Separate_Tree (Specification (N));
3199 New_Decl : constant Node_Id :=
3200 Make_Subprogram_Declaration (Loc,
3201 Copy_Separate_Tree (Specification (N)));
3203 SPARK_Mode_Aspect : Node_Id;
3204 Aspects : List_Id;
3205 Prag, Aspect : Node_Id;
3207 begin
3208 Insert_Before (N, New_Decl);
3209 Move_Aspects (From => N, To => New_Decl);
3211 -- Mark the newly moved aspects as not analyzed, so that
3212 -- their effect on New_Decl is properly analyzed.
3214 Aspect := First (Aspect_Specifications (New_Decl));
3215 while Present (Aspect) loop
3216 Set_Analyzed (Aspect, False);
3217 Next (Aspect);
3218 end loop;
3220 Analyze (New_Decl);
3222 -- The analysis of the generated subprogram declaration
3223 -- may have introduced pragmas that need to be analyzed.
3225 Prag := Next (New_Decl);
3226 while Prag /= N loop
3227 Analyze (Prag);
3228 Next (Prag);
3229 end loop;
3231 Spec_Id := Defining_Entity (New_Decl);
3233 -- As Body_Id originally comes from source, mark the new
3234 -- Spec_Id as such, which is required so that calls to
3235 -- this subprogram are registered in the local effects
3236 -- stored in ALI files for GNATprove.
3238 Set_Comes_From_Source (Spec_Id, True);
3240 -- If aspect SPARK_Mode was specified on the body, it
3241 -- needs to be repeated on the generated decl and the
3242 -- body. Since the original aspect was moved to the
3243 -- generated decl, copy it for the body.
3245 if Has_Aspect (Spec_Id, Aspect_SPARK_Mode) then
3246 SPARK_Mode_Aspect :=
3247 New_Copy (Find_Aspect (Spec_Id, Aspect_SPARK_Mode));
3248 Set_Analyzed (SPARK_Mode_Aspect, False);
3249 Aspects := New_List (SPARK_Mode_Aspect);
3250 Set_Aspect_Specifications (N, Aspects);
3251 end if;
3253 Set_Specification (N, Body_Spec);
3254 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3255 Set_Corresponding_Spec (N, Spec_Id);
3256 end;
3257 end if;
3258 end if;
3260 -- If this is a duplicate body, no point in analyzing it
3262 if Error_Posted (N) then
3263 return;
3264 end if;
3266 -- A subprogram body should cause freezing of its own declaration,
3267 -- but if there was no previous explicit declaration, then the
3268 -- subprogram will get frozen too late (there may be code within
3269 -- the body that depends on the subprogram having been frozen,
3270 -- such as uses of extra formals), so we force it to be frozen
3271 -- here. Same holds if the body and spec are compilation units.
3272 -- Finally, if the return type is an anonymous access to protected
3273 -- subprogram, it must be frozen before the body because its
3274 -- expansion has generated an equivalent type that is used when
3275 -- elaborating the body.
3277 -- An exception in the case of Ada 2012, AI05-177: The bodies
3278 -- created for expression functions do not freeze.
3280 if No (Spec_Id)
3281 and then Nkind (Original_Node (N)) /= N_Expression_Function
3282 then
3283 Freeze_Before (N, Body_Id);
3285 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3286 Freeze_Before (N, Spec_Id);
3288 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3289 Freeze_Before (N, Etype (Body_Id));
3290 end if;
3292 else
3293 Spec_Id := Corresponding_Spec (N);
3294 end if;
3295 end if;
3297 -- Previously we scanned the body to look for nested subprograms, and
3298 -- rejected an inline directive if nested subprograms were present,
3299 -- because the back-end would generate conflicting symbols for the
3300 -- nested bodies. This is now unnecessary.
3302 -- Look ahead to recognize a pragma Inline that appears after the body
3304 Check_Inline_Pragma (Spec_Id);
3306 -- Deal with special case of a fully private operation in the body of
3307 -- the protected type. We must create a declaration for the subprogram,
3308 -- in order to attach the protected subprogram that will be used in
3309 -- internal calls. We exclude compiler generated bodies from the
3310 -- expander since the issue does not arise for those cases.
3312 if No (Spec_Id)
3313 and then Comes_From_Source (N)
3314 and then Is_Protected_Type (Current_Scope)
3315 then
3316 Spec_Id := Build_Private_Protected_Declaration (N);
3317 end if;
3319 -- If a separate spec is present, then deal with freezing issues
3321 if Present (Spec_Id) then
3322 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3323 Verify_Overriding_Indicator;
3325 -- In general, the spec will be frozen when we start analyzing the
3326 -- body. However, for internally generated operations, such as
3327 -- wrapper functions for inherited operations with controlling
3328 -- results, the spec may not have been frozen by the time we expand
3329 -- the freeze actions that include the bodies. In particular, extra
3330 -- formals for accessibility or for return-in-place may need to be
3331 -- generated. Freeze nodes, if any, are inserted before the current
3332 -- body. These freeze actions are also needed in ASIS mode to enable
3333 -- the proper back-annotations.
3335 if not Is_Frozen (Spec_Id)
3336 and then (Expander_Active or ASIS_Mode)
3337 then
3338 -- Force the generation of its freezing node to ensure proper
3339 -- management of access types in the backend.
3341 -- This is definitely needed for some cases, but it is not clear
3342 -- why, to be investigated further???
3344 Set_Has_Delayed_Freeze (Spec_Id);
3345 Freeze_Before (N, Spec_Id);
3346 end if;
3347 end if;
3349 -- Mark presence of postcondition procedure in current scope and mark
3350 -- the procedure itself as needing debug info. The latter is important
3351 -- when analyzing decision coverage (for example, for MC/DC coverage).
3353 if Chars (Body_Id) = Name_uPostconditions then
3354 Set_Has_Postconditions (Current_Scope);
3355 Set_Debug_Info_Needed (Body_Id);
3356 end if;
3358 -- Place subprogram on scope stack, and make formals visible. If there
3359 -- is a spec, the visible entity remains that of the spec.
3361 if Present (Spec_Id) then
3362 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3364 if Is_Child_Unit (Spec_Id) then
3365 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3366 end if;
3368 if Style_Check then
3369 Style.Check_Identifier (Body_Id, Spec_Id);
3370 end if;
3372 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3373 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3375 if Is_Abstract_Subprogram (Spec_Id) then
3376 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3377 return;
3379 else
3380 Set_Convention (Body_Id, Convention (Spec_Id));
3381 Set_Has_Completion (Spec_Id);
3383 -- Inherit the "ghostness" of the subprogram spec. Note that this
3384 -- property is not directly inherited as the body may be subject
3385 -- to a different Ghost assertion policy.
3387 if Is_Ghost_Entity (Spec_Id) or else Within_Ghost_Scope then
3388 Set_Is_Ghost_Entity (Body_Id);
3390 -- The Ghost policy in effect at the point of declaration and
3391 -- at the point of completion must match (SPARK RM 6.9(15)).
3393 Check_Ghost_Completion (Spec_Id, Body_Id);
3394 end if;
3396 if Is_Protected_Type (Scope (Spec_Id)) then
3397 Prot_Typ := Scope (Spec_Id);
3398 end if;
3400 -- If this is a body generated for a renaming, do not check for
3401 -- full conformance. The check is redundant, because the spec of
3402 -- the body is a copy of the spec in the renaming declaration,
3403 -- and the test can lead to spurious errors on nested defaults.
3405 if Present (Spec_Decl)
3406 and then not Comes_From_Source (N)
3407 and then
3408 (Nkind (Original_Node (Spec_Decl)) =
3409 N_Subprogram_Renaming_Declaration
3410 or else (Present (Corresponding_Body (Spec_Decl))
3411 and then
3412 Nkind (Unit_Declaration_Node
3413 (Corresponding_Body (Spec_Decl))) =
3414 N_Subprogram_Renaming_Declaration))
3415 then
3416 Conformant := True;
3418 -- Conversely, the spec may have been generated for specless body
3419 -- with an inline pragma.
3421 elsif Comes_From_Source (N)
3422 and then not Comes_From_Source (Spec_Id)
3423 and then Has_Pragma_Inline (Spec_Id)
3424 then
3425 Conformant := True;
3427 else
3428 Check_Conformance
3429 (Body_Id, Spec_Id,
3430 Fully_Conformant, True, Conformant, Body_Id);
3431 end if;
3433 -- If the body is not fully conformant, we have to decide if we
3434 -- should analyze it or not. If it has a really messed up profile
3435 -- then we probably should not analyze it, since we will get too
3436 -- many bogus messages.
3438 -- Our decision is to go ahead in the non-fully conformant case
3439 -- only if it is at least mode conformant with the spec. Note
3440 -- that the call to Check_Fully_Conformant has issued the proper
3441 -- error messages to complain about the lack of conformance.
3443 if not Conformant
3444 and then not Mode_Conformant (Body_Id, Spec_Id)
3445 then
3446 return;
3447 end if;
3448 end if;
3450 if Spec_Id /= Body_Id then
3451 Reference_Body_Formals (Spec_Id, Body_Id);
3452 end if;
3454 Set_Ekind (Body_Id, E_Subprogram_Body);
3456 if Nkind (N) = N_Subprogram_Body_Stub then
3457 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3459 -- Regular body
3461 else
3462 Set_Corresponding_Spec (N, Spec_Id);
3464 -- Ada 2005 (AI-345): If the operation is a primitive operation
3465 -- of a concurrent type, the type of the first parameter has been
3466 -- replaced with the corresponding record, which is the proper
3467 -- run-time structure to use. However, within the body there may
3468 -- be uses of the formals that depend on primitive operations
3469 -- of the type (in particular calls in prefixed form) for which
3470 -- we need the original concurrent type. The operation may have
3471 -- several controlling formals, so the replacement must be done
3472 -- for all of them.
3474 if Comes_From_Source (Spec_Id)
3475 and then Present (First_Entity (Spec_Id))
3476 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3477 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3478 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3479 and then Present (Corresponding_Concurrent_Type
3480 (Etype (First_Entity (Spec_Id))))
3481 then
3482 declare
3483 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3484 Form : Entity_Id;
3486 begin
3487 Form := First_Formal (Spec_Id);
3488 while Present (Form) loop
3489 if Etype (Form) = Typ then
3490 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3491 end if;
3493 Next_Formal (Form);
3494 end loop;
3495 end;
3496 end if;
3498 -- Make the formals visible, and place subprogram on scope stack.
3499 -- This is also the point at which we set Last_Real_Spec_Entity
3500 -- to mark the entities which will not be moved to the body.
3502 Install_Formals (Spec_Id);
3503 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3505 -- Within an instance, add local renaming declarations so that
3506 -- gdb can retrieve the values of actuals more easily. This is
3507 -- only relevant if generating code (and indeed we definitely
3508 -- do not want these definitions -gnatc mode, because that would
3509 -- confuse ASIS).
3511 if Is_Generic_Instance (Spec_Id)
3512 and then Is_Wrapper_Package (Current_Scope)
3513 and then Expander_Active
3514 then
3515 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3516 end if;
3518 Push_Scope (Spec_Id);
3520 -- Make sure that the subprogram is immediately visible. For
3521 -- child units that have no separate spec this is indispensable.
3522 -- Otherwise it is safe albeit redundant.
3524 Set_Is_Immediately_Visible (Spec_Id);
3525 end if;
3527 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3528 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3529 Set_Scope (Body_Id, Scope (Spec_Id));
3530 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3532 -- Case of subprogram body with no previous spec
3534 else
3535 -- Check for style warning required
3537 if Style_Check
3539 -- Only apply check for source level subprograms for which checks
3540 -- have not been suppressed.
3542 and then Comes_From_Source (Body_Id)
3543 and then not Suppress_Style_Checks (Body_Id)
3545 -- No warnings within an instance
3547 and then not In_Instance
3549 -- No warnings for expression functions
3551 and then Nkind (Original_Node (N)) /= N_Expression_Function
3552 then
3553 Style.Body_With_No_Spec (N);
3554 end if;
3556 New_Overloaded_Entity (Body_Id);
3558 if Nkind (N) /= N_Subprogram_Body_Stub then
3559 Set_Acts_As_Spec (N);
3560 Generate_Definition (Body_Id);
3561 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3562 Generate_Reference
3563 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3564 Install_Formals (Body_Id);
3566 Push_Scope (Body_Id);
3567 end if;
3569 -- For stubs and bodies with no previous spec, generate references to
3570 -- formals.
3572 Generate_Reference_To_Formals (Body_Id);
3573 end if;
3575 -- Set SPARK_Mode from context
3577 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3578 Set_SPARK_Pragma_Inherited (Body_Id, True);
3580 -- If the return type is an anonymous access type whose designated type
3581 -- is the limited view of a class-wide type and the non-limited view is
3582 -- available, update the return type accordingly.
3584 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3585 declare
3586 Etyp : Entity_Id;
3587 Rtyp : Entity_Id;
3589 begin
3590 Rtyp := Etype (Current_Scope);
3592 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3593 Etyp := Directly_Designated_Type (Rtyp);
3595 if Is_Class_Wide_Type (Etyp)
3596 and then From_Limited_With (Etyp)
3597 then
3598 Set_Directly_Designated_Type
3599 (Etype (Current_Scope), Available_View (Etyp));
3600 end if;
3601 end if;
3602 end;
3603 end if;
3605 -- If this is the proper body of a stub, we must verify that the stub
3606 -- conforms to the body, and to the previous spec if one was present.
3607 -- We know already that the body conforms to that spec. This test is
3608 -- only required for subprograms that come from source.
3610 if Nkind (Parent (N)) = N_Subunit
3611 and then Comes_From_Source (N)
3612 and then not Error_Posted (Body_Id)
3613 and then Nkind (Corresponding_Stub (Parent (N))) =
3614 N_Subprogram_Body_Stub
3615 then
3616 declare
3617 Old_Id : constant Entity_Id :=
3618 Defining_Entity
3619 (Specification (Corresponding_Stub (Parent (N))));
3621 Conformant : Boolean := False;
3623 begin
3624 if No (Spec_Id) then
3625 Check_Fully_Conformant (Body_Id, Old_Id);
3627 else
3628 Check_Conformance
3629 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3631 if not Conformant then
3633 -- The stub was taken to be a new declaration. Indicate that
3634 -- it lacks a body.
3636 Set_Has_Completion (Old_Id, False);
3637 end if;
3638 end if;
3639 end;
3640 end if;
3642 Set_Has_Completion (Body_Id);
3643 Check_Eliminated (Body_Id);
3645 if Nkind (N) = N_Subprogram_Body_Stub then
3647 -- Analyze any aspect specifications that appear on the subprogram
3648 -- body stub.
3650 if Has_Aspects (N) then
3651 Analyze_Aspects_On_Body_Or_Stub;
3652 end if;
3654 -- Stop the analysis now as the stub cannot be inlined, plus it does
3655 -- not have declarative or statement lists.
3657 return;
3658 end if;
3660 -- Handle frontend inlining
3662 -- Note: Normally we don't do any inlining if expansion is off, since
3663 -- we won't generate code in any case. An exception arises in GNATprove
3664 -- mode where we want to expand some calls in place, even with expansion
3665 -- disabled, since the inlining eases formal verification.
3667 if not GNATprove_Mode
3668 and then Expander_Active
3669 and then Serious_Errors_Detected = 0
3670 and then Present (Spec_Id)
3671 and then Has_Pragma_Inline (Spec_Id)
3672 then
3673 -- Legacy implementation (relying on frontend inlining)
3675 if not Back_End_Inlining then
3676 if Has_Pragma_Inline_Always (Spec_Id)
3677 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining)
3678 then
3679 Build_Body_To_Inline (N, Spec_Id);
3680 end if;
3682 -- New implementation (relying on backend inlining)
3684 else
3685 if Has_Pragma_Inline_Always (Spec_Id)
3686 or else Optimization_Level > 0
3687 then
3688 -- Handle function returning an unconstrained type
3690 if Comes_From_Source (Body_Id)
3691 and then Ekind (Spec_Id) = E_Function
3692 and then Returns_Unconstrained_Type (Spec_Id)
3693 then
3694 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
3696 else
3697 declare
3698 Subp_Body : constant Node_Id :=
3699 Unit_Declaration_Node (Body_Id);
3700 Subp_Decl : constant List_Id := Declarations (Subp_Body);
3702 begin
3703 -- Do not pass inlining to the backend if the subprogram
3704 -- has declarations or statements which cannot be inlined
3705 -- by the backend. This check is done here to emit an
3706 -- error instead of the generic warning message reported
3707 -- by the GCC backend (ie. "function might not be
3708 -- inlinable").
3710 if Present (Subp_Decl)
3711 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
3712 then
3713 null;
3715 elsif Has_Excluded_Statement
3716 (Spec_Id,
3717 Statements
3718 (Handled_Statement_Sequence (Subp_Body)))
3719 then
3720 null;
3722 -- If the backend inlining is available then at this
3723 -- stage we only have to mark the subprogram as inlined.
3724 -- The expander will take care of registering it in the
3725 -- table of subprograms inlined by the backend a part of
3726 -- processing calls to it (cf. Expand_Call)
3728 else
3729 Set_Is_Inlined (Spec_Id);
3730 end if;
3731 end;
3732 end if;
3733 end if;
3734 end if;
3736 -- In GNATprove mode, inline only when there is a separate subprogram
3737 -- declaration for now, as inlining of subprogram bodies acting as
3738 -- declarations, or subprogram stubs, are not supported by frontend
3739 -- inlining. This inlining should occur after analysis of the body, so
3740 -- that it is known whether the value of SPARK_Mode applicable to the
3741 -- body, which can be defined by a pragma inside the body.
3743 elsif GNATprove_Mode
3744 and then Full_Analysis
3745 and then not Inside_A_Generic
3746 and then Present (Spec_Id)
3747 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3748 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
3749 and then not Body_Has_Contract
3750 then
3751 Build_Body_To_Inline (N, Spec_Id);
3752 end if;
3754 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3755 -- of the specification we have to install the private withed units.
3756 -- This holds for child units as well.
3758 if Is_Compilation_Unit (Body_Id)
3759 or else Nkind (Parent (N)) = N_Compilation_Unit
3760 then
3761 Install_Private_With_Clauses (Body_Id);
3762 end if;
3764 Check_Anonymous_Return;
3766 -- Set the Protected_Formal field of each extra formal of the protected
3767 -- subprogram to reference the corresponding extra formal of the
3768 -- subprogram that implements it. For regular formals this occurs when
3769 -- the protected subprogram's declaration is expanded, but the extra
3770 -- formals don't get created until the subprogram is frozen. We need to
3771 -- do this before analyzing the protected subprogram's body so that any
3772 -- references to the original subprogram's extra formals will be changed
3773 -- refer to the implementing subprogram's formals (see Expand_Formal).
3775 if Present (Spec_Id)
3776 and then Is_Protected_Type (Scope (Spec_Id))
3777 and then Present (Protected_Body_Subprogram (Spec_Id))
3778 then
3779 declare
3780 Impl_Subp : constant Entity_Id :=
3781 Protected_Body_Subprogram (Spec_Id);
3782 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3783 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3784 begin
3785 while Present (Prot_Ext_Formal) loop
3786 pragma Assert (Present (Impl_Ext_Formal));
3787 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3788 Next_Formal_With_Extras (Prot_Ext_Formal);
3789 Next_Formal_With_Extras (Impl_Ext_Formal);
3790 end loop;
3791 end;
3792 end if;
3794 -- Now we can go on to analyze the body
3796 HSS := Handled_Statement_Sequence (N);
3797 Set_Actual_Subtypes (N, Current_Scope);
3799 -- Add a declaration for the Protection object, renaming declarations
3800 -- for discriminals and privals and finally a declaration for the entry
3801 -- family index (if applicable). This form of early expansion is done
3802 -- when the Expander is active because Install_Private_Data_Declarations
3803 -- references entities which were created during regular expansion. The
3804 -- subprogram entity must come from source, and not be an internally
3805 -- generated subprogram.
3807 if Expander_Active
3808 and then Present (Prot_Typ)
3809 and then Present (Spec_Id)
3810 and then Comes_From_Source (Spec_Id)
3811 and then not Is_Eliminated (Spec_Id)
3812 then
3813 Install_Private_Data_Declarations
3814 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3815 end if;
3817 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3818 -- may now appear in parameter and result profiles. Since the analysis
3819 -- of a subprogram body may use the parameter and result profile of the
3820 -- spec, swap any limited views with their non-limited counterpart.
3822 if Ada_Version >= Ada_2012 then
3823 Exchange_Limited_Views (Spec_Id);
3824 end if;
3826 -- Analyze any aspect specifications that appear on the subprogram body
3828 if Has_Aspects (N) then
3829 Analyze_Aspects_On_Body_Or_Stub;
3830 end if;
3832 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3833 -- invariants and predicates associated with the body and its spec.
3834 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3835 -- prepares the contract assertions for generic subprograms or for ASIS.
3836 -- Do not generate contract checks in SPARK mode.
3838 if not GNATprove_Mode then
3839 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3840 end if;
3842 -- Analyze the declarations (this call will analyze the precondition
3843 -- Check pragmas we prepended to the list, as well as the declaration
3844 -- of the _Postconditions procedure).
3846 Analyze_Declarations (Declarations (N));
3848 -- Verify that the SPARK_Mode of the body agrees with that of its spec
3850 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3851 if Present (SPARK_Pragma (Spec_Id)) then
3852 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3853 and then
3854 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3855 then
3856 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3857 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3858 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3859 Error_Msg_NE
3860 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
3861 end if;
3863 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3864 null;
3866 else
3867 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3868 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
3869 Error_Msg_Sloc := Sloc (Spec_Id);
3870 Error_Msg_NE
3871 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
3872 end if;
3873 end if;
3875 -- If SPARK_Mode for body is not On, disable frontend inlining for this
3876 -- subprogram in GNATprove mode, as its body should not be analyzed.
3878 if SPARK_Mode /= On
3879 and then GNATprove_Mode
3880 and then Present (Spec_Id)
3881 and then Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Declaration
3882 then
3883 Set_Body_To_Inline (Parent (Parent (Spec_Id)), Empty);
3884 Set_Is_Inlined_Always (Spec_Id, False);
3885 end if;
3887 -- Check completion, and analyze the statements
3889 Check_Completion;
3890 Inspect_Deferred_Constant_Completion (Declarations (N));
3891 Analyze (HSS);
3893 -- Deal with end of scope processing for the body
3895 Process_End_Label (HSS, 't', Current_Scope);
3896 End_Scope;
3897 Check_Subprogram_Order (N);
3898 Set_Analyzed (Body_Id);
3900 -- If we have a separate spec, then the analysis of the declarations
3901 -- caused the entities in the body to be chained to the spec id, but
3902 -- we want them chained to the body id. Only the formal parameters
3903 -- end up chained to the spec id in this case.
3905 if Present (Spec_Id) then
3907 -- We must conform to the categorization of our spec
3909 Validate_Categorization_Dependency (N, Spec_Id);
3911 -- And if this is a child unit, the parent units must conform
3913 if Is_Child_Unit (Spec_Id) then
3914 Validate_Categorization_Dependency
3915 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3916 end if;
3918 -- Here is where we move entities from the spec to the body
3920 -- Case where there are entities that stay with the spec
3922 if Present (Last_Real_Spec_Entity) then
3924 -- No body entities (happens when the only real spec entities come
3925 -- from precondition and postcondition pragmas).
3927 if No (Last_Entity (Body_Id)) then
3928 Set_First_Entity
3929 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3931 -- Body entities present (formals), so chain stuff past them
3933 else
3934 Set_Next_Entity
3935 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3936 end if;
3938 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3939 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3940 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3942 -- Case where there are no spec entities, in this case there can be
3943 -- no body entities either, so just move everything.
3945 else
3946 pragma Assert (No (Last_Entity (Body_Id)));
3947 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3948 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3949 Set_First_Entity (Spec_Id, Empty);
3950 Set_Last_Entity (Spec_Id, Empty);
3951 end if;
3952 end if;
3954 Check_Missing_Return;
3956 -- Now we are going to check for variables that are never modified in
3957 -- the body of the procedure. But first we deal with a special case
3958 -- where we want to modify this check. If the body of the subprogram
3959 -- starts with a raise statement or its equivalent, or if the body
3960 -- consists entirely of a null statement, then it is pretty obvious that
3961 -- it is OK to not reference the parameters. For example, this might be
3962 -- the following common idiom for a stubbed function: statement of the
3963 -- procedure raises an exception. In particular this deals with the
3964 -- common idiom of a stubbed function, which appears something like:
3966 -- function F (A : Integer) return Some_Type;
3967 -- X : Some_Type;
3968 -- begin
3969 -- raise Program_Error;
3970 -- return X;
3971 -- end F;
3973 -- Here the purpose of X is simply to satisfy the annoying requirement
3974 -- in Ada that there be at least one return, and we certainly do not
3975 -- want to go posting warnings on X that it is not initialized. On
3976 -- the other hand, if X is entirely unreferenced that should still
3977 -- get a warning.
3979 -- What we do is to detect these cases, and if we find them, flag the
3980 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3981 -- suppress unwanted warnings. For the case of the function stub above
3982 -- we have a special test to set X as apparently assigned to suppress
3983 -- the warning.
3985 declare
3986 Stm : Node_Id;
3988 begin
3989 -- Skip initial labels (for one thing this occurs when we are in
3990 -- front end ZCX mode, but in any case it is irrelevant), and also
3991 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3993 Stm := First (Statements (HSS));
3994 while Nkind (Stm) = N_Label
3995 or else Nkind (Stm) in N_Push_xxx_Label
3996 loop
3997 Next (Stm);
3998 end loop;
4000 -- Do the test on the original statement before expansion
4002 declare
4003 Ostm : constant Node_Id := Original_Node (Stm);
4005 begin
4006 -- If explicit raise statement, turn on flag
4008 if Nkind (Ostm) = N_Raise_Statement then
4009 Set_Trivial_Subprogram (Stm);
4011 -- If null statement, and no following statements, turn on flag
4013 elsif Nkind (Stm) = N_Null_Statement
4014 and then Comes_From_Source (Stm)
4015 and then No (Next (Stm))
4016 then
4017 Set_Trivial_Subprogram (Stm);
4019 -- Check for explicit call cases which likely raise an exception
4021 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4022 if Is_Entity_Name (Name (Ostm)) then
4023 declare
4024 Ent : constant Entity_Id := Entity (Name (Ostm));
4026 begin
4027 -- If the procedure is marked No_Return, then likely it
4028 -- raises an exception, but in any case it is not coming
4029 -- back here, so turn on the flag.
4031 if Present (Ent)
4032 and then Ekind (Ent) = E_Procedure
4033 and then No_Return (Ent)
4034 then
4035 Set_Trivial_Subprogram (Stm);
4036 end if;
4037 end;
4038 end if;
4039 end if;
4040 end;
4041 end;
4043 -- Check for variables that are never modified
4045 declare
4046 E1, E2 : Entity_Id;
4048 begin
4049 -- If there is a separate spec, then transfer Never_Set_In_Source
4050 -- flags from out parameters to the corresponding entities in the
4051 -- body. The reason we do that is we want to post error flags on
4052 -- the body entities, not the spec entities.
4054 if Present (Spec_Id) then
4055 E1 := First_Entity (Spec_Id);
4056 while Present (E1) loop
4057 if Ekind (E1) = E_Out_Parameter then
4058 E2 := First_Entity (Body_Id);
4059 while Present (E2) loop
4060 exit when Chars (E1) = Chars (E2);
4061 Next_Entity (E2);
4062 end loop;
4064 if Present (E2) then
4065 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4066 end if;
4067 end if;
4069 Next_Entity (E1);
4070 end loop;
4071 end if;
4073 -- Check references in body
4075 Check_References (Body_Id);
4076 end;
4077 end Analyze_Subprogram_Body_Helper;
4079 ---------------------------------
4080 -- Analyze_Subprogram_Contract --
4081 ---------------------------------
4083 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
4084 Items : constant Node_Id := Contract (Subp);
4085 Case_Prag : Node_Id := Empty;
4086 Depends : Node_Id := Empty;
4087 Global : Node_Id := Empty;
4088 Mode : SPARK_Mode_Type;
4089 Nam : Name_Id;
4090 Post_Prag : Node_Id := Empty;
4091 Prag : Node_Id;
4092 Seen_In_Case : Boolean := False;
4093 Seen_In_Post : Boolean := False;
4095 begin
4096 -- Due to the timing of contract analysis, delayed pragmas may be
4097 -- subject to the wrong SPARK_Mode, usually that of the enclosing
4098 -- context. To remedy this, restore the original SPARK_Mode of the
4099 -- related subprogram body.
4101 Save_SPARK_Mode_And_Set (Subp, Mode);
4103 if Present (Items) then
4105 -- Analyze pre- and postconditions
4107 Prag := Pre_Post_Conditions (Items);
4108 while Present (Prag) loop
4109 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
4111 -- Verify whether a postcondition mentions attribute 'Result and
4112 -- its expression introduces a post-state.
4114 if Warn_On_Suspicious_Contract
4115 and then Pragma_Name (Prag) = Name_Postcondition
4116 then
4117 Post_Prag := Prag;
4118 Check_Result_And_Post_State (Prag, Seen_In_Post);
4119 end if;
4121 Prag := Next_Pragma (Prag);
4122 end loop;
4124 -- Analyze contract-cases and test-cases
4126 Prag := Contract_Test_Cases (Items);
4127 while Present (Prag) loop
4128 Nam := Pragma_Name (Prag);
4130 if Nam = Name_Contract_Cases then
4131 Analyze_Contract_Cases_In_Decl_Part (Prag);
4133 -- Verify whether contract-cases mention attribute 'Result and
4134 -- its expression introduces a post-state. Perform the check
4135 -- only when the pragma is legal.
4137 if Warn_On_Suspicious_Contract
4138 and then not Error_Posted (Prag)
4139 then
4140 Case_Prag := Prag;
4141 Check_Result_And_Post_State (Prag, Seen_In_Case);
4142 end if;
4144 else
4145 pragma Assert (Nam = Name_Test_Case);
4146 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
4147 end if;
4149 Prag := Next_Pragma (Prag);
4150 end loop;
4152 -- Analyze classification pragmas
4154 Prag := Classifications (Items);
4155 while Present (Prag) loop
4156 Nam := Pragma_Name (Prag);
4158 if Nam = Name_Depends then
4159 Depends := Prag;
4161 elsif Nam = Name_Global then
4162 Global := Prag;
4164 -- Note that pragma Extensions_Visible has already been analyzed
4166 end if;
4168 Prag := Next_Pragma (Prag);
4169 end loop;
4171 -- Analyze Global first as Depends may mention items classified in
4172 -- the global categorization.
4174 if Present (Global) then
4175 Analyze_Global_In_Decl_Part (Global);
4176 end if;
4178 -- Depends must be analyzed after Global in order to see the modes of
4179 -- all global items.
4181 if Present (Depends) then
4182 Analyze_Depends_In_Decl_Part (Depends);
4183 end if;
4184 end if;
4186 -- Emit an error when neither the postconditions nor the contract-cases
4187 -- mention attribute 'Result in the context of a function.
4189 if Warn_On_Suspicious_Contract
4190 and then Ekind_In (Subp, E_Function, E_Generic_Function)
4191 then
4192 if Present (Case_Prag)
4193 and then not Seen_In_Case
4194 and then Present (Post_Prag)
4195 and then not Seen_In_Post
4196 then
4197 Error_Msg_N
4198 ("neither function postcondition nor contract cases mention "
4199 & "result?T?", Post_Prag);
4201 elsif Present (Case_Prag) and then not Seen_In_Case then
4202 Error_Msg_N
4203 ("contract cases do not mention result?T?", Case_Prag);
4205 -- OK if we have at least one IN OUT parameter
4207 elsif Present (Post_Prag) and then not Seen_In_Post then
4208 declare
4209 F : Entity_Id;
4210 begin
4211 F := First_Formal (Subp);
4212 while Present (F) loop
4213 if Ekind (F) = E_In_Out_Parameter then
4214 return;
4215 else
4216 Next_Formal (F);
4217 end if;
4218 end loop;
4219 end;
4221 -- If no in-out parameters and no mention of Result, the contract
4222 -- is certainly suspicious.
4224 Error_Msg_N
4225 ("function postcondition does not mention result?T?", Post_Prag);
4226 end if;
4227 end if;
4229 -- Restore the SPARK_Mode of the enclosing context after all delayed
4230 -- pragmas have been analyzed.
4232 Restore_SPARK_Mode (Mode);
4233 end Analyze_Subprogram_Contract;
4235 ------------------------------------
4236 -- Analyze_Subprogram_Declaration --
4237 ------------------------------------
4239 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4240 Scop : constant Entity_Id := Current_Scope;
4241 Designator : Entity_Id;
4243 Is_Completion : Boolean;
4244 -- Indicates whether a null procedure declaration is a completion
4246 begin
4247 -- Null procedures are not allowed in SPARK
4249 if Nkind (Specification (N)) = N_Procedure_Specification
4250 and then Null_Present (Specification (N))
4251 then
4252 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4254 if Is_Protected_Type (Current_Scope) then
4255 Error_Msg_N ("protected operation cannot be a null procedure", N);
4256 end if;
4258 Analyze_Null_Procedure (N, Is_Completion);
4260 if Is_Completion then
4262 -- The null procedure acts as a body, nothing further is needed.
4264 return;
4265 end if;
4266 end if;
4268 Designator := Analyze_Subprogram_Specification (Specification (N));
4270 -- A reference may already have been generated for the unit name, in
4271 -- which case the following call is redundant. However it is needed for
4272 -- declarations that are the rewriting of an expression function.
4274 Generate_Definition (Designator);
4276 -- Set SPARK mode from current context (may be overwritten later with
4277 -- explicit pragma).
4279 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4280 Set_SPARK_Pragma_Inherited (Designator, True);
4282 -- A subprogram declared within a Ghost scope is automatically Ghost
4283 -- (SPARK RM 6.9(2)).
4285 if Comes_From_Source (Designator) and then Within_Ghost_Scope then
4286 Set_Is_Ghost_Entity (Designator);
4287 end if;
4289 if Debug_Flag_C then
4290 Write_Str ("==> subprogram spec ");
4291 Write_Name (Chars (Designator));
4292 Write_Str (" from ");
4293 Write_Location (Sloc (N));
4294 Write_Eol;
4295 Indent;
4296 end if;
4298 Validate_RCI_Subprogram_Declaration (N);
4299 New_Overloaded_Entity (Designator);
4300 Check_Delayed_Subprogram (Designator);
4302 -- If the type of the first formal of the current subprogram is a non-
4303 -- generic tagged private type, mark the subprogram as being a private
4304 -- primitive. Ditto if this is a function with controlling result, and
4305 -- the return type is currently private. In both cases, the type of the
4306 -- controlling argument or result must be in the current scope for the
4307 -- operation to be primitive.
4309 if Has_Controlling_Result (Designator)
4310 and then Is_Private_Type (Etype (Designator))
4311 and then Scope (Etype (Designator)) = Current_Scope
4312 and then not Is_Generic_Actual_Type (Etype (Designator))
4313 then
4314 Set_Is_Private_Primitive (Designator);
4316 elsif Present (First_Formal (Designator)) then
4317 declare
4318 Formal_Typ : constant Entity_Id :=
4319 Etype (First_Formal (Designator));
4320 begin
4321 Set_Is_Private_Primitive (Designator,
4322 Is_Tagged_Type (Formal_Typ)
4323 and then Scope (Formal_Typ) = Current_Scope
4324 and then Is_Private_Type (Formal_Typ)
4325 and then not Is_Generic_Actual_Type (Formal_Typ));
4326 end;
4327 end if;
4329 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4330 -- or null.
4332 if Ada_Version >= Ada_2005
4333 and then Comes_From_Source (N)
4334 and then Is_Dispatching_Operation (Designator)
4335 then
4336 declare
4337 E : Entity_Id;
4338 Etyp : Entity_Id;
4340 begin
4341 if Has_Controlling_Result (Designator) then
4342 Etyp := Etype (Designator);
4344 else
4345 E := First_Entity (Designator);
4346 while Present (E)
4347 and then Is_Formal (E)
4348 and then not Is_Controlling_Formal (E)
4349 loop
4350 Next_Entity (E);
4351 end loop;
4353 Etyp := Etype (E);
4354 end if;
4356 if Is_Access_Type (Etyp) then
4357 Etyp := Directly_Designated_Type (Etyp);
4358 end if;
4360 if Is_Interface (Etyp)
4361 and then not Is_Abstract_Subprogram (Designator)
4362 and then not (Ekind (Designator) = E_Procedure
4363 and then Null_Present (Specification (N)))
4364 then
4365 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4367 -- Specialize error message based on procedures vs. functions,
4368 -- since functions can't be null subprograms.
4370 if Ekind (Designator) = E_Procedure then
4371 Error_Msg_N
4372 ("interface procedure % must be abstract or null", N);
4373 else
4374 Error_Msg_N
4375 ("interface function % must be abstract", N);
4376 end if;
4377 end if;
4378 end;
4379 end if;
4381 -- What is the following code for, it used to be
4383 -- ??? Set_Suppress_Elaboration_Checks
4384 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4386 -- The following seems equivalent, but a bit dubious
4388 if Elaboration_Checks_Suppressed (Designator) then
4389 Set_Kill_Elaboration_Checks (Designator);
4390 end if;
4392 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4393 Set_Categorization_From_Scope (Designator, Scop);
4395 else
4396 -- For a compilation unit, check for library-unit pragmas
4398 Push_Scope (Designator);
4399 Set_Categorization_From_Pragmas (N);
4400 Validate_Categorization_Dependency (N, Designator);
4401 Pop_Scope;
4402 end if;
4404 -- For a compilation unit, set body required. This flag will only be
4405 -- reset if a valid Import or Interface pragma is processed later on.
4407 if Nkind (Parent (N)) = N_Compilation_Unit then
4408 Set_Body_Required (Parent (N), True);
4410 if Ada_Version >= Ada_2005
4411 and then Nkind (Specification (N)) = N_Procedure_Specification
4412 and then Null_Present (Specification (N))
4413 then
4414 Error_Msg_N
4415 ("null procedure cannot be declared at library level", N);
4416 end if;
4417 end if;
4419 Generate_Reference_To_Formals (Designator);
4420 Check_Eliminated (Designator);
4422 if Debug_Flag_C then
4423 Outdent;
4424 Write_Str ("<== subprogram spec ");
4425 Write_Name (Chars (Designator));
4426 Write_Str (" from ");
4427 Write_Location (Sloc (N));
4428 Write_Eol;
4429 end if;
4431 if Is_Protected_Type (Current_Scope) then
4433 -- Indicate that this is a protected operation, because it may be
4434 -- used in subsequent declarations within the protected type.
4436 Set_Convention (Designator, Convention_Protected);
4437 end if;
4439 List_Inherited_Pre_Post_Aspects (Designator);
4441 if Has_Aspects (N) then
4442 Analyze_Aspect_Specifications (N, Designator);
4443 end if;
4444 end Analyze_Subprogram_Declaration;
4446 --------------------------------------
4447 -- Analyze_Subprogram_Specification --
4448 --------------------------------------
4450 -- Reminder: N here really is a subprogram specification (not a subprogram
4451 -- declaration). This procedure is called to analyze the specification in
4452 -- both subprogram bodies and subprogram declarations (specs).
4454 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4455 Designator : constant Entity_Id := Defining_Entity (N);
4456 Formals : constant List_Id := Parameter_Specifications (N);
4458 -- Start of processing for Analyze_Subprogram_Specification
4460 begin
4461 -- User-defined operator is not allowed in SPARK, except as a renaming
4463 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4464 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4465 then
4466 Check_SPARK_05_Restriction
4467 ("user-defined operator is not allowed", N);
4468 end if;
4470 -- Proceed with analysis. Do not emit a cross-reference entry if the
4471 -- specification comes from an expression function, because it may be
4472 -- the completion of a previous declaration. It is is not, the cross-
4473 -- reference entry will be emitted for the new subprogram declaration.
4475 if Nkind (Parent (N)) /= N_Expression_Function then
4476 Generate_Definition (Designator);
4477 end if;
4479 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4481 if Nkind (N) = N_Function_Specification then
4482 Set_Ekind (Designator, E_Function);
4483 Set_Mechanism (Designator, Default_Mechanism);
4484 else
4485 Set_Ekind (Designator, E_Procedure);
4486 Set_Etype (Designator, Standard_Void_Type);
4487 end if;
4489 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4490 -- those subprograms which could be inlined in GNATprove mode (because
4491 -- Body_To_Inline is non-Empty) but cannot be inlined.
4493 if GNATprove_Mode then
4494 Set_Is_Inlined_Always (Designator);
4495 end if;
4497 -- Introduce new scope for analysis of the formals and the return type
4499 Set_Scope (Designator, Current_Scope);
4501 if Present (Formals) then
4502 Push_Scope (Designator);
4503 Process_Formals (Formals, N);
4505 -- Check dimensions in N for formals with default expression
4507 Analyze_Dimension_Formals (N, Formals);
4509 -- Ada 2005 (AI-345): If this is an overriding operation of an
4510 -- inherited interface operation, and the controlling type is
4511 -- a synchronized type, replace the type with its corresponding
4512 -- record, to match the proper signature of an overriding operation.
4513 -- Same processing for an access parameter whose designated type is
4514 -- derived from a synchronized interface.
4516 if Ada_Version >= Ada_2005 then
4517 declare
4518 Formal : Entity_Id;
4519 Formal_Typ : Entity_Id;
4520 Rec_Typ : Entity_Id;
4521 Desig_Typ : Entity_Id;
4523 begin
4524 Formal := First_Formal (Designator);
4525 while Present (Formal) loop
4526 Formal_Typ := Etype (Formal);
4528 if Is_Concurrent_Type (Formal_Typ)
4529 and then Present (Corresponding_Record_Type (Formal_Typ))
4530 then
4531 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4533 if Present (Interfaces (Rec_Typ)) then
4534 Set_Etype (Formal, Rec_Typ);
4535 end if;
4537 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4538 Desig_Typ := Designated_Type (Formal_Typ);
4540 if Is_Concurrent_Type (Desig_Typ)
4541 and then Present (Corresponding_Record_Type (Desig_Typ))
4542 then
4543 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4545 if Present (Interfaces (Rec_Typ)) then
4546 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4547 end if;
4548 end if;
4549 end if;
4551 Next_Formal (Formal);
4552 end loop;
4553 end;
4554 end if;
4556 End_Scope;
4558 -- The subprogram scope is pushed and popped around the processing of
4559 -- the return type for consistency with call above to Process_Formals
4560 -- (which itself can call Analyze_Return_Type), and to ensure that any
4561 -- itype created for the return type will be associated with the proper
4562 -- scope.
4564 elsif Nkind (N) = N_Function_Specification then
4565 Push_Scope (Designator);
4566 Analyze_Return_Type (N);
4567 End_Scope;
4568 end if;
4570 -- Function case
4572 if Nkind (N) = N_Function_Specification then
4574 -- Deal with operator symbol case
4576 if Nkind (Designator) = N_Defining_Operator_Symbol then
4577 Valid_Operator_Definition (Designator);
4578 end if;
4580 May_Need_Actuals (Designator);
4582 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4583 -- the subprogram is abstract also. This does not apply to renaming
4584 -- declarations, where abstractness is inherited, and to subprogram
4585 -- bodies generated for stream operations, which become renamings as
4586 -- bodies.
4588 -- In case of primitives associated with abstract interface types
4589 -- the check is applied later (see Analyze_Subprogram_Declaration).
4591 if not Nkind_In (Original_Node (Parent (N)),
4592 N_Subprogram_Renaming_Declaration,
4593 N_Abstract_Subprogram_Declaration,
4594 N_Formal_Abstract_Subprogram_Declaration)
4595 then
4596 if Is_Abstract_Type (Etype (Designator))
4597 and then not Is_Interface (Etype (Designator))
4598 then
4599 Error_Msg_N
4600 ("function that returns abstract type must be abstract", N);
4602 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4603 -- access result whose designated type is abstract.
4605 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4606 and then
4607 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4608 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4609 and then Ada_Version >= Ada_2012
4610 then
4611 Error_Msg_N ("function whose access result designates "
4612 & "abstract type must be abstract", N);
4613 end if;
4614 end if;
4615 end if;
4617 return Designator;
4618 end Analyze_Subprogram_Specification;
4620 -----------------------
4621 -- Check_Conformance --
4622 -----------------------
4624 procedure Check_Conformance
4625 (New_Id : Entity_Id;
4626 Old_Id : Entity_Id;
4627 Ctype : Conformance_Type;
4628 Errmsg : Boolean;
4629 Conforms : out Boolean;
4630 Err_Loc : Node_Id := Empty;
4631 Get_Inst : Boolean := False;
4632 Skip_Controlling_Formals : Boolean := False)
4634 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4635 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4636 -- If Errmsg is True, then processing continues to post an error message
4637 -- for conformance error on given node. Two messages are output. The
4638 -- first message points to the previous declaration with a general "no
4639 -- conformance" message. The second is the detailed reason, supplied as
4640 -- Msg. The parameter N provide information for a possible & insertion
4641 -- in the message, and also provides the location for posting the
4642 -- message in the absence of a specified Err_Loc location.
4644 -----------------------
4645 -- Conformance_Error --
4646 -----------------------
4648 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4649 Enode : Node_Id;
4651 begin
4652 Conforms := False;
4654 if Errmsg then
4655 if No (Err_Loc) then
4656 Enode := N;
4657 else
4658 Enode := Err_Loc;
4659 end if;
4661 Error_Msg_Sloc := Sloc (Old_Id);
4663 case Ctype is
4664 when Type_Conformant =>
4665 Error_Msg_N -- CODEFIX
4666 ("not type conformant with declaration#!", Enode);
4668 when Mode_Conformant =>
4669 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4670 Error_Msg_N
4671 ("not mode conformant with operation inherited#!",
4672 Enode);
4673 else
4674 Error_Msg_N
4675 ("not mode conformant with declaration#!", Enode);
4676 end if;
4678 when Subtype_Conformant =>
4679 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4680 Error_Msg_N
4681 ("not subtype conformant with operation inherited#!",
4682 Enode);
4683 else
4684 Error_Msg_N
4685 ("not subtype conformant with declaration#!", Enode);
4686 end if;
4688 when Fully_Conformant =>
4689 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4690 Error_Msg_N -- CODEFIX
4691 ("not fully conformant with operation inherited#!",
4692 Enode);
4693 else
4694 Error_Msg_N -- CODEFIX
4695 ("not fully conformant with declaration#!", Enode);
4696 end if;
4697 end case;
4699 Error_Msg_NE (Msg, Enode, N);
4700 end if;
4701 end Conformance_Error;
4703 -- Local Variables
4705 Old_Type : constant Entity_Id := Etype (Old_Id);
4706 New_Type : constant Entity_Id := Etype (New_Id);
4707 Old_Formal : Entity_Id;
4708 New_Formal : Entity_Id;
4709 Access_Types_Match : Boolean;
4710 Old_Formal_Base : Entity_Id;
4711 New_Formal_Base : Entity_Id;
4713 -- Start of processing for Check_Conformance
4715 begin
4716 Conforms := True;
4718 -- We need a special case for operators, since they don't appear
4719 -- explicitly.
4721 if Ctype = Type_Conformant then
4722 if Ekind (New_Id) = E_Operator
4723 and then Operator_Matches_Spec (New_Id, Old_Id)
4724 then
4725 return;
4726 end if;
4727 end if;
4729 -- If both are functions/operators, check return types conform
4731 if Old_Type /= Standard_Void_Type
4732 and then
4733 New_Type /= Standard_Void_Type
4734 then
4735 -- If we are checking interface conformance we omit controlling
4736 -- arguments and result, because we are only checking the conformance
4737 -- of the remaining parameters.
4739 if Has_Controlling_Result (Old_Id)
4740 and then Has_Controlling_Result (New_Id)
4741 and then Skip_Controlling_Formals
4742 then
4743 null;
4745 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4746 if Ctype >= Subtype_Conformant
4747 and then not Predicates_Match (Old_Type, New_Type)
4748 then
4749 Conformance_Error
4750 ("\predicate of return type does not match!", New_Id);
4751 else
4752 Conformance_Error
4753 ("\return type does not match!", New_Id);
4754 end if;
4756 return;
4757 end if;
4759 -- Ada 2005 (AI-231): In case of anonymous access types check the
4760 -- null-exclusion and access-to-constant attributes match.
4762 if Ada_Version >= Ada_2005
4763 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4764 and then
4765 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
4766 or else Is_Access_Constant (Etype (Old_Type)) /=
4767 Is_Access_Constant (Etype (New_Type)))
4768 then
4769 Conformance_Error ("\return type does not match!", New_Id);
4770 return;
4771 end if;
4773 -- If either is a function/operator and the other isn't, error
4775 elsif Old_Type /= Standard_Void_Type
4776 or else New_Type /= Standard_Void_Type
4777 then
4778 Conformance_Error ("\functions can only match functions!", New_Id);
4779 return;
4780 end if;
4782 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4783 -- If this is a renaming as body, refine error message to indicate that
4784 -- the conflict is with the original declaration. If the entity is not
4785 -- frozen, the conventions don't have to match, the one of the renamed
4786 -- entity is inherited.
4788 if Ctype >= Subtype_Conformant then
4789 if Convention (Old_Id) /= Convention (New_Id) then
4790 if not Is_Frozen (New_Id) then
4791 null;
4793 elsif Present (Err_Loc)
4794 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4795 and then Present (Corresponding_Spec (Err_Loc))
4796 then
4797 Error_Msg_Name_1 := Chars (New_Id);
4798 Error_Msg_Name_2 :=
4799 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4800 Conformance_Error ("\prior declaration for% has convention %!");
4802 else
4803 Conformance_Error ("\calling conventions do not match!");
4804 end if;
4806 return;
4808 elsif Is_Formal_Subprogram (Old_Id)
4809 or else Is_Formal_Subprogram (New_Id)
4810 then
4811 Conformance_Error ("\formal subprograms not allowed!");
4812 return;
4814 -- Pragma Ghost behaves as a convention in the context of subtype
4815 -- conformance (SPARK RM 6.9(5)). Do not check internally generated
4816 -- subprograms as their spec may reside in a Ghost region and their
4817 -- body not, or vice versa.
4819 elsif Comes_From_Source (Old_Id)
4820 and then Comes_From_Source (New_Id)
4821 and then Is_Ghost_Entity (Old_Id) /= Is_Ghost_Entity (New_Id)
4822 then
4823 Conformance_Error ("\ghost modes do not match!");
4824 return;
4825 end if;
4826 end if;
4828 -- Deal with parameters
4830 -- Note: we use the entity information, rather than going directly
4831 -- to the specification in the tree. This is not only simpler, but
4832 -- absolutely necessary for some cases of conformance tests between
4833 -- operators, where the declaration tree simply does not exist.
4835 Old_Formal := First_Formal (Old_Id);
4836 New_Formal := First_Formal (New_Id);
4837 while Present (Old_Formal) and then Present (New_Formal) loop
4838 if Is_Controlling_Formal (Old_Formal)
4839 and then Is_Controlling_Formal (New_Formal)
4840 and then Skip_Controlling_Formals
4841 then
4842 -- The controlling formals will have different types when
4843 -- comparing an interface operation with its match, but both
4844 -- or neither must be access parameters.
4846 if Is_Access_Type (Etype (Old_Formal))
4848 Is_Access_Type (Etype (New_Formal))
4849 then
4850 goto Skip_Controlling_Formal;
4851 else
4852 Conformance_Error
4853 ("\access parameter does not match!", New_Formal);
4854 end if;
4855 end if;
4857 -- Ada 2012: Mode conformance also requires that formal parameters
4858 -- be both aliased, or neither.
4860 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
4861 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
4862 Conformance_Error
4863 ("\aliased parameter mismatch!", New_Formal);
4864 end if;
4865 end if;
4867 if Ctype = Fully_Conformant then
4869 -- Names must match. Error message is more accurate if we do
4870 -- this before checking that the types of the formals match.
4872 if Chars (Old_Formal) /= Chars (New_Formal) then
4873 Conformance_Error ("\name& does not match!", New_Formal);
4875 -- Set error posted flag on new formal as well to stop
4876 -- junk cascaded messages in some cases.
4878 Set_Error_Posted (New_Formal);
4879 return;
4880 end if;
4882 -- Null exclusion must match
4884 if Null_Exclusion_Present (Parent (Old_Formal))
4886 Null_Exclusion_Present (Parent (New_Formal))
4887 then
4888 -- Only give error if both come from source. This should be
4889 -- investigated some time, since it should not be needed ???
4891 if Comes_From_Source (Old_Formal)
4892 and then
4893 Comes_From_Source (New_Formal)
4894 then
4895 Conformance_Error
4896 ("\null exclusion for& does not match", New_Formal);
4898 -- Mark error posted on the new formal to avoid duplicated
4899 -- complaint about types not matching.
4901 Set_Error_Posted (New_Formal);
4902 end if;
4903 end if;
4904 end if;
4906 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4907 -- case occurs whenever a subprogram is being renamed and one of its
4908 -- parameters imposes a null exclusion. For example:
4910 -- type T is null record;
4911 -- type Acc_T is access T;
4912 -- subtype Acc_T_Sub is Acc_T;
4914 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4915 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4916 -- renames P;
4918 Old_Formal_Base := Etype (Old_Formal);
4919 New_Formal_Base := Etype (New_Formal);
4921 if Get_Inst then
4922 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4923 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4924 end if;
4926 Access_Types_Match := Ada_Version >= Ada_2005
4928 -- Ensure that this rule is only applied when New_Id is a
4929 -- renaming of Old_Id.
4931 and then Nkind (Parent (Parent (New_Id))) =
4932 N_Subprogram_Renaming_Declaration
4933 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4934 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4935 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4937 -- Now handle the allowed access-type case
4939 and then Is_Access_Type (Old_Formal_Base)
4940 and then Is_Access_Type (New_Formal_Base)
4942 -- The type kinds must match. The only exception occurs with
4943 -- multiple generics of the form:
4945 -- generic generic
4946 -- type F is private; type A is private;
4947 -- type F_Ptr is access F; type A_Ptr is access A;
4948 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4949 -- package F_Pack is ... package A_Pack is
4950 -- package F_Inst is
4951 -- new F_Pack (A, A_Ptr, A_P);
4953 -- When checking for conformance between the parameters of A_P
4954 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4955 -- because the compiler has transformed A_Ptr into a subtype of
4956 -- F_Ptr. We catch this case in the code below.
4958 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4959 or else
4960 (Is_Generic_Type (Old_Formal_Base)
4961 and then Is_Generic_Type (New_Formal_Base)
4962 and then Is_Internal (New_Formal_Base)
4963 and then Etype (Etype (New_Formal_Base)) =
4964 Old_Formal_Base))
4965 and then Directly_Designated_Type (Old_Formal_Base) =
4966 Directly_Designated_Type (New_Formal_Base)
4967 and then ((Is_Itype (Old_Formal_Base)
4968 and then Can_Never_Be_Null (Old_Formal_Base))
4969 or else
4970 (Is_Itype (New_Formal_Base)
4971 and then Can_Never_Be_Null (New_Formal_Base)));
4973 -- Types must always match. In the visible part of an instance,
4974 -- usual overloading rules for dispatching operations apply, and
4975 -- we check base types (not the actual subtypes).
4977 if In_Instance_Visible_Part
4978 and then Is_Dispatching_Operation (New_Id)
4979 then
4980 if not Conforming_Types
4981 (T1 => Base_Type (Etype (Old_Formal)),
4982 T2 => Base_Type (Etype (New_Formal)),
4983 Ctype => Ctype,
4984 Get_Inst => Get_Inst)
4985 and then not Access_Types_Match
4986 then
4987 Conformance_Error ("\type of & does not match!", New_Formal);
4988 return;
4989 end if;
4991 elsif not Conforming_Types
4992 (T1 => Old_Formal_Base,
4993 T2 => New_Formal_Base,
4994 Ctype => Ctype,
4995 Get_Inst => Get_Inst)
4996 and then not Access_Types_Match
4997 then
4998 -- Don't give error message if old type is Any_Type. This test
4999 -- avoids some cascaded errors, e.g. in case of a bad spec.
5001 if Errmsg and then Old_Formal_Base = Any_Type then
5002 Conforms := False;
5003 else
5004 if Ctype >= Subtype_Conformant
5005 and then
5006 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5007 then
5008 Conformance_Error
5009 ("\predicate of & does not match!", New_Formal);
5010 else
5011 Conformance_Error
5012 ("\type of & does not match!", New_Formal);
5013 end if;
5014 end if;
5016 return;
5017 end if;
5019 -- For mode conformance, mode must match
5021 if Ctype >= Mode_Conformant then
5022 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5023 if not Ekind_In (New_Id, E_Function, E_Procedure)
5024 or else not Is_Primitive_Wrapper (New_Id)
5025 then
5026 Conformance_Error ("\mode of & does not match!", New_Formal);
5028 else
5029 declare
5030 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5031 begin
5032 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5033 then
5034 Error_Msg_PT (T, New_Id);
5035 else
5036 Conformance_Error
5037 ("\mode of & does not match!", New_Formal);
5038 end if;
5039 end;
5040 end if;
5042 return;
5044 -- Part of mode conformance for access types is having the same
5045 -- constant modifier.
5047 elsif Access_Types_Match
5048 and then Is_Access_Constant (Old_Formal_Base) /=
5049 Is_Access_Constant (New_Formal_Base)
5050 then
5051 Conformance_Error
5052 ("\constant modifier does not match!", New_Formal);
5053 return;
5054 end if;
5055 end if;
5057 if Ctype >= Subtype_Conformant then
5059 -- Ada 2005 (AI-231): In case of anonymous access types check
5060 -- the null-exclusion and access-to-constant attributes must
5061 -- match. For null exclusion, we test the types rather than the
5062 -- formals themselves, since the attribute is only set reliably
5063 -- on the formals in the Ada 95 case, and we exclude the case
5064 -- where Old_Formal is marked as controlling, to avoid errors
5065 -- when matching completing bodies with dispatching declarations
5066 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5068 if Ada_Version >= Ada_2005
5069 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5070 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5071 and then
5072 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5073 Can_Never_Be_Null (Etype (New_Formal))
5074 and then
5075 not Is_Controlling_Formal (Old_Formal))
5076 or else
5077 Is_Access_Constant (Etype (Old_Formal)) /=
5078 Is_Access_Constant (Etype (New_Formal)))
5080 -- Do not complain if error already posted on New_Formal. This
5081 -- avoids some redundant error messages.
5083 and then not Error_Posted (New_Formal)
5084 then
5085 -- It is allowed to omit the null-exclusion in case of stream
5086 -- attribute subprograms. We recognize stream subprograms
5087 -- through their TSS-generated suffix.
5089 declare
5090 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5092 begin
5093 if TSS_Name /= TSS_Stream_Read
5094 and then TSS_Name /= TSS_Stream_Write
5095 and then TSS_Name /= TSS_Stream_Input
5096 and then TSS_Name /= TSS_Stream_Output
5097 then
5098 -- Here we have a definite conformance error. It is worth
5099 -- special casing the error message for the case of a
5100 -- controlling formal (which excludes null).
5102 if Is_Controlling_Formal (New_Formal) then
5103 Error_Msg_Node_2 := Scope (New_Formal);
5104 Conformance_Error
5105 ("\controlling formal & of & excludes null, "
5106 & "declaration must exclude null as well",
5107 New_Formal);
5109 -- Normal case (couldn't we give more detail here???)
5111 else
5112 Conformance_Error
5113 ("\type of & does not match!", New_Formal);
5114 end if;
5116 return;
5117 end if;
5118 end;
5119 end if;
5120 end if;
5122 -- Full conformance checks
5124 if Ctype = Fully_Conformant then
5126 -- We have checked already that names match
5128 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5130 -- Check default expressions for in parameters
5132 declare
5133 NewD : constant Boolean :=
5134 Present (Default_Value (New_Formal));
5135 OldD : constant Boolean :=
5136 Present (Default_Value (Old_Formal));
5137 begin
5138 if NewD or OldD then
5140 -- The old default value has been analyzed because the
5141 -- current full declaration will have frozen everything
5142 -- before. The new default value has not been analyzed,
5143 -- so analyze it now before we check for conformance.
5145 if NewD then
5146 Push_Scope (New_Id);
5147 Preanalyze_Spec_Expression
5148 (Default_Value (New_Formal), Etype (New_Formal));
5149 End_Scope;
5150 end if;
5152 if not (NewD and OldD)
5153 or else not Fully_Conformant_Expressions
5154 (Default_Value (Old_Formal),
5155 Default_Value (New_Formal))
5156 then
5157 Conformance_Error
5158 ("\default expression for & does not match!",
5159 New_Formal);
5160 return;
5161 end if;
5162 end if;
5163 end;
5164 end if;
5165 end if;
5167 -- A couple of special checks for Ada 83 mode. These checks are
5168 -- skipped if either entity is an operator in package Standard,
5169 -- or if either old or new instance is not from the source program.
5171 if Ada_Version = Ada_83
5172 and then Sloc (Old_Id) > Standard_Location
5173 and then Sloc (New_Id) > Standard_Location
5174 and then Comes_From_Source (Old_Id)
5175 and then Comes_From_Source (New_Id)
5176 then
5177 declare
5178 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5179 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5181 begin
5182 -- Explicit IN must be present or absent in both cases. This
5183 -- test is required only in the full conformance case.
5185 if In_Present (Old_Param) /= In_Present (New_Param)
5186 and then Ctype = Fully_Conformant
5187 then
5188 Conformance_Error
5189 ("\(Ada 83) IN must appear in both declarations",
5190 New_Formal);
5191 return;
5192 end if;
5194 -- Grouping (use of comma in param lists) must be the same
5195 -- This is where we catch a misconformance like:
5197 -- A, B : Integer
5198 -- A : Integer; B : Integer
5200 -- which are represented identically in the tree except
5201 -- for the setting of the flags More_Ids and Prev_Ids.
5203 if More_Ids (Old_Param) /= More_Ids (New_Param)
5204 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5205 then
5206 Conformance_Error
5207 ("\grouping of & does not match!", New_Formal);
5208 return;
5209 end if;
5210 end;
5211 end if;
5213 -- This label is required when skipping controlling formals
5215 <<Skip_Controlling_Formal>>
5217 Next_Formal (Old_Formal);
5218 Next_Formal (New_Formal);
5219 end loop;
5221 if Present (Old_Formal) then
5222 Conformance_Error ("\too few parameters!");
5223 return;
5225 elsif Present (New_Formal) then
5226 Conformance_Error ("\too many parameters!", New_Formal);
5227 return;
5228 end if;
5229 end Check_Conformance;
5231 -----------------------
5232 -- Check_Conventions --
5233 -----------------------
5235 procedure Check_Conventions (Typ : Entity_Id) is
5236 Ifaces_List : Elist_Id;
5238 procedure Check_Convention (Op : Entity_Id);
5239 -- Verify that the convention of inherited dispatching operation Op is
5240 -- consistent among all subprograms it overrides. In order to minimize
5241 -- the search, Search_From is utilized to designate a specific point in
5242 -- the list rather than iterating over the whole list once more.
5244 ----------------------
5245 -- Check_Convention --
5246 ----------------------
5248 procedure Check_Convention (Op : Entity_Id) is
5249 Op_Conv : constant Convention_Id := Convention (Op);
5250 Iface_Conv : Convention_Id;
5251 Iface_Elmt : Elmt_Id;
5252 Iface_Prim_Elmt : Elmt_Id;
5253 Iface_Prim : Entity_Id;
5255 begin
5256 Iface_Elmt := First_Elmt (Ifaces_List);
5257 while Present (Iface_Elmt) loop
5258 Iface_Prim_Elmt :=
5259 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5260 while Present (Iface_Prim_Elmt) loop
5261 Iface_Prim := Node (Iface_Prim_Elmt);
5262 Iface_Conv := Convention (Iface_Prim);
5264 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5265 and then Iface_Conv /= Op_Conv
5266 then
5267 Error_Msg_N
5268 ("inconsistent conventions in primitive operations", Typ);
5270 Error_Msg_Name_1 := Chars (Op);
5271 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5272 Error_Msg_Sloc := Sloc (Op);
5274 if Comes_From_Source (Op) or else No (Alias (Op)) then
5275 if not Present (Overridden_Operation (Op)) then
5276 Error_Msg_N ("\\primitive % defined #", Typ);
5277 else
5278 Error_Msg_N
5279 ("\\overriding operation % with "
5280 & "convention % defined #", Typ);
5281 end if;
5283 else pragma Assert (Present (Alias (Op)));
5284 Error_Msg_Sloc := Sloc (Alias (Op));
5285 Error_Msg_N ("\\inherited operation % with "
5286 & "convention % defined #", Typ);
5287 end if;
5289 Error_Msg_Name_1 := Chars (Op);
5290 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5291 Error_Msg_Sloc := Sloc (Iface_Prim);
5292 Error_Msg_N ("\\overridden operation % with "
5293 & "convention % defined #", Typ);
5295 -- Avoid cascading errors
5297 return;
5298 end if;
5300 Next_Elmt (Iface_Prim_Elmt);
5301 end loop;
5303 Next_Elmt (Iface_Elmt);
5304 end loop;
5305 end Check_Convention;
5307 -- Local variables
5309 Prim_Op : Entity_Id;
5310 Prim_Op_Elmt : Elmt_Id;
5312 -- Start of processing for Check_Conventions
5314 begin
5315 if not Has_Interfaces (Typ) then
5316 return;
5317 end if;
5319 Collect_Interfaces (Typ, Ifaces_List);
5321 -- The algorithm checks every overriding dispatching operation against
5322 -- all the corresponding overridden dispatching operations, detecting
5323 -- differences in conventions.
5325 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5326 while Present (Prim_Op_Elmt) loop
5327 Prim_Op := Node (Prim_Op_Elmt);
5329 -- A small optimization: skip the predefined dispatching operations
5330 -- since they always have the same convention.
5332 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5333 Check_Convention (Prim_Op);
5334 end if;
5336 Next_Elmt (Prim_Op_Elmt);
5337 end loop;
5338 end Check_Conventions;
5340 ------------------------------
5341 -- Check_Delayed_Subprogram --
5342 ------------------------------
5344 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5345 F : Entity_Id;
5347 procedure Possible_Freeze (T : Entity_Id);
5348 -- T is the type of either a formal parameter or of the return type.
5349 -- If T is not yet frozen and needs a delayed freeze, then the
5350 -- subprogram itself must be delayed. If T is the limited view of an
5351 -- incomplete type the subprogram must be frozen as well, because
5352 -- T may depend on local types that have not been frozen yet.
5354 ---------------------
5355 -- Possible_Freeze --
5356 ---------------------
5358 procedure Possible_Freeze (T : Entity_Id) is
5359 begin
5360 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5361 Set_Has_Delayed_Freeze (Designator);
5363 elsif Is_Access_Type (T)
5364 and then Has_Delayed_Freeze (Designated_Type (T))
5365 and then not Is_Frozen (Designated_Type (T))
5366 then
5367 Set_Has_Delayed_Freeze (Designator);
5369 elsif Ekind (T) = E_Incomplete_Type
5370 and then From_Limited_With (T)
5371 then
5372 Set_Has_Delayed_Freeze (Designator);
5374 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5375 -- of a subprogram or entry declaration.
5377 elsif Ekind (T) = E_Incomplete_Type
5378 and then Ada_Version >= Ada_2012
5379 then
5380 Set_Has_Delayed_Freeze (Designator);
5381 end if;
5383 end Possible_Freeze;
5385 -- Start of processing for Check_Delayed_Subprogram
5387 begin
5388 -- All subprograms, including abstract subprograms, may need a freeze
5389 -- node if some formal type or the return type needs one.
5391 Possible_Freeze (Etype (Designator));
5392 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5394 -- Need delayed freeze if any of the formal types themselves need
5395 -- a delayed freeze and are not yet frozen.
5397 F := First_Formal (Designator);
5398 while Present (F) loop
5399 Possible_Freeze (Etype (F));
5400 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5401 Next_Formal (F);
5402 end loop;
5404 -- Mark functions that return by reference. Note that it cannot be
5405 -- done for delayed_freeze subprograms because the underlying
5406 -- returned type may not be known yet (for private types)
5408 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5409 declare
5410 Typ : constant Entity_Id := Etype (Designator);
5411 Utyp : constant Entity_Id := Underlying_Type (Typ);
5412 begin
5413 if Is_Limited_View (Typ) then
5414 Set_Returns_By_Ref (Designator);
5415 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5416 Set_Returns_By_Ref (Designator);
5417 end if;
5418 end;
5419 end if;
5420 end Check_Delayed_Subprogram;
5422 ------------------------------------
5423 -- Check_Discriminant_Conformance --
5424 ------------------------------------
5426 procedure Check_Discriminant_Conformance
5427 (N : Node_Id;
5428 Prev : Entity_Id;
5429 Prev_Loc : Node_Id)
5431 Old_Discr : Entity_Id := First_Discriminant (Prev);
5432 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5433 New_Discr_Id : Entity_Id;
5434 New_Discr_Type : Entity_Id;
5436 procedure Conformance_Error (Msg : String; N : Node_Id);
5437 -- Post error message for conformance error on given node. Two messages
5438 -- are output. The first points to the previous declaration with a
5439 -- general "no conformance" message. The second is the detailed reason,
5440 -- supplied as Msg. The parameter N provide information for a possible
5441 -- & insertion in the message.
5443 -----------------------
5444 -- Conformance_Error --
5445 -----------------------
5447 procedure Conformance_Error (Msg : String; N : Node_Id) is
5448 begin
5449 Error_Msg_Sloc := Sloc (Prev_Loc);
5450 Error_Msg_N -- CODEFIX
5451 ("not fully conformant with declaration#!", N);
5452 Error_Msg_NE (Msg, N, N);
5453 end Conformance_Error;
5455 -- Start of processing for Check_Discriminant_Conformance
5457 begin
5458 while Present (Old_Discr) and then Present (New_Discr) loop
5459 New_Discr_Id := Defining_Identifier (New_Discr);
5461 -- The subtype mark of the discriminant on the full type has not
5462 -- been analyzed so we do it here. For an access discriminant a new
5463 -- type is created.
5465 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5466 New_Discr_Type :=
5467 Access_Definition (N, Discriminant_Type (New_Discr));
5469 else
5470 Analyze (Discriminant_Type (New_Discr));
5471 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5473 -- Ada 2005: if the discriminant definition carries a null
5474 -- exclusion, create an itype to check properly for consistency
5475 -- with partial declaration.
5477 if Is_Access_Type (New_Discr_Type)
5478 and then Null_Exclusion_Present (New_Discr)
5479 then
5480 New_Discr_Type :=
5481 Create_Null_Excluding_Itype
5482 (T => New_Discr_Type,
5483 Related_Nod => New_Discr,
5484 Scope_Id => Current_Scope);
5485 end if;
5486 end if;
5488 if not Conforming_Types
5489 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5490 then
5491 Conformance_Error ("type of & does not match!", New_Discr_Id);
5492 return;
5493 else
5494 -- Treat the new discriminant as an occurrence of the old one,
5495 -- for navigation purposes, and fill in some semantic
5496 -- information, for completeness.
5498 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5499 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5500 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5501 end if;
5503 -- Names must match
5505 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5506 Conformance_Error ("name & does not match!", New_Discr_Id);
5507 return;
5508 end if;
5510 -- Default expressions must match
5512 declare
5513 NewD : constant Boolean :=
5514 Present (Expression (New_Discr));
5515 OldD : constant Boolean :=
5516 Present (Expression (Parent (Old_Discr)));
5518 begin
5519 if NewD or OldD then
5521 -- The old default value has been analyzed and expanded,
5522 -- because the current full declaration will have frozen
5523 -- everything before. The new default values have not been
5524 -- expanded, so expand now to check conformance.
5526 if NewD then
5527 Preanalyze_Spec_Expression
5528 (Expression (New_Discr), New_Discr_Type);
5529 end if;
5531 if not (NewD and OldD)
5532 or else not Fully_Conformant_Expressions
5533 (Expression (Parent (Old_Discr)),
5534 Expression (New_Discr))
5536 then
5537 Conformance_Error
5538 ("default expression for & does not match!",
5539 New_Discr_Id);
5540 return;
5541 end if;
5542 end if;
5543 end;
5545 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5547 if Ada_Version = Ada_83 then
5548 declare
5549 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5551 begin
5552 -- Grouping (use of comma in param lists) must be the same
5553 -- This is where we catch a misconformance like:
5555 -- A, B : Integer
5556 -- A : Integer; B : Integer
5558 -- which are represented identically in the tree except
5559 -- for the setting of the flags More_Ids and Prev_Ids.
5561 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5562 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5563 then
5564 Conformance_Error
5565 ("grouping of & does not match!", New_Discr_Id);
5566 return;
5567 end if;
5568 end;
5569 end if;
5571 Next_Discriminant (Old_Discr);
5572 Next (New_Discr);
5573 end loop;
5575 if Present (Old_Discr) then
5576 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5577 return;
5579 elsif Present (New_Discr) then
5580 Conformance_Error
5581 ("too many discriminants!", Defining_Identifier (New_Discr));
5582 return;
5583 end if;
5584 end Check_Discriminant_Conformance;
5586 ----------------------------
5587 -- Check_Fully_Conformant --
5588 ----------------------------
5590 procedure Check_Fully_Conformant
5591 (New_Id : Entity_Id;
5592 Old_Id : Entity_Id;
5593 Err_Loc : Node_Id := Empty)
5595 Result : Boolean;
5596 pragma Warnings (Off, Result);
5597 begin
5598 Check_Conformance
5599 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5600 end Check_Fully_Conformant;
5602 ---------------------------
5603 -- Check_Mode_Conformant --
5604 ---------------------------
5606 procedure Check_Mode_Conformant
5607 (New_Id : Entity_Id;
5608 Old_Id : Entity_Id;
5609 Err_Loc : Node_Id := Empty;
5610 Get_Inst : Boolean := False)
5612 Result : Boolean;
5613 pragma Warnings (Off, Result);
5614 begin
5615 Check_Conformance
5616 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
5617 end Check_Mode_Conformant;
5619 --------------------------------
5620 -- Check_Overriding_Indicator --
5621 --------------------------------
5623 procedure Check_Overriding_Indicator
5624 (Subp : Entity_Id;
5625 Overridden_Subp : Entity_Id;
5626 Is_Primitive : Boolean)
5628 Decl : Node_Id;
5629 Spec : Node_Id;
5631 begin
5632 -- No overriding indicator for literals
5634 if Ekind (Subp) = E_Enumeration_Literal then
5635 return;
5637 elsif Ekind (Subp) = E_Entry then
5638 Decl := Parent (Subp);
5640 -- No point in analyzing a malformed operator
5642 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5643 and then Error_Posted (Subp)
5644 then
5645 return;
5647 else
5648 Decl := Unit_Declaration_Node (Subp);
5649 end if;
5651 if Nkind_In (Decl, N_Subprogram_Body,
5652 N_Subprogram_Body_Stub,
5653 N_Subprogram_Declaration,
5654 N_Abstract_Subprogram_Declaration,
5655 N_Subprogram_Renaming_Declaration)
5656 then
5657 Spec := Specification (Decl);
5659 elsif Nkind (Decl) = N_Entry_Declaration then
5660 Spec := Decl;
5662 else
5663 return;
5664 end if;
5666 -- The overriding operation is type conformant with the overridden one,
5667 -- but the names of the formals are not required to match. If the names
5668 -- appear permuted in the overriding operation, this is a possible
5669 -- source of confusion that is worth diagnosing. Controlling formals
5670 -- often carry names that reflect the type, and it is not worthwhile
5671 -- requiring that their names match.
5673 if Present (Overridden_Subp)
5674 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5675 then
5676 declare
5677 Form1 : Entity_Id;
5678 Form2 : Entity_Id;
5680 begin
5681 Form1 := First_Formal (Subp);
5682 Form2 := First_Formal (Overridden_Subp);
5684 -- If the overriding operation is a synchronized operation, skip
5685 -- the first parameter of the overridden operation, which is
5686 -- implicit in the new one. If the operation is declared in the
5687 -- body it is not primitive and all formals must match.
5689 if Is_Concurrent_Type (Scope (Subp))
5690 and then Is_Tagged_Type (Scope (Subp))
5691 and then not Has_Completion (Scope (Subp))
5692 then
5693 Form2 := Next_Formal (Form2);
5694 end if;
5696 if Present (Form1) then
5697 Form1 := Next_Formal (Form1);
5698 Form2 := Next_Formal (Form2);
5699 end if;
5701 while Present (Form1) loop
5702 if not Is_Controlling_Formal (Form1)
5703 and then Present (Next_Formal (Form2))
5704 and then Chars (Form1) = Chars (Next_Formal (Form2))
5705 then
5706 Error_Msg_Node_2 := Alias (Overridden_Subp);
5707 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5708 Error_Msg_NE
5709 ("& does not match corresponding formal of&#",
5710 Form1, Form1);
5711 exit;
5712 end if;
5714 Next_Formal (Form1);
5715 Next_Formal (Form2);
5716 end loop;
5717 end;
5718 end if;
5720 -- If there is an overridden subprogram, then check that there is no
5721 -- "not overriding" indicator, and mark the subprogram as overriding.
5722 -- This is not done if the overridden subprogram is marked as hidden,
5723 -- which can occur for the case of inherited controlled operations
5724 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5725 -- subprogram is not itself hidden. (Note: This condition could probably
5726 -- be simplified, leaving out the testing for the specific controlled
5727 -- cases, but it seems safer and clearer this way, and echoes similar
5728 -- special-case tests of this kind in other places.)
5730 if Present (Overridden_Subp)
5731 and then (not Is_Hidden (Overridden_Subp)
5732 or else
5733 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
5734 Name_Adjust,
5735 Name_Finalize)
5736 and then Present (Alias (Overridden_Subp))
5737 and then not Is_Hidden (Alias (Overridden_Subp))))
5738 then
5739 if Must_Not_Override (Spec) then
5740 Error_Msg_Sloc := Sloc (Overridden_Subp);
5742 if Ekind (Subp) = E_Entry then
5743 Error_Msg_NE
5744 ("entry & overrides inherited operation #", Spec, Subp);
5745 else
5746 Error_Msg_NE
5747 ("subprogram & overrides inherited operation #", Spec, Subp);
5748 end if;
5750 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5751 -- as an extension of Root_Controlled, and thus has a useless Adjust
5752 -- operation. This operation should not be inherited by other limited
5753 -- controlled types. An explicit Adjust for them is not overriding.
5755 elsif Must_Override (Spec)
5756 and then Chars (Overridden_Subp) = Name_Adjust
5757 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5758 and then Present (Alias (Overridden_Subp))
5759 and then
5760 Is_Predefined_File_Name
5761 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5762 then
5763 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5765 elsif Is_Subprogram (Subp) then
5766 if Is_Init_Proc (Subp) then
5767 null;
5769 elsif No (Overridden_Operation (Subp)) then
5771 -- For entities generated by Derive_Subprograms the overridden
5772 -- operation is the inherited primitive (which is available
5773 -- through the attribute alias)
5775 if (Is_Dispatching_Operation (Subp)
5776 or else Is_Dispatching_Operation (Overridden_Subp))
5777 and then not Comes_From_Source (Overridden_Subp)
5778 and then Find_Dispatching_Type (Overridden_Subp) =
5779 Find_Dispatching_Type (Subp)
5780 and then Present (Alias (Overridden_Subp))
5781 and then Comes_From_Source (Alias (Overridden_Subp))
5782 then
5783 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5784 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
5786 else
5787 Set_Overridden_Operation (Subp, Overridden_Subp);
5788 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
5789 end if;
5790 end if;
5791 end if;
5793 -- If primitive flag is set or this is a protected operation, then
5794 -- the operation is overriding at the point of its declaration, so
5795 -- warn if necessary. Otherwise it may have been declared before the
5796 -- operation it overrides and no check is required.
5798 if Style_Check
5799 and then not Must_Override (Spec)
5800 and then (Is_Primitive
5801 or else Ekind (Scope (Subp)) = E_Protected_Type)
5802 then
5803 Style.Missing_Overriding (Decl, Subp);
5804 end if;
5806 -- If Subp is an operator, it may override a predefined operation, if
5807 -- it is defined in the same scope as the type to which it applies.
5808 -- In that case Overridden_Subp is empty because of our implicit
5809 -- representation for predefined operators. We have to check whether the
5810 -- signature of Subp matches that of a predefined operator. Note that
5811 -- first argument provides the name of the operator, and the second
5812 -- argument the signature that may match that of a standard operation.
5813 -- If the indicator is overriding, then the operator must match a
5814 -- predefined signature, because we know already that there is no
5815 -- explicit overridden operation.
5817 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5818 if Must_Not_Override (Spec) then
5820 -- If this is not a primitive or a protected subprogram, then
5821 -- "not overriding" is illegal.
5823 if not Is_Primitive
5824 and then Ekind (Scope (Subp)) /= E_Protected_Type
5825 then
5826 Error_Msg_N ("overriding indicator only allowed "
5827 & "if subprogram is primitive", Subp);
5829 elsif Can_Override_Operator (Subp) then
5830 Error_Msg_NE
5831 ("subprogram& overrides predefined operator ", Spec, Subp);
5832 end if;
5834 elsif Must_Override (Spec) then
5835 if No (Overridden_Operation (Subp))
5836 and then not Can_Override_Operator (Subp)
5837 then
5838 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5839 end if;
5841 elsif not Error_Posted (Subp)
5842 and then Style_Check
5843 and then Can_Override_Operator (Subp)
5844 and then
5845 not Is_Predefined_File_Name
5846 (Unit_File_Name (Get_Source_Unit (Subp)))
5847 then
5848 -- If style checks are enabled, indicate that the indicator is
5849 -- missing. However, at the point of declaration, the type of
5850 -- which this is a primitive operation may be private, in which
5851 -- case the indicator would be premature.
5853 if Has_Private_Declaration (Etype (Subp))
5854 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5855 then
5856 null;
5857 else
5858 Style.Missing_Overriding (Decl, Subp);
5859 end if;
5860 end if;
5862 elsif Must_Override (Spec) then
5863 if Ekind (Subp) = E_Entry then
5864 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5865 else
5866 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5867 end if;
5869 -- If the operation is marked "not overriding" and it's not primitive
5870 -- then an error is issued, unless this is an operation of a task or
5871 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5872 -- has been specified have already been checked above.
5874 elsif Must_Not_Override (Spec)
5875 and then not Is_Primitive
5876 and then Ekind (Subp) /= E_Entry
5877 and then Ekind (Scope (Subp)) /= E_Protected_Type
5878 then
5879 Error_Msg_N
5880 ("overriding indicator only allowed if subprogram is primitive",
5881 Subp);
5882 return;
5883 end if;
5884 end Check_Overriding_Indicator;
5886 -------------------
5887 -- Check_Returns --
5888 -------------------
5890 -- Note: this procedure needs to know far too much about how the expander
5891 -- messes with exceptions. The use of the flag Exception_Junk and the
5892 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5893 -- works, but is not very clean. It would be better if the expansion
5894 -- routines would leave Original_Node working nicely, and we could use
5895 -- Original_Node here to ignore all the peculiar expander messing ???
5897 procedure Check_Returns
5898 (HSS : Node_Id;
5899 Mode : Character;
5900 Err : out Boolean;
5901 Proc : Entity_Id := Empty)
5903 Handler : Node_Id;
5905 procedure Check_Statement_Sequence (L : List_Id);
5906 -- Internal recursive procedure to check a list of statements for proper
5907 -- termination by a return statement (or a transfer of control or a
5908 -- compound statement that is itself internally properly terminated).
5910 ------------------------------
5911 -- Check_Statement_Sequence --
5912 ------------------------------
5914 procedure Check_Statement_Sequence (L : List_Id) is
5915 Last_Stm : Node_Id;
5916 Stm : Node_Id;
5917 Kind : Node_Kind;
5919 function Assert_False return Boolean;
5920 -- Returns True if Last_Stm is a pragma Assert (False) that has been
5921 -- rewritten as a null statement when assertions are off. The assert
5922 -- is not active, but it is still enough to kill the warning.
5924 ------------------
5925 -- Assert_False --
5926 ------------------
5928 function Assert_False return Boolean is
5929 Orig : constant Node_Id := Original_Node (Last_Stm);
5931 begin
5932 if Nkind (Orig) = N_Pragma
5933 and then Pragma_Name (Orig) = Name_Assert
5934 and then not Error_Posted (Orig)
5935 then
5936 declare
5937 Arg : constant Node_Id :=
5938 First (Pragma_Argument_Associations (Orig));
5939 Exp : constant Node_Id := Expression (Arg);
5940 begin
5941 return Nkind (Exp) = N_Identifier
5942 and then Chars (Exp) = Name_False;
5943 end;
5945 else
5946 return False;
5947 end if;
5948 end Assert_False;
5950 -- Local variables
5952 Raise_Exception_Call : Boolean;
5953 -- Set True if statement sequence terminated by Raise_Exception call
5954 -- or a Reraise_Occurrence call.
5956 -- Start of processing for Check_Statement_Sequence
5958 begin
5959 Raise_Exception_Call := False;
5961 -- Get last real statement
5963 Last_Stm := Last (L);
5965 -- Deal with digging out exception handler statement sequences that
5966 -- have been transformed by the local raise to goto optimization.
5967 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5968 -- optimization has occurred, we are looking at something like:
5970 -- begin
5971 -- original stmts in block
5973 -- exception \
5974 -- when excep1 => |
5975 -- goto L1; | omitted if No_Exception_Propagation
5976 -- when excep2 => |
5977 -- goto L2; /
5978 -- end;
5980 -- goto L3; -- skip handler when exception not raised
5982 -- <<L1>> -- target label for local exception
5983 -- begin
5984 -- estmts1
5985 -- end;
5987 -- goto L3;
5989 -- <<L2>>
5990 -- begin
5991 -- estmts2
5992 -- end;
5994 -- <<L3>>
5996 -- and what we have to do is to dig out the estmts1 and estmts2
5997 -- sequences (which were the original sequences of statements in
5998 -- the exception handlers) and check them.
6000 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6001 Stm := Last_Stm;
6002 loop
6003 Prev (Stm);
6004 exit when No (Stm);
6005 exit when Nkind (Stm) /= N_Block_Statement;
6006 exit when not Exception_Junk (Stm);
6007 Prev (Stm);
6008 exit when No (Stm);
6009 exit when Nkind (Stm) /= N_Label;
6010 exit when not Exception_Junk (Stm);
6011 Check_Statement_Sequence
6012 (Statements (Handled_Statement_Sequence (Next (Stm))));
6014 Prev (Stm);
6015 Last_Stm := Stm;
6016 exit when No (Stm);
6017 exit when Nkind (Stm) /= N_Goto_Statement;
6018 exit when not Exception_Junk (Stm);
6019 end loop;
6020 end if;
6022 -- Don't count pragmas
6024 while Nkind (Last_Stm) = N_Pragma
6026 -- Don't count call to SS_Release (can happen after Raise_Exception)
6028 or else
6029 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6030 and then
6031 Nkind (Name (Last_Stm)) = N_Identifier
6032 and then
6033 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6035 -- Don't count exception junk
6037 or else
6038 (Nkind_In (Last_Stm, N_Goto_Statement,
6039 N_Label,
6040 N_Object_Declaration)
6041 and then Exception_Junk (Last_Stm))
6042 or else Nkind (Last_Stm) in N_Push_xxx_Label
6043 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6045 -- Inserted code, such as finalization calls, is irrelevant: we only
6046 -- need to check original source.
6048 or else Is_Rewrite_Insertion (Last_Stm)
6049 loop
6050 Prev (Last_Stm);
6051 end loop;
6053 -- Here we have the "real" last statement
6055 Kind := Nkind (Last_Stm);
6057 -- Transfer of control, OK. Note that in the No_Return procedure
6058 -- case, we already diagnosed any explicit return statements, so
6059 -- we can treat them as OK in this context.
6061 if Is_Transfer (Last_Stm) then
6062 return;
6064 -- Check cases of explicit non-indirect procedure calls
6066 elsif Kind = N_Procedure_Call_Statement
6067 and then Is_Entity_Name (Name (Last_Stm))
6068 then
6069 -- Check call to Raise_Exception procedure which is treated
6070 -- specially, as is a call to Reraise_Occurrence.
6072 -- We suppress the warning in these cases since it is likely that
6073 -- the programmer really does not expect to deal with the case
6074 -- of Null_Occurrence, and thus would find a warning about a
6075 -- missing return curious, and raising Program_Error does not
6076 -- seem such a bad behavior if this does occur.
6078 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6079 -- behavior will be to raise Constraint_Error (see AI-329).
6081 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6082 or else
6083 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6084 then
6085 Raise_Exception_Call := True;
6087 -- For Raise_Exception call, test first argument, if it is
6088 -- an attribute reference for a 'Identity call, then we know
6089 -- that the call cannot possibly return.
6091 declare
6092 Arg : constant Node_Id :=
6093 Original_Node (First_Actual (Last_Stm));
6094 begin
6095 if Nkind (Arg) = N_Attribute_Reference
6096 and then Attribute_Name (Arg) = Name_Identity
6097 then
6098 return;
6099 end if;
6100 end;
6101 end if;
6103 -- If statement, need to look inside if there is an else and check
6104 -- each constituent statement sequence for proper termination.
6106 elsif Kind = N_If_Statement
6107 and then Present (Else_Statements (Last_Stm))
6108 then
6109 Check_Statement_Sequence (Then_Statements (Last_Stm));
6110 Check_Statement_Sequence (Else_Statements (Last_Stm));
6112 if Present (Elsif_Parts (Last_Stm)) then
6113 declare
6114 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6116 begin
6117 while Present (Elsif_Part) loop
6118 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6119 Next (Elsif_Part);
6120 end loop;
6121 end;
6122 end if;
6124 return;
6126 -- Case statement, check each case for proper termination
6128 elsif Kind = N_Case_Statement then
6129 declare
6130 Case_Alt : Node_Id;
6131 begin
6132 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6133 while Present (Case_Alt) loop
6134 Check_Statement_Sequence (Statements (Case_Alt));
6135 Next_Non_Pragma (Case_Alt);
6136 end loop;
6137 end;
6139 return;
6141 -- Block statement, check its handled sequence of statements
6143 elsif Kind = N_Block_Statement then
6144 declare
6145 Err1 : Boolean;
6147 begin
6148 Check_Returns
6149 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6151 if Err1 then
6152 Err := True;
6153 end if;
6155 return;
6156 end;
6158 -- Loop statement. If there is an iteration scheme, we can definitely
6159 -- fall out of the loop. Similarly if there is an exit statement, we
6160 -- can fall out. In either case we need a following return.
6162 elsif Kind = N_Loop_Statement then
6163 if Present (Iteration_Scheme (Last_Stm))
6164 or else Has_Exit (Entity (Identifier (Last_Stm)))
6165 then
6166 null;
6168 -- A loop with no exit statement or iteration scheme is either
6169 -- an infinite loop, or it has some other exit (raise/return).
6170 -- In either case, no warning is required.
6172 else
6173 return;
6174 end if;
6176 -- Timed entry call, check entry call and delay alternatives
6178 -- Note: in expanded code, the timed entry call has been converted
6179 -- to a set of expanded statements on which the check will work
6180 -- correctly in any case.
6182 elsif Kind = N_Timed_Entry_Call then
6183 declare
6184 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6185 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6187 begin
6188 -- If statement sequence of entry call alternative is missing,
6189 -- then we can definitely fall through, and we post the error
6190 -- message on the entry call alternative itself.
6192 if No (Statements (ECA)) then
6193 Last_Stm := ECA;
6195 -- If statement sequence of delay alternative is missing, then
6196 -- we can definitely fall through, and we post the error
6197 -- message on the delay alternative itself.
6199 -- Note: if both ECA and DCA are missing the return, then we
6200 -- post only one message, should be enough to fix the bugs.
6201 -- If not we will get a message next time on the DCA when the
6202 -- ECA is fixed.
6204 elsif No (Statements (DCA)) then
6205 Last_Stm := DCA;
6207 -- Else check both statement sequences
6209 else
6210 Check_Statement_Sequence (Statements (ECA));
6211 Check_Statement_Sequence (Statements (DCA));
6212 return;
6213 end if;
6214 end;
6216 -- Conditional entry call, check entry call and else part
6218 -- Note: in expanded code, the conditional entry call has been
6219 -- converted to a set of expanded statements on which the check
6220 -- will work correctly in any case.
6222 elsif Kind = N_Conditional_Entry_Call then
6223 declare
6224 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6226 begin
6227 -- If statement sequence of entry call alternative is missing,
6228 -- then we can definitely fall through, and we post the error
6229 -- message on the entry call alternative itself.
6231 if No (Statements (ECA)) then
6232 Last_Stm := ECA;
6234 -- Else check statement sequence and else part
6236 else
6237 Check_Statement_Sequence (Statements (ECA));
6238 Check_Statement_Sequence (Else_Statements (Last_Stm));
6239 return;
6240 end if;
6241 end;
6242 end if;
6244 -- If we fall through, issue appropriate message
6246 if Mode = 'F' then
6248 -- Kill warning if last statement is a raise exception call,
6249 -- or a pragma Assert (False). Note that with assertions enabled,
6250 -- such a pragma has been converted into a raise exception call
6251 -- already, so the Assert_False is for the assertions off case.
6253 if not Raise_Exception_Call and then not Assert_False then
6255 -- In GNATprove mode, it is an error to have a missing return
6257 Error_Msg_Warn := SPARK_Mode /= On;
6259 -- Issue error message or warning
6261 Error_Msg_N
6262 ("RETURN statement missing following this statement<<!",
6263 Last_Stm);
6264 Error_Msg_N
6265 ("\Program_Error ]<<!", Last_Stm);
6266 end if;
6268 -- Note: we set Err even though we have not issued a warning
6269 -- because we still have a case of a missing return. This is
6270 -- an extremely marginal case, probably will never be noticed
6271 -- but we might as well get it right.
6273 Err := True;
6275 -- Otherwise we have the case of a procedure marked No_Return
6277 else
6278 if not Raise_Exception_Call then
6279 if GNATprove_Mode then
6280 Error_Msg_N
6281 ("implied return after this statement "
6282 & "would have raised Program_Error", Last_Stm);
6283 else
6284 Error_Msg_N
6285 ("implied return after this statement "
6286 & "will raise Program_Error??", Last_Stm);
6287 end if;
6289 Error_Msg_Warn := SPARK_Mode /= On;
6290 Error_Msg_NE
6291 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6292 end if;
6294 declare
6295 RE : constant Node_Id :=
6296 Make_Raise_Program_Error (Sloc (Last_Stm),
6297 Reason => PE_Implicit_Return);
6298 begin
6299 Insert_After (Last_Stm, RE);
6300 Analyze (RE);
6301 end;
6302 end if;
6303 end Check_Statement_Sequence;
6305 -- Start of processing for Check_Returns
6307 begin
6308 Err := False;
6309 Check_Statement_Sequence (Statements (HSS));
6311 if Present (Exception_Handlers (HSS)) then
6312 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6313 while Present (Handler) loop
6314 Check_Statement_Sequence (Statements (Handler));
6315 Next_Non_Pragma (Handler);
6316 end loop;
6317 end if;
6318 end Check_Returns;
6320 ----------------------------
6321 -- Check_Subprogram_Order --
6322 ----------------------------
6324 procedure Check_Subprogram_Order (N : Node_Id) is
6326 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6327 -- This is used to check if S1 > S2 in the sense required by this test,
6328 -- for example nameab < namec, but name2 < name10.
6330 -----------------------------
6331 -- Subprogram_Name_Greater --
6332 -----------------------------
6334 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6335 L1, L2 : Positive;
6336 N1, N2 : Natural;
6338 begin
6339 -- Deal with special case where names are identical except for a
6340 -- numerical suffix. These are handled specially, taking the numeric
6341 -- ordering from the suffix into account.
6343 L1 := S1'Last;
6344 while S1 (L1) in '0' .. '9' loop
6345 L1 := L1 - 1;
6346 end loop;
6348 L2 := S2'Last;
6349 while S2 (L2) in '0' .. '9' loop
6350 L2 := L2 - 1;
6351 end loop;
6353 -- If non-numeric parts non-equal, do straight compare
6355 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6356 return S1 > S2;
6358 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6359 -- that a missing suffix is treated as numeric zero in this test.
6361 else
6362 N1 := 0;
6363 while L1 < S1'Last loop
6364 L1 := L1 + 1;
6365 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6366 end loop;
6368 N2 := 0;
6369 while L2 < S2'Last loop
6370 L2 := L2 + 1;
6371 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6372 end loop;
6374 return N1 > N2;
6375 end if;
6376 end Subprogram_Name_Greater;
6378 -- Start of processing for Check_Subprogram_Order
6380 begin
6381 -- Check body in alpha order if this is option
6383 if Style_Check
6384 and then Style_Check_Order_Subprograms
6385 and then Nkind (N) = N_Subprogram_Body
6386 and then Comes_From_Source (N)
6387 and then In_Extended_Main_Source_Unit (N)
6388 then
6389 declare
6390 LSN : String_Ptr
6391 renames Scope_Stack.Table
6392 (Scope_Stack.Last).Last_Subprogram_Name;
6394 Body_Id : constant Entity_Id :=
6395 Defining_Entity (Specification (N));
6397 begin
6398 Get_Decoded_Name_String (Chars (Body_Id));
6400 if LSN /= null then
6401 if Subprogram_Name_Greater
6402 (LSN.all, Name_Buffer (1 .. Name_Len))
6403 then
6404 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6405 end if;
6407 Free (LSN);
6408 end if;
6410 LSN := new String'(Name_Buffer (1 .. Name_Len));
6411 end;
6412 end if;
6413 end Check_Subprogram_Order;
6415 ------------------------------
6416 -- Check_Subtype_Conformant --
6417 ------------------------------
6419 procedure Check_Subtype_Conformant
6420 (New_Id : Entity_Id;
6421 Old_Id : Entity_Id;
6422 Err_Loc : Node_Id := Empty;
6423 Skip_Controlling_Formals : Boolean := False;
6424 Get_Inst : Boolean := False)
6426 Result : Boolean;
6427 pragma Warnings (Off, Result);
6428 begin
6429 Check_Conformance
6430 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6431 Skip_Controlling_Formals => Skip_Controlling_Formals,
6432 Get_Inst => Get_Inst);
6433 end Check_Subtype_Conformant;
6435 ---------------------------
6436 -- Check_Type_Conformant --
6437 ---------------------------
6439 procedure Check_Type_Conformant
6440 (New_Id : Entity_Id;
6441 Old_Id : Entity_Id;
6442 Err_Loc : Node_Id := Empty)
6444 Result : Boolean;
6445 pragma Warnings (Off, Result);
6446 begin
6447 Check_Conformance
6448 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
6449 end Check_Type_Conformant;
6451 ---------------------------
6452 -- Can_Override_Operator --
6453 ---------------------------
6455 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
6456 Typ : Entity_Id;
6458 begin
6459 if Nkind (Subp) /= N_Defining_Operator_Symbol then
6460 return False;
6462 else
6463 Typ := Base_Type (Etype (First_Formal (Subp)));
6465 -- Check explicitly that the operation is a primitive of the type
6467 return Operator_Matches_Spec (Subp, Subp)
6468 and then not Is_Generic_Type (Typ)
6469 and then Scope (Subp) = Scope (Typ)
6470 and then not Is_Class_Wide_Type (Typ);
6471 end if;
6472 end Can_Override_Operator;
6474 ----------------------
6475 -- Conforming_Types --
6476 ----------------------
6478 function Conforming_Types
6479 (T1 : Entity_Id;
6480 T2 : Entity_Id;
6481 Ctype : Conformance_Type;
6482 Get_Inst : Boolean := False) return Boolean
6484 Type_1 : Entity_Id := T1;
6485 Type_2 : Entity_Id := T2;
6486 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6488 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6489 -- If neither T1 nor T2 are generic actual types, or if they are in
6490 -- different scopes (e.g. parent and child instances), then verify that
6491 -- the base types are equal. Otherwise T1 and T2 must be on the same
6492 -- subtype chain. The whole purpose of this procedure is to prevent
6493 -- spurious ambiguities in an instantiation that may arise if two
6494 -- distinct generic types are instantiated with the same actual.
6496 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6497 -- An access parameter can designate an incomplete type. If the
6498 -- incomplete type is the limited view of a type from a limited_
6499 -- with_clause, check whether the non-limited view is available. If
6500 -- it is a (non-limited) incomplete type, get the full view.
6502 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6503 -- Returns True if and only if either T1 denotes a limited view of T2
6504 -- or T2 denotes a limited view of T1. This can arise when the limited
6505 -- with view of a type is used in a subprogram declaration and the
6506 -- subprogram body is in the scope of a regular with clause for the
6507 -- same unit. In such a case, the two type entities can be considered
6508 -- identical for purposes of conformance checking.
6510 ----------------------
6511 -- Base_Types_Match --
6512 ----------------------
6514 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6515 BT1 : constant Entity_Id := Base_Type (T1);
6516 BT2 : constant Entity_Id := Base_Type (T2);
6518 begin
6519 if T1 = T2 then
6520 return True;
6522 elsif BT1 = BT2 then
6524 -- The following is too permissive. A more precise test should
6525 -- check that the generic actual is an ancestor subtype of the
6526 -- other ???.
6528 -- See code in Find_Corresponding_Spec that applies an additional
6529 -- filter to handle accidental amiguities in instances.
6531 return not Is_Generic_Actual_Type (T1)
6532 or else not Is_Generic_Actual_Type (T2)
6533 or else Scope (T1) /= Scope (T2);
6535 -- If T2 is a generic actual type it is declared as the subtype of
6536 -- the actual. If that actual is itself a subtype we need to use its
6537 -- own base type to check for compatibility.
6539 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
6540 return True;
6542 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
6543 return True;
6545 else
6546 return False;
6547 end if;
6548 end Base_Types_Match;
6550 --------------------------
6551 -- Find_Designated_Type --
6552 --------------------------
6554 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6555 Desig : Entity_Id;
6557 begin
6558 Desig := Directly_Designated_Type (T);
6560 if Ekind (Desig) = E_Incomplete_Type then
6562 -- If regular incomplete type, get full view if available
6564 if Present (Full_View (Desig)) then
6565 Desig := Full_View (Desig);
6567 -- If limited view of a type, get non-limited view if available,
6568 -- and check again for a regular incomplete type.
6570 elsif Present (Non_Limited_View (Desig)) then
6571 Desig := Get_Full_View (Non_Limited_View (Desig));
6572 end if;
6573 end if;
6575 return Desig;
6576 end Find_Designated_Type;
6578 -------------------------------
6579 -- Matches_Limited_With_View --
6580 -------------------------------
6582 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6583 begin
6584 -- In some cases a type imported through a limited_with clause, and
6585 -- its nonlimited view are both visible, for example in an anonymous
6586 -- access-to-class-wide type in a formal. Both entities designate the
6587 -- same type.
6589 if From_Limited_With (T1) and then T2 = Available_View (T1) then
6590 return True;
6592 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
6593 return True;
6595 elsif From_Limited_With (T1)
6596 and then From_Limited_With (T2)
6597 and then Available_View (T1) = Available_View (T2)
6598 then
6599 return True;
6601 else
6602 return False;
6603 end if;
6604 end Matches_Limited_With_View;
6606 -- Start of processing for Conforming_Types
6608 begin
6609 -- The context is an instance association for a formal access-to-
6610 -- subprogram type; the formal parameter types require mapping because
6611 -- they may denote other formal parameters of the generic unit.
6613 if Get_Inst then
6614 Type_1 := Get_Instance_Of (T1);
6615 Type_2 := Get_Instance_Of (T2);
6616 end if;
6618 -- If one of the types is a view of the other introduced by a limited
6619 -- with clause, treat these as conforming for all purposes.
6621 if Matches_Limited_With_View (T1, T2) then
6622 return True;
6624 elsif Base_Types_Match (Type_1, Type_2) then
6625 return Ctype <= Mode_Conformant
6626 or else Subtypes_Statically_Match (Type_1, Type_2);
6628 elsif Is_Incomplete_Or_Private_Type (Type_1)
6629 and then Present (Full_View (Type_1))
6630 and then Base_Types_Match (Full_View (Type_1), Type_2)
6631 then
6632 return Ctype <= Mode_Conformant
6633 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6635 elsif Ekind (Type_2) = E_Incomplete_Type
6636 and then Present (Full_View (Type_2))
6637 and then Base_Types_Match (Type_1, Full_View (Type_2))
6638 then
6639 return Ctype <= Mode_Conformant
6640 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6642 elsif Is_Private_Type (Type_2)
6643 and then In_Instance
6644 and then Present (Full_View (Type_2))
6645 and then Base_Types_Match (Type_1, Full_View (Type_2))
6646 then
6647 return Ctype <= Mode_Conformant
6648 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6649 end if;
6651 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6652 -- treated recursively because they carry a signature. As far as
6653 -- conformance is concerned, convention plays no role, and either
6654 -- or both could be access to protected subprograms.
6656 Are_Anonymous_Access_To_Subprogram_Types :=
6657 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
6658 E_Anonymous_Access_Protected_Subprogram_Type)
6659 and then
6660 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
6661 E_Anonymous_Access_Protected_Subprogram_Type);
6663 -- Test anonymous access type case. For this case, static subtype
6664 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6665 -- the base types because we may have built internal subtype entities
6666 -- to handle null-excluding types (see Process_Formals).
6668 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6669 and then
6670 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6672 -- Ada 2005 (AI-254)
6674 or else Are_Anonymous_Access_To_Subprogram_Types
6675 then
6676 declare
6677 Desig_1 : Entity_Id;
6678 Desig_2 : Entity_Id;
6680 begin
6681 -- In Ada 2005, access constant indicators must match for
6682 -- subtype conformance.
6684 if Ada_Version >= Ada_2005
6685 and then Ctype >= Subtype_Conformant
6686 and then
6687 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6688 then
6689 return False;
6690 end if;
6692 Desig_1 := Find_Designated_Type (Type_1);
6693 Desig_2 := Find_Designated_Type (Type_2);
6695 -- If the context is an instance association for a formal
6696 -- access-to-subprogram type; formal access parameter designated
6697 -- types require mapping because they may denote other formal
6698 -- parameters of the generic unit.
6700 if Get_Inst then
6701 Desig_1 := Get_Instance_Of (Desig_1);
6702 Desig_2 := Get_Instance_Of (Desig_2);
6703 end if;
6705 -- It is possible for a Class_Wide_Type to be introduced for an
6706 -- incomplete type, in which case there is a separate class_ wide
6707 -- type for the full view. The types conform if their Etypes
6708 -- conform, i.e. one may be the full view of the other. This can
6709 -- only happen in the context of an access parameter, other uses
6710 -- of an incomplete Class_Wide_Type are illegal.
6712 if Is_Class_Wide_Type (Desig_1)
6713 and then
6714 Is_Class_Wide_Type (Desig_2)
6715 then
6716 return
6717 Conforming_Types
6718 (Etype (Base_Type (Desig_1)),
6719 Etype (Base_Type (Desig_2)), Ctype);
6721 elsif Are_Anonymous_Access_To_Subprogram_Types then
6722 if Ada_Version < Ada_2005 then
6723 return Ctype = Type_Conformant
6724 or else
6725 Subtypes_Statically_Match (Desig_1, Desig_2);
6727 -- We must check the conformance of the signatures themselves
6729 else
6730 declare
6731 Conformant : Boolean;
6732 begin
6733 Check_Conformance
6734 (Desig_1, Desig_2, Ctype, False, Conformant);
6735 return Conformant;
6736 end;
6737 end if;
6739 else
6740 return Base_Type (Desig_1) = Base_Type (Desig_2)
6741 and then (Ctype = Type_Conformant
6742 or else
6743 Subtypes_Statically_Match (Desig_1, Desig_2));
6744 end if;
6745 end;
6747 -- Otherwise definitely no match
6749 else
6750 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6751 and then Is_Access_Type (Type_2))
6752 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6753 and then Is_Access_Type (Type_1)))
6754 and then
6755 Conforming_Types
6756 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6757 then
6758 May_Hide_Profile := True;
6759 end if;
6761 return False;
6762 end if;
6763 end Conforming_Types;
6765 --------------------------
6766 -- Create_Extra_Formals --
6767 --------------------------
6769 procedure Create_Extra_Formals (E : Entity_Id) is
6770 Formal : Entity_Id;
6771 First_Extra : Entity_Id := Empty;
6772 Last_Extra : Entity_Id;
6773 Formal_Type : Entity_Id;
6774 P_Formal : Entity_Id := Empty;
6776 function Add_Extra_Formal
6777 (Assoc_Entity : Entity_Id;
6778 Typ : Entity_Id;
6779 Scope : Entity_Id;
6780 Suffix : String) return Entity_Id;
6781 -- Add an extra formal to the current list of formals and extra formals.
6782 -- The extra formal is added to the end of the list of extra formals,
6783 -- and also returned as the result. These formals are always of mode IN.
6784 -- The new formal has the type Typ, is declared in Scope, and its name
6785 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6786 -- The following suffixes are currently used. They should not be changed
6787 -- without coordinating with CodePeer, which makes use of these to
6788 -- provide better messages.
6790 -- O denotes the Constrained bit.
6791 -- L denotes the accessibility level.
6792 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6793 -- the full list in exp_ch6.BIP_Formal_Kind.
6795 ----------------------
6796 -- Add_Extra_Formal --
6797 ----------------------
6799 function Add_Extra_Formal
6800 (Assoc_Entity : Entity_Id;
6801 Typ : Entity_Id;
6802 Scope : Entity_Id;
6803 Suffix : String) return Entity_Id
6805 EF : constant Entity_Id :=
6806 Make_Defining_Identifier (Sloc (Assoc_Entity),
6807 Chars => New_External_Name (Chars (Assoc_Entity),
6808 Suffix => Suffix));
6810 begin
6811 -- A little optimization. Never generate an extra formal for the
6812 -- _init operand of an initialization procedure, since it could
6813 -- never be used.
6815 if Chars (Formal) = Name_uInit then
6816 return Empty;
6817 end if;
6819 Set_Ekind (EF, E_In_Parameter);
6820 Set_Actual_Subtype (EF, Typ);
6821 Set_Etype (EF, Typ);
6822 Set_Scope (EF, Scope);
6823 Set_Mechanism (EF, Default_Mechanism);
6824 Set_Formal_Validity (EF);
6826 if No (First_Extra) then
6827 First_Extra := EF;
6828 Set_Extra_Formals (Scope, First_Extra);
6829 end if;
6831 if Present (Last_Extra) then
6832 Set_Extra_Formal (Last_Extra, EF);
6833 end if;
6835 Last_Extra := EF;
6837 return EF;
6838 end Add_Extra_Formal;
6840 -- Start of processing for Create_Extra_Formals
6842 begin
6843 -- We never generate extra formals if expansion is not active because we
6844 -- don't need them unless we are generating code.
6846 if not Expander_Active then
6847 return;
6848 end if;
6850 -- No need to generate extra formals in interface thunks whose target
6851 -- primitive has no extra formals.
6853 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
6854 return;
6855 end if;
6857 -- If this is a derived subprogram then the subtypes of the parent
6858 -- subprogram's formal parameters will be used to determine the need
6859 -- for extra formals.
6861 if Is_Overloadable (E) and then Present (Alias (E)) then
6862 P_Formal := First_Formal (Alias (E));
6863 end if;
6865 Last_Extra := Empty;
6866 Formal := First_Formal (E);
6867 while Present (Formal) loop
6868 Last_Extra := Formal;
6869 Next_Formal (Formal);
6870 end loop;
6872 -- If Extra_formals were already created, don't do it again. This
6873 -- situation may arise for subprogram types created as part of
6874 -- dispatching calls (see Expand_Dispatching_Call)
6876 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
6877 return;
6878 end if;
6880 -- If the subprogram is a predefined dispatching subprogram then don't
6881 -- generate any extra constrained or accessibility level formals. In
6882 -- general we suppress these for internal subprograms (by not calling
6883 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6884 -- generated stream attributes do get passed through because extra
6885 -- build-in-place formals are needed in some cases (limited 'Input).
6887 if Is_Predefined_Internal_Operation (E) then
6888 goto Test_For_Func_Result_Extras;
6889 end if;
6891 Formal := First_Formal (E);
6892 while Present (Formal) loop
6894 -- Create extra formal for supporting the attribute 'Constrained.
6895 -- The case of a private type view without discriminants also
6896 -- requires the extra formal if the underlying type has defaulted
6897 -- discriminants.
6899 if Ekind (Formal) /= E_In_Parameter then
6900 if Present (P_Formal) then
6901 Formal_Type := Etype (P_Formal);
6902 else
6903 Formal_Type := Etype (Formal);
6904 end if;
6906 -- Do not produce extra formals for Unchecked_Union parameters.
6907 -- Jump directly to the end of the loop.
6909 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6910 goto Skip_Extra_Formal_Generation;
6911 end if;
6913 if not Has_Discriminants (Formal_Type)
6914 and then Ekind (Formal_Type) in Private_Kind
6915 and then Present (Underlying_Type (Formal_Type))
6916 then
6917 Formal_Type := Underlying_Type (Formal_Type);
6918 end if;
6920 -- Suppress the extra formal if formal's subtype is constrained or
6921 -- indefinite, or we're compiling for Ada 2012 and the underlying
6922 -- type is tagged and limited. In Ada 2012, a limited tagged type
6923 -- can have defaulted discriminants, but 'Constrained is required
6924 -- to return True, so the formal is never needed (see AI05-0214).
6925 -- Note that this ensures consistency of calling sequences for
6926 -- dispatching operations when some types in a class have defaults
6927 -- on discriminants and others do not (and requiring the extra
6928 -- formal would introduce distributed overhead).
6930 -- If the type does not have a completion yet, treat as prior to
6931 -- Ada 2012 for consistency.
6933 if Has_Discriminants (Formal_Type)
6934 and then not Is_Constrained (Formal_Type)
6935 and then not Is_Indefinite_Subtype (Formal_Type)
6936 and then (Ada_Version < Ada_2012
6937 or else No (Underlying_Type (Formal_Type))
6938 or else not
6939 (Is_Limited_Type (Formal_Type)
6940 and then
6941 (Is_Tagged_Type
6942 (Underlying_Type (Formal_Type)))))
6943 then
6944 Set_Extra_Constrained
6945 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6946 end if;
6947 end if;
6949 -- Create extra formal for supporting accessibility checking. This
6950 -- is done for both anonymous access formals and formals of named
6951 -- access types that are marked as controlling formals. The latter
6952 -- case can occur when Expand_Dispatching_Call creates a subprogram
6953 -- type and substitutes the types of access-to-class-wide actuals
6954 -- for the anonymous access-to-specific-type of controlling formals.
6955 -- Base_Type is applied because in cases where there is a null
6956 -- exclusion the formal may have an access subtype.
6958 -- This is suppressed if we specifically suppress accessibility
6959 -- checks at the package level for either the subprogram, or the
6960 -- package in which it resides. However, we do not suppress it
6961 -- simply if the scope has accessibility checks suppressed, since
6962 -- this could cause trouble when clients are compiled with a
6963 -- different suppression setting. The explicit checks at the
6964 -- package level are safe from this point of view.
6966 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6967 or else (Is_Controlling_Formal (Formal)
6968 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6969 and then not
6970 (Explicit_Suppress (E, Accessibility_Check)
6971 or else
6972 Explicit_Suppress (Scope (E), Accessibility_Check))
6973 and then
6974 (No (P_Formal)
6975 or else Present (Extra_Accessibility (P_Formal)))
6976 then
6977 Set_Extra_Accessibility
6978 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6979 end if;
6981 -- This label is required when skipping extra formal generation for
6982 -- Unchecked_Union parameters.
6984 <<Skip_Extra_Formal_Generation>>
6986 if Present (P_Formal) then
6987 Next_Formal (P_Formal);
6988 end if;
6990 Next_Formal (Formal);
6991 end loop;
6993 <<Test_For_Func_Result_Extras>>
6995 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6996 -- function call is ... determined by the point of call ...".
6998 if Needs_Result_Accessibility_Level (E) then
6999 Set_Extra_Accessibility_Of_Result
7000 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7001 end if;
7003 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7004 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7006 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7007 declare
7008 Result_Subt : constant Entity_Id := Etype (E);
7009 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7010 Formal_Typ : Entity_Id;
7012 Discard : Entity_Id;
7013 pragma Warnings (Off, Discard);
7015 begin
7016 -- In the case of functions with unconstrained result subtypes,
7017 -- add a 4-state formal indicating whether the return object is
7018 -- allocated by the caller (1), or should be allocated by the
7019 -- callee on the secondary stack (2), in the global heap (3), or
7020 -- in a user-defined storage pool (4). For the moment we just use
7021 -- Natural for the type of this formal. Note that this formal
7022 -- isn't usually needed in the case where the result subtype is
7023 -- constrained, but it is needed when the function has a tagged
7024 -- result, because generally such functions can be called in a
7025 -- dispatching context and such calls must be handled like calls
7026 -- to a class-wide function.
7028 if Needs_BIP_Alloc_Form (E) then
7029 Discard :=
7030 Add_Extra_Formal
7031 (E, Standard_Natural,
7032 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7034 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7035 -- use a user-defined pool. This formal is not added on
7036 -- .NET/JVM/ZFP as those targets do not support pools.
7038 if VM_Target = No_VM
7039 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
7040 then
7041 Discard :=
7042 Add_Extra_Formal
7043 (E, RTE (RE_Root_Storage_Pool_Ptr),
7044 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7045 end if;
7046 end if;
7048 -- In the case of functions whose result type needs finalization,
7049 -- add an extra formal which represents the finalization master.
7051 if Needs_BIP_Finalization_Master (E) then
7052 Discard :=
7053 Add_Extra_Formal
7054 (E, RTE (RE_Finalization_Master_Ptr),
7055 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7056 end if;
7058 -- When the result type contains tasks, add two extra formals: the
7059 -- master of the tasks to be created, and the caller's activation
7060 -- chain.
7062 if Has_Task (Full_Subt) then
7063 Discard :=
7064 Add_Extra_Formal
7065 (E, RTE (RE_Master_Id),
7066 E, BIP_Formal_Suffix (BIP_Task_Master));
7067 Discard :=
7068 Add_Extra_Formal
7069 (E, RTE (RE_Activation_Chain_Access),
7070 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7071 end if;
7073 -- All build-in-place functions get an extra formal that will be
7074 -- passed the address of the return object within the caller.
7076 Formal_Typ :=
7077 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7079 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7080 Set_Etype (Formal_Typ, Formal_Typ);
7081 Set_Depends_On_Private
7082 (Formal_Typ, Has_Private_Component (Formal_Typ));
7083 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7084 Set_Is_Access_Constant (Formal_Typ, False);
7086 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7087 -- the designated type comes from the limited view (for back-end
7088 -- purposes).
7090 Set_From_Limited_With
7091 (Formal_Typ, From_Limited_With (Result_Subt));
7093 Layout_Type (Formal_Typ);
7095 Discard :=
7096 Add_Extra_Formal
7097 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7098 end;
7099 end if;
7100 end Create_Extra_Formals;
7102 -----------------------------
7103 -- Enter_Overloaded_Entity --
7104 -----------------------------
7106 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7107 E : Entity_Id := Current_Entity_In_Scope (S);
7108 C_E : Entity_Id := Current_Entity (S);
7110 begin
7111 if Present (E) then
7112 Set_Has_Homonym (E);
7113 Set_Has_Homonym (S);
7114 end if;
7116 Set_Is_Immediately_Visible (S);
7117 Set_Scope (S, Current_Scope);
7119 -- Chain new entity if front of homonym in current scope, so that
7120 -- homonyms are contiguous.
7122 if Present (E) and then E /= C_E then
7123 while Homonym (C_E) /= E loop
7124 C_E := Homonym (C_E);
7125 end loop;
7127 Set_Homonym (C_E, S);
7129 else
7130 E := C_E;
7131 Set_Current_Entity (S);
7132 end if;
7134 Set_Homonym (S, E);
7136 if Is_Inherited_Operation (S) then
7137 Append_Inherited_Subprogram (S);
7138 else
7139 Append_Entity (S, Current_Scope);
7140 end if;
7142 Set_Public_Status (S);
7144 if Debug_Flag_E then
7145 Write_Str ("New overloaded entity chain: ");
7146 Write_Name (Chars (S));
7148 E := S;
7149 while Present (E) loop
7150 Write_Str (" "); Write_Int (Int (E));
7151 E := Homonym (E);
7152 end loop;
7154 Write_Eol;
7155 end if;
7157 -- Generate warning for hiding
7159 if Warn_On_Hiding
7160 and then Comes_From_Source (S)
7161 and then In_Extended_Main_Source_Unit (S)
7162 then
7163 E := S;
7164 loop
7165 E := Homonym (E);
7166 exit when No (E);
7168 -- Warn unless genuine overloading. Do not emit warning on
7169 -- hiding predefined operators in Standard (these are either an
7170 -- (artifact of our implicit declarations, or simple noise) but
7171 -- keep warning on a operator defined on a local subtype, because
7172 -- of the real danger that different operators may be applied in
7173 -- various parts of the program.
7175 -- Note that if E and S have the same scope, there is never any
7176 -- hiding. Either the two conflict, and the program is illegal,
7177 -- or S is overriding an implicit inherited subprogram.
7179 if Scope (E) /= Scope (S)
7180 and then (not Is_Overloadable (E)
7181 or else Subtype_Conformant (E, S))
7182 and then (Is_Immediately_Visible (E)
7183 or else
7184 Is_Potentially_Use_Visible (S))
7185 then
7186 if Scope (E) /= Standard_Standard then
7187 Error_Msg_Sloc := Sloc (E);
7188 Error_Msg_N ("declaration of & hides one #?h?", S);
7190 elsif Nkind (S) = N_Defining_Operator_Symbol
7191 and then
7192 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7193 then
7194 Error_Msg_N
7195 ("declaration of & hides predefined operator?h?", S);
7196 end if;
7197 end if;
7198 end loop;
7199 end if;
7200 end Enter_Overloaded_Entity;
7202 -----------------------------
7203 -- Check_Untagged_Equality --
7204 -----------------------------
7206 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7207 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7208 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7209 Obj_Decl : Node_Id;
7211 begin
7212 -- This check applies only if we have a subprogram declaration with an
7213 -- untagged record type.
7215 if Nkind (Decl) /= N_Subprogram_Declaration
7216 or else not Is_Record_Type (Typ)
7217 or else Is_Tagged_Type (Typ)
7218 then
7219 return;
7220 end if;
7222 -- In Ada 2012 case, we will output errors or warnings depending on
7223 -- the setting of debug flag -gnatd.E.
7225 if Ada_Version >= Ada_2012 then
7226 Error_Msg_Warn := Debug_Flag_Dot_EE;
7228 -- In earlier versions of Ada, nothing to do unless we are warning on
7229 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7231 else
7232 if not Warn_On_Ada_2012_Compatibility then
7233 return;
7234 end if;
7235 end if;
7237 -- Cases where the type has already been frozen
7239 if Is_Frozen (Typ) then
7241 -- If the type is not declared in a package, or if we are in the body
7242 -- of the package or in some other scope, the new operation is not
7243 -- primitive, and therefore legal, though suspicious. Should we
7244 -- generate a warning in this case ???
7246 if Ekind (Scope (Typ)) /= E_Package
7247 or else Scope (Typ) /= Current_Scope
7248 then
7249 return;
7251 -- If the type is a generic actual (sub)type, the operation is not
7252 -- primitive either because the base type is declared elsewhere.
7254 elsif Is_Generic_Actual_Type (Typ) then
7255 return;
7257 -- Here we have a definite error of declaration after freezing
7259 else
7260 if Ada_Version >= Ada_2012 then
7261 Error_Msg_NE
7262 ("equality operator must be declared before type & is "
7263 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
7265 -- In Ada 2012 mode with error turned to warning, output one
7266 -- more warning to warn that the equality operation may not
7267 -- compose. This is the consequence of ignoring the error.
7269 if Error_Msg_Warn then
7270 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
7271 end if;
7273 else
7274 Error_Msg_NE
7275 ("equality operator must be declared before type& is "
7276 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
7277 end if;
7279 -- If we are in the package body, we could just move the
7280 -- declaration to the package spec, so add a message saying that.
7282 if In_Package_Body (Scope (Typ)) then
7283 if Ada_Version >= Ada_2012 then
7284 Error_Msg_N
7285 ("\move declaration to package spec<<", Eq_Op);
7286 else
7287 Error_Msg_N
7288 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
7289 end if;
7291 -- Otherwise try to find the freezing point
7293 else
7294 Obj_Decl := Next (Parent (Typ));
7295 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
7296 if Nkind (Obj_Decl) = N_Object_Declaration
7297 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
7298 then
7299 -- Freezing point, output warnings
7301 if Ada_Version >= Ada_2012 then
7302 Error_Msg_NE
7303 ("type& is frozen by declaration??", Obj_Decl, Typ);
7304 Error_Msg_N
7305 ("\an equality operator cannot be declared after "
7306 & "this point??",
7307 Obj_Decl);
7308 else
7309 Error_Msg_NE
7310 ("type& is frozen by declaration (Ada 2012)?y?",
7311 Obj_Decl, Typ);
7312 Error_Msg_N
7313 ("\an equality operator cannot be declared after "
7314 & "this point (Ada 2012)?y?",
7315 Obj_Decl);
7316 end if;
7318 exit;
7319 end if;
7321 Next (Obj_Decl);
7322 end loop;
7323 end if;
7324 end if;
7326 -- Here if type is not frozen yet. It is illegal to have a primitive
7327 -- equality declared in the private part if the type is visible.
7329 elsif not In_Same_List (Parent (Typ), Decl)
7330 and then not Is_Limited_Type (Typ)
7331 then
7332 -- Shouldn't we give an RM reference here???
7334 if Ada_Version >= Ada_2012 then
7335 Error_Msg_N
7336 ("equality operator appears too late<<", Eq_Op);
7337 else
7338 Error_Msg_N
7339 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
7340 end if;
7342 -- No error detected
7344 else
7345 return;
7346 end if;
7347 end Check_Untagged_Equality;
7349 -----------------------------
7350 -- Find_Corresponding_Spec --
7351 -----------------------------
7353 function Find_Corresponding_Spec
7354 (N : Node_Id;
7355 Post_Error : Boolean := True) return Entity_Id
7357 Spec : constant Node_Id := Specification (N);
7358 Designator : constant Entity_Id := Defining_Entity (Spec);
7360 E : Entity_Id;
7362 function Different_Generic_Profile (E : Entity_Id) return Boolean;
7363 -- Even if fully conformant, a body may depend on a generic actual when
7364 -- the spec does not, or vice versa, in which case they were distinct
7365 -- entities in the generic.
7367 -------------------------------
7368 -- Different_Generic_Profile --
7369 -------------------------------
7371 function Different_Generic_Profile (E : Entity_Id) return Boolean is
7372 F1, F2 : Entity_Id;
7374 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
7375 -- Check that the types of corresponding formals have the same
7376 -- generic actual if any. We have to account for subtypes of a
7377 -- generic formal, declared between a spec and a body, which may
7378 -- appear distinct in an instance but matched in the generic, and
7379 -- the subtype may be used either in the spec or the body of the
7380 -- subprogram being checked.
7382 -------------------------
7383 -- Same_Generic_Actual --
7384 -------------------------
7386 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
7388 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
7389 -- Predicate to check whether S1 is a subtype of S2 in the source
7390 -- of the instance.
7392 -------------------------
7393 -- Is_Declared_Subtype --
7394 -------------------------
7396 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
7397 begin
7398 return Comes_From_Source (Parent (S1))
7399 and then Nkind (Parent (S1)) = N_Subtype_Declaration
7400 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
7401 and then Entity (Subtype_Indication (Parent (S1))) = S2;
7402 end Is_Declared_Subtype;
7404 -- Start of processing for Same_Generic_Actual
7406 begin
7407 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
7408 or else Is_Declared_Subtype (T1, T2)
7409 or else Is_Declared_Subtype (T2, T1);
7410 end Same_Generic_Actual;
7412 -- Start of processing for Different_Generic_Profile
7414 begin
7415 if not In_Instance then
7416 return False;
7418 elsif Ekind (E) = E_Function
7419 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
7420 then
7421 return True;
7422 end if;
7424 F1 := First_Formal (Designator);
7425 F2 := First_Formal (E);
7426 while Present (F1) loop
7427 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
7428 return True;
7429 end if;
7431 Next_Formal (F1);
7432 Next_Formal (F2);
7433 end loop;
7435 return False;
7436 end Different_Generic_Profile;
7438 -- Start of processing for Find_Corresponding_Spec
7440 begin
7441 E := Current_Entity (Designator);
7442 while Present (E) loop
7444 -- We are looking for a matching spec. It must have the same scope,
7445 -- and the same name, and either be type conformant, or be the case
7446 -- of a library procedure spec and its body (which belong to one
7447 -- another regardless of whether they are type conformant or not).
7449 if Scope (E) = Current_Scope then
7450 if Current_Scope = Standard_Standard
7451 or else (Ekind (E) = Ekind (Designator)
7452 and then Type_Conformant (E, Designator))
7453 then
7454 -- Within an instantiation, we know that spec and body are
7455 -- subtype conformant, because they were subtype conformant in
7456 -- the generic. We choose the subtype-conformant entity here as
7457 -- well, to resolve spurious ambiguities in the instance that
7458 -- were not present in the generic (i.e. when two different
7459 -- types are given the same actual). If we are looking for a
7460 -- spec to match a body, full conformance is expected.
7462 if In_Instance then
7464 -- Inherit the convention and "ghostness" of the matching
7465 -- spec to ensure proper full and subtype conformance.
7467 Set_Convention (Designator, Convention (E));
7469 if Is_Ghost_Entity (E) then
7470 Set_Is_Ghost_Entity (Designator);
7471 end if;
7473 -- Skip past subprogram bodies and subprogram renamings that
7474 -- may appear to have a matching spec, but that aren't fully
7475 -- conformant with it. That can occur in cases where an
7476 -- actual type causes unrelated homographs in the instance.
7478 if Nkind_In (N, N_Subprogram_Body,
7479 N_Subprogram_Renaming_Declaration)
7480 and then Present (Homonym (E))
7481 and then not Fully_Conformant (Designator, E)
7482 then
7483 goto Next_Entity;
7485 elsif not Subtype_Conformant (Designator, E) then
7486 goto Next_Entity;
7488 elsif Different_Generic_Profile (E) then
7489 goto Next_Entity;
7490 end if;
7491 end if;
7493 -- Ada 2012 (AI05-0165): For internally generated bodies of
7494 -- null procedures locate the internally generated spec. We
7495 -- enforce mode conformance since a tagged type may inherit
7496 -- from interfaces several null primitives which differ only
7497 -- in the mode of the formals.
7499 if not (Comes_From_Source (E))
7500 and then Is_Null_Procedure (E)
7501 and then not Mode_Conformant (Designator, E)
7502 then
7503 null;
7505 -- For null procedures coming from source that are completions,
7506 -- analysis of the generated body will establish the link.
7508 elsif Comes_From_Source (E)
7509 and then Nkind (Spec) = N_Procedure_Specification
7510 and then Null_Present (Spec)
7511 then
7512 return E;
7514 elsif not Has_Completion (E) then
7515 if Nkind (N) /= N_Subprogram_Body_Stub then
7516 Set_Corresponding_Spec (N, E);
7517 end if;
7519 Set_Has_Completion (E);
7520 return E;
7522 elsif Nkind (Parent (N)) = N_Subunit then
7524 -- If this is the proper body of a subunit, the completion
7525 -- flag is set when analyzing the stub.
7527 return E;
7529 -- If E is an internal function with a controlling result that
7530 -- was created for an operation inherited by a null extension,
7531 -- it may be overridden by a body without a previous spec (one
7532 -- more reason why these should be shunned). In that case we
7533 -- remove the generated body if present, because the current
7534 -- one is the explicit overriding.
7536 elsif Ekind (E) = E_Function
7537 and then Ada_Version >= Ada_2005
7538 and then not Comes_From_Source (E)
7539 and then Has_Controlling_Result (E)
7540 and then Is_Null_Extension (Etype (E))
7541 and then Comes_From_Source (Spec)
7542 then
7543 Set_Has_Completion (E, False);
7545 if Expander_Active
7546 and then Nkind (Parent (E)) = N_Function_Specification
7547 then
7548 Remove
7549 (Unit_Declaration_Node
7550 (Corresponding_Body (Unit_Declaration_Node (E))));
7552 return E;
7554 -- If expansion is disabled, or if the wrapper function has
7555 -- not been generated yet, this a late body overriding an
7556 -- inherited operation, or it is an overriding by some other
7557 -- declaration before the controlling result is frozen. In
7558 -- either case this is a declaration of a new entity.
7560 else
7561 return Empty;
7562 end if;
7564 -- If the body already exists, then this is an error unless
7565 -- the previous declaration is the implicit declaration of a
7566 -- derived subprogram. It is also legal for an instance to
7567 -- contain type conformant overloadable declarations (but the
7568 -- generic declaration may not), per 8.3(26/2).
7570 elsif No (Alias (E))
7571 and then not Is_Intrinsic_Subprogram (E)
7572 and then not In_Instance
7573 and then Post_Error
7574 then
7575 Error_Msg_Sloc := Sloc (E);
7577 if Is_Imported (E) then
7578 Error_Msg_NE
7579 ("body not allowed for imported subprogram & declared#",
7580 N, E);
7581 else
7582 Error_Msg_NE ("duplicate body for & declared#", N, E);
7583 end if;
7584 end if;
7586 -- Child units cannot be overloaded, so a conformance mismatch
7587 -- between body and a previous spec is an error.
7589 elsif Is_Child_Unit (E)
7590 and then
7591 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
7592 and then
7593 Nkind (Parent (Unit_Declaration_Node (Designator))) =
7594 N_Compilation_Unit
7595 and then Post_Error
7596 then
7597 Error_Msg_N
7598 ("body of child unit does not match previous declaration", N);
7599 end if;
7600 end if;
7602 <<Next_Entity>>
7603 E := Homonym (E);
7604 end loop;
7606 -- On exit, we know that no previous declaration of subprogram exists
7608 return Empty;
7609 end Find_Corresponding_Spec;
7611 ----------------------
7612 -- Fully_Conformant --
7613 ----------------------
7615 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7616 Result : Boolean;
7617 begin
7618 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
7619 return Result;
7620 end Fully_Conformant;
7622 ----------------------------------
7623 -- Fully_Conformant_Expressions --
7624 ----------------------------------
7626 function Fully_Conformant_Expressions
7627 (Given_E1 : Node_Id;
7628 Given_E2 : Node_Id) return Boolean
7630 E1 : constant Node_Id := Original_Node (Given_E1);
7631 E2 : constant Node_Id := Original_Node (Given_E2);
7632 -- We always test conformance on original nodes, since it is possible
7633 -- for analysis and/or expansion to make things look as though they
7634 -- conform when they do not, e.g. by converting 1+2 into 3.
7636 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
7637 renames Fully_Conformant_Expressions;
7639 function FCL (L1, L2 : List_Id) return Boolean;
7640 -- Compare elements of two lists for conformance. Elements have to be
7641 -- conformant, and actuals inserted as default parameters do not match
7642 -- explicit actuals with the same value.
7644 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
7645 -- Compare an operator node with a function call
7647 ---------
7648 -- FCL --
7649 ---------
7651 function FCL (L1, L2 : List_Id) return Boolean is
7652 N1, N2 : Node_Id;
7654 begin
7655 if L1 = No_List then
7656 N1 := Empty;
7657 else
7658 N1 := First (L1);
7659 end if;
7661 if L2 = No_List then
7662 N2 := Empty;
7663 else
7664 N2 := First (L2);
7665 end if;
7667 -- Compare two lists, skipping rewrite insertions (we want to compare
7668 -- the original trees, not the expanded versions).
7670 loop
7671 if Is_Rewrite_Insertion (N1) then
7672 Next (N1);
7673 elsif Is_Rewrite_Insertion (N2) then
7674 Next (N2);
7675 elsif No (N1) then
7676 return No (N2);
7677 elsif No (N2) then
7678 return False;
7679 elsif not FCE (N1, N2) then
7680 return False;
7681 else
7682 Next (N1);
7683 Next (N2);
7684 end if;
7685 end loop;
7686 end FCL;
7688 ---------
7689 -- FCO --
7690 ---------
7692 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7693 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7694 Act : Node_Id;
7696 begin
7697 if No (Actuals)
7698 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7699 then
7700 return False;
7702 else
7703 Act := First (Actuals);
7705 if Nkind (Op_Node) in N_Binary_Op then
7706 if not FCE (Left_Opnd (Op_Node), Act) then
7707 return False;
7708 end if;
7710 Next (Act);
7711 end if;
7713 return Present (Act)
7714 and then FCE (Right_Opnd (Op_Node), Act)
7715 and then No (Next (Act));
7716 end if;
7717 end FCO;
7719 -- Start of processing for Fully_Conformant_Expressions
7721 begin
7722 -- Non-conformant if paren count does not match. Note: if some idiot
7723 -- complains that we don't do this right for more than 3 levels of
7724 -- parentheses, they will be treated with the respect they deserve.
7726 if Paren_Count (E1) /= Paren_Count (E2) then
7727 return False;
7729 -- If same entities are referenced, then they are conformant even if
7730 -- they have different forms (RM 8.3.1(19-20)).
7732 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7733 if Present (Entity (E1)) then
7734 return Entity (E1) = Entity (E2)
7735 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7736 and then Ekind (Entity (E1)) = E_Discriminant
7737 and then Ekind (Entity (E2)) = E_In_Parameter);
7739 elsif Nkind (E1) = N_Expanded_Name
7740 and then Nkind (E2) = N_Expanded_Name
7741 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7742 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7743 then
7744 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7746 else
7747 -- Identifiers in component associations don't always have
7748 -- entities, but their names must conform.
7750 return Nkind (E1) = N_Identifier
7751 and then Nkind (E2) = N_Identifier
7752 and then Chars (E1) = Chars (E2);
7753 end if;
7755 elsif Nkind (E1) = N_Character_Literal
7756 and then Nkind (E2) = N_Expanded_Name
7757 then
7758 return Nkind (Selector_Name (E2)) = N_Character_Literal
7759 and then Chars (E1) = Chars (Selector_Name (E2));
7761 elsif Nkind (E2) = N_Character_Literal
7762 and then Nkind (E1) = N_Expanded_Name
7763 then
7764 return Nkind (Selector_Name (E1)) = N_Character_Literal
7765 and then Chars (E2) = Chars (Selector_Name (E1));
7767 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
7768 return FCO (E1, E2);
7770 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
7771 return FCO (E2, E1);
7773 -- Otherwise we must have the same syntactic entity
7775 elsif Nkind (E1) /= Nkind (E2) then
7776 return False;
7778 -- At this point, we specialize by node type
7780 else
7781 case Nkind (E1) is
7783 when N_Aggregate =>
7784 return
7785 FCL (Expressions (E1), Expressions (E2))
7786 and then
7787 FCL (Component_Associations (E1),
7788 Component_Associations (E2));
7790 when N_Allocator =>
7791 if Nkind (Expression (E1)) = N_Qualified_Expression
7792 or else
7793 Nkind (Expression (E2)) = N_Qualified_Expression
7794 then
7795 return FCE (Expression (E1), Expression (E2));
7797 -- Check that the subtype marks and any constraints
7798 -- are conformant
7800 else
7801 declare
7802 Indic1 : constant Node_Id := Expression (E1);
7803 Indic2 : constant Node_Id := Expression (E2);
7804 Elt1 : Node_Id;
7805 Elt2 : Node_Id;
7807 begin
7808 if Nkind (Indic1) /= N_Subtype_Indication then
7809 return
7810 Nkind (Indic2) /= N_Subtype_Indication
7811 and then Entity (Indic1) = Entity (Indic2);
7813 elsif Nkind (Indic2) /= N_Subtype_Indication then
7814 return
7815 Nkind (Indic1) /= N_Subtype_Indication
7816 and then Entity (Indic1) = Entity (Indic2);
7818 else
7819 if Entity (Subtype_Mark (Indic1)) /=
7820 Entity (Subtype_Mark (Indic2))
7821 then
7822 return False;
7823 end if;
7825 Elt1 := First (Constraints (Constraint (Indic1)));
7826 Elt2 := First (Constraints (Constraint (Indic2)));
7827 while Present (Elt1) and then Present (Elt2) loop
7828 if not FCE (Elt1, Elt2) then
7829 return False;
7830 end if;
7832 Next (Elt1);
7833 Next (Elt2);
7834 end loop;
7836 return True;
7837 end if;
7838 end;
7839 end if;
7841 when N_Attribute_Reference =>
7842 return
7843 Attribute_Name (E1) = Attribute_Name (E2)
7844 and then FCL (Expressions (E1), Expressions (E2));
7846 when N_Binary_Op =>
7847 return
7848 Entity (E1) = Entity (E2)
7849 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7850 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7852 when N_Short_Circuit | N_Membership_Test =>
7853 return
7854 FCE (Left_Opnd (E1), Left_Opnd (E2))
7855 and then
7856 FCE (Right_Opnd (E1), Right_Opnd (E2));
7858 when N_Case_Expression =>
7859 declare
7860 Alt1 : Node_Id;
7861 Alt2 : Node_Id;
7863 begin
7864 if not FCE (Expression (E1), Expression (E2)) then
7865 return False;
7867 else
7868 Alt1 := First (Alternatives (E1));
7869 Alt2 := First (Alternatives (E2));
7870 loop
7871 if Present (Alt1) /= Present (Alt2) then
7872 return False;
7873 elsif No (Alt1) then
7874 return True;
7875 end if;
7877 if not FCE (Expression (Alt1), Expression (Alt2))
7878 or else not FCL (Discrete_Choices (Alt1),
7879 Discrete_Choices (Alt2))
7880 then
7881 return False;
7882 end if;
7884 Next (Alt1);
7885 Next (Alt2);
7886 end loop;
7887 end if;
7888 end;
7890 when N_Character_Literal =>
7891 return
7892 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7894 when N_Component_Association =>
7895 return
7896 FCL (Choices (E1), Choices (E2))
7897 and then
7898 FCE (Expression (E1), Expression (E2));
7900 when N_Explicit_Dereference =>
7901 return
7902 FCE (Prefix (E1), Prefix (E2));
7904 when N_Extension_Aggregate =>
7905 return
7906 FCL (Expressions (E1), Expressions (E2))
7907 and then Null_Record_Present (E1) =
7908 Null_Record_Present (E2)
7909 and then FCL (Component_Associations (E1),
7910 Component_Associations (E2));
7912 when N_Function_Call =>
7913 return
7914 FCE (Name (E1), Name (E2))
7915 and then
7916 FCL (Parameter_Associations (E1),
7917 Parameter_Associations (E2));
7919 when N_If_Expression =>
7920 return
7921 FCL (Expressions (E1), Expressions (E2));
7923 when N_Indexed_Component =>
7924 return
7925 FCE (Prefix (E1), Prefix (E2))
7926 and then
7927 FCL (Expressions (E1), Expressions (E2));
7929 when N_Integer_Literal =>
7930 return (Intval (E1) = Intval (E2));
7932 when N_Null =>
7933 return True;
7935 when N_Operator_Symbol =>
7936 return
7937 Chars (E1) = Chars (E2);
7939 when N_Others_Choice =>
7940 return True;
7942 when N_Parameter_Association =>
7943 return
7944 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7945 and then FCE (Explicit_Actual_Parameter (E1),
7946 Explicit_Actual_Parameter (E2));
7948 when N_Qualified_Expression =>
7949 return
7950 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7951 and then
7952 FCE (Expression (E1), Expression (E2));
7954 when N_Quantified_Expression =>
7955 if not FCE (Condition (E1), Condition (E2)) then
7956 return False;
7957 end if;
7959 if Present (Loop_Parameter_Specification (E1))
7960 and then Present (Loop_Parameter_Specification (E2))
7961 then
7962 declare
7963 L1 : constant Node_Id :=
7964 Loop_Parameter_Specification (E1);
7965 L2 : constant Node_Id :=
7966 Loop_Parameter_Specification (E2);
7968 begin
7969 return
7970 Reverse_Present (L1) = Reverse_Present (L2)
7971 and then
7972 FCE (Defining_Identifier (L1),
7973 Defining_Identifier (L2))
7974 and then
7975 FCE (Discrete_Subtype_Definition (L1),
7976 Discrete_Subtype_Definition (L2));
7977 end;
7979 elsif Present (Iterator_Specification (E1))
7980 and then Present (Iterator_Specification (E2))
7981 then
7982 declare
7983 I1 : constant Node_Id := Iterator_Specification (E1);
7984 I2 : constant Node_Id := Iterator_Specification (E2);
7986 begin
7987 return
7988 FCE (Defining_Identifier (I1),
7989 Defining_Identifier (I2))
7990 and then
7991 Of_Present (I1) = Of_Present (I2)
7992 and then
7993 Reverse_Present (I1) = Reverse_Present (I2)
7994 and then FCE (Name (I1), Name (I2))
7995 and then FCE (Subtype_Indication (I1),
7996 Subtype_Indication (I2));
7997 end;
7999 -- The quantified expressions used different specifications to
8000 -- walk their respective ranges.
8002 else
8003 return False;
8004 end if;
8006 when N_Range =>
8007 return
8008 FCE (Low_Bound (E1), Low_Bound (E2))
8009 and then
8010 FCE (High_Bound (E1), High_Bound (E2));
8012 when N_Real_Literal =>
8013 return (Realval (E1) = Realval (E2));
8015 when N_Selected_Component =>
8016 return
8017 FCE (Prefix (E1), Prefix (E2))
8018 and then
8019 FCE (Selector_Name (E1), Selector_Name (E2));
8021 when N_Slice =>
8022 return
8023 FCE (Prefix (E1), Prefix (E2))
8024 and then
8025 FCE (Discrete_Range (E1), Discrete_Range (E2));
8027 when N_String_Literal =>
8028 declare
8029 S1 : constant String_Id := Strval (E1);
8030 S2 : constant String_Id := Strval (E2);
8031 L1 : constant Nat := String_Length (S1);
8032 L2 : constant Nat := String_Length (S2);
8034 begin
8035 if L1 /= L2 then
8036 return False;
8038 else
8039 for J in 1 .. L1 loop
8040 if Get_String_Char (S1, J) /=
8041 Get_String_Char (S2, J)
8042 then
8043 return False;
8044 end if;
8045 end loop;
8047 return True;
8048 end if;
8049 end;
8051 when N_Type_Conversion =>
8052 return
8053 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8054 and then
8055 FCE (Expression (E1), Expression (E2));
8057 when N_Unary_Op =>
8058 return
8059 Entity (E1) = Entity (E2)
8060 and then
8061 FCE (Right_Opnd (E1), Right_Opnd (E2));
8063 when N_Unchecked_Type_Conversion =>
8064 return
8065 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8066 and then
8067 FCE (Expression (E1), Expression (E2));
8069 -- All other node types cannot appear in this context. Strictly
8070 -- we should raise a fatal internal error. Instead we just ignore
8071 -- the nodes. This means that if anyone makes a mistake in the
8072 -- expander and mucks an expression tree irretrievably, the result
8073 -- will be a failure to detect a (probably very obscure) case
8074 -- of non-conformance, which is better than bombing on some
8075 -- case where two expressions do in fact conform.
8077 when others =>
8078 return True;
8080 end case;
8081 end if;
8082 end Fully_Conformant_Expressions;
8084 ----------------------------------------
8085 -- Fully_Conformant_Discrete_Subtypes --
8086 ----------------------------------------
8088 function Fully_Conformant_Discrete_Subtypes
8089 (Given_S1 : Node_Id;
8090 Given_S2 : Node_Id) return Boolean
8092 S1 : constant Node_Id := Original_Node (Given_S1);
8093 S2 : constant Node_Id := Original_Node (Given_S2);
8095 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
8096 -- Special-case for a bound given by a discriminant, which in the body
8097 -- is replaced with the discriminal of the enclosing type.
8099 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
8100 -- Check both bounds
8102 -----------------------
8103 -- Conforming_Bounds --
8104 -----------------------
8106 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8107 begin
8108 if Is_Entity_Name (B1)
8109 and then Is_Entity_Name (B2)
8110 and then Ekind (Entity (B1)) = E_Discriminant
8111 then
8112 return Chars (B1) = Chars (B2);
8114 else
8115 return Fully_Conformant_Expressions (B1, B2);
8116 end if;
8117 end Conforming_Bounds;
8119 -----------------------
8120 -- Conforming_Ranges --
8121 -----------------------
8123 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8124 begin
8125 return
8126 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8127 and then
8128 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8129 end Conforming_Ranges;
8131 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8133 begin
8134 if Nkind (S1) /= Nkind (S2) then
8135 return False;
8137 elsif Is_Entity_Name (S1) then
8138 return Entity (S1) = Entity (S2);
8140 elsif Nkind (S1) = N_Range then
8141 return Conforming_Ranges (S1, S2);
8143 elsif Nkind (S1) = N_Subtype_Indication then
8144 return
8145 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8146 and then
8147 Conforming_Ranges
8148 (Range_Expression (Constraint (S1)),
8149 Range_Expression (Constraint (S2)));
8150 else
8151 return True;
8152 end if;
8153 end Fully_Conformant_Discrete_Subtypes;
8155 --------------------
8156 -- Install_Entity --
8157 --------------------
8159 procedure Install_Entity (E : Entity_Id) is
8160 Prev : constant Entity_Id := Current_Entity (E);
8161 begin
8162 Set_Is_Immediately_Visible (E);
8163 Set_Current_Entity (E);
8164 Set_Homonym (E, Prev);
8165 end Install_Entity;
8167 ---------------------
8168 -- Install_Formals --
8169 ---------------------
8171 procedure Install_Formals (Id : Entity_Id) is
8172 F : Entity_Id;
8173 begin
8174 F := First_Formal (Id);
8175 while Present (F) loop
8176 Install_Entity (F);
8177 Next_Formal (F);
8178 end loop;
8179 end Install_Formals;
8181 -----------------------------
8182 -- Is_Interface_Conformant --
8183 -----------------------------
8185 function Is_Interface_Conformant
8186 (Tagged_Type : Entity_Id;
8187 Iface_Prim : Entity_Id;
8188 Prim : Entity_Id) return Boolean
8190 -- The operation may in fact be an inherited (implicit) operation
8191 -- rather than the original interface primitive, so retrieve the
8192 -- ultimate ancestor.
8194 Iface : constant Entity_Id :=
8195 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
8196 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8198 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8199 -- Return the controlling formal of Prim
8201 ------------------------
8202 -- Controlling_Formal --
8203 ------------------------
8205 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8206 E : Entity_Id;
8208 begin
8209 E := First_Entity (Prim);
8210 while Present (E) loop
8211 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8212 return E;
8213 end if;
8215 Next_Entity (E);
8216 end loop;
8218 return Empty;
8219 end Controlling_Formal;
8221 -- Local variables
8223 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8224 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8226 -- Start of processing for Is_Interface_Conformant
8228 begin
8229 pragma Assert (Is_Subprogram (Iface_Prim)
8230 and then Is_Subprogram (Prim)
8231 and then Is_Dispatching_Operation (Iface_Prim)
8232 and then Is_Dispatching_Operation (Prim));
8234 pragma Assert (Is_Interface (Iface)
8235 or else (Present (Alias (Iface_Prim))
8236 and then
8237 Is_Interface
8238 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8240 if Prim = Iface_Prim
8241 or else not Is_Subprogram (Prim)
8242 or else Ekind (Prim) /= Ekind (Iface_Prim)
8243 or else not Is_Dispatching_Operation (Prim)
8244 or else Scope (Prim) /= Scope (Tagged_Type)
8245 or else No (Typ)
8246 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
8247 or else not Primitive_Names_Match (Iface_Prim, Prim)
8248 then
8249 return False;
8251 -- The mode of the controlling formals must match
8253 elsif Present (Iface_Ctrl_F)
8254 and then Present (Prim_Ctrl_F)
8255 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8256 then
8257 return False;
8259 -- Case of a procedure, or a function whose result type matches the
8260 -- result type of the interface primitive, or a function that has no
8261 -- controlling result (I or access I).
8263 elsif Ekind (Iface_Prim) = E_Procedure
8264 or else Etype (Prim) = Etype (Iface_Prim)
8265 or else not Has_Controlling_Result (Prim)
8266 then
8267 return Type_Conformant
8268 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
8270 -- Case of a function returning an interface, or an access to one. Check
8271 -- that the return types correspond.
8273 elsif Implements_Interface (Typ, Iface) then
8274 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
8276 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
8277 then
8278 return False;
8279 else
8280 return
8281 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
8282 Skip_Controlling_Formals => True);
8283 end if;
8285 else
8286 return False;
8287 end if;
8288 end Is_Interface_Conformant;
8290 ---------------------------------
8291 -- Is_Non_Overriding_Operation --
8292 ---------------------------------
8294 function Is_Non_Overriding_Operation
8295 (Prev_E : Entity_Id;
8296 New_E : Entity_Id) return Boolean
8298 Formal : Entity_Id;
8299 F_Typ : Entity_Id;
8300 G_Typ : Entity_Id := Empty;
8302 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
8303 -- If F_Type is a derived type associated with a generic actual subtype,
8304 -- then return its Generic_Parent_Type attribute, else return Empty.
8306 function Types_Correspond
8307 (P_Type : Entity_Id;
8308 N_Type : Entity_Id) return Boolean;
8309 -- Returns true if and only if the types (or designated types in the
8310 -- case of anonymous access types) are the same or N_Type is derived
8311 -- directly or indirectly from P_Type.
8313 -----------------------------
8314 -- Get_Generic_Parent_Type --
8315 -----------------------------
8317 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
8318 G_Typ : Entity_Id;
8319 Defn : Node_Id;
8320 Indic : Node_Id;
8322 begin
8323 if Is_Derived_Type (F_Typ)
8324 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
8325 then
8326 -- The tree must be traversed to determine the parent subtype in
8327 -- the generic unit, which unfortunately isn't always available
8328 -- via semantic attributes. ??? (Note: The use of Original_Node
8329 -- is needed for cases where a full derived type has been
8330 -- rewritten.)
8332 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
8333 if Nkind (Defn) = N_Derived_Type_Definition then
8334 Indic := Subtype_Indication (Defn);
8336 if Nkind (Indic) = N_Subtype_Indication then
8337 G_Typ := Entity (Subtype_Mark (Indic));
8338 else
8339 G_Typ := Entity (Indic);
8340 end if;
8342 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
8343 and then Present (Generic_Parent_Type (Parent (G_Typ)))
8344 then
8345 return Generic_Parent_Type (Parent (G_Typ));
8346 end if;
8347 end if;
8348 end if;
8350 return Empty;
8351 end Get_Generic_Parent_Type;
8353 ----------------------
8354 -- Types_Correspond --
8355 ----------------------
8357 function Types_Correspond
8358 (P_Type : Entity_Id;
8359 N_Type : Entity_Id) return Boolean
8361 Prev_Type : Entity_Id := Base_Type (P_Type);
8362 New_Type : Entity_Id := Base_Type (N_Type);
8364 begin
8365 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
8366 Prev_Type := Designated_Type (Prev_Type);
8367 end if;
8369 if Ekind (New_Type) = E_Anonymous_Access_Type then
8370 New_Type := Designated_Type (New_Type);
8371 end if;
8373 if Prev_Type = New_Type then
8374 return True;
8376 elsif not Is_Class_Wide_Type (New_Type) then
8377 while Etype (New_Type) /= New_Type loop
8378 New_Type := Etype (New_Type);
8379 if New_Type = Prev_Type then
8380 return True;
8381 end if;
8382 end loop;
8383 end if;
8384 return False;
8385 end Types_Correspond;
8387 -- Start of processing for Is_Non_Overriding_Operation
8389 begin
8390 -- In the case where both operations are implicit derived subprograms
8391 -- then neither overrides the other. This can only occur in certain
8392 -- obscure cases (e.g., derivation from homographs created in a generic
8393 -- instantiation).
8395 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
8396 return True;
8398 elsif Ekind (Current_Scope) = E_Package
8399 and then Is_Generic_Instance (Current_Scope)
8400 and then In_Private_Part (Current_Scope)
8401 and then Comes_From_Source (New_E)
8402 then
8403 -- We examine the formals and result type of the inherited operation,
8404 -- to determine whether their type is derived from (the instance of)
8405 -- a generic type. The first such formal or result type is the one
8406 -- tested.
8408 Formal := First_Formal (Prev_E);
8409 while Present (Formal) loop
8410 F_Typ := Base_Type (Etype (Formal));
8412 if Ekind (F_Typ) = E_Anonymous_Access_Type then
8413 F_Typ := Designated_Type (F_Typ);
8414 end if;
8416 G_Typ := Get_Generic_Parent_Type (F_Typ);
8417 exit when Present (G_Typ);
8419 Next_Formal (Formal);
8420 end loop;
8422 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
8423 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
8424 end if;
8426 if No (G_Typ) then
8427 return False;
8428 end if;
8430 -- If the generic type is a private type, then the original operation
8431 -- was not overriding in the generic, because there was no primitive
8432 -- operation to override.
8434 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
8435 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8436 N_Formal_Private_Type_Definition
8437 then
8438 return True;
8440 -- The generic parent type is the ancestor of a formal derived
8441 -- type declaration. We need to check whether it has a primitive
8442 -- operation that should be overridden by New_E in the generic.
8444 else
8445 declare
8446 P_Formal : Entity_Id;
8447 N_Formal : Entity_Id;
8448 P_Typ : Entity_Id;
8449 N_Typ : Entity_Id;
8450 P_Prim : Entity_Id;
8451 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
8453 begin
8454 while Present (Prim_Elt) loop
8455 P_Prim := Node (Prim_Elt);
8457 if Chars (P_Prim) = Chars (New_E)
8458 and then Ekind (P_Prim) = Ekind (New_E)
8459 then
8460 P_Formal := First_Formal (P_Prim);
8461 N_Formal := First_Formal (New_E);
8462 while Present (P_Formal) and then Present (N_Formal) loop
8463 P_Typ := Etype (P_Formal);
8464 N_Typ := Etype (N_Formal);
8466 if not Types_Correspond (P_Typ, N_Typ) then
8467 exit;
8468 end if;
8470 Next_Entity (P_Formal);
8471 Next_Entity (N_Formal);
8472 end loop;
8474 -- Found a matching primitive operation belonging to the
8475 -- formal ancestor type, so the new subprogram is
8476 -- overriding.
8478 if No (P_Formal)
8479 and then No (N_Formal)
8480 and then (Ekind (New_E) /= E_Function
8481 or else
8482 Types_Correspond
8483 (Etype (P_Prim), Etype (New_E)))
8484 then
8485 return False;
8486 end if;
8487 end if;
8489 Next_Elmt (Prim_Elt);
8490 end loop;
8492 -- If no match found, then the new subprogram does not override
8493 -- in the generic (nor in the instance).
8495 -- If the type in question is not abstract, and the subprogram
8496 -- is, this will be an error if the new operation is in the
8497 -- private part of the instance. Emit a warning now, which will
8498 -- make the subsequent error message easier to understand.
8500 if not Is_Abstract_Type (F_Typ)
8501 and then Is_Abstract_Subprogram (Prev_E)
8502 and then In_Private_Part (Current_Scope)
8503 then
8504 Error_Msg_Node_2 := F_Typ;
8505 Error_Msg_NE
8506 ("private operation& in generic unit does not override "
8507 & "any primitive operation of& (RM 12.3 (18))??",
8508 New_E, New_E);
8509 end if;
8511 return True;
8512 end;
8513 end if;
8514 else
8515 return False;
8516 end if;
8517 end Is_Non_Overriding_Operation;
8519 -------------------------------------
8520 -- List_Inherited_Pre_Post_Aspects --
8521 -------------------------------------
8523 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
8524 begin
8525 if Opt.List_Inherited_Aspects
8526 and then Is_Subprogram_Or_Generic_Subprogram (E)
8527 then
8528 declare
8529 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
8530 P : Node_Id;
8532 begin
8533 for J in Inherited'Range loop
8534 P := Pre_Post_Conditions (Contract (Inherited (J)));
8535 while Present (P) loop
8536 Error_Msg_Sloc := Sloc (P);
8538 if Class_Present (P) and then not Split_PPC (P) then
8539 if Pragma_Name (P) = Name_Precondition then
8540 Error_Msg_N ("info: & inherits `Pre''Class` aspect "
8541 & "from #?L?", E);
8542 else
8543 Error_Msg_N ("info: & inherits `Post''Class` aspect "
8544 & "from #?L?", E);
8545 end if;
8546 end if;
8548 P := Next_Pragma (P);
8549 end loop;
8550 end loop;
8551 end;
8552 end if;
8553 end List_Inherited_Pre_Post_Aspects;
8555 ------------------------------
8556 -- Make_Inequality_Operator --
8557 ------------------------------
8559 -- S is the defining identifier of an equality operator. We build a
8560 -- subprogram declaration with the right signature. This operation is
8561 -- intrinsic, because it is always expanded as the negation of the
8562 -- call to the equality function.
8564 procedure Make_Inequality_Operator (S : Entity_Id) is
8565 Loc : constant Source_Ptr := Sloc (S);
8566 Decl : Node_Id;
8567 Formals : List_Id;
8568 Op_Name : Entity_Id;
8570 FF : constant Entity_Id := First_Formal (S);
8571 NF : constant Entity_Id := Next_Formal (FF);
8573 begin
8574 -- Check that equality was properly defined, ignore call if not
8576 if No (NF) then
8577 return;
8578 end if;
8580 declare
8581 A : constant Entity_Id :=
8582 Make_Defining_Identifier (Sloc (FF),
8583 Chars => Chars (FF));
8585 B : constant Entity_Id :=
8586 Make_Defining_Identifier (Sloc (NF),
8587 Chars => Chars (NF));
8589 begin
8590 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
8592 Formals := New_List (
8593 Make_Parameter_Specification (Loc,
8594 Defining_Identifier => A,
8595 Parameter_Type =>
8596 New_Occurrence_Of (Etype (First_Formal (S)),
8597 Sloc (Etype (First_Formal (S))))),
8599 Make_Parameter_Specification (Loc,
8600 Defining_Identifier => B,
8601 Parameter_Type =>
8602 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
8603 Sloc (Etype (Next_Formal (First_Formal (S)))))));
8605 Decl :=
8606 Make_Subprogram_Declaration (Loc,
8607 Specification =>
8608 Make_Function_Specification (Loc,
8609 Defining_Unit_Name => Op_Name,
8610 Parameter_Specifications => Formals,
8611 Result_Definition =>
8612 New_Occurrence_Of (Standard_Boolean, Loc)));
8614 -- Insert inequality right after equality if it is explicit or after
8615 -- the derived type when implicit. These entities are created only
8616 -- for visibility purposes, and eventually replaced in the course
8617 -- of expansion, so they do not need to be attached to the tree and
8618 -- seen by the back-end. Keeping them internal also avoids spurious
8619 -- freezing problems. The declaration is inserted in the tree for
8620 -- analysis, and removed afterwards. If the equality operator comes
8621 -- from an explicit declaration, attach the inequality immediately
8622 -- after. Else the equality is inherited from a derived type
8623 -- declaration, so insert inequality after that declaration.
8625 if No (Alias (S)) then
8626 Insert_After (Unit_Declaration_Node (S), Decl);
8627 elsif Is_List_Member (Parent (S)) then
8628 Insert_After (Parent (S), Decl);
8629 else
8630 Insert_After (Parent (Etype (First_Formal (S))), Decl);
8631 end if;
8633 Mark_Rewrite_Insertion (Decl);
8634 Set_Is_Intrinsic_Subprogram (Op_Name);
8635 Analyze (Decl);
8636 Remove (Decl);
8637 Set_Has_Completion (Op_Name);
8638 Set_Corresponding_Equality (Op_Name, S);
8639 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
8640 end;
8641 end Make_Inequality_Operator;
8643 ----------------------
8644 -- May_Need_Actuals --
8645 ----------------------
8647 procedure May_Need_Actuals (Fun : Entity_Id) is
8648 F : Entity_Id;
8649 B : Boolean;
8651 begin
8652 F := First_Formal (Fun);
8653 B := True;
8654 while Present (F) loop
8655 if No (Default_Value (F)) then
8656 B := False;
8657 exit;
8658 end if;
8660 Next_Formal (F);
8661 end loop;
8663 Set_Needs_No_Actuals (Fun, B);
8664 end May_Need_Actuals;
8666 ---------------------
8667 -- Mode_Conformant --
8668 ---------------------
8670 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8671 Result : Boolean;
8672 begin
8673 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
8674 return Result;
8675 end Mode_Conformant;
8677 ---------------------------
8678 -- New_Overloaded_Entity --
8679 ---------------------------
8681 procedure New_Overloaded_Entity
8682 (S : Entity_Id;
8683 Derived_Type : Entity_Id := Empty)
8685 Overridden_Subp : Entity_Id := Empty;
8686 -- Set if the current scope has an operation that is type-conformant
8687 -- with S, and becomes hidden by S.
8689 Is_Primitive_Subp : Boolean;
8690 -- Set to True if the new subprogram is primitive
8692 E : Entity_Id;
8693 -- Entity that S overrides
8695 Prev_Vis : Entity_Id := Empty;
8696 -- Predecessor of E in Homonym chain
8698 procedure Check_For_Primitive_Subprogram
8699 (Is_Primitive : out Boolean;
8700 Is_Overriding : Boolean := False);
8701 -- If the subprogram being analyzed is a primitive operation of the type
8702 -- of a formal or result, set the Has_Primitive_Operations flag on the
8703 -- type, and set Is_Primitive to True (otherwise set to False). Set the
8704 -- corresponding flag on the entity itself for later use.
8706 procedure Check_Synchronized_Overriding
8707 (Def_Id : Entity_Id;
8708 Overridden_Subp : out Entity_Id);
8709 -- First determine if Def_Id is an entry or a subprogram either defined
8710 -- in the scope of a task or protected type, or is a primitive of such
8711 -- a type. Check whether Def_Id overrides a subprogram of an interface
8712 -- implemented by the synchronized type, return the overridden entity
8713 -- or Empty.
8715 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8716 -- Check that E is declared in the private part of the current package,
8717 -- or in the package body, where it may hide a previous declaration.
8718 -- We can't use In_Private_Part by itself because this flag is also
8719 -- set when freezing entities, so we must examine the place of the
8720 -- declaration in the tree, and recognize wrapper packages as well.
8722 function Is_Overriding_Alias
8723 (Old_E : Entity_Id;
8724 New_E : Entity_Id) return Boolean;
8725 -- Check whether new subprogram and old subprogram are both inherited
8726 -- from subprograms that have distinct dispatch table entries. This can
8727 -- occur with derivations from instances with accidental homonyms. The
8728 -- function is conservative given that the converse is only true within
8729 -- instances that contain accidental overloadings.
8731 ------------------------------------
8732 -- Check_For_Primitive_Subprogram --
8733 ------------------------------------
8735 procedure Check_For_Primitive_Subprogram
8736 (Is_Primitive : out Boolean;
8737 Is_Overriding : Boolean := False)
8739 Formal : Entity_Id;
8740 F_Typ : Entity_Id;
8741 B_Typ : Entity_Id;
8743 function Visible_Part_Type (T : Entity_Id) return Boolean;
8744 -- Returns true if T is declared in the visible part of the current
8745 -- package scope; otherwise returns false. Assumes that T is declared
8746 -- in a package.
8748 procedure Check_Private_Overriding (T : Entity_Id);
8749 -- Checks that if a primitive abstract subprogram of a visible
8750 -- abstract type is declared in a private part, then it must override
8751 -- an abstract subprogram declared in the visible part. Also checks
8752 -- that if a primitive function with a controlling result is declared
8753 -- in a private part, then it must override a function declared in
8754 -- the visible part.
8756 ------------------------------
8757 -- Check_Private_Overriding --
8758 ------------------------------
8760 procedure Check_Private_Overriding (T : Entity_Id) is
8761 begin
8762 if Is_Package_Or_Generic_Package (Current_Scope)
8763 and then In_Private_Part (Current_Scope)
8764 and then Visible_Part_Type (T)
8765 and then not In_Instance
8766 then
8767 if Is_Abstract_Type (T)
8768 and then Is_Abstract_Subprogram (S)
8769 and then (not Is_Overriding
8770 or else not Is_Abstract_Subprogram (E))
8771 then
8772 Error_Msg_N ("abstract subprograms must be visible "
8773 & "(RM 3.9.3(10))!", S);
8775 elsif Ekind (S) = E_Function and then not Is_Overriding then
8776 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
8777 Error_Msg_N ("private function with tagged result must"
8778 & " override visible-part function", S);
8779 Error_Msg_N ("\move subprogram to the visible part"
8780 & " (RM 3.9.3(10))", S);
8782 -- AI05-0073: extend this test to the case of a function
8783 -- with a controlling access result.
8785 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8786 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8787 and then
8788 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
8789 and then Ada_Version >= Ada_2012
8790 then
8791 Error_Msg_N
8792 ("private function with controlling access result "
8793 & "must override visible-part function", S);
8794 Error_Msg_N
8795 ("\move subprogram to the visible part"
8796 & " (RM 3.9.3(10))", S);
8797 end if;
8798 end if;
8799 end if;
8800 end Check_Private_Overriding;
8802 -----------------------
8803 -- Visible_Part_Type --
8804 -----------------------
8806 function Visible_Part_Type (T : Entity_Id) return Boolean is
8807 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8808 N : Node_Id;
8810 begin
8811 -- If the entity is a private type, then it must be declared in a
8812 -- visible part.
8814 if Ekind (T) in Private_Kind then
8815 return True;
8816 end if;
8818 -- Otherwise, we traverse the visible part looking for its
8819 -- corresponding declaration. We cannot use the declaration
8820 -- node directly because in the private part the entity of a
8821 -- private type is the one in the full view, which does not
8822 -- indicate that it is the completion of something visible.
8824 N := First (Visible_Declarations (Specification (P)));
8825 while Present (N) loop
8826 if Nkind (N) = N_Full_Type_Declaration
8827 and then Present (Defining_Identifier (N))
8828 and then T = Defining_Identifier (N)
8829 then
8830 return True;
8832 elsif Nkind_In (N, N_Private_Type_Declaration,
8833 N_Private_Extension_Declaration)
8834 and then Present (Defining_Identifier (N))
8835 and then T = Full_View (Defining_Identifier (N))
8836 then
8837 return True;
8838 end if;
8840 Next (N);
8841 end loop;
8843 return False;
8844 end Visible_Part_Type;
8846 -- Start of processing for Check_For_Primitive_Subprogram
8848 begin
8849 Is_Primitive := False;
8851 if not Comes_From_Source (S) then
8852 null;
8854 -- If subprogram is at library level, it is not primitive operation
8856 elsif Current_Scope = Standard_Standard then
8857 null;
8859 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8860 and then not In_Package_Body (Current_Scope))
8861 or else Is_Overriding
8862 then
8863 -- For function, check return type
8865 if Ekind (S) = E_Function then
8866 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8867 F_Typ := Designated_Type (Etype (S));
8868 else
8869 F_Typ := Etype (S);
8870 end if;
8872 B_Typ := Base_Type (F_Typ);
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_Has_Primitive_Operations (B_Typ);
8880 Set_Is_Primitive (S);
8881 Check_Private_Overriding (B_Typ);
8882 end if;
8883 end if;
8885 -- For all subprograms, check formals
8887 Formal := First_Formal (S);
8888 while Present (Formal) loop
8889 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8890 F_Typ := Designated_Type (Etype (Formal));
8891 else
8892 F_Typ := Etype (Formal);
8893 end if;
8895 B_Typ := Base_Type (F_Typ);
8897 if Ekind (B_Typ) = E_Access_Subtype then
8898 B_Typ := Base_Type (B_Typ);
8899 end if;
8901 if Scope (B_Typ) = Current_Scope
8902 and then not Is_Class_Wide_Type (B_Typ)
8903 and then not Is_Generic_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;
8911 Next_Formal (Formal);
8912 end loop;
8914 -- Special case: An equality function can be redefined for a type
8915 -- occurring in a declarative part, and won't otherwise be treated as
8916 -- a primitive because it doesn't occur in a package spec and doesn't
8917 -- override an inherited subprogram. It's important that we mark it
8918 -- primitive so it can be returned by Collect_Primitive_Operations
8919 -- and be used in composing the equality operation of later types
8920 -- that have a component of the type.
8922 elsif Chars (S) = Name_Op_Eq
8923 and then Etype (S) = Standard_Boolean
8924 then
8925 B_Typ := Base_Type (Etype (First_Formal (S)));
8927 if Scope (B_Typ) = Current_Scope
8928 and then
8929 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
8930 and then not Is_Limited_Type (B_Typ)
8931 then
8932 Is_Primitive := True;
8933 Set_Is_Primitive (S);
8934 Set_Has_Primitive_Operations (B_Typ);
8935 Check_Private_Overriding (B_Typ);
8936 end if;
8937 end if;
8938 end Check_For_Primitive_Subprogram;
8940 -----------------------------------
8941 -- Check_Synchronized_Overriding --
8942 -----------------------------------
8944 procedure Check_Synchronized_Overriding
8945 (Def_Id : Entity_Id;
8946 Overridden_Subp : out Entity_Id)
8948 Ifaces_List : Elist_Id;
8949 In_Scope : Boolean;
8950 Typ : Entity_Id;
8952 function Matches_Prefixed_View_Profile
8953 (Prim_Params : List_Id;
8954 Iface_Params : List_Id) return Boolean;
8955 -- Determine whether a subprogram's parameter profile Prim_Params
8956 -- matches that of a potentially overridden interface subprogram
8957 -- Iface_Params. Also determine if the type of first parameter of
8958 -- Iface_Params is an implemented interface.
8960 -----------------------------------
8961 -- Matches_Prefixed_View_Profile --
8962 -----------------------------------
8964 function Matches_Prefixed_View_Profile
8965 (Prim_Params : List_Id;
8966 Iface_Params : List_Id) return Boolean
8968 Iface_Id : Entity_Id;
8969 Iface_Param : Node_Id;
8970 Iface_Typ : Entity_Id;
8971 Prim_Id : Entity_Id;
8972 Prim_Param : Node_Id;
8973 Prim_Typ : Entity_Id;
8975 function Is_Implemented
8976 (Ifaces_List : Elist_Id;
8977 Iface : Entity_Id) return Boolean;
8978 -- Determine if Iface is implemented by the current task or
8979 -- protected type.
8981 --------------------
8982 -- Is_Implemented --
8983 --------------------
8985 function Is_Implemented
8986 (Ifaces_List : Elist_Id;
8987 Iface : Entity_Id) return Boolean
8989 Iface_Elmt : Elmt_Id;
8991 begin
8992 Iface_Elmt := First_Elmt (Ifaces_List);
8993 while Present (Iface_Elmt) loop
8994 if Node (Iface_Elmt) = Iface then
8995 return True;
8996 end if;
8998 Next_Elmt (Iface_Elmt);
8999 end loop;
9001 return False;
9002 end Is_Implemented;
9004 -- Start of processing for Matches_Prefixed_View_Profile
9006 begin
9007 Iface_Param := First (Iface_Params);
9008 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9010 if Is_Access_Type (Iface_Typ) then
9011 Iface_Typ := Designated_Type (Iface_Typ);
9012 end if;
9014 Prim_Param := First (Prim_Params);
9016 -- The first parameter of the potentially overridden subprogram
9017 -- must be an interface implemented by Prim.
9019 if not Is_Interface (Iface_Typ)
9020 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9021 then
9022 return False;
9023 end if;
9025 -- The checks on the object parameters are done, move onto the
9026 -- rest of the parameters.
9028 if not In_Scope then
9029 Prim_Param := Next (Prim_Param);
9030 end if;
9032 Iface_Param := Next (Iface_Param);
9033 while Present (Iface_Param) and then Present (Prim_Param) loop
9034 Iface_Id := Defining_Identifier (Iface_Param);
9035 Iface_Typ := Find_Parameter_Type (Iface_Param);
9037 Prim_Id := Defining_Identifier (Prim_Param);
9038 Prim_Typ := Find_Parameter_Type (Prim_Param);
9040 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9041 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9042 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9043 then
9044 Iface_Typ := Designated_Type (Iface_Typ);
9045 Prim_Typ := Designated_Type (Prim_Typ);
9046 end if;
9048 -- Case of multiple interface types inside a parameter profile
9050 -- (Obj_Param : in out Iface; ...; Param : Iface)
9052 -- If the interface type is implemented, then the matching type
9053 -- in the primitive should be the implementing record type.
9055 if Ekind (Iface_Typ) = E_Record_Type
9056 and then Is_Interface (Iface_Typ)
9057 and then Is_Implemented (Ifaces_List, Iface_Typ)
9058 then
9059 if Prim_Typ /= Typ then
9060 return False;
9061 end if;
9063 -- The two parameters must be both mode and subtype conformant
9065 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9066 or else not
9067 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9068 then
9069 return False;
9070 end if;
9072 Next (Iface_Param);
9073 Next (Prim_Param);
9074 end loop;
9076 -- One of the two lists contains more parameters than the other
9078 if Present (Iface_Param) or else Present (Prim_Param) then
9079 return False;
9080 end if;
9082 return True;
9083 end Matches_Prefixed_View_Profile;
9085 -- Start of processing for Check_Synchronized_Overriding
9087 begin
9088 Overridden_Subp := Empty;
9090 -- Def_Id must be an entry or a subprogram. We should skip predefined
9091 -- primitives internally generated by the frontend; however at this
9092 -- stage predefined primitives are still not fully decorated. As a
9093 -- minor optimization we skip here internally generated subprograms.
9095 if (Ekind (Def_Id) /= E_Entry
9096 and then Ekind (Def_Id) /= E_Function
9097 and then Ekind (Def_Id) /= E_Procedure)
9098 or else not Comes_From_Source (Def_Id)
9099 then
9100 return;
9101 end if;
9103 -- Search for the concurrent declaration since it contains the list
9104 -- of all implemented interfaces. In this case, the subprogram is
9105 -- declared within the scope of a protected or a task type.
9107 if Present (Scope (Def_Id))
9108 and then Is_Concurrent_Type (Scope (Def_Id))
9109 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9110 then
9111 Typ := Scope (Def_Id);
9112 In_Scope := True;
9114 -- The enclosing scope is not a synchronized type and the subprogram
9115 -- has no formals.
9117 elsif No (First_Formal (Def_Id)) then
9118 return;
9120 -- The subprogram has formals and hence it may be a primitive of a
9121 -- concurrent type.
9123 else
9124 Typ := Etype (First_Formal (Def_Id));
9126 if Is_Access_Type (Typ) then
9127 Typ := Directly_Designated_Type (Typ);
9128 end if;
9130 if Is_Concurrent_Type (Typ)
9131 and then not Is_Generic_Actual_Type (Typ)
9132 then
9133 In_Scope := False;
9135 -- This case occurs when the concurrent type is declared within
9136 -- a generic unit. As a result the corresponding record has been
9137 -- built and used as the type of the first formal, we just have
9138 -- to retrieve the corresponding concurrent type.
9140 elsif Is_Concurrent_Record_Type (Typ)
9141 and then not Is_Class_Wide_Type (Typ)
9142 and then Present (Corresponding_Concurrent_Type (Typ))
9143 then
9144 Typ := Corresponding_Concurrent_Type (Typ);
9145 In_Scope := False;
9147 else
9148 return;
9149 end if;
9150 end if;
9152 -- There is no overriding to check if is an inherited operation in a
9153 -- type derivation on for a generic actual.
9155 Collect_Interfaces (Typ, Ifaces_List);
9157 if Is_Empty_Elmt_List (Ifaces_List) then
9158 return;
9159 end if;
9161 -- Determine whether entry or subprogram Def_Id overrides a primitive
9162 -- operation that belongs to one of the interfaces in Ifaces_List.
9164 declare
9165 Candidate : Entity_Id := Empty;
9166 Hom : Entity_Id := Empty;
9167 Iface_Typ : Entity_Id;
9168 Subp : Entity_Id := Empty;
9170 begin
9171 -- Traverse the homonym chain, looking for a potentially
9172 -- overridden subprogram that belongs to an implemented
9173 -- interface.
9175 Hom := Current_Entity_In_Scope (Def_Id);
9176 while Present (Hom) loop
9177 Subp := Hom;
9179 if Subp = Def_Id
9180 or else not Is_Overloadable (Subp)
9181 or else not Is_Primitive (Subp)
9182 or else not Is_Dispatching_Operation (Subp)
9183 or else not Present (Find_Dispatching_Type (Subp))
9184 or else not Is_Interface (Find_Dispatching_Type (Subp))
9185 then
9186 null;
9188 -- Entries and procedures can override abstract or null
9189 -- interface procedures.
9191 elsif (Ekind (Def_Id) = E_Procedure
9192 or else Ekind (Def_Id) = E_Entry)
9193 and then Ekind (Subp) = E_Procedure
9194 and then Matches_Prefixed_View_Profile
9195 (Parameter_Specifications (Parent (Def_Id)),
9196 Parameter_Specifications (Parent (Subp)))
9197 then
9198 Candidate := Subp;
9200 -- For an overridden subprogram Subp, check whether the mode
9201 -- of its first parameter is correct depending on the kind
9202 -- of synchronized type.
9204 declare
9205 Formal : constant Node_Id := First_Formal (Candidate);
9207 begin
9208 -- In order for an entry or a protected procedure to
9209 -- override, the first parameter of the overridden
9210 -- routine must be of mode "out", "in out" or
9211 -- access-to-variable.
9213 if Ekind_In (Candidate, E_Entry, E_Procedure)
9214 and then Is_Protected_Type (Typ)
9215 and then Ekind (Formal) /= E_In_Out_Parameter
9216 and then Ekind (Formal) /= E_Out_Parameter
9217 and then Nkind (Parameter_Type (Parent (Formal))) /=
9218 N_Access_Definition
9219 then
9220 null;
9222 -- All other cases are OK since a task entry or routine
9223 -- does not have a restriction on the mode of the first
9224 -- parameter of the overridden interface routine.
9226 else
9227 Overridden_Subp := Candidate;
9228 return;
9229 end if;
9230 end;
9232 -- Functions can override abstract interface functions
9234 elsif Ekind (Def_Id) = E_Function
9235 and then Ekind (Subp) = E_Function
9236 and then Matches_Prefixed_View_Profile
9237 (Parameter_Specifications (Parent (Def_Id)),
9238 Parameter_Specifications (Parent (Subp)))
9239 and then Etype (Result_Definition (Parent (Def_Id))) =
9240 Etype (Result_Definition (Parent (Subp)))
9241 then
9242 Overridden_Subp := Subp;
9243 return;
9244 end if;
9246 Hom := Homonym (Hom);
9247 end loop;
9249 -- After examining all candidates for overriding, we are left with
9250 -- the best match which is a mode incompatible interface routine.
9251 -- Do not emit an error if the Expander is active since this error
9252 -- will be detected later on after all concurrent types are
9253 -- expanded and all wrappers are built. This check is meant for
9254 -- spec-only compilations.
9256 if Present (Candidate) and then not Expander_Active then
9257 Iface_Typ :=
9258 Find_Parameter_Type (Parent (First_Formal (Candidate)));
9260 -- Def_Id is primitive of a protected type, declared inside the
9261 -- type, and the candidate is primitive of a limited or
9262 -- synchronized interface.
9264 if In_Scope
9265 and then Is_Protected_Type (Typ)
9266 and then
9267 (Is_Limited_Interface (Iface_Typ)
9268 or else Is_Protected_Interface (Iface_Typ)
9269 or else Is_Synchronized_Interface (Iface_Typ)
9270 or else Is_Task_Interface (Iface_Typ))
9271 then
9272 Error_Msg_PT (Parent (Typ), Candidate);
9273 end if;
9274 end if;
9276 Overridden_Subp := Candidate;
9277 return;
9278 end;
9279 end Check_Synchronized_Overriding;
9281 ----------------------------
9282 -- Is_Private_Declaration --
9283 ----------------------------
9285 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9286 Priv_Decls : List_Id;
9287 Decl : constant Node_Id := Unit_Declaration_Node (E);
9289 begin
9290 if Is_Package_Or_Generic_Package (Current_Scope)
9291 and then In_Private_Part (Current_Scope)
9292 then
9293 Priv_Decls :=
9294 Private_Declarations (Package_Specification (Current_Scope));
9296 return In_Package_Body (Current_Scope)
9297 or else
9298 (Is_List_Member (Decl)
9299 and then List_Containing (Decl) = Priv_Decls)
9300 or else (Nkind (Parent (Decl)) = N_Package_Specification
9301 and then not
9302 Is_Compilation_Unit
9303 (Defining_Entity (Parent (Decl)))
9304 and then List_Containing (Parent (Parent (Decl))) =
9305 Priv_Decls);
9306 else
9307 return False;
9308 end if;
9309 end Is_Private_Declaration;
9311 --------------------------
9312 -- Is_Overriding_Alias --
9313 --------------------------
9315 function Is_Overriding_Alias
9316 (Old_E : Entity_Id;
9317 New_E : Entity_Id) return Boolean
9319 AO : constant Entity_Id := Alias (Old_E);
9320 AN : constant Entity_Id := Alias (New_E);
9321 begin
9322 return Scope (AO) /= Scope (AN)
9323 or else No (DTC_Entity (AO))
9324 or else No (DTC_Entity (AN))
9325 or else DT_Position (AO) = DT_Position (AN);
9326 end Is_Overriding_Alias;
9328 -- Start of processing for New_Overloaded_Entity
9330 begin
9331 -- We need to look for an entity that S may override. This must be a
9332 -- homonym in the current scope, so we look for the first homonym of
9333 -- S in the current scope as the starting point for the search.
9335 E := Current_Entity_In_Scope (S);
9337 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
9338 -- They are directly added to the list of primitive operations of
9339 -- Derived_Type, unless this is a rederivation in the private part
9340 -- of an operation that was already derived in the visible part of
9341 -- the current package.
9343 if Ada_Version >= Ada_2005
9344 and then Present (Derived_Type)
9345 and then Present (Alias (S))
9346 and then Is_Dispatching_Operation (Alias (S))
9347 and then Present (Find_Dispatching_Type (Alias (S)))
9348 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
9349 then
9350 -- For private types, when the full-view is processed we propagate to
9351 -- the full view the non-overridden entities whose attribute "alias"
9352 -- references an interface primitive. These entities were added by
9353 -- Derive_Subprograms to ensure that interface primitives are
9354 -- covered.
9356 -- Inside_Freeze_Actions is non zero when S corresponds with an
9357 -- internal entity that links an interface primitive with its
9358 -- covering primitive through attribute Interface_Alias (see
9359 -- Add_Internal_Interface_Entities).
9361 if Inside_Freezing_Actions = 0
9362 and then Is_Package_Or_Generic_Package (Current_Scope)
9363 and then In_Private_Part (Current_Scope)
9364 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
9365 and then Nkind (Parent (S)) = N_Full_Type_Declaration
9366 and then Full_View (Defining_Identifier (Parent (E)))
9367 = Defining_Identifier (Parent (S))
9368 and then Alias (E) = Alias (S)
9369 then
9370 Check_Operation_From_Private_View (S, E);
9371 Set_Is_Dispatching_Operation (S);
9373 -- Common case
9375 else
9376 Enter_Overloaded_Entity (S);
9377 Check_Dispatching_Operation (S, Empty);
9378 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9379 end if;
9381 return;
9382 end if;
9384 -- If there is no homonym then this is definitely not overriding
9386 if No (E) then
9387 Enter_Overloaded_Entity (S);
9388 Check_Dispatching_Operation (S, Empty);
9389 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9391 -- If subprogram has an explicit declaration, check whether it has an
9392 -- overriding indicator.
9394 if Comes_From_Source (S) then
9395 Check_Synchronized_Overriding (S, Overridden_Subp);
9397 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
9398 -- it may have overridden some hidden inherited primitive. Update
9399 -- Overridden_Subp to avoid spurious errors when checking the
9400 -- overriding indicator.
9402 if Ada_Version >= Ada_2012
9403 and then No (Overridden_Subp)
9404 and then Is_Dispatching_Operation (S)
9405 and then Present (Overridden_Operation (S))
9406 then
9407 Overridden_Subp := Overridden_Operation (S);
9408 end if;
9410 Check_Overriding_Indicator
9411 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9412 end if;
9414 -- If there is a homonym that is not overloadable, then we have an
9415 -- error, except for the special cases checked explicitly below.
9417 elsif not Is_Overloadable (E) then
9419 -- Check for spurious conflict produced by a subprogram that has the
9420 -- same name as that of the enclosing generic package. The conflict
9421 -- occurs within an instance, between the subprogram and the renaming
9422 -- declaration for the package. After the subprogram, the package
9423 -- renaming declaration becomes hidden.
9425 if Ekind (E) = E_Package
9426 and then Present (Renamed_Object (E))
9427 and then Renamed_Object (E) = Current_Scope
9428 and then Nkind (Parent (Renamed_Object (E))) =
9429 N_Package_Specification
9430 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
9431 then
9432 Set_Is_Hidden (E);
9433 Set_Is_Immediately_Visible (E, False);
9434 Enter_Overloaded_Entity (S);
9435 Set_Homonym (S, Homonym (E));
9436 Check_Dispatching_Operation (S, Empty);
9437 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
9439 -- If the subprogram is implicit it is hidden by the previous
9440 -- declaration. However if it is dispatching, it must appear in the
9441 -- dispatch table anyway, because it can be dispatched to even if it
9442 -- cannot be called directly.
9444 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
9445 Set_Scope (S, Current_Scope);
9447 if Is_Dispatching_Operation (Alias (S)) then
9448 Check_Dispatching_Operation (S, Empty);
9449 end if;
9451 return;
9453 else
9454 Error_Msg_Sloc := Sloc (E);
9456 -- Generate message, with useful additional warning if in generic
9458 if Is_Generic_Unit (E) then
9459 Error_Msg_N ("previous generic unit cannot be overloaded", S);
9460 Error_Msg_N ("\& conflicts with declaration#", S);
9461 else
9462 Error_Msg_N ("& conflicts with declaration#", S);
9463 end if;
9465 return;
9466 end if;
9468 -- E exists and is overloadable
9470 else
9471 Check_Synchronized_Overriding (S, Overridden_Subp);
9473 -- Loop through E and its homonyms to determine if any of them is
9474 -- the candidate for overriding by S.
9476 while Present (E) loop
9478 -- Definitely not interesting if not in the current scope
9480 if Scope (E) /= Current_Scope then
9481 null;
9483 -- A function can overload the name of an abstract state. The
9484 -- state can be viewed as a function with a profile that cannot
9485 -- be matched by anything.
9487 elsif Ekind (S) = E_Function
9488 and then Ekind (E) = E_Abstract_State
9489 then
9490 Enter_Overloaded_Entity (S);
9491 return;
9493 -- Ada 2012 (AI05-0165): For internally generated bodies of null
9494 -- procedures locate the internally generated spec. We enforce
9495 -- mode conformance since a tagged type may inherit from
9496 -- interfaces several null primitives which differ only in
9497 -- the mode of the formals.
9499 elsif not Comes_From_Source (S)
9500 and then Is_Null_Procedure (S)
9501 and then not Mode_Conformant (E, S)
9502 then
9503 null;
9505 -- Check if we have type conformance
9507 elsif Type_Conformant (E, S) then
9509 -- If the old and new entities have the same profile and one
9510 -- is not the body of the other, then this is an error, unless
9511 -- one of them is implicitly declared.
9513 -- There are some cases when both can be implicit, for example
9514 -- when both a literal and a function that overrides it are
9515 -- inherited in a derivation, or when an inherited operation
9516 -- of a tagged full type overrides the inherited operation of
9517 -- a private extension. Ada 83 had a special rule for the
9518 -- literal case. In Ada 95, the later implicit operation hides
9519 -- the former, and the literal is always the former. In the
9520 -- odd case where both are derived operations declared at the
9521 -- same point, both operations should be declared, and in that
9522 -- case we bypass the following test and proceed to the next
9523 -- part. This can only occur for certain obscure cases in
9524 -- instances, when an operation on a type derived from a formal
9525 -- private type does not override a homograph inherited from
9526 -- the actual. In subsequent derivations of such a type, the
9527 -- DT positions of these operations remain distinct, if they
9528 -- have been set.
9530 if Present (Alias (S))
9531 and then (No (Alias (E))
9532 or else Comes_From_Source (E)
9533 or else Is_Abstract_Subprogram (S)
9534 or else
9535 (Is_Dispatching_Operation (E)
9536 and then Is_Overriding_Alias (E, S)))
9537 and then Ekind (E) /= E_Enumeration_Literal
9538 then
9539 -- When an derived operation is overloaded it may be due to
9540 -- the fact that the full view of a private extension
9541 -- re-inherits. It has to be dealt with.
9543 if Is_Package_Or_Generic_Package (Current_Scope)
9544 and then In_Private_Part (Current_Scope)
9545 then
9546 Check_Operation_From_Private_View (S, E);
9547 end if;
9549 -- In any case the implicit operation remains hidden by the
9550 -- existing declaration, which is overriding. Indicate that
9551 -- E overrides the operation from which S is inherited.
9553 if Present (Alias (S)) then
9554 Set_Overridden_Operation (E, Alias (S));
9555 Inherit_Subprogram_Contract (E, Alias (S));
9557 else
9558 Set_Overridden_Operation (E, S);
9559 Inherit_Subprogram_Contract (E, S);
9560 end if;
9562 if Comes_From_Source (E) then
9563 Check_Overriding_Indicator (E, S, Is_Primitive => False);
9564 end if;
9566 return;
9568 -- Within an instance, the renaming declarations for actual
9569 -- subprograms may become ambiguous, but they do not hide each
9570 -- other.
9572 elsif Ekind (E) /= E_Entry
9573 and then not Comes_From_Source (E)
9574 and then not Is_Generic_Instance (E)
9575 and then (Present (Alias (E))
9576 or else Is_Intrinsic_Subprogram (E))
9577 and then (not In_Instance
9578 or else No (Parent (E))
9579 or else Nkind (Unit_Declaration_Node (E)) /=
9580 N_Subprogram_Renaming_Declaration)
9581 then
9582 -- A subprogram child unit is not allowed to override an
9583 -- inherited subprogram (10.1.1(20)).
9585 if Is_Child_Unit (S) then
9586 Error_Msg_N
9587 ("child unit overrides inherited subprogram in parent",
9589 return;
9590 end if;
9592 if Is_Non_Overriding_Operation (E, S) then
9593 Enter_Overloaded_Entity (S);
9595 if No (Derived_Type)
9596 or else Is_Tagged_Type (Derived_Type)
9597 then
9598 Check_Dispatching_Operation (S, Empty);
9599 end if;
9601 return;
9602 end if;
9604 -- E is a derived operation or an internal operator which
9605 -- is being overridden. Remove E from further visibility.
9606 -- Furthermore, if E is a dispatching operation, it must be
9607 -- replaced in the list of primitive operations of its type
9608 -- (see Override_Dispatching_Operation).
9610 Overridden_Subp := E;
9612 declare
9613 Prev : Entity_Id;
9615 begin
9616 Prev := First_Entity (Current_Scope);
9617 while Present (Prev) and then Next_Entity (Prev) /= E loop
9618 Next_Entity (Prev);
9619 end loop;
9621 -- It is possible for E to be in the current scope and
9622 -- yet not in the entity chain. This can only occur in a
9623 -- generic context where E is an implicit concatenation
9624 -- in the formal part, because in a generic body the
9625 -- entity chain starts with the formals.
9627 pragma Assert
9628 (Present (Prev) or else Chars (E) = Name_Op_Concat);
9630 -- E must be removed both from the entity_list of the
9631 -- current scope, and from the visibility chain
9633 if Debug_Flag_E then
9634 Write_Str ("Override implicit operation ");
9635 Write_Int (Int (E));
9636 Write_Eol;
9637 end if;
9639 -- If E is a predefined concatenation, it stands for four
9640 -- different operations. As a result, a single explicit
9641 -- declaration does not hide it. In a possible ambiguous
9642 -- situation, Disambiguate chooses the user-defined op,
9643 -- so it is correct to retain the previous internal one.
9645 if Chars (E) /= Name_Op_Concat
9646 or else Ekind (E) /= E_Operator
9647 then
9648 -- For nondispatching derived operations that are
9649 -- overridden by a subprogram declared in the private
9650 -- part of a package, we retain the derived subprogram
9651 -- but mark it as not immediately visible. If the
9652 -- derived operation was declared in the visible part
9653 -- then this ensures that it will still be visible
9654 -- outside the package with the proper signature
9655 -- (calls from outside must also be directed to this
9656 -- version rather than the overriding one, unlike the
9657 -- dispatching case). Calls from inside the package
9658 -- will still resolve to the overriding subprogram
9659 -- since the derived one is marked as not visible
9660 -- within the package.
9662 -- If the private operation is dispatching, we achieve
9663 -- the overriding by keeping the implicit operation
9664 -- but setting its alias to be the overriding one. In
9665 -- this fashion the proper body is executed in all
9666 -- cases, but the original signature is used outside
9667 -- of the package.
9669 -- If the overriding is not in the private part, we
9670 -- remove the implicit operation altogether.
9672 if Is_Private_Declaration (S) then
9673 if not Is_Dispatching_Operation (E) then
9674 Set_Is_Immediately_Visible (E, False);
9675 else
9676 -- Work done in Override_Dispatching_Operation,
9677 -- so nothing else needs to be done here.
9679 null;
9680 end if;
9682 else
9683 -- Find predecessor of E in Homonym chain
9685 if E = Current_Entity (E) then
9686 Prev_Vis := Empty;
9687 else
9688 Prev_Vis := Current_Entity (E);
9689 while Homonym (Prev_Vis) /= E loop
9690 Prev_Vis := Homonym (Prev_Vis);
9691 end loop;
9692 end if;
9694 if Prev_Vis /= Empty then
9696 -- Skip E in the visibility chain
9698 Set_Homonym (Prev_Vis, Homonym (E));
9700 else
9701 Set_Name_Entity_Id (Chars (E), Homonym (E));
9702 end if;
9704 Set_Next_Entity (Prev, Next_Entity (E));
9706 if No (Next_Entity (Prev)) then
9707 Set_Last_Entity (Current_Scope, Prev);
9708 end if;
9709 end if;
9710 end if;
9712 Enter_Overloaded_Entity (S);
9714 -- For entities generated by Derive_Subprograms the
9715 -- overridden operation is the inherited primitive
9716 -- (which is available through the attribute alias).
9718 if not (Comes_From_Source (E))
9719 and then Is_Dispatching_Operation (E)
9720 and then Find_Dispatching_Type (E) =
9721 Find_Dispatching_Type (S)
9722 and then Present (Alias (E))
9723 and then Comes_From_Source (Alias (E))
9724 then
9725 Set_Overridden_Operation (S, Alias (E));
9726 Inherit_Subprogram_Contract (S, Alias (E));
9728 -- Normal case of setting entity as overridden
9730 -- Note: Static_Initialization and Overridden_Operation
9731 -- attributes use the same field in subprogram entities.
9732 -- Static_Initialization is only defined for internal
9733 -- initialization procedures, where Overridden_Operation
9734 -- is irrelevant. Therefore the setting of this attribute
9735 -- must check whether the target is an init_proc.
9737 elsif not Is_Init_Proc (S) then
9738 Set_Overridden_Operation (S, E);
9739 Inherit_Subprogram_Contract (S, E);
9740 end if;
9742 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9744 -- If S is a user-defined subprogram or a null procedure
9745 -- expanded to override an inherited null procedure, or a
9746 -- predefined dispatching primitive then indicate that E
9747 -- overrides the operation from which S is inherited.
9749 if Comes_From_Source (S)
9750 or else
9751 (Present (Parent (S))
9752 and then
9753 Nkind (Parent (S)) = N_Procedure_Specification
9754 and then
9755 Null_Present (Parent (S)))
9756 or else
9757 (Present (Alias (E))
9758 and then
9759 Is_Predefined_Dispatching_Operation (Alias (E)))
9760 then
9761 if Present (Alias (E)) then
9762 Set_Overridden_Operation (S, Alias (E));
9763 Inherit_Subprogram_Contract (S, Alias (E));
9764 end if;
9765 end if;
9767 if Is_Dispatching_Operation (E) then
9769 -- An overriding dispatching subprogram inherits the
9770 -- convention of the overridden subprogram (AI-117).
9772 Set_Convention (S, Convention (E));
9773 Check_Dispatching_Operation (S, E);
9775 else
9776 Check_Dispatching_Operation (S, Empty);
9777 end if;
9779 Check_For_Primitive_Subprogram
9780 (Is_Primitive_Subp, Is_Overriding => True);
9781 goto Check_Inequality;
9782 end;
9784 -- Apparent redeclarations in instances can occur when two
9785 -- formal types get the same actual type. The subprograms in
9786 -- in the instance are legal, even if not callable from the
9787 -- outside. Calls from within are disambiguated elsewhere.
9788 -- For dispatching operations in the visible part, the usual
9789 -- rules apply, and operations with the same profile are not
9790 -- legal (B830001).
9792 elsif (In_Instance_Visible_Part
9793 and then not Is_Dispatching_Operation (E))
9794 or else In_Instance_Not_Visible
9795 then
9796 null;
9798 -- Here we have a real error (identical profile)
9800 else
9801 Error_Msg_Sloc := Sloc (E);
9803 -- Avoid cascaded errors if the entity appears in
9804 -- subsequent calls.
9806 Set_Scope (S, Current_Scope);
9808 -- Generate error, with extra useful warning for the case
9809 -- of a generic instance with no completion.
9811 if Is_Generic_Instance (S)
9812 and then not Has_Completion (E)
9813 then
9814 Error_Msg_N
9815 ("instantiation cannot provide body for&", S);
9816 Error_Msg_N ("\& conflicts with declaration#", S);
9817 else
9818 Error_Msg_N ("& conflicts with declaration#", S);
9819 end if;
9821 return;
9822 end if;
9824 else
9825 -- If one subprogram has an access parameter and the other
9826 -- a parameter of an access type, calls to either might be
9827 -- ambiguous. Verify that parameters match except for the
9828 -- access parameter.
9830 if May_Hide_Profile then
9831 declare
9832 F1 : Entity_Id;
9833 F2 : Entity_Id;
9835 begin
9836 F1 := First_Formal (S);
9837 F2 := First_Formal (E);
9838 while Present (F1) and then Present (F2) loop
9839 if Is_Access_Type (Etype (F1)) then
9840 if not Is_Access_Type (Etype (F2))
9841 or else not Conforming_Types
9842 (Designated_Type (Etype (F1)),
9843 Designated_Type (Etype (F2)),
9844 Type_Conformant)
9845 then
9846 May_Hide_Profile := False;
9847 end if;
9849 elsif
9850 not Conforming_Types
9851 (Etype (F1), Etype (F2), Type_Conformant)
9852 then
9853 May_Hide_Profile := False;
9854 end if;
9856 Next_Formal (F1);
9857 Next_Formal (F2);
9858 end loop;
9860 if May_Hide_Profile
9861 and then No (F1)
9862 and then No (F2)
9863 then
9864 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
9865 end if;
9866 end;
9867 end if;
9868 end if;
9870 E := Homonym (E);
9871 end loop;
9873 -- On exit, we know that S is a new entity
9875 Enter_Overloaded_Entity (S);
9876 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9877 Check_Overriding_Indicator
9878 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9880 -- Overloading is not allowed in SPARK, except for operators
9882 if Nkind (S) /= N_Defining_Operator_Symbol then
9883 Error_Msg_Sloc := Sloc (Homonym (S));
9884 Check_SPARK_05_Restriction
9885 ("overloading not allowed with entity#", S);
9886 end if;
9888 -- If S is a derived operation for an untagged type then by
9889 -- definition it's not a dispatching operation (even if the parent
9890 -- operation was dispatching), so Check_Dispatching_Operation is not
9891 -- called in that case.
9893 if No (Derived_Type)
9894 or else Is_Tagged_Type (Derived_Type)
9895 then
9896 Check_Dispatching_Operation (S, Empty);
9897 end if;
9898 end if;
9900 -- If this is a user-defined equality operator that is not a derived
9901 -- subprogram, create the corresponding inequality. If the operation is
9902 -- dispatching, the expansion is done elsewhere, and we do not create
9903 -- an explicit inequality operation.
9905 <<Check_Inequality>>
9906 if Chars (S) = Name_Op_Eq
9907 and then Etype (S) = Standard_Boolean
9908 and then Present (Parent (S))
9909 and then not Is_Dispatching_Operation (S)
9910 then
9911 Make_Inequality_Operator (S);
9912 Check_Untagged_Equality (S);
9913 end if;
9914 end New_Overloaded_Entity;
9916 ---------------------
9917 -- Process_Formals --
9918 ---------------------
9920 procedure Process_Formals
9921 (T : List_Id;
9922 Related_Nod : Node_Id)
9924 Param_Spec : Node_Id;
9925 Formal : Entity_Id;
9926 Formal_Type : Entity_Id;
9927 Default : Node_Id;
9928 Ptype : Entity_Id;
9930 Num_Out_Params : Nat := 0;
9931 First_Out_Param : Entity_Id := Empty;
9932 -- Used for setting Is_Only_Out_Parameter
9934 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
9935 -- Determine whether an access type designates a type coming from a
9936 -- limited view.
9938 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
9939 -- Check whether the default has a class-wide type. After analysis the
9940 -- default has the type of the formal, so we must also check explicitly
9941 -- for an access attribute.
9943 ----------------------------------
9944 -- Designates_From_Limited_With --
9945 ----------------------------------
9947 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
9948 Desig : Entity_Id := Typ;
9950 begin
9951 if Is_Access_Type (Desig) then
9952 Desig := Directly_Designated_Type (Desig);
9953 end if;
9955 if Is_Class_Wide_Type (Desig) then
9956 Desig := Root_Type (Desig);
9957 end if;
9959 return
9960 Ekind (Desig) = E_Incomplete_Type
9961 and then From_Limited_With (Desig);
9962 end Designates_From_Limited_With;
9964 ---------------------------
9965 -- Is_Class_Wide_Default --
9966 ---------------------------
9968 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9969 begin
9970 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9971 or else (Nkind (D) = N_Attribute_Reference
9972 and then Attribute_Name (D) = Name_Access
9973 and then Is_Class_Wide_Type (Etype (Prefix (D))));
9974 end Is_Class_Wide_Default;
9976 -- Start of processing for Process_Formals
9978 begin
9979 -- In order to prevent premature use of the formals in the same formal
9980 -- part, the Ekind is left undefined until all default expressions are
9981 -- analyzed. The Ekind is established in a separate loop at the end.
9983 Param_Spec := First (T);
9984 while Present (Param_Spec) loop
9985 Formal := Defining_Identifier (Param_Spec);
9986 Set_Never_Set_In_Source (Formal, True);
9987 Enter_Name (Formal);
9989 -- Case of ordinary parameters
9991 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9992 Find_Type (Parameter_Type (Param_Spec));
9993 Ptype := Parameter_Type (Param_Spec);
9995 if Ptype = Error then
9996 goto Continue;
9997 end if;
9999 Formal_Type := Entity (Ptype);
10001 if Is_Incomplete_Type (Formal_Type)
10002 or else
10003 (Is_Class_Wide_Type (Formal_Type)
10004 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10005 then
10006 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10007 -- primitive operations, as long as their completion is
10008 -- in the same declarative part. If in the private part
10009 -- this means that the type cannot be a Taft-amendment type.
10010 -- Check is done on package exit. For access to subprograms,
10011 -- the use is legal for Taft-amendment types.
10013 -- Ada 2012: tagged incomplete types are allowed as generic
10014 -- formal types. They do not introduce dependencies and the
10015 -- corresponding generic subprogram does not have a delayed
10016 -- freeze, because it does not need a freeze node. However,
10017 -- it is still the case that untagged incomplete types cannot
10018 -- be Taft-amendment types and must be completed in private
10019 -- part, so the subprogram must appear in the list of private
10020 -- dependents of the type.
10022 if Is_Tagged_Type (Formal_Type)
10023 or else (Ada_Version >= Ada_2012
10024 and then not From_Limited_With (Formal_Type)
10025 and then not Is_Generic_Type (Formal_Type))
10026 then
10027 if Ekind (Scope (Current_Scope)) = E_Package
10028 and then not Is_Generic_Type (Formal_Type)
10029 and then not Is_Class_Wide_Type (Formal_Type)
10030 then
10031 if not Nkind_In
10032 (Parent (T), N_Access_Function_Definition,
10033 N_Access_Procedure_Definition)
10034 then
10035 Append_Elmt
10036 (Current_Scope,
10037 To => Private_Dependents (Base_Type (Formal_Type)));
10039 -- Freezing is delayed to ensure that Register_Prim
10040 -- will get called for this operation, which is needed
10041 -- in cases where static dispatch tables aren't built.
10042 -- (Note that the same is done for controlling access
10043 -- parameter cases in function Access_Definition.)
10045 if not Is_Thunk (Current_Scope) then
10046 Set_Has_Delayed_Freeze (Current_Scope);
10047 end if;
10048 end if;
10049 end if;
10051 -- Special handling of Value_Type for CIL case
10053 elsif Is_Value_Type (Formal_Type) then
10054 null;
10056 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10057 N_Access_Procedure_Definition)
10058 then
10059 -- AI05-0151: Tagged incomplete types are allowed in all
10060 -- formal parts. Untagged incomplete types are not allowed
10061 -- in bodies. Limited views of either kind are not allowed
10062 -- if there is no place at which the non-limited view can
10063 -- become available.
10065 -- Incomplete formal untagged types are not allowed in
10066 -- subprogram bodies (but are legal in their declarations).
10068 if Is_Generic_Type (Formal_Type)
10069 and then not Is_Tagged_Type (Formal_Type)
10070 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
10071 then
10072 Error_Msg_N
10073 ("invalid use of formal incomplete type", Param_Spec);
10075 elsif Ada_Version >= Ada_2012 then
10076 if Is_Tagged_Type (Formal_Type)
10077 and then (not From_Limited_With (Formal_Type)
10078 or else not In_Package_Body)
10079 then
10080 null;
10082 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10083 N_Accept_Alternative,
10084 N_Entry_Body,
10085 N_Subprogram_Body)
10086 then
10087 Error_Msg_NE
10088 ("invalid use of untagged incomplete type&",
10089 Ptype, Formal_Type);
10090 end if;
10092 else
10093 Error_Msg_NE
10094 ("invalid use of incomplete type&",
10095 Param_Spec, Formal_Type);
10097 -- Further checks on the legality of incomplete types
10098 -- in formal parts are delayed until the freeze point
10099 -- of the enclosing subprogram or access to subprogram.
10100 end if;
10101 end if;
10103 elsif Ekind (Formal_Type) = E_Void then
10104 Error_Msg_NE
10105 ("premature use of&",
10106 Parameter_Type (Param_Spec), Formal_Type);
10107 end if;
10109 -- Ada 2012 (AI-142): Handle aliased parameters
10111 if Ada_Version >= Ada_2012
10112 and then Aliased_Present (Param_Spec)
10113 then
10114 Set_Is_Aliased (Formal);
10115 end if;
10117 -- Ada 2005 (AI-231): Create and decorate an internal subtype
10118 -- declaration corresponding to the null-excluding type of the
10119 -- formal in the enclosing scope. Finally, replace the parameter
10120 -- type of the formal with the internal subtype.
10122 if Ada_Version >= Ada_2005
10123 and then Null_Exclusion_Present (Param_Spec)
10124 then
10125 if not Is_Access_Type (Formal_Type) then
10126 Error_Msg_N
10127 ("`NOT NULL` allowed only for an access type", Param_Spec);
10129 else
10130 if Can_Never_Be_Null (Formal_Type)
10131 and then Comes_From_Source (Related_Nod)
10132 then
10133 Error_Msg_NE
10134 ("`NOT NULL` not allowed (& already excludes null)",
10135 Param_Spec, Formal_Type);
10136 end if;
10138 Formal_Type :=
10139 Create_Null_Excluding_Itype
10140 (T => Formal_Type,
10141 Related_Nod => Related_Nod,
10142 Scope_Id => Scope (Current_Scope));
10144 -- If the designated type of the itype is an itype that is
10145 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10146 -- on the access subtype, to prevent order-of-elaboration
10147 -- issues in the backend.
10149 -- Example:
10150 -- type T is access procedure;
10151 -- procedure Op (O : not null T);
10153 if Is_Itype (Directly_Designated_Type (Formal_Type))
10154 and then
10155 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10156 then
10157 Set_Has_Delayed_Freeze (Formal_Type);
10158 end if;
10159 end if;
10160 end if;
10162 -- An access formal type
10164 else
10165 Formal_Type :=
10166 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
10168 -- No need to continue if we already notified errors
10170 if not Present (Formal_Type) then
10171 return;
10172 end if;
10174 -- Ada 2005 (AI-254)
10176 declare
10177 AD : constant Node_Id :=
10178 Access_To_Subprogram_Definition
10179 (Parameter_Type (Param_Spec));
10180 begin
10181 if Present (AD) and then Protected_Present (AD) then
10182 Formal_Type :=
10183 Replace_Anonymous_Access_To_Protected_Subprogram
10184 (Param_Spec);
10185 end if;
10186 end;
10187 end if;
10189 Set_Etype (Formal, Formal_Type);
10191 -- Deal with default expression if present
10193 Default := Expression (Param_Spec);
10195 if Present (Default) then
10196 Check_SPARK_05_Restriction
10197 ("default expression is not allowed", Default);
10199 if Out_Present (Param_Spec) then
10200 Error_Msg_N
10201 ("default initialization only allowed for IN parameters",
10202 Param_Spec);
10203 end if;
10205 -- Do the special preanalysis of the expression (see section on
10206 -- "Handling of Default Expressions" in the spec of package Sem).
10208 Preanalyze_Spec_Expression (Default, Formal_Type);
10210 -- An access to constant cannot be the default for
10211 -- an access parameter that is an access to variable.
10213 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10214 and then not Is_Access_Constant (Formal_Type)
10215 and then Is_Access_Type (Etype (Default))
10216 and then Is_Access_Constant (Etype (Default))
10217 then
10218 Error_Msg_N
10219 ("formal that is access to variable cannot be initialized "
10220 & "with an access-to-constant expression", Default);
10221 end if;
10223 -- Check that the designated type of an access parameter's default
10224 -- is not a class-wide type unless the parameter's designated type
10225 -- is also class-wide.
10227 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10228 and then not Designates_From_Limited_With (Formal_Type)
10229 and then Is_Class_Wide_Default (Default)
10230 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10231 then
10232 Error_Msg_N
10233 ("access to class-wide expression not allowed here", Default);
10234 end if;
10236 -- Check incorrect use of dynamically tagged expressions
10238 if Is_Tagged_Type (Formal_Type) then
10239 Check_Dynamically_Tagged_Expression
10240 (Expr => Default,
10241 Typ => Formal_Type,
10242 Related_Nod => Default);
10243 end if;
10244 end if;
10246 -- Ada 2005 (AI-231): Static checks
10248 if Ada_Version >= Ada_2005
10249 and then Is_Access_Type (Etype (Formal))
10250 and then Can_Never_Be_Null (Etype (Formal))
10251 then
10252 Null_Exclusion_Static_Checks (Param_Spec);
10253 end if;
10255 -- The following checks are relevant when SPARK_Mode is on as these
10256 -- are not standard Ada legality rules.
10258 if SPARK_Mode = On then
10259 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
10261 -- A function cannot have a parameter of mode IN OUT or OUT
10262 -- (SPARK RM 6.1).
10264 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
10265 Error_Msg_N
10266 ("function cannot have parameter of mode `OUT` or "
10267 & "`IN OUT`", Formal);
10269 -- A function cannot have an effectively volatile formal
10270 -- parameter (SPARK RM 7.1.3(10)).
10272 elsif Is_Effectively_Volatile (Formal) then
10273 Error_Msg_N
10274 ("function cannot have a volatile formal parameter",
10275 Formal);
10276 end if;
10278 -- A procedure cannot have an effectively volatile formal
10279 -- parameter of mode IN because it behaves as a constant
10280 -- (SPARK RM 7.1.3(6)).
10282 elsif Ekind (Scope (Formal)) = E_Procedure
10283 and then Ekind (Formal) = E_In_Parameter
10284 and then Is_Effectively_Volatile (Formal)
10285 then
10286 Error_Msg_N
10287 ("formal parameter of mode `IN` cannot be volatile", Formal);
10288 end if;
10289 end if;
10291 <<Continue>>
10292 Next (Param_Spec);
10293 end loop;
10295 -- If this is the formal part of a function specification, analyze the
10296 -- subtype mark in the context where the formals are visible but not
10297 -- yet usable, and may hide outer homographs.
10299 if Nkind (Related_Nod) = N_Function_Specification then
10300 Analyze_Return_Type (Related_Nod);
10301 end if;
10303 -- Now set the kind (mode) of each formal
10305 Param_Spec := First (T);
10306 while Present (Param_Spec) loop
10307 Formal := Defining_Identifier (Param_Spec);
10308 Set_Formal_Mode (Formal);
10310 if Ekind (Formal) = E_In_Parameter then
10311 Set_Default_Value (Formal, Expression (Param_Spec));
10313 if Present (Expression (Param_Spec)) then
10314 Default := Expression (Param_Spec);
10316 if Is_Scalar_Type (Etype (Default)) then
10317 if Nkind (Parameter_Type (Param_Spec)) /=
10318 N_Access_Definition
10319 then
10320 Formal_Type := Entity (Parameter_Type (Param_Spec));
10321 else
10322 Formal_Type :=
10323 Access_Definition
10324 (Related_Nod, Parameter_Type (Param_Spec));
10325 end if;
10327 Apply_Scalar_Range_Check (Default, Formal_Type);
10328 end if;
10329 end if;
10331 elsif Ekind (Formal) = E_Out_Parameter then
10332 Num_Out_Params := Num_Out_Params + 1;
10334 if Num_Out_Params = 1 then
10335 First_Out_Param := Formal;
10336 end if;
10338 elsif Ekind (Formal) = E_In_Out_Parameter then
10339 Num_Out_Params := Num_Out_Params + 1;
10340 end if;
10342 -- Skip remaining processing if formal type was in error
10344 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10345 goto Next_Parameter;
10346 end if;
10348 -- Force call by reference if aliased
10350 if Is_Aliased (Formal) then
10351 Set_Mechanism (Formal, By_Reference);
10353 -- Warn if user asked this to be passed by copy
10355 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10356 Error_Msg_N
10357 ("cannot pass aliased parameter & by copy??", Formal);
10358 end if;
10360 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10362 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10363 Set_Mechanism (Formal, By_Copy);
10365 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10366 Set_Mechanism (Formal, By_Reference);
10367 end if;
10369 <<Next_Parameter>>
10370 Next (Param_Spec);
10371 end loop;
10373 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10374 Set_Is_Only_Out_Parameter (First_Out_Param);
10375 end if;
10376 end Process_Formals;
10378 ----------------------------
10379 -- Reference_Body_Formals --
10380 ----------------------------
10382 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10383 Fs : Entity_Id;
10384 Fb : Entity_Id;
10386 begin
10387 if Error_Posted (Spec) then
10388 return;
10389 end if;
10391 -- Iterate over both lists. They may be of different lengths if the two
10392 -- specs are not conformant.
10394 Fs := First_Formal (Spec);
10395 Fb := First_Formal (Bod);
10396 while Present (Fs) and then Present (Fb) loop
10397 Generate_Reference (Fs, Fb, 'b');
10399 if Style_Check then
10400 Style.Check_Identifier (Fb, Fs);
10401 end if;
10403 Set_Spec_Entity (Fb, Fs);
10404 Set_Referenced (Fs, False);
10405 Next_Formal (Fs);
10406 Next_Formal (Fb);
10407 end loop;
10408 end Reference_Body_Formals;
10410 -------------------------
10411 -- Set_Actual_Subtypes --
10412 -------------------------
10414 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10415 Decl : Node_Id;
10416 Formal : Entity_Id;
10417 T : Entity_Id;
10418 First_Stmt : Node_Id := Empty;
10419 AS_Needed : Boolean;
10421 begin
10422 -- If this is an empty initialization procedure, no need to create
10423 -- actual subtypes (small optimization).
10425 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
10426 return;
10427 end if;
10429 Formal := First_Formal (Subp);
10430 while Present (Formal) loop
10431 T := Etype (Formal);
10433 -- We never need an actual subtype for a constrained formal
10435 if Is_Constrained (T) then
10436 AS_Needed := False;
10438 -- If we have unknown discriminants, then we do not need an actual
10439 -- subtype, or more accurately we cannot figure it out. Note that
10440 -- all class-wide types have unknown discriminants.
10442 elsif Has_Unknown_Discriminants (T) then
10443 AS_Needed := False;
10445 -- At this stage we have an unconstrained type that may need an
10446 -- actual subtype. For sure the actual subtype is needed if we have
10447 -- an unconstrained array type.
10449 elsif Is_Array_Type (T) then
10450 AS_Needed := True;
10452 -- The only other case needing an actual subtype is an unconstrained
10453 -- record type which is an IN parameter (we cannot generate actual
10454 -- subtypes for the OUT or IN OUT case, since an assignment can
10455 -- change the discriminant values. However we exclude the case of
10456 -- initialization procedures, since discriminants are handled very
10457 -- specially in this context, see the section entitled "Handling of
10458 -- Discriminants" in Einfo.
10460 -- We also exclude the case of Discrim_SO_Functions (functions used
10461 -- in front end layout mode for size/offset values), since in such
10462 -- functions only discriminants are referenced, and not only are such
10463 -- subtypes not needed, but they cannot always be generated, because
10464 -- of order of elaboration issues.
10466 elsif Is_Record_Type (T)
10467 and then Ekind (Formal) = E_In_Parameter
10468 and then Chars (Formal) /= Name_uInit
10469 and then not Is_Unchecked_Union (T)
10470 and then not Is_Discrim_SO_Function (Subp)
10471 then
10472 AS_Needed := True;
10474 -- All other cases do not need an actual subtype
10476 else
10477 AS_Needed := False;
10478 end if;
10480 -- Generate actual subtypes for unconstrained arrays and
10481 -- unconstrained discriminated records.
10483 if AS_Needed then
10484 if Nkind (N) = N_Accept_Statement then
10486 -- If expansion is active, the formal is replaced by a local
10487 -- variable that renames the corresponding entry of the
10488 -- parameter block, and it is this local variable that may
10489 -- require an actual subtype.
10491 if Expander_Active then
10492 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10493 else
10494 Decl := Build_Actual_Subtype (T, Formal);
10495 end if;
10497 if Present (Handled_Statement_Sequence (N)) then
10498 First_Stmt :=
10499 First (Statements (Handled_Statement_Sequence (N)));
10500 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10501 Mark_Rewrite_Insertion (Decl);
10502 else
10503 -- If the accept statement has no body, there will be no
10504 -- reference to the actuals, so no need to compute actual
10505 -- subtypes.
10507 return;
10508 end if;
10510 else
10511 Decl := Build_Actual_Subtype (T, Formal);
10512 Prepend (Decl, Declarations (N));
10513 Mark_Rewrite_Insertion (Decl);
10514 end if;
10516 -- The declaration uses the bounds of an existing object, and
10517 -- therefore needs no constraint checks.
10519 Analyze (Decl, Suppress => All_Checks);
10521 -- We need to freeze manually the generated type when it is
10522 -- inserted anywhere else than in a declarative part.
10524 if Present (First_Stmt) then
10525 Insert_List_Before_And_Analyze (First_Stmt,
10526 Freeze_Entity (Defining_Identifier (Decl), N));
10528 -- Ditto if the type has a dynamic predicate, because the
10529 -- generated function will mention the actual subtype.
10531 elsif Has_Dynamic_Predicate_Aspect (T) then
10532 Insert_List_Before_And_Analyze (Decl,
10533 Freeze_Entity (Defining_Identifier (Decl), N));
10534 end if;
10536 if Nkind (N) = N_Accept_Statement
10537 and then Expander_Active
10538 then
10539 Set_Actual_Subtype (Renamed_Object (Formal),
10540 Defining_Identifier (Decl));
10541 else
10542 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10543 end if;
10544 end if;
10546 Next_Formal (Formal);
10547 end loop;
10548 end Set_Actual_Subtypes;
10550 ---------------------
10551 -- Set_Formal_Mode --
10552 ---------------------
10554 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10555 Spec : constant Node_Id := Parent (Formal_Id);
10557 begin
10558 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10559 -- since we ensure that corresponding actuals are always valid at the
10560 -- point of the call.
10562 if Out_Present (Spec) then
10563 if Ekind_In (Scope (Formal_Id), E_Function, E_Generic_Function) then
10565 -- [IN] OUT parameters allowed for functions in Ada 2012
10567 if Ada_Version >= Ada_2012 then
10569 -- Even in Ada 2012 operators can only have IN parameters
10571 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
10572 Error_Msg_N ("operators can only have IN parameters", Spec);
10573 end if;
10575 if In_Present (Spec) then
10576 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10577 else
10578 Set_Ekind (Formal_Id, E_Out_Parameter);
10579 end if;
10581 Set_Has_Out_Or_In_Out_Parameter (Scope (Formal_Id), True);
10583 -- But not in earlier versions of Ada
10585 else
10586 Error_Msg_N ("functions can only have IN parameters", Spec);
10587 Set_Ekind (Formal_Id, E_In_Parameter);
10588 end if;
10590 elsif In_Present (Spec) then
10591 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10593 else
10594 Set_Ekind (Formal_Id, E_Out_Parameter);
10595 Set_Never_Set_In_Source (Formal_Id, True);
10596 Set_Is_True_Constant (Formal_Id, False);
10597 Set_Current_Value (Formal_Id, Empty);
10598 end if;
10600 else
10601 Set_Ekind (Formal_Id, E_In_Parameter);
10602 end if;
10604 -- Set Is_Known_Non_Null for access parameters since the language
10605 -- guarantees that access parameters are always non-null. We also set
10606 -- Can_Never_Be_Null, since there is no way to change the value.
10608 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10610 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10611 -- null; In Ada 2005, only if then null_exclusion is explicit.
10613 if Ada_Version < Ada_2005
10614 or else Can_Never_Be_Null (Etype (Formal_Id))
10615 then
10616 Set_Is_Known_Non_Null (Formal_Id);
10617 Set_Can_Never_Be_Null (Formal_Id);
10618 end if;
10620 -- Ada 2005 (AI-231): Null-exclusion access subtype
10622 elsif Is_Access_Type (Etype (Formal_Id))
10623 and then Can_Never_Be_Null (Etype (Formal_Id))
10624 then
10625 Set_Is_Known_Non_Null (Formal_Id);
10627 -- We can also set Can_Never_Be_Null (thus preventing some junk
10628 -- access checks) for the case of an IN parameter, which cannot
10629 -- be changed, or for an IN OUT parameter, which can be changed but
10630 -- not to a null value. But for an OUT parameter, the initial value
10631 -- passed in can be null, so we can't set this flag in that case.
10633 if Ekind (Formal_Id) /= E_Out_Parameter then
10634 Set_Can_Never_Be_Null (Formal_Id);
10635 end if;
10636 end if;
10638 Set_Mechanism (Formal_Id, Default_Mechanism);
10639 Set_Formal_Validity (Formal_Id);
10640 end Set_Formal_Mode;
10642 -------------------------
10643 -- Set_Formal_Validity --
10644 -------------------------
10646 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10647 begin
10648 -- If no validity checking, then we cannot assume anything about the
10649 -- validity of parameters, since we do not know there is any checking
10650 -- of the validity on the call side.
10652 if not Validity_Checks_On then
10653 return;
10655 -- If validity checking for parameters is enabled, this means we are
10656 -- not supposed to make any assumptions about argument values.
10658 elsif Validity_Check_Parameters then
10659 return;
10661 -- If we are checking in parameters, we will assume that the caller is
10662 -- also checking parameters, so we can assume the parameter is valid.
10664 elsif Ekind (Formal_Id) = E_In_Parameter
10665 and then Validity_Check_In_Params
10666 then
10667 Set_Is_Known_Valid (Formal_Id, True);
10669 -- Similar treatment for IN OUT parameters
10671 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10672 and then Validity_Check_In_Out_Params
10673 then
10674 Set_Is_Known_Valid (Formal_Id, True);
10675 end if;
10676 end Set_Formal_Validity;
10678 ------------------------
10679 -- Subtype_Conformant --
10680 ------------------------
10682 function Subtype_Conformant
10683 (New_Id : Entity_Id;
10684 Old_Id : Entity_Id;
10685 Skip_Controlling_Formals : Boolean := False) return Boolean
10687 Result : Boolean;
10688 begin
10689 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10690 Skip_Controlling_Formals => Skip_Controlling_Formals);
10691 return Result;
10692 end Subtype_Conformant;
10694 ---------------------
10695 -- Type_Conformant --
10696 ---------------------
10698 function Type_Conformant
10699 (New_Id : Entity_Id;
10700 Old_Id : Entity_Id;
10701 Skip_Controlling_Formals : Boolean := False) return Boolean
10703 Result : Boolean;
10704 begin
10705 May_Hide_Profile := False;
10706 Check_Conformance
10707 (New_Id, Old_Id, Type_Conformant, False, Result,
10708 Skip_Controlling_Formals => Skip_Controlling_Formals);
10709 return Result;
10710 end Type_Conformant;
10712 -------------------------------
10713 -- Valid_Operator_Definition --
10714 -------------------------------
10716 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10717 N : Integer := 0;
10718 F : Entity_Id;
10719 Id : constant Name_Id := Chars (Designator);
10720 N_OK : Boolean;
10722 begin
10723 F := First_Formal (Designator);
10724 while Present (F) loop
10725 N := N + 1;
10727 if Present (Default_Value (F)) then
10728 Error_Msg_N
10729 ("default values not allowed for operator parameters",
10730 Parent (F));
10732 -- For function instantiations that are operators, we must check
10733 -- separately that the corresponding generic only has in-parameters.
10734 -- For subprogram declarations this is done in Set_Formal_Mode. Such
10735 -- an error could not arise in earlier versions of the language.
10737 elsif Ekind (F) /= E_In_Parameter then
10738 Error_Msg_N ("operators can only have IN parameters", F);
10739 end if;
10741 Next_Formal (F);
10742 end loop;
10744 -- Verify that user-defined operators have proper number of arguments
10745 -- First case of operators which can only be unary
10747 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
10748 N_OK := (N = 1);
10750 -- Case of operators which can be unary or binary
10752 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
10753 N_OK := (N in 1 .. 2);
10755 -- All other operators can only be binary
10757 else
10758 N_OK := (N = 2);
10759 end if;
10761 if not N_OK then
10762 Error_Msg_N
10763 ("incorrect number of arguments for operator", Designator);
10764 end if;
10766 if Id = Name_Op_Ne
10767 and then Base_Type (Etype (Designator)) = Standard_Boolean
10768 and then not Is_Intrinsic_Subprogram (Designator)
10769 then
10770 Error_Msg_N
10771 ("explicit definition of inequality not allowed", Designator);
10772 end if;
10773 end Valid_Operator_Definition;
10775 end Sem_Ch6;